OpenTTD Source: src/fios.cpp Source File

OpenTTD Source 14.1

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"
15 #include "fileio_func.h"
16 #include "fios.h"
17 #include "network/network_content.h"
18 #include "screenshot.h"
19 #include "string_func.h"
20 #include "strings_func.h"
21 #include "tar_type.h"
22 #include <sys/stat.h>
23 #include <charconv>
24
25 #ifndef _WIN32
26 # include <unistd.h>
27 #endif /* _WIN32 */
28
29 #include "table/strings.h"
30
31 #include "safeguards.h"
32
33 /* Variables to display file lists */
34 static std::string *_fios_path = nullptr;
35 SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
36
37 /* OS-specific functions are taken from their respective files (win32/unix .c) */
38 extern bool FiosIsRoot(const std::string &path);
39 extern bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb);
40 extern bool FiosIsHiddenFile(const struct dirent *ent);
41 extern void FiosGetDrives(FileList &file_list);
42
43 /* get the name of an oldstyle savegame */
44 extern std::string GetOldSaveGameName(const std::string &file);
45
51 bool FiosItem::operator< (const FiosItem &other) const
52 {
53 int r = false;
54
55 if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
56 r = this->mtime - other.mtime;
57 } else {
58 r = StrNaturalCompare((*this).title, other.title);
59 }
60 if (r == 0) return false;
61 return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
62 }
63
70 void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop, bool show_dirs)
71 {
72 this->clear();
73
74 assert(fop == SLO_LOAD || fop == SLO_SAVE);
75 switch (abstract_filetype) {
76 case FT_NONE:
77 break;
78
79 case FT_SAVEGAME:
80 FiosGetSavegameList(fop, show_dirs, *this);
81 break;
82
83 case FT_SCENARIO:
84 FiosGetScenarioList(fop, show_dirs, *this);
85 break;
86
87 case FT_HEIGHTMAP:
88 FiosGetHeightmapList(fop, show_dirs, *this);
89 break;
90
91 default:
92 NOT_REACHED();
93 }
94 }
95
102 const FiosItem *FileList::FindItem(const std::string_view file)
103 {
104 for (const auto &it : *this) {
105 const FiosItem *item = &it;
106 if (file == item->name) return item;
107 if (file == item->title) return item;
108 }
109
110 /* If no name matches, try to parse it as number */
111 char *endptr;
112 int i = std::strtol(file.data(), &endptr, 10);
113 if (file.data() == endptr || *endptr != '\0') i = -1;
114
115 if (IsInsideMM(i, 0, this->size())) return &this->at(i);
116
117 /* As a last effort assume it is an OpenTTD savegame and
118 * that the ".sav" part was not given. */
119 std::string long_file(file);
120 long_file += ".sav";
121 for (const auto &it : *this) {
122 const FiosItem *item = &it;
123 if (long_file == item->name) return item;
124 if (long_file == item->title) return item;
125 }
126
127 return nullptr;
128 }
129
133 std::string FiosGetCurrentPath()
134 {
135 return *_fios_path;
136 }
137
143 bool FiosBrowseTo(const FiosItem *item)
144 {
145 switch (item->type) {
146 case FIOS_TYPE_DRIVE:
147 #if defined(_WIN32)
148 assert(_fios_path != nullptr);
149 *_fios_path = std::string{ item->title, 0, 1 } + ":" PATHSEP;
150 #endif
151 break;
152
153 case FIOS_TYPE_INVALID:
154 break;
155
156 case FIOS_TYPE_PARENT: {
157 assert(_fios_path != nullptr);
158 auto s = _fios_path->find_last_of(PATHSEPCHAR);
159 if (s != std::string::npos && s != 0) {
160 _fios_path->erase(s); // Remove last path separator character, so we can go up one level.
161 }
162
163 s = _fios_path->find_last_of(PATHSEPCHAR);
164 if (s != std::string::npos) {
165 _fios_path->erase(s + 1); // go up a directory
166 }
167 break;
168 }
169
170 case FIOS_TYPE_DIR:
171 assert(_fios_path != nullptr);
172 *_fios_path += item->name;
173 *_fios_path += PATHSEP;
174 break;
175
176 case FIOS_TYPE_DIRECT:
177 assert(_fios_path != nullptr);
178 *_fios_path = item->name;
179 break;
180
181 case FIOS_TYPE_FILE:
182 case FIOS_TYPE_OLDFILE:
183 case FIOS_TYPE_SCENARIO:
184 case FIOS_TYPE_OLD_SCENARIO:
185 case FIOS_TYPE_PNG:
186 case FIOS_TYPE_BMP:
187 return false;
188 }
189
190 return true;
191 }
192
200 static std::string FiosMakeFilename(const std::string *path, const char *name, const char *ext)
201 {
202 std::string buf;
203
204 if (path != nullptr) {
205 buf = *path;
206 /* Remove trailing path separator, if present */
207 if (!buf.empty() && buf.back() == PATHSEPCHAR) buf.pop_back();
208 }
209
210 /* Don't append the extension if it is already there */
211 const char *period = strrchr(name, '.');
212 if (period != nullptr && StrEqualsIgnoreCase(period, ext)) ext = "";
213
214 return buf + PATHSEP + name + ext;
215 }
216
222 std::string FiosMakeSavegameName(const char *name)
223 {
224 const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
225
226 return FiosMakeFilename(_fios_path, name, extension);
227 }
228
234 std::string FiosMakeHeightmapName(const char *name)
235 {
236 std::string ext(".");
237 ext += GetCurrentScreenshotExtension();
238
239 return FiosMakeFilename(_fios_path, name, ext.c_str());
240 }
241
247 bool FiosDelete(const char *name)
248 {
249 std::string filename = FiosMakeSavegameName(name);
250 return unlink(filename.c_str()) == 0;
251 }
252
253 typedef std::tuple<FiosType, std::string> FiosGetTypeAndNameProc(SaveLoadOperation fop, const std::string &filename, const std::string_view ext);
254
258 class FiosFileScanner : public FileScanner {
259 SaveLoadOperation fop;
260 FiosGetTypeAndNameProc *callback_proc;
261 FileList &file_list;
262 public:
269 FiosFileScanner(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, FileList &file_list) :
270 fop(fop), callback_proc(callback_proc), file_list(file_list)
271 {}
272
273 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
274 };
275
281 bool FiosFileScanner::AddFile(const std::string &filename, size_t, const std::string &)
282 {
283 auto sep = filename.rfind('.');
284 if (sep == std::string::npos) return false;
285 std::string ext = filename.substr(sep);
286
287 auto [type, title] = this->callback_proc(this->fop, filename, ext);
288 if (type == FIOS_TYPE_INVALID) return false;
289
290 for (const auto &fios : file_list) {
291 if (filename == fios.name) return false;
292 }
293
294 FiosItem *fios = &file_list.emplace_back();
295 #ifdef _WIN32
296 // Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
297 HANDLE fh = CreateFile(OTTD2FS(filename).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
298
299 if (fh != INVALID_HANDLE_VALUE) {
300 FILETIME ft;
301 ULARGE_INTEGER ft_int64;
302
303 if (GetFileTime(fh, nullptr, nullptr, &ft) != 0) {
304 ft_int64.HighPart = ft.dwHighDateTime;
305 ft_int64.LowPart = ft.dwLowDateTime;
306
307 // Convert from hectonanoseconds since 01/01/1601 to seconds since 01/01/1970
308 fios->mtime = ft_int64.QuadPart / 10000000ULL - 11644473600ULL;
309 } else {
310 fios->mtime = 0;
311 }
312
313 CloseHandle(fh);
314 #else
315 struct stat sb;
316 if (stat(filename.c_str(), &sb) == 0) {
317 fios->mtime = sb.st_mtime;
318 #endif
319 } else {
320 fios->mtime = 0;
321 }
322
323 fios->type = type;
324 fios->name = filename;
325
326 /* If the file doesn't have a title, use its filename */
327 if (title.empty()) {
328 auto ps = filename.rfind(PATHSEPCHAR);
329 fios->title = StrMakeValid(filename.substr((ps == std::string::npos ? 0 : ps + 1)));
330 } else {
331 fios->title = StrMakeValid(title);
332 };
333
334 return true;
335 }
336
337
346 static void FiosGetFileList(SaveLoadOperation fop, bool show_dirs, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
347 {
348 struct stat sb;
349 struct dirent *dirent;
350 DIR *dir;
351 FiosItem *fios;
352 size_t sort_start;
353
354 file_list.clear();
355
356 assert(_fios_path != nullptr);
357
358 /* A parent directory link exists if we are not in the root directory */
359 if (show_dirs && !FiosIsRoot(*_fios_path)) {
360 fios = &file_list.emplace_back();
361 fios->type = FIOS_TYPE_PARENT;
362 fios->mtime = 0;
363 fios->name = "..";
364 SetDParamStr(0, "..");
365 fios->title = GetString(STR_SAVELOAD_PARENT_DIRECTORY);
366 }
367
368 /* Show subdirectories */
369 if (show_dirs && (dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
370 while ((dirent = readdir(dir)) != nullptr) {
371 std::string d_name = FS2OTTD(dirent->d_name);
372
373 /* found file must be directory, but not '.' or '..' */
374 if (FiosIsValidFile(*_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
375 (!FiosIsHiddenFile(dirent) || StrStartsWithIgnoreCase(PERSONAL_DIR, d_name)) &&
376 d_name != "." && d_name != "..") {
377 fios = &file_list.emplace_back();
378 fios->type = FIOS_TYPE_DIR;
379 fios->mtime = 0;
380 fios->name = d_name;
381 SetDParamStr(0, fios->name + PATHSEP);
382 fios->title = GetString(STR_SAVELOAD_DIRECTORY);
383 }
384 }
385 closedir(dir);
386 }
387
388 /* Sort the subdirs always by name, ascending, remember user-sorting order */
389 if (show_dirs) {
390 SortingBits order = _savegame_sort_order;
391 _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
392 std::sort(file_list.begin(), file_list.end());
393 _savegame_sort_order = order;
394 }
395
396 /* This is where to start sorting for the filenames */
397 sort_start = file_list.size();
398
399 /* Show files */
400 FiosFileScanner scanner(fop, callback_proc, file_list);
401 if (subdir == NO_DIRECTORY) {
402 scanner.Scan(nullptr, *_fios_path, false);
403 } else {
404 scanner.Scan(nullptr, subdir, true, true);
405 }
406
407 std::sort(file_list.begin() + sort_start, file_list.end());
408
409 /* Show drives */
410 FiosGetDrives(file_list);
411
412 file_list.shrink_to_fit();
413 }
414
422 static std::string GetFileTitle(const std::string &file, Subdirectory subdir)
423 {
424 FILE *f = FioFOpenFile(file + ".title", "r", subdir);
425 if (f == nullptr) return {};
426
427 char title[80];
428 size_t read = fread(title, 1, lengthof(title), f);
429 FioFCloseFile(f);
430
431 assert(read <= lengthof(title));
432 return StrMakeValid({title, read});
433 }
434
444 std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
445 {
446 /* Show savegame files
447 * .SAV OpenTTD saved game
448 * .SS1 Transport Tycoon Deluxe preset game
449 * .SV1 Transport Tycoon Deluxe (Patch) saved game
450 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
451
452 if (StrEqualsIgnoreCase(ext, ".sav")) {
453 return { FIOS_TYPE_FILE, GetFileTitle(file, SAVE_DIR) };
454 }
455
456 if (fop == SLO_LOAD) {
457 if (StrEqualsIgnoreCase(ext, ".ss1") || StrEqualsIgnoreCase(ext, ".sv1") ||
458 StrEqualsIgnoreCase(ext, ".sv2")) {
459 return { FIOS_TYPE_OLDFILE, GetOldSaveGameName(file) };
460 }
461 }
462
463 return { FIOS_TYPE_INVALID, {} };
464 }
465
473 void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
474 {
475 static std::optional<std::string> fios_save_path;
476
477 if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
478
479 _fios_path = &(*fios_save_path);
480
481 FiosGetFileList(fop, show_dirs, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
482 }
483
493 std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
494 {
495 /* Show scenario files
496 * .SCN OpenTTD style scenario file
497 * .SV0 Transport Tycoon Deluxe (Patch) scenario
498 * .SS0 Transport Tycoon Deluxe preset scenario */
499 if (StrEqualsIgnoreCase(ext, ".scn")) {
500 return { FIOS_TYPE_SCENARIO, GetFileTitle(file, SCENARIO_DIR) };
501
502 }
503
504 if (fop == SLO_LOAD) {
505 if (StrEqualsIgnoreCase(ext, ".sv0") || StrEqualsIgnoreCase(ext, ".ss0")) {
506 return { FIOS_TYPE_OLD_SCENARIO, GetOldSaveGameName(file) };
507 }
508 }
509
510 return { FIOS_TYPE_INVALID, {} };
511 }
512
520 void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
521 {
522 static std::optional<std::string> fios_scn_path;
523
524 /* Copy the default path on first run or on 'New Game' */
525 if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
526
527 _fios_path = &(*fios_scn_path);
528
529 std::string base_path = FioFindDirectory(SCENARIO_DIR);
530 Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
531 FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
532 }
533
534 std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, const std::string &file, const std::string_view ext)
535 {
536 /* Show heightmap files
537 * .PNG PNG Based heightmap files
538 * .BMP BMP Based heightmap files
539 */
540
541 FiosType type = FIOS_TYPE_INVALID;
542
543 #ifdef WITH_PNG
544 if (StrEqualsIgnoreCase(ext, ".png")) type = FIOS_TYPE_PNG;
545 #endif /* WITH_PNG */
546
547 if (StrEqualsIgnoreCase(ext, ".bmp")) type = FIOS_TYPE_BMP;
548
549 if (type == FIOS_TYPE_INVALID) return { FIOS_TYPE_INVALID, {} };
550
551 TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
552 if (it != _tar_filelist[SCENARIO_DIR].end()) {
553 /* If the file is in a tar and that tar is not in a heightmap
554 * directory we are for sure not supposed to see it.
555 * Examples of this are pngs part of documentation within
556 * collections of NewGRFs or 32 bpp graphics replacement PNGs.
557 */
558 bool match = false;
559 for (Searchpath sp : _valid_searchpaths) {
560 std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
561
562 if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
563 match = true;
564 break;
565 }
566 }
567
568 if (!match) return { FIOS_TYPE_INVALID, {} };
569 }
570
571 return { type, GetFileTitle(file, HEIGHTMAP_DIR) };
572 }
573
580 void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
581 {
582 static std::optional<std::string> fios_hmap_path;
583
584 if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
585
586 _fios_path = &(*fios_hmap_path);
587
588 std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
589 Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
590 FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
591 }
592
597 const char *FiosGetScreenshotDir()
598 {
599 static std::optional<std::string> fios_screenshot_path;
600
601 if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
602
603 return fios_screenshot_path->c_str();
604 }
605
607 struct ScenarioIdentifier {
608 uint32_t scenid;
609 MD5Hash md5sum;
610 std::string filename;
611
612 bool operator == (const ScenarioIdentifier &other) const
613 {
614 return this->scenid == other.scenid && this->md5sum == other.md5sum;
615 }
616
617 bool operator != (const ScenarioIdentifier &other) const
618 {
619 return !(*this == other);
620 }
621 };
622
626 class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
627 bool scanned;
628 public:
630 ScenarioScanner() : scanned(false) {}
631
636 void Scan(bool rescan)
637 {
638 if (this->scanned && !rescan) return;
639
640 this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
641 this->scanned = true;
642 }
643
644 bool AddFile(const std::string &filename, size_t, const std::string &) override
645 {
646 FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
647 if (f == nullptr) return false;
648
649 ScenarioIdentifier id;
650 int fret = fscanf(f, "%u", &id.scenid);
651 FioFCloseFile(f);
652 if (fret != 1) return false;
653 id.filename = filename;
654
655 Md5 checksum;
656 uint8_t buffer[1024];
657 size_t len, size;
658
659 /* open the scenario file, but first get the name.
660 * This is safe as we check on extension which
661 * must always exist. */
662 f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
663 if (f == nullptr) return false;
664
665 /* calculate md5sum */
666 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
667 size -= len;
668 checksum.Append(buffer, len);
669 }
670 checksum.Finish(id.md5sum);
671
672 FioFCloseFile(f);
673
674 include(*this, id);
675 return true;
676 }
677 };
678
680 static ScenarioScanner _scanner;
681
688 const char *FindScenario(const ContentInfo *ci, bool md5sum)
689 {
690 _scanner.Scan(false);
691
692 for (ScenarioIdentifier &id : _scanner) {
693 if (md5sum ? (id.md5sum == ci->md5sum)
694 : (id.scenid == ci->unique_id)) {
695 return id.filename.c_str();
696 }
697 }
698
699 return nullptr;
700 }
701
708 bool HasScenario(const ContentInfo *ci, bool md5sum)
709 {
710 return (FindScenario(ci, md5sum) != nullptr);
711 }
712
716 void ScanScenarios()
717 {
718 _scanner.Scan(true);
719 }
720
725 FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1)
726 {
727 static std::optional<std::string> _autosave_path;
728 if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR);
729
730 static std::string _prefix;
731
732 /* Callback for FiosFileScanner. */
733 static FiosGetTypeAndNameProc *proc = [](SaveLoadOperation, const std::string &file, const std::string_view ext) {
734 if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
735 return std::tuple(FIOS_TYPE_INVALID, std::string{});
736 };
737
738 /* Prefix to check in the callback. */
739 _prefix = *_autosave_path + this->prefix;
740
741 /* Get the save list. */
742 FileList list;
743 FiosFileScanner scanner(SLO_SAVE, proc, list);
744 scanner.Scan(".sav", _autosave_path->c_str(), false);
745
746 /* Find the number for the most recent save, if any. */
747 if (list.begin() != list.end()) {
748 SortingBits order = _savegame_sort_order;
749 _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
750 std::sort(list.begin(), list.end());
751 _savegame_sort_order = order;
752
753 std::string_view name = list.begin()->title;
754 std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
755 }
756 }
757
762 std::string FiosNumberedSaveName::Filename()
763 {
764 if (++this->number >= _settings_client.gui.max_num_autosaves) this->number = 0;
765 return fmt::format("{}{}.sav", this->prefix, this->number);
766 }
767
772 std::string FiosNumberedSaveName::Extension()
773 {
774 return fmt::format("-{}.sav", this->prefix);
775 }
network_content.h
StrStartsWithIgnoreCase
bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix)
Check whether the given string starts with the given prefix, ignoring case.
Definition: string.cpp:300
FiosFileScanner::FiosFileScanner
FiosFileScanner(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, FileList &file_list)
Create the scanner.
Definition: fios.cpp:269
ScenarioIdentifier::md5sum
MD5Hash md5sum
MD5 checksum of file.
Definition: fios.cpp:609
FT_SCENARIO
@ FT_SCENARIO
old or new scenario
Definition: fileio_type.h:19
SAVE_DIR
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:110
IsInsideMM
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition: math_func.hpp:268
ttd_opendir
DIR * ttd_opendir(const char *path)
A wrapper around opendir() which will convert the string from OPENTTD encoding to that of the filesys...
Definition: fileio_func.h:111
FiosGetScreenshotDir
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:597
ScanScenarios
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition: fios.cpp:716
FiosNumberedSaveName::FiosNumberedSaveName
FiosNumberedSaveName(const std::string &prefix)
Constructs FiosNumberedSaveName.
Definition: fios.cpp:725
SaveLoadOperation
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:47
SCREENSHOT_DIR
@ SCREENSHOT_DIR
Subdirectory for all screenshots.
Definition: fileio_type.h:123
Searchpath
Searchpath
Types of searchpaths OpenTTD might use.
Definition: fileio_type.h:132
FileScanner::Scan
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition: fileio.cpp:1223
StrMakeValid
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition: string.cpp:114
HEIGHTMAP_DIR
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:113
fileio_func.h
StrNaturalCompare
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:585
AUTOSAVE_DIR
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:111
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:54
fios.h
FiosGetCurrentPath
std::string FiosGetCurrentPath()
Get the current path/working directory.
Definition: fios.cpp:133
ContentInfo::md5sum
MD5Hash md5sum
The MD5 checksum.
Definition: tcp_content_type.h:72
FileList
List of file information.
Definition: fios.h:88
FiosFileScanner::fop
SaveLoadOperation fop
The kind of file we are looking for.
Definition: fios.cpp:259
ScenarioScanner::scanned
bool scanned
Whether we've already scanned.
Definition: fios.cpp:627
FileList::FindItem
const FiosItem * FindItem(const std::string_view file)
Find file information of a file by its name from the file list.
Definition: fios.cpp:102
screenshot.h
include
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Definition: container_func.hpp:24
AbstractFileType
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:16
FiosFileScanner::file_list
FileList & file_list
Destination of the found files.
Definition: fios.cpp:261
SLO_LOAD
@ SLO_LOAD
File is being loaded.
Definition: fileio_type.h:49
SLO_SAVE
@ SLO_SAVE
File is being saved.
Definition: fileio_type.h:50
FiosMakeHeightmapName
std::string FiosMakeHeightmapName(const char *name)
Construct a filename for a height map.
Definition: fios.cpp:234
FiosDelete
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:247
FS2OTTD
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
Definition: win32.cpp:462
FiosFileScanner::AddFile
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:281
ScenarioScanner::Scan
void Scan(bool rescan)
Scan, but only if it's needed.
Definition: fios.cpp:636
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:264
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content_type.h:52
tar_type.h
FiosItem
Deals with finding savegames.
Definition: fios.h:79
operator==
bool operator==(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Compare two MultiMap iterators.
Definition: multimap.hpp:200
safeguards.h
FiosMakeFilename
static std::string FiosMakeFilename(const std::string *path, const char *name, const char *ext)
Construct a filename from its components in destination buffer buf.
Definition: fios.cpp:200
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:303
FiosGetSavegameListCallback
std::tuple< FiosType, std::string > FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
Callback for FiosGetFileList.
Definition: fios.cpp:444
SCENARIO_DIR
@ SCENARIO_DIR
Base directory for all scenarios.
Definition: fileio_type.h:112
FileScanner::subdir
Subdirectory subdir
The current sub directory we are searching through.
Definition: fileio_func.h:39
FiosGetSavegameList
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
Definition: fios.cpp:473
GUISettings::max_num_autosaves
byte max_num_autosaves
controls how many autosavegames are made before the game starts to overwrite (names them 0 to max_num...
Definition: settings_type.h:166
stdafx.h
FT_SAVEGAME
@ FT_SAVEGAME
old or new savegame
Definition: fileio_type.h:18
ScenarioIdentifier::scenid
uint32_t scenid
ID for the scenario (generated by content).
Definition: fios.cpp:608
ScenarioScanner::ScenarioScanner
ScenarioScanner()
Initialise.
Definition: fios.cpp:630
FiosGetScenarioList
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
Definition: fios.cpp:520
FiosGetFileList
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:346
string_func.h
strings_func.h
FT_NONE
@ FT_NONE
nothing to do
Definition: fileio_type.h:17
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:126
DIR
Definition: win32.cpp:65
FiosGetScenarioListCallback
std::tuple< FiosType, std::string > FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
Callback for FiosGetFileList.
Definition: fios.cpp:493
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:327
StrEqualsIgnoreCase
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition: string.cpp:366
ScenarioScanner::AddFile
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
Definition: fios.cpp:644
FT_HEIGHTMAP
@ FT_HEIGHTMAP
heightmap file
Definition: fileio_type.h:20
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
SetDParamStr
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:352
FiosGetHeightmapList
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
Definition: fios.cpp:580
FiosType
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:67
FiosBrowseTo
bool FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at #_fios_path.
Definition: fios.cpp:143
ScenarioIdentifier
Basic data to distinguish a scenario.
Definition: fios.cpp:607
GetCurrentScreenshotExtension
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:579
FiosFileScanner
Scanner to scan for a particular type of FIOS file.
Definition: fios.cpp:258
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:37
_scanner
static ScenarioScanner _scanner
Scanner for scenarios.
Definition: fios.cpp:680
operator!=
bool operator!=(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Inverse of operator==().
Definition: multimap.hpp:217
FiosNumberedSaveName::Extension
std::string Extension()
Generate an extension for a savegame name.
Definition: fios.cpp:772
FiosMakeSavegameName
std::string FiosMakeSavegameName(const char *name)
Make a save game or scenario filename from a name.
Definition: fios.cpp:222
GetFileTitle
static std::string GetFileTitle(const std::string &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:422
ScenarioScanner
Scanner to find the unique IDs of scenarios.
Definition: fios.cpp:626
FindScenario
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:688
FiosNumberedSaveName::Filename
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition: fios.cpp:762
FileList::BuildFileList
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:70
OTTD2FS
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition: win32.cpp:479
ContentInfo::unique_id
uint32_t unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content_type.h:71
ScenarioIdentifier::filename
std::string filename
filename of the file.
Definition: fios.cpp:610
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:635
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:149
FiosFileScanner::callback_proc
FiosGetTypeAndNameProc * callback_proc
Callback to check whether the file may be added.
Definition: fios.cpp:260
HasScenario
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition: fios.cpp:708
FiosItem::operator<
bool operator<(const FiosItem &other) const
Compare two FiosItem's.
Definition: fios.cpp:51