OpenTTD Source: src/fios.cpp Source File

OpenTTD Source 1.11.0-RC1

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"
17 #include "network/network_content.h"
18 #include "screenshot.h"
19 #include "string_func.h"
20 #include "strings_func.h"
21 #include "tar_type.h"
22 #include <sys/stat.h>
23 #include <functional>
24 #include <optional>
25
26 #ifndef _WIN32
27 # include <unistd.h>
28 #endif /* _WIN32 */
29
30 #include "table/strings.h"
31
32 #include "safeguards.h"
33
34 /* Variables to display file lists */
35 static std::string *_fios_path = nullptr;
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 std::string &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
76 void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
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->c_str();
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 assert(_fios_path != nullptr);
158 *_fios_path = std::string{ item->title[0] } + ":" PATHSEP;
159 #endif
160 break;
161
162 case FIOS_TYPE_INVALID:
163 break;
164
165 case FIOS_TYPE_PARENT: {
166 assert(_fios_path != nullptr);
167 auto s = _fios_path->find_last_of(PATHSEPCHAR);
168 if (s != std::string::npos && s != 0) {
169 _fios_path->erase(s); // Remove last path separator character, so we can go up one level.
170 }
171
172 s = _fios_path->find_last_of(PATHSEPCHAR);
173 if (s != std::string::npos) {
174 _fios_path->erase(s + 1); // go up a directory
175 }
176 break;
177 }
178
179 case FIOS_TYPE_DIR:
180 assert(_fios_path != nullptr);
181 *_fios_path += item->name;
182 *_fios_path += PATHSEP;
183 break;
184
185 case FIOS_TYPE_DIRECT:
186 assert(_fios_path != nullptr);
187 *_fios_path = item->name;
188 break;
189
190 case FIOS_TYPE_FILE:
191 case FIOS_TYPE_OLDFILE:
192 case FIOS_TYPE_SCENARIO:
193 case FIOS_TYPE_OLD_SCENARIO:
194 case FIOS_TYPE_PNG:
195 case FIOS_TYPE_BMP:
196 return item->name;
197 }
198
199 return nullptr;
200 }
201
209 static std::string FiosMakeFilename(const std::string *path, const char *name, const char *ext)
210 {
211 std::string buf;
212
213 if (path != nullptr) {
214 buf = *path;
215 /* Remove trailing path separator, if present */
216 if (!buf.empty() && buf.back() == PATHSEPCHAR) buf.pop_back();
217 }
218
219 /* Don't append the extension if it is already there */
220 const char *period = strrchr(name, '.');
221 if (period != nullptr && strcasecmp(period, ext) == 0) ext = "";
222
223 return buf + PATHSEP + name + ext;
224 }
225
233 std::string FiosMakeSavegameName(const char *name)
234 {
235 const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
236
237 return FiosMakeFilename(_fios_path, name, extension);
238 }
239
245 std::string FiosMakeHeightmapName(const char *name)
246 {
247 std::string ext(".");
248 ext += GetCurrentScreenshotExtension();
249
250 return FiosMakeFilename(_fios_path, name, ext.c_str());
251 }
252
258 bool FiosDelete(const char *name)
259 {
260 std::string filename = FiosMakeSavegameName(name);
261 return unlink(filename.c_str()) == 0;
262 }
263
264 typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const std::string &filename, const char *ext, char *title, const char *last);
265
269 class FiosFileScanner : public FileScanner {
270 SaveLoadOperation fop;
271 fios_getlist_callback_proc *callback_proc;
272 FileList &file_list;
273 public:
280 FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) :
281 fop(fop), callback_proc(callback_proc), file_list(file_list)
282 {}
283
284 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
285 };
286
293 bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
294 {
295 auto sep = filename.rfind('.');
296 if (sep == std::string::npos) return false;
297 std::string ext = filename.substr(sep);
298
299 char fios_title[64];
300 fios_title[0] = '\0'; // reset the title;
301
302 FiosType type = this->callback_proc(this->fop, filename, ext.c_str(), fios_title, lastof(fios_title));
303 if (type == FIOS_TYPE_INVALID) return false;
304
305 for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
306 if (filename == fios->name) return false;
307 }
308
309 FiosItem *fios = file_list.Append();
310 #ifdef _WIN32
311 // Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
312 HANDLE fh = CreateFile(OTTD2FS(filename.c_str()), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
313
314 if (fh != INVALID_HANDLE_VALUE) {
315 FILETIME ft;
316 ULARGE_INTEGER ft_int64;
317
318 if (GetFileTime(fh, nullptr, nullptr, &ft) != 0) {
319 ft_int64.HighPart = ft.dwHighDateTime;
320 ft_int64.LowPart = ft.dwLowDateTime;
321
322 // Convert from hectonanoseconds since 01/01/1601 to seconds since 01/01/1970
323 fios->mtime = ft_int64.QuadPart / 10000000ULL - 11644473600ULL;
324 } else {
325 fios->mtime = 0;
326 }
327
328 CloseHandle(fh);
329 #else
330 struct stat sb;
331 if (stat(filename.c_str(), &sb) == 0) {
332 fios->mtime = sb.st_mtime;
333 #endif
334 } else {
335 fios->mtime = 0;
336 }
337
338 fios->type = type;
339 strecpy(fios->name, filename.c_str(), lastof(fios->name));
340
341 /* If the file doesn't have a title, use its filename */
342 const char *t = fios_title;
343 if (StrEmpty(fios_title)) {
344 auto ps = filename.rfind(PATHSEPCHAR);
345 t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
346 }
347 strecpy(fios->title, t, lastof(fios->title));
348 str_validate(fios->title, lastof(fios->title));
349
350 return true;
351 }
352
353
361 static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
362 {
363 struct stat sb;
364 struct dirent *dirent;
365 DIR *dir;
366 FiosItem *fios;
367 size_t sort_start;
368 char d_name[sizeof(fios->name)];
369
370 file_list.Clear();
371
372 assert(_fios_path != nullptr);
373
374 /* A parent directory link exists if we are not in the root directory */
375 if (!FiosIsRoot(_fios_path->c_str())) {
376 fios = file_list.Append();
377 fios->type = FIOS_TYPE_PARENT;
378 fios->mtime = 0;
379 strecpy(fios->name, "..", lastof(fios->name));
380 SetDParamStr(0, "..");
381 GetString(fios->title, STR_SAVELOAD_PARENT_DIRECTORY, lastof(fios->title));
382 }
383
384 /* Show subdirectories */
385 if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
386 while ((dirent = readdir(dir)) != nullptr) {
387 strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
388
389 /* found file must be directory, but not '.' or '..' */
390 if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) &&
391 (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
392 strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
393 fios = file_list.Append();
394 fios->type = FIOS_TYPE_DIR;
395 fios->mtime = 0;
396 strecpy(fios->name, d_name, lastof(fios->name));
397 std::string dirname = std::string(d_name) + PATHSEP;
398 SetDParamStr(0, dirname.c_str());
399 GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title));
400 str_validate(fios->title, lastof(fios->title));
401 }
402 }
403 closedir(dir);
404 }
405
406 /* Sort the subdirs always by name, ascending, remember user-sorting order */
407 {
408 SortingBits order = _savegame_sort_order;
409 _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
410 std::sort(file_list.files.begin(), file_list.files.end());
411 _savegame_sort_order = order;
412 }
413
414 /* This is where to start sorting for the filenames */
415 sort_start = file_list.Length();
416
417 /* Show files */
418 FiosFileScanner scanner(fop, callback_proc, file_list);
419 if (subdir == NO_DIRECTORY) {
420 scanner.Scan(nullptr, _fios_path->c_str(), false);
421 } else {
422 scanner.Scan(nullptr, subdir, true, true);
423 }
424
425 std::sort(file_list.files.begin() + sort_start, file_list.files.end());
426
427 /* Show drives */
428 FiosGetDrives(file_list);
429
430 file_list.Compact();
431 }
432
441 static void GetFileTitle(const std::string &file, char *title, const char *last, Subdirectory subdir)
442 {
443 std::string buf = file;
444 buf += ".title";
445
446 FILE *f = FioFOpenFile(buf, "r", subdir);
447 if (f == nullptr) return;
448
449 size_t read = fread(title, 1, last - title, f);
450 assert(title + read <= last);
451 title[read] = '\0';
452 str_validate(title, last);
453 FioFCloseFile(f);
454 }
455
467 FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
468 {
469 /* Show savegame files
470 * .SAV OpenTTD saved game
471 * .SS1 Transport Tycoon Deluxe preset game
472 * .SV1 Transport Tycoon Deluxe (Patch) saved game
473 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
474
475 /* Don't crash if we supply no extension */
476 if (ext == nullptr) return FIOS_TYPE_INVALID;
477
478 if (strcasecmp(ext, ".sav") == 0) {
479 GetFileTitle(file, title, last, SAVE_DIR);
480 return FIOS_TYPE_FILE;
481 }
482
483 if (fop == SLO_LOAD) {
484 if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
485 strcasecmp(ext, ".sv2") == 0) {
486 if (title != nullptr) GetOldSaveGameName(file, title, last);
487 return FIOS_TYPE_OLDFILE;
488 }
489 }
490
491 return FIOS_TYPE_INVALID;
492 }
493
500 void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
501 {
502 static std::optional<std::string> fios_save_path;
503
504 if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
505
506 _fios_path = &(*fios_save_path);
507
508 FiosGetFileList(fop, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
509 }
510
522 static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &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
549 void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
550 {
551 static std::optional<std::string> fios_scn_path;
552
553 /* Copy the default path on first run or on 'New Game' */
554 if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
555
556 _fios_path = &(*fios_scn_path);
557
558 std::string base_path = FioFindDirectory(SCENARIO_DIR);
559 Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
560 FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
561 }
562
563 static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
564 {
565 /* Show heightmap files
566 * .PNG PNG Based heightmap files
567 * .BMP BMP Based heightmap files
568 */
569
570 FiosType type = FIOS_TYPE_INVALID;
571
572 #ifdef WITH_PNG
573 if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
574 #endif /* WITH_PNG */
575
576 if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
577
578 if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
579
580 TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
581 if (it != _tar_filelist[SCENARIO_DIR].end()) {
582 /* If the file is in a tar and that tar is not in a heightmap
583 * directory we are for sure not supposed to see it.
584 * Examples of this are pngs part of documentation within
585 * collections of NewGRFs or 32 bpp graphics replacement PNGs.
586 */
587 bool match = false;
588 Searchpath sp;
589 FOR_ALL_SEARCHPATHS(sp) {
590 std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
591
592 if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
593 match = true;
594 break;
595 }
596 }
597
598 if (!match) return FIOS_TYPE_INVALID;
599 }
600
601 GetFileTitle(file, title, last, HEIGHTMAP_DIR);
602
603 return type;
604 }
605
611 void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
612 {
613 static std::optional<std::string> fios_hmap_path;
614
615 if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
616
617 _fios_path = &(*fios_hmap_path);
618
619 std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
620 Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
621 FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
622 }
623
628 const char *FiosGetScreenshotDir()
629 {
630 static std::optional<std::string> fios_screenshot_path;
631
632 if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
633
634 return fios_screenshot_path->c_str();
635 }
636
638 struct ScenarioIdentifier {
639 uint32 scenid;
640 uint8 md5sum[16];
641 char filename[MAX_PATH];
642
643 bool operator == (const ScenarioIdentifier &other) const
644 {
645 return this->scenid == other.scenid &&
646 memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
647 }
648
649 bool operator != (const ScenarioIdentifier &other) const
650 {
651 return !(*this == other);
652 }
653 };
654
658 class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
659 bool scanned;
660 public:
662 ScenarioScanner() : scanned(false) {}
663
668 void Scan(bool rescan)
669 {
670 if (this->scanned && !rescan) return;
671
672 this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
673 this->scanned = true;
674 }
675
676 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
677 {
678 FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
679 if (f == nullptr) return false;
680
681 ScenarioIdentifier id;
682 int fret = fscanf(f, "%u", &id.scenid);
683 FioFCloseFile(f);
684 if (fret != 1) return false;
685 strecpy(id.filename, filename.c_str(), lastof(id.filename));
686
687 Md5 checksum;
688 uint8 buffer[1024];
689 size_t len, size;
690
691 /* open the scenario file, but first get the name.
692 * This is safe as we check on extension which
693 * must always exist. */
694 f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
695 if (f == nullptr) return false;
696
697 /* calculate md5sum */
698 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
699 size -= len;
700 checksum.Append(buffer, len);
701 }
702 checksum.Finish(id.md5sum);
703
704 FioFCloseFile(f);
705
706 include(*this, id);
707 return true;
708 }
709 };
710
712 static ScenarioScanner _scanner;
713
720 const char *FindScenario(const ContentInfo *ci, bool md5sum)
721 {
722 _scanner.Scan(false);
723
724 for (ScenarioIdentifier &id : _scanner) {
725 if (md5sum ? (memcmp(id.md5sum, ci->md5sum, sizeof(id.md5sum)) == 0)
726 : (id.scenid == ci->unique_id)) {
727 return id.filename;
728 }
729 }
730
731 return nullptr;
732 }
733
740 bool HasScenario(const ContentInfo *ci, bool md5sum)
741 {
742 return (FindScenario(ci, md5sum) != nullptr);
743 }
744
748 void ScanScenarios()
749 {
750 _scanner.Scan(true);
751 }
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:522
FiosGetDescText
StringID FiosGetDescText(const char **path, uint64 *total_free)
Get descriptive texts.
Definition: fios.cpp:141
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
FS2OTTD
const char * FS2OTTD(const wchar_t *name)
Convert to OpenTTD's encoding from wide characters.
Definition: win32.cpp:565
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:628
FiosGetScenarioList
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
Get a list of scenarios.
Definition: fios.cpp:549
ScanScenarios
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition: fios.cpp:748
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:270
ScenarioScanner::scanned
bool scanned
Whether we've already scanned.
Definition: fios.cpp:659
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:611
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:441
FiosFileScanner::file_list
FileList & file_list
Destination of the found files.
Definition: fios.cpp:272
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:245
FiosDelete
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:258
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:293
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:668
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:639
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:76
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:152
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:108
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:209
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:271
FileList::Append
FiosItem * Append()
Construct a new entry in the file list.
Definition: fios.h:120
ScenarioScanner::ScenarioScanner
ScenarioScanner()
Initialise.
Definition: fios.cpp:662
string_func.h
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
strings_func.h
FT_NONE
@ FT_NONE
nothing to do
Definition: fileio_type.h:17
FiosGetSavegameListCallback
FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:467
FiosGetSavegameList
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
Get a list of savegames.
Definition: fios.cpp:500
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:125
DIR
Definition: win32.cpp:92
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:638
GetCurrentScreenshotExtension
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:576
FiosFileScanner
Scanner to scan for a particular type of FIOS file.
Definition: fios.cpp:269
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:712
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:676
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:640
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:233
FiosFileScanner::FiosFileScanner
FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list)
Create the scanner.
Definition: fios.cpp:280
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:658
FindScenario
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:720
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
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:361
OTTD2FS
const wchar_t * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD's encoding to wide characters.
Definition: win32.cpp:580
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:286
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:740
FiosItem::operator<
bool operator<(const FiosItem &other) const
Compare two FiosItem's.
Definition: fios.cpp:53