Invalidate the right goal list when changing goals
[openttd/fttd.git] / src / saveload / saveload_data.h
blob26cc11b1c5d2e0d30bb13019af059250176cd2f0
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_data.h Saveload data declarations. */
12 #ifndef SAVELOAD_DATA_H
13 #define SAVELOAD_DATA_H
15 #include "../core/bitmath_func.hpp"
17 /** Types of save games. */
18 enum SavegameType {
19 SGT_TTO, ///< TTO savegame
20 SGT_TTD, ///< TTD savegame (can be detected incorrectly)
21 SGT_TTDP1, ///< TTDP savegame ( -//- ) (data at NW border)
22 SGT_TTDP2, ///< TTDP savegame in new format (data at SE border)
23 SGT_OTTD, ///< OTTD savegame
24 SGT_FTTD, ///< FTTD savegame
25 SGT_INVALID = 0xFF, ///< broken savegame (used internally)
28 /** Type and version of a savegame. */
29 struct SavegameTypeVersion {
30 SavegameType type; ///< type of savegame
31 union {
32 struct {
33 uint version; ///< version of TTDP savegame (if applicable)
34 } ttdp;
35 struct {
36 uint version; ///< the major savegame version
37 uint minor_version; ///< the minor savegame version
38 } ottd;
39 struct {
40 uint version; ///< savegame version
41 } fttd;
45 /**
46 * Checks whether the savegame version is older than a given version.
47 * @param stv Savegame version to check.
48 * @param version Version to check against.
49 * @param major Major number of the version to check against, for legacy savegames.
50 * @param minor Minor number of the version to check against, ignored if 0, for legacy savegames.
51 * @return Savegame version is earlier than the specified version.
53 static inline bool IsFullSavegameVersionBefore(const SavegameTypeVersion *stv, uint version, uint major = UINT_MAX, uint minor = 0)
55 switch (stv->type) {
56 default: return major > 0;
57 case SGT_OTTD: return stv->ottd.version < major || (minor > 0 && stv->ottd.version == major && stv->ottd.minor_version < minor);
58 case SGT_FTTD: return stv->fttd.version < version;
62 /**
63 * Checks whether the savegame version is legacy and older than a given version.
64 * @param stv Savegame version to check.
65 * @param major Major number of the version to check against.
66 * @param minor Minor number of the version to check against, ignored if 0.
67 * @return Savegame version is earlier than the specified version.
69 static inline bool IsOTTDSavegameVersionBefore(const SavegameTypeVersion *stv, uint16 major, byte minor = 0)
71 return IsFullSavegameVersionBefore(stv, 0, major, minor);
75 /** Type of data saved. */
76 enum SaveLoadTypes {
77 SL_VAR = 0, ///< Save/load a variable.
78 SL_REF = 1, ///< Save/load a reference.
79 SL_ARR = 2, ///< Save/load an array.
80 SL_STR = 3, ///< Save/load a string.
81 SL_LST = 4, ///< Save/load a list.
82 /* non-normal save-load types */
83 SL_WRITEBYTE = 8,
84 SL_INCLUDE = 9,
85 SL_END = 15
88 typedef byte SaveLoadType; ///< Save/load type. @see SaveLoadTypes
90 /**
91 * VarTypes is the general bitmasked magic type that tells us
92 * certain characteristics about the variable it refers to. For example
93 * SLE_FILE_* gives the size(type) as it would be in the savegame and
94 * SLE_VAR_* the size(type) as it is in memory during runtime. These are
95 * the first 8 bits (0-3 SLE_FILE, 4-7 SLE_VAR).
96 * Bits 8-15 are reserved for various flags as explained below
98 enum VarTypes {
99 /* 4 bits allocated for a maximum of 16 types for NumberType */
100 SLE_FILE_I8 = 0,
101 SLE_FILE_U8 = 1,
102 SLE_FILE_I16 = 2,
103 SLE_FILE_U16 = 3,
104 SLE_FILE_I32 = 4,
105 SLE_FILE_U32 = 5,
106 SLE_FILE_I64 = 6,
107 SLE_FILE_U64 = 7,
108 SLE_FILE_STRINGID = 8, ///< StringID offset into strings-array
109 /* 7 more possible file-primitives */
111 /* 4 bits allocated for a maximum of 16 types for NumberType */
112 SLE_VAR_BL = 0 << 4,
113 SLE_VAR_I8 = 1 << 4,
114 SLE_VAR_U8 = 2 << 4,
115 SLE_VAR_I16 = 3 << 4,
116 SLE_VAR_U16 = 4 << 4,
117 SLE_VAR_I32 = 5 << 4,
118 SLE_VAR_U32 = 6 << 4,
119 SLE_VAR_I64 = 7 << 4,
120 SLE_VAR_U64 = 8 << 4,
121 SLE_VAR_NULL = 9 << 4, ///< useful to write zeros in savegame.
122 SLE_VAR_NAME = 10 << 4, ///< old custom name to be converted to a char pointer
123 /* 5 more possible memory-primitives */
125 /* Shortcut values */
126 SLE_VAR_CHAR = SLE_VAR_I8,
128 /* Default combinations of variables. As savegames change, so can variables
129 * and thus it is possible that the saved value and internal size do not
130 * match and you need to specify custom combo. The defaults are listed here */
131 SLE_BOOL = SLE_FILE_I8 | SLE_VAR_BL,
132 SLE_INT8 = SLE_FILE_I8 | SLE_VAR_I8,
133 SLE_UINT8 = SLE_FILE_U8 | SLE_VAR_U8,
134 SLE_INT16 = SLE_FILE_I16 | SLE_VAR_I16,
135 SLE_UINT16 = SLE_FILE_U16 | SLE_VAR_U16,
136 SLE_INT32 = SLE_FILE_I32 | SLE_VAR_I32,
137 SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32,
138 SLE_INT64 = SLE_FILE_I64 | SLE_VAR_I64,
139 SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64,
140 SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR,
141 SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U16,
142 SLE_NAME = SLE_FILE_STRINGID | SLE_VAR_NAME,
144 /* Shortcut values */
145 SLE_UINT = SLE_UINT32,
146 SLE_INT = SLE_INT32,
149 typedef byte VarType;
152 * StrTypes encodes information about saving and loading of strings (#SLE_STR).
154 enum StrTypes {
155 SLS_QUOTED = 1 << 0, ///< string is enclosed in quotes
156 SLS_POINTER = 1 << 1, ///< string is stored as a pointer (as opposed to a fixed buffer)
158 SLS_STRB = 0, ///< string (with pre-allocated buffer)
159 SLS_STRBQ = SLS_QUOTED, ///< string enclosed in quotes (with pre-allocated buffer)
160 SLS_STR = SLS_POINTER, ///< string pointer
161 SLS_STRQ = SLS_POINTER | SLS_QUOTED, ///< string pointer enclosed in quotes
163 SLS_ALLOW_CONTROL = 1 << 2, ///< allow control codes in the string
164 SLS_ALLOW_NEWLINE = 1 << 3, ///< allow newlines in the string
165 /* 4 more possible flags */
168 typedef byte StrType;
170 /** Type of reference (#SLE_REF, #SLE_CONDREF). */
171 enum SLRefType {
172 REF_ORDER = 0, ///< Load/save a reference to an order.
173 REF_VEHICLE = 1, ///< Load/save a reference to a vehicle.
174 REF_STATION = 2, ///< Load/save a reference to a station.
175 REF_TOWN = 3, ///< Load/save a reference to a town.
176 REF_VEHICLE_OLD = 4, ///< Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
177 REF_ROADSTOPS = 5, ///< Load/save a reference to a bus/truck stop.
178 REF_DOCKS = 6, ///< Load/save a reference to a dock.
179 REF_ENGINE_RENEWS = 7, ///< Load/save a reference to an engine renewal (autoreplace).
180 REF_CARGO_PACKET = 8, ///< Load/save a reference to a cargo packet.
181 REF_ORDERLIST = 9, ///< Load/save a reference to an orderlist.
182 REF_STORAGE = 10, ///< Load/save a reference to a persistent storage.
183 REF_LINK_GRAPH = 11, ///< Load/save a reference to a link graph.
184 REF_LINK_GRAPH_JOB = 12, ///< Load/save a reference to a link graph job.
187 /** Flags directing saving/loading of a variable */
188 enum SaveLoadFlags {
189 SLF_GLOBAL = 1 << 0, ///< global variable, instead of a struct field
190 SLF_NOT_IN_SAVE = 1 << 1, ///< do not save with savegame, basically client-based
191 SLF_NOT_IN_CONFIG = 1 << 2, ///< do not save to config file
192 SLF_NO_NETWORK_SYNC = 1 << 3, ///< do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
195 /** Version range for SaveLoad objects */
196 struct SaveLoadRange {
197 uint16 from; ///< save/load the variable starting from this savegame version
198 uint16 to; ///< save/load the variable until this savegame version
201 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
202 struct SaveLoad {
203 SaveLoadType type; ///< object type
204 byte conv; ///< object subtype/conversion
205 byte flags; ///< save/load flags
206 uint16 length; ///< (conditional) length of the variable (eg. arrays) (max array size is 65536 elements)
207 SaveLoadRange version; ///< save/load the variable in this version range
208 SaveLoadRange legacy; ///< save/load the variable in this legacy version range
209 /* NOTE: This element either denotes the address of the variable for a global
210 * variable, or the offset within a struct which is then bound to a variable
211 * during runtime. Decision on which one to use is controlled by the function
212 * that is called to save it. address: global=true, offset: global=false.
213 * For SL_INCLUDE, this points to the SaveLoad object to be included. */
214 void *address; ///< address of variable OR offset of variable in the struct (max offset is 65536)
217 /** Highest possible savegame version. */
218 #define SL_MAX_VERSION UINT16_MAX
220 /** Workaround for MSVC broken variadic macro support. */
221 #define SLE_EXPAND(x) x
223 /** Substitute first paramenter if non-empty, else second. */
224 #define SLE_DEFAULT_IF_EMPTY(value, default) ((sizeof(#value) != 1) ? (value + 0) : (default))
227 * Generic SaveLoad object, with version range and legacy version range.
228 * @param type Load/save type. @see SaveLoadType
229 * @param address Address of variable or offset of variable in the struct
230 * @param conv Subtype/conversion of the data between memory and savegame.
231 * @param flags Save/load flags
232 * @param length Length of object (for arrays and strings)
233 * @param from First savegame version that has the field.
234 * @param to Last savegame version that has the field, empty for maximum possible value.
235 * @param lfrom First legacy savegame version that has the field.
236 * @param lto Last legacy savegame version that has the field, empty for maximum possible value.
237 * @note This macro should not be used directly.
239 #define SLE_ANY_2(type, address, conv, flags, length, from, to, lfrom, lto) {type, conv, flags, length, {SLE_DEFAULT_IF_EMPTY(from, SL_MAX_VERSION), SLE_DEFAULT_IF_EMPTY(to, SL_MAX_VERSION)}, {lfrom, SLE_DEFAULT_IF_EMPTY(lto, SL_MAX_VERSION)}, address}
242 * Generic SaveLoad object, with version range.
243 * @param type Load/save type. @see SaveLoadType
244 * @param address Address of variable or offset of variable in the struct
245 * @param conv Subtype/conversion of the data between memory and savegame.
246 * @param flags Save/load flags
247 * @param length Length of object (for arrays and strings)
248 * @param from First savegame version that has the field.
249 * @param to Last savegame version that has the field, empty for maximum possible value.
250 * @note This macro should not be used directly.
252 #define SLE_ANY_1(type, address, conv, flags, length, from, to, ...) SLE_ANY_2(type, address, conv, flags, length, from, to, SL_MAX_VERSION, 0)
255 * Generic SaveLoad object, without version range.
256 * @param type Load/save type. @see SaveLoadType
257 * @param address Address of variable or offset of variable in the struct
258 * @param conv Subtype/conversion of the data between memory and savegame.
259 * @param flags Save/load flags
260 * @param length Length of object (for arrays and strings)
261 * @note This macro should not be used directly.
263 #define SLE_ANY_0(type, address, conv, flags, length, ...) SLE_ANY_2(type, address, conv, flags, length, 0, , 0, )
266 * Generic SaveLoad object, with or without version range.
267 * @param type Load/save type. @see SaveLoadType
268 * @param address Address of variable or offset of variable in the struct
269 * @param conv Subtype/conversion of the data between memory and savegame.
270 * @param flags Save/load flags
271 * @param length Length of object (for arrays and strings)
272 * @param from First savegame version that has the field (optional).
273 * @param to Last savegame version that has the field (optional).
274 * @param lfrom First legacy savegame version that has the field (optional).
275 * @param lto Last legacy savegame version that has the field (optional).
276 * @note Both savegame version interval endpoints must be present for any given range.
277 * @note There should be a trailing empty argument to this macro.
278 * @note This macro should not be used directly.
280 #define SLE_ANY_(type, address, conv, flags, ...) SLE_EXPAND(SLE_ANY(type, address, conv, flags, __VA_ARGS__ 2, INVALID, 1, INVALID, 0, INVALID, ))
281 #define SLE_ANY(type, address, conv, flags, length, from, to, lfrom, lto, n, ...) SLE_ANY_##n(type, address, conv, flags, length, from, to, lfrom, lto)
284 * Storage of simple variables, references (pointers), and arrays.
285 * @param type Load/save type. @see SaveLoadType
286 * @param base Name of the class or struct containing the variable.
287 * @param variable Name of the variable in the class or struct referenced by \a base.
288 * @param conv Subtype/conversion of the data between memory and savegame.
289 * @param flags Save/load flags
290 * @param length Length of object (for arrays and strings)
291 * @param from First savegame version that has the field (optional).
292 * @param to Last savegame version that has the field (optional).
293 * @param lfrom First legacy savegame version that has the field (optional).
294 * @param lto Last legacy savegame version that has the field (optional).
295 * @note Both savegame version interval endpoints must be present for any given range.
296 * @note In general, it is better to use one of the SLE_* macros below.
298 #define SLE_GENERAL(...) SLE_EXPAND(SLE_GENERAL_(__VA_ARGS__, ))
299 #define SLE_GENERAL_(type, base, variable, conv, flags, length, ...) SLE_EXPAND(SLE_ANY_(type, ((void*)cpp_offsetof(base, variable)), conv, flags, length, __VA_ARGS__))
302 * Storage of a variable.
303 * @param base Name of the class or struct containing the variable.
304 * @param variable Name of the variable in the class or struct referenced by \a base.
305 * @param conv Storage of the data in memory and in the savegame.
306 * @param from First savegame version that has the field (optional).
307 * @param to Last savegame version that has the field (optional).
308 * @param lfrom First legacy savegame version that has the field (optional).
309 * @param lto Last legacy savegame version that has the field (optional).
310 * @note Both savegame version interval endpoints must be present for any given range.
312 #define SLE_VAR(...) SLE_EXPAND(SLE_VAR_(__VA_ARGS__, ))
313 #define SLE_VAR_(base, variable, conv, ...) SLE_EXPAND(SLE_GENERAL_(SL_VAR, base, variable, conv, 0, 0, __VA_ARGS__))
316 * Storage of a reference.
317 * @param base Name of the class or struct containing the variable.
318 * @param variable Name of the variable in the class or struct referenced by \a base.
319 * @param reftype Type of the reference, a value from #SLRefType.
320 * @param from First savegame version that has the field (optional).
321 * @param to Last savegame version that has the field (optional).
322 * @param lfrom First legacy savegame version that has the field (optional).
323 * @param lto Last legacy savegame version that has the field (optional).
324 * @note Both savegame version interval endpoints must be present for any given range.
326 #define SLE_REF(...) SLE_EXPAND(SLE_REF_(__VA_ARGS__, ))
327 #define SLE_REF_(base, variable, reftype, ...) SLE_EXPAND(SLE_GENERAL_(SL_REF, base, variable, reftype, 0, 0, __VA_ARGS__))
330 * Storage of an array.
331 * @param base Name of the class or struct containing the array.
332 * @param variable Name of the variable in the class or struct referenced by \a base.
333 * @param conv Storage of the data in memory and in the savegame.
334 * @param length Number of elements in the array.
335 * @param from First savegame version that has the array (optional).
336 * @param to Last savegame version that has the array (optional).
337 * @param lfrom First legacy savegame version that has the array (optional).
338 * @param lto Last legacy savegame version that has the array (optional).
339 * @note Both savegame version interval endpoints must be present for any given range.
341 #define SLE_ARR(...) SLE_EXPAND(SLE_ARR_(__VA_ARGS__, ))
342 #define SLE_ARR_(base, variable, conv, length, ...) SLE_EXPAND(SLE_GENERAL_(SL_ARR, base, variable, conv, 0, length, __VA_ARGS__))
345 * Storage of a string.
346 * @param base Name of the class or struct containing the string.
347 * @param variable Name of the variable in the class or struct referenced by \a base.
348 * @param conv Storage of the data in memory and in the savegame.
349 * @param length Number of elements in the string (only used for fixed size buffers).
350 * @param from First savegame version that has the string (optional).
351 * @param to Last savegame version that has the string (optional).
352 * @param lfrom First legacy savegame version that has the string (optional).
353 * @param lto Last legacy savegame version that has the string (optional).
354 * @note Both savegame version interval endpoints must be present for any given range.
356 #define SLE_STR(...) SLE_EXPAND(SLE_STR_(__VA_ARGS__, ))
357 #define SLE_STR_(base, variable, conv, length, ...) SLE_EXPAND(SLE_GENERAL_(SL_STR, base, variable, conv, 0, length, __VA_ARGS__))
360 * Storage of a list.
361 * @param base Name of the class or struct containing the list.
362 * @param variable Name of the variable in the class or struct referenced by \a base.
363 * @param reftype Type of the reference, a value from #SLRefType.
364 * @param from First savegame version that has the list (optional).
365 * @param to Last savegame version that has the list (optional).
366 * @param lfrom First legacy savegame version that has the list (optional).
367 * @param lto Last legacy savegame version that has the list (optional).
368 * @note Both savegame version interval endpoints must be present for any given range.
370 #define SLE_LST(...) SLE_EXPAND(SLE_LST_(__VA_ARGS__, ))
371 #define SLE_LST_(base, variable, reftype, ...) SLE_EXPAND(SLE_GENERAL_(SL_LST, base, variable, reftype, 0, 0, __VA_ARGS__))
374 * Empty space.
375 * @param length Length of the empty space.
376 * @param from First savegame version that has the empty space (optional).
377 * @param to Last savegame version that has the empty space (optional).
378 * @param lfrom First legacy savegame version that has the empty space (optional).
379 * @param lto Last legacy savegame version that has the empty space (optional).
380 * @note Both savegame version interval endpoints must be present for any given range.
382 #define SLE_NULL(...) SLE_EXPAND(SLE_NULL_(__VA_ARGS__, ))
383 #define SLE_NULL_(length, ...) SLE_EXPAND(SLE_ANY_(SL_ARR, (void*)NULL, SLE_FILE_U8 | SLE_VAR_NULL, SLF_NOT_IN_CONFIG, length, __VA_ARGS__))
385 /** Translate values ingame to different values in the savegame and vv. */
386 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, value, 0, 0)
388 /** Include another SaveLoad object. */
389 #define SLE_INCLUDE(include) {SL_INCLUDE, 0, 0, 0, {0, SL_MAX_VERSION}, {0, SL_MAX_VERSION}, const_cast<SaveLoad *>(include)}
391 /** End marker of a struct/class save or load. */
392 #define SLE_END() {SL_END, 0, 0, 0, {0, 0}, {0, 0}, NULL}
395 * Storage of global simple variables, references (pointers), and arrays.
396 * @param type Load/save type. @see SaveLoadType
397 * @param variable Name of the global variable.
398 * @param conv Subtype/conversion of the data between memory and savegame.
399 * @param flags Save/load flags
400 * @param length Length of object (for arrays and strings)
401 * @param from First savegame version that has the field (optional).
402 * @param to Last savegame version that has the field (optional).
403 * @param lfrom First legacy savegame version that has the field (optional).
404 * @param lto Last legacy savegame version that has the field (optional).
405 * @note Both savegame version interval endpoints must be present for any given range.
406 * @note In general, it is better to use one of the SLEG_* macros below.
408 #define SLEG_GENERAL(...) SLE_EXPAND(SLEG_GENERAL_(__VA_ARGS__, ))
409 #define SLEG_GENERAL_(type, variable, conv, flags, length, ...) SLE_EXPAND(SLE_ANY_(type, (void*)&variable, conv, (flags) | SLF_GLOBAL, length, __VA_ARGS__))
412 * Storage of a global variable.
413 * @param variable Name of the global variable.
414 * @param conv Storage of the data in memory and in the savegame.
415 * @param from First savegame version that has the field (optional).
416 * @param to Last savegame version that has the field (optional).
417 * @param lfrom First legacy savegame version that has the field (optional).
418 * @param lto Last legacy savegame version that has the field (optional).
419 * @note Both savegame version interval endpoints must be present for any given range.
421 #define SLEG_VAR(...) SLE_EXPAND(SLEG_VAR_(__VA_ARGS__, ))
422 #define SLEG_VAR_(variable, conv, ...) SLE_EXPAND(SLEG_GENERAL_(SL_VAR, variable, conv, 0, 0, __VA_ARGS__))
425 * Storage of a global reference.
426 * @param variable Name of the global variable.
427 * @param reftype Type of the reference, a value from #SLRefType.
428 * @param from First savegame version that has the field (optional).
429 * @param to Last savegame version that has the field (optional).
430 * @param lfrom First legacy savegame version that has the field (optional).
431 * @param lto Last legacy savegame version that has the field (optional).
432 * @note Both savegame version interval endpoints must be present for any given range.
434 #define SLEG_REF(...) SLE_EXPAND(SLEG_REF_(__VA_ARGS__, ))
435 #define SLEG_REF_(variable, reftype, ...) SLE_EXPAND(SLEG_GENERAL_(SL_REF, variable, reftype, 0, 0, __VA_ARGS__))
438 * Storage of a global array.
439 * @param variable Name of the global variable.
440 * @param conv Storage of the data in memory and in the savegame.
441 * @param length Number of elements in the array.
442 * @param from First savegame version that has the array (optional).
443 * @param to Last savegame version that has the array (optional).
444 * @param lfrom First legacy savegame version that has the array (optional).
445 * @param lto Last legacy savegame version that has the array (optional).
446 * @note Both savegame version interval endpoints must be present for any given range.
448 #define SLEG_ARR(...) SLE_EXPAND(SLEG_ARR_(__VA_ARGS__, ))
449 #define SLEG_ARR_(variable, conv, length, ...) SLE_EXPAND(SLEG_GENERAL_(SL_ARR, variable, conv, 0, length, __VA_ARGS__))
452 * Storage of a global string.
453 * @param variable Name of the global variable.
454 * @param conv Storage of the data in memory and in the savegame.
455 * @param length Number of elements in the string (only used for fixed size buffers).
456 * @param from First savegame version that has the string (optional).
457 * @param to Last savegame version that has the string (optional).
458 * @param lfrom First legacy savegame version that has the string (optional).
459 * @param lto Last legacy savegame version that has the string (optional).
460 * @note Both savegame version interval endpoints must be present for any given range.
462 #define SLEG_STR(...) SLE_EXPAND(SLEG_STR_(__VA_ARGS__, ))
463 #define SLEG_STR_(variable, conv, length, ...) SLE_EXPAND(SLEG_GENERAL_(SL_STR, variable, conv, 0, length, __VA_ARGS__))
466 * Storage of a global list.
467 * @param variable Name of the global variable.
468 * @param reftype Type of the reference, a value from #SLRefType.
469 * @param from First savegame version that has the list (optional).
470 * @param to Last savegame version that has the list (optional).
471 * @param lfrom First legacy savegame version that has the list (optional).
472 * @param lto Last legacy savegame version that has the list (optional).
473 * @note Both savegame version interval endpoints must be present for any given range.
475 #define SLEG_LST(...) SLE_EXPAND(SLEG_LST_(__VA_ARGS__, ))
476 #define SLEG_LST_(variable, reftype, ...) SLE_EXPAND(SLEG_GENERAL_(SL_LST, variable, reftype, 0, 0, __VA_ARGS__))
479 * Get the NumberType of a setting. This describes the integer type
480 * as it is represented in memory
481 * @param type VarType holding information about the variable-type
482 * @return return the SLE_VAR_* part of a variable-type description
484 static inline VarType GetVarMemType(VarType type)
486 return type & 0xF0; // GB(type, 4, 4) << 4;
490 * Get the #FileType of a setting. This describes the integer type
491 * as it is represented in a savegame/file
492 * @param type VarType holding information about the file-type
493 * @param return the SLE_FILE_* part of a variable-type description
495 static inline VarType GetVarFileType(VarType type)
497 return type & 0xF; // GB(type, 0, 4);
501 * Check if the given saveload type is a numeric type.
502 * @param conv the type to check
503 * @return True if it's a numeric type.
505 static inline bool IsNumericType(VarType conv)
507 return GetVarMemType(conv) <= SLE_VAR_U64;
511 * Return the size in bytes of a certain type of normal/atomic variable
512 * as it appears in memory. See VarTypes
513 * @param conv VarType type of variable that is used for calculating the size
514 * @return Return the size of this type in bytes
516 static inline uint SlCalcConvMemLen(VarType conv)
518 extern const byte _conv_mem_size[];
519 byte length = GB(conv, 4, 4);
520 assert(length <= SLE_VAR_NULL);
521 return _conv_mem_size[length];
525 * Return the size in bytes of a certain type of normal/atomic variable
526 * as it appears in a saved game. See VarTypes
527 * @param conv VarType type of variable that is used for calculating the size
528 * @return Return the size of this type in bytes
530 static inline byte SlCalcConvFileLen(VarType conv)
532 extern const byte _conv_file_size[];
533 byte length = GB(conv, 0, 4);
534 assert(length <= SLE_FILE_STRINGID);
535 return _conv_file_size[length];
538 int64 ReadValue(const void *ptr, VarType conv);
539 void WriteValue(void *ptr, VarType conv, int64 val);
542 * Get the address of the variable. Which one to pick depends on the object
543 * pointer. If it is NULL we are dealing with global variables so the address
544 * is taken. If non-null only the offset is stored in the union and we need
545 * to add this to the address of the object
547 static inline void *GetVariableAddress(const SaveLoad *sld, void *object = NULL)
549 return (sld->flags & SLF_GLOBAL) ? sld->address : ((byte*)object + (ptrdiff_t)sld->address);
552 static inline const void *GetVariableAddress(const SaveLoad *sld, const void *object)
554 return GetVariableAddress(sld, const_cast<void *>(object));
557 /** Is this object valid in a certain savegame version? */
558 static inline bool SlIsObjectValidInSavegame(const SavegameTypeVersion *stv, const SaveLoad *sld)
560 switch (stv->type) {
561 default:
562 if (0 < sld->legacy.from) return false;
563 break;
565 case SGT_OTTD:
566 if (stv->ottd.version < sld->legacy.from || stv->ottd.version > sld->legacy.to) return false;
567 break;
569 case SGT_FTTD:
570 if (stv->fttd.version < sld->version.from || stv->fttd.version > sld->version.to) return false;
571 break;
574 if (sld->flags & SLF_NOT_IN_SAVE) return false;
576 return true;
580 * Checks if a SaveLoad object is active in the current savegame version.
581 * @param sld SaveLoad object to check
582 * @return Whether the object is active in the current savegame version.
584 static inline bool SlIsObjectCurrentlyValid(const SaveLoad *sld)
586 extern const uint16 SAVEGAME_VERSION;
587 if (SAVEGAME_VERSION < sld->version.from || SAVEGAME_VERSION > sld->version.to) return false;
589 return true;
592 size_t SlCalcObjLength(const void *object, const SaveLoad *sld);
594 void SlObjectPtrs(void *object, const SaveLoad *sld, const SavegameTypeVersion *stv);
596 #endif /* SAVELOAD_DATA_H */