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