(svn r27985) -Codechange: Convert VA2 switches into ones with non-overlapping ranges...
[openttd.git] / src / saveload / saveload.cpp
blob2c00ecc4a1e1d8df2f9319d54da7dd26e4b877b4
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /**
11 * @file saveload.cpp
12 * All actions handling saving and loading goes on in this file. The general actions
13 * are as follows for saving a game (loading is analogous):
14 * <ol>
15 * <li>initialize the writer by creating a temporary memory-buffer for it
16 * <li>go through all to-be saved elements, each 'chunk' (#ChunkHandler) prefixed by a label
17 * <li>use their description array (#SaveLoad) to know what elements to save and in what version
18 * of the game it was active (used when loading)
19 * <li>write all data byte-by-byte to the temporary buffer so it is endian-safe
20 * <li>when the buffer is full; flush it to the output (eg save to file) (_sl.buf, _sl.bufp, _sl.bufe)
21 * <li>repeat this until everything is done, and flush any remaining output to file
22 * </ol>
24 #include "../stdafx.h"
25 #include "../debug.h"
26 #include "../station_base.h"
27 #include "../thread/thread.h"
28 #include "../town.h"
29 #include "../network/network.h"
30 #include "../window_func.h"
31 #include "../strings_func.h"
32 #include "../core/endian_func.hpp"
33 #include "../vehicle_base.h"
34 #include "../company_func.h"
35 #include "../date_func.h"
36 #include "../autoreplace_base.h"
37 #include "../roadstop_base.h"
38 #include "../linkgraph/linkgraph.h"
39 #include "../linkgraph/linkgraphjob.h"
40 #include "../statusbar_gui.h"
41 #include "../fileio_func.h"
42 #include "../gamelog.h"
43 #include "../string_func.h"
44 #include "../fios.h"
45 #include "../error.h"
47 #include "table/strings.h"
49 #include "saveload_internal.h"
50 #include "saveload_filter.h"
52 #include "../safeguards.h"
55 * Previous savegame versions, the trunk revision where they were
56 * introduced and the released version that had that particular
57 * savegame version.
58 * Up to savegame version 18 there is a minor version as well.
60 * 1.0 0.1.x, 0.2.x
61 * 2.0 0.3.0
62 * 2.1 0.3.1, 0.3.2
63 * 3.x lost
64 * 4.0 1
65 * 4.1 122 0.3.3, 0.3.4
66 * 4.2 1222 0.3.5
67 * 4.3 1417
68 * 4.4 1426
69 * 5.0 1429
70 * 5.1 1440
71 * 5.2 1525 0.3.6
72 * 6.0 1721
73 * 6.1 1768
74 * 7.0 1770
75 * 8.0 1786
76 * 9.0 1909
77 * 10.0 2030
78 * 11.0 2033
79 * 11.1 2041
80 * 12.1 2046
81 * 13.1 2080 0.4.0, 0.4.0.1
82 * 14.0 2441
83 * 15.0 2499
84 * 16.0 2817
85 * 16.1 3155
86 * 17.0 3212
87 * 17.1 3218
88 * 18 3227
89 * 19 3396
90 * 20 3403
91 * 21 3472 0.4.x
92 * 22 3726
93 * 23 3915
94 * 24 4150
95 * 25 4259
96 * 26 4466
97 * 27 4757
98 * 28 4987
99 * 29 5070
100 * 30 5946
101 * 31 5999
102 * 32 6001
103 * 33 6440
104 * 34 6455
105 * 35 6602
106 * 36 6624
107 * 37 7182
108 * 38 7195
109 * 39 7269
110 * 40 7326
111 * 41 7348 0.5.x
112 * 42 7573
113 * 43 7642
114 * 44 8144
115 * 45 8501
116 * 46 8705
117 * 47 8735
118 * 48 8935
119 * 49 8969
120 * 50 8973
121 * 51 8978
122 * 52 9066
123 * 53 9316
124 * 54 9613
125 * 55 9638
126 * 56 9667
127 * 57 9691
128 * 58 9762
129 * 59 9779
130 * 60 9874
131 * 61 9892
132 * 62 9905
133 * 63 9956
134 * 64 10006
135 * 65 10210
136 * 66 10211
137 * 67 10236
138 * 68 10266
139 * 69 10319
140 * 70 10541
141 * 71 10567
142 * 72 10601
143 * 73 10903
144 * 74 11030
145 * 75 11107
146 * 76 11139
147 * 77 11172
148 * 78 11176
149 * 79 11188
150 * 80 11228
151 * 81 11244
152 * 82 11410
153 * 83 11589
154 * 84 11822
155 * 85 11874
156 * 86 12042
157 * 87 12129
158 * 88 12134
159 * 89 12160
160 * 90 12293
161 * 91 12347
162 * 92 12381 0.6.x
163 * 93 12648
164 * 94 12816
165 * 95 12924
166 * 96 13226
167 * 97 13256
168 * 98 13375
169 * 99 13838
170 * 100 13952
171 * 101 14233
172 * 102 14332
173 * 103 14598
174 * 104 14735
175 * 105 14803
176 * 106 14919
177 * 107 15027
178 * 108 15045
179 * 109 15075
180 * 110 15148
181 * 111 15190
182 * 112 15290
183 * 113 15340
184 * 114 15601
185 * 115 15695
186 * 116 15893 0.7.x
187 * 117 16037
188 * 118 16129
189 * 119 16242
190 * 120 16439
191 * 121 16694
192 * 122 16855
193 * 123 16909
194 * 124 16993
195 * 125 17113
196 * 126 17433
197 * 127 17439
198 * 128 18281
199 * 129 18292
200 * 130 18404
201 * 131 18481
202 * 132 18522
203 * 133 18674
204 * 134 18703
205 * 135 18719
206 * 136 18764
207 * 137 18912
208 * 138 18942 1.0.x
209 * 139 19346
210 * 140 19382
211 * 141 19799
212 * 142 20003
213 * 143 20048
214 * 144 20334
215 * 145 20376
216 * 146 20446
217 * 147 20621
218 * 148 20659
219 * 149 20832
220 * 150 20857
221 * 151 20918
222 * 152 21171
223 * 153 21263
224 * 154 21426
225 * 155 21453
226 * 156 21728
227 * 157 21862
228 * 158 21933
229 * 159 21962
230 * 160 21974 1.1.x
231 * 161 22567
232 * 162 22713
233 * 163 22767
234 * 164 23290
235 * 165 23304
236 * 166 23415
237 * 167 23504
238 * 168 23637
239 * 169 23816
240 * 170 23826
241 * 171 23835
242 * 172 23947
243 * 173 23967 1.2.0-RC1
244 * 174 23973 1.2.x
245 * 175 24136
246 * 176 24446
247 * 177 24619
248 * 178 24789
249 * 179 24810
250 * 180 24998 1.3.x
251 * 181 25012
252 * 182 25296
253 * 183 25363
254 * 184 25508
255 * 185 25620
256 * 186 25833
257 * 187 25899
258 * 188 26169 1.4.x
259 * 189 26450
260 * 190 26547
261 * 191 26646
262 * 192 26700
263 * 193 26802
264 * 194 26881 1.5.x, 1.6.0
265 * 195 27572 1.6.x
266 * 196 27778 1.7.x
267 * 197 27978
269 extern const uint16 SAVEGAME_VERSION = 197; ///< Current savegame version of OpenTTD.
271 SavegameType _savegame_type; ///< type of savegame we are loading
272 FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop.
274 uint32 _ttdp_version; ///< version of TTDP savegame (if applicable)
275 uint16 _sl_version; ///< the major savegame version identifier
276 byte _sl_minor_version; ///< the minor savegame version, DO NOT USE!
277 char _savegame_format[8]; ///< how to compress savegames
278 bool _do_autosave; ///< are we doing an autosave at the moment?
280 /** What are we currently doing? */
281 enum SaveLoadAction {
282 SLA_LOAD, ///< loading
283 SLA_SAVE, ///< saving
284 SLA_PTRS, ///< fixing pointers
285 SLA_NULL, ///< null all pointers (on loading error)
286 SLA_LOAD_CHECK, ///< partial loading into #_load_check_data
289 enum NeedLength {
290 NL_NONE = 0, ///< not working in NeedLength mode
291 NL_WANTLENGTH = 1, ///< writing length and data
292 NL_CALCLENGTH = 2, ///< need to calculate the length
295 /** Save in chunks of 128 KiB. */
296 static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
298 /** A buffer for reading (and buffering) savegame data. */
299 struct ReadBuffer {
300 byte buf[MEMORY_CHUNK_SIZE]; ///< Buffer we're going to read from.
301 byte *bufp; ///< Location we're at reading the buffer.
302 byte *bufe; ///< End of the buffer we can read from.
303 LoadFilter *reader; ///< The filter used to actually read.
304 size_t read; ///< The amount of read bytes so far from the filter.
307 * Initialise our variables.
308 * @param reader The filter to actually read data.
310 ReadBuffer(LoadFilter *reader) : bufp(NULL), bufe(NULL), reader(reader), read(0)
314 inline byte ReadByte()
316 if (this->bufp == this->bufe) {
317 size_t len = this->reader->Read(this->buf, lengthof(this->buf));
318 if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
320 this->read += len;
321 this->bufp = this->buf;
322 this->bufe = this->buf + len;
325 return *this->bufp++;
329 * Get the size of the memory dump made so far.
330 * @return The size.
332 size_t GetSize() const
334 return this->read - (this->bufe - this->bufp);
339 /** Container for dumping the savegame (quickly) to memory. */
340 struct MemoryDumper {
341 AutoFreeSmallVector<byte *, 16> blocks; ///< Buffer with blocks of allocated memory.
342 byte *buf; ///< Buffer we're going to write to.
343 byte *bufe; ///< End of the buffer we write to.
345 /** Initialise our variables. */
346 MemoryDumper() : buf(NULL), bufe(NULL)
351 * Write a single byte into the dumper.
352 * @param b The byte to write.
354 inline void WriteByte(byte b)
356 /* Are we at the end of this chunk? */
357 if (this->buf == this->bufe) {
358 this->buf = CallocT<byte>(MEMORY_CHUNK_SIZE);
359 *this->blocks.Append() = this->buf;
360 this->bufe = this->buf + MEMORY_CHUNK_SIZE;
363 *this->buf++ = b;
367 * Flush this dumper into a writer.
368 * @param writer The filter we want to use.
370 void Flush(SaveFilter *writer)
372 uint i = 0;
373 size_t t = this->GetSize();
375 while (t > 0) {
376 size_t to_write = min(MEMORY_CHUNK_SIZE, t);
378 writer->Write(this->blocks[i++], to_write);
379 t -= to_write;
382 writer->Finish();
386 * Get the size of the memory dump made so far.
387 * @return The size.
389 size_t GetSize() const
391 return this->blocks.Length() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
395 /** The saveload struct, containing reader-writer functions, buffer, version, etc. */
396 struct SaveLoadParams {
397 SaveLoadAction action; ///< are we doing a save or a load atm.
398 NeedLength need_length; ///< working in NeedLength (Autolength) mode?
399 byte block_mode; ///< ???
400 bool error; ///< did an error occur or not
402 size_t obj_len; ///< the length of the current object we are busy with
403 int array_index, last_array_index; ///< in the case of an array, the current and last positions
405 MemoryDumper *dumper; ///< Memory dumper to write the savegame to.
406 SaveFilter *sf; ///< Filter to write the savegame to.
408 ReadBuffer *reader; ///< Savegame reading buffer.
409 LoadFilter *lf; ///< Filter to read the savegame from.
411 StringID error_str; ///< the translatable error message to show
412 char *extra_msg; ///< the error message
414 byte ff_state; ///< The state of fast-forward when saving started.
415 bool saveinprogress; ///< Whether there is currently a save in progress.
418 static SaveLoadParams _sl; ///< Parameters used for/at saveload.
420 /* these define the chunks */
421 extern const ChunkHandler _gamelog_chunk_handlers[];
422 extern const ChunkHandler _map_chunk_handlers[];
423 extern const ChunkHandler _misc_chunk_handlers[];
424 extern const ChunkHandler _name_chunk_handlers[];
425 extern const ChunkHandler _cheat_chunk_handlers[] ;
426 extern const ChunkHandler _setting_chunk_handlers[];
427 extern const ChunkHandler _company_chunk_handlers[];
428 extern const ChunkHandler _engine_chunk_handlers[];
429 extern const ChunkHandler _veh_chunk_handlers[];
430 extern const ChunkHandler _waypoint_chunk_handlers[];
431 extern const ChunkHandler _depot_chunk_handlers[];
432 extern const ChunkHandler _order_chunk_handlers[];
433 extern const ChunkHandler _town_chunk_handlers[];
434 extern const ChunkHandler _sign_chunk_handlers[];
435 extern const ChunkHandler _station_chunk_handlers[];
436 extern const ChunkHandler _industry_chunk_handlers[];
437 extern const ChunkHandler _economy_chunk_handlers[];
438 extern const ChunkHandler _subsidy_chunk_handlers[];
439 extern const ChunkHandler _cargomonitor_chunk_handlers[];
440 extern const ChunkHandler _goal_chunk_handlers[];
441 extern const ChunkHandler _story_page_chunk_handlers[];
442 extern const ChunkHandler _ai_chunk_handlers[];
443 extern const ChunkHandler _game_chunk_handlers[];
444 extern const ChunkHandler _animated_tile_chunk_handlers[];
445 extern const ChunkHandler _newgrf_chunk_handlers[];
446 extern const ChunkHandler _group_chunk_handlers[];
447 extern const ChunkHandler _cargopacket_chunk_handlers[];
448 extern const ChunkHandler _autoreplace_chunk_handlers[];
449 extern const ChunkHandler _labelmaps_chunk_handlers[];
450 extern const ChunkHandler _linkgraph_chunk_handlers[];
451 extern const ChunkHandler _airport_chunk_handlers[];
452 extern const ChunkHandler _object_chunk_handlers[];
453 extern const ChunkHandler _persistent_storage_chunk_handlers[];
455 /** Array of all chunks in a savegame, \c NULL terminated. */
456 static const ChunkHandler * const _chunk_handlers[] = {
457 _gamelog_chunk_handlers,
458 _map_chunk_handlers,
459 _misc_chunk_handlers,
460 _name_chunk_handlers,
461 _cheat_chunk_handlers,
462 _setting_chunk_handlers,
463 _veh_chunk_handlers,
464 _waypoint_chunk_handlers,
465 _depot_chunk_handlers,
466 _order_chunk_handlers,
467 _industry_chunk_handlers,
468 _economy_chunk_handlers,
469 _subsidy_chunk_handlers,
470 _cargomonitor_chunk_handlers,
471 _goal_chunk_handlers,
472 _story_page_chunk_handlers,
473 _engine_chunk_handlers,
474 _town_chunk_handlers,
475 _sign_chunk_handlers,
476 _station_chunk_handlers,
477 _company_chunk_handlers,
478 _ai_chunk_handlers,
479 _game_chunk_handlers,
480 _animated_tile_chunk_handlers,
481 _newgrf_chunk_handlers,
482 _group_chunk_handlers,
483 _cargopacket_chunk_handlers,
484 _autoreplace_chunk_handlers,
485 _labelmaps_chunk_handlers,
486 _linkgraph_chunk_handlers,
487 _airport_chunk_handlers,
488 _object_chunk_handlers,
489 _persistent_storage_chunk_handlers,
490 NULL,
494 * Iterate over all chunk handlers.
495 * @param ch the chunk handler iterator
497 #define FOR_ALL_CHUNK_HANDLERS(ch) \
498 for (const ChunkHandler * const *chsc = _chunk_handlers; *chsc != NULL; chsc++) \
499 for (const ChunkHandler *ch = *chsc; ch != NULL; ch = (ch->flags & CH_LAST) ? NULL : ch + 1)
501 /** Null all pointers (convert index -> NULL) */
502 static void SlNullPointers()
504 _sl.action = SLA_NULL;
506 /* We don't want any savegame conversion code to run
507 * during NULLing; especially those that try to get
508 * pointers from other pools. */
509 _sl_version = SAVEGAME_VERSION;
511 DEBUG(sl, 1, "Nulling pointers");
513 FOR_ALL_CHUNK_HANDLERS(ch) {
514 if (ch->ptrs_proc != NULL) {
515 DEBUG(sl, 2, "Nulling pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
516 ch->ptrs_proc();
520 DEBUG(sl, 1, "All pointers nulled");
522 assert(_sl.action == SLA_NULL);
526 * Error handler. Sets everything up to show an error message and to clean
527 * up the mess of a partial savegame load.
528 * @param string The translatable error message to show.
529 * @param extra_msg An extra error message coming from one of the APIs.
530 * @note This function does never return as it throws an exception to
531 * break out of all the saveload code.
533 void NORETURN SlError(StringID string, const char *extra_msg)
535 /* Distinguish between loading into _load_check_data vs. normal save/load. */
536 if (_sl.action == SLA_LOAD_CHECK) {
537 _load_check_data.error = string;
538 free(_load_check_data.error_data);
539 _load_check_data.error_data = (extra_msg == NULL) ? NULL : stredup(extra_msg);
540 } else {
541 _sl.error_str = string;
542 free(_sl.extra_msg);
543 _sl.extra_msg = (extra_msg == NULL) ? NULL : stredup(extra_msg);
546 /* We have to NULL all pointers here; we might be in a state where
547 * the pointers are actually filled with indices, which means that
548 * when we access them during cleaning the pool dereferences of
549 * those indices will be made with segmentation faults as result. */
550 if (_sl.action == SLA_LOAD || _sl.action == SLA_PTRS) SlNullPointers();
551 throw std::exception();
555 * Error handler for corrupt savegames. Sets everything up to show the
556 * error message and to clean up the mess of a partial savegame load.
557 * @param msg Location the corruption has been spotted.
558 * @note This function does never return as it throws an exception to
559 * break out of all the saveload code.
561 void NORETURN SlErrorCorrupt(const char *msg)
563 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
567 typedef void (*AsyncSaveFinishProc)(); ///< Callback for when the savegame loading is finished.
568 static AsyncSaveFinishProc _async_save_finish = NULL; ///< Callback to call when the savegame loading is finished.
569 static ThreadObject *_save_thread; ///< The thread we're using to compress and write a savegame
572 * Called by save thread to tell we finished saving.
573 * @param proc The callback to call when saving is done.
575 static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
577 if (_exit_game) return;
578 while (_async_save_finish != NULL) CSleep(10);
580 _async_save_finish = proc;
584 * Handle async save finishes.
586 void ProcessAsyncSaveFinish()
588 if (_async_save_finish == NULL) return;
590 _async_save_finish();
592 _async_save_finish = NULL;
594 if (_save_thread != NULL) {
595 _save_thread->Join();
596 delete _save_thread;
597 _save_thread = NULL;
602 * Wrapper for reading a byte from the buffer.
603 * @return The read byte.
605 byte SlReadByte()
607 return _sl.reader->ReadByte();
611 * Wrapper for writing a byte to the dumper.
612 * @param b The byte to write.
614 void SlWriteByte(byte b)
616 _sl.dumper->WriteByte(b);
619 static inline int SlReadUint16()
621 int x = SlReadByte() << 8;
622 return x | SlReadByte();
625 static inline uint32 SlReadUint32()
627 uint32 x = SlReadUint16() << 16;
628 return x | SlReadUint16();
631 static inline uint64 SlReadUint64()
633 uint32 x = SlReadUint32();
634 uint32 y = SlReadUint32();
635 return (uint64)x << 32 | y;
638 static inline void SlWriteUint16(uint16 v)
640 SlWriteByte(GB(v, 8, 8));
641 SlWriteByte(GB(v, 0, 8));
644 static inline void SlWriteUint32(uint32 v)
646 SlWriteUint16(GB(v, 16, 16));
647 SlWriteUint16(GB(v, 0, 16));
650 static inline void SlWriteUint64(uint64 x)
652 SlWriteUint32((uint32)(x >> 32));
653 SlWriteUint32((uint32)x);
657 * Read in bytes from the file/data structure but don't do
658 * anything with them, discarding them in effect
659 * @param length The amount of bytes that is being treated this way
661 static inline void SlSkipBytes(size_t length)
663 for (; length != 0; length--) SlReadByte();
667 * Read in the header descriptor of an object or an array.
668 * If the highest bit is set (7), then the index is bigger than 127
669 * elements, so use the next byte to read in the real value.
670 * The actual value is then both bytes added with the first shifted
671 * 8 bits to the left, and dropping the highest bit (which only indicated a big index).
672 * x = ((x & 0x7F) << 8) + SlReadByte();
673 * @return Return the value of the index
675 static uint SlReadSimpleGamma()
677 uint i = SlReadByte();
678 if (HasBit(i, 7)) {
679 i &= ~0x80;
680 if (HasBit(i, 6)) {
681 i &= ~0x40;
682 if (HasBit(i, 5)) {
683 i &= ~0x20;
684 if (HasBit(i, 4)) {
685 i &= ~0x10;
686 if (HasBit(i, 3)) {
687 SlErrorCorrupt("Unsupported gamma");
689 i = SlReadByte(); // 32 bits only.
691 i = (i << 8) | SlReadByte();
693 i = (i << 8) | SlReadByte();
695 i = (i << 8) | SlReadByte();
697 return i;
701 * Write the header descriptor of an object or an array.
702 * If the element is bigger than 127, use 2 bytes for saving
703 * and use the highest byte of the first written one as a notice
704 * that the length consists of 2 bytes, etc.. like this:
705 * 0xxxxxxx
706 * 10xxxxxx xxxxxxxx
707 * 110xxxxx xxxxxxxx xxxxxxxx
708 * 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
709 * 11110--- xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
710 * We could extend the scheme ad infinum to support arbitrarily
711 * large chunks, but as sizeof(size_t) == 4 is still very common
712 * we don't support anything above 32 bits. That's why in the last
713 * case the 3 most significant bits are unused.
714 * @param i Index being written
717 static void SlWriteSimpleGamma(size_t i)
719 if (i >= (1 << 7)) {
720 if (i >= (1 << 14)) {
721 if (i >= (1 << 21)) {
722 if (i >= (1 << 28)) {
723 assert(i <= UINT32_MAX); // We can only support 32 bits for now.
724 SlWriteByte((byte)(0xF0));
725 SlWriteByte((byte)(i >> 24));
726 } else {
727 SlWriteByte((byte)(0xE0 | (i >> 24)));
729 SlWriteByte((byte)(i >> 16));
730 } else {
731 SlWriteByte((byte)(0xC0 | (i >> 16)));
733 SlWriteByte((byte)(i >> 8));
734 } else {
735 SlWriteByte((byte)(0x80 | (i >> 8)));
738 SlWriteByte((byte)i);
741 /** Return how many bytes used to encode a gamma value */
742 static inline uint SlGetGammaLength(size_t i)
744 return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
747 static inline uint SlReadSparseIndex()
749 return SlReadSimpleGamma();
752 static inline void SlWriteSparseIndex(uint index)
754 SlWriteSimpleGamma(index);
757 static inline uint SlReadArrayLength()
759 return SlReadSimpleGamma();
762 static inline void SlWriteArrayLength(size_t length)
764 SlWriteSimpleGamma(length);
767 static inline uint SlGetArrayLength(size_t length)
769 return SlGetGammaLength(length);
773 * Return the size in bytes of a certain type of normal/atomic variable
774 * as it appears in memory. See VarTypes
775 * @param conv VarType type of variable that is used for calculating the size
776 * @return Return the size of this type in bytes
778 static inline uint SlCalcConvMemLen(VarType conv)
780 static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
781 byte length = GB(conv, 4, 4);
783 switch (length << 4) {
784 case SLE_VAR_STRB:
785 case SLE_VAR_STRBQ:
786 case SLE_VAR_STR:
787 case SLE_VAR_STRQ:
788 return SlReadArrayLength();
790 default:
791 assert(length < lengthof(conv_mem_size));
792 return conv_mem_size[length];
797 * Return the size in bytes of a certain type of normal/atomic variable
798 * as it appears in a saved game. See VarTypes
799 * @param conv VarType type of variable that is used for calculating the size
800 * @return Return the size of this type in bytes
802 static inline byte SlCalcConvFileLen(VarType conv)
804 static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
805 byte length = GB(conv, 0, 4);
806 assert(length < lengthof(conv_file_size));
807 return conv_file_size[length];
810 /** Return the size in bytes of a reference (pointer) */
811 static inline size_t SlCalcRefLen()
813 return IsSavegameVersionBefore(69) ? 2 : 4;
816 void SlSetArrayIndex(uint index)
818 _sl.need_length = NL_WANTLENGTH;
819 _sl.array_index = index;
822 static size_t _next_offs;
825 * Iterate through the elements of an array and read the whole thing
826 * @return The index of the object, or -1 if we have reached the end of current block
828 int SlIterateArray()
830 int index;
832 /* After reading in the whole array inside the loop
833 * we must have read in all the data, so we must be at end of current block. */
834 if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) SlErrorCorrupt("Invalid chunk size");
836 for (;;) {
837 uint length = SlReadArrayLength();
838 if (length == 0) {
839 _next_offs = 0;
840 return -1;
843 _sl.obj_len = --length;
844 _next_offs = _sl.reader->GetSize() + length;
846 switch (_sl.block_mode) {
847 case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
848 case CH_ARRAY: index = _sl.array_index++; break;
849 default:
850 DEBUG(sl, 0, "SlIterateArray error");
851 return -1; // error
854 if (length != 0) return index;
859 * Skip an array or sparse array
861 void SlSkipArray()
863 while (SlIterateArray() != -1) {
864 SlSkipBytes(_next_offs - _sl.reader->GetSize());
869 * Sets the length of either a RIFF object or the number of items in an array.
870 * This lets us load an object or an array of arbitrary size
871 * @param length The length of the sought object/array
873 void SlSetLength(size_t length)
875 assert(_sl.action == SLA_SAVE);
877 switch (_sl.need_length) {
878 case NL_WANTLENGTH:
879 _sl.need_length = NL_NONE;
880 switch (_sl.block_mode) {
881 case CH_RIFF:
882 /* Ugly encoding of >16M RIFF chunks
883 * The lower 24 bits are normal
884 * The uppermost 4 bits are bits 24:27 */
885 assert(length < (1 << 28));
886 SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
887 break;
888 case CH_ARRAY:
889 assert(_sl.last_array_index <= _sl.array_index);
890 while (++_sl.last_array_index <= _sl.array_index) {
891 SlWriteArrayLength(1);
893 SlWriteArrayLength(length + 1);
894 break;
895 case CH_SPARSE_ARRAY:
896 SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
897 SlWriteSparseIndex(_sl.array_index);
898 break;
899 default: NOT_REACHED();
901 break;
903 case NL_CALCLENGTH:
904 _sl.obj_len += (int)length;
905 break;
907 default: NOT_REACHED();
912 * Save/Load bytes. These do not need to be converted to Little/Big Endian
913 * so directly write them or read them to/from file
914 * @param ptr The source or destination of the object being manipulated
915 * @param length number of bytes this fast CopyBytes lasts
917 static void SlCopyBytes(void *ptr, size_t length)
919 byte *p = (byte *)ptr;
921 switch (_sl.action) {
922 case SLA_LOAD_CHECK:
923 case SLA_LOAD:
924 for (; length != 0; length--) *p++ = SlReadByte();
925 break;
926 case SLA_SAVE:
927 for (; length != 0; length--) SlWriteByte(*p++);
928 break;
929 default: NOT_REACHED();
933 /** Get the length of the current object */
934 size_t SlGetFieldLength()
936 return _sl.obj_len;
940 * Return a signed-long version of the value of a setting
941 * @param ptr pointer to the variable
942 * @param conv type of variable, can be a non-clean
943 * type, eg one with other flags because it is parsed
944 * @return returns the value of the pointer-setting
946 int64 ReadValue(const void *ptr, VarType conv)
948 switch (GetVarMemType(conv)) {
949 case SLE_VAR_BL: return (*(const bool *)ptr != 0);
950 case SLE_VAR_I8: return *(const int8 *)ptr;
951 case SLE_VAR_U8: return *(const byte *)ptr;
952 case SLE_VAR_I16: return *(const int16 *)ptr;
953 case SLE_VAR_U16: return *(const uint16*)ptr;
954 case SLE_VAR_I32: return *(const int32 *)ptr;
955 case SLE_VAR_U32: return *(const uint32*)ptr;
956 case SLE_VAR_I64: return *(const int64 *)ptr;
957 case SLE_VAR_U64: return *(const uint64*)ptr;
958 case SLE_VAR_NULL:return 0;
959 default: NOT_REACHED();
964 * Write the value of a setting
965 * @param ptr pointer to the variable
966 * @param conv type of variable, can be a non-clean type, eg
967 * with other flags. It is parsed upon read
968 * @param val the new value being given to the variable
970 void WriteValue(void *ptr, VarType conv, int64 val)
972 switch (GetVarMemType(conv)) {
973 case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
974 case SLE_VAR_I8: *(int8 *)ptr = val; break;
975 case SLE_VAR_U8: *(byte *)ptr = val; break;
976 case SLE_VAR_I16: *(int16 *)ptr = val; break;
977 case SLE_VAR_U16: *(uint16*)ptr = val; break;
978 case SLE_VAR_I32: *(int32 *)ptr = val; break;
979 case SLE_VAR_U32: *(uint32*)ptr = val; break;
980 case SLE_VAR_I64: *(int64 *)ptr = val; break;
981 case SLE_VAR_U64: *(uint64*)ptr = val; break;
982 case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
983 case SLE_VAR_NULL: break;
984 default: NOT_REACHED();
989 * Handle all conversion and typechecking of variables here.
990 * In the case of saving, read in the actual value from the struct
991 * and then write them to file, endian safely. Loading a value
992 * goes exactly the opposite way
993 * @param ptr The object being filled/read
994 * @param conv VarType type of the current element of the struct
996 static void SlSaveLoadConv(void *ptr, VarType conv)
998 switch (_sl.action) {
999 case SLA_SAVE: {
1000 int64 x = ReadValue(ptr, conv);
1002 /* Write the value to the file and check if its value is in the desired range */
1003 switch (GetVarFileType(conv)) {
1004 case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
1005 case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
1006 case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
1007 case SLE_FILE_STRINGID:
1008 case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
1009 case SLE_FILE_I32:
1010 case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
1011 case SLE_FILE_I64:
1012 case SLE_FILE_U64: SlWriteUint64(x);break;
1013 default: NOT_REACHED();
1015 break;
1017 case SLA_LOAD_CHECK:
1018 case SLA_LOAD: {
1019 int64 x;
1020 /* Read a value from the file */
1021 switch (GetVarFileType(conv)) {
1022 case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
1023 case SLE_FILE_U8: x = (byte )SlReadByte(); break;
1024 case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
1025 case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
1026 case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
1027 case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
1028 case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
1029 case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
1030 case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
1031 default: NOT_REACHED();
1034 /* Write The value to the struct. These ARE endian safe. */
1035 WriteValue(ptr, conv, x);
1036 break;
1038 case SLA_PTRS: break;
1039 case SLA_NULL: break;
1040 default: NOT_REACHED();
1045 * Calculate the net length of a string. This is in almost all cases
1046 * just strlen(), but if the string is not properly terminated, we'll
1047 * resort to the maximum length of the buffer.
1048 * @param ptr pointer to the stringbuffer
1049 * @param length maximum length of the string (buffer). If -1 we don't care
1050 * about a maximum length, but take string length as it is.
1051 * @return return the net length of the string
1053 static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
1055 if (ptr == NULL) return 0;
1056 return min(strlen(ptr), length - 1);
1060 * Calculate the gross length of the string that it
1061 * will occupy in the savegame. This includes the real length, returned
1062 * by SlCalcNetStringLen and the length that the index will occupy.
1063 * @param ptr pointer to the stringbuffer
1064 * @param length maximum length of the string (buffer size, etc.)
1065 * @param conv type of data been used
1066 * @return return the gross length of the string
1068 static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
1070 size_t len;
1071 const char *str;
1073 switch (GetVarMemType(conv)) {
1074 default: NOT_REACHED();
1075 case SLE_VAR_STR:
1076 case SLE_VAR_STRQ:
1077 str = *(const char * const *)ptr;
1078 len = SIZE_MAX;
1079 break;
1080 case SLE_VAR_STRB:
1081 case SLE_VAR_STRBQ:
1082 str = (const char *)ptr;
1083 len = length;
1084 break;
1087 len = SlCalcNetStringLen(str, len);
1088 return len + SlGetArrayLength(len); // also include the length of the index
1092 * Save/Load a string.
1093 * @param ptr the string being manipulated
1094 * @param length of the string (full length)
1095 * @param conv must be SLE_FILE_STRING
1097 static void SlString(void *ptr, size_t length, VarType conv)
1099 switch (_sl.action) {
1100 case SLA_SAVE: {
1101 size_t len;
1102 switch (GetVarMemType(conv)) {
1103 default: NOT_REACHED();
1104 case SLE_VAR_STRB:
1105 case SLE_VAR_STRBQ:
1106 len = SlCalcNetStringLen((char *)ptr, length);
1107 break;
1108 case SLE_VAR_STR:
1109 case SLE_VAR_STRQ:
1110 ptr = *(char **)ptr;
1111 len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
1112 break;
1115 SlWriteArrayLength(len);
1116 SlCopyBytes(ptr, len);
1117 break;
1119 case SLA_LOAD_CHECK:
1120 case SLA_LOAD: {
1121 size_t len = SlReadArrayLength();
1123 switch (GetVarMemType(conv)) {
1124 default: NOT_REACHED();
1125 case SLE_VAR_STRB:
1126 case SLE_VAR_STRBQ:
1127 if (len >= length) {
1128 DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
1129 SlCopyBytes(ptr, length);
1130 SlSkipBytes(len - length);
1131 len = length - 1;
1132 } else {
1133 SlCopyBytes(ptr, len);
1135 break;
1136 case SLE_VAR_STR:
1137 case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
1138 free(*(char **)ptr);
1139 if (len == 0) {
1140 *(char **)ptr = NULL;
1141 return;
1142 } else {
1143 *(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
1144 ptr = *(char **)ptr;
1145 SlCopyBytes(ptr, len);
1147 break;
1150 ((char *)ptr)[len] = '\0'; // properly terminate the string
1151 StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK;
1152 if ((conv & SLF_ALLOW_CONTROL) != 0) {
1153 settings = settings | SVS_ALLOW_CONTROL_CODE;
1154 if (IsSavegameVersionBefore(169)) {
1155 str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
1158 if ((conv & SLF_ALLOW_NEWLINE) != 0) {
1159 settings = settings | SVS_ALLOW_NEWLINE;
1161 str_validate((char *)ptr, (char *)ptr + len, settings);
1162 break;
1164 case SLA_PTRS: break;
1165 case SLA_NULL: break;
1166 default: NOT_REACHED();
1171 * Return the size in bytes of a certain type of atomic array
1172 * @param length The length of the array counted in elements
1173 * @param conv VarType type of the variable that is used in calculating the size
1175 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
1177 return SlCalcConvFileLen(conv) * length;
1181 * Save/Load an array.
1182 * @param array The array being manipulated
1183 * @param length The length of the array in elements
1184 * @param conv VarType type of the atomic array (int, byte, uint64, etc.)
1186 void SlArray(void *array, size_t length, VarType conv)
1188 if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
1190 /* Automatically calculate the length? */
1191 if (_sl.need_length != NL_NONE) {
1192 SlSetLength(SlCalcArrayLen(length, conv));
1193 /* Determine length only? */
1194 if (_sl.need_length == NL_CALCLENGTH) return;
1197 /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1198 * as a byte-type. So detect this, and adjust array size accordingly */
1199 if (_sl.action != SLA_SAVE && _sl_version == 0) {
1200 /* all arrays except difficulty settings */
1201 if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1202 conv == SLE_INT32 || conv == SLE_UINT32) {
1203 SlCopyBytes(array, length * SlCalcConvFileLen(conv));
1204 return;
1206 /* used for conversion of Money 32bit->64bit */
1207 if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1208 for (uint i = 0; i < length; i++) {
1209 ((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
1211 return;
1215 /* If the size of elements is 1 byte both in file and memory, no special
1216 * conversion is needed, use specialized copy-copy function to speed up things */
1217 if (conv == SLE_INT8 || conv == SLE_UINT8) {
1218 SlCopyBytes(array, length);
1219 } else {
1220 byte *a = (byte*)array;
1221 byte mem_size = SlCalcConvMemLen(conv);
1223 for (; length != 0; length --) {
1224 SlSaveLoadConv(a, conv);
1225 a += mem_size; // get size
1232 * Pointers cannot be saved to a savegame, so this functions gets
1233 * the index of the item, and if not available, it hussles with
1234 * pointers (looks really bad :()
1235 * Remember that a NULL item has value 0, and all
1236 * indices have +1, so vehicle 0 is saved as index 1.
1237 * @param obj The object that we want to get the index of
1238 * @param rt SLRefType type of the object the index is being sought of
1239 * @return Return the pointer converted to an index of the type pointed to
1241 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1243 assert(_sl.action == SLA_SAVE);
1245 if (obj == NULL) return 0;
1247 switch (rt) {
1248 case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1249 case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1250 case REF_STATION: return ((const Station*)obj)->index + 1;
1251 case REF_TOWN: return ((const Town*)obj)->index + 1;
1252 case REF_ORDER: return ((const Order*)obj)->index + 1;
1253 case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1254 case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1255 case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1256 case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1257 case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1258 case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1259 case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1260 default: NOT_REACHED();
1265 * Pointers cannot be loaded from a savegame, so this function
1266 * gets the index from the savegame and returns the appropriate
1267 * pointer from the already loaded base.
1268 * Remember that an index of 0 is a NULL pointer so all indices
1269 * are +1 so vehicle 0 is saved as 1.
1270 * @param index The index that is being converted to a pointer
1271 * @param rt SLRefType type of the object the pointer is sought of
1272 * @return Return the index converted to a pointer of any type
1274 static void *IntToReference(size_t index, SLRefType rt)
1276 assert_compile(sizeof(size_t) <= sizeof(void *));
1278 assert(_sl.action == SLA_PTRS);
1280 /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1281 * and should be loaded like that */
1282 if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(4, 4)) {
1283 rt = REF_VEHICLE;
1286 /* No need to look up NULL pointers, just return immediately */
1287 if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return NULL;
1289 /* Correct index. Old vehicles were saved differently:
1290 * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1291 if (rt != REF_VEHICLE_OLD) index--;
1293 switch (rt) {
1294 case REF_ORDERLIST:
1295 if (OrderList::IsValidID(index)) return OrderList::Get(index);
1296 SlErrorCorrupt("Referencing invalid OrderList");
1298 case REF_ORDER:
1299 if (Order::IsValidID(index)) return Order::Get(index);
1300 /* in old versions, invalid order was used to mark end of order list */
1301 if (IsSavegameVersionBefore(5, 2)) return NULL;
1302 SlErrorCorrupt("Referencing invalid Order");
1304 case REF_VEHICLE_OLD:
1305 case REF_VEHICLE:
1306 if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1307 SlErrorCorrupt("Referencing invalid Vehicle");
1309 case REF_STATION:
1310 if (Station::IsValidID(index)) return Station::Get(index);
1311 SlErrorCorrupt("Referencing invalid Station");
1313 case REF_TOWN:
1314 if (Town::IsValidID(index)) return Town::Get(index);
1315 SlErrorCorrupt("Referencing invalid Town");
1317 case REF_ROADSTOPS:
1318 if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1319 SlErrorCorrupt("Referencing invalid RoadStop");
1321 case REF_ENGINE_RENEWS:
1322 if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1323 SlErrorCorrupt("Referencing invalid EngineRenew");
1325 case REF_CARGO_PACKET:
1326 if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1327 SlErrorCorrupt("Referencing invalid CargoPacket");
1329 case REF_STORAGE:
1330 if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1331 SlErrorCorrupt("Referencing invalid PersistentStorage");
1333 case REF_LINK_GRAPH:
1334 if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1335 SlErrorCorrupt("Referencing invalid LinkGraph");
1337 case REF_LINK_GRAPH_JOB:
1338 if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1339 SlErrorCorrupt("Referencing invalid LinkGraphJob");
1341 default: NOT_REACHED();
1346 * Return the size in bytes of a list
1347 * @param list The std::list to find the size of
1349 static inline size_t SlCalcListLen(const void *list)
1351 const std::list<void *> *l = (const std::list<void *> *) list;
1353 int type_size = IsSavegameVersionBefore(69) ? 2 : 4;
1354 /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1355 * of the list */
1356 return l->size() * type_size + type_size;
1361 * Save/Load a list.
1362 * @param list The list being manipulated
1363 * @param conv SLRefType type of the list (Vehicle *, Station *, etc)
1365 static void SlList(void *list, SLRefType conv)
1367 /* Automatically calculate the length? */
1368 if (_sl.need_length != NL_NONE) {
1369 SlSetLength(SlCalcListLen(list));
1370 /* Determine length only? */
1371 if (_sl.need_length == NL_CALCLENGTH) return;
1374 typedef std::list<void *> PtrList;
1375 PtrList *l = (PtrList *)list;
1377 switch (_sl.action) {
1378 case SLA_SAVE: {
1379 SlWriteUint32((uint32)l->size());
1381 PtrList::iterator iter;
1382 for (iter = l->begin(); iter != l->end(); ++iter) {
1383 void *ptr = *iter;
1384 SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
1386 break;
1388 case SLA_LOAD_CHECK:
1389 case SLA_LOAD: {
1390 size_t length = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1392 /* Load each reference and push to the end of the list */
1393 for (size_t i = 0; i < length; i++) {
1394 size_t data = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1395 l->push_back((void *)data);
1397 break;
1399 case SLA_PTRS: {
1400 PtrList temp = *l;
1402 l->clear();
1403 PtrList::iterator iter;
1404 for (iter = temp.begin(); iter != temp.end(); ++iter) {
1405 void *ptr = IntToReference((size_t)*iter, conv);
1406 l->push_back(ptr);
1408 break;
1410 case SLA_NULL:
1411 l->clear();
1412 break;
1413 default: NOT_REACHED();
1418 /** Are we going to save this object or not? */
1419 static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
1421 if (_sl_version < sld->version_from || _sl_version > sld->version_to) return false;
1422 if (sld->conv & SLF_NOT_IN_SAVE) return false;
1424 return true;
1428 * Are we going to load this variable when loading a savegame or not?
1429 * @note If the variable is skipped it is skipped in the savegame
1430 * bytestream itself as well, so there is no need to skip it somewhere else
1432 static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
1434 if ((sld->conv & SLF_NO_NETWORK_SYNC) && _sl.action != SLA_SAVE && _networking && !_network_server) {
1435 SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
1436 return true;
1439 return false;
1443 * Calculate the size of an object.
1444 * @param object to be measured
1445 * @param sld The SaveLoad description of the object so we know how to manipulate it
1446 * @return size of given object
1448 size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
1450 size_t length = 0;
1452 /* Need to determine the length and write a length tag. */
1453 for (; sld->cmd != SL_END; sld++) {
1454 length += SlCalcObjMemberLength(object, sld);
1456 return length;
1459 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
1461 assert(_sl.action == SLA_SAVE);
1463 switch (sld->cmd) {
1464 case SL_VAR:
1465 case SL_REF:
1466 case SL_ARR:
1467 case SL_STR:
1468 case SL_LST:
1469 /* CONDITIONAL saveload types depend on the savegame version */
1470 if (!SlIsObjectValidInSavegame(sld)) break;
1472 switch (sld->cmd) {
1473 case SL_VAR: return SlCalcConvFileLen(sld->conv);
1474 case SL_REF: return SlCalcRefLen();
1475 case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
1476 case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
1477 case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
1478 default: NOT_REACHED();
1480 break;
1481 case SL_WRITEBYTE: return 1; // a byte is logically of size 1
1482 case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
1483 case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
1484 default: NOT_REACHED();
1486 return 0;
1489 #ifdef OTTD_ASSERT
1492 * Check whether the variable size of the variable in the saveload configuration
1493 * matches with the actual variable size.
1494 * @param sld The saveload configuration to test.
1496 static bool IsVariableSizeRight(const SaveLoad *sld)
1498 switch (sld->cmd) {
1499 case SL_VAR:
1500 switch (GetVarMemType(sld->conv)) {
1501 case SLE_VAR_BL:
1502 return sld->size == sizeof(bool);
1503 case SLE_VAR_I8:
1504 case SLE_VAR_U8:
1505 return sld->size == sizeof(int8);
1506 case SLE_VAR_I16:
1507 case SLE_VAR_U16:
1508 return sld->size == sizeof(int16);
1509 case SLE_VAR_I32:
1510 case SLE_VAR_U32:
1511 return sld->size == sizeof(int32);
1512 case SLE_VAR_I64:
1513 case SLE_VAR_U64:
1514 return sld->size == sizeof(int64);
1515 default:
1516 return sld->size == sizeof(void *);
1518 case SL_REF:
1519 /* These should all be pointer sized. */
1520 return sld->size == sizeof(void *);
1522 case SL_STR:
1523 /* These should be pointer sized, or fixed array. */
1524 return sld->size == sizeof(void *) || sld->size == sld->length;
1526 default:
1527 return true;
1531 #endif /* OTTD_ASSERT */
1533 bool SlObjectMember(void *ptr, const SaveLoad *sld)
1535 #ifdef OTTD_ASSERT
1536 assert(IsVariableSizeRight(sld));
1537 #endif
1539 VarType conv = GB(sld->conv, 0, 8);
1540 switch (sld->cmd) {
1541 case SL_VAR:
1542 case SL_REF:
1543 case SL_ARR:
1544 case SL_STR:
1545 case SL_LST:
1546 /* CONDITIONAL saveload types depend on the savegame version */
1547 if (!SlIsObjectValidInSavegame(sld)) return false;
1548 if (SlSkipVariableOnLoad(sld)) return false;
1550 switch (sld->cmd) {
1551 case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1552 case SL_REF: // Reference variable, translate
1553 switch (_sl.action) {
1554 case SLA_SAVE:
1555 SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1556 break;
1557 case SLA_LOAD_CHECK:
1558 case SLA_LOAD:
1559 *(size_t *)ptr = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1560 break;
1561 case SLA_PTRS:
1562 *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1563 break;
1564 case SLA_NULL:
1565 *(void **)ptr = NULL;
1566 break;
1567 default: NOT_REACHED();
1569 break;
1570 case SL_ARR: SlArray(ptr, sld->length, conv); break;
1571 case SL_STR: SlString(ptr, sld->length, sld->conv); break;
1572 case SL_LST: SlList(ptr, (SLRefType)conv); break;
1573 default: NOT_REACHED();
1575 break;
1577 /* SL_WRITEBYTE translates a value of a variable to another one upon
1578 * saving or loading.
1579 * XXX - variable renaming abuse
1580 * game_value: the value of the variable ingame is abused by sld->version_from
1581 * file_value: the value of the variable in the savegame is abused by sld->version_to */
1582 case SL_WRITEBYTE:
1583 switch (_sl.action) {
1584 case SLA_SAVE: SlWriteByte(sld->version_to); break;
1585 case SLA_LOAD_CHECK:
1586 case SLA_LOAD: *(byte *)ptr = sld->version_from; break;
1587 case SLA_PTRS: break;
1588 case SLA_NULL: break;
1589 default: NOT_REACHED();
1591 break;
1593 /* SL_VEH_INCLUDE loads common code for vehicles */
1594 case SL_VEH_INCLUDE:
1595 SlObject(ptr, GetVehicleDescription(VEH_END));
1596 break;
1598 case SL_ST_INCLUDE:
1599 SlObject(ptr, GetBaseStationDescription());
1600 break;
1602 default: NOT_REACHED();
1604 return true;
1608 * Main SaveLoad function.
1609 * @param object The object that is being saved or loaded
1610 * @param sld The SaveLoad description of the object so we know how to manipulate it
1612 void SlObject(void *object, const SaveLoad *sld)
1614 /* Automatically calculate the length? */
1615 if (_sl.need_length != NL_NONE) {
1616 SlSetLength(SlCalcObjLength(object, sld));
1617 if (_sl.need_length == NL_CALCLENGTH) return;
1620 for (; sld->cmd != SL_END; sld++) {
1621 void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld);
1622 SlObjectMember(ptr, sld);
1627 * Save or Load (a list of) global variables
1628 * @param sldg The global variable that is being loaded or saved
1630 void SlGlobList(const SaveLoadGlobVarList *sldg)
1632 SlObject(NULL, (const SaveLoad*)sldg);
1636 * Do something of which I have no idea what it is :P
1637 * @param proc The callback procedure that is called
1638 * @param arg The variable that will be used for the callback procedure
1640 void SlAutolength(AutolengthProc *proc, void *arg)
1642 size_t offs;
1644 assert(_sl.action == SLA_SAVE);
1646 /* Tell it to calculate the length */
1647 _sl.need_length = NL_CALCLENGTH;
1648 _sl.obj_len = 0;
1649 proc(arg);
1651 /* Setup length */
1652 _sl.need_length = NL_WANTLENGTH;
1653 SlSetLength(_sl.obj_len);
1655 offs = _sl.dumper->GetSize() + _sl.obj_len;
1657 /* And write the stuff */
1658 proc(arg);
1660 if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
1664 * Load a chunk of data (eg vehicles, stations, etc.)
1665 * @param ch The chunkhandler that will be used for the operation
1667 static void SlLoadChunk(const ChunkHandler *ch)
1669 byte m = SlReadByte();
1670 size_t len;
1671 size_t endoffs;
1673 _sl.block_mode = m;
1674 _sl.obj_len = 0;
1676 switch (m) {
1677 case CH_ARRAY:
1678 _sl.array_index = 0;
1679 ch->load_proc();
1680 if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1681 break;
1682 case CH_SPARSE_ARRAY:
1683 ch->load_proc();
1684 if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1685 break;
1686 default:
1687 if ((m & 0xF) == CH_RIFF) {
1688 /* Read length */
1689 len = (SlReadByte() << 16) | ((m >> 4) << 24);
1690 len += SlReadUint16();
1691 _sl.obj_len = len;
1692 endoffs = _sl.reader->GetSize() + len;
1693 ch->load_proc();
1694 if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1695 } else {
1696 SlErrorCorrupt("Invalid chunk type");
1698 break;
1703 * Load a chunk of data for checking savegames.
1704 * If the chunkhandler is NULL, the chunk is skipped.
1705 * @param ch The chunkhandler that will be used for the operation
1707 static void SlLoadCheckChunk(const ChunkHandler *ch)
1709 byte m = SlReadByte();
1710 size_t len;
1711 size_t endoffs;
1713 _sl.block_mode = m;
1714 _sl.obj_len = 0;
1716 switch (m) {
1717 case CH_ARRAY:
1718 _sl.array_index = 0;
1719 if (ch->load_check_proc) {
1720 ch->load_check_proc();
1721 } else {
1722 SlSkipArray();
1724 break;
1725 case CH_SPARSE_ARRAY:
1726 if (ch->load_check_proc) {
1727 ch->load_check_proc();
1728 } else {
1729 SlSkipArray();
1731 break;
1732 default:
1733 if ((m & 0xF) == CH_RIFF) {
1734 /* Read length */
1735 len = (SlReadByte() << 16) | ((m >> 4) << 24);
1736 len += SlReadUint16();
1737 _sl.obj_len = len;
1738 endoffs = _sl.reader->GetSize() + len;
1739 if (ch->load_check_proc) {
1740 ch->load_check_proc();
1741 } else {
1742 SlSkipBytes(len);
1744 if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1745 } else {
1746 SlErrorCorrupt("Invalid chunk type");
1748 break;
1753 * Stub Chunk handlers to only calculate length and do nothing else.
1754 * The intended chunk handler that should be called.
1756 static ChunkSaveLoadProc *_stub_save_proc;
1759 * Stub Chunk handlers to only calculate length and do nothing else.
1760 * Actually call the intended chunk handler.
1761 * @param arg ignored parameter.
1763 static inline void SlStubSaveProc2(void *arg)
1765 _stub_save_proc();
1769 * Stub Chunk handlers to only calculate length and do nothing else.
1770 * Call SlAutoLenth with our stub save proc that will eventually
1771 * call the intended chunk handler.
1773 static void SlStubSaveProc()
1775 SlAutolength(SlStubSaveProc2, NULL);
1779 * Save a chunk of data (eg. vehicles, stations, etc.). Each chunk is
1780 * prefixed by an ID identifying it, followed by data, and terminator where appropriate
1781 * @param ch The chunkhandler that will be used for the operation
1783 static void SlSaveChunk(const ChunkHandler *ch)
1785 ChunkSaveLoadProc *proc = ch->save_proc;
1787 /* Don't save any chunk information if there is no save handler. */
1788 if (proc == NULL) return;
1790 SlWriteUint32(ch->id);
1791 DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1793 if (ch->flags & CH_AUTO_LENGTH) {
1794 /* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */
1795 _stub_save_proc = proc;
1796 proc = SlStubSaveProc;
1799 _sl.block_mode = ch->flags & CH_TYPE_MASK;
1800 switch (ch->flags & CH_TYPE_MASK) {
1801 case CH_RIFF:
1802 _sl.need_length = NL_WANTLENGTH;
1803 proc();
1804 break;
1805 case CH_ARRAY:
1806 _sl.last_array_index = 0;
1807 SlWriteByte(CH_ARRAY);
1808 proc();
1809 SlWriteArrayLength(0); // Terminate arrays
1810 break;
1811 case CH_SPARSE_ARRAY:
1812 SlWriteByte(CH_SPARSE_ARRAY);
1813 proc();
1814 SlWriteArrayLength(0); // Terminate arrays
1815 break;
1816 default: NOT_REACHED();
1820 /** Save all chunks */
1821 static void SlSaveChunks()
1823 FOR_ALL_CHUNK_HANDLERS(ch) {
1824 SlSaveChunk(ch);
1827 /* Terminator */
1828 SlWriteUint32(0);
1832 * Find the ChunkHandler that will be used for processing the found
1833 * chunk in the savegame or in memory
1834 * @param id the chunk in question
1835 * @return returns the appropriate chunkhandler
1837 static const ChunkHandler *SlFindChunkHandler(uint32 id)
1839 FOR_ALL_CHUNK_HANDLERS(ch) if (ch->id == id) return ch;
1840 return NULL;
1843 /** Load all chunks */
1844 static void SlLoadChunks()
1846 uint32 id;
1847 const ChunkHandler *ch;
1849 for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1850 DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1852 ch = SlFindChunkHandler(id);
1853 if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
1854 SlLoadChunk(ch);
1858 /** Load all chunks for savegame checking */
1859 static void SlLoadCheckChunks()
1861 uint32 id;
1862 const ChunkHandler *ch;
1864 for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1865 DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1867 ch = SlFindChunkHandler(id);
1868 if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
1869 SlLoadCheckChunk(ch);
1873 /** Fix all pointers (convert index -> pointer) */
1874 static void SlFixPointers()
1876 _sl.action = SLA_PTRS;
1878 DEBUG(sl, 1, "Fixing pointers");
1880 FOR_ALL_CHUNK_HANDLERS(ch) {
1881 if (ch->ptrs_proc != NULL) {
1882 DEBUG(sl, 2, "Fixing pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1883 ch->ptrs_proc();
1887 DEBUG(sl, 1, "All pointers fixed");
1889 assert(_sl.action == SLA_PTRS);
1893 /** Yes, simply reading from a file. */
1894 struct FileReader : LoadFilter {
1895 FILE *file; ///< The file to read from.
1896 long begin; ///< The begin of the file.
1899 * Create the file reader, so it reads from a specific file.
1900 * @param file The file to read from.
1902 FileReader(FILE *file) : LoadFilter(NULL), file(file), begin(ftell(file))
1906 /** Make sure everything is cleaned up. */
1907 ~FileReader()
1909 if (this->file != NULL) fclose(this->file);
1910 this->file = NULL;
1912 /* Make sure we don't double free. */
1913 _sl.sf = NULL;
1916 /* virtual */ size_t Read(byte *buf, size_t size)
1918 /* We're in the process of shutting down, i.e. in "failure" mode. */
1919 if (this->file == NULL) return 0;
1921 return fread(buf, 1, size, this->file);
1924 /* virtual */ void Reset()
1926 clearerr(this->file);
1927 if (fseek(this->file, this->begin, SEEK_SET)) {
1928 DEBUG(sl, 1, "Could not reset the file reading");
1933 /** Yes, simply writing to a file. */
1934 struct FileWriter : SaveFilter {
1935 FILE *file; ///< The file to write to.
1938 * Create the file writer, so it writes to a specific file.
1939 * @param file The file to write to.
1941 FileWriter(FILE *file) : SaveFilter(NULL), file(file)
1945 /** Make sure everything is cleaned up. */
1946 ~FileWriter()
1948 this->Finish();
1950 /* Make sure we don't double free. */
1951 _sl.sf = NULL;
1954 /* virtual */ void Write(byte *buf, size_t size)
1956 /* We're in the process of shutting down, i.e. in "failure" mode. */
1957 if (this->file == NULL) return;
1959 if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
1962 /* virtual */ void Finish()
1964 if (this->file != NULL) fclose(this->file);
1965 this->file = NULL;
1969 /*******************************************
1970 ********** START OF LZO CODE **************
1971 *******************************************/
1973 #ifdef WITH_LZO
1974 #include <lzo/lzo1x.h>
1976 /** Buffer size for the LZO compressor */
1977 static const uint LZO_BUFFER_SIZE = 8192;
1979 /** Filter using LZO compression. */
1980 struct LZOLoadFilter : LoadFilter {
1982 * Initialise this filter.
1983 * @param chain The next filter in this chain.
1985 LZOLoadFilter(LoadFilter *chain) : LoadFilter(chain)
1987 if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
1990 /* virtual */ size_t Read(byte *buf, size_t ssize)
1992 assert(ssize >= LZO_BUFFER_SIZE);
1994 /* Buffer size is from the LZO docs plus the chunk header size. */
1995 byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1996 uint32 tmp[2];
1997 uint32 size;
1998 lzo_uint len = ssize;
2000 /* Read header*/
2001 if (this->chain->Read((byte*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
2003 /* Check if size is bad */
2004 ((uint32*)out)[0] = size = tmp[1];
2006 if (_sl_version != 0) {
2007 tmp[0] = TO_BE32(tmp[0]);
2008 size = TO_BE32(size);
2011 if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
2013 /* Read block */
2014 if (this->chain->Read(out + sizeof(uint32), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2016 /* Verify checksum */
2017 if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
2019 /* Decompress */
2020 int ret = lzo1x_decompress_safe(out + sizeof(uint32) * 1, size, buf, &len, NULL);
2021 if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2022 return len;
2026 /** Filter using LZO compression. */
2027 struct LZOSaveFilter : SaveFilter {
2029 * Initialise this filter.
2030 * @param chain The next filter in this chain.
2031 * @param compression_level The requested level of compression.
2033 LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2035 if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2038 /* virtual */ void Write(byte *buf, size_t size)
2040 const lzo_bytep in = buf;
2041 /* Buffer size is from the LZO docs plus the chunk header size. */
2042 byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
2043 byte wrkmem[LZO1X_1_MEM_COMPRESS];
2044 lzo_uint outlen;
2046 do {
2047 /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2048 lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
2049 lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
2050 ((uint32*)out)[1] = TO_BE32((uint32)outlen);
2051 ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
2052 this->chain->Write(out, outlen + sizeof(uint32) * 2);
2054 /* Move to next data chunk. */
2055 size -= len;
2056 in += len;
2057 } while (size > 0);
2061 #endif /* WITH_LZO */
2063 /*********************************************
2064 ******** START OF NOCOMP CODE (uncompressed)*
2065 *********************************************/
2067 /** Filter without any compression. */
2068 struct NoCompLoadFilter : LoadFilter {
2070 * Initialise this filter.
2071 * @param chain The next filter in this chain.
2073 NoCompLoadFilter(LoadFilter *chain) : LoadFilter(chain)
2077 /* virtual */ size_t Read(byte *buf, size_t size)
2079 return this->chain->Read(buf, size);
2083 /** Filter without any compression. */
2084 struct NoCompSaveFilter : SaveFilter {
2086 * Initialise this filter.
2087 * @param chain The next filter in this chain.
2088 * @param compression_level The requested level of compression.
2090 NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2094 /* virtual */ void Write(byte *buf, size_t size)
2096 this->chain->Write(buf, size);
2100 /********************************************
2101 ********** START OF ZLIB CODE **************
2102 ********************************************/
2104 #if defined(WITH_ZLIB)
2105 #include <zlib.h>
2107 /** Filter using Zlib compression. */
2108 struct ZlibLoadFilter : LoadFilter {
2109 z_stream z; ///< Stream state we are reading from.
2110 byte fread_buf[MEMORY_CHUNK_SIZE]; ///< Buffer for reading from the file.
2113 * Initialise this filter.
2114 * @param chain The next filter in this chain.
2116 ZlibLoadFilter(LoadFilter *chain) : LoadFilter(chain)
2118 memset(&this->z, 0, sizeof(this->z));
2119 if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2122 /** Clean everything up. */
2123 ~ZlibLoadFilter()
2125 inflateEnd(&this->z);
2128 /* virtual */ size_t Read(byte *buf, size_t size)
2130 this->z.next_out = buf;
2131 this->z.avail_out = (uint)size;
2133 do {
2134 /* read more bytes from the file? */
2135 if (this->z.avail_in == 0) {
2136 this->z.next_in = this->fread_buf;
2137 this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2140 /* inflate the data */
2141 int r = inflate(&this->z, 0);
2142 if (r == Z_STREAM_END) break;
2144 if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2145 } while (this->z.avail_out != 0);
2147 return size - this->z.avail_out;
2151 /** Filter using Zlib compression. */
2152 struct ZlibSaveFilter : SaveFilter {
2153 z_stream z; ///< Stream state we are writing to.
2156 * Initialise this filter.
2157 * @param chain The next filter in this chain.
2158 * @param compression_level The requested level of compression.
2160 ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2162 memset(&this->z, 0, sizeof(this->z));
2163 if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2166 /** Clean up what we allocated. */
2167 ~ZlibSaveFilter()
2169 deflateEnd(&this->z);
2173 * Helper loop for writing the data.
2174 * @param p The bytes to write.
2175 * @param len Amount of bytes to write.
2176 * @param mode Mode for deflate.
2178 void WriteLoop(byte *p, size_t len, int mode)
2180 byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2181 uint n;
2182 this->z.next_in = p;
2183 this->z.avail_in = (uInt)len;
2184 do {
2185 this->z.next_out = buf;
2186 this->z.avail_out = sizeof(buf);
2189 * For the poor next soul who sees many valgrind warnings of the
2190 * "Conditional jump or move depends on uninitialised value(s)" kind:
2191 * According to the author of zlib it is not a bug and it won't be fixed.
2192 * http://groups.google.com/group/comp.compression/browse_thread/thread/b154b8def8c2a3ef/cdf9b8729ce17ee2
2193 * [Mark Adler, Feb 24 2004, 'zlib-1.2.1 valgrind warnings' in the newsgroup comp.compression]
2195 int r = deflate(&this->z, mode);
2197 /* bytes were emitted? */
2198 if ((n = sizeof(buf) - this->z.avail_out) != 0) {
2199 this->chain->Write(buf, n);
2201 if (r == Z_STREAM_END) break;
2203 if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2204 } while (this->z.avail_in || !this->z.avail_out);
2207 /* virtual */ void Write(byte *buf, size_t size)
2209 this->WriteLoop(buf, size, 0);
2212 /* virtual */ void Finish()
2214 this->WriteLoop(NULL, 0, Z_FINISH);
2215 this->chain->Finish();
2219 #endif /* WITH_ZLIB */
2221 /********************************************
2222 ********** START OF LZMA CODE **************
2223 ********************************************/
2225 #if defined(WITH_LZMA)
2226 #include <lzma.h>
2229 * Have a copy of an initialised LZMA stream. We need this as it's
2230 * impossible to "re"-assign LZMA_STREAM_INIT to a variable in some
2231 * compilers, i.e. LZMA_STREAM_INIT can't be used to set something.
2232 * This var has to be used instead.
2234 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2236 /** Filter without any compression. */
2237 struct LZMALoadFilter : LoadFilter {
2238 lzma_stream lzma; ///< Stream state that we are reading from.
2239 byte fread_buf[MEMORY_CHUNK_SIZE]; ///< Buffer for reading from the file.
2242 * Initialise this filter.
2243 * @param chain The next filter in this chain.
2245 LZMALoadFilter(LoadFilter *chain) : LoadFilter(chain), lzma(_lzma_init)
2247 /* Allow saves up to 256 MB uncompressed */
2248 if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2251 /** Clean everything up. */
2252 ~LZMALoadFilter()
2254 lzma_end(&this->lzma);
2257 /* virtual */ size_t Read(byte *buf, size_t size)
2259 this->lzma.next_out = buf;
2260 this->lzma.avail_out = size;
2262 do {
2263 /* read more bytes from the file? */
2264 if (this->lzma.avail_in == 0) {
2265 this->lzma.next_in = this->fread_buf;
2266 this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2269 /* inflate the data */
2270 lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2271 if (r == LZMA_STREAM_END) break;
2272 if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2273 } while (this->lzma.avail_out != 0);
2275 return size - this->lzma.avail_out;
2279 /** Filter using LZMA compression. */
2280 struct LZMASaveFilter : SaveFilter {
2281 lzma_stream lzma; ///< Stream state that we are writing to.
2284 * Initialise this filter.
2285 * @param chain The next filter in this chain.
2286 * @param compression_level The requested level of compression.
2288 LZMASaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain), lzma(_lzma_init)
2290 if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2293 /** Clean up what we allocated. */
2294 ~LZMASaveFilter()
2296 lzma_end(&this->lzma);
2300 * Helper loop for writing the data.
2301 * @param p The bytes to write.
2302 * @param len Amount of bytes to write.
2303 * @param action Action for lzma_code.
2305 void WriteLoop(byte *p, size_t len, lzma_action action)
2307 byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2308 size_t n;
2309 this->lzma.next_in = p;
2310 this->lzma.avail_in = len;
2311 do {
2312 this->lzma.next_out = buf;
2313 this->lzma.avail_out = sizeof(buf);
2315 lzma_ret r = lzma_code(&this->lzma, action);
2317 /* bytes were emitted? */
2318 if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
2319 this->chain->Write(buf, n);
2321 if (r == LZMA_STREAM_END) break;
2322 if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2323 } while (this->lzma.avail_in || !this->lzma.avail_out);
2326 /* virtual */ void Write(byte *buf, size_t size)
2328 this->WriteLoop(buf, size, LZMA_RUN);
2331 /* virtual */ void Finish()
2333 this->WriteLoop(NULL, 0, LZMA_FINISH);
2334 this->chain->Finish();
2338 #endif /* WITH_LZMA */
2340 /*******************************************
2341 ************* END OF CODE *****************
2342 *******************************************/
2344 /** The format for a reader/writer type of a savegame */
2345 struct SaveLoadFormat {
2346 const char *name; ///< name of the compressor/decompressor (debug-only)
2347 uint32 tag; ///< the 4-letter tag by which it is identified in the savegame
2349 LoadFilter *(*init_load)(LoadFilter *chain); ///< Constructor for the load filter.
2350 SaveFilter *(*init_write)(SaveFilter *chain, byte compression); ///< Constructor for the save filter.
2352 byte min_compression; ///< the minimum compression level of this format
2353 byte default_compression; ///< the default compression level of this format
2354 byte max_compression; ///< the maximum compression level of this format
2357 /** The different saveload formats known/understood by OpenTTD. */
2358 static const SaveLoadFormat _saveload_formats[] = {
2359 #if defined(WITH_LZO)
2360 /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2361 {"lzo", TO_BE32X('OTTD'), CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2362 #else
2363 {"lzo", TO_BE32X('OTTD'), NULL, NULL, 0, 0, 0},
2364 #endif
2365 /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2366 {"none", TO_BE32X('OTTN'), CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2367 #if defined(WITH_ZLIB)
2368 /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2369 * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2370 * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2371 {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2372 #else
2373 {"zlib", TO_BE32X('OTTZ'), NULL, NULL, 0, 0, 0},
2374 #endif
2375 #if defined(WITH_LZMA)
2376 /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2377 * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2378 * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2379 * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2380 * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2381 {"lzma", TO_BE32X('OTTX'), CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2382 #else
2383 {"lzma", TO_BE32X('OTTX'), NULL, NULL, 0, 0, 0},
2384 #endif
2388 * Return the savegameformat of the game. Whether it was created with ZLIB compression
2389 * uncompressed, or another type
2390 * @param s Name of the savegame format. If NULL it picks the first available one
2391 * @param compression_level Output for telling what compression level we want.
2392 * @return Pointer to SaveLoadFormat struct giving all characteristics of this type of savegame
2394 static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
2396 const SaveLoadFormat *def = lastof(_saveload_formats);
2398 /* find default savegame format, the highest one with which files can be written */
2399 while (!def->init_write) def--;
2401 if (!StrEmpty(s)) {
2402 /* Get the ":..." of the compression level out of the way */
2403 char *complevel = strrchr(s, ':');
2404 if (complevel != NULL) *complevel = '\0';
2406 for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
2407 if (slf->init_write != NULL && strcmp(s, slf->name) == 0) {
2408 *compression_level = slf->default_compression;
2409 if (complevel != NULL) {
2410 /* There is a compression level in the string.
2411 * First restore the : we removed to do proper name matching,
2412 * then move the the begin of the actual version. */
2413 *complevel = ':';
2414 complevel++;
2416 /* Get the version and determine whether all went fine. */
2417 char *end;
2418 long level = strtol(complevel, &end, 10);
2419 if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) {
2420 SetDParamStr(0, complevel);
2421 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2422 } else {
2423 *compression_level = level;
2426 return slf;
2430 SetDParamStr(0, s);
2431 SetDParamStr(1, def->name);
2432 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2434 /* Restore the string by adding the : back */
2435 if (complevel != NULL) *complevel = ':';
2437 *compression_level = def->default_compression;
2438 return def;
2441 /* actual loader/saver function */
2442 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2443 extern bool AfterLoadGame();
2444 extern bool LoadOldSaveGame(const char *file);
2447 * Clear/free saveload state.
2449 static inline void ClearSaveLoadState()
2451 delete _sl.dumper;
2452 _sl.dumper = NULL;
2454 delete _sl.sf;
2455 _sl.sf = NULL;
2457 delete _sl.reader;
2458 _sl.reader = NULL;
2460 delete _sl.lf;
2461 _sl.lf = NULL;
2465 * Update the gui accordingly when starting saving
2466 * and set locks on saveload. Also turn off fast-forward cause with that
2467 * saving takes Aaaaages
2469 static void SaveFileStart()
2471 _sl.ff_state = _fast_forward;
2472 _fast_forward = 0;
2473 SetMouseCursorBusy(true);
2475 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_START);
2476 _sl.saveinprogress = true;
2479 /** Update the gui accordingly when saving is done and release locks on saveload. */
2480 static void SaveFileDone()
2482 if (_game_mode != GM_MENU) _fast_forward = _sl.ff_state;
2483 SetMouseCursorBusy(false);
2485 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_FINISH);
2486 _sl.saveinprogress = false;
2489 /** Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friends) */
2490 void SetSaveLoadError(StringID str)
2492 _sl.error_str = str;
2495 /** Get the string representation of the error message */
2496 const char *GetSaveLoadErrorString()
2498 SetDParam(0, _sl.error_str);
2499 SetDParamStr(1, _sl.extra_msg);
2501 static char err_str[512];
2502 GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
2503 return err_str;
2506 /** Show a gui message when saving has failed */
2507 static void SaveFileError()
2509 SetDParamStr(0, GetSaveLoadErrorString());
2510 ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
2511 SaveFileDone();
2515 * We have written the whole game into memory, _memory_savegame, now find
2516 * and appropriate compressor and start writing to file.
2518 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2520 try {
2521 byte compression;
2522 const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
2524 /* We have written our stuff to memory, now write it to file! */
2525 uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
2526 _sl.sf->Write((byte*)hdr, sizeof(hdr));
2528 _sl.sf = fmt->init_write(_sl.sf, compression);
2529 _sl.dumper->Flush(_sl.sf);
2531 ClearSaveLoadState();
2533 if (threaded) SetAsyncSaveFinish(SaveFileDone);
2535 return SL_OK;
2536 } catch (...) {
2537 ClearSaveLoadState();
2539 AsyncSaveFinishProc asfp = SaveFileDone;
2541 /* We don't want to shout when saving is just
2542 * cancelled due to a client disconnecting. */
2543 if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2544 /* Skip the "colour" character */
2545 DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2546 asfp = SaveFileError;
2549 if (threaded) {
2550 SetAsyncSaveFinish(asfp);
2551 } else {
2552 asfp();
2554 return SL_ERROR;
2558 /** Thread run function for saving the file to disk. */
2559 static void SaveFileToDiskThread(void *arg)
2561 SaveFileToDisk(true);
2564 void WaitTillSaved()
2566 if (_save_thread == NULL) return;
2568 _save_thread->Join();
2569 delete _save_thread;
2570 _save_thread = NULL;
2572 /* Make sure every other state is handled properly as well. */
2573 ProcessAsyncSaveFinish();
2577 * Actually perform the saving of the savegame.
2578 * General tactics is to first save the game to memory, then write it to file
2579 * using the writer, either in threaded mode if possible, or single-threaded.
2580 * @param writer The filter to write the savegame to.
2581 * @param threaded Whether to try to perform the saving asynchronously.
2582 * @return Return the result of the action. #SL_OK or #SL_ERROR
2584 static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
2586 assert(!_sl.saveinprogress);
2588 _sl.dumper = new MemoryDumper();
2589 _sl.sf = writer;
2591 _sl_version = SAVEGAME_VERSION;
2593 SaveViewportBeforeSaveGame();
2594 SlSaveChunks();
2596 SaveFileStart();
2597 if (!threaded || !ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread, "ottd:savegame")) {
2598 if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2600 SaveOrLoadResult result = SaveFileToDisk(false);
2601 SaveFileDone();
2603 return result;
2606 return SL_OK;
2610 * Save the game using a (writer) filter.
2611 * @param writer The filter to write the savegame to.
2612 * @param threaded Whether to try to perform the saving asynchronously.
2613 * @return Return the result of the action. #SL_OK or #SL_ERROR
2615 SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
2617 try {
2618 _sl.action = SLA_SAVE;
2619 return DoSave(writer, threaded);
2620 } catch (...) {
2621 ClearSaveLoadState();
2622 return SL_ERROR;
2627 * Actually perform the loading of a "non-old" savegame.
2628 * @param reader The filter to read the savegame from.
2629 * @param load_check Whether to perform the checking ("preview") or actually load the game.
2630 * @return Return the result of the action. #SL_OK or #SL_REINIT ("unload" the game)
2632 static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
2634 _sl.lf = reader;
2636 if (load_check) {
2637 /* Clear previous check data */
2638 _load_check_data.Clear();
2639 /* Mark SL_LOAD_CHECK as supported for this savegame. */
2640 _load_check_data.checkable = true;
2643 uint32 hdr[2];
2644 if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2646 /* see if we have any loader for this type. */
2647 const SaveLoadFormat *fmt = _saveload_formats;
2648 for (;;) {
2649 /* No loader found, treat as version 0 and use LZO format */
2650 if (fmt == endof(_saveload_formats)) {
2651 DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2652 _sl.lf->Reset();
2653 _sl_version = 0;
2654 _sl_minor_version = 0;
2656 /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2657 fmt = _saveload_formats;
2658 for (;;) {
2659 if (fmt == endof(_saveload_formats)) {
2660 /* Who removed LZO support? Bad bad boy! */
2661 NOT_REACHED();
2663 if (fmt->tag == TO_BE32X('OTTD')) break;
2664 fmt++;
2666 break;
2669 if (fmt->tag == hdr[0]) {
2670 /* check version number */
2671 _sl_version = TO_BE32(hdr[1]) >> 16;
2672 /* Minor is not used anymore from version 18.0, but it is still needed
2673 * in versions before that (4 cases) which can't be removed easy.
2674 * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2675 _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
2677 DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
2679 /* Is the version higher than the current? */
2680 if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2681 break;
2684 fmt++;
2687 /* loader for this savegame type is not implemented? */
2688 if (fmt->init_load == NULL) {
2689 char err_str[64];
2690 seprintf(err_str, lastof(err_str), "Loader for '%s' is not available.", fmt->name);
2691 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
2694 _sl.lf = fmt->init_load(_sl.lf);
2695 _sl.reader = new ReadBuffer(_sl.lf);
2696 _next_offs = 0;
2698 if (!load_check) {
2699 /* Old maps were hardcoded to 256x256 and thus did not contain
2700 * any mapsize information. Pre-initialize to 256x256 to not to
2701 * confuse old games */
2702 InitializeGame(256, 256, true, true);
2704 GamelogReset();
2706 if (IsSavegameVersionBefore(4)) {
2708 * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
2709 * shared savegame version 4. Anything before that 'obviously'
2710 * does not have any NewGRFs. Between the introduction and
2711 * savegame version 41 (just before 0.5) the NewGRF settings
2712 * were not stored in the savegame and they were loaded by
2713 * using the settings from the main menu.
2714 * So, to recap:
2715 * - savegame version < 4: do not load any NewGRFs.
2716 * - savegame version >= 41: load NewGRFs from savegame, which is
2717 * already done at this stage by
2718 * overwriting the main menu settings.
2719 * - other savegame versions: use main menu settings.
2721 * This means that users *can* crash savegame version 4..40
2722 * savegames if they set incompatible NewGRFs in the main menu,
2723 * but can't crash anymore for savegame version < 4 savegames.
2725 * Note: this is done here because AfterLoadGame is also called
2726 * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
2728 ClearGRFConfigList(&_grfconfig);
2732 if (load_check) {
2733 /* Load chunks into _load_check_data.
2734 * No pools are loaded. References are not possible, and thus do not need resolving. */
2735 SlLoadCheckChunks();
2736 } else {
2737 /* Load chunks and resolve references */
2738 SlLoadChunks();
2739 SlFixPointers();
2742 ClearSaveLoadState();
2744 _savegame_type = SGT_OTTD;
2746 if (load_check) {
2747 /* The only part from AfterLoadGame() we need */
2748 _load_check_data.grf_compatibility = IsGoodGRFConfigList(_load_check_data.grfconfig);
2749 } else {
2750 GamelogStartAction(GLAT_LOAD);
2752 /* After loading fix up savegame for any internal changes that
2753 * might have occurred since then. If it fails, load back the old game. */
2754 if (!AfterLoadGame()) {
2755 GamelogStopAction();
2756 return SL_REINIT;
2759 GamelogStopAction();
2762 return SL_OK;
2766 * Load the game using a (reader) filter.
2767 * @param reader The filter to read the savegame from.
2768 * @return Return the result of the action. #SL_OK or #SL_REINIT ("unload" the game)
2770 SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
2772 try {
2773 _sl.action = SLA_LOAD;
2774 return DoLoad(reader, false);
2775 } catch (...) {
2776 ClearSaveLoadState();
2777 return SL_REINIT;
2782 * Main Save or Load function where the high-level saveload functions are
2783 * handled. It opens the savegame, selects format and checks versions
2784 * @param filename The name of the savegame being created/loaded
2785 * @param mode Save or load mode. Load can also be a TTD(Patch) game. Use #SL_LOAD, #SL_OLD_LOAD, #SL_LOAD_CHECK, or #SL_SAVE.
2786 * @param sb The sub directory to save the savegame in
2787 * @param threaded True when threaded saving is allowed
2788 * @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
2790 SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
2792 /* An instance of saving is already active, so don't go saving again */
2793 if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
2794 /* if not an autosave, but a user action, show error message */
2795 if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
2796 return SL_OK;
2798 WaitTillSaved();
2800 try {
2801 /* Load a TTDLX or TTDPatch game */
2802 if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
2803 InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
2805 /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
2806 * and if so a new NewGRF list will be made in LoadOldSaveGame.
2807 * Note: this is done here because AfterLoadGame is also called
2808 * for OTTD savegames which have their own NewGRF logic. */
2809 ClearGRFConfigList(&_grfconfig);
2810 GamelogReset();
2811 if (!LoadOldSaveGame(filename)) return SL_REINIT;
2812 _sl_version = 0;
2813 _sl_minor_version = 0;
2814 GamelogStartAction(GLAT_LOAD);
2815 if (!AfterLoadGame()) {
2816 GamelogStopAction();
2817 return SL_REINIT;
2819 GamelogStopAction();
2820 return SL_OK;
2823 assert(dft == DFT_GAME_FILE);
2824 switch (fop) {
2825 case SLO_CHECK:
2826 _sl.action = SLA_LOAD_CHECK;
2827 break;
2829 case SLO_LOAD:
2830 _sl.action = SLA_LOAD;
2831 break;
2833 case SLO_SAVE:
2834 _sl.action = SLA_SAVE;
2835 break;
2837 default: NOT_REACHED();
2840 FILE *fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
2842 /* Make it a little easier to load savegames from the console */
2843 if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
2844 if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
2845 if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
2847 if (fh == NULL) {
2848 SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2851 if (fop == SLO_SAVE) { // SAVE game
2852 DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
2853 if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
2855 return DoSave(new FileWriter(fh), threaded);
2858 /* LOAD game */
2859 assert(fop == SLO_LOAD || fop == SLO_CHECK);
2860 DEBUG(desync, 1, "load: %s", filename);
2861 return DoLoad(new FileReader(fh), fop == SLO_CHECK);
2862 } catch (...) {
2863 /* This code may be executed both for old and new save games. */
2864 ClearSaveLoadState();
2866 /* Skip the "colour" character */
2867 if (fop != SLO_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2869 /* A saver/loader exception!! reinitialize all variables to prevent crash! */
2870 return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
2874 /** Do a save when exiting the game (_settings_client.gui.autosave_on_exit) */
2875 void DoExitSave()
2877 SaveOrLoad("exit.sav", SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR);
2881 * Fill the buffer with the default name for a savegame *or* screenshot.
2882 * @param buf the buffer to write to.
2883 * @param last the last element in the buffer.
2885 void GenerateDefaultSaveName(char *buf, const char *last)
2887 /* Check if we have a name for this map, which is the name of the first
2888 * available company. When there's no company available we'll use
2889 * 'Spectator' as "company" name. */
2890 CompanyID cid = _local_company;
2891 if (!Company::IsValidID(cid)) {
2892 const Company *c;
2893 FOR_ALL_COMPANIES(c) {
2894 cid = c->index;
2895 break;
2899 SetDParam(0, cid);
2901 /* Insert current date */
2902 switch (_settings_client.gui.date_format_in_default_names) {
2903 case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
2904 case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
2905 case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
2906 default: NOT_REACHED();
2908 SetDParam(2, _date);
2910 /* Get the correct string (special string for when there's not company) */
2911 GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
2912 SanitizeFilename(buf);
2916 * Set the mode and file type of the file to save or load based on the type of file entry at the file system.
2917 * @param ft Type of file entry of the file system.
2919 void FileToSaveLoad::SetMode(FiosType ft)
2921 this->SetMode(SLO_LOAD, GetAbstractFileType(ft), GetDetailedFileType(ft));
2925 * Set the mode and file type of the file to save or load.
2926 * @param fop File operation being performed.
2927 * @param aft Abstract file type.
2928 * @param dft Detailed file type.
2930 void FileToSaveLoad::SetMode(SaveLoadOperation fop, AbstractFileType aft, DetailedFileType dft)
2932 if (aft == FT_INVALID || aft == FT_NONE) {
2933 this->file_op = SLO_INVALID;
2934 this->detail_ftype = DFT_INVALID;
2935 this->abstract_ftype = FT_INVALID;
2936 return;
2939 this->file_op = fop;
2940 this->detail_ftype = dft;
2941 this->abstract_ftype = aft;
2945 * Set the name of the file.
2946 * @param name Name of the file.
2948 void FileToSaveLoad::SetName(const char *name)
2950 strecpy(this->name, name, lastof(this->name));
2954 * Set the title of the file.
2955 * @param title Title of the file.
2957 void FileToSaveLoad::SetTitle(const char *title)
2959 strecpy(this->title, title, lastof(this->title));
2962 #if 0
2964 * Function to get the type of the savegame by looking at the file header.
2965 * NOTICE: Not used right now, but could be used if extensions of savegames are garbled
2966 * @param file Savegame to be checked
2967 * @return SL_OLD_LOAD or SL_LOAD of the file
2969 int GetSavegameType(char *file)
2971 const SaveLoadFormat *fmt;
2972 uint32 hdr;
2973 FILE *f;
2974 int mode = SL_OLD_LOAD;
2976 f = fopen(file, "rb");
2977 if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
2978 DEBUG(sl, 0, "Savegame is obsolete or invalid format");
2979 mode = SL_LOAD; // don't try to get filename, just show name as it is written
2980 } else {
2981 /* see if we have any loader for this type. */
2982 for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
2983 if (fmt->tag == hdr) {
2984 mode = SL_LOAD; // new type of savegame
2985 break;
2990 fclose(f);
2991 return mode;
2993 #endif