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