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