OpenTTD Source  1.11.0-beta1
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 "tar_type.h"
21 #include <sys/stat.h>
22 #include <functional>
23 #include <optional>
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/os2 .c) */
38 extern bool FiosIsRoot(const char *path);
39 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
40 extern bool FiosIsHiddenFile(const struct dirent *ent);
41 extern void FiosGetDrives(FileList &file_list);
42 extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
43 
44 /* get the name of an oldstyle savegame */
45 extern void GetOldSaveGameName(const std::string &file, char *title, const char *last);
46 
52 bool FiosItem::operator< (const FiosItem &other) const
53 {
54  int r = false;
55 
56  if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
57  r = (*this).mtime - other.mtime;
58  } else {
59  r = strnatcmp((*this).title, other.title);
60  }
61  if (r == 0) return false;
62  return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
63 }
64 
65 FileList::~FileList()
66 {
67  this->Clear();
68 }
69 
76 {
77  this->Clear();
78 
79  assert(fop == SLO_LOAD || fop == SLO_SAVE);
80  switch (abstract_filetype) {
81  case FT_NONE:
82  break;
83 
84  case FT_SAVEGAME:
85  FiosGetSavegameList(fop, *this);
86  break;
87 
88  case FT_SCENARIO:
89  FiosGetScenarioList(fop, *this);
90  break;
91 
92  case FT_HEIGHTMAP:
93  FiosGetHeightmapList(fop, *this);
94  break;
95 
96  default:
97  NOT_REACHED();
98  }
99 }
100 
107 const FiosItem *FileList::FindItem(const char *file)
108 {
109  for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
110  if (strcmp(file, item->name) == 0) return item;
111  if (strcmp(file, item->title) == 0) return item;
112  }
113 
114  /* If no name matches, try to parse it as number */
115  char *endptr;
116  int i = strtol(file, &endptr, 10);
117  if (file == endptr || *endptr != '\0') i = -1;
118 
119  if (IsInsideMM(i, 0, this->Length())) return this->Get(i);
120 
121  /* As a last effort assume it is an OpenTTD savegame and
122  * that the ".sav" part was not given. */
123  char long_file[MAX_PATH];
124  seprintf(long_file, lastof(long_file), "%s.sav", file);
125  for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
126  if (strcmp(long_file, item->name) == 0) return item;
127  if (strcmp(long_file, item->title) == 0) return item;
128  }
129 
130  return nullptr;
131 }
132 
140 StringID FiosGetDescText(const char **path, uint64 *total_free)
141 {
142  *path = _fios_path->c_str();
143  return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
144 }
145 
151 const char *FiosBrowseTo(const FiosItem *item)
152 {
153  switch (item->type) {
154  case FIOS_TYPE_DRIVE:
155 #if defined(_WIN32) || defined(__OS2__)
156  assert(_fios_path != nullptr);
157  *_fios_path = std::string{ item->title[0] } + ":" PATHSEP;
158 #endif
159  break;
160 
161  case FIOS_TYPE_INVALID:
162  break;
163 
164  case FIOS_TYPE_PARENT: {
165  assert(_fios_path != nullptr);
166  auto s = _fios_path->find_last_of(PATHSEPCHAR);
167  if (s != std::string::npos && s != 0) {
168  _fios_path->erase(s); // Remove last path separator character, so we can go up one level.
169  }
170 
171  s = _fios_path->find_last_of(PATHSEPCHAR);
172  if (s != std::string::npos) {
173  _fios_path->erase(s + 1); // go up a directory
174  }
175  break;
176  }
177 
178  case FIOS_TYPE_DIR:
179  assert(_fios_path != nullptr);
180  *_fios_path += item->name;
181  *_fios_path += PATHSEP;
182  break;
183 
184  case FIOS_TYPE_DIRECT:
185  assert(_fios_path != nullptr);
186  *_fios_path = item->name;
187  break;
188 
189  case FIOS_TYPE_FILE:
190  case FIOS_TYPE_OLDFILE:
191  case FIOS_TYPE_SCENARIO:
192  case FIOS_TYPE_OLD_SCENARIO:
193  case FIOS_TYPE_PNG:
194  case FIOS_TYPE_BMP:
195  return item->name;
196  }
197 
198  return nullptr;
199 }
200 
208 static std::string FiosMakeFilename(const std::string *path, const char *name, const char *ext)
209 {
210  std::string buf;
211 
212  if (path != nullptr) {
213  buf = *path;
214  /* Remove trailing path separator, if present */
215  if (!buf.empty() && buf.back() == PATHSEPCHAR) buf.pop_back();
216  }
217 
218  /* Don't append the extension if it is already there */
219  const char *period = strrchr(name, '.');
220  if (period != nullptr && strcasecmp(period, ext) == 0) ext = "";
221 
222  return buf + PATHSEP + name + ext;
223 }
224 
232 std::string FiosMakeSavegameName(const char *name)
233 {
234  const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
235 
236  return FiosMakeFilename(_fios_path, name, extension);
237 }
238 
244 std::string FiosMakeHeightmapName(const char *name)
245 {
246  std::string ext(".");
248 
249  return FiosMakeFilename(_fios_path, name, ext.c_str());
250 }
251 
257 bool FiosDelete(const char *name)
258 {
259  std::string filename = FiosMakeSavegameName(name);
260  return unlink(filename.c_str()) == 0;
261 }
262 
263 typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const std::string &filename, const char *ext, char *title, const char *last);
264 
268 class FiosFileScanner : public FileScanner {
270  fios_getlist_callback_proc *callback_proc;
272 public:
281  {}
282 
283  bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
284 };
285 
292 bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
293 {
294  auto sep = filename.rfind('.');
295  if (sep == std::string::npos) return false;
296  std::string ext = filename.substr(sep);
297 
298  char fios_title[64];
299  fios_title[0] = '\0'; // reset the title;
300 
301  FiosType type = this->callback_proc(this->fop, filename, ext.c_str(), fios_title, lastof(fios_title));
302  if (type == FIOS_TYPE_INVALID) return false;
303 
304  for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
305  if (filename == fios->name) return false;
306  }
307 
308  FiosItem *fios = file_list.Append();
309 #ifdef _WIN32
310  // Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
311  HANDLE fh = CreateFile(OTTD2FS(filename.c_str()), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
312 
313  if (fh != INVALID_HANDLE_VALUE) {
314  FILETIME ft;
315  ULARGE_INTEGER ft_int64;
316 
317  if (GetFileTime(fh, nullptr, nullptr, &ft) != 0) {
318  ft_int64.HighPart = ft.dwHighDateTime;
319  ft_int64.LowPart = ft.dwLowDateTime;
320 
321  // Convert from hectonanoseconds since 01/01/1601 to seconds since 01/01/1970
322  fios->mtime = ft_int64.QuadPart / 10000000ULL - 11644473600ULL;
323  } else {
324  fios->mtime = 0;
325  }
326 
327  CloseHandle(fh);
328 #else
329  struct stat sb;
330  if (stat(filename.c_str(), &sb) == 0) {
331  fios->mtime = sb.st_mtime;
332 #endif
333  } else {
334  fios->mtime = 0;
335  }
336 
337  fios->type = type;
338  strecpy(fios->name, filename.c_str(), lastof(fios->name));
339 
340  /* If the file doesn't have a title, use its filename */
341  const char *t = fios_title;
342  if (StrEmpty(fios_title)) {
343  auto ps = filename.rfind(PATHSEPCHAR);
344  t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
345  }
346  strecpy(fios->title, t, lastof(fios->title));
347  str_validate(fios->title, lastof(fios->title));
348 
349  return true;
350 }
351 
352 
361 {
362  struct stat sb;
363  struct dirent *dirent;
364  DIR *dir;
365  FiosItem *fios;
366  size_t sort_start;
367  char d_name[sizeof(fios->name)];
368 
369  file_list.Clear();
370 
371  assert(_fios_path != nullptr);
372 
373  /* A parent directory link exists if we are not in the root directory */
374  if (!FiosIsRoot(_fios_path->c_str())) {
375  fios = file_list.Append();
376  fios->type = FIOS_TYPE_PARENT;
377  fios->mtime = 0;
378  strecpy(fios->name, "..", lastof(fios->name));
379  strecpy(fios->title, ".. (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), 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.Append();
392  fios->type = FIOS_TYPE_DIR;
393  fios->mtime = 0;
394  strecpy(fios->name, d_name, lastof(fios->name));
395  seprintf(fios->title, lastof(fios->title), "%s" PATHSEP " (Directory)", d_name);
396  str_validate(fios->title, lastof(fios->title));
397  }
398  }
399  closedir(dir);
400  }
401 
402  /* Sort the subdirs always by name, ascending, remember user-sorting order */
403  {
404  SortingBits order = _savegame_sort_order;
405  _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
406  std::sort(file_list.files.begin(), file_list.files.end());
407  _savegame_sort_order = order;
408  }
409 
410  /* This is where to start sorting for the filenames */
411  sort_start = file_list.Length();
412 
413  /* Show files */
415  if (subdir == NO_DIRECTORY) {
416  scanner.Scan(nullptr, _fios_path->c_str(), false);
417  } else {
418  scanner.Scan(nullptr, subdir, true, true);
419  }
420 
421  std::sort(file_list.files.begin() + sort_start, file_list.files.end());
422 
423  /* Show drives */
424  FiosGetDrives(file_list);
425 
426  file_list.Compact();
427 }
428 
437 static void GetFileTitle(const std::string &file, char *title, const char *last, Subdirectory subdir)
438 {
439  std::string buf = file;
440  buf += ".title";
441 
442  FILE *f = FioFOpenFile(buf, "r", subdir);
443  if (f == nullptr) return;
444 
445  size_t read = fread(title, 1, last - title, f);
446  assert(title + read <= last);
447  title[read] = '\0';
448  str_validate(title, last);
449  FioFCloseFile(f);
450 }
451 
463 FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
464 {
465  /* Show savegame files
466  * .SAV OpenTTD saved game
467  * .SS1 Transport Tycoon Deluxe preset game
468  * .SV1 Transport Tycoon Deluxe (Patch) saved game
469  * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
470 
471  /* Don't crash if we supply no extension */
472  if (ext == nullptr) return FIOS_TYPE_INVALID;
473 
474  if (strcasecmp(ext, ".sav") == 0) {
475  GetFileTitle(file, title, last, SAVE_DIR);
476  return FIOS_TYPE_FILE;
477  }
478 
479  if (fop == SLO_LOAD) {
480  if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
481  strcasecmp(ext, ".sv2") == 0) {
482  if (title != nullptr) GetOldSaveGameName(file, title, last);
483  return FIOS_TYPE_OLDFILE;
484  }
485  }
486 
487  return FIOS_TYPE_INVALID;
488 }
489 
497 {
498  static std::optional<std::string> fios_save_path;
499 
500  if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
501 
502  _fios_path = &(*fios_save_path);
503 
505 }
506 
518 static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
519 {
520  /* Show scenario files
521  * .SCN OpenTTD style scenario file
522  * .SV0 Transport Tycoon Deluxe (Patch) scenario
523  * .SS0 Transport Tycoon Deluxe preset scenario */
524  if (strcasecmp(ext, ".scn") == 0) {
525  GetFileTitle(file, title, last, SCENARIO_DIR);
526  return FIOS_TYPE_SCENARIO;
527  }
528 
529  if (fop == SLO_LOAD) {
530  if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
531  GetOldSaveGameName(file, title, last);
532  return FIOS_TYPE_OLD_SCENARIO;
533  }
534  }
535 
536  return FIOS_TYPE_INVALID;
537 }
538 
546 {
547  static std::optional<std::string> fios_scn_path;
548 
549  /* Copy the default path on first run or on 'New Game' */
550  if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
551 
552  _fios_path = &(*fios_scn_path);
553 
554  std::string base_path = FioFindDirectory(SCENARIO_DIR);
555  Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
557 }
558 
559 static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
560 {
561  /* Show heightmap files
562  * .PNG PNG Based heightmap files
563  * .BMP BMP Based heightmap files
564  */
565 
566  FiosType type = FIOS_TYPE_INVALID;
567 
568 #ifdef WITH_PNG
569  if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
570 #endif /* WITH_PNG */
571 
572  if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
573 
574  if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
575 
576  TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
577  if (it != _tar_filelist[SCENARIO_DIR].end()) {
578  /* If the file is in a tar and that tar is not in a heightmap
579  * directory we are for sure not supposed to see it.
580  * Examples of this are pngs part of documentation within
581  * collections of NewGRFs or 32 bpp graphics replacement PNGs.
582  */
583  bool match = false;
584  Searchpath sp;
585  FOR_ALL_SEARCHPATHS(sp) {
586  std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
587 
588  if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
589  match = true;
590  break;
591  }
592  }
593 
594  if (!match) return FIOS_TYPE_INVALID;
595  }
596 
597  GetFileTitle(file, title, last, HEIGHTMAP_DIR);
598 
599  return type;
600 }
601 
608 {
609  static std::optional<std::string> fios_hmap_path;
610 
611  if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
612 
613  _fios_path = &(*fios_hmap_path);
614 
615  std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
616  Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
617  FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
618 }
619 
624 const char *FiosGetScreenshotDir()
625 {
626  static std::optional<std::string> fios_screenshot_path;
627 
628  if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
629 
630  return fios_screenshot_path->c_str();
631 }
632 
635  uint32 scenid;
636  uint8 md5sum[16];
637  char filename[MAX_PATH];
638 
639  bool operator == (const ScenarioIdentifier &other) const
640  {
641  return this->scenid == other.scenid &&
642  memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
643  }
644 
645  bool operator != (const ScenarioIdentifier &other) const
646  {
647  return !(*this == other);
648  }
649 };
650 
654 class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
655  bool scanned;
656 public:
658  ScenarioScanner() : scanned(false) {}
659 
664  void Scan(bool rescan)
665  {
666  if (this->scanned && !rescan) return;
667 
668  this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
669  this->scanned = true;
670  }
671 
672  bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
673  {
674  FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
675  if (f == nullptr) return false;
676 
678  int fret = fscanf(f, "%u", &id.scenid);
679  FioFCloseFile(f);
680  if (fret != 1) return false;
681  strecpy(id.filename, filename.c_str(), lastof(id.filename));
682 
683  Md5 checksum;
684  uint8 buffer[1024];
685  size_t len, size;
686 
687  /* open the scenario file, but first get the name.
688  * This is safe as we check on extension which
689  * must always exist. */
690  f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
691  if (f == nullptr) return false;
692 
693  /* calculate md5sum */
694  while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
695  size -= len;
696  checksum.Append(buffer, len);
697  }
698  checksum.Finish(id.md5sum);
699 
700  FioFCloseFile(f);
701 
702  include(*this, id);
703  return true;
704  }
705 };
706 
709 
716 const char *FindScenario(const ContentInfo *ci, bool md5sum)
717 {
718  _scanner.Scan(false);
719 
720  for (ScenarioIdentifier &id : _scanner) {
721  if (md5sum ? (memcmp(id.md5sum, ci->md5sum, sizeof(id.md5sum)) == 0)
722  : (id.scenid == ci->unique_id)) {
723  return id.filename;
724  }
725  }
726 
727  return nullptr;
728 }
729 
736 bool HasScenario(const ContentInfo *ci, bool md5sum)
737 {
738  return (FindScenario(ci, md5sum) != nullptr);
739 }
740 
745 {
746  _scanner.Scan(true);
747 }
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:518
FiosGetDescText
StringID FiosGetDescText(const char **path, uint64 *total_free)
Get descriptive texts.
Definition: fios.cpp:140
SAVE_DIR
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:110
FileList::End
const FiosItem * End() const
Get a pointer behind the last file information.
Definition: fios.h:147
str_validate
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:237
FileList::Compact
void Compact()
Compact the list down to the smallest block size boundary.
Definition: fios.h:191
FiosGetScreenshotDir
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:624
FiosGetScenarioList
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
Get a list of scenarios.
Definition: fios.cpp:545
ScanScenarios
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition: fios.cpp:744
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:1367
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
FOR_ALL_SEARCHPATHS
#define FOR_ALL_SEARCHPATHS(sp)
Iterator for all the search paths.
Definition: fileio_func.h:37
fileio_func.h
fios.h
FileList
List of file information.
Definition: fios.h:112
FiosFileScanner::fop
SaveLoadOperation fop
The kind of file we are looking for.
Definition: fios.cpp:269
ScenarioScanner::scanned
bool scanned
Whether we've already scanned.
Definition: fios.cpp:655
FileList::Clear
void Clear()
Remove all items from the list.
Definition: fios.h:185
FiosGetHeightmapList
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
Get a list of heightmaps.
Definition: fios.cpp:607
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:437
FiosFileScanner::file_list
FileList & file_list
Destination of the found files.
Definition: fios.cpp:271
OTTD2FS
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD's encoding to that of the local environment.
Definition: win32.cpp:601
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:244
FiosDelete
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:257
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:292
FileList::Get
const FiosItem * Get(size_t index) const
Get a pointer to the indicated file information.
Definition: fios.h:156
ScenarioScanner::Scan
void Scan(bool rescan)
Scan, but only if it's needed.
Definition: fios.cpp:664
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:406
ScenarioIdentifier::scenid
uint32 scenid
ID for the scenario (generated by content).
Definition: fios.cpp:635
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:75
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content.h:54
tar_type.h
FiosItem
Deals with finding savegames.
Definition: fios.h:103
ContentInfo::md5sum
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:74
FileList::files
std::vector< FiosItem > files
The list of files.
Definition: fios.h:199
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
FiosBrowseTo
const char * FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at #_fios_path.
Definition: fios.cpp:151
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:107
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:208
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:61
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
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:270
FileList::Append
FiosItem * Append()
Construct a new entry in the file list.
Definition: fios.h:120
ScenarioScanner::ScenarioScanner
ScenarioScanner()
Initialise.
Definition: fios.cpp:658
string_func.h
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
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:463
FiosGetSavegameList
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
Get a list of savegames.
Definition: fios.cpp:496
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:125
DIR
Definition: win32.cpp:93
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
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.h:73
ScenarioIdentifier
Basic data to distinguish a scenario.
Definition: fios.cpp:634
GetCurrentScreenshotExtension
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:575
FiosFileScanner
Scanner to scan for a particular type of FIOS file.
Definition: fios.cpp:268
FileScanner
Helper for scanning for files with a given name.
Definition: fileio_func.h:59
_scanner
static ScenarioScanner _scanner
Scanner for scenarios.
Definition: fios.cpp:708
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:672
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:625
ScenarioIdentifier::md5sum
uint8 md5sum[16]
MD5 checksum of file.
Definition: fios.cpp:636
FileList::Begin
const FiosItem * Begin() const
Get a pointer to the first file information.
Definition: fios.h:138
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
FiosMakeSavegameName
std::string FiosMakeSavegameName(const char *name)
Make a save game or scenario filename from a name.
Definition: fios.cpp:232
FiosFileScanner::FiosFileScanner
FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list)
Create the scanner.
Definition: fios.cpp:279
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:133
ScenarioScanner
Scanner to find the unique IDs of scenarios.
Definition: fios.cpp:654
FS2OTTD
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD's encoding from that of the local environment.
Definition: win32.cpp:583
FindScenario
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:716
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:393
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:360
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:288
FileList::Length
size_t Length() const
Get the number of files in the list.
Definition: fios.h:129
HasScenario
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we've got a given scenario based on its unique ID.
Definition: fios.cpp:736
FiosItem::operator<
bool operator<(const FiosItem &other) const
Compare two FiosItem's.
Definition: fios.cpp:52