Rework the trip history window layout.
[openttd-joker.git] / src / saveload / saveload.h
blob9463ad3be89d77ddc102324a14236f0b97a443e4
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 saveload.h Functions/types related to saving and loading games. */
12 #ifndef SAVELOAD_H
13 #define SAVELOAD_H
15 #include "../fileio_type.h"
16 #include "../strings_type.h"
18 /** Save or load result codes. */
19 enum SaveOrLoadResult {
20 SL_OK = 0, ///< completed successfully
21 SL_ERROR = 1, ///< error that was caught before internal structures were modified
22 SL_REINIT = 2, ///< error that was caught in the middle of updating game state, need to clear it. (can only happen during load)
25 /** Deals with the type of the savegame, independent of extension */
26 struct FileToSaveLoad {
27 SaveLoadOperation file_op; ///< File operation to perform.
28 DetailedFileType detail_ftype; ///< Concrete file type (PNG, BMP, old save, etc).
29 AbstractFileType abstract_ftype; ///< Abstract type of file (scenario, heightmap, etc).
30 char name[MAX_PATH]; ///< Name of the file.
31 char title[255]; ///< Internal name of the game.
33 void SetMode(FiosType ft);
34 void SetMode(SaveLoadOperation fop, AbstractFileType aft, DetailedFileType dft);
35 void SetName(const char *name);
36 void SetTitle(const char *title);
39 /** Types of save games. */
40 enum SavegameType {
41 SGT_TTD, ///< TTD savegame (can be detected incorrectly)
42 SGT_TTDP1, ///< TTDP savegame ( -//- ) (data at NW border)
43 SGT_TTDP2, ///< TTDP savegame in new format (data at SE border)
44 SGT_OTTD, ///< OTTD savegame
45 SGT_TTO, ///< TTO savegame
46 SGT_INVALID = 0xFF, ///< broken savegame (used internally)
49 extern FileToSaveLoad _file_to_saveload;
51 void GenerateDefaultSaveName(char *buf, const char *last);
52 void SetSaveLoadError(StringID str);
53 const char *GetSaveLoadErrorString();
54 SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true);
55 void WaitTillSaved();
56 void ProcessAsyncSaveFinish();
57 void DoExitSave();
59 SaveOrLoadResult SaveWithFilter(struct SaveFilter *writer, bool threaded);
60 SaveOrLoadResult LoadWithFilter(struct LoadFilter *reader);
62 typedef void ChunkSaveLoadProc();
63 typedef void AutolengthProc(void *arg);
65 /** Handlers and description of chunk. */
66 struct ChunkHandler {
67 uint32 id; ///< Unique ID (4 letters).
68 ChunkSaveLoadProc *save_proc; ///< Save procedure of the chunk.
69 ChunkSaveLoadProc *load_proc; ///< Load procedure of the chunk.
70 ChunkSaveLoadProc *ptrs_proc; ///< Manipulate pointers in the chunk.
71 ChunkSaveLoadProc *load_check_proc; ///< Load procedure for game preview.
72 uint32 flags; ///< Flags of the chunk. @see ChunkType
75 struct NullStruct {
76 byte null;
79 /** Type of reference (#SLE_REF, #SLE_CONDREF). */
80 enum SLRefType {
81 REF_ORDER = 0, ///< Load/save a reference to an order.
82 REF_VEHICLE = 1, ///< Load/save a reference to a vehicle.
83 REF_STATION = 2, ///< Load/save a reference to a station.
84 REF_TOWN = 3, ///< Load/save a reference to a town.
85 REF_VEHICLE_OLD = 4, ///< Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
86 REF_ROADSTOPS = 5, ///< Load/save a reference to a bus/truck stop.
87 REF_ENGINE_RENEWS = 6, ///< Load/save a reference to an engine renewal (autoreplace).
88 REF_CARGO_PACKET = 7, ///< Load/save a reference to a cargo packet.
89 REF_ORDERLIST = 8, ///< Load/save a reference to an orderlist.
90 REF_STORAGE = 9, ///< Load/save a reference to a persistent storage.
91 REF_LINK_GRAPH = 10, ///< Load/save a reference to a link graph.
92 REF_LINK_GRAPH_JOB = 11, ///< Load/save a reference to a link graph job.
93 REF_TEMPLATE_VEHICLE = 12, ///< Load/save a reference to a template vehicle
94 REF_DOCKS = 13, ///< Load/save a reference to a dock.
97 /** Highest possible savegame version. */
98 #define SL_MAX_VERSION UINT16_MAX
99 #define SL_PATCH_PACK 255
100 #define SL_PATCH_PACK_DAYLENGTH 256
101 #define SL_PATCH_PACK_TOWN_BUILDINGS 257
102 #define SL_PATCH_PACK_MAP_FEATURES 258
103 #define SL_PATCH_PACK_SAFE_WAITING_LOCATION 259
104 #define SL_PATCH_PACK_1_2 260
105 #define SL_PATCH_PACK_1_3 261
106 #define SL_PATCH_PACK_1_4 262
107 #define SL_PATCH_PACK_1_5 263
108 #define SL_PATCH_PACK_1_6 264
109 #define SL_PATCH_PACK_1_7 265
110 #define SL_PATCH_PACK_1_8 266
111 #define SL_PATCH_PACK_1_9 267
112 #define SL_PATCH_PACK_1_10 268
113 #define SL_PATCH_PACK_1_11 269
114 #define SL_PATCH_PACK_1_12 270
115 #define SL_PATCH_PACK_1_14 271
116 #define SL_PATCH_PACK_1_15 272
117 #define SL_PATCH_PACK_1_16 273
118 #define SL_PATCH_PACK_1_18 274
119 #define SL_PATCH_PACK_1_18_3 275
120 #define SL_PATCH_PACK_1_18_4 276
121 #define SL_PATCH_PACK_1_18_6 277
122 #define SL_PATCH_PACK_1_19 278
123 #define SL_PATCH_PACK_1_20 279
124 #define SL_PATCH_PACK_1_21 280
125 #define SL_PATCH_PACK_1_22 281
126 #define SL_PATCH_PACK_1_23 282
127 #define SL_PATCH_PACK_1_24 283
129 /** Flags of a chunk. */
130 enum ChunkType {
131 CH_RIFF = 0,
132 CH_ARRAY = 1,
133 CH_SPARSE_ARRAY = 2,
134 CH_TYPE_MASK = 3,
135 CH_LAST = 8, ///< Last chunk in this array.
139 * VarTypes is the general bitmasked magic type that tells us
140 * certain characteristics about the variable it refers to. For example
141 * SLE_FILE_* gives the size(type) as it would be in the savegame and
142 * SLE_VAR_* the size(type) as it is in memory during runtime. These are
143 * the first 8 bits (0-3 SLE_FILE, 4-7 SLE_VAR).
144 * Bits 8-15 are reserved for various flags as explained below
146 enum VarTypes {
147 /* 4 bits allocated a maximum of 16 types for NumberType */
148 SLE_FILE_I8 = 0,
149 SLE_FILE_U8 = 1,
150 SLE_FILE_I16 = 2,
151 SLE_FILE_U16 = 3,
152 SLE_FILE_I32 = 4,
153 SLE_FILE_U32 = 5,
154 SLE_FILE_I64 = 6,
155 SLE_FILE_U64 = 7,
156 SLE_FILE_STRINGID = 8, ///< StringID offset into strings-array
157 SLE_FILE_STRING = 9,
158 /* 6 more possible file-primitives */
160 /* 4 bits allocated a maximum of 16 types for NumberType */
161 SLE_VAR_BL = 0 << 4,
162 SLE_VAR_I8 = 1 << 4,
163 SLE_VAR_U8 = 2 << 4,
164 SLE_VAR_I16 = 3 << 4,
165 SLE_VAR_U16 = 4 << 4,
166 SLE_VAR_I32 = 5 << 4,
167 SLE_VAR_U32 = 6 << 4,
168 SLE_VAR_I64 = 7 << 4,
169 SLE_VAR_U64 = 8 << 4,
170 SLE_VAR_NULL = 9 << 4, ///< useful to write zeros in savegame.
171 SLE_VAR_STRB = 10 << 4, ///< string (with pre-allocated buffer)
172 SLE_VAR_STRBQ = 11 << 4, ///< string enclosed in quotes (with pre-allocated buffer)
173 SLE_VAR_STR = 12 << 4, ///< string pointer
174 SLE_VAR_STRQ = 13 << 4, ///< string pointer enclosed in quotes
175 SLE_VAR_NAME = 14 << 4, ///< old custom name to be converted to a char pointer
176 /* 1 more possible memory-primitives */
178 /* Shortcut values */
179 SLE_VAR_CHAR = SLE_VAR_I8,
181 /* Default combinations of variables. As savegames change, so can variables
182 * and thus it is possible that the saved value and internal size do not
183 * match and you need to specify custom combo. The defaults are listed here */
184 SLE_BOOL = SLE_FILE_I8 | SLE_VAR_BL,
185 SLE_INT8 = SLE_FILE_I8 | SLE_VAR_I8,
186 SLE_UINT8 = SLE_FILE_U8 | SLE_VAR_U8,
187 SLE_INT16 = SLE_FILE_I16 | SLE_VAR_I16,
188 SLE_UINT16 = SLE_FILE_U16 | SLE_VAR_U16,
189 SLE_INT32 = SLE_FILE_I32 | SLE_VAR_I32,
190 SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32,
191 SLE_INT64 = SLE_FILE_I64 | SLE_VAR_I64,
192 SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64,
193 SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR,
194 SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U32,
195 SLE_STRINGBUF = SLE_FILE_STRING | SLE_VAR_STRB,
196 SLE_STRINGBQUOTE = SLE_FILE_STRING | SLE_VAR_STRBQ,
197 SLE_STRING = SLE_FILE_STRING | SLE_VAR_STR,
198 SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ,
199 SLE_NAME = SLE_FILE_STRINGID | SLE_VAR_NAME,
201 /* Shortcut values */
202 SLE_UINT = SLE_UINT32,
203 SLE_INT = SLE_INT32,
204 SLE_STRB = SLE_STRINGBUF,
205 SLE_STRBQ = SLE_STRINGBQUOTE,
206 SLE_STR = SLE_STRING,
207 SLE_STRQ = SLE_STRINGQUOTE,
209 /* 8 bits allocated for a maximum of 8 flags
210 * Flags directing saving/loading of a variable */
211 SLF_NOT_IN_SAVE = 1 << 8, ///< do not save with savegame, basically client-based
212 SLF_NOT_IN_CONFIG = 1 << 9, ///< do not save to config file
213 SLF_NO_NETWORK_SYNC = 1 << 10, ///< do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
214 SLF_ALLOW_CONTROL = 1 << 11, ///< allow control codes in the strings
215 SLF_ALLOW_NEWLINE = 1 << 12, ///< allow new lines in the strings
216 /* 3 more possible flags */
219 typedef uint32 VarType;
221 /** Type of data saved. */
222 enum SaveLoadTypes {
223 SL_VAR = 0, ///< Save/load a variable.
224 SL_REF = 1, ///< Save/load a reference.
225 SL_ARR = 2, ///< Save/load an array.
226 SL_STR = 3, ///< Save/load a string.
227 SL_LST = 4, ///< Save/load a list.
228 SL_DEQ = 5, ///< Save/load a deque.
229 SL_VEC = 6, ///< Save/load a vector.
230 SL_STDSTR = 7, ///< Save/load a std::string.
231 /* non-normal save-load types */
232 SL_WRITEBYTE = 8,
233 SL_VEH_INCLUDE = 9,
234 SL_ST_INCLUDE = 10,
235 /* primitive type vector */
236 SL_VARVEC = 14,
237 SL_END = 15
240 typedef byte SaveLoadType; ///< Save/load type. @see SaveLoadTypes
242 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
243 struct SaveLoad {
244 bool global; ///< should we load a global variable or a non-global one
245 SaveLoadType cmd; ///< the action to take with the saved/loaded type, All types need different action
246 VarType conv; ///< type of the variable to be saved, int
247 uint16 length; ///< (conditional) length of the variable (eg. arrays) (max array size is 65536 elements)
248 uint16 version_from; ///< save/load the variable starting from this savegame version
249 uint16 version_to; ///< save/load the variable until this savegame version
250 /* NOTE: This element either denotes the address of the variable for a global
251 * variable, or the offset within a struct which is then bound to a variable
252 * during runtime. Decision on which one to use is controlled by the function
253 * that is called to save it. address: global=true, offset: global=false */
254 void *address; ///< address of variable OR offset of variable in the struct (max offset is 65536)
255 size_t size; ///< the sizeof size.
258 /** Same as #SaveLoad but global variables are used (for better readability); */
259 typedef SaveLoad SaveLoadGlobVarList;
262 * Storage of simple variables, references (pointers), and arrays.
263 * @param cmd Load/save type. @see SaveLoadType
264 * @param base Name of the class or struct containing the variable.
265 * @param variable Name of the variable in the class or struct referenced by \a base.
266 * @param type Storage of the data in memory and in the savegame.
267 * @param from First savegame version that has the field.
268 * @param to Last savegame version that has the field.
269 * @note In general, it is better to use one of the SLE_* macros below.
271 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable), cpp_sizeof(base, variable)}
274 * Storage of a variable in some savegame versions.
275 * @param base Name of the class or struct containing the variable.
276 * @param variable Name of the variable in the class or struct referenced by \a base.
277 * @param type Storage of the data in memory and in the savegame.
278 * @param from First savegame version that has the field.
279 * @param to Last savegame version that has the field.
281 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
284 * Storage of a reference in some savegame versions.
285 * @param base Name of the class or struct containing the variable.
286 * @param variable Name of the variable in the class or struct referenced by \a base.
287 * @param type Type of the reference, a value from #SLRefType.
288 * @param from First savegame version that has the field.
289 * @param to Last savegame version that has the field.
291 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
294 * Storage of an array in some savegame versions.
295 * @param base Name of the class or struct containing the array.
296 * @param variable Name of the variable in the class or struct referenced by \a base.
297 * @param type Storage of the data in memory and in the savegame.
298 * @param length Number of elements in the array.
299 * @param from First savegame version that has the array.
300 * @param to Last savegame version that has the array.
302 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
305 * Storage of a string in some savegame versions.
306 * @param base Name of the class or struct containing the string.
307 * @param variable Name of the variable in the class or struct referenced by \a base.
308 * @param type Storage of the data in memory and in the savegame.
309 * @param length Number of elements in the string (only used for fixed size buffers).
310 * @param from First savegame version that has the string.
311 * @param to Last savegame version that has the string.
313 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
316 * Storage of a std::string in some savegame versions.
317 * @param base Name of the class or struct containing the string.
318 * @param variable Name of the variable in the class or struct referenced by \a base.
319 * @param type Storage of the data in memory and in the savegame.
320 * @param from First savegame version that has the string.
321 * @param to Last savegame version that has the string.
323 #define SLE_CONDSTDSTR(base, variable, type, from, to) SLE_GENERAL(SL_STDSTR, base, variable, type, 0, from, to)
326 * Storage of a list in some savegame versions.
327 * @param base Name of the class or struct containing the list.
328 * @param variable Name of the variable in the class or struct referenced by \a base.
329 * @param type Storage of the data in memory and in the savegame.
330 * @param from First savegame version that has the list.
331 * @param to Last savegame version that has the list.
333 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
336 * Storage of a deque in some savegame versions.
337 * @param base Name of the class or struct containing the list.
338 * @param variable Name of the variable in the class or struct referenced by \a base.
339 * @param type Storage of the data in memory and in the savegame.
340 * @param from First savegame version that has the list.
341 * @param to Last savegame version that has the list.
343 #define SLE_CONDDEQ(base, variable, type, from, to) SLE_GENERAL(SL_DEQ, base, variable, type, 0, from, to)
346 * Storage of a vector in some savegame versions.
347 * @param base Name of the class or struct containing the list.
348 * @param variable Name of the variable in the class or struct referenced by \a base.
349 * @param type Storage of the data in memory and in the savegame.
350 * @param from First savegame version that has the list.
351 * @param to Last savegame version that has the list.
353 #define SLE_CONDVEC(base, variable, type, from, to) SLE_GENERAL(SL_VEC, base, variable, type, 0, from, to)
356 * Storage of a variable vector in some savegame versions.
357 * @param base Name of the class or struct containing the list.
358 * @param variable Name of the variable in the class or struct referenced by \a base.
359 * @param type Storage of the data in memory and in the savegame.
360 * @param from First savegame version that has the list.
361 * @param to Last savegame version that has the list.
363 #define SLE_CONDVARVEC(base, variable, type, from, to) SLE_GENERAL(SL_VARVEC, base, variable, type, 0, from, to)
366 * Storage of a variable in every version of a savegame.
367 * @param base Name of the class or struct containing the variable.
368 * @param variable Name of the variable in the class or struct referenced by \a base.
369 * @param type Storage of the data in memory and in the savegame.
371 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
374 * Storage of a reference in every version of a savegame.
375 * @param base Name of the class or struct containing the variable.
376 * @param variable Name of the variable in the class or struct referenced by \a base.
377 * @param type Type of the reference, a value from #SLRefType.
379 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
382 * Storage of an array in every version of a savegame.
383 * @param base Name of the class or struct containing the array.
384 * @param variable Name of the variable in the class or struct referenced by \a base.
385 * @param type Storage of the data in memory and in the savegame.
386 * @param length Number of elements in the array.
388 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
391 * Storage of a string in every savegame version.
392 * @param base Name of the class or struct containing the string.
393 * @param variable Name of the variable in the class or struct referenced by \a base.
394 * @param type Storage of the data in memory and in the savegame.
395 * @param length Number of elements in the string (only used for fixed size buffers).
397 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
400 * Storage of a std::string in every savegame version.
401 * @param base Name of the class or struct containing the string.
402 * @param variable Name of the variable in the class or struct referenced by \a base.
403 * @param type Storage of the data in memory and in the savegame.
405 #define SLE_STDSTR(base, variable, type) SLE_CONDSTDSTR(base, variable, type, 0, SL_MAX_VERSION)
408 * Storage of a list in every savegame version.
409 * @param base Name of the class or struct containing the list.
410 * @param variable Name of the variable in the class or struct referenced by \a base.
411 * @param type Storage of the data in memory and in the savegame.
413 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
416 * Storage of a deque in every savegame version.
417 * @param base Name of the class or struct containing the list.
418 * @param variable Name of the variable in the class or struct referenced by \a base.
419 * @param type Storage of the data in memory and in the savegame.
421 #define SLE_DEQ(base, variable, type) SLE_CONDDEQ(base, variable, type, 0, SL_MAX_VERSION)
424 * Storage of a vector in every savegame version.
425 * @param base Name of the class or struct containing the list.
426 * @param variable Name of the variable in the class or struct referenced by \a base.
427 * @param type Storage of the data in memory and in the savegame.
429 #define SLE_VEC(base, variable, type) SLE_CONDVEC(base, variable, type, 0, SL_MAX_VERSION)
432 * Empty space in every savegame version.
433 * @param length Length of the empty space.
435 #define SLE_NULL(length) SLE_CONDNULL(length, 0, SL_MAX_VERSION)
438 * Empty space in some savegame versions.
439 * @param length Length of the empty space in bytes.
440 * @param from First savegame version that has the empty space.
441 * @param to Last savegame version that has the empty space.
443 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)
445 /** Translate values ingame to different values in the savegame and vv. */
446 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
448 #define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
449 #define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
451 /** End marker of a struct/class save or load. */
452 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL, 0}
455 * Storage of global simple variables, references (pointers), and arrays.
456 * @param cmd Load/save type. @see SaveLoadType
457 * @param variable Name of the global variable.
458 * @param type Storage of the data in memory and in the savegame.
459 * @param from First savegame version that has the field.
460 * @param to Last savegame version that has the field.
461 * @note In general, it is better to use one of the SLEG_* macros below.
463 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable, sizeof(variable)}
466 * Storage of a global variable in some savegame versions.
467 * @param variable Name of the global variable.
468 * @param type Storage of the data in memory and in the savegame.
469 * @param from First savegame version that has the field.
470 * @param to Last savegame version that has the field.
472 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
475 * Storage of a global reference in some savegame versions.
476 * @param variable Name of the global variable.
477 * @param type Storage of the data in memory and in the savegame.
478 * @param from First savegame version that has the field.
479 * @param to Last savegame version that has the field.
481 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
484 * Storage of a global array in some savegame versions.
485 * @param variable Name of the global variable.
486 * @param type Storage of the data in memory and in the savegame.
487 * @param length Number of elements in the array.
488 * @param from First savegame version that has the array.
489 * @param to Last savegame version that has the array.
491 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
494 * Storage of a global string in some savegame versions.
495 * @param variable Name of the global variable.
496 * @param type Storage of the data in memory and in the savegame.
497 * @param length Number of elements in the string (only used for fixed size buffers).
498 * @param from First savegame version that has the string.
499 * @param to Last savegame version that has the string.
501 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
504 * Storage of a global list in some savegame versions.
505 * @param variable Name of the global variable.
506 * @param type Storage of the data in memory and in the savegame.
507 * @param from First savegame version that has the list.
508 * @param to Last savegame version that has the list.
510 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
513 * Storage of a global deque in some savegame versions.
514 * @param variable Name of the global variable.
515 * @param type Storage of the data in memory and in the savegame.
516 * @param from First savegame version that has the list.
517 * @param to Last savegame version that has the list.
519 #define SLEG_CONDDEQ(variable, type, from, to) SLEG_GENERAL(SL_DEQ, variable, type, 0, from, to)
522 * Storage of a global vector in some savegame versions.
523 * @param variable Name of the global variable.
524 * @param type Storage of the data in memory and in the savegame.
525 * @param from First savegame version that has the list.
526 * @param to Last savegame version that has the list.
528 #define SLEG_CONDVEC(variable, type, from, to) SLEG_GENERAL(SL_VEC, variable, type, 0, from, to)
531 * Storage of a global variable in every savegame version.
532 * @param variable Name of the global variable.
533 * @param type Storage of the data in memory and in the savegame.
535 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
538 * Storage of a global reference in every savegame version.
539 * @param variable Name of the global variable.
540 * @param type Storage of the data in memory and in the savegame.
542 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
545 * Storage of a global array in every savegame version.
546 * @param variable Name of the global variable.
547 * @param type Storage of the data in memory and in the savegame.
549 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
552 * Storage of a global string in every savegame version.
553 * @param variable Name of the global variable.
554 * @param type Storage of the data in memory and in the savegame.
556 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
559 * Storage of a global list in every savegame version.
560 * @param variable Name of the global variable.
561 * @param type Storage of the data in memory and in the savegame.
563 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
566 * Storage of a global deque in every savegame version.
567 * @param variable Name of the global variable.
568 * @param type Storage of the data in memory and in the savegame.
570 #define SLEG_DEQ(variable, type) SLEG_CONDDEQ(variable, type, 0, SL_MAX_VERSION)
573 * Storage of a global vector in every savegame version.
574 * @param variable Name of the global variable.
575 * @param type Storage of the data in memory and in the savegame.
577 #define SLEG_VEC(variable, type) SLEG_CONDVEC(variable, type, 0, SL_MAX_VERSION)
580 * Empty global space in some savegame versions.
581 * @param length Length of the empty space.
582 * @param from First savegame version that has the empty space.
583 * @param to Last savegame version that has the empty space.
585 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}
587 /** End marker of global variables save or load. */
588 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL, 0}
591 * Checks whether the savegame is below \a major.\a minor.
592 * @param major Major number of the version to check against.
593 * @param minor Minor number of the version to check against. If \a minor is 0 or not specified, only the major number is checked.
594 * @return Savegame version is earlier than the specified version.
596 static inline bool IsSavegameVersionBefore(uint16 major, byte minor = 0)
598 extern uint16 _sl_version;
599 extern byte _sl_minor_version;
600 return _sl_version < major || (minor > 0 && _sl_version == major && _sl_minor_version < minor);
604 * Checks whether the savegame is a patch pack savegame below \a major.
605 * @param major Major number of the version to check against.
606 * @return Savegame version is a patch pack version earlier than the specified version.
608 static inline bool IsPatchPackSavegameVersionBefore(uint16 major)
610 return !IsSavegameVersionBefore(SL_PATCH_PACK) && IsSavegameVersionBefore(major);
614 * Checks if some version from/to combination falls within the range of the
615 * active savegame version.
616 * @param version_from Lowest version number that falls within the range.
617 * @param version_to Highest version number that falls within the range.
618 * @return Active savegame version falls within the given range.
620 static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to)
622 extern const uint16 SAVEGAME_VERSION;
623 if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false;
625 return true;
629 * Get the NumberType of a setting. This describes the integer type
630 * as it is represented in memory
631 * @param type VarType holding information about the variable-type
632 * @return return the SLE_VAR_* part of a variable-type description
634 static inline VarType GetVarMemType(VarType type)
636 return type & 0xF0; // GB(type, 4, 4) << 4;
640 * Get the #FileType of a setting. This describes the integer type
641 * as it is represented in a savegame/file
642 * @param type VarType holding information about the file-type
643 * @param return the SLE_FILE_* part of a variable-type description
645 static inline VarType GetVarFileType(VarType type)
647 return type & 0xF; // GB(type, 0, 4);
651 * Check if the given saveload type is a numeric type.
652 * @param conv the type to check
653 * @return True if it's a numeric type.
655 static inline bool IsNumericType(VarType conv)
657 return GetVarMemType(conv) <= SLE_VAR_U64;
661 * Get the address of the variable. Which one to pick depends on the object
662 * pointer. If it is NULL we are dealing with global variables so the address
663 * is taken. If non-null only the offset is stored in the union and we need
664 * to add this to the address of the object
666 static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
668 return const_cast<byte *>((const byte*)(sld->global ? NULL : object) + (ptrdiff_t)sld->address);
671 int64 ReadValue(const void *ptr, VarType conv);
672 void WriteValue(void *ptr, VarType conv, int64 val);
674 void SlSetArrayIndex(uint index);
675 int SlIterateArray();
677 void SlAutolength(AutolengthProc *proc, void *arg);
678 size_t SlGetFieldLength();
679 void SlSetLength(size_t length);
680 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld);
681 size_t SlCalcObjLength(const void *object, const SaveLoad *sld);
683 byte SlReadByte();
684 void SlWriteByte(byte b);
686 void SlGlobList(const SaveLoadGlobVarList *sldg);
687 void SlArray(void *array, size_t length, VarType conv);
688 void SlObject(void *object, const SaveLoad *sld);
689 bool SlObjectMember(void *object, const SaveLoad *sld);
690 void NORETURN SlError(StringID string, const char *extra_msg = NULL);
691 void NORETURN SlErrorCorrupt(const char *msg);
693 bool SaveloadCrashWithMissingNewGRFs();
695 extern char _savegame_format[8];
696 extern bool _do_autosave;
698 #endif /* SAVELOAD_H */