2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 * This file contains functions for building file lists for the save/load dialogs.
14 #include "3rdparty/md5/md5.h"
15 #include "fileio_func.h"
17 #include "network/network_content.h"
18 #include "screenshot.h"
19 #include "string_func.h"
20 #include "strings_func.h"
29 #include "table/strings.h"
31 #include "safeguards.h"
33 /* Variables to display file lists */
34 static std::string
*_fios_path
= nullptr;
35 SortingBits _savegame_sort_order
= SORT_BY_DATE
| SORT_DESCENDING
;
37 /* OS-specific functions are taken from their respective files (win32/unix .c) */
38 extern bool FiosIsRoot(const std::string
&path
);
39 extern bool FiosIsValidFile(const std::string
&path
, const struct dirent
*ent
, struct stat
*sb
);
40 extern bool FiosIsHiddenFile(const struct dirent
*ent
);
41 extern void FiosGetDrives(FileList
&file_list
);
43 /* get the name of an oldstyle savegame */
44 extern std::string
GetOldSaveGameName(const std::string
&file
);
47 * Compare two FiosItem's. Used with sort when sorting the file list.
48 * @param other The FiosItem to compare to.
49 * @return for ascending order: returns true if da < db. Vice versa for descending order.
51 bool FiosItem::operator< (const FiosItem
&other
) const
55 if ((_savegame_sort_order
& SORT_BY_NAME
) == 0 && (*this).mtime
!= other
.mtime
) {
56 r
= this->mtime
- other
.mtime
;
58 r
= StrNaturalCompare((*this).title
, other
.title
);
60 if (r
== 0) return false;
61 return (_savegame_sort_order
& SORT_DESCENDING
) ? r
> 0 : r
< 0;
65 * Construct a file list with the given kind of files, for the stated purpose.
66 * @param abstract_filetype Kind of files to collect.
67 * @param fop Purpose of the collection, either #SLO_LOAD or #SLO_SAVE.
68 * @param show_dirs Whether to show directories.
70 void FileList::BuildFileList(AbstractFileType abstract_filetype
, SaveLoadOperation fop
, bool show_dirs
)
74 assert(fop
== SLO_LOAD
|| fop
== SLO_SAVE
);
75 switch (abstract_filetype
) {
80 FiosGetSavegameList(fop
, show_dirs
, *this);
84 FiosGetScenarioList(fop
, show_dirs
, *this);
88 FiosGetHeightmapList(fop
, show_dirs
, *this);
97 * Find file information of a file by its name from the file list.
98 * @param file The filename to return information about. Can be the actual name
99 * or a numbered entry into the filename list.
100 * @return The information on the file, or \c nullptr if the file is not available.
102 const FiosItem
*FileList::FindItem(const std::string_view file
)
104 for (const auto &it
: *this) {
105 const FiosItem
*item
= &it
;
106 if (file
== item
->name
) return item
;
107 if (file
== item
->title
) return item
;
110 /* If no name matches, try to parse it as number */
112 int i
= std::strtol(file
.data(), &endptr
, 10);
113 if (file
.data() == endptr
|| *endptr
!= '\0') i
= -1;
115 if (IsInsideMM(i
, 0, this->size())) return &this->at(i
);
117 /* As a last effort assume it is an OpenTTD savegame and
118 * that the ".sav" part was not given. */
119 std::string
long_file(file
);
121 for (const auto &it
: *this) {
122 const FiosItem
*item
= &it
;
123 if (long_file
== item
->name
) return item
;
124 if (long_file
== item
->title
) return item
;
131 * Get the current path/working directory.
133 std::string
FiosGetCurrentPath()
139 * Browse to a new path based on the passed \a item, starting at #_fios_path.
140 * @param *item Item telling us what to do.
141 * @return \c true when the path got changed.
143 bool FiosBrowseTo(const FiosItem
*item
)
145 switch (item
->type
) {
146 case FIOS_TYPE_DRIVE
:
148 assert(_fios_path
!= nullptr);
149 *_fios_path
= std::string
{ item
->title
, 0, 1 } + ":" PATHSEP
;
153 case FIOS_TYPE_INVALID
:
156 case FIOS_TYPE_PARENT
: {
157 assert(_fios_path
!= nullptr);
158 auto s
= _fios_path
->find_last_of(PATHSEPCHAR
);
159 if (s
!= std::string::npos
&& s
!= 0) {
160 _fios_path
->erase(s
); // Remove last path separator character, so we can go up one level.
163 s
= _fios_path
->find_last_of(PATHSEPCHAR
);
164 if (s
!= std::string::npos
) {
165 _fios_path
->erase(s
+ 1); // go up a directory
171 assert(_fios_path
!= nullptr);
172 *_fios_path
+= item
->name
;
173 *_fios_path
+= PATHSEP
;
176 case FIOS_TYPE_DIRECT
:
177 assert(_fios_path
!= nullptr);
178 *_fios_path
= item
->name
;
182 case FIOS_TYPE_OLDFILE
:
183 case FIOS_TYPE_SCENARIO
:
184 case FIOS_TYPE_OLD_SCENARIO
:
194 * Construct a filename from its components in destination buffer \a buf.
195 * @param path Directory path, may be \c nullptr.
196 * @param name Filename.
197 * @param ext Filename extension (use \c "" for no extension).
198 * @return The completed filename.
200 static std::string
FiosMakeFilename(const std::string
*path
, const char *name
, const char *ext
)
204 if (path
!= nullptr) {
206 /* Remove trailing path separator, if present */
207 if (!buf
.empty() && buf
.back() == PATHSEPCHAR
) buf
.pop_back();
210 /* Don't append the extension if it is already there */
211 const char *period
= strrchr(name
, '.');
212 if (period
!= nullptr && StrEqualsIgnoreCase(period
, ext
)) ext
= "";
214 return buf
+ PATHSEP
+ name
+ ext
;
218 * Make a save game or scenario filename from a name.
219 * @param name Name of the file.
220 * @return The completed filename.
222 std::string
FiosMakeSavegameName(const char *name
)
224 const char *extension
= (_game_mode
== GM_EDITOR
) ? ".scn" : ".sav";
226 return FiosMakeFilename(_fios_path
, name
, extension
);
230 * Construct a filename for a height map.
231 * @param name Filename.
232 * @return The completed filename.
234 std::string
FiosMakeHeightmapName(const char *name
)
236 std::string
ext(".");
237 ext
+= GetCurrentScreenshotExtension();
239 return FiosMakeFilename(_fios_path
, name
, ext
.c_str());
244 * @param name Filename to delete.
245 * @return Whether the file deletion was successful.
247 bool FiosDelete(const char *name
)
249 std::string filename
= FiosMakeSavegameName(name
);
250 return unlink(filename
.c_str()) == 0;
253 typedef std::tuple
<FiosType
, std::string
> FiosGetTypeAndNameProc(SaveLoadOperation fop
, const std::string
&filename
, const std::string_view ext
);
256 * Scanner to scan for a particular type of FIOS file.
258 class FiosFileScanner
: public FileScanner
{
259 SaveLoadOperation fop
; ///< The kind of file we are looking for.
260 FiosGetTypeAndNameProc
*callback_proc
; ///< Callback to check whether the file may be added
261 FileList
&file_list
; ///< Destination of the found files.
265 * @param fop Purpose of collecting the list.
266 * @param callback_proc The function that is called where you need to do the filtering.
267 * @param file_list Destination of the found files.
269 FiosFileScanner(SaveLoadOperation fop
, FiosGetTypeAndNameProc
*callback_proc
, FileList
&file_list
) :
270 fop(fop
), callback_proc(callback_proc
), file_list(file_list
)
273 bool AddFile(const std::string
&filename
, size_t basepath_length
, const std::string
&tar_filename
) override
;
277 * Try to add a fios item set with the given filename.
278 * @param filename the full path to the file to read
279 * @return true if the file is added.
281 bool FiosFileScanner::AddFile(const std::string
&filename
, size_t, const std::string
&)
283 auto sep
= filename
.rfind('.');
284 if (sep
== std::string::npos
) return false;
285 std::string ext
= filename
.substr(sep
);
287 auto [type
, title
] = this->callback_proc(this->fop
, filename
, ext
);
288 if (type
== FIOS_TYPE_INVALID
) return false;
290 for (const auto &fios
: file_list
) {
291 if (filename
== fios
.name
) return false;
294 FiosItem
*fios
= &file_list
.emplace_back();
296 // Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
297 HANDLE fh
= CreateFile(OTTD2FS(filename
).c_str(), GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, nullptr, OPEN_EXISTING
, 0, nullptr);
299 if (fh
!= INVALID_HANDLE_VALUE
) {
301 ULARGE_INTEGER ft_int64
;
303 if (GetFileTime(fh
, nullptr, nullptr, &ft
) != 0) {
304 ft_int64
.HighPart
= ft
.dwHighDateTime
;
305 ft_int64
.LowPart
= ft
.dwLowDateTime
;
307 // Convert from hectonanoseconds since 01/01/1601 to seconds since 01/01/1970
308 fios
->mtime
= ft_int64
.QuadPart
/ 10000000ULL - 11644473600ULL;
316 if (stat(filename
.c_str(), &sb
) == 0) {
317 fios
->mtime
= sb
.st_mtime
;
324 fios
->name
= filename
;
326 /* If the file doesn't have a title, use its filename */
328 auto ps
= filename
.rfind(PATHSEPCHAR
);
329 fios
->title
= StrMakeValid(filename
.substr((ps
== std::string::npos
? 0 : ps
+ 1)));
331 fios
->title
= StrMakeValid(title
);
339 * Fill the list of the files in a directory, according to some arbitrary rule.
340 * @param fop Purpose of collecting the list.
341 * @param show_dirs Whether to list directories.
342 * @param callback_proc The function that is called where you need to do the filtering.
343 * @param subdir The directory from where to start (global) searching.
344 * @param file_list Destination of the found files.
346 static void FiosGetFileList(SaveLoadOperation fop
, bool show_dirs
, FiosGetTypeAndNameProc
*callback_proc
, Subdirectory subdir
, FileList
&file_list
)
349 struct dirent
*dirent
;
356 assert(_fios_path
!= nullptr);
358 /* A parent directory link exists if we are not in the root directory */
359 if (show_dirs
&& !FiosIsRoot(*_fios_path
)) {
360 fios
= &file_list
.emplace_back();
361 fios
->type
= FIOS_TYPE_PARENT
;
364 SetDParamStr(0, "..");
365 fios
->title
= GetString(STR_SAVELOAD_PARENT_DIRECTORY
);
368 /* Show subdirectories */
369 if (show_dirs
&& (dir
= ttd_opendir(_fios_path
->c_str())) != nullptr) {
370 while ((dirent
= readdir(dir
)) != nullptr) {
371 std::string d_name
= FS2OTTD(dirent
->d_name
);
373 /* found file must be directory, but not '.' or '..' */
374 if (FiosIsValidFile(*_fios_path
, dirent
, &sb
) && S_ISDIR(sb
.st_mode
) &&
375 (!FiosIsHiddenFile(dirent
) || StrStartsWithIgnoreCase(PERSONAL_DIR
, d_name
)) &&
376 d_name
!= "." && d_name
!= "..") {
377 fios
= &file_list
.emplace_back();
378 fios
->type
= FIOS_TYPE_DIR
;
381 SetDParamStr(0, fios
->name
+ PATHSEP
);
382 fios
->title
= GetString(STR_SAVELOAD_DIRECTORY
);
388 /* Sort the subdirs always by name, ascending, remember user-sorting order */
390 SortingBits order
= _savegame_sort_order
;
391 _savegame_sort_order
= SORT_BY_NAME
| SORT_ASCENDING
;
392 std::sort(file_list
.begin(), file_list
.end());
393 _savegame_sort_order
= order
;
396 /* This is where to start sorting for the filenames */
397 sort_start
= file_list
.size();
400 FiosFileScanner
scanner(fop
, callback_proc
, file_list
);
401 if (subdir
== NO_DIRECTORY
) {
402 scanner
.Scan(nullptr, *_fios_path
, false);
404 scanner
.Scan(nullptr, subdir
, true, true);
407 std::sort(file_list
.begin() + sort_start
, file_list
.end());
410 FiosGetDrives(file_list
);
412 file_list
.shrink_to_fit();
416 * Get the title of a file, which (if exists) is stored in a file named
417 * the same as the data file but with '.title' added to it.
418 * @param file filename to get the title for
419 * @param subdir the sub directory to search in
420 * @return The file title.
422 static std::string
GetFileTitle(const std::string
&file
, Subdirectory subdir
)
424 FILE *f
= FioFOpenFile(file
+ ".title", "r", subdir
);
425 if (f
== nullptr) return {};
428 size_t read
= fread(title
, 1, lengthof(title
), f
);
431 assert(read
<= lengthof(title
));
432 return StrMakeValid({title
, read
});
436 * Callback for FiosGetFileList. It tells if a file is a savegame or not.
437 * @param fop Purpose of collecting the list.
438 * @param file Name of the file to check.
439 * @param ext A pointer to the extension identifier inside file
440 * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame, and the title of the file (if any).
441 * @see FiosGetFileList
442 * @see FiosGetSavegameList
444 std::tuple
<FiosType
, std::string
> FiosGetSavegameListCallback(SaveLoadOperation fop
, const std::string
&file
, const std::string_view ext
)
446 /* Show savegame files
447 * .SAV OpenTTD saved game
448 * .SS1 Transport Tycoon Deluxe preset game
449 * .SV1 Transport Tycoon Deluxe (Patch) saved game
450 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
452 if (StrEqualsIgnoreCase(ext
, ".sav")) {
453 return { FIOS_TYPE_FILE
, GetFileTitle(file
, SAVE_DIR
) };
456 if (fop
== SLO_LOAD
) {
457 if (StrEqualsIgnoreCase(ext
, ".ss1") || StrEqualsIgnoreCase(ext
, ".sv1") ||
458 StrEqualsIgnoreCase(ext
, ".sv2")) {
459 return { FIOS_TYPE_OLDFILE
, GetOldSaveGameName(file
) };
463 return { FIOS_TYPE_INVALID
, {} };
467 * Get a list of savegames.
468 * @param fop Purpose of collecting the list.
469 * @param show_dirs Whether to show directories.
470 * @param file_list Destination of the found files.
471 * @see FiosGetFileList
473 void FiosGetSavegameList(SaveLoadOperation fop
, bool show_dirs
, FileList
&file_list
)
475 static std::optional
<std::string
> fios_save_path
;
477 if (!fios_save_path
) fios_save_path
= FioFindDirectory(SAVE_DIR
);
479 _fios_path
= &(*fios_save_path
);
481 FiosGetFileList(fop
, show_dirs
, &FiosGetSavegameListCallback
, NO_DIRECTORY
, file_list
);
485 * Callback for FiosGetFileList. It tells if a file is a scenario or not.
486 * @param fop Purpose of collecting the list.
487 * @param file Name of the file to check.
488 * @param ext A pointer to the extension identifier inside file
489 * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario and the title of the file (if any).
490 * @see FiosGetFileList
491 * @see FiosGetScenarioList
493 std::tuple
<FiosType
, std::string
> FiosGetScenarioListCallback(SaveLoadOperation fop
, const std::string
&file
, const std::string_view ext
)
495 /* Show scenario files
496 * .SCN OpenTTD style scenario file
497 * .SV0 Transport Tycoon Deluxe (Patch) scenario
498 * .SS0 Transport Tycoon Deluxe preset scenario */
499 if (StrEqualsIgnoreCase(ext
, ".scn")) {
500 return { FIOS_TYPE_SCENARIO
, GetFileTitle(file
, SCENARIO_DIR
) };
504 if (fop
== SLO_LOAD
) {
505 if (StrEqualsIgnoreCase(ext
, ".sv0") || StrEqualsIgnoreCase(ext
, ".ss0")) {
506 return { FIOS_TYPE_OLD_SCENARIO
, GetOldSaveGameName(file
) };
510 return { FIOS_TYPE_INVALID
, {} };
514 * Get a list of scenarios.
515 * @param fop Purpose of collecting the list.
516 * @param show_dirs Whether to show directories.
517 * @param file_list Destination of the found files.
518 * @see FiosGetFileList
520 void FiosGetScenarioList(SaveLoadOperation fop
, bool show_dirs
, FileList
&file_list
)
522 static std::optional
<std::string
> fios_scn_path
;
524 /* Copy the default path on first run or on 'New Game' */
525 if (!fios_scn_path
) fios_scn_path
= FioFindDirectory(SCENARIO_DIR
);
527 _fios_path
= &(*fios_scn_path
);
529 std::string base_path
= FioFindDirectory(SCENARIO_DIR
);
530 Subdirectory subdir
= (fop
== SLO_LOAD
&& base_path
== *_fios_path
) ? SCENARIO_DIR
: NO_DIRECTORY
;
531 FiosGetFileList(fop
, show_dirs
, &FiosGetScenarioListCallback
, subdir
, file_list
);
534 std::tuple
<FiosType
, std::string
> FiosGetHeightmapListCallback(SaveLoadOperation
, const std::string
&file
, const std::string_view ext
)
536 /* Show heightmap files
537 * .PNG PNG Based heightmap files
538 * .BMP BMP Based heightmap files
541 FiosType type
= FIOS_TYPE_INVALID
;
544 if (StrEqualsIgnoreCase(ext
, ".png")) type
= FIOS_TYPE_PNG
;
545 #endif /* WITH_PNG */
547 if (StrEqualsIgnoreCase(ext
, ".bmp")) type
= FIOS_TYPE_BMP
;
549 if (type
== FIOS_TYPE_INVALID
) return { FIOS_TYPE_INVALID
, {} };
551 TarFileList::iterator it
= _tar_filelist
[SCENARIO_DIR
].find(file
);
552 if (it
!= _tar_filelist
[SCENARIO_DIR
].end()) {
553 /* If the file is in a tar and that tar is not in a heightmap
554 * directory we are for sure not supposed to see it.
555 * Examples of this are pngs part of documentation within
556 * collections of NewGRFs or 32 bpp graphics replacement PNGs.
559 for (Searchpath sp
: _valid_searchpaths
) {
560 std::string buf
= FioGetDirectory(sp
, HEIGHTMAP_DIR
);
562 if (buf
.compare(0, buf
.size(), it
->second
.tar_filename
, 0, buf
.size()) == 0) {
568 if (!match
) return { FIOS_TYPE_INVALID
, {} };
571 return { type
, GetFileTitle(file
, HEIGHTMAP_DIR
) };
575 * Get a list of heightmaps.
576 * @param fop Purpose of collecting the list.
577 * @param show_dirs Whether to show directories.
578 * @param file_list Destination of the found files.
580 void FiosGetHeightmapList(SaveLoadOperation fop
, bool show_dirs
, FileList
&file_list
)
582 static std::optional
<std::string
> fios_hmap_path
;
584 if (!fios_hmap_path
) fios_hmap_path
= FioFindDirectory(HEIGHTMAP_DIR
);
586 _fios_path
= &(*fios_hmap_path
);
588 std::string base_path
= FioFindDirectory(HEIGHTMAP_DIR
);
589 Subdirectory subdir
= base_path
== *_fios_path
? HEIGHTMAP_DIR
: NO_DIRECTORY
;
590 FiosGetFileList(fop
, show_dirs
, &FiosGetHeightmapListCallback
, subdir
, file_list
);
594 * Get the directory for screenshots.
595 * @return path to screenshots
597 const char *FiosGetScreenshotDir()
599 static std::optional
<std::string
> fios_screenshot_path
;
601 if (!fios_screenshot_path
) fios_screenshot_path
= FioFindDirectory(SCREENSHOT_DIR
);
603 return fios_screenshot_path
->c_str();
606 /** Basic data to distinguish a scenario. Used in the server list window */
607 struct ScenarioIdentifier
{
608 uint32_t scenid
; ///< ID for the scenario (generated by content).
609 MD5Hash md5sum
; ///< MD5 checksum of file.
610 std::string filename
; ///< filename of the file.
612 bool operator == (const ScenarioIdentifier
&other
) const
614 return this->scenid
== other
.scenid
&& this->md5sum
== other
.md5sum
;
617 bool operator != (const ScenarioIdentifier
&other
) const
619 return !(*this == other
);
624 * Scanner to find the unique IDs of scenarios
626 class ScenarioScanner
: protected FileScanner
, public std::vector
<ScenarioIdentifier
> {
627 bool scanned
; ///< Whether we've already scanned
630 ScenarioScanner() : scanned(false) {}
633 * Scan, but only if it's needed.
634 * @param rescan whether to force scanning even when it's not necessary
636 void Scan(bool rescan
)
638 if (this->scanned
&& !rescan
) return;
640 this->FileScanner::Scan(".id", SCENARIO_DIR
, true, true);
641 this->scanned
= true;
644 bool AddFile(const std::string
&filename
, size_t, const std::string
&) override
646 FILE *f
= FioFOpenFile(filename
, "r", SCENARIO_DIR
);
647 if (f
== nullptr) return false;
649 ScenarioIdentifier id
;
650 int fret
= fscanf(f
, "%u", &id
.scenid
);
652 if (fret
!= 1) return false;
653 id
.filename
= filename
;
656 uint8_t buffer
[1024];
659 /* open the scenario file, but first get the name.
660 * This is safe as we check on extension which
661 * must always exist. */
662 f
= FioFOpenFile(filename
.substr(0, filename
.rfind('.')), "rb", SCENARIO_DIR
, &size
);
663 if (f
== nullptr) return false;
665 /* calculate md5sum */
666 while ((len
= fread(buffer
, 1, (size
> sizeof(buffer
)) ? sizeof(buffer
) : size
, f
)) != 0 && size
!= 0) {
668 checksum
.Append(buffer
, len
);
670 checksum
.Finish(id
.md5sum
);
679 /** Scanner for scenarios */
680 static ScenarioScanner _scanner
;
683 * Find a given scenario based on its unique ID.
684 * @param ci The content info to compare it to.
685 * @param md5sum Whether to look at the md5sum or the id.
686 * @return The filename of the file, else \c nullptr.
688 const char *FindScenario(const ContentInfo
*ci
, bool md5sum
)
690 _scanner
.Scan(false);
692 for (ScenarioIdentifier
&id
: _scanner
) {
693 if (md5sum
? (id
.md5sum
== ci
->md5sum
)
694 : (id
.scenid
== ci
->unique_id
)) {
695 return id
.filename
.c_str();
703 * Check whether we've got a given scenario based on its unique ID.
704 * @param ci The content info to compare it to.
705 * @param md5sum Whether to look at the md5sum or the id.
706 * @return True iff we've got the scenario.
708 bool HasScenario(const ContentInfo
*ci
, bool md5sum
)
710 return (FindScenario(ci
, md5sum
) != nullptr);
714 * Force a (re)scan of the scenarios.
722 * Constructs FiosNumberedSaveName. Initial number is the most recent save, or -1 if not found.
723 * @param prefix The prefix to use to generate a filename.
725 FiosNumberedSaveName::FiosNumberedSaveName(const std::string
&prefix
) : prefix(prefix
), number(-1)
727 static std::optional
<std::string
> _autosave_path
;
728 if (!_autosave_path
) _autosave_path
= FioFindDirectory(AUTOSAVE_DIR
);
730 static std::string _prefix
; ///< Static as the lambda needs access to it.
732 /* Callback for FiosFileScanner. */
733 static FiosGetTypeAndNameProc
*proc
= [](SaveLoadOperation
, const std::string
&file
, const std::string_view ext
) {
734 if (StrEqualsIgnoreCase(ext
, ".sav") && file
.starts_with(_prefix
)) return std::tuple(FIOS_TYPE_FILE
, std::string
{});
735 return std::tuple(FIOS_TYPE_INVALID
, std::string
{});
738 /* Prefix to check in the callback. */
739 _prefix
= *_autosave_path
+ this->prefix
;
741 /* Get the save list. */
743 FiosFileScanner
scanner(SLO_SAVE
, proc
, list
);
744 scanner
.Scan(".sav", _autosave_path
->c_str(), false);
746 /* Find the number for the most recent save, if any. */
747 if (list
.begin() != list
.end()) {
748 SortingBits order
= _savegame_sort_order
;
749 _savegame_sort_order
= SORT_BY_DATE
| SORT_DESCENDING
;
750 std::sort(list
.begin(), list
.end());
751 _savegame_sort_order
= order
;
753 std::string_view name
= list
.begin()->title
;
754 std::from_chars(name
.data() + this->prefix
.size(), name
.data() + name
.size(), this->number
);
759 * Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
760 * @return A filename in format "<prefix><number>.sav".
762 std::string
FiosNumberedSaveName::Filename()
764 if (++this->number
>= _settings_client
.gui
.max_num_autosaves
) this->number
= 0;
765 return fmt::format("{}{}.sav", this->prefix
, this->number
);
769 * Generate an extension for a savegame name.
770 * @return An extension in format "-<prefix>.sav".
772 std::string
FiosNumberedSaveName::Extension()
774 return fmt::format("-{}.sav", this->prefix
);