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