OpenTTD Source  20241121-master-g67a0fccfad
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"
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 #include <filesystem>
25 
26 #include "table/strings.h"
27 
28 #include "safeguards.h"
29 
30 /* Variables to display file lists */
31 static std::string *_fios_path = nullptr;
32 SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
33 
34 /* OS-specific functions are taken from their respective files (win32/unix .c) */
35 extern bool FiosIsRoot(const std::string &path);
36 extern bool FiosIsHiddenFile(const std::filesystem::path &path);
37 extern void FiosGetDrives(FileList &file_list);
38 
39 /* get the name of an oldstyle savegame */
40 extern std::string GetOldSaveGameName(const std::string &file);
41 
47 bool FiosItem::operator< (const FiosItem &other) const
48 {
49  int r = false;
50 
51  if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
52  r = ClampTo<int32_t>(this->mtime - other.mtime);
53  } else {
54  r = StrNaturalCompare((*this).title, other.title);
55  }
56  if (r == 0) return false;
57  return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
58 }
59 
66 void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop, bool show_dirs)
67 {
68  this->clear();
69 
70  assert(fop == SLO_LOAD || fop == SLO_SAVE);
71  switch (abstract_filetype) {
72  case FT_NONE:
73  break;
74 
75  case FT_SAVEGAME:
76  FiosGetSavegameList(fop, show_dirs, *this);
77  break;
78 
79  case FT_SCENARIO:
80  FiosGetScenarioList(fop, show_dirs, *this);
81  break;
82 
83  case FT_HEIGHTMAP:
84  FiosGetHeightmapList(fop, show_dirs, *this);
85  break;
86 
87  case FT_TOWN_DATA:
88  FiosGetTownDataList(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  case FIOS_TYPE_JSON:
188  return false;
189  }
190 
191  return true;
192 }
193 
201 static std::string FiosMakeFilename(const std::string *path, const char *name, const char *ext)
202 {
203  std::string buf;
204 
205  if (path != nullptr) {
206  buf = *path;
207  /* Remove trailing path separator, if present */
208  if (!buf.empty() && buf.back() == PATHSEPCHAR) buf.pop_back();
209  }
210 
211  /* Don't append the extension if it is already there */
212  const char *period = strrchr(name, '.');
213  if (period != nullptr && StrEqualsIgnoreCase(period, ext)) ext = "";
214 
215  return buf + PATHSEP + name + ext;
216 }
217 
223 std::string FiosMakeSavegameName(const char *name)
224 {
225  const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
226 
227  return FiosMakeFilename(_fios_path, name, extension);
228 }
229 
235 std::string FiosMakeHeightmapName(const char *name)
236 {
237  std::string ext(".");
239 
240  return FiosMakeFilename(_fios_path, name, ext.c_str());
241 }
242 
248 bool FiosDelete(const char *name)
249 {
250  return FioRemove(FiosMakeSavegameName(name));
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 {
260  FiosGetTypeAndNameProc *callback_proc;
262 public:
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 
296  std::error_code error_code;
297  auto write_time = std::filesystem::last_write_time(OTTD2FS(filename), error_code);
298  if (error_code) {
299  fios->mtime = 0;
300  } else {
301  fios->mtime = std::chrono::duration_cast<std::chrono::milliseconds>(write_time.time_since_epoch()).count();
302  }
303 
304  fios->type = type;
305  fios->name = filename;
306 
307  /* If the file doesn't have a title, use its filename */
308  if (title.empty()) {
309  auto ps = filename.rfind(PATHSEPCHAR);
310  fios->title = StrMakeValid(filename.substr((ps == std::string::npos ? 0 : ps + 1)));
311  } else {
312  fios->title = StrMakeValid(title);
313  };
314 
315  return true;
316 }
317 
318 
327 static void FiosGetFileList(SaveLoadOperation fop, bool show_dirs, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
328 {
329  size_t sort_start = 0;
330 
331  file_list.clear();
332 
333  assert(_fios_path != nullptr);
334 
335  if (show_dirs) {
336  /* A parent directory link exists if we are not in the root directory */
337  if (!FiosIsRoot(*_fios_path)) {
338  FiosItem &fios = file_list.emplace_back();
339  fios.type = FIOS_TYPE_PARENT;
340  fios.mtime = 0;
341  fios.name = "..";
342  SetDParamStr(0, "..");
343  fios.title = GetString(STR_SAVELOAD_PARENT_DIRECTORY);
344  sort_start = file_list.size();
345  }
346 
347  /* Show subdirectories */
348  std::error_code error_code;
349  for (const auto &dir_entry : std::filesystem::directory_iterator(OTTD2FS(*_fios_path), error_code)) {
350  if (!dir_entry.is_directory()) continue;
351  if (FiosIsHiddenFile(dir_entry) && dir_entry.path().filename() != PERSONAL_DIR) continue;
352 
353  FiosItem &fios = file_list.emplace_back();
354  fios.type = FIOS_TYPE_DIR;
355  fios.mtime = 0;
356  fios.name = FS2OTTD(dir_entry.path().filename());
357  SetDParamStr(0, fios.name + PATHSEP);
358  fios.title = GetString(STR_SAVELOAD_DIRECTORY);
359  }
360 
361  /* Sort the subdirs always by name, ascending, remember user-sorting order */
362  SortingBits order = _savegame_sort_order;
363  _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
364  std::sort(file_list.begin() + sort_start, file_list.end());
365  _savegame_sort_order = order;
366  }
367 
368  /* This is where to start sorting for the filenames */
369  sort_start = file_list.size();
370 
371  /* Show files */
372  FiosFileScanner scanner(fop, callback_proc, file_list);
373  if (subdir == NO_DIRECTORY) {
374  scanner.Scan({}, *_fios_path, false);
375  } else {
376  scanner.Scan({}, subdir, true, true);
377  }
378 
379  std::sort(file_list.begin() + sort_start, file_list.end());
380 
381  /* Show drives */
382  FiosGetDrives(file_list);
383 }
384 
392 static std::string GetFileTitle(const std::string &file, Subdirectory subdir)
393 {
394  auto f = FioFOpenFile(file + ".title", "r", subdir);
395  if (!f.has_value()) return {};
396 
397  char title[80];
398  size_t read = fread(title, 1, lengthof(title), *f);
399 
400  assert(read <= lengthof(title));
401  return StrMakeValid({title, read});
402 }
403 
413 std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
414 {
415  /* Show savegame files
416  * .SAV OpenTTD saved game
417  * .SS1 Transport Tycoon Deluxe preset game
418  * .SV1 Transport Tycoon Deluxe (Patch) saved game
419  * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
420 
421  if (StrEqualsIgnoreCase(ext, ".sav")) {
422  return { FIOS_TYPE_FILE, GetFileTitle(file, SAVE_DIR) };
423  }
424 
425  if (fop == SLO_LOAD) {
426  if (StrEqualsIgnoreCase(ext, ".ss1") || StrEqualsIgnoreCase(ext, ".sv1") ||
427  StrEqualsIgnoreCase(ext, ".sv2")) {
428  return { FIOS_TYPE_OLDFILE, GetOldSaveGameName(file) };
429  }
430  }
431 
432  return { FIOS_TYPE_INVALID, {} };
433 }
434 
442 void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
443 {
444  static std::optional<std::string> fios_save_path;
445 
446  if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
447 
448  _fios_path = &(*fios_save_path);
449 
450  FiosGetFileList(fop, show_dirs, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
451 }
452 
462 std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
463 {
464  /* Show scenario files
465  * .SCN OpenTTD style scenario file
466  * .SV0 Transport Tycoon Deluxe (Patch) scenario
467  * .SS0 Transport Tycoon Deluxe preset scenario */
468  if (StrEqualsIgnoreCase(ext, ".scn")) {
469  return { FIOS_TYPE_SCENARIO, GetFileTitle(file, SCENARIO_DIR) };
470 
471  }
472 
473  if (fop == SLO_LOAD) {
474  if (StrEqualsIgnoreCase(ext, ".sv0") || StrEqualsIgnoreCase(ext, ".ss0")) {
475  return { FIOS_TYPE_OLD_SCENARIO, GetOldSaveGameName(file) };
476  }
477  }
478 
479  return { FIOS_TYPE_INVALID, {} };
480 }
481 
489 void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
490 {
491  static std::optional<std::string> fios_scn_path;
492 
493  /* Copy the default path on first run or on 'New Game' */
494  if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
495 
496  _fios_path = &(*fios_scn_path);
497 
498  std::string base_path = FioFindDirectory(SCENARIO_DIR);
499  Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
500  FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
501 }
502 
503 std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, const std::string &file, const std::string_view ext)
504 {
505  /* Show heightmap files
506  * .PNG PNG Based heightmap files
507  * .BMP BMP Based heightmap files
508  */
509 
510  FiosType type = FIOS_TYPE_INVALID;
511 
512 #ifdef WITH_PNG
513  if (StrEqualsIgnoreCase(ext, ".png")) type = FIOS_TYPE_PNG;
514 #endif /* WITH_PNG */
515 
516  if (StrEqualsIgnoreCase(ext, ".bmp")) type = FIOS_TYPE_BMP;
517 
518  if (type == FIOS_TYPE_INVALID) return { FIOS_TYPE_INVALID, {} };
519 
520  TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
521  if (it != _tar_filelist[SCENARIO_DIR].end()) {
522  /* If the file is in a tar and that tar is not in a heightmap
523  * directory we are for sure not supposed to see it.
524  * Examples of this are pngs part of documentation within
525  * collections of NewGRFs or 32 bpp graphics replacement PNGs.
526  */
527  bool match = false;
528  for (Searchpath sp : _valid_searchpaths) {
529  std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
530 
531  if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
532  match = true;
533  break;
534  }
535  }
536 
537  if (!match) return { FIOS_TYPE_INVALID, {} };
538  }
539 
540  return { type, GetFileTitle(file, HEIGHTMAP_DIR) };
541 }
542 
549 void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
550 {
551  static std::optional<std::string> fios_hmap_path;
552 
553  if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
554 
555  _fios_path = &(*fios_hmap_path);
556 
557  std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
558  Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
559  FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
560 }
561 
568 static std::tuple<FiosType, std::string> FiosGetTownDataListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
569 {
570  if (fop == SLO_LOAD) {
571  if (StrEqualsIgnoreCase(ext, ".json")) {
572  return { FIOS_TYPE_JSON, GetFileTitle(file, SAVE_DIR) };
573  }
574  }
575 
576  return { FIOS_TYPE_INVALID, {} };
577 }
578 
585 void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
586 {
587  static std::optional<std::string> fios_town_data_path;
588 
589  if (!fios_town_data_path) fios_town_data_path = FioFindDirectory(HEIGHTMAP_DIR);
590 
591  _fios_path = &(*fios_town_data_path);
592 
593  std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
594  Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
595  FiosGetFileList(fop, show_dirs, &FiosGetTownDataListCallback, subdir, file_list);
596 }
597 
602 const char *FiosGetScreenshotDir()
603 {
604  static std::optional<std::string> fios_screenshot_path;
605 
606  if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
607 
608  return fios_screenshot_path->c_str();
609 }
610 
613  uint32_t scenid;
614  MD5Hash md5sum;
615  std::string filename;
616 
617  bool operator == (const ScenarioIdentifier &other) const
618  {
619  return this->scenid == other.scenid && this->md5sum == other.md5sum;
620  }
621 
622  bool operator != (const ScenarioIdentifier &other) const
623  {
624  return !(*this == other);
625  }
626 };
627 
631 class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
632  bool scanned;
633 public:
635  ScenarioScanner() : scanned(false) {}
636 
641  void Scan(bool rescan)
642  {
643  if (this->scanned && !rescan) return;
644 
645  this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
646  this->scanned = true;
647  }
648 
649  bool AddFile(const std::string &filename, size_t, const std::string &) override
650  {
651  auto f = FioFOpenFile(filename, "r", SCENARIO_DIR);
652  if (!f.has_value()) return false;
653 
655  int fret = fscanf(*f, "%u", &id.scenid);
656  if (fret != 1) return false;
657  id.filename = filename;
658 
659  Md5 checksum;
660  uint8_t buffer[1024];
661  size_t len, size;
662 
663  /* open the scenario file, but first get the name.
664  * This is safe as we check on extension which
665  * must always exist. */
666  f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
667  if (!f.has_value()) return false;
668 
669  /* calculate md5sum */
670  while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, *f)) != 0 && size != 0) {
671  size -= len;
672  checksum.Append(buffer, len);
673  }
674  checksum.Finish(id.md5sum);
675 
676  include(*this, id);
677  return true;
678  }
679 };
680 
683 
690 const char *FindScenario(const ContentInfo *ci, bool md5sum)
691 {
692  _scanner.Scan(false);
693 
694  for (ScenarioIdentifier &id : _scanner) {
695  if (md5sum ? (id.md5sum == ci->md5sum)
696  : (id.scenid == ci->unique_id)) {
697  return id.filename.c_str();
698  }
699  }
700 
701  return nullptr;
702 }
703 
710 bool HasScenario(const ContentInfo *ci, bool md5sum)
711 {
712  return (FindScenario(ci, md5sum) != nullptr);
713 }
714 
719 {
720  _scanner.Scan(true);
721 }
722 
727 FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1)
728 {
729  static std::optional<std::string> _autosave_path;
730  if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR);
731 
732  static std::string _prefix;
733 
734  /* Callback for FiosFileScanner. */
735  static FiosGetTypeAndNameProc *proc = [](SaveLoadOperation, const std::string &file, const std::string_view ext) {
736  if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{});
737  return std::tuple(FIOS_TYPE_INVALID, std::string{});
738  };
739 
740  /* Prefix to check in the callback. */
741  _prefix = *_autosave_path + this->prefix;
742 
743  /* Get the save list. */
744  FileList list;
745  FiosFileScanner scanner(SLO_SAVE, proc, list);
746  scanner.Scan(".sav", *_autosave_path, false);
747 
748  /* Find the number for the most recent save, if any. */
749  if (list.begin() != list.end()) {
750  SortingBits order = _savegame_sort_order;
751  _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
752  std::sort(list.begin(), list.end());
753  _savegame_sort_order = order;
754 
755  std::string_view name = list.begin()->title;
756  std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
757  }
758 }
759 
765 {
766  if (++this->number >= _settings_client.gui.max_num_autosaves) this->number = 0;
767  return fmt::format("{}{}.sav", this->prefix, this->number);
768 }
769 
775 {
776  return fmt::format("-{}.sav", this->prefix);
777 }
List of file information.
Definition: fios.h:88
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
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:66
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:1114
Scanner to scan for a particular type of FIOS file.
Definition: fios.cpp:258
FiosFileScanner(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, FileList &file_list)
Create the scanner.
Definition: fios.cpp:269
SaveLoadOperation fop
The kind of file we are looking for.
Definition: fios.cpp:259
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
FileList & file_list
Destination of the found files.
Definition: fios.cpp:261
FiosGetTypeAndNameProc * callback_proc
Callback to check whether the file may be added.
Definition: fios.cpp:260
Scanner to find the unique IDs of scenarios.
Definition: fios.cpp:631
bool scanned
Whether we've already scanned.
Definition: fios.cpp:632
void Scan(bool rescan)
Scan, but only if it's needed.
Definition: fios.cpp:641
bool AddFile(const std::string &filename, size_t, const std::string &) override
Add a file with the given filename.
Definition: fios.cpp:649
ScenarioScanner()
Initialise.
Definition: fios.cpp:635
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:328
std::optional< FileHandle > 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:242
Functions for Standard In/Out file operations.
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
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:73
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:53
@ SLO_SAVE
File is being saved.
Definition: fileio_type.h:56
@ SLO_LOAD
File is being loaded.
Definition: fileio_type.h:55
Searchpath
Types of searchpaths OpenTTD might use.
Definition: fileio_type.h:139
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:133
@ SCREENSHOT_DIR
Subdirectory for all screenshots.
Definition: fileio_type.h:130
@ SCENARIO_DIR
Base directory for all scenarios.
Definition: fileio_type.h:119
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:117
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:120
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:118
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition: fios.cpp:710
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:392
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:690
std::string FiosGetCurrentPath()
Get the current path/working directory.
Definition: fios.cpp:133
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:602
std::string FiosMakeSavegameName(const char *name)
Make a save game or scenario filename from a name.
Definition: fios.cpp:223
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of savegames.
Definition: fios.cpp:442
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of heightmaps.
Definition: fios.cpp:549
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:327
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition: fios.cpp:718
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of scenarios.
Definition: fios.cpp:489
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:201
std::string FiosMakeHeightmapName(const char *name)
Construct a filename for a height map.
Definition: fios.cpp:235
std::tuple< FiosType, std::string > FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
Callback for FiosGetFileList.
Definition: fios.cpp:413
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:248
std::tuple< FiosType, std::string > FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
Callback for FiosGetFileList.
Definition: fios.cpp:462
static std::tuple< FiosType, std::string > FiosGetTownDataListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext)
Callback for FiosGetTownDataList.
Definition: fios.cpp:568
bool FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at #_fios_path.
Definition: fios.cpp:143
static ScenarioScanner _scanner
Scanner for scenarios.
Definition: fios.cpp:682
void FiosGetTownDataList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
Get a list of town data files.
Definition: fios.cpp:585
Declarations for savegames operations.
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
Part of the network protocol handling content distribution.
A number of safeguards to prevent using unsafe methods.
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:554
Functions to make screenshots.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
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:280
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:107
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:347
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:589
Functions related to low-level strings.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:319
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:357
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:79
bool operator<(const FiosItem &other) const
Compare two FiosItem's.
Definition: fios.cpp:47
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition: fios.cpp:764
FiosNumberedSaveName(const std::string &prefix)
Constructs FiosNumberedSaveName.
Definition: fios.cpp:727
std::string Extension()
Generate an extension for a savegame name.
Definition: fios.cpp:774
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:612
uint32_t scenid
ID for the scenario (generated by content).
Definition: fios.cpp:613
MD5Hash md5sum
MD5 checksum of file.
Definition: fios.cpp:614
std::string filename
filename of the file.
Definition: fios.cpp:615
Structs, typedefs and macros used for TAR file handling.
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition: win32.cpp:354
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
Definition: win32.cpp:337