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/>.
10 /** @file saveload.h Functions/types related to saving and loading games. */
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. */
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);
56 void ProcessAsyncSaveFinish();
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. */
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
79 /** Type of reference (#SLE_REF, #SLE_CONDREF). */
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
126 /** Flags of a chunk. */
132 CH_LAST
= 8, ///< Last chunk in this array.
136 * VarTypes is the general bitmasked magic type that tells us
137 * certain characteristics about the variable it refers to. For example
138 * SLE_FILE_* gives the size(type) as it would be in the savegame and
139 * SLE_VAR_* the size(type) as it is in memory during runtime. These are
140 * the first 8 bits (0-3 SLE_FILE, 4-7 SLE_VAR).
141 * Bits 8-15 are reserved for various flags as explained below
144 /* 4 bits allocated a maximum of 16 types for NumberType */
153 SLE_FILE_STRINGID
= 8, ///< StringID offset into strings-array
155 /* 6 more possible file-primitives */
157 /* 4 bits allocated a maximum of 16 types for NumberType */
161 SLE_VAR_I16
= 3 << 4,
162 SLE_VAR_U16
= 4 << 4,
163 SLE_VAR_I32
= 5 << 4,
164 SLE_VAR_U32
= 6 << 4,
165 SLE_VAR_I64
= 7 << 4,
166 SLE_VAR_U64
= 8 << 4,
167 SLE_VAR_NULL
= 9 << 4, ///< useful to write zeros in savegame.
168 SLE_VAR_STRB
= 10 << 4, ///< string (with pre-allocated buffer)
169 SLE_VAR_STRBQ
= 11 << 4, ///< string enclosed in quotes (with pre-allocated buffer)
170 SLE_VAR_STR
= 12 << 4, ///< string pointer
171 SLE_VAR_STRQ
= 13 << 4, ///< string pointer enclosed in quotes
172 SLE_VAR_NAME
= 14 << 4, ///< old custom name to be converted to a char pointer
173 /* 1 more possible memory-primitives */
175 /* Shortcut values */
176 SLE_VAR_CHAR
= SLE_VAR_I8
,
178 /* Default combinations of variables. As savegames change, so can variables
179 * and thus it is possible that the saved value and internal size do not
180 * match and you need to specify custom combo. The defaults are listed here */
181 SLE_BOOL
= SLE_FILE_I8
| SLE_VAR_BL
,
182 SLE_INT8
= SLE_FILE_I8
| SLE_VAR_I8
,
183 SLE_UINT8
= SLE_FILE_U8
| SLE_VAR_U8
,
184 SLE_INT16
= SLE_FILE_I16
| SLE_VAR_I16
,
185 SLE_UINT16
= SLE_FILE_U16
| SLE_VAR_U16
,
186 SLE_INT32
= SLE_FILE_I32
| SLE_VAR_I32
,
187 SLE_UINT32
= SLE_FILE_U32
| SLE_VAR_U32
,
188 SLE_INT64
= SLE_FILE_I64
| SLE_VAR_I64
,
189 SLE_UINT64
= SLE_FILE_U64
| SLE_VAR_U64
,
190 SLE_CHAR
= SLE_FILE_I8
| SLE_VAR_CHAR
,
191 SLE_STRINGID
= SLE_FILE_STRINGID
| SLE_VAR_U32
,
192 SLE_STRINGBUF
= SLE_FILE_STRING
| SLE_VAR_STRB
,
193 SLE_STRINGBQUOTE
= SLE_FILE_STRING
| SLE_VAR_STRBQ
,
194 SLE_STRING
= SLE_FILE_STRING
| SLE_VAR_STR
,
195 SLE_STRINGQUOTE
= SLE_FILE_STRING
| SLE_VAR_STRQ
,
196 SLE_NAME
= SLE_FILE_STRINGID
| SLE_VAR_NAME
,
198 /* Shortcut values */
199 SLE_UINT
= SLE_UINT32
,
201 SLE_STRB
= SLE_STRINGBUF
,
202 SLE_STRBQ
= SLE_STRINGBQUOTE
,
203 SLE_STR
= SLE_STRING
,
204 SLE_STRQ
= SLE_STRINGQUOTE
,
206 /* 8 bits allocated for a maximum of 8 flags
207 * Flags directing saving/loading of a variable */
208 SLF_NOT_IN_SAVE
= 1 << 8, ///< do not save with savegame, basically client-based
209 SLF_NOT_IN_CONFIG
= 1 << 9, ///< do not save to config file
210 SLF_NO_NETWORK_SYNC
= 1 << 10, ///< do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
211 SLF_ALLOW_CONTROL
= 1 << 11, ///< allow control codes in the strings
212 SLF_ALLOW_NEWLINE
= 1 << 12, ///< allow new lines in the strings
213 /* 3 more possible flags */
216 typedef uint32 VarType
;
218 /** Type of data saved. */
220 SL_VAR
= 0, ///< Save/load a variable.
221 SL_REF
= 1, ///< Save/load a reference.
222 SL_ARR
= 2, ///< Save/load an array.
223 SL_STR
= 3, ///< Save/load a string.
224 SL_LST
= 4, ///< Save/load a list.
225 SL_DEQ
= 5, ///< Save/load a deque.
226 SL_VEC
= 6, ///< Save/load a vector.
227 SL_STDSTR
= 7, ///< Save/load a std::string.
228 /* non-normal save-load types */
232 /* primitive type vector */
237 typedef byte SaveLoadType
; ///< Save/load type. @see SaveLoadTypes
239 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
241 bool global
; ///< should we load a global variable or a non-global one
242 SaveLoadType cmd
; ///< the action to take with the saved/loaded type, All types need different action
243 VarType conv
; ///< type of the variable to be saved, int
244 uint16 length
; ///< (conditional) length of the variable (eg. arrays) (max array size is 65536 elements)
245 uint16 version_from
; ///< save/load the variable starting from this savegame version
246 uint16 version_to
; ///< save/load the variable until this savegame version
247 /* NOTE: This element either denotes the address of the variable for a global
248 * variable, or the offset within a struct which is then bound to a variable
249 * during runtime. Decision on which one to use is controlled by the function
250 * that is called to save it. address: global=true, offset: global=false */
251 void *address
; ///< address of variable OR offset of variable in the struct (max offset is 65536)
252 size_t size
; ///< the sizeof size.
255 /** Same as #SaveLoad but global variables are used (for better readability); */
256 typedef SaveLoad SaveLoadGlobVarList
;
259 * Storage of simple variables, references (pointers), and arrays.
260 * @param cmd Load/save type. @see SaveLoadType
261 * @param base Name of the class or struct containing the variable.
262 * @param variable Name of the variable in the class or struct referenced by \a base.
263 * @param type Storage of the data in memory and in the savegame.
264 * @param from First savegame version that has the field.
265 * @param to Last savegame version that has the field.
266 * @note In general, it is better to use one of the SLE_* macros below.
268 #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)}
271 * Storage of a variable in some savegame versions.
272 * @param base Name of the class or struct containing the variable.
273 * @param variable Name of the variable in the class or struct referenced by \a base.
274 * @param type Storage of the data in memory and in the savegame.
275 * @param from First savegame version that has the field.
276 * @param to Last savegame version that has the field.
278 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
281 * Storage of a reference in some savegame versions.
282 * @param base Name of the class or struct containing the variable.
283 * @param variable Name of the variable in the class or struct referenced by \a base.
284 * @param type Type of the reference, a value from #SLRefType.
285 * @param from First savegame version that has the field.
286 * @param to Last savegame version that has the field.
288 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
291 * Storage of an array in some savegame versions.
292 * @param base Name of the class or struct containing the array.
293 * @param variable Name of the variable in the class or struct referenced by \a base.
294 * @param type Storage of the data in memory and in the savegame.
295 * @param length Number of elements in the array.
296 * @param from First savegame version that has the array.
297 * @param to Last savegame version that has the array.
299 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
302 * Storage of a string in some savegame versions.
303 * @param base Name of the class or struct containing the string.
304 * @param variable Name of the variable in the class or struct referenced by \a base.
305 * @param type Storage of the data in memory and in the savegame.
306 * @param length Number of elements in the string (only used for fixed size buffers).
307 * @param from First savegame version that has the string.
308 * @param to Last savegame version that has the string.
310 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
313 * Storage of a std::string in some savegame versions.
314 * @param base Name of the class or struct containing the string.
315 * @param variable Name of the variable in the class or struct referenced by \a base.
316 * @param type Storage of the data in memory and in the savegame.
317 * @param from First savegame version that has the string.
318 * @param to Last savegame version that has the string.
320 #define SLE_CONDSTDSTR(base, variable, type, from, to) SLE_GENERAL(SL_STDSTR, base, variable, type, 0, from, to)
323 * Storage of a list in some savegame versions.
324 * @param base Name of the class or struct containing the list.
325 * @param variable Name of the variable in the class or struct referenced by \a base.
326 * @param type Storage of the data in memory and in the savegame.
327 * @param from First savegame version that has the list.
328 * @param to Last savegame version that has the list.
330 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
333 * Storage of a deque in some savegame versions.
334 * @param base Name of the class or struct containing the list.
335 * @param variable Name of the variable in the class or struct referenced by \a base.
336 * @param type Storage of the data in memory and in the savegame.
337 * @param from First savegame version that has the list.
338 * @param to Last savegame version that has the list.
340 #define SLE_CONDDEQ(base, variable, type, from, to) SLE_GENERAL(SL_DEQ, base, variable, type, 0, from, to)
343 * Storage of a vector in some savegame versions.
344 * @param base Name of the class or struct containing the list.
345 * @param variable Name of the variable in the class or struct referenced by \a base.
346 * @param type Storage of the data in memory and in the savegame.
347 * @param from First savegame version that has the list.
348 * @param to Last savegame version that has the list.
350 #define SLE_CONDVEC(base, variable, type, from, to) SLE_GENERAL(SL_VEC, base, variable, type, 0, from, to)
353 * Storage of a variable vector in some savegame versions.
354 * @param base Name of the class or struct containing the list.
355 * @param variable Name of the variable in the class or struct referenced by \a base.
356 * @param type Storage of the data in memory and in the savegame.
357 * @param from First savegame version that has the list.
358 * @param to Last savegame version that has the list.
360 #define SLE_CONDVARVEC(base, variable, type, from, to) SLE_GENERAL(SL_VARVEC, base, variable, type, 0, from, to)
363 * Storage of a variable in every version of a savegame.
364 * @param base Name of the class or struct containing the variable.
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.
368 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
371 * Storage of a reference in every version of a savegame.
372 * @param base Name of the class or struct containing the variable.
373 * @param variable Name of the variable in the class or struct referenced by \a base.
374 * @param type Type of the reference, a value from #SLRefType.
376 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
379 * Storage of an array in every version of a savegame.
380 * @param base Name of the class or struct containing the array.
381 * @param variable Name of the variable in the class or struct referenced by \a base.
382 * @param type Storage of the data in memory and in the savegame.
383 * @param length Number of elements in the array.
385 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
388 * Storage of a string in every savegame version.
389 * @param base Name of the class or struct containing the string.
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.
392 * @param length Number of elements in the string (only used for fixed size buffers).
394 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
397 * Storage of a std::string in every savegame version.
398 * @param base Name of the class or struct containing the string.
399 * @param variable Name of the variable in the class or struct referenced by \a base.
400 * @param type Storage of the data in memory and in the savegame.
402 #define SLE_STDSTR(base, variable, type) SLE_CONDSTDSTR(base, variable, type, 0, SL_MAX_VERSION)
405 * Storage of a list in every savegame version.
406 * @param base Name of the class or struct containing the list.
407 * @param variable Name of the variable in the class or struct referenced by \a base.
408 * @param type Storage of the data in memory and in the savegame.
410 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
413 * Storage of a deque in every savegame version.
414 * @param base Name of the class or struct containing the list.
415 * @param variable Name of the variable in the class or struct referenced by \a base.
416 * @param type Storage of the data in memory and in the savegame.
418 #define SLE_DEQ(base, variable, type) SLE_CONDDEQ(base, variable, type, 0, SL_MAX_VERSION)
421 * Storage of a vector in every savegame version.
422 * @param base Name of the class or struct containing the list.
423 * @param variable Name of the variable in the class or struct referenced by \a base.
424 * @param type Storage of the data in memory and in the savegame.
426 #define SLE_VEC(base, variable, type) SLE_CONDVEC(base, variable, type, 0, SL_MAX_VERSION)
429 * Empty space in every savegame version.
430 * @param length Length of the empty space.
432 #define SLE_NULL(length) SLE_CONDNULL(length, 0, SL_MAX_VERSION)
435 * Empty space in some savegame versions.
436 * @param length Length of the empty space in bytes.
437 * @param from First savegame version that has the empty space.
438 * @param to Last savegame version that has the empty space.
440 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)
442 /** Translate values ingame to different values in the savegame and vv. */
443 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
445 #define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
446 #define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
448 /** End marker of a struct/class save or load. */
449 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL, 0}
452 * Storage of global simple variables, references (pointers), and arrays.
453 * @param cmd Load/save type. @see SaveLoadType
454 * @param variable Name of the global variable.
455 * @param type Storage of the data in memory and in the savegame.
456 * @param from First savegame version that has the field.
457 * @param to Last savegame version that has the field.
458 * @note In general, it is better to use one of the SLEG_* macros below.
460 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable, sizeof(variable)}
463 * Storage of a global variable in some savegame versions.
464 * @param variable Name of the global variable.
465 * @param type Storage of the data in memory and in the savegame.
466 * @param from First savegame version that has the field.
467 * @param to Last savegame version that has the field.
469 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
472 * Storage of a global reference in some savegame versions.
473 * @param variable Name of the global variable.
474 * @param type Storage of the data in memory and in the savegame.
475 * @param from First savegame version that has the field.
476 * @param to Last savegame version that has the field.
478 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
481 * Storage of a global array in some savegame versions.
482 * @param variable Name of the global variable.
483 * @param type Storage of the data in memory and in the savegame.
484 * @param length Number of elements in the array.
485 * @param from First savegame version that has the array.
486 * @param to Last savegame version that has the array.
488 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
491 * Storage of a global string in some savegame versions.
492 * @param variable Name of the global variable.
493 * @param type Storage of the data in memory and in the savegame.
494 * @param length Number of elements in the string (only used for fixed size buffers).
495 * @param from First savegame version that has the string.
496 * @param to Last savegame version that has the string.
498 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
501 * Storage of a global list in some savegame versions.
502 * @param variable Name of the global variable.
503 * @param type Storage of the data in memory and in the savegame.
504 * @param from First savegame version that has the list.
505 * @param to Last savegame version that has the list.
507 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
510 * Storage of a global deque in some savegame versions.
511 * @param variable Name of the global variable.
512 * @param type Storage of the data in memory and in the savegame.
513 * @param from First savegame version that has the list.
514 * @param to Last savegame version that has the list.
516 #define SLEG_CONDDEQ(variable, type, from, to) SLEG_GENERAL(SL_DEQ, variable, type, 0, from, to)
519 * Storage of a global vector in some savegame versions.
520 * @param variable Name of the global variable.
521 * @param type Storage of the data in memory and in the savegame.
522 * @param from First savegame version that has the list.
523 * @param to Last savegame version that has the list.
525 #define SLEG_CONDVEC(variable, type, from, to) SLEG_GENERAL(SL_VEC, variable, type, 0, from, to)
528 * Storage of a global variable in every savegame version.
529 * @param variable Name of the global variable.
530 * @param type Storage of the data in memory and in the savegame.
532 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
535 * Storage of a global reference in every savegame version.
536 * @param variable Name of the global variable.
537 * @param type Storage of the data in memory and in the savegame.
539 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
542 * Storage of a global array in every savegame version.
543 * @param variable Name of the global variable.
544 * @param type Storage of the data in memory and in the savegame.
546 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
549 * Storage of a global string in every savegame version.
550 * @param variable Name of the global variable.
551 * @param type Storage of the data in memory and in the savegame.
553 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
556 * Storage of a global list in every savegame version.
557 * @param variable Name of the global variable.
558 * @param type Storage of the data in memory and in the savegame.
560 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
563 * Storage of a global deque in every savegame version.
564 * @param variable Name of the global variable.
565 * @param type Storage of the data in memory and in the savegame.
567 #define SLEG_DEQ(variable, type) SLEG_CONDDEQ(variable, type, 0, SL_MAX_VERSION)
570 * Storage of a global vector in every savegame version.
571 * @param variable Name of the global variable.
572 * @param type Storage of the data in memory and in the savegame.
574 #define SLEG_VEC(variable, type) SLEG_CONDVEC(variable, type, 0, SL_MAX_VERSION)
577 * Empty global space in some savegame versions.
578 * @param length Length of the empty space.
579 * @param from First savegame version that has the empty space.
580 * @param to Last savegame version that has the empty space.
582 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}
584 /** End marker of global variables save or load. */
585 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL, 0}
588 * Checks whether the savegame is below \a major.\a minor.
589 * @param major Major number of the version to check against.
590 * @param minor Minor number of the version to check against. If \a minor is 0 or not specified, only the major number is checked.
591 * @return Savegame version is earlier than the specified version.
593 static inline bool IsSavegameVersionBefore(uint16 major
, byte minor
= 0)
595 extern uint16 _sl_version
;
596 extern byte _sl_minor_version
;
597 return _sl_version
< major
|| (minor
> 0 && _sl_version
== major
&& _sl_minor_version
< minor
);
601 * Checks whether the savegame is a patch pack savegame below \a major.
602 * @param major Major number of the version to check against.
603 * @return Savegame version is a patch pack version earlier than the specified version.
605 static inline bool IsPatchPackSavegameVersionBefore(uint16 major
)
607 return !IsSavegameVersionBefore(SL_PATCH_PACK
) && IsSavegameVersionBefore(major
);
611 * Checks if some version from/to combination falls within the range of the
612 * active savegame version.
613 * @param version_from Lowest version number that falls within the range.
614 * @param version_to Highest version number that falls within the range.
615 * @return Active savegame version falls within the given range.
617 static inline bool SlIsObjectCurrentlyValid(uint16 version_from
, uint16 version_to
)
619 extern const uint16 SAVEGAME_VERSION
;
620 if (SAVEGAME_VERSION
< version_from
|| SAVEGAME_VERSION
> version_to
) return false;
626 * Get the NumberType of a setting. This describes the integer type
627 * as it is represented in memory
628 * @param type VarType holding information about the variable-type
629 * @return return the SLE_VAR_* part of a variable-type description
631 static inline VarType
GetVarMemType(VarType type
)
633 return type
& 0xF0; // GB(type, 4, 4) << 4;
637 * Get the #FileType of a setting. This describes the integer type
638 * as it is represented in a savegame/file
639 * @param type VarType holding information about the file-type
640 * @param return the SLE_FILE_* part of a variable-type description
642 static inline VarType
GetVarFileType(VarType type
)
644 return type
& 0xF; // GB(type, 0, 4);
648 * Check if the given saveload type is a numeric type.
649 * @param conv the type to check
650 * @return True if it's a numeric type.
652 static inline bool IsNumericType(VarType conv
)
654 return GetVarMemType(conv
) <= SLE_VAR_U64
;
658 * Get the address of the variable. Which one to pick depends on the object
659 * pointer. If it is NULL we are dealing with global variables so the address
660 * is taken. If non-null only the offset is stored in the union and we need
661 * to add this to the address of the object
663 static inline void *GetVariableAddress(const void *object
, const SaveLoad
*sld
)
665 return const_cast<byte
*>((const byte
*)(sld
->global
? NULL
: object
) + (ptrdiff_t)sld
->address
);
668 int64
ReadValue(const void *ptr
, VarType conv
);
669 void WriteValue(void *ptr
, VarType conv
, int64 val
);
671 void SlSetArrayIndex(uint index
);
672 int SlIterateArray();
674 void SlAutolength(AutolengthProc
*proc
, void *arg
);
675 size_t SlGetFieldLength();
676 void SlSetLength(size_t length
);
677 size_t SlCalcObjMemberLength(const void *object
, const SaveLoad
*sld
);
678 size_t SlCalcObjLength(const void *object
, const SaveLoad
*sld
);
681 void SlWriteByte(byte b
);
683 void SlGlobList(const SaveLoadGlobVarList
*sldg
);
684 void SlArray(void *array
, size_t length
, VarType conv
);
685 void SlObject(void *object
, const SaveLoad
*sld
);
686 bool SlObjectMember(void *object
, const SaveLoad
*sld
);
687 void NORETURN
SlError(StringID string
, const char *extra_msg
= NULL
);
688 void NORETURN
SlErrorCorrupt(const char *msg
);
690 bool SaveloadCrashWithMissingNewGRFs();
692 extern char _savegame_format
[8];
693 extern bool _do_autosave
;
695 #endif /* SAVELOAD_H */