Update to 14.0-beta1

This commit is contained in:
dP
2024-02-04 02:18:17 +05:30
parent 79037e2c65
commit 33ef333b57
1325 changed files with 138461 additions and 70983 deletions
+111 -140
View File
@@ -20,8 +20,6 @@
#include "strings_func.h"
#include "tar_type.h"
#include <sys/stat.h>
#include <functional>
#include <optional>
#include <charconv>
#ifndef _WIN32
@@ -36,15 +34,14 @@
static std::string *_fios_path = nullptr;
SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
/* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
extern bool FiosIsRoot(const char *path);
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
/* OS-specific functions are taken from their respective files (win32/unix .c) */
extern bool FiosIsRoot(const std::string &path);
extern bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb);
extern bool FiosIsHiddenFile(const struct dirent *ent);
extern void FiosGetDrives(FileList &file_list);
extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
/* get the name of an oldstyle savegame */
extern void GetOldSaveGameName(const std::string &file, char *title, const char *last);
extern std::string GetOldSaveGameName(const std::string &file);
/**
* Compare two FiosItem's. Used with sort when sorting the file list.
@@ -56,9 +53,9 @@ bool FiosItem::operator< (const FiosItem &other) const
int r = false;
if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
r = (*this).mtime - other.mtime;
r = this->mtime - other.mtime;
} else {
r = strnatcmp((*this).title, other.title);
r = StrNaturalCompare((*this).title, other.title);
}
if (r == 0) return false;
return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
@@ -68,8 +65,9 @@ bool FiosItem::operator< (const FiosItem &other) const
* Construct a file list with the given kind of files, for the stated purpose.
* @param abstract_filetype Kind of files to collect.
* @param fop Purpose of the collection, either #SLO_LOAD or #SLO_SAVE.
* @param show_dirs Whether to show directories.
*/
void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop, bool show_dirs)
{
this->clear();
@@ -79,15 +77,15 @@ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperati
break;
case FT_SAVEGAME:
FiosGetSavegameList(fop, *this);
FiosGetSavegameList(fop, show_dirs, *this);
break;
case FT_SCENARIO:
FiosGetScenarioList(fop, *this);
FiosGetScenarioList(fop, show_dirs, *this);
break;
case FT_HEIGHTMAP:
FiosGetHeightmapList(fop, *this);
FiosGetHeightmapList(fop, show_dirs, *this);
break;
default:
@@ -101,59 +99,54 @@ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperati
* or a numbered entry into the filename list.
* @return The information on the file, or \c nullptr if the file is not available.
*/
const FiosItem *FileList::FindItem(const char *file)
const FiosItem *FileList::FindItem(const std::string_view file)
{
for (const auto &it : *this) {
const FiosItem *item = &it;
if (strcmp(file, item->name) == 0) return item;
if (strcmp(file, item->title) == 0) return item;
if (file == item->name) return item;
if (file == item->title) return item;
}
/* If no name matches, try to parse it as number */
char *endptr;
int i = strtol(file, &endptr, 10);
if (file == endptr || *endptr != '\0') i = -1;
int i = std::strtol(file.data(), &endptr, 10);
if (file.data() == endptr || *endptr != '\0') i = -1;
if (IsInsideMM(i, 0, this->size())) return &this->at(i);
/* As a last effort assume it is an OpenTTD savegame and
* that the ".sav" part was not given. */
char long_file[MAX_PATH];
seprintf(long_file, lastof(long_file), "%s.sav", file);
std::string long_file(file);
long_file += ".sav";
for (const auto &it : *this) {
const FiosItem *item = &it;
if (strcmp(long_file, item->name) == 0) return item;
if (strcmp(long_file, item->title) == 0) return item;
if (long_file == item->name) return item;
if (long_file == item->title) return item;
}
return nullptr;
}
/**
* Get descriptive texts. Returns the path and free space
* left on the device
* @param path string describing the path
* @param total_free total free space in megabytes, optional (can be nullptr)
* @return StringID describing the path (free space or failure)
* Get the current path/working directory.
*/
StringID FiosGetDescText(const char **path, uint64 *total_free)
std::string FiosGetCurrentPath()
{
*path = _fios_path->c_str();
return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
return *_fios_path;
}
/**
* Browse to a new path based on the passed \a item, starting at #_fios_path.
* @param *item Item telling us what to do.
* @return A filename w/path if we reached a file, otherwise \c nullptr.
* @return \c true when the path got changed.
*/
const char *FiosBrowseTo(const FiosItem *item)
bool FiosBrowseTo(const FiosItem *item)
{
switch (item->type) {
case FIOS_TYPE_DRIVE:
#if defined(_WIN32) || defined(__OS2__)
#if defined(_WIN32)
assert(_fios_path != nullptr);
*_fios_path = std::string{ item->title[0] } + ":" PATHSEP;
*_fios_path = std::string{ item->title, 0, 1 } + ":" PATHSEP;
#endif
break;
@@ -191,10 +184,10 @@ const char *FiosBrowseTo(const FiosItem *item)
case FIOS_TYPE_OLD_SCENARIO:
case FIOS_TYPE_PNG:
case FIOS_TYPE_BMP:
return item->name;
return false;
}
return nullptr;
return true;
}
/**
@@ -216,16 +209,14 @@ static std::string FiosMakeFilename(const std::string *path, const char *name, c
/* Don't append the extension if it is already there */
const char *period = strrchr(name, '.');
if (period != nullptr && strcasecmp(period, ext) == 0) ext = "";
if (period != nullptr && StrEqualsIgnoreCase(period, ext)) ext = "";
return buf + PATHSEP + name + ext;
}
/**
* Make a save game or scenario filename from a name.
* @param buf Destination buffer for saving the filename.
* @param name Name of the file.
* @param last Last element of buffer \a buf.
* @return The completed filename.
*/
std::string FiosMakeSavegameName(const char *name)
@@ -259,14 +250,14 @@ bool FiosDelete(const char *name)
return unlink(filename.c_str()) == 0;
}
typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const std::string &filename, const char *ext, char *title, const char *last);
typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(SaveLoadOperation fop, const std::string &filename, const std::string_view ext);
/**
* Scanner to scan for a particular type of FIOS file.
*/
class FiosFileScanner : public FileScanner {
SaveLoadOperation fop; ///< The kind of file we are looking for.
fios_getlist_callback_proc *callback_proc; ///< Callback to check whether the file may be added
FiosGetTypeAndNameProc *callback_proc; ///< Callback to check whether the file may be added
FileList &file_list; ///< Destination of the found files.
public:
/**
@@ -275,7 +266,7 @@ public:
* @param callback_proc The function that is called where you need to do the filtering.
* @param file_list Destination of the found files.
*/
FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) :
FiosFileScanner(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, FileList &file_list) :
fop(fop), callback_proc(callback_proc), file_list(file_list)
{}
@@ -285,19 +276,15 @@ public:
/**
* Try to add a fios item set with the given filename.
* @param filename the full path to the file to read
* @param basepath_length amount of characters to chop of before to get a relative filename
* @return true if the file is added.
*/
bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
bool FiosFileScanner::AddFile(const std::string &filename, size_t, const std::string &)
{
auto sep = filename.rfind('.');
if (sep == std::string::npos) return false;
std::string ext = filename.substr(sep);
char fios_title[64];
fios_title[0] = '\0'; // reset the title;
FiosType type = this->callback_proc(this->fop, filename, ext.c_str(), fios_title, lastof(fios_title));
auto [type, title] = this->callback_proc(this->fop, filename, ext);
if (type == FIOS_TYPE_INVALID) return false;
for (const auto &fios : file_list) {
@@ -334,16 +321,15 @@ bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_lengt
}
fios->type = type;
strecpy(fios->name, filename.c_str(), lastof(fios->name));
fios->name = filename;
/* If the file doesn't have a title, use its filename */
const char *t = fios_title;
if (StrEmpty(fios_title)) {
if (title.empty()) {
auto ps = filename.rfind(PATHSEPCHAR);
t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
}
strecpy(fios->title, t, lastof(fios->title));
StrMakeValidInPlace(fios->title, lastof(fios->title));
fios->title = StrMakeValid(filename.substr((ps == std::string::npos ? 0 : ps + 1)));
} else {
fios->title = StrMakeValid(title);
};
return true;
}
@@ -352,57 +338,55 @@ bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_lengt
/**
* Fill the list of the files in a directory, according to some arbitrary rule.
* @param fop Purpose of collecting the list.
* @param show_dirs Whether to list directories.
* @param callback_proc The function that is called where you need to do the filtering.
* @param subdir The directory from where to start (global) searching.
* @param file_list Destination of the found files.
*/
static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
static void FiosGetFileList(SaveLoadOperation fop, bool show_dirs, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
{
struct stat sb;
struct dirent *dirent;
DIR *dir;
FiosItem *fios;
size_t sort_start;
char d_name[sizeof(fios->name)];
file_list.clear();
assert(_fios_path != nullptr);
/* A parent directory link exists if we are not in the root directory */
if (!FiosIsRoot(_fios_path->c_str())) {
if (show_dirs && !FiosIsRoot(*_fios_path)) {
fios = &file_list.emplace_back();
fios->type = FIOS_TYPE_PARENT;
fios->mtime = 0;
strecpy(fios->name, "..", lastof(fios->name));
fios->name = "..";
SetDParamStr(0, "..");
GetString(fios->title, STR_SAVELOAD_PARENT_DIRECTORY, lastof(fios->title));
fios->title = GetString(STR_SAVELOAD_PARENT_DIRECTORY);
}
/* Show subdirectories */
if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
if (show_dirs && (dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
while ((dirent = readdir(dir)) != nullptr) {
strecpy(d_name, FS2OTTD(dirent->d_name).c_str(), lastof(d_name));
std::string d_name = FS2OTTD(dirent->d_name);
/* found file must be directory, but not '.' or '..' */
if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) &&
(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
if (FiosIsValidFile(*_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
(!FiosIsHiddenFile(dirent) || StrStartsWithIgnoreCase(PERSONAL_DIR, d_name)) &&
d_name != "." && d_name != "..") {
fios = &file_list.emplace_back();
fios->type = FIOS_TYPE_DIR;
fios->mtime = 0;
strecpy(fios->name, d_name, lastof(fios->name));
std::string dirname = std::string(d_name) + PATHSEP;
SetDParamStr(0, dirname);
GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title));
StrMakeValidInPlace(fios->title, lastof(fios->title));
fios->name = d_name;
SetDParamStr(0, fios->name + PATHSEP);
fios->title = GetString(STR_SAVELOAD_DIRECTORY);
}
}
closedir(dir);
}
/* Sort the subdirs always by name, ascending, remember user-sorting order */
{
if (show_dirs) {
SortingBits order = _savegame_sort_order;
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
std::sort(file_list.begin(), file_list.end());
@@ -415,7 +399,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
/* Show files */
FiosFileScanner scanner(fop, callback_proc, file_list);
if (subdir == NO_DIRECTORY) {
scanner.Scan(nullptr, _fios_path->c_str(), false);
scanner.Scan(nullptr, *_fios_path, false);
} else {
scanner.Scan(nullptr, subdir, true, true);
}
@@ -432,23 +416,20 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
* 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.
* @param file filename to get the title for
* @param title the title buffer to fill
* @param last the last element in the title buffer
* @param subdir the sub directory to search in
* @return The file title.
*/
static void GetFileTitle(const std::string &file, char *title, const char *last, Subdirectory subdir)
static std::string GetFileTitle(const std::string &file, Subdirectory subdir)
{
std::string buf = file;
buf += ".title";
FILE *f = FioFOpenFile(file + ".title", "r", subdir);
if (f == nullptr) return {};
FILE *f = FioFOpenFile(buf, "r", subdir);
if (f == nullptr) return;
size_t read = fread(title, 1, last - title, f);
assert(title + read <= last);
title[read] = '\0';
StrMakeValidInPlace(title, last);
char title[80];
size_t read = fread(title, 1, lengthof(title), f);
FioFCloseFile(f);
assert(read <= lengthof(title));
return StrMakeValid({title, read});
}
/**
@@ -456,13 +437,11 @@ static void GetFileTitle(const std::string &file, char *title, const char *last,
* @param fop Purpose of collecting the list.
* @param file Name of the file to check.
* @param ext A pointer to the extension identifier inside file
* @param title Buffer if a callback wants to lookup the title of the file; nullptr to skip the lookup
* @param last Last available byte in buffer (to prevent buffer overflows); not used when title == nullptr
* @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame
* @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame, and the title of the file (if any).
* @see FiosGetFileList
* @see FiosGetSavegameList
*/
FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
{
/* Show savegame files
* .SAV OpenTTD saved game
@@ -470,32 +449,28 @@ FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &f
* .SV1 Transport Tycoon Deluxe (Patch) saved game
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
/* Don't crash if we supply no extension */
if (ext == nullptr) return FIOS_TYPE_INVALID;
if (strcasecmp(ext, ".sav") == 0) {
GetFileTitle(file, title, last, SAVE_DIR);
return FIOS_TYPE_FILE;
if (StrEqualsIgnoreCase(ext, ".sav")) {
return { FIOS_TYPE_FILE, GetFileTitle(file, SAVE_DIR) };
}
if (fop == SLO_LOAD) {
if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
strcasecmp(ext, ".sv2") == 0) {
if (title != nullptr) GetOldSaveGameName(file, title, last);
return FIOS_TYPE_OLDFILE;
if (StrEqualsIgnoreCase(ext, ".ss1") || StrEqualsIgnoreCase(ext, ".sv1") ||
StrEqualsIgnoreCase(ext, ".sv2")) {
return { FIOS_TYPE_OLDFILE, GetOldSaveGameName(file) };
}
}
return FIOS_TYPE_INVALID;
return { FIOS_TYPE_INVALID, {} };
}
/**
* Get a list of savegames.
* @param fop Purpose of collecting the list.
* @param show_dirs Whether to show directories.
* @param file_list Destination of the found files.
* @see FiosGetFileList
*/
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
{
static std::optional<std::string> fios_save_path;
@@ -503,7 +478,7 @@ void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
_fios_path = &(*fios_save_path);
FiosGetFileList(fop, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
FiosGetFileList(fop, show_dirs, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
}
/**
@@ -511,40 +486,38 @@ void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
* @param fop Purpose of collecting the list.
* @param file Name of the file to check.
* @param ext A pointer to the extension identifier inside file
* @param title Buffer if a callback wants to lookup the title of the file
* @param last Last available byte in buffer (to prevent buffer overflows)
* @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario
* @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario and the title of the file (if any).
* @see FiosGetFileList
* @see FiosGetScenarioList
*/
static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
{
/* Show scenario files
* .SCN OpenTTD style scenario file
* .SV0 Transport Tycoon Deluxe (Patch) scenario
* .SS0 Transport Tycoon Deluxe preset scenario */
if (strcasecmp(ext, ".scn") == 0) {
GetFileTitle(file, title, last, SCENARIO_DIR);
return FIOS_TYPE_SCENARIO;
if (StrEqualsIgnoreCase(ext, ".scn")) {
return { FIOS_TYPE_SCENARIO, GetFileTitle(file, SCENARIO_DIR) };
}
if (fop == SLO_LOAD) {
if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
GetOldSaveGameName(file, title, last);
return FIOS_TYPE_OLD_SCENARIO;
if (StrEqualsIgnoreCase(ext, ".sv0") || StrEqualsIgnoreCase(ext, ".ss0")) {
return { FIOS_TYPE_OLD_SCENARIO, GetOldSaveGameName(file) };
}
}
return FIOS_TYPE_INVALID;
return { FIOS_TYPE_INVALID, {} };
}
/**
* Get a list of scenarios.
* @param fop Purpose of collecting the list.
* @param show_dirs Whether to show directories.
* @param file_list Destination of the found files.
* @see FiosGetFileList
*/
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
{
static std::optional<std::string> fios_scn_path;
@@ -555,10 +528,10 @@ void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
std::string base_path = FioFindDirectory(SCENARIO_DIR);
Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
}
static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, const std::string &file, const std::string_view ext)
{
/* Show heightmap files
* .PNG PNG Based heightmap files
@@ -568,12 +541,12 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::s
FiosType type = FIOS_TYPE_INVALID;
#ifdef WITH_PNG
if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
if (StrEqualsIgnoreCase(ext, ".png")) type = FIOS_TYPE_PNG;
#endif /* WITH_PNG */
if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
if (StrEqualsIgnoreCase(ext, ".bmp")) type = FIOS_TYPE_BMP;
if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
if (type == FIOS_TYPE_INVALID) return { FIOS_TYPE_INVALID, {} };
TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
if (it != _tar_filelist[SCENARIO_DIR].end()) {
@@ -592,20 +565,19 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::s
}
}
if (!match) return FIOS_TYPE_INVALID;
if (!match) return { FIOS_TYPE_INVALID, {} };
}
GetFileTitle(file, title, last, HEIGHTMAP_DIR);
return type;
return { type, GetFileTitle(file, HEIGHTMAP_DIR) };
}
/**
* Get a list of heightmaps.
* @param fop Purpose of collecting the list.
* @param show_dirs Whether to show directories.
* @param file_list Destination of the found files.
*/
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
{
static std::optional<std::string> fios_hmap_path;
@@ -615,7 +587,7 @@ void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
}
/**
@@ -633,14 +605,13 @@ const char *FiosGetScreenshotDir()
/** Basic data to distinguish a scenario. Used in the server list window */
struct ScenarioIdentifier {
uint32 scenid; ///< ID for the scenario (generated by content).
uint8 md5sum[16]; ///< MD5 checksum of file.
char filename[MAX_PATH]; ///< filename of the file.
uint32_t scenid; ///< ID for the scenario (generated by content).
MD5Hash md5sum; ///< MD5 checksum of file.
std::string filename; ///< filename of the file.
bool operator == (const ScenarioIdentifier &other) const
{
return this->scenid == other.scenid &&
memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
return this->scenid == other.scenid && this->md5sum == other.md5sum;
}
bool operator != (const ScenarioIdentifier &other) const
@@ -670,7 +641,7 @@ public:
this->scanned = true;
}
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
bool AddFile(const std::string &filename, size_t, const std::string &) override
{
FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
if (f == nullptr) return false;
@@ -679,10 +650,10 @@ public:
int fret = fscanf(f, "%u", &id.scenid);
FioFCloseFile(f);
if (fret != 1) return false;
strecpy(id.filename, filename.c_str(), lastof(id.filename));
id.filename = filename;
Md5 checksum;
uint8 buffer[1024];
uint8_t buffer[1024];
size_t len, size;
/* open the scenario file, but first get the name.
@@ -719,9 +690,9 @@ const char *FindScenario(const ContentInfo *ci, bool md5sum)
_scanner.Scan(false);
for (ScenarioIdentifier &id : _scanner) {
if (md5sum ? (memcmp(id.md5sum, ci->md5sum, sizeof(id.md5sum)) == 0)
if (md5sum ? (id.md5sum == ci->md5sum)
: (id.scenid == ci->unique_id)) {
return id.filename;
return id.filename.c_str();
}
}
@@ -750,7 +721,7 @@ void ScanScenarios()
/**
* Constructs FiosNumberedSaveName. Initial number is the most recent save, or -1 if not found.
* @param prefix The prefix to use to generate a filename.
*/
*/
FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1)
{
static std::optional<std::string> _autosave_path;
@@ -759,9 +730,9 @@ FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(p
static std::string _prefix; ///< Static as the lambda needs access to it.
/* Callback for FiosFileScanner. */
static fios_getlist_callback_proc *proc = [](SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last) {
if (strcasecmp(ext, ".sav") == 0 && StrStartsWith(file, _prefix)) return FIOS_TYPE_FILE;
return FIOS_TYPE_INVALID;
static FiosGetTypeAndNameProc *proc = [](SaveLoadOperation, const std::string &file, const std::string_view ext) {
if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
return std::tuple(FIOS_TYPE_INVALID, std::string{});
};
/* Prefix to check in the callback. */
@@ -787,7 +758,7 @@ FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(p
/**
* Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
* @return A filename in format "<prefix><number>.sav".
*/
*/
std::string FiosNumberedSaveName::Filename()
{
if (++this->number >= _settings_client.gui.max_num_autosaves) this->number = 0;
@@ -797,7 +768,7 @@ std::string FiosNumberedSaveName::Filename()
/**
* Generate an extension for a savegame name.
* @return An extension in format "-<prefix>.sav".
*/
*/
std::string FiosNumberedSaveName::Extension()
{
return fmt::format("-{}.sav", this->prefix);