src/saveload.cpp
author glx
Thu, 27 Nov 2008 18:39:55 +0000
changeset 10387 93e6f7a70691
parent 10347 6da2d30f2fe8
permissions -rw-r--r--
(svn r14638) -Fix [FS#2430]: don't restore fast-forward when SaveFileDone() callback happens after we returned to the menu
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
     3
/** @file saveload.cpp
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
     4
 * All actions handling saving and loading goes on in this file. The general actions
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
     5
 * are as follows for saving a game (loading is analogous):
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
     6
 * <ol>
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
     7
 * <li>initialize the writer by creating a temporary memory-buffer for it
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
     8
 * <li>go through all to-be saved elements, each 'chunk' (ChunkHandler) prefixed by a label
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
     9
 * <li>use their description array (SaveLoad) to know what elements to save and in what version
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
    10
 *    of the game it was active (used when loading)
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
    11
 * <li>write all data byte-by-byte to the temporary buffer so it is endian-safe
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
    12
 * <li>when the buffer is full; flush it to the output (eg save to file) (_sl.buf, _sl.bufp, _sl.bufe)
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
    13
 * <li>repeat this until everything is done, and flush any remaining output to file
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
    14
 * </ol>
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
    15
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1886
diff changeset
    17
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1284
diff changeset
    18
#include "debug.h"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8709
diff changeset
    19
#include "station_base.h"
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
    20
#include "thread.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "town.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
#include "saveload.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5436
diff changeset
    23
#include "network/network.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    24
#include "variables.h"
8107
bb7deea89175 (svn r11668) -Codechange: more refactoring aimed at reducing compile time and making it more logic where function definitions can be found.
rubidium
parents: 8037
diff changeset
    25
#include "window_func.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8107
diff changeset
    26
#include "strings_func.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8121
diff changeset
    27
#include "gfx_func.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
    28
#include "core/alloc_func.hpp"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    29
#include "functions.h"
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8131
diff changeset
    30
#include "core/endian_func.hpp"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8132
diff changeset
    31
#include "vehicle_base.h"
8212
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8144
diff changeset
    32
#include "autoreplace_base.h"
9248
5c057d6b7e21 (svn r13114) -Codechange: use InvalidateData instead of direct window access to modify the state of the statusbar from outside the statusbar.
rubidium
parents: 9165
diff changeset
    33
#include "statusbar_gui.h"
10039
1f236afd6cd1 (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium
parents: 9810
diff changeset
    34
#include "fileio_func.h"
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
    35
#include <list>
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
    36
#include "gamelog.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    38
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    39
10347
6da2d30f2fe8 (svn r14598) -Feature: Action0Industries property 24 (industry suuplies default name for nearby station).
rubidium
parents: 10207
diff changeset
    40
extern const uint16 SAVEGAME_VERSION = 103;
9101
40c05255536e (svn r12961) -Codechange: store the type of savegame we are loading in a global variable
smatz
parents: 9100
diff changeset
    41
40c05255536e (svn r12961) -Codechange: store the type of savegame we are loading in a global variable
smatz
parents: 9100
diff changeset
    42
SavegameType _savegame_type; ///< type of savegame we are loading
40c05255536e (svn r12961) -Codechange: store the type of savegame we are loading in a global variable
smatz
parents: 9100
diff changeset
    43
40c05255536e (svn r12961) -Codechange: store the type of savegame we are loading in a global variable
smatz
parents: 9100
diff changeset
    44
uint32 _ttdp_version;     ///< version of TTDP savegame (if applicable)
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    45
uint16 _sl_version;       ///< the major savegame version identifier
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    46
byte   _sl_minor_version; ///< the minor savegame version, DO NOT USE!
2295
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    47
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
    48
typedef void WriterProc(size_t len);
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
    49
typedef size_t ReaderProc();
2337
d8b9e722672d (svn r2863) Move some type declarations into saveload.c, should've been part of r2819
tron
parents: 2335
diff changeset
    50
2295
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    51
/** The saveload struct, containing reader-writer functions, bufffer, version, etc. */
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    52
static struct {
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    53
	bool save;                           ///< are we doing a save or a load atm. True when saving
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    54
	byte need_length;                    ///< ???
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    55
	byte block_mode;                     ///< ???
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    56
	bool error;                          ///< did an error occur or not
2295
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    57
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
    58
	size_t obj_len;                      ///< the length of the current object we are busy with
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    59
	int array_index, last_array_index;   ///< in the case of an array, the current and last positions
2295
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    60
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
    61
	size_t offs_base;                    ///< the offset in number of bytes since we started writing data (eg uncompressed savegame size)
2295
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    62
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    63
	WriterProc *write_bytes;             ///< savegame writer function
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    64
	ReaderProc *read_bytes;              ///< savegame loader function
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    65
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    66
	const ChunkHandler* const *chs;      ///< the chunk of data that is being processed atm (vehicles, signs, etc.)
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    67
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    68
	/* When saving/loading savegames, they are always saved to a temporary memory-place
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    69
	 * to be flushed to file (save) or to final place (load) when full. */
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    70
	byte *bufp, *bufe;                   ///< bufp(ointer) gives the current position in the buffer bufe(nd) gives the end of the buffer
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    71
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    72
	/* these 3 may be used by compressor/decompressors. */
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    73
	byte *buf;                           ///< pointer to temporary memory to read/write, initialized by SaveLoadFormat->initread/write
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    74
	byte *buf_ori;                       ///< pointer to the original memory location of buf, used to free it afterwards
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    75
	uint bufsize;                        ///< the size of the temporary memory *buf
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    76
	FILE *fh;                            ///< the file from which is read or written to
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
    77
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
    78
	void (*excpt_uninit)();              ///< the function to execute on any encountered error
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
    79
	StringID error_str;                  ///< the translateable error message to show
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
    80
	char *extra_msg;                     ///< the error message
2295
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    81
} _sl;
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    82
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    83
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
    84
enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
8935
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    86
/** Error handler, calls longjmp to simulate an exception.
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    87
 * @todo this was used to have a central place to handle errors, but it is
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    88
 * pretty ugly, and seriously interferes with any multithreaded approaches */
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    89
static void NORETURN SlError(StringID string, const char *extra_msg = NULL)
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    90
{
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    91
	_sl.error_str = string;
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    92
	free(_sl.extra_msg);
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    93
	_sl.extra_msg = (extra_msg == NULL) ? NULL : strdup(extra_msg);
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    94
	throw std::exception();
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    95
}
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
    96
8989
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
    97
typedef void (*AsyncSaveFinishProc)();
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
    98
static AsyncSaveFinishProc _async_save_finish = NULL;
9479
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
    99
static ThreadObject *_save_thread;
8989
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   100
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   101
/**
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   102
 * Called by save thread to tell we finished saving.
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   103
 */
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   104
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   105
{
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   106
	if (_exit_game) return;
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   107
	while (_async_save_finish != NULL) CSleep(10);
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   108
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   109
	_async_save_finish = proc;
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   110
}
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   111
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   112
/**
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   113
 * Handle async save finishes.
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   114
 */
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   115
void ProcessAsyncSaveFinish()
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   116
{
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   117
	if (_async_save_finish == NULL) return;
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   118
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   119
	_async_save_finish();
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   120
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   121
	_async_save_finish = NULL;
9479
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   122
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   123
	if (_save_thread != NULL) {
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   124
		_save_thread->Join();
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   125
		delete _save_thread;
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   126
		_save_thread = NULL;
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   127
	}
8989
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   128
}
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
   129
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   130
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   131
 * Fill the input buffer by reading from the file with the given reader
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   132
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   133
static void SlReadFill()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
{
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   135
	size_t len = _sl.read_bytes();
8935
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
   136
	if (len == 0) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected end of chunk");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
	_sl.bufp = _sl.buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
	_sl.bufe = _sl.buf + len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
	_sl.offs_base += len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   143
static inline size_t SlGetOffs() {return _sl.offs_base - (_sl.bufe - _sl.bufp);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   145
/** Return the size in bytes of a certain type of normal/atomic variable
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   146
 * as it appears in memory. See VarTypes
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   147
 * @param conv VarType type of variable that is used for calculating the size
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   148
 * @return Return the size of this type in bytes */
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   149
static inline byte SlCalcConvMemLen(VarType conv)
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   150
{
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   151
	static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
3625
929df0b3484a (svn r4524) - Codechange: Use GB() macros for getting the saveload file/memory types
Darkvater
parents: 3585
diff changeset
   152
	byte length = GB(conv, 4, 4);
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   153
	assert(length < lengthof(conv_mem_size));
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   154
	return conv_mem_size[length];
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   155
}
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   156
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   157
/** Return the size in bytes of a certain type of normal/atomic variable
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   158
 * as it appears in a saved game. See VarTypes
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   159
 * @param conv VarType type of variable that is used for calculating the size
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   160
 * @return Return the size of this type in bytes */
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   161
static inline byte SlCalcConvFileLen(VarType conv)
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   162
{
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   163
	static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
3625
929df0b3484a (svn r4524) - Codechange: Use GB() macros for getting the saveload file/memory types
Darkvater
parents: 3585
diff changeset
   164
	byte length = GB(conv, 0, 4);
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   165
	assert(length < lengthof(conv_file_size));
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   166
	return conv_file_size[length];
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   167
}
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   168
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   169
/** Return the size in bytes of a reference (pointer) */
7054
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   170
static inline size_t SlCalcRefLen() {return CheckSavegameVersion(69) ? 2 : 4;}
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   171
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   172
/** Flush the output buffer by writing to disk with the given reader.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   173
 * If the buffer pointer has not yet been set up, set it up now. Usually
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   174
 * only called when the buffer is full, or there is no more data to be processed
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   175
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   176
static void SlWriteFill()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
{
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   178
	/* flush the buffer to disk (the writer) */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
	if (_sl.bufp != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
		uint len = _sl.bufp - _sl.buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
		_sl.offs_base += len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
		if (len) _sl.write_bytes(len);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   184
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   185
	/* All the data from the buffer has been written away, rewind to the beginning
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   186
	 * to start reading in more data */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
	_sl.bufp = _sl.buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
	_sl.bufe = _sl.buf + _sl.bufsize;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   191
/** Read in a single byte from file. If the temporary buffer is full,
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   192
 * flush it to its final destination
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   193
 * @return return the read byte from file
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   194
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   195
static inline byte SlReadByteInternal()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
	if (_sl.bufp == _sl.bufe) SlReadFill();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
	return *_sl.bufp++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   201
/** Wrapper for SlReadByteInternal */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   202
byte SlReadByte() {return SlReadByteInternal();}
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   203
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   204
/** Write away a single byte from memory. If the temporary buffer is full,
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   205
 * flush it to its destination (file)
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   206
 * @param b the byte that is currently written
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   207
 */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   208
static inline void SlWriteByteInternal(byte b)
1514
b389a8fca4e0 (svn r2018) Yet another workaround
tron
parents: 1513
diff changeset
   209
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   210
	if (_sl.bufp == _sl.bufe) SlWriteFill();
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   211
	*_sl.bufp++ = b;
1514
b389a8fca4e0 (svn r2018) Yet another workaround
tron
parents: 1513
diff changeset
   212
}
b389a8fca4e0 (svn r2018) Yet another workaround
tron
parents: 1513
diff changeset
   213
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   214
/** Wrapper for SlWriteByteInternal */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   215
void SlWriteByte(byte b) {SlWriteByteInternal(b);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   217
static inline int SlReadUint16()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
	int x = SlReadByte() << 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
	return x | SlReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   223
static inline uint32 SlReadUint32()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
	uint32 x = SlReadUint16() << 16;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
	return x | SlReadUint16();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   229
static inline uint64 SlReadUint64()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
	uint32 x = SlReadUint32();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
	uint32 y = SlReadUint32();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
	return (uint64)x << 32 | y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
2144
eb9f5557b52f (svn r2654) SlWriteUint16() should have a uint16 as parameter, not some arbitrary enum
tron
parents: 2141
diff changeset
   236
static inline void SlWriteUint16(uint16 v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
{
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2144
diff changeset
   238
	SlWriteByte(GB(v, 8, 8));
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2144
diff changeset
   239
	SlWriteByte(GB(v, 0, 8));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   242
static inline void SlWriteUint32(uint32 v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
{
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2144
diff changeset
   244
	SlWriteUint16(GB(v, 16, 16));
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2144
diff changeset
   245
	SlWriteUint16(GB(v,  0, 16));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   248
static inline void SlWriteUint64(uint64 x)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
	SlWriteUint32((uint32)(x >> 32));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
	SlWriteUint32((uint32)x);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   254
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   255
 * Read in the header descriptor of an object or an array.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   256
 * If the highest bit is set (7), then the index is bigger than 127
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   257
 * elements, so use the next byte to read in the real value.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   258
 * The actual value is then both bytes added with the first shifted
1886
0c760142c249 (svn r2392) - Fix some typos (Tron)
Darkvater
parents: 1885
diff changeset
   259
 * 8 bits to the left, and dropping the highest bit (which only indicated a big index).
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   260
 * x = ((x & 0x7F) << 8) + SlReadByte();
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   261
 * @return Return the value of the index
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   262
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   263
static uint SlReadSimpleGamma()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   265
	uint i = SlReadByte();
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7860
diff changeset
   266
	if (HasBit(i, 7)) {
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   267
		i &= ~0x80;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7860
diff changeset
   268
		if (HasBit(i, 6)) {
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   269
			i &= ~0x40;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7860
diff changeset
   270
			if (HasBit(i, 5)) {
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   271
				i &= ~0x20;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7860
diff changeset
   272
				if (HasBit(i, 4))
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
   273
					SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unsupported gamma");
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   274
				i = (i << 8) | SlReadByte();
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   275
			}
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   276
			i = (i << 8) | SlReadByte();
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   277
		}
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   278
		i = (i << 8) | SlReadByte();
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   279
	}
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   280
	return i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   283
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   284
 * Write the header descriptor of an object or an array.
1886
0c760142c249 (svn r2392) - Fix some typos (Tron)
Darkvater
parents: 1885
diff changeset
   285
 * If the element is bigger than 127, use 2 bytes for saving
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   286
 * and use the highest byte of the first written one as a notice
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   287
 * that the length consists of 2 bytes, etc.. like this:
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   288
 * 0xxxxxxx
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   289
 * 10xxxxxx xxxxxxxx
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   290
 * 110xxxxx xxxxxxxx xxxxxxxx
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   291
 * 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   292
 * @param i Index being written
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   293
 */
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   294
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   295
static void SlWriteSimpleGamma(size_t i)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   297
	if (i >= (1 << 7)) {
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   298
		if (i >= (1 << 14)) {
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   299
			if (i >= (1 << 21)) {
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   300
				assert(i < (1 << 28));
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   301
				SlWriteByte((byte)(0xE0 | (i >> 24)));
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6486
diff changeset
   302
				SlWriteByte((byte)(i >> 16));
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   303
			} else {
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   304
				SlWriteByte((byte)(0xC0 | (i >> 16)));
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   305
			}
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6486
diff changeset
   306
			SlWriteByte((byte)(i >> 8));
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   307
		} else {
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6486
diff changeset
   308
			SlWriteByte((byte)(0x80 | (i >> 8)));
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   309
		}
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   310
	}
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   311
	SlWriteByte((byte)i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   314
/** Return how many bytes used to encode a gamma value */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   315
static inline uint SlGetGammaLength(size_t i)
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 7272
diff changeset
   316
{
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   317
	return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21));
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   318
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   320
static inline uint SlReadSparseIndex() {return SlReadSimpleGamma();}
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   321
static inline void SlWriteSparseIndex(uint index) {SlWriteSimpleGamma(index);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   323
static inline uint SlReadArrayLength() {return SlReadSimpleGamma();}
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   324
static inline void SlWriteArrayLength(size_t length) {SlWriteSimpleGamma(length);}
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   325
static inline uint SlGetArrayLength(size_t length) {return SlGetGammaLength(length);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
void SlSetArrayIndex(uint index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
	_sl.need_length = NL_WANTLENGTH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
	_sl.array_index = index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   333
static size_t _next_offs;
7100
a4dd8e1b9f6e (svn r10367) -Fix: do not crash horribly when the file you are trying to load is too short or decompressing it fails.
rubidium
parents: 7054
diff changeset
   334
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   335
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   336
 * Iterate through the elements of an array and read the whole thing
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   337
 * @return The index of the object, or -1 if we have reached the end of current block
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   338
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   339
int SlIterateArray()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   341
	int index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   342
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   343
	/* After reading in the whole array inside the loop
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   344
	 * we must have read in all the data, so we must be at end of current block. */
7100
a4dd8e1b9f6e (svn r10367) -Fix: do not crash horribly when the file you are trying to load is too short or decompressing it fails.
rubidium
parents: 7054
diff changeset
   345
	if (_next_offs != 0 && SlGetOffs() != _next_offs) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Invalid chunk size");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   347
	while (true) {
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   348
		uint length = SlReadArrayLength();
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   349
		if (length == 0) {
7100
a4dd8e1b9f6e (svn r10367) -Fix: do not crash horribly when the file you are trying to load is too short or decompressing it fails.
rubidium
parents: 7054
diff changeset
   350
			_next_offs = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
			return -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   354
		_sl.obj_len = --length;
7100
a4dd8e1b9f6e (svn r10367) -Fix: do not crash horribly when the file you are trying to load is too short or decompressing it fails.
rubidium
parents: 7054
diff changeset
   355
		_next_offs = SlGetOffs() + length;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   357
		switch (_sl.block_mode) {
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   358
		case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   359
		case CH_ARRAY:        index = _sl.array_index++; break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
		default:
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   361
			DEBUG(sl, 0, "SlIterateArray error");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
			return -1; // error
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   364
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   365
		if (length != 0) return index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   369
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   370
 * Sets the length of either a RIFF object or the number of items in an array.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   371
 * This lets us load an object or an array of arbitrary size
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   372
 * @param length The length of the sought object/array
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   373
 */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   374
void SlSetLength(size_t length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
{
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   376
	assert(_sl.save);
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   377
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   378
	switch (_sl.need_length) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
	case NL_WANTLENGTH:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
		_sl.need_length = NL_NONE;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   381
		switch (_sl.block_mode) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
		case CH_RIFF:
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   383
			/* Ugly encoding of >16M RIFF chunks
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   384
			 * The lower 24 bits are normal
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   385
			 * The uppermost 4 bits are bits 24:27 */
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8965
diff changeset
   386
			assert(length < (1 << 28));
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   387
			SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   388
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
		case CH_ARRAY:
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   390
			assert(_sl.last_array_index <= _sl.array_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
			while (++_sl.last_array_index <= _sl.array_index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
				SlWriteArrayLength(1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
			SlWriteArrayLength(length + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
		case CH_SPARSE_ARRAY:
3509
bd375685efd1 (svn r4360) - CodeChange: add shortcut to SlGetArrayLength of the gamma-function along the lines of the Write/Read functions
Darkvater
parents: 3431
diff changeset
   396
			SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
			SlWriteSparseIndex(_sl.array_index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
		default: NOT_REACHED();
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   400
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
	case NL_CALCLENGTH:
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   402
		_sl.obj_len += (int)length;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   407
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   408
 * Save/Load bytes. These do not need to be converted to Little/Big Endian
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   409
 * so directly write them or read them to/from file
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   410
 * @param ptr The source or destination of the object being manipulated
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   411
 * @param length number of bytes this fast CopyBytes lasts
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   412
 */
410
0efd84450b01 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 222
diff changeset
   413
static void SlCopyBytes(void *ptr, size_t length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
	byte *p = (byte*)ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
	if (_sl.save) {
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   418
		for (; length != 0; length--) {SlWriteByteInternal(*p++);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
	} else {
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   420
		for (; length != 0; length--) {*p++ = SlReadByteInternal();}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
3117
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   424
/** Read in bytes from the file/data structure but don't do
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   425
 * anything with them, discarding them in effect
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   426
 * @param length The amount of bytes that is being treated this way
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   427
 */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   428
static inline void SlSkipBytes(size_t length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
{
3117
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   430
	for (; length != 0; length--) SlReadByte();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   433
/* Get the length of the current object */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   434
size_t SlGetFieldLength() {return _sl.obj_len;}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   436
/** Return a signed-long version of the value of a setting
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   437
 * @param ptr pointer to the variable
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   438
 * @param conv type of variable, can be a non-clean
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   439
 * type, eg one with other flags because it is parsed
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   440
 * @return returns the value of the pointer-setting */
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   441
int64 ReadValue(const void *ptr, VarType conv)
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   442
{
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   443
	switch (GetVarMemType(conv)) {
3109
68dd88833161 (svn r3712) - Change the boolean assignment/reading from == 1 to != 0 as suggested by Tron.
Darkvater
parents: 3108
diff changeset
   444
	case SLE_VAR_BL:  return (*(bool*)ptr != 0);
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   445
	case SLE_VAR_I8:  return *(int8*  )ptr;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   446
	case SLE_VAR_U8:  return *(byte*  )ptr;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   447
	case SLE_VAR_I16: return *(int16* )ptr;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   448
	case SLE_VAR_U16: return *(uint16*)ptr;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   449
	case SLE_VAR_I32: return *(int32* )ptr;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   450
	case SLE_VAR_U32: return *(uint32*)ptr;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   451
	case SLE_VAR_I64: return *(int64* )ptr;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   452
	case SLE_VAR_U64: return *(uint64*)ptr;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   453
	case SLE_VAR_NULL:return 0;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   454
	default: NOT_REACHED();
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   455
	}
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   456
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   457
	/* useless, but avoids compiler warning this way */
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   458
	return 0;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   459
}
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   460
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   461
/** Write the value of a setting
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   462
 * @param ptr pointer to the variable
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   463
 * @param conv type of variable, can be a non-clean type, eg
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6333
diff changeset
   464
 *             with other flags. It is parsed upon read
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6333
diff changeset
   465
 * @param val the new value being given to the variable */
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   466
void WriteValue(void *ptr, VarType conv, int64 val)
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   467
{
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   468
	switch (GetVarMemType(conv)) {
3109
68dd88833161 (svn r3712) - Change the boolean assignment/reading from == 1 to != 0 as suggested by Tron.
Darkvater
parents: 3108
diff changeset
   469
	case SLE_VAR_BL:  *(bool  *)ptr = (val != 0);  break;
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   470
	case SLE_VAR_I8:  *(int8  *)ptr = val; break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   471
	case SLE_VAR_U8:  *(byte  *)ptr = val; break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   472
	case SLE_VAR_I16: *(int16 *)ptr = val; break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   473
	case SLE_VAR_U16: *(uint16*)ptr = val; break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   474
	case SLE_VAR_I32: *(int32 *)ptr = val; break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   475
	case SLE_VAR_U32: *(uint32*)ptr = val; break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   476
	case SLE_VAR_I64: *(int64 *)ptr = val; break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   477
	case SLE_VAR_U64: *(uint64*)ptr = val; break;
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   478
	case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   479
	case SLE_VAR_NULL: break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   480
	default: NOT_REACHED();
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   481
	}
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   482
}
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   483
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   484
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   485
 * Handle all conversion and typechecking of variables here.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   486
 * In the case of saving, read in the actual value from the struct
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   487
 * and then write them to file, endian safely. Loading a value
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   488
 * goes exactly the opposite way
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   489
 * @param ptr The object being filled/read
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   490
 * @param conv VarType type of the current element of the struct
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   491
 */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   492
static void SlSaveLoadConv(void *ptr, VarType conv)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
	int64 x = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   495
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   496
	if (_sl.save) { // SAVE values
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   497
		/* Read a value from the struct. These ARE endian safe. */
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   498
		x = ReadValue(ptr, conv);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   500
		/* Write the value to the file and check if its value is in the desired range */
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   501
		switch (GetVarFileType(conv)) {
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   502
		case SLE_FILE_I8: assert(x >= -128 && x <= 127);     SlWriteByte(x);break;
2026
567e3bc9af72 (svn r2535) Tabs
tron
parents: 1993
diff changeset
   503
		case SLE_FILE_U8: assert(x >= 0 && x <= 255);        SlWriteByte(x);break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
		case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
		case SLE_FILE_STRINGID:
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   506
		case SLE_FILE_U16:assert(x >= 0 && x <= 65535);      SlWriteUint16(x);break;
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   507
		case SLE_FILE_I32:
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   508
		case SLE_FILE_U32:                                   SlWriteUint32((uint32)x);break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   509
		case SLE_FILE_I64:
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   510
		case SLE_FILE_U64:                                   SlWriteUint64(x);break;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   511
		default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
		}
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   513
	} else { // LOAD values
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   514
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   515
		/* Read a value from the file */
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   516
		switch (GetVarFileType(conv)) {
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   517
		case SLE_FILE_I8:  x = (int8  )SlReadByte();   break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   518
		case SLE_FILE_U8:  x = (byte  )SlReadByte();   break;
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   519
		case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
		case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   521
		case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
		case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   523
		case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
		case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
		case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   526
		default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   529
		/* Write The value to the struct. These ARE endian safe. */
3108
797699723806 (svn r3711) - Extract the WriteValue() and ReadValue() parts of the saveload code to assign/read to/from a variable. Preparatory work to make this the general function type for such assignments
Darkvater
parents: 3073
diff changeset
   530
		WriteValue(ptr, conv, x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   534
/** Calculate the net length of a string. This is in almost all cases
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   535
 * just strlen(), but if the string is not properly terminated, we'll
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   536
 * resort to the maximum length of the buffer.
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   537
 * @param ptr pointer to the stringbuffer
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   538
 * @param length maximum length of the string (buffer). If -1 we don't care
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   539
 * about a maximum length, but take string length as it is.
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   540
 * @return return the net length of the string */
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   541
static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   542
{
7179
7f456e0f628a (svn r10453) -Codechange: Allow save/load of empty (unallocated) strings
peter1138
parents: 7138
diff changeset
   543
	if (ptr == NULL) return 0;
9156
b9618a02f28b (svn r13019) -Fix [FS#1997]: silence some more MSVC x64 warnings (michi_cc)
glx
parents: 9101
diff changeset
   544
	return min(strlen(ptr), length - 1);
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   545
}
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   546
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   547
/** Calculate the gross length of the string that it
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   548
 * will occupy in the savegame. This includes the real length, returned
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   549
 * by SlCalcNetStringLen and the length that the index will occupy.
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   550
 * @param ptr pointer to the stringbuffer
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   551
 * @param length maximum length of the string (buffer size, etc.)
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6333
diff changeset
   552
 * @param conv type of data been used
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   553
 * @return return the gross length of the string */
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   554
static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   555
{
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   556
	size_t len;
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   557
	const char *str;
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   558
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   559
	switch (GetVarMemType(conv)) {
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   560
		default: NOT_REACHED();
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   561
		case SLE_VAR_STR:
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   562
		case SLE_VAR_STRQ:
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   563
			str = *(const char**)ptr;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   564
			len = SIZE_MAX;
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   565
			break;
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   566
		case SLE_VAR_STRB:
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   567
		case SLE_VAR_STRBQ:
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   568
			str = (const char*)ptr;
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   569
			len = length;
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   570
			break;
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   571
	}
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   572
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   573
	len = SlCalcNetStringLen(str, len);
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   574
	return len + SlGetArrayLength(len); // also include the length of the index
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   575
}
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   576
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   577
/**
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   578
 * Save/Load a string.
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   579
 * @param ptr the string being manipulated
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6333
diff changeset
   580
 * @param length of the string (full length)
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   581
 * @param conv must be SLE_FILE_STRING */
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   582
static void SlString(void *ptr, size_t length, VarType conv)
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   583
{
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   584
	size_t len;
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   585
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   586
	if (_sl.save) { // SAVE string
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   587
		switch (GetVarMemType(conv)) {
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   588
			default: NOT_REACHED();
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   589
			case SLE_VAR_STRB:
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   590
			case SLE_VAR_STRBQ:
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   591
				len = SlCalcNetStringLen((char*)ptr, length);
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   592
				break;
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   593
			case SLE_VAR_STR:
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   594
			case SLE_VAR_STRQ:
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   595
				ptr = *(char**)ptr;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   596
				len = SlCalcNetStringLen((char*)ptr, SIZE_MAX);
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   597
				break;
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   598
		}
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   599
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   600
		SlWriteArrayLength(len);
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   601
		SlCopyBytes(ptr, len);
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   602
	} else { // LOAD string
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   603
		len = SlReadArrayLength();
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   604
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   605
		switch (GetVarMemType(conv)) {
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   606
			default: NOT_REACHED();
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   607
			case SLE_VAR_STRB:
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   608
			case SLE_VAR_STRBQ:
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   609
				if (len >= length) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   610
					DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   611
					SlCopyBytes(ptr, length);
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   612
					SlSkipBytes(len - length);
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   613
					len = length - 1;
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   614
				} else {
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   615
					SlCopyBytes(ptr, len);
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   616
				}
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   617
				break;
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   618
			case SLE_VAR_STR:
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   619
			case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   620
				free(*(char**)ptr);
7179
7f456e0f628a (svn r10453) -Codechange: Allow save/load of empty (unallocated) strings
peter1138
parents: 7138
diff changeset
   621
				if (len == 0) {
7f456e0f628a (svn r10453) -Codechange: Allow save/load of empty (unallocated) strings
peter1138
parents: 7138
diff changeset
   622
					*(char**)ptr = NULL;
7f456e0f628a (svn r10453) -Codechange: Allow save/load of empty (unallocated) strings
peter1138
parents: 7138
diff changeset
   623
				} else {
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 8029
diff changeset
   624
					*(char**)ptr = MallocT<char>(len + 1); // terminating '\0'
7179
7f456e0f628a (svn r10453) -Codechange: Allow save/load of empty (unallocated) strings
peter1138
parents: 7138
diff changeset
   625
					ptr = *(char**)ptr;
7f456e0f628a (svn r10453) -Codechange: Allow save/load of empty (unallocated) strings
peter1138
parents: 7138
diff changeset
   626
					SlCopyBytes(ptr, len);
7f456e0f628a (svn r10453) -Codechange: Allow save/load of empty (unallocated) strings
peter1138
parents: 7138
diff changeset
   627
				}
5140
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   628
				break;
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   629
		}
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   630
b1097c36a598 (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   631
		((char*)ptr)[len] = '\0'; // properly terminate the string
3510
3b5a7d59a907 (svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
Darkvater
parents: 3509
diff changeset
   632
	}
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   633
}
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   634
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   635
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   636
 * Return the size in bytes of a certain type of atomic array
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   637
 * @param length The length of the array counted in elements
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   638
 * @param conv VarType type of the variable that is used in calculating the size
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   639
 */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   640
static inline size_t SlCalcArrayLen(size_t length, VarType conv)
2958
ac0a9673b522 (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   641
{
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   642
	return SlCalcConvFileLen(conv) * length;
2958
ac0a9673b522 (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   643
}
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   644
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   645
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   646
 * Save/Load an array.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   647
 * @param array The array being manipulated
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   648
 * @param length The length of the array in elements
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   649
 * @param conv VarType type of the atomic array (int, byte, uint64, etc.)
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   650
 */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   651
void SlArray(void *array, size_t length, VarType conv)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
{
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   653
	/* Automatically calculate the length? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
	if (_sl.need_length != NL_NONE) {
2958
ac0a9673b522 (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   655
		SlSetLength(SlCalcArrayLen(length, conv));
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   656
		/* Determine length only? */
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   657
		if (_sl.need_length == NL_CALCLENGTH) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   660
	/* NOTICE - handle some buggy stuff, in really old versions everything was saved
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   661
	 * as a byte-type. So detect this, and adjust array size accordingly */
2295
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
   662
	if (!_sl.save && _sl_version == 0) {
9455
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   663
		/* all arrays except difficulty settings */
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   664
		if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
   665
				conv == SLE_INT32 || conv == SLE_UINT32) {
9455
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   666
			SlCopyBytes(array, length * SlCalcConvFileLen(conv));
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   667
			return;
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   668
		}
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   669
		/* used for conversion of Money 32bit->64bit */
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   670
		if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   671
			for (uint i = 0; i < length; i++) {
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   672
				((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   673
			}
955a7bd9027f (svn r13373) -Fix (r10210): loading of very old savegames was broken
smatz
parents: 9413
diff changeset
   674
			return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   676
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   677
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   678
	/* If the size of elements is 1 byte both in file and memory, no special
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   679
	 * conversion is needed, use specialized copy-copy function to speed up things */
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   680
	if (conv == SLE_INT8 || conv == SLE_UINT8) {
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   681
		SlCopyBytes(array, length);
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   682
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
		byte *a = (byte*)array;
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   684
		byte mem_size = SlCalcConvMemLen(conv);
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   685
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   686
		for (; length != 0; length --) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
			SlSaveLoadConv(a, conv);
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   688
			a += mem_size; // get size
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   693
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   694
static uint ReferenceToInt(const void* obj, SLRefType rt);
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   695
static void* IntToReference(uint index, SLRefType rt);
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   696
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   697
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   698
/**
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   699
 * Return the size in bytes of a list
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   700
 * @param list The std::list to find the size of
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   701
 */
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   702
static inline size_t SlCalcListLen(const void *list)
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   703
{
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   704
	std::list<void *> *l = (std::list<void *> *) list;
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   705
7054
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   706
	int type_size = CheckSavegameVersion(69) ? 2 : 4;
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   707
	/* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   708
	 * of the list */
7054
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   709
	return l->size() * type_size + type_size;
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   710
}
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   711
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   712
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   713
/**
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   714
 * Save/Load a list.
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   715
 * @param list The list being manipulated
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   716
 * @param conv SLRefType type of the list (Vehicle *, Station *, etc)
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   717
 */
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   718
void SlList(void *list, SLRefType conv)
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   719
{
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   720
	/* Automatically calculate the length? */
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   721
	if (_sl.need_length != NL_NONE) {
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   722
		SlSetLength(SlCalcListLen(list));
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   723
		/* Determine length only? */
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   724
		if (_sl.need_length == NL_CALCLENGTH) return;
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   725
	}
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   726
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   727
	std::list<void *> *l = (std::list<void *> *) list;
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   728
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   729
	if (_sl.save) {
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   730
		SlWriteUint32((uint32)l->size());
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   731
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   732
		std::list<void *>::iterator iter;
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   733
		for (iter = l->begin(); iter != l->end(); ++iter) {
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   734
			void *ptr = *iter;
7054
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   735
			SlWriteUint32(ReferenceToInt(ptr, conv));
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   736
		}
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   737
	} else {
7054
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   738
		uint length = CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32();
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   739
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   740
		/* Load each reference and push to the end of the list */
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   741
		for (uint i = 0; i < length; i++) {
7054
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   742
			void *ptr = IntToReference(CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(), conv);
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   743
			l->push_back(ptr);
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   744
		}
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   745
	}
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   746
}
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   747
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   748
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   749
/** Are we going to save this object or not? */
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   750
static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   751
{
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   752
	if (_sl_version < sld->version_from || _sl_version > sld->version_to) return false;
3117
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   753
	if (sld->conv & SLF_SAVE_NO) return false;
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   754
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   755
	return true;
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   756
}
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   757
3117
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   758
/** Are we going to load this variable when loading a savegame or not?
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   759
 * @note If the variable is skipped it is skipped in the savegame
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   760
 * bytestream itself as well, so there is no need to skip it somewhere else */
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   761
static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   762
{
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   763
	if ((sld->conv & SLF_NETWORK_NO) && !_sl.save && _networking && !_network_server) {
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   764
		SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   765
		return true;
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   766
	}
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   767
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   768
	return false;
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   769
}
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   770
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   771
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   772
 * Calculate the size of an object.
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6333
diff changeset
   773
 * @param object to be measured
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   774
 * @param sld The SaveLoad description of the object so we know how to manipulate it
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6333
diff changeset
   775
 * @return size of given objetc
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   776
 */
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
   777
size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   778
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   779
	size_t length = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   781
	/* Need to determine the length and write a length tag. */
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   782
	for (; sld->cmd != SL_END; sld++) {
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   783
		length += SlCalcObjMemberLength(object, sld);
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   784
	}
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   785
	return length;
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   786
}
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   787
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   788
size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   789
{
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   790
	assert(_sl.save);
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   791
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   792
	switch (sld->cmd) {
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   793
		case SL_VAR:
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   794
		case SL_REF:
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   795
		case SL_ARR:
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   796
		case SL_STR:
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   797
		case SL_LST:
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   798
			/* CONDITIONAL saveload types depend on the savegame version */
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   799
			if (!SlIsObjectValidInSavegame(sld)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   801
			switch (sld->cmd) {
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   802
			case SL_VAR: return SlCalcConvFileLen(sld->conv);
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   803
			case SL_REF: return SlCalcRefLen();
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   804
			case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   805
			case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   806
			case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   807
			default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
			}
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   809
			break;
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   810
		case SL_WRITEBYTE: return 1; // a byte is logically of size 1
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7449
diff changeset
   811
		case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   812
		default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   813
	}
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   814
	return 0;
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   815
}
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   816
4039
c4dcd5834f6b (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   817
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   818
bool SlObjectMember(void *ptr, const SaveLoad *sld)
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   819
{
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   820
	VarType conv = GB(sld->conv, 0, 8);
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   821
	switch (sld->cmd) {
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   822
	case SL_VAR:
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   823
	case SL_REF:
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   824
	case SL_ARR:
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   825
	case SL_STR:
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   826
	case SL_LST:
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   827
		/* CONDITIONAL saveload types depend on the savegame version */
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   828
		if (!SlIsObjectValidInSavegame(sld)) return false;
3117
4c9e8842598e (svn r3721) - [3/4] Present the game with a unified structure for the configuration-ini, saveload, console and gui representations of the settings. From part 3 on, OpenTTD is once again compilable.
Darkvater
parents: 3112
diff changeset
   829
		if (SlSkipVariableOnLoad(sld)) return false;
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   830
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   831
		switch (sld->cmd) {
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   832
		case SL_VAR: SlSaveLoadConv(ptr, conv); break;
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   833
		case SL_REF: // Reference variable, translate
7054
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   834
			if (_sl.save) {
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   835
				SlWriteUint32(ReferenceToInt(*(void**)ptr, (SLRefType)conv));
4039
c4dcd5834f6b (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   836
			} else {
7054
780add6d508b (svn r10319) -Fix (r10266): the limit of 65535 references was not enough for cargo packets. Increase this limit to approximately 2^32, which noone should ever be able to reach on any normal system ;)
rubidium
parents: 7036
diff changeset
   837
				*(void**)ptr = IntToReference(CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(), (SLRefType)conv);
4039
c4dcd5834f6b (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   838
			}
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   839
			break;
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   840
		case SL_ARR: SlArray(ptr, sld->length, conv); break;
3048
adf46229d390 (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later.
Darkvater
parents: 3046
diff changeset
   841
		case SL_STR: SlString(ptr, sld->length, conv); break;
6499
e46cf40d8743 (svn r9682) -Codechange: Add support for saving/loading std::lists containing object references (REF_*)
peter1138
parents: 6491
diff changeset
   842
		case SL_LST: SlList(ptr, (SLRefType)conv); break;
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   843
		default: NOT_REACHED();
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   844
		}
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   845
		break;
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   846
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   847
	/* SL_WRITEBYTE translates a value of a variable to another one upon
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   848
	 * saving or loading.
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   849
	 * XXX - variable renaming abuse
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   850
	 * game_value: the value of the variable ingame is abused by sld->version_from
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   851
	 * file_value: the value of the variable in the savegame is abused by sld->version_to */
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   852
	case SL_WRITEBYTE:
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   853
		if (_sl.save) {
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   854
			SlWriteByte(sld->version_to);
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   855
		} else {
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   856
			*(byte*)ptr = sld->version_from;
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   857
		}
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   858
		break;
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
   859
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7449
diff changeset
   860
	/* SL_VEH_INCLUDE loads common code for vehicles */
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7449
diff changeset
   861
	case SL_VEH_INCLUDE:
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7449
diff changeset
   862
		SlObject(ptr, GetVehicleDescription(VEH_END));
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   863
		break;
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   864
	default: NOT_REACHED();
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   865
	}
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   866
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   869
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   870
 * Main SaveLoad function.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   871
 * @param object The object that is being saved or loaded
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   872
 * @param sld The SaveLoad description of the object so we know how to manipulate it
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   873
 */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   874
void SlObject(void *object, const SaveLoad *sld)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
{
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   876
	/* Automatically calculate the length? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
	if (_sl.need_length != NL_NONE) {
5142
ee34e9968a62 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   878
		SlSetLength(SlCalcObjLength(object, sld));
3044
fbaef2401476 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   879
		if (_sl.need_length == NL_CALCLENGTH) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   881
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   882
	for (; sld->cmd != SL_END; sld++) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
   883
		void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld);
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   884
		SlObjectMember(ptr, sld);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   888
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   889
 * Save or Load (a list of) global variables
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6333
diff changeset
   890
 * @param sldg The global variable that is being loaded or saved
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   891
 */
3046
baa216f9911a (svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
Darkvater
parents: 3044
diff changeset
   892
void SlGlobList(const SaveLoadGlobVarList *sldg)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
{
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
   894
	SlObject(NULL, (const SaveLoad*)sldg);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   897
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   898
 * Do something of which I have no idea what it is :P
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   899
 * @param proc The callback procedure that is called
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   900
 * @param arg The variable that will be used for the callback procedure
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   901
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
void SlAutolength(AutolengthProc *proc, void *arg)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
{
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   904
	size_t offs;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
	assert(_sl.save);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   908
	/* Tell it to calculate the length */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   909
	_sl.need_length = NL_CALCLENGTH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
	_sl.obj_len = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
	proc(arg);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   913
	/* Setup length */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
	_sl.need_length = NL_WANTLENGTH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   915
	SlSetLength(_sl.obj_len);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   916
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   917
	offs = SlGetOffs() + _sl.obj_len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   918
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   919
	/* And write the stuff */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   920
	proc(arg);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   921
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
   922
	if (offs != SlGetOffs()) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Invalid chunk size");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   925
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   926
 * Load a chunk of data (eg vehicles, stations, etc.)
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   927
 * @param ch The chunkhandler that will be used for the operation
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   928
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
static void SlLoadChunk(const ChunkHandler *ch)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
	byte m = SlReadByte();
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   932
	size_t len;
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
   933
	size_t endoffs;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
	_sl.block_mode = m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
	_sl.obj_len = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   937
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   938
	switch (m) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
	case CH_ARRAY:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
		_sl.array_index = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
		ch->load_proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
	case CH_SPARSE_ARRAY:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
		ch->load_proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
		break;
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   946
	default:
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   947
		if ((m & 0xF) == CH_RIFF) {
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   948
			/* Read length */
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   949
			len = (SlReadByte() << 16) | ((m >> 4) << 24);
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   950
			len += SlReadUint16();
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   951
			_sl.obj_len = len;
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   952
			endoffs = SlGetOffs() + len;
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   953
			ch->load_proc();
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
   954
			if (SlGetOffs() != endoffs) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Invalid chunk size");
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   955
		} else {
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
   956
			SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Invalid chunk type");
2041
2210f5c42cc8 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   957
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   962
/* Stub Chunk handlers to only calculate length and do nothing else */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
static ChunkSaveLoadProc *_tmp_proc_1;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   964
static inline void SlStubSaveProc2(void *arg) {_tmp_proc_1();}
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   965
static void SlStubSaveProc() {SlAutolength(SlStubSaveProc2, NULL);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   967
/** Save a chunk of data (eg. vehicles, stations, etc.). Each chunk is
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   968
 * prefixed by an ID identifying it, followed by data, and terminator where appropiate
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   969
 * @param ch The chunkhandler that will be used for the operation
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   970
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
static void SlSaveChunk(const ChunkHandler *ch)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   973
	ChunkSaveLoadProc *proc = ch->save_proc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   975
	/* Don't save any chunk information if there is no save handler. */
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   976
	if (proc == NULL) return;
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   977
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
	SlWriteUint32(ch->id);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   979
	DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
	if (ch->flags & CH_AUTO_LENGTH) {
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
   982
		/* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
		_tmp_proc_1 = proc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
		proc = SlStubSaveProc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   986
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
	_sl.block_mode = ch->flags & CH_TYPE_MASK;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
   988
	switch (ch->flags & CH_TYPE_MASK) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
	case CH_RIFF:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
		_sl.need_length = NL_WANTLENGTH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
		proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
	case CH_ARRAY:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
		_sl.last_array_index = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   995
		SlWriteByte(CH_ARRAY);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
		proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
		SlWriteArrayLength(0); // Terminate arrays
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
	case CH_SPARSE_ARRAY:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
		SlWriteByte(CH_SPARSE_ARRAY);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
		proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
		SlWriteArrayLength(0); // Terminate arrays
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
		break;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1004
	default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1008
/** Save all chunks */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1009
static void SlSaveChunks()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
	const ChunkHandler *ch;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1012
	const ChunkHandler* const *chsc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
	uint p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1015
	for (p = 0; p != CH_NUM_PRI_LEVELS; p++) {
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1016
		for (chsc = _sl.chs; (ch = *chsc++) != NULL;) {
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1017
			while (true) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
				if (((ch->flags >> CH_PRI_SHL) & (CH_NUM_PRI_LEVELS - 1)) == p)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1019
					SlSaveChunk(ch);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
				if (ch->flags & CH_LAST)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1021
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1022
				ch++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1023
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1025
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1026
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1027
	/* Terminator */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
	SlWriteUint32(0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1029
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1030
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1031
/** Find the ChunkHandler that will be used for processing the found
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1032
 * chunk in the savegame or in memory
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1033
 * @param id the chunk in question
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1034
 * @return returns the appropiate chunkhandler
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1035
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
static const ChunkHandler *SlFindChunkHandler(uint32 id)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
	const ChunkHandler *ch;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1039
	const ChunkHandler *const *chsc;
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6486
diff changeset
  1040
	for (chsc = _sl.chs; (ch = *chsc++) != NULL;) {
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2927
diff changeset
  1041
		for (;;) {
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2927
diff changeset
  1042
			if (ch->id == id) return ch;
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2927
diff changeset
  1043
			if (ch->flags & CH_LAST) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1044
			ch++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1046
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1047
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1050
/** Load all chunks */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1051
static void SlLoadChunks()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
	uint32 id;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
	const ChunkHandler *ch;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1056
	for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1057
		DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1058
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
		ch = SlFindChunkHandler(id);
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1060
		if (ch == NULL) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unknown chunk type");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
		SlLoadChunk(ch);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1062
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1065
/*******************************************
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1066
 ********** START OF LZO CODE **************
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1067
 *******************************************/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1068
#define LZO_SIZE 8192
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 762
diff changeset
  1070
#include "minilzo.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1072
static size_t ReadLZO()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
	byte out[LZO_SIZE + LZO_SIZE / 64 + 16 + 3 + 8];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1075
	uint32 tmp[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
	uint32 size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
	uint len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1079
	/* Read header*/
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1080
	if (fread(tmp, sizeof(tmp), 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1082
	/* Check if size is bad */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
	((uint32*)out)[0] = size = tmp[1];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1084
2295
95e8858e9938 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
  1085
	if (_sl_version != 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
		tmp[0] = TO_BE32(tmp[0]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
		size = TO_BE32(size);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1090
	if (size >= sizeof(out)) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Inconsistent size");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1092
	/* Read block */
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1093
	if (fread(out + sizeof(uint32), size, 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1094
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1095
	/* Verify checksum */
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1096
	if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Bad checksum");
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1097
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1098
	/* Decompress */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1099
	lzo1x_decompress(out + sizeof(uint32)*1, size, _sl.buf, &len, NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1100
	return len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1101
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1102
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1103
/* p contains the pointer to the buffer, len contains the pointer to the length.
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1104
 * len bytes will be written, p and l will be updated to reflect the next buffer. */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1105
static void WriteLZO(size_t size)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1106
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1107
	byte out[LZO_SIZE + LZO_SIZE / 64 + 16 + 3 + 8];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1108
	byte wrkmem[sizeof(byte*)*4096];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1109
	uint outlen;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1110
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1111
	lzo1x_1_compress(_sl.buf, (lzo_uint)size, out + sizeof(uint32)*2, &outlen, wrkmem);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1112
	((uint32*)out)[1] = TO_BE32(outlen);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
	((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1114
	if (fwrite(out, outlen + sizeof(uint32)*2, 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1117
static bool InitLZO()
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1118
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
	_sl.bufsize = LZO_SIZE;
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 8029
diff changeset
  1120
	_sl.buf = _sl.buf_ori = MallocT<byte>(LZO_SIZE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1124
static void UninitLZO()
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1125
{
2414
cc0904ef2d0d (svn r2940) (Internal) Remove the need for the 'tmp' variable for _sl.buf, instead operate on _sl.buf_ori which is not changed for init/deinit.
Darkvater
parents: 2413
diff changeset
  1126
	free(_sl.buf_ori);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1129
/*********************************************
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1130
 ******** START OF NOCOMP CODE (uncompressed)*
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1131
 *********************************************/
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1132
static size_t ReadNoComp()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1133
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1134
	return fread(_sl.buf, 1, LZO_SIZE, _sl.fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1135
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1137
static void WriteNoComp(size_t size)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
{
9100
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9097
diff changeset
  1139
	if (fwrite(_sl.buf, 1, size, _sl.fh) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1142
static bool InitNoComp()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
	_sl.bufsize = LZO_SIZE;
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 8029
diff changeset
  1145
	_sl.buf = _sl.buf_ori = MallocT<byte>(LZO_SIZE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1148
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1149
static void UninitNoComp()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
{
2414
cc0904ef2d0d (svn r2940) (Internal) Remove the need for the 'tmp' variable for _sl.buf, instead operate on _sl.buf_ori which is not changed for init/deinit.
Darkvater
parents: 2413
diff changeset
  1151
	free(_sl.buf_ori);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1154
/********************************************
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1155
 ********** START OF MEMORY CODE (in ram)****
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1156
 ********************************************/
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1157
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1158
#include "table/sprites.h"
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1159
#include "gui.h"
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1160
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1161
struct ThreadedSave {
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1162
	uint count;
5719
2bb88fe71d01 (svn r8214) -Fix (r8038): The fast forward flag stores 2 bits, not 1. Remember the
peter1138
parents: 5705
diff changeset
  1163
	byte ff_state;
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1164
	bool saveinprogress;
1914
2b4b3c3a95b4 (svn r2420) - Codechange: magic number elminitation of cursorsprites.
Darkvater
parents: 1913
diff changeset
  1165
	CursorID cursor;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1166
};
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1167
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1168
/* A maximum size of of 128K * 500 = 64.000KB savegames */
5216
8bd14ee39af2 (svn r7331) - Codechange: Rename all memory pool macro's and types to "old pool", so the new pool implementation can be committed alongside it.
matthijs
parents: 5211
diff changeset
  1169
STATIC_OLD_POOL(Savegame, byte, 17, 500, NULL, NULL)
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1170
static ThreadedSave _ts;
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1171
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1172
static bool InitMem()
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1173
{
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1174
	_ts.count = 0;
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1175
7401
7a72cc2a1196 (svn r10773) -Codechange: use pool.CleanPool instead of CleanPool(&pool) and similarly for AddBlock*.
rubidium
parents: 7375
diff changeset
  1176
	_Savegame_pool.CleanPool();
7a72cc2a1196 (svn r10773) -Codechange: use pool.CleanPool instead of CleanPool(&pool) and similarly for AddBlock*.
rubidium
parents: 7375
diff changeset
  1177
	_Savegame_pool.AddBlockToPool();
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1178
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1179
	/* A block from the pool is a contigious area of memory, so it is safe to write to it sequentially */
4985
045fa9806635 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1180
	_sl.bufsize = GetSavegamePoolSize();
045fa9806635 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1181
	_sl.buf = GetSavegame(_ts.count);
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1182
	return true;
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1183
}
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1184
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1185
static void UnInitMem()
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1186
{
7401
7a72cc2a1196 (svn r10773) -Codechange: use pool.CleanPool instead of CleanPool(&pool) and similarly for AddBlock*.
rubidium
parents: 7375
diff changeset
  1187
	_Savegame_pool.CleanPool();
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1188
}
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1189
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1190
static void WriteMem(size_t size)
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1191
{
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1192
	_ts.count += (uint)size;
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1193
	/* Allocate new block and new buffer-pointer */
7401
7a72cc2a1196 (svn r10773) -Codechange: use pool.CleanPool instead of CleanPool(&pool) and similarly for AddBlock*.
rubidium
parents: 7375
diff changeset
  1194
	_Savegame_pool.AddBlockIfNeeded(_ts.count);
4985
045fa9806635 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1195
	_sl.buf = GetSavegame(_ts.count);
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1196
}
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1197
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1198
/********************************************
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1199
 ********** START OF ZLIB CODE **************
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1200
 ********************************************/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
#if defined(WITH_ZLIB)
2694
9b5f6f369ab3 (svn r3236) - Fix: warnings about 'CDECL must be used with ...' on VS6
Darkvater
parents: 2685
diff changeset
  1203
#include <zlib.h>
2138
44ab9e23c669 (svn r2648) Only use _stdcall calling convention for zlib on win32/msvc.
ludde
parents: 2126
diff changeset
  1204
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1205
static z_stream _z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1206
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1207
static bool InitReadZlib()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
	memset(&_z, 0, sizeof(_z));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
	if (inflateInit(&_z) != Z_OK) return false;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1211
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1212
	_sl.bufsize = 4096;
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 8029
diff changeset
  1213
	_sl.buf = _sl.buf_ori = MallocT<byte>(4096 + 4096); // also contains fread buffer
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1214
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1217
static size_t ReadZlib()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1218
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1219
	int r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1220
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1221
	_z.next_out = _sl.buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1222
	_z.avail_out = 4096;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1223
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1224
	do {
9156
b9618a02f28b (svn r13019) -Fix [FS#1997]: silence some more MSVC x64 warnings (michi_cc)
glx
parents: 9101
diff changeset
  1225
		/* read more bytes from the file? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1226
		if (_z.avail_in == 0) {
9156
b9618a02f28b (svn r13019) -Fix [FS#1997]: silence some more MSVC x64 warnings (michi_cc)
glx
parents: 9101
diff changeset
  1227
			_z.avail_in = (uint)fread(_z.next_in = _sl.buf + 4096, 1, 4096, _sl.fh);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1228
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1229
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1230
		/* inflate the data */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1231
		r = inflate(&_z, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1232
		if (r == Z_STREAM_END)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1233
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1234
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1235
		if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1236
	} while (_z.avail_out);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1237
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1238
	return 4096 - _z.avail_out;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1239
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1240
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1241
static void UninitReadZlib()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1242
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1243
	inflateEnd(&_z);
2414
cc0904ef2d0d (svn r2940) (Internal) Remove the need for the 'tmp' variable for _sl.buf, instead operate on _sl.buf_ori which is not changed for init/deinit.
Darkvater
parents: 2413
diff changeset
  1244
	free(_sl.buf_ori);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1246
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1247
static bool InitWriteZlib()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1249
	memset(&_z, 0, sizeof(_z));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
	if (deflateInit(&_z, 6) != Z_OK) return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1252
	_sl.bufsize = 4096;
8037
8aa4ace04383 (svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
rubidium
parents: 8029
diff changeset
  1253
	_sl.buf = _sl.buf_ori = MallocT<byte>(4096); // also contains fread buffer
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1257
static void WriteZlibLoop(z_streamp z, byte *p, size_t len, int mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
{
1884
804ff5f94ed0 (svn r2390) - Codechange: Fix some warnings on GCC 4.0.0
hackykid
parents: 1881
diff changeset
  1259
	byte buf[1024]; // output buffer
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
	int r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
	uint n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
	z->next_in = p;
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1263
	z->avail_in = (uInt)len;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1264
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
		z->next_out = buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1266
		z->avail_out = sizeof(buf);
2026
567e3bc9af72 (svn r2535) Tabs
tron
parents: 1993
diff changeset
  1267
		r = deflate(z, mode);
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1268
			/* bytes were emitted? */
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8965
diff changeset
  1269
		if ((n = sizeof(buf) - z->avail_out) != 0) {
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1270
			if (fwrite(buf, n, 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1271
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
		if (r == Z_STREAM_END)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
			break;
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1274
		if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
	} while (z->avail_in || !z->avail_out);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9359
diff changeset
  1278
static void WriteZlib(size_t len)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1279
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
	WriteZlibLoop(&_z, _sl.buf, len, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1281
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1283
static void UninitWriteZlib()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1284
{
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1285
	/* flush any pending output. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
	if (_sl.fh) WriteZlibLoop(&_z, NULL, 0, Z_FINISH);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
	deflateEnd(&_z);
2414
cc0904ef2d0d (svn r2940) (Internal) Remove the need for the 'tmp' variable for _sl.buf, instead operate on _sl.buf_ori which is not changed for init/deinit.
Darkvater
parents: 2413
diff changeset
  1288
	free(_sl.buf_ori);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1289
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1291
#endif /* WITH_ZLIB */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1293
/*******************************************
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1294
 ************* END OF CODE *****************
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1295
 *******************************************/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1296
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1297
/* these define the chunks */
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1298
extern const ChunkHandler _gamelog_chunk_handlers[];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1299
extern const ChunkHandler _misc_chunk_handlers[];
8987
80d22b9c2a0c (svn r12782) -Codechange: remove some functions from functions.h and do not statically 'waste' memory when the old name array is not needed anymore.
rubidium
parents: 8969
diff changeset
  1300
extern const ChunkHandler _name_chunk_handlers[];
8965
29a591456a2f (svn r12757) -Codechange: move all cheat related stuff from all over the place to a single location.
rubidium
parents: 8935
diff changeset
  1301
extern const ChunkHandler _cheat_chunk_handlers[] ;
3112
a6fda4d4c7ca (svn r3716) - Move the option settings (_game_opt_desc) from misc.c into settings.c. This will be merged with SettingDesc misc_settings above as they are actually the same. No functionality has changed beside the fact that the settings are now in a different Chunkhandler.
Darkvater
parents: 3109
diff changeset
  1302
extern const ChunkHandler _setting_chunk_handlers[];
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10146
diff changeset
  1303
extern const ChunkHandler _company_chunk_handlers[];
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2828
diff changeset
  1304
extern const ChunkHandler _engine_chunk_handlers[];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1305
extern const ChunkHandler _veh_chunk_handlers[];
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1537
diff changeset
  1306
extern const ChunkHandler _waypoint_chunk_handlers[];
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1307
extern const ChunkHandler _depot_chunk_handlers[];
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 950
diff changeset
  1308
extern const ChunkHandler _order_chunk_handlers[];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1309
extern const ChunkHandler _town_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1310
extern const ChunkHandler _sign_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1311
extern const ChunkHandler _station_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1312
extern const ChunkHandler _industry_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1313
extern const ChunkHandler _economy_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1314
extern const ChunkHandler _animated_tile_chunk_handlers[];
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents: 5216
diff changeset
  1315
extern const ChunkHandler _newgrf_chunk_handlers[];
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6571
diff changeset
  1316
extern const ChunkHandler _group_chunk_handlers[];
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
  1317
extern const ChunkHandler _cargopacket_chunk_handlers[];
10127
4f53c1cfcfe9 (svn r14313) -Codechange: Move functions dealing with the EngineRenew pool to their own file.
frosch
parents: 10066
diff changeset
  1318
extern const ChunkHandler _autoreplace_chunk_handlers[];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1320
static const ChunkHandler * const _chunk_handlers[] = {
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1321
	_gamelog_chunk_handlers,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1322
	_misc_chunk_handlers,
8987
80d22b9c2a0c (svn r12782) -Codechange: remove some functions from functions.h and do not statically 'waste' memory when the old name array is not needed anymore.
rubidium
parents: 8969
diff changeset
  1323
	_name_chunk_handlers,
8965
29a591456a2f (svn r12757) -Codechange: move all cheat related stuff from all over the place to a single location.
rubidium
parents: 8935
diff changeset
  1324
	_cheat_chunk_handlers,
3112
a6fda4d4c7ca (svn r3716) - Move the option settings (_game_opt_desc) from misc.c into settings.c. This will be merged with SettingDesc misc_settings above as they are actually the same. No functionality has changed beside the fact that the settings are now in a different Chunkhandler.
Darkvater
parents: 3109
diff changeset
  1325
	_setting_chunk_handlers,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1326
	_veh_chunk_handlers,
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1537
diff changeset
  1327
	_waypoint_chunk_handlers,
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1328
	_depot_chunk_handlers,
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 950
diff changeset
  1329
	_order_chunk_handlers,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1330
	_industry_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1331
	_economy_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1332
	_engine_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1333
	_town_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1334
	_sign_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1335
	_station_chunk_handlers,
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10146
diff changeset
  1336
	_company_chunk_handlers,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1337
	_animated_tile_chunk_handlers,
5228
26dc9acf7d94 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents: 5216
diff changeset
  1338
	_newgrf_chunk_handlers,
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6571
diff changeset
  1339
	_group_chunk_handlers,
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
  1340
	_cargopacket_chunk_handlers,
10127
4f53c1cfcfe9 (svn r14313) -Codechange: Move functions dealing with the EngineRenew pool to their own file.
frosch
parents: 10066
diff changeset
  1341
	_autoreplace_chunk_handlers,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
	NULL,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1344
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1345
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1346
 * Pointers cannot be saved to a savegame, so this functions gets
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1347
 * the index of the item, and if not available, it hussles with
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1348
 * pointers (looks really bad :()
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1349
 * Remember that a NULL item has value 0, and all
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1350
 * indeces have +1, so vehicle 0 is saved as index 1.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1351
 * @param obj The object that we want to get the index of
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1352
 * @param rt SLRefType type of the object the index is being sought of
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1353
 * @return Return the pointer converted to an index of the type pointed to
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1354
 */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1355
static uint ReferenceToInt(const void *obj, SLRefType rt)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1356
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1357
	if (obj == NULL) return 0;
938
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1358
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1359
	switch (rt) {
938
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1360
		case REF_VEHICLE_OLD: // Old vehicles we save as new onces
2548
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1361
		case REF_VEHICLE:   return ((const  Vehicle*)obj)->index + 1;
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1362
		case REF_STATION:   return ((const  Station*)obj)->index + 1;
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1363
		case REF_TOWN:      return ((const     Town*)obj)->index + 1;
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1364
		case REF_ORDER:     return ((const    Order*)obj)->index + 1;
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1365
		case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2828
diff changeset
  1366
		case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
  1367
		case REF_CARGO_PACKET:  return ((const CargoPacket*)obj)->index + 1;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1368
		default: NOT_REACHED();
938
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1369
	}
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1370
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1371
	return 0; // avoid compiler warning
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1374
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1375
 * Pointers cannot be loaded from a savegame, so this function
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1376
 * gets the index from the savegame and returns the appropiate
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1377
 * pointer from the already loaded base.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1378
 * Remember that an index of 0 is a NULL pointer so all indeces
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1379
 * are +1 so vehicle 0 is saved as 1.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1380
 * @param index The index that is being converted to a pointer
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1381
 * @param rt SLRefType type of the object the pointer is sought of
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1382
 * @return Return the index converted to a pointer of any type
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1383
 */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1384
static void *IntToReference(uint index, SLRefType rt)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1386
	/* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1387
	 * and should be loaded like that */
8492
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1388
	if (rt == REF_VEHICLE_OLD && !CheckSavegameVersionOldStyle(4, 4)) {
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1389
		rt = REF_VEHICLE;
8492
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1390
	}
938
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1391
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1392
	/* No need to look up NULL pointers, just return immediately */
8492
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1393
	if (rt != REF_VEHICLE_OLD && index == 0) {
938
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1394
		return NULL;
8492
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1395
	}
938
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1396
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1397
	index--; // correct for the NULL index
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1398
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1399
	switch (rt) {
8492
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1400
		case REF_ORDER:
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1401
			if (_Order_pool.AddBlockIfNeeded(index)) return GetOrder(index);
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1402
			error("Orders: failed loading savegame: too many orders");
938
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1403
8492
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1404
		case REF_VEHICLE:
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1405
			if (_Vehicle_pool.AddBlockIfNeeded(index)) return GetVehicle(index);
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1406
			error("Vehicles: failed loading savegame: too many vehicles");
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1407
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1408
		case REF_STATION:
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1409
			if (_Station_pool.AddBlockIfNeeded(index)) return GetStation(index);
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1410
			error("Stations: failed loading savegame: too many stations");
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1411
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1412
		case REF_TOWN:
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1413
			if (_Town_pool.AddBlockIfNeeded(index)) return GetTown(index);
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1414
			error("Towns: failed loading savegame: too many towns");
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1415
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1416
		case REF_ROADSTOPS:
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1417
			if (_RoadStop_pool.AddBlockIfNeeded(index)) return GetRoadStop(index);
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1418
			error("RoadStops: failed loading savegame: too many RoadStops");
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1419
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1420
		case REF_ENGINE_RENEWS:
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1421
			if (_EngineRenew_pool.AddBlockIfNeeded(index)) return GetEngineRenew(index);
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1422
			error("EngineRenews: failed loading savegame: too many EngineRenews");
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1423
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1424
		case REF_CARGO_PACKET:
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1425
			if (_CargoPacket_pool.AddBlockIfNeeded(index)) return GetCargoPacket(index);
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1426
			error("CargoPackets: failed loading savegame: too many Cargo packets");
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1427
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1428
		case REF_VEHICLE_OLD:
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1429
			/* Old vehicles were saved differently:
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1430
			 * invalid vehicle was 0xFFFF,
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1431
			 * and the index was not - 1.. correct for this */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1432
			index++;
8492
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1433
			if (index == INVALID_VEHICLE) return NULL;
1279
bc761aad52b3 (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1272
diff changeset
  1434
8493
0bf0f76578d4 (svn r12068) -Fix(r12067): forgot a return
belugas
parents: 8492
diff changeset
  1435
			if (_Vehicle_pool.AddBlockIfNeeded(index)) return GetVehicle(index);
8492
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1436
			error("Vehicles: failed loading savegame: too many vehicles");
1fa4f2606a78 (svn r12067) -Cleanup: Codestyle on loading object references
peter1138
parents: 8471
diff changeset
  1437
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1438
		default: NOT_REACHED();
938
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1439
	}
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1440
23cc832ac8a2 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1441
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1442
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1444
/** The format for a reader/writer type of a savegame */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1445
struct SaveLoadFormat {
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1446
	const char *name;           ///< name of the compressor/decompressor (debug-only)
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1447
	uint32 tag;                 ///< the 4-letter tag by which it is identified in the savegame
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1449
	bool (*init_read)();        ///< function executed upon initalization of the loader
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1450
	ReaderProc *reader;         ///< function that loads the data from the file
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1451
	void (*uninit_read)();      ///< function executed when reading is finished
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1452
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1453
	bool (*init_write)();       ///< function executed upon intialization of the saver
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1454
	WriterProc *writer;         ///< function that saves the data to the file
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1455
	void (*uninit_write)();     ///< function executed when writing is done
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1456
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1457
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1458
static const SaveLoadFormat _saveload_formats[] = {
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1459
	{"memory", 0,                NULL,         NULL,       NULL,           InitMem,       WriteMem,    UnInitMem},
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1460
	{"lzo",    TO_BE32X('OTTD'), InitLZO,      ReadLZO,    UninitLZO,      InitLZO,       WriteLZO,    UninitLZO},
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1461
	{"none",   TO_BE32X('OTTN'), InitNoComp,   ReadNoComp, UninitNoComp,   InitNoComp,    WriteNoComp, UninitNoComp},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1462
#if defined(WITH_ZLIB)
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1463
	{"zlib",   TO_BE32X('OTTZ'), InitReadZlib, ReadZlib,   UninitReadZlib, InitWriteZlib, WriteZlib,   UninitWriteZlib},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1464
#else
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1465
	{"zlib",   TO_BE32X('OTTZ'), NULL,         NULL,       NULL,           NULL,          NULL,        NULL},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1466
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1468
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1469
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1470
 * Return the savegameformat of the game. Whether it was create with ZLIB compression
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1471
 * uncompressed, or another type
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1472
 * @param s Name of the savegame format. If NULL it picks the first available one
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1473
 * @return Pointer to SaveLoadFormat struct giving all characteristics of this type of savegame
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1474
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1475
static const SaveLoadFormat *GetSavegameFormat(const char *s)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1476
{
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1477
	const SaveLoadFormat *def = endof(_saveload_formats) - 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1478
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1479
	/* find default savegame format, the highest one with which files can be written */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1480
	while (!def->init_write) def--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1481
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1482
	if (s != NULL && s[0] != '\0') {
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1483
		const SaveLoadFormat *slf;
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1484
		for (slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1485
			if (slf->init_write != NULL && strcmp(s, slf->name) == 0)
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1486
				return slf;
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1487
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1488
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1489
		ShowInfoF("Savegame format '%s' is not available. Reverting to '%s'.", s, def->name);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1490
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1491
	return def;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1494
/* actual loader/saver function */
9409
a4fc0d50c588 (svn r13320) -Codechange: move some enums from openttd.h to more logical locations.
rubidium
parents: 9390
diff changeset
  1495
void InitializeGame(uint size_x, uint size_y, bool reset_date);
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1496
extern bool AfterLoadGame();
9410
b8cbfacc9057 (svn r13321) -Codechange: move some GUI/viewport related functions out op openttd.cpp to viewport.cpp/window.cpp.
rubidium
parents: 9409
diff changeset
  1497
extern void SaveViewportBeforeSaveGame();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1498
extern bool LoadOldSaveGame(const char *file);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1499
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1500
/** Small helper function to close the to be loaded savegame an signal error */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1501
static inline SaveOrLoadResult AbortSaveLoad()
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1502
{
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1503
	if (_sl.fh != NULL) fclose(_sl.fh);
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1504
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1505
	_sl.fh = NULL;
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1506
	return SL_ERROR;
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1507
}
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1508
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1509
/** Update the gui accordingly when starting saving
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1510
 * and set locks on saveload. Also turn off fast-forward cause with that
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1511
 * saving takes Aaaaages */
8989
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
  1512
static void SaveFileStart()
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1513
{
5719
2bb88fe71d01 (svn r8214) -Fix (r8038): The fast forward flag stores 2 bits, not 1. Remember the
peter1138
parents: 5705
diff changeset
  1514
	_ts.ff_state = _fast_forward;
2bb88fe71d01 (svn r8214) -Fix (r8038): The fast forward flag stores 2 bits, not 1. Remember the
peter1138
parents: 5705
diff changeset
  1515
	_fast_forward = 0;
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5587
diff changeset
  1516
	if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1517
9248
5c057d6b7e21 (svn r13114) -Codechange: use InvalidateData instead of direct window access to modify the state of the statusbar from outside the statusbar.
rubidium
parents: 9165
diff changeset
  1518
	InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_START);
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1519
	_ts.saveinprogress = true;
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1520
}
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1521
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1522
/** Update the gui accordingly when saving is done and release locks
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1523
 * on saveload */
8989
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
  1524
static void SaveFileDone()
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1525
{
10387
93e6f7a70691 (svn r14638) -Fix [FS#2430]: don't restore fast-forward when SaveFileDone() callback happens after we returned to the menu
glx
parents: 10347
diff changeset
  1526
	if (_game_mode != GM_MENU) _fast_forward = _ts.ff_state;
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5587
diff changeset
  1527
	if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1528
9248
5c057d6b7e21 (svn r13114) -Codechange: use InvalidateData instead of direct window access to modify the state of the statusbar from outside the statusbar.
rubidium
parents: 9165
diff changeset
  1529
	InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_FINISH);
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1530
	_ts.saveinprogress = false;
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1531
}
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1532
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1533
/** Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friends) */
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1534
void SetSaveLoadError(StringID str)
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1535
{
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1536
	_sl.error_str = str;
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1537
}
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1538
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1539
/** Get the string representation of the error message */
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1540
const char *GetSaveLoadErrorString()
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1541
{
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1542
	SetDParam(0, _sl.error_str);
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1543
	SetDParamStr(1, _sl.extra_msg);
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1544
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1545
	static char err_str[512];
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1546
	GetString(err_str, _sl.save ? STR_4007_GAME_SAVE_FAILED : STR_4009_GAME_LOAD_FAILED, lastof(err_str));
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1547
	return err_str;
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1548
}
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1549
2380
392bba57462d (svn r2906) Fix some threaded saving problems. Now the thread only interfaces with the main program through a sort of mutex. Communication uses the function OTTD_SendThreadMessage() with the approiate message which is handled in ProcessSentMessage() during the main loop.
Darkvater
parents: 2337
diff changeset
  1550
/** Show a gui message when saving has failed */
8989
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
  1551
static void SaveFileError()
2380
392bba57462d (svn r2906) Fix some threaded saving problems. Now the thread only interfaces with the main program through a sort of mutex. Communication uses the function OTTD_SendThreadMessage() with the approiate message which is handled in ProcessSentMessage() during the main loop.
Darkvater
parents: 2337
diff changeset
  1552
{
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1553
	SetDParamStr(0, GetSaveLoadErrorString());
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1554
	ShowErrorMessage(STR_012D, STR_NULL, 0, 0);
2380
392bba57462d (svn r2906) Fix some threaded saving problems. Now the thread only interfaces with the main program through a sort of mutex. Communication uses the function OTTD_SendThreadMessage() with the approiate message which is handled in ProcessSentMessage() during the main loop.
Darkvater
parents: 2337
diff changeset
  1555
	SaveFileDone();
392bba57462d (svn r2906) Fix some threaded saving problems. Now the thread only interfaces with the main program through a sort of mutex. Communication uses the function OTTD_SendThreadMessage() with the approiate message which is handled in ProcessSentMessage() during the main loop.
Darkvater
parents: 2337
diff changeset
  1556
}
392bba57462d (svn r2906) Fix some threaded saving problems. Now the thread only interfaces with the main program through a sort of mutex. Communication uses the function OTTD_SendThreadMessage() with the approiate message which is handled in ProcessSentMessage() during the main loop.
Darkvater
parents: 2337
diff changeset
  1557
4978
a5d2e71f2e2c (svn r6981) Use the pool macros for the Savegame pool
tron
parents: 4977
diff changeset
  1558
/** We have written the whole game into memory, _Savegame_pool, now find
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1559
 * and appropiate compressor and start writing to file.
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1560
 */
5705
7509e8810d5a (svn r8171) -Fix (FS#556): return SL_ERROR when unthreaded saves failed, to make sure we do not try to send zero-byte savegames.
rubidium
parents: 5683
diff changeset
  1561
static SaveOrLoadResult SaveFileToDisk(bool threaded)
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1562
{
2927
b59e88328e03 (svn r3483) -Fix: fixed warning about setjmp (tnx Bjarni for testing, and tnx for
truelight
parents: 2916
diff changeset
  1563
	const SaveLoadFormat *fmt;
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1564
	uint32 hdr[2];
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1565
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1566
	_sl.excpt_uninit = NULL;
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1567
	try {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1568
		fmt = GetSavegameFormat(_savegame_format);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1569
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1570
		/* We have written our stuff to memory, now write it to file! */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1571
		hdr[0] = fmt->tag;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1572
		hdr[1] = TO_BE32(SAVEGAME_VERSION << 16);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1573
		if (fwrite(hdr, sizeof(hdr), 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1574
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1575
		if (!fmt->init_write()) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1576
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1577
		{
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1578
			uint i;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1579
			uint count = 1 << Savegame_POOL_BLOCK_SIZE_BITS;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1580
8935
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
  1581
			if (_ts.count != _sl.offs_base) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected size of chunk");
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1582
			for (i = 0; i != _Savegame_pool.GetBlockCount() - 1; i++) {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1583
				_sl.buf = _Savegame_pool.blocks[i];
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1584
				fmt->writer(count);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1585
			}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1586
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1587
			/* The last block is (almost) always not fully filled, so only write away
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1588
			 * as much data as it is in there */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1589
			_sl.buf = _Savegame_pool.blocks[i];
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1590
			fmt->writer(_ts.count - (i * count));
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1591
		}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1592
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1593
		fmt->uninit_write();
8935
e9bef9cc4e84 (svn r12707) -Fix: do not crash very hard on unrecognised savegames, just go back to the intro menu instead.
rubidium
parents: 8934
diff changeset
  1594
		if (_ts.count != _sl.offs_base) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, "Unexpected size of chunk");
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1595
		GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1596
		fclose(_sl.fh);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1597
8989
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
  1598
		if (threaded) SetAsyncSaveFinish(SaveFileDone);
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1599
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1600
		return SL_OK;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1601
	}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1602
	catch (...) {
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1603
		AbortSaveLoad();
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1604
		if (_sl.excpt_uninit != NULL) _sl.excpt_uninit();
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1605
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1606
		ShowInfo(GetSaveLoadErrorString());
9097
b6d5a834e4cd (svn r12957) -Codechange: do not misuse snprintf when you just want to copy a string, also use DEBUG instead of fprintf in one case.
rubidium
parents: 9070
diff changeset
  1607
		DEBUG(sl, 0, GetSaveLoadErrorString());
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1608
5705
7509e8810d5a (svn r8171) -Fix (FS#556): return SL_ERROR when unthreaded saves failed, to make sure we do not try to send zero-byte savegames.
rubidium
parents: 5683
diff changeset
  1609
		if (threaded) {
8989
42f45270fbca (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
rubidium
parents: 8987
diff changeset
  1610
			SetAsyncSaveFinish(SaveFileError);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1611
		} else {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1612
			SaveFileError();
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1613
		}
5705
7509e8810d5a (svn r8171) -Fix (FS#556): return SL_ERROR when unthreaded saves failed, to make sure we do not try to send zero-byte savegames.
rubidium
parents: 5683
diff changeset
  1614
		return SL_ERROR;
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1615
	}
5705
7509e8810d5a (svn r8171) -Fix (FS#556): return SL_ERROR when unthreaded saves failed, to make sure we do not try to send zero-byte savegames.
rubidium
parents: 5683
diff changeset
  1616
}
7509e8810d5a (svn r8171) -Fix (FS#556): return SL_ERROR when unthreaded saves failed, to make sure we do not try to send zero-byte savegames.
rubidium
parents: 5683
diff changeset
  1617
9476
902f9cf6373f (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 9457
diff changeset
  1618
static void SaveFileToDiskThread(void *arg)
5705
7509e8810d5a (svn r8171) -Fix (FS#556): return SL_ERROR when unthreaded saves failed, to make sure we do not try to send zero-byte savegames.
rubidium
parents: 5683
diff changeset
  1619
{
7509e8810d5a (svn r8171) -Fix (FS#556): return SL_ERROR when unthreaded saves failed, to make sure we do not try to send zero-byte savegames.
rubidium
parents: 5683
diff changeset
  1620
	SaveFileToDisk(true);
1885
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1621
}
aef601001f5e (svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents: 1884
diff changeset
  1622
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1623
void WaitTillSaved()
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1624
{
9299
258be16ef2bd (svn r13167) -Codechange: prepend _ to a global variable.
rubidium
parents: 9248
diff changeset
  1625
	if (_save_thread == NULL) return;
8934
f46812d21fe6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents: 8881
diff changeset
  1626
9299
258be16ef2bd (svn r13167) -Codechange: prepend _ to a global variable.
rubidium
parents: 9248
diff changeset
  1627
	_save_thread->Join();
9479
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
  1628
	delete _save_thread;
9299
258be16ef2bd (svn r13167) -Codechange: prepend _ to a global variable.
rubidium
parents: 9248
diff changeset
  1629
	_save_thread = NULL;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1630
}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1631
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1632
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1633
 * Main Save or Load function where the high-level saveload functions are
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1634
 * handled. It opens the savegame, selects format and checks versions
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1635
 * @param filename The name of the savegame being created/loaded
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1636
 * @param mode Save or load. Load can also be a TTD(Patch) game. Use SL_LOAD, SL_OLD_LOAD or SL_SAVE
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1637
 * @return Return the results of the action. SL_OK, SL_ERROR or SL_REINIT ("unload" the game)
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1638
 */
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6817
diff changeset
  1639
SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1641
	uint32 hdr[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1642
	const SaveLoadFormat *fmt;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1643
2413
b7bc7bb733ae (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1644
	/* An instance of saving is already active, so don't go saving again */
b7bc7bb733ae (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1645
	if (_ts.saveinprogress && mode == SL_SAVE) {
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1646
		/* if not an autosave, but a user action, show error message */
2749
c9312a4a1efe (svn r3294) - Fix: use INVALID_STRING_ID instead of -1.
Darkvater
parents: 2694
diff changeset
  1647
		if (!_do_autosave) ShowErrorMessage(INVALID_STRING_ID, STR_SAVE_STILL_IN_PROGRESS, 0, 0);
2413
b7bc7bb733ae (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1648
		return SL_OK;
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1649
	}
2413
b7bc7bb733ae (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1650
	WaitTillSaved();
1913
0798d6f1d682 (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1651
7100
a4dd8e1b9f6e (svn r10367) -Fix: do not crash horribly when the file you are trying to load is too short or decompressing it fails.
rubidium
parents: 7054
diff changeset
  1652
	_next_offs = 0;
a4dd8e1b9f6e (svn r10367) -Fix: do not crash horribly when the file you are trying to load is too short or decompressing it fails.
rubidium
parents: 7054
diff changeset
  1653
2413
b7bc7bb733ae (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1654
	/* Load a TTDLX or TTDPatch game */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1655
	if (mode == SL_OLD_LOAD) {
9409
a4fc0d50c588 (svn r13320) -Codechange: move some enums from openttd.h to more logical locations.
rubidium
parents: 9390
diff changeset
  1656
		InitializeGame(256, 256, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1657
		GamelogReset();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1658
		if (!LoadOldSaveGame(filename)) return SL_REINIT;
2808
8ece7af41664 (svn r3356) - Remove unused parameter of AfterLoadGame().
peter1138
parents: 2749
diff changeset
  1659
		_sl_version = 0;
9101
40c05255536e (svn r12961) -Codechange: store the type of savegame we are loading in a global variable
smatz
parents: 9100
diff changeset
  1660
		_sl_minor_version = 0;
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1661
		GamelogStartAction(GLAT_LOAD);
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1662
		if (!AfterLoadGame()) {
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1663
			GamelogStopAction();
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1664
			return SL_REINIT;
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1665
		}
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1666
		GamelogStopAction();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1667
		return SL_OK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1668
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1669
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1670
	_sl.excpt_uninit = NULL;
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1671
	try {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1672
		_sl.fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1673
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1674
		/* Make it a little easier to load savegames from the console */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1675
		if (_sl.fh == NULL && mode == SL_LOAD) _sl.fh = FioFOpenFile(filename, "rb", SAVE_DIR);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1676
		if (_sl.fh == NULL && mode == SL_LOAD) _sl.fh = FioFOpenFile(filename, "rb", BASE_DIR);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1677
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1678
		if (_sl.fh == NULL) {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1679
			SlError(mode == SL_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1680
		}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1681
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1682
		_sl.bufe = _sl.bufp = NULL;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1683
		_sl.offs_base = 0;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1684
		_sl.save = (mode != 0);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1685
		_sl.chs = _chunk_handlers;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1686
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1687
		/* General tactic is to first save the game to memory, then use an available writer
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1688
		 * to write it to file, either in threaded mode if possible, or single-threaded */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1689
		if (mode == SL_SAVE) { /* SAVE game */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1690
			fmt = GetSavegameFormat("memory"); // write to memory
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1691
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1692
			_sl.write_bytes = fmt->writer;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1693
			_sl.excpt_uninit = fmt->uninit_write;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1694
			if (!fmt->init_write()) {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1695
				DEBUG(sl, 0, "Initializing writer '%s' failed.", fmt->name);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1696
				return AbortSaveLoad();
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1697
			}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1698
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1699
			_sl_version = SAVEGAME_VERSION;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1700
9410
b8cbfacc9057 (svn r13321) -Codechange: move some GUI/viewport related functions out op openttd.cpp to viewport.cpp/window.cpp.
rubidium
parents: 9409
diff changeset
  1701
			SaveViewportBeforeSaveGame();
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1702
			SlSaveChunks();
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1703
			SlWriteFill(); // flush the save buffer
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1704
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1705
			SaveFileStart();
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1706
			if (_network_server ||
9479
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
  1707
						(_save_thread = ThreadObject::New(&SaveFileToDiskThread, NULL)) == NULL) {
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1708
				if (!_network_server) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1709
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1710
				SaveOrLoadResult result = SaveFileToDisk(false);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1711
				SaveFileDone();
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1712
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1713
				return result;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1714
			}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1715
		} else { /* LOAD game */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1716
			assert(mode == SL_LOAD);
8247
16e1c8db6f55 (svn r11811) -Fix: make compilation without networking work again (and thus move the debugdumpcommand stuff out of the network 'area').
rubidium
parents: 8212
diff changeset
  1717
			DebugDumpCommands("ddc:load:%s\n", filename);
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1718
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1719
			if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1720
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1721
			/* see if we have any loader for this type. */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1722
			for (fmt = _saveload_formats; ; fmt++) {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1723
				/* No loader found, treat as version 0 and use LZO format */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1724
				if (fmt == endof(_saveload_formats)) {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1725
					DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1726
	#if defined(WINCE)
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1727
					/* Of course some system had not to support rewind ;) */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1728
					fseek(_sl.fh, 0L, SEEK_SET);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1729
					clearerr(_sl.fh);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1730
	#else
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1731
					rewind(_sl.fh);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1732
	#endif
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1733
					_sl_version = 0;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1734
					_sl_minor_version = 0;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1735
					fmt = _saveload_formats + 1; // LZO
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1736
					break;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1737
				}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1738
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1739
				if (fmt->tag == hdr[0]) {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1740
					/* check version number */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1741
					_sl_version = TO_BE32(hdr[1]) >> 16;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1742
					/* Minor is not used anymore from version 18.0, but it is still needed
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1743
					 * in versions before that (4 cases) which can't be removed easy.
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1744
					 * Therefor it is loaded, but never saved (or, it saves a 0 in any scenario).
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1745
					 * So never EVER use this minor version again. -- TrueLight -- 22-11-2005 */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1746
					_sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1747
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1748
					DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1749
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1750
					/* Is the version higher than the current? */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1751
					if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1752
					break;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1753
				}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1754
			}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1755
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1756
			_sl.read_bytes = fmt->reader;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1757
			_sl.excpt_uninit = fmt->uninit_read;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1758
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1759
			/* loader for this savegame type is not implemented? */
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1760
			if (fmt->init_read == NULL) {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1761
				char err_str[64];
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1762
				snprintf(err_str, lengthof(err_str), "Loader for '%s' is not available.", fmt->name);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1763
				SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1764
			}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1765
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1766
			if (!fmt->init_read()) {
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1767
				char err_str[64];
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1768
				snprintf(err_str, lengthof(err_str), "Initializing loader '%s' failed", fmt->name);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1769
				SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1770
			}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1771
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1772
			/* Old maps were hardcoded to 256x256 and thus did not contain
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1773
			 * any mapsize information. Pre-initialize to 256x256 to not to
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1774
			 * confuse old games */
9409
a4fc0d50c588 (svn r13320) -Codechange: move some enums from openttd.h to more logical locations.
rubidium
parents: 9390
diff changeset
  1775
			InitializeGame(256, 256, true);
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1776
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1777
			GamelogReset();
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1778
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1779
			SlLoadChunks();
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1780
			fmt->uninit_read();
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1781
			fclose(_sl.fh);
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1782
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1783
			GamelogStartAction(GLAT_LOAD);
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1784
9101
40c05255536e (svn r12961) -Codechange: store the type of savegame we are loading in a global variable
smatz
parents: 9100
diff changeset
  1785
			_savegame_type = SGT_OTTD;
40c05255536e (svn r12961) -Codechange: store the type of savegame we are loading in a global variable
smatz
parents: 9100
diff changeset
  1786
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1787
			/* After loading fix up savegame for any internal changes that
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1788
			 * might've occured since then. If it fails, load back the old game */
9457
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1789
			if (!AfterLoadGame()) {
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1790
				GamelogStopAction();
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1791
				return SL_REINIT;
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1792
			}
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1793
75f11a6caef8 (svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
smatz
parents: 9455
diff changeset
  1794
			GamelogStopAction();
7449
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1795
		}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1796
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1797
		return SL_OK;
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1798
	}
82bf2769bffa (svn r10923) -Codechange: use 'real' exception handling in saveload code
glx
parents: 7445
diff changeset
  1799
	catch (...) {
7036
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1800
		AbortSaveLoad();
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1801
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1802
		/* deinitialize compressor. */
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1803
		if (_sl.excpt_uninit != NULL) _sl.excpt_uninit();
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1804
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1805
		/* Skip the "color" character */
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1806
		ShowInfoF(GetSaveLoadErrorString() + 3);
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1807
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1808
		/* A saver/loader exception!! reinitialize all variables to prevent crash! */
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1809
		return (mode == SL_LOAD) ? SL_REINIT : SL_ERROR;
f1e26f21aa08 (svn r10300) -Fix [FS#917]: give a better explanation why the loading of a savegame failed and do not crash on loading savegames that were altered by patches or branches.
rubidium
parents: 7010
diff changeset
  1810
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1811
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1812
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9410
diff changeset
  1813
/** Do a save when exiting the game (patch option) _settings_client.gui.autosave_on_exit */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  1814
void DoExitSave()
643
a9c84d04eacb (svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents: 617
diff changeset
  1815
{
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6817
diff changeset
  1816
	SaveOrLoad("exit.sav", SL_SAVE, AUTOSAVE_DIR);
643
a9c84d04eacb (svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents: 617
diff changeset
  1817
}
a9c84d04eacb (svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents: 617
diff changeset
  1818
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1819
#if 0
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1820
/**
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1821
 * Function to get the type of the savegame by looking at the file header.
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1822
 * NOTICE: Not used right now, but could be used if extensions of savegames are garbled
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1823
 * @param file Savegame to be checked
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1824
 * @return SL_OLD_LOAD or SL_LOAD of the file
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1825
 */
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1826
int GetSavegameType(char *file)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1828
	const SaveLoadFormat *fmt;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1829
	uint32 hdr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1830
	FILE *f;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1831
	int mode = SL_OLD_LOAD;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1832
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1833
	f = fopen(file, "rb");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1834
	if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1835
		DEBUG(sl, 0, "Savegame is obsolete or invalid format");
2026
567e3bc9af72 (svn r2535) Tabs
tron
parents: 1993
diff changeset
  1836
		mode = SL_LOAD; // don't try to get filename, just show name as it is written
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1837
	} else {
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6012
diff changeset
  1838
		/* see if we have any loader for this type. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1839
		for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1840
			if (fmt->tag == hdr) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1841
				mode = SL_LOAD; // new type of savegame
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1842
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1843
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1844
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1845
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1846
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1847
	fclose(f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1848
	return mode;
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1849
}
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1614
diff changeset
  1850
#endif