OpenTTD
fios.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
15 #include "stdafx.h"
16 #include "3rdparty/md5/md5.h"
17 #include "fileio_func.h"
18 #include "fios.h"
20 #include "screenshot.h"
21 #include "string_func.h"
22 #include "tar_type.h"
23 #include <sys/stat.h>
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 char *_fios_path;
35 static const char *_fios_path_last;
36 SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
37 
38 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
39 extern bool FiosIsRoot(const char *path);
40 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
41 extern bool FiosIsHiddenFile(const struct dirent *ent);
42 extern void FiosGetDrives(FileList &file_list);
43 extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
44 
45 /* get the name of an oldstyle savegame */
46 extern void GetOldSaveGameName(const char *file, char *title, const char *last);
47 
53 bool FiosItem::operator< (const FiosItem &other) const
54 {
55  int r = false;
56 
57  if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
58  r = (*this).mtime - other.mtime;
59  } else {
60  r = strnatcmp((*this).title, other.title);
61  }
62  if (r == 0) return false;
63  return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
64 }
65 
66 FileList::~FileList()
67 {
68  this->Clear();
69 }
70 
77 {
78  this->Clear();
79 
80  assert(fop == SLO_LOAD || fop == SLO_SAVE);
81  switch (abstract_filetype) {
82  case FT_NONE:
83  break;
84 
85  case FT_SAVEGAME:
86  FiosGetSavegameList(fop, *this);
87  break;
88 
89  case FT_SCENARIO:
90  FiosGetScenarioList(fop, *this);
91  break;
92 
93  case FT_HEIGHTMAP:
94  FiosGetHeightmapList(fop, *this);
95  break;
96 
97  default:
98  NOT_REACHED();
99  }
100 }
101 
108 const FiosItem *FileList::FindItem(const char *file)
109 {
110  for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
111  if (strcmp(file, item->name) == 0) return item;
112  if (strcmp(file, item->title) == 0) return item;
113  }
114 
115  /* If no name matches, try to parse it as number */
116  char *endptr;
117  int i = strtol(file, &endptr, 10);
118  if (file == endptr || *endptr != '\0') i = -1;
119 
120  if (IsInsideMM(i, 0, this->Length())) return this->Get(i);
121 
122  /* As a last effort assume it is an OpenTTD savegame and
123  * that the ".sav" part was not given. */
124  char long_file[MAX_PATH];
125  seprintf(long_file, lastof(long_file), "%s.sav", file);
126  for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
127  if (strcmp(long_file, item->name) == 0) return item;
128  if (strcmp(long_file, item->title) == 0) return item;
129  }
130 
131  return nullptr;
132 }
133 
141 StringID FiosGetDescText(const char **path, uint64 *total_free)
142 {
143  *path = _fios_path;
144  return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
145 }
146 
152 const char *FiosBrowseTo(const FiosItem *item)
153 {
154  switch (item->type) {
155  case FIOS_TYPE_DRIVE:
156 #if defined(_WIN32) || defined(__OS2__)
157  seprintf(_fios_path, _fios_path_last, "%c:" PATHSEP, item->title[0]);
158 #endif
159  break;
160 
161  case FIOS_TYPE_INVALID:
162  break;
163 
164  case FIOS_TYPE_PARENT: {
165  /* Check for possible nullptr ptr */
166  char *s = strrchr(_fios_path, PATHSEPCHAR);
167  if (s != nullptr && s != _fios_path) {
168  s[0] = '\0'; // Remove last path separator character, so we can go up one level.
169  }
170  s = strrchr(_fios_path, PATHSEPCHAR);
171  if (s != nullptr) {
172  s[1] = '\0'; // go up a directory
173  }
174  break;
175  }
176 
177  case FIOS_TYPE_DIR:
178  strecat(_fios_path, item->name, _fios_path_last);
179  strecat(_fios_path, PATHSEP, _fios_path_last);
180  break;
181 
182  case FIOS_TYPE_DIRECT:
183  seprintf(_fios_path, _fios_path_last, "%s", item->name);
184  break;
185 
186  case FIOS_TYPE_FILE:
187  case FIOS_TYPE_OLDFILE:
188  case FIOS_TYPE_SCENARIO:
189  case FIOS_TYPE_OLD_SCENARIO:
190  case FIOS_TYPE_PNG:
191  case FIOS_TYPE_BMP:
192  return item->name;
193  }
194 
195  return nullptr;
196 }
197 
206 static void FiosMakeFilename(char *buf, const char *path, const char *name, const char *ext, const char *last)
207 {
208  if (path != nullptr) {
209  const char *buf_start = buf;
210  buf = strecpy(buf, path, last);
211  /* Remove trailing path separator, if present */
212  if (buf > buf_start && buf[-1] == PATHSEPCHAR) buf--;
213  }
214 
215  /* Don't append the extension if it is already there */
216  const char *period = strrchr(name, '.');
217  if (period != nullptr && strcasecmp(period, ext) == 0) ext = "";
218 
219  seprintf(buf, last, PATHSEP "%s%s", name, ext);
220 }
221 
228 void FiosMakeSavegameName(char *buf, const char *name, const char *last)
229 {
230  const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
231 
232  FiosMakeFilename(buf, _fios_path, name, extension, last);
233 }
234 
241 void FiosMakeHeightmapName(char *buf, const char *name, const char *last)
242 {
243  char ext[5];
244  ext[0] = '.';
245  strecpy(ext + 1, GetCurrentScreenshotExtension(), lastof(ext));
246 
247  FiosMakeFilename(buf, _fios_path, name, ext, last);
248 }
249 
255 bool FiosDelete(const char *name)
256 {
257  char filename[512];
258 
259  FiosMakeSavegameName(filename, name, lastof(filename));
260  return unlink(filename) == 0;
261 }
262 
263 typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const char *filename, const char *ext, char *title, const char *last);
264 
268 class FiosFileScanner : public FileScanner {
270  fios_getlist_callback_proc *callback_proc;
272 public:
279  FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) :
280  fop(fop), callback_proc(callback_proc), file_list(file_list)
281  {}
282 
283  bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
284 };
285 
292 bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
293 {
294  const char *ext = strrchr(filename, '.');
295  if (ext == nullptr) return false;
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, fios_title, lastof(fios_title));
301  if (type == FIOS_TYPE_INVALID) return false;
302 
303  for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
304  if (strcmp(fios->name, filename) == 0) return false;
305  }
306 
307  FiosItem *fios = file_list.Append();
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), 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, &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, 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  t = strrchr(filename, PATHSEPCHAR);
343  t = (t == nullptr) ? filename : (t + 1);
344  }
345  strecpy(fios->title, t, lastof(fios->title));
346  str_validate(fios->title, lastof(fios->title));
347 
348  return true;
349 }
350 
351 
359 static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
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  /* A parent directory link exists if we are not in the root directory */
371  if (!FiosIsRoot(_fios_path)) {
372  fios = file_list.Append();
373  fios->type = FIOS_TYPE_PARENT;
374  fios->mtime = 0;
375  strecpy(fios->name, "..", lastof(fios->name));
376  strecpy(fios->title, ".. (Parent directory)", lastof(fios->title));
377  }
378 
379  /* Show subdirectories */
380  if ((dir = ttd_opendir(_fios_path)) != nullptr) {
381  while ((dirent = readdir(dir)) != nullptr) {
382  strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
383 
384  /* found file must be directory, but not '.' or '..' */
385  if (FiosIsValidFile(_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
386  (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
387  strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
388  fios = file_list.Append();
389  fios->type = FIOS_TYPE_DIR;
390  fios->mtime = 0;
391  strecpy(fios->name, d_name, lastof(fios->name));
392  seprintf(fios->title, lastof(fios->title), "%s" PATHSEP " (Directory)", d_name);
393  str_validate(fios->title, lastof(fios->title));
394  }
395  }
396  closedir(dir);
397  }
398 
399  /* Sort the subdirs always by name, ascending, remember user-sorting order */
400  {
401  SortingBits order = _savegame_sort_order;
402  _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
403  std::sort(file_list.files.begin(), file_list.files.end());
404  _savegame_sort_order = order;
405  }
406 
407  /* This is where to start sorting for the filenames */
408  sort_start = file_list.Length();
409 
410  /* Show files */
411  FiosFileScanner scanner(fop, callback_proc, file_list);
412  if (subdir == NO_DIRECTORY) {
413  scanner.Scan(nullptr, _fios_path, false);
414  } else {
415  scanner.Scan(nullptr, subdir, true, true);
416  }
417 
418  std::sort(file_list.files.begin() + sort_start, file_list.files.end());
419 
420  /* Show drives */
421  FiosGetDrives(file_list);
422 
423  file_list.Compact();
424 }
425 
434 static void GetFileTitle(const char *file, char *title, const char *last, Subdirectory subdir)
435 {
436  char buf[MAX_PATH];
437  strecpy(buf, file, lastof(buf));
438  strecat(buf, ".title", lastof(buf));
439 
440  FILE *f = FioFOpenFile(buf, "r", subdir);
441  if (f == nullptr) return;
442 
443  size_t read = fread(title, 1, last - title, f);
444  assert(title + read <= last);
445  title[read] = '\0';
446  str_validate(title, last);
447  FioFCloseFile(f);
448 }
449 
461 FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
462 {
463  /* Show savegame files
464  * .SAV OpenTTD saved game
465  * .SS1 Transport Tycoon Deluxe preset game
466  * .SV1 Transport Tycoon Deluxe (Patch) saved game
467  * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
468 
469  /* Don't crash if we supply no extension */
470  if (ext == nullptr) return FIOS_TYPE_INVALID;
471 
472  if (strcasecmp(ext, ".sav") == 0) {
473  GetFileTitle(file, title, last, SAVE_DIR);
474  return FIOS_TYPE_FILE;
475  }
476 
477  if (fop == SLO_LOAD) {
478  if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
479  strcasecmp(ext, ".sv2") == 0) {
480  if (title != nullptr) GetOldSaveGameName(file, title, last);
481  return FIOS_TYPE_OLDFILE;
482  }
483  }
484 
485  return FIOS_TYPE_INVALID;
486 }
487 
495 {
496  static char *fios_save_path = nullptr;
497  static char *fios_save_path_last = nullptr;
498 
499  if (fios_save_path == nullptr) {
500  fios_save_path = MallocT<char>(MAX_PATH);
501  fios_save_path_last = fios_save_path + MAX_PATH - 1;
502  FioGetDirectory(fios_save_path, fios_save_path_last, SAVE_DIR);
503  }
504 
505  _fios_path = fios_save_path;
506  _fios_path_last = fios_save_path_last;
507 
509 }
510 
522 static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
523 {
524  /* Show scenario files
525  * .SCN OpenTTD style scenario file
526  * .SV0 Transport Tycoon Deluxe (Patch) scenario
527  * .SS0 Transport Tycoon Deluxe preset scenario */
528  if (strcasecmp(ext, ".scn") == 0) {
529  GetFileTitle(file, title, last, SCENARIO_DIR);
530  return FIOS_TYPE_SCENARIO;
531  }
532 
533  if (fop == SLO_LOAD) {
534  if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
535  GetOldSaveGameName(file, title, last);
536  return FIOS_TYPE_OLD_SCENARIO;
537  }
538  }
539 
540  return FIOS_TYPE_INVALID;
541 }
542 
550 {
551  static char *fios_scn_path = nullptr;
552  static char *fios_scn_path_last = nullptr;
553 
554  /* Copy the default path on first run or on 'New Game' */
555  if (fios_scn_path == nullptr) {
556  fios_scn_path = MallocT<char>(MAX_PATH);
557  fios_scn_path_last = fios_scn_path + MAX_PATH - 1;
558  FioGetDirectory(fios_scn_path, fios_scn_path_last, SCENARIO_DIR);
559  }
560 
561  _fios_path = fios_scn_path;
562  _fios_path_last = fios_scn_path_last;
563 
564  char base_path[MAX_PATH];
565  FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR);
566 
567  Subdirectory subdir = (fop == SLO_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY;
568  FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
569 }
570 
571 static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
572 {
573  /* Show heightmap files
574  * .PNG PNG Based heightmap files
575  * .BMP BMP Based heightmap files
576  */
577 
578  FiosType type = FIOS_TYPE_INVALID;
579 
580 #ifdef WITH_PNG
581  if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
582 #endif /* WITH_PNG */
583 
584  if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
585 
586  if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
587 
588  TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
589  if (it != _tar_filelist[SCENARIO_DIR].end()) {
590  /* If the file is in a tar and that tar is not in a heightmap
591  * directory we are for sure not supposed to see it.
592  * Examples of this are pngs part of documentation within
593  * collections of NewGRFs or 32 bpp graphics replacement PNGs.
594  */
595  bool match = false;
596  Searchpath sp;
597  FOR_ALL_SEARCHPATHS(sp) {
598  char buf[MAX_PATH];
599  FioAppendDirectory(buf, lastof(buf), sp, HEIGHTMAP_DIR);
600 
601  if (strncmp(buf, it->second.tar_filename, strlen(buf)) == 0) {
602  match = true;
603  break;
604  }
605  }
606 
607  if (!match) return FIOS_TYPE_INVALID;
608  }
609 
610  GetFileTitle(file, title, last, HEIGHTMAP_DIR);
611 
612  return type;
613 }
614 
621 {
622  static char *fios_hmap_path = nullptr;
623  static char *fios_hmap_path_last = nullptr;
624 
625  if (fios_hmap_path == nullptr) {
626  fios_hmap_path = MallocT<char>(MAX_PATH);
627  fios_hmap_path_last = fios_hmap_path + MAX_PATH - 1;
628  FioGetDirectory(fios_hmap_path, fios_hmap_path_last, HEIGHTMAP_DIR);
629  }
630 
631  _fios_path = fios_hmap_path;
632  _fios_path_last = fios_hmap_path_last;
633 
634  char base_path[MAX_PATH];
635  FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR);
636 
637  Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY;
638  FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
639 }
640 
645 const char *FiosGetScreenshotDir()
646 {
647  static char *fios_screenshot_path = nullptr;
648 
649  if (fios_screenshot_path == nullptr) {
650  fios_screenshot_path = MallocT<char>(MAX_PATH);
651  FioGetDirectory(fios_screenshot_path, fios_screenshot_path + MAX_PATH - 1, SCREENSHOT_DIR);
652  }
653 
654  return fios_screenshot_path;
655 }
656 
659  uint32 scenid;
660  uint8 md5sum[16];
661  char filename[MAX_PATH];
662 
663  bool operator == (const ScenarioIdentifier &other) const
664  {
665  return this->scenid == other.scenid &&
666  memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
667  }
668 
669  bool operator != (const ScenarioIdentifier &other) const
670  {
671  return !(*this == other);
672  }
673 };
674 
678 class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
679  bool scanned;
680 public:
682  ScenarioScanner() : scanned(false) {}
683 
688  void Scan(bool rescan)
689  {
690  if (this->scanned && !rescan) return;
691 
692  this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
693  this->scanned = true;
694  }
695 
696  bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
697  {
698  FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
699  if (f == nullptr) return false;
700 
702  int fret = fscanf(f, "%i", &id.scenid);
703  FioFCloseFile(f);
704  if (fret != 1) return false;
705  strecpy(id.filename, filename, lastof(id.filename));
706 
707  Md5 checksum;
708  uint8 buffer[1024];
709  char basename[MAX_PATH];
710  size_t len, size;
711 
712  /* open the scenario file, but first get the name.
713  * This is safe as we check on extension which
714  * must always exist. */
715  strecpy(basename, filename, lastof(basename));
716  *strrchr(basename, '.') = '\0';
717  f = FioFOpenFile(basename, "rb", SCENARIO_DIR, &size);
718  if (f == nullptr) return false;
719 
720  /* calculate md5sum */
721  while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
722  size -= len;
723  checksum.Append(buffer, len);
724  }
725  checksum.Finish(id.md5sum);
726 
727  FioFCloseFile(f);
728 
729  include(*this, id);
730  return true;
731  }
732 };
733 
736 
743 const char *FindScenario(const ContentInfo *ci, bool md5sum)
744 {
745  _scanner.Scan(false);
746 
747  for (ScenarioIdentifier &id : _scanner) {
748  if (md5sum ? (memcmp(id.md5sum, ci->md5sum, sizeof(id.md5sum)) == 0)
749  : (id.scenid == ci->unique_id)) {
750  return id.filename;
751  }
752  }
753 
754  return nullptr;
755 }
756 
763 bool HasScenario(const ContentInfo *ci, bool md5sum)
764 {
765  return (FindScenario(ci, md5sum) != nullptr);
766 }
767 
772 {
773  _scanner.Scan(true);
774 }
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:292
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:69
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:18
Basic data to distinguish a scenario.
Definition: fios.cpp:658
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:75
Scanner to find the unique IDs of scenarios.
Definition: fios.cpp:678
FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:461
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
Add a file with the given filename.
Definition: fios.cpp:696
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:99
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD&#39;s encoding from that of the local environment.
Definition: win32.cpp:560
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:334
Structs, typedefs and macros used for TAR file handling.
void Clear()
Remove all items from the list.
Definition: fios.h:188
uint8 md5sum[16]
MD5 checksum of file.
Definition: fios.cpp:660
std::vector< FiosItem > files
The list of files.
Definition: fios.h:202
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:409
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:1375
size_t Length() const
Get the number of files in the list.
Definition: fios.h:132
#define FOR_ALL_SEARCHPATHS(sp)
Iterator for all the search paths.
Definition: fileio_func.h:49
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:115
Subdirectory for all screenshots.
Definition: fileio_type.h:125
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
Get a list of savegames.
Definition: fios.cpp:494
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
Functions to make screenshots.
Searchpath
Types of searchpaths OpenTTD might use.
Definition: fileio_type.h:133
uint32 scenid
ID for the scenario (generated by content).
Definition: fios.cpp:659
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
Get a list of scenarios.
Definition: fios.cpp:549
File is being saved.
Definition: fileio_type.h:52
Deals with finding savegames.
Definition: fios.h:105
StringID FiosGetDescText(const char **path, uint64 *total_free)
Get descriptive texts.
Definition: fios.cpp:141
Helper for scanning for files with a given name.
Definition: fileio_func.h:72
const FiosItem * FindItem(const char *file)
Find file information of a file by its name from the file list.
Definition: fios.cpp:108
old or new savegame
Definition: fileio_type.h:20
old or new scenario
Definition: fileio_type.h:21
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:266
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we&#39;ve got a given scenario based on its unique ID.
Definition: fios.cpp:763
FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list)
Create the scanner.
Definition: fios.cpp:279
Functions related to low-level strings.
Base directory for all scenarios.
Definition: fileio_type.h:114
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:743
static ScenarioScanner _scanner
Scanner for scenarios.
Definition: fios.cpp:735
bool scanned
Whether we&#39;ve already scanned.
Definition: fios.cpp:679
void FiosMakeSavegameName(char *buf, const char *name, const char *last)
Make a save game or scenario filename from a name.
Definition: fios.cpp:228
nothing to do
Definition: fileio_type.h:19
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:222
Definition: win32.cpp:94
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:196
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:206
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:465
A path without any base directory.
Definition: fileio_type.h:127
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:688
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition: fios.cpp:771
Base directory for all savegames.
Definition: fileio_type.h:112
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:205
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
Construct a file list with the given kind of files, for the stated purpose.
Definition: fios.cpp:76
FileList & file_list
Destination of the found files.
Definition: fios.cpp:271
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:434
File is being loaded.
Definition: fileio_type.h:51
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:76
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:578
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:49
void Compact()
Compact the list down to the smallest block size boundary.
Definition: fios.h:194
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:255
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
Get a list of heightmaps.
Definition: fios.cpp:620
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
void FiosMakeHeightmapName(char *buf, const char *name, const char *last)
Construct a filename for a height map.
Definition: fios.cpp:241
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:580
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
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:114
const char * FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at #_fios_path.
Definition: fios.cpp:152
static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:522
Scanner to scan for a particular type of FIOS file.
Definition: fios.cpp:268
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:574
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:146
heightmap file
Definition: fileio_type.h:22
SaveLoadOperation fop
The kind of file we are looking for.
Definition: fios.cpp:269
FiosItem * Append()
Construct a new entry in the file list.
Definition: fios.h:122
bool operator<(const FiosItem &other) const
Compare two FiosItem&#39;s.
Definition: fios.cpp:53
ScenarioScanner()
Initialise.
Definition: fios.cpp:682
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:645
Container for all important information about a piece of content.
Definition: tcp_content.h:56