author | darkvater |
Sun, 12 Sep 2004 21:49:38 +0000 | |
changeset 222 | b88456001397 |
parent 193 | 0a7025304867 |
child 410 | 8de2aaf20800 |
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 |
||
116 |
int SlReadUint16() |
|
117 |
{ |
|
118 |
int x = SlReadByte() << 8; |
|
119 |
return x | SlReadByte(); |
|
120 |
} |
|
121 |
||
122 |
uint32 SlReadUint32() |
|
123 |
{ |
|
124 |
uint32 x = SlReadUint16() << 16; |
|
125 |
return x | SlReadUint16(); |
|
126 |
} |
|
127 |
||
128 |
uint64 SlReadUint64() |
|
129 |
{ |
|
130 |
uint32 x = SlReadUint32(); |
|
131 |
uint32 y = SlReadUint32(); |
|
132 |
return (uint64)x << 32 | y; |
|
133 |
} |
|
134 |
||
135 |
void SlWriteUint16(uint16 v) |
|
136 |
{ |
|
137 |
SlWriteByte((byte)(v >> 8)); |
|
138 |
SlWriteByte((byte)v); |
|
139 |
} |
|
140 |
||
141 |
void SlWriteUint32(uint32 v) |
|
142 |
{ |
|
143 |
SlWriteUint16((uint16)(v >> 16)); |
|
144 |
SlWriteUint16((uint16)v); |
|
145 |
} |
|
146 |
||
147 |
void SlWriteUint64(uint64 x) |
|
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 |
||
176 |
int INLINE SlReadSparseIndex() |
|
177 |
{ |
|
178 |
return SlReadSimpleGamma(); |
|
179 |
} |
|
180 |
||
181 |
void INLINE SlWriteSparseIndex(uint index) |
|
182 |
{ |
|
183 |
SlWriteSimpleGamma(index); |
|
184 |
} |
|
185 |
||
186 |
int INLINE SlReadArrayLength() |
|
187 |
{ |
|
188 |
return SlReadSimpleGamma(); |
|
189 |
} |
|
190 |
||
191 |
void INLINE SlWriteArrayLength(uint length) |
|
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 |
||
263 |
void SlCopyBytes(void *ptr, size_t length) |
|
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) { |
|
539 |
length += SlCalcConvLen(desc->conv, NULL); |
|
540 |
desc++; |
|
541 |
} |
|
542 |
return length; |
|
543 |
} |
|
544 |
||
545 |
// Save/Load a list of global variables |
|
546 |
void SlGlobList(const SaveLoadGlobVarList *desc) |
|
547 |
{ |
|
548 |
if (_sl.need_length != NL_NONE) { |
|
549 |
SlSetLength(SlCalcGlobListLength(desc)); |
|
550 |
if (_sl.need_length == NL_CALCLENGTH) |
|
551 |
return; |
|
552 |
} |
|
553 |
while (true) { |
|
554 |
void *ptr = desc->address; |
|
555 |
if (ptr == NULL) |
|
556 |
break; |
|
557 |
if(_sl.version >= desc->from_version && _sl.version <= desc->to_version) |
|
558 |
SlSaveLoadConv(ptr, desc->conv); |
|
559 |
desc++; |
|
560 |
} |
|
561 |
} |
|
562 |
||
563 |
||
564 |
void SlAutolength(AutolengthProc *proc, void *arg) |
|
565 |
{ |
|
566 |
uint32 offs; |
|
567 |
||
568 |
assert(_sl.save); |
|
569 |
||
570 |
// Tell it to calculate the length |
|
571 |
_sl.need_length = NL_CALCLENGTH; |
|
572 |
_sl.obj_len = 0; |
|
573 |
proc(arg); |
|
574 |
||
575 |
// Setup length |
|
576 |
_sl.need_length = NL_WANTLENGTH; |
|
577 |
SlSetLength(_sl.obj_len); |
|
578 |
||
579 |
offs = SlGetOffs() + _sl.obj_len; |
|
580 |
||
581 |
// And write the stuff |
|
582 |
proc(arg); |
|
583 |
||
584 |
assert(offs == SlGetOffs()); |
|
585 |
} |
|
586 |
||
587 |
static void SlLoadChunk(const ChunkHandler *ch) |
|
588 |
{ |
|
589 |
byte m = SlReadByte(); |
|
590 |
size_t len; |
|
591 |
uint32 endoffs; |
|
592 |
||
593 |
_sl.block_mode = m; |
|
594 |
_sl.obj_len = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
595 |
|
0 | 596 |
switch(m) { |
597 |
case CH_ARRAY: |
|
598 |
_sl.array_index = 0; |
|
599 |
ch->load_proc(); |
|
600 |
break; |
|
601 |
||
602 |
case CH_SPARSE_ARRAY: |
|
603 |
ch->load_proc(); |
|
604 |
break; |
|
605 |
||
606 |
case CH_RIFF: |
|
607 |
// Read length |
|
608 |
len = SlReadByte() << 16; |
|
609 |
len += SlReadUint16(); |
|
610 |
_sl.obj_len = len; |
|
611 |
endoffs = SlGetOffs() + len; |
|
612 |
ch->load_proc(); |
|
613 |
assert(SlGetOffs() == endoffs); |
|
614 |
break; |
|
615 |
default: |
|
616 |
assert(0); |
|
617 |
} |
|
618 |
} |
|
619 |
||
620 |
static ChunkSaveLoadProc *_tmp_proc_1; |
|
621 |
||
622 |
static void SlStubSaveProc2(void *arg) |
|
623 |
{ |
|
624 |
_tmp_proc_1(); |
|
625 |
} |
|
626 |
||
627 |
static void SlStubSaveProc() |
|
628 |
{ |
|
629 |
SlAutolength(SlStubSaveProc2, NULL); |
|
630 |
} |
|
631 |
||
632 |
static void SlSaveChunk(const ChunkHandler *ch) |
|
633 |
{ |
|
634 |
ChunkSaveLoadProc *proc; |
|
635 |
||
636 |
SlWriteUint32(ch->id); |
|
637 |
||
638 |
proc = ch->save_proc; |
|
639 |
if (ch->flags & CH_AUTO_LENGTH) { |
|
640 |
// Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. |
|
641 |
_tmp_proc_1 = proc; |
|
642 |
proc = SlStubSaveProc; |
|
643 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
644 |
|
0 | 645 |
_sl.block_mode = ch->flags & CH_TYPE_MASK; |
646 |
switch(ch->flags & CH_TYPE_MASK) { |
|
647 |
case CH_RIFF: |
|
648 |
_sl.need_length = NL_WANTLENGTH; |
|
649 |
proc(); |
|
650 |
break; |
|
651 |
case CH_ARRAY: |
|
652 |
_sl.last_array_index = 0; |
|
653 |
SlWriteByte(CH_ARRAY); |
|
654 |
proc(); |
|
655 |
SlWriteArrayLength(0); // Terminate arrays |
|
656 |
break; |
|
657 |
case CH_SPARSE_ARRAY: |
|
658 |
SlWriteByte(CH_SPARSE_ARRAY); |
|
659 |
proc(); |
|
660 |
SlWriteArrayLength(0); // Terminate arrays |
|
661 |
break; |
|
662 |
default: |
|
663 |
NOT_REACHED(); |
|
664 |
} |
|
665 |
} |
|
666 |
||
667 |
void SlSaveChunks() |
|
668 |
{ |
|
669 |
const ChunkHandler *ch; |
|
670 |
const ChunkHandler * const * chsc; |
|
671 |
uint p; |
|
672 |
||
673 |
for(p=0; p!=CH_NUM_PRI_LEVELS; p++) { |
|
674 |
for(chsc=_sl.chs;(ch=*chsc++) != NULL;) { |
|
675 |
while(true) { |
|
676 |
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
|
677 |
SlSaveChunk(ch); |
0 | 678 |
if (ch->flags & CH_LAST) |
679 |
break; |
|
680 |
ch++; |
|
681 |
} |
|
682 |
} |
|
683 |
} |
|
684 |
||
685 |
// Terminator |
|
686 |
SlWriteUint32(0); |
|
687 |
} |
|
688 |
||
689 |
static const ChunkHandler *SlFindChunkHandler(uint32 id) |
|
690 |
{ |
|
691 |
const ChunkHandler *ch; |
|
692 |
const ChunkHandler * const * chsc; |
|
693 |
for(chsc=_sl.chs;(ch=*chsc++) != NULL;) { |
|
694 |
while(true) { |
|
695 |
if (ch->id == id) |
|
696 |
return ch; |
|
697 |
if (ch->flags & CH_LAST) |
|
698 |
break; |
|
699 |
ch++; |
|
700 |
} |
|
701 |
} |
|
702 |
return NULL; |
|
703 |
} |
|
704 |
||
705 |
void SlLoadChunks() |
|
706 |
{ |
|
707 |
uint32 id; |
|
708 |
const ChunkHandler *ch; |
|
709 |
||
710 |
while(true) { |
|
711 |
id = SlReadUint32(); |
|
712 |
if (id == 0) |
|
713 |
return; |
|
714 |
#if 0 |
|
715 |
printf("Loading chunk %c%c%c%c\n", id >> 24, id>>16, id>>8,id); |
|
716 |
#endif |
|
717 |
ch = SlFindChunkHandler(id); |
|
718 |
if (ch == NULL) SlError("found unknown tag in savegame (sync error)"); |
|
719 |
SlLoadChunk(ch); |
|
720 |
} |
|
721 |
} |
|
722 |
||
723 |
//******************************************* |
|
724 |
//********** START OF LZO CODE ************** |
|
725 |
//******************************************* |
|
726 |
#define LZO_SIZE 8192 |
|
727 |
||
728 |
int CDECL lzo1x_1_compress( const byte *src, uint src_len,byte *dst, uint *dst_len,void *wrkmem ); |
|
729 |
uint32 CDECL lzo_adler32(uint32 adler, const byte *buf, uint len); |
|
730 |
int CDECL lzo1x_decompress( const byte *src, uint src_len,byte *dst, uint *dst_len,void *wrkmem /* NOT USED */ ); |
|
731 |
||
732 |
static uint ReadLZO() |
|
733 |
{ |
|
734 |
byte out[LZO_SIZE + LZO_SIZE / 64 + 16 + 3 + 8]; |
|
735 |
uint32 tmp[2]; |
|
736 |
uint32 size; |
|
737 |
uint len; |
|
738 |
||
739 |
// Read header |
|
740 |
if (fread(tmp, sizeof(tmp), 1, _sl.fh) != 1) SlError("file read failed"); |
|
741 |
||
742 |
// Check if size is bad |
|
743 |
((uint32*)out)[0] = size = tmp[1]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
744 |
|
0 | 745 |
if (_sl.version != 0) { |
746 |
tmp[0] = TO_BE32(tmp[0]); |
|
747 |
size = TO_BE32(size); |
|
748 |
} |
|
749 |
||
750 |
if (size >= sizeof(out)) SlError("inconsistent size"); |
|
751 |
||
752 |
// Read block |
|
753 |
if (fread(out + sizeof(uint32), size, 1, _sl.fh) != 1) SlError("file read failed"); |
|
754 |
||
755 |
// Verify checksum |
|
756 |
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
|
757 |
|
0 | 758 |
// Decompress |
759 |
lzo1x_decompress(out + sizeof(uint32)*1, size, _sl.buf, &len, NULL); |
|
760 |
return len; |
|
761 |
} |
|
762 |
||
763 |
// p contains the pointer to the buffer, len contains the pointer to the length. |
|
764 |
// len bytes will be written, p and l will be updated to reflect the next buffer. |
|
765 |
static void WriteLZO(uint size) |
|
766 |
{ |
|
767 |
byte out[LZO_SIZE + LZO_SIZE / 64 + 16 + 3 + 8]; |
|
768 |
byte wrkmem[sizeof(byte*)*4096]; |
|
769 |
uint outlen; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
770 |
|
0 | 771 |
lzo1x_1_compress(_sl.buf, size, out + sizeof(uint32)*2, &outlen, wrkmem); |
772 |
((uint32*)out)[1] = TO_BE32(outlen); |
|
773 |
((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32))); |
|
774 |
if (fwrite(out, outlen + sizeof(uint32)*2, 1, _sl.fh) != 1) SlError("file write failed"); |
|
775 |
} |
|
776 |
||
777 |
static bool InitLZO() { |
|
778 |
_sl.bufsize = LZO_SIZE; |
|
779 |
_sl.buf = (byte*)malloc(LZO_SIZE); |
|
780 |
return true; |
|
781 |
} |
|
782 |
||
783 |
static void UninitLZO() { |
|
784 |
free(_sl.buf); |
|
785 |
} |
|
786 |
||
787 |
//******************************************* |
|
788 |
//******** START OF NOCOMP CODE ************* |
|
789 |
//******************************************* |
|
790 |
static uint ReadNoComp() |
|
791 |
{ |
|
792 |
return fread(_sl.buf, 1, LZO_SIZE, _sl.fh); |
|
793 |
} |
|
794 |
||
795 |
static void WriteNoComp(uint size) |
|
796 |
{ |
|
797 |
fwrite(_sl.buf, 1, size, _sl.fh); |
|
798 |
} |
|
799 |
||
800 |
static bool InitNoComp() |
|
801 |
{ |
|
802 |
_sl.bufsize = LZO_SIZE; |
|
803 |
_sl.buf = (byte*)malloc(LZO_SIZE); |
|
804 |
return true; |
|
805 |
} |
|
806 |
||
807 |
static void UninitNoComp() |
|
808 |
{ |
|
809 |
free(_sl.buf); |
|
810 |
} |
|
811 |
||
812 |
//******************************************** |
|
813 |
//********** START OF ZLIB CODE ************** |
|
814 |
//******************************************** |
|
815 |
||
816 |
#if defined(WITH_ZLIB) |
|
817 |
#include <zlib.h> |
|
818 |
static z_stream _z; |
|
819 |
||
820 |
static bool InitReadZlib() |
|
821 |
{ |
|
822 |
memset(&_z, 0, sizeof(_z)); |
|
823 |
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
|
824 |
|
0 | 825 |
_sl.bufsize = 4096; |
826 |
_sl.buf = (byte*)malloc(4096 + 4096); // also contains fread buffer |
|
827 |
return true; |
|
828 |
} |
|
829 |
||
830 |
static uint ReadZlib() |
|
831 |
{ |
|
832 |
int r; |
|
833 |
||
834 |
_z.next_out = _sl.buf; |
|
835 |
_z.avail_out = 4096; |
|
836 |
||
837 |
do { |
|
838 |
// read more bytes from the file? |
|
839 |
if (_z.avail_in == 0) { |
|
840 |
_z.avail_in = fread(_z.next_in = _sl.buf + 4096, 1, 4096, _sl.fh); |
|
841 |
} |
|
842 |
||
843 |
// inflate the data |
|
844 |
r = inflate(&_z, 0); |
|
845 |
if (r == Z_STREAM_END) |
|
846 |
break; |
|
847 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
848 |
if (r != Z_OK) |
0 | 849 |
SlError("inflate() failed"); |
850 |
} while (_z.avail_out); |
|
851 |
||
852 |
return 4096 - _z.avail_out; |
|
853 |
} |
|
854 |
||
855 |
static void UninitReadZlib() |
|
856 |
{ |
|
857 |
inflateEnd(&_z); |
|
858 |
free(_sl.buf); |
|
859 |
} |
|
860 |
||
861 |
static bool InitWriteZlib() |
|
862 |
{ |
|
863 |
memset(&_z, 0, sizeof(_z)); |
|
864 |
if (deflateInit(&_z, 6) != Z_OK) return false; |
|
865 |
||
866 |
_sl.bufsize = 4096; |
|
867 |
_sl.buf = (byte*)malloc(4096); // also contains fread buffer |
|
868 |
return true; |
|
869 |
} |
|
870 |
||
871 |
static void WriteZlibLoop(z_streamp z, byte *p, uint len, int mode) |
|
872 |
{ |
|
873 |
char buf[1024]; // output buffer |
|
874 |
int r; |
|
875 |
uint n; |
|
876 |
z->next_in = p; |
|
877 |
z->avail_in = len; |
|
878 |
do { |
|
879 |
z->next_out = buf; |
|
880 |
z->avail_out = sizeof(buf); |
|
881 |
r = deflate(z, mode); |
|
882 |
// bytes were emitted? |
|
883 |
if ((n=sizeof(buf) - z->avail_out) != 0) { |
|
884 |
if (fwrite(buf, n, 1, _sl.fh) != 1) SlError("file write error"); |
|
885 |
} |
|
886 |
if (r == Z_STREAM_END) |
|
887 |
break; |
|
888 |
if (r != Z_OK) SlError("zlib returned error code"); |
|
889 |
} while (z->avail_in || !z->avail_out); |
|
890 |
} |
|
891 |
||
892 |
static void WriteZlib(uint len) |
|
893 |
{ |
|
894 |
WriteZlibLoop(&_z, _sl.buf, len, 0); |
|
895 |
} |
|
896 |
||
897 |
static void UninitWriteZlib() |
|
898 |
{ |
|
899 |
// flush any pending output. |
|
900 |
if (_sl.fh) WriteZlibLoop(&_z, NULL, 0, Z_FINISH); |
|
901 |
deflateEnd(&_z); |
|
902 |
free(_sl.buf); |
|
903 |
} |
|
904 |
||
905 |
#endif //WITH_ZLIB |
|
906 |
||
907 |
//******************************************* |
|
908 |
//************* END OF CODE ***************** |
|
909 |
//******************************************* |
|
910 |
||
911 |
// these define the chunks |
|
912 |
extern const ChunkHandler _misc_chunk_handlers[]; |
|
913 |
extern const ChunkHandler _player_chunk_handlers[]; |
|
914 |
extern const ChunkHandler _veh_chunk_handlers[]; |
|
915 |
extern const ChunkHandler _town_chunk_handlers[]; |
|
916 |
extern const ChunkHandler _sign_chunk_handlers[]; |
|
917 |
extern const ChunkHandler _station_chunk_handlers[]; |
|
918 |
extern const ChunkHandler _industry_chunk_handlers[]; |
|
919 |
extern const ChunkHandler _engine_chunk_handlers[]; |
|
920 |
extern const ChunkHandler _economy_chunk_handlers[]; |
|
921 |
extern const ChunkHandler _animated_tile_chunk_handlers[]; |
|
922 |
||
923 |
static const ChunkHandler * const _chunk_handlers[] = { |
|
924 |
_misc_chunk_handlers, |
|
925 |
_veh_chunk_handlers, |
|
926 |
_industry_chunk_handlers, |
|
927 |
_economy_chunk_handlers, |
|
928 |
_engine_chunk_handlers, |
|
929 |
_town_chunk_handlers, |
|
930 |
_sign_chunk_handlers, |
|
931 |
_station_chunk_handlers, |
|
932 |
_player_chunk_handlers, |
|
933 |
_animated_tile_chunk_handlers, |
|
934 |
NULL, |
|
935 |
}; |
|
936 |
||
937 |
// used to include a vehicle desc in another desc. |
|
938 |
extern const byte _common_veh_desc[]; |
|
939 |
static const byte * const _desc_includes[] = { |
|
940 |
_common_veh_desc |
|
941 |
}; |
|
942 |
||
943 |
typedef struct { |
|
944 |
void *base; |
|
945 |
size_t size; |
|
946 |
} ReferenceSetup; |
|
947 |
||
948 |
// used to translate "pointers" |
|
949 |
static const ReferenceSetup _ref_setup[] = { |
|
950 |
{_order_array,sizeof(_order_array[0])}, |
|
951 |
{_vehicles,sizeof(_vehicles[0])}, |
|
952 |
{_stations,sizeof(_stations[0])}, |
|
953 |
{_towns,sizeof(_towns[0])}, |
|
954 |
}; |
|
955 |
||
956 |
uint ReferenceToInt(void *v, uint t) |
|
957 |
{ |
|
958 |
if (v == NULL) return 0; |
|
959 |
return ((byte*)v - (byte*)_ref_setup[t].base) / _ref_setup[t].size + 1; |
|
960 |
} |
|
961 |
||
962 |
void *IntToReference(uint r, uint t) |
|
963 |
{ |
|
964 |
if (r == 0) return NULL; |
|
965 |
return (byte*)_ref_setup[t].base + (r-1) * _ref_setup[t].size; |
|
966 |
} |
|
967 |
||
968 |
typedef struct { |
|
969 |
const char *name; |
|
970 |
uint32 tag; |
|
971 |
||
972 |
bool (*init_read)(); |
|
973 |
ReaderProc *reader; |
|
974 |
void (*uninit_read)(); |
|
975 |
||
976 |
bool (*init_write)(); |
|
977 |
WriterProc *writer; |
|
978 |
void (*uninit_write)(); |
|
979 |
||
980 |
} SaveLoadFormat; |
|
981 |
||
982 |
static const SaveLoadFormat _saveload_formats[] = { |
|
983 |
{"lzo", TO_BE32X('OTTD'), InitLZO,ReadLZO, UninitLZO, InitLZO, WriteLZO, UninitLZO}, |
|
984 |
{"none", TO_BE32X('OTTN'), InitNoComp,ReadNoComp, UninitNoComp, InitNoComp, WriteNoComp, UninitNoComp}, |
|
985 |
#if defined(WITH_ZLIB) |
|
986 |
{"zlib", TO_BE32X('OTTZ'), InitReadZlib,ReadZlib, UninitReadZlib, InitWriteZlib, WriteZlib, UninitWriteZlib}, |
|
987 |
#else |
|
988 |
{"zlib", TO_BE32X('OTTZ'), NULL,NULL,NULL,NULL,NULL,NULL} |
|
989 |
#endif |
|
990 |
}; |
|
991 |
||
992 |
static const SaveLoadFormat *GetSavegameFormat(const char *s) |
|
993 |
{ |
|
994 |
const SaveLoadFormat *def; |
|
995 |
int i; |
|
996 |
||
997 |
// find default savegame format |
|
998 |
def = endof(_saveload_formats) - 1; |
|
999 |
while (!def->init_write) def--; |
|
1000 |
||
1001 |
if (_savegame_format[0]) { |
|
1002 |
for(i = 0; i!=lengthof(_saveload_formats); i++) |
|
1003 |
if (_saveload_formats[i].init_write && !strcmp(s, _saveload_formats[i].name)) |
|
1004 |
return _saveload_formats + i; |
|
1005 |
||
1006 |
ShowInfoF("Savegame format '%s' is not available. Reverting to '%s'.", s, def->name); |
|
1007 |
} |
|
1008 |
return def; |
|
1009 |
} |
|
1010 |
||
1011 |
// actual loader/saver function |
|
1012 |
extern void InitializeGame(); |
|
1013 |
extern bool AfterLoadGame(uint version); |
|
1014 |
extern void BeforeSaveGame(); |
|
1015 |
extern bool LoadOldSaveGame(const char *file); |
|
1016 |
||
1017 |
// Save or Load files SL_LOAD, SL_SAVE, SL_OLD_LOAD |
|
1018 |
int SaveOrLoad(const char *filename, int mode) |
|
1019 |
{ |
|
1020 |
uint32 hdr[2]; |
|
1021 |
const SaveLoadFormat *fmt; |
|
1022 |
uint version; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1023 |
|
0 | 1024 |
// old style load |
1025 |
if (mode == SL_OLD_LOAD) { |
|
1026 |
InitializeGame(); |
|
1027 |
if (!LoadOldSaveGame(filename)) return SL_REINIT; |
|
1028 |
AfterLoadGame(0); |
|
1029 |
return SL_OK; |
|
1030 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1031 |
|
0 | 1032 |
_sl.fh = fopen(filename, mode?"wb":"rb"); |
1033 |
if (_sl.fh == NULL) |
|
1034 |
return SL_ERROR; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1035 |
|
0 | 1036 |
_sl.bufe = _sl.bufp = NULL; |
1037 |
_sl.offs_base = 0; |
|
1038 |
_sl.int_to_ref_proc = IntToReference; |
|
1039 |
_sl.ref_to_int_proc = ReferenceToInt; |
|
1040 |
_sl.save = mode; |
|
1041 |
_sl.includes = _desc_includes; |
|
1042 |
_sl.chs = _chunk_handlers; |
|
1043 |
||
1044 |
// setup setjmp error handler |
|
1045 |
if (setjmp(_sl.excpt)) { |
|
1046 |
// close file handle. |
|
1047 |
fclose(_sl.fh); _sl.fh = NULL; |
|
1048 |
||
1049 |
// deinitialize compressor. |
|
1050 |
_sl.excpt_uninit(); |
|
1051 |
||
1052 |
// a saver/loader exception!! |
|
1053 |
// reinitialize all variables to prevent crash! |
|
1054 |
if (mode == SL_LOAD) { |
|
1055 |
ShowInfoF("Load game failed: %s.", _sl.excpt_msg); |
|
1056 |
return SL_REINIT; |
|
1057 |
} else { |
|
1058 |
ShowInfoF("Save game failed: %s.", _sl.excpt_msg); |
|
1059 |
return SL_ERROR; |
|
1060 |
} |
|
1061 |
} |
|
1062 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1063 |
// we first initialize here to avoid: "warning: variable `version' might |
0 | 1064 |
// be clobbered by `longjmp' or `vfork'" |
1065 |
version = 0; |
|
1066 |
||
1067 |
if (mode != SL_LOAD) { |
|
1068 |
fmt = GetSavegameFormat(_savegame_format); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1069 |
|
0 | 1070 |
_sl.write_bytes = fmt->writer; |
1071 |
_sl.excpt_uninit = fmt->uninit_write; |
|
1072 |
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
|
1073 |
|
0 | 1074 |
hdr[0] = fmt->tag; |
1075 |
hdr[1] = TO_BE32((SAVEGAME_MAJOR_VERSION<<16) + (SAVEGAME_MINOR_VERSION << 8)); |
|
1076 |
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
|
1077 |
|
0 | 1078 |
_sl.version = SAVEGAME_MAJOR_VERSION; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1079 |
|
0 | 1080 |
BeforeSaveGame(); |
1081 |
SlSaveChunks(); |
|
1082 |
SlWriteFill(); // flush the save buffer |
|
1083 |
fmt->uninit_write(); |
|
1084 |
||
1085 |
} else { |
|
1086 |
if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) { |
|
1087 |
read_err: |
|
1088 |
printf("Savegame is obsolete or invalid format.\n"); |
|
1089 |
init_err: |
|
1090 |
fclose(_sl.fh); |
|
1091 |
return SL_ERROR; |
|
1092 |
} |
|
1093 |
||
1094 |
// see if we have any loader for this type. |
|
1095 |
for(fmt = _saveload_formats; ; fmt++) { |
|
1096 |
if (fmt == endof(_saveload_formats)) { |
|
1097 |
printf("Unknown savegame type, trying to load it as the buggy format.\n"); |
|
1098 |
rewind(_sl.fh); |
|
1099 |
_sl.version = 0; |
|
1100 |
version = 0; |
|
1101 |
fmt = _saveload_formats + 0; // lzo |
|
1102 |
break; |
|
1103 |
} |
|
1104 |
if (fmt->tag == hdr[0]) { |
|
1105 |
// check version number |
|
1106 |
version = TO_BE32(hdr[1]) >> 8; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1107 |
|
0 | 1108 |
// incompatible version? |
1109 |
if (version > SAVEGAME_LOADABLE_VERSION) goto read_err; |
|
1110 |
_sl.version = (version>>8); |
|
1111 |
break; |
|
1112 |
} |
|
1113 |
} |
|
1114 |
||
1115 |
_sl.read_bytes = fmt->reader; |
|
1116 |
_sl.excpt_uninit = fmt->uninit_read; |
|
1117 |
||
1118 |
// loader for this savegame type is not implemented? |
|
1119 |
if (fmt->init_read == NULL) { |
|
1120 |
ShowInfoF("Loader for '%s' is not available.", fmt->name); |
|
1121 |
fclose(_sl.fh); |
|
1122 |
return SL_ERROR; |
|
1123 |
} |
|
1124 |
||
1125 |
if (!fmt->init_read()) goto init_err; |
|
1126 |
// Clear everything |
|
1127 |
InitializeGame(); |
|
1128 |
SlLoadChunks(); |
|
1129 |
fmt->uninit_read(); |
|
1130 |
} |
|
1131 |
||
1132 |
fclose(_sl.fh); |
|
1133 |
||
1134 |
if (mode == SL_LOAD) { |
|
1135 |
if (!AfterLoadGame(version)) |
|
1136 |
return SL_REINIT; |
|
1137 |
} |
|
1138 |
||
1139 |
return SL_OK; |
|
1140 |
} |
|
1141 |
||
1142 |
bool EmergencySave() |
|
1143 |
{ |
|
1144 |
SaveOrLoad("crash.sav", SL_SAVE); |
|
1145 |
return true; |
|
1146 |
} |
|
1147 |
||
1148 |
// not used right now, but could be used if extensions of savegames are garbled |
|
1149 |
/*int GetSavegameType(char *file) |
|
1150 |
{ |
|
1151 |
const SaveLoadFormat *fmt; |
|
1152 |
uint32 hdr; |
|
1153 |
FILE *f; |
|
1154 |
int mode = SL_OLD_LOAD; |
|
1155 |
||
1156 |
f = fopen(file, "rb"); |
|
1157 |
if (fread(&hdr, sizeof(hdr), 1, f) != 1) { |
|
1158 |
printf("Savegame is obsolete or invalid format.\n"); |
|
1159 |
mode = SL_LOAD; // don't try to get filename, just show name as it is written |
|
1160 |
} |
|
1161 |
else { |
|
1162 |
// see if we have any loader for this type. |
|
1163 |
for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) { |
|
1164 |
if (fmt->tag == hdr) { |
|
1165 |
mode = SL_LOAD; // new type of savegame |
|
1166 |
break; |
|
1167 |
} |
|
1168 |
} |
|
1169 |
} |
|
1170 |
||
1171 |
fclose(f); |
|
1172 |
return mode; |
|
1173 |
}*/ |