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"
31 #include "table/strings.h"
33 #include "safeguards.h"
35 /* Variables to display file lists */
36 static std::string
*_fios_path
= nullptr;
37 SortingBits _savegame_sort_order
= SORT_BY_DATE
| SORT_DESCENDING
;
39 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
40 extern bool FiosIsRoot(const char *path
);
41 extern bool FiosIsValidFile(const char *path
, const struct dirent
*ent
, struct stat
*sb
);
42 extern bool FiosIsHiddenFile(const struct dirent
*ent
);
43 extern void FiosGetDrives(FileList
&file_list
);
44 extern bool FiosGetDiskFreeSpace(const char *path
, uint64
*tot
);
46 /* get the name of an oldstyle savegame */
47 extern void GetOldSaveGameName(const std::string
&file
, char *title
, const char *last
);
50 * Compare two FiosItem's. Used with sort when sorting the file list.
51 * @param other The FiosItem to compare to.
52 * @return for ascending order: returns true if da < db. Vice versa for descending order.
54 bool FiosItem::operator< (const FiosItem
&other
) const
58 if ((_savegame_sort_order
& SORT_BY_NAME
) == 0 && (*this).mtime
!= other
.mtime
) {
59 r
= (*this).mtime
- other
.mtime
;
61 r
= strnatcmp((*this).title
, other
.title
);
63 if (r
== 0) return false;
64 return (_savegame_sort_order
& SORT_DESCENDING
) ? r
> 0 : r
< 0;
68 * Construct a file list with the given kind of files, for the stated purpose.
69 * @param abstract_filetype Kind of files to collect.
70 * @param fop Purpose of the collection, either #SLO_LOAD or #SLO_SAVE.
72 void FileList::BuildFileList(AbstractFileType abstract_filetype
, SaveLoadOperation fop
)
76 assert(fop
== SLO_LOAD
|| fop
== SLO_SAVE
);
77 switch (abstract_filetype
) {
82 FiosGetSavegameList(fop
, *this);
86 FiosGetScenarioList(fop
, *this);
90 FiosGetHeightmapList(fop
, *this);
99 * Find file information of a file by its name from the file list.
100 * @param file The filename to return information about. Can be the actual name
101 * or a numbered entry into the filename list.
102 * @return The information on the file, or \c nullptr if the file is not available.
104 const FiosItem
*FileList::FindItem(const char *file
)
106 for (const auto &it
: *this) {
107 const FiosItem
*item
= &it
;
108 if (strcmp(file
, item
->name
) == 0) return item
;
109 if (strcmp(file
, item
->title
) == 0) return item
;
112 /* If no name matches, try to parse it as number */
114 int i
= strtol(file
, &endptr
, 10);
115 if (file
== endptr
|| *endptr
!= '\0') i
= -1;
117 if (IsInsideMM(i
, 0, this->size())) return &this->at(i
);
119 /* As a last effort assume it is an OpenTTD savegame and
120 * that the ".sav" part was not given. */
121 char long_file
[MAX_PATH
];
122 seprintf(long_file
, lastof(long_file
), "%s.sav", file
);
123 for (const auto &it
: *this) {
124 const FiosItem
*item
= &it
;
125 if (strcmp(long_file
, item
->name
) == 0) return item
;
126 if (strcmp(long_file
, item
->title
) == 0) return item
;
133 * Get descriptive texts. Returns the path and free space
135 * @param path string describing the path
136 * @param total_free total free space in megabytes, optional (can be nullptr)
137 * @return StringID describing the path (free space or failure)
139 StringID
FiosGetDescText(const char **path
, uint64
*total_free
)
141 *path
= _fios_path
->c_str();
142 return FiosGetDiskFreeSpace(*path
, total_free
) ? STR_SAVELOAD_BYTES_FREE
: STR_ERROR_UNABLE_TO_READ_DRIVE
;
146 * Browse to a new path based on the passed \a item, starting at #_fios_path.
147 * @param *item Item telling us what to do.
148 * @return A filename w/path if we reached a file, otherwise \c nullptr.
150 const char *FiosBrowseTo(const FiosItem
*item
)
152 switch (item
->type
) {
153 case FIOS_TYPE_DRIVE
:
154 #if defined(_WIN32) || defined(__OS2__)
155 assert(_fios_path
!= nullptr);
156 *_fios_path
= std::string
{ item
->title
[0] } + ":" PATHSEP
;
160 case FIOS_TYPE_INVALID
:
163 case FIOS_TYPE_PARENT
: {
164 assert(_fios_path
!= nullptr);
165 auto s
= _fios_path
->find_last_of(PATHSEPCHAR
);
166 if (s
!= std::string::npos
&& s
!= 0) {
167 _fios_path
->erase(s
); // Remove last path separator character, so we can go up one level.
170 s
= _fios_path
->find_last_of(PATHSEPCHAR
);
171 if (s
!= std::string::npos
) {
172 _fios_path
->erase(s
+ 1); // go up a directory
178 assert(_fios_path
!= nullptr);
179 *_fios_path
+= item
->name
;
180 *_fios_path
+= PATHSEP
;
183 case FIOS_TYPE_DIRECT
:
184 assert(_fios_path
!= nullptr);
185 *_fios_path
= item
->name
;
189 case FIOS_TYPE_OLDFILE
:
190 case FIOS_TYPE_SCENARIO
:
191 case FIOS_TYPE_OLD_SCENARIO
:
201 * Construct a filename from its components in destination buffer \a buf.
202 * @param path Directory path, may be \c nullptr.
203 * @param name Filename.
204 * @param ext Filename extension (use \c "" for no extension).
205 * @return The completed filename.
207 static std::string
FiosMakeFilename(const std::string
*path
, const char *name
, const char *ext
)
211 if (path
!= nullptr) {
213 /* Remove trailing path separator, if present */
214 if (!buf
.empty() && buf
.back() == PATHSEPCHAR
) buf
.pop_back();
217 /* Don't append the extension if it is already there */
218 const char *period
= strrchr(name
, '.');
219 if (period
!= nullptr && strcasecmp(period
, ext
) == 0) ext
= "";
221 return buf
+ PATHSEP
+ name
+ ext
;
225 * Make a save game or scenario filename from a name.
226 * @param buf Destination buffer for saving the filename.
227 * @param name Name of the file.
228 * @param last Last element of buffer \a buf.
229 * @return The completed filename.
231 std::string
FiosMakeSavegameName(const char *name
)
233 const char *extension
= (_game_mode
== GM_EDITOR
) ? ".scn" : ".sav";
235 return FiosMakeFilename(_fios_path
, name
, extension
);
239 * Construct a filename for a height map.
240 * @param name Filename.
241 * @return The completed filename.
243 std::string
FiosMakeHeightmapName(const char *name
)
245 std::string
ext(".");
246 ext
+= GetCurrentScreenshotExtension();
248 return FiosMakeFilename(_fios_path
, name
, ext
.c_str());
253 * @param name Filename to delete.
254 * @return Whether the file deletion was successful.
256 bool FiosDelete(const char *name
)
258 std::string filename
= FiosMakeSavegameName(name
);
259 return unlink(filename
.c_str()) == 0;
262 typedef FiosType
fios_getlist_callback_proc(SaveLoadOperation fop
, const std::string
&filename
, const char *ext
, char *title
, const char *last
);
265 * Scanner to scan for a particular type of FIOS file.
267 class FiosFileScanner
: public FileScanner
{
268 SaveLoadOperation fop
; ///< The kind of file we are looking for.
269 fios_getlist_callback_proc
*callback_proc
; ///< Callback to check whether the file may be added
270 FileList
&file_list
; ///< Destination of the found files.
274 * @param fop Purpose of collecting the list.
275 * @param callback_proc The function that is called where you need to do the filtering.
276 * @param file_list Destination of the found files.
278 FiosFileScanner(SaveLoadOperation fop
, fios_getlist_callback_proc
*callback_proc
, FileList
&file_list
) :
279 fop(fop
), callback_proc(callback_proc
), file_list(file_list
)
282 bool AddFile(const std::string
&filename
, size_t basepath_length
, const std::string
&tar_filename
) override
;
286 * Try to add a fios item set with the given filename.
287 * @param filename the full path to the file to read
288 * @param basepath_length amount of characters to chop of before to get a relative filename
289 * @return true if the file is added.
291 bool FiosFileScanner::AddFile(const std::string
&filename
, size_t basepath_length
, const std::string
&tar_filename
)
293 auto sep
= filename
.rfind('.');
294 if (sep
== std::string::npos
) return false;
295 std::string ext
= filename
.substr(sep
);
298 fios_title
[0] = '\0'; // reset the title;
300 FiosType type
= this->callback_proc(this->fop
, filename
, ext
.c_str(), fios_title
, lastof(fios_title
));
301 if (type
== FIOS_TYPE_INVALID
) return false;
303 for (const auto &fios
: file_list
) {
304 if (filename
== fios
.name
) return false;
307 FiosItem
*fios
= &file_list
.emplace_back();
309 // Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
310 HANDLE fh
= CreateFile(OTTD2FS(filename
).c_str(), GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, nullptr, OPEN_EXISTING
, 0, nullptr);
312 if (fh
!= INVALID_HANDLE_VALUE
) {
314 ULARGE_INTEGER ft_int64
;
316 if (GetFileTime(fh
, nullptr, nullptr, &ft
) != 0) {
317 ft_int64
.HighPart
= ft
.dwHighDateTime
;
318 ft_int64
.LowPart
= ft
.dwLowDateTime
;
320 // Convert from hectonanoseconds since 01/01/1601 to seconds since 01/01/1970
321 fios
->mtime
= ft_int64
.QuadPart
/ 10000000ULL - 11644473600ULL;
329 if (stat(filename
.c_str(), &sb
) == 0) {
330 fios
->mtime
= sb
.st_mtime
;
337 strecpy(fios
->name
, filename
.c_str(), lastof(fios
->name
));
339 /* If the file doesn't have a title, use its filename */
340 const char *t
= fios_title
;
341 if (StrEmpty(fios_title
)) {
342 auto ps
= filename
.rfind(PATHSEPCHAR
);
343 t
= filename
.c_str() + (ps
== std::string::npos
? 0 : ps
+ 1);
345 strecpy(fios
->title
, t
, lastof(fios
->title
));
346 StrMakeValidInPlace(fios
->title
, lastof(fios
->title
));
353 * Fill the list of the files in a directory, according to some arbitrary rule.
354 * @param fop Purpose of collecting the list.
355 * @param callback_proc The function that is called where you need to do the filtering.
356 * @param subdir The directory from where to start (global) searching.
357 * @param file_list Destination of the found files.
359 static void FiosGetFileList(SaveLoadOperation fop
, fios_getlist_callback_proc
*callback_proc
, Subdirectory subdir
, FileList
&file_list
)
362 struct dirent
*dirent
;
366 char d_name
[sizeof(fios
->name
)];
370 assert(_fios_path
!= nullptr);
372 /* A parent directory link exists if we are not in the root directory */
373 if (!FiosIsRoot(_fios_path
->c_str())) {
374 fios
= &file_list
.emplace_back();
375 fios
->type
= FIOS_TYPE_PARENT
;
377 strecpy(fios
->name
, "..", lastof(fios
->name
));
378 SetDParamStr(0, "..");
379 GetString(fios
->title
, STR_SAVELOAD_PARENT_DIRECTORY
, lastof(fios
->title
));
382 /* Show subdirectories */
383 if ((dir
= ttd_opendir(_fios_path
->c_str())) != nullptr) {
384 while ((dirent
= readdir(dir
)) != nullptr) {
385 strecpy(d_name
, FS2OTTD(dirent
->d_name
).c_str(), lastof(d_name
));
387 /* found file must be directory, but not '.' or '..' */
388 if (FiosIsValidFile(_fios_path
->c_str(), dirent
, &sb
) && S_ISDIR(sb
.st_mode
) &&
389 (!FiosIsHiddenFile(dirent
) || strncasecmp(d_name
, PERSONAL_DIR
, strlen(d_name
)) == 0) &&
390 strcmp(d_name
, ".") != 0 && strcmp(d_name
, "..") != 0) {
391 fios
= &file_list
.emplace_back();
392 fios
->type
= FIOS_TYPE_DIR
;
394 strecpy(fios
->name
, d_name
, lastof(fios
->name
));
395 std::string dirname
= std::string(d_name
) + PATHSEP
;
396 SetDParamStr(0, dirname
);
397 GetString(fios
->title
, STR_SAVELOAD_DIRECTORY
, lastof(fios
->title
));
398 StrMakeValidInPlace(fios
->title
, lastof(fios
->title
));
404 /* Sort the subdirs always by name, ascending, remember user-sorting order */
406 SortingBits order
= _savegame_sort_order
;
407 _savegame_sort_order
= SORT_BY_NAME
| SORT_ASCENDING
;
408 std::sort(file_list
.begin(), file_list
.end());
409 _savegame_sort_order
= order
;
412 /* This is where to start sorting for the filenames */
413 sort_start
= file_list
.size();
416 FiosFileScanner
scanner(fop
, callback_proc
, file_list
);
417 if (subdir
== NO_DIRECTORY
) {
418 scanner
.Scan(nullptr, _fios_path
->c_str(), false);
420 scanner
.Scan(nullptr, subdir
, true, true);
423 std::sort(file_list
.begin() + sort_start
, file_list
.end());
426 FiosGetDrives(file_list
);
428 file_list
.shrink_to_fit();
432 * Get the title of a file, which (if exists) is stored in a file named
433 * the same as the data file but with '.title' added to it.
434 * @param file filename to get the title for
435 * @param title the title buffer to fill
436 * @param last the last element in the title buffer
437 * @param subdir the sub directory to search in
439 static void GetFileTitle(const std::string
&file
, char *title
, const char *last
, Subdirectory subdir
)
441 std::string buf
= file
;
444 FILE *f
= FioFOpenFile(buf
, "r", subdir
);
445 if (f
== nullptr) return;
447 size_t read
= fread(title
, 1, last
- title
, f
);
448 assert(title
+ read
<= last
);
450 StrMakeValidInPlace(title
, last
);
455 * Callback for FiosGetFileList. It tells if a file is a savegame or not.
456 * @param fop Purpose of collecting the list.
457 * @param file Name of the file to check.
458 * @param ext A pointer to the extension identifier inside file
459 * @param title Buffer if a callback wants to lookup the title of the file; nullptr to skip the lookup
460 * @param last Last available byte in buffer (to prevent buffer overflows); not used when title == nullptr
461 * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame
462 * @see FiosGetFileList
463 * @see FiosGetSavegameList
465 FiosType
FiosGetSavegameListCallback(SaveLoadOperation fop
, const std::string
&file
, const char *ext
, char *title
, const char *last
)
467 /* Show savegame files
468 * .SAV OpenTTD saved game
469 * .SS1 Transport Tycoon Deluxe preset game
470 * .SV1 Transport Tycoon Deluxe (Patch) saved game
471 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
473 /* Don't crash if we supply no extension */
474 if (ext
== nullptr) return FIOS_TYPE_INVALID
;
476 if (strcasecmp(ext
, ".sav") == 0) {
477 GetFileTitle(file
, title
, last
, SAVE_DIR
);
478 return FIOS_TYPE_FILE
;
481 if (fop
== SLO_LOAD
) {
482 if (strcasecmp(ext
, ".ss1") == 0 || strcasecmp(ext
, ".sv1") == 0 ||
483 strcasecmp(ext
, ".sv2") == 0) {
484 if (title
!= nullptr) GetOldSaveGameName(file
, title
, last
);
485 return FIOS_TYPE_OLDFILE
;
489 return FIOS_TYPE_INVALID
;
493 * Get a list of savegames.
494 * @param fop Purpose of collecting the list.
495 * @param file_list Destination of the found files.
496 * @see FiosGetFileList
498 void FiosGetSavegameList(SaveLoadOperation fop
, FileList
&file_list
)
500 static std::optional
<std::string
> fios_save_path
;
502 if (!fios_save_path
) fios_save_path
= FioFindDirectory(SAVE_DIR
);
504 _fios_path
= &(*fios_save_path
);
506 FiosGetFileList(fop
, &FiosGetSavegameListCallback
, NO_DIRECTORY
, file_list
);
510 * Callback for FiosGetFileList. It tells if a file is a scenario or not.
511 * @param fop Purpose of collecting the list.
512 * @param file Name of the file to check.
513 * @param ext A pointer to the extension identifier inside file
514 * @param title Buffer if a callback wants to lookup the title of the file
515 * @param last Last available byte in buffer (to prevent buffer overflows)
516 * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario
517 * @see FiosGetFileList
518 * @see FiosGetScenarioList
520 static FiosType
FiosGetScenarioListCallback(SaveLoadOperation fop
, const std::string
&file
, const char *ext
, char *title
, const char *last
)
522 /* Show scenario files
523 * .SCN OpenTTD style scenario file
524 * .SV0 Transport Tycoon Deluxe (Patch) scenario
525 * .SS0 Transport Tycoon Deluxe preset scenario */
526 if (strcasecmp(ext
, ".scn") == 0) {
527 GetFileTitle(file
, title
, last
, SCENARIO_DIR
);
528 return FIOS_TYPE_SCENARIO
;
531 if (fop
== SLO_LOAD
) {
532 if (strcasecmp(ext
, ".sv0") == 0 || strcasecmp(ext
, ".ss0") == 0 ) {
533 GetOldSaveGameName(file
, title
, last
);
534 return FIOS_TYPE_OLD_SCENARIO
;
538 return FIOS_TYPE_INVALID
;
542 * Get a list of scenarios.
543 * @param fop Purpose of collecting the list.
544 * @param file_list Destination of the found files.
545 * @see FiosGetFileList
547 void FiosGetScenarioList(SaveLoadOperation fop
, FileList
&file_list
)
549 static std::optional
<std::string
> fios_scn_path
;
551 /* Copy the default path on first run or on 'New Game' */
552 if (!fios_scn_path
) fios_scn_path
= FioFindDirectory(SCENARIO_DIR
);
554 _fios_path
= &(*fios_scn_path
);
556 std::string base_path
= FioFindDirectory(SCENARIO_DIR
);
557 Subdirectory subdir
= (fop
== SLO_LOAD
&& base_path
== *_fios_path
) ? SCENARIO_DIR
: NO_DIRECTORY
;
558 FiosGetFileList(fop
, &FiosGetScenarioListCallback
, subdir
, file_list
);
561 static FiosType
FiosGetHeightmapListCallback(SaveLoadOperation fop
, const std::string
&file
, const char *ext
, char *title
, const char *last
)
563 /* Show heightmap files
564 * .PNG PNG Based heightmap files
565 * .BMP BMP Based heightmap files
568 FiosType type
= FIOS_TYPE_INVALID
;
571 if (strcasecmp(ext
, ".png") == 0) type
= FIOS_TYPE_PNG
;
572 #endif /* WITH_PNG */
574 if (strcasecmp(ext
, ".bmp") == 0) type
= FIOS_TYPE_BMP
;
576 if (type
== FIOS_TYPE_INVALID
) return FIOS_TYPE_INVALID
;
578 TarFileList::iterator it
= _tar_filelist
[SCENARIO_DIR
].find(file
);
579 if (it
!= _tar_filelist
[SCENARIO_DIR
].end()) {
580 /* If the file is in a tar and that tar is not in a heightmap
581 * directory we are for sure not supposed to see it.
582 * Examples of this are pngs part of documentation within
583 * collections of NewGRFs or 32 bpp graphics replacement PNGs.
586 for (Searchpath sp
: _valid_searchpaths
) {
587 std::string buf
= FioGetDirectory(sp
, HEIGHTMAP_DIR
);
589 if (buf
.compare(0, buf
.size(), it
->second
.tar_filename
, 0, buf
.size()) == 0) {
595 if (!match
) return FIOS_TYPE_INVALID
;
598 GetFileTitle(file
, title
, last
, HEIGHTMAP_DIR
);
604 * Get a list of heightmaps.
605 * @param fop Purpose of collecting the list.
606 * @param file_list Destination of the found files.
608 void FiosGetHeightmapList(SaveLoadOperation fop
, FileList
&file_list
)
610 static std::optional
<std::string
> fios_hmap_path
;
612 if (!fios_hmap_path
) fios_hmap_path
= FioFindDirectory(HEIGHTMAP_DIR
);
614 _fios_path
= &(*fios_hmap_path
);
616 std::string base_path
= FioFindDirectory(HEIGHTMAP_DIR
);
617 Subdirectory subdir
= base_path
== *_fios_path
? HEIGHTMAP_DIR
: NO_DIRECTORY
;
618 FiosGetFileList(fop
, &FiosGetHeightmapListCallback
, subdir
, file_list
);
622 * Get the directory for screenshots.
623 * @return path to screenshots
625 const char *FiosGetScreenshotDir()
627 static std::optional
<std::string
> fios_screenshot_path
;
629 if (!fios_screenshot_path
) fios_screenshot_path
= FioFindDirectory(SCREENSHOT_DIR
);
631 return fios_screenshot_path
->c_str();
634 /** Basic data to distinguish a scenario. Used in the server list window */
635 struct ScenarioIdentifier
{
636 uint32 scenid
; ///< ID for the scenario (generated by content).
637 uint8 md5sum
[16]; ///< MD5 checksum of file.
638 char filename
[MAX_PATH
]; ///< filename of the file.
640 bool operator == (const ScenarioIdentifier
&other
) const
642 return this->scenid
== other
.scenid
&&
643 memcmp(this->md5sum
, other
.md5sum
, sizeof(this->md5sum
)) == 0;
646 bool operator != (const ScenarioIdentifier
&other
) const
648 return !(*this == other
);
653 * Scanner to find the unique IDs of scenarios
655 class ScenarioScanner
: protected FileScanner
, public std::vector
<ScenarioIdentifier
> {
656 bool scanned
; ///< Whether we've already scanned
659 ScenarioScanner() : scanned(false) {}
662 * Scan, but only if it's needed.
663 * @param rescan whether to force scanning even when it's not necessary
665 void Scan(bool rescan
)
667 if (this->scanned
&& !rescan
) return;
669 this->FileScanner::Scan(".id", SCENARIO_DIR
, true, true);
670 this->scanned
= true;
673 bool AddFile(const std::string
&filename
, size_t basepath_length
, const std::string
&tar_filename
) override
675 FILE *f
= FioFOpenFile(filename
, "r", SCENARIO_DIR
);
676 if (f
== nullptr) return false;
678 ScenarioIdentifier id
;
679 int fret
= fscanf(f
, "%u", &id
.scenid
);
681 if (fret
!= 1) return false;
682 strecpy(id
.filename
, filename
.c_str(), lastof(id
.filename
));
688 /* open the scenario file, but first get the name.
689 * This is safe as we check on extension which
690 * must always exist. */
691 f
= FioFOpenFile(filename
.substr(0, filename
.rfind('.')), "rb", SCENARIO_DIR
, &size
);
692 if (f
== nullptr) return false;
694 /* calculate md5sum */
695 while ((len
= fread(buffer
, 1, (size
> sizeof(buffer
)) ? sizeof(buffer
) : size
, f
)) != 0 && size
!= 0) {
697 checksum
.Append(buffer
, len
);
699 checksum
.Finish(id
.md5sum
);
708 /** Scanner for scenarios */
709 static ScenarioScanner _scanner
;
712 * Find a given scenario based on its unique ID.
713 * @param ci The content info to compare it to.
714 * @param md5sum Whether to look at the md5sum or the id.
715 * @return The filename of the file, else \c nullptr.
717 const char *FindScenario(const ContentInfo
*ci
, bool md5sum
)
719 _scanner
.Scan(false);
721 for (ScenarioIdentifier
&id
: _scanner
) {
722 if (md5sum
? (memcmp(id
.md5sum
, ci
->md5sum
, sizeof(id
.md5sum
)) == 0)
723 : (id
.scenid
== ci
->unique_id
)) {
732 * Check whether we've got a given scenario based on its unique ID.
733 * @param ci The content info to compare it to.
734 * @param md5sum Whether to look at the md5sum or the id.
735 * @return True iff we've got the scenario.
737 bool HasScenario(const ContentInfo
*ci
, bool md5sum
)
739 return (FindScenario(ci
, md5sum
) != nullptr);
743 * Force a (re)scan of the scenarios.
751 * Constructs FiosNumberedSaveName. Initial number is the most recent save, or -1 if not found.
752 * @param prefix The prefix to use to generate a filename.
754 FiosNumberedSaveName::FiosNumberedSaveName(const std::string
&prefix
) : prefix(prefix
), number(-1)
756 static std::optional
<std::string
> _autosave_path
;
757 if (!_autosave_path
) _autosave_path
= FioFindDirectory(AUTOSAVE_DIR
);
759 static std::string _prefix
; ///< Static as the lambda needs access to it.
761 /* Callback for FiosFileScanner. */
762 static fios_getlist_callback_proc
*proc
= [](SaveLoadOperation fop
, const std::string
&file
, const char *ext
, char *title
, const char *last
) {
763 if (strcasecmp(ext
, ".sav") == 0 && StrStartsWith(file
, _prefix
)) return FIOS_TYPE_FILE
;
764 return FIOS_TYPE_INVALID
;
767 /* Prefix to check in the callback. */
768 _prefix
= *_autosave_path
+ this->prefix
;
770 /* Get the save list. */
772 FiosFileScanner
scanner(SLO_SAVE
, proc
, list
);
773 scanner
.Scan(".sav", _autosave_path
->c_str(), false);
775 /* Find the number for the most recent save, if any. */
776 if (list
.begin() != list
.end()) {
777 SortingBits order
= _savegame_sort_order
;
778 _savegame_sort_order
= SORT_BY_DATE
| SORT_DESCENDING
;
779 std::sort(list
.begin(), list
.end());
780 _savegame_sort_order
= order
;
782 std::string_view name
= list
.begin()->title
;
783 std::from_chars(name
.data() + this->prefix
.size(), name
.data() + name
.size(), this->number
);
788 * Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
789 * @return A filename in format "<prefix><number>.sav".
791 std::string
FiosNumberedSaveName::Filename()
793 if (++this->number
>= _settings_client
.gui
.max_num_autosaves
) this->number
= 0;
794 return fmt::format("{}{}.sav", this->prefix
, this->number
);
798 * Generate an extension for a savegame name.
799 * @return An extension in format "-<prefix>.sav".
801 std::string
FiosNumberedSaveName::Extension()
803 return fmt::format("-{}.sav", this->prefix
);