1 /*
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/>.
6 */
7
8 /** @file saveload.h Functions/types related to saving and loading games. */
9
10 #ifndef SAVELOAD_H
11 #define SAVELOAD_H
12
13 #include "../fileio_type.h"
14 #include "../fios.h"
15 #include "../strings_type.h"
16 #include "../core/span_type.hpp"
17 #include <optional>
18 #include <string>
19 #include <vector>
20
21 /** SaveLoad versions
22 * Previous savegame versions, the trunk revision where they were
23 * introduced and the released version that had that particular
24 * savegame version.
25 * Up to savegame version 18 there is a minor version as well.
26 *
27 * Older entries keep their original numbering.
28 *
29 * Newer entries should use a descriptive labels, numeric version
30 * and PR can be added to comment.
31 *
32 * Note that this list must not be reordered.
33 */
34 enum SaveLoadVersion : uint16 {
35 SL_MIN_VERSION, ///< First savegame version
36
37 SLV_1, ///< 1.0 0.1.x, 0.2.x
38 SLV_2, /**< 2.0 0.3.0
39 * 2.1 0.3.1, 0.3.2 */
40 SLV_3, ///< 3.x lost
41 SLV_4, /**< 4.0 1
42 * 4.1 122 0.3.3, 0.3.4
43 * 4.2 1222 0.3.5
44 * 4.3 1417
45 * 4.4 1426 */
46
47 SLV_5, /**< 5.0 1429
48 * 5.1 1440
49 * 5.2 1525 0.3.6 */
50 SLV_6, /**< 6.0 1721
51 * 6.1 1768 */
52 SLV_7, ///< 7.0 1770
53 SLV_8, ///< 8.0 1786
54 SLV_9, ///< 9.0 1909
55
56 SLV_10, ///< 10.0 2030
57 SLV_11, /**< 11.0 2033
58 * 11.1 2041 */
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
62
63 SLV_15, ///< 15.0 2499
64 SLV_16, /**< 16.0 2817
65 * 16.1 3155 */
66 SLV_17, /**< 17.0 3212
67 * 17.1 3218 */
68 SLV_18, ///< 18 3227
69 SLV_19, ///< 19 3396
70
71 SLV_20, ///< 20 3403
72 SLV_21, ///< 21 3472 0.4.x
73 SLV_22, ///< 22 3726
74 SLV_23, ///< 23 3915
75 SLV_24, ///< 24 4150
76
77 SLV_25, ///< 25 4259
78 SLV_26, ///< 26 4466
79 SLV_27, ///< 27 4757
80 SLV_28, ///< 28 4987
81 SLV_29, ///< 29 5070
82
83 SLV_30, ///< 30 5946
84 SLV_31, ///< 31 5999
85 SLV_32, ///< 32 6001
86 SLV_33, ///< 33 6440
87 SLV_34, ///< 34 6455
88
89 SLV_35, ///< 35 6602
90 SLV_36, ///< 36 6624
91 SLV_37, ///< 37 7182
92 SLV_38, ///< 38 7195
93 SLV_39, ///< 39 7269
94
95 SLV_40, ///< 40 7326
96 SLV_41, ///< 41 7348 0.5.x
97 SLV_42, ///< 42 7573
98 SLV_43, ///< 43 7642
99 SLV_44, ///< 44 8144
100
101 SLV_45, ///< 45 8501
102 SLV_46, ///< 46 8705
103 SLV_47, ///< 47 8735
104 SLV_48, ///< 48 8935
105 SLV_49, ///< 49 8969
106
107 SLV_50, ///< 50 8973
108 SLV_51, ///< 51 8978
109 SLV_52, ///< 52 9066
110 SLV_53, ///< 53 9316
111 SLV_54, ///< 54 9613
112
113 SLV_55, ///< 55 9638
114 SLV_56, ///< 56 9667
115 SLV_57, ///< 57 9691
116 SLV_58, ///< 58 9762
117 SLV_59, ///< 59 9779
118
119 SLV_60, ///< 60 9874
120 SLV_61, ///< 61 9892
121 SLV_62, ///< 62 9905
122 SLV_63, ///< 63 9956
123 SLV_64, ///< 64 10006
124
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
130
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
136
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
142
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
148
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
154
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
160
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
166
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
172
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
178
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
184
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
190
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
196
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
202
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
208
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
214
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
220
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
226
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
232
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
238
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
244
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
250
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
256
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
262
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
268
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
274
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
281
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
287
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.
293
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.
299
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.
305
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.
311
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.
324 */
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.
327
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.
331
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.
337
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 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 Fixing issue with docking tiles overlapping objects.
343
344 SL_MAX_VERSION, ///< Highest possible saveload version
345 };
346
347 /** Save or load result codes. */
348 enum SaveOrLoadResult {
349 SL_OK = 0, ///< completed successfully
350 SL_ERROR = 1, ///< error that was caught before internal structures were modified
351 SL_REINIT = 2, ///< error that was caught in the middle of updating game state, need to clear it. (can only happen during load)
352 };
353
354 /** Deals with the type of the savegame, independent of extension */
355 struct FileToSaveLoad {
356 SaveLoadOperation file_op; ///< File operation to perform.
357 DetailedFileType detail_ftype; ///< Concrete file type (PNG, BMP, old save, etc).
358 AbstractFileType abstract_ftype; ///< Abstract type of file (scenario, heightmap, etc).
359 std::string name; ///< Name of the file.
360 char title[255]; ///< Internal name of the game.
361
362 void SetMode(FiosType ft);
363 void SetMode(SaveLoadOperation fop, AbstractFileType aft, DetailedFileType dft);
364 void SetName(const char *name);
365 void SetTitle(const char *title);
366 };
367
368 /** Types of save games. */
369 enum SavegameType {
370 SGT_TTD, ///< TTD savegame (can be detected incorrectly)
371 SGT_TTDP1, ///< TTDP savegame ( -//- ) (data at NW border)
372 SGT_TTDP2, ///< TTDP savegame in new format (data at SE border)
373 SGT_OTTD, ///< OTTD savegame
374 SGT_TTO, ///< TTO savegame
375 SGT_INVALID = 0xFF, ///< broken savegame (used internally)
376 };
377
378 extern FileToSaveLoad _file_to_saveload;
379
380 void GenerateDefaultSaveName(char *buf, const char *last);
381 void SetSaveLoadError(StringID str);
382 const char *GetSaveLoadErrorString();
383 SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true);
384 void WaitTillSaved();
385 void ProcessAsyncSaveFinish();
386 void DoExitSave();
387
388 void DoAutoOrNetsave(FiosNumberedSaveName &counter);
389
390 SaveOrLoadResult SaveWithFilter(struct SaveFilter *writer, bool threaded);
391 SaveOrLoadResult LoadWithFilter(struct LoadFilter *reader);
392
393 typedef void AutolengthProc(void *arg);
394
395 /** Type of a chunk. */
396 enum ChunkType {
397 CH_RIFF = 0,
398 CH_ARRAY = 1,
399 CH_SPARSE_ARRAY = 2,
400 CH_TABLE = 3,
401 CH_SPARSE_TABLE = 4,
402
403 CH_TYPE_MASK = 0xf, ///< All ChunkType values have to be within this mask.
404 CH_READONLY, ///< Chunk is never saved.
405 };
406
407 /** Handlers and description of chunk. */
408 struct ChunkHandler {
409 uint32 id; ///< Unique ID (4 letters).
410 ChunkType type; ///< Type of the chunk. @see ChunkType
411
ChunkHandlerChunkHandler412 ChunkHandler(uint32 id, ChunkType type) : id(id), type(type) {}
413
~ChunkHandlerChunkHandler414 virtual ~ChunkHandler() {}
415
416 /**
417 * Save the chunk.
418 * Must be overridden, unless Chunk type is CH_READONLY.
419 */
SaveChunkHandler420 virtual void Save() const { NOT_REACHED(); }
421
422 /**
423 * Load the chunk.
424 * Must be overridden.
425 */
426 virtual void Load() const = 0;
427
428 /**
429 * Fix the pointers.
430 * Pointers are saved using the index of the pointed object.
431 * On load, pointers are filled with indices and need to be fixed to point to the real object.
432 * Must be overridden if the chunk saves any pointer.
433 */
FixPointersChunkHandler434 virtual void FixPointers() const {}
435
436 /**
437 * Load the chunk for game preview.
438 * Default implementation just skips the data.
439 * @param len Number of bytes to skip.
440 */
441 virtual void LoadCheck(size_t len = 0) const;
442 };
443
444 /** A reference to ChunkHandler. */
445 using ChunkHandlerRef = std::reference_wrapper<const ChunkHandler>;
446
447 /** A table of ChunkHandler entries. */
448 using ChunkHandlerTable = span<const ChunkHandlerRef>;
449
450 /** A table of SaveLoad entries. */
451 using SaveLoadTable = span<const struct SaveLoad>;
452
453 /** A table of SaveLoadCompat entries. */
454 using SaveLoadCompatTable = span<const struct SaveLoadCompat>;
455
456 /** Handler for saving/loading an object to/from disk. */
457 class SaveLoadHandler {
458 public:
459 std::optional<std::vector<SaveLoad>> load_description;
460
~SaveLoadHandler()461 virtual ~SaveLoadHandler() {}
462
463 /**
464 * Save the object to disk.
465 * @param object The object to store.
466 */
Save(void * object)467 virtual void Save(void *object) const {}
468
469 /**
470 * Load the object from disk.
471 * @param object The object to load.
472 */
Load(void * object)473 virtual void Load(void *object) const {}
474
475 /**
476 * Similar to load, but used only to validate savegames.
477 * @param object The object to load.
478 */
LoadCheck(void * object)479 virtual void LoadCheck(void *object) const {}
480
481 /**
482 * A post-load callback to fix #SL_REF integers into pointers.
483 * @param object The object to fix.
484 */
FixPointers(void * object)485 virtual void FixPointers(void *object) const {}
486
487 /**
488 * Get the description of the fields in the savegame.
489 */
490 virtual SaveLoadTable GetDescription() const = 0;
491
492 /**
493 * Get the pre-header description of the fields in the savegame.
494 */
495 virtual SaveLoadCompatTable GetCompatDescription() const = 0;
496
497 /**
498 * Get the description for how to load the chunk. Depending on the
499 * savegame version this can either use the headers in the savegame or
500 * fall back to backwards compatibility and uses hard-coded headers.
501 */
502 SaveLoadTable GetLoadDescription() const;
503 };
504
505 /**
506 * Default handler for saving/loading an object to/from disk.
507 *
508 * This handles a few common things for handlers, meaning the actual handler
509 * needs less code.
510 *
511 * Usage: class SlMine : public DefaultSaveLoadHandler<SlMine, MyObject> {}
512 *
513 * @tparam TImpl The class initializing this template.
514 * @tparam TObject The class of the object using this SaveLoadHandler.
515 */
516 template <class TImpl, class TObject>
517 class DefaultSaveLoadHandler : public SaveLoadHandler {
518 public:
GetDescription()519 SaveLoadTable GetDescription() const override { return static_cast<const TImpl *>(this)->description; }
GetCompatDescription()520 SaveLoadCompatTable GetCompatDescription() const override { return static_cast<const TImpl *>(this)->compat_description; }
521
Save(TObject * object)522 virtual void Save(TObject *object) const {}
Save(void * object)523 void Save(void *object) const override { this->Save(static_cast<TObject *>(object)); }
524
Load(TObject * object)525 virtual void Load(TObject *object) const {}
Load(void * object)526 void Load(void *object) const override { this->Load(static_cast<TObject *>(object)); }
527
LoadCheck(TObject * object)528 virtual void LoadCheck(TObject *object) const {}
LoadCheck(void * object)529 void LoadCheck(void *object) const override { this->LoadCheck(static_cast<TObject *>(object)); }
530
FixPointers(TObject * object)531 virtual void FixPointers(TObject *object) const {}
FixPointers(void * object)532 void FixPointers(void *object) const override { this->FixPointers(static_cast<TObject *>(object)); }
533 };
534
535 /** Type of reference (#SLE_REF, #SLE_CONDREF). */
536 enum SLRefType {
537 REF_ORDER = 0, ///< Load/save a reference to an order.
538 REF_VEHICLE = 1, ///< Load/save a reference to a vehicle.
539 REF_STATION = 2, ///< Load/save a reference to a station.
540 REF_TOWN = 3, ///< Load/save a reference to a town.
541 REF_VEHICLE_OLD = 4, ///< Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
542 REF_ROADSTOPS = 5, ///< Load/save a reference to a bus/truck stop.
543 REF_ENGINE_RENEWS = 6, ///< Load/save a reference to an engine renewal (autoreplace).
544 REF_CARGO_PACKET = 7, ///< Load/save a reference to a cargo packet.
545 REF_ORDERLIST = 8, ///< Load/save a reference to an orderlist.
546 REF_STORAGE = 9, ///< Load/save a reference to a persistent storage.
547 REF_LINK_GRAPH = 10, ///< Load/save a reference to a link graph.
548 REF_LINK_GRAPH_JOB = 11, ///< Load/save a reference to a link graph job.
549 };
550
551 /**
552 * VarTypes is the general bitmasked magic type that tells us
553 * certain characteristics about the variable it refers to. For example
554 * SLE_FILE_* gives the size(type) as it would be in the savegame and
555 * SLE_VAR_* the size(type) as it is in memory during runtime. These are
556 * the first 8 bits (0-3 SLE_FILE, 4-7 SLE_VAR).
557 * Bits 8-15 are reserved for various flags as explained below
558 */
559 enum VarTypes {
560 /* 4 bits allocated a maximum of 16 types for NumberType.
561 * NOTE: the SLE_FILE_NNN values are stored in the savegame! */
562 SLE_FILE_END = 0, ///< Used to mark end-of-header in tables.
563 SLE_FILE_I8 = 1,
564 SLE_FILE_U8 = 2,
565 SLE_FILE_I16 = 3,
566 SLE_FILE_U16 = 4,
567 SLE_FILE_I32 = 5,
568 SLE_FILE_U32 = 6,
569 SLE_FILE_I64 = 7,
570 SLE_FILE_U64 = 8,
571 SLE_FILE_STRINGID = 9, ///< StringID offset into strings-array
572 SLE_FILE_STRING = 10,
573 SLE_FILE_STRUCT = 11,
574 /* 4 more possible file-primitives */
575
576 SLE_FILE_TYPE_MASK = 0xf, ///< Mask to get the file-type (and not any flags).
577 SLE_FILE_HAS_LENGTH_FIELD = 1 << 4, ///< Bit stored in savegame to indicate field has a length field for each entry.
578
579 /* 4 bits allocated a maximum of 16 types for NumberType */
580 SLE_VAR_BL = 0 << 4,
581 SLE_VAR_I8 = 1 << 4,
582 SLE_VAR_U8 = 2 << 4,
583 SLE_VAR_I16 = 3 << 4,
584 SLE_VAR_U16 = 4 << 4,
585 SLE_VAR_I32 = 5 << 4,
586 SLE_VAR_U32 = 6 << 4,
587 SLE_VAR_I64 = 7 << 4,
588 SLE_VAR_U64 = 8 << 4,
589 SLE_VAR_NULL = 9 << 4, ///< useful to write zeros in savegame.
590 SLE_VAR_STRB = 10 << 4, ///< string (with pre-allocated buffer)
591 SLE_VAR_STR = 12 << 4, ///< string pointer
592 SLE_VAR_STRQ = 13 << 4, ///< string pointer enclosed in quotes
593 SLE_VAR_NAME = 14 << 4, ///< old custom name to be converted to a char pointer
594 /* 1 more possible memory-primitives */
595
596 /* Shortcut values */
597 SLE_VAR_CHAR = SLE_VAR_I8,
598
599 /* Default combinations of variables. As savegames change, so can variables
600 * and thus it is possible that the saved value and internal size do not
601 * match and you need to specify custom combo. The defaults are listed here */
602 SLE_BOOL = SLE_FILE_I8 | SLE_VAR_BL,
603 SLE_INT8 = SLE_FILE_I8 | SLE_VAR_I8,
604 SLE_UINT8 = SLE_FILE_U8 | SLE_VAR_U8,
605 SLE_INT16 = SLE_FILE_I16 | SLE_VAR_I16,
606 SLE_UINT16 = SLE_FILE_U16 | SLE_VAR_U16,
607 SLE_INT32 = SLE_FILE_I32 | SLE_VAR_I32,
608 SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32,
609 SLE_INT64 = SLE_FILE_I64 | SLE_VAR_I64,
610 SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64,
611 SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR,
612 SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U32,
613 SLE_STRINGBUF = SLE_FILE_STRING | SLE_VAR_STRB,
614 SLE_STRING = SLE_FILE_STRING | SLE_VAR_STR,
615 SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ,
616 SLE_NAME = SLE_FILE_STRINGID | SLE_VAR_NAME,
617
618 /* Shortcut values */
619 SLE_UINT = SLE_UINT32,
620 SLE_INT = SLE_INT32,
621 SLE_STRB = SLE_STRINGBUF,
622 SLE_STR = SLE_STRING,
623 SLE_STRQ = SLE_STRINGQUOTE,
624
625 /* 8 bits allocated for a maximum of 8 flags
626 * Flags directing saving/loading of a variable */
627 SLF_ALLOW_CONTROL = 1 << 8, ///< Allow control codes in the strings.
628 SLF_ALLOW_NEWLINE = 1 << 9, ///< Allow new lines in the strings.
629 };
630
631 typedef uint32 VarType;
632
633 /** Type of data saved. */
634 enum SaveLoadType : byte {
635 SL_VAR = 0, ///< Save/load a variable.
636 SL_REF = 1, ///< Save/load a reference.
637 SL_STRUCT = 2, ///< Save/load a struct.
638
639 SL_STR = 3, ///< Save/load a string.
640 SL_STDSTR = 4, ///< Save/load a \c std::string.
641
642 SL_ARR = 5, ///< Save/load a fixed-size array of #SL_VAR elements.
643 SL_DEQUE = 6, ///< Save/load a deque of #SL_VAR elements.
644 SL_VECTOR = 7, ///< Save/load a vector of #SL_VAR elements.
645 SL_REFLIST = 8, ///< Save/load a list of #SL_REF elements.
646 SL_STRUCTLIST = 9, ///< Save/load a list of structs.
647
648 SL_SAVEBYTE = 10, ///< Save (but not load) a byte.
649 SL_NULL = 11, ///< Save null-bytes and load to nowhere.
650 };
651
652 typedef void *SaveLoadAddrProc(void *base, size_t extra);
653
654 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
655 struct SaveLoad {
656 std::string name; ///< Name of this field (optional, used for tables).
657 SaveLoadType cmd; ///< The action to take with the saved/loaded type, All types need different action.
658 VarType conv; ///< Type of the variable to be saved; this field combines both FileVarType and MemVarType.
659 uint16 length; ///< (Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
660 SaveLoadVersion version_from; ///< Save/load the variable starting from this savegame version.
661 SaveLoadVersion version_to; ///< Save/load the variable before this savegame version.
662 size_t size; ///< The sizeof size.
663 SaveLoadAddrProc *address_proc; ///< Callback proc the get the actual variable address in memory.
664 size_t extra_data; ///< Extra data for the callback proc.
665 std::shared_ptr<SaveLoadHandler> handler; ///< Custom handler for Save/Load procs.
666 };
667
668 /**
669 * SaveLoad information for backwards compatibility.
670 *
671 * At SLV_SETTINGS_NAME a new method of keeping track of fields in a savegame
672 * was added, where the order of fields is no longer important. For older
673 * savegames we still need to know the correct order. This struct is the glue
674 * to make that happen.
675 */
676 struct SaveLoadCompat {
677 std::string name; ///< Name of the field.
678 uint16 length; ///< Length of the NULL field.
679 SaveLoadVersion version_from; ///< Save/load the variable starting from this savegame version.
680 SaveLoadVersion version_to; ///< Save/load the variable before this savegame version.
681 };
682
683 /**
684 * Storage of simple variables, references (pointers), and arrays.
685 * @param cmd Load/save type. @see SaveLoadType
686 * @param base Name of the class or struct containing the variable.
687 * @param variable Name of the variable in the class or struct referenced by \a base.
688 * @param type Storage of the data in memory and in the savegame.
689 * @param from First savegame version that has the field.
690 * @param to Last savegame version that has the field.
691 * @param extra Extra data to pass to the address callback function.
692 * @note In general, it is better to use one of the SLE_* macros below.
693 */
694 #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}
695
696 /**
697 * Storage of a variable in some savegame versions.
698 * @param base Name of the class or struct containing the variable.
699 * @param variable Name of the variable in the class or struct referenced by \a base.
700 * @param type Storage of the data in memory and in the savegame.
701 * @param from First savegame version that has the field.
702 * @param to Last savegame version that has the field.
703 */
704 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to, 0)
705
706 /**
707 * Storage of a reference in some savegame versions.
708 * @param base Name of the class or struct containing the variable.
709 * @param variable Name of the variable in the class or struct referenced by \a base.
710 * @param type Type of the reference, a value from #SLRefType.
711 * @param from First savegame version that has the field.
712 * @param to Last savegame version that has the field.
713 */
714 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to, 0)
715
716 /**
717 * Storage of a fixed-size array of #SL_VAR elements in some savegame versions.
718 * @param base Name of the class or struct containing the array.
719 * @param variable Name of the variable in the class or struct referenced by \a base.
720 * @param type Storage of the data in memory and in the savegame.
721 * @param length Number of elements in the array.
722 * @param from First savegame version that has the array.
723 * @param to Last savegame version that has the array.
724 */
725 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to, 0)
726
727 /**
728 * Storage of a string in some savegame versions.
729 * @param base Name of the class or struct containing the string.
730 * @param variable Name of the variable in the class or struct referenced by \a base.
731 * @param type Storage of the data in memory and in the savegame.
732 * @param length Number of elements in the string (only used for fixed size buffers).
733 * @param from First savegame version that has the string.
734 * @param to Last savegame version that has the string.
735 */
736 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to, 0)
737
738 /**
739 * Storage of a \c std::string in some savegame versions.
740 * @param base Name of the class or struct containing the string.
741 * @param variable Name of the variable in the class or struct referenced by \a base.
742 * @param type Storage of the data in memory and in the savegame.
743 * @param from First savegame version that has the string.
744 * @param to Last savegame version that has the string.
745 */
746 #define SLE_CONDSSTR(base, variable, type, from, to) SLE_GENERAL(SL_STDSTR, base, variable, type, 0, from, to, 0)
747
748 /**
749 * Storage of a list of #SL_REF elements in some savegame versions.
750 * @param base Name of the class or struct containing the list.
751 * @param variable Name of the variable in the class or struct referenced by \a base.
752 * @param type Storage of the data in memory and in the savegame.
753 * @param from First savegame version that has the list.
754 * @param to Last savegame version that has the list.
755 */
756 #define SLE_CONDREFLIST(base, variable, type, from, to) SLE_GENERAL(SL_REFLIST, base, variable, type, 0, from, to, 0)
757
758 /**
759 * Storage of a deque of #SL_VAR elements in some savegame versions.
760 * @param base Name of the class or struct containing the list.
761 * @param variable Name of the variable in the class or struct referenced by \a base.
762 * @param type Storage of the data in memory and in the savegame.
763 * @param from First savegame version that has the list.
764 * @param to Last savegame version that has the list.
765 */
766 #define SLE_CONDDEQUE(base, variable, type, from, to) SLE_GENERAL(SL_DEQUE, base, variable, type, 0, from, to, 0)
767
768 /**
769 * Storage of a variable in every version of a savegame.
770 * @param base Name of the class or struct containing the variable.
771 * @param variable Name of the variable in the class or struct referenced by \a base.
772 * @param type Storage of the data in memory and in the savegame.
773 */
774 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
775
776 /**
777 * Storage of a reference in every version of a savegame.
778 * @param base Name of the class or struct containing the variable.
779 * @param variable Name of the variable in the class or struct referenced by \a base.
780 * @param type Type of the reference, a value from #SLRefType.
781 */
782 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
783
784 /**
785 * Storage of fixed-size array of #SL_VAR elements in every version of a savegame.
786 * @param base Name of the class or struct containing the array.
787 * @param variable Name of the variable in the class or struct referenced by \a base.
788 * @param type Storage of the data in memory and in the savegame.
789 * @param length Number of elements in the array.
790 */
791 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, SL_MIN_VERSION, SL_MAX_VERSION)
792
793 /**
794 * Storage of a string in every savegame version.
795 * @param base Name of the class or struct containing the string.
796 * @param variable Name of the variable in the class or struct referenced by \a base.
797 * @param type Storage of the data in memory and in the savegame.
798 * @param length Number of elements in the string (only used for fixed size buffers).
799 */
800 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, SL_MIN_VERSION, SL_MAX_VERSION)
801
802 /**
803 * Storage of a \c std::string in every savegame version.
804 * @param base Name of the class or struct containing the string.
805 * @param variable Name of the variable in the class or struct referenced by \a base.
806 * @param type Storage of the data in memory and in the savegame.
807 */
808 #define SLE_SSTR(base, variable, type) SLE_CONDSSTR(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
809
810 /**
811 * Storage of a list of #SL_REF elements in every savegame version.
812 * @param base Name of the class or struct containing the list.
813 * @param variable Name of the variable in the class or struct referenced by \a base.
814 * @param type Storage of the data in memory and in the savegame.
815 */
816 #define SLE_REFLIST(base, variable, type) SLE_CONDREFLIST(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
817
818 /**
819 * Only write byte during saving; never read it during loading.
820 * When using SLE_SAVEBYTE you will have to read this byte before the table
821 * this is in is read. This also means SLE_SAVEBYTE can only be used at the
822 * top of a chunk.
823 * This is intended to be used to indicate what type of entry this is in a
824 * list of entries.
825 * @param base Name of the class or struct containing the variable.
826 * @param variable Name of the variable in the class or struct referenced by \a base.
827 */
828 #define SLE_SAVEBYTE(base, variable) SLE_GENERAL(SL_SAVEBYTE, base, variable, 0, 0, SL_MIN_VERSION, SL_MAX_VERSION, 0)
829
830 /**
831 * Storage of global simple variables, references (pointers), and arrays.
832 * @param name The name of the field.
833 * @param cmd Load/save type. @see SaveLoadType
834 * @param variable Name of the global variable.
835 * @param type Storage of the data in memory and in the savegame.
836 * @param from First savegame version that has the field.
837 * @param to Last savegame version that has the field.
838 * @param extra Extra data to pass to the address callback function.
839 * @note In general, it is better to use one of the SLEG_* macros below.
840 */
841 #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}
842
843 /**
844 * Storage of a global variable in some savegame versions.
845 * @param name The name of the field.
846 * @param variable Name of the global variable.
847 * @param type Storage of the data in memory and in the savegame.
848 * @param from First savegame version that has the field.
849 * @param to Last savegame version that has the field.
850 */
851 #define SLEG_CONDVAR(name, variable, type, from, to) SLEG_GENERAL(name, SL_VAR, variable, type, 0, from, to, 0)
852
853 /**
854 * Storage of a global reference in some savegame versions.
855 * @param name The name of the field.
856 * @param variable Name of the global variable.
857 * @param type Storage of the data in memory and in the savegame.
858 * @param from First savegame version that has the field.
859 * @param to Last savegame version that has the field.
860 */
861 #define SLEG_CONDREF(name, variable, type, from, to) SLEG_GENERAL(name, SL_REF, variable, type, 0, from, to, 0)
862
863 /**
864 * Storage of a global fixed-size array of #SL_VAR elements in some savegame versions.
865 * @param name The name of the field.
866 * @param variable Name of the global variable.
867 * @param type Storage of the data in memory and in the savegame.
868 * @param length Number of elements in the array.
869 * @param from First savegame version that has the array.
870 * @param to Last savegame version that has the array.
871 */
872 #define SLEG_CONDARR(name, variable, type, length, from, to) SLEG_GENERAL(name, SL_ARR, variable, type, length, from, to, 0)
873
874 /**
875 * Storage of a global string in some savegame versions.
876 * @param name The name of the field.
877 * @param variable Name of the global variable.
878 * @param type Storage of the data in memory and in the savegame.
879 * @param length Number of elements in the string (only used for fixed size buffers).
880 * @param from First savegame version that has the string.
881 * @param to Last savegame version that has the string.
882 */
883 #define SLEG_CONDSTR(name, variable, type, length, from, to) SLEG_GENERAL(name, SL_STR, variable, type, length, from, to, 0)
884
885 /**
886 * Storage of a global \c std::string in some savegame versions.
887 * @param name The name of the field.
888 * @param variable Name of the global variable.
889 * @param type Storage of the data in memory and in the savegame.
890 * @param from First savegame version that has the string.
891 * @param to Last savegame version that has the string.
892 */
893 #define SLEG_CONDSSTR(name, variable, type, from, to) SLEG_GENERAL(name, SL_STDSTR, variable, type, 0, from, to, 0)
894
895 /**
896 * Storage of a structs in some savegame versions.
897 * @param name The name of the field.
898 * @param handler SaveLoadHandler for the structs.
899 * @param from First savegame version that has the struct.
900 * @param to Last savegame version that has the struct.
901 */
902 #define SLEG_CONDSTRUCT(name, handler, from, to) SaveLoad {name, SL_STRUCT, 0, 0, from, to, 0, nullptr, 0, std::make_shared<handler>()}
903
904 /**
905 * Storage of a global reference list in some savegame versions.
906 * @param name The name of the field.
907 * @param variable Name of the global variable.
908 * @param type Storage of the data in memory and in the savegame.
909 * @param from First savegame version that has the list.
910 * @param to Last savegame version that has the list.
911 */
912 #define SLEG_CONDREFLIST(name, variable, type, from, to) SLEG_GENERAL(name, SL_REFLIST, variable, type, 0, from, to, 0)
913
914 /**
915 * Storage of a global vector of #SL_VAR elements in some savegame versions.
916 * @param name The name of the field.
917 * @param variable Name of the global variable.
918 * @param type Storage of the data in memory and in the savegame.
919 * @param from First savegame version that has the list.
920 * @param to Last savegame version that has the list.
921 */
922 #define SLEG_CONDVECTOR(name, variable, type, from, to) SLEG_GENERAL(name, SL_VECTOR, variable, type, 0, from, to, 0)
923
924 /**
925 * Storage of a list of structs in some savegame versions.
926 * @param name The name of the field.
927 * @param handler SaveLoadHandler for the list of structs.
928 * @param from First savegame version that has the list.
929 * @param to Last savegame version that has the list.
930 */
931 #define SLEG_CONDSTRUCTLIST(name, handler, from, to) SaveLoad {name, SL_STRUCTLIST, 0, 0, from, to, 0, nullptr, 0, std::make_shared<handler>()}
932
933 /**
934 * Storage of a global variable in every savegame version.
935 * @param name The name of the field.
936 * @param variable Name of the global variable.
937 * @param type Storage of the data in memory and in the savegame.
938 */
939 #define SLEG_VAR(name, variable, type) SLEG_CONDVAR(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
940
941 /**
942 * Storage of a global reference in every savegame version.
943 * @param name The name of the field.
944 * @param variable Name of the global variable.
945 * @param type Storage of the data in memory and in the savegame.
946 */
947 #define SLEG_REF(name, variable, type) SLEG_CONDREF(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
948
949 /**
950 * Storage of a global fixed-size array of #SL_VAR elements in every savegame version.
951 * @param name The name of the field.
952 * @param variable Name of the global variable.
953 * @param type Storage of the data in memory and in the savegame.
954 */
955 #define SLEG_ARR(name, variable, type) SLEG_CONDARR(name, variable, type, lengthof(variable), SL_MIN_VERSION, SL_MAX_VERSION)
956
957 /**
958 * Storage of a global string in every savegame version.
959 * @param name The name of the field.
960 * @param variable Name of the global variable.
961 * @param type Storage of the data in memory and in the savegame.
962 */
963 #define SLEG_STR(name, variable, type) SLEG_CONDSTR(name, variable, type, sizeof(variable), SL_MIN_VERSION, SL_MAX_VERSION)
964
965 /**
966 * Storage of a global \c std::string in every savegame version.
967 * @param name The name of the field.
968 * @param variable Name of the global variable.
969 * @param type Storage of the data in memory and in the savegame.
970 */
971 #define SLEG_SSTR(name, variable, type) SLEG_CONDSSTR(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
972
973 /**
974 * Storage of a structs in every savegame version.
975 * @param name The name of the field.
976 * @param handler SaveLoadHandler for the structs.
977 */
978 #define SLEG_STRUCT(name, handler) SLEG_CONDSTRUCT(name, handler, SL_MIN_VERSION, SL_MAX_VERSION)
979
980 /**
981 * Storage of a global reference list in every savegame version.
982 * @param name The name of the field.
983 * @param variable Name of the global variable.
984 * @param type Storage of the data in memory and in the savegame.
985 */
986 #define SLEG_REFLIST(name, variable, type) SLEG_CONDREFLIST(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
987
988 /**
989 * Storage of a global vector of #SL_VAR elements in every savegame version.
990 * @param name The name of the field.
991 * @param variable Name of the global variable.
992 * @param type Storage of the data in memory and in the savegame.
993 */
994 #define SLEG_VECTOR(name, variable, type) SLEG_CONDVECTOR(name, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
995
996 /**
997 * Storage of a list of structs in every savegame version.
998 * @param name The name of the field.
999 * @param handler SaveLoadHandler for the list of structs.
1000 */
1001 #define SLEG_STRUCTLIST(name, handler) SLEG_CONDSTRUCTLIST(name, handler, SL_MIN_VERSION, SL_MAX_VERSION)
1002
1003 /**
1004 * Field name where the real SaveLoad can be located.
1005 * @param name The name of the field.
1006 */
1007 #define SLC_VAR(name) {name, 0, SL_MIN_VERSION, SL_MAX_VERSION}
1008
1009 /**
1010 * Empty space in every savegame version.
1011 * @param length Length of the empty space.
1012 * @param from First savegame version that has the empty space.
1013 * @param to Last savegame version that has the empty space.
1014 */
1015 #define SLC_NULL(length, from, to) {{}, length, from, to}
1016
1017 /** End marker of compat variables save or load. */
1018 #define SLC_END() {{}, 0, SL_MIN_VERSION, SL_MIN_VERSION}
1019
1020 /**
1021 * Checks whether the savegame is below \a major.\a minor.
1022 * @param major Major number of the version to check against.
1023 * @param minor Minor number of the version to check against. If \a minor is 0 or not specified, only the major number is checked.
1024 * @return Savegame version is earlier than the specified version.
1025 */
1026 static inline bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor = 0)
1027 {
1028 extern SaveLoadVersion _sl_version;
1029 extern byte _sl_minor_version;
1030 return _sl_version < major || (minor > 0 && _sl_version == major && _sl_minor_version < minor);
1031 }
1032
1033 /**
1034 * Checks whether the savegame is below or at \a major. This should be used to repair data from existing
1035 * savegames which is no longer corrupted in new savegames, but for which otherwise no savegame
1036 * bump is required.
1037 * @param major Major number of the version to check against.
1038 * @return Savegame version is at most the specified version.
1039 */
IsSavegameVersionBeforeOrAt(SaveLoadVersion major)1040 static inline bool IsSavegameVersionBeforeOrAt(SaveLoadVersion major)
1041 {
1042 extern SaveLoadVersion _sl_version;
1043 return _sl_version <= major;
1044 }
1045
1046 /**
1047 * Checks if some version from/to combination falls within the range of the
1048 * active savegame version.
1049 * @param version_from Inclusive savegame version lower bound.
1050 * @param version_to Exclusive savegame version upper bound. SL_MAX_VERSION if no upper bound.
1051 * @return Active savegame version falls within the given range.
1052 */
SlIsObjectCurrentlyValid(SaveLoadVersion version_from,SaveLoadVersion version_to)1053 static inline bool SlIsObjectCurrentlyValid(SaveLoadVersion version_from, SaveLoadVersion version_to)
1054 {
1055 extern const SaveLoadVersion SAVEGAME_VERSION;
1056 return version_from <= SAVEGAME_VERSION && SAVEGAME_VERSION < version_to;
1057 }
1058
1059 /**
1060 * Get the NumberType of a setting. This describes the integer type
1061 * as it is represented in memory
1062 * @param type VarType holding information about the variable-type
1063 * @return the SLE_VAR_* part of a variable-type description
1064 */
GetVarMemType(VarType type)1065 static inline VarType GetVarMemType(VarType type)
1066 {
1067 return type & 0xF0; // GB(type, 4, 4) << 4;
1068 }
1069
1070 /**
1071 * Get the FileType of a setting. This describes the integer type
1072 * as it is represented in a savegame/file
1073 * @param type VarType holding information about the file-type
1074 * @return the SLE_FILE_* part of a variable-type description
1075 */
GetVarFileType(VarType type)1076 static inline VarType GetVarFileType(VarType type)
1077 {
1078 return type & 0xF; // GB(type, 0, 4);
1079 }
1080
1081 /**
1082 * Check if the given saveload type is a numeric type.
1083 * @param conv the type to check
1084 * @return True if it's a numeric type.
1085 */
IsNumericType(VarType conv)1086 static inline bool IsNumericType(VarType conv)
1087 {
1088 return GetVarMemType(conv) <= SLE_VAR_U64;
1089 }
1090
1091 /**
1092 * Get the address of the variable. Null-variables don't have an address,
1093 * everything else has a callback function that returns the address based
1094 * on the saveload data and the current object for non-globals.
1095 */
GetVariableAddress(const void * object,const SaveLoad & sld)1096 static inline void *GetVariableAddress(const void *object, const SaveLoad &sld)
1097 {
1098 /* Entry is a null-variable, mostly used to read old savegames etc. */
1099 if (GetVarMemType(sld.conv) == SLE_VAR_NULL) {
1100 assert(sld.address_proc == nullptr);
1101 return nullptr;
1102 }
1103
1104 /* Everything else should be a non-null pointer. */
1105 assert(sld.address_proc != nullptr);
1106 return sld.address_proc(const_cast<void *>(object), sld.extra_data);
1107 }
1108
1109 int64 ReadValue(const void *ptr, VarType conv);
1110 void WriteValue(void *ptr, VarType conv, int64 val);
1111
1112 void SlSetArrayIndex(uint index);
1113 int SlIterateArray();
1114
1115 void SlSetStructListLength(size_t length);
1116 size_t SlGetStructListLength(size_t limit);
1117
1118 void SlAutolength(AutolengthProc *proc, void *arg);
1119 size_t SlGetFieldLength();
1120 void SlSetLength(size_t length);
1121 size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld);
1122 size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt);
1123
1124 byte SlReadByte();
1125 void SlWriteByte(byte b);
1126
1127 void SlGlobList(const SaveLoadTable &slt);
1128 void SlCopy(void *object, size_t length, VarType conv);
1129 std::vector<SaveLoad> SlTableHeader(const SaveLoadTable &slt);
1130 std::vector<SaveLoad> SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct);
1131 void SlObject(void *object, const SaveLoadTable &slt);
1132 void NORETURN SlError(StringID string, const char *extra_msg = nullptr);
1133 void NORETURN SlErrorCorrupt(const char *msg);
1134 void NORETURN SlErrorCorruptFmt(const char *format, ...) WARN_FORMAT(1, 2);
1135
1136 bool SaveloadCrashWithMissingNewGRFs();
1137
1138 /**
1139 * Read in bytes from the file/data structure but don't do
1140 * anything with them, discarding them in effect
1141 * @param length The amount of bytes that is being treated this way
1142 */
SlSkipBytes(size_t length)1143 static inline void SlSkipBytes(size_t length)
1144 {
1145 for (; length != 0; length--) SlReadByte();
1146 }
1147
1148 extern std::string _savegame_format;
1149 extern bool _do_autosave;
1150
1151 #endif /* SAVELOAD_H */
1152