/* * This file is part of OpenTTD. * 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. * 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. * 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 . */ /** * @file saveload.cpp * All actions handling saving and loading goes on in this file. The general actions * are as follows for saving a game (loading is analogous): *
    *
  1. initialize the writer by creating a temporary memory-buffer for it *
  2. go through all to-be saved elements, each 'chunk' (#ChunkHandler) prefixed by a label *
  3. use their description array (#SaveLoad) to know what elements to save and in what version * of the game it was active (used when loading) *
  4. write all data byte-by-byte to the temporary buffer so it is endian-safe *
  5. when the buffer is full; flush it to the output (eg save to file) (_sl.buf, _sl.bufp, _sl.bufe) *
  6. repeat this until everything is done, and flush any remaining output to file *
*/ #include "../stdafx.h" #include "../debug.h" #include "../station_base.h" #include "../thread.h" #include "../town.h" #include "../network/network.h" #include "../window_func.h" #include "../strings_func.h" #include "../core/endian_func.hpp" #include "../vehicle_base.h" #include "../company_func.h" #include "../timer/timer_game_economy.h" #include "../autoreplace_base.h" #include "../roadstop_base.h" #include "../linkgraph/linkgraph.h" #include "../linkgraph/linkgraphjob.h" #include "../statusbar_gui.h" #include "../fileio_func.h" #include "../gamelog.h" #include "../string_func.h" #include "../fios.h" #include "../error.h" #include "../strings_type.h" #include "../newgrf_railtype.h" #include "../newgrf_roadtype.h" #include #ifdef __EMSCRIPTEN__ # include #endif #include "table/strings.h" #include "saveload_internal.h" #include "saveload_filter.h" #include "../safeguards.h" extern const SaveLoadVersion SAVEGAME_VERSION = (SaveLoadVersion)(SL_MAX_VERSION - 1); ///< Current savegame version of OpenTTD. SavegameType _savegame_type; ///< type of savegame we are loading FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop. uint32_t _ttdp_version; ///< version of TTDP savegame (if applicable) SaveLoadVersion _sl_version; ///< the major savegame version identifier uint8_t _sl_minor_version; ///< the minor savegame version, DO NOT USE! std::string _savegame_format; ///< how to compress savegames bool _do_autosave; ///< are we doing an autosave at the moment? /** What are we currently doing? */ enum SaveLoadAction : uint8_t { SLA_LOAD, ///< loading SLA_SAVE, ///< saving SLA_PTRS, ///< fixing pointers SLA_NULL, ///< null all pointers (on loading error) SLA_LOAD_CHECK, ///< partial loading into #_load_check_data }; enum NeedLength : uint8_t { NL_NONE = 0, ///< not working in NeedLength mode NL_WANTLENGTH = 1, ///< writing length and data NL_CALCLENGTH = 2, ///< need to calculate the length }; /** Save in chunks of 128 KiB. */ static const size_t MEMORY_CHUNK_SIZE = 128 * 1024; /** A buffer for reading (and buffering) savegame data. */ struct ReadBuffer { uint8_t buf[MEMORY_CHUNK_SIZE]; ///< Buffer we're going to read from. uint8_t *bufp; ///< Location we're at reading the buffer. uint8_t *bufe; ///< End of the buffer we can read from. std::shared_ptr reader; ///< The filter used to actually read. size_t read; ///< The amount of read bytes so far from the filter. /** * Initialise our variables. * @param reader The filter to actually read data. */ ReadBuffer(std::shared_ptr reader) : bufp(nullptr), bufe(nullptr), reader(reader), read(0) { } inline uint8_t ReadByte() { if (this->bufp == this->bufe) { size_t len = this->reader->Read(this->buf, lengthof(this->buf)); if (len == 0) SlErrorCorrupt("Unexpected end of chunk"); this->read += len; this->bufp = this->buf; this->bufe = this->buf + len; } return *this->bufp++; } /** * Get the size of the memory dump made so far. * @return The size. */ size_t GetSize() const { return this->read - (this->bufe - this->bufp); } }; /** Container for dumping the savegame (quickly) to memory. */ struct MemoryDumper { std::vector> blocks{}; ///< Buffer with blocks of allocated memory. uint8_t *buf = nullptr; ///< Buffer we're going to write to. uint8_t *bufe = nullptr; ///< End of the buffer we write to. /** * Write a single byte into the dumper. * @param b The byte to write. */ inline void WriteByte(uint8_t b) { /* Are we at the end of this chunk? */ if (this->buf == this->bufe) { this->buf = this->blocks.emplace_back(std::make_unique(MEMORY_CHUNK_SIZE)).get(); this->bufe = this->buf + MEMORY_CHUNK_SIZE; } *this->buf++ = b; } /** * Flush this dumper into a writer. * @param writer The filter we want to use. */ void Flush(std::shared_ptr writer) { uint i = 0; size_t t = this->GetSize(); while (t > 0) { size_t to_write = std::min(MEMORY_CHUNK_SIZE, t); writer->Write(this->blocks[i++].get(), to_write); t -= to_write; } writer->Finish(); } /** * Get the size of the memory dump made so far. * @return The size. */ size_t GetSize() const { return this->blocks.size() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf); } }; /** The saveload struct, containing reader-writer functions, buffer, version, etc. */ struct SaveLoadParams { SaveLoadAction action; ///< are we doing a save or a load atm. NeedLength need_length; ///< working in NeedLength (Autolength) mode? uint8_t block_mode; ///< ??? bool error; ///< did an error occur or not size_t obj_len; ///< the length of the current object we are busy with int array_index, last_array_index; ///< in the case of an array, the current and last positions bool expect_table_header; ///< In the case of a table, if the header is saved/loaded. std::unique_ptr dumper; ///< Memory dumper to write the savegame to. std::shared_ptr sf; ///< Filter to write the savegame to. std::unique_ptr reader; ///< Savegame reading buffer. std::shared_ptr lf; ///< Filter to read the savegame from. StringID error_str; ///< the translatable error message to show std::string extra_msg; ///< the error message bool saveinprogress; ///< Whether there is currently a save in progress. }; static SaveLoadParams _sl; ///< Parameters used for/at saveload. static const std::vector &ChunkHandlers() { /* These define the chunks */ extern const ChunkHandlerTable _gamelog_chunk_handlers; extern const ChunkHandlerTable _map_chunk_handlers; extern const ChunkHandlerTable _misc_chunk_handlers; extern const ChunkHandlerTable _name_chunk_handlers; extern const ChunkHandlerTable _cheat_chunk_handlers; extern const ChunkHandlerTable _setting_chunk_handlers; extern const ChunkHandlerTable _company_chunk_handlers; extern const ChunkHandlerTable _engine_chunk_handlers; extern const ChunkHandlerTable _veh_chunk_handlers; extern const ChunkHandlerTable _waypoint_chunk_handlers; extern const ChunkHandlerTable _depot_chunk_handlers; extern const ChunkHandlerTable _order_chunk_handlers; extern const ChunkHandlerTable _town_chunk_handlers; extern const ChunkHandlerTable _sign_chunk_handlers; extern const ChunkHandlerTable _station_chunk_handlers; extern const ChunkHandlerTable _industry_chunk_handlers; extern const ChunkHandlerTable _economy_chunk_handlers; extern const ChunkHandlerTable _subsidy_chunk_handlers; extern const ChunkHandlerTable _cargomonitor_chunk_handlers; extern const ChunkHandlerTable _goal_chunk_handlers; extern const ChunkHandlerTable _story_page_chunk_handlers; extern const ChunkHandlerTable _league_chunk_handlers; extern const ChunkHandlerTable _ai_chunk_handlers; extern const ChunkHandlerTable _game_chunk_handlers; extern const ChunkHandlerTable _animated_tile_chunk_handlers; extern const ChunkHandlerTable _newgrf_chunk_handlers; extern const ChunkHandlerTable _group_chunk_handlers; extern const ChunkHandlerTable _cargopacket_chunk_handlers; extern const ChunkHandlerTable _autoreplace_chunk_handlers; extern const ChunkHandlerTable _labelmaps_chunk_handlers; extern const ChunkHandlerTable _linkgraph_chunk_handlers; extern const ChunkHandlerTable _airport_chunk_handlers; extern const ChunkHandlerTable _object_chunk_handlers; extern const ChunkHandlerTable _persistent_storage_chunk_handlers; extern const ChunkHandlerTable _water_region_chunk_handlers; extern const ChunkHandlerTable _randomizer_chunk_handlers; /** List of all chunks in a savegame. */ static const ChunkHandlerTable _chunk_handler_tables[] = { _gamelog_chunk_handlers, _map_chunk_handlers, _misc_chunk_handlers, _name_chunk_handlers, _cheat_chunk_handlers, _setting_chunk_handlers, _veh_chunk_handlers, _waypoint_chunk_handlers, _depot_chunk_handlers, _order_chunk_handlers, _industry_chunk_handlers, _economy_chunk_handlers, _subsidy_chunk_handlers, _cargomonitor_chunk_handlers, _goal_chunk_handlers, _story_page_chunk_handlers, _league_chunk_handlers, _engine_chunk_handlers, _town_chunk_handlers, _sign_chunk_handlers, _station_chunk_handlers, _company_chunk_handlers, _ai_chunk_handlers, _game_chunk_handlers, _animated_tile_chunk_handlers, _newgrf_chunk_handlers, _group_chunk_handlers, _cargopacket_chunk_handlers, _autoreplace_chunk_handlers, _labelmaps_chunk_handlers, _linkgraph_chunk_handlers, _airport_chunk_handlers, _object_chunk_handlers, _persistent_storage_chunk_handlers, _water_region_chunk_handlers, _randomizer_chunk_handlers, }; static std::vector _chunk_handlers; if (_chunk_handlers.empty()) { for (auto &chunk_handler_table : _chunk_handler_tables) { for (auto &chunk_handler : chunk_handler_table) { _chunk_handlers.push_back(chunk_handler); } } } return _chunk_handlers; } /** Null all pointers (convert index -> nullptr) */ static void SlNullPointers() { _sl.action = SLA_NULL; /* We don't want any savegame conversion code to run * during NULLing; especially those that try to get * pointers from other pools. */ _sl_version = SAVEGAME_VERSION; for (const ChunkHandler &ch : ChunkHandlers()) { Debug(sl, 3, "Nulling pointers for {}", ch.GetName()); ch.FixPointers(); } assert(_sl.action == SLA_NULL); } /** * Error handler. Sets everything up to show an error message and to clean * up the mess of a partial savegame load. * @param string The translatable error message to show. * @param extra_msg An extra error message coming from one of the APIs. * @note This function does never return as it throws an exception to * break out of all the saveload code. */ [[noreturn]] void SlError(StringID string, const std::string &extra_msg) { /* Distinguish between loading into _load_check_data vs. normal save/load. */ if (_sl.action == SLA_LOAD_CHECK) { _load_check_data.error = string; _load_check_data.error_msg = extra_msg; } else { _sl.error_str = string; _sl.extra_msg = extra_msg; } /* We have to nullptr all pointers here; we might be in a state where * the pointers are actually filled with indices, which means that * when we access them during cleaning the pool dereferences of * those indices will be made with segmentation faults as result. */ if (_sl.action == SLA_LOAD || _sl.action == SLA_PTRS) SlNullPointers(); /* Logging could be active. */ _gamelog.StopAnyAction(); throw std::exception(); } /** * Error handler for corrupt savegames. Sets everything up to show the * error message and to clean up the mess of a partial savegame load. * @param msg Location the corruption has been spotted. * @note This function does never return as it throws an exception to * break out of all the saveload code. */ [[noreturn]] void SlErrorCorrupt(const std::string &msg) { SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg); } typedef void (*AsyncSaveFinishProc)(); ///< Callback for when the savegame loading is finished. static std::atomic _async_save_finish; ///< Callback to call when the savegame loading is finished. static std::thread _save_thread; ///< The thread we're using to compress and write a savegame /** * Called by save thread to tell we finished saving. * @param proc The callback to call when saving is done. */ static void SetAsyncSaveFinish(AsyncSaveFinishProc proc) { if (_exit_game) return; while (_async_save_finish.load(std::memory_order_acquire) != nullptr) CSleep(10); _async_save_finish.store(proc, std::memory_order_release); } /** * Handle async save finishes. */ void ProcessAsyncSaveFinish() { AsyncSaveFinishProc proc = _async_save_finish.exchange(nullptr, std::memory_order_acq_rel); if (proc == nullptr) return; proc(); if (_save_thread.joinable()) { _save_thread.join(); } } /** * Wrapper for reading a byte from the buffer. * @return The read byte. */ uint8_t SlReadByte() { return _sl.reader->ReadByte(); } /** * Wrapper for writing a byte to the dumper. * @param b The byte to write. */ void SlWriteByte(uint8_t b) { _sl.dumper->WriteByte(b); } static inline int SlReadUint16() { int x = SlReadByte() << 8; return x | SlReadByte(); } static inline uint32_t SlReadUint32() { uint32_t x = SlReadUint16() << 16; return x | SlReadUint16(); } static inline uint64_t SlReadUint64() { uint32_t x = SlReadUint32(); uint32_t y = SlReadUint32(); return (uint64_t)x << 32 | y; } static inline void SlWriteUint16(uint16_t v) { SlWriteByte(GB(v, 8, 8)); SlWriteByte(GB(v, 0, 8)); } static inline void SlWriteUint32(uint32_t v) { SlWriteUint16(GB(v, 16, 16)); SlWriteUint16(GB(v, 0, 16)); } static inline void SlWriteUint64(uint64_t x) { SlWriteUint32((uint32_t)(x >> 32)); SlWriteUint32((uint32_t)x); } /** * Read in the header descriptor of an object or an array. * If the highest bit is set (7), then the index is bigger than 127 * elements, so use the next byte to read in the real value. * The actual value is then both bytes added with the first shifted * 8 bits to the left, and dropping the highest bit (which only indicated a big index). * x = ((x & 0x7F) << 8) + SlReadByte(); * @return Return the value of the index */ static uint SlReadSimpleGamma() { uint i = SlReadByte(); if (HasBit(i, 7)) { i &= ~0x80; if (HasBit(i, 6)) { i &= ~0x40; if (HasBit(i, 5)) { i &= ~0x20; if (HasBit(i, 4)) { i &= ~0x10; if (HasBit(i, 3)) { SlErrorCorrupt("Unsupported gamma"); } i = SlReadByte(); // 32 bits only. } i = (i << 8) | SlReadByte(); } i = (i << 8) | SlReadByte(); } i = (i << 8) | SlReadByte(); } return i; } /** * Write the header descriptor of an object or an array. * If the element is bigger than 127, use 2 bytes for saving * and use the highest byte of the first written one as a notice * that the length consists of 2 bytes, etc.. like this: * 0xxxxxxx * 10xxxxxx xxxxxxxx * 110xxxxx xxxxxxxx xxxxxxxx * 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx * 11110--- xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx * We could extend the scheme ad infinum to support arbitrarily * large chunks, but as sizeof(size_t) == 4 is still very common * we don't support anything above 32 bits. That's why in the last * case the 3 most significant bits are unused. * @param i Index being written */ static void SlWriteSimpleGamma(size_t i) { if (i >= (1 << 7)) { if (i >= (1 << 14)) { if (i >= (1 << 21)) { if (i >= (1 << 28)) { assert(i <= UINT32_MAX); // We can only support 32 bits for now. SlWriteByte((uint8_t)(0xF0)); SlWriteByte((uint8_t)(i >> 24)); } else { SlWriteByte((uint8_t)(0xE0 | (i >> 24))); } SlWriteByte((uint8_t)(i >> 16)); } else { SlWriteByte((uint8_t)(0xC0 | (i >> 16))); } SlWriteByte((uint8_t)(i >> 8)); } else { SlWriteByte((uint8_t)(0x80 | (i >> 8))); } } SlWriteByte((uint8_t)i); } /** Return how many bytes used to encode a gamma value */ static inline uint SlGetGammaLength(size_t i) { return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28)); } static inline uint SlReadSparseIndex() { return SlReadSimpleGamma(); } static inline void SlWriteSparseIndex(uint index) { SlWriteSimpleGamma(index); } static inline uint SlReadArrayLength() { return SlReadSimpleGamma(); } static inline void SlWriteArrayLength(size_t length) { SlWriteSimpleGamma(length); } static inline uint SlGetArrayLength(size_t length) { return SlGetGammaLength(length); } /** * Return the type as saved/loaded inside the savegame. */ static uint8_t GetSavegameFileType(const SaveLoad &sld) { switch (sld.cmd) { case SL_VAR: return GetVarFileType(sld.conv); break; case SL_STDSTR: case SL_ARR: case SL_VECTOR: case SL_DEQUE: return GetVarFileType(sld.conv) | SLE_FILE_HAS_LENGTH_FIELD; break; case SL_REF: return IsSavegameVersionBefore(SLV_69) ? SLE_FILE_U16 : SLE_FILE_U32; case SL_REFLIST: case SL_REFVECTOR: return (IsSavegameVersionBefore(SLV_69) ? SLE_FILE_U16 : SLE_FILE_U32) | SLE_FILE_HAS_LENGTH_FIELD; case SL_SAVEBYTE: return SLE_FILE_U8; case SL_STRUCT: case SL_STRUCTLIST: return SLE_FILE_STRUCT | SLE_FILE_HAS_LENGTH_FIELD; default: NOT_REACHED(); } } /** * Return the size in bytes of a certain type of normal/atomic variable * as it appears in memory. See VarTypes * @param conv VarType type of variable that is used for calculating the size * @return Return the size of this type in bytes */ static inline uint SlCalcConvMemLen(VarType conv) { switch (GetVarMemType(conv)) { case SLE_VAR_BL: return sizeof(bool); case SLE_VAR_I8: return sizeof(int8_t); case SLE_VAR_U8: return sizeof(uint8_t); case SLE_VAR_I16: return sizeof(int16_t); case SLE_VAR_U16: return sizeof(uint16_t); case SLE_VAR_I32: return sizeof(int32_t); case SLE_VAR_U32: return sizeof(uint32_t); case SLE_VAR_I64: return sizeof(int64_t); case SLE_VAR_U64: return sizeof(uint64_t); case SLE_VAR_NULL: return 0; case SLE_VAR_STR: case SLE_VAR_STRQ: return SlReadArrayLength(); case SLE_VAR_NAME: default: NOT_REACHED(); } } /** * Return the size in bytes of a certain type of normal/atomic variable * as it appears in a saved game. See VarTypes * @param conv VarType type of variable that is used for calculating the size * @return Return the size of this type in bytes */ static inline uint8_t SlCalcConvFileLen(VarType conv) { switch (GetVarFileType(conv)) { case SLE_FILE_END: return 0; case SLE_FILE_I8: return sizeof(int8_t); case SLE_FILE_U8: return sizeof(uint8_t); case SLE_FILE_I16: return sizeof(int16_t); case SLE_FILE_U16: return sizeof(uint16_t); case SLE_FILE_I32: return sizeof(int32_t); case SLE_FILE_U32: return sizeof(uint32_t); case SLE_FILE_I64: return sizeof(int64_t); case SLE_FILE_U64: return sizeof(uint64_t); case SLE_FILE_STRINGID: return sizeof(uint16_t); case SLE_FILE_STRING: return SlReadArrayLength(); case SLE_FILE_STRUCT: default: NOT_REACHED(); } } /** Return the size in bytes of a reference (pointer) */ static inline size_t SlCalcRefLen() { return IsSavegameVersionBefore(SLV_69) ? 2 : 4; } void SlSetArrayIndex(uint index) { _sl.need_length = NL_WANTLENGTH; _sl.array_index = index; } static size_t _next_offs; /** * Iterate through the elements of an array and read the whole thing * @return The index of the object, or -1 if we have reached the end of current block */ int SlIterateArray() { /* After reading in the whole array inside the loop * we must have read in all the data, so we must be at end of current block. */ if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) { SlErrorCorruptFmt("Invalid chunk size iterating array - expected to be at position {}, actually at {}", _next_offs, _sl.reader->GetSize()); } for (;;) { uint length = SlReadArrayLength(); if (length == 0) { assert(!_sl.expect_table_header); _next_offs = 0; return -1; } _sl.obj_len = --length; _next_offs = _sl.reader->GetSize() + length; if (_sl.expect_table_header) { _sl.expect_table_header = false; return INT32_MAX; } int index; switch (_sl.block_mode) { case CH_SPARSE_TABLE: case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break; case CH_TABLE: case CH_ARRAY: index = _sl.array_index++; break; default: Debug(sl, 0, "SlIterateArray error"); return -1; // error } if (length != 0) return index; } } /** * Skip an array or sparse array */ void SlSkipArray() { while (SlIterateArray() != -1) { SlSkipBytes(_next_offs - _sl.reader->GetSize()); } } /** * Sets the length of either a RIFF object or the number of items in an array. * This lets us load an object or an array of arbitrary size * @param length The length of the sought object/array */ void SlSetLength(size_t length) { assert(_sl.action == SLA_SAVE); switch (_sl.need_length) { case NL_WANTLENGTH: _sl.need_length = NL_NONE; if ((_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE) && _sl.expect_table_header) { _sl.expect_table_header = false; SlWriteArrayLength(length + 1); break; } switch (_sl.block_mode) { case CH_RIFF: /* Ugly encoding of >16M RIFF chunks * The lower 24 bits are normal * The uppermost 4 bits are bits 24:27 */ assert(length < (1 << 28)); SlWriteUint32((uint32_t)((length & 0xFFFFFF) | ((length >> 24) << 28))); break; case CH_TABLE: case CH_ARRAY: assert(_sl.last_array_index <= _sl.array_index); while (++_sl.last_array_index <= _sl.array_index) { SlWriteArrayLength(1); } SlWriteArrayLength(length + 1); break; case CH_SPARSE_TABLE: case CH_SPARSE_ARRAY: SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index. SlWriteSparseIndex(_sl.array_index); break; default: NOT_REACHED(); } break; case NL_CALCLENGTH: _sl.obj_len += (int)length; break; default: NOT_REACHED(); } } /** * Save/Load bytes. These do not need to be converted to Little/Big Endian * so directly write them or read them to/from file * @param ptr The source or destination of the object being manipulated * @param length number of bytes this fast CopyBytes lasts */ static void SlCopyBytes(void *ptr, size_t length) { uint8_t *p = (uint8_t *)ptr; switch (_sl.action) { case SLA_LOAD_CHECK: case SLA_LOAD: for (; length != 0; length--) *p++ = SlReadByte(); break; case SLA_SAVE: for (; length != 0; length--) SlWriteByte(*p++); break; default: NOT_REACHED(); } } /** Get the length of the current object */ size_t SlGetFieldLength() { return _sl.obj_len; } /** * Return a signed-long version of the value of a setting * @param ptr pointer to the variable * @param conv type of variable, can be a non-clean * type, eg one with other flags because it is parsed * @return returns the value of the pointer-setting */ int64_t ReadValue(const void *ptr, VarType conv) { switch (GetVarMemType(conv)) { case SLE_VAR_BL: return (*(const bool *)ptr != 0); case SLE_VAR_I8: return *(const int8_t *)ptr; case SLE_VAR_U8: return *(const uint8_t *)ptr; case SLE_VAR_I16: return *(const int16_t *)ptr; case SLE_VAR_U16: return *(const uint16_t*)ptr; case SLE_VAR_I32: return *(const int32_t *)ptr; case SLE_VAR_U32: return *(const uint32_t*)ptr; case SLE_VAR_I64: return *(const int64_t *)ptr; case SLE_VAR_U64: return *(const uint64_t*)ptr; case SLE_VAR_NULL:return 0; default: NOT_REACHED(); } } /** * Write the value of a setting * @param ptr pointer to the variable * @param conv type of variable, can be a non-clean type, eg * with other flags. It is parsed upon read * @param val the new value being given to the variable */ void WriteValue(void *ptr, VarType conv, int64_t val) { switch (GetVarMemType(conv)) { case SLE_VAR_BL: *(bool *)ptr = (val != 0); break; case SLE_VAR_I8: *(int8_t *)ptr = val; break; case SLE_VAR_U8: *(uint8_t *)ptr = val; break; case SLE_VAR_I16: *(int16_t *)ptr = val; break; case SLE_VAR_U16: *(uint16_t*)ptr = val; break; case SLE_VAR_I32: *(int32_t *)ptr = val; break; case SLE_VAR_U32: *(uint32_t*)ptr = val; break; case SLE_VAR_I64: *(int64_t *)ptr = val; break; case SLE_VAR_U64: *(uint64_t*)ptr = val; break; case SLE_VAR_NAME: *reinterpret_cast(ptr) = CopyFromOldName(val); break; case SLE_VAR_NULL: break; default: NOT_REACHED(); } } /** * Handle all conversion and typechecking of variables here. * In the case of saving, read in the actual value from the struct * and then write them to file, endian safely. Loading a value * goes exactly the opposite way * @param ptr The object being filled/read * @param conv VarType type of the current element of the struct */ static void SlSaveLoadConv(void *ptr, VarType conv) { switch (_sl.action) { case SLA_SAVE: { int64_t x = ReadValue(ptr, conv); /* Write the value to the file and check if its value is in the desired range */ switch (GetVarFileType(conv)) { case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break; case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break; case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break; case SLE_FILE_STRINGID: case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break; case SLE_FILE_I32: case SLE_FILE_U32: SlWriteUint32((uint32_t)x);break; case SLE_FILE_I64: case SLE_FILE_U64: SlWriteUint64(x);break; default: NOT_REACHED(); } break; } case SLA_LOAD_CHECK: case SLA_LOAD: { int64_t x; /* Read a value from the file */ switch (GetVarFileType(conv)) { case SLE_FILE_I8: x = (int8_t )SlReadByte(); break; case SLE_FILE_U8: x = (uint8_t )SlReadByte(); break; case SLE_FILE_I16: x = (int16_t )SlReadUint16(); break; case SLE_FILE_U16: x = (uint16_t)SlReadUint16(); break; case SLE_FILE_I32: x = (int32_t )SlReadUint32(); break; case SLE_FILE_U32: x = (uint32_t)SlReadUint32(); break; case SLE_FILE_I64: x = (int64_t )SlReadUint64(); break; case SLE_FILE_U64: x = (uint64_t)SlReadUint64(); break; case SLE_FILE_STRINGID: x = RemapOldStringID((uint16_t)SlReadUint16()); break; default: NOT_REACHED(); } /* Write The value to the struct. These ARE endian safe. */ WriteValue(ptr, conv, x); break; } case SLA_PTRS: break; case SLA_NULL: break; default: NOT_REACHED(); } } /** * Calculate the gross length of the string that it * will occupy in the savegame. This includes the real length, returned * by SlCalcNetStringLen and the length that the index will occupy. * @param ptr Pointer to the \c std::string. * @return The gross length of the string. */ static inline size_t SlCalcStdStringLen(const void *ptr) { const std::string *str = reinterpret_cast(ptr); size_t len = str->length(); return len + SlGetArrayLength(len); // also include the length of the index } /** * Scan the string for old values of SCC_ENCODED and fix it to it's new, value. * Note that at the moment this runs, the string has not been validated yet * because the validation looks for SCC_ENCODED. If there is something invalid, * just bail out and do not continue trying to replace the tokens. * @param str the string to fix. */ void FixSCCEncoded(std::string &str, bool fix_code) { if (str.empty()) return; /* We need to convert from old escape-style encoding to record separator encoding. * Initial `` stays the same. * * `:` becomes `` * `:` becomes `` * `:""` becomes `` */ std::string result; auto output = std::back_inserter(result); bool is_encoded = false; // Set if we determine by the presence of SCC_ENCODED that the string is an encoded string. bool in_string = false; // Set if we in a string, between double-quotes. bool need_type = true; // Set if a parameter type needs to be emitted. for (auto it = std::begin(str); it != std::end(str); /* nothing */) { size_t len = Utf8EncodedCharLen(*it); if (len == 0 || it + len > std::end(str)) break; char32_t c; Utf8Decode(&c, &*it); if (c == SCC_ENCODED || (fix_code && (c == 0xE028 || c == 0xE02A))) { Utf8Encode(output, SCC_ENCODED); need_type = false; is_encoded = true; it += len; continue; } /* If the first character is not SCC_ENCODED then we don't have to do any conversion. */ if (!is_encoded) return; if (c == '"') { in_string = !in_string; if (in_string && need_type) { /* Started a new string parameter. */ Utf8Encode(output, SCC_ENCODED_STRING); need_type = false; } it += len; continue; } if (!in_string && c == ':') { *output = SCC_RECORD_SEPARATOR; need_type = true; it += len; continue; } if (need_type) { /* Started a new numeric parameter. */ Utf8Encode(output, SCC_ENCODED_NUMERIC); need_type = false; } Utf8Encode(output, c); it += len; } str = result; } /** * Read the given amount of bytes from the buffer into the string. * @param str The string to write to. * @param length The amount of bytes to read into the string. * @note Does not perform any validation on validity of the string. */ void SlReadString(std::string &str, size_t length) { str.resize(length); SlCopyBytes(str.data(), length); } /** * Save/Load a \c std::string. * @param ptr the string being manipulated * @param conv must be SLE_FILE_STRING */ static void SlStdString(void *ptr, VarType conv) { std::string *str = reinterpret_cast(ptr); switch (_sl.action) { case SLA_SAVE: { size_t len = str->length(); SlWriteArrayLength(len); SlCopyBytes(const_cast(static_cast(str->c_str())), len); break; } case SLA_LOAD_CHECK: case SLA_LOAD: { size_t len = SlReadArrayLength(); if (GetVarMemType(conv) == SLE_VAR_NULL) { SlSkipBytes(len); return; } SlReadString(*str, len); StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK; if ((conv & SLF_ALLOW_CONTROL) != 0) { settings = settings | SVS_ALLOW_CONTROL_CODE; if (IsSavegameVersionBefore(SLV_ENCODED_STRING_FORMAT)) FixSCCEncoded(*str, IsSavegameVersionBefore(SLV_169)); } if ((conv & SLF_ALLOW_NEWLINE) != 0) { settings = settings | SVS_ALLOW_NEWLINE; } *str = StrMakeValid(*str, settings); } case SLA_PTRS: break; case SLA_NULL: break; default: NOT_REACHED(); } } /** * Internal function to save/Load a list of SL_VARs. * SlCopy() and SlArray() are very similar, with the exception of the header. * This function represents the common part. * @param object The object being manipulated. * @param length The length of the object in elements * @param conv VarType type of the items. */ static void SlCopyInternal(void *object, size_t length, VarType conv) { if (GetVarMemType(conv) == SLE_VAR_NULL) { assert(_sl.action != SLA_SAVE); // Use SL_NULL if you want to write null-bytes SlSkipBytes(length * SlCalcConvFileLen(conv)); return; } /* NOTICE - handle some buggy stuff, in really old versions everything was saved * as a byte-type. So detect this, and adjust object size accordingly */ if (_sl.action != SLA_SAVE && _sl_version == 0) { /* all objects except difficulty settings */ if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID || conv == SLE_INT32 || conv == SLE_UINT32) { SlCopyBytes(object, length * SlCalcConvFileLen(conv)); return; } /* used for conversion of Money 32bit->64bit */ if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) { for (uint i = 0; i < length; i++) { ((int64_t*)object)[i] = (int32_t)std::byteswap(SlReadUint32()); } return; } } /* If the size of elements is 1 byte both in file and memory, no special * conversion is needed, use specialized copy-copy function to speed up things */ if (conv == SLE_INT8 || conv == SLE_UINT8) { SlCopyBytes(object, length); } else { uint8_t *a = (uint8_t*)object; uint8_t mem_size = SlCalcConvMemLen(conv); for (; length != 0; length --) { SlSaveLoadConv(a, conv); a += mem_size; // get size } } } /** * Copy a list of SL_VARs to/from a savegame. * These entries are copied as-is, and you as caller have to make sure things * like length-fields are calculated correctly. * @param object The object being manipulated. * @param length The length of the object in elements * @param conv VarType type of the items. */ void SlCopy(void *object, size_t length, VarType conv) { if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return; /* Automatically calculate the length? */ if (_sl.need_length != NL_NONE) { SlSetLength(length * SlCalcConvFileLen(conv)); /* Determine length only? */ if (_sl.need_length == NL_CALCLENGTH) return; } SlCopyInternal(object, length, conv); } /** * Return the size in bytes of a certain type of atomic array * @param length The length of the array counted in elements * @param conv VarType type of the variable that is used in calculating the size */ static inline size_t SlCalcArrayLen(size_t length, VarType conv) { return SlCalcConvFileLen(conv) * length + SlGetArrayLength(length); } /** * Save/Load the length of the array followed by the array of SL_VAR elements. * @param array The array being manipulated * @param length The length of the array in elements * @param conv VarType type of the atomic array (int, uint8_t, uint64_t, etc.) */ static void SlArray(void *array, size_t length, VarType conv) { switch (_sl.action) { case SLA_SAVE: SlWriteArrayLength(length); SlCopyInternal(array, length, conv); return; case SLA_LOAD_CHECK: case SLA_LOAD: { if (!IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH)) { size_t sv_length = SlReadArrayLength(); if (GetVarMemType(conv) == SLE_VAR_NULL) { /* We don't know this field, so we assume the length in the savegame is correct. */ length = sv_length; } else if (sv_length != length) { /* If the SLE_ARR changes size, a savegame bump is required * and the developer should have written conversion lines. * Error out to make this more visible. */ SlErrorCorrupt("Fixed-length array is of wrong length"); } } SlCopyInternal(array, length, conv); return; } case SLA_PTRS: case SLA_NULL: return; default: NOT_REACHED(); } } /** * Pointers cannot be saved to a savegame, so this functions gets * the index of the item, and if not available, it hussles with * pointers (looks really bad :() * Remember that a nullptr item has value 0, and all * indices have +1, so vehicle 0 is saved as index 1. * @param obj The object that we want to get the index of * @param rt SLRefType type of the object the index is being sought of * @return Return the pointer converted to an index of the type pointed to */ static size_t ReferenceToInt(const void *obj, SLRefType rt) { assert(_sl.action == SLA_SAVE); if (obj == nullptr) return 0; switch (rt) { case REF_VEHICLE_OLD: // Old vehicles we save as new ones case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1; case REF_STATION: return ((const Station*)obj)->index + 1; case REF_TOWN: return ((const Town*)obj)->index + 1; case REF_ORDER: return ((const Order*)obj)->index + 1; case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1; case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1; case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1; case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1; case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1; case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1; case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1; default: NOT_REACHED(); } } /** * Pointers cannot be loaded from a savegame, so this function * gets the index from the savegame and returns the appropriate * pointer from the already loaded base. * Remember that an index of 0 is a nullptr pointer so all indices * are +1 so vehicle 0 is saved as 1. * @param index The index that is being converted to a pointer * @param rt SLRefType type of the object the pointer is sought of * @return Return the index converted to a pointer of any type */ static void *IntToReference(size_t index, SLRefType rt) { static_assert(sizeof(size_t) <= sizeof(void *)); assert(_sl.action == SLA_PTRS); /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE, * and should be loaded like that */ if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) { rt = REF_VEHICLE; } /* No need to look up nullptr pointers, just return immediately */ if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return nullptr; /* Correct index. Old vehicles were saved differently: * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */ if (rt != REF_VEHICLE_OLD) index--; switch (rt) { case REF_ORDERLIST: if (OrderList::IsValidID(index)) return OrderList::Get(index); SlErrorCorrupt("Referencing invalid OrderList"); case REF_ORDER: if (Order::IsValidID(index)) return Order::Get(index); /* in old versions, invalid order was used to mark end of order list */ if (IsSavegameVersionBefore(SLV_5, 2)) return nullptr; SlErrorCorrupt("Referencing invalid Order"); case REF_VEHICLE_OLD: case REF_VEHICLE: if (Vehicle::IsValidID(index)) return Vehicle::Get(index); SlErrorCorrupt("Referencing invalid Vehicle"); case REF_STATION: if (Station::IsValidID(index)) return Station::Get(index); SlErrorCorrupt("Referencing invalid Station"); case REF_TOWN: if (Town::IsValidID(index)) return Town::Get(index); SlErrorCorrupt("Referencing invalid Town"); case REF_ROADSTOPS: if (RoadStop::IsValidID(index)) return RoadStop::Get(index); SlErrorCorrupt("Referencing invalid RoadStop"); case REF_ENGINE_RENEWS: if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index); SlErrorCorrupt("Referencing invalid EngineRenew"); case REF_CARGO_PACKET: if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index); SlErrorCorrupt("Referencing invalid CargoPacket"); case REF_STORAGE: if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index); SlErrorCorrupt("Referencing invalid PersistentStorage"); case REF_LINK_GRAPH: if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index); SlErrorCorrupt("Referencing invalid LinkGraph"); case REF_LINK_GRAPH_JOB: if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index); SlErrorCorrupt("Referencing invalid LinkGraphJob"); default: NOT_REACHED(); } } /** * Handle conversion for references. * @param ptr The object being filled/read. * @param conv VarType type of the current element of the struct. */ void SlSaveLoadRef(void *ptr, VarType conv) { switch (_sl.action) { case SLA_SAVE: SlWriteUint32((uint32_t)ReferenceToInt(*(void **)ptr, (SLRefType)conv)); break; case SLA_LOAD_CHECK: case SLA_LOAD: *(size_t *)ptr = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32(); break; case SLA_PTRS: *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv); break; case SLA_NULL: *(void **)ptr = nullptr; break; default: NOT_REACHED(); } } /** * Template class to help with list-like types. */ template