2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file saveload.h Functions/types related to saving and loading games. */
13 #include "../fileio_type.h"
15 #include "../strings_type.h"
16 #include "../core/span_type.hpp"
22 * Previous savegame versions, the trunk revision where they were
23 * introduced and the released version that had that particular
25 * Up to savegame version 18 there is a minor version as well.
27 * Older entries keep their original numbering.
29 * Newer entries should use a descriptive labels, numeric version
30 * and PR can be added to comment.
32 * Note that this list must not be reordered.
34 enum SaveLoadVersion
: uint16
{
35 SL_MIN_VERSION
, ///< First savegame version
37 SLV_1
, ///< 1.0 0.1.x, 0.2.x
42 * 4.1 122 0.3.3, 0.3.4
56 SLV_10
, ///< 10.0 2030
57 SLV_11
, /**< 11.0 2033
59 SLV_12
, ///< 12.1 2046
60 SLV_13
, ///< 13.1 2080 0.4.0, 0.4.0.1
61 SLV_14
, ///< 14.0 2441
63 SLV_15
, ///< 15.0 2499
64 SLV_16
, /**< 16.0 2817
66 SLV_17
, /**< 17.0 3212
72 SLV_21
, ///< 21 3472 0.4.x
96 SLV_41
, ///< 41 7348 0.5.x
123 SLV_64
, ///< 64 10006
125 SLV_65
, ///< 65 10210
126 SLV_66
, ///< 66 10211
127 SLV_67
, ///< 67 10236
128 SLV_68
, ///< 68 10266
129 SLV_69
, ///< 69 10319
131 SLV_70
, ///< 70 10541
132 SLV_71
, ///< 71 10567
133 SLV_72
, ///< 72 10601
134 SLV_73
, ///< 73 10903
135 SLV_74
, ///< 74 11030
137 SLV_75
, ///< 75 11107
138 SLV_76
, ///< 76 11139
139 SLV_77
, ///< 77 11172
140 SLV_78
, ///< 78 11176
141 SLV_79
, ///< 79 11188
143 SLV_80
, ///< 80 11228
144 SLV_81
, ///< 81 11244
145 SLV_82
, ///< 82 11410
146 SLV_83
, ///< 83 11589
147 SLV_84
, ///< 84 11822
149 SLV_85
, ///< 85 11874
150 SLV_86
, ///< 86 12042
151 SLV_87
, ///< 87 12129
152 SLV_88
, ///< 88 12134
153 SLV_89
, ///< 89 12160
155 SLV_90
, ///< 90 12293
156 SLV_91
, ///< 91 12347
157 SLV_92
, ///< 92 12381 0.6.x
158 SLV_93
, ///< 93 12648
159 SLV_94
, ///< 94 12816
161 SLV_95
, ///< 95 12924
162 SLV_96
, ///< 96 13226
163 SLV_97
, ///< 97 13256
164 SLV_98
, ///< 98 13375
165 SLV_99
, ///< 99 13838
167 SLV_100
, ///< 100 13952
168 SLV_101
, ///< 101 14233
169 SLV_102
, ///< 102 14332
170 SLV_103
, ///< 103 14598
171 SLV_104
, ///< 104 14735
173 SLV_105
, ///< 105 14803
174 SLV_106
, ///< 106 14919
175 SLV_107
, ///< 107 15027
176 SLV_108
, ///< 108 15045
177 SLV_109
, ///< 109 15075
179 SLV_110
, ///< 110 15148
180 SLV_111
, ///< 111 15190
181 SLV_112
, ///< 112 15290
182 SLV_113
, ///< 113 15340
183 SLV_114
, ///< 114 15601
185 SLV_115
, ///< 115 15695
186 SLV_116
, ///< 116 15893 0.7.x
187 SLV_117
, ///< 117 16037
188 SLV_118
, ///< 118 16129
189 SLV_119
, ///< 119 16242
191 SLV_120
, ///< 120 16439
192 SLV_121
, ///< 121 16694
193 SLV_122
, ///< 122 16855
194 SLV_123
, ///< 123 16909
195 SLV_124
, ///< 124 16993
197 SLV_125
, ///< 125 17113
198 SLV_126
, ///< 126 17433
199 SLV_127
, ///< 127 17439
200 SLV_128
, ///< 128 18281
201 SLV_129
, ///< 129 18292
203 SLV_130
, ///< 130 18404
204 SLV_131
, ///< 131 18481
205 SLV_132
, ///< 132 18522
206 SLV_133
, ///< 133 18674
207 SLV_134
, ///< 134 18703
209 SLV_135
, ///< 135 18719
210 SLV_136
, ///< 136 18764
211 SLV_137
, ///< 137 18912
212 SLV_138
, ///< 138 18942 1.0.x
213 SLV_139
, ///< 139 19346
215 SLV_140
, ///< 140 19382
216 SLV_141
, ///< 141 19799
217 SLV_142
, ///< 142 20003
218 SLV_143
, ///< 143 20048
219 SLV_144
, ///< 144 20334
221 SLV_145
, ///< 145 20376
222 SLV_146
, ///< 146 20446
223 SLV_147
, ///< 147 20621
224 SLV_148
, ///< 148 20659
225 SLV_149
, ///< 149 20832
227 SLV_150
, ///< 150 20857
228 SLV_151
, ///< 151 20918
229 SLV_152
, ///< 152 21171
230 SLV_153
, ///< 153 21263
231 SLV_154
, ///< 154 21426
233 SLV_155
, ///< 155 21453
234 SLV_156
, ///< 156 21728
235 SLV_157
, ///< 157 21862
236 SLV_158
, ///< 158 21933
237 SLV_159
, ///< 159 21962
239 SLV_160
, ///< 160 21974 1.1.x
240 SLV_161
, ///< 161 22567
241 SLV_162
, ///< 162 22713
242 SLV_163
, ///< 163 22767
243 SLV_164
, ///< 164 23290
245 SLV_165
, ///< 165 23304
246 SLV_166
, ///< 166 23415
247 SLV_167
, ///< 167 23504
248 SLV_168
, ///< 168 23637
249 SLV_169
, ///< 169 23816
251 SLV_170
, ///< 170 23826
252 SLV_171
, ///< 171 23835
253 SLV_172
, ///< 172 23947
254 SLV_173
, ///< 173 23967 1.2.0-RC1
255 SLV_174
, ///< 174 23973 1.2.x
257 SLV_175
, ///< 175 24136
258 SLV_176
, ///< 176 24446
259 SLV_177
, ///< 177 24619
260 SLV_178
, ///< 178 24789
261 SLV_179
, ///< 179 24810
263 SLV_180
, ///< 180 24998 1.3.x
264 SLV_181
, ///< 181 25012
265 SLV_182
, ///< 182 25115 FS#5492, r25259, r25296 Goal status
266 SLV_183
, ///< 183 25363 Cargodist
267 SLV_184
, ///< 184 25508 Unit localisation split
269 SLV_185
, ///< 185 25620 Storybooks
270 SLV_186
, ///< 186 25833 Objects storage
271 SLV_187
, ///< 187 25899 Linkgraph - restricted flows
272 SLV_188
, ///< 188 26169 v1.4 FS#5831 Unify RV travel time
273 SLV_189
, ///< 189 26450 Hierarchical vehicle subgroups
275 SLV_190
, ///< 190 26547 Separate order travel and wait times
276 SLV_191
, ///< 191 26636 FS#6026 Fix disaster vehicle storage (No bump)
277 ///< 191 26646 FS#6041 Linkgraph - store locations
278 SLV_192
, ///< 192 26700 FS#6066 Fix saving of order backups
279 SLV_193
, ///< 193 26802
280 SLV_194
, ///< 194 26881 v1.5
282 SLV_195
, ///< 195 27572 v1.6.1
283 SLV_196
, ///< 196 27778 v1.7
284 SLV_197
, ///< 197 27978 v1.8
285 SLV_198
, ///< 198 PR#6763 Switch town growth rate and counter to actual game ticks
286 SLV_EXTEND_CARGOTYPES
, ///< 199 PR#6802 Extend cargotypes to 64
288 SLV_EXTEND_RAILTYPES
, ///< 200 PR#6805 Extend railtypes to 64, adding uint16 to map array.
289 SLV_EXTEND_PERSISTENT_STORAGE
, ///< 201 PR#6885 Extend NewGRF persistent storages.
290 SLV_EXTEND_INDUSTRY_CARGO_SLOTS
, ///< 202 PR#6867 Increase industry cargo slots to 16 in, 16 out
291 SLV_SHIP_PATH_CACHE
, ///< 203 PR#7072 Add path cache for ships
292 SLV_SHIP_ROTATION
, ///< 204 PR#7065 Add extra rotation stages for ships.
294 SLV_GROUP_LIVERIES
, ///< 205 PR#7108 Livery storage change and group liveries.
295 SLV_SHIPS_STOP_IN_LOCKS
, ///< 206 PR#7150 Ship/lock movement changes.
296 SLV_FIX_CARGO_MONITOR
, ///< 207 PR#7175 v1.9 Cargo monitor data packing fix to support 64 cargotypes.
297 SLV_TOWN_CARGOGEN
, ///< 208 PR#6965 New algorithms for town building cargo generation.
298 SLV_SHIP_CURVE_PENALTY
, ///< 209 PR#7289 Configurable ship curve penalties.
300 SLV_SERVE_NEUTRAL_INDUSTRIES
, ///< 210 PR#7234 Company stations can serve industries with attached neutral stations.
301 SLV_ROADVEH_PATH_CACHE
, ///< 211 PR#7261 Add path cache for road vehicles.
302 SLV_REMOVE_OPF
, ///< 212 PR#7245 Remove OPF.
303 SLV_TREES_WATER_CLASS
, ///< 213 PR#7405 WaterClass update for tree tiles.
304 SLV_ROAD_TYPES
, ///< 214 PR#6811 NewGRF road types.
306 SLV_SCRIPT_MEMLIMIT
, ///< 215 PR#7516 Limit on AI/GS memory consumption.
307 SLV_MULTITILE_DOCKS
, ///< 216 PR#7380 Multiple docks per station.
308 SLV_TRADING_AGE
, ///< 217 PR#7780 Configurable company trading age.
309 SLV_ENDING_YEAR
, ///< 218 PR#7747 v1.10 Configurable ending year.
310 SLV_REMOVE_TOWN_CARGO_CACHE
, ///< 219 PR#8258 Remove town cargo acceptance and production caches.
312 /* Patchpacks for a while considered it a good idea to jump a few versions
313 * above our version for their savegames. But as time continued, this gap
314 * has been closing, up to the point we would start to reuse versions from
315 * their patchpacks. This is not a problem from our perspective: the
316 * savegame will simply fail to load because they all contain chunks we
317 * cannot digest. But, this gives for ugly errors. As we have plenty of
318 * versions anyway, we simply skip the versions we know belong to
319 * patchpacks. This way we can present the user with a clean error
320 * indicate they are loading a savegame from a patchpack.
321 * For future patchpack creators: please follow a system like JGRPP, where
322 * the version is masked with 0x8000, and the true version is stored in
323 * its own chunk with feature toggles.
325 SLV_START_PATCHPACKS
, ///< 220 First known patchpack to use a version just above ours.
326 SLV_END_PATCHPACKS
= 286, ///< 286 Last known patchpack to use a version just above ours.
328 SLV_GS_INDUSTRY_CONTROL
, ///< 287 PR#7912 and PR#8115 GS industry control.
329 SLV_VEH_MOTION_COUNTER
, ///< 288 PR#8591 Desync safe motion counter
330 SLV_INDUSTRY_TEXT
, ///< 289 PR#8576 v1.11.0-RC1 Additional GS text for industries.
332 SLV_MAPGEN_SETTINGS_REVAMP
, ///< 290 PR#8891 v1.11 Revamp of some mapgen settings (snow coverage, desert coverage, heightmap height, custom terrain type).
333 SLV_GROUP_REPLACE_WAGON_REMOVAL
, ///< 291 PR#7441 Per-group wagon removal flag.
334 SLV_CUSTOM_SUBSIDY_DURATION
, ///< 292 PR#9081 Configurable subsidy duration.
335 SLV_SAVELOAD_LIST_LENGTH
, ///< 293 PR#9374 Consistency in list length with SL_STRUCT / SL_STRUCTLIST / SL_DEQUE / SL_REFLIST.
336 SLV_RIFF_TO_ARRAY
, ///< 294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
338 SLV_TABLE_CHUNKS
, ///< 295 PR#9322 Introduction of CH_TABLE and CH_SPARSE_TABLE.
339 SLV_SCRIPT_INT64
, ///< 296 PR#9415 SQInteger is 64bit but was saved as 32bit.
340 SLV_LINKGRAPH_TRAVEL_TIME
, ///< 297 PR#9457 v12.0-RC1 Store travel time in the linkgraph.
341 SLV_DOCK_DOCKINGTILES
, ///< 298 PR#9578 All tiles around docks may be docking tiles.
342 SLV_REPAIR_OBJECT_DOCKING_TILES
, ///< 299 PR#9594 v12.0 Fixing issue with docking tiles overlapping objects.
343 SLV_U64_TICK_COUNTER
, ///< 300 PR#10035 Make _tick_counter 64bit to avoid wrapping.
344 SLV_LAST_LOADING_TICK
, ///< 301 PR#9693 Store tick of last loading for vehicles.
345 SLV_MULTITRACK_LEVEL_CROSSINGS
, ///< 302 PR#9931 Multi-track level crossings.
347 SL_MAX_VERSION
, ///< Highest possible saveload version
350 /** Save or load result codes. */
351 enum SaveOrLoadResult
{
352 SL_OK
= 0, ///< completed successfully
353 SL_ERROR
= 1, ///< error that was caught before internal structures were modified
354 SL_REINIT
= 2, ///< error that was caught in the middle of updating game state, need to clear it. (can only happen during load)
357 /** Deals with the type of the savegame, independent of extension */
358 struct FileToSaveLoad
{
359 SaveLoadOperation file_op
; ///< File operation to perform.
360 DetailedFileType detail_ftype
; ///< Concrete file type (PNG, BMP, old save, etc).
361 AbstractFileType abstract_ftype
; ///< Abstract type of file (scenario, heightmap, etc).
362 std::string name
; ///< Name of the file.
363 char title
[255]; ///< Internal name of the game.
365 void SetMode(FiosType ft
);
366 void SetMode(SaveLoadOperation fop
, AbstractFileType aft
, DetailedFileType dft
);
367 void SetName(const char *name
);
368 void SetTitle(const char *title
);
371 /** Types of save games. */
373 SGT_TTD
, ///< TTD savegame (can be detected incorrectly)
374 SGT_TTDP1
, ///< TTDP savegame ( -//- ) (data at NW border)
375 SGT_TTDP2
, ///< TTDP savegame in new format (data at SE border)
376 SGT_OTTD
, ///< OTTD savegame
377 SGT_TTO
, ///< TTO savegame
378 SGT_INVALID
= 0xFF, ///< broken savegame (used internally)
381 extern FileToSaveLoad _file_to_saveload
;
383 void GenerateDefaultSaveName(char *buf
, const char *last
);
384 void SetSaveLoadError(StringID str
);
385 const char *GetSaveLoadErrorString();
386 SaveOrLoadResult
SaveOrLoad(const std::string
&filename
, SaveLoadOperation fop
, DetailedFileType dft
, Subdirectory sb
, bool threaded
= true);
387 void WaitTillSaved();
388 void ProcessAsyncSaveFinish();
391 void DoAutoOrNetsave(FiosNumberedSaveName
&counter
);
393 SaveOrLoadResult
SaveWithFilter(struct SaveFilter
*writer
, bool threaded
);
394 SaveOrLoadResult
LoadWithFilter(struct LoadFilter
*reader
);
396 typedef void AutolengthProc(void *arg
);
398 /** Type of a chunk. */
406 CH_TYPE_MASK
= 0xf, ///< All ChunkType values have to be within this mask.
407 CH_READONLY
, ///< Chunk is never saved.
410 /** Handlers and description of chunk. */
411 struct ChunkHandler
{
412 uint32 id
; ///< Unique ID (4 letters).
413 ChunkType type
; ///< Type of the chunk. @see ChunkType
415 ChunkHandler(uint32 id
, ChunkType type
) : id(id
), type(type
) {}
417 virtual ~ChunkHandler() {}
421 * Must be overridden, unless Chunk type is CH_READONLY.
423 virtual void Save() const { NOT_REACHED(); }
427 * Must be overridden.
429 virtual void Load() const = 0;
433 * Pointers are saved using the index of the pointed object.
434 * On load, pointers are filled with indices and need to be fixed to point to the real object.
435 * Must be overridden if the chunk saves any pointer.
437 virtual void FixPointers() const {}
440 * Load the chunk for game preview.
441 * Default implementation just skips the data.
442 * @param len Number of bytes to skip.
444 virtual void LoadCheck(size_t len
= 0) const;
447 /** A reference to ChunkHandler. */
448 using ChunkHandlerRef
= std::reference_wrapper
<const ChunkHandler
>;
450 /** A table of ChunkHandler entries. */
451 using ChunkHandlerTable
= span
<const ChunkHandlerRef
>;
453 /** A table of SaveLoad entries. */
454 using SaveLoadTable
= span
<const struct SaveLoad
>;
456 /** A table of SaveLoadCompat entries. */
457 using SaveLoadCompatTable
= span
<const struct SaveLoadCompat
>;
459 /** Handler for saving/loading an object to/from disk. */
460 class SaveLoadHandler
{
462 std::optional
<std::vector
<SaveLoad
>> load_description
;
464 virtual ~SaveLoadHandler() {}
467 * Save the object to disk.
468 * @param object The object to store.
470 virtual void Save(void *object
) const {}
473 * Load the object from disk.
474 * @param object The object to load.
476 virtual void Load(void *object
) const {}
479 * Similar to load, but used only to validate savegames.
480 * @param object The object to load.
482 virtual void LoadCheck(void *object
) const {}
485 * A post-load callback to fix #SL_REF integers into pointers.
486 * @param object The object to fix.
488 virtual void FixPointers(void *object
) const {}
491 * Get the description of the fields in the savegame.
493 virtual SaveLoadTable
GetDescription() const = 0;
496 * Get the pre-header description of the fields in the savegame.
498 virtual SaveLoadCompatTable
GetCompatDescription() const = 0;
501 * Get the description for how to load the chunk. Depending on the
502 * savegame version this can either use the headers in the savegame or
503 * fall back to backwards compatibility and uses hard-coded headers.
505 SaveLoadTable
GetLoadDescription() const;
509 * Default handler for saving/loading an object to/from disk.
511 * This handles a few common things for handlers, meaning the actual handler
514 * Usage: class SlMine : public DefaultSaveLoadHandler<SlMine, MyObject> {}
516 * @tparam TImpl The class initializing this template.
517 * @tparam TObject The class of the object using this SaveLoadHandler.
519 template <class TImpl
, class TObject
>
520 class DefaultSaveLoadHandler
: public SaveLoadHandler
{
522 SaveLoadTable
GetDescription() const override
{ return static_cast<const TImpl
*>(this)->description
; }
523 SaveLoadCompatTable
GetCompatDescription() const override
{ return static_cast<const TImpl
*>(this)->compat_description
; }
525 virtual void Save(TObject
*object
) const {}
526 void Save(void *object
) const override
{ this->Save(static_cast<TObject
*>(object
)); }
528 virtual void Load(TObject
*object
) const {}
529 void Load(void *object
) const override
{ this->Load(static_cast<TObject
*>(object
)); }
531 virtual void LoadCheck(TObject
*object
) const {}
532 void LoadCheck(void *object
) const override
{ this->LoadCheck(static_cast<TObject
*>(object
)); }
534 virtual void FixPointers(TObject
*object
) const {}
535 void FixPointers(void *object
) const override
{ this->FixPointers(static_cast<TObject
*>(object
)); }
538 /** Type of reference (#SLE_REF, #SLE_CONDREF). */
540 REF_ORDER
= 0, ///< Load/save a reference to an order.
541 REF_VEHICLE
= 1, ///< Load/save a reference to a vehicle.
542 REF_STATION
= 2, ///< Load/save a reference to a station.
543 REF_TOWN
= 3, ///< Load/save a reference to a town.
544 REF_VEHICLE_OLD
= 4, ///< Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
545 REF_ROADSTOPS
= 5, ///< Load/save a reference to a bus/truck stop.
546 REF_ENGINE_RENEWS
= 6, ///< Load/save a reference to an engine renewal (autoreplace).
547 REF_CARGO_PACKET
= 7, ///< Load/save a reference to a cargo packet.
548 REF_ORDERLIST
= 8, ///< Load/save a reference to an orderlist.
549 REF_STORAGE
= 9, ///< Load/save a reference to a persistent storage.
550 REF_LINK_GRAPH
= 10, ///< Load/save a reference to a link graph.
551 REF_LINK_GRAPH_JOB
= 11, ///< Load/save a reference to a link graph job.
555 * VarTypes is the general bitmasked magic type that tells us
556 * certain characteristics about the variable it refers to. For example
557 * SLE_FILE_* gives the size(type) as it would be in the savegame and
558 * SLE_VAR_* the size(type) as it is in memory during runtime. These are
559 * the first 8 bits (0-3 SLE_FILE, 4-7 SLE_VAR).
560 * Bits 8-15 are reserved for various flags as explained below
563 /* 4 bits allocated a maximum of 16 types for NumberType.
564 * NOTE: the SLE_FILE_NNN values are stored in the savegame! */
565 SLE_FILE_END
= 0, ///< Used to mark end-of-header in tables.
574 SLE_FILE_STRINGID
= 9, ///< StringID offset into strings-array
575 SLE_FILE_STRING
= 10,
576 SLE_FILE_STRUCT
= 11,
577 /* 4 more possible file-primitives */
579 SLE_FILE_TYPE_MASK
= 0xf, ///< Mask to get the file-type (and not any flags).
580 SLE_FILE_HAS_LENGTH_FIELD
= 1 << 4, ///< Bit stored in savegame to indicate field has a length field for each entry.
582 /* 4 bits allocated a maximum of 16 types for NumberType */
586 SLE_VAR_I16
= 3 << 4,
587 SLE_VAR_U16
= 4 << 4,
588 SLE_VAR_I32
= 5 << 4,
589 SLE_VAR_U32
= 6 << 4,
590 SLE_VAR_I64
= 7 << 4,
591 SLE_VAR_U64
= 8 << 4,
592 SLE_VAR_NULL
= 9 << 4, ///< useful to write zeros in savegame.
593 SLE_VAR_STRB
= 10 << 4, ///< string (with pre-allocated buffer)
594 SLE_VAR_STR
= 12 << 4, ///< string pointer
595 SLE_VAR_STRQ
= 13 << 4, ///< string pointer enclosed in quotes
596 SLE_VAR_NAME
= 14 << 4, ///< old custom name to be converted to a char pointer
597 /* 1 more possible memory-primitives */
599 /* Shortcut values */
600 SLE_VAR_CHAR
= SLE_VAR_I8
,
602 /* Default combinations of variables. As savegames change, so can variables
603 * and thus it is possible that the saved value and internal size do not
604 * match and you need to specify custom combo. The defaults are listed here */
605 SLE_BOOL
= SLE_FILE_I8
| SLE_VAR_BL
,
606 SLE_INT8
= SLE_FILE_I8
| SLE_VAR_I8
,
607 SLE_UINT8
= SLE_FILE_U8
| SLE_VAR_U8
,
608 SLE_INT16
= SLE_FILE_I16
| SLE_VAR_I16
,
609 SLE_UINT16
= SLE_FILE_U16
| SLE_VAR_U16
,
610 SLE_INT32
= SLE_FILE_I32
| SLE_VAR_I32
,
611 SLE_UINT32
= SLE_FILE_U32
| SLE_VAR_U32
,
612 SLE_INT64
= SLE_FILE_I64
| SLE_VAR_I64
,
613 SLE_UINT64
= SLE_FILE_U64
| SLE_VAR_U64
,
614 SLE_CHAR
= SLE_FILE_I8
| SLE_VAR_CHAR
,
615 SLE_STRINGID
= SLE_FILE_STRINGID
| SLE_VAR_U32
,
616 SLE_STRINGBUF
= SLE_FILE_STRING
| SLE_VAR_STRB
,
617 SLE_STRING
= SLE_FILE_STRING
| SLE_VAR_STR
,
618 SLE_STRINGQUOTE
= SLE_FILE_STRING
| SLE_VAR_STRQ
,
619 SLE_NAME
= SLE_FILE_STRINGID
| SLE_VAR_NAME
,
621 /* Shortcut values */
622 SLE_UINT
= SLE_UINT32
,
624 SLE_STRB
= SLE_STRINGBUF
,
625 SLE_STR
= SLE_STRING
,
626 SLE_STRQ
= SLE_STRINGQUOTE
,
628 /* 8 bits allocated for a maximum of 8 flags
629 * Flags directing saving/loading of a variable */
630 SLF_ALLOW_CONTROL
= 1 << 8, ///< Allow control codes in the strings.
631 SLF_ALLOW_NEWLINE
= 1 << 9, ///< Allow new lines in the strings.
634 typedef uint32 VarType
;
636 /** Type of data saved. */
637 enum SaveLoadType
: byte
{
638 SL_VAR
= 0, ///< Save/load a variable.
639 SL_REF
= 1, ///< Save/load a reference.
640 SL_STRUCT
= 2, ///< Save/load a struct.
642 SL_STR
= 3, ///< Save/load a string.
643 SL_STDSTR
= 4, ///< Save/load a \c std::string.
645 SL_ARR
= 5, ///< Save/load a fixed-size array of #SL_VAR elements.
646 SL_DEQUE
= 6, ///< Save/load a deque of #SL_VAR elements.
647 SL_VECTOR
= 7, ///< Save/load a vector of #SL_VAR elements.
648 SL_REFLIST
= 8, ///< Save/load a list of #SL_REF elements.
649 SL_STRUCTLIST
= 9, ///< Save/load a list of structs.
651 SL_SAVEBYTE
= 10, ///< Save (but not load) a byte.
652 SL_NULL
= 11, ///< Save null-bytes and load to nowhere.
655 typedef void *SaveLoadAddrProc(void *base
, size_t extra
);
657 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
659 std::string name
; ///< Name of this field (optional, used for tables).
660 SaveLoadType cmd
; ///< The action to take with the saved/loaded type, All types need different action.
661 VarType conv
; ///< Type of the variable to be saved; this field combines both FileVarType and MemVarType.
662 uint16 length
; ///< (Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
663 SaveLoadVersion version_from
; ///< Save/load the variable starting from this savegame version.
664 SaveLoadVersion version_to
; ///< Save/load the variable before this savegame version.
665 size_t size
; ///< The sizeof size.
666 SaveLoadAddrProc
*address_proc
; ///< Callback proc the get the actual variable address in memory.
667 size_t extra_data
; ///< Extra data for the callback proc.
668 std::shared_ptr
<SaveLoadHandler
> handler
; ///< Custom handler for Save/Load procs.
672 * SaveLoad information for backwards compatibility.
674 * At SLV_SETTINGS_NAME a new method of keeping track of fields in a savegame
675 * was added, where the order of fields is no longer important. For older
676 * savegames we still need to know the correct order. This struct is the glue
677 * to make that happen.
679 struct SaveLoadCompat
{
680 std::string name
; ///< Name of the field.
681 uint16 length
; ///< Length of the NULL field.
682 SaveLoadVersion version_from
; ///< Save/load the variable starting from this savegame version.
683 SaveLoadVersion version_to
; ///< Save/load the variable before this savegame version.
687 * Storage of simple variables, references (pointers), and arrays.
688 * @param cmd Load/save type. @see SaveLoadType
689 * @param base Name of the class or struct containing the variable.
690 * @param variable Name of the variable in the class or struct referenced by \a base.
691 * @param type Storage of the data in memory and in the savegame.
692 * @param from First savegame version that has the field.
693 * @param to Last savegame version that has the field.
694 * @param extra Extra data to pass to the address callback function.
695 * @note In general, it is better to use one of the SLE_* macros below.
697 #define SLE_GENERAL(cmd, base, variable, type, length, from, to, extra) SaveLoad {#variable, cmd, type, length, from, to, cpp_sizeof(base, variable), [] (void *b, size_t) -> void * { assert(b != nullptr); return const_cast<void *>(static_cast<const void *>(std::addressof(static_cast<base *>(b)->variable))); }, extra, nullptr}
700 * Storage of a variable in some savegame versions.
701 * @param base Name of the class or struct containing the variable.
702 * @param variable Name of the variable in the class or struct referenced by \a base.
703 * @param type Storage of the data in memory and in the savegame.
704 * @param from First savegame version that has the field.
705 * @param to Last savegame version that has the field.
707 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to, 0)
710 * Storage of a reference in some savegame versions.
711 * @param base Name of the class or struct containing the variable.
712 * @param variable Name of the variable in the class or struct referenced by \a base.
713 * @param type Type of the reference, a value from #SLRefType.
714 * @param from First savegame version that has the field.
715 * @param to Last savegame version that has the field.
717 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to, 0)
720 * Storage of a fixed-size array of #SL_VAR elements in some savegame versions.
721 * @param base Name of the class or struct containing the array.
722 * @param variable Name of the variable in the class or struct referenced by \a base.
723 * @param type Storage of the data in memory and in the savegame.
724 * @param length Number of elements in the array.
725 * @param from First savegame version that has the array.
726 * @param to Last savegame version that has the array.
728 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to, 0)
731 * Storage of a string in some savegame versions.
732 * @param base Name of the class or struct containing the string.
733 * @param variable Name of the variable in the class or struct referenced by \a base.
734 * @param type Storage of the data in memory and in the savegame.
735 * @param length Number of elements in the string (only used for fixed size buffers).
736 * @param from First savegame version that has the string.
737 * @param to Last savegame version that has the string.
739 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to, 0)
742 * Storage of a \c std::string in some savegame versions.
743 * @param base Name of the class or struct containing the string.
744 * @param variable Name of the variable in the class or struct referenced by \a base.
745 * @param type Storage of the data in memory and in the savegame.
746 * @param from First savegame version that has the string.
747 * @param to Last savegame version that has the string.
749 #define SLE_CONDSSTR(base, variable, type, from, to) SLE_GENERAL(SL_STDSTR, base, variable, type, 0, from, to, 0)
752 * Storage of a list of #SL_REF elements in some savegame versions.
753 * @param base Name of the class or struct containing the list.
754 * @param variable Name of the variable in the class or struct referenced by \a base.
755 * @param type Storage of the data in memory and in the savegame.
756 * @param from First savegame version that has the list.
757 * @param to Last savegame version that has the list.
759 #define SLE_CONDREFLIST(base, variable, type, from, to) SLE_GENERAL(SL_REFLIST, base, variable, type, 0, from, to, 0)
762 * Storage of a deque of #SL_VAR elements in some savegame versions.
763 * @param base Name of the class or struct containing the list.
764 * @param variable Name of the variable in the class or struct referenced by \a base.
765 * @param type Storage of the data in memory and in the savegame.
766 * @param from First savegame version that has the list.
767 * @param to Last savegame version that has the list.
769 #define SLE_CONDDEQUE(base, variable, type, from, to) SLE_GENERAL(SL_DEQUE, base, variable, type, 0, from, to, 0)
772 * Storage of a variable in every version of a savegame.
773 * @param base Name of the class or struct containing the variable.
774 * @param variable Name of the variable in the class or struct referenced by \a base.
775 * @param type Storage of the data in memory and in the savegame.
777 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
780 * Storage of a reference in every version of a savegame.
781 * @param base Name of the class or struct containing the variable.
782 * @param variable Name of the variable in the class or struct referenced by \a base.
783 * @param type Type of the reference, a value from #SLRefType.
785 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
788 * Storage of fixed-size array of #SL_VAR elements in every version of a savegame.
789 * @param base Name of the class or struct containing the array.
790 * @param variable Name of the variable in the class or struct referenced by \a base.
791 * @param type Storage of the data in memory and in the savegame.
792 * @param length Number of elements in the array.
794 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, SL_MIN_VERSION, SL_MAX_VERSION)
797 * Storage of a string in every savegame version.
798 * @param base Name of the class or struct containing the string.
799 * @param variable Name of the variable in the class or struct referenced by \a base.
800 * @param type Storage of the data in memory and in the savegame.
801 * @param length Number of elements in the string (only used for fixed size buffers).
803 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, SL_MIN_VERSION, SL_MAX_VERSION)
806 * Storage of a \c std::string in every savegame version.
807 * @param base Name of the class or struct containing the string.
808 * @param variable Name of the variable in the class or struct referenced by \a base.
809 * @param type Storage of the data in memory and in the savegame.
811 #define SLE_SSTR(base, variable, type) SLE_CONDSSTR(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
814 * Storage of a list of #SL_REF elements in every savegame version.
815 * @param base Name of the class or struct containing the list.
816 * @param variable Name of the variable in the class or struct referenced by \a base.
817 * @param type Storage of the data in memory and in the savegame.
819 #define SLE_REFLIST(base, variable, type) SLE_CONDREFLIST(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
822 * Only write byte during saving; never read it during loading.
823 * When using SLE_SAVEBYTE you will have to read this byte before the table
824 * this is in is read. This also means SLE_SAVEBYTE can only be used at the
826 * This is intended to be used to indicate what type of entry this is in a
828 * @param base Name of the class or struct containing the variable.
829 * @param variable Name of the variable in the class or struct referenced by \a base.
831 #define SLE_SAVEBYTE(base, variable) SLE_GENERAL(SL_SAVEBYTE, base, variable, 0, 0, SL_MIN_VERSION, SL_MAX_VERSION, 0)
834 * Storage of global simple variables, references (pointers), and arrays.
835 * @param name The name of the field.
836 * @param cmd Load/save type. @see SaveLoadType
837 * @param variable Name of the global variable.
838 * @param type Storage of the data in memory and in the savegame.
839 * @param from First savegame version that has the field.
840 * @param to Last savegame version that has the field.
841 * @param extra Extra data to pass to the address callback function.
842 * @note In general, it is better to use one of the SLEG_* macros below.
844 #define SLEG_GENERAL(name, cmd, variable, type, length, from, to, extra) SaveLoad {name, cmd, type, length, from, to, sizeof(variable), [] (void *, size_t) -> void * { return static_cast<void *>(std::addressof(variable)); }, extra, nullptr}
847 * Storage of a global variable in some savegame versions.
848 * @param name The name of the field.
849 * @param variable Name of the global variable.
850 * @param type Storage of the data in memory and in the savegame.
851 * @param from First savegame version that has the field.
852 * @param to Last savegame version that has the field.
854 #define SLEG_CONDVAR(name, variable, type, from, to) SLEG_GENERAL(name, SL_VAR, variable, type, 0, from, to, 0)
857 * Storage of a global reference in some savegame versions.
858 * @param name The name of the field.
859 * @param variable Name of the global variable.
860 * @param type Storage of the data in memory and in the savegame.
861 * @param from First savegame version that has the field.
862 * @param to Last savegame version that has the field.
864 #define SLEG_CONDREF(name, variable, type, from, to) SLEG_GENERAL(name, SL_REF, variable, type, 0, from, to, 0)
867 * Storage of a global fixed-size array of #SL_VAR elements in some savegame versions.
868 * @param name The name of the field.
869 * @param variable Name of the global variable.
870 * @param type Storage of the data in memory and in the savegame.
871 * @param length Number of elements in the array.
872 * @param from First savegame version that has the array.
873 * @param to Last savegame version that has the array.
875 #define SLEG_CONDARR(name, variable, type, length, from, to) SLEG_GENERAL(name, SL_ARR, variable, type, length, from, to, 0)
878 * Storage of a global string in some savegame versions.
879 * @param name The name of the field.
880 * @param variable Name of the global variable.
881 * @param type Storage of the data in memory and in the savegame.
882 * @param length Number of elements in the string (only used for fixed size buffers).
883 * @param from First savegame version that has the string.
884 * @param to Last savegame version that has the string.
886 #define SLEG_CONDSTR(name, variable, type, length, from, to) SLEG_GENERAL(name, SL_STR, variable, type, length, from, to, 0)
889 * Storage of a global \c std::string in some savegame versions.
890 * @param name The name of the field.
891 * @param variable Name of the global variable.
892 * @param type Storage of the data in memory and in the savegame.
893 * @param from First savegame version that has the string.
894 * @param to Last savegame version that has the string.
896 #define SLEG_CONDSSTR(name, variable, type, from, to) SLEG_GENERAL(name, SL_STDSTR, variable, type, 0, from, to, 0)
899 * Storage of a structs in some savegame versions.
900 * @param name The name of the field.
901 * @param handler SaveLoadHandler for the structs.
902 * @param from First savegame version that has the struct.
903 * @param to Last savegame version that has the struct.
905 #define SLEG_CONDSTRUCT(name, handler, from, to) SaveLoad {name, SL_STRUCT, 0, 0, from, to, 0, nullptr, 0, std::make_shared<handler>()}
908 * Storage of a global reference list in some savegame versions.
909 * @param name The name of the field.
910 * @param variable Name of the global variable.
911 * @param type Storage of the data in memory and in the savegame.
912 * @param from First savegame version that has the list.
913 * @param to Last savegame version that has the list.
915 #define SLEG_CONDREFLIST(name, variable, type, from, to) SLEG_GENERAL(name, SL_REFLIST, variable, type, 0, from, to, 0)
918 * Storage of a global vector of #SL_VAR elements in some savegame versions.
919 * @param name The name of the field.
920 * @param variable Name of the global variable.
921 * @param type Storage of the data in memory and in the savegame.
922 * @param from First savegame version that has the list.
923 * @param to Last savegame version that has the list.
925 #define SLEG_CONDVECTOR(name, variable, type, from, to) SLEG_GENERAL(name, SL_VECTOR, variable, type, 0, from, to, 0)
928 * Storage of a list of structs in some savegame versions.
929 * @param name The name of the field.
930 * @param handler SaveLoadHandler for the list of structs.
931 * @param from First savegame version that has the list.
932 * @param to Last savegame version that has the list.
934 #define SLEG_CONDSTRUCTLIST(name, handler, from, to) SaveLoad {name, SL_STRUCTLIST, 0, 0, from, to, 0, nullptr, 0, std::make_shared<handler>()}
937 * Storage of a global variable in every savegame version.
938 * @param name The name of the field.
939 * @param variable Name of the global variable.
940 * @param type Storage of the data in memory and in the savegame.
942 #define SLEG_VAR(name, variable, type) SLEG_CONDVAR(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
945 * Storage of a global reference in every savegame version.
946 * @param name The name of the field.
947 * @param variable Name of the global variable.
948 * @param type Storage of the data in memory and in the savegame.
950 #define SLEG_REF(name, variable, type) SLEG_CONDREF(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
953 * Storage of a global fixed-size array of #SL_VAR elements in every savegame version.
954 * @param name The name of the field.
955 * @param variable Name of the global variable.
956 * @param type Storage of the data in memory and in the savegame.
958 #define SLEG_ARR(name, variable, type) SLEG_CONDARR(name, variable, type, lengthof(variable), SL_MIN_VERSION, SL_MAX_VERSION)
961 * Storage of a global string in every savegame version.
962 * @param name The name of the field.
963 * @param variable Name of the global variable.
964 * @param type Storage of the data in memory and in the savegame.
966 #define SLEG_STR(name, variable, type) SLEG_CONDSTR(name, variable, type, sizeof(variable), SL_MIN_VERSION, SL_MAX_VERSION)
969 * Storage of a global \c std::string in every savegame version.
970 * @param name The name of the field.
971 * @param variable Name of the global variable.
972 * @param type Storage of the data in memory and in the savegame.
974 #define SLEG_SSTR(name, variable, type) SLEG_CONDSSTR(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
977 * Storage of a structs in every savegame version.
978 * @param name The name of the field.
979 * @param handler SaveLoadHandler for the structs.
981 #define SLEG_STRUCT(name, handler) SLEG_CONDSTRUCT(name, handler, SL_MIN_VERSION, SL_MAX_VERSION)
984 * Storage of a global reference list in every savegame version.
985 * @param name The name of the field.
986 * @param variable Name of the global variable.
987 * @param type Storage of the data in memory and in the savegame.
989 #define SLEG_REFLIST(name, variable, type) SLEG_CONDREFLIST(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
992 * Storage of a global vector of #SL_VAR elements in every savegame version.
993 * @param name The name of the field.
994 * @param variable Name of the global variable.
995 * @param type Storage of the data in memory and in the savegame.
997 #define SLEG_VECTOR(name, variable, type) SLEG_CONDVECTOR(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
1000 * Storage of a list of structs in every savegame version.
1001 * @param name The name of the field.
1002 * @param handler SaveLoadHandler for the list of structs.
1004 #define SLEG_STRUCTLIST(name, handler) SLEG_CONDSTRUCTLIST(name, handler, SL_MIN_VERSION, SL_MAX_VERSION)
1007 * Field name where the real SaveLoad can be located.
1008 * @param name The name of the field.
1010 #define SLC_VAR(name) {name, 0, SL_MIN_VERSION, SL_MAX_VERSION}
1013 * Empty space in every savegame version.
1014 * @param length Length of the empty space.
1015 * @param from First savegame version that has the empty space.
1016 * @param to Last savegame version that has the empty space.
1018 #define SLC_NULL(length, from, to) {{}, length, from, to}
1020 /** End marker of compat variables save or load. */
1021 #define SLC_END() {{}, 0, SL_MIN_VERSION, SL_MIN_VERSION}
1024 * Checks whether the savegame is below \a major.\a minor.
1025 * @param major Major number of the version to check against.
1026 * @param minor Minor number of the version to check against. If \a minor is 0 or not specified, only the major number is checked.
1027 * @return Savegame version is earlier than the specified version.
1029 static inline bool IsSavegameVersionBefore(SaveLoadVersion major
, byte minor
= 0)
1031 extern SaveLoadVersion _sl_version
;
1032 extern byte _sl_minor_version
;
1033 return _sl_version
< major
|| (minor
> 0 && _sl_version
== major
&& _sl_minor_version
< minor
);
1037 * Checks whether the savegame is below or at \a major. This should be used to repair data from existing
1038 * savegames which is no longer corrupted in new savegames, but for which otherwise no savegame
1040 * @param major Major number of the version to check against.
1041 * @return Savegame version is at most the specified version.
1043 static inline bool IsSavegameVersionBeforeOrAt(SaveLoadVersion major
)
1045 extern SaveLoadVersion _sl_version
;
1046 return _sl_version
<= major
;
1050 * Checks if some version from/to combination falls within the range of the
1051 * active savegame version.
1052 * @param version_from Inclusive savegame version lower bound.
1053 * @param version_to Exclusive savegame version upper bound. SL_MAX_VERSION if no upper bound.
1054 * @return Active savegame version falls within the given range.
1056 static inline bool SlIsObjectCurrentlyValid(SaveLoadVersion version_from
, SaveLoadVersion version_to
)
1058 extern const SaveLoadVersion SAVEGAME_VERSION
;
1059 return version_from
<= SAVEGAME_VERSION
&& SAVEGAME_VERSION
< version_to
;
1063 * Get the NumberType of a setting. This describes the integer type
1064 * as it is represented in memory
1065 * @param type VarType holding information about the variable-type
1066 * @return the SLE_VAR_* part of a variable-type description
1068 static inline VarType
GetVarMemType(VarType type
)
1070 return type
& 0xF0; // GB(type, 4, 4) << 4;
1074 * Get the FileType of a setting. This describes the integer type
1075 * as it is represented in a savegame/file
1076 * @param type VarType holding information about the file-type
1077 * @return the SLE_FILE_* part of a variable-type description
1079 static inline VarType
GetVarFileType(VarType type
)
1081 return type
& 0xF; // GB(type, 0, 4);
1085 * Check if the given saveload type is a numeric type.
1086 * @param conv the type to check
1087 * @return True if it's a numeric type.
1089 static inline bool IsNumericType(VarType conv
)
1091 return GetVarMemType(conv
) <= SLE_VAR_U64
;
1095 * Get the address of the variable. Null-variables don't have an address,
1096 * everything else has a callback function that returns the address based
1097 * on the saveload data and the current object for non-globals.
1099 static inline void *GetVariableAddress(const void *object
, const SaveLoad
&sld
)
1101 /* Entry is a null-variable, mostly used to read old savegames etc. */
1102 if (GetVarMemType(sld
.conv
) == SLE_VAR_NULL
) {
1103 assert(sld
.address_proc
== nullptr);
1107 /* Everything else should be a non-null pointer. */
1108 assert(sld
.address_proc
!= nullptr);
1109 return sld
.address_proc(const_cast<void *>(object
), sld
.extra_data
);
1112 int64
ReadValue(const void *ptr
, VarType conv
);
1113 void WriteValue(void *ptr
, VarType conv
, int64 val
);
1115 void SlSetArrayIndex(uint index
);
1116 int SlIterateArray();
1118 void SlSetStructListLength(size_t length
);
1119 size_t SlGetStructListLength(size_t limit
);
1121 void SlAutolength(AutolengthProc
*proc
, void *arg
);
1122 size_t SlGetFieldLength();
1123 void SlSetLength(size_t length
);
1124 size_t SlCalcObjMemberLength(const void *object
, const SaveLoad
&sld
);
1125 size_t SlCalcObjLength(const void *object
, const SaveLoadTable
&slt
);
1128 void SlWriteByte(byte b
);
1130 void SlGlobList(const SaveLoadTable
&slt
);
1131 void SlCopy(void *object
, size_t length
, VarType conv
);
1132 std::vector
<SaveLoad
> SlTableHeader(const SaveLoadTable
&slt
);
1133 std::vector
<SaveLoad
> SlCompatTableHeader(const SaveLoadTable
&slt
, const SaveLoadCompatTable
&slct
);
1134 void SlObject(void *object
, const SaveLoadTable
&slt
);
1135 void NORETURN
SlError(StringID string
, const char *extra_msg
= nullptr);
1136 void NORETURN
SlErrorCorrupt(const char *msg
);
1137 void NORETURN
SlErrorCorruptFmt(const char *format
, ...) WARN_FORMAT(1, 2);
1139 bool SaveloadCrashWithMissingNewGRFs();
1142 * Read in bytes from the file/data structure but don't do
1143 * anything with them, discarding them in effect
1144 * @param length The amount of bytes that is being treated this way
1146 static inline void SlSkipBytes(size_t length
)
1148 for (; length
!= 0; length
--) SlReadByte();
1151 extern std::string _savegame_format
;
1152 extern bool _do_autosave
;
1154 #endif /* SAVELOAD_H */