OpenTTD Source 20250529-master-g10c159a79f
fios.cpp
Go to the documentation of this file.
1/*
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/>.
6 */
7
13#include "stdafx.h"
14#include "3rdparty/md5/md5.h"
16#include "fileio_func.h"
17#include "fios.h"
19#include "screenshot.h"
20#include "string_func.h"
21#include "strings_func.h"
22#include "tar_type.h"
23#include <sys/stat.h>
24#include <charconv>
25#include <filesystem>
26
27#include "table/strings.h"
28
29#include "safeguards.h"
30
31/* Variables to display file lists */
32static std::string *_fios_path = nullptr;
33SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
34
35/* OS-specific functions are taken from their respective files (win32/unix .c) */
36extern bool FiosIsRoot(const std::string &path);
37extern bool FiosIsHiddenFile(const std::filesystem::path &path);
38extern void FiosGetDrives(FileList &file_list);
39
40/* get the name of an oldstyle savegame */
41extern std::string GetOldSaveGameName(std::string_view file);
42
48bool FiosItem::operator< (const FiosItem &other) const
49{
50 int r = false;
51
52 if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
53 r = ClampTo<int32_t>(this->mtime - other.mtime);
54 } else {
55 r = StrNaturalCompare((*this).title, other.title);
56 }
57 if (r == 0) return false;
58 return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
59}
60
67void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop, bool show_dirs)
68{
69 this->clear();
70
71 assert(fop == SLO_LOAD || fop == SLO_SAVE);
72 switch (abstract_filetype) {
73 case FT_NONE:
74 break;
75
76 case FT_SAVEGAME:
77 FiosGetSavegameList(fop, show_dirs, *this);
78 break;
79
80 case FT_SCENARIO:
81 FiosGetScenarioList(fop, show_dirs, *this);
82 break;
83
84 case FT_HEIGHTMAP:
85 FiosGetHeightmapList(fop, show_dirs, *this);
86 break;
87
88 case FT_TOWN_DATA:
89 FiosGetTownDataList(fop, show_dirs, *this);
90 break;
91
92 default:
93 NOT_REACHED();
94 }
95}
96
103const FiosItem *FileList::FindItem(std::string_view file)
104{
105 for (const auto &it : *this) {
106 const FiosItem *item = &it;
107 if (file == item->name) return item;
108 if (file == item->title) return item;
109 }
110
111 /* If no name matches, try to parse it as number */
112 StringConsumer consumer{file};
113 auto number = consumer.TryReadIntegerBase<int>(10);
114 if (number.has_value() && !consumer.AnyBytesLeft() && IsInsideMM(*number, 0, this->size())) return &this->at(*number);
115
116 /* As a last effort assume it is an OpenTTD savegame and
117 * that the ".sav" part was not given. */
118 std::string long_file(file);
119 long_file += ".sav";
120 for (const auto &it : *this) {
121 const FiosItem *item = &it;
122 if (long_file == item->name) return item;
123 if (long_file == item->title) return item;
124 }
125
126 return nullptr;
127}
128
133{
134 return *_fios_path;
135}
136
142bool FiosBrowseTo(const FiosItem *item)
143{
144 switch (item->type.detailed) {
145 case DFT_FIOS_DRIVE:
146#if defined(_WIN32)
147 assert(_fios_path != nullptr);
148 *_fios_path = std::string{ item->title, 0, 1 } + ":" PATHSEP;
149#endif
150 break;
151
152 case DFT_INVALID:
153 break;
154
155 case DFT_FIOS_PARENT: {
156 assert(_fios_path != nullptr);
157 auto s = _fios_path->find_last_of(PATHSEPCHAR);
158 if (s != std::string::npos && s != 0) {
159 _fios_path->erase(s); // Remove last path separator character, so we can go up one level.
160 }
161
162 s = _fios_path->find_last_of(PATHSEPCHAR);
163 if (s != std::string::npos) {
164 _fios_path->erase(s + 1); // go up a directory
165 }
166 break;
167 }
168
169 case DFT_FIOS_DIR:
170 assert(_fios_path != nullptr);
171 *_fios_path += item->name;
172 *_fios_path += PATHSEP;
173 break;
174
175 case DFT_FIOS_DIRECT:
176 assert(_fios_path != nullptr);
177 *_fios_path = item->name;
178 break;
179
180 default:
181 return false;
182 }
183
184 return true;
185}
186
194static std::string FiosMakeFilename(const std::string *path, std::string_view name, std::string_view ext)
195{
196 std::string_view base_path;
197
198 if (path != nullptr) {
199 base_path = *path;
200 /* Remove trailing path separator, if present */
201 if (!base_path.empty() && base_path.back() == PATHSEPCHAR) base_path.remove_suffix(1);
202 }
203
204 /* Don't append the extension if it is already there */
205 auto period = name.find_last_of('.');
206 if (period != std::string_view::npos && StrEqualsIgnoreCase(name.substr(period), ext)) ext = "";
207
208 return fmt::format("{}{}{}{}", base_path, PATHSEP, name, ext);
209}
210
216std::string FiosMakeSavegameName(std::string_view name)
217{
218 std::string_view extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
219
220 return FiosMakeFilename(_fios_path, name, extension);
221}
222
228std::string FiosMakeHeightmapName(std::string_view name)
229{
230 return FiosMakeFilename(_fios_path, name, fmt::format(".{}", GetCurrentScreenshotExtension()));
231}
232
238bool FiosDelete(std::string_view name)
239{
240 return FioRemove(FiosMakeSavegameName(name));
241}
242
243typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(SaveLoadOperation fop, std::string_view filename, std::string_view ext);
244
250 FiosGetTypeAndNameProc *callback_proc;
252public:
262
263 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
264};
265
271bool FiosFileScanner::AddFile(const std::string &filename, size_t, const std::string &)
272{
273 auto sep = filename.rfind('.');
274 if (sep == std::string::npos) return false;
275 std::string ext = filename.substr(sep);
276
277 auto [type, title] = this->callback_proc(this->fop, filename, ext);
278 if (type == FIOS_TYPE_INVALID) return false;
279
280 for (const auto &fios : file_list) {
281 if (filename == fios.name) return false;
282 }
283
284 FiosItem *fios = &file_list.emplace_back();
285
286 std::error_code error_code;
287 auto write_time = std::filesystem::last_write_time(OTTD2FS(filename), error_code);
288 if (error_code) {
289 fios->mtime = 0;
290 } else {
291 fios->mtime = std::chrono::duration_cast<std::chrono::milliseconds>(write_time.time_since_epoch()).count();
292 }
293
294 fios->type = type;
295 fios->name = filename;
296
297 /* If the file doesn't have a title, use its filename */
298 if (title.empty()) {
299 auto ps = filename.rfind(PATHSEPCHAR);
300 fios->title = StrMakeValid(filename.substr((ps == std::string::npos ? 0 : ps + 1)));
301 } else {
302 fios->title = StrMakeValid(title);
303 };
304
305 return true;
306}
307
308
317static void FiosGetFileList(SaveLoadOperation fop, bool show_dirs, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
318{
319 size_t sort_start = 0;
320
321 file_list.clear();
322
323 assert(_fios_path != nullptr);
324
325 if (show_dirs) {
326 /* A parent directory link exists if we are not in the root directory */
327 if (!FiosIsRoot(*_fios_path)) {
328 FiosItem &fios = file_list.emplace_back();
329 fios.type = FIOS_TYPE_PARENT;
330 fios.mtime = 0;
331 fios.name = "..";
332 fios.title = GetString(STR_SAVELOAD_PARENT_DIRECTORY, ".."sv);
333 sort_start = file_list.size();
334 }
335
336 /* Show subdirectories */
337 std::error_code error_code;
338 for (const auto &dir_entry : std::filesystem::directory_iterator(OTTD2FS(*_fios_path), error_code)) {
339 if (!dir_entry.is_directory()) continue;
340 if (FiosIsHiddenFile(dir_entry) && dir_entry.path().filename() != PERSONAL_DIR) continue;
341
342 FiosItem &fios = file_list.emplace_back();
343 fios.type = FIOS_TYPE_DIR;
344 fios.mtime = 0;
345 fios.name = FS2OTTD(dir_entry.path().filename().native());
346 fios.title = GetString(STR_SAVELOAD_DIRECTORY, fios.name + PATHSEP);
347 }
348
349 /* Sort the subdirs always by name, ascending, remember user-sorting order */
350 SortingBits order = _savegame_sort_order;
351 _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
352 std::sort(file_list.begin() + sort_start, file_list.end());
353 _savegame_sort_order = order;
354 }
355
356 /* This is where to start sorting for the filenames */
357 sort_start = file_list.size();
358
359 /* Show files */
360 FiosFileScanner scanner(fop, callback_proc, file_list);
361 if (subdir == NO_DIRECTORY) {
362 scanner.Scan({}, *_fios_path, false);
363 } else {
364 scanner.Scan({}, subdir, true, true);
365 }
366
367 std::sort(file_list.begin() + sort_start, file_list.end());
368
369 /* Show drives */
370 FiosGetDrives(file_list);
371}
372
380static std::string GetFileTitle(std::string_view file, Subdirectory subdir)
381{
382 std::string filename = fmt::format("{}.title", file);
383 auto f = FioFOpenFile(filename, "r", subdir);
384 if (!f.has_value()) return {};
385
386 char title[80];
387 size_t read = fread(title, 1, lengthof(title), *f);
388
389 assert(read <= lengthof(title));
390 return StrMakeValid(std::string_view{title, read});
391}
392
402std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
403{
404 /* Show savegame files
405 * .SAV OpenTTD saved game
406 * .SS1 Transport Tycoon Deluxe preset game
407 * .SV1 Transport Tycoon Deluxe (Patch) saved game
408 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
409
410 if (StrEqualsIgnoreCase(ext, ".sav")) {
411 return { FIOS_TYPE_FILE, GetFileTitle(file, SAVE_DIR) };
412 }
413
414 if (fop == SLO_LOAD) {
415 if (StrEqualsIgnoreCase(ext, ".ss1") || StrEqualsIgnoreCase(ext, ".sv1") ||
416 StrEqualsIgnoreCase(ext, ".sv2")) {
417 return { FIOS_TYPE_OLDFILE, GetOldSaveGameName(file) };
418 }
419 }
420
421 return { FIOS_TYPE_INVALID, {} };
422}
423
431void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
432{
433 static std::optional<std::string> fios_save_path;
434
435 if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
436
437 _fios_path = &(*fios_save_path);
438
439 FiosGetFileList(fop, show_dirs, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
440}
441
451std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
452{
453 /* Show scenario files
454 * .SCN OpenTTD style scenario file
455 * .SV0 Transport Tycoon Deluxe (Patch) scenario
456 * .SS0 Transport Tycoon Deluxe preset scenario */
457 if (StrEqualsIgnoreCase(ext, ".scn")) {
458 return { FIOS_TYPE_SCENARIO, GetFileTitle(file, SCENARIO_DIR) };
459
460 }
461
462 if (fop == SLO_LOAD) {
463 if (StrEqualsIgnoreCase(ext, ".sv0") || StrEqualsIgnoreCase(ext, ".ss0")) {
464 return { FIOS_TYPE_OLD_SCENARIO, GetOldSaveGameName(file) };
465 }
466 }
467
468 return { FIOS_TYPE_INVALID, {} };
469}
470
478void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
479{
480 static std::optional<std::string> fios_scn_path;
481
482 /* Copy the default path on first run or on 'New Game' */
483 if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
484
485 _fios_path = &(*fios_scn_path);
486
487 std::string base_path = FioFindDirectory(SCENARIO_DIR);
488 Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
489 FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
490}
491
492std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, std::string_view file, std::string_view ext)
493{
494 /* Show heightmap files
495 * .PNG PNG Based heightmap files
496 * .BMP BMP Based heightmap files
497 */
498
499 FiosType type = FIOS_TYPE_INVALID;
500
501#ifdef WITH_PNG
502 if (StrEqualsIgnoreCase(ext, ".png")) type = FIOS_TYPE_PNG;
503#endif /* WITH_PNG */
504
505 if (StrEqualsIgnoreCase(ext, ".bmp")) type = FIOS_TYPE_BMP;
506
507 if (type == FIOS_TYPE_INVALID) return { FIOS_TYPE_INVALID, {} };
508
509 TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
510 if (it != _tar_filelist[SCENARIO_DIR].end()) {
511 /* If the file is in a tar and that tar is not in a heightmap
512 * directory we are for sure not supposed to see it.
513 * Examples of this are pngs part of documentation within
514 * collections of NewGRFs or 32 bpp graphics replacement PNGs.
515 */
516 bool match = false;
517 for (Searchpath sp : _valid_searchpaths) {
518 std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
519
520 if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
521 match = true;
522 break;
523 }
524 }
525
526 if (!match) return { FIOS_TYPE_INVALID, {} };
527 }
528
529 return { type, GetFileTitle(file, HEIGHTMAP_DIR) };
530}
531
538void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
539{
540 static std::optional<std::string> fios_hmap_path;
541
542 if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
543
544 _fios_path = &(*fios_hmap_path);
545
546 std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
547 Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
548 FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
549}
550
557static std::tuple<FiosType, std::string> FiosGetTownDataListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
558{
559 if (fop == SLO_LOAD) {
560 if (StrEqualsIgnoreCase(ext, ".json")) {
561 return { FIOS_TYPE_JSON, GetFileTitle(file, SAVE_DIR) };
562 }
563 }
564
565 return { FIOS_TYPE_INVALID, {} };
566}
567
574void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
575{
576 static std::optional<std::string> fios_town_data_path;
577
578 if (!fios_town_data_path) fios_town_data_path = FioFindDirectory(HEIGHTMAP_DIR);
579
580 _fios_path = &(*fios_town_data_path);
581
582 std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
583 Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
584 FiosGetFileList(fop, show_dirs, &FiosGetTownDataListCallback, subdir, file_list);
585}
586
591std::string_view FiosGetScreenshotDir()
592{
593 static std::optional<std::string> fios_screenshot_path;
594
595 if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
596
597 return *fios_screenshot_path;
598}
599
602 uint32_t scenid;
603 MD5Hash md5sum;
604 std::string filename;
605
606 bool operator == (const ScenarioIdentifier &other) const
607 {
608 return this->scenid == other.scenid && this->md5sum == other.md5sum;
609 }
610};
611
615class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
616 bool scanned;
617public:
620
625 void Scan(bool rescan)
626 {
627 if (this->scanned && !rescan) return;
628
629 this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
630 this->scanned = true;
631 }
632
633 bool AddFile(const std::string &filename, size_t, const std::string &) override
634 {
635 auto f = FioFOpenFile(filename, "r", SCENARIO_DIR);
636 if (!f.has_value()) return false;
637
639 int fret = fscanf(*f, "%u", &id.scenid);
640 if (fret != 1) return false;
641 id.filename = filename;
642
643 Md5 checksum;
644 uint8_t buffer[1024];
645 size_t len, size;
646
647 /* open the scenario file, but first get the name.
648 * This is safe as we check on extension which
649 * must always exist. */
650 f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
651 if (!f.has_value()) return false;
652
653 /* calculate md5sum */
654 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, *f)) != 0 && size != 0) {
655 size -= len;
656 checksum.Append(buffer, len);
657 }
658 checksum.Finish(id.md5sum);
659
660 include(*this, id);
661 return true;
662 }
663};
664
667
674std::optional<std::string_view> FindScenario(const ContentInfo &ci, bool md5sum)
675{
676 _scanner.Scan(false);
677
678 for (ScenarioIdentifier &id : _scanner) {
679 if (md5sum ? (id.md5sum == ci.md5sum)
680 : (id.scenid == ci.unique_id)) {
681 return id.filename;
682 }
683 }
684
685 return std::nullopt;
686}
687
694bool HasScenario(const ContentInfo &ci, bool md5sum)
695{
696 return FindScenario(ci, md5sum).has_value();
697}
698
703{
704 _scanner.Scan(true);
705}
706
711FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1)
712{
713 static std::optional<std::string> _autosave_path;
714 if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR);
715
716 static std::string _prefix;
717
718 /* Callback for FiosFileScanner. */
719 static FiosGetTypeAndNameProc *const proc = [](SaveLoadOperation, std::string_view file, std::string_view ext) {
720 if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
721 return std::tuple(FIOS_TYPE_INVALID, std::string{});
722 };
723
724 /* Prefix to check in the callback. */
725 _prefix = *_autosave_path + this->prefix;
726
727 /* Get the save list. */
728 FileList list;
729 FiosFileScanner scanner(SLO_SAVE, proc, list);
730 scanner.Scan(".sav", *_autosave_path, false);
731
732 /* Find the number for the most recent save, if any. */
733 if (list.begin() != list.end()) {
734 SortingBits order = _savegame_sort_order;
735 _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
736 std::sort(list.begin(), list.end());
737 _savegame_sort_order = order;
738
739 std::string_view name = list.begin()->title;
740 std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
741 }
742}
743
749{
750 if (++this->number >= _settings_client.gui.max_num_autosaves) this->number = 0;
751 return fmt::format("{}{}.sav", this->prefix, this->number);
752}
753
759{
760 return fmt::format("-{}.sav", this->prefix);
761}
List of file information.
Definition fios.h:87
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop, bool show_dirs)
Construct a file list with the given kind of files, for the stated purpose.
Definition fios.cpp:67
const FiosItem * FindItem(std::string_view file)
Find file information of a file by its name from the file list.
Definition fios.cpp:103
Helper for scanning for files with a given name.
Definition fileio_func.h:37
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.
Definition fileio.cpp:1112
Scanner to scan for a particular type of FIOS file.
Definition fios.cpp:248
FiosFileScanner(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, FileList &file_list)
Create the scanner.
Definition fios.cpp:259
SaveLoadOperation fop
The kind of file we are looking for.
Definition fios.cpp:249
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Try to add a fios item set with the given filename.
Definition fios.cpp:271
FileList & file_list
Destination of the found files.
Definition fios.cpp:251
FiosGetTypeAndNameProc * callback_proc
Callback to check whether the file may be added.
Definition fios.cpp:250
Scanner to find the unique IDs of scenarios.
Definition fios.cpp:615
bool scanned
Whether we've already scanned.
Definition fios.cpp:616
void Scan(bool rescan)
Scan, but only if it's needed.
Definition fios.cpp:625
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
Definition fios.cpp:633
ScenarioScanner()
Initialise.
Definition fios.cpp:619
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 include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
bool FioRemove(const std::string &filename)
Remove a file.
Definition fileio.cpp:327
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.
Definition fileio.cpp:242
Functions for Standard In/Out file operations.
SaveLoadOperation
Operation performed on the file.
Definition fileio_type.h:51
@ SLO_SAVE
File is being saved.
Definition fileio_type.h:54
@ SLO_LOAD
File is being loaded.
Definition fileio_type.h:53
@ DFT_FIOS_DRIVE
A drive (letter) entry.
Definition fileio_type.h:40
@ DFT_FIOS_DIR
A directory entry.
Definition fileio_type.h:42
@ DFT_FIOS_PARENT
A parent directory entry.
Definition fileio_type.h:41
@ DFT_INVALID
Unknown or invalid file.
Definition fileio_type.h:47
@ DFT_FIOS_DIRECT
Direct filename.
Definition fileio_type.h:43
Searchpath
Types of searchpaths OpenTTD might use.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
@ NO_DIRECTORY
A path without any base directory.
@ SCREENSHOT_DIR
Subdirectory for all screenshots.
@ SCENARIO_DIR
Base directory for all scenarios.
Definition fileio_type.h:91
@ SAVE_DIR
Base directory for all savegames.
Definition fileio_type.h:89
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition fileio_type.h:92
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition fileio_type.h:90
AbstractFileType
The different abstract types of files that the system knows about.
Definition fileio_type.h:16
@ FT_SCENARIO
old or new scenario
Definition fileio_type.h:19
@ FT_HEIGHTMAP
heightmap file
Definition fileio_type.h:20
@ FT_NONE
nothing to do
Definition fileio_type.h:17
@ FT_SAVEGAME
old or new savegame
Definition fileio_type.h:18
@ FT_TOWN_DATA
town data file
Definition fileio_type.h:21
std::tuple< FiosType, std::string > FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetFileList.
Definition fios.cpp:451
static std::tuple< FiosType, std::string > FiosGetTownDataListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetTownDataList.
Definition fios.cpp:557
std::optional< std::string_view > FindScenario(const ContentInfo &ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition fios.cpp:674
std::string FiosMakeSavegameName(std::string_view name)
Make a save game or scenario filename from a name.
Definition fios.cpp:216
std::tuple< FiosType, std::string > FiosGetSavegameListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetFileList.
Definition fios.cpp:402
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...
Definition fios.cpp:380
std::string FiosGetCurrentPath()
Get the current path/working directory.
Definition fios.cpp:132
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.
Definition fios.cpp:194
bool FiosDelete(std::string_view name)
Delete a file.
Definition fios.cpp:238
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
Definition fios.cpp:431
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
Definition fios.cpp:538
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.
Definition fios.cpp:317
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition fios.cpp:702
std::string FiosMakeHeightmapName(std::string_view name)
Construct a filename for a height map.
Definition fios.cpp:228
bool HasScenario(const ContentInfo &ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition fios.cpp:694
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
Definition fios.cpp:478
std::string_view FiosGetScreenshotDir()
Get the directory for screenshots.
Definition fios.cpp:591
bool FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at _fios_path.
Definition fios.cpp:142
static ScenarioScanner _scanner
Scanner for scenarios.
Definition fios.cpp:666
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
Definition fios.cpp:574
Declarations for savegames operations.
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
Definition fios.cpp:431
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
Definition fios.cpp:538
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
Definition fios.cpp:478
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
Definition fios.cpp:574
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 settings.cpp:60
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
bool StrEqualsIgnoreCase(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:321
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition string.cpp:425
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:117
Parse strings.
Functions related to low-level strings.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:415
Functions related to OTTD's strings.
GUISettings gui
settings related to the GUI
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.
Definition fios.h:78
bool operator<(const FiosItem &other) const
Compare two FiosItem's.
Definition fios.cpp:48
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition fios.cpp:748
FiosNumberedSaveName(const std::string &prefix)
Constructs FiosNumberedSaveName.
Definition fios.cpp:711
std::string Extension()
Generate an extension for a savegame name.
Definition fios.cpp:758
Elements of a file system that are recognized.
Definition fileio_type.h:62
DetailedFileType detailed
Detailed file type.
Definition fileio_type.h:64
uint8_t max_num_autosaves
controls how many autosavegames are made before the game starts to overwrite (names them 0 to max_num...
Basic data to distinguish a scenario.
Definition fios.cpp:601
uint32_t scenid
ID for the scenario (generated by content).
Definition fios.cpp:602
MD5Hash md5sum
MD5 checksum of file.
Definition fios.cpp:603
std::string filename
filename of the file.
Definition fios.cpp:604
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.
Definition win32.cpp:354
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:337