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.
95 /** Highest possible savegame version. */
96 #define SL_MAX_VERSION UINT16_MAX
98 /** Flags of a chunk. */
104 CH_LAST
= 8, ///< Last chunk in this array.
109 * VarTypes is the general bitmasked magic type that tells us
110 * certain characteristics about the variable it refers to. For example
111 * SLE_FILE_* gives the size(type) as it would be in the savegame and
112 * SLE_VAR_* the size(type) as it is in memory during runtime. These are
113 * the first 8 bits (0-3 SLE_FILE, 4-7 SLE_VAR).
114 * Bits 8-15 are reserved for various flags as explained below
117 /* 4 bits allocated a maximum of 16 types for NumberType */
126 SLE_FILE_STRINGID
= 8, ///< StringID offset into strings-array
128 /* 6 more possible file-primitives */
130 /* 4 bits allocated a maximum of 16 types for NumberType */
134 SLE_VAR_I16
= 3 << 4,
135 SLE_VAR_U16
= 4 << 4,
136 SLE_VAR_I32
= 5 << 4,
137 SLE_VAR_U32
= 6 << 4,
138 SLE_VAR_I64
= 7 << 4,
139 SLE_VAR_U64
= 8 << 4,
140 SLE_VAR_NULL
= 9 << 4, ///< useful to write zeros in savegame.
141 SLE_VAR_STRB
= 10 << 4, ///< string (with pre-allocated buffer)
142 SLE_VAR_STRBQ
= 11 << 4, ///< string enclosed in quotes (with pre-allocated buffer)
143 SLE_VAR_STR
= 12 << 4, ///< string pointer
144 SLE_VAR_STRQ
= 13 << 4, ///< string pointer enclosed in quotes
145 SLE_VAR_NAME
= 14 << 4, ///< old custom name to be converted to a char pointer
146 /* 1 more possible memory-primitives */
148 /* Shortcut values */
149 SLE_VAR_CHAR
= SLE_VAR_I8
,
151 /* Default combinations of variables. As savegames change, so can variables
152 * and thus it is possible that the saved value and internal size do not
153 * match and you need to specify custom combo. The defaults are listed here */
154 SLE_BOOL
= SLE_FILE_I8
| SLE_VAR_BL
,
155 SLE_INT8
= SLE_FILE_I8
| SLE_VAR_I8
,
156 SLE_UINT8
= SLE_FILE_U8
| SLE_VAR_U8
,
157 SLE_INT16
= SLE_FILE_I16
| SLE_VAR_I16
,
158 SLE_UINT16
= SLE_FILE_U16
| SLE_VAR_U16
,
159 SLE_INT32
= SLE_FILE_I32
| SLE_VAR_I32
,
160 SLE_UINT32
= SLE_FILE_U32
| SLE_VAR_U32
,
161 SLE_INT64
= SLE_FILE_I64
| SLE_VAR_I64
,
162 SLE_UINT64
= SLE_FILE_U64
| SLE_VAR_U64
,
163 SLE_CHAR
= SLE_FILE_I8
| SLE_VAR_CHAR
,
164 SLE_STRINGID
= SLE_FILE_STRINGID
| SLE_VAR_U32
,
165 SLE_STRINGBUF
= SLE_FILE_STRING
| SLE_VAR_STRB
,
166 SLE_STRINGBQUOTE
= SLE_FILE_STRING
| SLE_VAR_STRBQ
,
167 SLE_STRING
= SLE_FILE_STRING
| SLE_VAR_STR
,
168 SLE_STRINGQUOTE
= SLE_FILE_STRING
| SLE_VAR_STRQ
,
169 SLE_NAME
= SLE_FILE_STRINGID
| SLE_VAR_NAME
,
171 /* Shortcut values */
172 SLE_UINT
= SLE_UINT32
,
174 SLE_STRB
= SLE_STRINGBUF
,
175 SLE_STRBQ
= SLE_STRINGBQUOTE
,
176 SLE_STR
= SLE_STRING
,
177 SLE_STRQ
= SLE_STRINGQUOTE
,
179 /* 8 bits allocated for a maximum of 8 flags
180 * Flags directing saving/loading of a variable */
181 SLF_NOT_IN_SAVE
= 1 << 8, ///< do not save with savegame, basically client-based
182 SLF_NOT_IN_CONFIG
= 1 << 9, ///< do not save to config file
183 SLF_NO_NETWORK_SYNC
= 1 << 10, ///< do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
184 SLF_ALLOW_CONTROL
= 1 << 11, ///< allow control codes in the strings
185 SLF_ALLOW_NEWLINE
= 1 << 12, ///< allow new lines in the strings
186 /* 3 more possible flags */
189 typedef uint32 VarType
;
191 /** Type of data saved. */
193 SL_VAR
= 0, ///< Save/load a variable.
194 SL_REF
= 1, ///< Save/load a reference.
195 SL_ARR
= 2, ///< Save/load an array.
196 SL_STR
= 3, ///< Save/load a string.
197 SL_LST
= 4, ///< Save/load a list.
198 /* non-normal save-load types */
205 typedef byte SaveLoadType
; ///< Save/load type. @see SaveLoadTypes
207 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
209 bool global
; ///< should we load a global variable or a non-global one
210 SaveLoadType cmd
; ///< the action to take with the saved/loaded type, All types need different action
211 VarType conv
; ///< type of the variable to be saved, int
212 uint16 length
; ///< (conditional) length of the variable (eg. arrays) (max array size is 65536 elements)
213 uint16 version_from
; ///< save/load the variable starting from this savegame version
214 uint16 version_to
; ///< save/load the variable until this savegame version
215 /* NOTE: This element either denotes the address of the variable for a global
216 * variable, or the offset within a struct which is then bound to a variable
217 * during runtime. Decision on which one to use is controlled by the function
218 * that is called to save it. address: global=true, offset: global=false */
219 void *address
; ///< address of variable OR offset of variable in the struct (max offset is 65536)
220 size_t size
; ///< the sizeof size.
223 /** Same as #SaveLoad but global variables are used (for better readability); */
224 typedef SaveLoad SaveLoadGlobVarList
;
227 * Storage of simple variables, references (pointers), and arrays.
228 * @param cmd Load/save type. @see SaveLoadType
229 * @param base Name of the class or struct containing the variable.
230 * @param variable Name of the variable in the class or struct referenced by \a base.
231 * @param type Storage of the data in memory and in the savegame.
232 * @param from First savegame version that has the field.
233 * @param to Last savegame version that has the field.
234 * @note In general, it is better to use one of the SLE_* macros below.
236 #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)}
239 * Storage of a variable in some savegame versions.
240 * @param base Name of the class or struct containing the variable.
241 * @param variable Name of the variable in the class or struct referenced by \a base.
242 * @param type Storage of the data in memory and in the savegame.
243 * @param from First savegame version that has the field.
244 * @param to Last savegame version that has the field.
246 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
249 * Storage of a reference in some savegame versions.
250 * @param base Name of the class or struct containing the variable.
251 * @param variable Name of the variable in the class or struct referenced by \a base.
252 * @param type Type of the reference, a value from #SLRefType.
253 * @param from First savegame version that has the field.
254 * @param to Last savegame version that has the field.
256 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
259 * Storage of an array in some savegame versions.
260 * @param base Name of the class or struct containing the array.
261 * @param variable Name of the variable in the class or struct referenced by \a base.
262 * @param type Storage of the data in memory and in the savegame.
263 * @param length Number of elements in the array.
264 * @param from First savegame version that has the array.
265 * @param to Last savegame version that has the array.
267 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
270 * Storage of a string in some savegame versions.
271 * @param base Name of the class or struct containing the string.
272 * @param variable Name of the variable in the class or struct referenced by \a base.
273 * @param type Storage of the data in memory and in the savegame.
274 * @param length Number of elements in the string (only used for fixed size buffers).
275 * @param from First savegame version that has the string.
276 * @param to Last savegame version that has the string.
278 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
281 * Storage of a list in some savegame versions.
282 * @param base Name of the class or struct containing the list.
283 * @param variable Name of the variable in the class or struct referenced by \a base.
284 * @param type Storage of the data in memory and in the savegame.
285 * @param from First savegame version that has the list.
286 * @param to Last savegame version that has the list.
288 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
291 * Storage of a variable in every version of a savegame.
292 * @param base Name of the class or struct containing the variable.
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.
296 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
299 * Storage of a reference in every version of a savegame.
300 * @param base Name of the class or struct containing the variable.
301 * @param variable Name of the variable in the class or struct referenced by \a base.
302 * @param type Type of the reference, a value from #SLRefType.
304 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
307 * Storage of an array in every version of a savegame.
308 * @param base Name of the class or struct containing the array.
309 * @param variable Name of the variable in the class or struct referenced by \a base.
310 * @param type Storage of the data in memory and in the savegame.
311 * @param length Number of elements in the array.
313 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
316 * Storage of a string in every savegame version.
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 length Number of elements in the string (only used for fixed size buffers).
322 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
325 * Storage of a list in every savegame version.
326 * @param base Name of the class or struct containing the list.
327 * @param variable Name of the variable in the class or struct referenced by \a base.
328 * @param type Storage of the data in memory and in the savegame.
330 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
333 * Empty space in every savegame version.
334 * @param length Length of the empty space.
336 #define SLE_NULL(length) SLE_CONDNULL(length, 0, SL_MAX_VERSION)
339 * Empty space in some savegame versions.
340 * @param length Length of the empty space.
341 * @param from First savegame version that has the empty space.
342 * @param to Last savegame version that has the empty space.
344 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)
346 /** Translate values ingame to different values in the savegame and vv. */
347 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
349 #define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
350 #define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
352 /** End marker of a struct/class save or load. */
353 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL, 0}
356 * Storage of global simple variables, references (pointers), and arrays.
357 * @param cmd Load/save type. @see SaveLoadType
358 * @param variable Name of the global variable.
359 * @param type Storage of the data in memory and in the savegame.
360 * @param from First savegame version that has the field.
361 * @param to Last savegame version that has the field.
362 * @note In general, it is better to use one of the SLEG_* macros below.
364 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable, sizeof(variable)}
367 * Storage of a global variable in some savegame versions.
368 * @param variable Name of the global variable.
369 * @param type Storage of the data in memory and in the savegame.
370 * @param from First savegame version that has the field.
371 * @param to Last savegame version that has the field.
373 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
376 * Storage of a global reference in some savegame versions.
377 * @param variable Name of the global variable.
378 * @param type Storage of the data in memory and in the savegame.
379 * @param from First savegame version that has the field.
380 * @param to Last savegame version that has the field.
382 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
385 * Storage of a global array in some savegame versions.
386 * @param variable Name of the global variable.
387 * @param type Storage of the data in memory and in the savegame.
388 * @param length Number of elements in the array.
389 * @param from First savegame version that has the array.
390 * @param to Last savegame version that has the array.
392 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
395 * Storage of a global string in some savegame versions.
396 * @param variable Name of the global variable.
397 * @param type Storage of the data in memory and in the savegame.
398 * @param length Number of elements in the string (only used for fixed size buffers).
399 * @param from First savegame version that has the string.
400 * @param to Last savegame version that has the string.
402 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
405 * Storage of a global list in some savegame versions.
406 * @param variable Name of the global variable.
407 * @param type Storage of the data in memory and in the savegame.
408 * @param from First savegame version that has the list.
409 * @param to Last savegame version that has the list.
411 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
414 * Storage of a global variable in every savegame version.
415 * @param variable Name of the global variable.
416 * @param type Storage of the data in memory and in the savegame.
418 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
421 * Storage of a global reference in every savegame version.
422 * @param variable Name of the global variable.
423 * @param type Storage of the data in memory and in the savegame.
425 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
428 * Storage of a global array in every savegame version.
429 * @param variable Name of the global variable.
430 * @param type Storage of the data in memory and in the savegame.
432 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
435 * Storage of a global string in every savegame version.
436 * @param variable Name of the global variable.
437 * @param type Storage of the data in memory and in the savegame.
439 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
442 * Storage of a global list in every savegame version.
443 * @param variable Name of the global variable.
444 * @param type Storage of the data in memory and in the savegame.
446 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
449 * Empty global space in some savegame versions.
450 * @param length Length of the empty space.
451 * @param from First savegame version that has the empty space.
452 * @param to Last savegame version that has the empty space.
454 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}
456 /** End marker of global variables save or load. */
457 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL, 0}
460 * Checks whether the savegame is below \a major.\a minor.
461 * @param major Major number of the version to check against.
462 * @param minor Minor number of the version to check against. If \a minor is 0 or not specified, only the major number is checked.
463 * @return Savegame version is earlier than the specified version.
465 static inline bool IsSavegameVersionBefore(uint16 major
, byte minor
= 0)
467 extern uint16 _sl_version
;
468 extern byte _sl_minor_version
;
469 return _sl_version
< major
|| (minor
> 0 && _sl_version
== major
&& _sl_minor_version
< minor
);
473 * Checks if some version from/to combination falls within the range of the
474 * active savegame version.
475 * @param version_from Lowest version number that falls within the range.
476 * @param version_to Highest version number that falls within the range.
477 * @return Active savegame version falls within the given range.
479 static inline bool SlIsObjectCurrentlyValid(uint16 version_from
, uint16 version_to
)
481 extern const uint16 SAVEGAME_VERSION
;
482 if (SAVEGAME_VERSION
< version_from
|| SAVEGAME_VERSION
> version_to
) return false;
488 * Get the NumberType of a setting. This describes the integer type
489 * as it is represented in memory
490 * @param type VarType holding information about the variable-type
491 * @return return the SLE_VAR_* part of a variable-type description
493 static inline VarType
GetVarMemType(VarType type
)
495 return type
& 0xF0; // GB(type, 4, 4) << 4;
499 * Get the #FileType of a setting. This describes the integer type
500 * as it is represented in a savegame/file
501 * @param type VarType holding information about the file-type
502 * @param return the SLE_FILE_* part of a variable-type description
504 static inline VarType
GetVarFileType(VarType type
)
506 return type
& 0xF; // GB(type, 0, 4);
510 * Check if the given saveload type is a numeric type.
511 * @param conv the type to check
512 * @return True if it's a numeric type.
514 static inline bool IsNumericType(VarType conv
)
516 return GetVarMemType(conv
) <= SLE_VAR_U64
;
520 * Get the address of the variable. Which one to pick depends on the object
521 * pointer. If it is NULL we are dealing with global variables so the address
522 * is taken. If non-null only the offset is stored in the union and we need
523 * to add this to the address of the object
525 static inline void *GetVariableAddress(const void *object
, const SaveLoad
*sld
)
527 return const_cast<byte
*>((const byte
*)(sld
->global
? NULL
: object
) + (ptrdiff_t)sld
->address
);
530 int64
ReadValue(const void *ptr
, VarType conv
);
531 void WriteValue(void *ptr
, VarType conv
, int64 val
);
533 void SlSetArrayIndex(uint index
);
534 int SlIterateArray();
536 void SlAutolength(AutolengthProc
*proc
, void *arg
);
537 size_t SlGetFieldLength();
538 void SlSetLength(size_t length
);
539 size_t SlCalcObjMemberLength(const void *object
, const SaveLoad
*sld
);
540 size_t SlCalcObjLength(const void *object
, const SaveLoad
*sld
);
543 void SlWriteByte(byte b
);
545 void SlGlobList(const SaveLoadGlobVarList
*sldg
);
546 void SlArray(void *array
, size_t length
, VarType conv
);
547 void SlObject(void *object
, const SaveLoad
*sld
);
548 bool SlObjectMember(void *object
, const SaveLoad
*sld
);
549 void NORETURN
SlError(StringID string
, const char *extra_msg
= NULL
);
550 void NORETURN
SlErrorCorrupt(const char *msg
);
552 bool SaveloadCrashWithMissingNewGRFs();
554 extern char _savegame_format
[8];
555 extern bool _do_autosave
;
557 #endif /* SAVELOAD_H */