annotate src/fios.cpp @ 14396:4de60292e2a6 draft

(svn r18953) -Feature: [NoAI] allow editing AI settings while an AI is running Only settings with the AICONFIG_INGAME flag can be editted in this way
author yexo <yexo@openttd.org>
date Fri, 29 Jan 2010 21:38:55 +0000
parents afdfdda87cd4
children ec975dc25575
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1 /* $Id$ */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2
12778
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12777
diff changeset
3 /*
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12777
diff changeset
4 * This file is part of OpenTTD.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12777
diff changeset
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.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12777
diff changeset
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.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12777
diff changeset
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/>.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12777
diff changeset
8 */
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12777
diff changeset
9
6117
6def6ecb1bf7 (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas <belugas@openttd.org>
parents: 6088
diff changeset
10 /** @file fios.cpp
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
11 * This file contains functions for building file lists for the save/load dialogs.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
12 */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
13
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
14 #include "stdafx.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
15 #include "openttd.h"
13030
12fa346326b0 (svn r17528) -Codechange: use QSortT instead of qsort for sorting FiosItems
rubidium <rubidium@openttd.org>
parents: 12778
diff changeset
16 #include "core/sort_func.hpp"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
17 #include "fios.h"
10039
6301378f3e0f (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium <rubidium@openttd.org>
parents: 9730
diff changeset
18 #include "fileio_func.h"
11643
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
19 #include "tar_type.h"
8214
6385dffc0b37 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium <rubidium@openttd.org>
parents: 8151
diff changeset
20 #include "string_func.h"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
21 #include <sys/stat.h>
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
22
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
23 #ifdef WIN32
8378
7d35afa253ec (svn r11945) -Fix: FileExists() failed for non latin paths (win32)
glx <glx@openttd.org>
parents: 8264
diff changeset
24 # define access _taccess
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
25 #else
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
26 # include <unistd.h>
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
27 #endif /* WIN32 */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
28
8264
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8214
diff changeset
29 #include "table/strings.h"
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8214
diff changeset
30
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
31 /* Variables to display file lists */
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
32 SmallVector<FiosItem, 32> _fios_items;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
33 static char *_fios_path;
8151
bee108a98a39 (svn r11713) -Codechange: Move some declarations and definitions in a more logical disposition
belugas <belugas@openttd.org>
parents: 8131
diff changeset
34 SmallFiosItem _file_to_saveload;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
35
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
36 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
37 extern bool FiosIsRoot(const char *path);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
38 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
6240
1d99c3541c11 (svn r9043) -Fix [FS#652]: the personal (.openttd) directories were hidden in the load/save directory listings (stepancheg).
rubidium <rubidium@openttd.org>
parents: 6179
diff changeset
39 extern bool FiosIsHiddenFile(const struct dirent *ent);
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6240
diff changeset
40 extern void FiosGetDrives();
10771
44e71a85349d (svn r15104) -Codechange: unify the resolving of the xyz kbytes/megabytes strings.
rubidium <rubidium@openttd.org>
parents: 10405
diff changeset
41 extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
42
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
43 /* get the name of an oldstyle savegame */
11261
5a50ba45cf24 (svn r15610) -Change: allow loading of heightmaps and savegames (any form) that are in .tar files
rubidium <rubidium@openttd.org>
parents: 11259
diff changeset
44 extern void GetOldSaveGameName(const char *file, char *title, const char *last);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
45
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
46 /**
13030
12fa346326b0 (svn r17528) -Codechange: use QSortT instead of qsort for sorting FiosItems
rubidium <rubidium@openttd.org>
parents: 12778
diff changeset
47 * Compare two FiosItem's. Used with sort when sorting the file list.
12fa346326b0 (svn r17528) -Codechange: use QSortT instead of qsort for sorting FiosItems
rubidium <rubidium@openttd.org>
parents: 12778
diff changeset
48 * @param da A pointer to the first FiosItem to compare.
12fa346326b0 (svn r17528) -Codechange: use QSortT instead of qsort for sorting FiosItems
rubidium <rubidium@openttd.org>
parents: 12778
diff changeset
49 * @param db A pointer to the second FiosItem to compare.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
50 * @return -1, 0 or 1, depending on how the two items should be sorted.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
51 */
13030
12fa346326b0 (svn r17528) -Codechange: use QSortT instead of qsort for sorting FiosItems
rubidium <rubidium@openttd.org>
parents: 12778
diff changeset
52 int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
53 {
11262
de226219239a (svn r15611) -Change: when sorting files on modification date and the modification date is equal sort on the title so reversing the sorting direction actually reverses the order the listed files
rubidium <rubidium@openttd.org>
parents: 11261
diff changeset
54 int r = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
55
11308
4228af157233 (svn r15657) -Fix [FS#2716]: undeterministic file sorting when the date is equal for all files
rubidium <rubidium@openttd.org>
parents: 11285
diff changeset
56 if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da->mtime != db->mtime) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
57 r = da->mtime < db->mtime ? -1 : 1;
11308
4228af157233 (svn r15657) -Fix [FS#2716]: undeterministic file sorting when the date is equal for all files
rubidium <rubidium@openttd.org>
parents: 11285
diff changeset
58 } else {
4228af157233 (svn r15657) -Fix [FS#2716]: undeterministic file sorting when the date is equal for all files
rubidium <rubidium@openttd.org>
parents: 11285
diff changeset
59 r = strcasecmp(da->title, db->title);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
60 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
61
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
62 if (_savegame_sort_order & SORT_DESCENDING) r = -r;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
63 return r;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
64 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
65
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
66 /** Clear the list */
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6240
diff changeset
67 void FiosFreeSavegameList()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
68 {
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
69 _fios_items.Clear();
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
70 _fios_items.Compact();
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
71 };
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
72
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
73 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
74 * Get descriptive texts. Returns the path and free space
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
75 * left on the device
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
76 * @param path string describing the path
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
77 * @param total_free total free space in megabytes, optional (can be NULL)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
78 * @return StringID describing the path (free space or failure)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
79 */
10771
44e71a85349d (svn r15104) -Codechange: unify the resolving of the xyz kbytes/megabytes strings.
rubidium <rubidium@openttd.org>
parents: 10405
diff changeset
80 StringID FiosGetDescText(const char **path, uint64 *total_free)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
81 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
82 *path = _fios_path;
11730
a39da1e4c1f5 (svn r16118) -Change/cleanup: remove the hexadecimal 'in TTD the string had this ID' from 'some' strings and replace the string name with something more sensible.
rubidium <rubidium@openttd.org>
parents: 11643
diff changeset
83 return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
84 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
85
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
86 /* Browse to a new path based on the passed FiosItem struct
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
87 * @param *item FiosItem object telling us what to do
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
88 * @return a string if we have given a file as a target, otherwise NULL */
11256
f63c1d69ed92 (svn r15605) -Codechange: constify a function
rubidium <rubidium@openttd.org>
parents: 10960
diff changeset
89 const char *FiosBrowseTo(const FiosItem *item)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
90 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
91 char *path = _fios_path;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
92
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
93 switch (item->type) {
9032
e95cf3676a54 (svn r12850) -Fix (r12848): compilation was broken for some platforms
glx <glx@openttd.org>
parents: 9031
diff changeset
94 case FIOS_TYPE_DRIVE:
7408
53fa9a58449a (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight <truelight@openttd.org>
parents: 7138
diff changeset
95 #if defined(WINCE)
10301
2dc5b1b8f525 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium <rubidium@openttd.org>
parents: 10039
diff changeset
96 snprintf(path, MAX_PATH, PATHSEP "");
7408
53fa9a58449a (svn r10781) -Fix: ported more pieces to WinCE, so it compiles a bit more
truelight <truelight@openttd.org>
parents: 7138
diff changeset
97 #elif defined(WIN32) || defined(__OS2__)
10301
2dc5b1b8f525 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium <rubidium@openttd.org>
parents: 10039
diff changeset
98 snprintf(path, MAX_PATH, "%c:" PATHSEP, item->title[0]);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
99 #endif
9032
e95cf3676a54 (svn r12850) -Fix (r12848): compilation was broken for some platforms
glx <glx@openttd.org>
parents: 9031
diff changeset
100 /* Fallthrough */
e95cf3676a54 (svn r12850) -Fix (r12848): compilation was broken for some platforms
glx <glx@openttd.org>
parents: 9031
diff changeset
101 case FIOS_TYPE_INVALID:
e95cf3676a54 (svn r12850) -Fix (r12848): compilation was broken for some platforms
glx <glx@openttd.org>
parents: 9031
diff changeset
102 break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
103
9030
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
104 case FIOS_TYPE_PARENT: {
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
105 /* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
106 char *s = strrchr(path, PATHSEPCHAR);
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
107 if (s != NULL && s != path) {
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
108 s[0] = '\0'; // Remove last path separator character, so we can go up one level.
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
109 }
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
110 s = strrchr(path, PATHSEPCHAR);
12762
839af08c39fc (svn r17228) -Codechange: some coding style fixes
rubidium <rubidium@openttd.org>
parents: 12328
diff changeset
111 if (s != NULL) {
839af08c39fc (svn r17228) -Codechange: some coding style fixes
rubidium <rubidium@openttd.org>
parents: 12328
diff changeset
112 s[1] = '\0'; // go up a directory
9030
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
113 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
114 /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
12762
839af08c39fc (svn r17228) -Codechange: some coding style fixes
rubidium <rubidium@openttd.org>
parents: 12328
diff changeset
115 } else if ((s = strrchr(path, ':')) != NULL) {
839af08c39fc (svn r17228) -Codechange: some coding style fixes
rubidium <rubidium@openttd.org>
parents: 12328
diff changeset
116 s[1] = '\0';
9030
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
117 #endif
12762
839af08c39fc (svn r17228) -Codechange: some coding style fixes
rubidium <rubidium@openttd.org>
parents: 12328
diff changeset
118 }
9030
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
119 break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
120 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
121
9030
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
122 case FIOS_TYPE_DIR:
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
123 strcat(path, item->name);
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
124 strcat(path, PATHSEP);
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
125 break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
126
9030
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
127 case FIOS_TYPE_DIRECT:
10301
2dc5b1b8f525 (svn r14542) -Codechange: replace some sprintf with s[en]printf to make sure they will not overflow their buffers.
rubidium <rubidium@openttd.org>
parents: 10039
diff changeset
128 snprintf(path, MAX_PATH, "%s", item->name);
9030
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
129 break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
130
9030
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
131 case FIOS_TYPE_FILE:
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
132 case FIOS_TYPE_OLDFILE:
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
133 case FIOS_TYPE_SCENARIO:
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
134 case FIOS_TYPE_OLD_SCENARIO:
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
135 case FIOS_TYPE_PNG:
17520dee2ed1 (svn r12847) -Cleanup: Identing and variable scope
peter1138 <peter1138@openttd.org>
parents: 8378
diff changeset
136 case FIOS_TYPE_BMP:
11259
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
137 return item->name;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
138 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
139
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
140 return NULL;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
141 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
142
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
143 void FiosMakeSavegameName(char *buf, const char *name, size_t size)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
144 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
145 const char *extension, *period;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
146
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
147 extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
148
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
149 /* Don't append the extension if it is already there */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
150 period = strrchr(name, '.');
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
151 if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
7911
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
152 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
153 if (_fios_path != NULL) {
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
154 unsigned char sepchar = _fios_path[(strlen(_fios_path) - 1)];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
155
7911
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
156 if (sepchar != ':' && sepchar != '/') {
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
157 snprintf(buf, size, "%s" PATHSEP "%s%s", _fios_path, name, extension);
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
158 } else {
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
159 snprintf(buf, size, "%s%s%s", _fios_path, name, extension);
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
160 }
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
161 } else {
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
162 snprintf(buf, size, "%s%s", name, extension);
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
163 }
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
164 #else
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
165 snprintf(buf, size, "%s" PATHSEP "%s%s", _fios_path, name, extension);
7911
6a9e47d1d4dd (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents: 7408
diff changeset
166 #endif
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
167 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
168
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
169 bool FiosDelete(const char *name)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
170 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
171 char filename[512];
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
172
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
173 FiosMakeSavegameName(filename, name, lengthof(filename));
11356
99326e76187e (svn r15706) -Fix: Make sure the filename passed to unlink() is always in file system encoding.
michi_cc <michi_cc@openttd.org>
parents: 11314
diff changeset
174 return unlink(filename) == 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
175 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
176
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
177 bool FileExists(const char *filename)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
178 {
5757
d5cbd7ee4d7a (svn r8308) [WinCE] -Revert r5764: in case of WinCE, use the code for FileExists as it was in pre-r5764 for Win32 (tnx Darkvater)
truelight <truelight@openttd.org>
parents: 5609
diff changeset
179 #if defined(WINCE)
d5cbd7ee4d7a (svn r8308) [WinCE] -Revert r5764: in case of WinCE, use the code for FileExists as it was in pre-r5764 for Win32 (tnx Darkvater)
truelight <truelight@openttd.org>
parents: 5609
diff changeset
180 /* There is always one platform that doesn't support basic commands... */
6124
b688d35abaf4 (svn r8860) -Cleanup: some style changes, proper #endif comments, variable initialisation, WINCE ifdef and a vsprintf to vsnprintf change.
Darkvater <Darkvater@openttd.org>
parents: 6117
diff changeset
181 HANDLE hand = CreateFile(OTTD2FS(filename), 0, 0, NULL, OPEN_EXISTING, 0, NULL);
5757
d5cbd7ee4d7a (svn r8308) [WinCE] -Revert r5764: in case of WinCE, use the code for FileExists as it was in pre-r5764 for Win32 (tnx Darkvater)
truelight <truelight@openttd.org>
parents: 5609
diff changeset
182 if (hand == INVALID_HANDLE_VALUE) return 1;
d5cbd7ee4d7a (svn r8308) [WinCE] -Revert r5764: in case of WinCE, use the code for FileExists as it was in pre-r5764 for Win32 (tnx Darkvater)
truelight <truelight@openttd.org>
parents: 5609
diff changeset
183 CloseHandle(hand);
d5cbd7ee4d7a (svn r8308) [WinCE] -Revert r5764: in case of WinCE, use the code for FileExists as it was in pre-r5764 for Win32 (tnx Darkvater)
truelight <truelight@openttd.org>
parents: 5609
diff changeset
184 return 0;
d5cbd7ee4d7a (svn r8308) [WinCE] -Revert r5764: in case of WinCE, use the code for FileExists as it was in pre-r5764 for Win32 (tnx Darkvater)
truelight <truelight@openttd.org>
parents: 5609
diff changeset
185 #else
8378
7d35afa253ec (svn r11945) -Fix: FileExists() failed for non latin paths (win32)
glx <glx@openttd.org>
parents: 8264
diff changeset
186 return access(OTTD2FS(filename), 0) == 0;
5757
d5cbd7ee4d7a (svn r8308) [WinCE] -Revert r5764: in case of WinCE, use the code for FileExists as it was in pre-r5764 for Win32 (tnx Darkvater)
truelight <truelight@openttd.org>
parents: 5609
diff changeset
187 #endif
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
188 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
189
10809
0b7a4eee5b46 (svn r15144) -Codechange: make sure we don't run out of bounds while determining old savegame name
smatz <smatz@openttd.org>
parents: 10771
diff changeset
190 typedef FiosType fios_getlist_callback_proc(SaveLoadDialogMode mode, const char *filename, const char *ext, char *title, const char *last);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
191
11259
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
192 /**
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
193 * Scanner to scan for a particular type of FIOS file.
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
194 */
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
195 class FiosFileScanner : public FileScanner {
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
196 SaveLoadDialogMode mode; ///< The mode we want to search for
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
197 fios_getlist_callback_proc *callback_proc; ///< Callback to check whether the file may be added
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
198 public:
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
199 /**
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
200 * Create the scanner
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
201 * @param mode The mode we are in. Some modes don't allow 'parent'.
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
202 * @param callback_proc The function that is called where you need to do the filtering.
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
203 */
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
204 FiosFileScanner(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc) :
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
205 mode(mode),
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
206 callback_proc(callback_proc)
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
207 {}
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
208
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
209 /* virtual */ bool AddFile(const char *filename, size_t basepath_length);
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
210 };
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
211
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
212 /**
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
213 * Try to add a fios item set with the given filename.
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
214 * @param filename the full path to the file to read
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
215 * @param basepath_length amount of characters to chop of before to get a relative filename
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
216 * @return true if the file is added.
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
217 */
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
218 bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length)
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
219 {
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
220 const char *ext = strrchr(filename, '.');
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
221 if (ext == NULL) return false;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
222
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
223 char fios_title[64];
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
224 fios_title[0] = '\0'; // reset the title;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
225
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
226 FiosType type = this->callback_proc(this->mode, filename, ext, fios_title, lastof(fios_title));
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
227 if (type == FIOS_TYPE_INVALID) return false;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
228
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
229 for (const FiosItem *fios = _fios_items.Begin(); fios != _fios_items.End(); fios++) {
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
230 if (strcmp(fios->name, filename) == 0) return false;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
231 }
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
232
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
233 FiosItem *fios = _fios_items.Append();
11314
3aabfbcad7b2 (svn r15664) -Fix [FS#2716]: as always windows needs special handling when strings are involved
glx <glx@openttd.org>
parents: 11308
diff changeset
234 #ifdef WIN32
3aabfbcad7b2 (svn r15664) -Fix [FS#2716]: as always windows needs special handling when strings are involved
glx <glx@openttd.org>
parents: 11308
diff changeset
235 struct _stat sb;
3aabfbcad7b2 (svn r15664) -Fix [FS#2716]: as always windows needs special handling when strings are involved
glx <glx@openttd.org>
parents: 11308
diff changeset
236 if (_tstat(OTTD2FS(filename), &sb) == 0) {
3aabfbcad7b2 (svn r15664) -Fix [FS#2716]: as always windows needs special handling when strings are involved
glx <glx@openttd.org>
parents: 11308
diff changeset
237 #else
11259
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
238 struct stat sb;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
239 if (stat(filename, &sb) == 0) {
11314
3aabfbcad7b2 (svn r15664) -Fix [FS#2716]: as always windows needs special handling when strings are involved
glx <glx@openttd.org>
parents: 11308
diff changeset
240 #endif
11259
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
241 fios->mtime = sb.st_mtime;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
242 } else {
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
243 fios->mtime = 0;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
244 }
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
245
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
246 fios->type = type;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
247 strecpy(fios->name, filename, lastof(fios->name));
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
248
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
249 /* If the file doesn't have a title, use it's filename */
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
250 const char *t = fios_title;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
251 if (StrEmpty(fios_title)) {
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
252 t = strrchr(filename, PATHSEPCHAR);
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
253 t = (t == NULL) ? filename : (t + 1);
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
254 }
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
255 strecpy(fios->title, t, lastof(fios->title));
11277
15487697cc18 (svn r15626) -Fix [FS#2698]: UTF8 string handling could cause buffer overruns.
rubidium <rubidium@openttd.org>
parents: 11274
diff changeset
256 str_validate(fios->title, lastof(fios->title));
11259
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
257
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
258 return true;
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
259 }
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
260
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
261
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
262 /** Fill the list of the files in a directory, according to some arbitrary rule.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
263 * @param mode The mode we are in. Some modes don't allow 'parent'.
6481
85a1a79387a2 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas <belugas@openttd.org>
parents: 6251
diff changeset
264 * @param callback_proc The function that is called where you need to do the filtering.
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
265 * @param subdir The directory from where to start (global) searching.
11259
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
266 */
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
267 static void FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_proc *callback_proc, Subdirectory subdir)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
268 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
269 struct stat sb;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
270 struct dirent *dirent;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
271 DIR *dir;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
272 FiosItem *fios;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
273 int sort_start;
6054
2822a4a9c4f7 (svn r8784) -Regression (r7274 most likely): Unable to load TTDP (on *NIX) games because the static buffer used for name conversion is overwritten in the callback function.
Darkvater <Darkvater@openttd.org>
parents: 5757
diff changeset
274 char d_name[sizeof(fios->name)];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
275
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
276 _fios_items.Clear();
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
277
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
278 /* A parent directory link exists if we are not in the root directory */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
279 if (!FiosIsRoot(_fios_path) && mode != SLD_NEW_GAME) {
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
280 fios = _fios_items.Append();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
281 fios->type = FIOS_TYPE_PARENT;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
282 fios->mtime = 0;
10310
5cefbb1c3fd7 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13 <skidd13@openttd.org>
parents: 10301
diff changeset
283 strecpy(fios->name, "..", lastof(fios->name));
5cefbb1c3fd7 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13 <skidd13@openttd.org>
parents: 10301
diff changeset
284 strecpy(fios->title, ".. (Parent directory)", lastof(fios->title));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
285 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
286
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
287 /* Show subdirectories */
6086
d242fc975d65 (svn r8821) -Regression: Unable to browse directories on *nix if the filesystem is not in UTF-8 charset and special characters are used. The string passed to opendir() which is UTF-8 was not parsed back to the filesystem format. Use a wrapper called ttd_opendir() instead of redefining opendir itself.
Darkvater <Darkvater@openttd.org>
parents: 6054
diff changeset
288 if (mode != SLD_NEW_GAME && (dir = ttd_opendir(_fios_path)) != NULL) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
289 while ((dirent = readdir(dir)) != NULL) {
10310
5cefbb1c3fd7 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13 <skidd13@openttd.org>
parents: 10301
diff changeset
290 strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
291
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
292 /* found file must be directory, but not '.' or '..' */
10405
fa276b356907 (svn r14656) -Change: replace instances of x & S_IFREG with S_ISREG(x) as S_IFREG can be 0 on some platforms.
rubidium <rubidium@openttd.org>
parents: 10310
diff changeset
293 if (FiosIsValidFile(_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
6240
1d99c3541c11 (svn r9043) -Fix [FS#652]: the personal (.openttd) directories were hidden in the load/save directory listings (stepancheg).
rubidium <rubidium@openttd.org>
parents: 6179
diff changeset
294 (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
1d99c3541c11 (svn r9043) -Fix [FS#652]: the personal (.openttd) directories were hidden in the load/save directory listings (stepancheg).
rubidium <rubidium@openttd.org>
parents: 6179
diff changeset
295 strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
296 fios = _fios_items.Append();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
297 fios->type = FIOS_TYPE_DIR;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
298 fios->mtime = 0;
10310
5cefbb1c3fd7 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13 <skidd13@openttd.org>
parents: 10301
diff changeset
299 strecpy(fios->name, d_name, lastof(fios->name));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
300 snprintf(fios->title, lengthof(fios->title), "%s" PATHSEP " (Directory)", d_name);
11277
15487697cc18 (svn r15626) -Fix [FS#2698]: UTF8 string handling could cause buffer overruns.
rubidium <rubidium@openttd.org>
parents: 11274
diff changeset
301 str_validate(fios->title, lastof(fios->title));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
302 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
303 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
304 closedir(dir);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
305 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
306
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
307 /* Sort the subdirs always by name, ascending, remember user-sorting order */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
308 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
309 byte order = _savegame_sort_order;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
310 _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
13030
12fa346326b0 (svn r17528) -Codechange: use QSortT instead of qsort for sorting FiosItems
rubidium <rubidium@openttd.org>
parents: 12778
diff changeset
311 QSortT(_fios_items.Begin(), _fios_items.Length(), CompareFiosItems);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
312 _savegame_sort_order = order;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
313 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
314
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
315 /* This is where to start sorting for the filenames */
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
316 sort_start = _fios_items.Length();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
317
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
318 /* Show files */
11259
9b1e7de49fbf (svn r15608) -Codechange: use the file scanner to find the files for the saveload window.
rubidium <rubidium@openttd.org>
parents: 11256
diff changeset
319 FiosFileScanner scanner(mode, callback_proc);
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
320 if (subdir == NO_DIRECTORY) {
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
321 scanner.Scan(NULL, _fios_path, false);
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
322 } else {
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
323 scanner.Scan(NULL, subdir, true, true);
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
324 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
325
13030
12fa346326b0 (svn r17528) -Codechange: use QSortT instead of qsort for sorting FiosItems
rubidium <rubidium@openttd.org>
parents: 12778
diff changeset
326 QSortT(_fios_items.Get(sort_start), _fios_items.Length() - sort_start, CompareFiosItems);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
327
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
328 /* Show drives */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
329 if (mode != SLD_NEW_GAME) FiosGetDrives();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
330
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
331 _fios_items.Compact();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
332 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
333
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
334 /**
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
335 * Get the title of a file, which (if exists) is stored in a file named
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
336 * the same as the data file but with '.title' added to it.
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
337 * @param file filename to get the title for
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
338 * @param title the title buffer to fill
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
339 * @param last the last element in the title buffer
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
340 */
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
341 static void GetFileTitle(const char *file, char *title, const char *last)
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
342 {
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
343 char buf[MAX_PATH];
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
344 strecpy(buf, file, lastof(buf));
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
345 strecat(buf, ".title", lastof(buf));
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
346
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
347 FILE *f = FioFOpenFile(buf, "r");
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
348 if (f == NULL) return;
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
349
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
350 size_t read = fread(title, 1, last - title, f);
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
351 assert(title + read <= last);
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
352 title[read] = '\0';
11277
15487697cc18 (svn r15626) -Fix [FS#2698]: UTF8 string handling could cause buffer overruns.
rubidium <rubidium@openttd.org>
parents: 11274
diff changeset
353 str_validate(title, last);
11274
e9f928943b3f (svn r15623) -Fix (r15615): close the files you've opened
rubidium <rubidium@openttd.org>
parents: 11266
diff changeset
354 FioFCloseFile(f);
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
355 }
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
356
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
357 /**
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
358 * Callback for FiosGetFileList. It tells if a file is a savegame or not.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
359 * @param mode Save/load mode.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
360 * @param file Name of the file to check.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
361 * @param ext A pointer to the extension identifier inside file
9730
6d3372e45a23 (svn r13859) -Fix: loading of TTD(Patch) savegames from the command line didn't work
smatz <smatz@openttd.org>
parents: 9441
diff changeset
362 * @param title Buffer if a callback wants to lookup the title of the file; NULL to skip the lookup
10809
0b7a4eee5b46 (svn r15144) -Codechange: make sure we don't run out of bounds while determining old savegame name
smatz <smatz@openttd.org>
parents: 10771
diff changeset
363 * @param last Last available byte in buffer (to prevent buffer overflows); not used when title == NULL
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
364 * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
365 * @see FiosGetFileList
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
366 * @see FiosGetSavegameList
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
367 */
10809
0b7a4eee5b46 (svn r15144) -Codechange: make sure we don't run out of bounds while determining old savegame name
smatz <smatz@openttd.org>
parents: 10771
diff changeset
368 FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
369 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
370 /* Show savegame files
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
371 * .SAV OpenTTD saved game
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
372 * .SS1 Transport Tycoon Deluxe preset game
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
373 * .SV1 Transport Tycoon Deluxe (Patch) saved game
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
374 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
375 if (strcasecmp(ext, ".sav") == 0) {
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
376 GetFileTitle(file, title, last);
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
377 return FIOS_TYPE_FILE;
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
378 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
379
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
380 if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
381 if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
382 strcasecmp(ext, ".sv2") == 0) {
11261
5a50ba45cf24 (svn r15610) -Change: allow loading of heightmaps and savegames (any form) that are in .tar files
rubidium <rubidium@openttd.org>
parents: 11259
diff changeset
383 if (title != NULL) GetOldSaveGameName(file, title, last);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
384 return FIOS_TYPE_OLDFILE;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
385 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
386 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
387
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
388 return FIOS_TYPE_INVALID;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
389 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
390
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
391 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
392 * Get a list of savegames.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
393 * @param mode Save/load mode.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
394 * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
395 * @see FiosGetFileList
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
396 */
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
397 void FiosGetSavegameList(SaveLoadDialogMode mode)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
398 {
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
399 static char *fios_save_path = NULL;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
400
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
401 if (fios_save_path == NULL) {
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
402 fios_save_path = MallocT<char>(MAX_PATH);
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
403 FioGetDirectory(fios_save_path, MAX_PATH, SAVE_DIR);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
404 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
405
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
406 _fios_path = fios_save_path;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
407
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
408 FiosGetFileList(mode, &FiosGetSavegameListCallback, NO_DIRECTORY);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
409 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
410
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
411 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
412 * Callback for FiosGetFileList. It tells if a file is a scenario or not.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
413 * @param mode Save/load mode.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
414 * @param file Name of the file to check.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
415 * @param ext A pointer to the extension identifier inside file
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
416 * @param title Buffer if a callback wants to lookup the title of the file
10809
0b7a4eee5b46 (svn r15144) -Codechange: make sure we don't run out of bounds while determining old savegame name
smatz <smatz@openttd.org>
parents: 10771
diff changeset
417 * @param last Last available byte in buffer (to prevent buffer overflows)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
418 * @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
419 * @see FiosGetFileList
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
420 * @see FiosGetScenarioList
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
421 */
10809
0b7a4eee5b46 (svn r15144) -Codechange: make sure we don't run out of bounds while determining old savegame name
smatz <smatz@openttd.org>
parents: 10771
diff changeset
422 static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
423 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
424 /* Show scenario files
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
425 * .SCN OpenTTD style scenario file
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
426 * .SV0 Transport Tycoon Deluxe (Patch) scenario
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
427 * .SS0 Transport Tycoon Deluxe preset scenario */
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
428 if (strcasecmp(ext, ".scn") == 0) {
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
429 GetFileTitle(file, title, last);
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
430 return FIOS_TYPE_SCENARIO;
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
431 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
432
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
433 if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
434 if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
11261
5a50ba45cf24 (svn r15610) -Change: allow loading of heightmaps and savegames (any form) that are in .tar files
rubidium <rubidium@openttd.org>
parents: 11259
diff changeset
435 GetOldSaveGameName(file, title, last);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
436 return FIOS_TYPE_OLD_SCENARIO;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
437 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
438 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
439
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
440 return FIOS_TYPE_INVALID;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
441 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
442
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
443 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
444 * Get a list of scenarios.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
445 * @param mode Save/load mode.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
446 * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
447 * @see FiosGetFileList
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
448 */
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
449 void FiosGetScenarioList(SaveLoadDialogMode mode)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
450 {
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
451 static char *fios_scn_path = NULL;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
452
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6763
diff changeset
453 /* Copy the default path on first run or on 'New Game' */
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
454 if (fios_scn_path == NULL) {
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
455 fios_scn_path = MallocT<char>(MAX_PATH);
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
456 FioGetDirectory(fios_scn_path, MAX_PATH, SCENARIO_DIR);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
457 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
458
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
459 _fios_path = fios_scn_path;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
460
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
461 char base_path[MAX_PATH];
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
462 FioGetDirectory(base_path, sizeof(base_path), SCENARIO_DIR);
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
463
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
464 FiosGetFileList(mode, &FiosGetScenarioListCallback, (mode == SLD_LOAD_SCENARIO && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
465 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
466
10809
0b7a4eee5b46 (svn r15144) -Codechange: make sure we don't run out of bounds while determining old savegame name
smatz <smatz@openttd.org>
parents: 10771
diff changeset
467 static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title, const char *last)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
468 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
469 /* Show heightmap files
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
470 * .PNG PNG Based heightmap files
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
471 * .BMP BMP Based heightmap files
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
472 */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
473
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
474 FiosType type = FIOS_TYPE_INVALID;
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
475
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
476 #ifdef WITH_PNG
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
477 if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
478 #endif /* WITH_PNG */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
479
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
480 if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
481
11643
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
482 if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
483
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
484 TarFileList::iterator it = _tar_filelist.find(file);
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
485 if (it != _tar_filelist.end()) {
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
486 /* If the file is in a tar and that tar is not in a heightmap
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
487 * directory we are for sure not supposed to see it.
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
488 * Examples of this are pngs part of documentation within
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
489 * collections of NewGRFs or 32 bpp graphics replacement PNGs.
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
490 */
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
491 bool match = false;
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
492 Searchpath sp;
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
493 FOR_ALL_SEARCHPATHS(sp) {
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
494 char buf[MAX_PATH];
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
495 FioAppendDirectory(buf, sizeof(buf), sp, HEIGHTMAP_DIR);
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
496
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
497 if (strncmp(buf, it->second.tar_filename, strlen(buf)) == 0) {
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
498 match = true;
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
499 break;
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
500 }
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
501 }
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
502
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
503 if (!match) return FIOS_TYPE_INVALID;
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
504 }
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
505
c5b2433f26e5 (svn r16023) -Fix (r15615) [FS#2817]: 32 bpp sprites in tars would also be shown in the list of heightmaps.
rubidium <rubidium@openttd.org>
parents: 11360
diff changeset
506 GetFileTitle(file, title, last);
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
507
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
508 return type;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
509 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
510
6179
e3e61b92574b (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 6124
diff changeset
511 /* Get a list of Heightmaps */
9441
e899ed594e7c (svn r13359) -Codechange: convert _fios_items to a SmallVector
skidd13 <skidd13@openttd.org>
parents: 9139
diff changeset
512 void FiosGetHeightmapList(SaveLoadDialogMode mode)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
513 {
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
514 static char *fios_hmap_path = NULL;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
515
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
516 if (fios_hmap_path == NULL) {
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
517 fios_hmap_path = MallocT<char>(MAX_PATH);
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
518 FioGetDirectory(fios_hmap_path, MAX_PATH, HEIGHTMAP_DIR);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
519 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
520
9031
9c6b04429f26 (svn r12848) -Cleanup/Codechange: Use correct variable types, don't prefix with _ for non-globals, and use implicit enum numbering.
peter1138 <peter1138@openttd.org>
parents: 9030
diff changeset
521 _fios_path = fios_hmap_path;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
522
11266
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
523 char base_path[MAX_PATH];
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
524 FioGetDirectory(base_path, sizeof(base_path), HEIGHTMAP_DIR);
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
525
47b87f7aa945 (svn r15615) -Feature: show scenarios/heightmaps from both your home directory and installation directory.
rubidium <rubidium@openttd.org>
parents: 11262
diff changeset
526 FiosGetFileList(mode, &FiosGetHeightmapListCallback, strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
527 }
11283
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
528
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
529 #if defined(ENABLE_NETWORK)
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
530 #include "network/network_content.h"
12777
5732e685a232 (svn r17247) -Change: move '3rd party' MD5 implementation also to the 3rdparty directory.
rubidium <rubidium@openttd.org>
parents: 12762
diff changeset
531 #include "3rdparty/md5/md5.h"
11283
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
532
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
533 /** Basic data to distinguish a scenario. Used in the server list window */
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
534 struct ScenarioIdentifier {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
535 uint32 scenid; ///< ID for the scenario (generated by content)
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
536 uint8 md5sum[16]; ///< MD5 checksum of file
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
537
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
538 bool operator == (const ScenarioIdentifier &other) const
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
539 {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
540 return this->scenid == other.scenid &&
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
541 memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
542 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
543
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
544 bool operator != (const ScenarioIdentifier &other) const
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
545 {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
546 return !(*this == other);
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
547 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
548 };
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
549
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
550 /**
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
551 * Scanner to find the unique IDs of scenarios
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
552 */
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
553 class ScenarioScanner : protected FileScanner, public SmallVector<ScenarioIdentifier, 8> {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
554 bool scanned; ///< Whether we've already scanned
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
555 public:
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
556 /** Initialise */
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
557 ScenarioScanner() : scanned(false) {}
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
558
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
559 /**
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
560 * Scan, but only if it's needed.
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
561 * @param rescan whether to force scanning even when it's not necessary
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
562 */
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
563 void Scan(bool rescan)
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
564 {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
565 if (this->scanned && !rescan) return;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
566
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
567 this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
568 this->scanned = true;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
569 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
570
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
571 /* virtual */ bool AddFile(const char *filename, size_t basepath_length)
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
572 {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
573 FILE *f = FioFOpenFile(filename, "r");
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
574 if (f == NULL) return false;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
575
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
576 ScenarioIdentifier id;
11285
0bb67e3cb046 (svn r15634) -Fix (r15632): some compilers seem to warn about the return value of fscanf.
rubidium <rubidium@openttd.org>
parents: 11284
diff changeset
577 int fret = fscanf(f, "%i", &id.scenid);
11283
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
578 FioFCloseFile(f);
11285
0bb67e3cb046 (svn r15634) -Fix (r15632): some compilers seem to warn about the return value of fscanf.
rubidium <rubidium@openttd.org>
parents: 11284
diff changeset
579 if (fret != 1) return false;
11283
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
580
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
581 Md5 checksum;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
582 uint8 buffer[1024];
12328
75053deacb14 (svn r16748) -Codechange: Don't cast away constness.
alberth <alberth@openttd.org>
parents: 11730
diff changeset
583 char basename[MAX_PATH]; ///< \a filename without the extension.
11283
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
584 size_t len, size;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
585
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
586 /* open the scenario file, but first get the name.
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
587 * This is safe as we check on extension which
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
588 * must always exist. */
12328
75053deacb14 (svn r16748) -Codechange: Don't cast away constness.
alberth <alberth@openttd.org>
parents: 11730
diff changeset
589 strecpy(basename, filename, lastof(basename));
75053deacb14 (svn r16748) -Codechange: Don't cast away constness.
alberth <alberth@openttd.org>
parents: 11730
diff changeset
590 *strrchr(basename, '.') = '\0';
75053deacb14 (svn r16748) -Codechange: Don't cast away constness.
alberth <alberth@openttd.org>
parents: 11730
diff changeset
591 f = FioFOpenFile(basename, "rb", SCENARIO_DIR, &size);
11283
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
592 if (f == NULL) return false;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
593
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
594 /* calculate md5sum */
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
595 while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
596 size -= len;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
597 checksum.Append(buffer, len);
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
598 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
599 checksum.Finish(id.md5sum);
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
600
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
601 FioFCloseFile(f);
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
602
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
603 this->Include(id);
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
604 return true;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
605 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
606 };
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
607
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
608 /** Scanner for scenarios */
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
609 static ScenarioScanner _scanner;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
610
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
611 /**
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
612 * Check whether we've got a given scenario based on it's unique ID.
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
613 * @param ci the content info to compare it to
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
614 * @param md5sum whether to look at the md5sum or the id
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
615 * @return true if we've got the scenario
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
616 */
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
617 bool HasScenario(const ContentInfo *ci, bool md5sum)
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
618 {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
619 _scanner.Scan(false);
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
620
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
621 for (ScenarioIdentifier *id = _scanner.Begin(); id != _scanner.End(); id++) {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
622 if (md5sum ?
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
623 (memcmp(id->md5sum, ci->md5sum, sizeof(id->md5sum)) == 0) :
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
624 (id->scenid == ci->unique_id)) {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
625 return true;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
626 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
627 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
628
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
629 return false;
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
630 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
631
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
632 /**
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
633 * Force a (re)scan of the scenarios.
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
634 */
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
635 void ScanScenarios()
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
636 {
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
637 _scanner.Scan(true);
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
638 }
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
639
65d6451a3be8 (svn r15632) -Feature: allow downloading scenarios and heightmaps via bananas.
rubidium <rubidium@openttd.org>
parents: 11277
diff changeset
640 #endif /* ENABLE_NETWORK */