OpenTTD Source 20250924-master-gbec4e71d53
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.GetDecodedString(), other.title.GetDecodedString());
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.GetDecodedString()) 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.GetDecodedString()) 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->name, 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
233typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(SaveLoadOperation fop, std::string_view filename, std::string_view ext);
234
240 FiosGetTypeAndNameProc *callback_proc;
242public:
252
253 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
254};
255
261bool FiosFileScanner::AddFile(const std::string &filename, size_t, const std::string &)
262{
263 auto sep = filename.rfind('.');
264 if (sep == std::string::npos) return false;
265 std::string ext = filename.substr(sep);
266
267 auto [type, title] = this->callback_proc(this->fop, filename, ext);
268 if (type == FIOS_TYPE_INVALID) return false;
269
270 for (const auto &fios : file_list) {
271 if (filename == fios.name) return false;
272 }
273
274 FiosItem *fios = &file_list.emplace_back();
275
276 std::error_code error_code;
277 auto write_time = std::filesystem::last_write_time(OTTD2FS(filename), error_code);
278 if (error_code) {
279 fios->mtime = 0;
280 } else {
281 fios->mtime = std::chrono::duration_cast<std::chrono::milliseconds>(write_time.time_since_epoch()).count();
282 }
283
284 fios->type = type;
285 fios->name = filename;
286
287 /* If the file doesn't have a title, use its filename */
288 if (title.empty()) {
289 auto ps = filename.rfind(PATHSEPCHAR);
290 fios->title = GetEncodedString(STR_JUST_RAW_STRING, StrMakeValid(filename.substr((ps == std::string::npos ? 0 : ps + 1))));
291 } else {
292 fios->title = GetEncodedString(STR_JUST_RAW_STRING, StrMakeValid(title));
293 };
294
295 return true;
296}
297
298
307static void FiosGetFileList(SaveLoadOperation fop, bool show_dirs, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
308{
309 size_t sort_start = 0;
310
311 file_list.clear();
312
313 assert(_fios_path != nullptr);
314
315 if (show_dirs) {
316 /* A parent directory link exists if we are not in the root directory */
317 if (!FiosIsRoot(*_fios_path)) {
318 FiosItem &fios = file_list.emplace_back();
319 fios.type = FIOS_TYPE_PARENT;
320 fios.mtime = 0;
321 fios.name = "..";
322 fios.title = GetEncodedString(STR_SAVELOAD_PARENT_DIRECTORY, ".."sv);
323 sort_start = file_list.size();
324 }
325
326 /* Show subdirectories */
327 std::error_code error_code;
328 for (const auto &dir_entry : std::filesystem::directory_iterator(OTTD2FS(*_fios_path), error_code)) {
329 if (!dir_entry.is_directory()) continue;
330 if (FiosIsHiddenFile(dir_entry) && dir_entry.path().filename() != PERSONAL_DIR) continue;
331
332 FiosItem &fios = file_list.emplace_back();
333 fios.type = FIOS_TYPE_DIR;
334 fios.mtime = 0;
335 fios.name = FS2OTTD(dir_entry.path().filename().native());
336 fios.title = GetEncodedString(STR_SAVELOAD_DIRECTORY, fios.name + PATHSEP);
337 }
338
339 /* Sort the subdirs always by name, ascending, remember user-sorting order */
340 SortingBits order = _savegame_sort_order;
341 _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
342 std::sort(file_list.begin() + sort_start, file_list.end());
343 _savegame_sort_order = order;
344 }
345
346 /* This is where to start sorting for the filenames */
347 sort_start = file_list.size();
348
349 /* Show files */
350 FiosFileScanner scanner(fop, callback_proc, file_list);
351 if (subdir == NO_DIRECTORY) {
352 scanner.Scan({}, *_fios_path, false);
353 } else {
354 scanner.Scan({}, subdir, true, true);
355 }
356
357 std::sort(file_list.begin() + sort_start, file_list.end());
358
359 /* Show drives */
360 FiosGetDrives(file_list);
361}
362
370static std::string GetFileTitle(std::string_view file, Subdirectory subdir)
371{
372 std::string filename = fmt::format("{}.title", file);
373 auto f = FioFOpenFile(filename, "r", subdir);
374 if (!f.has_value()) return {};
375
376 char title[80];
377 size_t read = fread(title, 1, lengthof(title), *f);
378
379 assert(read <= lengthof(title));
380 return StrMakeValid(std::string_view{title, read});
381}
382
392std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
393{
394 /* Show savegame files
395 * .SAV OpenTTD saved game
396 * .SS1 Transport Tycoon Deluxe preset game
397 * .SV1 Transport Tycoon Deluxe (Patch) saved game
398 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
399
400 if (StrEqualsIgnoreCase(ext, ".sav")) {
401 return { FIOS_TYPE_FILE, GetFileTitle(file, SAVE_DIR) };
402 }
403
404 if (fop == SLO_LOAD) {
405 if (StrEqualsIgnoreCase(ext, ".ss1") || StrEqualsIgnoreCase(ext, ".sv1") ||
406 StrEqualsIgnoreCase(ext, ".sv2")) {
407 return { FIOS_TYPE_OLDFILE, GetOldSaveGameName(file) };
408 }
409 }
410
411 return { FIOS_TYPE_INVALID, {} };
412}
413
421void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
422{
423 static std::optional<std::string> fios_save_path;
424
425 if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
426
427 _fios_path = &(*fios_save_path);
428
429 FiosGetFileList(fop, show_dirs, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
430}
431
441std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
442{
443 /* Show scenario files
444 * .SCN OpenTTD style scenario file
445 * .SV0 Transport Tycoon Deluxe (Patch) scenario
446 * .SS0 Transport Tycoon Deluxe preset scenario */
447 if (StrEqualsIgnoreCase(ext, ".scn")) {
448 return { FIOS_TYPE_SCENARIO, GetFileTitle(file, SCENARIO_DIR) };
449
450 }
451
452 if (fop == SLO_LOAD) {
453 if (StrEqualsIgnoreCase(ext, ".sv0") || StrEqualsIgnoreCase(ext, ".ss0")) {
454 return { FIOS_TYPE_OLD_SCENARIO, GetOldSaveGameName(file) };
455 }
456 }
457
458 return { FIOS_TYPE_INVALID, {} };
459}
460
468void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
469{
470 static std::optional<std::string> fios_scn_path;
471
472 /* Copy the default path on first run or on 'New Game' */
473 if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
474
475 _fios_path = &(*fios_scn_path);
476
477 std::string base_path = FioFindDirectory(SCENARIO_DIR);
478 Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
479 FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
480}
481
482std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, std::string_view file, std::string_view ext)
483{
484 /* Show heightmap files
485 * .PNG PNG Based heightmap files
486 * .BMP BMP Based heightmap files
487 */
488
489 FiosType type = FIOS_TYPE_INVALID;
490
491#ifdef WITH_PNG
492 if (StrEqualsIgnoreCase(ext, ".png")) type = FIOS_TYPE_PNG;
493#endif /* WITH_PNG */
494
495 if (StrEqualsIgnoreCase(ext, ".bmp")) type = FIOS_TYPE_BMP;
496
497 if (type == FIOS_TYPE_INVALID) return { FIOS_TYPE_INVALID, {} };
498
499 TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
500 if (it != _tar_filelist[SCENARIO_DIR].end()) {
501 /* If the file is in a tar and that tar is not in a heightmap
502 * directory we are for sure not supposed to see it.
503 * Examples of this are pngs part of documentation within
504 * collections of NewGRFs or 32 bpp graphics replacement PNGs.
505 */
506 bool match = false;
507 for (Searchpath sp : _valid_searchpaths) {
508 std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
509
510 if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
511 match = true;
512 break;
513 }
514 }
515
516 if (!match) return { FIOS_TYPE_INVALID, {} };
517 }
518
519 return { type, GetFileTitle(file, HEIGHTMAP_DIR) };
520}
521
528void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
529{
530 static std::optional<std::string> fios_hmap_path;
531
532 if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
533
534 _fios_path = &(*fios_hmap_path);
535
536 std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
537 Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
538 FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
539}
540
547static std::tuple<FiosType, std::string> FiosGetTownDataListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
548{
549 if (fop == SLO_LOAD) {
550 if (StrEqualsIgnoreCase(ext, ".json")) {
551 return { FIOS_TYPE_JSON, GetFileTitle(file, SAVE_DIR) };
552 }
553 }
554
555 return { FIOS_TYPE_INVALID, {} };
556}
557
564void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
565{
566 static std::optional<std::string> fios_town_data_path;
567
568 if (!fios_town_data_path) fios_town_data_path = FioFindDirectory(HEIGHTMAP_DIR);
569
570 _fios_path = &(*fios_town_data_path);
571
572 std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
573 Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
574 FiosGetFileList(fop, show_dirs, &FiosGetTownDataListCallback, subdir, file_list);
575}
576
581std::string_view FiosGetScreenshotDir()
582{
583 static std::optional<std::string> fios_screenshot_path;
584
585 if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
586
587 return *fios_screenshot_path;
588}
589
592 uint32_t scenid;
593 MD5Hash md5sum;
594 std::string filename;
595
596 bool operator == (const ScenarioIdentifier &other) const
597 {
598 return this->scenid == other.scenid && this->md5sum == other.md5sum;
599 }
600};
601
605class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
606 bool scanned;
607public:
610
615 void Scan(bool rescan)
616 {
617 if (this->scanned && !rescan) return;
618
619 this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
620 this->scanned = true;
621 }
622
623 bool AddFile(const std::string &filename, size_t, const std::string &) override
624 {
625 auto f = FioFOpenFile(filename, "r", SCENARIO_DIR);
626 if (!f.has_value()) return false;
627
629 int fret = fscanf(*f, "%u", &id.scenid);
630 if (fret != 1) return false;
631 id.filename = filename;
632
633 Md5 checksum;
634 uint8_t buffer[1024];
635 size_t len, size;
636
637 /* open the scenario file, but first get the name.
638 * This is safe as we check on extension which
639 * must always exist. */
640 f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
641 if (!f.has_value()) return false;
642
643 /* calculate md5sum */
644 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, *f)) != 0 && size != 0) {
645 size -= len;
646 checksum.Append(buffer, len);
647 }
648 checksum.Finish(id.md5sum);
649
650 include(*this, id);
651 return true;
652 }
653};
654
657
664std::optional<std::string_view> FindScenario(const ContentInfo &ci, bool md5sum)
665{
666 _scanner.Scan(false);
667
668 for (ScenarioIdentifier &id : _scanner) {
669 if (md5sum ? (id.md5sum == ci.md5sum)
670 : (id.scenid == ci.unique_id)) {
671 return id.filename;
672 }
673 }
674
675 return std::nullopt;
676}
677
684bool HasScenario(const ContentInfo &ci, bool md5sum)
685{
686 return FindScenario(ci, md5sum).has_value();
687}
688
693{
694 _scanner.Scan(true);
695}
696
701FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1)
702{
703 static std::optional<std::string> _autosave_path;
704 if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR);
705
706 static std::string _prefix;
707
708 /* Callback for FiosFileScanner. */
709 static FiosGetTypeAndNameProc *const proc = [](SaveLoadOperation, std::string_view file, std::string_view ext) {
710 if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
711 return std::tuple(FIOS_TYPE_INVALID, std::string{});
712 };
713
714 /* Prefix to check in the callback. */
715 _prefix = *_autosave_path + this->prefix;
716
717 /* Get the save list. */
718 FileList list;
719 FiosFileScanner scanner(SLO_SAVE, proc, list);
720 scanner.Scan(".sav", *_autosave_path, false);
721
722 /* Find the number for the most recent save, if any. */
723 if (list.begin() != list.end()) {
724 SortingBits order = _savegame_sort_order;
725 _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
726 std::sort(list.begin(), list.end());
727 _savegame_sort_order = order;
728
729 std::string name = list.begin()->title.GetDecodedString();
730 std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
731 }
732}
733
739{
740 if (++this->number >= _settings_client.gui.max_num_autosaves) this->number = 0;
741 return fmt::format("{}{}.sav", this->prefix, this->number);
742}
743
749{
750 return fmt::format("-{}.sav", this->prefix);
751}
std::string GetDecodedString() const
Decode the encoded string.
Definition strings.cpp:207
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:1115
Scanner to scan for a particular type of FIOS file.
Definition fios.cpp:238
FiosFileScanner(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, FileList &file_list)
Create the scanner.
Definition fios.cpp:249
SaveLoadOperation fop
The kind of file we are looking for.
Definition fios.cpp:239
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:261
FileList & file_list
Destination of the found files.
Definition fios.cpp:241
FiosGetTypeAndNameProc * callback_proc
Callback to check whether the file may be added.
Definition fios.cpp:240
Scanner to find the unique IDs of scenarios.
Definition fios.cpp:605
bool scanned
Whether we've already scanned.
Definition fios.cpp:606
void Scan(bool rescan)
Scan, but only if it's needed.
Definition fios.cpp:615
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
Definition fios.cpp:623
ScenarioScanner()
Initialise.
Definition fios.cpp:609
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.
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:52
@ SLO_SAVE
File is being saved.
Definition fileio_type.h:55
@ SLO_LOAD
File is being loaded.
Definition fileio_type.h:54
@ DFT_FIOS_DRIVE
A drive (letter) entry.
Definition fileio_type.h:41
@ DFT_FIOS_DIR
A directory entry.
Definition fileio_type.h:43
@ DFT_FIOS_PARENT
A parent directory entry.
Definition fileio_type.h:42
@ DFT_INVALID
Unknown or invalid file.
Definition fileio_type.h:48
@ DFT_FIOS_DIRECT
Direct filename.
Definition fileio_type.h:44
Searchpath
Types of searchpaths OpenTTD might use.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
@ 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:92
@ SAVE_DIR
Base directory for all savegames.
Definition fileio_type.h:90
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition fileio_type.h:93
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition fileio_type.h:91
AbstractFileType
The different abstract types of files that the system knows about.
Definition fileio_type.h:17
@ FT_SCENARIO
old or new scenario
Definition fileio_type.h:20
@ FT_HEIGHTMAP
heightmap file
Definition fileio_type.h:21
@ FT_NONE
nothing to do
Definition fileio_type.h:18
@ FT_SAVEGAME
old or new savegame
Definition fileio_type.h:19
@ FT_TOWN_DATA
town data file
Definition fileio_type.h:22
std::tuple< FiosType, std::string > FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetFileList.
Definition fios.cpp:441
static std::tuple< FiosType, std::string > FiosGetTownDataListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetTownDataList.
Definition fios.cpp:547
std::optional< std::string_view > FindScenario(const ContentInfo &ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition fios.cpp:664
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:392
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:370
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
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
Definition fios.cpp:421
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
Definition fios.cpp:528
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:307
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition fios.cpp:692
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:684
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
Definition fios.cpp:468
std::string_view FiosGetScreenshotDir()
Get the directory for screenshots.
Definition fios.cpp:581
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:656
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
Definition fios.cpp:564
Declarations for savegames operations.
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
Definition fios.cpp:421
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
Definition fios.cpp:528
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
Definition fios.cpp:468
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
Definition fios.cpp:564
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.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
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:738
FiosNumberedSaveName(const std::string &prefix)
Constructs FiosNumberedSaveName.
Definition fios.cpp:701
std::string Extension()
Generate an extension for a savegame name.
Definition fios.cpp:748
Elements of a file system that are recognized.
Definition fileio_type.h:63
DetailedFileType detailed
Detailed file type.
Definition fileio_type.h:65
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:591
uint32_t scenid
ID for the scenario (generated by content).
Definition fios.cpp:592
MD5Hash md5sum
MD5 checksum of file.
Definition fios.cpp:593
std::string filename
filename of the file.
Definition fios.cpp:594
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:357
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:340