11#include "3rdparty/md5/md5.h"
24#include "table/strings.h"
28static std::string *_fios_path =
nullptr;
31extern bool FiosIsRoot(
const std::string &path);
32extern bool FiosIsHiddenFile(
const std::filesystem::path &path);
33extern void FiosGetDrives(
FileList &file_list);
36extern std::string GetOldSaveGameName(std::string_view file);
48 return a.mtime < b.mtime;
62 switch (abstract_filetype) {
95 for (
const auto &it : *
this) {
97 if (file == item->name)
return item;
104 if (number.has_value() && !consumer.
AnyBytesLeft() &&
IsInsideMM(*number, 0, this->size()))
return &this->at(*number);
108 std::string long_file(file);
110 for (
const auto &it : *
this) {
112 if (long_file == item->name)
return item;
138 assert(_fios_path !=
nullptr);
139 *_fios_path = std::string{ item->name, 0, 1 } +
":" PATHSEP;
147 assert(_fios_path !=
nullptr);
148 auto s = _fios_path->find_last_of(PATHSEPCHAR);
149 if (s != std::string::npos && s != 0) {
150 _fios_path->erase(s);
153 s = _fios_path->find_last_of(PATHSEPCHAR);
154 if (s != std::string::npos) {
155 _fios_path->erase(s + 1);
161 assert(_fios_path !=
nullptr);
162 *_fios_path += item->name;
163 *_fios_path += PATHSEP;
167 assert(_fios_path !=
nullptr);
168 *_fios_path = item->name;
185static std::string
FiosMakeFilename(
const std::string *path, std::string_view name, std::string_view ext)
187 std::string_view base_path;
189 if (path !=
nullptr) {
192 if (!base_path.empty() && base_path.back() == PATHSEPCHAR) base_path.remove_suffix(1);
196 auto period = name.find_last_of(
'.');
197 if (period != std::string_view::npos &&
StrEqualsIgnoreCase(name.substr(period), ext)) ext =
"";
199 return fmt::format(
"{}{}{}{}", base_path, PATHSEP, name, ext);
209 std::string_view extension = (_game_mode == GM_EDITOR) ?
".scn" :
".sav";
224typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(
SaveLoadOperation fop, std::string_view filename, std::string_view ext);
244 bool AddFile(
const std::string &filename,
size_t,
const std::string &)
override;
254 auto sep = filename.rfind(
'.');
255 if (sep == std::string::npos)
return false;
256 std::string ext = filename.substr(sep);
259 if (type == FIOS_TYPE_INVALID)
return false;
262 if (filename == fios.name)
return false;
267 std::error_code error_code;
268 auto write_time = std::filesystem::last_write_time(
OTTD2FS(filename), error_code);
272 fios->mtime = std::chrono::duration_cast<std::chrono::milliseconds>(write_time.time_since_epoch()).count();
276 fios->name = filename;
280 auto ps = filename.rfind(PATHSEPCHAR);
300 size_t sort_start = 0;
304 assert(_fios_path !=
nullptr);
308 if (!FiosIsRoot(*_fios_path)) {
309 FiosItem &fios = file_list.emplace_back();
310 fios.type = FIOS_TYPE_PARENT;
314 sort_start = file_list.size();
318 std::error_code error_code;
319 for (
const auto &dir_entry : std::filesystem::directory_iterator(
OTTD2FS(*_fios_path), error_code)) {
320 if (!dir_entry.is_directory())
continue;
321 if (FiosIsHiddenFile(dir_entry) && dir_entry.path().filename() != PERSONAL_DIR)
continue;
323 FiosItem &fios = file_list.emplace_back();
324 fios.type = FIOS_TYPE_DIR;
326 fios.name =
FS2OTTD(dir_entry.path().filename().native());
335 sort_start = file_list.size();
340 scanner.
Scan({}, *_fios_path,
false);
342 scanner.
Scan({}, subdir,
true,
true);
345 std::sort(file_list.begin() + sort_start, file_list.end(),
FiosItemSorter);
348 FiosGetDrives(file_list);
360 std::string filename = fmt::format(
"{}.title", file);
362 if (!f.has_value())
return {};
365 size_t read = fread(title, 1,
lengthof(title), *f);
395 return { FIOS_TYPE_OLDFILE, GetOldSaveGameName(file) };
399 return { FIOS_TYPE_INVALID, {} };
411 static std::optional<std::string> fios_save_path;
415 _fios_path = &(*fios_save_path);
442 return { FIOS_TYPE_OLD_SCENARIO, GetOldSaveGameName(file) };
446 return { FIOS_TYPE_INVALID, {} };
458 static std::optional<std::string> fios_scn_path;
463 _fios_path = &(*fios_scn_path);
470std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(
SaveLoadOperation, std::string_view file, std::string_view ext)
485 if (type == FIOS_TYPE_INVALID)
return { FIOS_TYPE_INVALID, {} };
498 if (it->second.tar_filename.starts_with(buf)) {
504 if (!match)
return { FIOS_TYPE_INVALID, {} };
518 static std::optional<std::string> fios_hmap_path;
522 _fios_path = &(*fios_hmap_path);
526 FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
544 return { FIOS_TYPE_INVALID, {} };
555 static std::optional<std::string> fios_town_data_path;
559 _fios_path = &(*fios_town_data_path);
572 static std::optional<std::string> fios_screenshot_path;
576 return *fios_screenshot_path;
587 return this->scenid == other.
scenid && this->md5sum == other.
md5sum;
606 if (this->scanned && !rescan)
return;
609 this->scanned =
true;
612 bool AddFile(
const std::string &filename,
size_t,
const std::string &)
override
615 if (!f.has_value())
return false;
618 int fret = fscanf(*f,
"%u", &
id.scenid);
619 if (fret != 1)
return false;
620 id.filename = filename;
623 uint8_t buffer[1024];
630 if (!f.has_value())
return false;
633 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, *f)) != 0 && size != 0) {
635 checksum.Append(buffer, len);
637 checksum.Finish(
id.md5sum);
658 if (md5sum ? (
id.md5sum == ci.
md5sum)
692 static std::optional<std::string> _autosave_path;
695 static std::string _prefix;
698 static FiosGetTypeAndNameProc *
const proc = [](
SaveLoadOperation, std::string_view file, std::string_view ext) {
699 if (
StrEqualsIgnoreCase(ext,
".sav") && file.starts_with(_prefix))
return std::tuple(FIOS_TYPE_FILE, std::string{});
700 return std::tuple(FIOS_TYPE_INVALID, std::string{});
704 _prefix = *_autosave_path + this->prefix;
709 scanner.
Scan(
".sav", *_autosave_path,
false);
714 std::string name = elem->title.GetDecodedString();
715 std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
725 if (++this->number >=
_settings_client.gui.max_num_autosaves) this->number = 0;
726 return fmt::format(
"{}{}.sav", this->prefix, this->number);
735 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.
FileList & file_list
Destination of the found files.
bool AddFile(const std::string &filename, size_t, const std::string &) override
Try to add a fios item set with the given filename.
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 AnyBytesLeft() const noexcept
Check whether any bytes left to read.
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.
EnumClassIndexContainer< std::array< TarFileList, to_underlying(Subdirectory::End)>, Subdirectory > _tar_filelist
List of files within tar files found in each subdirectory.
Functions for standard in/out file operations.
SaveLoadOperation
Operation performed on the file.
@ Save
File is being saved.
@ Load
File is being loaded.
@ FiosDirect
Direct filename.
@ FiosDrive
A drive (letter) entry.
@ Invalid
Unknown or invalid file.
@ FiosDirectory
A directory entry.
@ FiosParent
A parent directory entry.
Searchpath
Types of searchpaths OpenTTD might use.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
@ Autosave
Subdirectory of save for autosaves.
@ Screenshot
Subdirectory for all screenshots.
@ Scenario
Base directory for all scenarios.
@ Heightmap
Subdirectory of scenario for heightmaps.
@ None
A path without any base directory.
@ Save
Base directory for all savegames.
AbstractFileType
The different abstract types of files that the system knows about.
@ Savegame
old or new savegame
@ Scenario
old or new scenario
@ Heightmap
heightmap file
std::tuple< FiosType, std::string > FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetFileList.
bool FiosItemModificationDateSorter(const FiosItem &a, const FiosItem &b)
Sort files by their modification date, and name when they are equal.
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.
bool FiosItemNameSorter(const FiosItem &a, const FiosItem &b)
Sort files by their 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.
bool FiosItemSorter(const FiosItem &a, const FiosItem &b)
Sorts the FiosItems based on the savegame sorter and order.
bool FiosItemModificationDateSorter(const FiosItem &a, const FiosItem &b)
Sort files by their modification date, and name when they are equal.
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.
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.
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.
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.