tron@2186: /* $Id$ */
tron@2186:
Darkvater@1881: /** @file
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: * - initialize the writer by creating a temporary memory-buffer for it
Darkvater@1881: *
- go through all to-be saved elements, each 'chunk' (ChunkHandler) prefixed by a label
Darkvater@1881: *
- 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: *
- write all data byte-by-byte to the temporary buffer so it is endian-safe
Darkvater@1881: *
- when the buffer is full; flush it to the output (eg save to file) (_sl.buf, _sl.bufp, _sl.bufe)
Darkvater@1881: *
- repeat this until everything is done, and flush any remaining output to file
Darkvater@1881: *
Darkvater@1881: * @see ChunkHandler
Darkvater@1881: * @see SaveLoad
Darkvater@1881: */
truelight@0: #include "stdafx.h"
Darkvater@1891: #include "openttd.h"
tron@1299: #include "debug.h"
tron@2163: #include "functions.h"
Darkvater@3329: #include "hal.h"
truelight@0: #include "vehicle.h"
truelight@0: #include "station.h"
tron@2285: #include "thread.h"
truelight@0: #include "town.h"
truelight@0: #include "player.h"
truelight@0: #include "saveload.h"
rubidium@5469: #include "network/network.h"
tron@2159: #include "variables.h"
tron@2335: #include
truelight@0:
rubidium@5587: extern const uint16 SAVEGAME_VERSION = 43;
truelight@2685: uint16 _sl_version; /// the major savegame version identifier
truelight@2685: byte _sl_minor_version; /// the minor savegame version, DO NOT USE!
tron@2295:
tron@2337: typedef void WriterProc(uint len);
tron@2337: typedef uint ReaderProc(void);
tron@2337:
tron@2295: /** The saveload struct, containing reader-writer functions, bufffer, version, etc. */
tron@2295: static struct {
tron@2295: bool save; /// are we doing a save or a load atm. True when saving
tron@2295: byte need_length; /// ???
tron@2295: byte block_mode; /// ???
tron@2295: bool error; /// did an error occur or not
tron@2295:
tron@2295: int obj_len; /// the length of the current object we are busy with
tron@2295: int array_index, last_array_index; /// in the case of an array, the current and last positions
tron@2295:
tron@2295: uint32 offs_base; /// the offset in number of bytes since we started writing data (eg uncompressed savegame size)
tron@2295:
tron@2295: WriterProc *write_bytes; /// savegame writer function
tron@2295: ReaderProc *read_bytes; /// savegame loader function
tron@2295:
tron@2295: const ChunkHandler* const *chs; /// the chunk of data that is being processed atm (vehicles, signs, etc.)
tron@2295: const SaveLoad* const *includes; /// the internal layouf of the given chunk
tron@2295:
tron@2295: /** When saving/loading savegames, they are always saved to a temporary memory-place
tron@2295: * to be flushed to file (save) or to final place (load) when full. */
tron@2295: byte *bufp, *bufe; /// bufp(ointer) gives the current position in the buffer bufe(nd) gives the end of the buffer
tron@2295:
tron@2295: // these 3 may be used by compressor/decompressors.
tron@2295: byte *buf; /// pointer to temporary memory to read/write, initialized by SaveLoadFormat->initread/write
Darkvater@2414: byte *buf_ori; /// pointer to the original memory location of buf, used to free it afterwards
tron@2295: uint bufsize; /// the size of the temporary memory *buf
tron@2295: FILE *fh; /// the file from which is read or written to
tron@2295:
tron@2295: void (*excpt_uninit)(void); /// the function to execute on any encountered error
tron@2295: const char *excpt_msg; /// the error message
tron@2295: jmp_buf excpt; /// @todo used to jump to "exception handler"; really ugly
tron@2295: } _sl;
tron@2295:
tron@2295:
Darkvater@1881: enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2};
truelight@0:
Darkvater@1881: /**
Darkvater@1881: * Fill the input buffer by reading from the file with the given reader
Darkvater@1881: */
tron@1093: static void SlReadFill(void)
truelight@0: {
truelight@0: uint len = _sl.read_bytes();
truelight@0: assert(len != 0);
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:
Darkvater@1881: static inline uint32 SlGetOffs(void) {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
Darkvater@3044: * as it appears in memory. @see VarTypes
Darkvater@3044: * @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
Darkvater@3044: * as it appears in a saved game. @see VarTypes
Darkvater@3044: * @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:
Darkvater@3044: /* Return the size in bytes of a reference (pointer) */
Darkvater@3044: static inline size_t SlCalcRefLen(void) {return 2;}
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: */
tron@1093: static void SlWriteFill(void)
truelight@0: {
Darkvater@1881: // 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
Darkvater@1881: * 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: /** Error handler, calls longjmp to simulate an exception.
Darkvater@1881: * @todo this was used to have a central place to handle errors, but it is
Darkvater@1881: * pretty ugly, and seriously interferes with any multithreaded approaches */
truelight@0: static void NORETURN SlError(const char *msg)
truelight@0: {
truelight@0: _sl.excpt_msg = msg;
truelight@0: longjmp(_sl.excpt, 0);
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: */
Darkvater@3044: static inline byte SlReadByteInternal(void)
truelight@0: {
truelight@0: if (_sl.bufp == _sl.bufe) SlReadFill();
truelight@0: return *_sl.bufp++;
truelight@0: }
truelight@0:
Darkvater@1881: /** Wrapper for SlReadByteInternal */
Darkvater@3044: byte SlReadByte(void) {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:
Darkvater@1881: static inline int SlReadUint16(void)
truelight@0: {
truelight@0: int x = SlReadByte() << 8;
truelight@0: return x | SlReadByte();
truelight@0: }
truelight@0:
Darkvater@1881: static inline uint32 SlReadUint32(void)
truelight@0: {
truelight@0: uint32 x = SlReadUint16() << 16;
truelight@0: return x | SlReadUint16();
truelight@0: }
truelight@0:
Darkvater@1881: static inline uint64 SlReadUint64(void)
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: */
Darkvater@1881: static uint SlReadSimpleGamma(void)
truelight@0: {
Darkvater@1881: uint i = SlReadByte();
Darkvater@1881: if (HASBIT(i, 7)) {
ludde@2041: i &= ~0x80;
ludde@2041: if (HASBIT(i, 6)) {
ludde@2041: i &= ~0x40;
ludde@2041: if (HASBIT(i, 5)) {
ludde@2041: i &= ~0x20;
ludde@2041: if (HASBIT(i, 4))
ludde@2041: SlError("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));
ludde@2041: SlWriteByte((byte)0xE0 | (i>>24));
ludde@2041: SlWriteByte((byte)(i>>16));
ludde@2041: } else {
ludde@2041: SlWriteByte((byte)0xC0 | (i>>16));
ludde@2041: }
ludde@2041: SlWriteByte((byte)(i>>8));
ludde@2041: } else {
ludde@2041: 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 */
ludde@2041: static inline uint SlGetGammaLength(uint i) {
ludde@2041: return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21));
ludde@2041: }
truelight@0:
Darkvater@3044: static inline uint SlReadSparseIndex(void) {return SlReadSimpleGamma();}
Darkvater@1881: static inline void SlWriteSparseIndex(uint index) {SlWriteSimpleGamma(index);}
truelight@0:
Darkvater@3044: static inline uint SlReadArrayLength(void) {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:
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: */
tron@1093: int SlIterateArray(void)
truelight@0: {
Darkvater@1881: int index;
truelight@0: static uint32 next_offs;
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. */
truelight@0: assert(next_offs == 0 || SlGetOffs() == next_offs);
truelight@0:
Darkvater@1881: while (true) {
Darkvater@1881: uint length = SlReadArrayLength();
Darkvater@1881: if (length == 0) {
truelight@0: next_offs = 0;
truelight@0: return -1;
truelight@0: }
truelight@0:
Darkvater@1881: _sl.obj_len = --length;
Darkvater@1881: 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@5380: 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:
ludde@2041: // Ugly encoding of >16M RIFF chunks
ludde@2041: // The lower 24 bits are normal
ludde@2041: // 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 */
Darkvater@1881: uint SlGetFieldLength(void) {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
Darkvater@3108: * with other flags. It is parsed upon read
Darkvater@3108: * @param var 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;
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
Darkvater@1881: * @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:
Darkvater@1881: 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: }
Darkvater@1881: } 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: {
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.)
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@5587: 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
Darkvater@3048: * @param the 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:
Darkvater@5140: 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@5587: 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@5587: len = SlCalcNetStringLen((char*)ptr, SIZE_MAX);
Darkvater@5140: break;
Darkvater@5140: }
Darkvater@5140:
Darkvater@3510: SlWriteArrayLength(len);
Darkvater@3510: SlCopyBytes(ptr, len);
Darkvater@5140: } 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@5380: 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:
Darkvater@5140: case SLE_VAR_STRQ: /* Malloc'd string, free previous incarnation, and allocate */
Darkvater@5140: free(*(char**)ptr);
rubidium@5587: *(char**)ptr = (char*)malloc(len + 1); // terminating '\0'
Darkvater@5140: ptr = *(char**)ptr;
Darkvater@5140: SlCopyBytes(ptr, len);
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
Darkvater@1881: * @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
Darkvater@1881: * @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: {
truelight@0: // Automatically calculate the length?
truelight@0: if (_sl.need_length != NL_NONE) {
tron@2958: SlSetLength(SlCalcArrayLen(length, conv));
truelight@0: // 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:
Darkvater@3044: /* 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.
Darkvater@1881: * @param sld The @SaveLoad description of the object so we know how to manipulate it
Darkvater@1881: */
Darkvater@5142: static size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
truelight@0: {
truelight@0: size_t length = 0;
truelight@0:
truelight@0: // 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:
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);
Darkvater@1881: default: NOT_REACHED();
truelight@0: }
Darkvater@3046: break;
Darkvater@3046: case SL_WRITEBYTE: return 1; // a byte is logically of size 1
Darkvater@5142: case SL_INCLUDE: return SlCalcObjLength(object, _sl.includes[sld->version_from]);
Darkvater@3046: default: NOT_REACHED();
truelight@0: }
Darkvater@3046: return 0;
Darkvater@3046: }
Darkvater@3046:
tron@4039:
tron@4039: static uint ReferenceToInt(const void* obj, SLRefType rt);
tron@4039: static void* IntToReference(uint index, SLRefType rt);
tron@4039:
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:
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;
Darkvater@3046: case SL_REF: /* Reference variable, translate */
Darkvater@3046: /// @todo XXX - another artificial limitof 65K elements of pointers?
Darkvater@3046: if (_sl.save) { // XXX - read/write pointer as uint16? What is with higher indeces?
rubidium@5587: SlWriteUint16(ReferenceToInt(*(void**)ptr, (SLRefType)conv));
tron@4039: } else {
rubidium@5587: *(void**)ptr = IntToReference(SlReadUint16(), (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;
Darkvater@3046: default: NOT_REACHED();
Darkvater@3046: }
Darkvater@3046: break;
Darkvater@3046:
Darkvater@3046: /* SL_WRITEBYTE translates a value of a variable to another one upon
rubidium@4549: * saving or loading.
rubidium@4549: * XXX - variable renaming abuse
rubidium@4549: * game_value: the value of the variable ingame is abused by sld->version_from
rubidium@4549: * file_value: the value of the variable in the savegame is abused by sld->version_to */
Darkvater@3046: case SL_WRITEBYTE:
Darkvater@3046: if (_sl.save) {
Darkvater@3046: SlWriteByte(sld->version_to);
Darkvater@3046: } else {
Darkvater@3046: *(byte*)ptr = sld->version_from;
Darkvater@3046: }
Darkvater@3046: break;
Darkvater@3046:
Darkvater@3046: /* SL_INCLUDE loads common code for a type
Darkvater@3046: * XXX - variable renaming abuse
Darkvater@3046: * include_index: common code to include from _desc_includes[], abused by sld->version_from */
Darkvater@3046: case SL_INCLUDE:
Darkvater@3046: SlObject(ptr, _sl.includes[sld->version_from]);
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
Darkvater@1881: * @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: {
truelight@0: // 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++) {
Darkvater@5141: void *ptr = 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
Darkvater@1881: * @param desc The global variable that is being loaded or saved
Darkvater@1881: */
Darkvater@3046: void SlGlobList(const SaveLoadGlobVarList *sldg)
truelight@0: {
truelight@0: if (_sl.need_length != NL_NONE) {
Darkvater@5142: SlSetLength(SlCalcObjLength(NULL, (const SaveLoad*)sldg));
Darkvater@3046: if (_sl.need_length == NL_CALCLENGTH) return;
truelight@0: }
Darkvater@1881:
Darkvater@3046: for (; sldg->cmd != SL_END; sldg++) {
Darkvater@3073: SlObjectMember(sldg->address, (const SaveLoad*)sldg);
truelight@0: }
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:
truelight@0: // 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:
truelight@0: // 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:
truelight@0: // And write the stuff
truelight@0: proc(arg);
truelight@0:
truelight@0: assert(offs == SlGetOffs());
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) {
ludde@2041: // 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();
ludde@2041: assert(SlGetOffs() == endoffs);
ludde@2041: } else {
ludde@2041: SlError("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();}
Darkvater@1881: static void SlStubSaveProc(void) {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:
truelight@0: SlWriteUint32(ch->id);
Darkvater@5380: 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) {
truelight@0: // 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 */
tron@1093: static void SlSaveChunks(void)
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:
truelight@0: // 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;
Darkvater@1881: 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 */
tron@1093: static void SlLoadChunks(void)
truelight@0: {
truelight@0: uint32 id;
truelight@0: const ChunkHandler *ch;
truelight@0:
Darkvater@1881: for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
Darkvater@5380: DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
Darkvater@1881:
truelight@0: ch = SlFindChunkHandler(id);
truelight@0: if (ch == NULL) SlError("found unknown tag in savegame (sync error)");
truelight@0: SlLoadChunk(ch);
truelight@0: }
truelight@0: }
truelight@0:
truelight@0: //*******************************************
truelight@0: //********** START OF LZO CODE **************
truelight@0: //*******************************************
truelight@0: #define LZO_SIZE 8192
truelight@0:
truelight@781: #include "minilzo.h"
truelight@0:
tron@1093: static uint ReadLZO(void)
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:
truelight@0: // Read header
truelight@0: if (fread(tmp, sizeof(tmp), 1, _sl.fh) != 1) SlError("file read failed");
truelight@0:
truelight@0: // 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:
truelight@0: if (size >= sizeof(out)) SlError("inconsistent size");
truelight@0:
truelight@0: // Read block
truelight@0: if (fread(out + sizeof(uint32), size, 1, _sl.fh) != 1) SlError("file read failed");
truelight@0:
truelight@0: // Verify checksum
truelight@0: if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlError("bad checksum");
truelight@193:
truelight@0: // Decompress
truelight@0: lzo1x_decompress(out + sizeof(uint32)*1, size, _sl.buf, &len, NULL);
truelight@0: return len;
truelight@0: }
truelight@0:
truelight@0: // p contains the pointer to the buffer, len contains the pointer to the length.
truelight@0: // 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)));
truelight@0: if (fwrite(out, outlen + sizeof(uint32)*2, 1, _sl.fh) != 1) SlError("file write failed");
truelight@0: }
truelight@0:
tron@1093: static bool InitLZO(void)
tron@1093: {
truelight@0: _sl.bufsize = LZO_SIZE;
Darkvater@2414: _sl.buf = _sl.buf_ori = (byte*)malloc(LZO_SIZE);
truelight@0: return true;
truelight@0: }
truelight@0:
tron@1093: static void UninitLZO(void)
tron@1093: {
Darkvater@2414: free(_sl.buf_ori);
truelight@0: }
truelight@0:
Darkvater@1881: //*********************************************
Darkvater@1881: //******** START OF NOCOMP CODE (uncompressed)*
Darkvater@1881: //*********************************************
tron@1093: static uint ReadNoComp(void)
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:
tron@1093: static bool InitNoComp(void)
truelight@0: {
truelight@0: _sl.bufsize = LZO_SIZE;
Darkvater@2414: _sl.buf = _sl.buf_ori =(byte*)malloc(LZO_SIZE);
truelight@0: return true;
truelight@0: }
truelight@0:
tron@1093: static void UninitNoComp(void)
truelight@0: {
Darkvater@2414: free(_sl.buf_ori);
truelight@0: }
truelight@0:
truelight@0: //********************************************
Darkvater@1885: //********** START OF MEMORY CODE (in ram)****
Darkvater@1885: //********************************************
Darkvater@1885:
Darkvater@1913: #include "table/strings.h"
Darkvater@1913: #include "table/sprites.h"
Darkvater@1913: #include "gfx.h"
Darkvater@1913: #include "gui.h"
Darkvater@1913:
Darkvater@1913: typedef struct ThreadedSave {
Darkvater@1913: uint count;
Darkvater@1913: bool ff_state;
Darkvater@1913: bool saveinprogress;
Darkvater@1914: CursorID cursor;
Darkvater@1913: } ThreadedSave;
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:
Darkvater@1885: static bool InitMem(void)
Darkvater@1885: {
Darkvater@1913: _ts.count = 0;
Darkvater@1913:
tron@4985: CleanPool(&_Savegame_pool);
tron@4985: AddBlockToPool(&_Savegame_pool);
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:
Darkvater@1885: static void UnInitMem(void)
Darkvater@1885: {
tron@4985: CleanPool(&_Savegame_pool);
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 */
tron@4985: AddBlockIfNeeded(&_Savegame_pool, _ts.count);
tron@4985: _sl.buf = GetSavegame(_ts.count);
Darkvater@1885: }
Darkvater@1885:
Darkvater@1885: //********************************************
truelight@0: //********** START OF ZLIB CODE **************
truelight@0: //********************************************
truelight@0:
truelight@0: #if defined(WITH_ZLIB)
Darkvater@2694: #include
ludde@2138:
truelight@0: static z_stream _z;
truelight@0:
tron@1093: static bool InitReadZlib(void)
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;
Darkvater@2414: _sl.buf = _sl.buf_ori = (byte*)malloc(4096 + 4096); // also contains fread buffer
truelight@0: return true;
truelight@0: }
truelight@0:
tron@1093: static uint ReadZlib(void)
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 {
truelight@0: // 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:
truelight@0: // inflate the data
truelight@0: r = inflate(&_z, 0);
truelight@0: if (r == Z_STREAM_END)
truelight@0: break;
truelight@0:
truelight@193: if (r != Z_OK)
truelight@0: SlError("inflate() failed");
truelight@0: } while (_z.avail_out);
truelight@0:
truelight@0: return 4096 - _z.avail_out;
truelight@0: }
truelight@0:
tron@1093: static void UninitReadZlib(void)
truelight@0: {
truelight@0: inflateEnd(&_z);
Darkvater@2414: free(_sl.buf_ori);
truelight@0: }
truelight@0:
tron@1093: static bool InitWriteZlib(void)
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;
Darkvater@2414: _sl.buf = _sl.buf_ori = (byte*)malloc(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);
truelight@0: // bytes were emitted?
truelight@0: if ((n=sizeof(buf) - z->avail_out) != 0) {
truelight@0: if (fwrite(buf, n, 1, _sl.fh) != 1) SlError("file write error");
truelight@0: }
truelight@0: if (r == Z_STREAM_END)
truelight@0: break;
truelight@0: if (r != Z_OK) SlError("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:
tron@1093: static void UninitWriteZlib(void)
truelight@0: {
truelight@0: // 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:
truelight@0: //*******************************************
truelight@0: //************* END OF CODE *****************
truelight@0: //*******************************************
truelight@0:
truelight@0: // 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[];
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,
truelight@0: NULL,
truelight@0: };
truelight@0:
truelight@0: // used to include a vehicle desc in another desc.
Darkvater@1881: extern const SaveLoad _common_veh_desc[];
Darkvater@1881: static const SaveLoad* const _desc_includes[] = {
truelight@0: _common_veh_desc
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
Darkvater@1881: * @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;
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
Darkvater@1881: * @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 */
truelight@2685: if (rt == REF_VEHICLE_OLD && !CheckSavegameVersionOldStyle(4, 4))
Darkvater@1881: rt = REF_VEHICLE;
truelight@938:
Darkvater@1881: /* No need to look up NULL pointers, just return immediately */
Darkvater@1881: if (rt != REF_VEHICLE_OLD && index == 0)
truelight@938: return NULL;
truelight@938:
Darkvater@1881: index--; // correct for the NULL index
Darkvater@1881:
Darkvater@1881: switch (rt) {
truelight@1314: case REF_ORDER: {
tron@4977: if (!AddBlockIfNeeded(&_Order_pool, index))
truelight@1314: error("Orders: failed loading savegame: too many orders");
Darkvater@1881: return GetOrder(index);
truelight@1314: }
truelight@1279: case REF_VEHICLE: {
tron@4972: if (!AddBlockIfNeeded(&_Vehicle_pool, index))
truelight@1279: error("Vehicles: failed loading savegame: too many vehicles");
Darkvater@1881: return GetVehicle(index);
truelight@1279: }
truelight@1272: case REF_STATION: {
tron@4980: if (!AddBlockIfNeeded(&_Station_pool, index))
truelight@1272: error("Stations: failed loading savegame: too many stations");
Darkvater@1881: return GetStation(index);
truelight@1272: }
truelight@1260: case REF_TOWN: {
tron@4983: if (!AddBlockIfNeeded(&_Town_pool, index))
truelight@1260: error("Towns: failed loading savegame: too many towns");
Darkvater@1881: return GetTown(index);
truelight@1260: }
truelight@1284: case REF_ROADSTOPS: {
tron@4981: if (!AddBlockIfNeeded(&_RoadStop_pool, index))
truelight@1314: error("RoadStops: failed loading savegame: too many RoadStops");
Darkvater@1881: return GetRoadStop(index);
truelight@1284: }
peter1138@2848: case REF_ENGINE_RENEWS: {
tron@4974: if (!AddBlockIfNeeded(&_EngineRenew_pool, index))
peter1138@2848: error("EngineRenews: failed loading savegame: too many EngineRenews");
peter1138@2848: return GetEngineRenew(index);
peter1138@2848: }
truelight@938:
truelight@938: 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++;
Darkvater@1881: if (index == INVALID_VEHICLE)
truelight@938: return NULL;
truelight@1279:
tron@4972: if (!AddBlockIfNeeded(&_Vehicle_pool, index))
truelight@1279: error("Vehicles: failed loading savegame: too many vehicles");
Darkvater@1881: return GetVehicle(index);
truelight@938: }
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 */
truelight@0: typedef struct {
Darkvater@1881: const char *name; /// name of the compressor/decompressor (debug-only)
Darkvater@1881: uint32 tag; /// the 4-letter tag by which it is identified in the savegame
truelight@0:
Darkvater@1881: bool (*init_read)(void); /// function executed upon initalization of the loader
Darkvater@1881: ReaderProc *reader; /// function that loads the data from the file
Darkvater@1881: void (*uninit_read)(void); /// function executed when reading is finished
truelight@0:
Darkvater@1881: bool (*init_write)(void); /// function executed upon intialization of the saver
Darkvater@1881: WriterProc *writer; /// function that saves the data to the file
Darkvater@1881: void (*uninit_write)(void); /// function executed when writing is done
truelight@0: } SaveLoadFormat;
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
Darkvater@1881: * @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:
Darkvater@1881: // 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:
truelight@0: // actual loader/saver function
truelight@2828: void InitializeGame(int mode, uint size_x, uint size_y);
peter1138@2808: extern bool AfterLoadGame(void);
tron@1093: extern void BeforeSaveGame(void);
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 */
tron@2162: static inline SaveOrLoadResult AbortSaveLoad(void)
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 */
Darkvater@2380: void SaveFileStart(void)
Darkvater@1885: {
rubidium@5587: _ts.ff_state = (_fast_forward != 0);
Darkvater@1913: _fast_forward = false;
Darkvater@1913: if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ);
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 */
Darkvater@2380: void SaveFileDone(void)
Darkvater@1885: {
Darkvater@1913: _fast_forward = _ts.ff_state;
Darkvater@1885: if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE);
Darkvater@1913:
Darkvater@1885: SendWindowMessage(WC_STATUS_BAR, 0, false, 0, 0);
Darkvater@1913: _ts.saveinprogress = false;
Darkvater@1885: }
Darkvater@1885:
Darkvater@2380: /** Show a gui message when saving has failed */
Darkvater@2380: void SaveFileError(void)
Darkvater@2380: {
Darkvater@2380: ShowErrorMessage(STR_4007_GAME_SAVE_FAILED, 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: */
Darkvater@2380: static void* SaveFileToDisk(void *arg)
Darkvater@1885: {
truelight@2927: const SaveLoadFormat *fmt;
Darkvater@1885: uint32 hdr[2];
Darkvater@1885:
Darkvater@1885: /* XXX - Setup setjmp error handler if an error occurs anywhere deep during
Darkvater@1885: * loading/saving execute a longjmp() and continue execution here */
Darkvater@1885: if (setjmp(_sl.excpt)) {
Darkvater@1885: AbortSaveLoad();
Darkvater@1885: _sl.excpt_uninit();
Darkvater@1885:
Darkvater@2413: fprintf(stderr, "Save game failed: %s.", _sl.excpt_msg);
tron@4077: if (arg != NULL) {
tron@4077: OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR);
tron@4077: } else {
tron@4077: SaveFileError();
tron@4077: }
tron@2286: return NULL;
Darkvater@1885: }
Darkvater@1885:
truelight@2927: fmt = GetSavegameFormat(_savegame_format);
truelight@2927:
Darkvater@1885: /* We have written our stuff to memory, now write it to file! */
Darkvater@1885: hdr[0] = fmt->tag;
truelight@2685: hdr[1] = TO_BE32(SAVEGAME_VERSION << 16);
Darkvater@1885: if (fwrite(hdr, sizeof(hdr), 1, _sl.fh) != 1) SlError("file write failed");
Darkvater@1885:
Darkvater@1885: if (!fmt->init_write()) SlError("cannot initialize compressor");
Darkvater@1885:
Darkvater@1885: {
Darkvater@1885: uint i;
tron@4985: uint count = 1 << Savegame_POOL_BLOCK_SIZE_BITS;
Darkvater@1885:
Darkvater@1913: assert(_ts.count == _sl.offs_base);
tron@4985: for (i = 0; i != _Savegame_pool.current_blocks - 1; i++) {
tron@4985: _sl.buf = _Savegame_pool.blocks[i];
Darkvater@1885: fmt->writer(count);
Darkvater@1885: }
Darkvater@1885:
Darkvater@1885: /* The last block is (almost) always not fully filled, so only write away
Darkvater@1885: * as much data as it is in there */
tron@4985: _sl.buf = _Savegame_pool.blocks[i];
Darkvater@1913: fmt->writer(_ts.count - (i * count));
Darkvater@1885: }
Darkvater@1885:
Darkvater@1885: fmt->uninit_write();
Darkvater@1913: assert(_ts.count == _sl.offs_base);
Darkvater@1885: GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
Darkvater@1885: fclose(_sl.fh);
Darkvater@1885:
Darkvater@2413: if (arg != NULL) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE);
tron@2286: return NULL;
Darkvater@1885: }
Darkvater@1885:
tron@2285: void WaitTillSaved(void)
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: */
tron@2162: SaveOrLoadResult SaveOrLoad(const char *filename, int mode)
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) {
Darkvater@2413: // 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:
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@2808: AfterLoadGame();
truelight@0: return SL_OK;
truelight@0: }
truelight@193:
Darkvater@5167: _sl.fh = (mode == SL_SAVE) ? fopen(filename, "wb") : fopen(filename, "rb");
Darkvater@1881: if (_sl.fh == NULL) {
Darkvater@5380: DEBUG(sl, 0, "Cannot open savegame '%s' for saving/loading.", filename);
truelight@0: return SL_ERROR;
Darkvater@1881: }
truelight@193:
truelight@0: _sl.bufe = _sl.bufp = NULL;
truelight@0: _sl.offs_base = 0;
rubidium@5587: _sl.save = (mode != 0);
truelight@0: _sl.includes = _desc_includes;
truelight@0: _sl.chs = _chunk_handlers;
truelight@0:
Darkvater@1885: /* XXX - Setup setjmp error handler if an error occurs anywhere deep during
Darkvater@1885: * loading/saving execute a longjmp() and continue execution here */
truelight@0: if (setjmp(_sl.excpt)) {
Darkvater@1881: AbortSaveLoad();
truelight@0:
truelight@0: // deinitialize compressor.
truelight@0: _sl.excpt_uninit();
truelight@0:
Darkvater@1881: /* A saver/loader exception!! reinitialize all variables to prevent crash! */
truelight@0: if (mode == SL_LOAD) {
truelight@0: ShowInfoF("Load game failed: %s.", _sl.excpt_msg);
truelight@0: return SL_REINIT;
truelight@0: }
Darkvater@2382:
Darkvater@2380: ShowInfoF("Save game failed: %s.", _sl.excpt_msg);
Darkvater@2380: return SL_ERROR;
truelight@0: }
truelight@0:
Darkvater@1885: /* General tactic is to first save the game to memory, then use an available writer
Darkvater@1885: * to write it to file, either in threaded mode if possible, or single-threaded */
Darkvater@1881: if (mode == SL_SAVE) { /* SAVE game */
Darkvater@1885: fmt = GetSavegameFormat("memory"); // write to memory
truelight@193:
truelight@0: _sl.write_bytes = fmt->writer;
truelight@0: _sl.excpt_uninit = fmt->uninit_write;
Darkvater@1881: if (!fmt->init_write()) {
Darkvater@5380: DEBUG(sl, 0, "Initializing writer '%s' failed.", fmt->name);
Darkvater@1881: return AbortSaveLoad();
Darkvater@1881: }
truelight@193:
truelight@2685: _sl_version = SAVEGAME_VERSION;
truelight@193:
truelight@0: BeforeSaveGame();
truelight@0: SlSaveChunks();
truelight@0: SlWriteFill(); // flush the save buffer
Darkvater@1885:
truelight@4323: SaveFileStart();
tron@2285: if (_network_server ||
Darkvater@2413: (save_thread = OTTDCreateThread(&SaveFileToDisk, (void*)"")) == NULL) {
Darkvater@5380: DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
tron@2283: SaveFileToDisk(NULL);
truelight@4323: SaveFileDone();
Darkvater@1885: }
truelight@0:
Darkvater@1881: } else { /* LOAD game */
Darkvater@1885: assert(mode == SL_LOAD);
Darkvater@1885:
truelight@0: if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) {
Darkvater@5380: DEBUG(sl, 0, "Cannot read savegame header, aborting");
Darkvater@1881: return AbortSaveLoad();
truelight@0: }
truelight@0:
truelight@0: // see if we have any loader for this type.
Darkvater@1881: for (fmt = _saveload_formats; ; fmt++) {
Darkvater@1881: /* No loader found, treat as version 0 and use LZO format */
truelight@0: if (fmt == endof(_saveload_formats)) {
Darkvater@5380: DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
truelight@0: rewind(_sl.fh);
glx@4016: _sl_version = 0;
truelight@2685: _sl_minor_version = 0;
Darkvater@1913: fmt = _saveload_formats + 1; // LZO
truelight@0: break;
truelight@0: }
Darkvater@1881:
truelight@0: if (fmt->tag == hdr[0]) {
truelight@0: // check version number
glx@4016: _sl_version = TO_BE32(hdr[1]) >> 16;
truelight@2685: /* Minor is not used anymore from version 18.0, but it is still needed
truelight@2685: * in versions before that (4 cases) which can't be removed easy.
truelight@2685: * Therefor it is loaded, but never saved (or, it saves a 0 in any scenario).
truelight@2685: * So never EVER use this minor version again. -- TrueLight -- 22-11-2005 */
truelight@2685: _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
truelight@2685:
Darkvater@5380: DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
truelight@193:
truelight@938: /* Is the version higher than the current? */
truelight@2685: if (_sl_version > SAVEGAME_VERSION) {
Darkvater@5380: DEBUG(sl, 0, "Savegame version invalid");
Darkvater@1881: return AbortSaveLoad();
Darkvater@1881: }
truelight@0: break;
truelight@0: }
truelight@0: }
truelight@0:
truelight@0: _sl.read_bytes = fmt->reader;
truelight@0: _sl.excpt_uninit = fmt->uninit_read;
truelight@0:
truelight@0: // loader for this savegame type is not implemented?
truelight@0: if (fmt->init_read == NULL) {
truelight@0: ShowInfoF("Loader for '%s' is not available.", fmt->name);
Darkvater@1881: return AbortSaveLoad();
truelight@0: }
truelight@0:
Darkvater@1881: if (!fmt->init_read()) {
Darkvater@5380: DEBUG(sl, 0, "Initializing loader '%s' failed", fmt->name);
Darkvater@1881: return AbortSaveLoad();
Darkvater@1881: }
Darkvater@1881:
Darkvater@1885: /* Old maps were hardcoded to 256x256 and thus did not contain
Darkvater@1885: * any mapsize information. Pre-initialize to 256x256 to not to
Darkvater@1885: * confuse old games */
truelight@2828: InitializeGame(IG_DATE_RESET, 256, 256);
tron@1218:
truelight@0: SlLoadChunks();
truelight@0: fmt->uninit_read();
Darkvater@1885: fclose(_sl.fh);
truelight@0:
Darkvater@1885: /* After loading fix up savegame for any internal changes that
rubidium@4549: * might've occured since then. If it fails, load back the old game */
peter1138@2808: if (!AfterLoadGame()) return SL_REINIT;
Darkvater@1885: }
truelight@0:
truelight@0: return SL_OK;
truelight@0: }
truelight@0:
Darkvater@1881: /** Do a save when exiting the game (patch option) _patches.autosave_on_exit */
tron@1093: void DoExitSave(void)
dominik@643: {
dominik@643: char buf[200];
Darkvater@5296: snprintf(buf, sizeof(buf), "%s%sexit.sav", _paths.autosave_dir, PATHSEP);
dominik@643: SaveOrLoad(buf, SL_SAVE);
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@5380: 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 {
truelight@0: // 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