2005-07-24 15:12:37 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 21:21:05 +01:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
|
|
|
* @file saveload.cpp
|
2005-05-30 23:16:05 +01: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
|
2010-11-20 14:15:02 +00:00
|
|
|
* <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
|
2005-05-30 23:16:05 +01:00
|
|
|
* 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>
|
|
|
|
*/
|
2009-01-04 15:32:25 +00:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "../station_base.h"
|
2009-09-01 11:07:22 +01:00
|
|
|
#include "../thread/thread.h"
|
2009-01-04 15:32:25 +00:00
|
|
|
#include "../town.h"
|
|
|
|
#include "../network/network.h"
|
|
|
|
#include "../window_func.h"
|
|
|
|
#include "../strings_func.h"
|
|
|
|
#include "../core/endian_func.hpp"
|
|
|
|
#include "../vehicle_base.h"
|
|
|
|
#include "../company_func.h"
|
|
|
|
#include "../date_func.h"
|
|
|
|
#include "../autoreplace_base.h"
|
2009-06-23 22:44:48 +01:00
|
|
|
#include "../roadstop_base.h"
|
2009-01-04 15:32:25 +00:00
|
|
|
#include "../statusbar_gui.h"
|
|
|
|
#include "../fileio_func.h"
|
|
|
|
#include "../gamelog.h"
|
2009-03-06 01:23:25 +00:00
|
|
|
#include "../string_func.h"
|
2009-03-08 16:51:08 +00:00
|
|
|
#include "../engine_base.h"
|
2010-06-13 15:13:23 +01:00
|
|
|
#include "../fios.h"
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
#include "saveload_internal.h"
|
|
|
|
|
2010-07-28 10:23:42 +01:00
|
|
|
/*
|
|
|
|
* Previous savegame versions, the trunk revision where they were
|
|
|
|
* introduced and the released version that had that particular
|
|
|
|
* savegame version.
|
|
|
|
* Up to savegame version 18 there is a minor version as well.
|
|
|
|
*
|
|
|
|
* 1.0 0.1.x, 0.2.x
|
|
|
|
* 2.0 0.3.0
|
|
|
|
* 2.1 0.3.1, 0.3.2
|
|
|
|
* 3.x lost
|
|
|
|
* 4.0 1
|
|
|
|
* 4.1 122 0.3.3, 0.3.4
|
|
|
|
* 4.2 1222 0.3.5
|
|
|
|
* 4.3 1417
|
|
|
|
* 4.4 1426
|
|
|
|
* 5.0 1429
|
|
|
|
* 5.1 1440
|
|
|
|
* 5.2 1525 0.3.6
|
|
|
|
* 6.0 1721
|
|
|
|
* 6.1 1768
|
|
|
|
* 7.0 1770
|
|
|
|
* 8.0 1786
|
|
|
|
* 9.0 1909
|
|
|
|
* 10.0 2030
|
|
|
|
* 11.0 2033
|
|
|
|
* 11.1 2041
|
2010-07-28 10:48:35 +01:00
|
|
|
* 12.1 2046
|
2010-07-28 10:23:42 +01:00
|
|
|
* 13.1 2080 0.4.0, 0.4.0.1
|
|
|
|
* 14.0 2441
|
|
|
|
* 15.0 2499
|
|
|
|
* 16.0 2817
|
|
|
|
* 16.1 3155
|
|
|
|
* 17.0 3212
|
|
|
|
* 17.1 3218
|
|
|
|
* 18 3227
|
|
|
|
* 19 3396
|
|
|
|
* 20 3403
|
|
|
|
* 21 3472 0.4.x
|
|
|
|
* 22 3726
|
|
|
|
* 23 3915
|
|
|
|
* 24 4150
|
|
|
|
* 25 4259
|
|
|
|
* 26 4466
|
|
|
|
* 27 4757
|
|
|
|
* 28 4987
|
|
|
|
* 29 5070
|
|
|
|
* 30 5946
|
|
|
|
* 31 5999
|
|
|
|
* 32 6001
|
|
|
|
* 33 6440
|
|
|
|
* 34 6455
|
|
|
|
* 35 6602
|
|
|
|
* 36 6624
|
|
|
|
* 37 7182
|
|
|
|
* 38 7195
|
|
|
|
* 39 7269
|
|
|
|
* 40 7326
|
|
|
|
* 41 7348 0.5.x
|
|
|
|
* 42 7573
|
|
|
|
* 43 7642
|
|
|
|
* 44 8144
|
|
|
|
* 45 8501
|
|
|
|
* 46 8705
|
|
|
|
* 47 8735
|
|
|
|
* 48 8935
|
|
|
|
* 49 8969
|
|
|
|
* 50 8973
|
|
|
|
* 51 8978
|
|
|
|
* 52 9066
|
|
|
|
* 53 9316
|
|
|
|
* 54 9613
|
|
|
|
* 55 9638
|
|
|
|
* 56 9667
|
|
|
|
* 57 9691
|
|
|
|
* 58 9762
|
|
|
|
* 59 9779
|
|
|
|
* 60 9874
|
|
|
|
* 61 9892
|
|
|
|
* 62 9905
|
|
|
|
* 63 9956
|
|
|
|
* 64 10006
|
|
|
|
* 65 10210
|
|
|
|
* 66 10211
|
|
|
|
* 67 10236
|
|
|
|
* 68 10266
|
|
|
|
* 69 10319
|
|
|
|
* 70 10541
|
|
|
|
* 71 10567
|
|
|
|
* 72 10601
|
|
|
|
* 73 10903
|
|
|
|
* 74 11030
|
|
|
|
* 75 11107
|
|
|
|
* 76 11139
|
|
|
|
* 77 11172
|
|
|
|
* 78 11176
|
|
|
|
* 79 11188
|
|
|
|
* 80 11228
|
|
|
|
* 81 11244
|
|
|
|
* 82 11410
|
|
|
|
* 83 11589
|
|
|
|
* 84 11822
|
|
|
|
* 85 11874
|
|
|
|
* 86 12042
|
|
|
|
* 87 12129
|
|
|
|
* 88 12134
|
|
|
|
* 89 12160
|
|
|
|
* 90 12293
|
|
|
|
* 91 12347
|
|
|
|
* 92 12381 0.6.x
|
|
|
|
* 93 12648
|
|
|
|
* 94 12816
|
|
|
|
* 95 12924
|
|
|
|
* 96 13226
|
|
|
|
* 97 13256
|
|
|
|
* 98 13375
|
|
|
|
* 99 13838
|
|
|
|
* 100 13952
|
|
|
|
* 101 14233
|
|
|
|
* 102 14332
|
|
|
|
* 103 14598
|
|
|
|
* 104 14735
|
|
|
|
* 105 14803
|
|
|
|
* 106 14919
|
|
|
|
* 107 15027
|
|
|
|
* 108 15045
|
|
|
|
* 109 15075
|
|
|
|
* 110 15148
|
|
|
|
* 111 15190
|
|
|
|
* 112 15290
|
|
|
|
* 113 15340
|
|
|
|
* 114 15601
|
|
|
|
* 115 15695
|
|
|
|
* 116 15893 0.7.x
|
|
|
|
* 117 16037
|
|
|
|
* 118 16129
|
|
|
|
* 119 16242
|
|
|
|
* 120 16439
|
|
|
|
* 121 16694
|
|
|
|
* 122 16855
|
|
|
|
* 123 16909
|
|
|
|
* 124 16993
|
|
|
|
* 125 17113
|
|
|
|
* 126 17433
|
|
|
|
* 127 17439
|
|
|
|
* 128 18281
|
|
|
|
* 129 18292
|
|
|
|
* 130 18404
|
|
|
|
* 131 18481
|
|
|
|
* 132 18522
|
|
|
|
* 133 18674
|
|
|
|
* 134 18703
|
|
|
|
* 135 18719
|
|
|
|
* 136 18764
|
|
|
|
* 137 18912
|
|
|
|
* 138 18942 1.0.x
|
|
|
|
* 139 19346
|
|
|
|
* 140 19382
|
|
|
|
* 141 19799
|
|
|
|
* 142 20003
|
|
|
|
* 143 20048
|
2010-08-03 09:09:45 +01:00
|
|
|
* 144 20334
|
2010-08-05 13:09:08 +01:00
|
|
|
* 145 20376
|
2010-08-11 15:14:06 +01:00
|
|
|
* 146 20446
|
2010-08-26 15:36:00 +01:00
|
|
|
* 147 20621
|
2010-08-28 19:37:49 +01:00
|
|
|
* 148 20659
|
2010-09-20 19:56:35 +01:00
|
|
|
* 149 20832
|
2010-09-28 23:00:24 +01:00
|
|
|
* 150 20857
|
2010-10-12 18:47:15 +01:00
|
|
|
* 151 20918
|
2010-11-13 15:15:25 +00:00
|
|
|
* 152 21171
|
2010-11-20 09:09:57 +00:00
|
|
|
* 153 21263
|
2010-07-28 10:23:42 +01:00
|
|
|
*/
|
2010-11-20 09:09:57 +00:00
|
|
|
extern const uint16 SAVEGAME_VERSION = 153; ///< Current savegame version of OpenTTD.
|
2008-05-05 23:03:01 +01:00
|
|
|
|
|
|
|
SavegameType _savegame_type; ///< type of savegame we are loading
|
|
|
|
|
|
|
|
uint32 _ttdp_version; ///< version of TTDP savegame (if applicable)
|
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!
|
2009-02-25 21:45:14 +00:00
|
|
|
char _savegame_format[8]; ///< how to compress savegames
|
2010-07-19 16:44:49 +01:00
|
|
|
bool _do_autosave; ///< are we doing an autosave at the moment?
|
2005-08-06 18:40:21 +01:00
|
|
|
|
2008-05-27 22:41:00 +01:00
|
|
|
typedef size_t ReaderProc();
|
2005-08-13 22:34:05 +01:00
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
/** What are we currently doing? */
|
|
|
|
enum SaveLoadAction {
|
2010-06-13 15:13:23 +01:00
|
|
|
SLA_LOAD, ///< loading
|
|
|
|
SLA_SAVE, ///< saving
|
|
|
|
SLA_PTRS, ///< fixing pointers
|
|
|
|
SLA_NULL, ///< null all pointers (on loading error)
|
|
|
|
SLA_LOAD_CHECK, ///< partial loading into #_load_check_data
|
2009-05-17 12:15:11 +01:00
|
|
|
};
|
|
|
|
|
2009-05-17 12:43:51 +01:00
|
|
|
enum NeedLength {
|
|
|
|
NL_NONE = 0, ///< not working in NeedLength mode
|
|
|
|
NL_WANTLENGTH = 1, ///< writing length and data
|
|
|
|
NL_CALCLENGTH = 2, ///< need to calculate the length
|
|
|
|
};
|
|
|
|
|
2010-12-02 23:08:22 +00:00
|
|
|
/** Save in chunks of 128 KiB. */
|
|
|
|
static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
|
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/** Interface for filtering a savegame till it is written. */
|
|
|
|
struct SaveFilter {
|
|
|
|
/** Chained to the (savegame) filters. */
|
|
|
|
SaveFilter *chain;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise this filter.
|
|
|
|
* @param chain The next filter in this chain.
|
|
|
|
*/
|
|
|
|
SaveFilter(SaveFilter *chain) : chain(chain)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Make sure the writers are properly closed. */
|
|
|
|
virtual ~SaveFilter()
|
|
|
|
{
|
|
|
|
delete this->chain;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a given number of bytes into the savegame.
|
|
|
|
* @param buf The bytes to write.
|
|
|
|
* @param len The number of bytes to write.
|
|
|
|
*/
|
|
|
|
virtual void Write(byte *buf, size_t len) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare everything to finish writing the savegame.
|
|
|
|
*/
|
|
|
|
virtual void Finish()
|
|
|
|
{
|
|
|
|
if (this->chain != NULL) this->chain->Finish();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiator for a save filter.
|
|
|
|
* @param chain The next filter in this chain.
|
|
|
|
* @param compression_level The requested level of compression.
|
|
|
|
* @tparam T The type of save filter to create.
|
|
|
|
*/
|
|
|
|
template <typename T> SaveFilter *CreateSaveFilter(SaveFilter *chain, byte compression_level)
|
|
|
|
{
|
|
|
|
return new T(chain, compression_level);
|
|
|
|
}
|
|
|
|
|
2010-12-02 23:08:22 +00:00
|
|
|
/** Container for dumping the savegame (quickly) to memory. */
|
|
|
|
struct MemoryDumper {
|
|
|
|
AutoFreeSmallVector<byte *, 16> blocks; ///< Buffer with blocks of allocated memory.
|
|
|
|
byte *buf; ///< Buffer we're going to write to.
|
|
|
|
byte *bufe; ///< End of the buffer we write to.
|
|
|
|
|
|
|
|
/** Initialise our variables. */
|
|
|
|
MemoryDumper() : buf(NULL), bufe(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a single byte into the dumper.
|
|
|
|
* @param b The byte to write.
|
|
|
|
*/
|
|
|
|
FORCEINLINE void WriteByte(byte b)
|
|
|
|
{
|
|
|
|
/* Are we at the end of this chunk? */
|
|
|
|
if (this->buf == this->bufe) {
|
|
|
|
this->buf = CallocT<byte>(MEMORY_CHUNK_SIZE);
|
|
|
|
*this->blocks.Append() = this->buf;
|
|
|
|
this->bufe = this->buf + MEMORY_CHUNK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*this->buf++ = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush this dumper into a writer.
|
2010-12-04 09:09:29 +00:00
|
|
|
* @param writer The filter we want to use.
|
2010-12-02 23:08:22 +00:00
|
|
|
*/
|
2010-12-04 09:09:29 +00:00
|
|
|
void Flush(SaveFilter *writer)
|
2010-12-02 23:08:22 +00:00
|
|
|
{
|
|
|
|
uint i = 0;
|
|
|
|
size_t t = this->GetSize();
|
|
|
|
|
|
|
|
while (t > 0) {
|
|
|
|
size_t to_write = min(MEMORY_CHUNK_SIZE, t);
|
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
writer->Write(this->blocks[i++], to_write);
|
2010-12-02 23:08:22 +00:00
|
|
|
t -= to_write;
|
|
|
|
}
|
2010-12-04 09:09:29 +00:00
|
|
|
|
|
|
|
writer->Finish();
|
2010-12-02 23:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size of the memory dump made so far.
|
|
|
|
* @return The size.
|
|
|
|
*/
|
|
|
|
size_t GetSize()
|
|
|
|
{
|
|
|
|
return this->blocks.Length() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-11-20 13:33:24 +00:00
|
|
|
/** The saveload struct, containing reader-writer functions, buffer, version, etc. */
|
2009-05-17 12:15:11 +01:00
|
|
|
struct SaveLoadParams {
|
|
|
|
SaveLoadAction action; ///< are we doing a save or a load atm.
|
2009-05-17 12:43:51 +01:00
|
|
|
NeedLength need_length; ///< working in NeedLength (Autolength) mode?
|
2007-02-23 01:48:53 +00:00
|
|
|
byte block_mode; ///< ???
|
|
|
|
bool error; ///< did an error occur or not
|
2005-08-06 18:40:21 +01:00
|
|
|
|
2008-05-27 22:41:00 +01:00
|
|
|
size_t obj_len; ///< the length of the current object we are busy with
|
2007-02-23 01:48:53 +00:00
|
|
|
int array_index, last_array_index; ///< in the case of an array, the current and last positions
|
2005-08-06 18:40:21 +01:00
|
|
|
|
2008-05-27 22:41:00 +01:00
|
|
|
size_t offs_base; ///< the offset in number of bytes since we started writing data (eg uncompressed savegame size)
|
2005-08-06 18:40:21 +01:00
|
|
|
|
2010-12-02 23:08:22 +00:00
|
|
|
MemoryDumper *dumper; ///< Memory dumper to write the savegame to.
|
2010-12-04 09:09:29 +00:00
|
|
|
SaveFilter *sf; ///< Filter to write the savegame to.
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
ReaderProc *read_bytes; ///< savegame loader function
|
2005-08-06 18:40:21 +01: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 18:40:21 +01: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 18:40:21 +01: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 18:40:21 +01:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void (*excpt_uninit)(); ///< the function to execute on any encountered error
|
2010-11-20 13:33:24 +00:00
|
|
|
StringID error_str; ///< the translatable error message to show
|
2007-06-24 13:27:11 +01:00
|
|
|
char *extra_msg; ///< the error message
|
2009-05-17 12:15:11 +01:00
|
|
|
};
|
|
|
|
|
2010-01-02 15:48:07 +00:00
|
|
|
/* these define the chunks */
|
|
|
|
extern const ChunkHandler _gamelog_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _map_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _misc_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _name_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _cheat_chunk_handlers[] ;
|
|
|
|
extern const ChunkHandler _setting_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _company_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _engine_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _veh_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _waypoint_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _depot_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _order_chunk_handlers[];
|
|
|
|
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 _subsidy_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _ai_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _animated_tile_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _newgrf_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _group_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _cargopacket_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _autoreplace_chunk_handlers[];
|
|
|
|
extern const ChunkHandler _labelmaps_chunk_handlers[];
|
2010-03-18 23:12:38 +00:00
|
|
|
extern const ChunkHandler _airport_chunk_handlers[];
|
2010-08-13 13:45:26 +01:00
|
|
|
extern const ChunkHandler _object_chunk_handlers[];
|
2010-01-02 15:48:07 +00:00
|
|
|
|
2010-11-21 12:12:23 +00:00
|
|
|
/** Array of all chunks in a savegame, \c NULL terminated. */
|
2010-01-02 15:48:07 +00:00
|
|
|
static const ChunkHandler * const _chunk_handlers[] = {
|
|
|
|
_gamelog_chunk_handlers,
|
|
|
|
_map_chunk_handlers,
|
|
|
|
_misc_chunk_handlers,
|
|
|
|
_name_chunk_handlers,
|
|
|
|
_cheat_chunk_handlers,
|
|
|
|
_setting_chunk_handlers,
|
|
|
|
_veh_chunk_handlers,
|
|
|
|
_waypoint_chunk_handlers,
|
|
|
|
_depot_chunk_handlers,
|
|
|
|
_order_chunk_handlers,
|
|
|
|
_industry_chunk_handlers,
|
|
|
|
_economy_chunk_handlers,
|
|
|
|
_subsidy_chunk_handlers,
|
|
|
|
_engine_chunk_handlers,
|
|
|
|
_town_chunk_handlers,
|
|
|
|
_sign_chunk_handlers,
|
|
|
|
_station_chunk_handlers,
|
|
|
|
_company_chunk_handlers,
|
|
|
|
_ai_chunk_handlers,
|
|
|
|
_animated_tile_chunk_handlers,
|
|
|
|
_newgrf_chunk_handlers,
|
|
|
|
_group_chunk_handlers,
|
|
|
|
_cargopacket_chunk_handlers,
|
|
|
|
_autoreplace_chunk_handlers,
|
|
|
|
_labelmaps_chunk_handlers,
|
2010-03-18 23:12:38 +00:00
|
|
|
_airport_chunk_handlers,
|
2010-08-13 13:45:26 +01:00
|
|
|
_object_chunk_handlers,
|
2010-01-02 15:48:07 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2010-01-02 15:56:20 +00:00
|
|
|
/**
|
|
|
|
* Iterate over all chunk handlers.
|
|
|
|
* @param ch the chunk handler iterator
|
|
|
|
*/
|
|
|
|
#define FOR_ALL_CHUNK_HANDLERS(ch) \
|
|
|
|
for (const ChunkHandler * const *chsc = _chunk_handlers; *chsc != NULL; chsc++) \
|
|
|
|
for (const ChunkHandler *ch = *chsc; ch != NULL; ch = (ch->flags & CH_LAST) ? NULL : ch + 1)
|
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
static SaveLoadParams _sl;
|
2005-08-06 18:40:21 +01:00
|
|
|
|
2009-12-25 23:14:12 +00:00
|
|
|
/** Null all pointers (convert index -> NULL) */
|
|
|
|
static void SlNullPointers()
|
|
|
|
{
|
|
|
|
_sl.action = SLA_NULL;
|
|
|
|
|
|
|
|
DEBUG(sl, 1, "Nulling pointers");
|
|
|
|
|
2010-01-02 15:56:20 +00:00
|
|
|
FOR_ALL_CHUNK_HANDLERS(ch) {
|
|
|
|
if (ch->ptrs_proc != NULL) {
|
|
|
|
DEBUG(sl, 2, "Nulling pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
|
|
|
|
ch->ptrs_proc();
|
2009-12-25 23:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(sl, 1, "All pointers nulled");
|
|
|
|
|
|
|
|
assert(_sl.action == SLA_NULL);
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Error handler. Sets everything up to show an error message and to clean
|
|
|
|
* up the mess of a partial savegame load.
|
|
|
|
* @param string The translatable error message to show.
|
|
|
|
* @param extra_msg An extra error message coming from one of the APIs.
|
|
|
|
* @note This function does never return as it throws an exception to
|
|
|
|
* break out of all the saveload code.
|
|
|
|
*/
|
2008-04-14 21:00:28 +01:00
|
|
|
static void NORETURN SlError(StringID string, const char *extra_msg = NULL)
|
|
|
|
{
|
2010-06-13 15:13:23 +01:00
|
|
|
/* Distinguish between loading into _load_check_data vs. normal save/load. */
|
|
|
|
if (_sl.action == SLA_LOAD_CHECK) {
|
|
|
|
_load_check_data.error = string;
|
|
|
|
free(_load_check_data.error_data);
|
|
|
|
_load_check_data.error_data = (extra_msg == NULL) ? NULL : strdup(extra_msg);
|
|
|
|
} else {
|
|
|
|
_sl.error_str = string;
|
|
|
|
free(_sl.extra_msg);
|
|
|
|
_sl.extra_msg = (extra_msg == NULL) ? NULL : strdup(extra_msg);
|
|
|
|
/* We have to NULL all pointers here; we might be in a state where
|
|
|
|
* the pointers are actually filled with indices, which means that
|
|
|
|
* when we access them during cleaning the pool dereferences of
|
|
|
|
* those indices will be made with segmentation faults as result. */
|
|
|
|
}
|
2010-02-05 20:18:30 +00:00
|
|
|
if (_sl.action == SLA_LOAD || _sl.action == SLA_PTRS) SlNullPointers();
|
2008-04-14 21:00:28 +01:00
|
|
|
throw std::exception();
|
|
|
|
}
|
|
|
|
|
2010-09-16 23:06:36 +01:00
|
|
|
/**
|
|
|
|
* Error handler for corrupt savegames. Sets everything up to show the
|
|
|
|
* error message and to clean up the mess of a partial savegame load.
|
|
|
|
* @param msg Location the corruption has been spotted.
|
|
|
|
* @note This function does never return as it throws an exception to
|
|
|
|
* break out of all the saveload code.
|
|
|
|
*/
|
|
|
|
void NORETURN SlErrorCorrupt(const char *msg)
|
|
|
|
{
|
|
|
|
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
typedef void (*AsyncSaveFinishProc)(); ///< Callback for when the savegame loading is finished.
|
|
|
|
static AsyncSaveFinishProc _async_save_finish = NULL; ///< Callback to call when the savegame loading is finished.
|
|
|
|
static ThreadObject *_save_thread; ///< The thread we're using to compress and write a savegame
|
2008-04-19 11:18:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by save thread to tell we finished saving.
|
2010-07-28 10:48:35 +01:00
|
|
|
* @param proc The callback to call when saving is done.
|
2008-04-19 11:18:38 +01:00
|
|
|
*/
|
|
|
|
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
|
|
|
|
{
|
|
|
|
if (_exit_game) return;
|
|
|
|
while (_async_save_finish != NULL) CSleep(10);
|
|
|
|
|
|
|
|
_async_save_finish = proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle async save finishes.
|
|
|
|
*/
|
|
|
|
void ProcessAsyncSaveFinish()
|
|
|
|
{
|
|
|
|
if (_async_save_finish == NULL) return;
|
|
|
|
|
|
|
|
_async_save_finish();
|
|
|
|
|
|
|
|
_async_save_finish = NULL;
|
2008-06-08 16:27:57 +01:00
|
|
|
|
|
|
|
if (_save_thread != NULL) {
|
|
|
|
_save_thread->Join();
|
|
|
|
delete _save_thread;
|
|
|
|
_save_thread = NULL;
|
|
|
|
}
|
2008-04-19 11:18:38 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 18:04:08 +01:00
|
|
|
{
|
2008-05-27 22:41:00 +01:00
|
|
|
size_t len = _sl.read_bytes();
|
2010-09-16 23:06:36 +01:00
|
|
|
if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
|
2004-08-09 18:04:08 +01:00
|
|
|
|
|
|
|
_sl.bufp = _sl.buf;
|
|
|
|
_sl.bufe = _sl.buf + len;
|
|
|
|
_sl.offs_base += len;
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
static inline size_t SlGetOffs()
|
|
|
|
{
|
|
|
|
return _sl.offs_base - (_sl.bufe - _sl.bufp);
|
|
|
|
}
|
2006-02-20 19:04:18 +00:00
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
|
|
|
* Read in a single byte from file. If the temporary buffer is full,
|
2005-05-30 23:16:05 +01:00
|
|
|
* 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 18:04:08 +01:00
|
|
|
{
|
|
|
|
if (_sl.bufp == _sl.bufe) SlReadFill();
|
|
|
|
return *_sl.bufp++;
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/** Wrapper for SlReadByteInternal */
|
2007-03-07 11:47:46 +00:00
|
|
|
byte SlReadByte() {return SlReadByteInternal();}
|
2005-03-15 21:07:09 +00:00
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
2010-12-02 23:08:22 +00:00
|
|
|
* Wrapper for writing a byte to the dumper.
|
|
|
|
* @param b The byte to write.
|
2005-05-30 23:16:05 +01:00
|
|
|
*/
|
2010-12-02 23:08:22 +00:00
|
|
|
void SlWriteByte(byte b)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2010-12-02 23:08:22 +00:00
|
|
|
_sl.dumper->WriteByte(b);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static inline int SlReadUint16()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
int x = SlReadByte() << 8;
|
|
|
|
return x | SlReadByte();
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static inline uint32 SlReadUint32()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
uint32 x = SlReadUint16() << 16;
|
|
|
|
return x | SlReadUint16();
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static inline uint64 SlReadUint64()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
uint32 x = SlReadUint32();
|
|
|
|
uint32 y = SlReadUint32();
|
|
|
|
return (uint64)x << 32 | y;
|
|
|
|
}
|
|
|
|
|
2005-07-20 20:08:02 +01:00
|
|
|
static inline void SlWriteUint16(uint16 v)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2005-07-21 07:31:02 +01:00
|
|
|
SlWriteByte(GB(v, 8, 8));
|
|
|
|
SlWriteByte(GB(v, 0, 8));
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
static inline void SlWriteUint32(uint32 v)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2005-07-21 07:31:02 +01:00
|
|
|
SlWriteUint16(GB(v, 16, 16));
|
|
|
|
SlWriteUint16(GB(v, 0, 16));
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
static inline void SlWriteUint64(uint64 x)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
SlWriteUint32((uint32)(x >> 32));
|
|
|
|
SlWriteUint32((uint32)x);
|
|
|
|
}
|
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
|
|
|
* Read in bytes from the file/data structure but don't do
|
2010-06-13 15:12:21 +01:00
|
|
|
* anything with them, discarding them in effect
|
|
|
|
* @param length The amount of bytes that is being treated this way
|
|
|
|
*/
|
|
|
|
static inline void SlSkipBytes(size_t length)
|
|
|
|
{
|
|
|
|
for (; length != 0; length--) SlReadByte();
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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-02 00:12:29 +01:00
|
|
|
* 8 bits to the left, and dropping the highest bit (which only indicated a big index).
|
2005-05-30 23:16:05 +01: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 18:04:08 +01:00
|
|
|
{
|
2005-05-30 23:16:05 +01:00
|
|
|
uint i = SlReadByte();
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(i, 7)) {
|
2005-07-12 20:15:56 +01:00
|
|
|
i &= ~0x80;
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(i, 6)) {
|
2005-07-12 20:15:56 +01:00
|
|
|
i &= ~0x40;
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(i, 5)) {
|
2005-07-12 20:15:56 +01:00
|
|
|
i &= ~0x20;
|
2010-07-24 11:14:39 +01:00
|
|
|
if (HasBit(i, 4)) {
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Unsupported gamma");
|
2010-07-24 11:14:39 +01:00
|
|
|
}
|
2005-07-12 20:15:56 +01:00
|
|
|
i = (i << 8) | SlReadByte();
|
|
|
|
}
|
|
|
|
i = (i << 8) | SlReadByte();
|
|
|
|
}
|
|
|
|
i = (i << 8) | SlReadByte();
|
2005-05-30 23:16:05 +01:00
|
|
|
}
|
|
|
|
return i;
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/**
|
|
|
|
* Write the header descriptor of an object or an array.
|
2005-06-02 00:12:29 +01:00
|
|
|
* If the element is bigger than 127, use 2 bytes for saving
|
2005-05-30 23:16:05 +01:00
|
|
|
* and use the highest byte of the first written one as a notice
|
2005-07-12 20:15:56 +01:00
|
|
|
* that the length consists of 2 bytes, etc.. like this:
|
|
|
|
* 0xxxxxxx
|
|
|
|
* 10xxxxxx xxxxxxxx
|
|
|
|
* 110xxxxx xxxxxxxx xxxxxxxx
|
|
|
|
* 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
|
2005-05-30 23:16:05 +01:00
|
|
|
* @param i Index being written
|
|
|
|
*/
|
2005-07-12 20:15:56 +01:00
|
|
|
|
2008-05-27 22:41:00 +01:00
|
|
|
static void SlWriteSimpleGamma(size_t i)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2005-05-30 23:16:05 +01:00
|
|
|
if (i >= (1 << 7)) {
|
2005-07-12 20:15:56 +01:00
|
|
|
if (i >= (1 << 14)) {
|
|
|
|
if (i >= (1 << 21)) {
|
|
|
|
assert(i < (1 << 28));
|
2008-05-27 22:41:00 +01:00
|
|
|
SlWriteByte((byte)(0xE0 | (i >> 24)));
|
2007-04-18 23:10:36 +01:00
|
|
|
SlWriteByte((byte)(i >> 16));
|
2005-07-12 20:15:56 +01:00
|
|
|
} else {
|
2008-05-27 22:41:00 +01:00
|
|
|
SlWriteByte((byte)(0xC0 | (i >> 16)));
|
2005-07-12 20:15:56 +01:00
|
|
|
}
|
2007-04-18 23:10:36 +01:00
|
|
|
SlWriteByte((byte)(i >> 8));
|
2005-07-12 20:15:56 +01:00
|
|
|
} else {
|
2007-04-18 23:10:36 +01:00
|
|
|
SlWriteByte((byte)(0x80 | (i >> 8)));
|
2005-07-12 20:15:56 +01:00
|
|
|
}
|
|
|
|
}
|
2008-05-27 22:41:00 +01:00
|
|
|
SlWriteByte((byte)i);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-07-12 20:15:56 +01:00
|
|
|
/** Return how many bytes used to encode a gamma value */
|
2008-05-27 22:41:00 +01:00
|
|
|
static inline uint SlGetGammaLength(size_t i)
|
2007-07-25 01:16:30 +01:00
|
|
|
{
|
2005-07-12 20:15:56 +01:00
|
|
|
return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21));
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
static inline uint SlReadSparseIndex()
|
|
|
|
{
|
|
|
|
return SlReadSimpleGamma();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void SlWriteSparseIndex(uint index)
|
|
|
|
{
|
|
|
|
SlWriteSimpleGamma(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint SlReadArrayLength()
|
|
|
|
{
|
|
|
|
return SlReadSimpleGamma();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void SlWriteArrayLength(size_t length)
|
|
|
|
{
|
|
|
|
SlWriteSimpleGamma(length);
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
static inline uint SlGetArrayLength(size_t length)
|
|
|
|
{
|
|
|
|
return SlGetGammaLength(length);
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Return the size in bytes of a certain type of normal/atomic variable
|
2010-07-28 10:47:31 +01:00
|
|
|
* as it appears in memory. See VarTypes
|
|
|
|
* @param conv VarType type of variable that is used for calculating the size
|
2010-07-28 10:48:35 +01:00
|
|
|
* @return Return the size of this type in bytes
|
|
|
|
*/
|
2010-07-28 10:47:31 +01:00
|
|
|
static inline uint SlCalcConvMemLen(VarType conv)
|
|
|
|
{
|
|
|
|
static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
|
|
|
|
byte length = GB(conv, 4, 4);
|
|
|
|
|
|
|
|
switch (length << 4) {
|
|
|
|
case SLE_VAR_STRB:
|
|
|
|
case SLE_VAR_STRBQ:
|
|
|
|
case SLE_VAR_STR:
|
|
|
|
case SLE_VAR_STRQ:
|
|
|
|
return SlReadArrayLength();
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(length < lengthof(conv_mem_size));
|
|
|
|
return conv_mem_size[length];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Return the size in bytes of a certain type of normal/atomic variable
|
2010-07-28 10:47:31 +01:00
|
|
|
* as it appears in a saved game. See VarTypes
|
|
|
|
* @param conv VarType type of variable that is used for calculating the size
|
2010-07-28 10:48:35 +01:00
|
|
|
* @return Return the size of this type in bytes
|
|
|
|
*/
|
2010-07-28 10:47:31 +01:00
|
|
|
static inline byte SlCalcConvFileLen(VarType conv)
|
|
|
|
{
|
|
|
|
static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
|
|
|
|
byte length = GB(conv, 0, 4);
|
|
|
|
assert(length < lengthof(conv_file_size));
|
|
|
|
return conv_file_size[length];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the size in bytes of a reference (pointer) */
|
2010-07-28 10:48:35 +01:00
|
|
|
static inline size_t SlCalcRefLen()
|
|
|
|
{
|
2010-11-21 12:47:04 +00:00
|
|
|
return IsSavegameVersionBefore(69) ? 2 : 4;
|
2010-07-28 10:48:35 +01:00
|
|
|
}
|
2010-07-28 10:47:31 +01:00
|
|
|
|
2004-08-09 18:04:08 +01:00
|
|
|
void SlSetArrayIndex(uint index)
|
|
|
|
{
|
|
|
|
_sl.need_length = NL_WANTLENGTH;
|
|
|
|
_sl.array_index = index;
|
|
|
|
}
|
|
|
|
|
2008-05-27 22:41:00 +01:00
|
|
|
static size_t _next_offs;
|
2007-06-28 00:26:40 +01:00
|
|
|
|
2005-05-30 23:16:05 +01: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 18:04:08 +01:00
|
|
|
{
|
2005-05-30 23:16:05 +01:00
|
|
|
int index;
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2005-05-30 23:16:05 +01: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. */
|
2010-09-16 23:06:36 +01:00
|
|
|
if (_next_offs != 0 && SlGetOffs() != _next_offs) SlErrorCorrupt("Invalid chunk size");
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
while (true) {
|
|
|
|
uint length = SlReadArrayLength();
|
|
|
|
if (length == 0) {
|
2007-06-28 00:26:40 +01:00
|
|
|
_next_offs = 0;
|
2004-08-09 18:04:08 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
_sl.obj_len = --length;
|
2007-06-28 00:26:40 +01:00
|
|
|
_next_offs = SlGetOffs() + length;
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
switch (_sl.block_mode) {
|
2009-05-17 12:47:56 +01:00
|
|
|
case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
|
|
|
|
case CH_ARRAY: index = _sl.array_index++; break;
|
|
|
|
default:
|
|
|
|
DEBUG(sl, 0, "SlIterateArray error");
|
|
|
|
return -1; // error
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
if (length != 0) return index;
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 15:12:21 +01:00
|
|
|
/**
|
|
|
|
* Skip an array or sparse array
|
|
|
|
*/
|
|
|
|
void SlSkipArray()
|
|
|
|
{
|
|
|
|
while (SlIterateArray() != -1) {
|
|
|
|
SlSkipBytes(_next_offs - SlGetOffs());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 18:04:08 +01:00
|
|
|
{
|
2009-05-17 12:15:11 +01:00
|
|
|
assert(_sl.action == SLA_SAVE);
|
2006-02-20 19:04:18 +00:00
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
switch (_sl.need_length) {
|
2009-05-17 12:43:51 +01:00
|
|
|
case NL_WANTLENGTH:
|
|
|
|
_sl.need_length = NL_NONE;
|
|
|
|
switch (_sl.block_mode) {
|
|
|
|
case CH_RIFF:
|
|
|
|
/* Ugly encoding of >16M RIFF chunks
|
2009-11-03 17:30:08 +00:00
|
|
|
* The lower 24 bits are normal
|
|
|
|
* The uppermost 4 bits are bits 24:27 */
|
2009-05-17 12:43:51 +01:00
|
|
|
assert(length < (1 << 28));
|
|
|
|
SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
|
|
|
|
break;
|
|
|
|
case CH_ARRAY:
|
|
|
|
assert(_sl.last_array_index <= _sl.array_index);
|
2010-07-24 11:14:39 +01:00
|
|
|
while (++_sl.last_array_index <= _sl.array_index) {
|
2009-05-17 12:43:51 +01:00
|
|
|
SlWriteArrayLength(1);
|
2010-07-24 11:14:39 +01:00
|
|
|
}
|
2009-05-17 12:43:51 +01:00
|
|
|
SlWriteArrayLength(length + 1);
|
|
|
|
break;
|
|
|
|
case CH_SPARSE_ARRAY:
|
|
|
|
SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
|
|
|
|
SlWriteSparseIndex(_sl.array_index);
|
|
|
|
break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
break;
|
2009-05-17 12:43:51 +01:00
|
|
|
|
|
|
|
case NL_CALCLENGTH:
|
|
|
|
_sl.obj_len += (int)length;
|
2004-08-09 18:04:08 +01:00
|
|
|
break;
|
2009-05-17 12:43:51 +01:00
|
|
|
|
2004-08-09 18:04:08 +01:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 18:04:08 +01:00
|
|
|
{
|
2009-05-17 12:15:11 +01:00
|
|
|
byte *p = (byte *)ptr;
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
switch (_sl.action) {
|
2010-06-13 15:13:23 +01:00
|
|
|
case SLA_LOAD_CHECK:
|
2009-05-17 12:15:11 +01:00
|
|
|
case SLA_LOAD:
|
2010-07-28 10:48:35 +01:00
|
|
|
for (; length != 0; length--) *p++ = SlReadByteInternal();
|
2009-05-17 12:15:11 +01:00
|
|
|
break;
|
|
|
|
case SLA_SAVE:
|
2010-12-02 23:08:22 +00:00
|
|
|
for (; length != 0; length--) SlWriteByte(*p++);
|
2009-05-17 12:15:11 +01:00
|
|
|
break;
|
|
|
|
default: NOT_REACHED();
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/** Get the length of the current object */
|
|
|
|
size_t SlGetFieldLength()
|
|
|
|
{
|
|
|
|
return _sl.obj_len;
|
|
|
|
}
|
2005-05-30 23:16:05 +01:00
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Return a signed-long version of the value of a setting
|
2006-03-01 20:34:51 +00:00
|
|
|
* @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
|
2010-07-28 10:48:35 +01:00
|
|
|
* @return returns the value of the pointer-setting
|
|
|
|
*/
|
2006-03-01 20:34:51 +00:00
|
|
|
int64 ReadValue(const void *ptr, VarType conv)
|
|
|
|
{
|
|
|
|
switch (GetVarMemType(conv)) {
|
2009-05-17 12:47:56 +01:00
|
|
|
case SLE_VAR_BL: return (*(bool *)ptr != 0);
|
|
|
|
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();
|
2006-03-01 20:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Write the value of a setting
|
2006-03-01 20:34:51 +00:00
|
|
|
* @param ptr pointer to the variable
|
|
|
|
* @param conv type of variable, can be a non-clean type, eg
|
2007-04-04 02:35:16 +01:00
|
|
|
* with other flags. It is parsed upon read
|
2010-07-28 10:48:35 +01:00
|
|
|
* @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)) {
|
2009-05-17 12:47:56 +01:00
|
|
|
case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
|
|
|
|
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;
|
|
|
|
case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
|
|
|
|
case SLE_VAR_NULL: break;
|
|
|
|
default: NOT_REACHED();
|
2006-03-01 20:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 23:16:05 +01:00
|
|
|
*/
|
|
|
|
static void SlSaveLoadConv(void *ptr, VarType conv)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2009-05-17 12:15:11 +01:00
|
|
|
switch (_sl.action) {
|
|
|
|
case SLA_SAVE: {
|
|
|
|
int64 x = ReadValue(ptr, conv);
|
|
|
|
|
|
|
|
/* Write the value to the file and check if its value is in the desired range */
|
|
|
|
switch (GetVarFileType(conv)) {
|
|
|
|
case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
|
|
|
|
case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
|
|
|
|
case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
|
|
|
|
case SLE_FILE_STRINGID:
|
|
|
|
case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
|
|
|
|
case SLE_FILE_I32:
|
|
|
|
case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
|
|
|
|
case SLE_FILE_I64:
|
|
|
|
case SLE_FILE_U64: SlWriteUint64(x);break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
break;
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
2010-06-13 15:13:23 +01:00
|
|
|
case SLA_LOAD_CHECK:
|
2009-05-17 12:15:11 +01:00
|
|
|
case SLA_LOAD: {
|
|
|
|
int64 x;
|
|
|
|
/* 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;
|
|
|
|
case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
|
|
|
|
case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
|
|
|
|
case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
|
|
|
|
case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
|
|
|
|
case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
|
|
|
|
case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
/* Write The value to the struct. These ARE endian safe. */
|
|
|
|
WriteValue(ptr, conv, x);
|
|
|
|
break;
|
|
|
|
}
|
2009-05-17 17:28:29 +01:00
|
|
|
case SLA_PTRS: break;
|
2009-12-25 23:14:12 +00:00
|
|
|
case SLA_NULL: break;
|
2009-05-17 12:15:11 +01:00
|
|
|
default: NOT_REACHED();
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Calculate the net length of a string. This is in almost all cases
|
2006-04-11 18:05:50 +01:00
|
|
|
* 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.
|
2010-07-28 10:48:35 +01: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 18:05:50 +01:00
|
|
|
{
|
2007-07-06 09:32:00 +01:00
|
|
|
if (ptr == NULL) return 0;
|
2008-05-08 21:05:32 +01:00
|
|
|
return min(strlen(ptr), length - 1);
|
2006-04-11 18:05:50 +01:00
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Calculate the gross length of the string that it
|
2006-04-11 18:05:50 +01:00
|
|
|
* 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 02:35:16 +01:00
|
|
|
* @param conv type of data been used
|
2010-07-28 10:48:35 +01: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 18:05:50 +01: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 02:35:16 +01:00
|
|
|
* @param length of the string (full length)
|
2010-07-28 10:48:35 +01:00
|
|
|
* @param conv must be SLE_FILE_STRING
|
|
|
|
*/
|
2006-11-21 16:54:16 +00:00
|
|
|
static void SlString(void *ptr, size_t length, VarType conv)
|
|
|
|
{
|
2009-05-17 12:15:11 +01:00
|
|
|
switch (_sl.action) {
|
|
|
|
case SLA_SAVE: {
|
|
|
|
size_t len;
|
|
|
|
switch (GetVarMemType(conv)) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case SLE_VAR_STRB:
|
|
|
|
case SLE_VAR_STRBQ:
|
|
|
|
len = SlCalcNetStringLen((char *)ptr, length);
|
|
|
|
break;
|
|
|
|
case SLE_VAR_STR:
|
|
|
|
case SLE_VAR_STRQ:
|
|
|
|
ptr = *(char **)ptr;
|
|
|
|
len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
|
|
|
|
break;
|
|
|
|
}
|
2006-11-21 16:54:16 +00:00
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
SlWriteArrayLength(len);
|
|
|
|
SlCopyBytes(ptr, len);
|
|
|
|
break;
|
2006-11-21 16:54:16 +00:00
|
|
|
}
|
2010-06-13 15:13:23 +01:00
|
|
|
case SLA_LOAD_CHECK:
|
2009-05-17 12:15:11 +01:00
|
|
|
case SLA_LOAD: {
|
|
|
|
size_t len = SlReadArrayLength();
|
|
|
|
|
|
|
|
switch (GetVarMemType(conv)) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case SLE_VAR_STRB:
|
|
|
|
case SLE_VAR_STRBQ:
|
|
|
|
if (len >= length) {
|
|
|
|
DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
|
|
|
|
SlCopyBytes(ptr, length);
|
|
|
|
SlSkipBytes(len - length);
|
|
|
|
len = length - 1;
|
|
|
|
} else {
|
|
|
|
SlCopyBytes(ptr, len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SLE_VAR_STR:
|
|
|
|
case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
|
|
|
|
free(*(char **)ptr);
|
|
|
|
if (len == 0) {
|
|
|
|
*(char **)ptr = NULL;
|
|
|
|
} else {
|
|
|
|
*(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
|
|
|
|
ptr = *(char **)ptr;
|
|
|
|
SlCopyBytes(ptr, len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-04-11 18:05:50 +01:00
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
((char *)ptr)[len] = '\0'; // properly terminate the string
|
|
|
|
str_validate((char *)ptr, (char *)ptr + len);
|
|
|
|
break;
|
2006-11-21 16:54:16 +00:00
|
|
|
}
|
2009-05-17 17:28:29 +01:00
|
|
|
case SLA_PTRS: break;
|
2009-12-25 23:14:12 +00:00
|
|
|
case SLA_NULL: break;
|
2009-05-17 12:15:11 +01:00
|
|
|
default: NOT_REACHED();
|
2006-04-11 18:05:50 +01:00
|
|
|
}
|
2006-02-20 19:58:46 +00:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 23:16:05 +01:00
|
|
|
*/
|
2008-05-27 22:41:00 +01:00
|
|
|
static inline size_t SlCalcArrayLen(size_t length, VarType conv)
|
2006-02-02 07:15:46 +00:00
|
|
|
{
|
2006-02-20 19:04:18 +00:00
|
|
|
return SlCalcConvFileLen(conv) * length;
|
2006-02-02 07:15:46 +00:00
|
|
|
}
|
2005-05-30 23:16:05 +01: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 23:16:05 +01:00
|
|
|
*/
|
2008-05-27 22:41:00 +01:00
|
|
|
void SlArray(void *array, size_t length, VarType conv)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2009-12-25 23:14:12 +00:00
|
|
|
if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
|
2009-05-17 17:28:29 +01:00
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Automatically calculate the length? */
|
2004-08-09 18:04:08 +01: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 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 */
|
2009-05-17 12:15:11 +01:00
|
|
|
if (_sl.action != SLA_SAVE && _sl_version == 0) {
|
2008-06-03 12:39:15 +01:00
|
|
|
/* all arrays except difficulty settings */
|
2006-02-20 19:04:18 +00:00
|
|
|
if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
|
2006-06-27 22:25:53 +01:00
|
|
|
conv == SLE_INT32 || conv == SLE_UINT32) {
|
2008-06-03 12:39:15 +01:00
|
|
|
SlCopyBytes(array, length * SlCalcConvFileLen(conv));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* used for conversion of Money 32bit->64bit */
|
|
|
|
if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
|
|
|
|
for (uint i = 0; i < length; i++) {
|
|
|
|
((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
|
|
|
|
}
|
|
|
|
return;
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 23:16:05 +01:00
|
|
|
if (conv == SLE_INT8 || conv == SLE_UINT8) {
|
|
|
|
SlCopyBytes(array, length);
|
|
|
|
} else {
|
2004-08-09 18:04:08 +01:00
|
|
|
byte *a = (byte*)array;
|
2006-02-20 19:04:18 +00:00
|
|
|
byte mem_size = SlCalcConvMemLen(conv);
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
for (; length != 0; length --) {
|
2004-08-09 18:04:08 +01:00
|
|
|
SlSaveLoadConv(a, conv);
|
2006-02-20 19:04:18 +00:00
|
|
|
a += mem_size; // get size
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-20 08:51:20 +01:00
|
|
|
|
2010-07-28 10:47:31 +01: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
|
2010-11-01 22:12:12 +00:00
|
|
|
* indices have +1, so vehicle 0 is saved as index 1.
|
2010-07-28 10:47:31 +01:00
|
|
|
* @param obj The object that we want to get the index of
|
|
|
|
* @param rt SLRefType type of the object the index is being sought of
|
|
|
|
* @return Return the pointer converted to an index of the type pointed to
|
|
|
|
*/
|
|
|
|
static size_t ReferenceToInt(const void *obj, SLRefType rt)
|
|
|
|
{
|
|
|
|
assert(_sl.action == SLA_SAVE);
|
|
|
|
|
|
|
|
if (obj == NULL) return 0;
|
|
|
|
|
|
|
|
switch (rt) {
|
|
|
|
case REF_VEHICLE_OLD: // Old vehicles we save as new onces
|
|
|
|
case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
|
|
|
|
case REF_STATION: return ((const Station*)obj)->index + 1;
|
|
|
|
case REF_TOWN: return ((const Town*)obj)->index + 1;
|
|
|
|
case REF_ORDER: return ((const Order*)obj)->index + 1;
|
|
|
|
case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
|
|
|
|
case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
|
|
|
|
case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
|
|
|
|
case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2010-11-01 22:12:12 +00:00
|
|
|
* Remember that an index of 0 is a NULL pointer so all indices
|
2010-07-28 10:47:31 +01:00
|
|
|
* are +1 so vehicle 0 is saved as 1.
|
|
|
|
* @param index The index that is being converted to a pointer
|
|
|
|
* @param rt SLRefType type of the object the pointer is sought of
|
|
|
|
* @return Return the index converted to a pointer of any type
|
|
|
|
*/
|
|
|
|
static void *IntToReference(size_t index, SLRefType rt)
|
|
|
|
{
|
|
|
|
assert_compile(sizeof(size_t) <= sizeof(void *));
|
|
|
|
|
|
|
|
assert(_sl.action == SLA_PTRS);
|
|
|
|
|
|
|
|
/* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
|
|
|
|
* and should be loaded like that */
|
2010-11-21 12:31:16 +00:00
|
|
|
if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(4, 4)) {
|
2010-07-28 10:47:31 +01:00
|
|
|
rt = REF_VEHICLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No need to look up NULL pointers, just return immediately */
|
|
|
|
if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return NULL;
|
|
|
|
|
|
|
|
/* Correct index. Old vehicles were saved differently:
|
|
|
|
* invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
|
|
|
|
if (rt != REF_VEHICLE_OLD) index--;
|
|
|
|
|
|
|
|
switch (rt) {
|
|
|
|
case REF_ORDERLIST:
|
|
|
|
if (OrderList::IsValidID(index)) return OrderList::Get(index);
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Referencing invalid OrderList");
|
2010-07-28 10:47:31 +01:00
|
|
|
|
|
|
|
case REF_ORDER:
|
|
|
|
if (Order::IsValidID(index)) return Order::Get(index);
|
|
|
|
/* in old versions, invalid order was used to mark end of order list */
|
2010-11-21 12:31:16 +00:00
|
|
|
if (IsSavegameVersionBefore(5, 2)) return NULL;
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Referencing invalid Order");
|
2010-07-28 10:47:31 +01:00
|
|
|
|
|
|
|
case REF_VEHICLE_OLD:
|
|
|
|
case REF_VEHICLE:
|
|
|
|
if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Referencing invalid Vehicle");
|
2010-07-28 10:47:31 +01:00
|
|
|
|
|
|
|
case REF_STATION:
|
|
|
|
if (Station::IsValidID(index)) return Station::Get(index);
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Referencing invalid Station");
|
2010-07-28 10:47:31 +01:00
|
|
|
|
|
|
|
case REF_TOWN:
|
|
|
|
if (Town::IsValidID(index)) return Town::Get(index);
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Referencing invalid Town");
|
2010-07-28 10:47:31 +01:00
|
|
|
|
|
|
|
case REF_ROADSTOPS:
|
|
|
|
if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Referencing invalid RoadStop");
|
2007-04-20 08:51:20 +01:00
|
|
|
|
2010-07-28 10:47:31 +01:00
|
|
|
case REF_ENGINE_RENEWS:
|
|
|
|
if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Referencing invalid EngineRenew");
|
2010-07-28 10:47:31 +01:00
|
|
|
|
|
|
|
case REF_CARGO_PACKET:
|
|
|
|
if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Referencing invalid CargoPacket");
|
2010-07-28 10:47:31 +01:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
2007-04-20 08:51:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2010-11-21 12:47:04 +00:00
|
|
|
int type_size = IsSavegameVersionBefore(69) ? 2 : 4;
|
2007-06-25 11:10:37 +01:00
|
|
|
/* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
|
2007-04-20 08:51:20 +01:00
|
|
|
* of the list */
|
2007-06-25 11:10:37 +01:00
|
|
|
return l->size() * type_size + type_size;
|
2007-04-20 08:51:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save/Load a list.
|
|
|
|
* @param list The list being manipulated
|
|
|
|
* @param conv SLRefType type of the list (Vehicle *, Station *, etc)
|
|
|
|
*/
|
2009-11-09 10:40:33 +00:00
|
|
|
static void SlList(void *list, SLRefType conv)
|
2007-04-20 08:51:20 +01:00
|
|
|
{
|
|
|
|
/* Automatically calculate the length? */
|
|
|
|
if (_sl.need_length != NL_NONE) {
|
|
|
|
SlSetLength(SlCalcListLen(list));
|
|
|
|
/* Determine length only? */
|
|
|
|
if (_sl.need_length == NL_CALCLENGTH) return;
|
|
|
|
}
|
|
|
|
|
2009-05-17 17:28:29 +01:00
|
|
|
typedef std::list<void *> PtrList;
|
|
|
|
PtrList *l = (PtrList *)list;
|
2007-04-20 08:51:20 +01:00
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
switch (_sl.action) {
|
|
|
|
case SLA_SAVE: {
|
|
|
|
SlWriteUint32((uint32)l->size());
|
2007-04-20 08:51:20 +01:00
|
|
|
|
2009-05-17 17:28:29 +01:00
|
|
|
PtrList::iterator iter;
|
2009-05-17 12:15:11 +01:00
|
|
|
for (iter = l->begin(); iter != l->end(); ++iter) {
|
|
|
|
void *ptr = *iter;
|
2009-05-22 16:13:50 +01:00
|
|
|
SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
|
2009-05-17 12:15:11 +01:00
|
|
|
}
|
|
|
|
break;
|
2007-04-20 08:51:20 +01:00
|
|
|
}
|
2010-06-13 15:13:23 +01:00
|
|
|
case SLA_LOAD_CHECK:
|
2009-05-17 12:15:11 +01:00
|
|
|
case SLA_LOAD: {
|
2010-11-21 12:47:04 +00:00
|
|
|
size_t length = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
|
2007-04-20 08:51:20 +01:00
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
/* Load each reference and push to the end of the list */
|
2009-05-22 16:13:50 +01:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2010-11-21 12:47:04 +00:00
|
|
|
size_t data = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
|
2009-05-17 17:28:29 +01:00
|
|
|
l->push_back((void *)data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SLA_PTRS: {
|
|
|
|
PtrList temp = *l;
|
|
|
|
|
|
|
|
l->clear();
|
|
|
|
PtrList::iterator iter;
|
|
|
|
for (iter = temp.begin(); iter != temp.end(); ++iter) {
|
|
|
|
void *ptr = IntToReference((size_t)*iter, conv);
|
2009-05-17 12:15:11 +01:00
|
|
|
l->push_back(ptr);
|
|
|
|
}
|
|
|
|
break;
|
2007-04-20 08:51:20 +01:00
|
|
|
}
|
2009-12-25 23:14:12 +00:00
|
|
|
case SLA_NULL:
|
|
|
|
l->clear();
|
|
|
|
break;
|
2009-05-17 12:15:11 +01:00
|
|
|
default: NOT_REACHED();
|
2007-04-20 08:51:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
|
|
|
* Are we going to load this variable when loading a savegame or not?
|
2006-03-02 00:32:48 +00:00
|
|
|
* @note If the variable is skipped it is skipped in the savegame
|
2010-08-01 20:44:49 +01:00
|
|
|
* bytestream itself as well, so there is no need to skip it somewhere else
|
|
|
|
*/
|
2006-03-02 00:32:48 +00:00
|
|
|
static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
|
|
|
|
{
|
2009-05-17 12:15:11 +01:00
|
|
|
if ((sld->conv & SLF_NETWORK_NO) && _sl.action != SLA_SAVE && _networking && !_network_server) {
|
2006-03-02 00:32:48 +00:00
|
|
|
SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/**
|
|
|
|
* Calculate the size of an object.
|
2007-04-04 02:35:16 +01: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 02:35:16 +01:00
|
|
|
* @return size of given objetc
|
2005-05-30 23:16:05 +01:00
|
|
|
*/
|
2008-06-03 19:35:58 +01:00
|
|
|
size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
|
2004-08-09 18:04:08 +01: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 23:16:05 +01: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
|
|
|
{
|
2009-05-17 12:15:11 +01:00
|
|
|
assert(_sl.action == SLA_SAVE);
|
2006-02-20 19:42:39 +00:00
|
|
|
|
|
|
|
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 08:51:20 +01: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 18:04:08 +01:00
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
switch (sld->cmd) {
|
2009-05-17 12:15:11 +01:00
|
|
|
case SL_VAR: return SlCalcConvFileLen(sld->conv);
|
|
|
|
case SL_REF: return SlCalcRefLen();
|
|
|
|
case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
|
|
|
|
case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
|
|
|
|
case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
|
|
|
|
default: NOT_REACHED();
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
2006-02-20 19:42:39 +00:00
|
|
|
break;
|
2007-06-07 13:37:48 +01:00
|
|
|
case SL_WRITEBYTE: return 1; // a byte is logically of size 1
|
2007-08-30 14:09:44 +01:00
|
|
|
case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
|
2009-07-22 09:59:57 +01:00
|
|
|
case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
|
2006-02-20 19:42:39 +00:00
|
|
|
default: NOT_REACHED();
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
2006-02-20 19:42:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-18 08:49:25 +01: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) {
|
2009-05-17 12:15:11 +01:00
|
|
|
case SL_VAR:
|
|
|
|
case SL_REF:
|
|
|
|
case SL_ARR:
|
|
|
|
case SL_STR:
|
|
|
|
case SL_LST:
|
|
|
|
/* CONDITIONAL saveload types depend on the savegame version */
|
|
|
|
if (!SlIsObjectValidInSavegame(sld)) return false;
|
|
|
|
if (SlSkipVariableOnLoad(sld)) return false;
|
|
|
|
|
|
|
|
switch (sld->cmd) {
|
|
|
|
case SL_VAR: SlSaveLoadConv(ptr, conv); break;
|
|
|
|
case SL_REF: // Reference variable, translate
|
|
|
|
switch (_sl.action) {
|
|
|
|
case SLA_SAVE:
|
2009-05-22 16:13:50 +01:00
|
|
|
SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
|
2009-05-17 12:15:11 +01:00
|
|
|
break;
|
2010-06-13 15:13:23 +01:00
|
|
|
case SLA_LOAD_CHECK:
|
2009-05-17 12:15:11 +01:00
|
|
|
case SLA_LOAD:
|
2010-11-21 12:47:04 +00:00
|
|
|
*(size_t *)ptr = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
|
2009-05-17 17:28:29 +01:00
|
|
|
break;
|
|
|
|
case SLA_PTRS:
|
|
|
|
*(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
|
2009-05-17 12:15:11 +01:00
|
|
|
break;
|
2009-12-25 23:14:12 +00:00
|
|
|
case SLA_NULL:
|
|
|
|
*(void **)ptr = NULL;
|
|
|
|
break;
|
2009-05-17 12:15:11 +01:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SL_ARR: SlArray(ptr, sld->length, conv); break;
|
|
|
|
case SL_STR: SlString(ptr, sld->length, conv); break;
|
|
|
|
case SL_LST: SlList(ptr, (SLRefType)conv); break;
|
|
|
|
default: NOT_REACHED();
|
2006-06-18 08:49:25 +01:00
|
|
|
}
|
2006-02-20 19:42:39 +00:00
|
|
|
break;
|
|
|
|
|
2009-05-17 12:15:11 +01: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:
|
|
|
|
switch (_sl.action) {
|
|
|
|
case SLA_SAVE: SlWriteByte(sld->version_to); break;
|
2010-06-13 15:13:23 +01:00
|
|
|
case SLA_LOAD_CHECK:
|
2009-05-17 12:15:11 +01:00
|
|
|
case SLA_LOAD: *(byte *)ptr = sld->version_from; break;
|
2009-05-17 17:28:29 +01:00
|
|
|
case SLA_PTRS: break;
|
2009-12-25 23:14:12 +00:00
|
|
|
case SLA_NULL: break;
|
2009-05-17 12:15:11 +01:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
break;
|
2007-06-07 13:37:48 +01:00
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
/* SL_VEH_INCLUDE loads common code for vehicles */
|
|
|
|
case SL_VEH_INCLUDE:
|
|
|
|
SlObject(ptr, GetVehicleDescription(VEH_END));
|
|
|
|
break;
|
|
|
|
|
2009-07-22 09:59:57 +01:00
|
|
|
case SL_ST_INCLUDE:
|
|
|
|
SlObject(ptr, GetBaseStationDescription());
|
|
|
|
break;
|
|
|
|
|
2009-05-17 12:15:11 +01:00
|
|
|
default: NOT_REACHED();
|
2006-02-20 19:42:39 +00:00
|
|
|
}
|
|
|
|
return true;
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 23:16:05 +01:00
|
|
|
*/
|
|
|
|
void SlObject(void *object, const SaveLoad *sld)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Automatically calculate the length? */
|
2004-08-09 18:04:08 +01: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 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
for (; sld->cmd != SL_END; sld++) {
|
2007-06-22 12:58:59 +01:00
|
|
|
void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld);
|
2006-02-20 19:42:39 +00:00
|
|
|
SlObjectMember(ptr, sld);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/**
|
|
|
|
* Save or Load (a list of) global variables
|
2007-04-04 02:35:16 +01:00
|
|
|
* @param sldg The global variable that is being loaded or saved
|
2005-05-30 23:16:05 +01:00
|
|
|
*/
|
2006-02-20 19:42:39 +00:00
|
|
|
void SlGlobList(const SaveLoadGlobVarList *sldg)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2007-06-22 12:58:59 +01:00
|
|
|
SlObject(NULL, (const SaveLoad*)sldg);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 18:04:08 +01:00
|
|
|
void SlAutolength(AutolengthProc *proc, void *arg)
|
|
|
|
{
|
2008-05-27 22:41:00 +01:00
|
|
|
size_t offs;
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2009-05-17 12:43:51 +01:00
|
|
|
assert(_sl.action == SLA_SAVE);
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Tell it to calculate the length */
|
2004-08-09 18:04:08 +01: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 18:04:08 +01:00
|
|
|
_sl.need_length = NL_WANTLENGTH;
|
|
|
|
SlSetLength(_sl.obj_len);
|
|
|
|
|
2010-12-02 23:08:22 +00:00
|
|
|
offs = _sl.dumper->GetSize() + _sl.obj_len;
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* And write the stuff */
|
2004-08-09 18:04:08 +01:00
|
|
|
proc(arg);
|
|
|
|
|
2010-12-02 23:08:22 +00:00
|
|
|
if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/**
|
|
|
|
* Load a chunk of data (eg vehicles, stations, etc.)
|
|
|
|
* @param ch The chunkhandler that will be used for the operation
|
|
|
|
*/
|
2004-08-09 18:04:08 +01:00
|
|
|
static void SlLoadChunk(const ChunkHandler *ch)
|
|
|
|
{
|
|
|
|
byte m = SlReadByte();
|
2008-05-27 22:41:00 +01:00
|
|
|
size_t len;
|
|
|
|
size_t endoffs;
|
2004-08-09 18:04:08 +01:00
|
|
|
|
|
|
|
_sl.block_mode = m;
|
|
|
|
_sl.obj_len = 0;
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
switch (m) {
|
2009-05-17 12:47:56 +01:00
|
|
|
case CH_ARRAY:
|
|
|
|
_sl.array_index = 0;
|
2005-07-12 20:15:56 +01:00
|
|
|
ch->load_proc();
|
2009-05-17 12:47:56 +01:00
|
|
|
break;
|
|
|
|
case CH_SPARSE_ARRAY:
|
|
|
|
ch->load_proc();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ((m & 0xF) == CH_RIFF) {
|
|
|
|
/* Read length */
|
|
|
|
len = (SlReadByte() << 16) | ((m >> 4) << 24);
|
|
|
|
len += SlReadUint16();
|
|
|
|
_sl.obj_len = len;
|
|
|
|
endoffs = SlGetOffs() + len;
|
|
|
|
ch->load_proc();
|
2010-09-16 23:06:36 +01:00
|
|
|
if (SlGetOffs() != endoffs) SlErrorCorrupt("Invalid chunk size");
|
2009-05-17 12:47:56 +01:00
|
|
|
} else {
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Invalid chunk type");
|
2009-05-17 12:47:56 +01:00
|
|
|
}
|
|
|
|
break;
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 15:13:23 +01:00
|
|
|
/**
|
|
|
|
* Load a chunk of data for checking savegames.
|
|
|
|
* If the chunkhandler is NULL, the chunk is skipped.
|
|
|
|
* @param ch The chunkhandler that will be used for the operation
|
|
|
|
*/
|
|
|
|
static void SlLoadCheckChunk(const ChunkHandler *ch)
|
|
|
|
{
|
|
|
|
byte m = SlReadByte();
|
|
|
|
size_t len;
|
|
|
|
size_t endoffs;
|
|
|
|
|
|
|
|
_sl.block_mode = m;
|
|
|
|
_sl.obj_len = 0;
|
|
|
|
|
|
|
|
switch (m) {
|
|
|
|
case CH_ARRAY:
|
|
|
|
_sl.array_index = 0;
|
|
|
|
if (ch->load_check_proc) {
|
|
|
|
ch->load_check_proc();
|
|
|
|
} else {
|
|
|
|
SlSkipArray();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CH_SPARSE_ARRAY:
|
|
|
|
if (ch->load_check_proc) {
|
|
|
|
ch->load_check_proc();
|
|
|
|
} else {
|
|
|
|
SlSkipArray();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ((m & 0xF) == CH_RIFF) {
|
|
|
|
/* Read length */
|
|
|
|
len = (SlReadByte() << 16) | ((m >> 4) << 24);
|
|
|
|
len += SlReadUint16();
|
|
|
|
_sl.obj_len = len;
|
|
|
|
endoffs = SlGetOffs() + len;
|
|
|
|
if (ch->load_check_proc) {
|
|
|
|
ch->load_check_proc();
|
|
|
|
} else {
|
|
|
|
SlSkipBytes(len);
|
|
|
|
}
|
2010-09-16 23:06:36 +01:00
|
|
|
if (SlGetOffs() != endoffs) SlErrorCorrupt("Invalid chunk size");
|
2010-06-13 15:13:23 +01:00
|
|
|
} else {
|
2010-09-16 23:06:36 +01:00
|
|
|
SlErrorCorrupt("Invalid chunk type");
|
2010-06-13 15:13:23 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Stub Chunk handlers to only calculate length and do nothing else.
|
|
|
|
* The intended chunk handler that should be called.
|
|
|
|
*/
|
|
|
|
static ChunkSaveLoadProc *_stub_save_proc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stub Chunk handlers to only calculate length and do nothing else.
|
|
|
|
* Actually call the intended chunk handler.
|
|
|
|
* @param arg ignored parameter.
|
|
|
|
*/
|
|
|
|
static inline void SlStubSaveProc2(void *arg)
|
|
|
|
{
|
|
|
|
_stub_save_proc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stub Chunk handlers to only calculate length and do nothing else.
|
|
|
|
* Call SlAutoLenth with our stub save proc that will eventually
|
|
|
|
* call the intended chunk handler.
|
|
|
|
*/
|
|
|
|
static void SlStubSaveProc()
|
|
|
|
{
|
|
|
|
SlAutolength(SlStubSaveProc2, NULL);
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Save a chunk of data (eg. vehicles, stations, etc.). Each chunk is
|
2005-05-30 23:16:05 +01:00
|
|
|
* 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 18:04:08 +01:00
|
|
|
static void SlSaveChunk(const ChunkHandler *ch)
|
|
|
|
{
|
2005-05-30 23:16:05 +01:00
|
|
|
ChunkSaveLoadProc *proc = ch->save_proc;
|
2004-08-09 18:04:08 +01: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 18:04:08 +01: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 18:04:08 +01: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. */
|
2010-07-28 10:48:35 +01:00
|
|
|
_stub_save_proc = proc;
|
2004-08-09 18:04:08 +01:00
|
|
|
proc = SlStubSaveProc;
|
|
|
|
}
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2004-08-09 18:04:08 +01:00
|
|
|
_sl.block_mode = ch->flags & CH_TYPE_MASK;
|
2005-05-30 23:16:05 +01:00
|
|
|
switch (ch->flags & CH_TYPE_MASK) {
|
2009-05-17 12:43:51 +01: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;
|
|
|
|
default: NOT_REACHED();
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/** Save all chunks */
|
2007-03-07 11:47:46 +00:00
|
|
|
static void SlSaveChunks()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2010-01-02 15:56:20 +00:00
|
|
|
FOR_ALL_CHUNK_HANDLERS(ch) {
|
|
|
|
SlSaveChunk(ch);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Terminator */
|
2004-08-09 18:04:08 +01:00
|
|
|
SlWriteUint32(0);
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Find the ChunkHandler that will be used for processing the found
|
2005-05-30 23:16:05 +01:00
|
|
|
* chunk in the savegame or in memory
|
|
|
|
* @param id the chunk in question
|
|
|
|
* @return returns the appropiate chunkhandler
|
|
|
|
*/
|
2004-08-09 18:04:08 +01:00
|
|
|
static const ChunkHandler *SlFindChunkHandler(uint32 id)
|
|
|
|
{
|
2010-01-02 15:56:20 +00:00
|
|
|
FOR_ALL_CHUNK_HANDLERS(ch) if (ch->id == id) return ch;
|
2004-08-09 18:04:08 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/** Load all chunks */
|
2007-03-07 11:47:46 +00:00
|
|
|
static void SlLoadChunks()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
uint32 id;
|
|
|
|
const ChunkHandler *ch;
|
|
|
|
|
2005-05-30 23:16:05 +01: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 23:16:05 +01:00
|
|
|
|
2004-08-09 18:04:08 +01:00
|
|
|
ch = SlFindChunkHandler(id);
|
2010-09-16 23:06:36 +01:00
|
|
|
if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
|
2004-08-09 18:04:08 +01:00
|
|
|
SlLoadChunk(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 15:13:23 +01:00
|
|
|
/** Load all chunks for savegame checking */
|
|
|
|
static void SlLoadCheckChunks()
|
|
|
|
{
|
|
|
|
uint32 id;
|
|
|
|
const ChunkHandler *ch;
|
|
|
|
|
|
|
|
for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
|
|
|
|
DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
|
|
|
|
|
|
|
|
ch = SlFindChunkHandler(id);
|
2010-09-16 23:06:36 +01:00
|
|
|
if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
|
2010-06-13 15:13:23 +01:00
|
|
|
SlLoadCheckChunk(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-17 17:28:29 +01:00
|
|
|
/** Fix all pointers (convert index -> pointer) */
|
|
|
|
static void SlFixPointers()
|
|
|
|
{
|
|
|
|
_sl.action = SLA_PTRS;
|
|
|
|
|
|
|
|
DEBUG(sl, 1, "Fixing pointers");
|
|
|
|
|
2010-01-02 15:56:20 +00:00
|
|
|
FOR_ALL_CHUNK_HANDLERS(ch) {
|
|
|
|
if (ch->ptrs_proc != NULL) {
|
|
|
|
DEBUG(sl, 2, "Fixing pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
|
|
|
|
ch->ptrs_proc();
|
2009-05-17 17:28:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(sl, 1, "All pointers fixed");
|
|
|
|
|
|
|
|
assert(_sl.action == SLA_PTRS);
|
|
|
|
}
|
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/** Yes, simply writing to a file. */
|
|
|
|
struct FileWriter : SaveFilter {
|
|
|
|
FILE *file; ///< The file to write to.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the file writer, so it writes to a specific file.
|
|
|
|
* @param file The file to write to.
|
|
|
|
*/
|
|
|
|
FileWriter(FILE *file) : SaveFilter(NULL), file(file)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Make sure everything is cleaned up. */
|
|
|
|
~FileWriter()
|
|
|
|
{
|
|
|
|
this->Finish();
|
|
|
|
|
|
|
|
/* Make sure we don't double free. */
|
|
|
|
_sl.sf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void Write(byte *buf, size_t size)
|
|
|
|
{
|
|
|
|
/* We're in the process of shutting down, i.e. in "failure" mode. */
|
|
|
|
if (this->file == NULL) return;
|
|
|
|
|
|
|
|
if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void Finish()
|
|
|
|
{
|
|
|
|
if (this->file != NULL) fclose(this->file);
|
|
|
|
this->file = NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/*******************************************
|
|
|
|
********** START OF LZO CODE **************
|
|
|
|
*******************************************/
|
2010-01-10 13:12:34 +00:00
|
|
|
|
|
|
|
#ifdef WITH_LZO
|
|
|
|
#include <lzo/lzo1x.h>
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-01-10 21:20:51 +00:00
|
|
|
/** Buffer size for the LZO compressor */
|
|
|
|
static const uint LZO_BUFFER_SIZE = 8192;
|
|
|
|
|
2008-05-27 22:41:00 +01:00
|
|
|
static size_t ReadLZO()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2010-01-23 22:23:45 +00:00
|
|
|
/* Buffer size is from the LZO docs plus the chunk header size. */
|
|
|
|
byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
|
2004-08-09 18:04:08 +01:00
|
|
|
uint32 tmp[2];
|
|
|
|
uint32 size;
|
2009-08-18 23:26:38 +01:00
|
|
|
lzo_uint len;
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Read header*/
|
2007-06-24 13:27:11 +01:00
|
|
|
if (fread(tmp, sizeof(tmp), 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Check if size is bad */
|
2004-08-09 18:04:08 +01:00
|
|
|
((uint32*)out)[0] = size = tmp[1];
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2005-08-06 18:40:21 +01:00
|
|
|
if (_sl_version != 0) {
|
2004-08-09 18:04:08 +01:00
|
|
|
tmp[0] = TO_BE32(tmp[0]);
|
|
|
|
size = TO_BE32(size);
|
|
|
|
}
|
|
|
|
|
2010-09-16 23:06:36 +01:00
|
|
|
if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Read block */
|
2007-06-24 13:27:11 +01:00
|
|
|
if (fread(out + sizeof(uint32), size, 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Verify checksum */
|
2010-09-16 23:06:36 +01:00
|
|
|
if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* Decompress */
|
2009-01-27 21:51:06 +00:00
|
|
|
lzo1x_decompress(out + sizeof(uint32) * 1, size, _sl.buf, &len, NULL);
|
2004-08-09 18:04:08 +01:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/** Filter using LZO compression. */
|
|
|
|
struct LZOSaveFilter : SaveFilter {
|
|
|
|
/**
|
|
|
|
* Initialise this filter.
|
|
|
|
* @param chain The next filter in this chain.
|
|
|
|
* @param compression_level The requested level of compression.
|
|
|
|
*/
|
|
|
|
LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
|
|
|
|
{
|
|
|
|
if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
|
|
|
|
}
|
2010-01-23 22:23:45 +00:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/* virtual */ void Write(byte *buf, size_t size)
|
|
|
|
{
|
|
|
|
const lzo_bytep in = buf;
|
|
|
|
/* Buffer size is from the LZO docs plus the chunk header size. */
|
|
|
|
byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
|
|
|
|
byte wrkmem[LZO1X_1_MEM_COMPRESS];
|
|
|
|
lzo_uint outlen;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Compress up to LZO_BUFFER_SIZE bytes at once. */
|
|
|
|
lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
|
|
|
|
lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
|
|
|
|
((uint32*)out)[1] = TO_BE32((uint32)outlen);
|
|
|
|
((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
|
|
|
|
this->chain->Write(out, outlen + sizeof(uint32) * 2);
|
|
|
|
|
|
|
|
/* Move to next data chunk. */
|
|
|
|
size -= len;
|
|
|
|
in += len;
|
|
|
|
} while (size > 0);
|
|
|
|
}
|
|
|
|
};
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-01-10 15:35:13 +00:00
|
|
|
static bool InitLZO(byte compression)
|
2005-01-22 20:23:18 +00:00
|
|
|
{
|
2010-01-23 22:23:45 +00:00
|
|
|
if (lzo_init() != LZO_E_OK) return false;
|
2010-01-10 21:20:51 +00:00
|
|
|
_sl.bufsize = LZO_BUFFER_SIZE;
|
|
|
|
_sl.buf = _sl.buf_ori = MallocT<byte>(LZO_BUFFER_SIZE);
|
2004-08-09 18:04:08 +01: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 15:20:46 +01:00
|
|
|
free(_sl.buf_ori);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2010-01-10 13:12:34 +00:00
|
|
|
#endif /* WITH_LZO */
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/*********************************************
|
|
|
|
******** START OF NOCOMP CODE (uncompressed)*
|
|
|
|
*********************************************/
|
2010-01-10 21:20:51 +00:00
|
|
|
|
|
|
|
/** Buffer size used for the uncompressing 'compressor' */
|
|
|
|
static const uint NOCOMP_BUFFER_SIZE = 8192;
|
|
|
|
|
2008-05-27 22:41:00 +01:00
|
|
|
static size_t ReadNoComp()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2010-01-10 21:20:51 +00:00
|
|
|
return fread(_sl.buf, 1, NOCOMP_BUFFER_SIZE, _sl.fh);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2010-01-10 15:35:13 +00:00
|
|
|
static bool InitNoComp(byte compression)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2010-01-10 21:20:51 +00:00
|
|
|
_sl.bufsize = NOCOMP_BUFFER_SIZE;
|
|
|
|
_sl.buf = _sl.buf_ori = MallocT<byte>(NOCOMP_BUFFER_SIZE);
|
2004-08-09 18:04:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void UninitNoComp()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2005-09-11 15:20:46 +01:00
|
|
|
free(_sl.buf_ori);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/** Filter without any compression. */
|
|
|
|
struct NoCompSaveFilter : SaveFilter {
|
|
|
|
/**
|
|
|
|
* Initialise this filter.
|
|
|
|
* @param chain The next filter in this chain.
|
|
|
|
* @param compression_level The requested level of compression.
|
|
|
|
*/
|
|
|
|
NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void Write(byte *buf, size_t size)
|
|
|
|
{
|
|
|
|
this->chain->Write(buf, size);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
#include "../gui.h"
|
2005-06-06 14:11:35 +01:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct ThreadedSave {
|
2007-01-17 22:44:49 +00:00
|
|
|
byte ff_state;
|
2005-06-06 14:11:35 +01:00
|
|
|
bool saveinprogress;
|
2005-06-06 14:47:06 +01:00
|
|
|
CursorID cursor;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-06-06 14:11:35 +01:00
|
|
|
|
|
|
|
static ThreadedSave _ts;
|
2005-06-02 00:08:33 +01:00
|
|
|
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/********************************************
|
|
|
|
********** START OF ZLIB CODE **************
|
|
|
|
********************************************/
|
2004-08-09 18:04:08 +01:00
|
|
|
|
|
|
|
#if defined(WITH_ZLIB)
|
|
|
|
#include <zlib.h>
|
2005-11-26 12:57:42 +00:00
|
|
|
|
2010-01-10 21:20:51 +00:00
|
|
|
/** Buffer size for the LZO compressor */
|
|
|
|
static const uint ZLIB_BUFFER_SIZE = 8192;
|
|
|
|
|
2004-08-09 18:04:08 +01:00
|
|
|
static z_stream _z;
|
|
|
|
|
2010-01-10 15:35:13 +00:00
|
|
|
static bool InitReadZlib(byte compression)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
memset(&_z, 0, sizeof(_z));
|
|
|
|
if (inflateInit(&_z) != Z_OK) return false;
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2010-01-10 21:20:51 +00:00
|
|
|
_sl.bufsize = ZLIB_BUFFER_SIZE;
|
|
|
|
_sl.buf = _sl.buf_ori = MallocT<byte>(ZLIB_BUFFER_SIZE + ZLIB_BUFFER_SIZE); // also contains fread buffer
|
2004-08-09 18:04:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-27 22:41:00 +01:00
|
|
|
static size_t ReadZlib()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
_z.next_out = _sl.buf;
|
2010-01-10 21:20:51 +00:00
|
|
|
_z.avail_out = ZLIB_BUFFER_SIZE;
|
2004-08-09 18:04:08 +01:00
|
|
|
|
|
|
|
do {
|
2008-05-08 21:05:32 +01:00
|
|
|
/* read more bytes from the file? */
|
2004-08-09 18:04:08 +01:00
|
|
|
if (_z.avail_in == 0) {
|
2010-01-10 21:20:51 +00:00
|
|
|
_z.avail_in = (uint)fread(_z.next_in = _sl.buf + ZLIB_BUFFER_SIZE, 1, ZLIB_BUFFER_SIZE, _sl.fh);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* inflate the data */
|
2004-08-09 18:04:08 +01:00
|
|
|
r = inflate(&_z, 0);
|
2010-07-24 11:14:39 +01:00
|
|
|
if (r == Z_STREAM_END) break;
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-06-24 13:27:11 +01:00
|
|
|
if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
|
2004-08-09 18:04:08 +01:00
|
|
|
} while (_z.avail_out);
|
|
|
|
|
2010-01-10 21:20:51 +00:00
|
|
|
return ZLIB_BUFFER_SIZE - _z.avail_out;
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void UninitReadZlib()
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
inflateEnd(&_z);
|
2005-09-11 15:20:46 +01:00
|
|
|
free(_sl.buf_ori);
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/** Filter using Zlib compression. */
|
|
|
|
struct ZlibSaveFilter : SaveFilter {
|
|
|
|
z_stream z; ///< Stream state we are writing to.
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/**
|
|
|
|
* Initialise this filter.
|
|
|
|
* @param chain The next filter in this chain.
|
|
|
|
* @param compression_level The requested level of compression.
|
|
|
|
*/
|
|
|
|
ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
|
|
|
|
{
|
|
|
|
memset(&this->z, 0, sizeof(this->z));
|
|
|
|
if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/**
|
|
|
|
* Helper loop for writing the data.
|
|
|
|
* @param p The bytes to write.
|
|
|
|
* @param len Amount of bytes to write.
|
|
|
|
* @param mode Mode for deflate.
|
|
|
|
*/
|
|
|
|
void WriteLoop(byte *p, size_t len, int mode)
|
|
|
|
{
|
|
|
|
byte buf[MEMORY_CHUNK_SIZE]; // output buffer
|
|
|
|
uint n;
|
|
|
|
this->z.next_in = p;
|
|
|
|
this->z.avail_in = (uInt)len;
|
|
|
|
do {
|
|
|
|
this->z.next_out = buf;
|
|
|
|
this->z.avail_out = sizeof(buf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the poor next soul who sees many valgrind warnings of the
|
|
|
|
* "Conditional jump or move depends on uninitialised value(s)" kind:
|
|
|
|
* According to the author of zlib it is not a bug and it won't be fixed.
|
|
|
|
* http://groups.google.com/group/comp.compression/browse_thread/thread/b154b8def8c2a3ef/cdf9b8729ce17ee2
|
|
|
|
* [Mark Adler, Feb 24 2004, 'zlib-1.2.1 valgrind warnings' in the newgroup comp.compression]
|
|
|
|
*/
|
|
|
|
int r = deflate(&this->z, mode);
|
|
|
|
|
|
|
|
/* bytes were emitted? */
|
|
|
|
if ((n = sizeof(buf) - this->z.avail_out) != 0) {
|
|
|
|
this->chain->Write(buf, n);
|
|
|
|
}
|
|
|
|
if (r == Z_STREAM_END) break;
|
2010-07-24 11:14:39 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
|
|
|
|
} while (this->z.avail_in || !this->z.avail_out);
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/* virtual */ void Write(byte *buf, size_t size)
|
|
|
|
{
|
|
|
|
this->WriteLoop(buf, size, 0);
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/* virtual */ void Finish()
|
|
|
|
{
|
|
|
|
this->WriteLoop(NULL, 0, Z_FINISH);
|
|
|
|
this->chain->Finish();
|
|
|
|
deflateEnd(&this->z);
|
|
|
|
}
|
|
|
|
};
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
#endif /* WITH_ZLIB */
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-10-27 17:42:20 +01:00
|
|
|
/********************************************
|
|
|
|
********** START OF LZMA CODE **************
|
|
|
|
********************************************/
|
|
|
|
|
|
|
|
#if defined(WITH_LZMA)
|
|
|
|
#include <lzma.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Have a copy of an initialised LZMA stream. We need this as it's
|
|
|
|
* impossible to "re"-assign LZMA_STREAM_INIT to a variable, i.e.
|
|
|
|
* LZMA_STREAM_INIT can't be used to reset something. This var can.
|
|
|
|
*/
|
|
|
|
static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
|
|
|
|
/** The current LZMA stream we're processing. */
|
|
|
|
static lzma_stream _lzma;
|
|
|
|
|
|
|
|
static bool InitReadLZMA(byte compression)
|
|
|
|
{
|
|
|
|
_lzma = _lzma_init;
|
|
|
|
/* Allow saves up to 256 MB uncompressed */
|
|
|
|
if (lzma_auto_decoder(&_lzma, 1 << 28, 0) != LZMA_OK) return false;
|
|
|
|
|
|
|
|
_sl.bufsize = MEMORY_CHUNK_SIZE;
|
|
|
|
_sl.buf = _sl.buf_ori = MallocT<byte>(MEMORY_CHUNK_SIZE + MEMORY_CHUNK_SIZE); // also contains fread buffer
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ReadLZMA()
|
|
|
|
{
|
|
|
|
_lzma.next_out = _sl.buf;
|
|
|
|
_lzma.avail_out = MEMORY_CHUNK_SIZE;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* read more bytes from the file? */
|
|
|
|
if (_lzma.avail_in == 0) {
|
|
|
|
_lzma.next_in = _sl.buf + MEMORY_CHUNK_SIZE;
|
|
|
|
_lzma.avail_in = fread(_sl.buf + MEMORY_CHUNK_SIZE, 1, MEMORY_CHUNK_SIZE, _sl.fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* inflate the data */
|
|
|
|
lzma_ret r = lzma_code(&_lzma, LZMA_RUN);
|
|
|
|
if (r == LZMA_STREAM_END) break;
|
|
|
|
if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
|
|
|
|
} while (_lzma.avail_out != 0);
|
|
|
|
|
|
|
|
return MEMORY_CHUNK_SIZE - _lzma.avail_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UninitReadLZMA()
|
|
|
|
{
|
|
|
|
lzma_end(&_lzma);
|
|
|
|
free(_sl.buf_ori);
|
|
|
|
}
|
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/** Filter using LZMA compression. */
|
|
|
|
struct LZMASaveFilter : SaveFilter {
|
|
|
|
lzma_stream lzma; ///< Stream state that we are writing to.
|
2010-10-27 17:42:20 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/**
|
|
|
|
* Initialise this filter.
|
|
|
|
* @param chain The next filter in this chain.
|
|
|
|
* @param compression_level The requested level of compression.
|
|
|
|
*/
|
|
|
|
LZMASaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
|
|
|
|
{
|
|
|
|
this->lzma = LZMA_STREAM_INIT;
|
|
|
|
if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
|
|
|
|
}
|
2010-10-27 17:42:20 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/**
|
|
|
|
* Helper loop for writing the data.
|
|
|
|
* @param p The bytes to write.
|
|
|
|
* @param len Amount of bytes to write.
|
|
|
|
* @param action Action for lzma_code.
|
|
|
|
*/
|
|
|
|
void WriteLoop(byte *p, size_t len, lzma_action action)
|
|
|
|
{
|
|
|
|
byte buf[MEMORY_CHUNK_SIZE]; // output buffer
|
|
|
|
size_t n;
|
|
|
|
this->lzma.next_in = p;
|
|
|
|
this->lzma.avail_in = len;
|
|
|
|
do {
|
|
|
|
this->lzma.next_out = buf;
|
|
|
|
this->lzma.avail_out = sizeof(buf);
|
|
|
|
|
|
|
|
lzma_ret r = lzma_code(&this->lzma, action);
|
|
|
|
|
|
|
|
/* bytes were emitted? */
|
|
|
|
if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
|
|
|
|
this->chain->Write(buf, n);
|
|
|
|
}
|
|
|
|
if (r == LZMA_STREAM_END) break;
|
|
|
|
if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
|
|
|
|
} while (this->lzma.avail_in || !this->lzma.avail_out);
|
|
|
|
}
|
2010-10-27 17:42:20 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/* virtual */ void Write(byte *buf, size_t size)
|
|
|
|
{
|
|
|
|
this->WriteLoop(buf, size, LZMA_RUN);
|
|
|
|
}
|
2010-10-27 17:42:20 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
/* virtual */ void Finish()
|
|
|
|
{
|
|
|
|
this->WriteLoop(NULL, 0, LZMA_FINISH);
|
|
|
|
this->chain->Finish();
|
|
|
|
lzma_end(&this->lzma);
|
|
|
|
}
|
|
|
|
};
|
2010-10-27 17:42:20 +01:00
|
|
|
|
|
|
|
#endif /* WITH_LZMA */
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/*******************************************
|
|
|
|
************* END OF CODE *****************
|
|
|
|
*******************************************/
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/** The format for a reader/writer type of a savegame */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct SaveLoadFormat {
|
2010-01-10 15:35:13 +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 18:04:08 +01:00
|
|
|
|
2010-01-10 15:35:13 +00:00
|
|
|
bool (*init_read)(byte compression); ///< function executed upon initalization of the loader
|
|
|
|
ReaderProc *reader; ///< function that loads the data from the file
|
|
|
|
void (*uninit_read)(); ///< function executed when reading is finished
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
SaveFilter *(*init_write)(SaveFilter *chain, byte compression); ///< Constructor for the save filter.
|
2010-01-10 15:35:13 +00:00
|
|
|
|
|
|
|
byte min_compression; ///< the minimum compression level of this format
|
|
|
|
byte default_compression; ///< the default compression level of this format
|
|
|
|
byte max_compression; ///< the maximum compression level of this format
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2010-10-27 17:42:20 +01:00
|
|
|
/** The different saveload formats known/understood by OpenTTD. */
|
2004-08-09 18:04:08 +01:00
|
|
|
static const SaveLoadFormat _saveload_formats[] = {
|
2010-01-10 13:12:34 +00:00
|
|
|
#if defined(WITH_LZO)
|
2010-10-27 17:42:20 +01:00
|
|
|
/* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
|
2010-12-04 09:09:29 +00:00
|
|
|
{"lzo", TO_BE32X('OTTD'), InitLZO, ReadLZO, UninitLZO, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
|
2010-01-10 13:12:34 +00:00
|
|
|
#else
|
2010-12-04 09:09:29 +00:00
|
|
|
{"lzo", TO_BE32X('OTTD'), NULL, NULL, NULL, NULL, 0, 0, 0},
|
2010-01-10 13:12:34 +00:00
|
|
|
#endif
|
2010-10-27 17:42:20 +01:00
|
|
|
/* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
|
2010-12-04 09:09:29 +00:00
|
|
|
{"none", TO_BE32X('OTTN'), InitNoComp, ReadNoComp, UninitNoComp, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
|
2004-08-09 18:04:08 +01:00
|
|
|
#if defined(WITH_ZLIB)
|
2010-10-27 17:42:20 +01:00
|
|
|
/* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
|
|
|
|
* fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
|
|
|
|
* 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
|
2010-12-04 09:09:29 +00:00
|
|
|
{"zlib", TO_BE32X('OTTZ'), InitReadZlib, ReadZlib, UninitReadZlib, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
|
2004-08-09 18:04:08 +01:00
|
|
|
#else
|
2010-12-04 09:09:29 +00:00
|
|
|
{"zlib", TO_BE32X('OTTZ'), NULL, NULL, NULL, NULL, 0, 0, 0},
|
2004-08-09 18:04:08 +01:00
|
|
|
#endif
|
2010-10-27 17:42:20 +01:00
|
|
|
#if defined(WITH_LZMA)
|
|
|
|
/* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
|
|
|
|
* Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
|
|
|
|
* The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
|
|
|
|
* slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
|
|
|
|
* It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is prefered over .tar.lzma. */
|
2010-12-04 09:09:29 +00:00
|
|
|
{"lzma", TO_BE32X('OTTX'), InitReadLZMA, ReadLZMA, UninitReadLZMA, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
|
2010-10-27 17:42:20 +01:00
|
|
|
#else
|
2010-12-04 09:09:29 +00:00
|
|
|
{"lzma", TO_BE32X('OTTX'), NULL, NULL, NULL, NULL, 0, 0, 0},
|
2010-10-27 17:42:20 +01:00
|
|
|
#endif
|
2004-08-09 18:04:08 +01:00
|
|
|
};
|
|
|
|
|
2005-05-30 23:16:05 +01:00
|
|
|
/**
|
2010-01-10 14:57:05 +00:00
|
|
|
* Return the savegameformat of the game. Whether it was created with ZLIB compression
|
2005-05-30 23:16:05 +01:00
|
|
|
* uncompressed, or another type
|
2005-06-02 00:08:33 +01:00
|
|
|
* @param s Name of the savegame format. If NULL it picks the first available one
|
2010-01-10 15:35:13 +00:00
|
|
|
* @param compression_level Output for telling what compression level we want.
|
2007-02-23 01:48:53 +00:00
|
|
|
* @return Pointer to SaveLoadFormat struct giving all characteristics of this type of savegame
|
2005-05-30 23:16:05 +01:00
|
|
|
*/
|
2010-01-10 15:35:13 +00:00
|
|
|
static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
2010-01-10 14:57:05 +00:00
|
|
|
const SaveLoadFormat *def = lastof(_saveload_formats);
|
2004-08-09 18:04:08 +01: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 18:04:08 +01:00
|
|
|
while (!def->init_write) def--;
|
|
|
|
|
2010-01-10 14:57:05 +00:00
|
|
|
if (!StrEmpty(s)) {
|
2010-01-10 15:35:13 +00:00
|
|
|
/* Get the ":..." of the compression level out of the way */
|
|
|
|
char *complevel = strrchr(s, ':');
|
|
|
|
if (complevel != NULL) *complevel = '\0';
|
|
|
|
|
2010-01-10 14:57:05 +00:00
|
|
|
for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
|
|
|
|
if (slf->init_write != NULL && strcmp(s, slf->name) == 0) {
|
2010-01-10 15:35:13 +00:00
|
|
|
*compression_level = slf->default_compression;
|
|
|
|
if (complevel != NULL) {
|
|
|
|
/* There is a compression level in the string.
|
|
|
|
* First restore the : we removed to do proper name matching,
|
|
|
|
* then move the the begin of the actual version. */
|
|
|
|
*complevel = ':';
|
|
|
|
complevel++;
|
|
|
|
|
|
|
|
/* Get the version and determine whether all went fine. */
|
|
|
|
char *end;
|
|
|
|
long level = strtol(complevel, &end, 10);
|
|
|
|
if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) {
|
|
|
|
ShowInfoF("Compression level '%s' is not valid.", complevel);
|
|
|
|
} else {
|
|
|
|
*compression_level = level;
|
|
|
|
}
|
|
|
|
}
|
2005-06-02 00:08:33 +01:00
|
|
|
return slf;
|
2010-01-10 14:57:05 +00:00
|
|
|
}
|
2005-06-02 00:08:33 +01:00
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
|
|
|
ShowInfoF("Savegame format '%s' is not available. Reverting to '%s'.", s, def->name);
|
2010-01-10 15:35:13 +00:00
|
|
|
|
|
|
|
/* Restore the string by adding the : back */
|
|
|
|
if (complevel != NULL) *complevel = ':';
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
2010-01-10 15:35:13 +00:00
|
|
|
*compression_level = def->default_compression;
|
2004-08-09 18:04:08 +01:00
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/* actual loader/saver function */
|
2009-08-06 23:00:32 +01:00
|
|
|
void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
|
2007-03-07 11:47:46 +00:00
|
|
|
extern bool AfterLoadGame();
|
2004-08-09 18:04:08 +01:00
|
|
|
extern bool LoadOldSaveGame(const char *file);
|
|
|
|
|
2010-12-02 23:08:22 +00:00
|
|
|
/**
|
|
|
|
* Clear/free the memory dumper.
|
|
|
|
*/
|
|
|
|
static inline void ClearMemoryDumper()
|
|
|
|
{
|
|
|
|
delete _sl.dumper;
|
|
|
|
_sl.dumper = NULL;
|
2010-12-04 09:09:29 +00:00
|
|
|
|
|
|
|
delete _sl.sf;
|
|
|
|
_sl.sf = NULL;
|
2010-12-02 23:08:22 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/** Small helper function to close the to be loaded savegame and signal error */
|
2007-03-07 11:47:46 +00:00
|
|
|
static inline SaveOrLoadResult AbortSaveLoad()
|
2005-05-30 23:16:05 +01:00
|
|
|
{
|
|
|
|
if (_sl.fh != NULL) fclose(_sl.fh);
|
|
|
|
|
|
|
|
_sl.fh = NULL;
|
|
|
|
return SL_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/**
|
|
|
|
* Update the gui accordingly when starting saving
|
2005-06-06 14:11:35 +01:00
|
|
|
* and set locks on saveload. Also turn off fast-forward cause with that
|
2010-07-28 10:48:35 +01:00
|
|
|
* saving takes Aaaaages
|
|
|
|
*/
|
2008-04-19 11:18:38 +01:00
|
|
|
static void SaveFileStart()
|
2005-06-02 00:08:33 +01: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 14:11:35 +01:00
|
|
|
|
2008-05-16 08:08:04 +01:00
|
|
|
InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_START);
|
2005-06-06 14:11:35 +01:00
|
|
|
_ts.saveinprogress = true;
|
2005-06-02 00:08:33 +01:00
|
|
|
}
|
|
|
|
|
2010-07-28 10:48:35 +01:00
|
|
|
/** Update the gui accordingly when saving is done and release locks on saveload. */
|
2008-04-19 11:18:38 +01:00
|
|
|
static void SaveFileDone()
|
2005-06-02 00:08:33 +01:00
|
|
|
{
|
2008-11-27 18:39:55 +00:00
|
|
|
if (_game_mode != GM_MENU) _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 14:11:35 +01:00
|
|
|
|
2008-05-16 08:08:04 +01:00
|
|
|
InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_FINISH);
|
2005-06-06 14:11:35 +01:00
|
|
|
_ts.saveinprogress = false;
|
2005-06-02 00:08:33 +01:00
|
|
|
}
|
|
|
|
|
2007-06-24 13:27:11 +01: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];
|
2009-05-17 12:15:11 +01:00
|
|
|
GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
|
2007-06-24 13:27:11 +01:00
|
|
|
return err_str;
|
|
|
|
}
|
|
|
|
|
2005-09-02 17:05:59 +01:00
|
|
|
/** Show a gui message when saving has failed */
|
2008-04-19 11:18:38 +01:00
|
|
|
static void SaveFileError()
|
2005-09-02 17:05:59 +01:00
|
|
|
{
|
2007-06-24 13:27:11 +01:00
|
|
|
SetDParamStr(0, GetSaveLoadErrorString());
|
2010-02-24 14:46:15 +00:00
|
|
|
ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
|
2005-09-02 17:05:59 +01:00
|
|
|
SaveFileDone();
|
|
|
|
}
|
|
|
|
|
2010-08-01 20:22:34 +01:00
|
|
|
/**
|
|
|
|
* We have written the whole game into memory, _memory_savegame, now find
|
2005-06-02 00:08:33 +01:00
|
|
|
* and appropiate compressor and start writing to file.
|
|
|
|
*/
|
2007-01-17 00:01:55 +00:00
|
|
|
static SaveOrLoadResult SaveFileToDisk(bool threaded)
|
2005-06-02 00:08:33 +01:00
|
|
|
{
|
2007-06-24 13:27:11 +01:00
|
|
|
_sl.excpt_uninit = NULL;
|
2007-08-16 17:18:22 +01:00
|
|
|
try {
|
2010-01-10 15:35:13 +00:00
|
|
|
byte compression;
|
|
|
|
const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
|
2007-08-16 17:18:22 +01:00
|
|
|
|
|
|
|
/* We have written our stuff to memory, now write it to file! */
|
2010-01-10 14:57:05 +00:00
|
|
|
uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
|
2010-12-04 09:09:29 +00:00
|
|
|
_sl.sf->Write((byte*)hdr, sizeof(hdr));
|
2007-08-16 17:18:22 +01:00
|
|
|
|
2010-12-04 09:09:29 +00:00
|
|
|
_sl.sf = fmt->init_write(_sl.sf, compression);
|
|
|
|
_sl.dumper->Flush(_sl.sf);
|
2007-08-16 17:18:22 +01:00
|
|
|
|
2010-12-02 23:08:22 +00:00
|
|
|
ClearMemoryDumper();
|
2007-08-16 17:18:22 +01:00
|
|
|
|
2008-04-19 11:18:38 +01:00
|
|
|
if (threaded) SetAsyncSaveFinish(SaveFileDone);
|
2007-08-16 17:18:22 +01:00
|
|
|
|
|
|
|
return SL_OK;
|
2010-07-28 10:48:35 +01:00
|
|
|
} catch (...) {
|
2005-06-02 00:08:33 +01:00
|
|
|
AbortSaveLoad();
|
2007-06-24 13:27:11 +01:00
|
|
|
if (_sl.excpt_uninit != NULL) _sl.excpt_uninit();
|
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
/* Skip the "colour" character */
|
2009-05-10 18:27:25 +01:00
|
|
|
DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
|
2005-06-02 00:08:33 +01:00
|
|
|
|
2007-01-17 00:01:55 +00:00
|
|
|
if (threaded) {
|
2008-04-19 11:18:38 +01:00
|
|
|
SetAsyncSaveFinish(SaveFileError);
|
2006-06-27 22:25:53 +01:00
|
|
|
} else {
|
|
|
|
SaveFileError();
|
|
|
|
}
|
2007-01-17 00:01:55 +00:00
|
|
|
return SL_ERROR;
|
2005-06-02 00:08:33 +01:00
|
|
|
}
|
2007-01-17 00:01:55 +00:00
|
|
|
}
|
|
|
|
|
2010-11-21 12:12:23 +00:00
|
|
|
/** Thread run function for saving the file to disk. */
|
2008-06-08 11:51:36 +01:00
|
|
|
static void SaveFileToDiskThread(void *arg)
|
2007-01-17 00:01:55 +00:00
|
|
|
{
|
|
|
|
SaveFileToDisk(true);
|
2005-06-02 00:08:33 +01:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void WaitTillSaved()
|
2005-08-05 10:15:41 +01:00
|
|
|
{
|
2008-05-18 20:41:47 +01:00
|
|
|
if (_save_thread == NULL) return;
|
2008-04-14 20:54:33 +01:00
|
|
|
|
2008-05-18 20:41:47 +01:00
|
|
|
_save_thread->Join();
|
2008-06-08 16:27:57 +01:00
|
|
|
delete _save_thread;
|
2008-05-18 20:41:47 +01:00
|
|
|
_save_thread = NULL;
|
2005-08-05 10:15:41 +01:00
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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
|
2010-11-21 12:12:23 +00:00
|
|
|
* @param mode Save or load mode. Load can also be a TTD(Patch) game. Use #SL_LOAD, #SL_OLD_LOAD, #SL_LOAD_CHECK, or #SL_SAVE.
|
2009-09-19 10:51:14 +01:00
|
|
|
* @param sb The sub directory to save the savegame in
|
2009-09-14 16:53:19 +01:00
|
|
|
* @param threaded True when threaded saving is allowed
|
2010-11-21 12:12:23 +00:00
|
|
|
* @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
|
2005-05-30 23:16:05 +01:00
|
|
|
*/
|
2009-09-14 16:53:19 +01:00
|
|
|
SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded)
|
2004-08-09 18:04:08 +01:00
|
|
|
{
|
|
|
|
uint32 hdr[2];
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2005-09-11 15:17:21 +01: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 */
|
2010-02-24 14:46:15 +00:00
|
|
|
if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
|
2005-09-11 15:17:21 +01:00
|
|
|
return SL_OK;
|
2005-06-06 14:11:35 +01:00
|
|
|
}
|
2005-09-11 15:17:21 +01:00
|
|
|
WaitTillSaved();
|
2005-06-06 14:11:35 +01:00
|
|
|
|
2010-06-13 15:13:23 +01:00
|
|
|
/* Clear previous check data */
|
|
|
|
if (mode == SL_LOAD_CHECK) _load_check_data.Clear();
|
2007-06-28 00:26:40 +01:00
|
|
|
_next_offs = 0;
|
|
|
|
|
2005-09-11 15:17:21 +01:00
|
|
|
/* Load a TTDLX or TTDPatch game */
|
2004-08-09 18:04:08 +01:00
|
|
|
if (mode == SL_OLD_LOAD) {
|
2009-03-08 16:51:08 +00:00
|
|
|
_engine_mngr.ResetToDefaultMapping();
|
2009-08-06 23:00:32 +01:00
|
|
|
InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
|
2010-02-06 17:26:21 +00:00
|
|
|
|
|
|
|
/* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
|
|
|
|
* and if so a new NewGRF list will be made in LoadOldSaveGame.
|
|
|
|
* Note: this is done here because AfterLoadGame is also called
|
|
|
|
* for OTTD savegames which have their own NewGRF logic. */
|
|
|
|
ClearGRFConfigList(&_grfconfig);
|
2008-06-03 19:35:58 +01:00
|
|
|
GamelogReset();
|
2004-08-09 18:04:08 +01:00
|
|
|
if (!LoadOldSaveGame(filename)) return SL_REINIT;
|
2005-12-29 22:28:19 +00:00
|
|
|
_sl_version = 0;
|
2008-05-05 23:03:01 +01:00
|
|
|
_sl_minor_version = 0;
|
2008-06-03 19:35:58 +01:00
|
|
|
GamelogStartAction(GLAT_LOAD);
|
|
|
|
if (!AfterLoadGame()) {
|
|
|
|
GamelogStopAction();
|
|
|
|
return SL_REINIT;
|
|
|
|
}
|
|
|
|
GamelogStopAction();
|
2004-08-09 18:04:08 +01:00
|
|
|
return SL_OK;
|
|
|
|
}
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2010-06-13 15:13:23 +01:00
|
|
|
/* Mark SL_LOAD_CHECK as supported for this savegame. */
|
|
|
|
if (mode == SL_LOAD_CHECK) _load_check_data.checkable = true;
|
|
|
|
|
2007-06-24 13:27:11 +01:00
|
|
|
_sl.excpt_uninit = NULL;
|
2010-07-08 12:50:42 +01:00
|
|
|
_sl.bufe = _sl.bufp = NULL;
|
|
|
|
_sl.offs_base = 0;
|
|
|
|
switch (mode) {
|
|
|
|
case SL_LOAD_CHECK: _sl.action = SLA_LOAD_CHECK; break;
|
|
|
|
case SL_LOAD: _sl.action = SLA_LOAD; break;
|
|
|
|
case SL_SAVE: _sl.action = SLA_SAVE; break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
try {
|
|
|
|
_sl.fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
|
2007-06-24 13:27:11 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
/* Make it a little easier to load savegames from the console */
|
2010-06-13 15:13:23 +01:00
|
|
|
if (_sl.fh == NULL && mode != SL_SAVE) _sl.fh = FioFOpenFile(filename, "rb", SAVE_DIR);
|
|
|
|
if (_sl.fh == NULL && mode != SL_SAVE) _sl.fh = FioFOpenFile(filename, "rb", BASE_DIR);
|
2007-06-24 13:27:11 +01:00
|
|
|
|
2007-08-16 17:18:22 +01: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 21:09:05 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
/* 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 */
|
2009-03-15 00:32:18 +00:00
|
|
|
if (mode == SL_SAVE) { // SAVE game
|
2010-04-14 21:29:39 +01:00
|
|
|
DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
|
2007-08-16 17:18:22 +01:00
|
|
|
|
2010-12-02 23:08:22 +00:00
|
|
|
_sl.dumper = new MemoryDumper();
|
2010-12-04 09:09:29 +00:00
|
|
|
_sl.sf = new FileWriter(_sl.fh);
|
|
|
|
_sl.fh = NULL; // This shouldn't be closed; goes via _sl.sf now.
|
2010-12-02 23:08:22 +00:00
|
|
|
_sl.excpt_uninit = ClearMemoryDumper;
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
_sl_version = SAVEGAME_VERSION;
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2008-05-29 11:16:59 +01:00
|
|
|
SaveViewportBeforeSaveGame();
|
2007-08-16 17:18:22 +01:00
|
|
|
SlSaveChunks();
|
2004-09-10 20:02:27 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
SaveFileStart();
|
2009-10-27 20:28:10 +00:00
|
|
|
if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
|
2009-09-14 16:53:19 +01:00
|
|
|
if (!threaded || !ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread)) {
|
|
|
|
if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
|
2005-06-02 00:08:33 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
SaveOrLoadResult result = SaveFileToDisk(false);
|
|
|
|
SaveFileDone();
|
2007-01-17 00:01:55 +00:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
return result;
|
|
|
|
}
|
2009-03-15 00:32:18 +00:00
|
|
|
} else { // LOAD game
|
2010-06-13 15:13:23 +01:00
|
|
|
assert(mode == SL_LOAD || mode == SL_LOAD_CHECK);
|
2010-04-08 22:14:49 +01:00
|
|
|
DEBUG(desync, 1, "load: %s", filename);
|
2007-08-16 17:18:22 +01:00
|
|
|
|
2009-03-04 09:53:31 +00:00
|
|
|
/* Can't fseek to 0 as in tar files that is not correct */
|
|
|
|
long pos = ftell(_sl.fh);
|
2007-08-16 17:18:22 +01: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. */
|
2010-01-10 14:30:32 +00:00
|
|
|
const SaveLoadFormat *fmt = _saveload_formats;
|
|
|
|
for (;;) {
|
2007-08-16 17:18:22 +01:00
|
|
|
/* 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");
|
|
|
|
clearerr(_sl.fh);
|
2009-03-04 09:53:31 +00:00
|
|
|
fseek(_sl.fh, pos, SEEK_SET);
|
2007-08-16 17:18:22 +01:00
|
|
|
_sl_version = 0;
|
|
|
|
_sl_minor_version = 0;
|
2010-02-05 20:22:01 +00:00
|
|
|
|
|
|
|
/* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
|
|
|
|
fmt = _saveload_formats;
|
|
|
|
for (;;) {
|
|
|
|
if (fmt == endof(_saveload_formats)) {
|
|
|
|
/* Who removed LZO support? Bad bad boy! */
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
if (fmt->tag == TO_BE32X('OTTD')) break;
|
|
|
|
fmt++;
|
|
|
|
}
|
2007-08-16 17:18:22 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-08-16 17:18:22 +01: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-02 00:08:33 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
/* Is the version higher than the current? */
|
|
|
|
if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
|
|
|
|
break;
|
|
|
|
}
|
2010-01-10 14:30:32 +00:00
|
|
|
|
|
|
|
fmt++;
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
2005-05-30 23:16:05 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
_sl.read_bytes = fmt->reader;
|
|
|
|
_sl.excpt_uninit = fmt->uninit_read;
|
2005-11-22 19:33:29 +00:00
|
|
|
|
2007-08-16 17:18:22 +01: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 20:02:27 +01:00
|
|
|
|
2010-01-10 15:35:13 +00:00
|
|
|
if (!fmt->init_read(0)) {
|
2007-08-16 17:18:22 +01:00
|
|
|
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 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2010-06-13 15:13:23 +01:00
|
|
|
if (mode != SL_LOAD_CHECK) {
|
|
|
|
_engine_mngr.ResetToDefaultMapping();
|
|
|
|
|
|
|
|
/* 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(256, 256, true, true);
|
|
|
|
|
|
|
|
GamelogReset();
|
|
|
|
|
2010-11-21 12:47:04 +00:00
|
|
|
if (IsSavegameVersionBefore(4)) {
|
2010-06-13 15:13:23 +01:00
|
|
|
/*
|
|
|
|
* NewGRFs were introduced between 0.3,4 and 0.3.5, which both
|
|
|
|
* shared savegame version 4. Anything before that 'obviously'
|
|
|
|
* does not have any NewGRFs. Between the introduction and
|
|
|
|
* savegame version 41 (just before 0.5) the NewGRF settings
|
|
|
|
* were not stored in the savegame and they were loaded by
|
|
|
|
* using the settings from the main menu.
|
|
|
|
* So, to recap:
|
|
|
|
* - savegame version < 4: do not load any NewGRFs.
|
|
|
|
* - savegame version >= 41: load NewGRFs from savegame, which is
|
|
|
|
* already done at this stage by
|
|
|
|
* overwriting the main menu settings.
|
|
|
|
* - other savegame versions: use main menu settings.
|
|
|
|
*
|
|
|
|
* This means that users *can* crash savegame version 4..40
|
|
|
|
* savegames if they set incompatible NewGRFs in the main menu,
|
|
|
|
* but can't crash anymore for savegame version < 4 savegames.
|
|
|
|
*
|
|
|
|
* Note: this is done here because AfterLoadGame is also called
|
|
|
|
* for TTO/TTD/TTDP savegames which have their own NewGRF logic.
|
|
|
|
*/
|
|
|
|
ClearGRFConfigList(&_grfconfig);
|
|
|
|
}
|
2010-02-06 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-13 15:13:23 +01:00
|
|
|
if (mode == SL_LOAD_CHECK) {
|
|
|
|
/* Load chunks into _load_check_data.
|
|
|
|
* No pools are loaded. References are not possible, and thus do not need resolving. */
|
|
|
|
SlLoadCheckChunks();
|
|
|
|
} else {
|
|
|
|
/* Load chunks and resolve references */
|
|
|
|
SlLoadChunks();
|
|
|
|
SlFixPointers();
|
|
|
|
}
|
2007-08-16 17:18:22 +01:00
|
|
|
fmt->uninit_read();
|
|
|
|
fclose(_sl.fh);
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2008-05-05 23:03:01 +01:00
|
|
|
_savegame_type = SGT_OTTD;
|
|
|
|
|
2010-06-13 15:15:58 +01:00
|
|
|
if (mode == SL_LOAD_CHECK) {
|
|
|
|
/* The only part from AfterLoadGame() we need */
|
|
|
|
_load_check_data.grf_compatibility = IsGoodGRFConfigList(_load_check_data.grfconfig);
|
|
|
|
} else {
|
2010-06-13 15:13:23 +01:00
|
|
|
GamelogStartAction(GLAT_LOAD);
|
|
|
|
|
|
|
|
/* After loading fix up savegame for any internal changes that
|
2010-07-31 22:02:56 +01:00
|
|
|
* might have occurred since then. If it fails, load back the old game. */
|
2010-06-13 15:13:23 +01:00
|
|
|
if (!AfterLoadGame()) {
|
|
|
|
GamelogStopAction();
|
|
|
|
return SL_REINIT;
|
|
|
|
}
|
|
|
|
|
2008-06-03 19:35:58 +01:00
|
|
|
GamelogStopAction();
|
|
|
|
}
|
2005-05-30 23:16:05 +01:00
|
|
|
}
|
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
return SL_OK;
|
2010-07-28 10:48:35 +01:00
|
|
|
} catch (...) {
|
2007-08-16 17:18:22 +01:00
|
|
|
AbortSaveLoad();
|
2005-01-29 19:45:14 +00:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
/* deinitialize compressor. */
|
|
|
|
if (_sl.excpt_uninit != NULL) _sl.excpt_uninit();
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
/* Skip the "colour" character */
|
2010-06-13 15:13:23 +01:00
|
|
|
if (mode != SL_LOAD_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
|
2004-08-09 18:04:08 +01:00
|
|
|
|
2007-08-16 17:18:22 +01:00
|
|
|
/* A saver/loader exception!! reinitialize all variables to prevent crash! */
|
|
|
|
return (mode == SL_LOAD) ? SL_REINIT : SL_ERROR;
|
|
|
|
}
|
2004-08-09 18:04:08 +01:00
|
|
|
}
|
|
|
|
|
2009-02-08 12:25:13 +00:00
|
|
|
/** Do a save when exiting the game (_settings_client.gui.autosave_on_exit) */
|
2007-03-07 11:47:46 +00:00
|
|
|
void DoExitSave()
|
2004-12-13 22:10:12 +00:00
|
|
|
{
|
2007-06-17 16:48:57 +01:00
|
|
|
SaveOrLoad("exit.sav", SL_SAVE, AUTOSAVE_DIR);
|
2004-12-13 22:10:12 +00:00
|
|
|
}
|
|
|
|
|
2009-01-02 22:49:43 +00:00
|
|
|
/**
|
|
|
|
* Fill the buffer with the default name for a savegame *or* screenshot.
|
|
|
|
* @param buf the buffer to write to.
|
|
|
|
* @param last the last element in the buffer.
|
|
|
|
*/
|
|
|
|
void GenerateDefaultSaveName(char *buf, const char *last)
|
|
|
|
{
|
2009-01-03 13:27:08 +00:00
|
|
|
/* Check if we have a name for this map, which is the name of the first
|
|
|
|
* available company. When there's no company available we'll use
|
|
|
|
* 'Spectator' as "company" name. */
|
|
|
|
CompanyID cid = _local_company;
|
2009-05-17 02:00:56 +01:00
|
|
|
if (!Company::IsValidID(cid)) {
|
2009-01-03 13:27:08 +00:00
|
|
|
const Company *c;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
cid = c->index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDParam(0, cid);
|
|
|
|
|
|
|
|
/* Insert current date */
|
2009-01-03 00:58:59 +00:00
|
|
|
switch (_settings_client.gui.date_format_in_default_names) {
|
|
|
|
case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
|
|
|
|
case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
|
|
|
|
case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
SetDParam(2, _date);
|
2009-01-03 13:27:08 +00:00
|
|
|
|
|
|
|
/* Get the correct string (special string for when there's not company) */
|
2009-08-05 18:59:21 +01:00
|
|
|
GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
|
2009-01-02 22:49:43 +00:00
|
|
|
SanitizeFilename(buf);
|
|
|
|
}
|
|
|
|
|
2005-05-30 23:16:05 +01: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 18:04:08 +01: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 23:25:24 +01:00
|
|
|
mode = SL_LOAD; // don't try to get filename, just show name as it is written
|
2006-06-27 22:25:53 +01:00
|
|
|
} else {
|
2007-02-23 01:48:53 +00:00
|
|
|
/* see if we have any loader for this type. */
|
2004-08-09 18:04:08 +01: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 23:16:05 +01:00
|
|
|
}
|
|
|
|
#endif
|