OpenTTD
fios.cpp
Go to the documentation of this file.
1 /* $Id: fios.cpp 27893 2017-08-13 18:38:42Z frosch $ */
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 "fios.h"
17 #include "fileio_func.h"
18 #include "tar_type.h"
19 #include "screenshot.h"
20 #include "string_func.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 
52 int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db)
53 {
54  int r = 0;
55 
56  if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da->mtime != db->mtime) {
57  r = da->mtime < db->mtime ? -1 : 1;
58  } else {
59  r = strcasecmp(da->title, db->title);
60  }
61 
62  if (_savegame_sort_order & SORT_DESCENDING) r = -r;
63  return r;
64 }
65 
66 FileList::~FileList()
67 {
68  this->Clear();
69 }
70 
77 {
78  this->Clear();
79 
80  assert(fop == SLO_LOAD || 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 NULL;
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(WINCE)
157  seprintf(_fios_path, _fios_path_last, PATHSEP "");
158 #elif defined(WIN32) || defined(__OS2__)
159  seprintf(_fios_path, _fios_path_last, "%c:" PATHSEP, item->title[0]);
160 #endif
161  break;
162 
163  case FIOS_TYPE_INVALID:
164  break;
165 
166  case FIOS_TYPE_PARENT: {
167  /* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
168  char *s = strrchr(_fios_path, PATHSEPCHAR);
169  if (s != NULL && s != _fios_path) {
170  s[0] = '\0'; // Remove last path separator character, so we can go up one level.
171  }
172  s = strrchr(_fios_path, PATHSEPCHAR);
173  if (s != NULL) {
174  s[1] = '\0'; // go up a directory
175 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
176  /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
177  } else if ((s = strrchr(_fios_path, ':')) != NULL) {
178  s[1] = '\0';
179 #endif
180  }
181  break;
182  }
183 
184  case FIOS_TYPE_DIR:
185  strecat(_fios_path, item->name, _fios_path_last);
186  strecat(_fios_path, PATHSEP, _fios_path_last);
187  break;
188 
189  case FIOS_TYPE_DIRECT:
190  seprintf(_fios_path, _fios_path_last, "%s", item->name);
191  break;
192 
193  case FIOS_TYPE_FILE:
194  case FIOS_TYPE_OLDFILE:
195  case FIOS_TYPE_SCENARIO:
196  case FIOS_TYPE_OLD_SCENARIO:
197  case FIOS_TYPE_PNG:
198  case FIOS_TYPE_BMP:
199  return item->name;
200  }
201 
202  return NULL;
203 }
204 
213 static void FiosMakeFilename(char *buf, const char *path, const char *name, const char *ext, const char *last)
214 {
215  const char *period;
216 
217  /* Don't append the extension if it is already there */
218  period = strrchr(name, '.');
219  if (period != NULL && strcasecmp(period, ext) == 0) ext = "";
220 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
221  if (path != NULL) {
222  unsigned char sepchar = path[(strlen(path) - 1)];
223 
224  if (sepchar != ':' && sepchar != '/') {
225  seprintf(buf, last, "%s" PATHSEP "%s%s", path, name, ext);
226  } else {
227  seprintf(buf, last, "%s%s%s", path, name, ext);
228  }
229  } else {
230  seprintf(buf, last, "%s%s", name, ext);
231  }
232 #else
233  seprintf(buf, last, "%s" PATHSEP "%s%s", path, name, ext);
234 #endif
235 }
236 
243 void FiosMakeSavegameName(char *buf, const char *name, const char *last)
244 {
245  const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
246 
247  FiosMakeFilename(buf, _fios_path, name, extension, last);
248 }
249 
256 void FiosMakeHeightmapName(char *buf, const char *name, const char *last)
257 {
258  char ext[5];
259  ext[0] = '.';
260  strecpy(ext + 1, GetCurrentScreenshotExtension(), lastof(ext));
261 
262  FiosMakeFilename(buf, _fios_path, name, ext, last);
263 }
264 
270 bool FiosDelete(const char *name)
271 {
272  char filename[512];
273 
274  FiosMakeSavegameName(filename, name, lastof(filename));
275  return unlink(filename) == 0;
276 }
277 
278 typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const char *filename, const char *ext, char *title, const char *last);
279 
283 class FiosFileScanner : public FileScanner {
285  fios_getlist_callback_proc *callback_proc;
287 public:
295  fop(fop), callback_proc(callback_proc), file_list(file_list)
296  {}
297 
298  /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
299 };
300 
307 bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
308 {
309  const char *ext = strrchr(filename, '.');
310  if (ext == NULL) return false;
311 
312  char fios_title[64];
313  fios_title[0] = '\0'; // reset the title;
314 
315  FiosType type = this->callback_proc(this->fop, filename, ext, fios_title, lastof(fios_title));
316  if (type == FIOS_TYPE_INVALID) return false;
317 
318  for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
319  if (strcmp(fios->name, filename) == 0) return false;
320  }
321 
322  FiosItem *fios = file_list.Append();
323 #ifdef WIN32
324  struct _stat sb;
325  if (_tstat(OTTD2FS(filename), &sb) == 0) {
326 #else
327  struct stat sb;
328  if (stat(filename, &sb) == 0) {
329 #endif
330  fios->mtime = sb.st_mtime;
331  } else {
332  fios->mtime = 0;
333  }
334 
335  fios->type = type;
336  strecpy(fios->name, filename, lastof(fios->name));
337 
338  /* If the file doesn't have a title, use its filename */
339  const char *t = fios_title;
340  if (StrEmpty(fios_title)) {
341  t = strrchr(filename, PATHSEPCHAR);
342  t = (t == NULL) ? filename : (t + 1);
343  }
344  strecpy(fios->title, t, lastof(fios->title));
345  str_validate(fios->title, lastof(fios->title));
346 
347  return true;
348 }
349 
350 
359 {
360  struct stat sb;
361  struct dirent *dirent;
362  DIR *dir;
363  FiosItem *fios;
364  int sort_start;
365  char d_name[sizeof(fios->name)];
366 
367  file_list.Clear();
368 
369  /* A parent directory link exists if we are not in the root directory */
370  if (!FiosIsRoot(_fios_path)) {
371  fios = file_list.Append();
372  fios->type = FIOS_TYPE_PARENT;
373  fios->mtime = 0;
374  strecpy(fios->name, "..", lastof(fios->name));
375  strecpy(fios->title, ".. (Parent directory)", lastof(fios->title));
376  }
377 
378  /* Show subdirectories */
379  if ((dir = ttd_opendir(_fios_path)) != NULL) {
380  while ((dirent = readdir(dir)) != NULL) {
381  strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
382 
383  /* found file must be directory, but not '.' or '..' */
384  if (FiosIsValidFile(_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
385  (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
386  strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
387  fios = file_list.Append();
388  fios->type = FIOS_TYPE_DIR;
389  fios->mtime = 0;
390  strecpy(fios->name, d_name, lastof(fios->name));
391  seprintf(fios->title, lastof(fios->title), "%s" PATHSEP " (Directory)", d_name);
392  str_validate(fios->title, lastof(fios->title));
393  }
394  }
395  closedir(dir);
396  }
397 
398  /* Sort the subdirs always by name, ascending, remember user-sorting order */
399  {
400  SortingBits order = _savegame_sort_order;
401  _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
402  QSortT(file_list.files.Begin(), file_list.files.Length(), CompareFiosItems);
403  _savegame_sort_order = order;
404  }
405 
406  /* This is where to start sorting for the filenames */
407  sort_start = file_list.Length();
408 
409  /* Show files */
410  FiosFileScanner scanner(fop, callback_proc, file_list);
411  if (subdir == NO_DIRECTORY) {
412  scanner.Scan(NULL, _fios_path, false);
413  } else {
414  scanner.Scan(NULL, subdir, true, true);
415  }
416 
417  QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems);
418 
419  /* Show drives */
420  FiosGetDrives(file_list);
421 
422  file_list.Compact();
423 }
424 
433 static void GetFileTitle(const char *file, char *title, const char *last, Subdirectory subdir)
434 {
435  char buf[MAX_PATH];
436  strecpy(buf, file, lastof(buf));
437  strecat(buf, ".title", lastof(buf));
438 
439  FILE *f = FioFOpenFile(buf, "r", subdir);
440  if (f == NULL) return;
441 
442  size_t read = fread(title, 1, last - title, f);
443  assert(title + read <= last);
444  title[read] = '\0';
445  str_validate(title, last);
446  FioFCloseFile(f);
447 }
448 
460 FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
461 {
462  /* Show savegame files
463  * .SAV OpenTTD saved game
464  * .SS1 Transport Tycoon Deluxe preset game
465  * .SV1 Transport Tycoon Deluxe (Patch) saved game
466  * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
467 
468  /* Don't crash if we supply no extension */
469  if (ext == NULL) return FIOS_TYPE_INVALID;
470 
471  if (strcasecmp(ext, ".sav") == 0) {
472  GetFileTitle(file, title, last, SAVE_DIR);
473  return FIOS_TYPE_FILE;
474  }
475 
476  if (fop == SLO_LOAD) {
477  if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
478  strcasecmp(ext, ".sv2") == 0) {
479  if (title != NULL) GetOldSaveGameName(file, title, last);
480  return FIOS_TYPE_OLDFILE;
481  }
482  }
483 
484  return FIOS_TYPE_INVALID;
485 }
486 
494 {
495  static char *fios_save_path = NULL;
496  static char *fios_save_path_last = NULL;
497 
498  if (fios_save_path == NULL) {
499  fios_save_path = MallocT<char>(MAX_PATH);
500  fios_save_path_last = fios_save_path + MAX_PATH - 1;
501  FioGetDirectory(fios_save_path, fios_save_path_last, SAVE_DIR);
502  }
503 
504  _fios_path = fios_save_path;
505  _fios_path_last = fios_save_path_last;
506 
508 }
509 
521 static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
522 {
523  /* Show scenario files
524  * .SCN OpenTTD style scenario file
525  * .SV0 Transport Tycoon Deluxe (Patch) scenario
526  * .SS0 Transport Tycoon Deluxe preset scenario */
527  if (strcasecmp(ext, ".scn") == 0) {
528  GetFileTitle(file, title, last, SCENARIO_DIR);
529  return FIOS_TYPE_SCENARIO;
530  }
531 
532  if (fop == SLO_LOAD) {
533  if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
534  GetOldSaveGameName(file, title, last);
535  return FIOS_TYPE_OLD_SCENARIO;
536  }
537  }
538 
539  return FIOS_TYPE_INVALID;
540 }
541 
549 {
550  static char *fios_scn_path = NULL;
551  static char *fios_scn_path_last = NULL;
552 
553  /* Copy the default path on first run or on 'New Game' */
554  if (fios_scn_path == NULL) {
555  fios_scn_path = MallocT<char>(MAX_PATH);
556  fios_scn_path_last = fios_scn_path + MAX_PATH - 1;
557  FioGetDirectory(fios_scn_path, fios_scn_path_last, SCENARIO_DIR);
558  }
559 
560  _fios_path = fios_scn_path;
561  _fios_path_last = fios_scn_path_last;
562 
563  char base_path[MAX_PATH];
564  FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR);
565 
566  Subdirectory subdir = (fop == SLO_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY;
567  FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
568 }
569 
570 static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
571 {
572  /* Show heightmap files
573  * .PNG PNG Based heightmap files
574  * .BMP BMP Based heightmap files
575  */
576 
577  FiosType type = FIOS_TYPE_INVALID;
578 
579 #ifdef WITH_PNG
580  if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
581 #endif /* WITH_PNG */
582 
583  if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
584 
585  if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
586 
587  TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
588  if (it != _tar_filelist[SCENARIO_DIR].end()) {
589  /* If the file is in a tar and that tar is not in a heightmap
590  * directory we are for sure not supposed to see it.
591  * Examples of this are pngs part of documentation within
592  * collections of NewGRFs or 32 bpp graphics replacement PNGs.
593  */
594  bool match = false;
595  Searchpath sp;
596  FOR_ALL_SEARCHPATHS(sp) {
597  char buf[MAX_PATH];
598  FioAppendDirectory(buf, lastof(buf), sp, HEIGHTMAP_DIR);
599 
600  if (strncmp(buf, it->second.tar_filename, strlen(buf)) == 0) {
601  match = true;
602  break;
603  }
604  }
605 
606  if (!match) return FIOS_TYPE_INVALID;
607  }
608 
609  GetFileTitle(file, title, last, HEIGHTMAP_DIR);
610 
611  return type;
612 }
613 
620 {
621  static char *fios_hmap_path = NULL;
622  static char *fios_hmap_path_last = NULL;
623 
624  if (fios_hmap_path == NULL) {
625  fios_hmap_path = MallocT<char>(MAX_PATH);
626  fios_hmap_path_last = fios_hmap_path + MAX_PATH - 1;
627  FioGetDirectory(fios_hmap_path, fios_hmap_path_last, HEIGHTMAP_DIR);
628  }
629 
630  _fios_path = fios_hmap_path;
631  _fios_path_last = fios_hmap_path_last;
632 
633  char base_path[MAX_PATH];
634  FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR);
635 
636  Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY;
637  FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
638 }
639 
644 const char *FiosGetScreenshotDir()
645 {
646  static char *fios_screenshot_path = NULL;
647 
648  if (fios_screenshot_path == NULL) {
649  fios_screenshot_path = MallocT<char>(MAX_PATH);
650  FioGetDirectory(fios_screenshot_path, fios_screenshot_path + MAX_PATH - 1, SCREENSHOT_DIR);
651  }
652 
653  return fios_screenshot_path;
654 }
655 
656 #if defined(ENABLE_NETWORK)
657 #include "network/network_content.h"
658 #include "3rdparty/md5/md5.h"
659 
662  uint32 scenid;
663  uint8 md5sum[16];
664  char filename[MAX_PATH];
665 
666  bool operator == (const ScenarioIdentifier &other) const
667  {
668  return this->scenid == other.scenid &&
669  memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
670  }
671 
672  bool operator != (const ScenarioIdentifier &other) const
673  {
674  return !(*this == other);
675  }
676 };
677 
681 class ScenarioScanner : protected FileScanner, public SmallVector<ScenarioIdentifier, 8> {
682  bool scanned;
683 public:
685  ScenarioScanner() : scanned(false) {}
686 
691  void Scan(bool rescan)
692  {
693  if (this->scanned && !rescan) return;
694 
695  this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
696  this->scanned = true;
697  }
698 
699  /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
700  {
701  FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
702  if (f == NULL) return false;
703 
705  int fret = fscanf(f, "%i", &id.scenid);
706  FioFCloseFile(f);
707  if (fret != 1) return false;
708  strecpy(id.filename, filename, lastof(id.filename));
709 
710  Md5 checksum;
711  uint8 buffer[1024];
712  char basename[MAX_PATH];
713  size_t len, size;
714 
715  /* open the scenario file, but first get the name.
716  * This is safe as we check on extension which
717  * must always exist. */
718  strecpy(basename, filename, lastof(basename));
719  *strrchr(basename, '.') = '\0';
720  f = FioFOpenFile(basename, "rb", SCENARIO_DIR, &size);
721  if (f == NULL) return false;
722 
723  /* calculate md5sum */
724  while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
725  size -= len;
726  checksum.Append(buffer, len);
727  }
728  checksum.Finish(id.md5sum);
729 
730  FioFCloseFile(f);
731 
732  this->Include(id);
733  return true;
734  }
735 };
736 
739 
746 const char *FindScenario(const ContentInfo *ci, bool md5sum)
747 {
748  _scanner.Scan(false);
749 
750  for (ScenarioIdentifier *id = _scanner.Begin(); id != _scanner.End(); id++) {
751  if (md5sum ? (memcmp(id->md5sum, ci->md5sum, sizeof(id->md5sum)) == 0)
752  : (id->scenid == ci->unique_id)) {
753  return id->filename;
754  }
755  }
756 
757  return NULL;
758 }
759 
766 bool HasScenario(const ContentInfo *ci, bool md5sum)
767 {
768  return (FindScenario(ci, md5sum) != NULL);
769 }
770 
775 {
776  _scanner.Scan(true);
777 }
778 
779 #endif /* ENABLE_NETWORK */