14#include "3rdparty/md5/md5.h"
27#include "table/strings.h"
32static std::string *_fios_path =
nullptr;
33SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
36extern bool FiosIsRoot(
const std::string &path);
37extern bool FiosIsHiddenFile(
const std::filesystem::path &path);
38extern void FiosGetDrives(
FileList &file_list);
41extern std::string GetOldSaveGameName(std::string_view file);
52 if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
53 r = ClampTo<int32_t>(this->mtime - other.mtime);
57 if (r == 0)
return false;
58 return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
72 switch (abstract_filetype) {
105 for (
const auto &it : *
this) {
107 if (file == item->name)
return item;
114 if (number.has_value() && !consumer.AnyBytesLeft() &&
IsInsideMM(*number, 0, this->size()))
return &this->at(*number);
118 std::string long_file(file);
120 for (
const auto &it : *
this) {
122 if (long_file == item->name)
return item;
147 assert(_fios_path !=
nullptr);
148 *_fios_path = std::string{ item->name, 0, 1 } +
":" PATHSEP;
156 assert(_fios_path !=
nullptr);
157 auto s = _fios_path->find_last_of(PATHSEPCHAR);
158 if (s != std::string::npos && s != 0) {
159 _fios_path->erase(s);
162 s = _fios_path->find_last_of(PATHSEPCHAR);
163 if (s != std::string::npos) {
164 _fios_path->erase(s + 1);
170 assert(_fios_path !=
nullptr);
171 *_fios_path += item->name;
172 *_fios_path += PATHSEP;
176 assert(_fios_path !=
nullptr);
177 *_fios_path = item->name;
194static std::string
FiosMakeFilename(
const std::string *path, std::string_view name, std::string_view ext)
196 std::string_view base_path;
198 if (path !=
nullptr) {
201 if (!base_path.empty() && base_path.back() == PATHSEPCHAR) base_path.remove_suffix(1);
205 auto period = name.find_last_of(
'.');
206 if (period != std::string_view::npos &&
StrEqualsIgnoreCase(name.substr(period), ext)) ext =
"";
208 return fmt::format(
"{}{}{}{}", base_path, PATHSEP, name, ext);
218 std::string_view extension = (_game_mode == GM_EDITOR) ?
".scn" :
".sav";
233typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(
SaveLoadOperation fop, std::string_view filename, std::string_view ext);
253 bool AddFile(
const std::string &filename,
size_t basepath_length,
const std::string &tar_filename)
override;
263 auto sep = filename.rfind(
'.');
264 if (sep == std::string::npos)
return false;
265 std::string ext = filename.substr(sep);
268 if (type == FIOS_TYPE_INVALID)
return false;
271 if (filename == fios.name)
return false;
276 std::error_code error_code;
277 auto write_time = std::filesystem::last_write_time(
OTTD2FS(filename), error_code);
281 fios->mtime = std::chrono::duration_cast<std::chrono::milliseconds>(write_time.time_since_epoch()).count();
285 fios->name = filename;
289 auto ps = filename.rfind(PATHSEPCHAR);
309 size_t sort_start = 0;
313 assert(_fios_path !=
nullptr);
317 if (!FiosIsRoot(*_fios_path)) {
318 FiosItem &fios = file_list.emplace_back();
319 fios.type = FIOS_TYPE_PARENT;
323 sort_start = file_list.size();
327 std::error_code error_code;
328 for (
const auto &dir_entry : std::filesystem::directory_iterator(
OTTD2FS(*_fios_path), error_code)) {
329 if (!dir_entry.is_directory())
continue;
330 if (FiosIsHiddenFile(dir_entry) && dir_entry.path().filename() != PERSONAL_DIR)
continue;
332 FiosItem &fios = file_list.emplace_back();
333 fios.type = FIOS_TYPE_DIR;
335 fios.name =
FS2OTTD(dir_entry.path().filename().native());
340 SortingBits order = _savegame_sort_order;
341 _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
342 std::sort(file_list.begin() + sort_start, file_list.end());
343 _savegame_sort_order = order;
347 sort_start = file_list.size();
352 scanner.
Scan({}, *_fios_path,
false);
354 scanner.
Scan({}, subdir,
true,
true);
357 std::sort(file_list.begin() + sort_start, file_list.end());
360 FiosGetDrives(file_list);
372 std::string filename = fmt::format(
"{}.title", file);
374 if (!f.has_value())
return {};
377 size_t read = fread(title, 1,
lengthof(title), *f);
407 return { FIOS_TYPE_OLDFILE, GetOldSaveGameName(file) };
411 return { FIOS_TYPE_INVALID, {} };
423 static std::optional<std::string> fios_save_path;
425 if (!fios_save_path) fios_save_path = FioFindDirectory(
SAVE_DIR);
427 _fios_path = &(*fios_save_path);
454 return { FIOS_TYPE_OLD_SCENARIO, GetOldSaveGameName(file) };
458 return { FIOS_TYPE_INVALID, {} };
470 static std::optional<std::string> fios_scn_path;
473 if (!fios_scn_path) fios_scn_path = FioFindDirectory(
SCENARIO_DIR);
475 _fios_path = &(*fios_scn_path);
482std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(
SaveLoadOperation, std::string_view file, std::string_view ext)
497 if (type == FIOS_TYPE_INVALID)
return { FIOS_TYPE_INVALID, {} };
499 TarFileList::iterator it = _tar_filelist[
SCENARIO_DIR].find(file);
510 if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
516 if (!match)
return { FIOS_TYPE_INVALID, {} };
530 static std::optional<std::string> fios_hmap_path;
532 if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(
HEIGHTMAP_DIR);
534 _fios_path = &(*fios_hmap_path);
538 FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
555 return { FIOS_TYPE_INVALID, {} };
566 static std::optional<std::string> fios_town_data_path;
568 if (!fios_town_data_path) fios_town_data_path = FioFindDirectory(
HEIGHTMAP_DIR);
570 _fios_path = &(*fios_town_data_path);
583 static std::optional<std::string> fios_screenshot_path;
585 if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(
SCREENSHOT_DIR);
587 return *fios_screenshot_path;
598 return this->scenid == other.
scenid && this->md5sum == other.
md5sum;
617 if (this->scanned && !rescan)
return;
620 this->scanned =
true;
623 bool AddFile(
const std::string &filename,
size_t,
const std::string &)
override
626 if (!f.has_value())
return false;
629 int fret = fscanf(*f,
"%u", &
id.scenid);
630 if (fret != 1)
return false;
631 id.filename = filename;
634 uint8_t buffer[1024];
641 if (!f.has_value())
return false;
644 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, *f)) != 0 && size != 0) {
646 checksum.Append(buffer, len);
648 checksum.Finish(
id.md5sum);
669 if (md5sum ? (
id.md5sum == ci.
md5sum)
703 static std::optional<std::string> _autosave_path;
704 if (!_autosave_path) _autosave_path = FioFindDirectory(
AUTOSAVE_DIR);
706 static std::string _prefix;
709 static FiosGetTypeAndNameProc *
const proc = [](
SaveLoadOperation, std::string_view file, std::string_view ext) {
710 if (
StrEqualsIgnoreCase(ext,
".sav") && file.starts_with(_prefix))
return std::tuple(FIOS_TYPE_FILE, std::string{});
711 return std::tuple(FIOS_TYPE_INVALID, std::string{});
715 _prefix = *_autosave_path + this->prefix;
720 scanner.
Scan(
".sav", *_autosave_path,
false);
723 if (list.begin() != list.end()) {
724 SortingBits order = _savegame_sort_order;
725 _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
726 std::sort(list.begin(), list.end());
727 _savegame_sort_order = order;
729 std::string name = list.begin()->title.GetDecodedString();
730 std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
741 return fmt::format(
"{}{}.sav", this->prefix, this->number);
750 return fmt::format(
"-{}.sav", this->prefix);
std::string GetDecodedString() const
Decode the encoded string.
List of file information.
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop, bool show_dirs)
Construct a file list with the given kind of files, for the stated purpose.
const FiosItem * FindItem(std::string_view file)
Find file information of a file by its name from the file list.
Helper for scanning for files with a given name.
uint Scan(std::string_view extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Scanner to scan for a particular type of FIOS file.
FiosFileScanner(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, FileList &file_list)
Create the scanner.
SaveLoadOperation fop
The kind of file we are looking for.
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Try to add a fios item set with the given filename.
FileList & file_list
Destination of the found files.
FiosGetTypeAndNameProc * callback_proc
Callback to check whether the file may be added.
Scanner to find the unique IDs of scenarios.
bool scanned
Whether we've already scanned.
void Scan(bool rescan)
Scan, but only if it's needed.
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
ScenarioScanner()
Initialise.
Parse data from a string / buffer.
std::optional< T > TryReadIntegerBase(int base, bool clamp=false)
Try to read and parse an integer in number 'base', and then advance the reader.
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Functions for Standard In/Out file operations.
SaveLoadOperation
Operation performed on the file.
@ SLO_SAVE
File is being saved.
@ SLO_LOAD
File is being loaded.
@ DFT_FIOS_DRIVE
A drive (letter) entry.
@ DFT_FIOS_DIR
A directory entry.
@ DFT_FIOS_PARENT
A parent directory entry.
@ DFT_INVALID
Unknown or invalid file.
@ DFT_FIOS_DIRECT
Direct filename.
Searchpath
Types of searchpaths OpenTTD might use.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
@ NO_DIRECTORY
A path without any base directory.
@ SCREENSHOT_DIR
Subdirectory for all screenshots.
@ SCENARIO_DIR
Base directory for all scenarios.
@ SAVE_DIR
Base directory for all savegames.
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
AbstractFileType
The different abstract types of files that the system knows about.
@ FT_SCENARIO
old or new scenario
@ FT_HEIGHTMAP
heightmap file
@ FT_SAVEGAME
old or new savegame
@ FT_TOWN_DATA
town data file
std::tuple< FiosType, std::string > FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetFileList.
static std::tuple< FiosType, std::string > FiosGetTownDataListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetTownDataList.
std::optional< std::string_view > FindScenario(const ContentInfo &ci, bool md5sum)
Find a given scenario based on its unique ID.
std::string FiosMakeSavegameName(std::string_view name)
Make a save game or scenario filename from a name.
std::tuple< FiosType, std::string > FiosGetSavegameListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetFileList.
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 wi...
std::string FiosGetCurrentPath()
Get the current path/working directory.
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.
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
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.
void ScanScenarios()
Force a (re)scan of the scenarios.
std::string FiosMakeHeightmapName(std::string_view name)
Construct a filename for a height map.
bool HasScenario(const ContentInfo &ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
std::string_view FiosGetScreenshotDir()
Get the directory for screenshots.
bool FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at _fios_path.
static ScenarioScanner _scanner
Scanner for scenarios.
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
Declarations for savegames operations.
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Part of the network protocol handling content distribution.
A number of safeguards to prevent using unsafe methods.
std::string_view GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Functions to make screenshots.
ClientSettings _settings_client
The current settings for this game.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Functions related to low-level strings.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Functions related to OTTD's strings.
GUISettings gui
settings related to the GUI
Container for all important information about a piece of content.
uint32_t unique_id
Unique ID; either GRF ID or shortname.
MD5Hash md5sum
The MD5 checksum.
Deals with finding savegames.
bool operator<(const FiosItem &other) const
Compare two FiosItem's.
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
FiosNumberedSaveName(const std::string &prefix)
Constructs FiosNumberedSaveName.
std::string Extension()
Generate an extension for a savegame name.
Elements of a file system that are recognized.
DetailedFileType detailed
Detailed file type.
uint8_t max_num_autosaves
controls how many autosavegames are made before the game starts to overwrite (names them 0 to max_num...
Basic data to distinguish a scenario.
uint32_t scenid
ID for the scenario (generated by content).
MD5Hash md5sum
MD5 checksum of file.
std::string filename
filename of the file.
Structs, typedefs and macros used for TAR file handling.
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.