saveload.c
author glx
Mon, 24 Sep 2007 03:08:47 +0000
branch0.5
changeset 5545 f42dc59a45f5
parent 5421 96c2e24bfe30
permissions -rw-r--r--
(svn r11153) [0.5] -Fix [FS#1251]: incorrect usage of {G} tag in slovak translation
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
1881
023a134a4b12 (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
     3
/** @file
023a134a4b12 (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
023a134a4b12 (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):
023a134a4b12 (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>
023a134a4b12 (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
023a134a4b12 (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
023a134a4b12 (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
023a134a4b12 (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)
023a134a4b12 (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
023a134a4b12 (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)
023a134a4b12 (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
023a134a4b12 (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>
023a134a4b12 (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
 * @see ChunkHandler
023a134a4b12 (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
    16
 * @see SaveLoad
023a134a4b12 (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
    17
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1886
diff changeset
    19
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1284
diff changeset
    20
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2162
diff changeset
    21
#include "functions.h"
3329
992d1f7cb747 (svn r4105) - Feature: Add proper ISO-8859-15 <> LOCALCODE conversion. As the mess that is makefile can't properly support it at the moment, it is only available for MACOSX. Windows doesn't need FS conversion and I have no idea about OS/2 so it's disabled for them.
Darkvater
parents: 3294
diff changeset
    22
#include "hal.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
#include "station.h"
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
    25
#include "thread.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
#include "town.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
#include "saveload.h"
3117
8066b8d0802b (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
    29
#include "network.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    30
#include "variables.h"
2335
1222aa57deb7 (svn r2861) Move inclusion of setjmp.h into saveload.c, should've been part of r2819
tron
parents: 2295
diff changeset
    31
#include <setjmp.h>
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents: 5216
diff changeset
    33
const uint16 SAVEGAME_VERSION = 41;
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
    34
uint16 _sl_version;       /// the major savegame version identifier
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
    35
byte   _sl_minor_version; /// the minor savegame version, DO NOT USE!
2295
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    36
2337
a4587769d43a (svn r2863) Move some type declarations into saveload.c, should've been part of r2819
tron
parents: 2335
diff changeset
    37
typedef void WriterProc(uint len);
a4587769d43a (svn r2863) Move some type declarations into saveload.c, should've been part of r2819
tron
parents: 2335
diff changeset
    38
typedef uint ReaderProc(void);
a4587769d43a (svn r2863) Move some type declarations into saveload.c, should've been part of r2819
tron
parents: 2335
diff changeset
    39
2295
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    40
/** The saveload struct, containing reader-writer functions, bufffer, version, etc. */
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    41
static struct {
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    42
	bool save;                           /// are we doing a save or a load atm. True when saving
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    43
	byte need_length;                    /// ???
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    44
	byte block_mode;                     /// ???
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    45
	bool error;                          /// did an error occur or not
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    46
af67e1ea8687 (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
	int obj_len;                         /// the length of the current object we are busy with
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    48
	int array_index, last_array_index;   /// in the case of an array, the current and last positions
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    49
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    50
	uint32 offs_base;                    /// the offset in number of bytes since we started writing data (eg uncompressed savegame size)
af67e1ea8687 (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
af67e1ea8687 (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
	WriterProc *write_bytes;             /// savegame writer function
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    53
	ReaderProc *read_bytes;              /// savegame loader function
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    54
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    55
	const ChunkHandler* const *chs;      /// the chunk of data that is being processed atm (vehicles, signs, etc.)
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    56
	const SaveLoad* const *includes;     /// the internal layouf of the given chunk
af67e1ea8687 (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
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    58
	/** When saving/loading savegames, they are always saved to a temporary memory-place
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    59
	 * to be flushed to file (save) or to final place (load) when full. */
af67e1ea8687 (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
	byte *bufp, *bufe;                   /// bufp(ointer) gives the current position in the buffer bufe(nd) gives the end of the buffer
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    61
af67e1ea8687 (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
	// these 3 may be used by compressor/decompressors.
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    63
	byte *buf;                           /// pointer to temporary memory to read/write, initialized by SaveLoadFormat->initread/write
2414
53ecd2131592 (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
    64
	byte *buf_ori;                       /// pointer to the original memory location of buf, used to free it afterwards
2295
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    65
	uint bufsize;                        /// the size of the temporary memory *buf
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    66
	FILE *fh;                            /// the file from which is read or written to
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    67
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    68
	void (*excpt_uninit)(void);          /// the function to execute on any encountered error
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    69
	const char *excpt_msg;               /// the error message
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    70
	jmp_buf excpt;                       /// @todo used to jump to "exception handler";  really ugly
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    71
} _sl;
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    72
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
    73
1881
023a134a4b12 (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
    74
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
    75
1881
023a134a4b12 (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
    76
/**
023a134a4b12 (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
    77
 * Fill the input buffer by reading from the file with the given reader
023a134a4b12 (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
    78
 */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
    79
static void SlReadFill(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
	uint len = _sl.read_bytes();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
	assert(len != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
	_sl.bufp = _sl.buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
	_sl.bufe = _sl.buf + len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
	_sl.offs_base += len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
1881
023a134a4b12 (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
    89
static inline uint32 SlGetOffs(void) {return _sl.offs_base - (_sl.bufe - _sl.bufp);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
    91
/** Return the size in bytes of a certain type of normal/atomic variable
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
    92
 * as it appears in memory. @see VarTypes
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
    93
 * @param conv @VarType type of variable that is used for calculating the size
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
    94
 * @return Return the size of this type in bytes */
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
    95
static inline byte SlCalcConvMemLen(VarType conv)
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
    96
{
3048
9dd3128a6358 (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
    97
	static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
3625
5112d9999b6a (svn r4524) - Codechange: Use GB() macros for getting the saveload file/memory types
Darkvater
parents: 3585
diff changeset
    98
	byte length = GB(conv, 4, 4);
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
    99
	assert(length < lengthof(conv_mem_size));
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   100
	return conv_mem_size[length];
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   101
}
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   102
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   103
/** Return the size in bytes of a certain type of normal/atomic variable
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   104
 * as it appears in a saved game. @see VarTypes
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   105
 * @param conv @VarType type of variable that is used for calculating the size
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   106
 * @return Return the size of this type in bytes */
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   107
static inline byte SlCalcConvFileLen(VarType conv)
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   108
{
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   109
	static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
3625
5112d9999b6a (svn r4524) - Codechange: Use GB() macros for getting the saveload file/memory types
Darkvater
parents: 3585
diff changeset
   110
	byte length = GB(conv, 0, 4);
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   111
	assert(length < lengthof(conv_file_size));
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   112
	return conv_file_size[length];
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   113
}
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   114
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   115
/* Return the size in bytes of a reference (pointer) */
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   116
static inline size_t SlCalcRefLen(void) {return 2;}
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   117
1881
023a134a4b12 (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
   118
/** Flush the output buffer by writing to disk with the given reader.
023a134a4b12 (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
   119
 * If the buffer pointer has not yet been set up, set it up now. Usually
023a134a4b12 (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
   120
 * only called when the buffer is full, or there is no more data to be processed
023a134a4b12 (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
   121
 */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
   122
static void SlWriteFill(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
{
1881
023a134a4b12 (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
   124
	// flush the buffer to disk (the writer)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	if (_sl.bufp != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
		uint len = _sl.bufp - _sl.buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
		_sl.offs_base += len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
		if (len) _sl.write_bytes(len);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   130
1881
023a134a4b12 (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
	/* All the data from the buffer has been written away, rewind to the beginning
023a134a4b12 (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
	* to start reading in more data */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
	_sl.bufp = _sl.buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
	_sl.bufe = _sl.buf + _sl.bufsize;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
1881
023a134a4b12 (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
   137
/** Error handler, calls longjmp to simulate an exception.
023a134a4b12 (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
   138
 * @todo this was used to have a central place to handle errors, but it is
023a134a4b12 (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
   139
 * pretty ugly, and seriously interferes with any multithreaded approaches */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
static void NORETURN SlError(const char *msg)
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
	_sl.excpt_msg = msg;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
	longjmp(_sl.excpt, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
1881
023a134a4b12 (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
   146
/** Read in a single byte from file. If the temporary buffer is full,
023a134a4b12 (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
   147
 * flush it to its final destination
023a134a4b12 (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
   148
 * @return return the read byte from file
023a134a4b12 (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
   149
 */
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   150
static inline byte SlReadByteInternal(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
	if (_sl.bufp == _sl.bufe) SlReadFill();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
	return *_sl.bufp++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
1881
023a134a4b12 (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
   156
/** Wrapper for SlReadByteInternal */
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   157
byte SlReadByte(void) {return SlReadByteInternal();}
1881
023a134a4b12 (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
   158
023a134a4b12 (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
   159
/** Write away a single byte from memory. If the temporary buffer is full,
023a134a4b12 (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
   160
 * flush it to its destination (file)
023a134a4b12 (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
   161
 * @param b the byte that is currently written
023a134a4b12 (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
   162
 */
023a134a4b12 (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
   163
static inline void SlWriteByteInternal(byte b)
1514
18dda44cfa72 (svn r2018) Yet another workaround
tron
parents: 1513
diff changeset
   164
{
1881
023a134a4b12 (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
   165
	if (_sl.bufp == _sl.bufe) SlWriteFill();
023a134a4b12 (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
   166
	*_sl.bufp++ = b;
1514
18dda44cfa72 (svn r2018) Yet another workaround
tron
parents: 1513
diff changeset
   167
}
18dda44cfa72 (svn r2018) Yet another workaround
tron
parents: 1513
diff changeset
   168
1881
023a134a4b12 (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
   169
/** Wrapper for SlWriteByteInternal */
023a134a4b12 (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
   170
void SlWriteByte(byte b) {SlWriteByteInternal(b);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
1881
023a134a4b12 (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
static inline int SlReadUint16(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
	int x = SlReadByte() << 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
	return x | SlReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
1881
023a134a4b12 (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
   178
static inline uint32 SlReadUint32(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
	uint32 x = SlReadUint16() << 16;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
	return x | SlReadUint16();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
1881
023a134a4b12 (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
   184
static inline uint64 SlReadUint64(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
	uint32 x = SlReadUint32();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
	uint32 y = SlReadUint32();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
	return (uint64)x << 32 | y;
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
2144
011a8b5ddcaa (svn r2654) SlWriteUint16() should have a uint16 as parameter, not some arbitrary enum
tron
parents: 2141
diff changeset
   191
static inline void SlWriteUint16(uint16 v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
{
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2144
diff changeset
   193
	SlWriteByte(GB(v, 8, 8));
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2144
diff changeset
   194
	SlWriteByte(GB(v, 0, 8));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
1881
023a134a4b12 (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
   197
static inline void SlWriteUint32(uint32 v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
{
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2144
diff changeset
   199
	SlWriteUint16(GB(v, 16, 16));
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2144
diff changeset
   200
	SlWriteUint16(GB(v,  0, 16));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
1881
023a134a4b12 (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
static inline void SlWriteUint64(uint64 x)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
	SlWriteUint32((uint32)(x >> 32));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
	SlWriteUint32((uint32)x);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
1881
023a134a4b12 (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
   209
/**
023a134a4b12 (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
 * Read in the header descriptor of an object or an array.
023a134a4b12 (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
 * If the highest bit is set (7), then the index is bigger than 127
023a134a4b12 (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
   212
 * elements, so use the next byte to read in the real value.
023a134a4b12 (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
   213
 * The actual value is then both bytes added with the first shifted
1886
06d2bb05350a (svn r2392) - Fix some typos (Tron)
Darkvater
parents: 1885
diff changeset
   214
 * 8 bits to the left, and dropping the highest bit (which only indicated a big index).
1881
023a134a4b12 (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
 * x = ((x & 0x7F) << 8) + SlReadByte();
023a134a4b12 (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
   216
 * @return Return the value of the index
023a134a4b12 (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
   217
 */
023a134a4b12 (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
   218
static uint SlReadSimpleGamma(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
{
1881
023a134a4b12 (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
   220
	uint i = SlReadByte();
023a134a4b12 (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
   221
	if (HASBIT(i, 7)) {
2041
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   222
		i &= ~0x80;
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   223
		if (HASBIT(i, 6)) {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   224
			i &= ~0x40;
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   225
			if (HASBIT(i, 5)) {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   226
				i &= ~0x20;
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   227
				if (HASBIT(i, 4))
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   228
					SlError("Unsupported gamma");
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   229
				i = (i << 8) | SlReadByte();
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   230
			}
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   231
			i = (i << 8) | SlReadByte();
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   232
		}
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   233
		i = (i << 8) | SlReadByte();
1881
023a134a4b12 (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
   234
	}
023a134a4b12 (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
   235
	return i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
1881
023a134a4b12 (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
   238
/**
023a134a4b12 (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
   239
 * Write the header descriptor of an object or an array.
1886
06d2bb05350a (svn r2392) - Fix some typos (Tron)
Darkvater
parents: 1885
diff changeset
   240
 * If the element is bigger than 127, use 2 bytes for saving
1881
023a134a4b12 (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
   241
 * and use the highest byte of the first written one as a notice
2041
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   242
 * that the length consists of 2 bytes, etc.. like this:
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   243
 * 0xxxxxxx
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   244
 * 10xxxxxx xxxxxxxx
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   245
 * 110xxxxx xxxxxxxx xxxxxxxx
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   246
 * 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
1881
023a134a4b12 (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
   247
 * @param i Index being written
023a134a4b12 (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
 */
2041
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   249
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
static void SlWriteSimpleGamma(uint i)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
{
1881
023a134a4b12 (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
   252
	if (i >= (1 << 7)) {
2041
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   253
		if (i >= (1 << 14)) {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   254
			if (i >= (1 << 21)) {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   255
				assert(i < (1 << 28));
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   256
				SlWriteByte((byte)0xE0 | (i>>24));
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   257
				SlWriteByte((byte)(i>>16));
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   258
			} else {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   259
				SlWriteByte((byte)0xC0 | (i>>16));
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   260
			}
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   261
			SlWriteByte((byte)(i>>8));
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   262
		} else {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   263
			SlWriteByte((byte)(0x80 | (i>>8)));
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   264
		}
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   265
	}
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   266
	SlWriteByte(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   268
2041
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   269
/** Return how many bytes used to encode a gamma value */
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   270
static inline uint SlGetGammaLength(uint i) {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   271
	return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21));
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   272
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   274
static inline uint SlReadSparseIndex(void) {return SlReadSimpleGamma();}
1881
023a134a4b12 (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
   275
static inline void SlWriteSparseIndex(uint index) {SlWriteSimpleGamma(index);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   277
static inline uint SlReadArrayLength(void) {return SlReadSimpleGamma();}
1881
023a134a4b12 (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
   278
static inline void SlWriteArrayLength(uint length) {SlWriteSimpleGamma(length);}
3509
5304adc9b471 (svn r4360) - CodeChange: add shortcut to SlGetArrayLength of the gamma-function along the lines of the Write/Read functions
Darkvater
parents: 3431
diff changeset
   279
static inline uint SlGetArrayLength(uint length) {return SlGetGammaLength(length);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
void SlSetArrayIndex(uint index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
	_sl.need_length = NL_WANTLENGTH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
	_sl.array_index = index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
1881
023a134a4b12 (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
   287
/**
023a134a4b12 (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
   288
 * Iterate through the elements of an array and read the whole thing
023a134a4b12 (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
   289
 * @return The index of the object, or -1 if we have reached the end of current block
023a134a4b12 (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
   290
 */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
   291
int SlIterateArray(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
{
1881
023a134a4b12 (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
	int index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
	static uint32 next_offs;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   295
1881
023a134a4b12 (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
   296
	/* After reading in the whole array inside the loop
023a134a4b12 (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
	 * we must have read in all the data, so we must be at end of current block. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
	assert(next_offs == 0 || SlGetOffs() == next_offs);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
1881
023a134a4b12 (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
   300
	while (true) {
023a134a4b12 (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
   301
		uint length = SlReadArrayLength();
023a134a4b12 (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
   302
		if (length == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
			next_offs = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
			return -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
1881
023a134a4b12 (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
   307
		_sl.obj_len = --length;
023a134a4b12 (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
   308
		next_offs = SlGetOffs() + length;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
1881
023a134a4b12 (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
   310
		switch (_sl.block_mode) {
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   311
		case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
1881
023a134a4b12 (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
   312
		case CH_ARRAY:        index = _sl.array_index++; break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
		default:
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
   314
			DEBUG(misc, 0) ("[Sl] SlIterateArray: error");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
			return -1; // error
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   317
1881
023a134a4b12 (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
   318
		if (length != 0) return index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
1881
023a134a4b12 (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
   322
/**
023a134a4b12 (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
   323
 * Sets the length of either a RIFF object or the number of items in an array.
023a134a4b12 (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
   324
 * This lets us load an object or an array of arbitrary size
023a134a4b12 (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
   325
 * @param length The length of the sought object/array
023a134a4b12 (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
   326
 */
023a134a4b12 (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
   327
void SlSetLength(size_t length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
{
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   329
	assert(_sl.save);
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   330
1881
023a134a4b12 (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
   331
	switch (_sl.need_length) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
	case NL_WANTLENGTH:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
		_sl.need_length = NL_NONE;
1881
023a134a4b12 (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
   334
		switch (_sl.block_mode) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
		case CH_RIFF:
2041
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   336
			// Ugly encoding of >16M RIFF chunks
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   337
			// The lower 24 bits are normal
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   338
			// The uppermost 4 bits are bits 24:27
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   339
			assert(length < (1<<28));
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   340
			SlWriteUint32((length & 0xFFFFFF) | ((length >> 24) << 28));
1881
023a134a4b12 (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
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
		case CH_ARRAY:
1881
023a134a4b12 (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
			assert(_sl.last_array_index <= _sl.array_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
			while (++_sl.last_array_index <= _sl.array_index)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
				SlWriteArrayLength(1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
			SlWriteArrayLength(length + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
		case CH_SPARSE_ARRAY:
3509
5304adc9b471 (svn r4360) - CodeChange: add shortcut to SlGetArrayLength of the gamma-function along the lines of the Write/Read functions
Darkvater
parents: 3431
diff changeset
   349
			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
   350
			SlWriteSparseIndex(_sl.array_index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
		default: NOT_REACHED();
1881
023a134a4b12 (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
   353
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
	case NL_CALCLENGTH:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
		_sl.obj_len += length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
1881
023a134a4b12 (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
   360
/**
023a134a4b12 (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
   361
 * Save/Load bytes. These do not need to be converted to Little/Big Endian
023a134a4b12 (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
   362
 * so directly write them or read them to/from file
023a134a4b12 (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
   363
 * @param ptr The source or destination of the object being manipulated
023a134a4b12 (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
   364
 * @param length number of bytes this fast CopyBytes lasts
023a134a4b12 (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
 */
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 222
diff changeset
   366
static void SlCopyBytes(void *ptr, size_t length)
0
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
	byte *p = (byte*)ptr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
	if (_sl.save) {
1881
023a134a4b12 (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
		for (; length != 0; length--) {SlWriteByteInternal(*p++);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
	} else {
1881
023a134a4b12 (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
		for (; length != 0; length--) {*p++ = SlReadByteInternal();}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
3117
8066b8d0802b (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
   377
/** Read in bytes from the file/data structure but don't do
8066b8d0802b (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
   378
 * anything with them, discarding them in effect
1881
023a134a4b12 (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
   379
 * @param length The amount of bytes that is being treated this way
023a134a4b12 (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
   380
 */
023a134a4b12 (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
static inline void SlSkipBytes(size_t length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
{
3117
8066b8d0802b (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
   383
	for (; length != 0; length--) SlReadByte();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
1881
023a134a4b12 (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
   386
/* Get the length of the current object */
023a134a4b12 (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
   387
uint SlGetFieldLength(void) {return _sl.obj_len;}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
3108
de4252bcae08 (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
   389
/** Return a signed-long version of the value of a setting
de4252bcae08 (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
   390
 * @param ptr pointer to the variable
de4252bcae08 (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
   391
 * @param conv type of variable, can be a non-clean
de4252bcae08 (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
   392
 * type, eg one with other flags because it is parsed
de4252bcae08 (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
   393
 * @return returns the value of the pointer-setting */
de4252bcae08 (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
   394
int64 ReadValue(const void *ptr, VarType conv)
de4252bcae08 (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
   395
{
de4252bcae08 (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
   396
	switch (GetVarMemType(conv)) {
3109
13db09ee3039 (svn r3712) - Change the boolean assignment/reading from == 1 to != 0 as suggested by Tron.
Darkvater
parents: 3108
diff changeset
   397
	case SLE_VAR_BL:  return (*(bool*)ptr != 0);
3108
de4252bcae08 (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
   398
	case SLE_VAR_I8:  return *(int8*  )ptr;
de4252bcae08 (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
   399
	case SLE_VAR_U8:  return *(byte*  )ptr;
de4252bcae08 (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
   400
	case SLE_VAR_I16: return *(int16* )ptr;
de4252bcae08 (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
   401
	case SLE_VAR_U16: return *(uint16*)ptr;
de4252bcae08 (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
   402
	case SLE_VAR_I32: return *(int32* )ptr;
de4252bcae08 (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
   403
	case SLE_VAR_U32: return *(uint32*)ptr;
de4252bcae08 (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
   404
	case SLE_VAR_I64: return *(int64* )ptr;
de4252bcae08 (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
   405
	case SLE_VAR_U64: return *(uint64*)ptr;
de4252bcae08 (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
   406
	case SLE_VAR_NULL:return 0;
de4252bcae08 (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
   407
	default: NOT_REACHED();
de4252bcae08 (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
   408
	}
de4252bcae08 (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
   409
de4252bcae08 (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
   410
	/* useless, but avoids compiler warning this way */
de4252bcae08 (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
   411
	return 0;
de4252bcae08 (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
   412
}
de4252bcae08 (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
   413
de4252bcae08 (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
   414
/** Write the value of a setting
de4252bcae08 (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
   415
 * @param ptr pointer to the variable
de4252bcae08 (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
   416
 * @param conv type of variable, can be a non-clean type, eg
de4252bcae08 (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
   417
 * with other flags. It is parsed upon read
de4252bcae08 (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
   418
 * @param var the new value being given to the variable */
de4252bcae08 (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
   419
void WriteValue(void *ptr, VarType conv, int64 val)
de4252bcae08 (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
   420
{
de4252bcae08 (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
   421
	switch (GetVarMemType(conv)) {
3109
13db09ee3039 (svn r3712) - Change the boolean assignment/reading from == 1 to != 0 as suggested by Tron.
Darkvater
parents: 3108
diff changeset
   422
	case SLE_VAR_BL:  *(bool  *)ptr = (val != 0);  break;
3108
de4252bcae08 (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
   423
	case SLE_VAR_I8:  *(int8  *)ptr = val; break;
de4252bcae08 (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
   424
	case SLE_VAR_U8:  *(byte  *)ptr = val; break;
de4252bcae08 (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
   425
	case SLE_VAR_I16: *(int16 *)ptr = val; break;
de4252bcae08 (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
   426
	case SLE_VAR_U16: *(uint16*)ptr = val; break;
de4252bcae08 (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
   427
	case SLE_VAR_I32: *(int32 *)ptr = val; break;
de4252bcae08 (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
   428
	case SLE_VAR_U32: *(uint32*)ptr = val; break;
de4252bcae08 (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
   429
	case SLE_VAR_I64: *(int64 *)ptr = val; break;
de4252bcae08 (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
   430
	case SLE_VAR_U64: *(uint64*)ptr = val; break;
de4252bcae08 (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
   431
	case SLE_VAR_NULL: break;
de4252bcae08 (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
   432
	default: NOT_REACHED();
de4252bcae08 (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
   433
	}
de4252bcae08 (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
   434
}
de4252bcae08 (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
   435
1881
023a134a4b12 (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
   436
/**
023a134a4b12 (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
   437
 * Handle all conversion and typechecking of variables here.
023a134a4b12 (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
   438
 * In the case of saving, read in the actual value from the struct
023a134a4b12 (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
   439
 * and then write them to file, endian safely. Loading a value
023a134a4b12 (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
   440
 * goes exactly the opposite way
023a134a4b12 (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
   441
 * @param ptr The object being filled/read
023a134a4b12 (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
   442
 * @param conv @VarType type of the current element of the struct
023a134a4b12 (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
   443
 */
023a134a4b12 (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
   444
static void SlSaveLoadConv(void *ptr, VarType conv)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
	int64 x = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   447
1881
023a134a4b12 (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
   448
	if (_sl.save) { /* SAVE values */
023a134a4b12 (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
   449
		/* Read a value from the struct. These ARE endian safe. */
3108
de4252bcae08 (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
		x = ReadValue(ptr, conv);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
3108
de4252bcae08 (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
		/* Write the value to the file and check if its value is in the desired range */
de4252bcae08 (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
		switch (GetVarFileType(conv)) {
1881
023a134a4b12 (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
   454
		case SLE_FILE_I8: assert(x >= -128 && x <= 127);     SlWriteByte(x);break;
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1993
diff changeset
   455
		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
   456
		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
   457
		case SLE_FILE_STRINGID:
1881
023a134a4b12 (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
   458
		case SLE_FILE_U16:assert(x >= 0 && x <= 65535);      SlWriteUint16(x);break;
3108
de4252bcae08 (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
		case SLE_FILE_I32:
de4252bcae08 (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
		case SLE_FILE_U32:                                   SlWriteUint32((uint32)x);break;
de4252bcae08 (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
		case SLE_FILE_I64:
de4252bcae08 (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
		case SLE_FILE_U64:                                   SlWriteUint64(x);break;
1881
023a134a4b12 (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
   463
		default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
		}
1881
023a134a4b12 (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
   465
	} else { /* LOAD values */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   466
3108
de4252bcae08 (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
		/* Read a value from the file */
de4252bcae08 (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 (GetVarFileType(conv)) {
de4252bcae08 (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
   469
		case SLE_FILE_I8:  x = (int8  )SlReadByte();   break;
de4252bcae08 (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_FILE_U8:  x = (byte  )SlReadByte();   break;
de4252bcae08 (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_FILE_I16: x = (int16 )SlReadUint16(); break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
		case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
3108
de4252bcae08 (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_FILE_I32: x = (int32 )SlReadUint32(); break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
		case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
3108
de4252bcae08 (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_FILE_I64: x = (int64 )SlReadUint64(); break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
		case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
		case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
1881
023a134a4b12 (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
   478
		default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
1881
023a134a4b12 (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
   481
		/* Write The value to the struct. These ARE endian safe. */
3108
de4252bcae08 (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
		WriteValue(ptr, conv, x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
3510
26ef8131b359 (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
   486
/** Calculate the net length of a string. This is in almost all cases
26ef8131b359 (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
   487
 * just strlen(), but if the string is not properly terminated, we'll
26ef8131b359 (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
   488
 * resort to the maximum length of the buffer.
26ef8131b359 (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
   489
 * @param ptr pointer to the stringbuffer
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   490
 * @param length maximum length of the string (buffer). If -1 we don't care
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   491
 * about a maximum length, but take string length as it is.
3510
26ef8131b359 (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
   492
 * @return return the net length of the string */
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   493
static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
3510
26ef8131b359 (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
   494
{
26ef8131b359 (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
   495
	return minu(strlen(ptr), length - 1);
26ef8131b359 (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
   496
}
26ef8131b359 (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
   497
26ef8131b359 (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
   498
/** Calculate the gross length of the string that it
26ef8131b359 (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
   499
 * will occupy in the savegame. This includes the real length, returned
26ef8131b359 (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
   500
 * by SlCalcNetStringLen and the length that the index will occupy.
26ef8131b359 (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
   501
 * @param ptr pointer to the stringbuffer
26ef8131b359 (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
   502
 * @param length maximum length of the string (buffer size, etc.)
26ef8131b359 (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
   503
 * @return return the gross length of the string */
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   504
static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
3048
9dd3128a6358 (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
   505
{
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   506
	size_t len;
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   507
	const char *str;
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   508
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   509
	switch (GetVarMemType(conv)) {
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   510
		default: NOT_REACHED();
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   511
		case SLE_VAR_STR:
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   512
		case SLE_VAR_STRQ:
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   513
			str = *(const char**)ptr;
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   514
			len = -1;
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   515
			break;
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   516
		case SLE_VAR_STRB:
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   517
		case SLE_VAR_STRBQ:
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   518
			str = (const char*)ptr;
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   519
			len = length;
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   520
			break;
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   521
	}
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   522
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   523
	len = SlCalcNetStringLen(str, len);
3510
26ef8131b359 (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
   524
	return len + SlGetArrayLength(len); // also include the length of the index
3048
9dd3128a6358 (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
   525
}
9dd3128a6358 (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
   526
9dd3128a6358 (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
   527
/**
9dd3128a6358 (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
   528
 * Save/Load a string.
9dd3128a6358 (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
   529
 * @param ptr the string being manipulated
9dd3128a6358 (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
   530
 * @param the length of the string (full length)
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   531
 * @param conv must be SLE_FILE_STRING */
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   532
static void SlString(void *ptr, size_t length, VarType conv)
3048
9dd3128a6358 (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
   533
{
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   534
	size_t len;
3048
9dd3128a6358 (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
   535
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   536
	if (_sl.save) { /* SAVE string */
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   537
		switch (GetVarMemType(conv)) {
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   538
			default: NOT_REACHED();
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   539
			case SLE_VAR_STRB:
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   540
			case SLE_VAR_STRBQ:
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   541
				len = SlCalcNetStringLen(ptr, length);
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   542
				break;
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   543
			case SLE_VAR_STR:
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   544
			case SLE_VAR_STRQ:
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   545
				ptr = *(char**)ptr;
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   546
				len = SlCalcNetStringLen(ptr, -1);
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   547
				break;
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   548
		}
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   549
3510
26ef8131b359 (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
		SlWriteArrayLength(len);
26ef8131b359 (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
		SlCopyBytes(ptr, len);
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   552
	} else { /* LOAD string */
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   553
		len = SlReadArrayLength();
3510
26ef8131b359 (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
   554
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   555
		switch (GetVarMemType(conv)) {
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   556
			default: NOT_REACHED();
5140
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   557
			case SLE_VAR_STRB:
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   558
			case SLE_VAR_STRBQ:
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   559
				if (len >= length) {
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   560
					DEBUG(misc, 0) ("[Sl] String length in savegame is bigger than buffer, truncating");
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   561
					SlCopyBytes(ptr, length);
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   562
					SlSkipBytes(len - length);
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   563
					len = length - 1;
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   564
				} else {
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   565
					SlCopyBytes(ptr, len);
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   566
				}
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   567
				break;
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   568
			case SLE_VAR_STR:
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   569
			case SLE_VAR_STRQ: /* Malloc'd string, free previous incarnation, and allocate */
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   570
				free(*(char**)ptr);
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   571
				*(char**)ptr = malloc(len + 1); // terminating '\0'
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   572
				ptr = *(char**)ptr;
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   573
				SlCopyBytes(ptr, len);
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   574
				break;
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   575
		}
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   576
3d58647a202a (svn r7228) -Codechange: [internal] Add the possibility to save/load string-pointers which do not
Darkvater
parents: 5116
diff changeset
   577
		((char*)ptr)[len] = '\0'; // properly terminate the string
3510
26ef8131b359 (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
   578
	}
3048
9dd3128a6358 (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
}
9dd3128a6358 (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
   580
1881
023a134a4b12 (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
   581
/**
023a134a4b12 (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
   582
 * Return the size in bytes of a certain type of atomic array
023a134a4b12 (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
   583
 * @param length The length of the array counted in elements
023a134a4b12 (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
   584
 * @param conv @VarType type of the variable that is used in calculating the size
023a134a4b12 (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
   585
 */
2958
3f8946daf55f (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   586
static inline size_t SlCalcArrayLen(uint length, VarType conv)
3f8946daf55f (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   587
{
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   588
	return SlCalcConvFileLen(conv) * length;
2958
3f8946daf55f (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   589
}
1881
023a134a4b12 (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
   590
023a134a4b12 (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
   591
/**
023a134a4b12 (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
   592
 * Save/Load an array.
023a134a4b12 (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
   593
 * @param array The array being manipulated
023a134a4b12 (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
   594
 * @param length The length of the array in elements
023a134a4b12 (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
   595
 * @param conv @VarType type of the atomic array (int, byte, uint64, etc.)
023a134a4b12 (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
   596
 */
023a134a4b12 (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
   597
void SlArray(void *array, uint length, VarType conv)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
	// Automatically calculate the length?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
	if (_sl.need_length != NL_NONE) {
2958
3f8946daf55f (svn r3520) Remove unused parameters from some functions
tron
parents: 2952
diff changeset
   601
		SlSetLength(SlCalcArrayLen(length, conv));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
		// Determine length only?
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   603
		if (_sl.need_length == NL_CALCLENGTH) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
1881
023a134a4b12 (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
   606
	/* NOTICE - handle some buggy stuff, in really old versions everything was saved
023a134a4b12 (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
   607
	 * as a byte-type. So detect this, and adjust array size accordingly */
2295
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
   608
	if (!_sl.save && _sl_version == 0) {
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   609
		if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
   610
				conv == SLE_INT32 || conv == SLE_UINT32) {
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   611
			length *= SlCalcConvFileLen(conv);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
			conv = SLE_INT8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   616
	/* If the size of elements is 1 byte both in file and memory, no special
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   617
	 * conversion is needed, use specialized copy-copy function to speed up things */
1881
023a134a4b12 (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
   618
	if (conv == SLE_INT8 || conv == SLE_UINT8) {
023a134a4b12 (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
   619
		SlCopyBytes(array, length);
023a134a4b12 (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
   620
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
		byte *a = (byte*)array;
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   622
		byte mem_size = SlCalcConvMemLen(conv);
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   623
1881
023a134a4b12 (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
   624
		for (; length != 0; length --) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
			SlSaveLoadConv(a, conv);
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   626
			a += mem_size; // get size
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   631
/* Are we going to save this object or not? */
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   632
static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   633
{
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   634
	if (_sl_version < sld->version_from || _sl_version > sld->version_to) return false;
3117
8066b8d0802b (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
   635
	if (sld->conv & SLF_SAVE_NO) return false;
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   636
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   637
	return true;
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   638
}
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   639
3117
8066b8d0802b (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
   640
/** Are we going to load this variable when loading a savegame or not?
8066b8d0802b (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
   641
 * @note If the variable is skipped it is skipped in the savegame
8066b8d0802b (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
   642
 * bytestream itself as well, so there is no need to skip it somewhere else */
8066b8d0802b (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
   643
static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
8066b8d0802b (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
   644
{
8066b8d0802b (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
   645
	if ((sld->conv & SLF_NETWORK_NO) && !_sl.save && _networking && !_network_server) {
8066b8d0802b (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
   646
		SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
8066b8d0802b (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
   647
		return true;
8066b8d0802b (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
   648
	}
8066b8d0802b (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
   649
8066b8d0802b (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
   650
	return false;
8066b8d0802b (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
   651
}
8066b8d0802b (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
   652
1881
023a134a4b12 (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
   653
/**
023a134a4b12 (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
   654
 * Calculate the size of an object.
023a134a4b12 (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
   655
 * @param sld The @SaveLoad description of the object so we know how to manipulate it
023a134a4b12 (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
   656
 */
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   657
static 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
   658
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
	size_t length = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
	// Need to determine the length and write a length tag.
1881
023a134a4b12 (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
   662
	for (; sld->cmd != SL_END; sld++) {
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   663
		length += SlCalcObjMemberLength(object, sld);
3046
f81bee40701f (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
   664
	}
f81bee40701f (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
   665
	return length;
f81bee40701f (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
   666
}
f81bee40701f (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
   667
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   668
size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
3046
f81bee40701f (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
   669
{
f81bee40701f (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
   670
	assert(_sl.save);
f81bee40701f (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
   671
f81bee40701f (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
   672
	switch (sld->cmd) {
f81bee40701f (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
   673
		case SL_VAR:
f81bee40701f (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
   674
		case SL_REF:
f81bee40701f (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
   675
		case SL_ARR:
3048
9dd3128a6358 (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
   676
		case SL_STR:
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   677
			/* CONDITIONAL saveload types depend on the savegame version */
3046
f81bee40701f (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
   678
			if (!SlIsObjectValidInSavegame(sld)) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
1881
023a134a4b12 (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
			switch (sld->cmd) {
3046
f81bee40701f (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
   681
			case SL_VAR: return SlCalcConvFileLen(sld->conv);
f81bee40701f (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
   682
			case SL_REF: return SlCalcRefLen();
f81bee40701f (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
   683
			case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   684
			case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
1881
023a134a4b12 (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
   685
			default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
			}
3046
f81bee40701f (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
   687
			break;
f81bee40701f (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
   688
		case SL_WRITEBYTE: return 1; // a byte is logically of size 1
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   689
		case SL_INCLUDE: return SlCalcObjLength(object, _sl.includes[sld->version_from]);
3046
f81bee40701f (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
   690
		default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
	}
3046
f81bee40701f (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
   692
	return 0;
f81bee40701f (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
   693
}
f81bee40701f (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
   694
4039
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   695
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   696
static uint ReferenceToInt(const void* obj, SLRefType rt);
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   697
static void* IntToReference(uint index, SLRefType rt);
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   698
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   699
3046
f81bee40701f (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
   700
bool SlObjectMember(void *ptr, const SaveLoad *sld)
f81bee40701f (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
   701
{
f81bee40701f (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
   702
	VarType conv = GB(sld->conv, 0, 8);
f81bee40701f (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
   703
	switch (sld->cmd) {
f81bee40701f (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
   704
	case SL_VAR:
f81bee40701f (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
   705
	case SL_REF:
f81bee40701f (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
   706
	case SL_ARR:
3048
9dd3128a6358 (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
   707
	case SL_STR:
3046
f81bee40701f (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
   708
		/* CONDITIONAL saveload types depend on the savegame version */
f81bee40701f (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
   709
		if (!SlIsObjectValidInSavegame(sld)) return false;
3117
8066b8d0802b (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
   710
		if (SlSkipVariableOnLoad(sld)) return false;
3046
f81bee40701f (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
   711
f81bee40701f (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
   712
		switch (sld->cmd) {
f81bee40701f (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
   713
		case SL_VAR: SlSaveLoadConv(ptr, conv); break;
f81bee40701f (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
   714
		case SL_REF: /* Reference variable, translate */
f81bee40701f (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
   715
			/// @todo XXX - another artificial limitof 65K elements of pointers?
f81bee40701f (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
   716
			if (_sl.save) { // XXX - read/write pointer as uint16? What is with higher indeces?
4039
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   717
				SlWriteUint16(ReferenceToInt(*(void**)ptr, conv));
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   718
			} else {
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   719
				*(void**)ptr = IntToReference(SlReadUint16(), conv);
eacd29ce18d9 (svn r5307) Remove two unnecessary indirections which used function pointers instead of directly calling the functions
tron
parents: 4016
diff changeset
   720
			}
3046
f81bee40701f (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
   721
			break;
f81bee40701f (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
   722
		case SL_ARR: SlArray(ptr, sld->length, conv); break;
3048
9dd3128a6358 (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
   723
		case SL_STR: SlString(ptr, sld->length, conv); break;
3046
f81bee40701f (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
   724
		default: NOT_REACHED();
f81bee40701f (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
   725
		}
f81bee40701f (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
   726
		break;
f81bee40701f (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
   727
f81bee40701f (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
   728
	/* SL_WRITEBYTE translates a value of a variable to another one upon
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4328
diff changeset
   729
   * saving or loading.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4328
diff changeset
   730
   * XXX - variable renaming abuse
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4328
diff changeset
   731
   * game_value: the value of the variable ingame is abused by sld->version_from
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4328
diff changeset
   732
   * file_value: the value of the variable in the savegame is abused by sld->version_to */
3046
f81bee40701f (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
   733
	case SL_WRITEBYTE:
f81bee40701f (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
   734
		if (_sl.save) {
f81bee40701f (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
   735
			SlWriteByte(sld->version_to);
f81bee40701f (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
   736
		} else {
f81bee40701f (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
   737
			*(byte*)ptr = sld->version_from;
f81bee40701f (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
   738
		}
f81bee40701f (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
   739
		break;
f81bee40701f (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
   740
f81bee40701f (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
   741
	/* SL_INCLUDE loads common code for a type
f81bee40701f (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
   742
	 * XXX - variable renaming abuse
f81bee40701f (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
   743
	 * include_index: common code to include from _desc_includes[], abused by sld->version_from */
f81bee40701f (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
   744
	case SL_INCLUDE:
f81bee40701f (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
   745
		SlObject(ptr, _sl.includes[sld->version_from]);
f81bee40701f (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
   746
		break;
f81bee40701f (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
   747
	default: NOT_REACHED();
f81bee40701f (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
   748
	}
f81bee40701f (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
   749
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   751
1881
023a134a4b12 (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
   752
/**
023a134a4b12 (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
   753
 * Main SaveLoad function.
023a134a4b12 (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
   754
 * @param object The object that is being saved or loaded
023a134a4b12 (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
   755
 * @param sld The @SaveLoad description of the object so we know how to manipulate it
023a134a4b12 (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
   756
 */
023a134a4b12 (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
   757
void SlObject(void *object, const SaveLoad *sld)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
	// Automatically calculate the length?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   760
	if (_sl.need_length != NL_NONE) {
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   761
		SlSetLength(SlCalcObjLength(object, sld));
3044
631d21c289e2 (svn r3624) - CodeChange: Some cosmetic changes in the saveload code;
Darkvater
parents: 3043
diff changeset
   762
		if (_sl.need_length == NL_CALCLENGTH) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
1881
023a134a4b12 (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
   765
	for (; sld->cmd != SL_END; sld++) {
5141
05a806850445 (svn r7231) -Codechange: rename ini_get_variable to GetVariableAddress for use both in settings.c
Darkvater
parents: 5140
diff changeset
   766
		void *ptr = GetVariableAddress(object, sld);
3046
f81bee40701f (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
   767
		SlObjectMember(ptr, sld);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   769
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   770
1881
023a134a4b12 (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
/**
023a134a4b12 (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
 * Save or Load (a list of) global variables
023a134a4b12 (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
   773
 * @param desc The global variable that is being loaded or saved
023a134a4b12 (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
   774
 */
3046
f81bee40701f (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
   775
void SlGlobList(const SaveLoadGlobVarList *sldg)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   776
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   777
	if (_sl.need_length != NL_NONE) {
5142
d423c895a5b4 (svn r7232) -Codechange: Also allow for the save/load of non pre-allocated strings inside structs.
Darkvater
parents: 5141
diff changeset
   778
		SlSetLength(SlCalcObjLength(NULL, (const SaveLoad*)sldg));
3046
f81bee40701f (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
   779
		if (_sl.need_length == NL_CALCLENGTH) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
	}
1881
023a134a4b12 (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
   781
3046
f81bee40701f (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
   782
	for (; sldg->cmd != SL_END; sldg++) {
3073
eb443625edae (svn r3662) Fix regression FS#58 with union, endiannes and static decleration problems. Removed the union.
Darkvater
parents: 3048
diff changeset
   783
		SlObjectMember(sldg->address, (const SaveLoad*)sldg);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
1881
023a134a4b12 (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
   787
/**
023a134a4b12 (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
   788
 * Do something of which I have no idea what it is :P
023a134a4b12 (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
   789
 * @param proc The callback procedure that is called
023a134a4b12 (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
   790
 * @param arg The variable that will be used for the callback procedure
023a134a4b12 (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
   791
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
void SlAutolength(AutolengthProc *proc, void *arg)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
	uint32 offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
	assert(_sl.save);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
	// Tell it to calculate the length
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
	_sl.need_length = NL_CALCLENGTH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
	_sl.obj_len = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
	proc(arg);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
	// Setup length
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
	_sl.need_length = NL_WANTLENGTH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
	SlSetLength(_sl.obj_len);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
	offs = SlGetOffs() + _sl.obj_len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   809
	// And write the stuff
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
	proc(arg);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   812
	assert(offs == SlGetOffs());
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   813
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
1881
023a134a4b12 (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
   815
/**
023a134a4b12 (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
   816
 * Load a chunk of data (eg vehicles, stations, etc.)
023a134a4b12 (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
   817
 * @param ch The chunkhandler that will be used for the operation
023a134a4b12 (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
   818
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
static void SlLoadChunk(const ChunkHandler *ch)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
	byte m = SlReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
	size_t len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   823
	uint32 endoffs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   824
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
	_sl.block_mode = m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
	_sl.obj_len = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   827
1881
023a134a4b12 (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
   828
	switch (m) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
	case CH_ARRAY:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
		_sl.array_index = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
		ch->load_proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
	case CH_SPARSE_ARRAY:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
		ch->load_proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
		break;
2041
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   836
	default:
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   837
		if ((m & 0xF) == CH_RIFF) {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   838
			// Read length
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   839
			len = (SlReadByte() << 16) | ((m >> 4) << 24);
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   840
			len += SlReadUint16();
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   841
			_sl.obj_len = len;
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   842
			endoffs = SlGetOffs() + len;
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   843
			ch->load_proc();
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   844
			assert(SlGetOffs() == endoffs);
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   845
		} else {
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   846
			SlError("Invalid chunk type");
8b8899617403 (svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.
ludde
parents: 2026
diff changeset
   847
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
1881
023a134a4b12 (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
   852
/* 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
   853
static ChunkSaveLoadProc *_tmp_proc_1;
1881
023a134a4b12 (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
   854
static inline void SlStubSaveProc2(void *arg) {_tmp_proc_1();}
023a134a4b12 (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
   855
static void SlStubSaveProc(void) {SlAutolength(SlStubSaveProc2, NULL);}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
1881
023a134a4b12 (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
   857
/** Save a chunk of data (eg. vehicles, stations, etc.). Each chunk is
023a134a4b12 (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
   858
 * prefixed by an ID identifying it, followed by data, and terminator where appropiate
023a134a4b12 (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
   859
 * @param ch The chunkhandler that will be used for the operation
023a134a4b12 (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
   860
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
static void SlSaveChunk(const ChunkHandler *ch)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
{
1881
023a134a4b12 (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
   863
	ChunkSaveLoadProc *proc = ch->save_proc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
	SlWriteUint32(ch->id);
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
   866
	DEBUG(misc, 1) ("[Sl] 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
   867
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
	if (ch->flags & CH_AUTO_LENGTH) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
		// Need to calculate the length. Solve that by calling SlAutoLength in the save_proc.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
		_tmp_proc_1 = proc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   871
		proc = SlStubSaveProc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   873
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   874
	_sl.block_mode = ch->flags & CH_TYPE_MASK;
1881
023a134a4b12 (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
   875
	switch (ch->flags & CH_TYPE_MASK) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
	case CH_RIFF:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
		_sl.need_length = NL_WANTLENGTH;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   878
		proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   879
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
	case CH_ARRAY:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   881
		_sl.last_array_index = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
		SlWriteByte(CH_ARRAY);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
		proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
		SlWriteArrayLength(0); // Terminate arrays
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
	case CH_SPARSE_ARRAY:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
		SlWriteByte(CH_SPARSE_ARRAY);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
		proc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
		SlWriteArrayLength(0); // Terminate arrays
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
		break;
1881
023a134a4b12 (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
	default: NOT_REACHED();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   894
1881
023a134a4b12 (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
   895
/** Save all chunks */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
   896
static void SlSaveChunks(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
	const ChunkHandler *ch;
1881
023a134a4b12 (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
	const ChunkHandler* const *chsc;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
	uint p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
1881
023a134a4b12 (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
   902
	for (p = 0; p != CH_NUM_PRI_LEVELS; p++) {
023a134a4b12 (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
   903
		for (chsc = _sl.chs; (ch = *chsc++) != NULL;) {
023a134a4b12 (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
   904
			while (true) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
				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
   906
					SlSaveChunk(ch);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
				if (ch->flags & CH_LAST)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   909
				ch++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
	// Terminator
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   915
	SlWriteUint32(0);
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
1881
023a134a4b12 (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
   918
/** Find the ChunkHandler that will be used for processing the found
023a134a4b12 (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
   919
 * chunk in the savegame or in memory
023a134a4b12 (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
   920
 * @param id the chunk in question
023a134a4b12 (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
   921
 * @return returns the appropiate chunkhandler
023a134a4b12 (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
   922
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
static const ChunkHandler *SlFindChunkHandler(uint32 id)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   925
	const ChunkHandler *ch;
1881
023a134a4b12 (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
	const ChunkHandler *const *chsc;
023a134a4b12 (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
	for (chsc = _sl.chs; (ch=*chsc++) != NULL;) {
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2927
diff changeset
   928
		for (;;) {
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2927
diff changeset
   929
			if (ch->id == id) return ch;
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2927
diff changeset
   930
			if (ch->flags & CH_LAST) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
			ch++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
1881
023a134a4b12 (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
   937
/** Load all chunks */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
   938
static void SlLoadChunks(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
	uint32 id;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
	const ChunkHandler *ch;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
1881
023a134a4b12 (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
   943
	for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
   944
		DEBUG(misc, 1) ("[Sl] Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1881
023a134a4b12 (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
   945
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
		ch = SlFindChunkHandler(id);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
		if (ch == NULL) SlError("found unknown tag in savegame (sync error)");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
		SlLoadChunk(ch);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
//*******************************************
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
//********** START OF LZO CODE **************
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
//*******************************************
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
#define LZO_SIZE 8192
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
781
9717ff353c17 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 762
diff changeset
   957
#include "minilzo.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
   959
static uint ReadLZO(void)
0
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
	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
   962
	uint32 tmp[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
	uint32 size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
	uint len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
	// Read header
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
	if (fread(tmp, sizeof(tmp), 1, _sl.fh) != 1) SlError("file read failed");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
	// Check if size is bad
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
	((uint32*)out)[0] = size = tmp[1];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   971
2295
af67e1ea8687 (svn r2819) Make variables, which are exclusive for internal use of the save/load code, static in saveload.c
tron
parents: 2293
diff changeset
   972
	if (_sl_version != 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
		tmp[0] = TO_BE32(tmp[0]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
		size = TO_BE32(size);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
	if (size >= sizeof(out)) SlError("inconsistent size");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
	// Read block
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
	if (fread(out + sizeof(uint32), size, 1, _sl.fh) != 1) SlError("file read failed");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
	// Verify checksum
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
	if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlError("bad checksum");
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   984
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
	// Decompress
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
	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
   987
	return len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
// p contains the pointer to the buffer, len contains the pointer to the length.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
// len bytes will be written, p and l will be updated to reflect the next buffer.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
static void WriteLZO(uint size)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
	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
   995
	byte wrkmem[sizeof(byte*)*4096];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
	uint outlen;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
   997
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
	lzo1x_1_compress(_sl.buf, size, out + sizeof(uint32)*2, &outlen, wrkmem);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
	((uint32*)out)[1] = TO_BE32(outlen);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
	((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
	if (fwrite(out, outlen + sizeof(uint32)*2, 1, _sl.fh) != 1) SlError("file write failed");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1004
static bool InitLZO(void)
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1005
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
	_sl.bufsize = LZO_SIZE;
2414
53ecd2131592 (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
  1007
	_sl.buf = _sl.buf_ori = (byte*)malloc(LZO_SIZE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1011
static void UninitLZO(void)
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1012
{
2414
53ecd2131592 (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
  1013
	free(_sl.buf_ori);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
1881
023a134a4b12 (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
//*********************************************
023a134a4b12 (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
//******** START OF NOCOMP CODE (uncompressed)*
023a134a4b12 (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
  1018
//*********************************************
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1019
static uint ReadNoComp(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1021
	return fread(_sl.buf, 1, LZO_SIZE, _sl.fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1022
}
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
static void WriteNoComp(uint size)
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
	fwrite(_sl.buf, 1, size, _sl.fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1027
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1029
static bool InitNoComp(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1030
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1031
	_sl.bufsize = LZO_SIZE;
2414
53ecd2131592 (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
  1032
	_sl.buf = _sl.buf_ori =(byte*)malloc(LZO_SIZE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1033
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1034
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1036
static void UninitNoComp(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
{
2414
53ecd2131592 (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
  1038
	free(_sl.buf_ori);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1039
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1040
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
//********************************************
1885
4ce583a5275b (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
  1042
//********** START OF MEMORY CODE (in ram)****
4ce583a5275b (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
  1043
//********************************************
4ce583a5275b (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
  1044
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1045
#include "network.h"
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1046
#include "table/strings.h"
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1047
#include "table/sprites.h"
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1048
#include "gfx.h"
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1049
#include "gui.h"
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1050
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1051
typedef struct ThreadedSave {
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1052
	uint count;
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1053
	bool ff_state;
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1054
	bool saveinprogress;
1914
5ede46fd496f (svn r2420) - Codechange: magic number elminitation of cursorsprites.
Darkvater
parents: 1913
diff changeset
  1055
	CursorID cursor;
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1056
} ThreadedSave;
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1057
1885
4ce583a5275b (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
  1058
/* A maximum size of of 128K * 500 = 64.000KB savegames */
5216
d581e4db95b6 (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
  1059
STATIC_OLD_POOL(Savegame, byte, 17, 500, NULL, NULL)
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1060
static ThreadedSave _ts;
1885
4ce583a5275b (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
  1061
4ce583a5275b (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
  1062
static bool InitMem(void)
4ce583a5275b (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
  1063
{
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1064
	_ts.count = 0;
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1065
4985
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1066
	CleanPool(&_Savegame_pool);
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1067
	AddBlockToPool(&_Savegame_pool);
1885
4ce583a5275b (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
  1068
4ce583a5275b (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
  1069
	/* A block from the pool is a contigious area of memory, so it is safe to write to it sequentially */
4985
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1070
	_sl.bufsize = GetSavegamePoolSize();
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1071
	_sl.buf = GetSavegame(_ts.count);
1885
4ce583a5275b (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
  1072
	return true;
4ce583a5275b (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
  1073
}
4ce583a5275b (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
  1074
4ce583a5275b (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
  1075
static void UnInitMem(void)
4ce583a5275b (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
  1076
{
4985
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1077
	CleanPool(&_Savegame_pool);
1885
4ce583a5275b (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
  1078
}
4ce583a5275b (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
  1079
4ce583a5275b (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
  1080
static void WriteMem(uint size)
4ce583a5275b (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
  1081
{
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1082
	_ts.count += size;
1885
4ce583a5275b (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
  1083
	/* Allocate new block and new buffer-pointer */
4985
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1084
	AddBlockIfNeeded(&_Savegame_pool, _ts.count);
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1085
	_sl.buf = GetSavegame(_ts.count);
1885
4ce583a5275b (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
  1086
}
4ce583a5275b (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
  1087
4ce583a5275b (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
  1088
//********************************************
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
//********** START OF ZLIB CODE **************
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1090
//********************************************
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
#if defined(WITH_ZLIB)
2694
99d88a9d5337 (svn r3236) - Fix: warnings about 'CDECL must be used with ...' on VS6
Darkvater
parents: 2685
diff changeset
  1093
#include <zlib.h>
2138
20c2cf0fb452 (svn r2648) Only use _stdcall calling convention for zlib on win32/msvc.
ludde
parents: 2126
diff changeset
  1094
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1095
static z_stream _z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1096
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1097
static bool InitReadZlib(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1098
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1099
	memset(&_z, 0, sizeof(_z));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1100
	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
  1101
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1102
	_sl.bufsize = 4096;
2414
53ecd2131592 (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
  1103
	_sl.buf = _sl.buf_ori = (byte*)malloc(4096 + 4096); // also contains fread buffer
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1104
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1105
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1106
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1107
static uint ReadZlib(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1108
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1109
	int r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1110
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
	_z.next_out = _sl.buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1112
	_z.avail_out = 4096;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1114
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
		// read more bytes from the file?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
		if (_z.avail_in == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
			_z.avail_in = fread(_z.next_in = _sl.buf + 4096, 1, 4096, _sl.fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
		// inflate the data
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
		r = inflate(&_z, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
		if (r == Z_STREAM_END)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1124
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1125
		if (r != Z_OK)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
			SlError("inflate() failed");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
	} while (_z.avail_out);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1129
	return 4096 - _z.avail_out;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1132
static void UninitReadZlib(void)
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
	inflateEnd(&_z);
2414
53ecd2131592 (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
  1135
	free(_sl.buf_ori);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1138
static bool InitWriteZlib(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
	memset(&_z, 0, sizeof(_z));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
	if (deflateInit(&_z, 6) != Z_OK) return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
	_sl.bufsize = 4096;
2414
53ecd2131592 (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
  1144
	_sl.buf = _sl.buf_ori = (byte*)malloc(4096); // also contains fread buffer
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
}
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
static void WriteZlibLoop(z_streamp z, byte *p, uint len, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
{
1884
ae1d6213c6dd (svn r2390) - Codechange: Fix some warnings on GCC 4.0.0
hackykid
parents: 1881
diff changeset
  1150
	byte buf[1024]; // output buffer
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
	int r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
	uint n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
	z->next_in = p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
	z->avail_in = len;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1156
		z->next_out = buf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
		z->avail_out = sizeof(buf);
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1993
diff changeset
  1158
		r = deflate(z, mode);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
			// bytes were emitted?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1160
		if ((n=sizeof(buf) - z->avail_out) != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1161
			if (fwrite(buf, n, 1, _sl.fh) != 1) SlError("file write error");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1162
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
		if (r == Z_STREAM_END)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1164
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1165
		if (r != Z_OK) SlError("zlib returned error code");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1166
	} while (z->avail_in || !z->avail_out);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1167
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1168
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1169
static void WriteZlib(uint len)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1170
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1171
	WriteZlibLoop(&_z, _sl.buf, len, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1172
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1174
static void UninitWriteZlib(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
	// flush any pending output.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1177
	if (_sl.fh) WriteZlibLoop(&_z, NULL, 0, Z_FINISH);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
	deflateEnd(&_z);
2414
53ecd2131592 (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
  1179
	free(_sl.buf_ori);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
1881
023a134a4b12 (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
  1182
#endif /* WITH_ZLIB */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
//*******************************************
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1185
//************* END OF CODE *****************
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1186
//*******************************************
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1187
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
// these define the chunks
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1189
extern const ChunkHandler _misc_chunk_handlers[];
3112
05a599216d83 (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
  1190
extern const ChunkHandler _setting_chunk_handlers[];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
extern const ChunkHandler _player_chunk_handlers[];
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2828
diff changeset
  1192
extern const ChunkHandler _engine_chunk_handlers[];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1193
extern const ChunkHandler _veh_chunk_handlers[];
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1537
diff changeset
  1194
extern const ChunkHandler _waypoint_chunk_handlers[];
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1195
extern const ChunkHandler _depot_chunk_handlers[];
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 950
diff changeset
  1196
extern const ChunkHandler _order_chunk_handlers[];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1197
extern const ChunkHandler _town_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1198
extern const ChunkHandler _sign_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1199
extern const ChunkHandler _station_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1200
extern const ChunkHandler _industry_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
extern const ChunkHandler _economy_chunk_handlers[];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
extern const ChunkHandler _animated_tile_chunk_handlers[];
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents: 5216
diff changeset
  1203
extern const ChunkHandler _newgrf_chunk_handlers[];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1205
static const ChunkHandler * const _chunk_handlers[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1206
	_misc_chunk_handlers,
3112
05a599216d83 (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
  1207
	_setting_chunk_handlers,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
	_veh_chunk_handlers,
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1537
diff changeset
  1209
	_waypoint_chunk_handlers,
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1210
	_depot_chunk_handlers,
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 950
diff changeset
  1211
	_order_chunk_handlers,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1212
	_industry_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1213
	_economy_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1214
	_engine_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
	_town_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
	_sign_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
	_station_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1218
	_player_chunk_handlers,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1219
	_animated_tile_chunk_handlers,
5228
c4a780348f66 (svn r7348) -Feature: Initial support for saving NewGRF settings with savegames. Back up your savegames...
peter1138
parents: 5216
diff changeset
  1220
	_newgrf_chunk_handlers,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1221
	NULL,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1222
};
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
// used to include a vehicle desc in another desc.
1881
023a134a4b12 (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
  1225
extern const SaveLoad _common_veh_desc[];
023a134a4b12 (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
  1226
static const SaveLoad* const _desc_includes[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1227
	_common_veh_desc
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
1881
023a134a4b12 (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
  1230
/**
023a134a4b12 (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
  1231
 * Pointers cannot be saved to a savegame, so this functions gets
023a134a4b12 (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
  1232
 * the index of the item, and if not available, it hussles with
023a134a4b12 (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
  1233
 * pointers (looks really bad :()
023a134a4b12 (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
  1234
 * Remember that a NULL item has value 0, and all
023a134a4b12 (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
  1235
 * indeces have +1, so vehicle 0 is saved as index 1.
023a134a4b12 (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
  1236
 * @param obj The object that we want to get the index of
023a134a4b12 (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
  1237
 * @param rt @SLRefType type of the object the index is being sought of
023a134a4b12 (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
  1238
 * @return Return the pointer converted to an index of the type pointed to
023a134a4b12 (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
  1239
 */
023a134a4b12 (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
  1240
static uint ReferenceToInt(const void *obj, SLRefType rt)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1241
{
1881
023a134a4b12 (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
  1242
	if (obj == NULL) return 0;
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1243
1881
023a134a4b12 (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
  1244
	switch (rt) {
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1245
		case REF_VEHICLE_OLD: // Old vehicles we save as new onces
2548
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1246
		case REF_VEHICLE:   return ((const  Vehicle*)obj)->index + 1;
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1247
		case REF_STATION:   return ((const  Station*)obj)->index + 1;
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1248
		case REF_TOWN:      return ((const     Town*)obj)->index + 1;
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1249
		case REF_ORDER:     return ((const    Order*)obj)->index + 1;
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2482
diff changeset
  1250
		case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2828
diff changeset
  1251
		case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1881
023a134a4b12 (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
  1252
		default: NOT_REACHED();
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1253
	}
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1254
1881
023a134a4b12 (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
  1255
	return 0; // avoid compiler warning
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
1881
023a134a4b12 (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
  1258
/**
023a134a4b12 (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
  1259
 * Pointers cannot be loaded from a savegame, so this function
023a134a4b12 (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
  1260
 * gets the index from the savegame and returns the appropiate
023a134a4b12 (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
  1261
 * pointer from the already loaded base.
023a134a4b12 (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
  1262
 * Remember that an index of 0 is a NULL pointer so all indeces
023a134a4b12 (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
  1263
 * are +1 so vehicle 0 is saved as 1.
023a134a4b12 (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
  1264
 * @param index The index that is being converted to a pointer
023a134a4b12 (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
  1265
 * @param rt @SLRefType type of the object the pointer is sought of
023a134a4b12 (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
  1266
 * @return Return the index converted to a pointer of any type
023a134a4b12 (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
  1267
 */
023a134a4b12 (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
  1268
static void *IntToReference(uint index, SLRefType rt)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
{
1881
023a134a4b12 (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
  1270
	/* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
023a134a4b12 (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
  1271
	 * and should be loaded like that */
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1272
	if (rt == REF_VEHICLE_OLD && !CheckSavegameVersionOldStyle(4, 4))
1881
023a134a4b12 (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
  1273
		rt = REF_VEHICLE;
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1274
1881
023a134a4b12 (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
  1275
	/* No need to look up NULL pointers, just return immediately */
023a134a4b12 (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
  1276
	if (rt != REF_VEHICLE_OLD && index == 0)
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1277
		return NULL;
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1278
1881
023a134a4b12 (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
  1279
	index--; // correct for the NULL index
023a134a4b12 (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
  1280
023a134a4b12 (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
  1281
	switch (rt) {
1314
d6a253cf92c3 (svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents: 1313
diff changeset
  1282
		case REF_ORDER: {
4977
ccecdac3075c (svn r6980) Use the pool macros for the Order pool
tron
parents: 4974
diff changeset
  1283
			if (!AddBlockIfNeeded(&_Order_pool, index))
1314
d6a253cf92c3 (svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents: 1313
diff changeset
  1284
				error("Orders: failed loading savegame: too many orders");
1881
023a134a4b12 (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
  1285
			return GetOrder(index);
1314
d6a253cf92c3 (svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents: 1313
diff changeset
  1286
		}
1279
4f83fbde72de (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1272
diff changeset
  1287
		case REF_VEHICLE: {
4972
af023b864bad (svn r6975) Use the pool macros for the Vehicle pool
tron
parents: 4712
diff changeset
  1288
			if (!AddBlockIfNeeded(&_Vehicle_pool, index))
1279
4f83fbde72de (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1272
diff changeset
  1289
				error("Vehicles: failed loading savegame: too many vehicles");
1881
023a134a4b12 (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
  1290
			return GetVehicle(index);
1279
4f83fbde72de (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1272
diff changeset
  1291
		}
1272
d3e09adb7736 (svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents: 1266
diff changeset
  1292
		case REF_STATION: {
4980
bdc86a42db2c (svn r6983) Use the pool macros for the Station pool
tron
parents: 4978
diff changeset
  1293
			if (!AddBlockIfNeeded(&_Station_pool, index))
1272
d3e09adb7736 (svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents: 1266
diff changeset
  1294
				error("Stations: failed loading savegame: too many stations");
1881
023a134a4b12 (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
  1295
			return GetStation(index);
1272
d3e09adb7736 (svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents: 1266
diff changeset
  1296
		}
1260
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1218
diff changeset
  1297
		case REF_TOWN: {
4983
16a0ede2f7fb (svn r6986) Use the pool macros for the Town pool
tron
parents: 4981
diff changeset
  1298
			if (!AddBlockIfNeeded(&_Town_pool, index))
1260
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1218
diff changeset
  1299
				error("Towns: failed loading savegame: too many towns");
1881
023a134a4b12 (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
  1300
			return GetTown(index);
1260
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1218
diff changeset
  1301
		}
1284
06a52178bf46 (svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents: 1282
diff changeset
  1302
		case REF_ROADSTOPS: {
4981
f19132348a43 (svn r6984) Use the pool macros for the RoadStop pool
tron
parents: 4980
diff changeset
  1303
			if (!AddBlockIfNeeded(&_RoadStop_pool, index))
1314
d6a253cf92c3 (svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents: 1313
diff changeset
  1304
				error("RoadStops: failed loading savegame: too many RoadStops");
1881
023a134a4b12 (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
  1305
			return GetRoadStop(index);
1284
06a52178bf46 (svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents: 1282
diff changeset
  1306
		}
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2828
diff changeset
  1307
		case REF_ENGINE_RENEWS: {
4974
c962d0622bc1 (svn r6977) Use the pool macros for the EngineRenew pool
tron
parents: 4972
diff changeset
  1308
			if (!AddBlockIfNeeded(&_EngineRenew_pool, index))
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2828
diff changeset
  1309
				error("EngineRenews: failed loading savegame: too many EngineRenews");
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2828
diff changeset
  1310
			return GetEngineRenew(index);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2828
diff changeset
  1311
		}
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1312
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1313
		case REF_VEHICLE_OLD: {
1881
023a134a4b12 (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
  1314
			/* Old vehicles were saved differently:
023a134a4b12 (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
  1315
			 * invalid vehicle was 0xFFFF,
023a134a4b12 (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
  1316
			 * and the index was not - 1.. correct for this */
023a134a4b12 (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
  1317
			index++;
023a134a4b12 (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
  1318
			if (index == INVALID_VEHICLE)
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1319
				return NULL;
1279
4f83fbde72de (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1272
diff changeset
  1320
4972
af023b864bad (svn r6975) Use the pool macros for the Vehicle pool
tron
parents: 4712
diff changeset
  1321
			if (!AddBlockIfNeeded(&_Vehicle_pool, index))
1279
4f83fbde72de (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1272
diff changeset
  1322
				error("Vehicles: failed loading savegame: too many vehicles");
1881
023a134a4b12 (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
  1323
			return GetVehicle(index);
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1324
		}
1881
023a134a4b12 (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
  1325
		default: NOT_REACHED();
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1326
	}
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1327
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1328
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1329
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1330
1881
023a134a4b12 (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
  1331
/** The format for a reader/writer type of a savegame */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1332
typedef struct {
1881
023a134a4b12 (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
  1333
	const char *name;           /// name of the compressor/decompressor (debug-only)
023a134a4b12 (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
  1334
	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
  1335
1881
023a134a4b12 (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
  1336
	bool (*init_read)(void);    /// function executed upon initalization of the loader
023a134a4b12 (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
  1337
	ReaderProc *reader;         /// function that loads the data from the file
023a134a4b12 (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
  1338
	void (*uninit_read)(void);  /// function executed when reading is finished
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1339
1881
023a134a4b12 (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
  1340
	bool (*init_write)(void);   /// function executed upon intialization of the saver
023a134a4b12 (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
  1341
	WriterProc *writer;         /// function that saves the data to the file
023a134a4b12 (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
  1342
	void (*uninit_write)(void); /// function executed when writing is done
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
} SaveLoadFormat;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1344
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1345
static const SaveLoadFormat _saveload_formats[] = {
1885
4ce583a5275b (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
  1346
	{"memory", 0,                NULL,         NULL,       NULL,           InitMem,       WriteMem,    UnInitMem},
4ce583a5275b (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
  1347
	{"lzo",    TO_BE32X('OTTD'), InitLZO,      ReadLZO,    UninitLZO,      InitLZO,       WriteLZO,    UninitLZO},
4ce583a5275b (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
  1348
	{"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
  1349
#if defined(WITH_ZLIB)
1885
4ce583a5275b (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
  1350
	{"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
  1351
#else
1885
4ce583a5275b (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
  1352
	{"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
  1353
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1354
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1355
1881
023a134a4b12 (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
  1356
/**
023a134a4b12 (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
 * Return the savegameformat of the game. Whether it was create with ZLIB compression
023a134a4b12 (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
  1358
 * uncompressed, or another type
1885
4ce583a5275b (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
  1359
 * @param s Name of the savegame format. If NULL it picks the first available one
1881
023a134a4b12 (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
  1360
 * @return Pointer to @SaveLoadFormat struct giving all characteristics of this type of savegame
023a134a4b12 (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
  1361
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1362
static const SaveLoadFormat *GetSavegameFormat(const char *s)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1363
{
1881
023a134a4b12 (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
  1364
	const SaveLoadFormat *def = endof(_saveload_formats) - 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1365
1881
023a134a4b12 (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
  1366
	// 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
  1367
	while (!def->init_write) def--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1368
1885
4ce583a5275b (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
  1369
	if (s != NULL && s[0] != '\0') {
4ce583a5275b (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
  1370
		const SaveLoadFormat *slf;
4ce583a5275b (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
  1371
		for (slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
4ce583a5275b (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
  1372
			if (slf->init_write != NULL && strcmp(s, slf->name) == 0)
4ce583a5275b (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
  1373
				return slf;
4ce583a5275b (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
  1374
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1375
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1376
		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
  1377
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1378
	return def;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1379
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1380
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1381
// actual loader/saver function
2828
996de8e891c7 (svn r3376) -Codechange: made enums for GenerateWorld and InitializeGame 'mode'
truelight
parents: 2808
diff changeset
  1382
void InitializeGame(int mode, uint size_x, uint size_y);
2808
35e0e9c6c227 (svn r3356) - Remove unused parameter of AfterLoadGame().
peter1138
parents: 2749
diff changeset
  1383
extern bool AfterLoadGame(void);
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1384
extern void BeforeSaveGame(void);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
extern bool LoadOldSaveGame(const char *file);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
1881
023a134a4b12 (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
/** Small helper function to close the to be loaded savegame an signal error */
2162
c1ded3bd3d0c (svn r2672) Move saving/loading related declarations to saveload.h
tron
parents: 2159
diff changeset
  1388
static inline SaveOrLoadResult AbortSaveLoad(void)
1881
023a134a4b12 (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
{
023a134a4b12 (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
  1390
	if (_sl.fh != NULL) fclose(_sl.fh);
023a134a4b12 (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
  1391
023a134a4b12 (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
	_sl.fh = NULL;
023a134a4b12 (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
  1393
	return SL_ERROR;
023a134a4b12 (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
  1394
}
023a134a4b12 (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
  1395
1885
4ce583a5275b (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
  1396
/** Update the gui accordingly when starting saving
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1397
 * and set locks on saveload. Also turn off fast-forward cause with that
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1398
 * saving takes Aaaaages */
2380
3b26659b4a9a (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
  1399
void SaveFileStart(void)
1885
4ce583a5275b (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
  1400
{
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1401
	_ts.ff_state = _fast_forward;
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1402
	_fast_forward = false;
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1403
	if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ);
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1404
1885
4ce583a5275b (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
  1405
	SendWindowMessage(WC_STATUS_BAR, 0, true, 0, 0);
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1406
	_ts.saveinprogress = true;
1885
4ce583a5275b (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
  1407
}
4ce583a5275b (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
  1408
4ce583a5275b (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
  1409
/** Update the gui accordingly when saving is done and release locks
4ce583a5275b (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
  1410
 * on saveload */
2380
3b26659b4a9a (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
  1411
void SaveFileDone(void)
1885
4ce583a5275b (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
  1412
{
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1413
	_fast_forward = _ts.ff_state;
1885
4ce583a5275b (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
  1414
	if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE);
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1415
1885
4ce583a5275b (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
  1416
	SendWindowMessage(WC_STATUS_BAR, 0, false, 0, 0);
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1417
	_ts.saveinprogress = false;
1885
4ce583a5275b (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
  1418
}
4ce583a5275b (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
  1419
2380
3b26659b4a9a (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
  1420
/** Show a gui message when saving has failed */
3b26659b4a9a (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
  1421
void SaveFileError(void)
3b26659b4a9a (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
  1422
{
3b26659b4a9a (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
  1423
	ShowErrorMessage(STR_4007_GAME_SAVE_FAILED, STR_NULL, 0, 0);
3b26659b4a9a (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
  1424
	SaveFileDone();
3b26659b4a9a (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
  1425
}
3b26659b4a9a (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
  1426
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 4257
diff changeset
  1427
static OTTDThread* save_thread;
2382
5af5004feae4 (svn r2908) If threaded saving fails or does not happen, sending subsequent messages might hang ottd. So don't send them :)
Darkvater
parents: 2380
diff changeset
  1428
4978
c25cbe937318 (svn r6981) Use the pool macros for the Savegame pool
tron
parents: 4977
diff changeset
  1429
/** We have written the whole game into memory, _Savegame_pool, now find
1885
4ce583a5275b (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
  1430
 * and appropiate compressor and start writing to file.
4ce583a5275b (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
  1431
 */
5421
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1432
static SaveOrLoadResult SaveFileToDisk(bool threaded)
1885
4ce583a5275b (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
  1433
{
2927
15690f1a9bb8 (svn r3483) -Fix: fixed warning about setjmp (tnx Bjarni for testing, and tnx for
truelight
parents: 2916
diff changeset
  1434
	const SaveLoadFormat *fmt;
1885
4ce583a5275b (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
  1435
	uint32 hdr[2];
4ce583a5275b (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
  1436
4ce583a5275b (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
  1437
	/* XXX - Setup setjmp error handler if an error occurs anywhere deep during
4ce583a5275b (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
  1438
	 * loading/saving execute a longjmp() and continue execution here */
4ce583a5275b (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
  1439
	if (setjmp(_sl.excpt)) {
4ce583a5275b (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
  1440
		AbortSaveLoad();
4ce583a5275b (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
  1441
		_sl.excpt_uninit();
4ce583a5275b (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
  1442
2413
4ba8f2b0da2e (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1443
		fprintf(stderr, "Save game failed: %s.", _sl.excpt_msg);
5421
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1444
		if (threaded) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1445
			OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1446
		} else {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1447
			SaveFileError();
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1448
		}
5421
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1449
		return SL_ERROR;
1885
4ce583a5275b (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
  1450
	}
4ce583a5275b (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
  1451
2927
15690f1a9bb8 (svn r3483) -Fix: fixed warning about setjmp (tnx Bjarni for testing, and tnx for
truelight
parents: 2916
diff changeset
  1452
	fmt = GetSavegameFormat(_savegame_format);
15690f1a9bb8 (svn r3483) -Fix: fixed warning about setjmp (tnx Bjarni for testing, and tnx for
truelight
parents: 2916
diff changeset
  1453
1885
4ce583a5275b (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
  1454
	/* We have written our stuff to memory, now write it to file! */
4ce583a5275b (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
  1455
	hdr[0] = fmt->tag;
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1456
	hdr[1] = TO_BE32(SAVEGAME_VERSION << 16);
1885
4ce583a5275b (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
  1457
	if (fwrite(hdr, sizeof(hdr), 1, _sl.fh) != 1) SlError("file write failed");
4ce583a5275b (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
  1458
4ce583a5275b (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
	if (!fmt->init_write()) SlError("cannot initialize compressor");
4ce583a5275b (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
4ce583a5275b (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
	{
4ce583a5275b (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
  1462
		uint i;
4985
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1463
		uint count = 1 << Savegame_POOL_BLOCK_SIZE_BITS;
1885
4ce583a5275b (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
  1464
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1465
		assert(_ts.count == _sl.offs_base);
4985
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1466
		for (i = 0; i != _Savegame_pool.current_blocks - 1; i++) {
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1467
			_sl.buf = _Savegame_pool.blocks[i];
1885
4ce583a5275b (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
  1468
			fmt->writer(count);
4ce583a5275b (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
  1469
		}
4ce583a5275b (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
  1470
4ce583a5275b (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
  1471
		/* The last block is (almost) always not fully filled, so only write away
4ce583a5275b (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
		 * as much data as it is in there */
4985
5eabae0d9108 (svn r6988) Remove a layer of indirection when using the Savegame pool
tron
parents: 4983
diff changeset
  1473
		_sl.buf = _Savegame_pool.blocks[i];
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1474
		fmt->writer(_ts.count - (i * count));
1885
4ce583a5275b (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
  1475
	}
4ce583a5275b (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
  1476
4ce583a5275b (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
  1477
	fmt->uninit_write();
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1478
	assert(_ts.count == _sl.offs_base);
1885
4ce583a5275b (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
  1479
	GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
4ce583a5275b (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
  1480
	fclose(_sl.fh);
4ce583a5275b (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
  1481
5421
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1482
	if (threaded) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE);
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1483
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1484
	return SL_OK;
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1485
}
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1486
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1487
static void* SaveFileToDiskThread(void *arg)
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1488
{
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1489
	SaveFileToDisk(true);
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
  1490
	return NULL;
1885
4ce583a5275b (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
  1491
}
4ce583a5275b (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
  1492
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1493
void WaitTillSaved(void)
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1494
{
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1495
	OTTDJoinThread(save_thread);
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1496
	save_thread = NULL;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1497
}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents: 2283
diff changeset
  1498
1881
023a134a4b12 (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
  1499
/**
023a134a4b12 (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
 * Main Save or Load function where the high-level saveload functions are
023a134a4b12 (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
  1501
 * handled. It opens the savegame, selects format and checks versions
023a134a4b12 (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
 * @param filename The name of the savegame being created/loaded
023a134a4b12 (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
 * @param mode Save or load. Load can also be a TTD(Patch) game. Use SL_LOAD, SL_OLD_LOAD or SL_SAVE
023a134a4b12 (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
 * @return Return the results of the action. SL_OK, SL_ERROR or SL_REINIT ("unload" the game)
023a134a4b12 (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
 */
2162
c1ded3bd3d0c (svn r2672) Move saving/loading related declarations to saveload.h
tron
parents: 2159
diff changeset
  1506
SaveOrLoadResult SaveOrLoad(const char *filename, int mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1507
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1508
	uint32 hdr[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1509
	const SaveLoadFormat *fmt;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1510
2413
4ba8f2b0da2e (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1511
	/* An instance of saving is already active, so don't go saving again */
4ba8f2b0da2e (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1512
	if (_ts.saveinprogress && mode == SL_SAVE) {
4ba8f2b0da2e (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1513
		// if not an autosave, but a user action, show error message
2749
46aba09ca365 (svn r3294) - Fix: use INVALID_STRING_ID instead of -1.
Darkvater
parents: 2694
diff changeset
  1514
		if (!_do_autosave) ShowErrorMessage(INVALID_STRING_ID, STR_SAVE_STILL_IN_PROGRESS, 0, 0);
2413
4ba8f2b0da2e (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1515
		return SL_OK;
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1516
	}
2413
4ba8f2b0da2e (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1517
	WaitTillSaved();
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1518
2413
4ba8f2b0da2e (svn r2939) Fix racing condition when using threaded saving (last one I hope).
Darkvater
parents: 2382
diff changeset
  1519
	/* Load a TTDLX or TTDPatch game */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
	if (mode == SL_OLD_LOAD) {
2828
996de8e891c7 (svn r3376) -Codechange: made enums for GenerateWorld and InitializeGame 'mode'
truelight
parents: 2808
diff changeset
  1521
		InitializeGame(IG_DATE_RESET, 256, 256); // set a mapsize of 256x256 for TTDPatch games or it might get confused
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
		if (!LoadOldSaveGame(filename)) return SL_REINIT;
2808
35e0e9c6c227 (svn r3356) - Remove unused parameter of AfterLoadGame().
peter1138
parents: 2749
diff changeset
  1523
		_sl_version = 0;
35e0e9c6c227 (svn r3356) - Remove unused parameter of AfterLoadGame().
peter1138
parents: 2749
diff changeset
  1524
		AfterLoadGame();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1525
		return SL_OK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1527
5167
2b9d61386688 (svn r7274) -Codechange [utf8]: Add input/output unicode support. This actually only changes win32
Darkvater
parents: 5163
diff changeset
  1528
	_sl.fh = (mode == SL_SAVE) ? fopen(filename, "wb") : fopen(filename, "rb");
1881
023a134a4b12 (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
  1529
	if (_sl.fh == NULL) {
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
  1530
		DEBUG(misc, 0) ("[Sl] Cannot open savegame for saving/loading.");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
		return SL_ERROR;
1881
023a134a4b12 (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
  1532
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1533
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1534
	_sl.bufe = _sl.bufp = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
	_sl.offs_base = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
	_sl.save = mode;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
	_sl.includes = _desc_includes;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
	_sl.chs = _chunk_handlers;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
1885
4ce583a5275b (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
  1540
	/* XXX - Setup setjmp error handler if an error occurs anywhere deep during
4ce583a5275b (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
  1541
	 * loading/saving execute a longjmp() and continue execution here */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1542
	if (setjmp(_sl.excpt)) {
1881
023a134a4b12 (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
  1543
		AbortSaveLoad();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1544
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
		// deinitialize compressor.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
		_sl.excpt_uninit();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
1881
023a134a4b12 (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
  1548
		/* A saver/loader exception!! reinitialize all variables to prevent crash! */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1549
		if (mode == SL_LOAD) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1550
			ShowInfoF("Load game failed: %s.", _sl.excpt_msg);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
			return SL_REINIT;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
		}
2382
5af5004feae4 (svn r2908) If threaded saving fails or does not happen, sending subsequent messages might hang ottd. So don't send them :)
Darkvater
parents: 2380
diff changeset
  1553
2380
3b26659b4a9a (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
  1554
		ShowInfoF("Save game failed: %s.", _sl.excpt_msg);
3b26659b4a9a (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
		return SL_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
1885
4ce583a5275b (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
  1558
	/* General tactic is to first save the game to memory, then use an available writer
4ce583a5275b (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
	 * to write it to file, either in threaded mode if possible, or single-threaded */
1881
023a134a4b12 (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
  1560
	if (mode == SL_SAVE) { /* SAVE game */
1885
4ce583a5275b (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
  1561
		fmt = GetSavegameFormat("memory"); // write to memory
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1562
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
		_sl.write_bytes = fmt->writer;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1564
		_sl.excpt_uninit = fmt->uninit_write;
1881
023a134a4b12 (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
  1565
		if (!fmt->init_write()) {
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
  1566
			DEBUG(misc, 0) ("[Sl] Initializing writer %s failed.", fmt->name);
1881
023a134a4b12 (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
  1567
			return AbortSaveLoad();
023a134a4b12 (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
  1568
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1569
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1570
		_sl_version = SAVEGAME_VERSION;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1571
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1572
		BeforeSaveGame();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1573
		SlSaveChunks();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1574
		SlWriteFill(); // flush the save buffer
1885
4ce583a5275b (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
  1575
4323
78204e7c3100 (svn r5977) -Fix [FS#78]: never set I-am-a-thread bool to true IN the thread. Machines with
truelight
parents: 4300
diff changeset
  1576
		SaveFileStart();
5421
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1577
		if (_network_server || (save_thread = OTTDCreateThread(&SaveFileToDiskThread, NULL)) == NULL) {
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1578
			SaveOrLoadResult result;
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1579
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1580
			if (!_network_server) DEBUG(misc, 1) ("Cannot create savegame thread, reverting to single-threaded mode...");
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1581
			result = SaveFileToDisk(false);
4323
78204e7c3100 (svn r5977) -Fix [FS#78]: never set I-am-a-thread bool to true IN the thread. Machines with
truelight
parents: 4300
diff changeset
  1582
			SaveFileDone();
5421
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1583
96c2e24bfe30 (svn r8222) [0.5] -Backport from trunk (lang, r8171, r8186, r8218):
Darkvater
parents: 5296
diff changeset
  1584
			return result;
1885
4ce583a5275b (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
  1585
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1586
1881
023a134a4b12 (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
  1587
	} else { /* LOAD game */
1885
4ce583a5275b (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
  1588
		assert(mode == SL_LOAD);
4ce583a5275b (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
  1589
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1590
		if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) {
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
  1591
			DEBUG(misc, 0) ("[Sl] Cannot read savegame header, aborting.");
1881
023a134a4b12 (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
  1592
			return AbortSaveLoad();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1593
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1594
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1595
		// see if we have any loader for this type.
1881
023a134a4b12 (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
  1596
		for (fmt = _saveload_formats; ; fmt++) {
023a134a4b12 (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
  1597
			/* No loader found, treat as version 0 and use LZO format */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1598
			if (fmt == endof(_saveload_formats)) {
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
  1599
				DEBUG(misc, 0) ("[Sl] Unknown savegame type, trying to load it as the buggy format.");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1600
				rewind(_sl.fh);
4016
ee7635041a37 (svn r5229) Remove a write-only variable
glx
parents: 3977
diff changeset
  1601
				_sl_version = 0;
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1602
				_sl_minor_version = 0;
1913
57f516fa418c (svn r2419) - Fix: saving no longer changes your cursor if it is in a non-mouse state.
Darkvater
parents: 1891
diff changeset
  1603
				fmt = _saveload_formats + 1; // LZO
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1604
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1605
			}
1881
023a134a4b12 (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
  1606
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1607
			if (fmt->tag == hdr[0]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1608
				// check version number
4016
ee7635041a37 (svn r5229) Remove a write-only variable
glx
parents: 3977
diff changeset
  1609
				_sl_version = TO_BE32(hdr[1]) >> 16;
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1610
				/* Minor is not used anymore from version 18.0, but it is still needed
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1611
				 *  in versions before that (4 cases) which can't be removed easy.
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1612
				 *  Therefor it is loaded, but never saved (or, it saves a 0 in any scenario).
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1613
				 *  So never EVER use this minor version again. -- TrueLight -- 22-11-2005 */
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1614
				_sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1615
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
  1616
				DEBUG(misc, 1)("[Sl] Loading savegame version %d", _sl_version);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 121
diff changeset
  1617
938
4f84a0530758 (svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents: 929
diff changeset
  1618
				/* Is the version higher than the current? */
2685
00111d5ca47f (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2676
diff changeset
  1619
				if (_sl_version > SAVEGAME_VERSION) {
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
  1620
					DEBUG(misc, 0) ("[Sl] Savegame version invalid.");
1881
023a134a4b12 (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
  1621
					return AbortSaveLoad();
023a134a4b12 (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
  1622
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1623
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1624
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1625
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1626
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1627
		_sl.read_bytes = fmt->reader;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
		_sl.excpt_uninit = fmt->uninit_read;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1629
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1630
		// loader for this savegame type is not implemented?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1631
		if (fmt->init_read == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1632
			ShowInfoF("Loader for '%s' is not available.", fmt->name);
1881
023a134a4b12 (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
			return AbortSaveLoad();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1634
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1635
1881
023a134a4b12 (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
		if (!fmt->init_read()) {
3043
cb8509834e42 (svn r3623) - Standardize DEBUG() messages in saveload.c
Darkvater
parents: 2958
diff changeset
  1637
			DEBUG(misc, 0) ("[Sl] Initializing loader %s failed.", fmt->name);
1881
023a134a4b12 (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
			return AbortSaveLoad();
023a134a4b12 (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
  1639
		}
023a134a4b12 (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
  1640
1885
4ce583a5275b (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
  1641
		/* Old maps were hardcoded to 256x256 and thus did not contain
4ce583a5275b (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
  1642
		 * any mapsize information. Pre-initialize to 256x256 to not to
4ce583a5275b (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
  1643
		 * confuse old games */
2828
996de8e891c7 (svn r3376) -Codechange: made enums for GenerateWorld and InitializeGame 'mode'
truelight
parents: 2808
diff changeset
  1644
		InitializeGame(IG_DATE_RESET, 256, 256);
1218
353a7773bc3c (svn r1722) -Feature: Bigger maps - anyone?
tron
parents: 1217
diff changeset
  1645
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1646
		SlLoadChunks();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1647
		fmt->uninit_read();
1885
4ce583a5275b (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
  1648
		fclose(_sl.fh);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1649
1885
4ce583a5275b (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
  1650
		/* After loading fix up savegame for any internal changes that
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4328
diff changeset
  1651
		 * might've occured since then. If it fails, load back the old game */
2808
35e0e9c6c227 (svn r3356) - Remove unused parameter of AfterLoadGame().
peter1138
parents: 2749
diff changeset
  1652
		if (!AfterLoadGame()) return SL_REINIT;
1885
4ce583a5275b (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
  1653
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1654
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1655
	return SL_OK;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1656
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1657
1881
023a134a4b12 (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
  1658
/** Do a save when exiting the game (patch option) _patches.autosave_on_exit */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1024
diff changeset
  1659
void DoExitSave(void)
643
6f04156241bd (svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents: 617
diff changeset
  1660
{
6f04156241bd (svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents: 617
diff changeset
  1661
	char buf[200];
5296
6a4aaa66eed3 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5228
diff changeset
  1662
	snprintf(buf, sizeof(buf), "%s%sexit.sav", _paths.autosave_dir, PATHSEP);
643
6f04156241bd (svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents: 617
diff changeset
  1663
	SaveOrLoad(buf, SL_SAVE);
6f04156241bd (svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents: 617
diff changeset
  1664
}
6f04156241bd (svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents: 617
diff changeset
  1665
1881
023a134a4b12 (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
  1666
#if 0
023a134a4b12 (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
  1667
/**
023a134a4b12 (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
  1668
 * Function to get the type of the savegame by looking at the file header.
023a134a4b12 (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
  1669
 * NOTICE: Not used right now, but could be used if extensions of savegames are garbled
023a134a4b12 (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
  1670
 * @param file Savegame to be checked
023a134a4b12 (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
  1671
 * @return SL_OLD_LOAD or SL_LOAD of the file
023a134a4b12 (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
  1672
 */
023a134a4b12 (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
  1673
int GetSavegameType(char *file)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1674
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1675
	const SaveLoadFormat *fmt;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1676
	uint32 hdr;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1677
	FILE *f;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1678
	int mode = SL_OLD_LOAD;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1679
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1680
	f = fopen(file, "rb");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1681
	if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
5015
8e2f6a5df288 (svn r7048) -Cleanup: DEBUG doesn't need a newline character, use DEBUG instead of printf and the
Darkvater
parents: 4985
diff changeset
  1682
		DEBUG(misc, 0) ("[Sl] Savegame is obsolete or invalid format");
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1993
diff changeset
  1683
		mode = SL_LOAD; // don't try to get filename, just show name as it is written
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4059
diff changeset
  1684
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1685
		// see if we have any loader for this type.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1686
		for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1687
			if (fmt->tag == hdr) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1688
				mode = SL_LOAD; // new type of savegame
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1689
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1690
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1691
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1692
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1693
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1694
	fclose(f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1695
	return mode;
1881
023a134a4b12 (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
  1696
}
023a134a4b12 (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
  1697
#endif