OpenTTD
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 
23 #ifndef _WIN32
24 # include <unistd.h>
25 #endif /* _WIN32 */
26 
27 #include "table/strings.h"
28 
29 #include "safeguards.h"
30 
31 /* Variables to display file lists */
32 static char *_fios_path;
33 static const char *_fios_path_last;
34 SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
35 
36 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
37 extern bool FiosIsRoot(const char *path);
38 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
39 extern bool FiosIsHiddenFile(const struct dirent *ent);
40 extern void FiosGetDrives(FileList &file_list);
41 extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
42 
43 /* get the name of an oldstyle savegame */
44 extern void GetOldSaveGameName(const char *file, char *title, const char *last);
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 = strnatcmp((*this).title, other.title);
59  }
60  if (r == 0) return false;
61  return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
62 }
63 
64 FileList::~FileList()
65 {
66  this->Clear();
67 }
68 
75 {
76  this->Clear();
77 
78  assert(fop == SLO_LOAD || fop == SLO_SAVE);
79  switch (abstract_filetype) {
80  case FT_NONE:
81  break;
82 
83  case FT_SAVEGAME:
84  FiosGetSavegameList(fop, *this);
85  break;
86 
87  case FT_SCENARIO:
88  FiosGetScenarioList(fop, *this);
89  break;
90 
91  case FT_HEIGHTMAP:
92  FiosGetHeightmapList(fop, *this);
93  break;
94 
95  default:
96  NOT_REACHED();
97  }
98 }
99 
106 const FiosItem *FileList::FindItem(const char *file)
107 {
108  for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
109  if (strcmp(file, item->name) == 0) return item;
110  if (strcmp(file, item->title) == 0) return item;
111  }
112 
113  /* If no name matches, try to parse it as number */
114  char *endptr;
115  int i = strtol(file, &endptr, 10);
116  if (file == endptr || *endptr != '\0') i = -1;
117 
118  if (IsInsideMM(i, 0, this->Length())) return this->Get(i);
119 
120  /* As a last effort assume it is an OpenTTD savegame and
121  * that the ".sav" part was not given. */
122  char long_file[MAX_PATH];
123  seprintf(long_file, lastof(long_file), "%s.sav", file);
124  for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
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;
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  seprintf(_fios_path, _fios_path_last, "%c:" PATHSEP, item->title[0]);
156 #endif
157  break;
158 
159  case FIOS_TYPE_INVALID:
160  break;
161 
162  case FIOS_TYPE_PARENT: {
163  /* Check for possible nullptr ptr */
164  char *s = strrchr(_fios_path, PATHSEPCHAR);
165  if (s != nullptr && s != _fios_path) {
166  s[0] = '\0'; // Remove last path separator character, so we can go up one level.
167  }
168  s = strrchr(_fios_path, PATHSEPCHAR);
169  if (s != nullptr) {
170  s[1] = '\0'; // go up a directory
171  }
172  break;
173  }
174 
175  case FIOS_TYPE_DIR:
176  strecat(_fios_path, item->name, _fios_path_last);
177  strecat(_fios_path, PATHSEP, _fios_path_last);
178  break;
179 
180  case FIOS_TYPE_DIRECT:
181  seprintf(_fios_path, _fios_path_last, "%s", item->name);
182  break;
183 
184  case FIOS_TYPE_FILE:
185  case FIOS_TYPE_OLDFILE:
186  case FIOS_TYPE_SCENARIO:
187  case FIOS_TYPE_OLD_SCENARIO:
188  case FIOS_TYPE_PNG:
189  case FIOS_TYPE_BMP:
190  return item->name;
191  }
192 
193  return nullptr;
194 }
195 
204 static void FiosMakeFilename(char *buf, const char *path, const char *name, const char *ext, const char *last)
205 {
206  if (path != nullptr) {
207  const char *buf_start = buf;
208  buf = strecpy(buf, path, last);
209  /* Remove trailing path separator, if present */
210  if (buf > buf_start && buf[-1] == PATHSEPCHAR) buf--;
211  }
212 
213  /* Don't append the extension if it is already there */
214  const char *period = strrchr(name, '.');
215  if (period != nullptr && strcasecmp(period, ext) == 0) ext = "";
216 
217  seprintf(buf, last, PATHSEP "%s%s", name, ext);
218 }
219 
226 void FiosMakeSavegameName(char *buf, const char *name, const char *last)
227 {
228  const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
229 
230  FiosMakeFilename(buf, _fios_path, name, extension, last);
231 }
232 
239 void FiosMakeHeightmapName(char *buf, const char *name, const char *last)
240 {
241  char ext[5];
242  ext[0] = '.';
243  strecpy(ext + 1, GetCurrentScreenshotExtension(), lastof(ext));
244 
245  FiosMakeFilename(buf, _fios_path, name, ext, last);
246 }
247 
253 bool FiosDelete(const char *name)
254 {
255  char filename[512];
256 
257  FiosMakeSavegameName(filename, name, lastof(filename));
258  return unlink(filename) == 0;
259 }
260 
261 typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const char *filename, const char *ext, char *title, const char *last);
262 
266 class FiosFileScanner : public FileScanner {
268  fios_getlist_callback_proc *callback_proc;
270 public:
277  FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) :
278  fop(fop), callback_proc(callback_proc), file_list(file_list)
279  {}
280 
281  bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
282 };
283 
290 bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
291 {
292  const char *ext = strrchr(filename, '.');
293  if (ext == nullptr) return false;
294 
295  char fios_title[64];
296  fios_title[0] = '\0'; // reset the title;
297 
298  FiosType type = this->callback_proc(this->fop, filename, ext, fios_title, lastof(fios_title));
299  if (type == FIOS_TYPE_INVALID) return false;
300 
301  for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
302  if (strcmp(fios->name, filename) == 0) return false;
303  }
304 
305  FiosItem *fios = file_list.Append();
306 #ifdef _WIN32
307  // Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
308  HANDLE fh = CreateFile(OTTD2FS(filename), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
309 
310  if (fh != INVALID_HANDLE_VALUE) {
311  FILETIME ft;
312  ULARGE_INTEGER ft_int64;
313 
314  if (GetFileTime(fh, nullptr, nullptr, &ft) != 0) {
315  ft_int64.HighPart = ft.dwHighDateTime;
316  ft_int64.LowPart = ft.dwLowDateTime;
317 
318  // Convert from hectonanoseconds since 01/01/1601 to seconds since 01/01/1970
319  fios->mtime = ft_int64.QuadPart / 10000000ULL - 11644473600ULL;
320  } else {
321  fios->mtime = 0;
322  }
323 
324  CloseHandle(fh);
325 #else
326  struct stat sb;
327  if (stat(filename, &sb) == 0) {
328  fios->mtime = sb.st_mtime;
329 #endif
330  } else {
331  fios->mtime = 0;
332  }
333 
334  fios->type = type;
335  strecpy(fios->name, filename, lastof(fios->name));
336 
337  /* If the file doesn't have a title, use its filename */
338  const char *t = fios_title;
339  if (StrEmpty(fios_title)) {
340  t = strrchr(filename, PATHSEPCHAR);
341  t = (t == nullptr) ? filename : (t + 1);
342  }
343  strecpy(fios->title, t, lastof(fios->title));
344  str_validate(fios->title, lastof(fios->title));
345 
346  return true;
347 }
348 
349 
357 static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
358 {
359  struct stat sb;
360  struct dirent *dirent;
361  DIR *dir;
362  FiosItem *fios;
363  size_t sort_start;
364  char d_name[sizeof(fios->name)];
365 
366  file_list.Clear();
367 
368  /* A parent directory link exists if we are not in the root directory */
369  if (!FiosIsRoot(_fios_path)) {
370  fios = file_list.Append();
371  fios->type = FIOS_TYPE_PARENT;
372  fios->mtime = 0;
373  strecpy(fios->name, "..", lastof(fios->name));
374  strecpy(fios->title, ".. (Parent directory)", lastof(fios->title));
375  }
376 
377  /* Show subdirectories */
378  if ((dir = ttd_opendir(_fios_path)) != nullptr) {
379  while ((dirent = readdir(dir)) != nullptr) {
380  strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
381 
382  /* found file must be directory, but not '.' or '..' */
383  if (FiosIsValidFile(_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
384  (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
385  strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
386  fios = file_list.Append();
387  fios->type = FIOS_TYPE_DIR;
388  fios->mtime = 0;
389  strecpy(fios->name, d_name, lastof(fios->name));
390  seprintf(fios->title, lastof(fios->title), "%s" PATHSEP " (Directory)", d_name);
391  str_validate(fios->title, lastof(fios->title));
392  }
393  }
394  closedir(dir);
395  }
396 
397  /* Sort the subdirs always by name, ascending, remember user-sorting order */
398  {
399  SortingBits order = _savegame_sort_order;
400  _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
401  std::sort(file_list.files.begin(), file_list.files.end());
402  _savegame_sort_order = order;
403  }
404 
405  /* This is where to start sorting for the filenames */
406  sort_start = file_list.Length();
407 
408  /* Show files */
409  FiosFileScanner scanner(fop, callback_proc, file_list);
410  if (subdir == NO_DIRECTORY) {
411  scanner.Scan(nullptr, _fios_path, false);
412  } else {
413  scanner.Scan(nullptr, subdir, true, true);
414  }
415 
416  std::sort(file_list.files.begin() + sort_start, file_list.files.end());
417 
418  /* Show drives */
419  FiosGetDrives(file_list);
420 
421  file_list.Compact();
422 }
423 
432 static void GetFileTitle(const char *file, char *title, const char *last, Subdirectory subdir)
433 {
434  char buf[MAX_PATH];
435  strecpy(buf, file, lastof(buf));
436  strecat(buf, ".title", lastof(buf));
437 
438  FILE *f = FioFOpenFile(buf, "r", subdir);
439  if (f == nullptr) return;
440 
441  size_t read = fread(title, 1, last - title, f);
442  assert(title + read <= last);
443  title[read] = '\0';
444  str_validate(title, last);
445  FioFCloseFile(f);
446 }
447 
459 FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
460 {
461  /* Show savegame files
462  * .SAV OpenTTD saved game
463  * .SS1 Transport Tycoon Deluxe preset game
464  * .SV1 Transport Tycoon Deluxe (Patch) saved game
465  * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
466 
467  /* Don't crash if we supply no extension */
468  if (ext == nullptr) return FIOS_TYPE_INVALID;
469 
470  if (strcasecmp(ext, ".sav") == 0) {
471  GetFileTitle(file, title, last, SAVE_DIR);
472  return FIOS_TYPE_FILE;
473  }
474 
475  if (fop == SLO_LOAD) {
476  if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
477  strcasecmp(ext, ".sv2") == 0) {
478  if (title != nullptr) GetOldSaveGameName(file, title, last);
479  return FIOS_TYPE_OLDFILE;
480  }
481  }
482 
483  return FIOS_TYPE_INVALID;
484 }
485 
493 {
494  static char *fios_save_path = nullptr;
495  static char *fios_save_path_last = nullptr;
496 
497  if (fios_save_path == nullptr) {
498  fios_save_path = MallocT<char>(MAX_PATH);
499  fios_save_path_last = fios_save_path + MAX_PATH - 1;
500  FioGetDirectory(fios_save_path, fios_save_path_last, SAVE_DIR);
501  }
502 
503  _fios_path = fios_save_path;
504  _fios_path_last = fios_save_path_last;
505 
507 }
508 
520 static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *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 char *fios_scn_path = nullptr;
550  static char *fios_scn_path_last = nullptr;
551 
552  /* Copy the default path on first run or on 'New Game' */
553  if (fios_scn_path == nullptr) {
554  fios_scn_path = MallocT<char>(MAX_PATH);
555  fios_scn_path_last = fios_scn_path + MAX_PATH - 1;
556  FioGetDirectory(fios_scn_path, fios_scn_path_last, SCENARIO_DIR);
557  }
558 
559  _fios_path = fios_scn_path;
560  _fios_path_last = fios_scn_path_last;
561 
562  char base_path[MAX_PATH];
563  FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR);
564 
565  Subdirectory subdir = (fop == SLO_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY;
566  FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
567 }
568 
569 static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
570 {
571  /* Show heightmap files
572  * .PNG PNG Based heightmap files
573  * .BMP BMP Based heightmap files
574  */
575 
576  FiosType type = FIOS_TYPE_INVALID;
577 
578 #ifdef WITH_PNG
579  if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
580 #endif /* WITH_PNG */
581 
582  if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
583 
584  if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
585 
586  TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
587  if (it != _tar_filelist[SCENARIO_DIR].end()) {
588  /* If the file is in a tar and that tar is not in a heightmap
589  * directory we are for sure not supposed to see it.
590  * Examples of this are pngs part of documentation within
591  * collections of NewGRFs or 32 bpp graphics replacement PNGs.
592  */
593  bool match = false;
594  Searchpath sp;
595  FOR_ALL_SEARCHPATHS(sp) {
596  char buf[MAX_PATH];
597  FioAppendDirectory(buf, lastof(buf), sp, HEIGHTMAP_DIR);
598 
599  if (strncmp(buf, it->second.tar_filename, strlen(buf)) == 0) {
600  match = true;
601  break;
602  }
603  }
604 
605  if (!match) return FIOS_TYPE_INVALID;
606  }
607 
608  GetFileTitle(file, title, last, HEIGHTMAP_DIR);
609 
610  return type;
611 }
612 
619 {
620  static char *fios_hmap_path = nullptr;
621  static char *fios_hmap_path_last = nullptr;
622 
623  if (fios_hmap_path == nullptr) {
624  fios_hmap_path = MallocT<char>(MAX_PATH);
625  fios_hmap_path_last = fios_hmap_path + MAX_PATH - 1;
626  FioGetDirectory(fios_hmap_path, fios_hmap_path_last, HEIGHTMAP_DIR);
627  }
628 
629  _fios_path = fios_hmap_path;
630  _fios_path_last = fios_hmap_path_last;
631 
632  char base_path[MAX_PATH];
633  FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR);
634 
635  Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY;
636  FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
637 }
638 
643 const char *FiosGetScreenshotDir()
644 {
645  static char *fios_screenshot_path = nullptr;
646 
647  if (fios_screenshot_path == nullptr) {
648  fios_screenshot_path = MallocT<char>(MAX_PATH);
649  FioGetDirectory(fios_screenshot_path, fios_screenshot_path + MAX_PATH - 1, SCREENSHOT_DIR);
650  }
651 
652  return fios_screenshot_path;
653 }
654 
657  uint32 scenid;
658  uint8 md5sum[16];
659  char filename[MAX_PATH];
660 
661  bool operator == (const ScenarioIdentifier &other) const
662  {
663  return this->scenid == other.scenid &&
664  memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
665  }
666 
667  bool operator != (const ScenarioIdentifier &other) const
668  {
669  return !(*this == other);
670  }
671 };
672 
676 class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
677  bool scanned;
678 public:
680  ScenarioScanner() : scanned(false) {}
681 
686  void Scan(bool rescan)
687  {
688  if (this->scanned && !rescan) return;
689 
690  this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
691  this->scanned = true;
692  }
693 
694  bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
695  {
696  FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
697  if (f == nullptr) return false;
698 
700  int fret = fscanf(f, "%i", &id.scenid);
701  FioFCloseFile(f);
702  if (fret != 1) return false;
703  strecpy(id.filename, filename, lastof(id.filename));
704 
705  Md5 checksum;
706  uint8 buffer[1024];
707  char basename[MAX_PATH];
708  size_t len, size;
709 
710  /* open the scenario file, but first get the name.
711  * This is safe as we check on extension which
712  * must always exist. */
713  strecpy(basename, filename, lastof(basename));
714  *strrchr(basename, '.') = '\0';
715  f = FioFOpenFile(basename, "rb", SCENARIO_DIR, &size);
716  if (f == nullptr) return false;
717 
718  /* calculate md5sum */
719  while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
720  size -= len;
721  checksum.Append(buffer, len);
722  }
723  checksum.Finish(id.md5sum);
724 
725  FioFCloseFile(f);
726 
727  include(*this, id);
728  return true;
729  }
730 };
731 
734 
741 const char *FindScenario(const ContentInfo *ci, bool md5sum)
742 {
743  _scanner.Scan(false);
744 
745  for (ScenarioIdentifier &id : _scanner) {
746  if (md5sum ? (memcmp(id.md5sum, ci->md5sum, sizeof(id.md5sum)) == 0)
747  : (id.scenid == ci->unique_id)) {
748  return id.filename;
749  }
750  }
751 
752  return nullptr;
753 }
754 
761 bool HasScenario(const ContentInfo *ci, bool md5sum)
762 {
763  return (FindScenario(ci, md5sum) != nullptr);
764 }
765 
770 {
771  _scanner.Scan(true);
772 }
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
Try to add a fios item set with the given filename.
Definition: fios.cpp:290
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:67
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:16
Basic data to distinguish a scenario.
Definition: fios.cpp:656
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:73
Scanner to find the unique IDs of scenarios.
Definition: fios.cpp:676
FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:459
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
Add a file with the given filename.
Definition: fios.cpp:694
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:97
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD&#39;s encoding from that of the local environment.
Definition: win32.cpp:558
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:332
Structs, typedefs and macros used for TAR file handling.
void Clear()
Remove all items from the list.
Definition: fios.h:186
uint8 md5sum[16]
MD5 checksum of file.
Definition: fios.cpp:658
std::vector< FiosItem > files
The list of files.
Definition: fios.h:200
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
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:1373
size_t Length() const
Get the number of files in the list.
Definition: fios.h:130
#define FOR_ALL_SEARCHPATHS(sp)
Iterator for all the search paths.
Definition: fileio_func.h:47
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:113
Subdirectory for all screenshots.
Definition: fileio_type.h:123
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
Get a list of savegames.
Definition: fios.cpp:492
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
Functions to make screenshots.
Searchpath
Types of searchpaths OpenTTD might use.
Definition: fileio_type.h:131
uint32 scenid
ID for the scenario (generated by content).
Definition: fios.cpp:657
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
Get a list of scenarios.
Definition: fios.cpp:547
File is being saved.
Definition: fileio_type.h:50
Deals with finding savegames.
Definition: fios.h:103
StringID FiosGetDescText(const char **path, uint64 *total_free)
Get descriptive texts.
Definition: fios.cpp:139
Helper for scanning for files with a given name.
Definition: fileio_func.h:70
const FiosItem * FindItem(const char *file)
Find file information of a file by its name from the file list.
Definition: fios.cpp:106
old or new savegame
Definition: fileio_type.h:18
old or new scenario
Definition: fileio_type.h:19
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:264
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we&#39;ve got a given scenario based on its unique ID.
Definition: fios.cpp:761
FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list)
Create the scanner.
Definition: fios.cpp:277
Functions related to low-level strings.
Base directory for all scenarios.
Definition: fileio_type.h:112
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:741
static ScenarioScanner _scanner
Scanner for scenarios.
Definition: fios.cpp:733
bool scanned
Whether we&#39;ve already scanned.
Definition: fios.cpp:677
void FiosMakeSavegameName(char *buf, const char *name, const char *last)
Make a save game or scenario filename from a name.
Definition: fios.cpp:226
nothing to do
Definition: fileio_type.h:17
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
Definition: win32.cpp:92
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:194
static void FiosMakeFilename(char *buf, const char *path, const char *name, const char *ext, const char *last)
Construct a filename from its components in destination buffer buf.
Definition: fios.cpp:204
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:463
A path without any base directory.
Definition: fileio_type.h:125
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
void Scan(bool rescan)
Scan, but only if it&#39;s needed.
Definition: fios.cpp:686
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition: fios.cpp:769
Base directory for all savegames.
Definition: fileio_type.h:110
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
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
Construct a file list with the given kind of files, for the stated purpose.
Definition: fios.cpp:74
FileList & file_list
Destination of the found files.
Definition: fios.cpp:269
Part of the network protocol handling content distribution.
static void GetFileTitle(const char *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:432
File is being loaded.
Definition: fileio_type.h:49
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:74
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:576
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:47
void Compact()
Compact the list down to the smallest block size boundary.
Definition: fios.h:192
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:253
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
Get a list of heightmaps.
Definition: fios.cpp:618
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:357
void FiosMakeHeightmapName(char *buf, const char *name, const char *last)
Construct a filename for a height map.
Definition: fios.cpp:239
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:578
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
Declarations for savegames operations.
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...
List of file information.
Definition: fios.h:112
const char * FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at #_fios_path.
Definition: fios.cpp:150
static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:520
Scanner to scan for a particular type of FIOS file.
Definition: fios.cpp:266
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:571
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:144
heightmap file
Definition: fileio_type.h:20
SaveLoadOperation fop
The kind of file we are looking for.
Definition: fios.cpp:267
FiosItem * Append()
Construct a new entry in the file list.
Definition: fios.h:120
bool operator<(const FiosItem &other) const
Compare two FiosItem&#39;s.
Definition: fios.cpp:51
ScenarioScanner()
Initialise.
Definition: fios.cpp:680
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:643
Container for all important information about a piece of content.
Definition: tcp_content.h:54