Let HandleWindowDragging return a boolean status
[openttd/fttd.git] / src / fios.h
blob7685463633af3d0bf42779cf5d0023690525bb8d
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file fios.h Declarations for savegames operations */
12 #ifndef FIOS_H
13 #define FIOS_H
15 #include "gfx_type.h"
16 #include "company_base.h"
17 #include "gamelog.h"
18 #include "newgrf_config.h"
19 #include "network/core/tcp_content.h"
20 #include "saveload/saveload_data.h"
21 #include "saveload/saveload_error.h"
24 /** The different abstract types of files that the system knows about. */
25 enum AbstractFileType {
26 FT_NONE, ///< nothing to do
27 FT_SAVEGAME, ///< old or new savegame
28 FT_SCENARIO, ///< old or new scenario
29 FT_HEIGHTMAP, ///< heightmap file
31 FT_INVALID = 7, ///< Invalid or unknown file type.
32 FT_NUMBITS = 3, ///< Number of bits required for storing a #AbstractFileType value.
33 FT_MASK = (1 << FT_NUMBITS) - 1, ///< Bitmask for extracting an abstract file type.
36 /** Kinds of files in each #AbstractFileType. */
37 enum DetailedFileType {
38 /* Save game and scenario files. */
39 DFT_OLD_GAME_FILE, ///< Old save game or scenario file.
40 DFT_GAME_FILE, ///< Save game or scenario file.
42 /* Heightmap files. */
43 DFT_HEIGHTMAP_BMP, ///< BMP file.
44 DFT_HEIGHTMAP_PNG, ///< PNG file.
46 /* fios 'files' */
47 DFT_FIOS_DRIVE, ///< A drive (letter) entry.
48 DFT_FIOS_PARENT, ///< A parent directory entry.
49 DFT_FIOS_DIR, ///< A directory entry.
51 DFT_INVALID = 255, ///< Unknown or invalid file.
54 /**
55 * Construct an enum value for #FiosType as a combination of an abstract and a detailed file type.
56 * @param abstract Abstract file type (one of #AbstractFileType).
57 * @param detailed Detailed file type (one of #DetailedFileType).
59 #define MAKE_FIOS_TYPE(abstract, detailed) ((abstract) | ((detailed) << FT_NUMBITS))
61 /**
62 * Elements of a file system that are recognized.
63 * Values are a combination of #AbstractFileType and #DetailedFileType.
64 * @see GetAbstractFileType GetDetailedFileType
66 enum FiosType {
67 FIOS_TYPE_DRIVE = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_DRIVE),
68 FIOS_TYPE_PARENT = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_PARENT),
69 FIOS_TYPE_DIR = MAKE_FIOS_TYPE(FT_NONE, DFT_FIOS_DIR),
71 FIOS_TYPE_FILE = MAKE_FIOS_TYPE(FT_SAVEGAME, DFT_GAME_FILE),
72 FIOS_TYPE_OLDFILE = MAKE_FIOS_TYPE(FT_SAVEGAME, DFT_OLD_GAME_FILE),
73 FIOS_TYPE_SCENARIO = MAKE_FIOS_TYPE(FT_SCENARIO, DFT_GAME_FILE),
74 FIOS_TYPE_OLD_SCENARIO = MAKE_FIOS_TYPE(FT_SCENARIO, DFT_OLD_GAME_FILE),
75 FIOS_TYPE_PNG = MAKE_FIOS_TYPE(FT_HEIGHTMAP, DFT_HEIGHTMAP_PNG),
76 FIOS_TYPE_BMP = MAKE_FIOS_TYPE(FT_HEIGHTMAP, DFT_HEIGHTMAP_BMP),
78 FIOS_TYPE_INVALID = MAKE_FIOS_TYPE(FT_INVALID, DFT_INVALID),
81 #undef MAKE_FIOS_TYPE
83 /**
84 * Extract the abstract file type from a #FiosType.
85 * @param fios_type Type to query.
86 * @return The Abstract file type of the \a fios_type.
88 inline AbstractFileType GetAbstractFileType(FiosType fios_type)
90 return static_cast<AbstractFileType>(fios_type & FT_MASK);
93 /**
94 * Extract the detailed file type from a #FiosType.
95 * @param fios_type Type to query.
96 * @return The Detailed file type of the \a fios_type.
98 inline DetailedFileType GetDetailedFileType(FiosType fios_type)
100 return static_cast<DetailedFileType>(fios_type >> FT_NUMBITS);
104 typedef SmallMap<uint, CompanyProperties *> CompanyPropertiesMap;
107 * Container for loading in mode SL_LOAD_CHECK.
109 struct LoadCheckData {
110 bool checkable; ///< True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.)
111 SlErrorData error; ///< Error message from loading. INVALID_STRING_ID if no error.
113 SavegameTypeVersion sl_version; ///< Savegame type and version
115 uint32 map_size_x, map_size_y;
116 Date current_date;
118 GameSettings settings;
120 CompanyPropertiesMap companies; ///< Company information.
122 GRFConfig *grfconfig; ///< NewGrf configuration from save.
123 GRFListCompatibility grf_compatibility; ///< Summary state of NewGrfs, whether missing files or only compatible found.
125 Gamelog gamelog; ///< Gamelog
127 LoadCheckData() : grfconfig(NULL), grf_compatibility(GLC_NOT_FOUND), gamelog()
129 this->Clear();
133 * Don't leak memory at program exit
135 ~LoadCheckData()
137 this->Clear();
141 * Check whether loading the game resulted in errors.
142 * @return true if errors were encountered.
144 bool HasErrors()
146 return this->checkable && this->error.str != INVALID_STRING_ID;
150 * Check whether the game uses any NewGrfs.
151 * @return true if NewGrfs are used.
153 bool HasNewGrfs()
155 return this->checkable && this->error.str == INVALID_STRING_ID && this->grfconfig != NULL;
158 void Clear();
161 extern LoadCheckData _load_check_data;
164 enum FileSlots {
166 * Slot used for the GRF scanning and such.
167 * This slot is used for all temporary accesses to files when scanning/testing files,
168 * and thus cannot be used for files, which are continuously accessed during a game.
170 CONFIG_SLOT = 0,
171 /** Slot for the sound. */
172 SOUND_SLOT = 1,
173 /** First slot usable for (New)GRFs used during the game. */
174 FIRST_GRF_SLOT = 2,
175 /** Maximum number of slots. */
176 MAX_FILE_SLOTS = 128,
179 /** Deals with finding savegames */
180 struct FiosItem {
181 FiosType type;
182 uint64 mtime;
183 char title[64];
184 char name[MAX_PATH];
187 /** List of file information. */
188 class FileList {
189 public:
190 struct Path {
191 char home [MAX_PATH]; ///< The home path.
192 char cur [MAX_PATH]; ///< The current path.
194 void reset (void)
196 memcpy (this->cur, this->home, MAX_PATH);
200 SmallVector<FiosItem, 32> files; ///< The list of files.
201 Path *path; ///< The current and home paths.
204 * Construct a new entry in the file list.
205 * @return Pointer to the new items to be initialized.
207 inline FiosItem *Append()
209 return this->files.Append();
213 * Get the number of files in the list.
214 * @return The number of files stored in the list.
216 inline uint Length() const
218 return this->files.Length();
222 * Get a pointer to the first file information.
223 * @return Address of the first file information.
225 inline const FiosItem *Begin() const
227 return this->files.Begin();
231 * Get a pointer behind the last file information.
232 * @return Address behind the last file information.
234 inline const FiosItem *End() const
236 return this->files.End();
240 * Get a pointer to the indicated file information. File information must exist.
241 * @return Address of the indicated existing file information.
243 inline const FiosItem *Get(uint index) const
245 return this->files.Get(index);
249 * Get a pointer to the indicated file information. File information must exist.
250 * @return Address of the indicated existing file information.
252 inline FiosItem *Get(uint index)
254 return this->files.Get(index);
257 inline const FiosItem &operator[](uint index) const
259 return this->files[index];
263 * Get a reference to the indicated file information. File information must exist.
264 * @return The requested file information.
266 inline FiosItem &operator[](uint index)
268 return this->files[index];
271 /** Remove all items from the list. */
272 inline void Clear()
274 this->files.Clear();
277 void BuildFileList (AbstractFileType abstract_filetype, bool save);
280 enum SortingBits {
281 SORT_ASCENDING = 0,
282 SORT_DESCENDING = 1,
283 SORT_BY_DATE = 0,
284 SORT_BY_NAME = 2
286 DECLARE_ENUM_AS_BIT_SET(SortingBits)
288 /* Variables to display file lists */
289 extern SortingBits _savegame_sort_order;
291 void ShowSaveLoadDialog (AbstractFileType abstract_filetype, bool save = false);
293 const char *FiosBrowseTo (char *path, const FiosItem *item);
295 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
296 bool FiosGetDiskFreeSpace (const char *path, uint64 *tot);
298 bool FiosDelete (const char *path, const char *name);
299 void FiosMakeHeightmapName (char *buf, const char *path, const char *name, size_t size);
300 void FiosMakeSavegameName (char *buf, const char *path, const char *name, size_t size);
302 FiosType FiosGetSavegameListCallback (const char *file, const char *ext, stringb *title = NULL, bool save = false);
304 int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b);
306 #endif /* FIOS_H */