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