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/>.
10 * All actions handling saving and loading goes on in this file. The general actions
11 * are as follows for saving a game (loading is analogous):
13 * <li>initialize the writer by creating a temporary memory-buffer for it
14 * <li>go through all to-be saved elements, each 'chunk' (#ChunkHandler) prefixed by a label
15 * <li>use their description array (#SaveLoad) to know what elements to save and in what version
16 * of the game it was active (used when loading)
17 * <li>write all data byte-by-byte to the temporary buffer so it is endian-safe
18 * <li>when the buffer is full; flush it to the output (eg save to file) (_sl.buf, _sl.bufp, _sl.bufe)
19 * <li>repeat this until everything is done, and flush any remaining output to file
24 #include "../stdafx.h"
26 #include "../station_base.h"
27 #include "../thread.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"
48 #include "table/strings.h"
50 #include "saveload_internal.h"
51 #include "saveload_filter.h"
53 #include "../safeguards.h"
55 extern const SaveLoadVersion SAVEGAME_VERSION
= (SaveLoadVersion
)(SL_MAX_VERSION
- 1); ///< Current savegame version of OpenTTD.
57 SavegameType _savegame_type
; ///< type of savegame we are loading
58 FileToSaveLoad _file_to_saveload
; ///< File to save or load in the openttd loop.
60 uint32 _ttdp_version
; ///< version of TTDP savegame (if applicable)
61 SaveLoadVersion _sl_version
; ///< the major savegame version identifier
62 byte _sl_minor_version
; ///< the minor savegame version, DO NOT USE!
63 char _savegame_format
[8]; ///< how to compress savegames
64 bool _do_autosave
; ///< are we doing an autosave at the moment?
66 /** What are we currently doing? */
68 SLA_LOAD
, ///< loading
70 SLA_PTRS
, ///< fixing pointers
71 SLA_NULL
, ///< null all pointers (on loading error)
72 SLA_LOAD_CHECK
, ///< partial loading into #_load_check_data
76 NL_NONE
= 0, ///< not working in NeedLength mode
77 NL_WANTLENGTH
= 1, ///< writing length and data
78 NL_CALCLENGTH
= 2, ///< need to calculate the length
81 /** Save in chunks of 128 KiB. */
82 static const size_t MEMORY_CHUNK_SIZE
= 128 * 1024;
84 /** A buffer for reading (and buffering) savegame data. */
86 byte buf
[MEMORY_CHUNK_SIZE
]; ///< Buffer we're going to read from.
87 byte
*bufp
; ///< Location we're at reading the buffer.
88 byte
*bufe
; ///< End of the buffer we can read from.
89 LoadFilter
*reader
; ///< The filter used to actually read.
90 size_t read
; ///< The amount of read bytes so far from the filter.
93 * Initialise our variables.
94 * @param reader The filter to actually read data.
96 ReadBuffer(LoadFilter
*reader
) : bufp(nullptr), bufe(nullptr), reader(reader
), read(0)
100 inline byte
ReadByte()
102 if (this->bufp
== this->bufe
) {
103 size_t len
= this->reader
->Read(this->buf
, lengthof(this->buf
));
104 if (len
== 0) SlErrorCorrupt("Unexpected end of chunk");
107 this->bufp
= this->buf
;
108 this->bufe
= this->buf
+ len
;
111 return *this->bufp
++;
115 * Get the size of the memory dump made so far.
118 size_t GetSize() const
120 return this->read
- (this->bufe
- this->bufp
);
125 /** Container for dumping the savegame (quickly) to memory. */
126 struct MemoryDumper
{
127 std::vector
<byte
*> blocks
; ///< Buffer with blocks of allocated memory.
128 byte
*buf
; ///< Buffer we're going to write to.
129 byte
*bufe
; ///< End of the buffer we write to.
131 /** Initialise our variables. */
132 MemoryDumper() : buf(nullptr), bufe(nullptr)
138 for (auto p
: this->blocks
) {
144 * Write a single byte into the dumper.
145 * @param b The byte to write.
147 inline void WriteByte(byte b
)
149 /* Are we at the end of this chunk? */
150 if (this->buf
== this->bufe
) {
151 this->buf
= CallocT
<byte
>(MEMORY_CHUNK_SIZE
);
152 this->blocks
.push_back(this->buf
);
153 this->bufe
= this->buf
+ MEMORY_CHUNK_SIZE
;
160 * Flush this dumper into a writer.
161 * @param writer The filter we want to use.
163 void Flush(SaveFilter
*writer
)
166 size_t t
= this->GetSize();
169 size_t to_write
= min(MEMORY_CHUNK_SIZE
, t
);
171 writer
->Write(this->blocks
[i
++], to_write
);
179 * Get the size of the memory dump made so far.
182 size_t GetSize() const
184 return this->blocks
.size() * MEMORY_CHUNK_SIZE
- (this->bufe
- this->buf
);
188 /** The saveload struct, containing reader-writer functions, buffer, version, etc. */
189 struct SaveLoadParams
{
190 SaveLoadAction action
; ///< are we doing a save or a load atm.
191 NeedLength need_length
; ///< working in NeedLength (Autolength) mode?
192 byte block_mode
; ///< ???
193 bool error
; ///< did an error occur or not
195 size_t obj_len
; ///< the length of the current object we are busy with
196 int array_index
, last_array_index
; ///< in the case of an array, the current and last positions
198 MemoryDumper
*dumper
; ///< Memory dumper to write the savegame to.
199 SaveFilter
*sf
; ///< Filter to write the savegame to.
201 ReadBuffer
*reader
; ///< Savegame reading buffer.
202 LoadFilter
*lf
; ///< Filter to read the savegame from.
204 StringID error_str
; ///< the translatable error message to show
205 char *extra_msg
; ///< the error message
207 byte ff_state
; ///< The state of fast-forward when saving started.
208 bool saveinprogress
; ///< Whether there is currently a save in progress.
211 static SaveLoadParams _sl
; ///< Parameters used for/at saveload.
213 /* these define the chunks */
214 extern const ChunkHandler _gamelog_chunk_handlers
[];
215 extern const ChunkHandler _map_chunk_handlers
[];
216 extern const ChunkHandler _misc_chunk_handlers
[];
217 extern const ChunkHandler _name_chunk_handlers
[];
218 extern const ChunkHandler _cheat_chunk_handlers
[] ;
219 extern const ChunkHandler _setting_chunk_handlers
[];
220 extern const ChunkHandler _company_chunk_handlers
[];
221 extern const ChunkHandler _engine_chunk_handlers
[];
222 extern const ChunkHandler _veh_chunk_handlers
[];
223 extern const ChunkHandler _waypoint_chunk_handlers
[];
224 extern const ChunkHandler _depot_chunk_handlers
[];
225 extern const ChunkHandler _order_chunk_handlers
[];
226 extern const ChunkHandler _town_chunk_handlers
[];
227 extern const ChunkHandler _sign_chunk_handlers
[];
228 extern const ChunkHandler _station_chunk_handlers
[];
229 extern const ChunkHandler _industry_chunk_handlers
[];
230 extern const ChunkHandler _economy_chunk_handlers
[];
231 extern const ChunkHandler _subsidy_chunk_handlers
[];
232 extern const ChunkHandler _cargomonitor_chunk_handlers
[];
233 extern const ChunkHandler _goal_chunk_handlers
[];
234 extern const ChunkHandler _story_page_chunk_handlers
[];
235 extern const ChunkHandler _ai_chunk_handlers
[];
236 extern const ChunkHandler _game_chunk_handlers
[];
237 extern const ChunkHandler _animated_tile_chunk_handlers
[];
238 extern const ChunkHandler _newgrf_chunk_handlers
[];
239 extern const ChunkHandler _group_chunk_handlers
[];
240 extern const ChunkHandler _cargopacket_chunk_handlers
[];
241 extern const ChunkHandler _autoreplace_chunk_handlers
[];
242 extern const ChunkHandler _labelmaps_chunk_handlers
[];
243 extern const ChunkHandler _linkgraph_chunk_handlers
[];
244 extern const ChunkHandler _airport_chunk_handlers
[];
245 extern const ChunkHandler _object_chunk_handlers
[];
246 extern const ChunkHandler _persistent_storage_chunk_handlers
[];
248 /** Array of all chunks in a savegame, \c nullptr terminated. */
249 static const ChunkHandler
* const _chunk_handlers
[] = {
250 _gamelog_chunk_handlers
,
252 _misc_chunk_handlers
,
253 _name_chunk_handlers
,
254 _cheat_chunk_handlers
,
255 _setting_chunk_handlers
,
257 _waypoint_chunk_handlers
,
258 _depot_chunk_handlers
,
259 _order_chunk_handlers
,
260 _industry_chunk_handlers
,
261 _economy_chunk_handlers
,
262 _subsidy_chunk_handlers
,
263 _cargomonitor_chunk_handlers
,
264 _goal_chunk_handlers
,
265 _story_page_chunk_handlers
,
266 _engine_chunk_handlers
,
267 _town_chunk_handlers
,
268 _sign_chunk_handlers
,
269 _station_chunk_handlers
,
270 _company_chunk_handlers
,
272 _game_chunk_handlers
,
273 _animated_tile_chunk_handlers
,
274 _newgrf_chunk_handlers
,
275 _group_chunk_handlers
,
276 _cargopacket_chunk_handlers
,
277 _autoreplace_chunk_handlers
,
278 _labelmaps_chunk_handlers
,
279 _linkgraph_chunk_handlers
,
280 _airport_chunk_handlers
,
281 _object_chunk_handlers
,
282 _persistent_storage_chunk_handlers
,
287 * Iterate over all chunk handlers.
288 * @param ch the chunk handler iterator
290 #define FOR_ALL_CHUNK_HANDLERS(ch) \
291 for (const ChunkHandler * const *chsc = _chunk_handlers; *chsc != nullptr; chsc++) \
292 for (const ChunkHandler *ch = *chsc; ch != nullptr; ch = (ch->flags & CH_LAST) ? nullptr : ch + 1)
294 /** Null all pointers (convert index -> nullptr) */
295 static void SlNullPointers()
297 _sl
.action
= SLA_NULL
;
299 /* We don't want any savegame conversion code to run
300 * during NULLing; especially those that try to get
301 * pointers from other pools. */
302 _sl_version
= SAVEGAME_VERSION
;
304 DEBUG(sl
, 1, "Nulling pointers");
306 FOR_ALL_CHUNK_HANDLERS(ch
) {
307 if (ch
->ptrs_proc
!= nullptr) {
308 DEBUG(sl
, 2, "Nulling pointers for %c%c%c%c", ch
->id
>> 24, ch
->id
>> 16, ch
->id
>> 8, ch
->id
);
313 DEBUG(sl
, 1, "All pointers nulled");
315 assert(_sl
.action
== SLA_NULL
);
319 * Error handler. Sets everything up to show an error message and to clean
320 * up the mess of a partial savegame load.
321 * @param string The translatable error message to show.
322 * @param extra_msg An extra error message coming from one of the APIs.
323 * @note This function does never return as it throws an exception to
324 * break out of all the saveload code.
326 void NORETURN
SlError(StringID string
, const char *extra_msg
)
328 /* Distinguish between loading into _load_check_data vs. normal save/load. */
329 if (_sl
.action
== SLA_LOAD_CHECK
) {
330 _load_check_data
.error
= string
;
331 free(_load_check_data
.error_data
);
332 _load_check_data
.error_data
= (extra_msg
== nullptr) ? nullptr : stredup(extra_msg
);
334 _sl
.error_str
= string
;
336 _sl
.extra_msg
= (extra_msg
== nullptr) ? nullptr : stredup(extra_msg
);
339 /* We have to nullptr all pointers here; we might be in a state where
340 * the pointers are actually filled with indices, which means that
341 * when we access them during cleaning the pool dereferences of
342 * those indices will be made with segmentation faults as result. */
343 if (_sl
.action
== SLA_LOAD
|| _sl
.action
== SLA_PTRS
) SlNullPointers();
344 throw std::exception();
348 * Error handler for corrupt savegames. Sets everything up to show the
349 * error message and to clean up the mess of a partial savegame load.
350 * @param msg Location the corruption has been spotted.
351 * @note This function does never return as it throws an exception to
352 * break out of all the saveload code.
354 void NORETURN
SlErrorCorrupt(const char *msg
)
356 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME
, msg
);
360 * Issue an SlErrorCorrupt with a format string.
361 * @param format format string
362 * @param ... arguments to format string
363 * @note This function does never return as it throws an exception to
364 * break out of all the saveload code.
366 void NORETURN
SlErrorCorruptFmt(const char *format
, ...)
371 va_start(ap
, format
);
372 vseprintf(msg
, lastof(msg
), format
, ap
);
379 typedef void (*AsyncSaveFinishProc
)(); ///< Callback for when the savegame loading is finished.
380 static std::atomic
<AsyncSaveFinishProc
> _async_save_finish
; ///< Callback to call when the savegame loading is finished.
381 static std::thread _save_thread
; ///< The thread we're using to compress and write a savegame
384 * Called by save thread to tell we finished saving.
385 * @param proc The callback to call when saving is done.
387 static void SetAsyncSaveFinish(AsyncSaveFinishProc proc
)
389 if (_exit_game
) return;
390 while (_async_save_finish
.load(std::memory_order_acquire
) != nullptr) CSleep(10);
392 _async_save_finish
.store(proc
, std::memory_order_release
);
396 * Handle async save finishes.
398 void ProcessAsyncSaveFinish()
400 AsyncSaveFinishProc proc
= _async_save_finish
.exchange(nullptr, std::memory_order_acq_rel
);
401 if (proc
== nullptr) return;
405 if (_save_thread
.joinable()) {
411 * Wrapper for reading a byte from the buffer.
412 * @return The read byte.
416 return _sl
.reader
->ReadByte();
420 * Wrapper for writing a byte to the dumper.
421 * @param b The byte to write.
423 void SlWriteByte(byte b
)
425 _sl
.dumper
->WriteByte(b
);
428 static inline int SlReadUint16()
430 int x
= SlReadByte() << 8;
431 return x
| SlReadByte();
434 static inline uint32
SlReadUint32()
436 uint32 x
= SlReadUint16() << 16;
437 return x
| SlReadUint16();
440 static inline uint64
SlReadUint64()
442 uint32 x
= SlReadUint32();
443 uint32 y
= SlReadUint32();
444 return (uint64
)x
<< 32 | y
;
447 static inline void SlWriteUint16(uint16 v
)
449 SlWriteByte(GB(v
, 8, 8));
450 SlWriteByte(GB(v
, 0, 8));
453 static inline void SlWriteUint32(uint32 v
)
455 SlWriteUint16(GB(v
, 16, 16));
456 SlWriteUint16(GB(v
, 0, 16));
459 static inline void SlWriteUint64(uint64 x
)
461 SlWriteUint32((uint32
)(x
>> 32));
462 SlWriteUint32((uint32
)x
);
466 * Read in bytes from the file/data structure but don't do
467 * anything with them, discarding them in effect
468 * @param length The amount of bytes that is being treated this way
470 static inline void SlSkipBytes(size_t length
)
472 for (; length
!= 0; length
--) SlReadByte();
476 * Read in the header descriptor of an object or an array.
477 * If the highest bit is set (7), then the index is bigger than 127
478 * elements, so use the next byte to read in the real value.
479 * The actual value is then both bytes added with the first shifted
480 * 8 bits to the left, and dropping the highest bit (which only indicated a big index).
481 * x = ((x & 0x7F) << 8) + SlReadByte();
482 * @return Return the value of the index
484 static uint
SlReadSimpleGamma()
486 uint i
= SlReadByte();
496 SlErrorCorrupt("Unsupported gamma");
498 i
= SlReadByte(); // 32 bits only.
500 i
= (i
<< 8) | SlReadByte();
502 i
= (i
<< 8) | SlReadByte();
504 i
= (i
<< 8) | SlReadByte();
510 * Write the header descriptor of an object or an array.
511 * If the element is bigger than 127, use 2 bytes for saving
512 * and use the highest byte of the first written one as a notice
513 * that the length consists of 2 bytes, etc.. like this:
516 * 110xxxxx xxxxxxxx xxxxxxxx
517 * 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
518 * 11110--- xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
519 * We could extend the scheme ad infinum to support arbitrarily
520 * large chunks, but as sizeof(size_t) == 4 is still very common
521 * we don't support anything above 32 bits. That's why in the last
522 * case the 3 most significant bits are unused.
523 * @param i Index being written
526 static void SlWriteSimpleGamma(size_t i
)
529 if (i
>= (1 << 14)) {
530 if (i
>= (1 << 21)) {
531 if (i
>= (1 << 28)) {
532 assert(i
<= UINT32_MAX
); // We can only support 32 bits for now.
533 SlWriteByte((byte
)(0xF0));
534 SlWriteByte((byte
)(i
>> 24));
536 SlWriteByte((byte
)(0xE0 | (i
>> 24)));
538 SlWriteByte((byte
)(i
>> 16));
540 SlWriteByte((byte
)(0xC0 | (i
>> 16)));
542 SlWriteByte((byte
)(i
>> 8));
544 SlWriteByte((byte
)(0x80 | (i
>> 8)));
547 SlWriteByte((byte
)i
);
550 /** Return how many bytes used to encode a gamma value */
551 static inline uint
SlGetGammaLength(size_t i
)
553 return 1 + (i
>= (1 << 7)) + (i
>= (1 << 14)) + (i
>= (1 << 21)) + (i
>= (1 << 28));
556 static inline uint
SlReadSparseIndex()
558 return SlReadSimpleGamma();
561 static inline void SlWriteSparseIndex(uint index
)
563 SlWriteSimpleGamma(index
);
566 static inline uint
SlReadArrayLength()
568 return SlReadSimpleGamma();
571 static inline void SlWriteArrayLength(size_t length
)
573 SlWriteSimpleGamma(length
);
576 static inline uint
SlGetArrayLength(size_t length
)
578 return SlGetGammaLength(length
);
582 * Return the size in bytes of a certain type of normal/atomic variable
583 * as it appears in memory. See VarTypes
584 * @param conv VarType type of variable that is used for calculating the size
585 * @return Return the size of this type in bytes
587 static inline uint
SlCalcConvMemLen(VarType conv
)
589 static const byte conv_mem_size
[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
590 byte length
= GB(conv
, 4, 4);
592 switch (length
<< 4) {
597 return SlReadArrayLength();
600 assert(length
< lengthof(conv_mem_size
));
601 return conv_mem_size
[length
];
606 * Return the size in bytes of a certain type of normal/atomic variable
607 * as it appears in a saved game. See VarTypes
608 * @param conv VarType type of variable that is used for calculating the size
609 * @return Return the size of this type in bytes
611 static inline byte
SlCalcConvFileLen(VarType conv
)
613 static const byte conv_file_size
[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
614 byte length
= GB(conv
, 0, 4);
615 assert(length
< lengthof(conv_file_size
));
616 return conv_file_size
[length
];
619 /** Return the size in bytes of a reference (pointer) */
620 static inline size_t SlCalcRefLen()
622 return IsSavegameVersionBefore(SLV_69
) ? 2 : 4;
625 void SlSetArrayIndex(uint index
)
627 _sl
.need_length
= NL_WANTLENGTH
;
628 _sl
.array_index
= index
;
631 static size_t _next_offs
;
634 * Iterate through the elements of an array and read the whole thing
635 * @return The index of the object, or -1 if we have reached the end of current block
641 /* After reading in the whole array inside the loop
642 * we must have read in all the data, so we must be at end of current block. */
643 if (_next_offs
!= 0 && _sl
.reader
->GetSize() != _next_offs
) SlErrorCorrupt("Invalid chunk size");
646 uint length
= SlReadArrayLength();
652 _sl
.obj_len
= --length
;
653 _next_offs
= _sl
.reader
->GetSize() + length
;
655 switch (_sl
.block_mode
) {
656 case CH_SPARSE_ARRAY
: index
= (int)SlReadSparseIndex(); break;
657 case CH_ARRAY
: index
= _sl
.array_index
++; break;
659 DEBUG(sl
, 0, "SlIterateArray error");
663 if (length
!= 0) return index
;
668 * Skip an array or sparse array
672 while (SlIterateArray() != -1) {
673 SlSkipBytes(_next_offs
- _sl
.reader
->GetSize());
678 * Sets the length of either a RIFF object or the number of items in an array.
679 * This lets us load an object or an array of arbitrary size
680 * @param length The length of the sought object/array
682 void SlSetLength(size_t length
)
684 assert(_sl
.action
== SLA_SAVE
);
686 switch (_sl
.need_length
) {
688 _sl
.need_length
= NL_NONE
;
689 switch (_sl
.block_mode
) {
691 /* Ugly encoding of >16M RIFF chunks
692 * The lower 24 bits are normal
693 * The uppermost 4 bits are bits 24:27 */
694 assert(length
< (1 << 28));
695 SlWriteUint32((uint32
)((length
& 0xFFFFFF) | ((length
>> 24) << 28)));
698 assert(_sl
.last_array_index
<= _sl
.array_index
);
699 while (++_sl
.last_array_index
<= _sl
.array_index
) {
700 SlWriteArrayLength(1);
702 SlWriteArrayLength(length
+ 1);
704 case CH_SPARSE_ARRAY
:
705 SlWriteArrayLength(length
+ 1 + SlGetArrayLength(_sl
.array_index
)); // Also include length of sparse index.
706 SlWriteSparseIndex(_sl
.array_index
);
708 default: NOT_REACHED();
713 _sl
.obj_len
+= (int)length
;
716 default: NOT_REACHED();
721 * Save/Load bytes. These do not need to be converted to Little/Big Endian
722 * so directly write them or read them to/from file
723 * @param ptr The source or destination of the object being manipulated
724 * @param length number of bytes this fast CopyBytes lasts
726 static void SlCopyBytes(void *ptr
, size_t length
)
728 byte
*p
= (byte
*)ptr
;
730 switch (_sl
.action
) {
733 for (; length
!= 0; length
--) *p
++ = SlReadByte();
736 for (; length
!= 0; length
--) SlWriteByte(*p
++);
738 default: NOT_REACHED();
742 /** Get the length of the current object */
743 size_t SlGetFieldLength()
749 * Return a signed-long version of the value of a setting
750 * @param ptr pointer to the variable
751 * @param conv type of variable, can be a non-clean
752 * type, eg one with other flags because it is parsed
753 * @return returns the value of the pointer-setting
755 int64
ReadValue(const void *ptr
, VarType conv
)
757 switch (GetVarMemType(conv
)) {
758 case SLE_VAR_BL
: return (*(const bool *)ptr
!= 0);
759 case SLE_VAR_I8
: return *(const int8
*)ptr
;
760 case SLE_VAR_U8
: return *(const byte
*)ptr
;
761 case SLE_VAR_I16
: return *(const int16
*)ptr
;
762 case SLE_VAR_U16
: return *(const uint16
*)ptr
;
763 case SLE_VAR_I32
: return *(const int32
*)ptr
;
764 case SLE_VAR_U32
: return *(const uint32
*)ptr
;
765 case SLE_VAR_I64
: return *(const int64
*)ptr
;
766 case SLE_VAR_U64
: return *(const uint64
*)ptr
;
767 case SLE_VAR_NULL
:return 0;
768 default: NOT_REACHED();
773 * Write the value of a setting
774 * @param ptr pointer to the variable
775 * @param conv type of variable, can be a non-clean type, eg
776 * with other flags. It is parsed upon read
777 * @param val the new value being given to the variable
779 void WriteValue(void *ptr
, VarType conv
, int64 val
)
781 switch (GetVarMemType(conv
)) {
782 case SLE_VAR_BL
: *(bool *)ptr
= (val
!= 0); break;
783 case SLE_VAR_I8
: *(int8
*)ptr
= val
; break;
784 case SLE_VAR_U8
: *(byte
*)ptr
= val
; break;
785 case SLE_VAR_I16
: *(int16
*)ptr
= val
; break;
786 case SLE_VAR_U16
: *(uint16
*)ptr
= val
; break;
787 case SLE_VAR_I32
: *(int32
*)ptr
= val
; break;
788 case SLE_VAR_U32
: *(uint32
*)ptr
= val
; break;
789 case SLE_VAR_I64
: *(int64
*)ptr
= val
; break;
790 case SLE_VAR_U64
: *(uint64
*)ptr
= val
; break;
791 case SLE_VAR_NAME
: *(char**)ptr
= CopyFromOldName(val
); break;
792 case SLE_VAR_NULL
: break;
793 default: NOT_REACHED();
798 * Handle all conversion and typechecking of variables here.
799 * In the case of saving, read in the actual value from the struct
800 * and then write them to file, endian safely. Loading a value
801 * goes exactly the opposite way
802 * @param ptr The object being filled/read
803 * @param conv VarType type of the current element of the struct
805 static void SlSaveLoadConv(void *ptr
, VarType conv
)
807 switch (_sl
.action
) {
809 int64 x
= ReadValue(ptr
, conv
);
811 /* Write the value to the file and check if its value is in the desired range */
812 switch (GetVarFileType(conv
)) {
813 case SLE_FILE_I8
: assert(x
>= -128 && x
<= 127); SlWriteByte(x
);break;
814 case SLE_FILE_U8
: assert(x
>= 0 && x
<= 255); SlWriteByte(x
);break;
815 case SLE_FILE_I16
:assert(x
>= -32768 && x
<= 32767); SlWriteUint16(x
);break;
816 case SLE_FILE_STRINGID
:
817 case SLE_FILE_U16
:assert(x
>= 0 && x
<= 65535); SlWriteUint16(x
);break;
819 case SLE_FILE_U32
: SlWriteUint32((uint32
)x
);break;
821 case SLE_FILE_U64
: SlWriteUint64(x
);break;
822 default: NOT_REACHED();
829 /* Read a value from the file */
830 switch (GetVarFileType(conv
)) {
831 case SLE_FILE_I8
: x
= (int8
)SlReadByte(); break;
832 case SLE_FILE_U8
: x
= (byte
)SlReadByte(); break;
833 case SLE_FILE_I16
: x
= (int16
)SlReadUint16(); break;
834 case SLE_FILE_U16
: x
= (uint16
)SlReadUint16(); break;
835 case SLE_FILE_I32
: x
= (int32
)SlReadUint32(); break;
836 case SLE_FILE_U32
: x
= (uint32
)SlReadUint32(); break;
837 case SLE_FILE_I64
: x
= (int64
)SlReadUint64(); break;
838 case SLE_FILE_U64
: x
= (uint64
)SlReadUint64(); break;
839 case SLE_FILE_STRINGID
: x
= RemapOldStringID((uint16
)SlReadUint16()); break;
840 default: NOT_REACHED();
843 /* Write The value to the struct. These ARE endian safe. */
844 WriteValue(ptr
, conv
, x
);
847 case SLA_PTRS
: break;
848 case SLA_NULL
: break;
849 default: NOT_REACHED();
854 * Calculate the net length of a string. This is in almost all cases
855 * just strlen(), but if the string is not properly terminated, we'll
856 * resort to the maximum length of the buffer.
857 * @param ptr pointer to the stringbuffer
858 * @param length maximum length of the string (buffer). If -1 we don't care
859 * about a maximum length, but take string length as it is.
860 * @return return the net length of the string
862 static inline size_t SlCalcNetStringLen(const char *ptr
, size_t length
)
864 if (ptr
== nullptr) return 0;
865 return min(strlen(ptr
), length
- 1);
869 * Calculate the gross length of the string that it
870 * will occupy in the savegame. This includes the real length, returned
871 * by SlCalcNetStringLen and the length that the index will occupy.
872 * @param ptr pointer to the stringbuffer
873 * @param length maximum length of the string (buffer size, etc.)
874 * @param conv type of data been used
875 * @return return the gross length of the string
877 static inline size_t SlCalcStringLen(const void *ptr
, size_t length
, VarType conv
)
882 switch (GetVarMemType(conv
)) {
883 default: NOT_REACHED();
886 str
= *(const char * const *)ptr
;
891 str
= (const char *)ptr
;
896 len
= SlCalcNetStringLen(str
, len
);
897 return len
+ SlGetArrayLength(len
); // also include the length of the index
901 * Save/Load a string.
902 * @param ptr the string being manipulated
903 * @param length of the string (full length)
904 * @param conv must be SLE_FILE_STRING
906 static void SlString(void *ptr
, size_t length
, VarType conv
)
908 switch (_sl
.action
) {
911 switch (GetVarMemType(conv
)) {
912 default: NOT_REACHED();
915 len
= SlCalcNetStringLen((char *)ptr
, length
);
920 len
= SlCalcNetStringLen((char *)ptr
, SIZE_MAX
);
924 SlWriteArrayLength(len
);
925 SlCopyBytes(ptr
, len
);
930 size_t len
= SlReadArrayLength();
932 switch (GetVarMemType(conv
)) {
933 default: NOT_REACHED();
937 DEBUG(sl
, 1, "String length in savegame is bigger than buffer, truncating");
938 SlCopyBytes(ptr
, length
);
939 SlSkipBytes(len
- length
);
942 SlCopyBytes(ptr
, len
);
946 case SLE_VAR_STRQ
: // Malloc'd string, free previous incarnation, and allocate
949 *(char **)ptr
= nullptr;
952 *(char **)ptr
= MallocT
<char>(len
+ 1); // terminating '\0'
954 SlCopyBytes(ptr
, len
);
959 ((char *)ptr
)[len
] = '\0'; // properly terminate the string
960 StringValidationSettings settings
= SVS_REPLACE_WITH_QUESTION_MARK
;
961 if ((conv
& SLF_ALLOW_CONTROL
) != 0) {
962 settings
= settings
| SVS_ALLOW_CONTROL_CODE
;
963 if (IsSavegameVersionBefore(SLV_169
)) {
964 str_fix_scc_encoded((char *)ptr
, (char *)ptr
+ len
);
967 if ((conv
& SLF_ALLOW_NEWLINE
) != 0) {
968 settings
= settings
| SVS_ALLOW_NEWLINE
;
970 str_validate((char *)ptr
, (char *)ptr
+ len
, settings
);
973 case SLA_PTRS
: break;
974 case SLA_NULL
: break;
975 default: NOT_REACHED();
980 * Return the size in bytes of a certain type of atomic array
981 * @param length The length of the array counted in elements
982 * @param conv VarType type of the variable that is used in calculating the size
984 static inline size_t SlCalcArrayLen(size_t length
, VarType conv
)
986 return SlCalcConvFileLen(conv
) * length
;
990 * Save/Load an array.
991 * @param array The array being manipulated
992 * @param length The length of the array in elements
993 * @param conv VarType type of the atomic array (int, byte, uint64, etc.)
995 void SlArray(void *array
, size_t length
, VarType conv
)
997 if (_sl
.action
== SLA_PTRS
|| _sl
.action
== SLA_NULL
) return;
999 /* Automatically calculate the length? */
1000 if (_sl
.need_length
!= NL_NONE
) {
1001 SlSetLength(SlCalcArrayLen(length
, conv
));
1002 /* Determine length only? */
1003 if (_sl
.need_length
== NL_CALCLENGTH
) return;
1006 /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1007 * as a byte-type. So detect this, and adjust array size accordingly */
1008 if (_sl
.action
!= SLA_SAVE
&& _sl_version
== 0) {
1009 /* all arrays except difficulty settings */
1010 if (conv
== SLE_INT16
|| conv
== SLE_UINT16
|| conv
== SLE_STRINGID
||
1011 conv
== SLE_INT32
|| conv
== SLE_UINT32
) {
1012 SlCopyBytes(array
, length
* SlCalcConvFileLen(conv
));
1015 /* used for conversion of Money 32bit->64bit */
1016 if (conv
== (SLE_FILE_I32
| SLE_VAR_I64
)) {
1017 for (uint i
= 0; i
< length
; i
++) {
1018 ((int64
*)array
)[i
] = (int32
)BSWAP32(SlReadUint32());
1024 /* If the size of elements is 1 byte both in file and memory, no special
1025 * conversion is needed, use specialized copy-copy function to speed up things */
1026 if (conv
== SLE_INT8
|| conv
== SLE_UINT8
) {
1027 SlCopyBytes(array
, length
);
1029 byte
*a
= (byte
*)array
;
1030 byte mem_size
= SlCalcConvMemLen(conv
);
1032 for (; length
!= 0; length
--) {
1033 SlSaveLoadConv(a
, conv
);
1034 a
+= mem_size
; // get size
1041 * Pointers cannot be saved to a savegame, so this functions gets
1042 * the index of the item, and if not available, it hussles with
1043 * pointers (looks really bad :()
1044 * Remember that a nullptr item has value 0, and all
1045 * indices have +1, so vehicle 0 is saved as index 1.
1046 * @param obj The object that we want to get the index of
1047 * @param rt SLRefType type of the object the index is being sought of
1048 * @return Return the pointer converted to an index of the type pointed to
1050 static size_t ReferenceToInt(const void *obj
, SLRefType rt
)
1052 assert(_sl
.action
== SLA_SAVE
);
1054 if (obj
== nullptr) return 0;
1057 case REF_VEHICLE_OLD
: // Old vehicles we save as new ones
1058 case REF_VEHICLE
: return ((const Vehicle
*)obj
)->index
+ 1;
1059 case REF_STATION
: return ((const Station
*)obj
)->index
+ 1;
1060 case REF_TOWN
: return ((const Town
*)obj
)->index
+ 1;
1061 case REF_ORDER
: return ((const Order
*)obj
)->index
+ 1;
1062 case REF_ROADSTOPS
: return ((const RoadStop
*)obj
)->index
+ 1;
1063 case REF_ENGINE_RENEWS
: return ((const EngineRenew
*)obj
)->index
+ 1;
1064 case REF_CARGO_PACKET
: return ((const CargoPacket
*)obj
)->index
+ 1;
1065 case REF_ORDERLIST
: return ((const OrderList
*)obj
)->index
+ 1;
1066 case REF_STORAGE
: return ((const PersistentStorage
*)obj
)->index
+ 1;
1067 case REF_LINK_GRAPH
: return ((const LinkGraph
*)obj
)->index
+ 1;
1068 case REF_LINK_GRAPH_JOB
: return ((const LinkGraphJob
*)obj
)->index
+ 1;
1069 default: NOT_REACHED();
1074 * Pointers cannot be loaded from a savegame, so this function
1075 * gets the index from the savegame and returns the appropriate
1076 * pointer from the already loaded base.
1077 * Remember that an index of 0 is a nullptr pointer so all indices
1078 * are +1 so vehicle 0 is saved as 1.
1079 * @param index The index that is being converted to a pointer
1080 * @param rt SLRefType type of the object the pointer is sought of
1081 * @return Return the index converted to a pointer of any type
1083 static void *IntToReference(size_t index
, SLRefType rt
)
1085 assert_compile(sizeof(size_t) <= sizeof(void *));
1087 assert(_sl
.action
== SLA_PTRS
);
1089 /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1090 * and should be loaded like that */
1091 if (rt
== REF_VEHICLE_OLD
&& !IsSavegameVersionBefore(SLV_4
, 4)) {
1095 /* No need to look up nullptr pointers, just return immediately */
1096 if (index
== (rt
== REF_VEHICLE_OLD
? 0xFFFF : 0)) return nullptr;
1098 /* Correct index. Old vehicles were saved differently:
1099 * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1100 if (rt
!= REF_VEHICLE_OLD
) index
--;
1104 if (OrderList::IsValidID(index
)) return OrderList::Get(index
);
1105 SlErrorCorrupt("Referencing invalid OrderList");
1108 if (Order::IsValidID(index
)) return Order::Get(index
);
1109 /* in old versions, invalid order was used to mark end of order list */
1110 if (IsSavegameVersionBefore(SLV_5
, 2)) return nullptr;
1111 SlErrorCorrupt("Referencing invalid Order");
1113 case REF_VEHICLE_OLD
:
1115 if (Vehicle::IsValidID(index
)) return Vehicle::Get(index
);
1116 SlErrorCorrupt("Referencing invalid Vehicle");
1119 if (Station::IsValidID(index
)) return Station::Get(index
);
1120 SlErrorCorrupt("Referencing invalid Station");
1123 if (Town::IsValidID(index
)) return Town::Get(index
);
1124 SlErrorCorrupt("Referencing invalid Town");
1127 if (RoadStop::IsValidID(index
)) return RoadStop::Get(index
);
1128 SlErrorCorrupt("Referencing invalid RoadStop");
1130 case REF_ENGINE_RENEWS
:
1131 if (EngineRenew::IsValidID(index
)) return EngineRenew::Get(index
);
1132 SlErrorCorrupt("Referencing invalid EngineRenew");
1134 case REF_CARGO_PACKET
:
1135 if (CargoPacket::IsValidID(index
)) return CargoPacket::Get(index
);
1136 SlErrorCorrupt("Referencing invalid CargoPacket");
1139 if (PersistentStorage::IsValidID(index
)) return PersistentStorage::Get(index
);
1140 SlErrorCorrupt("Referencing invalid PersistentStorage");
1142 case REF_LINK_GRAPH
:
1143 if (LinkGraph::IsValidID(index
)) return LinkGraph::Get(index
);
1144 SlErrorCorrupt("Referencing invalid LinkGraph");
1146 case REF_LINK_GRAPH_JOB
:
1147 if (LinkGraphJob::IsValidID(index
)) return LinkGraphJob::Get(index
);
1148 SlErrorCorrupt("Referencing invalid LinkGraphJob");
1150 default: NOT_REACHED();
1155 * Return the size in bytes of a list
1156 * @param list The std::list to find the size of
1158 static inline size_t SlCalcListLen(const void *list
)
1160 const std::list
<void *> *l
= (const std::list
<void *> *) list
;
1162 int type_size
= IsSavegameVersionBefore(SLV_69
) ? 2 : 4;
1163 /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1165 return l
->size() * type_size
+ type_size
;
1171 * @param list The list being manipulated
1172 * @param conv SLRefType type of the list (Vehicle *, Station *, etc)
1174 static void SlList(void *list
, SLRefType conv
)
1176 /* Automatically calculate the length? */
1177 if (_sl
.need_length
!= NL_NONE
) {
1178 SlSetLength(SlCalcListLen(list
));
1179 /* Determine length only? */
1180 if (_sl
.need_length
== NL_CALCLENGTH
) return;
1183 typedef std::list
<void *> PtrList
;
1184 PtrList
*l
= (PtrList
*)list
;
1186 switch (_sl
.action
) {
1188 SlWriteUint32((uint32
)l
->size());
1190 PtrList::iterator iter
;
1191 for (iter
= l
->begin(); iter
!= l
->end(); ++iter
) {
1193 SlWriteUint32((uint32
)ReferenceToInt(ptr
, conv
));
1197 case SLA_LOAD_CHECK
:
1199 size_t length
= IsSavegameVersionBefore(SLV_69
) ? SlReadUint16() : SlReadUint32();
1201 /* Load each reference and push to the end of the list */
1202 for (size_t i
= 0; i
< length
; i
++) {
1203 size_t data
= IsSavegameVersionBefore(SLV_69
) ? SlReadUint16() : SlReadUint32();
1204 l
->push_back((void *)data
);
1212 PtrList::iterator iter
;
1213 for (iter
= temp
.begin(); iter
!= temp
.end(); ++iter
) {
1214 void *ptr
= IntToReference((size_t)*iter
, conv
);
1222 default: NOT_REACHED();
1228 * Template class to help with std::deque.
1230 template <typename T
>
1231 class SlDequeHelper
{
1232 typedef std::deque
<T
> SlDequeT
;
1235 * Internal templated helper to return the size in bytes of a std::deque.
1236 * @param deque The std::deque to find the size of
1237 * @param conv VarType type of variable that is used for calculating the size
1239 static size_t SlCalcDequeLen(const void *deque
, VarType conv
)
1241 const SlDequeT
*l
= (const SlDequeT
*)deque
;
1244 /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1246 return l
->size() * SlCalcConvFileLen(conv
) + type_size
;
1250 * Internal templated helper to save/load a std::deque.
1251 * @param deque The std::deque being manipulated
1252 * @param conv VarType type of variable that is used for calculating the size
1254 static void SlDeque(void *deque
, VarType conv
)
1256 SlDequeT
*l
= (SlDequeT
*)deque
;
1258 switch (_sl
.action
) {
1260 SlWriteUint32((uint32
)l
->size());
1262 typename
SlDequeT::iterator iter
;
1263 for (iter
= l
->begin(); iter
!= l
->end(); ++iter
) {
1264 SlSaveLoadConv(&(*iter
), conv
);
1268 case SLA_LOAD_CHECK
:
1270 size_t length
= SlReadUint32();
1272 /* Load each value and push to the end of the deque */
1273 for (size_t i
= 0; i
< length
; i
++) {
1275 SlSaveLoadConv(&data
, conv
);
1285 default: NOT_REACHED();
1292 * Return the size in bytes of a std::deque.
1293 * @param deque The std::deque to find the size of
1294 * @param conv VarType type of variable that is used for calculating the size
1296 static inline size_t SlCalcDequeLen(const void *deque
, VarType conv
)
1298 switch (GetVarMemType(conv
)) {
1300 return SlDequeHelper
<bool>::SlCalcDequeLen(deque
, conv
);
1303 return SlDequeHelper
<uint8
>::SlCalcDequeLen(deque
, conv
);
1306 return SlDequeHelper
<uint16
>::SlCalcDequeLen(deque
, conv
);
1309 return SlDequeHelper
<uint32
>::SlCalcDequeLen(deque
, conv
);
1312 return SlDequeHelper
<uint64
>::SlCalcDequeLen(deque
, conv
);
1313 default: NOT_REACHED();
1319 * Save/load a std::deque.
1320 * @param deque The std::deque being manipulated
1321 * @param conv VarType type of variable that is used for calculating the size
1323 static void SlDeque(void *deque
, VarType conv
)
1325 switch (GetVarMemType(conv
)) {
1327 SlDequeHelper
<bool>::SlDeque(deque
, conv
);
1331 SlDequeHelper
<uint8
>::SlDeque(deque
, conv
);
1335 SlDequeHelper
<uint16
>::SlDeque(deque
, conv
);
1339 SlDequeHelper
<uint32
>::SlDeque(deque
, conv
);
1343 SlDequeHelper
<uint64
>::SlDeque(deque
, conv
);
1345 default: NOT_REACHED();
1350 /** Are we going to save this object or not? */
1351 static inline bool SlIsObjectValidInSavegame(const SaveLoad
*sld
)
1353 if (_sl_version
< sld
->version_from
|| _sl_version
>= sld
->version_to
) return false;
1354 if (sld
->conv
& SLF_NOT_IN_SAVE
) return false;
1360 * Are we going to load this variable when loading a savegame or not?
1361 * @note If the variable is skipped it is skipped in the savegame
1362 * bytestream itself as well, so there is no need to skip it somewhere else
1364 static inline bool SlSkipVariableOnLoad(const SaveLoad
*sld
)
1366 if ((sld
->conv
& SLF_NO_NETWORK_SYNC
) && _sl
.action
!= SLA_SAVE
&& _networking
&& !_network_server
) {
1367 SlSkipBytes(SlCalcConvMemLen(sld
->conv
) * sld
->length
);
1375 * Calculate the size of an object.
1376 * @param object to be measured
1377 * @param sld The SaveLoad description of the object so we know how to manipulate it
1378 * @return size of given object
1380 size_t SlCalcObjLength(const void *object
, const SaveLoad
*sld
)
1384 /* Need to determine the length and write a length tag. */
1385 for (; sld
->cmd
!= SL_END
; sld
++) {
1386 length
+= SlCalcObjMemberLength(object
, sld
);
1391 size_t SlCalcObjMemberLength(const void *object
, const SaveLoad
*sld
)
1393 assert(_sl
.action
== SLA_SAVE
);
1402 /* CONDITIONAL saveload types depend on the savegame version */
1403 if (!SlIsObjectValidInSavegame(sld
)) break;
1406 case SL_VAR
: return SlCalcConvFileLen(sld
->conv
);
1407 case SL_REF
: return SlCalcRefLen();
1408 case SL_ARR
: return SlCalcArrayLen(sld
->length
, sld
->conv
);
1409 case SL_STR
: return SlCalcStringLen(GetVariableAddress(object
, sld
), sld
->length
, sld
->conv
);
1410 case SL_LST
: return SlCalcListLen(GetVariableAddress(object
, sld
));
1411 case SL_DEQUE
: return SlCalcDequeLen(GetVariableAddress(object
, sld
), sld
->conv
);
1412 default: NOT_REACHED();
1415 case SL_WRITEBYTE
: return 1; // a byte is logically of size 1
1416 case SL_VEH_INCLUDE
: return SlCalcObjLength(object
, GetVehicleDescription(VEH_END
));
1417 case SL_ST_INCLUDE
: return SlCalcObjLength(object
, GetBaseStationDescription());
1418 default: NOT_REACHED();
1426 * Check whether the variable size of the variable in the saveload configuration
1427 * matches with the actual variable size.
1428 * @param sld The saveload configuration to test.
1430 static bool IsVariableSizeRight(const SaveLoad
*sld
)
1434 switch (GetVarMemType(sld
->conv
)) {
1436 return sld
->size
== sizeof(bool);
1439 return sld
->size
== sizeof(int8
);
1442 return sld
->size
== sizeof(int16
);
1445 return sld
->size
== sizeof(int32
);
1448 return sld
->size
== sizeof(int64
);
1450 return sld
->size
== sizeof(void *);
1453 /* These should all be pointer sized. */
1454 return sld
->size
== sizeof(void *);
1457 /* These should be pointer sized, or fixed array. */
1458 return sld
->size
== sizeof(void *) || sld
->size
== sld
->length
;
1465 #endif /* OTTD_ASSERT */
1467 bool SlObjectMember(void *ptr
, const SaveLoad
*sld
)
1470 assert(IsVariableSizeRight(sld
));
1473 VarType conv
= GB(sld
->conv
, 0, 8);
1481 /* CONDITIONAL saveload types depend on the savegame version */
1482 if (!SlIsObjectValidInSavegame(sld
)) return false;
1483 if (SlSkipVariableOnLoad(sld
)) return false;
1486 case SL_VAR
: SlSaveLoadConv(ptr
, conv
); break;
1487 case SL_REF
: // Reference variable, translate
1488 switch (_sl
.action
) {
1490 SlWriteUint32((uint32
)ReferenceToInt(*(void **)ptr
, (SLRefType
)conv
));
1492 case SLA_LOAD_CHECK
:
1494 *(size_t *)ptr
= IsSavegameVersionBefore(SLV_69
) ? SlReadUint16() : SlReadUint32();
1497 *(void **)ptr
= IntToReference(*(size_t *)ptr
, (SLRefType
)conv
);
1500 *(void **)ptr
= nullptr;
1502 default: NOT_REACHED();
1505 case SL_ARR
: SlArray(ptr
, sld
->length
, conv
); break;
1506 case SL_STR
: SlString(ptr
, sld
->length
, sld
->conv
); break;
1507 case SL_LST
: SlList(ptr
, (SLRefType
)conv
); break;
1508 case SL_DEQUE
: SlDeque(ptr
, conv
); break;
1509 default: NOT_REACHED();
1513 /* SL_WRITEBYTE writes a value to the savegame to identify the type of an object.
1514 * When loading, the value is read explicitly with SlReadByte() to determine which
1515 * object description to use. */
1517 switch (_sl
.action
) {
1518 case SLA_SAVE
: SlWriteByte(*(uint8
*)ptr
); break;
1519 case SLA_LOAD_CHECK
:
1522 case SLA_NULL
: break;
1523 default: NOT_REACHED();
1527 /* SL_VEH_INCLUDE loads common code for vehicles */
1528 case SL_VEH_INCLUDE
:
1529 SlObject(ptr
, GetVehicleDescription(VEH_END
));
1533 SlObject(ptr
, GetBaseStationDescription());
1536 default: NOT_REACHED();
1542 * Main SaveLoad function.
1543 * @param object The object that is being saved or loaded
1544 * @param sld The SaveLoad description of the object so we know how to manipulate it
1546 void SlObject(void *object
, const SaveLoad
*sld
)
1548 /* Automatically calculate the length? */
1549 if (_sl
.need_length
!= NL_NONE
) {
1550 SlSetLength(SlCalcObjLength(object
, sld
));
1551 if (_sl
.need_length
== NL_CALCLENGTH
) return;
1554 for (; sld
->cmd
!= SL_END
; sld
++) {
1555 void *ptr
= sld
->global
? sld
->address
: GetVariableAddress(object
, sld
);
1556 SlObjectMember(ptr
, sld
);
1561 * Save or Load (a list of) global variables
1562 * @param sldg The global variable that is being loaded or saved
1564 void SlGlobList(const SaveLoadGlobVarList
*sldg
)
1566 SlObject(nullptr, (const SaveLoad
*)sldg
);
1570 * Do something of which I have no idea what it is :P
1571 * @param proc The callback procedure that is called
1572 * @param arg The variable that will be used for the callback procedure
1574 void SlAutolength(AutolengthProc
*proc
, void *arg
)
1578 assert(_sl
.action
== SLA_SAVE
);
1580 /* Tell it to calculate the length */
1581 _sl
.need_length
= NL_CALCLENGTH
;
1586 _sl
.need_length
= NL_WANTLENGTH
;
1587 SlSetLength(_sl
.obj_len
);
1589 offs
= _sl
.dumper
->GetSize() + _sl
.obj_len
;
1591 /* And write the stuff */
1594 if (offs
!= _sl
.dumper
->GetSize()) SlErrorCorrupt("Invalid chunk size");
1598 * Load a chunk of data (eg vehicles, stations, etc.)
1599 * @param ch The chunkhandler that will be used for the operation
1601 static void SlLoadChunk(const ChunkHandler
*ch
)
1603 byte m
= SlReadByte();
1612 _sl
.array_index
= 0;
1614 if (_next_offs
!= 0) SlErrorCorrupt("Invalid array length");
1616 case CH_SPARSE_ARRAY
:
1618 if (_next_offs
!= 0) SlErrorCorrupt("Invalid array length");
1621 if ((m
& 0xF) == CH_RIFF
) {
1623 len
= (SlReadByte() << 16) | ((m
>> 4) << 24);
1624 len
+= SlReadUint16();
1626 endoffs
= _sl
.reader
->GetSize() + len
;
1628 if (_sl
.reader
->GetSize() != endoffs
) SlErrorCorrupt("Invalid chunk size");
1630 SlErrorCorrupt("Invalid chunk type");
1637 * Load a chunk of data for checking savegames.
1638 * If the chunkhandler is nullptr, the chunk is skipped.
1639 * @param ch The chunkhandler that will be used for the operation
1641 static void SlLoadCheckChunk(const ChunkHandler
*ch
)
1643 byte m
= SlReadByte();
1652 _sl
.array_index
= 0;
1653 if (ch
->load_check_proc
) {
1654 ch
->load_check_proc();
1659 case CH_SPARSE_ARRAY
:
1660 if (ch
->load_check_proc
) {
1661 ch
->load_check_proc();
1667 if ((m
& 0xF) == CH_RIFF
) {
1669 len
= (SlReadByte() << 16) | ((m
>> 4) << 24);
1670 len
+= SlReadUint16();
1672 endoffs
= _sl
.reader
->GetSize() + len
;
1673 if (ch
->load_check_proc
) {
1674 ch
->load_check_proc();
1678 if (_sl
.reader
->GetSize() != endoffs
) SlErrorCorrupt("Invalid chunk size");
1680 SlErrorCorrupt("Invalid chunk type");
1687 * Stub Chunk handlers to only calculate length and do nothing else.
1688 * The intended chunk handler that should be called.
1690 static ChunkSaveLoadProc
*_stub_save_proc
;
1693 * Stub Chunk handlers to only calculate length and do nothing else.
1694 * Actually call the intended chunk handler.
1695 * @param arg ignored parameter.
1697 static inline void SlStubSaveProc2(void *arg
)
1703 * Stub Chunk handlers to only calculate length and do nothing else.
1704 * Call SlAutoLenth with our stub save proc that will eventually
1705 * call the intended chunk handler.
1707 static void SlStubSaveProc()
1709 SlAutolength(SlStubSaveProc2
, nullptr);
1713 * Save a chunk of data (eg. vehicles, stations, etc.). Each chunk is
1714 * prefixed by an ID identifying it, followed by data, and terminator where appropriate
1715 * @param ch The chunkhandler that will be used for the operation
1717 static void SlSaveChunk(const ChunkHandler
*ch
)
1719 ChunkSaveLoadProc
*proc
= ch
->save_proc
;
1721 /* Don't save any chunk information if there is no save handler. */
1722 if (proc
== nullptr) return;
1724 SlWriteUint32(ch
->id
);
1725 DEBUG(sl
, 2, "Saving chunk %c%c%c%c", ch
->id
>> 24, ch
->id
>> 16, ch
->id
>> 8, ch
->id
);
1727 if (ch
->flags
& CH_AUTO_LENGTH
) {
1728 /* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */
1729 _stub_save_proc
= proc
;
1730 proc
= SlStubSaveProc
;
1733 _sl
.block_mode
= ch
->flags
& CH_TYPE_MASK
;
1734 switch (ch
->flags
& CH_TYPE_MASK
) {
1736 _sl
.need_length
= NL_WANTLENGTH
;
1740 _sl
.last_array_index
= 0;
1741 SlWriteByte(CH_ARRAY
);
1743 SlWriteArrayLength(0); // Terminate arrays
1745 case CH_SPARSE_ARRAY
:
1746 SlWriteByte(CH_SPARSE_ARRAY
);
1748 SlWriteArrayLength(0); // Terminate arrays
1750 default: NOT_REACHED();
1754 /** Save all chunks */
1755 static void SlSaveChunks()
1757 FOR_ALL_CHUNK_HANDLERS(ch
) {
1766 * Find the ChunkHandler that will be used for processing the found
1767 * chunk in the savegame or in memory
1768 * @param id the chunk in question
1769 * @return returns the appropriate chunkhandler
1771 static const ChunkHandler
*SlFindChunkHandler(uint32 id
)
1773 FOR_ALL_CHUNK_HANDLERS(ch
) if (ch
->id
== id
) return ch
;
1777 /** Load all chunks */
1778 static void SlLoadChunks()
1781 const ChunkHandler
*ch
;
1783 for (id
= SlReadUint32(); id
!= 0; id
= SlReadUint32()) {
1784 DEBUG(sl
, 2, "Loading chunk %c%c%c%c", id
>> 24, id
>> 16, id
>> 8, id
);
1786 ch
= SlFindChunkHandler(id
);
1787 if (ch
== nullptr) SlErrorCorrupt("Unknown chunk type");
1792 /** Load all chunks for savegame checking */
1793 static void SlLoadCheckChunks()
1796 const ChunkHandler
*ch
;
1798 for (id
= SlReadUint32(); id
!= 0; id
= SlReadUint32()) {
1799 DEBUG(sl
, 2, "Loading chunk %c%c%c%c", id
>> 24, id
>> 16, id
>> 8, id
);
1801 ch
= SlFindChunkHandler(id
);
1802 if (ch
== nullptr) SlErrorCorrupt("Unknown chunk type");
1803 SlLoadCheckChunk(ch
);
1807 /** Fix all pointers (convert index -> pointer) */
1808 static void SlFixPointers()
1810 _sl
.action
= SLA_PTRS
;
1812 DEBUG(sl
, 1, "Fixing pointers");
1814 FOR_ALL_CHUNK_HANDLERS(ch
) {
1815 if (ch
->ptrs_proc
!= nullptr) {
1816 DEBUG(sl
, 2, "Fixing pointers for %c%c%c%c", ch
->id
>> 24, ch
->id
>> 16, ch
->id
>> 8, ch
->id
);
1821 DEBUG(sl
, 1, "All pointers fixed");
1823 assert(_sl
.action
== SLA_PTRS
);
1827 /** Yes, simply reading from a file. */
1828 struct FileReader
: LoadFilter
{
1829 FILE *file
; ///< The file to read from.
1830 long begin
; ///< The begin of the file.
1833 * Create the file reader, so it reads from a specific file.
1834 * @param file The file to read from.
1836 FileReader(FILE *file
) : LoadFilter(nullptr), file(file
), begin(ftell(file
))
1840 /** Make sure everything is cleaned up. */
1843 if (this->file
!= nullptr) fclose(this->file
);
1844 this->file
= nullptr;
1846 /* Make sure we don't double free. */
1850 size_t Read(byte
*buf
, size_t size
) override
1852 /* We're in the process of shutting down, i.e. in "failure" mode. */
1853 if (this->file
== nullptr) return 0;
1855 return fread(buf
, 1, size
, this->file
);
1858 void Reset() override
1860 clearerr(this->file
);
1861 if (fseek(this->file
, this->begin
, SEEK_SET
)) {
1862 DEBUG(sl
, 1, "Could not reset the file reading");
1867 /** Yes, simply writing to a file. */
1868 struct FileWriter
: SaveFilter
{
1869 FILE *file
; ///< The file to write to.
1872 * Create the file writer, so it writes to a specific file.
1873 * @param file The file to write to.
1875 FileWriter(FILE *file
) : SaveFilter(nullptr), file(file
)
1879 /** Make sure everything is cleaned up. */
1884 /* Make sure we don't double free. */
1888 void Write(byte
*buf
, size_t size
) override
1890 /* We're in the process of shutting down, i.e. in "failure" mode. */
1891 if (this->file
== nullptr) return;
1893 if (fwrite(buf
, 1, size
, this->file
) != size
) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE
);
1896 void Finish() override
1898 if (this->file
!= nullptr) fclose(this->file
);
1899 this->file
= nullptr;
1903 /*******************************************
1904 ********** START OF LZO CODE **************
1905 *******************************************/
1908 #include <lzo/lzo1x.h>
1910 /** Buffer size for the LZO compressor */
1911 static const uint LZO_BUFFER_SIZE
= 8192;
1913 /** Filter using LZO compression. */
1914 struct LZOLoadFilter
: LoadFilter
{
1916 * Initialise this filter.
1917 * @param chain The next filter in this chain.
1919 LZOLoadFilter(LoadFilter
*chain
) : LoadFilter(chain
)
1921 if (lzo_init() != LZO_E_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "cannot initialize decompressor");
1924 size_t Read(byte
*buf
, size_t ssize
) override
1926 assert(ssize
>= LZO_BUFFER_SIZE
);
1928 /* Buffer size is from the LZO docs plus the chunk header size. */
1929 byte out
[LZO_BUFFER_SIZE
+ LZO_BUFFER_SIZE
/ 16 + 64 + 3 + sizeof(uint32
) * 2];
1932 lzo_uint len
= ssize
;
1935 if (this->chain
->Read((byte
*)tmp
, sizeof(tmp
)) != sizeof(tmp
)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE
, "File read failed");
1937 /* Check if size is bad */
1938 ((uint32
*)out
)[0] = size
= tmp
[1];
1940 if (_sl_version
!= SL_MIN_VERSION
) {
1941 tmp
[0] = TO_BE32(tmp
[0]);
1942 size
= TO_BE32(size
);
1945 if (size
>= sizeof(out
)) SlErrorCorrupt("Inconsistent size");
1948 if (this->chain
->Read(out
+ sizeof(uint32
), size
) != size
) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE
);
1950 /* Verify checksum */
1951 if (tmp
[0] != lzo_adler32(0, out
, size
+ sizeof(uint32
))) SlErrorCorrupt("Bad checksum");
1954 int ret
= lzo1x_decompress_safe(out
+ sizeof(uint32
) * 1, size
, buf
, &len
, nullptr);
1955 if (ret
!= LZO_E_OK
) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE
);
1960 /** Filter using LZO compression. */
1961 struct LZOSaveFilter
: SaveFilter
{
1963 * Initialise this filter.
1964 * @param chain The next filter in this chain.
1965 * @param compression_level The requested level of compression.
1967 LZOSaveFilter(SaveFilter
*chain
, byte compression_level
) : SaveFilter(chain
)
1969 if (lzo_init() != LZO_E_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "cannot initialize compressor");
1972 void Write(byte
*buf
, size_t size
) override
1974 const lzo_bytep in
= buf
;
1975 /* Buffer size is from the LZO docs plus the chunk header size. */
1976 byte out
[LZO_BUFFER_SIZE
+ LZO_BUFFER_SIZE
/ 16 + 64 + 3 + sizeof(uint32
) * 2];
1977 byte wrkmem
[LZO1X_1_MEM_COMPRESS
];
1981 /* Compress up to LZO_BUFFER_SIZE bytes at once. */
1982 lzo_uint len
= size
> LZO_BUFFER_SIZE
? LZO_BUFFER_SIZE
: (lzo_uint
)size
;
1983 lzo1x_1_compress(in
, len
, out
+ sizeof(uint32
) * 2, &outlen
, wrkmem
);
1984 ((uint32
*)out
)[1] = TO_BE32((uint32
)outlen
);
1985 ((uint32
*)out
)[0] = TO_BE32(lzo_adler32(0, out
+ sizeof(uint32
), outlen
+ sizeof(uint32
)));
1986 this->chain
->Write(out
, outlen
+ sizeof(uint32
) * 2);
1988 /* Move to next data chunk. */
1995 #endif /* WITH_LZO */
1997 /*********************************************
1998 ******** START OF NOCOMP CODE (uncompressed)*
1999 *********************************************/
2001 /** Filter without any compression. */
2002 struct NoCompLoadFilter
: LoadFilter
{
2004 * Initialise this filter.
2005 * @param chain The next filter in this chain.
2007 NoCompLoadFilter(LoadFilter
*chain
) : LoadFilter(chain
)
2011 size_t Read(byte
*buf
, size_t size
) override
2013 return this->chain
->Read(buf
, size
);
2017 /** Filter without any compression. */
2018 struct NoCompSaveFilter
: SaveFilter
{
2020 * Initialise this filter.
2021 * @param chain The next filter in this chain.
2022 * @param compression_level The requested level of compression.
2024 NoCompSaveFilter(SaveFilter
*chain
, byte compression_level
) : SaveFilter(chain
)
2028 void Write(byte
*buf
, size_t size
) override
2030 this->chain
->Write(buf
, size
);
2034 /********************************************
2035 ********** START OF ZLIB CODE **************
2036 ********************************************/
2038 #if defined(WITH_ZLIB)
2041 /** Filter using Zlib compression. */
2042 struct ZlibLoadFilter
: LoadFilter
{
2043 z_stream z
; ///< Stream state we are reading from.
2044 byte fread_buf
[MEMORY_CHUNK_SIZE
]; ///< Buffer for reading from the file.
2047 * Initialise this filter.
2048 * @param chain The next filter in this chain.
2050 ZlibLoadFilter(LoadFilter
*chain
) : LoadFilter(chain
)
2052 memset(&this->z
, 0, sizeof(this->z
));
2053 if (inflateInit(&this->z
) != Z_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "cannot initialize decompressor");
2056 /** Clean everything up. */
2059 inflateEnd(&this->z
);
2062 size_t Read(byte
*buf
, size_t size
) override
2064 this->z
.next_out
= buf
;
2065 this->z
.avail_out
= (uint
)size
;
2068 /* read more bytes from the file? */
2069 if (this->z
.avail_in
== 0) {
2070 this->z
.next_in
= this->fread_buf
;
2071 this->z
.avail_in
= (uint
)this->chain
->Read(this->fread_buf
, sizeof(this->fread_buf
));
2074 /* inflate the data */
2075 int r
= inflate(&this->z
, 0);
2076 if (r
== Z_STREAM_END
) break;
2078 if (r
!= Z_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "inflate() failed");
2079 } while (this->z
.avail_out
!= 0);
2081 return size
- this->z
.avail_out
;
2085 /** Filter using Zlib compression. */
2086 struct ZlibSaveFilter
: SaveFilter
{
2087 z_stream z
; ///< Stream state we are writing to.
2090 * Initialise this filter.
2091 * @param chain The next filter in this chain.
2092 * @param compression_level The requested level of compression.
2094 ZlibSaveFilter(SaveFilter
*chain
, byte compression_level
) : SaveFilter(chain
)
2096 memset(&this->z
, 0, sizeof(this->z
));
2097 if (deflateInit(&this->z
, compression_level
) != Z_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "cannot initialize compressor");
2100 /** Clean up what we allocated. */
2103 deflateEnd(&this->z
);
2107 * Helper loop for writing the data.
2108 * @param p The bytes to write.
2109 * @param len Amount of bytes to write.
2110 * @param mode Mode for deflate.
2112 void WriteLoop(byte
*p
, size_t len
, int mode
)
2114 byte buf
[MEMORY_CHUNK_SIZE
]; // output buffer
2116 this->z
.next_in
= p
;
2117 this->z
.avail_in
= (uInt
)len
;
2119 this->z
.next_out
= buf
;
2120 this->z
.avail_out
= sizeof(buf
);
2123 * For the poor next soul who sees many valgrind warnings of the
2124 * "Conditional jump or move depends on uninitialised value(s)" kind:
2125 * According to the author of zlib it is not a bug and it won't be fixed.
2126 * http://groups.google.com/group/comp.compression/browse_thread/thread/b154b8def8c2a3ef/cdf9b8729ce17ee2
2127 * [Mark Adler, Feb 24 2004, 'zlib-1.2.1 valgrind warnings' in the newsgroup comp.compression]
2129 int r
= deflate(&this->z
, mode
);
2131 /* bytes were emitted? */
2132 if ((n
= sizeof(buf
) - this->z
.avail_out
) != 0) {
2133 this->chain
->Write(buf
, n
);
2135 if (r
== Z_STREAM_END
) break;
2137 if (r
!= Z_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "zlib returned error code");
2138 } while (this->z
.avail_in
|| !this->z
.avail_out
);
2141 void Write(byte
*buf
, size_t size
) override
2143 this->WriteLoop(buf
, size
, 0);
2146 void Finish() override
2148 this->WriteLoop(nullptr, 0, Z_FINISH
);
2149 this->chain
->Finish();
2153 #endif /* WITH_ZLIB */
2155 /********************************************
2156 ********** START OF LZMA CODE **************
2157 ********************************************/
2159 #if defined(WITH_LIBLZMA)
2163 * Have a copy of an initialised LZMA stream. We need this as it's
2164 * impossible to "re"-assign LZMA_STREAM_INIT to a variable in some
2165 * compilers, i.e. LZMA_STREAM_INIT can't be used to set something.
2166 * This var has to be used instead.
2168 static const lzma_stream _lzma_init
= LZMA_STREAM_INIT
;
2170 /** Filter without any compression. */
2171 struct LZMALoadFilter
: LoadFilter
{
2172 lzma_stream lzma
; ///< Stream state that we are reading from.
2173 byte fread_buf
[MEMORY_CHUNK_SIZE
]; ///< Buffer for reading from the file.
2176 * Initialise this filter.
2177 * @param chain The next filter in this chain.
2179 LZMALoadFilter(LoadFilter
*chain
) : LoadFilter(chain
), lzma(_lzma_init
)
2181 /* Allow saves up to 256 MB uncompressed */
2182 if (lzma_auto_decoder(&this->lzma
, 1 << 28, 0) != LZMA_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "cannot initialize decompressor");
2185 /** Clean everything up. */
2188 lzma_end(&this->lzma
);
2191 size_t Read(byte
*buf
, size_t size
) override
2193 this->lzma
.next_out
= buf
;
2194 this->lzma
.avail_out
= size
;
2197 /* read more bytes from the file? */
2198 if (this->lzma
.avail_in
== 0) {
2199 this->lzma
.next_in
= this->fread_buf
;
2200 this->lzma
.avail_in
= this->chain
->Read(this->fread_buf
, sizeof(this->fread_buf
));
2203 /* inflate the data */
2204 lzma_ret r
= lzma_code(&this->lzma
, LZMA_RUN
);
2205 if (r
== LZMA_STREAM_END
) break;
2206 if (r
!= LZMA_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "liblzma returned error code");
2207 } while (this->lzma
.avail_out
!= 0);
2209 return size
- this->lzma
.avail_out
;
2213 /** Filter using LZMA compression. */
2214 struct LZMASaveFilter
: SaveFilter
{
2215 lzma_stream lzma
; ///< Stream state that we are writing to.
2218 * Initialise this filter.
2219 * @param chain The next filter in this chain.
2220 * @param compression_level The requested level of compression.
2222 LZMASaveFilter(SaveFilter
*chain
, byte compression_level
) : SaveFilter(chain
), lzma(_lzma_init
)
2224 if (lzma_easy_encoder(&this->lzma
, compression_level
, LZMA_CHECK_CRC32
) != LZMA_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "cannot initialize compressor");
2227 /** Clean up what we allocated. */
2230 lzma_end(&this->lzma
);
2234 * Helper loop for writing the data.
2235 * @param p The bytes to write.
2236 * @param len Amount of bytes to write.
2237 * @param action Action for lzma_code.
2239 void WriteLoop(byte
*p
, size_t len
, lzma_action action
)
2241 byte buf
[MEMORY_CHUNK_SIZE
]; // output buffer
2243 this->lzma
.next_in
= p
;
2244 this->lzma
.avail_in
= len
;
2246 this->lzma
.next_out
= buf
;
2247 this->lzma
.avail_out
= sizeof(buf
);
2249 lzma_ret r
= lzma_code(&this->lzma
, action
);
2251 /* bytes were emitted? */
2252 if ((n
= sizeof(buf
) - this->lzma
.avail_out
) != 0) {
2253 this->chain
->Write(buf
, n
);
2255 if (r
== LZMA_STREAM_END
) break;
2256 if (r
!= LZMA_OK
) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, "liblzma returned error code");
2257 } while (this->lzma
.avail_in
|| !this->lzma
.avail_out
);
2260 void Write(byte
*buf
, size_t size
) override
2262 this->WriteLoop(buf
, size
, LZMA_RUN
);
2265 void Finish() override
2267 this->WriteLoop(nullptr, 0, LZMA_FINISH
);
2268 this->chain
->Finish();
2272 #endif /* WITH_LIBLZMA */
2274 /*******************************************
2275 ************* END OF CODE *****************
2276 *******************************************/
2278 /** The format for a reader/writer type of a savegame */
2279 struct SaveLoadFormat
{
2280 const char *name
; ///< name of the compressor/decompressor (debug-only)
2281 uint32 tag
; ///< the 4-letter tag by which it is identified in the savegame
2283 LoadFilter
*(*init_load
)(LoadFilter
*chain
); ///< Constructor for the load filter.
2284 SaveFilter
*(*init_write
)(SaveFilter
*chain
, byte compression
); ///< Constructor for the save filter.
2286 byte min_compression
; ///< the minimum compression level of this format
2287 byte default_compression
; ///< the default compression level of this format
2288 byte max_compression
; ///< the maximum compression level of this format
2291 /** The different saveload formats known/understood by OpenTTD. */
2292 static const SaveLoadFormat _saveload_formats
[] = {
2293 #if defined(WITH_LZO)
2294 /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2295 {"lzo", TO_BE32X('OTTD'), CreateLoadFilter
<LZOLoadFilter
>, CreateSaveFilter
<LZOSaveFilter
>, 0, 0, 0},
2297 {"lzo", TO_BE32X('OTTD'), nullptr, nullptr, 0, 0, 0},
2299 /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2300 {"none", TO_BE32X('OTTN'), CreateLoadFilter
<NoCompLoadFilter
>, CreateSaveFilter
<NoCompSaveFilter
>, 0, 0, 0},
2301 #if defined(WITH_ZLIB)
2302 /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2303 * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2304 * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2305 {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter
<ZlibLoadFilter
>, CreateSaveFilter
<ZlibSaveFilter
>, 0, 6, 9},
2307 {"zlib", TO_BE32X('OTTZ'), nullptr, nullptr, 0, 0, 0},
2309 #if defined(WITH_LIBLZMA)
2310 /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2311 * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2312 * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2313 * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2314 * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2315 {"lzma", TO_BE32X('OTTX'), CreateLoadFilter
<LZMALoadFilter
>, CreateSaveFilter
<LZMASaveFilter
>, 0, 2, 9},
2317 {"lzma", TO_BE32X('OTTX'), nullptr, nullptr, 0, 0, 0},
2322 * Return the savegameformat of the game. Whether it was created with ZLIB compression
2323 * uncompressed, or another type
2324 * @param s Name of the savegame format. If nullptr it picks the first available one
2325 * @param compression_level Output for telling what compression level we want.
2326 * @return Pointer to SaveLoadFormat struct giving all characteristics of this type of savegame
2328 static const SaveLoadFormat
*GetSavegameFormat(char *s
, byte
*compression_level
)
2330 const SaveLoadFormat
*def
= lastof(_saveload_formats
);
2332 /* find default savegame format, the highest one with which files can be written */
2333 while (!def
->init_write
) def
--;
2336 /* Get the ":..." of the compression level out of the way */
2337 char *complevel
= strrchr(s
, ':');
2338 if (complevel
!= nullptr) *complevel
= '\0';
2340 for (const SaveLoadFormat
*slf
= &_saveload_formats
[0]; slf
!= endof(_saveload_formats
); slf
++) {
2341 if (slf
->init_write
!= nullptr && strcmp(s
, slf
->name
) == 0) {
2342 *compression_level
= slf
->default_compression
;
2343 if (complevel
!= nullptr) {
2344 /* There is a compression level in the string.
2345 * First restore the : we removed to do proper name matching,
2346 * then move the the begin of the actual version. */
2350 /* Get the version and determine whether all went fine. */
2352 long level
= strtol(complevel
, &end
, 10);
2353 if (end
== complevel
|| level
!= Clamp(level
, slf
->min_compression
, slf
->max_compression
)) {
2354 SetDParamStr(0, complevel
);
2355 ShowErrorMessage(STR_CONFIG_ERROR
, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL
, WL_CRITICAL
);
2357 *compression_level
= level
;
2365 SetDParamStr(1, def
->name
);
2366 ShowErrorMessage(STR_CONFIG_ERROR
, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM
, WL_CRITICAL
);
2368 /* Restore the string by adding the : back */
2369 if (complevel
!= nullptr) *complevel
= ':';
2371 *compression_level
= def
->default_compression
;
2375 /* actual loader/saver function */
2376 void InitializeGame(uint size_x
, uint size_y
, bool reset_date
, bool reset_settings
);
2377 extern bool AfterLoadGame();
2378 extern bool LoadOldSaveGame(const char *file
);
2381 * Clear temporary data that is passed between various saveload phases.
2383 static void ResetSaveloadData()
2385 ResetTempEngineData();
2387 ResetOldWaypoints();
2391 * Clear/free saveload state.
2393 static inline void ClearSaveLoadState()
2396 _sl
.dumper
= nullptr;
2402 _sl
.reader
= nullptr;
2409 * Update the gui accordingly when starting saving
2410 * and set locks on saveload. Also turn off fast-forward cause with that
2411 * saving takes Aaaaages
2413 static void SaveFileStart()
2415 _sl
.ff_state
= _fast_forward
;
2417 SetMouseCursorBusy(true);
2419 InvalidateWindowData(WC_STATUS_BAR
, 0, SBI_SAVELOAD_START
);
2420 _sl
.saveinprogress
= true;
2423 /** Update the gui accordingly when saving is done and release locks on saveload. */
2424 static void SaveFileDone()
2426 if (_game_mode
!= GM_MENU
) _fast_forward
= _sl
.ff_state
;
2427 SetMouseCursorBusy(false);
2429 InvalidateWindowData(WC_STATUS_BAR
, 0, SBI_SAVELOAD_FINISH
);
2430 _sl
.saveinprogress
= false;
2433 /** Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friends) */
2434 void SetSaveLoadError(StringID str
)
2436 _sl
.error_str
= str
;
2439 /** Get the string representation of the error message */
2440 const char *GetSaveLoadErrorString()
2442 SetDParam(0, _sl
.error_str
);
2443 SetDParamStr(1, _sl
.extra_msg
);
2445 static char err_str
[512];
2446 GetString(err_str
, _sl
.action
== SLA_SAVE
? STR_ERROR_GAME_SAVE_FAILED
: STR_ERROR_GAME_LOAD_FAILED
, lastof(err_str
));
2450 /** Show a gui message when saving has failed */
2451 static void SaveFileError()
2453 SetDParamStr(0, GetSaveLoadErrorString());
2454 ShowErrorMessage(STR_JUST_RAW_STRING
, INVALID_STRING_ID
, WL_ERROR
);
2459 * We have written the whole game into memory, _memory_savegame, now find
2460 * and appropriate compressor and start writing to file.
2462 static SaveOrLoadResult
SaveFileToDisk(bool threaded
)
2466 const SaveLoadFormat
*fmt
= GetSavegameFormat(_savegame_format
, &compression
);
2468 /* We have written our stuff to memory, now write it to file! */
2469 uint32 hdr
[2] = { fmt
->tag
, TO_BE32(SAVEGAME_VERSION
<< 16) };
2470 _sl
.sf
->Write((byte
*)hdr
, sizeof(hdr
));
2472 _sl
.sf
= fmt
->init_write(_sl
.sf
, compression
);
2473 _sl
.dumper
->Flush(_sl
.sf
);
2475 ClearSaveLoadState();
2477 if (threaded
) SetAsyncSaveFinish(SaveFileDone
);
2481 ClearSaveLoadState();
2483 AsyncSaveFinishProc asfp
= SaveFileDone
;
2485 /* We don't want to shout when saving is just
2486 * cancelled due to a client disconnecting. */
2487 if (_sl
.error_str
!= STR_NETWORK_ERROR_LOSTCONNECTION
) {
2488 /* Skip the "colour" character */
2489 DEBUG(sl
, 0, "%s", GetSaveLoadErrorString() + 3);
2490 asfp
= SaveFileError
;
2494 SetAsyncSaveFinish(asfp
);
2502 void WaitTillSaved()
2504 if (!_save_thread
.joinable()) return;
2506 _save_thread
.join();
2508 /* Make sure every other state is handled properly as well. */
2509 ProcessAsyncSaveFinish();
2513 * Actually perform the saving of the savegame.
2514 * General tactics is to first save the game to memory, then write it to file
2515 * using the writer, either in threaded mode if possible, or single-threaded.
2516 * @param writer The filter to write the savegame to.
2517 * @param threaded Whether to try to perform the saving asynchronously.
2518 * @return Return the result of the action. #SL_OK or #SL_ERROR
2520 static SaveOrLoadResult
DoSave(SaveFilter
*writer
, bool threaded
)
2522 assert(!_sl
.saveinprogress
);
2524 _sl
.dumper
= new MemoryDumper();
2527 _sl_version
= SAVEGAME_VERSION
;
2529 SaveViewportBeforeSaveGame();
2534 if (!threaded
|| !StartNewThread(&_save_thread
, "ottd:savegame", &SaveFileToDisk
, true)) {
2535 if (threaded
) DEBUG(sl
, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2537 SaveOrLoadResult result
= SaveFileToDisk(false);
2547 * Save the game using a (writer) filter.
2548 * @param writer The filter to write the savegame to.
2549 * @param threaded Whether to try to perform the saving asynchronously.
2550 * @return Return the result of the action. #SL_OK or #SL_ERROR
2552 SaveOrLoadResult
SaveWithFilter(SaveFilter
*writer
, bool threaded
)
2555 _sl
.action
= SLA_SAVE
;
2556 return DoSave(writer
, threaded
);
2558 ClearSaveLoadState();
2564 * Actually perform the loading of a "non-old" savegame.
2565 * @param reader The filter to read the savegame from.
2566 * @param load_check Whether to perform the checking ("preview") or actually load the game.
2567 * @return Return the result of the action. #SL_OK or #SL_REINIT ("unload" the game)
2569 static SaveOrLoadResult
DoLoad(LoadFilter
*reader
, bool load_check
)
2574 /* Clear previous check data */
2575 _load_check_data
.Clear();
2576 /* Mark SL_LOAD_CHECK as supported for this savegame. */
2577 _load_check_data
.checkable
= true;
2581 if (_sl
.lf
->Read((byte
*)hdr
, sizeof(hdr
)) != sizeof(hdr
)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE
);
2583 /* see if we have any loader for this type. */
2584 const SaveLoadFormat
*fmt
= _saveload_formats
;
2586 /* No loader found, treat as version 0 and use LZO format */
2587 if (fmt
== endof(_saveload_formats
)) {
2588 DEBUG(sl
, 0, "Unknown savegame type, trying to load it as the buggy format");
2590 _sl_version
= SL_MIN_VERSION
;
2591 _sl_minor_version
= 0;
2593 /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2594 fmt
= _saveload_formats
;
2596 if (fmt
== endof(_saveload_formats
)) {
2597 /* Who removed LZO support? Bad bad boy! */
2600 if (fmt
->tag
== TO_BE32X('OTTD')) break;
2606 if (fmt
->tag
== hdr
[0]) {
2607 /* check version number */
2608 _sl_version
= (SaveLoadVersion
)(TO_BE32(hdr
[1]) >> 16);
2609 /* Minor is not used anymore from version 18.0, but it is still needed
2610 * in versions before that (4 cases) which can't be removed easy.
2611 * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2612 _sl_minor_version
= (TO_BE32(hdr
[1]) >> 8) & 0xFF;
2614 DEBUG(sl
, 1, "Loading savegame version %d", _sl_version
);
2616 /* Is the version higher than the current? */
2617 if (_sl_version
> SAVEGAME_VERSION
) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME
);
2624 /* loader for this savegame type is not implemented? */
2625 if (fmt
->init_load
== nullptr) {
2627 seprintf(err_str
, lastof(err_str
), "Loader for '%s' is not available.", fmt
->name
);
2628 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR
, err_str
);
2631 _sl
.lf
= fmt
->init_load(_sl
.lf
);
2632 _sl
.reader
= new ReadBuffer(_sl
.lf
);
2636 ResetSaveloadData();
2638 /* Old maps were hardcoded to 256x256 and thus did not contain
2639 * any mapsize information. Pre-initialize to 256x256 to not to
2640 * confuse old games */
2641 InitializeGame(256, 256, true, true);
2645 if (IsSavegameVersionBefore(SLV_4
)) {
2647 * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
2648 * shared savegame version 4. Anything before that 'obviously'
2649 * does not have any NewGRFs. Between the introduction and
2650 * savegame version 41 (just before 0.5) the NewGRF settings
2651 * were not stored in the savegame and they were loaded by
2652 * using the settings from the main menu.
2654 * - savegame version < 4: do not load any NewGRFs.
2655 * - savegame version >= 41: load NewGRFs from savegame, which is
2656 * already done at this stage by
2657 * overwriting the main menu settings.
2658 * - other savegame versions: use main menu settings.
2660 * This means that users *can* crash savegame version 4..40
2661 * savegames if they set incompatible NewGRFs in the main menu,
2662 * but can't crash anymore for savegame version < 4 savegames.
2664 * Note: this is done here because AfterLoadGame is also called
2665 * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
2667 ClearGRFConfigList(&_grfconfig
);
2672 /* Load chunks into _load_check_data.
2673 * No pools are loaded. References are not possible, and thus do not need resolving. */
2674 SlLoadCheckChunks();
2676 /* Load chunks and resolve references */
2681 ClearSaveLoadState();
2683 _savegame_type
= SGT_OTTD
;
2686 /* The only part from AfterLoadGame() we need */
2687 _load_check_data
.grf_compatibility
= IsGoodGRFConfigList(_load_check_data
.grfconfig
);
2689 GamelogStartAction(GLAT_LOAD
);
2691 /* After loading fix up savegame for any internal changes that
2692 * might have occurred since then. If it fails, load back the old game. */
2693 if (!AfterLoadGame()) {
2694 GamelogStopAction();
2698 GamelogStopAction();
2705 * Load the game using a (reader) filter.
2706 * @param reader The filter to read the savegame from.
2707 * @return Return the result of the action. #SL_OK or #SL_REINIT ("unload" the game)
2709 SaveOrLoadResult
LoadWithFilter(LoadFilter
*reader
)
2712 _sl
.action
= SLA_LOAD
;
2713 return DoLoad(reader
, false);
2715 ClearSaveLoadState();
2721 * Main Save or Load function where the high-level saveload functions are
2722 * handled. It opens the savegame, selects format and checks versions
2723 * @param filename The name of the savegame being created/loaded
2724 * @param fop Save or load mode. Load can also be a TTD(Patch) game.
2725 * @param sb The sub directory to save the savegame in
2726 * @param threaded True when threaded saving is allowed
2727 * @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
2729 SaveOrLoadResult
SaveOrLoad(const char *filename
, SaveLoadOperation fop
, DetailedFileType dft
, Subdirectory sb
, bool threaded
)
2731 /* An instance of saving is already active, so don't go saving again */
2732 if (_sl
.saveinprogress
&& fop
== SLO_SAVE
&& dft
== DFT_GAME_FILE
&& threaded
) {
2733 /* if not an autosave, but a user action, show error message */
2734 if (!_do_autosave
) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS
, INVALID_STRING_ID
, WL_ERROR
);
2740 /* Load a TTDLX or TTDPatch game */
2741 if (fop
== SLO_LOAD
&& dft
== DFT_OLD_GAME_FILE
) {
2742 ResetSaveloadData();
2744 InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
2746 /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
2747 * and if so a new NewGRF list will be made in LoadOldSaveGame.
2748 * Note: this is done here because AfterLoadGame is also called
2749 * for OTTD savegames which have their own NewGRF logic. */
2750 ClearGRFConfigList(&_grfconfig
);
2752 if (!LoadOldSaveGame(filename
)) return SL_REINIT
;
2753 _sl_version
= SL_MIN_VERSION
;
2754 _sl_minor_version
= 0;
2755 GamelogStartAction(GLAT_LOAD
);
2756 if (!AfterLoadGame()) {
2757 GamelogStopAction();
2760 GamelogStopAction();
2764 assert(dft
== DFT_GAME_FILE
);
2767 _sl
.action
= SLA_LOAD_CHECK
;
2771 _sl
.action
= SLA_LOAD
;
2775 _sl
.action
= SLA_SAVE
;
2778 default: NOT_REACHED();
2781 FILE *fh
= (fop
== SLO_SAVE
) ? FioFOpenFile(filename
, "wb", sb
) : FioFOpenFile(filename
, "rb", sb
);
2783 /* Make it a little easier to load savegames from the console */
2784 if (fh
== nullptr && fop
!= SLO_SAVE
) fh
= FioFOpenFile(filename
, "rb", SAVE_DIR
);
2785 if (fh
== nullptr && fop
!= SLO_SAVE
) fh
= FioFOpenFile(filename
, "rb", BASE_DIR
);
2786 if (fh
== nullptr && fop
!= SLO_SAVE
) fh
= FioFOpenFile(filename
, "rb", SCENARIO_DIR
);
2788 if (fh
== nullptr) {
2789 SlError(fop
== SLO_SAVE
? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE
: STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE
);
2792 if (fop
== SLO_SAVE
) { // SAVE game
2793 DEBUG(desync
, 1, "save: %08x; %02x; %s", _date
, _date_fract
, filename
);
2794 if (_network_server
|| !_settings_client
.gui
.threaded_saves
) threaded
= false;
2796 return DoSave(new FileWriter(fh
), threaded
);
2800 assert(fop
== SLO_LOAD
|| fop
== SLO_CHECK
);
2801 DEBUG(desync
, 1, "load: %s", filename
);
2802 return DoLoad(new FileReader(fh
), fop
== SLO_CHECK
);
2804 /* This code may be executed both for old and new save games. */
2805 ClearSaveLoadState();
2807 /* Skip the "colour" character */
2808 if (fop
!= SLO_CHECK
) DEBUG(sl
, 0, "%s", GetSaveLoadErrorString() + 3);
2810 /* A saver/loader exception!! reinitialize all variables to prevent crash! */
2811 return (fop
== SLO_LOAD
) ? SL_REINIT
: SL_ERROR
;
2815 /** Do a save when exiting the game (_settings_client.gui.autosave_on_exit) */
2818 SaveOrLoad("exit.sav", SLO_SAVE
, DFT_GAME_FILE
, AUTOSAVE_DIR
);
2822 * Fill the buffer with the default name for a savegame *or* screenshot.
2823 * @param buf the buffer to write to.
2824 * @param last the last element in the buffer.
2826 void GenerateDefaultSaveName(char *buf
, const char *last
)
2828 /* Check if we have a name for this map, which is the name of the first
2829 * available company. When there's no company available we'll use
2830 * 'Spectator' as "company" name. */
2831 CompanyID cid
= _local_company
;
2832 if (!Company::IsValidID(cid
)) {
2833 for (const Company
*c
: Company::Iterate()) {
2841 /* Insert current date */
2842 switch (_settings_client
.gui
.date_format_in_default_names
) {
2843 case 0: SetDParam(1, STR_JUST_DATE_LONG
); break;
2844 case 1: SetDParam(1, STR_JUST_DATE_TINY
); break;
2845 case 2: SetDParam(1, STR_JUST_DATE_ISO
); break;
2846 default: NOT_REACHED();
2848 SetDParam(2, _date
);
2850 /* Get the correct string (special string for when there's not company) */
2851 GetString(buf
, !Company::IsValidID(cid
) ? STR_SAVEGAME_NAME_SPECTATOR
: STR_SAVEGAME_NAME_DEFAULT
, last
);
2852 SanitizeFilename(buf
);
2856 * Set the mode and file type of the file to save or load based on the type of file entry at the file system.
2857 * @param ft Type of file entry of the file system.
2859 void FileToSaveLoad::SetMode(FiosType ft
)
2861 this->SetMode(SLO_LOAD
, GetAbstractFileType(ft
), GetDetailedFileType(ft
));
2865 * Set the mode and file type of the file to save or load.
2866 * @param fop File operation being performed.
2867 * @param aft Abstract file type.
2868 * @param dft Detailed file type.
2870 void FileToSaveLoad::SetMode(SaveLoadOperation fop
, AbstractFileType aft
, DetailedFileType dft
)
2872 if (aft
== FT_INVALID
|| aft
== FT_NONE
) {
2873 this->file_op
= SLO_INVALID
;
2874 this->detail_ftype
= DFT_INVALID
;
2875 this->abstract_ftype
= FT_INVALID
;
2879 this->file_op
= fop
;
2880 this->detail_ftype
= dft
;
2881 this->abstract_ftype
= aft
;
2885 * Set the name of the file.
2886 * @param name Name of the file.
2888 void FileToSaveLoad::SetName(const char *name
)
2890 strecpy(this->name
, name
, lastof(this->name
));
2894 * Set the title of the file.
2895 * @param title Title of the file.
2897 void FileToSaveLoad::SetTitle(const char *title
)
2899 strecpy(this->title
, title
, lastof(this->title
));