author | Darkvater |
Wed, 23 Feb 2005 00:07:03 +0000 | |
changeset 1405 | 1ca0d65db76c |
parent 1314 | 10c3417e9c22 |
child 1510 | 8c1956e47e09 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
1299
39c06aba09aa
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1284
diff
changeset
|
3 |
#include "debug.h" |
0 | 4 |
#include "vehicle.h" |
5 |
#include "station.h" |
|
6 |
#include "town.h" |
|
7 |
#include "player.h" |
|
8 |
#include "saveload.h" |
|
9 |
||
10 |
enum { |
|
1405
1ca0d65db76c
(svn r1909) - Fix: [ 1145593 ] assertion failed when saving the game. It is nice to also update the savegame format when you update town statistics :)
Darkvater
parents:
1314
diff
changeset
|
11 |
SAVEGAME_MAJOR_VERSION = 9, |
1266
eccd576e322f
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1264
diff
changeset
|
12 |
SAVEGAME_MINOR_VERSION = 0, |
0 | 13 |
|
14 |
SAVEGAME_LOADABLE_VERSION = (SAVEGAME_MAJOR_VERSION << 8) + SAVEGAME_MINOR_VERSION |
|
15 |
}; |
|
16 |
||
17 |
/******************************************************/ |
|
18 |
/******************************************************/ |
|
19 |
/******************************************************/ |
|
20 |
||
21 |
enum NeedLengthValues { NL_NONE = 0,NL_WANTLENGTH = 1,NL_CALCLENGTH = 2}; |
|
22 |
||
23 |
SaverLoader _sl; |
|
24 |
||
25 |
// fill the input buffer |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
26 |
static void SlReadFill(void) |
0 | 27 |
{ |
28 |
uint len = _sl.read_bytes(); |
|
29 |
assert(len != 0); |
|
30 |
||
31 |
_sl.bufp = _sl.buf; |
|
32 |
_sl.bufe = _sl.buf + len; |
|
33 |
_sl.offs_base += len; |
|
34 |
} |
|
35 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
36 |
static uint32 SlGetOffs(void) |
0 | 37 |
{ |
38 |
return _sl.offs_base - (_sl.bufe - _sl.bufp); |
|
39 |
} |
|
40 |
||
41 |
// flush the output buffer |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
42 |
static void SlWriteFill(void) |
0 | 43 |
{ |
44 |
// flush current buffer? |
|
45 |
if (_sl.bufp != NULL) { |
|
46 |
uint len = _sl.bufp - _sl.buf; |
|
47 |
_sl.offs_base += len; |
|
48 |
if (len) _sl.write_bytes(len); |
|
49 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
50 |
|
0 | 51 |
// setup next buffer |
52 |
_sl.bufp = _sl.buf; |
|
53 |
_sl.bufe = _sl.buf + _sl.bufsize; |
|
54 |
} |
|
55 |
||
56 |
// error handler, calls longjmp to simulate an exception. |
|
57 |
static void NORETURN SlError(const char *msg) |
|
58 |
{ |
|
59 |
_sl.excpt_msg = msg; |
|
60 |
longjmp(_sl.excpt, 0); |
|
61 |
} |
|
62 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
63 |
int SlReadByte(void) |
0 | 64 |
{ |
65 |
if (_sl.bufp == _sl.bufe) SlReadFill(); |
|
66 |
return *_sl.bufp++; |
|
67 |
} |
|
68 |
||
69 |
void SlWriteByte(byte v) |
|
70 |
{ |
|
71 |
if (_sl.bufp == _sl.bufe) SlWriteFill(); |
|
72 |
*_sl.bufp++ = v; |
|
73 |
} |
|
74 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
75 |
static int SlReadUint16(void) |
0 | 76 |
{ |
77 |
int x = SlReadByte() << 8; |
|
78 |
return x | SlReadByte(); |
|
79 |
} |
|
80 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
81 |
static uint32 SlReadUint32(void) |
0 | 82 |
{ |
83 |
uint32 x = SlReadUint16() << 16; |
|
84 |
return x | SlReadUint16(); |
|
85 |
} |
|
86 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
87 |
static uint64 SlReadUint64(void) |
0 | 88 |
{ |
89 |
uint32 x = SlReadUint32(); |
|
90 |
uint32 y = SlReadUint32(); |
|
91 |
return (uint64)x << 32 | y; |
|
92 |
} |
|
93 |
||
410 | 94 |
static void SlWriteUint16(uint16 v) |
0 | 95 |
{ |
96 |
SlWriteByte((byte)(v >> 8)); |
|
97 |
SlWriteByte((byte)v); |
|
98 |
} |
|
99 |
||
410 | 100 |
static void SlWriteUint32(uint32 v) |
0 | 101 |
{ |
102 |
SlWriteUint16((uint16)(v >> 16)); |
|
103 |
SlWriteUint16((uint16)v); |
|
104 |
} |
|
105 |
||
410 | 106 |
static void SlWriteUint64(uint64 x) |
0 | 107 |
{ |
108 |
SlWriteUint32((uint32)(x >> 32)); |
|
109 |
SlWriteUint32((uint32)x); |
|
110 |
} |
|
111 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
112 |
static int SlReadSimpleGamma(void) |
0 | 113 |
{ |
114 |
int x = SlReadByte(); |
|
115 |
if (x & 0x80) |
|
116 |
x = ((x&0x7F) << 8) + SlReadByte(); |
|
117 |
return x; |
|
118 |
} |
|
119 |
||
120 |
static void SlWriteSimpleGamma(uint i) |
|
121 |
{ |
|
122 |
assert(i < (1 << 14)); |
|
123 |
if (i >= 0x80) { |
|
124 |
SlWriteByte((byte)(0x80|(i >> 8))); |
|
125 |
SlWriteByte((byte)i); |
|
126 |
} else { |
|
127 |
SlWriteByte(i); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
static uint SlGetGammaLength(uint i) { |
|
132 |
return (i>=0x80) ? 2 : 1; |
|
133 |
} |
|
134 |
||
1095 | 135 |
static inline int SlReadSparseIndex(void) |
0 | 136 |
{ |
137 |
return SlReadSimpleGamma(); |
|
138 |
} |
|
139 |
||
1095 | 140 |
static inline void SlWriteSparseIndex(uint index) |
0 | 141 |
{ |
142 |
SlWriteSimpleGamma(index); |
|
143 |
} |
|
144 |
||
1095 | 145 |
static inline int SlReadArrayLength(void) |
0 | 146 |
{ |
147 |
return SlReadSimpleGamma(); |
|
148 |
} |
|
149 |
||
1095 | 150 |
static inline void SlWriteArrayLength(uint length) |
0 | 151 |
{ |
152 |
SlWriteSimpleGamma(length); |
|
153 |
} |
|
154 |
||
155 |
void SlSetArrayIndex(uint index) |
|
156 |
{ |
|
157 |
_sl.need_length = NL_WANTLENGTH; |
|
158 |
_sl.array_index = index; |
|
159 |
} |
|
160 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
161 |
int SlIterateArray(void) |
0 | 162 |
{ |
163 |
int ind; |
|
164 |
static uint32 next_offs; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
165 |
|
0 | 166 |
// Must be at end of current block. |
167 |
assert(next_offs == 0 || SlGetOffs() == next_offs); |
|
168 |
||
169 |
while(true) { |
|
170 |
uint len = SlReadArrayLength(); |
|
171 |
if (len == 0) { |
|
172 |
next_offs = 0; |
|
173 |
return -1; |
|
174 |
} |
|
175 |
||
176 |
_sl.obj_len = --len; |
|
177 |
next_offs = SlGetOffs() + len; |
|
178 |
||
179 |
switch(_sl.block_mode) { |
|
180 |
case CH_SPARSE_ARRAY: ind = SlReadSparseIndex(); break; |
|
181 |
case CH_ARRAY: ind = _sl.array_index++; break; |
|
182 |
default: |
|
183 |
DEBUG(misc, 0) ("SlIterateArray: error\n"); |
|
184 |
return -1; // error |
|
185 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
186 |
|
0 | 187 |
if (len != 0) |
188 |
return ind; |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
// Sets the length of either a RIFF object or the number of items in an array. |
|
193 |
void SlSetLength(uint length) |
|
194 |
{ |
|
195 |
switch(_sl.need_length) { |
|
196 |
case NL_WANTLENGTH: |
|
197 |
_sl.need_length = NL_NONE; |
|
198 |
switch(_sl.block_mode) { |
|
199 |
case CH_RIFF: |
|
200 |
// Really simple to write a RIFF length :) |
|
201 |
SlWriteUint32(length); |
|
202 |
break; |
|
203 |
case CH_ARRAY: |
|
204 |
assert(_sl.array_index >= _sl.last_array_index); |
|
205 |
while (++_sl.last_array_index <= _sl.array_index) |
|
206 |
SlWriteArrayLength(1); |
|
207 |
SlWriteArrayLength(length + 1); |
|
208 |
break; |
|
209 |
case CH_SPARSE_ARRAY: |
|
210 |
SlWriteArrayLength(length + 1 + SlGetGammaLength(_sl.array_index)); // Also include length of sparse index. |
|
211 |
SlWriteSparseIndex(_sl.array_index); |
|
212 |
break; |
|
213 |
default: NOT_REACHED(); |
|
214 |
} |
|
215 |
break; |
|
216 |
case NL_CALCLENGTH: |
|
217 |
_sl.obj_len += length; |
|
218 |
break; |
|
219 |
} |
|
220 |
} |
|
221 |
||
410 | 222 |
static void SlCopyBytes(void *ptr, size_t length) |
0 | 223 |
{ |
224 |
byte *p = (byte*)ptr; |
|
225 |
||
226 |
if (_sl.save) { |
|
227 |
while(length) { |
|
228 |
SlWriteByte(*p++); |
|
229 |
length--; |
|
230 |
} |
|
231 |
} else { |
|
232 |
while(length) { |
|
233 |
// INLINED SlReadByte |
|
234 |
#if !defined(_DEBUG) |
|
235 |
if (_sl.bufp == _sl.bufe) SlReadFill(); |
|
236 |
*p++ = *_sl.bufp++; |
|
237 |
#else |
|
238 |
*p++ = SlReadByte(); |
|
239 |
#endif |
|
240 |
length--; |
|
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
||
1095 | 245 |
static void SlSkipBytes(size_t length) |
0 | 246 |
{ |
247 |
while (length) { |
|
248 |
SlReadByte(); |
|
249 |
length--; |
|
250 |
} |
|
251 |
} |
|
252 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
253 |
uint SlGetFieldLength(void) |
0 | 254 |
{ |
255 |
return _sl.obj_len; |
|
256 |
} |
|
257 |
||
258 |
||
259 |
static void SlSaveLoadConv(void *ptr, uint conv) |
|
260 |
{ |
|
261 |
int64 x = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
262 |
|
0 | 263 |
if (_sl.save) { |
264 |
// Read a value from the struct. These ARE endian safe. |
|
265 |
switch((conv >> 4)&0xf) { |
|
266 |
case SLE_VAR_I8>>4: x = *(int8*)ptr; break; |
|
267 |
case SLE_VAR_U8>>4: x = *(byte*)ptr; break; |
|
268 |
case SLE_VAR_I16>>4: x = *(int16*)ptr; break; |
|
269 |
case SLE_VAR_U16>>4: x = *(uint16*)ptr; break; |
|
270 |
case SLE_VAR_I32>>4: x = *(int32*)ptr; break; |
|
271 |
case SLE_VAR_U32>>4: x = *(uint32*)ptr; break; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
272 |
case SLE_VAR_I64>>4: x = *(int64*)ptr; break; |
0 | 273 |
case SLE_VAR_U64>>4: x = *(uint64*)ptr; break; |
274 |
case SLE_VAR_NULL>>4: x = 0; break; |
|
275 |
default: |
|
276 |
NOT_REACHED(); |
|
277 |
} |
|
278 |
||
279 |
// Write it to the file |
|
280 |
switch(conv & 0xF) { |
|
281 |
case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break; |
|
282 |
case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break; |
|
283 |
case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break; |
|
284 |
case SLE_FILE_STRINGID: |
|
285 |
case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break; |
|
286 |
case SLE_FILE_I32: |
|
287 |
case SLE_FILE_U32:SlWriteUint32((uint32)x);break; |
|
288 |
case SLE_FILE_I64: |
|
289 |
case SLE_FILE_U64:SlWriteUint64(x);break; |
|
290 |
default: |
|
291 |
assert(0); |
|
292 |
NOT_REACHED(); |
|
293 |
} |
|
294 |
} else { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
295 |
|
0 | 296 |
// Read a value from the file |
297 |
switch(conv & 0xF) { |
|
298 |
case SLE_FILE_I8: x = (int8)SlReadByte(); break; |
|
299 |
case SLE_FILE_U8: x = (byte)SlReadByte(); break; |
|
300 |
case SLE_FILE_I16: x = (int16)SlReadUint16(); break; |
|
301 |
case SLE_FILE_U16: x = (uint16)SlReadUint16(); break; |
|
302 |
case SLE_FILE_I32: x = (int32)SlReadUint32(); break; |
|
303 |
case SLE_FILE_U32: x = (uint32)SlReadUint32(); break; |
|
304 |
case SLE_FILE_I64: x = (int64)SlReadUint64(); break; |
|
305 |
case SLE_FILE_U64: x = (uint64)SlReadUint64(); break; |
|
306 |
case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break; |
|
307 |
default: |
|
308 |
assert(0); |
|
309 |
NOT_REACHED(); |
|
310 |
} |
|
311 |
||
312 |
// Write it to the struct, these ARE endian safe. |
|
313 |
switch((conv >> 4)&0xf) { |
|
314 |
case SLE_VAR_I8>>4: *(int8*)ptr = x; break; |
|
315 |
case SLE_VAR_U8>>4: *(byte*)ptr = x; break; |
|
316 |
case SLE_VAR_I16>>4: *(int16*)ptr = x; break; |
|
317 |
case SLE_VAR_U16>>4: *(uint16*)ptr = x; break; |
|
318 |
case SLE_VAR_I32>>4: *(int32*)ptr = x; break; |
|
319 |
case SLE_VAR_U32>>4: *(uint32*)ptr = x; break; |
|
320 |
case SLE_VAR_I64>>4: *(int64*)ptr = x; break; |
|
321 |
case SLE_VAR_U64>>4: *(uint64*)ptr = x; break; |
|
322 |
case SLE_VAR_NULL: break; |
|
323 |
default: |
|
324 |
NOT_REACHED(); |
|
325 |
} |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
static const byte _conv_lengths[] = {1,1,2,2,4,4,8,8,2}; |
|
330 |
||
331 |
static uint SlCalcConvLen(uint conv, void *p) |
|
332 |
{ |
|
333 |
return _conv_lengths[conv & 0xF]; |
|
334 |
} |
|
335 |
||
336 |
static uint SlCalcArrayLen(void *array, uint length, uint conv) |
|
337 |
{ |
|
338 |
return _conv_lengths[conv & 0xF] * length; |
|
339 |
} |
|
340 |
||
341 |
static const byte _conv_mem_size[9] = {1,1,2,2,4,4,8,8,0}; |
|
342 |
void SlArray(void *array, uint length, uint conv) |
|
343 |
{ |
|
344 |
// Automatically calculate the length? |
|
345 |
if (_sl.need_length != NL_NONE) { |
|
346 |
SlSetLength(SlCalcArrayLen(array, length, conv)); |
|
347 |
// Determine length only? |
|
348 |
if (_sl.need_length == NL_CALCLENGTH) |
|
349 |
return; |
|
350 |
} |
|
351 |
||
352 |
// handle buggy stuff |
|
353 |
if (!_sl.save && _sl.version == 0) { |
|
354 |
if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID) { |
|
355 |
length *= 2; |
|
356 |
conv = SLE_INT8; |
|
357 |
} else if (conv == SLE_INT32 || conv == SLE_UINT32) { |
|
358 |
length *= 4; |
|
359 |
conv = SLE_INT8; |
|
360 |
} |
|
361 |
} |
|
362 |
||
363 |
// Optimized cases when input equals output. |
|
364 |
switch(conv) { |
|
365 |
case SLE_INT8: |
|
366 |
case SLE_UINT8:SlCopyBytes(array, length);break; |
|
367 |
default: { |
|
368 |
// Default "slow" case. |
|
369 |
byte *a = (byte*)array; |
|
370 |
while (length) { |
|
371 |
SlSaveLoadConv(a, conv); |
|
372 |
a += _conv_mem_size[(conv >> 4)&0xf]; |
|
373 |
length--; |
|
374 |
} |
|
375 |
} |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
// Calculate the size of an object. |
|
380 |
static size_t SlCalcObjLength(void *object, const void *desc) |
|
381 |
{ |
|
382 |
size_t length = 0; |
|
383 |
uint cmd,conv; |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
193
diff
changeset
|
384 |
const byte *d = (const byte*)desc; |
0 | 385 |
|
386 |
// Need to determine the length and write a length tag. |
|
387 |
while (true) { |
|
388 |
cmd = (d[0] >> 4); |
|
389 |
if (cmd < 8) { |
|
390 |
conv = d[2]; |
|
391 |
d += 3; |
|
392 |
if (cmd&4) { |
|
393 |
d += 2; |
|
394 |
// check if the field is of the right version |
|
395 |
if (_sl.version < d[-2] || _sl.version > d[-1]) { |
|
396 |
if ((cmd & 3) == 2) d++; |
|
397 |
continue; |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
switch(cmd&3) { |
|
402 |
// Normal variable |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
403 |
case 0: length += SlCalcConvLen(conv, NULL);break; |
0 | 404 |
// Reference |
405 |
case 1: length += 2; break; |
|
406 |
// Array |
|
407 |
case 2: length += SlCalcArrayLen(NULL, *d++, conv); break; |
|
408 |
default:NOT_REACHED(); |
|
409 |
} |
|
410 |
} else if (cmd == 8) { |
|
411 |
length++; |
|
412 |
d += 4; |
|
413 |
} else if (cmd == 9) { |
|
414 |
length += SlCalcObjLength(NULL, _sl.includes[d[2]]); |
|
415 |
d += 3; |
|
416 |
} else if (cmd == 15) |
|
417 |
break; |
|
418 |
else |
|
419 |
assert(0); |
|
420 |
} |
|
421 |
return length; |
|
422 |
} |
|
423 |
||
424 |
void SlObject(void *object, const void *desc) |
|
425 |
{ |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
193
diff
changeset
|
426 |
const byte *d = (const byte*)desc; |
0 | 427 |
void *ptr; |
428 |
uint cmd,conv; |
|
429 |
||
430 |
// Automatically calculate the length? |
|
431 |
if (_sl.need_length != NL_NONE) { |
|
432 |
SlSetLength(SlCalcObjLength(object, d)); |
|
433 |
if (_sl.need_length == NL_CALCLENGTH) |
|
434 |
return; |
|
435 |
} |
|
436 |
||
437 |
while (true) { |
|
438 |
// Currently it only supports up to 4096 byte big objects |
|
439 |
ptr = (byte*)object + (d[0] & 0xF) + (d[1] << 4); |
|
440 |
||
441 |
cmd = d[0] >> 4; |
|
442 |
||
443 |
if (cmd < 8) { |
|
444 |
conv = d[2]; |
|
445 |
d += 3; |
|
446 |
||
447 |
if (cmd&4) { |
|
448 |
d += 2; |
|
449 |
// check if the field is of the right version |
|
450 |
if (_sl.version < d[-2] || _sl.version > d[-1]) { |
|
451 |
if ((cmd & 3) == 2) d++; |
|
452 |
continue; |
|
453 |
} |
|
454 |
} |
|
455 |
||
456 |
switch(cmd&3) { |
|
457 |
// Normal variable |
|
458 |
case 0: SlSaveLoadConv(ptr, conv); break; |
|
459 |
// Reference |
|
460 |
case 1: |
|
461 |
if (_sl.save) { |
|
462 |
SlWriteUint16(_sl.ref_to_int_proc(*(void**)ptr, conv)); |
|
463 |
} else { |
|
464 |
*(void**)ptr = _sl.int_to_ref_proc(SlReadUint16(), conv); |
|
465 |
} |
|
466 |
break; |
|
467 |
// Array |
|
468 |
case 2: SlArray(ptr, *d++, conv); break; |
|
469 |
default:NOT_REACHED(); |
|
470 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
471 |
|
0 | 472 |
// Write byte. |
473 |
} else if (cmd == 8) { |
|
474 |
if (_sl.save) { |
|
475 |
SlWriteByte(d[3]); |
|
476 |
} else { |
|
477 |
*(byte*)ptr = d[2]; |
|
478 |
} |
|
479 |
d += 4; |
|
480 |
||
481 |
// Include |
|
482 |
} else if (cmd == 9) { |
|
483 |
SlObject(ptr, _sl.includes[d[2]]); |
|
484 |
d += 3; |
|
485 |
} else if (cmd == 15) |
|
486 |
break; |
|
487 |
else |
|
488 |
assert(0); |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
||
493 |
static size_t SlCalcGlobListLength(const SaveLoadGlobVarList *desc) |
|
494 |
{ |
|
495 |
size_t length = 0; |
|
496 |
||
497 |
while (desc->address) { |
|
617
279ed033e2d4
(svn r1041) Under certain circumstances obsolete fields are included in the size calculation of a chunk in a savegame which leads to failed loads. This wasn't triggered because the necessary conditions weren't met.
tron
parents:
536
diff
changeset
|
498 |
if(_sl.version >= desc->from_version && _sl.version <= desc->to_version) |
279ed033e2d4
(svn r1041) Under certain circumstances obsolete fields are included in the size calculation of a chunk in a savegame which leads to failed loads. This wasn't triggered because the necessary conditions weren't met.
tron
parents:
536
diff
changeset
|
499 |
length += SlCalcConvLen(desc->conv, NULL); |
0 | 500 |
desc++; |
501 |
} |
|
502 |
return length; |
|
503 |
} |
|
504 |
||
505 |
// Save/Load a list of global variables |
|
506 |
void SlGlobList(const SaveLoadGlobVarList *desc) |
|
507 |
{ |
|
508 |
if (_sl.need_length != NL_NONE) { |
|
509 |
SlSetLength(SlCalcGlobListLength(desc)); |
|
510 |
if (_sl.need_length == NL_CALCLENGTH) |
|
511 |
return; |
|
512 |
} |
|
513 |
while (true) { |
|
514 |
void *ptr = desc->address; |
|
515 |
if (ptr == NULL) |
|
516 |
break; |
|
517 |
if(_sl.version >= desc->from_version && _sl.version <= desc->to_version) |
|
518 |
SlSaveLoadConv(ptr, desc->conv); |
|
519 |
desc++; |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
||
524 |
void SlAutolength(AutolengthProc *proc, void *arg) |
|
525 |
{ |
|
526 |
uint32 offs; |
|
527 |
||
528 |
assert(_sl.save); |
|
529 |
||
530 |
// Tell it to calculate the length |
|
531 |
_sl.need_length = NL_CALCLENGTH; |
|
532 |
_sl.obj_len = 0; |
|
533 |
proc(arg); |
|
534 |
||
535 |
// Setup length |
|
536 |
_sl.need_length = NL_WANTLENGTH; |
|
537 |
SlSetLength(_sl.obj_len); |
|
538 |
||
539 |
offs = SlGetOffs() + _sl.obj_len; |
|
540 |
||
541 |
// And write the stuff |
|
542 |
proc(arg); |
|
543 |
||
544 |
assert(offs == SlGetOffs()); |
|
545 |
} |
|
546 |
||
547 |
static void SlLoadChunk(const ChunkHandler *ch) |
|
548 |
{ |
|
549 |
byte m = SlReadByte(); |
|
550 |
size_t len; |
|
551 |
uint32 endoffs; |
|
552 |
||
553 |
_sl.block_mode = m; |
|
554 |
_sl.obj_len = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
555 |
|
0 | 556 |
switch(m) { |
557 |
case CH_ARRAY: |
|
558 |
_sl.array_index = 0; |
|
559 |
ch->load_proc(); |
|
560 |
break; |
|
561 |
||
562 |
case CH_SPARSE_ARRAY: |
|
563 |
ch->load_proc(); |
|
564 |
break; |
|
565 |
||
566 |
case CH_RIFF: |
|
567 |
// Read length |
|
568 |
len = SlReadByte() << 16; |
|
569 |
len += SlReadUint16(); |
|
570 |
_sl.obj_len = len; |
|
571 |
endoffs = SlGetOffs() + len; |
|
572 |
ch->load_proc(); |
|
573 |
assert(SlGetOffs() == endoffs); |
|
574 |
break; |
|
575 |
default: |
|
576 |
assert(0); |
|
577 |
} |
|
578 |
} |
|
579 |
||
580 |
static ChunkSaveLoadProc *_tmp_proc_1; |
|
581 |
||
582 |
static void SlStubSaveProc2(void *arg) |
|
583 |
{ |
|
584 |
_tmp_proc_1(); |
|
585 |
} |
|
586 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
587 |
static void SlStubSaveProc(void) |
0 | 588 |
{ |
589 |
SlAutolength(SlStubSaveProc2, NULL); |
|
590 |
} |
|
591 |
||
592 |
static void SlSaveChunk(const ChunkHandler *ch) |
|
593 |
{ |
|
594 |
ChunkSaveLoadProc *proc; |
|
595 |
||
596 |
SlWriteUint32(ch->id); |
|
597 |
||
598 |
proc = ch->save_proc; |
|
599 |
if (ch->flags & CH_AUTO_LENGTH) { |
|
600 |
// Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. |
|
601 |
_tmp_proc_1 = proc; |
|
602 |
proc = SlStubSaveProc; |
|
603 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
604 |
|
0 | 605 |
_sl.block_mode = ch->flags & CH_TYPE_MASK; |
606 |
switch(ch->flags & CH_TYPE_MASK) { |
|
607 |
case CH_RIFF: |
|
608 |
_sl.need_length = NL_WANTLENGTH; |
|
609 |
proc(); |
|
610 |
break; |
|
611 |
case CH_ARRAY: |
|
612 |
_sl.last_array_index = 0; |
|
613 |
SlWriteByte(CH_ARRAY); |
|
614 |
proc(); |
|
615 |
SlWriteArrayLength(0); // Terminate arrays |
|
616 |
break; |
|
617 |
case CH_SPARSE_ARRAY: |
|
618 |
SlWriteByte(CH_SPARSE_ARRAY); |
|
619 |
proc(); |
|
620 |
SlWriteArrayLength(0); // Terminate arrays |
|
621 |
break; |
|
622 |
default: |
|
623 |
NOT_REACHED(); |
|
624 |
} |
|
625 |
} |
|
626 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
627 |
static void SlSaveChunks(void) |
0 | 628 |
{ |
629 |
const ChunkHandler *ch; |
|
630 |
const ChunkHandler * const * chsc; |
|
631 |
uint p; |
|
632 |
||
633 |
for(p=0; p!=CH_NUM_PRI_LEVELS; p++) { |
|
634 |
for(chsc=_sl.chs;(ch=*chsc++) != NULL;) { |
|
635 |
while(true) { |
|
636 |
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
|
637 |
SlSaveChunk(ch); |
0 | 638 |
if (ch->flags & CH_LAST) |
639 |
break; |
|
640 |
ch++; |
|
641 |
} |
|
642 |
} |
|
643 |
} |
|
644 |
||
645 |
// Terminator |
|
646 |
SlWriteUint32(0); |
|
647 |
} |
|
648 |
||
649 |
static const ChunkHandler *SlFindChunkHandler(uint32 id) |
|
650 |
{ |
|
651 |
const ChunkHandler *ch; |
|
652 |
const ChunkHandler * const * chsc; |
|
653 |
for(chsc=_sl.chs;(ch=*chsc++) != NULL;) { |
|
654 |
while(true) { |
|
655 |
if (ch->id == id) |
|
656 |
return ch; |
|
657 |
if (ch->flags & CH_LAST) |
|
658 |
break; |
|
659 |
ch++; |
|
660 |
} |
|
661 |
} |
|
662 |
return NULL; |
|
663 |
} |
|
664 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
665 |
static void SlLoadChunks(void) |
0 | 666 |
{ |
667 |
uint32 id; |
|
668 |
const ChunkHandler *ch; |
|
669 |
||
670 |
while(true) { |
|
671 |
id = SlReadUint32(); |
|
672 |
if (id == 0) |
|
673 |
return; |
|
674 |
#if 0 |
|
675 |
printf("Loading chunk %c%c%c%c\n", id >> 24, id>>16, id>>8,id); |
|
676 |
#endif |
|
677 |
ch = SlFindChunkHandler(id); |
|
678 |
if (ch == NULL) SlError("found unknown tag in savegame (sync error)"); |
|
679 |
SlLoadChunk(ch); |
|
680 |
} |
|
681 |
} |
|
682 |
||
683 |
//******************************************* |
|
684 |
//********** START OF LZO CODE ************** |
|
685 |
//******************************************* |
|
686 |
#define LZO_SIZE 8192 |
|
687 |
||
781
4c9177888196
(svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents:
762
diff
changeset
|
688 |
#include "minilzo.h" |
0 | 689 |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
690 |
static uint ReadLZO(void) |
0 | 691 |
{ |
692 |
byte out[LZO_SIZE + LZO_SIZE / 64 + 16 + 3 + 8]; |
|
693 |
uint32 tmp[2]; |
|
694 |
uint32 size; |
|
695 |
uint len; |
|
696 |
||
697 |
// Read header |
|
698 |
if (fread(tmp, sizeof(tmp), 1, _sl.fh) != 1) SlError("file read failed"); |
|
699 |
||
700 |
// Check if size is bad |
|
701 |
((uint32*)out)[0] = size = tmp[1]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
702 |
|
0 | 703 |
if (_sl.version != 0) { |
704 |
tmp[0] = TO_BE32(tmp[0]); |
|
705 |
size = TO_BE32(size); |
|
706 |
} |
|
707 |
||
708 |
if (size >= sizeof(out)) SlError("inconsistent size"); |
|
709 |
||
710 |
// Read block |
|
711 |
if (fread(out + sizeof(uint32), size, 1, _sl.fh) != 1) SlError("file read failed"); |
|
712 |
||
713 |
// Verify checksum |
|
714 |
if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlError("bad checksum"); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
715 |
|
0 | 716 |
// Decompress |
717 |
lzo1x_decompress(out + sizeof(uint32)*1, size, _sl.buf, &len, NULL); |
|
718 |
return len; |
|
719 |
} |
|
720 |
||
721 |
// p contains the pointer to the buffer, len contains the pointer to the length. |
|
722 |
// len bytes will be written, p and l will be updated to reflect the next buffer. |
|
723 |
static void WriteLZO(uint size) |
|
724 |
{ |
|
725 |
byte out[LZO_SIZE + LZO_SIZE / 64 + 16 + 3 + 8]; |
|
726 |
byte wrkmem[sizeof(byte*)*4096]; |
|
727 |
uint outlen; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
728 |
|
0 | 729 |
lzo1x_1_compress(_sl.buf, size, out + sizeof(uint32)*2, &outlen, wrkmem); |
730 |
((uint32*)out)[1] = TO_BE32(outlen); |
|
731 |
((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32))); |
|
732 |
if (fwrite(out, outlen + sizeof(uint32)*2, 1, _sl.fh) != 1) SlError("file write failed"); |
|
733 |
} |
|
734 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
735 |
static bool InitLZO(void) |
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
736 |
{ |
0 | 737 |
_sl.bufsize = LZO_SIZE; |
738 |
_sl.buf = (byte*)malloc(LZO_SIZE); |
|
739 |
return true; |
|
740 |
} |
|
741 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
742 |
static void UninitLZO(void) |
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
743 |
{ |
0 | 744 |
free(_sl.buf); |
745 |
} |
|
746 |
||
747 |
//******************************************* |
|
748 |
//******** START OF NOCOMP CODE ************* |
|
749 |
//******************************************* |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
750 |
static uint ReadNoComp(void) |
0 | 751 |
{ |
752 |
return fread(_sl.buf, 1, LZO_SIZE, _sl.fh); |
|
753 |
} |
|
754 |
||
755 |
static void WriteNoComp(uint size) |
|
756 |
{ |
|
757 |
fwrite(_sl.buf, 1, size, _sl.fh); |
|
758 |
} |
|
759 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
760 |
static bool InitNoComp(void) |
0 | 761 |
{ |
762 |
_sl.bufsize = LZO_SIZE; |
|
763 |
_sl.buf = (byte*)malloc(LZO_SIZE); |
|
764 |
return true; |
|
765 |
} |
|
766 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
767 |
static void UninitNoComp(void) |
0 | 768 |
{ |
769 |
free(_sl.buf); |
|
770 |
} |
|
771 |
||
772 |
//******************************************** |
|
773 |
//********** START OF ZLIB CODE ************** |
|
774 |
//******************************************** |
|
775 |
||
776 |
#if defined(WITH_ZLIB) |
|
777 |
#include <zlib.h> |
|
778 |
static z_stream _z; |
|
779 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
780 |
static bool InitReadZlib(void) |
0 | 781 |
{ |
782 |
memset(&_z, 0, sizeof(_z)); |
|
783 |
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
|
784 |
|
0 | 785 |
_sl.bufsize = 4096; |
786 |
_sl.buf = (byte*)malloc(4096 + 4096); // also contains fread buffer |
|
787 |
return true; |
|
788 |
} |
|
789 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
790 |
static uint ReadZlib(void) |
0 | 791 |
{ |
792 |
int r; |
|
793 |
||
794 |
_z.next_out = _sl.buf; |
|
795 |
_z.avail_out = 4096; |
|
796 |
||
797 |
do { |
|
798 |
// read more bytes from the file? |
|
799 |
if (_z.avail_in == 0) { |
|
800 |
_z.avail_in = fread(_z.next_in = _sl.buf + 4096, 1, 4096, _sl.fh); |
|
801 |
} |
|
802 |
||
803 |
// inflate the data |
|
804 |
r = inflate(&_z, 0); |
|
805 |
if (r == Z_STREAM_END) |
|
806 |
break; |
|
807 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
808 |
if (r != Z_OK) |
0 | 809 |
SlError("inflate() failed"); |
810 |
} while (_z.avail_out); |
|
811 |
||
812 |
return 4096 - _z.avail_out; |
|
813 |
} |
|
814 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
815 |
static void UninitReadZlib(void) |
0 | 816 |
{ |
817 |
inflateEnd(&_z); |
|
818 |
free(_sl.buf); |
|
819 |
} |
|
820 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
821 |
static bool InitWriteZlib(void) |
0 | 822 |
{ |
823 |
memset(&_z, 0, sizeof(_z)); |
|
824 |
if (deflateInit(&_z, 6) != Z_OK) return false; |
|
825 |
||
826 |
_sl.bufsize = 4096; |
|
827 |
_sl.buf = (byte*)malloc(4096); // also contains fread buffer |
|
828 |
return true; |
|
829 |
} |
|
830 |
||
831 |
static void WriteZlibLoop(z_streamp z, byte *p, uint len, int mode) |
|
832 |
{ |
|
833 |
char buf[1024]; // output buffer |
|
834 |
int r; |
|
835 |
uint n; |
|
836 |
z->next_in = p; |
|
837 |
z->avail_in = len; |
|
838 |
do { |
|
839 |
z->next_out = buf; |
|
840 |
z->avail_out = sizeof(buf); |
|
841 |
r = deflate(z, mode); |
|
842 |
// bytes were emitted? |
|
843 |
if ((n=sizeof(buf) - z->avail_out) != 0) { |
|
844 |
if (fwrite(buf, n, 1, _sl.fh) != 1) SlError("file write error"); |
|
845 |
} |
|
846 |
if (r == Z_STREAM_END) |
|
847 |
break; |
|
848 |
if (r != Z_OK) SlError("zlib returned error code"); |
|
849 |
} while (z->avail_in || !z->avail_out); |
|
850 |
} |
|
851 |
||
852 |
static void WriteZlib(uint len) |
|
853 |
{ |
|
854 |
WriteZlibLoop(&_z, _sl.buf, len, 0); |
|
855 |
} |
|
856 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
857 |
static void UninitWriteZlib(void) |
0 | 858 |
{ |
859 |
// flush any pending output. |
|
860 |
if (_sl.fh) WriteZlibLoop(&_z, NULL, 0, Z_FINISH); |
|
861 |
deflateEnd(&_z); |
|
862 |
free(_sl.buf); |
|
863 |
} |
|
864 |
||
865 |
#endif //WITH_ZLIB |
|
866 |
||
867 |
//******************************************* |
|
868 |
//************* END OF CODE ***************** |
|
869 |
//******************************************* |
|
870 |
||
871 |
// these define the chunks |
|
872 |
extern const ChunkHandler _misc_chunk_handlers[]; |
|
873 |
extern const ChunkHandler _player_chunk_handlers[]; |
|
874 |
extern const ChunkHandler _veh_chunk_handlers[]; |
|
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
875 |
extern const ChunkHandler _depot_chunk_handlers[]; |
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
950
diff
changeset
|
876 |
extern const ChunkHandler _order_chunk_handlers[]; |
0 | 877 |
extern const ChunkHandler _town_chunk_handlers[]; |
878 |
extern const ChunkHandler _sign_chunk_handlers[]; |
|
879 |
extern const ChunkHandler _station_chunk_handlers[]; |
|
880 |
extern const ChunkHandler _industry_chunk_handlers[]; |
|
881 |
extern const ChunkHandler _engine_chunk_handlers[]; |
|
882 |
extern const ChunkHandler _economy_chunk_handlers[]; |
|
883 |
extern const ChunkHandler _animated_tile_chunk_handlers[]; |
|
884 |
||
885 |
static const ChunkHandler * const _chunk_handlers[] = { |
|
886 |
_misc_chunk_handlers, |
|
887 |
_veh_chunk_handlers, |
|
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
888 |
_depot_chunk_handlers, |
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
950
diff
changeset
|
889 |
_order_chunk_handlers, |
0 | 890 |
_industry_chunk_handlers, |
891 |
_economy_chunk_handlers, |
|
892 |
_engine_chunk_handlers, |
|
893 |
_town_chunk_handlers, |
|
894 |
_sign_chunk_handlers, |
|
895 |
_station_chunk_handlers, |
|
896 |
_player_chunk_handlers, |
|
897 |
_animated_tile_chunk_handlers, |
|
898 |
NULL, |
|
899 |
}; |
|
900 |
||
901 |
// used to include a vehicle desc in another desc. |
|
902 |
extern const byte _common_veh_desc[]; |
|
903 |
static const byte * const _desc_includes[] = { |
|
904 |
_common_veh_desc |
|
905 |
}; |
|
906 |
||
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
907 |
/* We can't save pointers to a savegame, so this functions get's |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
908 |
the index of the item, and if not available, it hussles with |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
909 |
pointers (looks really bad :() |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
910 |
Remember that a NULL item has value 0, and all |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
911 |
indexes have + 1, so vehicle 0 is saved as index 1. */ |
410 | 912 |
static uint ReferenceToInt(void *v, uint t) |
0 | 913 |
{ |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
914 |
if (v == NULL) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
915 |
return 0; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
916 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
917 |
switch (t) { |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
918 |
case REF_VEHICLE_OLD: // Old vehicles we save as new onces |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
919 |
case REF_VEHICLE: return ((Vehicle *)v)->index + 1; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
920 |
case REF_STATION: return ((Station *)v)->index + 1; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
921 |
case REF_TOWN: return ((Town *)v)->index + 1; |
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
950
diff
changeset
|
922 |
case REF_ORDER: return ((Order *)v)->index + 1; |
1284
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
923 |
case REF_ROADSTOPS: return ((RoadStop *)v)->index + 1; |
1217
59c024cfaf54
(svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents:
1171
diff
changeset
|
924 |
|
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
925 |
default: |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
926 |
NOT_REACHED(); |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
927 |
} |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
928 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
929 |
return 0; |
0 | 930 |
} |
931 |
||
1095 | 932 |
static void *IntToReference(uint r, uint t) |
0 | 933 |
{ |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
934 |
/* From version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE, and should be loaded |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
935 |
like that */ |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
936 |
if (t == REF_VEHICLE_OLD && |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
937 |
_sl.full_version >= 0x404) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
938 |
t = REF_VEHICLE; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
939 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
940 |
if (t != REF_VEHICLE_OLD && r == 0) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
941 |
return NULL; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
942 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
943 |
switch (t) { |
1314
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
944 |
case REF_ORDER: { |
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
945 |
if (!AddBlockIfNeeded(&_order_pool, r - 1)) |
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
946 |
error("Orders: failed loading savegame: too many orders"); |
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
947 |
return GetOrder(r - 1); |
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
948 |
} |
1279
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
949 |
case REF_VEHICLE: { |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
950 |
if (!AddBlockIfNeeded(&_vehicle_pool, r - 1)) |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
951 |
error("Vehicles: failed loading savegame: too many vehicles"); |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
952 |
return GetVehicle(r - 1); |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
953 |
} |
1272
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
954 |
case REF_STATION: { |
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
955 |
if (!AddBlockIfNeeded(&_station_pool, r - 1)) |
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
956 |
error("Stations: failed loading savegame: too many stations"); |
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
957 |
return GetStation(r - 1); |
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
958 |
} |
1260
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
959 |
case REF_TOWN: { |
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
960 |
if (!AddBlockIfNeeded(&_town_pool, r - 1)) |
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
961 |
error("Towns: failed loading savegame: too many towns"); |
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
962 |
return GetTown(r - 1); |
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
963 |
} |
1284
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
964 |
case REF_ROADSTOPS: { |
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
965 |
if (!AddBlockIfNeeded(&_roadstop_pool, r - 1)) |
1314
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
966 |
error("RoadStops: failed loading savegame: too many RoadStops"); |
1284
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
967 |
return GetRoadStop(r - 1); |
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
968 |
} |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
969 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
970 |
case REF_VEHICLE_OLD: { |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
971 |
/* Old vehicles were saved differently: invalid vehicle was 0xFFFF, |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
972 |
and the index was not - 1.. correct for this */ |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
973 |
if (r == INVALID_VEHICLE) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
974 |
return NULL; |
1279
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
975 |
|
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
976 |
if (!AddBlockIfNeeded(&_vehicle_pool, r)) |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
977 |
error("Vehicles: failed loading savegame: too many vehicles"); |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
978 |
return GetVehicle(r); |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
979 |
} |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
980 |
default: |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
981 |
NOT_REACHED(); |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
982 |
} |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
983 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
984 |
return NULL; |
0 | 985 |
} |
986 |
||
987 |
typedef struct { |
|
988 |
const char *name; |
|
989 |
uint32 tag; |
|
990 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
991 |
bool (*init_read)(void); |
0 | 992 |
ReaderProc *reader; |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
993 |
void (*uninit_read)(void); |
0 | 994 |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
995 |
bool (*init_write)(void); |
0 | 996 |
WriterProc *writer; |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
997 |
void (*uninit_write)(void); |
0 | 998 |
|
999 |
} SaveLoadFormat; |
|
1000 |
||
1001 |
static const SaveLoadFormat _saveload_formats[] = { |
|
1002 |
{"lzo", TO_BE32X('OTTD'), InitLZO,ReadLZO, UninitLZO, InitLZO, WriteLZO, UninitLZO}, |
|
1003 |
{"none", TO_BE32X('OTTN'), InitNoComp,ReadNoComp, UninitNoComp, InitNoComp, WriteNoComp, UninitNoComp}, |
|
1004 |
#if defined(WITH_ZLIB) |
|
1005 |
{"zlib", TO_BE32X('OTTZ'), InitReadZlib,ReadZlib, UninitReadZlib, InitWriteZlib, WriteZlib, UninitWriteZlib}, |
|
1006 |
#else |
|
1007 |
{"zlib", TO_BE32X('OTTZ'), NULL,NULL,NULL,NULL,NULL,NULL} |
|
1008 |
#endif |
|
1009 |
}; |
|
1010 |
||
1011 |
static const SaveLoadFormat *GetSavegameFormat(const char *s) |
|
1012 |
{ |
|
1013 |
const SaveLoadFormat *def; |
|
1014 |
int i; |
|
1015 |
||
1016 |
// find default savegame format |
|
1017 |
def = endof(_saveload_formats) - 1; |
|
1018 |
while (!def->init_write) def--; |
|
1019 |
||
1020 |
if (_savegame_format[0]) { |
|
1021 |
for(i = 0; i!=lengthof(_saveload_formats); i++) |
|
1022 |
if (_saveload_formats[i].init_write && !strcmp(s, _saveload_formats[i].name)) |
|
1023 |
return _saveload_formats + i; |
|
1024 |
||
1025 |
ShowInfoF("Savegame format '%s' is not available. Reverting to '%s'.", s, def->name); |
|
1026 |
} |
|
1027 |
return def; |
|
1028 |
} |
|
1029 |
||
1030 |
// actual loader/saver function |
|
1218 | 1031 |
void InitializeGame(uint log_x, uint log_y); |
0 | 1032 |
extern bool AfterLoadGame(uint version); |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
1033 |
extern void BeforeSaveGame(void); |
0 | 1034 |
extern bool LoadOldSaveGame(const char *file); |
1035 |
||
1036 |
// Save or Load files SL_LOAD, SL_SAVE, SL_OLD_LOAD |
|
1037 |
int SaveOrLoad(const char *filename, int mode) |
|
1038 |
{ |
|
1039 |
uint32 hdr[2]; |
|
1040 |
const SaveLoadFormat *fmt; |
|
1041 |
uint version; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1042 |
|
0 | 1043 |
// old style load |
1044 |
if (mode == SL_OLD_LOAD) { |
|
1218 | 1045 |
InitializeGame(8, 8); |
0 | 1046 |
if (!LoadOldSaveGame(filename)) return SL_REINIT; |
1047 |
AfterLoadGame(0); |
|
1048 |
return SL_OK; |
|
1049 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1050 |
|
0 | 1051 |
_sl.fh = fopen(filename, mode?"wb":"rb"); |
1052 |
if (_sl.fh == NULL) |
|
1053 |
return SL_ERROR; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1054 |
|
0 | 1055 |
_sl.bufe = _sl.bufp = NULL; |
1056 |
_sl.offs_base = 0; |
|
1057 |
_sl.int_to_ref_proc = IntToReference; |
|
1058 |
_sl.ref_to_int_proc = ReferenceToInt; |
|
1059 |
_sl.save = mode; |
|
1060 |
_sl.includes = _desc_includes; |
|
1061 |
_sl.chs = _chunk_handlers; |
|
1062 |
||
1063 |
// setup setjmp error handler |
|
1064 |
if (setjmp(_sl.excpt)) { |
|
1065 |
// close file handle. |
|
1066 |
fclose(_sl.fh); _sl.fh = NULL; |
|
1067 |
||
1068 |
// deinitialize compressor. |
|
1069 |
_sl.excpt_uninit(); |
|
1070 |
||
1071 |
// a saver/loader exception!! |
|
1072 |
// reinitialize all variables to prevent crash! |
|
1073 |
if (mode == SL_LOAD) { |
|
1074 |
ShowInfoF("Load game failed: %s.", _sl.excpt_msg); |
|
1075 |
return SL_REINIT; |
|
1076 |
} else { |
|
1077 |
ShowInfoF("Save game failed: %s.", _sl.excpt_msg); |
|
1078 |
return SL_ERROR; |
|
1079 |
} |
|
1080 |
} |
|
1081 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1082 |
// we first initialize here to avoid: "warning: variable `version' might |
0 | 1083 |
// be clobbered by `longjmp' or `vfork'" |
1084 |
version = 0; |
|
1085 |
||
1086 |
if (mode != SL_LOAD) { |
|
1087 |
fmt = GetSavegameFormat(_savegame_format); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1088 |
|
0 | 1089 |
_sl.write_bytes = fmt->writer; |
1090 |
_sl.excpt_uninit = fmt->uninit_write; |
|
1091 |
if (!fmt->init_write()) goto init_err; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1092 |
|
0 | 1093 |
hdr[0] = fmt->tag; |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1094 |
hdr[1] = TO_BE32((SAVEGAME_MAJOR_VERSION << 16) + (SAVEGAME_MINOR_VERSION << 8)); |
0 | 1095 |
if (fwrite(hdr, sizeof(hdr), 1, _sl.fh) != 1) SlError("file write failed"); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1096 |
|
0 | 1097 |
_sl.version = SAVEGAME_MAJOR_VERSION; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1098 |
|
0 | 1099 |
BeforeSaveGame(); |
1100 |
SlSaveChunks(); |
|
1101 |
SlWriteFill(); // flush the save buffer |
|
1102 |
fmt->uninit_write(); |
|
1103 |
||
1104 |
} else { |
|
1105 |
if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) { |
|
1106 |
read_err: |
|
1107 |
printf("Savegame is obsolete or invalid format.\n"); |
|
1108 |
init_err: |
|
1109 |
fclose(_sl.fh); |
|
1110 |
return SL_ERROR; |
|
1111 |
} |
|
1112 |
||
1113 |
// see if we have any loader for this type. |
|
1114 |
for(fmt = _saveload_formats; ; fmt++) { |
|
1115 |
if (fmt == endof(_saveload_formats)) { |
|
1116 |
printf("Unknown savegame type, trying to load it as the buggy format.\n"); |
|
1117 |
rewind(_sl.fh); |
|
1118 |
_sl.version = 0; |
|
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1119 |
_sl.full_version = 0; |
0 | 1120 |
version = 0; |
1121 |
fmt = _saveload_formats + 0; // lzo |
|
1122 |
break; |
|
1123 |
} |
|
1124 |
if (fmt->tag == hdr[0]) { |
|
1125 |
// check version number |
|
1126 |
version = TO_BE32(hdr[1]) >> 8; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1127 |
|
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1128 |
/* Is the version higher than the current? */ |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1129 |
if (version > SAVEGAME_LOADABLE_VERSION) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1130 |
goto read_err; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1131 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1132 |
_sl.version = (version >> 8); |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1133 |
_sl.full_version = version; |
0 | 1134 |
break; |
1135 |
} |
|
1136 |
} |
|
1137 |
||
1138 |
_sl.read_bytes = fmt->reader; |
|
1139 |
_sl.excpt_uninit = fmt->uninit_read; |
|
1140 |
||
1141 |
// loader for this savegame type is not implemented? |
|
1142 |
if (fmt->init_read == NULL) { |
|
1143 |
ShowInfoF("Loader for '%s' is not available.", fmt->name); |
|
1144 |
fclose(_sl.fh); |
|
1145 |
return SL_ERROR; |
|
1146 |
} |
|
1147 |
||
1148 |
if (!fmt->init_read()) goto init_err; |
|
1149 |
// Clear everything |
|
1218 | 1150 |
/* Set the current map to 256x256, in case of an old map. |
1151 |
* Else MAPS will read the right information */ |
|
1152 |
InitializeGame(8, 8); |
|
1153 |
||
0 | 1154 |
SlLoadChunks(); |
1155 |
fmt->uninit_read(); |
|
1156 |
} |
|
1157 |
||
1158 |
fclose(_sl.fh); |
|
1159 |
||
1160 |
if (mode == SL_LOAD) { |
|
1161 |
if (!AfterLoadGame(version)) |
|
1162 |
return SL_REINIT; |
|
1163 |
} |
|
1164 |
||
1165 |
return SL_OK; |
|
1166 |
} |
|
1167 |
||
1094
9a01482df45a
(svn r1595) EmergencySave() is only used on WIN32, add preprocessor guard to hide it everywhere else
tron
parents:
1093
diff
changeset
|
1168 |
#ifdef WIN32 |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
1169 |
bool EmergencySave(void) |
0 | 1170 |
{ |
1171 |
SaveOrLoad("crash.sav", SL_SAVE); |
|
1172 |
return true; |
|
1173 |
} |
|
1094
9a01482df45a
(svn r1595) EmergencySave() is only used on WIN32, add preprocessor guard to hide it everywhere else
tron
parents:
1093
diff
changeset
|
1174 |
#endif |
0 | 1175 |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
1176 |
void DoExitSave(void) |
643
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1177 |
{ |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1178 |
char buf[200]; |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1179 |
sprintf(buf, "%s%sexit.sav", _path.autosave_dir, PATHSEP); |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1180 |
SaveOrLoad(buf, SL_SAVE); |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1181 |
} |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1182 |
|
0 | 1183 |
// not used right now, but could be used if extensions of savegames are garbled |
1184 |
/*int GetSavegameType(char *file) |
|
1185 |
{ |
|
1186 |
const SaveLoadFormat *fmt; |
|
1187 |
uint32 hdr; |
|
1188 |
FILE *f; |
|
1189 |
int mode = SL_OLD_LOAD; |
|
1190 |
||
1191 |
f = fopen(file, "rb"); |
|
1192 |
if (fread(&hdr, sizeof(hdr), 1, f) != 1) { |
|
1193 |
printf("Savegame is obsolete or invalid format.\n"); |
|
1194 |
mode = SL_LOAD; // don't try to get filename, just show name as it is written |
|
1195 |
} |
|
1196 |
else { |
|
1197 |
// see if we have any loader for this type. |
|
1198 |
for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) { |
|
1199 |
if (fmt->tag == hdr) { |
|
1200 |
mode = SL_LOAD; // new type of savegame |
|
1201 |
break; |
|
1202 |
} |
|
1203 |
} |
|
1204 |
} |
|
1205 |
||
1206 |
fclose(f); |
|
1207 |
return mode; |
|
1208 |
}*/ |