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"
truelight@0: #include "vehicle.h"
truelight@0: #include "station.h"
truelight@0: #include "town.h"
truelight@0: #include "player.h"
truelight@0: #include "saveload.h"
truelight@0:
truelight@0: enum {
celestar@1993: SAVEGAME_MAJOR_VERSION = 15,
celestar@1935: SAVEGAME_MINOR_VERSION = 0,
truelight@0:
truelight@0: SAVEGAME_LOADABLE_VERSION = (SAVEGAME_MAJOR_VERSION << 8) + SAVEGAME_MINOR_VERSION
truelight@0: };
truelight@0:
Darkvater@1881: enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2};
truelight@0:
truelight@0: SaverLoader _sl;
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@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: */
tron@1514: static inline int 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@1881: int 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:
Darkvater@1881: static inline void SlWriteUint16(VarType v)
truelight@0: {
truelight@0: SlWriteByte((byte)(v >> 8));
truelight@0: SlWriteByte((byte)v);
truelight@0: }
truelight@0:
Darkvater@1881: static inline void SlWriteUint32(uint32 v)
truelight@0: {
truelight@0: SlWriteUint16((uint16)(v >> 16));
truelight@0: SlWriteUint16((uint16)v);
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@1881: static inline int SlReadSparseIndex(void) {return SlReadSimpleGamma();}
Darkvater@1881: static inline void SlWriteSparseIndex(uint index) {SlWriteSimpleGamma(index);}
truelight@0:
Darkvater@1881: static inline int SlReadArrayLength(void) {return SlReadSimpleGamma();}
Darkvater@1881: static inline void SlWriteArrayLength(uint length) {SlWriteSimpleGamma(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) {
tron@2026: case CH_SPARSE_ARRAY: index = SlReadSparseIndex(); break;
Darkvater@1881: case CH_ARRAY: index = _sl.array_index++; break;
truelight@0: default:
Darkvater@1881: DEBUG(misc, 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@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:
truelight@0: SlWriteArrayLength(length + 1 + SlGetGammaLength(_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@1614: #if 0
Darkvater@1881: /**
Darkvater@1881: * Read in bytes from the file/data structure but don't do
Darkvater@1881: * anything with them
Darkvater@1881: * NOTICE: currently unused
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@1881: for (; length != 0; length--)
truelight@0: SlReadByte();
truelight@0: }
Darkvater@1614: #endif
truelight@0:
Darkvater@1881: /* Get the length of the current object */
Darkvater@1881: uint SlGetFieldLength(void) {return _sl.obj_len;}
truelight@0:
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@1881: switch ((conv >> 4) & 0xF) {
Darkvater@1881: case SLE_VAR_I8 >> 4: x = *(int8*)ptr; break;
Darkvater@1881: case SLE_VAR_U8 >> 4: x = *(byte*)ptr; break;
Darkvater@1881: case SLE_VAR_I16 >> 4: x = *(int16*)ptr; break;
Darkvater@1881: case SLE_VAR_U16 >> 4: x = *(uint16*)ptr; break;
Darkvater@1881: case SLE_VAR_I32 >> 4: x = *(int32*)ptr; break;
Darkvater@1881: case SLE_VAR_U32 >> 4: x = *(uint32*)ptr; break;
Darkvater@1881: case SLE_VAR_I64 >> 4: x = *(int64*)ptr; break;
Darkvater@1881: case SLE_VAR_U64 >> 4: x = *(uint64*)ptr; break;
Darkvater@1881: case SLE_VAR_NULL >> 4: x = 0; break;
Darkvater@1881: default: NOT_REACHED();
truelight@0: }
truelight@0:
Darkvater@1881: // Write the value to the file and check if its value is in the desired range
Darkvater@1881: switch (conv & 0xF) {
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@1881: case SLE_FILE_I32: case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
Darkvater@1881: case SLE_FILE_I64: case SLE_FILE_U64: SlWriteUint64(x);break;
Darkvater@1881: default: NOT_REACHED();
truelight@0: }
Darkvater@1881: } else { /* LOAD values */
truelight@193:
truelight@0: // Read a value from the file
Darkvater@1881: switch (conv & 0xF) {
Darkvater@1881: case SLE_FILE_I8: x = (int8)SlReadByte(); break;
Darkvater@1881: case SLE_FILE_U8: x = (byte)SlReadByte(); break;
Darkvater@1881: case SLE_FILE_I16: x = (int16)SlReadUint16(); break;
truelight@0: case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
Darkvater@1881: case SLE_FILE_I32: x = (int32)SlReadUint32(); break;
truelight@0: case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
Darkvater@1881: 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@1881: switch ((conv >> 4) & 0xF) {
Darkvater@1881: case SLE_VAR_I8 >> 4: *(int8*)ptr = x; break;
Darkvater@1881: case SLE_VAR_U8 >> 4: *(byte*)ptr = x; break;
Darkvater@1881: case SLE_VAR_I16 >> 4: *(int16*)ptr = x; break;
Darkvater@1881: case SLE_VAR_U16 >> 4: *(uint16*)ptr = x; break;
Darkvater@1881: case SLE_VAR_I32 >> 4: *(int32*)ptr = x; break;
Darkvater@1881: case SLE_VAR_U32 >> 4: *(uint32*)ptr = x; break;
Darkvater@1881: case SLE_VAR_I64 >> 4: *(int64*)ptr = x; break;
Darkvater@1881: case SLE_VAR_U64 >> 4: *(uint64*)ptr = x; break;
Darkvater@1881: case SLE_VAR_NULL >> 4: break;
Darkvater@1881: default: NOT_REACHED();
truelight@0: }
truelight@0: }
truelight@0: }
truelight@0:
Darkvater@1881: /* Length in bytes of the various datatypes in a savefile. These
Darkvater@1881: * sizes are guaranteed by assert_compiles in stdafx.h */
Darkvater@1881: static const byte _conv_lengths[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
truelight@0:
Darkvater@1881: /**
Darkvater@1881: * Return the size in bytes of a certain type of normal/atomic variable
Darkvater@1881: * @param var The variable the size is being asked of (NOTICE: unused)
Darkvater@1881: * @param conv @VarType type of variable that is used for calculating the size
Darkvater@1881: * @return Return the size of this type in byes
Darkvater@1881: */
Darkvater@1881: static inline size_t SlCalcConvLen(const void *var, VarType conv) {return _conv_lengths[conv & 0xF];}
Darkvater@1881:
Darkvater@1881: /**
Darkvater@1881: * Return the size in bytes of a reference (pointer)
Darkvater@1881: */
Darkvater@1881: static inline size_t SlCalcRefLen(void) {return 2;}
Darkvater@1881:
Darkvater@1881: /**
Darkvater@1881: * Return the size in bytes of a certain type of atomic array
Darkvater@1881: * @param array The variable the size is being asked of (NOTICE: unused)
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: */
Darkvater@1881: static inline size_t SlCalcArrayLen(const void *array, uint length, VarType conv) {return _conv_lengths[conv & 0xF] * length;}
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: {
Darkvater@1881: static const byte conv_mem_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 0};
truelight@0:
truelight@0: // Automatically calculate the length?
truelight@0: if (_sl.need_length != NL_NONE) {
truelight@0: SlSetLength(SlCalcArrayLen(array, length, conv));
truelight@0: // Determine length only?
truelight@0: if (_sl.need_length == NL_CALCLENGTH)
truelight@0: 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 */
truelight@0: if (!_sl.save && _sl.version == 0) {
truelight@0: if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID) {
Darkvater@1881: length *= 2; // int16, uint16 and StringID are 2 bytes in size
truelight@0: conv = SLE_INT8;
truelight@0: } else if (conv == SLE_INT32 || conv == SLE_UINT32) {
Darkvater@1881: length *= 4; // int32 and uint32 are 4 bytes in size
truelight@0: conv = SLE_INT8;
truelight@0: }
truelight@0: }
truelight@0:
Darkvater@1881: /* If the size of elements is 1 byte, no special conversion is needed,
Darkvater@1881: * use specialized copy-to-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@1881: for (; length != 0; length --) {
truelight@0: SlSaveLoadConv(a, conv);
Darkvater@1881: a += conv_mem_size[(conv >> 4) & 0xF]; // get size
truelight@0: }
truelight@0: }
truelight@0: }
truelight@0:
Darkvater@1881: /**
Darkvater@1881: * Calculate the size of an object.
Darkvater@1881: * @param object Object that needs its length calculated
Darkvater@1881: * @param sld The @SaveLoad description of the object so we know how to manipulate it
Darkvater@1881: */
Darkvater@1881: static size_t SlCalcObjLength(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@1881: if (sld->cmd < SL_WRITEBYTE) {
Darkvater@1881: if (HASBIT(sld->cmd, 2)) {
Darkvater@1881: // check if the field is used in the current savegame version
Darkvater@1881: if (_sl.version < sld->version_from || _sl.version > sld->version_to)
truelight@0: continue;
truelight@0: }
truelight@0:
Darkvater@1881: switch (sld->cmd) {
Darkvater@1881: case SL_VAR: case SL_CONDVAR: /* Normal Variable */
Darkvater@1881: length += SlCalcConvLen(NULL, sld->type); break;
Darkvater@1881: case SL_REF: case SL_CONDREF: /* Reference variable */
Darkvater@1881: length += SlCalcRefLen(); break;
Darkvater@1881: case SL_ARR: case SL_CONDARR: /* Array */
Darkvater@1881: length += SlCalcArrayLen(NULL, sld->length, sld->type); break;
Darkvater@1881: default: NOT_REACHED();
truelight@0: }
Darkvater@1881: } else if (sld->cmd == SL_WRITEBYTE) {
Darkvater@1881: length++; // a byte is logically of size 1
Darkvater@1881: } else if (sld->cmd == SL_INCLUDE) {
Darkvater@1881: length += SlCalcObjLength(NULL, _sl.includes[sld->version_from]);
tron@2026: } else
Darkvater@1881: assert(sld->cmd == SL_END);
truelight@0: }
truelight@0: return length;
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@1881: SlSetLength(SlCalcObjLength(object, sld));
truelight@0: if (_sl.need_length == NL_CALCLENGTH)
truelight@0: return;
truelight@0: }
truelight@0:
Darkvater@1881: for (; sld->cmd != SL_END; sld++) {
Darkvater@1881: void *ptr = (byte*)object + sld->offset;
truelight@0:
Darkvater@1881: if (sld->cmd < SL_WRITEBYTE) {
Darkvater@1881: /* CONDITIONAL saveload types depend on the savegame version */
Darkvater@1881: if (HASBIT(sld->cmd, 2)) {
Darkvater@1881: // check if the field is of the right version, if not, proceed to next one
Darkvater@1881: if (_sl.version < sld->version_from || _sl.version > sld->version_to)
truelight@0: continue;
truelight@0: }
truelight@0:
Darkvater@1881: switch (sld->cmd) {
Darkvater@1881: case SL_VAR: case SL_CONDVAR: /* Normal variable */
Darkvater@1881: SlSaveLoadConv(ptr, sld->type); break;
Darkvater@1881: case SL_REF: case SL_CONDREF: /* Reference variable, translate */
Darkvater@1881: /// @todo XXX - another artificial limitof 65K elements of pointers?
Darkvater@1881: if (_sl.save) { // XXX - read/write pointer as uint16? What is with higher indeces?
Darkvater@1881: SlWriteUint16(_sl.ref_to_int_proc(*(void**)ptr, sld->type));
Darkvater@1881: } else
Darkvater@1881: *(void**)ptr = _sl.int_to_ref_proc(SlReadUint16(), sld->type);
truelight@0: break;
Darkvater@1881: case SL_ARR: case SL_CONDARR: /* Array */
Darkvater@1881: SlArray(ptr, sld->length, sld->type); break;
Darkvater@1881: default: NOT_REACHED();
truelight@0: }
truelight@193:
Darkvater@1881: /* SL_WRITEBYTE translates a value of a variable to another one upon
Darkvater@1881: * saving or loading.
Darkvater@1881: * XXX - variable renaming abuse
Darkvater@1881: * g_value: the value of the variable ingame is abused by sld->version_from
Darkvater@1881: * f_value: the value of the variable in the savegame is abused by sld->version_to */
Darkvater@1881: } else if (sld->cmd == SL_WRITEBYTE) {
truelight@0: if (_sl.save) {
Darkvater@1881: SlWriteByte(sld->version_to);
Darkvater@1881: } else
Darkvater@1881: *(byte*)ptr = sld->version_from;
Darkvater@1881: /* SL_INCLUDE loads common code for a type
Darkvater@1881: * XXX - variable renaming abuse
Darkvater@1881: * include_index: common code to include from _desc_includes[], abused by sld->version_from */
Darkvater@1881: } else if (sld->cmd == SL_INCLUDE) {
Darkvater@1881: SlObject(ptr, _sl.includes[sld->version_from]);
tron@2026: } else
Darkvater@1881: assert(sld->cmd == SL_END);
truelight@0: }
truelight@0: }
truelight@0:
Darkvater@1881: /** Calculate the length of global variables
Darkvater@1881: * @param desc The global variable that we want to know the size of
Darkvater@1881: * @return Returns the length of the sought global object
Darkvater@1881: */
truelight@0: static size_t SlCalcGlobListLength(const SaveLoadGlobVarList *desc)
truelight@0: {
truelight@0: size_t length = 0;
truelight@0:
Darkvater@1881: for (; desc->address != NULL; desc++) {
Darkvater@1881: // Of course the global variable must exist in the sought savegame version
Darkvater@1881: if (_sl.version >= desc->from_version && _sl.version <= desc->to_version)
Darkvater@1881: length += SlCalcConvLen(NULL, desc->conv);
truelight@0: }
truelight@0: return length;
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: */
truelight@0: void SlGlobList(const SaveLoadGlobVarList *desc)
truelight@0: {
truelight@0: if (_sl.need_length != NL_NONE) {
truelight@0: SlSetLength(SlCalcGlobListLength(desc));
truelight@0: if (_sl.need_length == NL_CALCLENGTH)
truelight@0: return;
truelight@0: }
Darkvater@1881:
Darkvater@1881: for (; desc->address != NULL; desc++) {
Darkvater@1881: if (_sl.version >= desc->from_version && _sl.version <= desc->to_version)
Darkvater@1881: SlSaveLoadConv(desc->address, desc->conv);
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);
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;) {
truelight@0: while(true) {
truelight@0: if (ch->id == id)
truelight@0: return ch;
truelight@0: if (ch->flags & CH_LAST)
truelight@0: 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@1881: DEBUG(misc, 1) ("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:
truelight@0: 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;
truelight@0: _sl.buf = (byte*)malloc(LZO_SIZE);
truelight@0: return true;
truelight@0: }
truelight@0:
tron@1093: static void UninitLZO(void)
tron@1093: {
truelight@0: free(_sl.buf);
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;
truelight@0: _sl.buf = (byte*)malloc(LZO_SIZE);
truelight@0: return true;
truelight@0: }
truelight@0:
tron@1093: static void UninitNoComp(void)
truelight@0: {
truelight@0: free(_sl.buf);
truelight@0: }
truelight@0:
truelight@0: //********************************************
Darkvater@1885: //********** START OF MEMORY CODE (in ram)****
Darkvater@1885: //********************************************
Darkvater@1885:
Darkvater@1885: enum {
Darkvater@1913: SAVE_POOL_BLOCK_SIZE_BITS = 17,
Darkvater@1913: SAVE_POOL_MAX_BLOCKS = 500
Darkvater@1885: };
Darkvater@1885:
Darkvater@1913: #include "network.h"
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: MemoryPool *save;
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 */
Darkvater@1913: static MemoryPool _save_pool = {"Savegame", SAVE_POOL_MAX_BLOCKS, SAVE_POOL_BLOCK_SIZE_BITS, sizeof(byte), NULL, 0, 0, NULL};
Darkvater@1913: static ThreadedSave _ts;
Darkvater@1885:
Darkvater@1885: static bool InitMem(void)
Darkvater@1885: {
Darkvater@1913: _ts.save = &_save_pool;
Darkvater@1913: _ts.count = 0;
Darkvater@1913:
Darkvater@1913: CleanPool(_ts.save);
Darkvater@1913: AddBlockToPool(_ts.save);
Darkvater@1885:
Darkvater@1885: /* A block from the pool is a contigious area of memory, so it is safe to write to it sequentially */
Darkvater@1913: _sl.bufsize = _ts.save->total_items;
Darkvater@1913: _sl.buf = (byte*)GetItemFromPool(_ts.save, _ts.count);
Darkvater@1885: return true;
Darkvater@1885: }
Darkvater@1885:
Darkvater@1885: static void UnInitMem(void)
Darkvater@1885: {
Darkvater@1913: CleanPool(_ts.save);
Darkvater@1913: _ts.save = NULL;
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 */
Darkvater@1913: AddBlockIfNeeded(_ts.save, _ts.count);
Darkvater@1913: _sl.buf = (byte*)GetItemFromPool(_ts.save, _ts.count);
Darkvater@1885: }
Darkvater@1885:
Darkvater@1885: //********************************************
truelight@0: //********** START OF ZLIB CODE **************
truelight@0: //********************************************
truelight@0:
truelight@0: #if defined(WITH_ZLIB)
truelight@0: #include
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;
truelight@0: _sl.buf = (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);
truelight@0: free(_sl.buf);
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;
truelight@0: _sl.buf = (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);
truelight@0: free(_sl.buf);
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[];
truelight@0: extern const ChunkHandler _player_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 _engine_chunk_handlers[];
truelight@0: extern const ChunkHandler _economy_chunk_handlers[];
truelight@0: extern const ChunkHandler _animated_tile_chunk_handlers[];
truelight@0:
truelight@0: static const ChunkHandler * const _chunk_handlers[] = {
truelight@0: _misc_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,
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
Darkvater@1881: case REF_VEHICLE: return ((Vehicle *)obj)->index + 1;
Darkvater@1881: case REF_STATION: return ((Station *)obj)->index + 1;
Darkvater@1881: case REF_TOWN: return ((Town *)obj)->index + 1;
Darkvater@1881: case REF_ORDER: return ((Order *)obj)->index + 1;
Darkvater@1881: case REF_ROADSTOPS: return ((RoadStop *)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 */
Darkvater@1881: if (rt == REF_VEHICLE_OLD && _sl.full_version >= ((4 << 8) | 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: {
Darkvater@1881: 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: {
Darkvater@1881: 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: {
Darkvater@1881: 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: {
Darkvater@1881: 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: {
Darkvater@1881: if (!AddBlockIfNeeded(&_roadstop_pool, index))
truelight@1314: error("RoadStops: failed loading savegame: too many RoadStops");
Darkvater@1881: return GetRoadStop(index);
truelight@1284: }
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:
Darkvater@1881: 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
ludde@2051: void InitializeGame(uint size_x, uint size_y);
truelight@0: extern bool AfterLoadGame(uint version);
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 */
Darkvater@1881: static inline int 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@1885: static inline void SaveFileStart(void)
Darkvater@1885: {
Darkvater@1913: _ts.ff_state = _fast_forward;
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@1885: static inline 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@1913: /** We have written the whole game into memory, _save_pool, now find
Darkvater@1885: * and appropiate compressor and start writing to file.
Darkvater@1885: */
Darkvater@1885: static bool SaveFileToDisk(void *ptr)
Darkvater@1885: {
Darkvater@1885: const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format);
Darkvater@1885: /* XXX - backup _sl.buf cause it is used internally by the writer
Darkvater@1885: * and we update it for our own purposes */
Darkvater@1885: byte *tmp = _sl.buf;
Darkvater@1885: uint32 hdr[2];
Darkvater@1885:
Darkvater@1885: SaveFileStart();
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.buf = tmp;
Darkvater@1885: _sl.excpt_uninit();
Darkvater@1885:
Darkvater@1885: ShowInfoF("Save game failed: %s.", _sl.excpt_msg);
Darkvater@1885: ShowErrorMessage(STR_4007_GAME_SAVE_FAILED, STR_NULL, 0, 0);
Darkvater@1885:
Darkvater@1885: SaveFileDone();
Darkvater@1885: return false;
Darkvater@1885: }
Darkvater@1885:
Darkvater@1885: /* We have written our stuff to memory, now write it to file! */
Darkvater@1885: hdr[0] = fmt->tag;
Darkvater@1885: hdr[1] = TO_BE32((SAVEGAME_MAJOR_VERSION << 16) + (SAVEGAME_MINOR_VERSION << 8));
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: tmp = _sl.buf; // XXX - init_write can change _sl.buf, so update it
Darkvater@1885:
Darkvater@1885: {
Darkvater@1885: uint i;
Darkvater@1913: uint count = 1 << _ts.save->block_size_bits;
Darkvater@1885:
Darkvater@1913: assert(_ts.count == _sl.offs_base);
Darkvater@1913: for (i = 0; i != _ts.save->current_blocks - 1; i++) {
Darkvater@1913: _sl.buf = _ts.save->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 */
Darkvater@1913: _sl.buf = _ts.save->blocks[i];
Darkvater@1913: fmt->writer(_ts.count - (i * count));
Darkvater@1885:
Darkvater@1885: _sl.buf = tmp; // XXX - reset _sl.buf to its original value to let it continue its internal usage
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@1885: SaveFileDone();
Darkvater@2077: if (*(bool*)ptr) CloseOTTDThread();
Darkvater@1885: return true;
Darkvater@1885: }
Darkvater@1885:
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: */
truelight@0: int SaveOrLoad(const char *filename, int mode)
truelight@0: {
truelight@0: uint32 hdr[2];
truelight@0: const SaveLoadFormat *fmt;
truelight@0: uint version;
truelight@193:
Darkvater@1913: /* An instance of saving is already active, so wait until it is done */
Darkvater@1913: if (_ts.saveinprogress) {
Darkvater@1913: if (!_do_autosave) ShowErrorMessage(_error_message, STR_SAVE_STILL_IN_PROGRESS, 0, 0);
Darkvater@1913: JoinOTTDThread(); // synchronize and wait until save is finished to continue
Darkvater@1913: // nonsense to do an autosave while we were still saving our game, so skip it
Darkvater@1913: if (_do_autosave) return SL_OK;
Darkvater@1913: }
Darkvater@1913:
Darkvater@1913: /* Load a TTDLX or TTDPatch game */
truelight@0: if (mode == SL_OLD_LOAD) {
ludde@2051: InitializeGame(256, 256); // set a mapsize of 256x256 for TTDPatch games or it might get confused
truelight@0: if (!LoadOldSaveGame(filename)) return SL_REINIT;
truelight@0: AfterLoadGame(0);
truelight@0: return SL_OK;
truelight@0: }
truelight@193:
Darkvater@1881: _sl.fh = fopen(filename, (mode == SL_SAVE) ? "wb" : "rb");
Darkvater@1881: if (_sl.fh == NULL) {
Darkvater@1881: DEBUG(misc, 0) ("Cannot open savegame for saving/loading.");
truelight@0: return SL_ERROR;
Darkvater@1881: }
truelight@193:
truelight@0: _sl.bufe = _sl.bufp = NULL;
truelight@0: _sl.offs_base = 0;
truelight@0: _sl.int_to_ref_proc = IntToReference;
truelight@0: _sl.ref_to_int_proc = ReferenceToInt;
truelight@0: _sl.save = mode;
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: } else {
truelight@0: ShowInfoF("Save game failed: %s.", _sl.excpt_msg);
truelight@0: return SL_ERROR;
truelight@0: }
truelight@0: }
truelight@0:
Darkvater@1881: /* We first initialize here to avoid: "warning: variable `version' might
Darkvater@1881: * be clobbered by `longjmp' or `vfork'" */
truelight@0: version = 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@2077: bool threaded = true;
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@1881: DEBUG(misc, 0) ("Initializing writer %s failed.", fmt->name);
Darkvater@1881: return AbortSaveLoad();
Darkvater@1881: }
truelight@193:
truelight@0: _sl.version = SAVEGAME_MAJOR_VERSION;
truelight@193:
truelight@0: BeforeSaveGame();
truelight@0: SlSaveChunks();
truelight@0: SlWriteFill(); // flush the save buffer
Darkvater@1885:
Darkvater@1885: /* Write to file */
Darkvater@2077: if (_network_server || !CreateOTTDThread(&SaveFileToDisk, &threaded)) {
Darkvater@1885: DEBUG(misc, 1) ("cannot create savegame thread, reverting to single-threaded mode...");
Darkvater@2077: threaded = false;
Darkvater@2077: SaveFileToDisk(&threaded);
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@1885: DEBUG(misc, 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@1881: DEBUG(misc, 0) ("Unknown savegame type, trying to load it as the buggy format.");
truelight@0: rewind(_sl.fh);
Darkvater@1881: _sl.version = version = 0;
truelight@938: _sl.full_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
truelight@0: version = TO_BE32(hdr[1]) >> 8;
truelight@193:
truelight@938: /* Is the version higher than the current? */
Darkvater@1881: if (version > SAVEGAME_LOADABLE_VERSION) {
Darkvater@1881: DEBUG(misc, 0) ("Savegame version invalid.");
Darkvater@1881: return AbortSaveLoad();
Darkvater@1881: }
truelight@938:
truelight@938: _sl.version = (version >> 8);
truelight@938: _sl.full_version = version;
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@1881: DEBUG(misc, 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 */
ludde@2051: InitializeGame(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
Darkvater@1885: * might've occured since then. If it fails, load back the old game */
Darkvater@1885: if (!AfterLoadGame(version)) 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@1881: snprintf(buf, sizeof(buf), "%s%sexit.sav", _path.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) {
truelight@0: printf("Savegame is obsolete or invalid format.\n");
tron@2026: mode = SL_LOAD; // don't try to get filename, just show name as it is written
truelight@0: }
truelight@0: 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