OpenTTD Source 20260218-master-g2123fca5ea
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "3rdparty/md5/md5.h"
13#include "fileio_func.h"
14#include "fios.h"
16#include "screenshot.h"
17#include "string_func.h"
18#include "strings_func.h"
19#include "tar_type.h"
20#include <sys/stat.h>
21#include <charconv>
22#include <filesystem>
23
24#include "table/strings.h"
25
26#include "safeguards.h"
27
28/* Variables to display file lists */
29static std::string *_fios_path = nullptr;
30SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
31
32/* OS-specific functions are taken from their respective files (win32/unix .c) */
33extern bool FiosIsRoot(const std::string &path);
34extern bool FiosIsHiddenFile(const std::filesystem::path &path);
35extern void FiosGetDrives(FileList &file_list);
36
37/* get the name of an oldstyle savegame */
38extern std::string GetOldSaveGameName(std::string_view file);
39
45bool FiosItem::operator< (const FiosItem &other) const
46{
47 int r = false;
48
49 if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
50 r = ClampTo<int32_t>(this->mtime - other.mtime);
51 } else {
52 r = StrNaturalCompare(this->title.GetDecodedString(), other.title.GetDecodedString());
53 }
54 if (r == 0) return false;
55 return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
56}
57
64void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop, bool show_dirs)
65{
66 this->clear();
67
68 assert(fop == SLO_LOAD || fop == SLO_SAVE);
69 switch (abstract_filetype) {
70 case FT_NONE:
71 break;
72
73 case FT_SAVEGAME:
74 FiosGetSavegameList(fop, show_dirs, *this);
75 break;
76
77 case FT_SCENARIO:
78 FiosGetScenarioList(fop, show_dirs, *this);
79 break;
80
81 case FT_HEIGHTMAP:
82 FiosGetHeightmapList(fop, show_dirs, *this);
83 break;
84
85 case FT_TOWN_DATA:
86 FiosGetTownDataList(fop, show_dirs, *this);
87 break;
88
89 default:
90 NOT_REACHED();
91 }
92}
93
100const FiosItem *FileList::FindItem(std::string_view file)
101{
102 for (const auto &it : *this) {
103 const FiosItem *item = &it;
104 if (file == item->name) return item;
105 if (file == item->title.GetDecodedString()) return item;
106 }
107
108 /* If no name matches, try to parse it as number */
109 StringConsumer consumer{file};
110 auto number = consumer.TryReadIntegerBase<int>(10);
111 if (number.has_value() && !consumer.AnyBytesLeft() && IsInsideMM(*number, 0, this->size())) return &this->at(*number);
112
113 /* As a last effort assume it is an OpenTTD savegame and
114 * that the ".sav" part was not given. */
115 std::string long_file(file);
116 long_file += ".sav";
117 for (const auto &it : *this) {
118 const FiosItem *item = &it;
119 if (long_file == item->name) return item;
120 if (long_file == item->title.GetDecodedString()) return item;
121 }
122
123 return nullptr;
124}
125
131{
132 return *_fios_path;
133}
134
140bool FiosBrowseTo(const FiosItem *item)
141{
142 switch (item->type.detailed) {
143 case DFT_FIOS_DRIVE:
144#if defined(_WIN32)
145 assert(_fios_path != nullptr);
146 *_fios_path = std::string{ item->name, 0, 1 } + ":" PATHSEP;
147#endif
148 break;
149
150 case DFT_INVALID:
151 break;
152
153 case DFT_FIOS_PARENT: {
154 assert(_fios_path != nullptr);
155 auto s = _fios_path->find_last_of(PATHSEPCHAR);
156 if (s != std::string::npos && s != 0) {
157 _fios_path->erase(s); // Remove last path separator character, so we can go up one level.
158 }
159
160 s = _fios_path->find_last_of(PATHSEPCHAR);
161 if (s != std::string::npos) {
162 _fios_path->erase(s + 1); // go up a directory
163 }
164 break;
165 }
166
167 case DFT_FIOS_DIR:
168 assert(_fios_path != nullptr);
169 *_fios_path += item->name;
170 *_fios_path += PATHSEP;
171 break;
172
173 case DFT_FIOS_DIRECT:
174 assert(_fios_path != nullptr);
175 *_fios_path = item->name;
176 break;
177
178 default:
179 return false;
180 }
181
182 return true;
183}
184
192static std::string FiosMakeFilename(const std::string *path, std::string_view name, std::string_view ext)
193{
194 std::string_view base_path;
195
196 if (path != nullptr) {
197 base_path = *path;
198 /* Remove trailing path separator, if present */
199 if (!base_path.empty() && base_path.back() == PATHSEPCHAR) base_path.remove_suffix(1);
200 }
201
202 /* Don't append the extension if it is already there */
203 auto period = name.find_last_of('.');
204 if (period != std::string_view::npos && StrEqualsIgnoreCase(name.substr(period), ext)) ext = "";
205
206 return fmt::format("{}{}{}{}", base_path, PATHSEP, name, ext);
207}
208
214std::string FiosMakeSavegameName(std::string_view name)
215{
216 std::string_view extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
217
218 return FiosMakeFilename(_fios_path, name, extension);
219}
220
226std::string FiosMakeHeightmapName(std::string_view name)
227{
228 return FiosMakeFilename(_fios_path, name, fmt::format(".{}", GetCurrentScreenshotExtension()));
229}
230
231typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(SaveLoadOperation fop, std::string_view filename, std::string_view ext);
232
238 FiosGetTypeAndNameProc *callback_proc;
240public:
250
251 bool AddFile(const std::string &filename, size_t, const std::string &) override;
252};
253
259bool FiosFileScanner::AddFile(const std::string &filename, size_t, const std::string &)
260{
261 auto sep = filename.rfind('.');
262 if (sep == std::string::npos) return false;
263 std::string ext = filename.substr(sep);
264
265 auto [type, title] = this->callback_proc(this->fop, filename, ext);
266 if (type == FIOS_TYPE_INVALID) return false;
267
268 for (const auto &fios : file_list) {
269 if (filename == fios.name) return false;
270 }
271
272 FiosItem *fios = &file_list.emplace_back();
273
274 std::error_code error_code;
275 auto write_time = std::filesystem::last_write_time(OTTD2FS(filename), error_code);
276 if (error_code) {
277 fios->mtime = 0;
278 } else {
279 fios->mtime = std::chrono::duration_cast<std::chrono::milliseconds>(write_time.time_since_epoch()).count();
280 }
281
282 fios->type = type;
283 fios->name = filename;
284
285 /* If the file doesn't have a title, use its filename */
286 if (title.empty()) {
287 auto ps = filename.rfind(PATHSEPCHAR);
288 fios->title = GetEncodedString(STR_JUST_RAW_STRING, StrMakeValid(filename.substr((ps == std::string::npos ? 0 : ps + 1))));
289 } else {
290 fios->title = GetEncodedString(STR_JUST_RAW_STRING, StrMakeValid(title));
291 };
292
293 return true;
294}
295
296
305static void FiosGetFileList(SaveLoadOperation fop, bool show_dirs, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
306{
307 size_t sort_start = 0;
308
309 file_list.clear();
310
311 assert(_fios_path != nullptr);
312
313 if (show_dirs) {
314 /* A parent directory link exists if we are not in the root directory */
315 if (!FiosIsRoot(*_fios_path)) {
316 FiosItem &fios = file_list.emplace_back();
317 fios.type = FIOS_TYPE_PARENT;
318 fios.mtime = 0;
319 fios.name = "..";
320 fios.title = GetEncodedString(STR_SAVELOAD_PARENT_DIRECTORY, ".."sv);
321 sort_start = file_list.size();
322 }
323
324 /* Show subdirectories */
325 std::error_code error_code;
326 for (const auto &dir_entry : std::filesystem::directory_iterator(OTTD2FS(*_fios_path), error_code)) {
327 if (!dir_entry.is_directory()) continue;
328 if (FiosIsHiddenFile(dir_entry) && dir_entry.path().filename() != PERSONAL_DIR) continue;
329
330 FiosItem &fios = file_list.emplace_back();
331 fios.type = FIOS_TYPE_DIR;
332 fios.mtime = 0;
333 fios.name = FS2OTTD(dir_entry.path().filename().native());
334 fios.title = GetEncodedString(STR_SAVELOAD_DIRECTORY, fios.name + PATHSEP);
335 }
336
337 /* Sort the subdirs always by name, ascending, remember user-sorting order */
338 SortingBits order = _savegame_sort_order;
339 _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
340 std::sort(file_list.begin() + sort_start, file_list.end());
341 _savegame_sort_order = order;
342 }
343
344 /* This is where to start sorting for the filenames */
345 sort_start = file_list.size();
346
347 /* Show files */
348 FiosFileScanner scanner(fop, callback_proc, file_list);
349 if (subdir == NO_DIRECTORY) {
350 scanner.Scan({}, *_fios_path, false);
351 } else {
352 scanner.Scan({}, subdir, true, true);
353 }
354
355 std::sort(file_list.begin() + sort_start, file_list.end());
356
357 /* Show drives */
358 FiosGetDrives(file_list);
359}
360
368static std::string GetFileTitle(std::string_view file, Subdirectory subdir)
369{
370 std::string filename = fmt::format("{}.title", file);
371 auto f = FioFOpenFile(filename, "r", subdir);
372 if (!f.has_value()) return {};
373
374 char title[80];
375 size_t read = fread(title, 1, lengthof(title), *f);
376
377 assert(read <= lengthof(title));
378 return StrMakeValid(std::string_view{title, read});
379}
380
390std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
391{
392 /* Show savegame files
393 * .SAV OpenTTD saved game
394 * .SS1 Transport Tycoon Deluxe preset game
395 * .SV1 Transport Tycoon Deluxe (Patch) saved game
396 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
397
398 if (StrEqualsIgnoreCase(ext, ".sav")) {
399 return { FIOS_TYPE_FILE, GetFileTitle(file, SAVE_DIR) };
400 }
401
402 if (fop == SLO_LOAD) {
403 if (StrEqualsIgnoreCase(ext, ".ss1") || StrEqualsIgnoreCase(ext, ".sv1") ||
404 StrEqualsIgnoreCase(ext, ".sv2")) {
405 return { FIOS_TYPE_OLDFILE, GetOldSaveGameName(file) };
406 }
407 }
408
409 return { FIOS_TYPE_INVALID, {} };
410}
411
419void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
420{
421 static std::optional<std::string> fios_save_path;
422
423 if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
424
425 _fios_path = &(*fios_save_path);
426
427 FiosGetFileList(fop, show_dirs, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
428}
429
439std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
440{
441 /* Show scenario files
442 * .SCN OpenTTD style scenario file
443 * .SV0 Transport Tycoon Deluxe (Patch) scenario
444 * .SS0 Transport Tycoon Deluxe preset scenario */
445 if (StrEqualsIgnoreCase(ext, ".scn")) {
446 return { FIOS_TYPE_SCENARIO, GetFileTitle(file, SCENARIO_DIR) };
447
448 }
449
450 if (fop == SLO_LOAD) {
451 if (StrEqualsIgnoreCase(ext, ".sv0") || StrEqualsIgnoreCase(ext, ".ss0")) {
452 return { FIOS_TYPE_OLD_SCENARIO, GetOldSaveGameName(file) };
453 }
454 }
455
456 return { FIOS_TYPE_INVALID, {} };
457}
458
466void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
467{
468 static std::optional<std::string> fios_scn_path;
469
470 /* Copy the default path on first run or on 'New Game' */
471 if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
472
473 _fios_path = &(*fios_scn_path);
474
475 std::string base_path = FioFindDirectory(SCENARIO_DIR);
476 Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
477 FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
478}
479
480std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, std::string_view file, std::string_view ext)
481{
482 /* Show heightmap files
483 * .PNG PNG Based heightmap files
484 * .BMP BMP Based heightmap files
485 */
486
487 FiosType type = FIOS_TYPE_INVALID;
488
489#ifdef WITH_PNG
490 if (StrEqualsIgnoreCase(ext, ".png")) type = FIOS_TYPE_PNG;
491#endif /* WITH_PNG */
492
493 if (StrEqualsIgnoreCase(ext, ".bmp")) type = FIOS_TYPE_BMP;
494
495 if (type == FIOS_TYPE_INVALID) return { FIOS_TYPE_INVALID, {} };
496
497 TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
498 if (it != _tar_filelist[SCENARIO_DIR].end()) {
499 /* If the file is in a tar and that tar is not in a heightmap
500 * directory we are for sure not supposed to see it.
501 * Examples of this are pngs part of documentation within
502 * collections of NewGRFs or 32 bpp graphics replacement PNGs.
503 */
504 bool match = false;
505 for (Searchpath sp : _valid_searchpaths) {
506 std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
507
508 if (it->second.tar_filename.starts_with(buf)) {
509 match = true;
510 break;
511 }
512 }
513
514 if (!match) return { FIOS_TYPE_INVALID, {} };
515 }
516
517 return { type, GetFileTitle(file, HEIGHTMAP_DIR) };
518}
519
526void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
527{
528 static std::optional<std::string> fios_hmap_path;
529
530 if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
531
532 _fios_path = &(*fios_hmap_path);
533
534 std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
535 Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
536 FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
537}
538
546static std::tuple<FiosType, std::string> FiosGetTownDataListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
547{
548 if (fop == SLO_LOAD) {
549 if (StrEqualsIgnoreCase(ext, ".json")) {
550 return { FIOS_TYPE_JSON, GetFileTitle(file, SAVE_DIR) };
551 }
552 }
553
554 return { FIOS_TYPE_INVALID, {} };
555}
556
563void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
564{
565 static std::optional<std::string> fios_town_data_path;
566
567 if (!fios_town_data_path) fios_town_data_path = FioFindDirectory(HEIGHTMAP_DIR);
568
569 _fios_path = &(*fios_town_data_path);
570
571 std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
572 Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
573 FiosGetFileList(fop, show_dirs, &FiosGetTownDataListCallback, subdir, file_list);
574}
575
580std::string_view FiosGetScreenshotDir()
581{
582 static std::optional<std::string> fios_screenshot_path;
583
584 if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
585
586 return *fios_screenshot_path;
587}
588
591 uint32_t scenid;
592 MD5Hash md5sum;
593 std::string filename;
594
595 bool operator == (const ScenarioIdentifier &other) const
596 {
597 return this->scenid == other.scenid && this->md5sum == other.md5sum;
598 }
599};
600
604class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
605 bool scanned;
606public:
609
614 void Scan(bool rescan)
615 {
616 if (this->scanned && !rescan) return;
617
618 this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
619 this->scanned = true;
620 }
621
622 bool AddFile(const std::string &filename, size_t, const std::string &) override
623 {
624 auto f = FioFOpenFile(filename, "r", SCENARIO_DIR);
625 if (!f.has_value()) return false;
626
628 int fret = fscanf(*f, "%u", &id.scenid);
629 if (fret != 1) return false;
630 id.filename = filename;
631
632 Md5 checksum;
633 uint8_t buffer[1024];
634 size_t len, size;
635
636 /* open the scenario file, but first get the name.
637 * This is safe as we check on extension which
638 * must always exist. */
639 f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
640 if (!f.has_value()) return false;
641
642 /* calculate md5sum */
643 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, *f)) != 0 && size != 0) {
644 size -= len;
645 checksum.Append(buffer, len);
646 }
647 checksum.Finish(id.md5sum);
648
649 include(*this, id);
650 return true;
651 }
652};
653
656
663std::optional<std::string_view> FindScenario(const ContentInfo &ci, bool md5sum)
664{
665 _scanner.Scan(false);
666
667 for (ScenarioIdentifier &id : _scanner) {
668 if (md5sum ? (id.md5sum == ci.md5sum)
669 : (id.scenid == ci.unique_id)) {
670 return id.filename;
671 }
672 }
673
674 return std::nullopt;
675}
676
683bool HasScenario(const ContentInfo &ci, bool md5sum)
684{
685 return FindScenario(ci, md5sum).has_value();
686}
687
692{
693 _scanner.Scan(true);
694}
695
700FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1)
701{
702 static std::optional<std::string> _autosave_path;
703 if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR);
704
705 static std::string _prefix;
706
707 /* Callback for FiosFileScanner. */
708 static FiosGetTypeAndNameProc *const proc = [](SaveLoadOperation, std::string_view file, std::string_view ext) {
709 if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
710 return std::tuple(FIOS_TYPE_INVALID, std::string{});
711 };
712
713 /* Prefix to check in the callback. */
714 _prefix = *_autosave_path + this->prefix;
715
716 /* Get the save list. */
717 FileList list;
718 FiosFileScanner scanner(SLO_SAVE, proc, list);
719 scanner.Scan(".sav", *_autosave_path, false);
720
721 /* Find the number for the most recent save, if any. */
722 if (list.begin() != list.end()) {
723 SortingBits order = _savegame_sort_order;
724 _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
725 std::sort(list.begin(), list.end());
726 _savegame_sort_order = order;
727
728 std::string name = list.begin()->title.GetDecodedString();
729 std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
730 }
731}
732
738{
739 if (++this->number >= _settings_client.gui.max_num_autosaves) this->number = 0;
740 return fmt::format("{}{}.sav", this->prefix, this->number);
741}
742
748{
749 return fmt::format("-{}.sav", this->prefix);
750}
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:64
const FiosItem * FindItem(std::string_view file)
Find file information of a file by its name from the file list.
Definition fios.cpp:100
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:1124
Scanner to scan for a particular type of FIOS file.
Definition fios.cpp:236
FiosFileScanner(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, FileList &file_list)
Create the scanner.
Definition fios.cpp:247
SaveLoadOperation fop
The kind of file we are looking for.
Definition fios.cpp:237
FileList & file_list
Destination of the found files.
Definition fios.cpp:239
bool AddFile(const std::string &filename, size_t, const std::string &) override
Try to add a fios item set with the given filename.
Definition fios.cpp:259
FiosGetTypeAndNameProc * callback_proc
Callback to check whether the file may be added.
Definition fios.cpp:238
Scanner to find the unique IDs of scenarios.
Definition fios.cpp:604
bool scanned
Whether we've already scanned.
Definition fios.cpp:605
void Scan(bool rescan)
Scan, but only if it's needed.
Definition fios.cpp:614
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
Definition fios.cpp:622
ScenarioScanner()
Initialise.
Definition fios.cpp:608
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 AnyBytesLeft() const noexcept
Check whether any bytes left to read.
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:244
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:439
static std::tuple< FiosType, std::string > FiosGetTownDataListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetTownDataList.
Definition fios.cpp:546
std::optional< std::string_view > FindScenario(const ContentInfo &ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition fios.cpp:663
std::string FiosMakeSavegameName(std::string_view name)
Make a save game or scenario filename from a name.
Definition fios.cpp:214
std::tuple< FiosType, std::string > FiosGetSavegameListCallback(SaveLoadOperation fop, std::string_view file, std::string_view ext)
Callback for FiosGetFileList.
Definition fios.cpp:390
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:368
std::string FiosGetCurrentPath()
Get the current path/working directory.
Definition fios.cpp:130
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:192
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
Definition fios.cpp:419
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
Definition fios.cpp:526
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:305
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition fios.cpp:691
std::string FiosMakeHeightmapName(std::string_view name)
Construct a filename for a height map.
Definition fios.cpp:226
bool HasScenario(const ContentInfo &ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition fios.cpp:683
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
Definition fios.cpp:466
std::string_view FiosGetScreenshotDir()
Get the directory for screenshots.
Definition fios.cpp:580
bool FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at _fios_path.
Definition fios.cpp:140
static ScenarioScanner _scanner
Scanner for scenarios.
Definition fios.cpp:655
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
Definition fios.cpp:563
Declarations for savegames operations.
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
Definition fios.cpp:419
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
Definition fios.cpp:526
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
Definition fios.cpp:466
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
Definition fios.cpp:563
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
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:323
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:427
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:119
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.
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:45
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition fios.cpp:737
FiosNumberedSaveName(const std::string &prefix)
Constructs FiosNumberedSaveName.
Definition fios.cpp:700
std::string Extension()
Generate an extension for a savegame name.
Definition fios.cpp:747
Elements of a file system that are recognized.
Definition fileio_type.h:63
DetailedFileType detailed
Detailed file type.
Definition fileio_type.h:65
Basic data to distinguish a scenario.
Definition fios.cpp:590
uint32_t scenid
ID for the scenario (generated by content).
Definition fios.cpp:591
MD5Hash md5sum
MD5 checksum of file.
Definition fios.cpp:592
std::string filename
filename of the file.
Definition fios.cpp:593
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:356
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:340