mirror of https://github.com/OpenTTD/OpenTTD
Codechange: make some saveload functions work natively with std::string_view
parent
855377191e
commit
c1a287ad17
19
src/fios.cpp
19
src/fios.cpp
|
@ -38,7 +38,7 @@ extern bool FiosIsHiddenFile(const std::filesystem::path &path);
|
||||||
extern void FiosGetDrives(FileList &file_list);
|
extern void FiosGetDrives(FileList &file_list);
|
||||||
|
|
||||||
/* get the name of an oldstyle savegame */
|
/* get the name of an oldstyle savegame */
|
||||||
extern std::string GetOldSaveGameName(const std::string &file);
|
extern std::string GetOldSaveGameName(std::string_view file);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two FiosItem's. Used with sort when sorting the file list.
|
* Compare two FiosItem's. Used with sort when sorting the file list.
|
||||||
|
@ -240,7 +240,7 @@ bool FiosDelete(std::string_view name)
|
||||||
return FioRemove(FiosMakeSavegameName(name));
|
return FioRemove(FiosMakeSavegameName(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(SaveLoadOperation fop, const std::string &filename, std::string_view ext);
|
typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(SaveLoadOperation fop, std::string_view filename, std::string_view ext);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scanner to scan for a particular type of FIOS file.
|
* Scanner to scan for a particular type of FIOS file.
|
||||||
|
@ -377,9 +377,10 @@ static void FiosGetFileList(SaveLoadOperation fop, bool show_dirs, FiosGetTypeAn
|
||||||
* @param subdir the sub directory to search in
|
* @param subdir the sub directory to search in
|
||||||
* @return The file title.
|
* @return The file title.
|
||||||
*/
|
*/
|
||||||
static std::string GetFileTitle(const std::string &file, Subdirectory subdir)
|
static std::string GetFileTitle(std::string_view file, Subdirectory subdir)
|
||||||
{
|
{
|
||||||
auto f = FioFOpenFile(file + ".title", "r", subdir);
|
std::string filename = fmt::format("{}.title", file);
|
||||||
|
auto f = FioFOpenFile(filename, "r", subdir);
|
||||||
if (!f.has_value()) return {};
|
if (!f.has_value()) return {};
|
||||||
|
|
||||||
char title[80];
|
char title[80];
|
||||||
|
@ -398,7 +399,7 @@ static std::string GetFileTitle(const std::string &file, Subdirectory subdir)
|
||||||
* @see FiosGetFileList
|
* @see FiosGetFileList
|
||||||
* @see FiosGetSavegameList
|
* @see FiosGetSavegameList
|
||||||
*/
|
*/
|
||||||
std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, std::string_view ext)
|
std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
|
||||||
{
|
{
|
||||||
/* Show savegame files
|
/* Show savegame files
|
||||||
* .SAV OpenTTD saved game
|
* .SAV OpenTTD saved game
|
||||||
|
@ -447,7 +448,7 @@ void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_l
|
||||||
* @see FiosGetFileList
|
* @see FiosGetFileList
|
||||||
* @see FiosGetScenarioList
|
* @see FiosGetScenarioList
|
||||||
*/
|
*/
|
||||||
std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, std::string_view ext)
|
std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
|
||||||
{
|
{
|
||||||
/* Show scenario files
|
/* Show scenario files
|
||||||
* .SCN OpenTTD style scenario file
|
* .SCN OpenTTD style scenario file
|
||||||
|
@ -488,7 +489,7 @@ void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_l
|
||||||
FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
|
FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, const std::string &file, std::string_view ext)
|
std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, std::string_view file, std::string_view ext)
|
||||||
{
|
{
|
||||||
/* Show heightmap files
|
/* Show heightmap files
|
||||||
* .PNG PNG Based heightmap files
|
* .PNG PNG Based heightmap files
|
||||||
|
@ -553,7 +554,7 @@ void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_
|
||||||
* @param file Name of the file to check.
|
* @param file Name of the file to check.
|
||||||
* @return 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).
|
* @return 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).
|
||||||
*/
|
*/
|
||||||
static std::tuple<FiosType, std::string> FiosGetTownDataListCallback(SaveLoadOperation fop, const std::string &file, std::string_view ext)
|
static std::tuple<FiosType, std::string> FiosGetTownDataListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
|
||||||
{
|
{
|
||||||
if (fop == SLO_LOAD) {
|
if (fop == SLO_LOAD) {
|
||||||
if (StrEqualsIgnoreCase(ext, ".json")) {
|
if (StrEqualsIgnoreCase(ext, ".json")) {
|
||||||
|
@ -715,7 +716,7 @@ FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(p
|
||||||
static std::string _prefix; ///< Static as the lambda needs access to it.
|
static std::string _prefix; ///< Static as the lambda needs access to it.
|
||||||
|
|
||||||
/* Callback for FiosFileScanner. */
|
/* Callback for FiosFileScanner. */
|
||||||
static FiosGetTypeAndNameProc *proc = [](SaveLoadOperation, const std::string &file, std::string_view ext) {
|
static FiosGetTypeAndNameProc *proc = [](SaveLoadOperation, std::string_view file, std::string_view ext) {
|
||||||
if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
|
if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
|
||||||
return std::tuple(FIOS_TYPE_INVALID, std::string{});
|
return std::tuple(FIOS_TYPE_INVALID, std::string{});
|
||||||
};
|
};
|
||||||
|
|
|
@ -116,9 +116,9 @@ bool FiosDelete(std::string_view name);
|
||||||
std::string FiosMakeHeightmapName(std::string_view name);
|
std::string FiosMakeHeightmapName(std::string_view name);
|
||||||
std::string FiosMakeSavegameName(std::string_view name);
|
std::string FiosMakeSavegameName(std::string_view name);
|
||||||
|
|
||||||
std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, std::string_view ext);
|
std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext);
|
||||||
std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, std::string_view ext);
|
std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext);
|
||||||
std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, std::string_view ext);
|
std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext);
|
||||||
|
|
||||||
void ScanScenarios();
|
void ScanScenarios();
|
||||||
std::optional<std::string_view> FindScenario(const ContentInfo &ci, bool md5sum);
|
std::optional<std::string_view> FindScenario(const ContentInfo &ci, bool md5sum);
|
||||||
|
|
|
@ -231,7 +231,7 @@ static std::tuple<SavegameType, std::string> DetermineOldSavegameTypeAndName(Fil
|
||||||
|
|
||||||
typedef bool LoadOldMainProc(LoadgameState &ls);
|
typedef bool LoadOldMainProc(LoadgameState &ls);
|
||||||
|
|
||||||
bool LoadOldSaveGame(const std::string &file)
|
bool LoadOldSaveGame(std::string_view file)
|
||||||
{
|
{
|
||||||
LoadgameState ls{};
|
LoadgameState ls{};
|
||||||
|
|
||||||
|
@ -282,7 +282,7 @@ bool LoadOldSaveGame(const std::string &file)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GetOldSaveGameName(const std::string &file)
|
std::string GetOldSaveGameName(std::string_view file)
|
||||||
{
|
{
|
||||||
auto f = FioFOpenFile(file, "rb", NO_DIRECTORY);
|
auto f = FioFOpenFile(file, "rb", NO_DIRECTORY);
|
||||||
if (!f.has_value()) return {};
|
if (!f.has_value()) return {};
|
||||||
|
|
|
@ -2889,7 +2889,7 @@ static std::pair<const SaveLoadFormat &, uint8_t> GetSavegameFormat(const std::s
|
||||||
/* actual loader/saver function */
|
/* actual loader/saver function */
|
||||||
void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
|
void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
|
||||||
extern bool AfterLoadGame();
|
extern bool AfterLoadGame();
|
||||||
extern bool LoadOldSaveGame(const std::string &file);
|
extern bool LoadOldSaveGame(std::string_view file);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset all settings to their default, so any settings missing in the savegame
|
* Reset all settings to their default, so any settings missing in the savegame
|
||||||
|
@ -3250,7 +3250,7 @@ SaveOrLoadResult LoadWithFilter(std::shared_ptr<LoadFilter> reader)
|
||||||
* @param threaded True when threaded saving is allowed
|
* @param threaded True when threaded saving is allowed
|
||||||
* @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
|
* @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
|
||||||
*/
|
*/
|
||||||
SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
|
SaveOrLoadResult SaveOrLoad(std::string_view filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
|
||||||
{
|
{
|
||||||
/* An instance of saving is already active, so don't go saving again */
|
/* An instance of saving is already active, so don't go saving again */
|
||||||
if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
|
if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
|
||||||
|
|
|
@ -439,7 +439,7 @@ std::string GenerateDefaultSaveName();
|
||||||
void SetSaveLoadError(StringID str);
|
void SetSaveLoadError(StringID str);
|
||||||
EncodedString GetSaveLoadErrorType();
|
EncodedString GetSaveLoadErrorType();
|
||||||
EncodedString GetSaveLoadErrorMessage();
|
EncodedString GetSaveLoadErrorMessage();
|
||||||
SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true);
|
SaveOrLoadResult SaveOrLoad(std::string_view filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true);
|
||||||
void WaitTillSaved();
|
void WaitTillSaved();
|
||||||
void ProcessAsyncSaveFinish();
|
void ProcessAsyncSaveFinish();
|
||||||
void DoExitSave();
|
void DoExitSave();
|
||||||
|
|
|
@ -19,8 +19,8 @@ struct TarFileListEntry {
|
||||||
size_t position;
|
size_t position;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::map<std::string, std::string> TarList; ///< Map of tar file to tar directory.
|
using TarList = std::map<std::string, std::string, std::less<>>; ///< Map of tar file to tar directory.
|
||||||
typedef std::map<std::string, TarFileListEntry> TarFileList;
|
using TarFileList = std::map<std::string, TarFileListEntry, std::less<>> ;
|
||||||
extern std::array<TarList, NUM_SUBDIRS> _tar_list;
|
extern std::array<TarList, NUM_SUBDIRS> _tar_list;
|
||||||
extern TarFileList _tar_filelist[NUM_SUBDIRS];
|
extern TarFileList _tar_filelist[NUM_SUBDIRS];
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue