OpenTTD Source  12.0-beta2
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 <functional>
24 #include <optional>
25 #include <charconv>
26 
27 #ifndef _WIN32
28 # include <unistd.h>
29 #endif /* _WIN32 */
30 
31 #include "table/strings.h"
32 
33 #include "safeguards.h"
34 
35 /* Variables to display file lists */
36 static std::string *_fios_path = nullptr;
37 SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
38 
39 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
40 extern bool FiosIsRoot(const char *path);
41 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
42 extern bool FiosIsHiddenFile(const struct dirent *ent);
43 extern void FiosGetDrives(FileList &file_list);
44 extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
45 
46 /* get the name of an oldstyle savegame */
47 extern void GetOldSaveGameName(const std::string &file, char *title, const char *last);
48 
54 bool FiosItem::operator< (const FiosItem &other) const
55 {
56  int r = false;
57 
58  if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
59  r = (*this).mtime - other.mtime;
60  } else {
61  r = strnatcmp((*this).title, other.title);
62  }
63  if (r == 0) return false;
64  return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
65 }
66 
73 {
74  this->clear();
75 
76  assert(fop == SLO_LOAD || fop == SLO_SAVE);
77  switch (abstract_filetype) {
78  case FT_NONE:
79  break;
80 
81  case FT_SAVEGAME:
82  FiosGetSavegameList(fop, *this);
83  break;
84 
85  case FT_SCENARIO:
86  FiosGetScenarioList(fop, *this);
87  break;
88 
89  case FT_HEIGHTMAP:
90  FiosGetHeightmapList(fop, *this);
91  break;
92 
93  default:
94  NOT_REACHED();
95  }
96 }
97 
104 const FiosItem *FileList::FindItem(const char *file)
105 {
106  for (const auto &it : *this) {
107  const FiosItem *item = &it;
108  if (strcmp(file, item->name) == 0) return item;
109  if (strcmp(file, item->title) == 0) return item;
110  }
111 
112  /* If no name matches, try to parse it as number */
113  char *endptr;
114  int i = strtol(file, &endptr, 10);
115  if (file == endptr || *endptr != '\0') i = -1;
116 
117  if (IsInsideMM(i, 0, this->size())) return &this->at(i);
118 
119  /* As a last effort assume it is an OpenTTD savegame and
120  * that the ".sav" part was not given. */
121  char long_file[MAX_PATH];
122  seprintf(long_file, lastof(long_file), "%s.sav", file);
123  for (const auto &it : *this) {
124  const FiosItem *item = &it;
125  if (strcmp(long_file, item->name) == 0) return item;
126  if (strcmp(long_file, item->title) == 0) return item;
127  }
128 
129  return nullptr;
130 }
131 
139 StringID FiosGetDescText(const char **path, uint64 *total_free)
140 {
141  *path = _fios_path->c_str();
142  return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
143 }
144 
150 const char *FiosBrowseTo(const FiosItem *item)
151 {
152  switch (item->type) {
153  case FIOS_TYPE_DRIVE:
154 #if defined(_WIN32) || defined(__OS2__)
155  assert(_fios_path != nullptr);
156  *_fios_path = std::string{ item->title[0] } + ":" PATHSEP;
157 #endif
158  break;
159 
160  case FIOS_TYPE_INVALID:
161  break;
162 
163  case FIOS_TYPE_PARENT: {
164  assert(_fios_path != nullptr);
165  auto s = _fios_path->find_last_of(PATHSEPCHAR);
166  if (s != std::string::npos && s != 0) {
167  _fios_path->erase(s); // Remove last path separator character, so we can go up one level.
168  }
169 
170  s = _fios_path->find_last_of(PATHSEPCHAR);
171  if (s != std::string::npos) {
172  _fios_path->erase(s + 1); // go up a directory
173  }
174  break;
175  }
176 
177  case FIOS_TYPE_DIR:
178  assert(_fios_path != nullptr);
179  *_fios_path += item->name;
180  *_fios_path += PATHSEP;
181  break;
182 
183  case FIOS_TYPE_DIRECT:
184  assert(_fios_path != nullptr);
185  *_fios_path = item->name;
186  break;
187 
188  case FIOS_TYPE_FILE:
189  case FIOS_TYPE_OLDFILE:
190  case FIOS_TYPE_SCENARIO:
191  case FIOS_TYPE_OLD_SCENARIO:
192  case FIOS_TYPE_PNG:
193  case FIOS_TYPE_BMP:
194  return item->name;
195  }
196 
197  return nullptr;
198 }
199 
207 static std::string FiosMakeFilename(const std::string *path, const char *name, const char *ext)
208 {
209  std::string buf;
210 
211  if (path != nullptr) {
212  buf = *path;
213  /* Remove trailing path separator, if present */
214  if (!buf.empty() && buf.back() == PATHSEPCHAR) buf.pop_back();
215  }
216 
217  /* Don't append the extension if it is already there */
218  const char *period = strrchr(name, '.');
219  if (period != nullptr && strcasecmp(period, ext) == 0) ext = "";
220 
221  return buf + PATHSEP + name + ext;
222 }
223 
231 std::string FiosMakeSavegameName(const char *name)
232 {
233  const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
234 
235  return FiosMakeFilename(_fios_path, name, extension);
236 }
237 
243 std::string FiosMakeHeightmapName(const char *name)
244 {
245  std::string ext(".");
247 
248  return FiosMakeFilename(_fios_path, name, ext.c_str());
249 }
250 
256 bool FiosDelete(const char *name)
257 {
258  std::string filename = FiosMakeSavegameName(name);
259  return unlink(filename.c_str()) == 0;
260 }
261 
262 typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const std::string &filename, const char *ext, char *title, const char *last);
263 
267 class FiosFileScanner : public FileScanner {
269  fios_getlist_callback_proc *callback_proc;
271 public:
280  {}
281 
282  bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
283 };
284 
291 bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
292 {
293  auto sep = filename.rfind('.');
294  if (sep == std::string::npos) return false;
295  std::string ext = filename.substr(sep);
296 
297  char fios_title[64];
298  fios_title[0] = '\0'; // reset the title;
299 
300  FiosType type = this->callback_proc(this->fop, filename, ext.c_str(), fios_title, lastof(fios_title));
301  if (type == FIOS_TYPE_INVALID) return false;
302 
303  for (const auto &fios : file_list) {
304  if (filename == fios.name) return false;
305  }
306 
307  FiosItem *fios = &file_list.emplace_back();
308 #ifdef _WIN32
309  // Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
310  HANDLE fh = CreateFile(OTTD2FS(filename).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
311 
312  if (fh != INVALID_HANDLE_VALUE) {
313  FILETIME ft;
314  ULARGE_INTEGER ft_int64;
315 
316  if (GetFileTime(fh, nullptr, nullptr, &ft) != 0) {
317  ft_int64.HighPart = ft.dwHighDateTime;
318  ft_int64.LowPart = ft.dwLowDateTime;
319 
320  // Convert from hectonanoseconds since 01/01/1601 to seconds since 01/01/1970
321  fios->mtime = ft_int64.QuadPart / 10000000ULL - 11644473600ULL;
322  } else {
323  fios->mtime = 0;
324  }
325 
326  CloseHandle(fh);
327 #else
328  struct stat sb;
329  if (stat(filename.c_str(), &sb) == 0) {
330  fios->mtime = sb.st_mtime;
331 #endif
332  } else {
333  fios->mtime = 0;
334  }
335 
336  fios->type = type;
337  strecpy(fios->name, filename.c_str(), lastof(fios->name));
338 
339  /* If the file doesn't have a title, use its filename */
340  const char *t = fios_title;
341  if (StrEmpty(fios_title)) {
342  auto ps = filename.rfind(PATHSEPCHAR);
343  t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
344  }
345  strecpy(fios->title, t, lastof(fios->title));
346  StrMakeValidInPlace(fios->title, lastof(fios->title));
347 
348  return true;
349 }
350 
351 
360 {
361  struct stat sb;
362  struct dirent *dirent;
363  DIR *dir;
364  FiosItem *fios;
365  size_t sort_start;
366  char d_name[sizeof(fios->name)];
367 
368  file_list.clear();
369 
370  assert(_fios_path != nullptr);
371 
372  /* A parent directory link exists if we are not in the root directory */
373  if (!FiosIsRoot(_fios_path->c_str())) {
374  fios = &file_list.emplace_back();
375  fios->type = FIOS_TYPE_PARENT;
376  fios->mtime = 0;
377  strecpy(fios->name, "..", lastof(fios->name));
378  SetDParamStr(0, "..");
379  GetString(fios->title, STR_SAVELOAD_PARENT_DIRECTORY, lastof(fios->title));
380  }
381 
382  /* Show subdirectories */
383  if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
384  while ((dirent = readdir(dir)) != nullptr) {
385  strecpy(d_name, FS2OTTD(dirent->d_name).c_str(), lastof(d_name));
386 
387  /* found file must be directory, but not '.' or '..' */
388  if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) &&
389  (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
390  strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
391  fios = &file_list.emplace_back();
392  fios->type = FIOS_TYPE_DIR;
393  fios->mtime = 0;
394  strecpy(fios->name, d_name, lastof(fios->name));
395  std::string dirname = std::string(d_name) + PATHSEP;
396  SetDParamStr(0, dirname);
397  GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title));
398  StrMakeValidInPlace(fios->title, lastof(fios->title));
399  }
400  }
401  closedir(dir);
402  }
403 
404  /* Sort the subdirs always by name, ascending, remember user-sorting order */
405  {
406  SortingBits order = _savegame_sort_order;
407  _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
408  std::sort(file_list.begin(), file_list.end());
409  _savegame_sort_order = order;
410  }
411 
412  /* This is where to start sorting for the filenames */
413  sort_start = file_list.size();
414 
415  /* Show files */
417  if (subdir == NO_DIRECTORY) {
418  scanner.Scan(nullptr, _fios_path->c_str(), false);
419  } else {
420  scanner.Scan(nullptr, subdir, true, true);
421  }
422 
423  std::sort(file_list.begin() + sort_start, file_list.end());
424 
425  /* Show drives */
426  FiosGetDrives(file_list);
427 
428  file_list.shrink_to_fit();
429 }
430 
439 static void GetFileTitle(const std::string &file, char *title, const char *last, Subdirectory subdir)
440 {
441  std::string buf = file;
442  buf += ".title";
443 
444  FILE *f = FioFOpenFile(buf, "r", subdir);
445  if (f == nullptr) return;
446 
447  size_t read = fread(title, 1, last - title, f);
448  assert(title + read <= last);
449  title[read] = '\0';
450  StrMakeValidInPlace(title, last);
451  FioFCloseFile(f);
452 }
453 
465 FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
466 {
467  /* Show savegame files
468  * .SAV OpenTTD saved game
469  * .SS1 Transport Tycoon Deluxe preset game
470  * .SV1 Transport Tycoon Deluxe (Patch) saved game
471  * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
472 
473  /* Don't crash if we supply no extension */
474  if (ext == nullptr) return FIOS_TYPE_INVALID;
475 
476  if (strcasecmp(ext, ".sav") == 0) {
477  GetFileTitle(file, title, last, SAVE_DIR);
478  return FIOS_TYPE_FILE;
479  }
480 
481  if (fop == SLO_LOAD) {
482  if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
483  strcasecmp(ext, ".sv2") == 0) {
484  if (title != nullptr) GetOldSaveGameName(file, title, last);
485  return FIOS_TYPE_OLDFILE;
486  }
487  }
488 
489  return FIOS_TYPE_INVALID;
490 }
491 
499 {
500  static std::optional<std::string> fios_save_path;
501 
502  if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
503 
504  _fios_path = &(*fios_save_path);
505 
507 }
508 
520 static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
521 {
522  /* Show scenario files
523  * .SCN OpenTTD style scenario file
524  * .SV0 Transport Tycoon Deluxe (Patch) scenario
525  * .SS0 Transport Tycoon Deluxe preset scenario */
526  if (strcasecmp(ext, ".scn") == 0) {
527  GetFileTitle(file, title, last, SCENARIO_DIR);
528  return FIOS_TYPE_SCENARIO;
529  }
530 
531  if (fop == SLO_LOAD) {
532  if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
533  GetOldSaveGameName(file, title, last);
534  return FIOS_TYPE_OLD_SCENARIO;
535  }
536  }
537 
538  return FIOS_TYPE_INVALID;
539 }
540 
548 {
549  static std::optional<std::string> fios_scn_path;
550 
551  /* Copy the default path on first run or on 'New Game' */
552  if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
553 
554  _fios_path = &(*fios_scn_path);
555 
556  std::string base_path = FioFindDirectory(SCENARIO_DIR);
557  Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
559 }
560 
561 static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
562 {
563  /* Show heightmap files
564  * .PNG PNG Based heightmap files
565  * .BMP BMP Based heightmap files
566  */
567 
568  FiosType type = FIOS_TYPE_INVALID;
569 
570 #ifdef WITH_PNG
571  if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
572 #endif /* WITH_PNG */
573 
574  if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
575 
576  if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
577 
578  TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
579  if (it != _tar_filelist[SCENARIO_DIR].end()) {
580  /* If the file is in a tar and that tar is not in a heightmap
581  * directory we are for sure not supposed to see it.
582  * Examples of this are pngs part of documentation within
583  * collections of NewGRFs or 32 bpp graphics replacement PNGs.
584  */
585  bool match = false;
586  for (Searchpath sp : _valid_searchpaths) {
587  std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
588 
589  if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
590  match = true;
591  break;
592  }
593  }
594 
595  if (!match) return FIOS_TYPE_INVALID;
596  }
597 
598  GetFileTitle(file, title, last, HEIGHTMAP_DIR);
599 
600  return type;
601 }
602 
609 {
610  static std::optional<std::string> fios_hmap_path;
611 
612  if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
613 
614  _fios_path = &(*fios_hmap_path);
615 
616  std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
617  Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
618  FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
619 }
620 
625 const char *FiosGetScreenshotDir()
626 {
627  static std::optional<std::string> fios_screenshot_path;
628 
629  if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
630 
631  return fios_screenshot_path->c_str();
632 }
633 
636  uint32 scenid;
637  uint8 md5sum[16];
638  char filename[MAX_PATH];
639 
640  bool operator == (const ScenarioIdentifier &other) const
641  {
642  return this->scenid == other.scenid &&
643  memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
644  }
645 
646  bool operator != (const ScenarioIdentifier &other) const
647  {
648  return !(*this == other);
649  }
650 };
651 
655 class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
656  bool scanned;
657 public:
659  ScenarioScanner() : scanned(false) {}
660 
665  void Scan(bool rescan)
666  {
667  if (this->scanned && !rescan) return;
668 
669  this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
670  this->scanned = true;
671  }
672 
673  bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
674  {
675  FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
676  if (f == nullptr) return false;
677 
679  int fret = fscanf(f, "%u", &id.scenid);
680  FioFCloseFile(f);
681  if (fret != 1) return false;
682  strecpy(id.filename, filename.c_str(), lastof(id.filename));
683 
684  Md5 checksum;
685  uint8 buffer[1024];
686  size_t len, size;
687 
688  /* open the scenario file, but first get the name.
689  * This is safe as we check on extension which
690  * must always exist. */
691  f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
692  if (f == nullptr) return false;
693 
694  /* calculate md5sum */
695  while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
696  size -= len;
697  checksum.Append(buffer, len);
698  }
699  checksum.Finish(id.md5sum);
700 
701  FioFCloseFile(f);
702 
703  include(*this, id);
704  return true;
705  }
706 };
707 
710 
717 const char *FindScenario(const ContentInfo *ci, bool md5sum)
718 {
719  _scanner.Scan(false);
720 
721  for (ScenarioIdentifier &id : _scanner) {
722  if (md5sum ? (memcmp(id.md5sum, ci->md5sum, sizeof(id.md5sum)) == 0)
723  : (id.scenid == ci->unique_id)) {
724  return id.filename;
725  }
726  }
727 
728  return nullptr;
729 }
730 
737 bool HasScenario(const ContentInfo *ci, bool md5sum)
738 {
739  return (FindScenario(ci, md5sum) != nullptr);
740 }
741 
746 {
747  _scanner.Scan(true);
748 }
749 
754 FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1)
755 {
756  static std::optional<std::string> _autosave_path;
757  if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR);
758 
759  static std::string _prefix;
760 
761  /* Callback for FiosFileScanner. */
762  static fios_getlist_callback_proc *proc = [](SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last) {
763  if (strcasecmp(ext, ".sav") == 0 && StrStartsWith(file, _prefix)) return FIOS_TYPE_FILE;
764  return FIOS_TYPE_INVALID;
765  };
766 
767  /* Prefix to check in the callback. */
768  _prefix = *_autosave_path + this->prefix;
769 
770  /* Get the save list. */
771  FileList list;
772  FiosFileScanner scanner(SLO_SAVE, proc, list);
773  scanner.Scan(".sav", _autosave_path->c_str(), false);
774 
775  /* Find the number for the most recent save, if any. */
776  if (list.begin() != list.end()) {
777  SortingBits order = _savegame_sort_order;
778  _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
779  std::sort(list.begin(), list.end());
780  _savegame_sort_order = order;
781 
782  std::string_view name = list.begin()->title;
783  std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
784  }
785 }
786 
792 {
793  if (++this->number >= _settings_client.gui.max_num_autosaves) this->number = 0;
794  return fmt::format("{}{}.sav", this->prefix, this->number);
795 }
796 
802 {
803  return fmt::format("-{}.sav", this->prefix);
804 }
network_content.h
FT_SCENARIO
@ FT_SCENARIO
old or new scenario
Definition: fileio_type.h:19
FiosGetScenarioListCallback
static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:520
FiosGetDescText
StringID FiosGetDescText(const char **path, uint64 *total_free)
Get descriptive texts.
Definition: fios.cpp:139
SAVE_DIR
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:110
FiosGetScreenshotDir
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:625
FiosGetScenarioList
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
Get a list of scenarios.
Definition: fios.cpp:547
ScanScenarios
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition: fios.cpp:745
FiosNumberedSaveName::FiosNumberedSaveName
FiosNumberedSaveName(const std::string &prefix)
Constructs FiosNumberedSaveName.
Definition: fios.cpp:754
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:131
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:1216
include
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set,...
Definition: smallvec_type.hpp:27
HEIGHTMAP_DIR
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:113
fileio_func.h
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:52
fios.h
FileList
List of file information.
Definition: fios.h:96
FiosFileScanner::fop
SaveLoadOperation fop
The kind of file we are looking for.
Definition: fios.cpp:268
ScenarioScanner::scanned
bool scanned
Whether we've already scanned.
Definition: fios.cpp:656
FiosGetHeightmapList
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
Get a list of heightmaps.
Definition: fios.cpp:608
IsInsideMM
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:204
screenshot.h
AbstractFileType
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:16
GetFileTitle
static void GetFileTitle(const std::string &file, char *title, const char *last, 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:439
FiosFileScanner::file_list
FileList & file_list
Destination of the found files.
Definition: fios.cpp:270
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:243
FiosDelete
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:256
FS2OTTD
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
Definition: win32.cpp:542
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:291
ScenarioScanner::Scan
void Scan(bool rescan)
Scan, but only if it's needed.
Definition: fios.cpp:665
StrMakeValidInPlace
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:255
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:245
ScenarioIdentifier::scenid
uint32 scenid
ID for the scenario (generated by content).
Definition: fios.cpp:636
FileList::BuildFileList
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
Construct a file list with the given kind of files, for the stated purpose.
Definition: fios.cpp:72
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content_type.h:50
tar_type.h
FiosItem
Deals with finding savegames.
Definition: fios.h:87
ContentInfo::md5sum
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content_type.h:70
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:203
StrStartsWith
bool StrStartsWith(const std::string_view str, const std::string_view prefix)
Check whether the given string starts with the given prefix.
Definition: string.cpp:367
FiosBrowseTo
const char * FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at #_fios_path.
Definition: fios.cpp:150
safeguards.h
FileList::FindItem
const FiosItem * FindItem(const char *file)
Find file information of a file by its name from the file list.
Definition: fios.cpp:104
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:207
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:41
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:64
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:138
stdafx.h
FT_SAVEGAME
@ FT_SAVEGAME
old or new savegame
Definition: fileio_type.h:18
FiosFileScanner::callback_proc
fios_getlist_callback_proc * callback_proc
Callback to check whether the file may be added.
Definition: fios.cpp:269
ScenarioScanner::ScenarioScanner
ScenarioScanner()
Initialise.
Definition: fios.cpp:659
string_func.h
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
strings_func.h
FT_NONE
@ FT_NONE
nothing to do
Definition: fileio_type.h:17
FiosGetSavegameListCallback
FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:465
FiosGetSavegameList
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
Get a list of savegames.
Definition: fios.cpp:498
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:125
DIR
Definition: win32.cpp:68
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
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
FiosType
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:67
ContentInfo::unique_id
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content_type.h:69
ScenarioIdentifier
Basic data to distinguish a scenario.
Definition: fios.cpp:635
GetCurrentScreenshotExtension
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:577
FiosFileScanner
Scanner to scan for a particular type of FIOS file.
Definition: fios.cpp:267
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:39
_scanner
static ScenarioScanner _scanner
Scanner for scenarios.
Definition: fios.cpp:709
ScenarioScanner::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
Definition: fios.cpp:673
strnatcmp
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:718
ScenarioIdentifier::md5sum
uint8 md5sum[16]
MD5 checksum of file.
Definition: fios.cpp:637
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:220
FiosNumberedSaveName::Extension
std::string Extension()
Generate an extension for a savegame name.
Definition: fios.cpp:801
FiosMakeSavegameName
std::string FiosMakeSavegameName(const char *name)
Make a save game or scenario filename from a name.
Definition: fios.cpp:231
FiosFileScanner::FiosFileScanner
FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list)
Create the scanner.
Definition: fios.cpp:278
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
ttd_opendir
static 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:113
ScenarioScanner
Scanner to find the unique IDs of scenarios.
Definition: fios.cpp:655
FindScenario
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:717
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
FiosNumberedSaveName::Filename
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition: fios.cpp:791
FiosGetFileList
static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
Fill the list of the files in a directory, according to some arbitrary rule.
Definition: fios.cpp:359
OTTD2FS
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition: win32.cpp:560
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:296
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:593
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:130
HasScenario
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition: fios.cpp:737
FiosItem::operator<
bool operator<(const FiosItem &other) const
Compare two FiosItem's.
Definition: fios.cpp:54