OpenTTD Source  20241108-master-g80f628063a
fios.cpp File Reference

This file contains functions for building file lists for the save/load dialogs. More...

#include "stdafx.h"
#include "3rdparty/md5/md5.h"
#include "fileio_func.h"
#include "fios.h"
#include "network/network_content.h"
#include "screenshot.h"
#include "string_func.h"
#include "strings_func.h"
#include "tar_type.h"
#include <sys/stat.h>
#include <charconv>
#include <filesystem>
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

class  FiosFileScanner
 Scanner to scan for a particular type of FIOS file. More...
 
struct  ScenarioIdentifier
 Basic data to distinguish a scenario. More...
 
class  ScenarioScanner
 Scanner to find the unique IDs of scenarios. More...
 

Typedefs

typedef std::tuple< FiosType, std::string > FiosGetTypeAndNameProc(SaveLoadOperation fop, const std::string &filename, const std::string_view ext)
 

Functions

bool FiosIsRoot (const std::string &path)
 
bool FiosIsHiddenFile (const std::filesystem::path &path)
 
void FiosGetDrives (FileList &file_list)
 
std::string GetOldSaveGameName (const std::string &file)
 
std::string FiosGetCurrentPath ()
 Get the current path/working directory.
 
bool FiosBrowseTo (const FiosItem *item)
 Browse to a new path based on the passed item, starting at #_fios_path. More...
 
static std::string FiosMakeFilename (const std::string *path, const char *name, const char *ext)
 Construct a filename from its components in destination buffer buf. More...
 
std::string FiosMakeSavegameName (const char *name)
 Make a save game or scenario filename from a name. More...
 
std::string FiosMakeHeightmapName (const char *name)
 Construct a filename for a height map. More...
 
bool FiosDelete (const char *name)
 Delete a file. More...
 
static void FiosGetFileList (SaveLoadOperation fop, bool show_dirs, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
 Fill the list of the files in a directory, according to some arbitrary rule. More...
 
static std::string GetFileTitle (const std::string &file, Subdirectory subdir)
 Get the title of a file, which (if exists) is stored in a file named the same as the data file but with '.title' added to it. More...
 
std::tuple< FiosType, std::string > FiosGetSavegameListCallback (SaveLoadOperation fop, const std::string &file, const std::string_view ext)
 Callback for FiosGetFileList. More...
 
void FiosGetSavegameList (SaveLoadOperation fop, bool show_dirs, FileList &file_list)
 Get a list of savegames. More...
 
std::tuple< FiosType, std::string > FiosGetScenarioListCallback (SaveLoadOperation fop, const std::string &file, const std::string_view ext)
 Callback for FiosGetFileList. More...
 
void FiosGetScenarioList (SaveLoadOperation fop, bool show_dirs, FileList &file_list)
 Get a list of scenarios. More...
 
std::tuple< FiosType, std::string > FiosGetHeightmapListCallback (SaveLoadOperation, const std::string &file, const std::string_view ext)
 
void FiosGetHeightmapList (SaveLoadOperation fop, bool show_dirs, FileList &file_list)
 Get a list of heightmaps. More...
 
static std::tuple< FiosType, std::string > FiosGetTownDataListCallback (SaveLoadOperation fop, const std::string &file, const std::string_view ext)
 Callback for FiosGetTownDataList. More...
 
void FiosGetTownDataList (SaveLoadOperation fop, bool show_dirs, FileList &file_list)
 Get a list of town data files. More...
 
const char * FiosGetScreenshotDir ()
 Get the directory for screenshots. More...
 
const char * FindScenario (const ContentInfo *ci, bool md5sum)
 Find a given scenario based on its unique ID. More...
 
bool HasScenario (const ContentInfo *ci, bool md5sum)
 Check whether we've got a given scenario based on its unique ID. More...
 
void ScanScenarios ()
 Force a (re)scan of the scenarios.
 

Variables

static std::string * _fios_path = nullptr
 
SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING
 
static ScenarioScanner _scanner
 Scanner for scenarios.
 

Detailed Description

This file contains functions for building file lists for the save/load dialogs.

Definition in file fios.cpp.

Function Documentation

◆ FindScenario()

const char* FindScenario ( const ContentInfo ci,
bool  md5sum 
)

Find a given scenario based on its unique ID.

Parameters
ciThe content info to compare it to.
md5sumWhether to look at the md5sum or the id.
Returns
The filename of the file, else nullptr.

Definition at line 690 of file fios.cpp.

References _scanner, ContentInfo::md5sum, ScenarioScanner::Scan(), and ContentInfo::unique_id.

Referenced by HasScenario().

◆ FiosBrowseTo()

bool FiosBrowseTo ( const FiosItem item)

Browse to a new path based on the passed item, starting at #_fios_path.

Parameters
*itemItem telling us what to do.
Returns
true when the path got changed.

Definition at line 143 of file fios.cpp.

◆ FiosDelete()

bool FiosDelete ( const char *  name)

Delete a file.

Parameters
nameFilename to delete.
Returns
Whether the file deletion was successful.

Definition at line 248 of file fios.cpp.

References FioRemove(), and FiosMakeSavegameName().

Referenced by SaveLoadWindow::OnTimeout().

◆ FiosGetFileList()

static void FiosGetFileList ( SaveLoadOperation  fop,
bool  show_dirs,
FiosGetTypeAndNameProc *  callback_proc,
Subdirectory  subdir,
FileList file_list 
)
static

Fill the list of the files in a directory, according to some arbitrary rule.

Parameters
fopPurpose of collecting the list.
show_dirsWhether to list directories.
callback_procThe function that is called where you need to do the filtering.
subdirThe directory from where to start (global) searching.
file_listDestination of the found files.

Definition at line 327 of file fios.cpp.

◆ FiosGetHeightmapList()

void FiosGetHeightmapList ( SaveLoadOperation  fop,
bool  show_dirs,
FileList file_list 
)

Get a list of heightmaps.

Parameters
fopPurpose of collecting the list.
show_dirsWhether to show directories.
file_listDestination of the found files.

Definition at line 549 of file fios.cpp.

Referenced by FileList::BuildFileList().

◆ FiosGetSavegameList()

void FiosGetSavegameList ( SaveLoadOperation  fop,
bool  show_dirs,
FileList file_list 
)

Get a list of savegames.

Parameters
fopPurpose of collecting the list.
show_dirsWhether to show directories.
file_listDestination of the found files.
See also
FiosGetFileList

Definition at line 442 of file fios.cpp.

Referenced by FileList::BuildFileList().

◆ FiosGetSavegameListCallback()

std::tuple<FiosType, std::string> FiosGetSavegameListCallback ( SaveLoadOperation  fop,
const std::string &  file,
const std::string_view  ext 
)

Callback for FiosGetFileList.

It tells if a file is a savegame or not.

Parameters
fopPurpose of collecting the list.
fileName of the file to check.
extA pointer to the extension identifier inside file
Returns
a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame, and the title of the file (if any).
See also
FiosGetFileList
FiosGetSavegameList

Definition at line 413 of file fios.cpp.

References StrEqualsIgnoreCase().

◆ FiosGetScenarioList()

void FiosGetScenarioList ( SaveLoadOperation  fop,
bool  show_dirs,
FileList file_list 
)

Get a list of scenarios.

Parameters
fopPurpose of collecting the list.
show_dirsWhether to show directories.
file_listDestination of the found files.
See also
FiosGetFileList

Definition at line 489 of file fios.cpp.

Referenced by FileList::BuildFileList().

◆ FiosGetScenarioListCallback()

std::tuple<FiosType, std::string> FiosGetScenarioListCallback ( SaveLoadOperation  fop,
const std::string &  file,
const std::string_view  ext 
)

Callback for FiosGetFileList.

It tells if a file is a scenario or not.

Parameters
fopPurpose of collecting the list.
fileName of the file to check.
extA pointer to the extension identifier inside file
Returns
a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario and the title of the file (if any).
See also
FiosGetFileList
FiosGetScenarioList

Definition at line 462 of file fios.cpp.

References StrEqualsIgnoreCase().

◆ FiosGetScreenshotDir()

const char* FiosGetScreenshotDir ( )

Get the directory for screenshots.

Returns
path to screenshots

Definition at line 602 of file fios.cpp.

Referenced by NewGRFProfiler::GetOutputFilename().

◆ FiosGetTownDataList()

void FiosGetTownDataList ( SaveLoadOperation  fop,
bool  show_dirs,
FileList file_list 
)

Get a list of town data files.

Parameters
fopPurpose of collecting the list.
show_dirsWhether to show directories.
file_listDestination of the found files.

Definition at line 585 of file fios.cpp.

Referenced by FileList::BuildFileList().

◆ FiosGetTownDataListCallback()

static std::tuple<FiosType, std::string> FiosGetTownDataListCallback ( SaveLoadOperation  fop,
const std::string &  file,
const std::string_view  ext 
)
static

Callback for FiosGetTownDataList.

Parameters
fopPurpose of collecting the list.
fileName of the file to check.
Returns
a FIOS_TYPE_JSON type of the found file, FIOS_TYPE_INVALID if not a valid JSON file, and the title of the file (if any).

Definition at line 568 of file fios.cpp.

References SLO_LOAD, and StrEqualsIgnoreCase().

◆ FiosMakeFilename()

static std::string FiosMakeFilename ( const std::string *  path,
const char *  name,
const char *  ext 
)
static

Construct a filename from its components in destination buffer buf.

Parameters
pathDirectory path, may be nullptr.
nameFilename.
extFilename extension (use "" for no extension).
Returns
The completed filename.

Definition at line 201 of file fios.cpp.

Referenced by FiosMakeHeightmapName().

◆ FiosMakeHeightmapName()

std::string FiosMakeHeightmapName ( const char *  name)

Construct a filename for a height map.

Parameters
nameFilename.
Returns
The completed filename.

Definition at line 235 of file fios.cpp.

References FiosMakeFilename(), and GetCurrentScreenshotExtension().

Referenced by SaveLoadWindow::OnTimeout().

◆ FiosMakeSavegameName()

std::string FiosMakeSavegameName ( const char *  name)

Make a save game or scenario filename from a name.

Parameters
nameName of the file.
Returns
The completed filename.

Definition at line 223 of file fios.cpp.

Referenced by FiosDelete(), and SaveLoadWindow::OnTimeout().

◆ GetFileTitle()

static std::string GetFileTitle ( const std::string &  file,
Subdirectory  subdir 
)
static

Get the title of a file, which (if exists) is stored in a file named the same as the data file but with '.title' added to it.

Parameters
filefilename to get the title for
subdirthe sub directory to search in
Returns
The file title.

Definition at line 392 of file fios.cpp.

References FioFOpenFile(), lengthof, and StrMakeValid().

◆ HasScenario()

bool HasScenario ( const ContentInfo ci,
bool  md5sum 
)

Check whether we've got a given scenario based on its unique ID.

Parameters
ciThe content info to compare it to.
md5sumWhether to look at the md5sum or the id.
Returns
True iff we've got the scenario.

Definition at line 710 of file fios.cpp.

References FindScenario().

Referenced by ClientNetworkContentSocketHandler::Receive_SERVER_INFO().