OpenTTD Source 20260421-master-gc2fbc6fdeb
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 "core/string_consumer.hpp"
#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, std::string_view filename, 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 (std::string_view file)
bool FiosItemNameSorter (const FiosItem &a, const FiosItem &b)
 Sort files by their name.
bool FiosItemModificationDateSorter (const FiosItem &a, const FiosItem &b)
 Sort files by their modification date, and name when they are equal.
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.
static std::string FiosMakeFilename (const std::string *path, std::string_view name, std::string_view ext)
 Construct a filename from its components in destination buffer buf.
std::string FiosMakeSavegameName (std::string_view name)
 Make a save game or scenario filename from a name.
std::string FiosMakeHeightmapName (std::string_view name)
 Construct a filename for a height map.
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.
static std::string GetFileTitle (std::string_view 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.
std::tuple< FiosType, std::string > FiosGetSavegameListCallback (SaveLoadOperation fop, std::string_view file, std::string_view ext)
 Callback for FiosGetFileList.
void FiosGetSavegameList (SaveLoadOperation fop, bool show_dirs, FileList &file_list)
 Get a list of savegames.
std::tuple< FiosType, std::string > FiosGetScenarioListCallback (SaveLoadOperation fop, std::string_view file, std::string_view ext)
 Callback for FiosGetFileList.
void FiosGetScenarioList (SaveLoadOperation fop, bool show_dirs, FileList &file_list)
 Get a list of scenarios.
std::tuple< FiosType, std::string > FiosGetHeightmapListCallback (SaveLoadOperation, std::string_view file, std::string_view ext)
void FiosGetHeightmapList (SaveLoadOperation fop, bool show_dirs, FileList &file_list)
 Get a list of heightmaps.
static std::tuple< FiosType, std::string > FiosGetTownDataListCallback (SaveLoadOperation fop, std::string_view file, std::string_view ext)
 Callback for FiosGetTownDataList.
void FiosGetTownDataList (SaveLoadOperation fop, bool show_dirs, FileList &file_list)
 Get a list of town data files.
std::string_view FiosGetScreenshotDir ()
 Get the directory for screenshots.
std::optional< std::string_view > FindScenario (const ContentInfo &ci, bool md5sum)
 Find a given scenario based on its unique ID.
bool HasScenario (const ContentInfo &ci, bool md5sum)
 Check whether we've got a given scenario based on its unique ID.
void ScanScenarios ()
 Force a (re)scan of the scenarios.

Variables

static std::string * _fios_path = nullptr
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.

Typedef Documentation

◆ FiosGetTypeAndNameProc

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

Definition at line 224 of file fios.cpp.

Function Documentation

◆ FindScenario()

std::optional< std::string_view > 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 std::nullopt.

Definition at line 653 of file fios.cpp.

References _scanner, ContentInfo::md5sum, and ContentInfo::unique_id.

Referenced by ContentInfo::GetTextfile(), and 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 133 of file fios.cpp.

References FiosType::detailed, FiosDirect, FiosDirectory, FiosDrive, FiosParent, and Invalid.

Referenced by ConChangeDirectory(), and SaveLoadWindow::OnClick().

◆ FiosGetCurrentPath()

std::string FiosGetCurrentPath ( )

Get the current path/working directory.

Returns
The current path.

Definition at line 123 of file fios.cpp.

Referenced by ConPrintWorkingDirectory(), and SaveLoadWindow::DrawWidget().

◆ FiosGetDrives()

void FiosGetDrives ( FileList & file_list)
extern

Definition at line 65 of file unix.cpp.

◆ FiosGetFileList()

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 298 of file fios.cpp.

References FiosItemNameSorter(), FiosItemSorter(), FS2OTTD(), GetEncodedString(), None, OTTD2FS(), and FileScanner::Scan().

Referenced by FiosGetHeightmapList(), FiosGetSavegameList(), FiosGetScenarioList(), and FiosGetTownDataList().

◆ 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 516 of file fios.cpp.

References FiosGetFileList(), Heightmap, and None.

Referenced by FileList::BuildFileList().

◆ FiosGetHeightmapListCallback()

std::tuple< FiosType, std::string > FiosGetHeightmapListCallback ( SaveLoadOperation fop,
std::string_view file,
std::string_view ext )

Definition at line 470 of file fios.cpp.

◆ 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 409 of file fios.cpp.

References FiosGetFileList(), FiosGetSavegameListCallback(), None, and Save.

Referenced by FileList::BuildFileList().

◆ FiosGetSavegameListCallback()

std::tuple< FiosType, std::string > FiosGetSavegameListCallback ( SaveLoadOperation fop,
std::string_view file,
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 380 of file fios.cpp.

References GetFileTitle(), Load, Save, and StrEqualsIgnoreCase().

Referenced by FiosGetSavegameList(), and openttd_main().

◆ 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 456 of file fios.cpp.

References FiosGetFileList(), FiosGetScenarioListCallback(), Load, None, and Scenario.

Referenced by FileList::BuildFileList().

◆ FiosGetScenarioListCallback()

std::tuple< FiosType, std::string > FiosGetScenarioListCallback ( SaveLoadOperation fop,
std::string_view file,
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 429 of file fios.cpp.

References GetFileTitle(), Load, Scenario, and StrEqualsIgnoreCase().

Referenced by FiosGetScenarioList(), and openttd_main().

◆ FiosGetScreenshotDir()

std::string_view FiosGetScreenshotDir ( )

Get the directory for screenshots.

Returns
path to screenshots

Definition at line 570 of file fios.cpp.

References Screenshot.

Referenced by NewGRFProfiler::GetOutputFilename(), and MakeScreenshotName().

◆ 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 553 of file fios.cpp.

References FiosGetFileList(), FiosGetTownDataListCallback(), Heightmap, and None.

Referenced by FileList::BuildFileList().

◆ FiosGetTownDataListCallback()

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

Callback for FiosGetTownDataList.

Parameters
fopPurpose of collecting the list.
fileName of the file to check.
extThe file extension 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 536 of file fios.cpp.

References GetFileTitle(), Load, Save, and StrEqualsIgnoreCase().

Referenced by FiosGetTownDataList().

◆ FiosIsHiddenFile()

bool FiosIsHiddenFile ( const std::filesystem::path & path)
extern

Definition at line 84 of file unix.cpp.

◆ FiosIsRoot()

bool FiosIsRoot ( const std::string & path)
extern

Definition at line 60 of file unix.cpp.

◆ FiosItemModificationDateSorter()

bool FiosItemModificationDateSorter ( const FiosItem & a,
const FiosItem & b )

Sort files by their modification date, and name when they are equal.

Comparison helper for the sorter, comparing a to b.

Parameters
aThe first element.
bThe second element.
Returns
true if the first element is less than the second element.

Definition at line 45 of file fios.cpp.

References FiosItemNameSorter().

Referenced by FiosItemSorter(), and FiosNumberedSaveName::FiosNumberedSaveName().

◆ FiosItemNameSorter()

bool FiosItemNameSorter ( const FiosItem & a,
const FiosItem & b )

Sort files by their name.

Comparison helper for the sorter, comparing a to b.

Parameters
aThe first element.
bThe second element.
Returns
true if the first element is less than the second element.

Definition at line 39 of file fios.cpp.

References EncodedString::GetDecodedString(), and StrNaturalCompare().

Referenced by FiosGetFileList(), FiosItemModificationDateSorter(), and FiosItemSorter().

◆ FiosMakeFilename()

std::string FiosMakeFilename ( const std::string * path,
std::string_view name,
std::string_view 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 185 of file fios.cpp.

References StrEqualsIgnoreCase().

Referenced by FiosMakeHeightmapName(), and FiosMakeSavegameName().

◆ FiosMakeHeightmapName()

std::string FiosMakeHeightmapName ( std::string_view name)

Construct a filename for a height map.

Parameters
nameFilename.
Returns
The completed filename.

Definition at line 219 of file fios.cpp.

References FiosMakeFilename(), and GetCurrentScreenshotExtension().

Referenced by SaveLoadWindow::OnTimeout().

◆ FiosMakeSavegameName()

std::string FiosMakeSavegameName ( std::string_view name)

Make a save game or scenario filename from a name.

Parameters
nameName of the file.
Returns
The completed filename.

Definition at line 207 of file fios.cpp.

References FiosMakeFilename().

Referenced by SaveLoadWindow::OnTimeout().

◆ GetFileTitle()

std::string GetFileTitle ( std::string_view 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 358 of file fios.cpp.

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

Referenced by FiosGetSavegameListCallback(), FiosGetScenarioListCallback(), and FiosGetTownDataListCallback().

◆ GetOldSaveGameName()

std::string GetOldSaveGameName ( std::string_view file)
extern

Definition at line 287 of file oldloader.cpp.

◆ 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 673 of file fios.cpp.

References FindScenario().

Referenced by GetHasContentProcforContentType().

◆ ScanScenarios()

void ScanScenarios ( )

Force a (re)scan of the scenarios.

Definition at line 681 of file fios.cpp.

References _scanner.

Referenced by NetworkContentDownloadStatusWindow::Close().

Variable Documentation

◆ _fios_path

std::string* _fios_path = nullptr
static

Definition at line 28 of file fios.cpp.

◆ _scanner

ScenarioScanner _scanner
static

Scanner for scenarios.

Definition at line 645 of file fios.cpp.

Referenced by FindScenario(), and ScanScenarios().