tron@2186: /* $Id$ */ tron@2186: belugas@6443: /** @file saveload.cpp Darkvater@1881: * All actions handling saving and loading goes on in this file. The general actions Darkvater@1881: * are as follows for saving a game (loading is analogous): Darkvater@1881: *
    Darkvater@1881: *
  1. initialize the writer by creating a temporary memory-buffer for it Darkvater@1881: *
  2. go through all to-be saved elements, each 'chunk' (ChunkHandler) prefixed by a label Darkvater@1881: *
  3. use their description array (SaveLoad) to know what elements to save and in what version Darkvater@1881: * of the game it was active (used when loading) Darkvater@1881: *
  4. write all data byte-by-byte to the temporary buffer so it is endian-safe Darkvater@1881: *
  5. when the buffer is full; flush it to the output (eg save to file) (_sl.buf, _sl.bufp, _sl.bufe) Darkvater@1881: *
  6. repeat this until everything is done, and flush any remaining output to file Darkvater@1881: *
Darkvater@1881: */ truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" truelight@0: #include "station.h" tron@2285: #include "thread.h" truelight@0: #include "town.h" truelight@0: #include "saveload.h" rubidium@5720: #include "network/network.h" tron@2159: #include "variables.h" rubidium@8603: #include "window_func.h" rubidium@8610: #include "strings_func.h" rubidium@8619: #include "gfx_func.h" rubidium@8626: #include "core/alloc_func.hpp" rubidium@8627: #include "functions.h" rubidium@8628: #include "core/endian_func.hpp" rubidium@8640: #include "vehicle_base.h" rubidium@8708: #include "autoreplace_base.h" peter1138@6995: #include truelight@0: rubidium@8760: #include "table/strings.h" rubidium@8760: skidd13@9203: extern const uint16 SAVEGAME_VERSION = 92; belugas@6443: uint16 _sl_version; ///< the major savegame version identifier belugas@6443: byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! tron@2295: tron@2337: typedef void WriterProc(uint len); rubidium@6573: typedef uint ReaderProc(); tron@2337: tron@2295: /** The saveload struct, containing reader-writer functions, bufffer, version, etc. */ tron@2295: static struct { belugas@6443: bool save; ///< are we doing a save or a load atm. True when saving belugas@6443: byte need_length; ///< ??? belugas@6443: byte block_mode; ///< ??? belugas@6443: bool error; ///< did an error occur or not tron@2295: belugas@6443: int obj_len; ///< the length of the current object we are busy with belugas@6443: int array_index, last_array_index; ///< in the case of an array, the current and last positions tron@2295: belugas@6443: uint32 offs_base; ///< the offset in number of bytes since we started writing data (eg uncompressed savegame size) tron@2295: belugas@6443: WriterProc *write_bytes; ///< savegame writer function belugas@6443: ReaderProc *read_bytes; ///< savegame loader function belugas@6443: belugas@6443: const ChunkHandler* const *chs; ///< the chunk of data that is being processed atm (vehicles, signs, etc.) belugas@6443: belugas@6443: /* When saving/loading savegames, they are always saved to a temporary memory-place belugas@6443: * to be flushed to file (save) or to final place (load) when full. */ belugas@6443: byte *bufp, *bufe; ///< bufp(ointer) gives the current position in the buffer bufe(nd) gives the end of the buffer belugas@6443: belugas@6443: /* these 3 may be used by compressor/decompressors. */ belugas@6443: byte *buf; ///< pointer to temporary memory to read/write, initialized by SaveLoadFormat->initread/write belugas@6443: byte *buf_ori; ///< pointer to the original memory location of buf, used to free it afterwards belugas@6443: uint bufsize; ///< the size of the temporary memory *buf belugas@6443: FILE *fh; ///< the file from which is read or written to belugas@6443: rubidium@6573: void (*excpt_uninit)(); ///< the function to execute on any encountered error rubidium@7532: StringID error_str; ///< the translateable error message to show rubidium@7532: char *extra_msg; ///< the error message tron@2295: } _sl; tron@2295: tron@2295: Darkvater@1881: enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2}; truelight@0: rubidium@10326: /** Error handler, calls longjmp to simulate an exception. rubidium@10326: * @todo this was used to have a central place to handle errors, but it is rubidium@10326: * pretty ugly, and seriously interferes with any multithreaded approaches */ rubidium@10326: static void NORETURN SlError(StringID string, const char *extra_msg = NULL) rubidium@10326: { rubidium@10326: _sl.error_str = string; rubidium@10326: free(_sl.extra_msg); rubidium@10326: _sl.extra_msg = (extra_msg == NULL) ? NULL : strdup(extra_msg); rubidium@10326: throw std::exception(); rubidium@10326: } rubidium@10326: Darkvater@1881: /** Darkvater@1881: * Fill the input buffer by reading from the file with the given reader Darkvater@1881: */ rubidium@6573: static void SlReadFill() truelight@0: { truelight@0: uint len = _sl.read_bytes(); rubidium@10326: if (len == 0) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected end of chunk"); truelight@0: truelight@0: _sl.bufp = _sl.buf; truelight@0: _sl.bufe = _sl.buf + len; truelight@0: _sl.offs_base += len; truelight@0: } truelight@0: rubidium@6573: static inline uint32 SlGetOffs() {return _sl.offs_base - (_sl.bufe - _sl.bufp);} truelight@0: Darkvater@3044: /** Return the size in bytes of a certain type of normal/atomic variable belugas@6443: * as it appears in memory. See VarTypes belugas@6443: * @param conv VarType type of variable that is used for calculating the size Darkvater@3044: * @return Return the size of this type in bytes */ Darkvater@3044: static inline byte SlCalcConvMemLen(VarType conv) Darkvater@3044: { Darkvater@3048: static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0}; Darkvater@3625: byte length = GB(conv, 4, 4); Darkvater@3044: assert(length < lengthof(conv_mem_size)); Darkvater@3044: return conv_mem_size[length]; Darkvater@3044: } Darkvater@3044: Darkvater@3044: /** Return the size in bytes of a certain type of normal/atomic variable belugas@6443: * as it appears in a saved game. See VarTypes belugas@6443: * @param conv VarType type of variable that is used for calculating the size Darkvater@3044: * @return Return the size of this type in bytes */ Darkvater@3044: static inline byte SlCalcConvFileLen(VarType conv) Darkvater@3044: { Darkvater@3044: static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2}; Darkvater@3625: byte length = GB(conv, 0, 4); Darkvater@3044: assert(length < lengthof(conv_file_size)); Darkvater@3044: return conv_file_size[length]; Darkvater@3044: } Darkvater@3044: belugas@6443: /** Return the size in bytes of a reference (pointer) */ rubidium@7550: static inline size_t SlCalcRefLen() {return CheckSavegameVersion(69) ? 2 : 4;} Darkvater@3044: Darkvater@1881: /** Flush the output buffer by writing to disk with the given reader. Darkvater@1881: * If the buffer pointer has not yet been set up, set it up now. Usually Darkvater@1881: * only called when the buffer is full, or there is no more data to be processed Darkvater@1881: */ rubidium@6573: static void SlWriteFill() truelight@0: { belugas@6443: /* flush the buffer to disk (the writer) */ truelight@0: if (_sl.bufp != NULL) { truelight@0: uint len = _sl.bufp - _sl.buf; truelight@0: _sl.offs_base += len; truelight@0: if (len) _sl.write_bytes(len); truelight@0: } truelight@193: Darkvater@1881: /* All the data from the buffer has been written away, rewind to the beginning belugas@6443: * to start reading in more data */ truelight@0: _sl.bufp = _sl.buf; truelight@0: _sl.bufe = _sl.buf + _sl.bufsize; truelight@0: } truelight@0: Darkvater@1881: /** Read in a single byte from file. If the temporary buffer is full, Darkvater@1881: * flush it to its final destination Darkvater@1881: * @return return the read byte from file Darkvater@1881: */ rubidium@6573: static inline byte SlReadByteInternal() truelight@0: { truelight@0: if (_sl.bufp == _sl.bufe) SlReadFill(); truelight@0: return *_sl.bufp++; truelight@0: } truelight@0: Darkvater@1881: /** Wrapper for SlReadByteInternal */ rubidium@6573: byte SlReadByte() {return SlReadByteInternal();} Darkvater@1881: Darkvater@1881: /** Write away a single byte from memory. If the temporary buffer is full, Darkvater@1881: * flush it to its destination (file) Darkvater@1881: * @param b the byte that is currently written Darkvater@1881: */ Darkvater@1881: static inline void SlWriteByteInternal(byte b) tron@1514: { Darkvater@1881: if (_sl.bufp == _sl.bufe) SlWriteFill(); Darkvater@1881: *_sl.bufp++ = b; tron@1514: } tron@1514: Darkvater@1881: /** Wrapper for SlWriteByteInternal */ Darkvater@1881: void SlWriteByte(byte b) {SlWriteByteInternal(b);} truelight@0: rubidium@6573: static inline int SlReadUint16() truelight@0: { truelight@0: int x = SlReadByte() << 8; truelight@0: return x | SlReadByte(); truelight@0: } truelight@0: rubidium@6573: static inline uint32 SlReadUint32() truelight@0: { truelight@0: uint32 x = SlReadUint16() << 16; truelight@0: return x | SlReadUint16(); truelight@0: } truelight@0: rubidium@6573: static inline uint64 SlReadUint64() truelight@0: { truelight@0: uint32 x = SlReadUint32(); truelight@0: uint32 y = SlReadUint32(); truelight@0: return (uint64)x << 32 | y; truelight@0: } truelight@0: tron@2144: static inline void SlWriteUint16(uint16 v) truelight@0: { tron@2150: SlWriteByte(GB(v, 8, 8)); tron@2150: SlWriteByte(GB(v, 0, 8)); truelight@0: } truelight@0: Darkvater@1881: static inline void SlWriteUint32(uint32 v) truelight@0: { tron@2150: SlWriteUint16(GB(v, 16, 16)); tron@2150: SlWriteUint16(GB(v, 0, 16)); truelight@0: } truelight@0: Darkvater@1881: static inline void SlWriteUint64(uint64 x) truelight@0: { truelight@0: SlWriteUint32((uint32)(x >> 32)); truelight@0: SlWriteUint32((uint32)x); truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Read in the header descriptor of an object or an array. Darkvater@1881: * If the highest bit is set (7), then the index is bigger than 127 Darkvater@1881: * elements, so use the next byte to read in the real value. Darkvater@1881: * The actual value is then both bytes added with the first shifted Darkvater@1886: * 8 bits to the left, and dropping the highest bit (which only indicated a big index). Darkvater@1881: * x = ((x & 0x7F) << 8) + SlReadByte(); Darkvater@1881: * @return Return the value of the index Darkvater@1881: */ rubidium@6573: static uint SlReadSimpleGamma() truelight@0: { Darkvater@1881: uint i = SlReadByte(); skidd13@8424: if (HasBit(i, 7)) { ludde@2041: i &= ~0x80; skidd13@8424: if (HasBit(i, 6)) { ludde@2041: i &= ~0x40; skidd13@8424: if (HasBit(i, 5)) { ludde@2041: i &= ~0x20; skidd13@8424: if (HasBit(i, 4)) rubidium@7532: SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unsupported gamma"); ludde@2041: i = (i << 8) | SlReadByte(); ludde@2041: } ludde@2041: i = (i << 8) | SlReadByte(); ludde@2041: } ludde@2041: i = (i << 8) | SlReadByte(); Darkvater@1881: } Darkvater@1881: return i; truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Write the header descriptor of an object or an array. Darkvater@1886: * If the element is bigger than 127, use 2 bytes for saving Darkvater@1881: * and use the highest byte of the first written one as a notice ludde@2041: * that the length consists of 2 bytes, etc.. like this: ludde@2041: * 0xxxxxxx ludde@2041: * 10xxxxxx xxxxxxxx ludde@2041: * 110xxxxx xxxxxxxx xxxxxxxx ludde@2041: * 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx Darkvater@1881: * @param i Index being written Darkvater@1881: */ ludde@2041: truelight@0: static void SlWriteSimpleGamma(uint i) truelight@0: { Darkvater@1881: if (i >= (1 << 7)) { ludde@2041: if (i >= (1 << 14)) { ludde@2041: if (i >= (1 << 21)) { ludde@2041: assert(i < (1 << 28)); rubidium@6987: SlWriteByte((byte)0xE0 | (i >> 24)); rubidium@6987: SlWriteByte((byte)(i >> 16)); ludde@2041: } else { rubidium@6987: SlWriteByte((byte)0xC0 | (i >> 16)); ludde@2041: } rubidium@6987: SlWriteByte((byte)(i >> 8)); ludde@2041: } else { rubidium@6987: SlWriteByte((byte)(0x80 | (i >> 8))); ludde@2041: } ludde@2041: } ludde@2041: SlWriteByte(i); truelight@0: } truelight@0: ludde@2041: /** Return how many bytes used to encode a gamma value */ rubidium@7817: static inline uint SlGetGammaLength(uint i) rubidium@7817: { ludde@2041: return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)); ludde@2041: } truelight@0: rubidium@6573: static inline uint SlReadSparseIndex() {return SlReadSimpleGamma();} Darkvater@1881: static inline void SlWriteSparseIndex(uint index) {SlWriteSimpleGamma(index);} truelight@0: rubidium@6573: static inline uint SlReadArrayLength() {return SlReadSimpleGamma();} Darkvater@1881: static inline void SlWriteArrayLength(uint length) {SlWriteSimpleGamma(length);} Darkvater@3509: static inline uint SlGetArrayLength(uint length) {return SlGetGammaLength(length);} truelight@0: truelight@0: void SlSetArrayIndex(uint index) truelight@0: { truelight@0: _sl.need_length = NL_WANTLENGTH; truelight@0: _sl.array_index = index; truelight@0: } truelight@0: rubidium@7596: static uint32 _next_offs; rubidium@7596: Darkvater@1881: /** Darkvater@1881: * Iterate through the elements of an array and read the whole thing Darkvater@1881: * @return The index of the object, or -1 if we have reached the end of current block Darkvater@1881: */ rubidium@6573: int SlIterateArray() truelight@0: { Darkvater@1881: int index; truelight@193: Darkvater@1881: /* After reading in the whole array inside the loop Darkvater@1881: * we must have read in all the data, so we must be at end of current block. */ rubidium@7596: if (_next_offs != 0 && SlGetOffs() != _next_offs) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Invalid chunk size"); truelight@0: Darkvater@1881: while (true) { Darkvater@1881: uint length = SlReadArrayLength(); Darkvater@1881: if (length == 0) { rubidium@7596: _next_offs = 0; truelight@0: return -1; truelight@0: } truelight@0: Darkvater@1881: _sl.obj_len = --length; rubidium@7596: _next_offs = SlGetOffs() + length; truelight@0: Darkvater@1881: switch (_sl.block_mode) { Darkvater@3044: case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break; Darkvater@1881: case CH_ARRAY: index = _sl.array_index++; break; truelight@0: default: Darkvater@5568: DEBUG(sl, 0, "SlIterateArray error"); truelight@0: return -1; // error truelight@0: } truelight@193: Darkvater@1881: if (length != 0) return index; truelight@0: } truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Sets the length of either a RIFF object or the number of items in an array. Darkvater@1881: * This lets us load an object or an array of arbitrary size Darkvater@1881: * @param length The length of the sought object/array Darkvater@1881: */ Darkvater@1881: void SlSetLength(size_t length) truelight@0: { Darkvater@3044: assert(_sl.save); Darkvater@3044: Darkvater@1881: switch (_sl.need_length) { truelight@0: case NL_WANTLENGTH: truelight@0: _sl.need_length = NL_NONE; Darkvater@1881: switch (_sl.block_mode) { truelight@0: case CH_RIFF: belugas@6443: /* Ugly encoding of >16M RIFF chunks belugas@6443: * The lower 24 bits are normal belugas@6443: * The uppermost 4 bits are bits 24:27 */ ludde@2041: assert(length < (1<<28)); ludde@2041: SlWriteUint32((length & 0xFFFFFF) | ((length >> 24) << 28)); Darkvater@1881: break; truelight@0: case CH_ARRAY: Darkvater@1881: assert(_sl.last_array_index <= _sl.array_index); truelight@0: while (++_sl.last_array_index <= _sl.array_index) truelight@0: SlWriteArrayLength(1); truelight@0: SlWriteArrayLength(length + 1); truelight@0: break; truelight@0: case CH_SPARSE_ARRAY: Darkvater@3509: SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index. truelight@0: SlWriteSparseIndex(_sl.array_index); truelight@0: break; truelight@0: default: NOT_REACHED(); Darkvater@1881: } break; truelight@0: case NL_CALCLENGTH: truelight@0: _sl.obj_len += length; truelight@0: break; truelight@0: } truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Save/Load bytes. These do not need to be converted to Little/Big Endian Darkvater@1881: * so directly write them or read them to/from file Darkvater@1881: * @param ptr The source or destination of the object being manipulated Darkvater@1881: * @param length number of bytes this fast CopyBytes lasts Darkvater@1881: */ tron@410: static void SlCopyBytes(void *ptr, size_t length) truelight@0: { truelight@0: byte *p = (byte*)ptr; truelight@0: truelight@0: if (_sl.save) { Darkvater@1881: for (; length != 0; length--) {SlWriteByteInternal(*p++);} truelight@0: } else { Darkvater@1881: for (; length != 0; length--) {*p++ = SlReadByteInternal();} truelight@0: } truelight@0: } truelight@0: Darkvater@3117: /** Read in bytes from the file/data structure but don't do Darkvater@3117: * anything with them, discarding them in effect Darkvater@1881: * @param length The amount of bytes that is being treated this way Darkvater@1881: */ Darkvater@1881: static inline void SlSkipBytes(size_t length) truelight@0: { Darkvater@3117: for (; length != 0; length--) SlReadByte(); truelight@0: } truelight@0: Darkvater@1881: /* Get the length of the current object */ rubidium@6573: uint SlGetFieldLength() {return _sl.obj_len;} truelight@0: Darkvater@3108: /** Return a signed-long version of the value of a setting Darkvater@3108: * @param ptr pointer to the variable Darkvater@3108: * @param conv type of variable, can be a non-clean Darkvater@3108: * type, eg one with other flags because it is parsed Darkvater@3108: * @return returns the value of the pointer-setting */ Darkvater@3108: int64 ReadValue(const void *ptr, VarType conv) Darkvater@3108: { Darkvater@3108: switch (GetVarMemType(conv)) { Darkvater@3109: case SLE_VAR_BL: return (*(bool*)ptr != 0); Darkvater@3108: case SLE_VAR_I8: return *(int8* )ptr; Darkvater@3108: case SLE_VAR_U8: return *(byte* )ptr; Darkvater@3108: case SLE_VAR_I16: return *(int16* )ptr; Darkvater@3108: case SLE_VAR_U16: return *(uint16*)ptr; Darkvater@3108: case SLE_VAR_I32: return *(int32* )ptr; Darkvater@3108: case SLE_VAR_U32: return *(uint32*)ptr; Darkvater@3108: case SLE_VAR_I64: return *(int64* )ptr; Darkvater@3108: case SLE_VAR_U64: return *(uint64*)ptr; Darkvater@3108: case SLE_VAR_NULL:return 0; Darkvater@3108: default: NOT_REACHED(); Darkvater@3108: } Darkvater@3108: Darkvater@3108: /* useless, but avoids compiler warning this way */ Darkvater@3108: return 0; Darkvater@3108: } Darkvater@3108: Darkvater@3108: /** Write the value of a setting Darkvater@3108: * @param ptr pointer to the variable Darkvater@3108: * @param conv type of variable, can be a non-clean type, eg belugas@6916: * with other flags. It is parsed upon read belugas@6916: * @param val the new value being given to the variable */ Darkvater@3108: void WriteValue(void *ptr, VarType conv, int64 val) Darkvater@3108: { Darkvater@3108: switch (GetVarMemType(conv)) { Darkvater@3109: case SLE_VAR_BL: *(bool *)ptr = (val != 0); break; Darkvater@3108: case SLE_VAR_I8: *(int8 *)ptr = val; break; Darkvater@3108: case SLE_VAR_U8: *(byte *)ptr = val; break; Darkvater@3108: case SLE_VAR_I16: *(int16 *)ptr = val; break; Darkvater@3108: case SLE_VAR_U16: *(uint16*)ptr = val; break; Darkvater@3108: case SLE_VAR_I32: *(int32 *)ptr = val; break; Darkvater@3108: case SLE_VAR_U32: *(uint32*)ptr = val; break; Darkvater@3108: case SLE_VAR_I64: *(int64 *)ptr = val; break; Darkvater@3108: case SLE_VAR_U64: *(uint64*)ptr = val; break; peter1138@8754: case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break; Darkvater@3108: case SLE_VAR_NULL: break; Darkvater@3108: default: NOT_REACHED(); Darkvater@3108: } Darkvater@3108: } Darkvater@3108: Darkvater@1881: /** Darkvater@1881: * Handle all conversion and typechecking of variables here. Darkvater@1881: * In the case of saving, read in the actual value from the struct Darkvater@1881: * and then write them to file, endian safely. Loading a value Darkvater@1881: * goes exactly the opposite way Darkvater@1881: * @param ptr The object being filled/read belugas@6443: * @param conv VarType type of the current element of the struct Darkvater@1881: */ Darkvater@1881: static void SlSaveLoadConv(void *ptr, VarType conv) truelight@0: { truelight@0: int64 x = 0; truelight@193: belugas@6443: if (_sl.save) { // SAVE values Darkvater@1881: /* Read a value from the struct. These ARE endian safe. */ Darkvater@3108: x = ReadValue(ptr, conv); truelight@0: Darkvater@3108: /* Write the value to the file and check if its value is in the desired range */ Darkvater@3108: switch (GetVarFileType(conv)) { Darkvater@1881: case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break; tron@2026: case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break; truelight@0: case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break; truelight@0: case SLE_FILE_STRINGID: Darkvater@1881: case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break; Darkvater@3108: case SLE_FILE_I32: Darkvater@3108: case SLE_FILE_U32: SlWriteUint32((uint32)x);break; Darkvater@3108: case SLE_FILE_I64: Darkvater@3108: case SLE_FILE_U64: SlWriteUint64(x);break; Darkvater@1881: default: NOT_REACHED(); truelight@0: } belugas@6443: } else { // LOAD values truelight@193: Darkvater@3108: /* Read a value from the file */ Darkvater@3108: switch (GetVarFileType(conv)) { Darkvater@3108: case SLE_FILE_I8: x = (int8 )SlReadByte(); break; Darkvater@3108: case SLE_FILE_U8: x = (byte )SlReadByte(); break; Darkvater@3108: case SLE_FILE_I16: x = (int16 )SlReadUint16(); break; truelight@0: case SLE_FILE_U16: x = (uint16)SlReadUint16(); break; Darkvater@3108: case SLE_FILE_I32: x = (int32 )SlReadUint32(); break; truelight@0: case SLE_FILE_U32: x = (uint32)SlReadUint32(); break; Darkvater@3108: case SLE_FILE_I64: x = (int64 )SlReadUint64(); break; truelight@0: case SLE_FILE_U64: x = (uint64)SlReadUint64(); break; truelight@0: case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break; Darkvater@1881: default: NOT_REACHED(); truelight@0: } truelight@0: Darkvater@1881: /* Write The value to the struct. These ARE endian safe. */ Darkvater@3108: WriteValue(ptr, conv, x); truelight@0: } truelight@0: } truelight@0: Darkvater@3510: /** Calculate the net length of a string. This is in almost all cases Darkvater@3510: * just strlen(), but if the string is not properly terminated, we'll Darkvater@3510: * resort to the maximum length of the buffer. Darkvater@3510: * @param ptr pointer to the stringbuffer Darkvater@5142: * @param length maximum length of the string (buffer). If -1 we don't care Darkvater@5142: * about a maximum length, but take string length as it is. Darkvater@3510: * @return return the net length of the string */ Darkvater@5140: static inline size_t SlCalcNetStringLen(const char *ptr, size_t length) Darkvater@3510: { peter1138@7675: if (ptr == NULL) return 0; Darkvater@3510: return minu(strlen(ptr), length - 1); Darkvater@3510: } Darkvater@3510: Darkvater@3510: /** Calculate the gross length of the string that it Darkvater@3510: * will occupy in the savegame. This includes the real length, returned Darkvater@3510: * by SlCalcNetStringLen and the length that the index will occupy. Darkvater@3510: * @param ptr pointer to the stringbuffer Darkvater@3510: * @param length maximum length of the string (buffer size, etc.) belugas@6916: * @param conv type of data been used Darkvater@3510: * @return return the gross length of the string */ Darkvater@5140: static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv) Darkvater@3048: { Darkvater@5140: size_t len; Darkvater@5140: const char *str; Darkvater@5140: Darkvater@5142: switch (GetVarMemType(conv)) { Darkvater@5142: default: NOT_REACHED(); Darkvater@5142: case SLE_VAR_STR: Darkvater@5142: case SLE_VAR_STRQ: Darkvater@5142: str = *(const char**)ptr; rubidium@5838: len = SIZE_MAX; Darkvater@5142: break; Darkvater@5142: case SLE_VAR_STRB: Darkvater@5142: case SLE_VAR_STRBQ: Darkvater@5142: str = (const char*)ptr; Darkvater@5142: len = length; Darkvater@5142: break; Darkvater@5142: } Darkvater@5142: Darkvater@5142: len = SlCalcNetStringLen(str, len); Darkvater@3510: return len + SlGetArrayLength(len); // also include the length of the index Darkvater@3048: } Darkvater@3048: Darkvater@3048: /** Darkvater@3048: * Save/Load a string. Darkvater@3048: * @param ptr the string being manipulated belugas@6916: * @param length of the string (full length) Darkvater@5140: * @param conv must be SLE_FILE_STRING */ Darkvater@5140: static void SlString(void *ptr, size_t length, VarType conv) Darkvater@3048: { Darkvater@5140: size_t len; Darkvater@3048: belugas@6443: if (_sl.save) { // SAVE string Darkvater@5140: switch (GetVarMemType(conv)) { Darkvater@5142: default: NOT_REACHED(); Darkvater@5140: case SLE_VAR_STRB: Darkvater@5140: case SLE_VAR_STRBQ: rubidium@5838: len = SlCalcNetStringLen((char*)ptr, length); Darkvater@5140: break; Darkvater@5140: case SLE_VAR_STR: Darkvater@5140: case SLE_VAR_STRQ: Darkvater@5140: ptr = *(char**)ptr; rubidium@5838: len = SlCalcNetStringLen((char*)ptr, SIZE_MAX); Darkvater@5140: break; Darkvater@5140: } Darkvater@5140: Darkvater@3510: SlWriteArrayLength(len); Darkvater@3510: SlCopyBytes(ptr, len); belugas@6443: } else { // LOAD string Darkvater@5140: len = SlReadArrayLength(); Darkvater@3510: Darkvater@5140: switch (GetVarMemType(conv)) { Darkvater@5142: default: NOT_REACHED(); Darkvater@5140: case SLE_VAR_STRB: Darkvater@5140: case SLE_VAR_STRBQ: Darkvater@5140: if (len >= length) { Darkvater@5568: DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating"); Darkvater@5140: SlCopyBytes(ptr, length); Darkvater@5140: SlSkipBytes(len - length); Darkvater@5140: len = length - 1; Darkvater@5140: } else { Darkvater@5140: SlCopyBytes(ptr, len); Darkvater@5140: } Darkvater@5140: break; Darkvater@5140: case SLE_VAR_STR: belugas@6443: case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate Darkvater@5140: free(*(char**)ptr); peter1138@7675: if (len == 0) { peter1138@7675: *(char**)ptr = NULL; peter1138@7675: } else { rubidium@8533: *(char**)ptr = MallocT(len + 1); // terminating '\0' peter1138@7675: ptr = *(char**)ptr; peter1138@7675: SlCopyBytes(ptr, len); peter1138@7675: } Darkvater@5140: break; Darkvater@5140: } Darkvater@5140: Darkvater@5140: ((char*)ptr)[len] = '\0'; // properly terminate the string Darkvater@3510: } Darkvater@3048: } Darkvater@3048: Darkvater@1881: /** Darkvater@1881: * Return the size in bytes of a certain type of atomic array Darkvater@1881: * @param length The length of the array counted in elements belugas@6443: * @param conv VarType type of the variable that is used in calculating the size Darkvater@1881: */ tron@2958: static inline size_t SlCalcArrayLen(uint length, VarType conv) tron@2958: { Darkvater@3044: return SlCalcConvFileLen(conv) * length; tron@2958: } Darkvater@1881: Darkvater@1881: /** Darkvater@1881: * Save/Load an array. Darkvater@1881: * @param array The array being manipulated Darkvater@1881: * @param length The length of the array in elements belugas@6443: * @param conv VarType type of the atomic array (int, byte, uint64, etc.) Darkvater@1881: */ Darkvater@1881: void SlArray(void *array, uint length, VarType conv) truelight@0: { belugas@6443: /* Automatically calculate the length? */ truelight@0: if (_sl.need_length != NL_NONE) { tron@2958: SlSetLength(SlCalcArrayLen(length, conv)); belugas@6443: /* Determine length only? */ Darkvater@3044: if (_sl.need_length == NL_CALCLENGTH) return; truelight@0: } truelight@0: Darkvater@1881: /* NOTICE - handle some buggy stuff, in really old versions everything was saved Darkvater@1881: * as a byte-type. So detect this, and adjust array size accordingly */ tron@2295: if (!_sl.save && _sl_version == 0) { Darkvater@3044: if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID || tron@4077: conv == SLE_INT32 || conv == SLE_UINT32) { Darkvater@3044: length *= SlCalcConvFileLen(conv); truelight@0: conv = SLE_INT8; truelight@0: } truelight@0: } truelight@0: Darkvater@3044: /* If the size of elements is 1 byte both in file and memory, no special Darkvater@3044: * conversion is needed, use specialized copy-copy function to speed up things */ Darkvater@1881: if (conv == SLE_INT8 || conv == SLE_UINT8) { Darkvater@1881: SlCopyBytes(array, length); Darkvater@1881: } else { truelight@0: byte *a = (byte*)array; Darkvater@3044: byte mem_size = SlCalcConvMemLen(conv); Darkvater@3044: Darkvater@1881: for (; length != 0; length --) { truelight@0: SlSaveLoadConv(a, conv); Darkvater@3044: a += mem_size; // get size truelight@0: } truelight@0: } truelight@0: } truelight@0: peter1138@6995: peter1138@6995: static uint ReferenceToInt(const void* obj, SLRefType rt); peter1138@6995: static void* IntToReference(uint index, SLRefType rt); peter1138@6995: peter1138@6995: peter1138@6995: /** peter1138@6995: * Return the size in bytes of a list peter1138@6995: * @param list The std::list to find the size of peter1138@6995: */ peter1138@6995: static inline size_t SlCalcListLen(const void *list) peter1138@6995: { peter1138@6995: std::list *l = (std::list *) list; peter1138@6995: rubidium@7550: int type_size = CheckSavegameVersion(69) ? 2 : 4; rubidium@7550: /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length peter1138@6995: * of the list */ rubidium@7550: return l->size() * type_size + type_size; peter1138@6995: } peter1138@6995: peter1138@6995: peter1138@6995: /** peter1138@6995: * Save/Load a list. peter1138@6995: * @param list The list being manipulated peter1138@6995: * @param conv SLRefType type of the list (Vehicle *, Station *, etc) peter1138@6995: */ peter1138@6995: void SlList(void *list, SLRefType conv) peter1138@6995: { peter1138@6995: /* Automatically calculate the length? */ peter1138@6995: if (_sl.need_length != NL_NONE) { peter1138@6995: SlSetLength(SlCalcListLen(list)); peter1138@6995: /* Determine length only? */ peter1138@6995: if (_sl.need_length == NL_CALCLENGTH) return; peter1138@6995: } peter1138@6995: peter1138@6995: std::list *l = (std::list *) list; peter1138@6995: peter1138@6995: if (_sl.save) { rubidium@7550: SlWriteUint32(l->size()); peter1138@6995: peter1138@6995: std::list::iterator iter; peter1138@6995: for (iter = l->begin(); iter != l->end(); ++iter) { peter1138@6995: void *ptr = *iter; rubidium@7550: SlWriteUint32(ReferenceToInt(ptr, conv)); peter1138@6995: } peter1138@6995: } else { rubidium@7550: uint length = CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(); peter1138@6995: peter1138@6995: /* Load each reference and push to the end of the list */ peter1138@6995: for (uint i = 0; i < length; i++) { rubidium@7550: void *ptr = IntToReference(CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(), conv); peter1138@6995: l->push_back(ptr); peter1138@6995: } peter1138@6995: } peter1138@6995: } peter1138@6995: peter1138@6995: belugas@6443: /** Are we going to save this object or not? */ Darkvater@3044: static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld) Darkvater@3044: { Darkvater@3044: if (_sl_version < sld->version_from || _sl_version > sld->version_to) return false; Darkvater@3117: if (sld->conv & SLF_SAVE_NO) return false; Darkvater@3044: Darkvater@3044: return true; Darkvater@3044: } Darkvater@3044: Darkvater@3117: /** Are we going to load this variable when loading a savegame or not? Darkvater@3117: * @note If the variable is skipped it is skipped in the savegame Darkvater@3117: * bytestream itself as well, so there is no need to skip it somewhere else */ Darkvater@3117: static inline bool SlSkipVariableOnLoad(const SaveLoad *sld) Darkvater@3117: { Darkvater@3117: if ((sld->conv & SLF_NETWORK_NO) && !_sl.save && _networking && !_network_server) { Darkvater@3117: SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length); Darkvater@3117: return true; Darkvater@3117: } Darkvater@3117: Darkvater@3117: return false; Darkvater@3117: } Darkvater@3117: Darkvater@1881: /** Darkvater@1881: * Calculate the size of an object. belugas@6916: * @param object to be measured belugas@6443: * @param sld The SaveLoad description of the object so we know how to manipulate it belugas@6916: * @return size of given objetc Darkvater@1881: */ Darkvater@5142: static size_t SlCalcObjLength(const void *object, const SaveLoad *sld) truelight@0: { truelight@0: size_t length = 0; truelight@0: belugas@6443: /* Need to determine the length and write a length tag. */ Darkvater@1881: for (; sld->cmd != SL_END; sld++) { Darkvater@5142: length += SlCalcObjMemberLength(object, sld); Darkvater@3046: } Darkvater@3046: return length; Darkvater@3046: } Darkvater@3046: Darkvater@5142: size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld) Darkvater@3046: { Darkvater@3046: assert(_sl.save); Darkvater@3046: Darkvater@3046: switch (sld->cmd) { Darkvater@3046: case SL_VAR: Darkvater@3046: case SL_REF: Darkvater@3046: case SL_ARR: Darkvater@3048: case SL_STR: peter1138@6995: case SL_LST: Darkvater@3044: /* CONDITIONAL saveload types depend on the savegame version */ Darkvater@3046: if (!SlIsObjectValidInSavegame(sld)) break; truelight@0: Darkvater@1881: switch (sld->cmd) { Darkvater@3046: case SL_VAR: return SlCalcConvFileLen(sld->conv); Darkvater@3046: case SL_REF: return SlCalcRefLen(); Darkvater@3046: case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv); Darkvater@5142: case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv); peter1138@6995: case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld)); Darkvater@1881: default: NOT_REACHED(); truelight@0: } Darkvater@3046: break; rubidium@7313: case SL_WRITEBYTE: return 1; // a byte is logically of size 1 rubidium@7989: case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END)); Darkvater@3046: default: NOT_REACHED(); truelight@0: } Darkvater@3046: return 0; Darkvater@3046: } Darkvater@3046: tron@4039: Darkvater@3046: bool SlObjectMember(void *ptr, const SaveLoad *sld) Darkvater@3046: { Darkvater@3046: VarType conv = GB(sld->conv, 0, 8); Darkvater@3046: switch (sld->cmd) { Darkvater@3046: case SL_VAR: Darkvater@3046: case SL_REF: Darkvater@3046: case SL_ARR: Darkvater@3048: case SL_STR: peter1138@6995: case SL_LST: Darkvater@3046: /* CONDITIONAL saveload types depend on the savegame version */ Darkvater@3046: if (!SlIsObjectValidInSavegame(sld)) return false; Darkvater@3117: if (SlSkipVariableOnLoad(sld)) return false; Darkvater@3046: Darkvater@3046: switch (sld->cmd) { Darkvater@3046: case SL_VAR: SlSaveLoadConv(ptr, conv); break; belugas@6443: case SL_REF: // Reference variable, translate rubidium@7550: if (_sl.save) { rubidium@7550: SlWriteUint32(ReferenceToInt(*(void**)ptr, (SLRefType)conv)); tron@4039: } else { rubidium@7550: *(void**)ptr = IntToReference(CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(), (SLRefType)conv); tron@4039: } Darkvater@3046: break; Darkvater@3046: case SL_ARR: SlArray(ptr, sld->length, conv); break; Darkvater@3048: case SL_STR: SlString(ptr, sld->length, conv); break; peter1138@6995: case SL_LST: SlList(ptr, (SLRefType)conv); break; Darkvater@3046: default: NOT_REACHED(); Darkvater@3046: } Darkvater@3046: break; Darkvater@3046: rubidium@7313: /* SL_WRITEBYTE translates a value of a variable to another one upon rubidium@7313: * saving or loading. rubidium@7313: * XXX - variable renaming abuse rubidium@7313: * game_value: the value of the variable ingame is abused by sld->version_from rubidium@7313: * file_value: the value of the variable in the savegame is abused by sld->version_to */ rubidium@7313: case SL_WRITEBYTE: rubidium@7313: if (_sl.save) { rubidium@7313: SlWriteByte(sld->version_to); rubidium@7313: } else { rubidium@7313: *(byte*)ptr = sld->version_from; rubidium@7313: } rubidium@7313: break; rubidium@7313: rubidium@7989: /* SL_VEH_INCLUDE loads common code for vehicles */ rubidium@7989: case SL_VEH_INCLUDE: rubidium@7989: SlObject(ptr, GetVehicleDescription(VEH_END)); Darkvater@3046: break; Darkvater@3046: default: NOT_REACHED(); Darkvater@3046: } Darkvater@3046: return true; truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Main SaveLoad function. Darkvater@1881: * @param object The object that is being saved or loaded belugas@6443: * @param sld The SaveLoad description of the object so we know how to manipulate it Darkvater@1881: */ Darkvater@1881: void SlObject(void *object, const SaveLoad *sld) truelight@0: { belugas@6443: /* Automatically calculate the length? */ truelight@0: if (_sl.need_length != NL_NONE) { Darkvater@5142: SlSetLength(SlCalcObjLength(object, sld)); Darkvater@3044: if (_sl.need_length == NL_CALCLENGTH) return; truelight@0: } truelight@0: Darkvater@1881: for (; sld->cmd != SL_END; sld++) { rubidium@7506: void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld); Darkvater@3046: SlObjectMember(ptr, sld); truelight@0: } truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Save or Load (a list of) global variables belugas@6916: * @param sldg The global variable that is being loaded or saved Darkvater@1881: */ Darkvater@3046: void SlGlobList(const SaveLoadGlobVarList *sldg) truelight@0: { rubidium@7506: SlObject(NULL, (const SaveLoad*)sldg); truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Do something of which I have no idea what it is :P Darkvater@1881: * @param proc The callback procedure that is called Darkvater@1881: * @param arg The variable that will be used for the callback procedure Darkvater@1881: */ truelight@0: void SlAutolength(AutolengthProc *proc, void *arg) truelight@0: { truelight@0: uint32 offs; truelight@0: truelight@0: assert(_sl.save); truelight@0: belugas@6443: /* Tell it to calculate the length */ truelight@0: _sl.need_length = NL_CALCLENGTH; truelight@0: _sl.obj_len = 0; truelight@0: proc(arg); truelight@0: belugas@6443: /* Setup length */ truelight@0: _sl.need_length = NL_WANTLENGTH; truelight@0: SlSetLength(_sl.obj_len); truelight@0: truelight@0: offs = SlGetOffs() + _sl.obj_len; truelight@0: belugas@6443: /* And write the stuff */ truelight@0: proc(arg); truelight@0: rubidium@7532: if (offs != SlGetOffs()) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Invalid chunk size"); truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Load a chunk of data (eg vehicles, stations, etc.) Darkvater@1881: * @param ch The chunkhandler that will be used for the operation Darkvater@1881: */ truelight@0: static void SlLoadChunk(const ChunkHandler *ch) truelight@0: { truelight@0: byte m = SlReadByte(); truelight@0: size_t len; truelight@0: uint32 endoffs; truelight@0: truelight@0: _sl.block_mode = m; truelight@0: _sl.obj_len = 0; truelight@193: Darkvater@1881: switch (m) { truelight@0: case CH_ARRAY: truelight@0: _sl.array_index = 0; truelight@0: ch->load_proc(); truelight@0: break; truelight@0: case CH_SPARSE_ARRAY: truelight@0: ch->load_proc(); truelight@0: break; ludde@2041: default: ludde@2041: if ((m & 0xF) == CH_RIFF) { belugas@6443: /* Read length */ ludde@2041: len = (SlReadByte() << 16) | ((m >> 4) << 24); ludde@2041: len += SlReadUint16(); ludde@2041: _sl.obj_len = len; ludde@2041: endoffs = SlGetOffs() + len; ludde@2041: ch->load_proc(); rubidium@7532: if (SlGetOffs() != endoffs) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Invalid chunk size"); ludde@2041: } else { rubidium@7532: SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Invalid chunk type"); ludde@2041: } truelight@0: break; truelight@0: } truelight@0: } truelight@0: Darkvater@1881: /* Stub Chunk handlers to only calculate length and do nothing else */ truelight@0: static ChunkSaveLoadProc *_tmp_proc_1; Darkvater@1881: static inline void SlStubSaveProc2(void *arg) {_tmp_proc_1();} rubidium@6573: static void SlStubSaveProc() {SlAutolength(SlStubSaveProc2, NULL);} truelight@0: Darkvater@1881: /** Save a chunk of data (eg. vehicles, stations, etc.). Each chunk is Darkvater@1881: * prefixed by an ID identifying it, followed by data, and terminator where appropiate Darkvater@1881: * @param ch The chunkhandler that will be used for the operation Darkvater@1881: */ truelight@0: static void SlSaveChunk(const ChunkHandler *ch) truelight@0: { Darkvater@1881: ChunkSaveLoadProc *proc = ch->save_proc; truelight@0: peter1138@8754: /* Don't save any chunk information if there is no save handler. */ peter1138@8754: if (proc == NULL) return; peter1138@8754: truelight@0: SlWriteUint32(ch->id); Darkvater@5568: DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id); truelight@0: truelight@0: if (ch->flags & CH_AUTO_LENGTH) { belugas@6443: /* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */ truelight@0: _tmp_proc_1 = proc; truelight@0: proc = SlStubSaveProc; truelight@0: } truelight@193: truelight@0: _sl.block_mode = ch->flags & CH_TYPE_MASK; Darkvater@1881: switch (ch->flags & CH_TYPE_MASK) { truelight@0: case CH_RIFF: truelight@0: _sl.need_length = NL_WANTLENGTH; truelight@0: proc(); truelight@0: break; truelight@0: case CH_ARRAY: truelight@0: _sl.last_array_index = 0; truelight@0: SlWriteByte(CH_ARRAY); truelight@0: proc(); truelight@0: SlWriteArrayLength(0); // Terminate arrays truelight@0: break; truelight@0: case CH_SPARSE_ARRAY: truelight@0: SlWriteByte(CH_SPARSE_ARRAY); truelight@0: proc(); truelight@0: SlWriteArrayLength(0); // Terminate arrays truelight@0: break; Darkvater@1881: default: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: Darkvater@1881: /** Save all chunks */ rubidium@6573: static void SlSaveChunks() truelight@0: { truelight@0: const ChunkHandler *ch; Darkvater@1881: const ChunkHandler* const *chsc; truelight@0: uint p; truelight@0: Darkvater@1881: for (p = 0; p != CH_NUM_PRI_LEVELS; p++) { Darkvater@1881: for (chsc = _sl.chs; (ch = *chsc++) != NULL;) { Darkvater@1881: while (true) { truelight@0: if (((ch->flags >> CH_PRI_SHL) & (CH_NUM_PRI_LEVELS - 1)) == p) truelight@193: SlSaveChunk(ch); truelight@0: if (ch->flags & CH_LAST) truelight@0: break; truelight@0: ch++; truelight@0: } truelight@0: } truelight@0: } truelight@0: belugas@6443: /* Terminator */ truelight@0: SlWriteUint32(0); truelight@0: } truelight@0: Darkvater@1881: /** Find the ChunkHandler that will be used for processing the found Darkvater@1881: * chunk in the savegame or in memory Darkvater@1881: * @param id the chunk in question Darkvater@1881: * @return returns the appropiate chunkhandler Darkvater@1881: */ truelight@0: static const ChunkHandler *SlFindChunkHandler(uint32 id) truelight@0: { truelight@0: const ChunkHandler *ch; Darkvater@1881: const ChunkHandler *const *chsc; rubidium@6987: for (chsc = _sl.chs; (ch = *chsc++) != NULL;) { tron@2952: for (;;) { tron@2952: if (ch->id == id) return ch; tron@2952: if (ch->flags & CH_LAST) break; truelight@0: ch++; truelight@0: } truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: Darkvater@1881: /** Load all chunks */ rubidium@6573: static void SlLoadChunks() truelight@0: { truelight@0: uint32 id; truelight@0: const ChunkHandler *ch; truelight@0: Darkvater@1881: for (id = SlReadUint32(); id != 0; id = SlReadUint32()) { Darkvater@5568: DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id); Darkvater@1881: truelight@0: ch = SlFindChunkHandler(id); rubidium@7532: if (ch == NULL) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unknown chunk type"); truelight@0: SlLoadChunk(ch); truelight@0: } truelight@0: } truelight@0: belugas@6443: /******************************************* belugas@6443: ********** START OF LZO CODE ************** belugas@6443: *******************************************/ truelight@0: #define LZO_SIZE 8192 truelight@0: truelight@781: #include "minilzo.h" truelight@0: rubidium@6573: static uint ReadLZO() truelight@0: { truelight@0: byte out[LZO_SIZE + LZO_SIZE / 64 + 16 + 3 + 8]; truelight@0: uint32 tmp[2]; truelight@0: uint32 size; truelight@0: uint len; truelight@0: belugas@6443: /* Read header*/ rubidium@7532: if (fread(tmp, sizeof(tmp), 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed"); truelight@0: belugas@6443: /* Check if size is bad */ truelight@0: ((uint32*)out)[0] = size = tmp[1]; truelight@193: tron@2295: if (_sl_version != 0) { truelight@0: tmp[0] = TO_BE32(tmp[0]); truelight@0: size = TO_BE32(size); truelight@0: } truelight@0: rubidium@7532: if (size >= sizeof(out)) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Inconsistent size"); truelight@0: belugas@6443: /* Read block */ rubidium@7532: if (fread(out + sizeof(uint32), size, 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); truelight@0: belugas@6443: /* Verify checksum */ rubidium@7532: if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Bad checksum"); truelight@193: belugas@6443: /* Decompress */ truelight@0: lzo1x_decompress(out + sizeof(uint32)*1, size, _sl.buf, &len, NULL); truelight@0: return len; truelight@0: } truelight@0: belugas@6443: /* p contains the pointer to the buffer, len contains the pointer to the length. belugas@6443: * len bytes will be written, p and l will be updated to reflect the next buffer. */ truelight@0: static void WriteLZO(uint size) truelight@0: { truelight@0: byte out[LZO_SIZE + LZO_SIZE / 64 + 16 + 3 + 8]; truelight@0: byte wrkmem[sizeof(byte*)*4096]; truelight@0: uint outlen; truelight@193: truelight@0: lzo1x_1_compress(_sl.buf, size, out + sizeof(uint32)*2, &outlen, wrkmem); truelight@0: ((uint32*)out)[1] = TO_BE32(outlen); truelight@0: ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32))); rubidium@7532: if (fwrite(out, outlen + sizeof(uint32)*2, 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE); truelight@0: } truelight@0: rubidium@6573: static bool InitLZO() tron@1093: { truelight@0: _sl.bufsize = LZO_SIZE; rubidium@8533: _sl.buf = _sl.buf_ori = MallocT(LZO_SIZE); truelight@0: return true; truelight@0: } truelight@0: rubidium@6573: static void UninitLZO() tron@1093: { Darkvater@2414: free(_sl.buf_ori); truelight@0: } truelight@0: belugas@6443: /********************************************* belugas@6443: ******** START OF NOCOMP CODE (uncompressed)* belugas@6443: *********************************************/ rubidium@6573: static uint ReadNoComp() truelight@0: { truelight@0: return fread(_sl.buf, 1, LZO_SIZE, _sl.fh); truelight@0: } truelight@0: truelight@0: static void WriteNoComp(uint size) truelight@0: { truelight@0: fwrite(_sl.buf, 1, size, _sl.fh); truelight@0: } truelight@0: rubidium@6573: static bool InitNoComp() truelight@0: { truelight@0: _sl.bufsize = LZO_SIZE; rubidium@8533: _sl.buf = _sl.buf_ori = MallocT(LZO_SIZE); truelight@0: return true; truelight@0: } truelight@0: rubidium@6573: static void UninitNoComp() truelight@0: { Darkvater@2414: free(_sl.buf_ori); truelight@0: } truelight@0: belugas@6443: /******************************************** belugas@6443: ********** START OF MEMORY CODE (in ram)**** belugas@6443: ********************************************/ Darkvater@1885: Darkvater@1913: #include "table/sprites.h" Darkvater@1913: #include "gui.h" Darkvater@1913: rubidium@6574: struct ThreadedSave { Darkvater@1913: uint count; peter1138@5970: byte ff_state; Darkvater@1913: bool saveinprogress; Darkvater@1914: CursorID cursor; rubidium@6574: }; Darkvater@1913: Darkvater@1885: /* A maximum size of of 128K * 500 = 64.000KB savegames */ matthijs@5216: STATIC_OLD_POOL(Savegame, byte, 17, 500, NULL, NULL) Darkvater@1913: static ThreadedSave _ts; Darkvater@1885: rubidium@6573: static bool InitMem() Darkvater@1885: { Darkvater@1913: _ts.count = 0; Darkvater@1913: rubidium@7897: _Savegame_pool.CleanPool(); rubidium@7897: _Savegame_pool.AddBlockToPool(); Darkvater@1885: Darkvater@1885: /* A block from the pool is a contigious area of memory, so it is safe to write to it sequentially */ tron@4985: _sl.bufsize = GetSavegamePoolSize(); tron@4985: _sl.buf = GetSavegame(_ts.count); Darkvater@1885: return true; Darkvater@1885: } Darkvater@1885: rubidium@6573: static void UnInitMem() Darkvater@1885: { rubidium@7897: _Savegame_pool.CleanPool(); Darkvater@1885: } Darkvater@1885: Darkvater@1885: static void WriteMem(uint size) Darkvater@1885: { Darkvater@1913: _ts.count += size; Darkvater@1885: /* Allocate new block and new buffer-pointer */ rubidium@7897: _Savegame_pool.AddBlockIfNeeded(_ts.count); tron@4985: _sl.buf = GetSavegame(_ts.count); Darkvater@1885: } Darkvater@1885: belugas@6443: /******************************************** belugas@6443: ********** START OF ZLIB CODE ************** belugas@6443: ********************************************/ truelight@0: truelight@0: #if defined(WITH_ZLIB) Darkvater@2694: #include ludde@2138: truelight@0: static z_stream _z; truelight@0: rubidium@6573: static bool InitReadZlib() truelight@0: { truelight@0: memset(&_z, 0, sizeof(_z)); truelight@0: if (inflateInit(&_z) != Z_OK) return false; truelight@193: truelight@0: _sl.bufsize = 4096; rubidium@8533: _sl.buf = _sl.buf_ori = MallocT(4096 + 4096); // also contains fread buffer truelight@0: return true; truelight@0: } truelight@0: rubidium@6573: static uint ReadZlib() truelight@0: { truelight@0: int r; truelight@0: truelight@0: _z.next_out = _sl.buf; truelight@0: _z.avail_out = 4096; truelight@0: truelight@0: do { belugas@6443: /* read more bytes from the file?*/ truelight@0: if (_z.avail_in == 0) { truelight@0: _z.avail_in = fread(_z.next_in = _sl.buf + 4096, 1, 4096, _sl.fh); truelight@0: } truelight@0: belugas@6443: /* inflate the data */ truelight@0: r = inflate(&_z, 0); truelight@0: if (r == Z_STREAM_END) truelight@0: break; truelight@0: rubidium@7532: if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed"); truelight@0: } while (_z.avail_out); truelight@0: truelight@0: return 4096 - _z.avail_out; truelight@0: } truelight@0: rubidium@6573: static void UninitReadZlib() truelight@0: { truelight@0: inflateEnd(&_z); Darkvater@2414: free(_sl.buf_ori); truelight@0: } truelight@0: rubidium@6573: static bool InitWriteZlib() truelight@0: { truelight@0: memset(&_z, 0, sizeof(_z)); truelight@0: if (deflateInit(&_z, 6) != Z_OK) return false; truelight@0: truelight@0: _sl.bufsize = 4096; rubidium@8533: _sl.buf = _sl.buf_ori = MallocT(4096); // also contains fread buffer truelight@0: return true; truelight@0: } truelight@0: truelight@0: static void WriteZlibLoop(z_streamp z, byte *p, uint len, int mode) truelight@0: { hackykid@1884: byte buf[1024]; // output buffer truelight@0: int r; truelight@0: uint n; truelight@0: z->next_in = p; truelight@0: z->avail_in = len; truelight@0: do { truelight@0: z->next_out = buf; truelight@0: z->avail_out = sizeof(buf); tron@2026: r = deflate(z, mode); belugas@6443: /* bytes were emitted? */ truelight@0: if ((n=sizeof(buf) - z->avail_out) != 0) { rubidium@7532: if (fwrite(buf, n, 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE); truelight@0: } truelight@0: if (r == Z_STREAM_END) truelight@0: break; rubidium@7532: if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code"); truelight@0: } while (z->avail_in || !z->avail_out); truelight@0: } truelight@0: truelight@0: static void WriteZlib(uint len) truelight@0: { truelight@0: WriteZlibLoop(&_z, _sl.buf, len, 0); truelight@0: } truelight@0: rubidium@6573: static void UninitWriteZlib() truelight@0: { belugas@6443: /* flush any pending output. */ truelight@0: if (_sl.fh) WriteZlibLoop(&_z, NULL, 0, Z_FINISH); truelight@0: deflateEnd(&_z); Darkvater@2414: free(_sl.buf_ori); truelight@0: } truelight@0: Darkvater@1881: #endif /* WITH_ZLIB */ truelight@0: belugas@6443: /******************************************* belugas@6443: ************* END OF CODE ***************** belugas@6443: *******************************************/ truelight@0: belugas@6443: /* these define the chunks */ truelight@0: extern const ChunkHandler _misc_chunk_handlers[]; Darkvater@3112: extern const ChunkHandler _setting_chunk_handlers[]; truelight@0: extern const ChunkHandler _player_chunk_handlers[]; peter1138@2848: extern const ChunkHandler _engine_chunk_handlers[]; truelight@0: extern const ChunkHandler _veh_chunk_handlers[]; truelight@1542: extern const ChunkHandler _waypoint_chunk_handlers[]; truelight@1313: extern const ChunkHandler _depot_chunk_handlers[]; truelight@1024: extern const ChunkHandler _order_chunk_handlers[]; truelight@0: extern const ChunkHandler _town_chunk_handlers[]; truelight@0: extern const ChunkHandler _sign_chunk_handlers[]; truelight@0: extern const ChunkHandler _station_chunk_handlers[]; truelight@0: extern const ChunkHandler _industry_chunk_handlers[]; truelight@0: extern const ChunkHandler _economy_chunk_handlers[]; truelight@0: extern const ChunkHandler _animated_tile_chunk_handlers[]; peter1138@5228: extern const ChunkHandler _newgrf_chunk_handlers[]; rubidium@7139: extern const ChunkHandler _group_chunk_handlers[]; rubidium@7506: extern const ChunkHandler _cargopacket_chunk_handlers[]; truelight@0: truelight@0: static const ChunkHandler * const _chunk_handlers[] = { truelight@0: _misc_chunk_handlers, Darkvater@3112: _setting_chunk_handlers, truelight@0: _veh_chunk_handlers, truelight@1542: _waypoint_chunk_handlers, truelight@1313: _depot_chunk_handlers, truelight@1024: _order_chunk_handlers, truelight@0: _industry_chunk_handlers, truelight@0: _economy_chunk_handlers, truelight@0: _engine_chunk_handlers, truelight@0: _town_chunk_handlers, truelight@0: _sign_chunk_handlers, truelight@0: _station_chunk_handlers, truelight@0: _player_chunk_handlers, truelight@0: _animated_tile_chunk_handlers, peter1138@5228: _newgrf_chunk_handlers, rubidium@7139: _group_chunk_handlers, rubidium@7506: _cargopacket_chunk_handlers, truelight@0: NULL, truelight@0: }; truelight@0: Darkvater@1881: /** Darkvater@1881: * Pointers cannot be saved to a savegame, so this functions gets Darkvater@1881: * the index of the item, and if not available, it hussles with Darkvater@1881: * pointers (looks really bad :() Darkvater@1881: * Remember that a NULL item has value 0, and all Darkvater@1881: * indeces have +1, so vehicle 0 is saved as index 1. Darkvater@1881: * @param obj The object that we want to get the index of belugas@6443: * @param rt SLRefType type of the object the index is being sought of Darkvater@1881: * @return Return the pointer converted to an index of the type pointed to Darkvater@1881: */ Darkvater@1881: static uint ReferenceToInt(const void *obj, SLRefType rt) truelight@0: { Darkvater@1881: if (obj == NULL) return 0; truelight@938: Darkvater@1881: switch (rt) { truelight@938: case REF_VEHICLE_OLD: // Old vehicles we save as new onces tron@2548: case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1; tron@2548: case REF_STATION: return ((const Station*)obj)->index + 1; tron@2548: case REF_TOWN: return ((const Town*)obj)->index + 1; tron@2548: case REF_ORDER: return ((const Order*)obj)->index + 1; tron@2548: case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1; peter1138@2848: case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1; rubidium@7506: case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1; Darkvater@1881: default: NOT_REACHED(); truelight@938: } truelight@938: Darkvater@1881: return 0; // avoid compiler warning truelight@0: } truelight@0: Darkvater@1881: /** Darkvater@1881: * Pointers cannot be loaded from a savegame, so this function Darkvater@1881: * gets the index from the savegame and returns the appropiate Darkvater@1881: * pointer from the already loaded base. Darkvater@1881: * Remember that an index of 0 is a NULL pointer so all indeces Darkvater@1881: * are +1 so vehicle 0 is saved as 1. Darkvater@1881: * @param index The index that is being converted to a pointer belugas@6443: * @param rt SLRefType type of the object the pointer is sought of Darkvater@1881: * @return Return the index converted to a pointer of any type Darkvater@1881: */ Darkvater@1881: static void *IntToReference(uint index, SLRefType rt) truelight@0: { Darkvater@1881: /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE, Darkvater@1881: * and should be loaded like that */ peter1138@8988: if (rt == REF_VEHICLE_OLD && !CheckSavegameVersionOldStyle(4, 4)) { Darkvater@1881: rt = REF_VEHICLE; peter1138@8988: } truelight@938: Darkvater@1881: /* No need to look up NULL pointers, just return immediately */ peter1138@8988: if (rt != REF_VEHICLE_OLD && index == 0) { truelight@938: return NULL; peter1138@8988: } truelight@938: Darkvater@1881: index--; // correct for the NULL index Darkvater@1881: Darkvater@1881: switch (rt) { peter1138@8988: case REF_ORDER: peter1138@8988: if (_Order_pool.AddBlockIfNeeded(index)) return GetOrder(index); peter1138@8988: error("Orders: failed loading savegame: too many orders"); truelight@938: peter1138@8988: case REF_VEHICLE: peter1138@8988: if (_Vehicle_pool.AddBlockIfNeeded(index)) return GetVehicle(index); peter1138@8988: error("Vehicles: failed loading savegame: too many vehicles"); peter1138@8988: peter1138@8988: case REF_STATION: peter1138@8988: if (_Station_pool.AddBlockIfNeeded(index)) return GetStation(index); peter1138@8988: error("Stations: failed loading savegame: too many stations"); peter1138@8988: peter1138@8988: case REF_TOWN: peter1138@8988: if (_Town_pool.AddBlockIfNeeded(index)) return GetTown(index); peter1138@8988: error("Towns: failed loading savegame: too many towns"); peter1138@8988: peter1138@8988: case REF_ROADSTOPS: peter1138@8988: if (_RoadStop_pool.AddBlockIfNeeded(index)) return GetRoadStop(index); peter1138@8988: error("RoadStops: failed loading savegame: too many RoadStops"); peter1138@8988: peter1138@8988: case REF_ENGINE_RENEWS: peter1138@8988: if (_EngineRenew_pool.AddBlockIfNeeded(index)) return GetEngineRenew(index); peter1138@8988: error("EngineRenews: failed loading savegame: too many EngineRenews"); peter1138@8988: peter1138@8988: case REF_CARGO_PACKET: peter1138@8988: if (_CargoPacket_pool.AddBlockIfNeeded(index)) return GetCargoPacket(index); peter1138@8988: error("CargoPackets: failed loading savegame: too many Cargo packets"); peter1138@8988: peter1138@8988: case REF_VEHICLE_OLD: Darkvater@1881: /* Old vehicles were saved differently: Darkvater@1881: * invalid vehicle was 0xFFFF, Darkvater@1881: * and the index was not - 1.. correct for this */ Darkvater@1881: index++; peter1138@8988: if (index == INVALID_VEHICLE) return NULL; truelight@1279: belugas@8989: if (_Vehicle_pool.AddBlockIfNeeded(index)) return GetVehicle(index); peter1138@8988: error("Vehicles: failed loading savegame: too many vehicles"); peter1138@8988: Darkvater@1881: default: NOT_REACHED(); truelight@938: } truelight@938: truelight@938: return NULL; truelight@0: } truelight@0: Darkvater@1881: /** The format for a reader/writer type of a savegame */ rubidium@6574: struct SaveLoadFormat { belugas@6443: const char *name; ///< name of the compressor/decompressor (debug-only) belugas@6443: uint32 tag; ///< the 4-letter tag by which it is identified in the savegame truelight@0: rubidium@6573: bool (*init_read)(); ///< function executed upon initalization of the loader belugas@6443: ReaderProc *reader; ///< function that loads the data from the file rubidium@6573: void (*uninit_read)(); ///< function executed when reading is finished truelight@0: rubidium@6573: bool (*init_write)(); ///< function executed upon intialization of the saver belugas@6443: WriterProc *writer; ///< function that saves the data to the file rubidium@6573: void (*uninit_write)(); ///< function executed when writing is done rubidium@6574: }; truelight@0: truelight@0: static const SaveLoadFormat _saveload_formats[] = { Darkvater@1885: {"memory", 0, NULL, NULL, NULL, InitMem, WriteMem, UnInitMem}, Darkvater@1885: {"lzo", TO_BE32X('OTTD'), InitLZO, ReadLZO, UninitLZO, InitLZO, WriteLZO, UninitLZO}, Darkvater@1885: {"none", TO_BE32X('OTTN'), InitNoComp, ReadNoComp, UninitNoComp, InitNoComp, WriteNoComp, UninitNoComp}, truelight@0: #if defined(WITH_ZLIB) Darkvater@1885: {"zlib", TO_BE32X('OTTZ'), InitReadZlib, ReadZlib, UninitReadZlib, InitWriteZlib, WriteZlib, UninitWriteZlib}, truelight@0: #else Darkvater@1885: {"zlib", TO_BE32X('OTTZ'), NULL, NULL, NULL, NULL, NULL, NULL}, truelight@0: #endif truelight@0: }; truelight@0: Darkvater@1881: /** Darkvater@1881: * Return the savegameformat of the game. Whether it was create with ZLIB compression Darkvater@1881: * uncompressed, or another type Darkvater@1885: * @param s Name of the savegame format. If NULL it picks the first available one belugas@6443: * @return Pointer to SaveLoadFormat struct giving all characteristics of this type of savegame Darkvater@1881: */ truelight@0: static const SaveLoadFormat *GetSavegameFormat(const char *s) truelight@0: { Darkvater@1881: const SaveLoadFormat *def = endof(_saveload_formats) - 1; truelight@0: belugas@6443: /* find default savegame format, the highest one with which files can be written */ truelight@0: while (!def->init_write) def--; truelight@0: Darkvater@1885: if (s != NULL && s[0] != '\0') { Darkvater@1885: const SaveLoadFormat *slf; Darkvater@1885: for (slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) { Darkvater@1885: if (slf->init_write != NULL && strcmp(s, slf->name) == 0) Darkvater@1885: return slf; Darkvater@1885: } truelight@0: truelight@0: ShowInfoF("Savegame format '%s' is not available. Reverting to '%s'.", s, def->name); truelight@0: } truelight@0: return def; truelight@0: } truelight@0: belugas@6443: /* actual loader/saver function */ truelight@2828: void InitializeGame(int mode, uint size_x, uint size_y); rubidium@6573: extern bool AfterLoadGame(); rubidium@6573: extern void BeforeSaveGame(); truelight@0: extern bool LoadOldSaveGame(const char *file); truelight@0: Darkvater@1881: /** Small helper function to close the to be loaded savegame an signal error */ rubidium@6573: static inline SaveOrLoadResult AbortSaveLoad() Darkvater@1881: { Darkvater@1881: if (_sl.fh != NULL) fclose(_sl.fh); Darkvater@1881: Darkvater@1881: _sl.fh = NULL; Darkvater@1881: return SL_ERROR; Darkvater@1881: } Darkvater@1881: Darkvater@1885: /** Update the gui accordingly when starting saving Darkvater@1913: * and set locks on saveload. Also turn off fast-forward cause with that Darkvater@1913: * saving takes Aaaaages */ rubidium@6573: void SaveFileStart() Darkvater@1885: { peter1138@5970: _ts.ff_state = _fast_forward; peter1138@5970: _fast_forward = 0; peter1138@5919: if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE); Darkvater@1913: Darkvater@1885: SendWindowMessage(WC_STATUS_BAR, 0, true, 0, 0); Darkvater@1913: _ts.saveinprogress = true; Darkvater@1885: } Darkvater@1885: Darkvater@1885: /** Update the gui accordingly when saving is done and release locks Darkvater@1885: * on saveload */ rubidium@6573: void SaveFileDone() Darkvater@1885: { Darkvater@1913: _fast_forward = _ts.ff_state; peter1138@5919: if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE); Darkvater@1913: Darkvater@1885: SendWindowMessage(WC_STATUS_BAR, 0, false, 0, 0); Darkvater@1913: _ts.saveinprogress = false; Darkvater@1885: } Darkvater@1885: rubidium@7532: /** Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friends) */ rubidium@7532: void SetSaveLoadError(StringID str) rubidium@7532: { rubidium@7532: _sl.error_str = str; rubidium@7532: } rubidium@7532: rubidium@7532: /** Get the string representation of the error message */ rubidium@7532: const char *GetSaveLoadErrorString() rubidium@7532: { rubidium@7532: SetDParam(0, _sl.error_str); rubidium@7532: SetDParamStr(1, _sl.extra_msg); rubidium@7532: rubidium@7532: static char err_str[512]; rubidium@7532: GetString(err_str, _sl.save ? STR_4007_GAME_SAVE_FAILED : STR_4009_GAME_LOAD_FAILED, lastof(err_str)); rubidium@7532: return err_str; rubidium@7532: } rubidium@7532: Darkvater@2380: /** Show a gui message when saving has failed */ rubidium@6573: void SaveFileError() Darkvater@2380: { rubidium@7532: SetDParamStr(0, GetSaveLoadErrorString()); rubidium@7532: ShowErrorMessage(STR_012D, STR_NULL, 0, 0); Darkvater@2380: SaveFileDone(); Darkvater@2380: } Darkvater@2380: truelight@4298: static OTTDThread* save_thread; Darkvater@2382: tron@4978: /** We have written the whole game into memory, _Savegame_pool, now find Darkvater@1885: * and appropiate compressor and start writing to file. Darkvater@1885: */ rubidium@5956: static SaveOrLoadResult SaveFileToDisk(bool threaded) Darkvater@1885: { truelight@2927: const SaveLoadFormat *fmt; Darkvater@1885: uint32 hdr[2]; Darkvater@1885: rubidium@7532: _sl.excpt_uninit = NULL; glx@7945: try { glx@7945: fmt = GetSavegameFormat(_savegame_format); glx@7945: glx@7945: /* We have written our stuff to memory, now write it to file! */ glx@7945: hdr[0] = fmt->tag; glx@7945: hdr[1] = TO_BE32(SAVEGAME_VERSION << 16); glx@7945: if (fwrite(hdr, sizeof(hdr), 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE); glx@7945: glx@7945: if (!fmt->init_write()) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor"); glx@7945: glx@7945: { glx@7945: uint i; glx@7945: uint count = 1 << Savegame_POOL_BLOCK_SIZE_BITS; glx@7945: rubidium@10326: if (_ts.count != _sl.offs_base) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected size of chunk"); glx@7945: for (i = 0; i != _Savegame_pool.GetBlockCount() - 1; i++) { glx@7945: _sl.buf = _Savegame_pool.blocks[i]; glx@7945: fmt->writer(count); glx@7945: } glx@7945: glx@7945: /* The last block is (almost) always not fully filled, so only write away glx@7945: * as much data as it is in there */ glx@7945: _sl.buf = _Savegame_pool.blocks[i]; glx@7945: fmt->writer(_ts.count - (i * count)); glx@7945: } glx@7945: glx@7945: fmt->uninit_write(); rubidium@10326: if (_ts.count != _sl.offs_base) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected size of chunk"); glx@7945: GetSavegameFormat("memory")->uninit_write(); // clean the memorypool glx@7945: fclose(_sl.fh); glx@7945: glx@7945: if (threaded) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE); glx@7945: glx@7945: return SL_OK; glx@7945: } glx@7945: catch (...) { Darkvater@1885: AbortSaveLoad(); rubidium@7532: if (_sl.excpt_uninit != NULL) _sl.excpt_uninit(); Darkvater@1885: rubidium@7532: ShowInfo(GetSaveLoadErrorString()); rubidium@7532: fprintf(stderr, GetSaveLoadErrorString()); rubidium@7532: rubidium@5956: if (threaded) { tron@4077: OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR); tron@4077: } else { tron@4077: SaveFileError(); tron@4077: } rubidium@5956: return SL_ERROR; Darkvater@1885: } rubidium@5956: } rubidium@5956: rubidium@5956: static void* SaveFileToDiskThread(void *arg) rubidium@5956: { rubidium@5956: SaveFileToDisk(true); tron@2286: return NULL; Darkvater@1885: } Darkvater@1885: rubidium@6573: void WaitTillSaved() tron@2285: { tron@2285: OTTDJoinThread(save_thread); tron@2285: save_thread = NULL; tron@2285: } tron@2285: Darkvater@1881: /** Darkvater@1881: * Main Save or Load function where the high-level saveload functions are Darkvater@1881: * handled. It opens the savegame, selects format and checks versions Darkvater@1881: * @param filename The name of the savegame being created/loaded Darkvater@1881: * @param mode Save or load. Load can also be a TTD(Patch) game. Use SL_LOAD, SL_OLD_LOAD or SL_SAVE Darkvater@1881: * @return Return the results of the action. SL_OK, SL_ERROR or SL_REINIT ("unload" the game) Darkvater@1881: */ rubidium@7425: SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb) truelight@0: { truelight@0: uint32 hdr[2]; truelight@0: const SaveLoadFormat *fmt; truelight@193: Darkvater@2413: /* An instance of saving is already active, so don't go saving again */ Darkvater@2413: if (_ts.saveinprogress && mode == SL_SAVE) { belugas@6443: /* if not an autosave, but a user action, show error message */ Darkvater@2749: if (!_do_autosave) ShowErrorMessage(INVALID_STRING_ID, STR_SAVE_STILL_IN_PROGRESS, 0, 0); Darkvater@2413: return SL_OK; Darkvater@1913: } Darkvater@2413: WaitTillSaved(); Darkvater@1913: rubidium@7596: _next_offs = 0; rubidium@7596: Darkvater@2413: /* Load a TTDLX or TTDPatch game */ truelight@0: if (mode == SL_OLD_LOAD) { truelight@2828: InitializeGame(IG_DATE_RESET, 256, 256); // set a mapsize of 256x256 for TTDPatch games or it might get confused truelight@0: if (!LoadOldSaveGame(filename)) return SL_REINIT; peter1138@2808: _sl_version = 0; peter1138@9205: if (!AfterLoadGame()) return SL_REINIT; truelight@0: return SL_OK; truelight@0: } truelight@193: rubidium@7532: _sl.excpt_uninit = NULL; glx@7945: try { glx@7945: _sl.fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb); glx@7945: glx@7945: /* Make it a little easier to load savegames from the console */ glx@7945: if (_sl.fh == NULL && mode == SL_LOAD) _sl.fh = FioFOpenFile(filename, "rb", SAVE_DIR); glx@7945: if (_sl.fh == NULL && mode == SL_LOAD) _sl.fh = FioFOpenFile(filename, "rb", BASE_DIR); glx@7945: glx@7945: if (_sl.fh == NULL) { glx@7945: SlError(mode == SL_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); glx@7945: } glx@7945: glx@7945: _sl.bufe = _sl.bufp = NULL; glx@7945: _sl.offs_base = 0; glx@7945: _sl.save = (mode != 0); glx@7945: _sl.chs = _chunk_handlers; glx@7945: glx@7945: /* General tactic is to first save the game to memory, then use an available writer glx@7945: * to write it to file, either in threaded mode if possible, or single-threaded */ glx@7945: if (mode == SL_SAVE) { /* SAVE game */ glx@7945: fmt = GetSavegameFormat("memory"); // write to memory glx@7945: glx@7945: _sl.write_bytes = fmt->writer; glx@7945: _sl.excpt_uninit = fmt->uninit_write; glx@7945: if (!fmt->init_write()) { glx@7945: DEBUG(sl, 0, "Initializing writer '%s' failed.", fmt->name); glx@7945: return AbortSaveLoad(); glx@7945: } glx@7945: glx@7945: _sl_version = SAVEGAME_VERSION; glx@7945: glx@7945: BeforeSaveGame(); glx@7945: SlSaveChunks(); glx@7945: SlWriteFill(); // flush the save buffer glx@7945: glx@7945: SaveFileStart(); glx@7945: if (_network_server || glx@7945: (save_thread = OTTDCreateThread(&SaveFileToDiskThread, NULL)) == NULL) { glx@7945: if (!_network_server) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode..."); glx@7945: glx@7945: SaveOrLoadResult result = SaveFileToDisk(false); glx@7945: SaveFileDone(); glx@7945: glx@7945: return result; glx@7945: } glx@7945: } else { /* LOAD game */ glx@7945: assert(mode == SL_LOAD); rubidium@8743: DebugDumpCommands("ddc:load:%s\n", filename); glx@7945: glx@7945: if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); glx@7945: glx@7945: /* see if we have any loader for this type. */ glx@7945: for (fmt = _saveload_formats; ; fmt++) { glx@7945: /* No loader found, treat as version 0 and use LZO format */ glx@7945: if (fmt == endof(_saveload_formats)) { glx@7945: DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format"); glx@7945: #if defined(WINCE) glx@7945: /* Of course some system had not to support rewind ;) */ glx@7945: fseek(_sl.fh, 0L, SEEK_SET); glx@7945: clearerr(_sl.fh); glx@7945: #else glx@7945: rewind(_sl.fh); glx@7945: #endif glx@7945: _sl_version = 0; glx@7945: _sl_minor_version = 0; glx@7945: fmt = _saveload_formats + 1; // LZO glx@7945: break; glx@7945: } glx@7945: glx@7945: if (fmt->tag == hdr[0]) { glx@7945: /* check version number */ glx@7945: _sl_version = TO_BE32(hdr[1]) >> 16; glx@7945: /* Minor is not used anymore from version 18.0, but it is still needed glx@7945: * in versions before that (4 cases) which can't be removed easy. glx@7945: * Therefor it is loaded, but never saved (or, it saves a 0 in any scenario). glx@7945: * So never EVER use this minor version again. -- TrueLight -- 22-11-2005 */ glx@7945: _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF; glx@7945: glx@7945: DEBUG(sl, 1, "Loading savegame version %d", _sl_version); glx@7945: glx@7945: /* Is the version higher than the current? */ glx@7945: if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME); glx@7945: break; glx@7945: } glx@7945: } glx@7945: glx@7945: _sl.read_bytes = fmt->reader; glx@7945: _sl.excpt_uninit = fmt->uninit_read; glx@7945: glx@7945: /* loader for this savegame type is not implemented? */ glx@7945: if (fmt->init_read == NULL) { glx@7945: char err_str[64]; glx@7945: snprintf(err_str, lengthof(err_str), "Loader for '%s' is not available.", fmt->name); glx@7945: SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str); glx@7945: } glx@7945: glx@7945: if (!fmt->init_read()) { glx@7945: char err_str[64]; glx@7945: snprintf(err_str, lengthof(err_str), "Initializing loader '%s' failed", fmt->name); glx@7945: SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str); glx@7945: } glx@7945: glx@7945: /* Old maps were hardcoded to 256x256 and thus did not contain glx@7945: * any mapsize information. Pre-initialize to 256x256 to not to glx@7945: * confuse old games */ glx@7945: InitializeGame(IG_DATE_RESET, 256, 256); glx@7945: glx@7945: SlLoadChunks(); glx@7945: fmt->uninit_read(); glx@7945: fclose(_sl.fh); glx@7945: glx@7945: /* After loading fix up savegame for any internal changes that glx@7945: * might've occured since then. If it fails, load back the old game */ glx@7945: if (!AfterLoadGame()) return SL_REINIT; glx@7945: } glx@7945: glx@7945: return SL_OK; glx@7945: } glx@7945: catch (...) { rubidium@7532: AbortSaveLoad(); rubidium@7532: rubidium@7532: /* deinitialize compressor. */ rubidium@7532: if (_sl.excpt_uninit != NULL) _sl.excpt_uninit(); rubidium@7532: rubidium@7532: /* Skip the "color" character */ rubidium@7532: ShowInfoF(GetSaveLoadErrorString() + 3); rubidium@7532: rubidium@7532: /* A saver/loader exception!! reinitialize all variables to prevent crash! */ rubidium@7532: return (mode == SL_LOAD) ? SL_REINIT : SL_ERROR; rubidium@7532: } truelight@0: } truelight@0: Darkvater@1881: /** Do a save when exiting the game (patch option) _patches.autosave_on_exit */ rubidium@6573: void DoExitSave() dominik@643: { rubidium@7425: SaveOrLoad("exit.sav", SL_SAVE, AUTOSAVE_DIR); dominik@643: } dominik@643: Darkvater@1881: #if 0 Darkvater@1881: /** Darkvater@1881: * Function to get the type of the savegame by looking at the file header. Darkvater@1881: * NOTICE: Not used right now, but could be used if extensions of savegames are garbled Darkvater@1881: * @param file Savegame to be checked Darkvater@1881: * @return SL_OLD_LOAD or SL_LOAD of the file Darkvater@1881: */ Darkvater@1881: int GetSavegameType(char *file) truelight@0: { truelight@0: const SaveLoadFormat *fmt; truelight@0: uint32 hdr; truelight@0: FILE *f; truelight@0: int mode = SL_OLD_LOAD; truelight@0: truelight@0: f = fopen(file, "rb"); truelight@0: if (fread(&hdr, sizeof(hdr), 1, f) != 1) { Darkvater@5568: DEBUG(sl, 0, "Savegame is obsolete or invalid format"); tron@2026: mode = SL_LOAD; // don't try to get filename, just show name as it is written tron@4077: } else { belugas@6443: /* see if we have any loader for this type. */ truelight@0: for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) { truelight@0: if (fmt->tag == hdr) { truelight@0: mode = SL_LOAD; // new type of savegame truelight@0: break; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: fclose(f); truelight@0: return mode; Darkvater@1881: } Darkvater@1881: #endif