Maintain a circular buffer of recent commands, add to crashlog.
[openttd-joker.git] / src / saveload / saveload.h
blobf1f46c9687d9bcabc0a00180f6a324f3b332218d
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
124 /** Flags of a chunk. */
125 enum ChunkType {
126 CH_RIFF = 0,
127 CH_ARRAY = 1,
128 CH_SPARSE_ARRAY = 2,
129 CH_TYPE_MASK = 3,
130 CH_LAST = 8, ///< Last chunk in this array.
134 * VarTypes is the general bitmasked magic type that tells us
135 * certain characteristics about the variable it refers to. For example
136 * SLE_FILE_* gives the size(type) as it would be in the savegame and
137 * SLE_VAR_* the size(type) as it is in memory during runtime. These are
138 * the first 8 bits (0-3 SLE_FILE, 4-7 SLE_VAR).
139 * Bits 8-15 are reserved for various flags as explained below
141 enum VarTypes {
142 /* 4 bits allocated a maximum of 16 types for NumberType */
143 SLE_FILE_I8 = 0,
144 SLE_FILE_U8 = 1,
145 SLE_FILE_I16 = 2,
146 SLE_FILE_U16 = 3,
147 SLE_FILE_I32 = 4,
148 SLE_FILE_U32 = 5,
149 SLE_FILE_I64 = 6,
150 SLE_FILE_U64 = 7,
151 SLE_FILE_STRINGID = 8, ///< StringID offset into strings-array
152 SLE_FILE_STRING = 9,
153 /* 6 more possible file-primitives */
155 /* 4 bits allocated a maximum of 16 types for NumberType */
156 SLE_VAR_BL = 0 << 4,
157 SLE_VAR_I8 = 1 << 4,
158 SLE_VAR_U8 = 2 << 4,
159 SLE_VAR_I16 = 3 << 4,
160 SLE_VAR_U16 = 4 << 4,
161 SLE_VAR_I32 = 5 << 4,
162 SLE_VAR_U32 = 6 << 4,
163 SLE_VAR_I64 = 7 << 4,
164 SLE_VAR_U64 = 8 << 4,
165 SLE_VAR_NULL = 9 << 4, ///< useful to write zeros in savegame.
166 SLE_VAR_STRB = 10 << 4, ///< string (with pre-allocated buffer)
167 SLE_VAR_STRBQ = 11 << 4, ///< string enclosed in quotes (with pre-allocated buffer)
168 SLE_VAR_STR = 12 << 4, ///< string pointer
169 SLE_VAR_STRQ = 13 << 4, ///< string pointer enclosed in quotes
170 SLE_VAR_NAME = 14 << 4, ///< old custom name to be converted to a char pointer
171 /* 1 more possible memory-primitives */
173 /* Shortcut values */
174 SLE_VAR_CHAR = SLE_VAR_I8,
176 /* Default combinations of variables. As savegames change, so can variables
177 * and thus it is possible that the saved value and internal size do not
178 * match and you need to specify custom combo. The defaults are listed here */
179 SLE_BOOL = SLE_FILE_I8 | SLE_VAR_BL,
180 SLE_INT8 = SLE_FILE_I8 | SLE_VAR_I8,
181 SLE_UINT8 = SLE_FILE_U8 | SLE_VAR_U8,
182 SLE_INT16 = SLE_FILE_I16 | SLE_VAR_I16,
183 SLE_UINT16 = SLE_FILE_U16 | SLE_VAR_U16,
184 SLE_INT32 = SLE_FILE_I32 | SLE_VAR_I32,
185 SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32,
186 SLE_INT64 = SLE_FILE_I64 | SLE_VAR_I64,
187 SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64,
188 SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR,
189 SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U32,
190 SLE_STRINGBUF = SLE_FILE_STRING | SLE_VAR_STRB,
191 SLE_STRINGBQUOTE = SLE_FILE_STRING | SLE_VAR_STRBQ,
192 SLE_STRING = SLE_FILE_STRING | SLE_VAR_STR,
193 SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ,
194 SLE_NAME = SLE_FILE_STRINGID | SLE_VAR_NAME,
196 /* Shortcut values */
197 SLE_UINT = SLE_UINT32,
198 SLE_INT = SLE_INT32,
199 SLE_STRB = SLE_STRINGBUF,
200 SLE_STRBQ = SLE_STRINGBQUOTE,
201 SLE_STR = SLE_STRING,
202 SLE_STRQ = SLE_STRINGQUOTE,
204 /* 8 bits allocated for a maximum of 8 flags
205 * Flags directing saving/loading of a variable */
206 SLF_NOT_IN_SAVE = 1 << 8, ///< do not save with savegame, basically client-based
207 SLF_NOT_IN_CONFIG = 1 << 9, ///< do not save to config file
208 SLF_NO_NETWORK_SYNC = 1 << 10, ///< do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
209 SLF_ALLOW_CONTROL = 1 << 11, ///< allow control codes in the strings
210 SLF_ALLOW_NEWLINE = 1 << 12, ///< allow new lines in the strings
211 /* 3 more possible flags */
214 typedef uint32 VarType;
216 /** Type of data saved. */
217 enum SaveLoadTypes {
218 SL_VAR = 0, ///< Save/load a variable.
219 SL_REF = 1, ///< Save/load a reference.
220 SL_ARR = 2, ///< Save/load an array.
221 SL_STR = 3, ///< Save/load a string.
222 SL_LST = 4, ///< Save/load a list.
223 SL_DEQ = 5, ///< Save/load a deque.
224 SL_VEC = 6, ///< Save/load a vector.
225 /* non-normal save-load types */
226 SL_WRITEBYTE = 8,
227 SL_VEH_INCLUDE = 9,
228 SL_ST_INCLUDE = 10,
229 SL_END = 15
232 typedef byte SaveLoadType; ///< Save/load type. @see SaveLoadTypes
234 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
235 struct SaveLoad {
236 bool global; ///< should we load a global variable or a non-global one
237 SaveLoadType cmd; ///< the action to take with the saved/loaded type, All types need different action
238 VarType conv; ///< type of the variable to be saved, int
239 uint16 length; ///< (conditional) length of the variable (eg. arrays) (max array size is 65536 elements)
240 uint16 version_from; ///< save/load the variable starting from this savegame version
241 uint16 version_to; ///< save/load the variable until this savegame version
242 /* NOTE: This element either denotes the address of the variable for a global
243 * variable, or the offset within a struct which is then bound to a variable
244 * during runtime. Decision on which one to use is controlled by the function
245 * that is called to save it. address: global=true, offset: global=false */
246 void *address; ///< address of variable OR offset of variable in the struct (max offset is 65536)
247 size_t size; ///< the sizeof size.
250 /** Same as #SaveLoad but global variables are used (for better readability); */
251 typedef SaveLoad SaveLoadGlobVarList;
254 * Storage of simple variables, references (pointers), and arrays.
255 * @param cmd Load/save type. @see SaveLoadType
256 * @param base Name of the class or struct containing the variable.
257 * @param variable Name of the variable in the class or struct referenced by \a base.
258 * @param type Storage of the data in memory and in the savegame.
259 * @param from First savegame version that has the field.
260 * @param to Last savegame version that has the field.
261 * @note In general, it is better to use one of the SLE_* macros below.
263 #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)}
266 * Storage of a variable in some savegame versions.
267 * @param base Name of the class or struct containing the variable.
268 * @param variable Name of the variable in the class or struct referenced by \a base.
269 * @param type Storage of the data in memory and in the savegame.
270 * @param from First savegame version that has the field.
271 * @param to Last savegame version that has the field.
273 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
276 * Storage of a reference in some savegame versions.
277 * @param base Name of the class or struct containing the variable.
278 * @param variable Name of the variable in the class or struct referenced by \a base.
279 * @param type Type of the reference, a value from #SLRefType.
280 * @param from First savegame version that has the field.
281 * @param to Last savegame version that has the field.
283 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
286 * Storage of an array in some savegame versions.
287 * @param base Name of the class or struct containing the array.
288 * @param variable Name of the variable in the class or struct referenced by \a base.
289 * @param type Storage of the data in memory and in the savegame.
290 * @param length Number of elements in the array.
291 * @param from First savegame version that has the array.
292 * @param to Last savegame version that has the array.
294 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
297 * Storage of a string in some savegame versions.
298 * @param base Name of the class or struct containing the string.
299 * @param variable Name of the variable in the class or struct referenced by \a base.
300 * @param type Storage of the data in memory and in the savegame.
301 * @param length Number of elements in the string (only used for fixed size buffers).
302 * @param from First savegame version that has the string.
303 * @param to Last savegame version that has the string.
305 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
308 * Storage of a list in some savegame versions.
309 * @param base Name of the class or struct containing the list.
310 * @param variable Name of the variable in the class or struct referenced by \a base.
311 * @param type Storage of the data in memory and in the savegame.
312 * @param from First savegame version that has the list.
313 * @param to Last savegame version that has the list.
315 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
318 * Storage of a deque in some savegame versions.
319 * @param base Name of the class or struct containing the list.
320 * @param variable Name of the variable in the class or struct referenced by \a base.
321 * @param type Storage of the data in memory and in the savegame.
322 * @param from First savegame version that has the list.
323 * @param to Last savegame version that has the list.
325 #define SLE_CONDDEQ(base, variable, type, from, to) SLE_GENERAL(SL_DEQ, base, variable, type, 0, from, to)
328 * Storage of a vector in some savegame versions.
329 * @param base Name of the class or struct containing the list.
330 * @param variable Name of the variable in the class or struct referenced by \a base.
331 * @param type Storage of the data in memory and in the savegame.
332 * @param from First savegame version that has the list.
333 * @param to Last savegame version that has the list.
335 #define SLE_CONDVEC(base, variable, type, from, to) SLE_GENERAL(SL_VEC, base, variable, type, 0, from, to)
338 * Storage of a variable in every version of a savegame.
339 * @param base Name of the class or struct containing the variable.
340 * @param variable Name of the variable in the class or struct referenced by \a base.
341 * @param type Storage of the data in memory and in the savegame.
343 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
346 * Storage of a reference in every version of a savegame.
347 * @param base Name of the class or struct containing the variable.
348 * @param variable Name of the variable in the class or struct referenced by \a base.
349 * @param type Type of the reference, a value from #SLRefType.
351 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
354 * Storage of an array in every version of a savegame.
355 * @param base Name of the class or struct containing the array.
356 * @param variable Name of the variable in the class or struct referenced by \a base.
357 * @param type Storage of the data in memory and in the savegame.
358 * @param length Number of elements in the array.
360 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
363 * Storage of a string in every savegame version.
364 * @param base Name of the class or struct containing the string.
365 * @param variable Name of the variable in the class or struct referenced by \a base.
366 * @param type Storage of the data in memory and in the savegame.
367 * @param length Number of elements in the string (only used for fixed size buffers).
369 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
372 * Storage of a list in every savegame version.
373 * @param base Name of the class or struct containing the list.
374 * @param variable Name of the variable in the class or struct referenced by \a base.
375 * @param type Storage of the data in memory and in the savegame.
377 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
380 * Storage of a deque in every savegame version.
381 * @param base Name of the class or struct containing the list.
382 * @param variable Name of the variable in the class or struct referenced by \a base.
383 * @param type Storage of the data in memory and in the savegame.
385 #define SLE_DEQ(base, variable, type) SLE_CONDDEQ(base, variable, type, 0, SL_MAX_VERSION)
388 * Storage of a vector in every savegame version.
389 * @param base Name of the class or struct containing the list.
390 * @param variable Name of the variable in the class or struct referenced by \a base.
391 * @param type Storage of the data in memory and in the savegame.
393 #define SLE_VEC(base, variable, type) SLE_CONDVEC(base, variable, type, 0, SL_MAX_VERSION)
396 * Empty space in every savegame version.
397 * @param length Length of the empty space.
399 #define SLE_NULL(length) SLE_CONDNULL(length, 0, SL_MAX_VERSION)
402 * Empty space in some savegame versions.
403 * @param length Length of the empty space in bytes.
404 * @param from First savegame version that has the empty space.
405 * @param to Last savegame version that has the empty space.
407 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)
409 /** Translate values ingame to different values in the savegame and vv. */
410 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
412 #define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
413 #define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
415 /** End marker of a struct/class save or load. */
416 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL, 0}
419 * Storage of global simple variables, references (pointers), and arrays.
420 * @param cmd Load/save type. @see SaveLoadType
421 * @param variable Name of the global variable.
422 * @param type Storage of the data in memory and in the savegame.
423 * @param from First savegame version that has the field.
424 * @param to Last savegame version that has the field.
425 * @note In general, it is better to use one of the SLEG_* macros below.
427 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable, sizeof(variable)}
430 * Storage of a global variable in some savegame versions.
431 * @param variable Name of the global variable.
432 * @param type Storage of the data in memory and in the savegame.
433 * @param from First savegame version that has the field.
434 * @param to Last savegame version that has the field.
436 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
439 * Storage of a global reference in some savegame versions.
440 * @param variable Name of the global variable.
441 * @param type Storage of the data in memory and in the savegame.
442 * @param from First savegame version that has the field.
443 * @param to Last savegame version that has the field.
445 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
448 * Storage of a global array in some savegame versions.
449 * @param variable Name of the global variable.
450 * @param type Storage of the data in memory and in the savegame.
451 * @param length Number of elements in the array.
452 * @param from First savegame version that has the array.
453 * @param to Last savegame version that has the array.
455 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
458 * Storage of a global string in some savegame versions.
459 * @param variable Name of the global variable.
460 * @param type Storage of the data in memory and in the savegame.
461 * @param length Number of elements in the string (only used for fixed size buffers).
462 * @param from First savegame version that has the string.
463 * @param to Last savegame version that has the string.
465 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
468 * Storage of a global list in some savegame versions.
469 * @param variable Name of the global variable.
470 * @param type Storage of the data in memory and in the savegame.
471 * @param from First savegame version that has the list.
472 * @param to Last savegame version that has the list.
474 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
477 * Storage of a global deque in some savegame versions.
478 * @param variable Name of the global variable.
479 * @param type Storage of the data in memory and in the savegame.
480 * @param from First savegame version that has the list.
481 * @param to Last savegame version that has the list.
483 #define SLEG_CONDDEQ(variable, type, from, to) SLEG_GENERAL(SL_DEQ, variable, type, 0, from, to)
486 * Storage of a global vector in some savegame versions.
487 * @param variable Name of the global variable.
488 * @param type Storage of the data in memory and in the savegame.
489 * @param from First savegame version that has the list.
490 * @param to Last savegame version that has the list.
492 #define SLEG_CONDVEC(variable, type, from, to) SLEG_GENERAL(SL_VEC, variable, type, 0, from, to)
495 * Storage of a global variable in every savegame version.
496 * @param variable Name of the global variable.
497 * @param type Storage of the data in memory and in the savegame.
499 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
502 * Storage of a global reference in every savegame version.
503 * @param variable Name of the global variable.
504 * @param type Storage of the data in memory and in the savegame.
506 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
509 * Storage of a global array in every savegame version.
510 * @param variable Name of the global variable.
511 * @param type Storage of the data in memory and in the savegame.
513 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
516 * Storage of a global string in every savegame version.
517 * @param variable Name of the global variable.
518 * @param type Storage of the data in memory and in the savegame.
520 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
523 * Storage of a global list in every savegame version.
524 * @param variable Name of the global variable.
525 * @param type Storage of the data in memory and in the savegame.
527 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
530 * Storage of a global deque in every savegame version.
531 * @param variable Name of the global variable.
532 * @param type Storage of the data in memory and in the savegame.
534 #define SLEG_DEQ(variable, type) SLEG_CONDDEQ(variable, type, 0, SL_MAX_VERSION)
537 * Storage of a global vector in every savegame version.
538 * @param variable Name of the global variable.
539 * @param type Storage of the data in memory and in the savegame.
541 #define SLEG_VEC(variable, type) SLEG_CONDVEC(variable, type, 0, SL_MAX_VERSION)
544 * Empty global space in some savegame versions.
545 * @param length Length of the empty space.
546 * @param from First savegame version that has the empty space.
547 * @param to Last savegame version that has the empty space.
549 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}
551 /** End marker of global variables save or load. */
552 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL, 0}
555 * Checks whether the savegame is below \a major.\a minor.
556 * @param major Major number of the version to check against.
557 * @param minor Minor number of the version to check against. If \a minor is 0 or not specified, only the major number is checked.
558 * @return Savegame version is earlier than the specified version.
560 static inline bool IsSavegameVersionBefore(uint16 major, byte minor = 0)
562 extern uint16 _sl_version;
563 extern byte _sl_minor_version;
564 return _sl_version < major || (minor > 0 && _sl_version == major && _sl_minor_version < minor);
568 * Checks whether the savegame is a patch pack savegame below \a major.
569 * @param major Major number of the version to check against.
570 * @return Savegame version is a patch pack version earlier than the specified version.
572 static inline bool IsPatchPackSavegameVersionBefore(uint16 major)
574 return !IsSavegameVersionBefore(SL_PATCH_PACK) && IsSavegameVersionBefore(major);
578 * Checks if some version from/to combination falls within the range of the
579 * active savegame version.
580 * @param version_from Lowest version number that falls within the range.
581 * @param version_to Highest version number that falls within the range.
582 * @return Active savegame version falls within the given range.
584 static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to)
586 extern const uint16 SAVEGAME_VERSION;
587 if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false;
589 return true;
593 * Get the NumberType of a setting. This describes the integer type
594 * as it is represented in memory
595 * @param type VarType holding information about the variable-type
596 * @return return the SLE_VAR_* part of a variable-type description
598 static inline VarType GetVarMemType(VarType type)
600 return type & 0xF0; // GB(type, 4, 4) << 4;
604 * Get the #FileType of a setting. This describes the integer type
605 * as it is represented in a savegame/file
606 * @param type VarType holding information about the file-type
607 * @param return the SLE_FILE_* part of a variable-type description
609 static inline VarType GetVarFileType(VarType type)
611 return type & 0xF; // GB(type, 0, 4);
615 * Check if the given saveload type is a numeric type.
616 * @param conv the type to check
617 * @return True if it's a numeric type.
619 static inline bool IsNumericType(VarType conv)
621 return GetVarMemType(conv) <= SLE_VAR_U64;
625 * Get the address of the variable. Which one to pick depends on the object
626 * pointer. If it is NULL we are dealing with global variables so the address
627 * is taken. If non-null only the offset is stored in the union and we need
628 * to add this to the address of the object
630 static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
632 return const_cast<byte *>((const byte*)(sld->global ? NULL : object) + (ptrdiff_t)sld->address);
635 int64 ReadValue(const void *ptr, VarType conv);
636 void WriteValue(void *ptr, VarType conv, int64 val);
638 void SlSetArrayIndex(uint index);
639 int SlIterateArray();
641 void SlAutolength(AutolengthProc *proc, void *arg);
642 size_t SlGetFieldLength();
643 void SlSetLength(size_t length);
644 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld);
645 size_t SlCalcObjLength(const void *object, const SaveLoad *sld);
647 byte SlReadByte();
648 void SlWriteByte(byte b);
650 void SlGlobList(const SaveLoadGlobVarList *sldg);
651 void SlArray(void *array, size_t length, VarType conv);
652 void SlObject(void *object, const SaveLoad *sld);
653 bool SlObjectMember(void *object, const SaveLoad *sld);
654 void NORETURN SlError(StringID string, const char *extra_msg = NULL);
655 void NORETURN SlErrorCorrupt(const char *msg);
657 bool SaveloadCrashWithMissingNewGRFs();
659 extern char _savegame_format[8];
660 extern bool _do_autosave;
662 #endif /* SAVELOAD_H */