author | truelight |
Thu, 24 Mar 2005 14:43:40 +0000 | |
changeset 1540 | 6eef12dab5bf |
parent 1537 | 517b9b9cfaa0 |
child 1542 | 62a03537ad0b |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
1299
39c06aba09aa
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1284
diff
changeset
|
3 |
#include "debug.h" |
0 | 4 |
#include "vehicle.h" |
5 |
#include "station.h" |
|
6 |
#include "town.h" |
|
7 |
#include "player.h" |
|
8 |
#include "saveload.h" |
|
9 |
||
10 |
enum { |
|
1537
517b9b9cfaa0
(svn r2041) -Fix: not all vehicles did get a day_proc call (because of rounding errors)
truelight
parents:
1529
diff
changeset
|
11 |
SAVEGAME_MAJOR_VERSION = 0xB, |
517b9b9cfaa0
(svn r2041) -Fix: not all vehicles did get a day_proc call (because of rounding errors)
truelight
parents:
1529
diff
changeset
|
12 |
SAVEGAME_MINOR_VERSION = 0x1, |
0 | 13 |
|
14 |
SAVEGAME_LOADABLE_VERSION = (SAVEGAME_MAJOR_VERSION << 8) + SAVEGAME_MINOR_VERSION |
|
15 |
}; |
|
16 |
||
17 |
/******************************************************/ |
|
18 |
/******************************************************/ |
|
19 |
/******************************************************/ |
|
20 |
||
21 |
enum NeedLengthValues { NL_NONE = 0,NL_WANTLENGTH = 1,NL_CALCLENGTH = 2}; |
|
22 |
||
23 |
SaverLoader _sl; |
|
24 |
||
25 |
// fill the input buffer |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
26 |
static void SlReadFill(void) |
0 | 27 |
{ |
28 |
uint len = _sl.read_bytes(); |
|
29 |
assert(len != 0); |
|
30 |
||
31 |
_sl.bufp = _sl.buf; |
|
32 |
_sl.bufe = _sl.buf + len; |
|
33 |
_sl.offs_base += len; |
|
34 |
} |
|
35 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
36 |
static uint32 SlGetOffs(void) |
0 | 37 |
{ |
38 |
return _sl.offs_base - (_sl.bufe - _sl.bufp); |
|
39 |
} |
|
40 |
||
41 |
// flush the output buffer |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
42 |
static void SlWriteFill(void) |
0 | 43 |
{ |
44 |
// flush current buffer? |
|
45 |
if (_sl.bufp != NULL) { |
|
46 |
uint len = _sl.bufp - _sl.buf; |
|
47 |
_sl.offs_base += len; |
|
48 |
if (len) _sl.write_bytes(len); |
|
49 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
50 |
|
0 | 51 |
// setup next buffer |
52 |
_sl.bufp = _sl.buf; |
|
53 |
_sl.bufe = _sl.buf + _sl.bufsize; |
|
54 |
} |
|
55 |
||
56 |
// error handler, calls longjmp to simulate an exception. |
|
57 |
static void NORETURN SlError(const char *msg) |
|
58 |
{ |
|
59 |
_sl.excpt_msg = msg; |
|
60 |
longjmp(_sl.excpt, 0); |
|
61 |
} |
|
62 |
||
1514 | 63 |
static inline int SlReadByteInternal(void) |
0 | 64 |
{ |
65 |
if (_sl.bufp == _sl.bufe) SlReadFill(); |
|
66 |
return *_sl.bufp++; |
|
67 |
} |
|
68 |
||
1514 | 69 |
int SlReadByte(void) |
70 |
{ |
|
71 |
return SlReadByteInternal(); |
|
72 |
} |
|
73 |
||
0 | 74 |
void SlWriteByte(byte v) |
75 |
{ |
|
76 |
if (_sl.bufp == _sl.bufe) SlWriteFill(); |
|
77 |
*_sl.bufp++ = v; |
|
78 |
} |
|
79 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
80 |
static int SlReadUint16(void) |
0 | 81 |
{ |
82 |
int x = SlReadByte() << 8; |
|
83 |
return x | SlReadByte(); |
|
84 |
} |
|
85 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
86 |
static uint32 SlReadUint32(void) |
0 | 87 |
{ |
88 |
uint32 x = SlReadUint16() << 16; |
|
89 |
return x | SlReadUint16(); |
|
90 |
} |
|
91 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
92 |
static uint64 SlReadUint64(void) |
0 | 93 |
{ |
94 |
uint32 x = SlReadUint32(); |
|
95 |
uint32 y = SlReadUint32(); |
|
96 |
return (uint64)x << 32 | y; |
|
97 |
} |
|
98 |
||
410 | 99 |
static void SlWriteUint16(uint16 v) |
0 | 100 |
{ |
101 |
SlWriteByte((byte)(v >> 8)); |
|
102 |
SlWriteByte((byte)v); |
|
103 |
} |
|
104 |
||
410 | 105 |
static void SlWriteUint32(uint32 v) |
0 | 106 |
{ |
107 |
SlWriteUint16((uint16)(v >> 16)); |
|
108 |
SlWriteUint16((uint16)v); |
|
109 |
} |
|
110 |
||
410 | 111 |
static void SlWriteUint64(uint64 x) |
0 | 112 |
{ |
113 |
SlWriteUint32((uint32)(x >> 32)); |
|
114 |
SlWriteUint32((uint32)x); |
|
115 |
} |
|
116 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
117 |
static int SlReadSimpleGamma(void) |
0 | 118 |
{ |
119 |
int x = SlReadByte(); |
|
120 |
if (x & 0x80) |
|
121 |
x = ((x&0x7F) << 8) + SlReadByte(); |
|
122 |
return x; |
|
123 |
} |
|
124 |
||
125 |
static void SlWriteSimpleGamma(uint i) |
|
126 |
{ |
|
127 |
assert(i < (1 << 14)); |
|
128 |
if (i >= 0x80) { |
|
129 |
SlWriteByte((byte)(0x80|(i >> 8))); |
|
130 |
SlWriteByte((byte)i); |
|
131 |
} else { |
|
132 |
SlWriteByte(i); |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
static uint SlGetGammaLength(uint i) { |
|
137 |
return (i>=0x80) ? 2 : 1; |
|
138 |
} |
|
139 |
||
1095 | 140 |
static inline int SlReadSparseIndex(void) |
0 | 141 |
{ |
142 |
return SlReadSimpleGamma(); |
|
143 |
} |
|
144 |
||
1095 | 145 |
static inline void SlWriteSparseIndex(uint index) |
0 | 146 |
{ |
147 |
SlWriteSimpleGamma(index); |
|
148 |
} |
|
149 |
||
1095 | 150 |
static inline int SlReadArrayLength(void) |
0 | 151 |
{ |
152 |
return SlReadSimpleGamma(); |
|
153 |
} |
|
154 |
||
1095 | 155 |
static inline void SlWriteArrayLength(uint length) |
0 | 156 |
{ |
157 |
SlWriteSimpleGamma(length); |
|
158 |
} |
|
159 |
||
160 |
void SlSetArrayIndex(uint index) |
|
161 |
{ |
|
162 |
_sl.need_length = NL_WANTLENGTH; |
|
163 |
_sl.array_index = index; |
|
164 |
} |
|
165 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
166 |
int SlIterateArray(void) |
0 | 167 |
{ |
168 |
int ind; |
|
169 |
static uint32 next_offs; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
170 |
|
0 | 171 |
// Must be at end of current block. |
172 |
assert(next_offs == 0 || SlGetOffs() == next_offs); |
|
173 |
||
174 |
while(true) { |
|
175 |
uint len = SlReadArrayLength(); |
|
176 |
if (len == 0) { |
|
177 |
next_offs = 0; |
|
178 |
return -1; |
|
179 |
} |
|
180 |
||
181 |
_sl.obj_len = --len; |
|
182 |
next_offs = SlGetOffs() + len; |
|
183 |
||
184 |
switch(_sl.block_mode) { |
|
185 |
case CH_SPARSE_ARRAY: ind = SlReadSparseIndex(); break; |
|
186 |
case CH_ARRAY: ind = _sl.array_index++; break; |
|
187 |
default: |
|
188 |
DEBUG(misc, 0) ("SlIterateArray: error\n"); |
|
189 |
return -1; // error |
|
190 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
191 |
|
0 | 192 |
if (len != 0) |
193 |
return ind; |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
// Sets the length of either a RIFF object or the number of items in an array. |
|
198 |
void SlSetLength(uint length) |
|
199 |
{ |
|
200 |
switch(_sl.need_length) { |
|
201 |
case NL_WANTLENGTH: |
|
202 |
_sl.need_length = NL_NONE; |
|
203 |
switch(_sl.block_mode) { |
|
204 |
case CH_RIFF: |
|
205 |
// Really simple to write a RIFF length :) |
|
206 |
SlWriteUint32(length); |
|
207 |
break; |
|
208 |
case CH_ARRAY: |
|
209 |
assert(_sl.array_index >= _sl.last_array_index); |
|
210 |
while (++_sl.last_array_index <= _sl.array_index) |
|
211 |
SlWriteArrayLength(1); |
|
212 |
SlWriteArrayLength(length + 1); |
|
213 |
break; |
|
214 |
case CH_SPARSE_ARRAY: |
|
215 |
SlWriteArrayLength(length + 1 + SlGetGammaLength(_sl.array_index)); // Also include length of sparse index. |
|
216 |
SlWriteSparseIndex(_sl.array_index); |
|
217 |
break; |
|
218 |
default: NOT_REACHED(); |
|
219 |
} |
|
220 |
break; |
|
221 |
case NL_CALCLENGTH: |
|
222 |
_sl.obj_len += length; |
|
223 |
break; |
|
224 |
} |
|
225 |
} |
|
226 |
||
410 | 227 |
static void SlCopyBytes(void *ptr, size_t length) |
0 | 228 |
{ |
229 |
byte *p = (byte*)ptr; |
|
230 |
||
231 |
if (_sl.save) { |
|
232 |
while(length) { |
|
233 |
SlWriteByte(*p++); |
|
234 |
length--; |
|
235 |
} |
|
236 |
} else { |
|
237 |
while(length) { |
|
1514 | 238 |
*p++ = SlReadByteInternal(); |
0 | 239 |
length--; |
240 |
} |
|
241 |
} |
|
242 |
} |
|
243 |
||
1095 | 244 |
static void SlSkipBytes(size_t length) |
0 | 245 |
{ |
246 |
while (length) { |
|
247 |
SlReadByte(); |
|
248 |
length--; |
|
249 |
} |
|
250 |
} |
|
251 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
252 |
uint SlGetFieldLength(void) |
0 | 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
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
|
497 |
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
|
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 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
586 |
static void SlStubSaveProc(void) |
0 | 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 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
626 |
static void SlSaveChunks(void) |
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 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
664 |
static void SlLoadChunks(void) |
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
4c9177888196
(svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents:
762
diff
changeset
|
687 |
#include "minilzo.h" |
0 | 688 |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
689 |
static uint ReadLZO(void) |
0 | 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 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
734 |
static bool InitLZO(void) |
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
735 |
{ |
0 | 736 |
_sl.bufsize = LZO_SIZE; |
737 |
_sl.buf = (byte*)malloc(LZO_SIZE); |
|
738 |
return true; |
|
739 |
} |
|
740 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
741 |
static void UninitLZO(void) |
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
742 |
{ |
0 | 743 |
free(_sl.buf); |
744 |
} |
|
745 |
||
746 |
//******************************************* |
|
747 |
//******** START OF NOCOMP CODE ************* |
|
748 |
//******************************************* |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
749 |
static uint ReadNoComp(void) |
0 | 750 |
{ |
751 |
return fread(_sl.buf, 1, LZO_SIZE, _sl.fh); |
|
752 |
} |
|
753 |
||
754 |
static void WriteNoComp(uint size) |
|
755 |
{ |
|
756 |
fwrite(_sl.buf, 1, size, _sl.fh); |
|
757 |
} |
|
758 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
759 |
static bool InitNoComp(void) |
0 | 760 |
{ |
761 |
_sl.bufsize = LZO_SIZE; |
|
762 |
_sl.buf = (byte*)malloc(LZO_SIZE); |
|
763 |
return true; |
|
764 |
} |
|
765 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
766 |
static void UninitNoComp(void) |
0 | 767 |
{ |
768 |
free(_sl.buf); |
|
769 |
} |
|
770 |
||
771 |
//******************************************** |
|
772 |
//********** START OF ZLIB CODE ************** |
|
773 |
//******************************************** |
|
774 |
||
775 |
#if defined(WITH_ZLIB) |
|
776 |
#include <zlib.h> |
|
777 |
static z_stream _z; |
|
778 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
779 |
static bool InitReadZlib(void) |
0 | 780 |
{ |
781 |
memset(&_z, 0, sizeof(_z)); |
|
782 |
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
|
783 |
|
0 | 784 |
_sl.bufsize = 4096; |
785 |
_sl.buf = (byte*)malloc(4096 + 4096); // also contains fread buffer |
|
786 |
return true; |
|
787 |
} |
|
788 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
789 |
static uint ReadZlib(void) |
0 | 790 |
{ |
791 |
int r; |
|
792 |
||
793 |
_z.next_out = _sl.buf; |
|
794 |
_z.avail_out = 4096; |
|
795 |
||
796 |
do { |
|
797 |
// read more bytes from the file? |
|
798 |
if (_z.avail_in == 0) { |
|
799 |
_z.avail_in = fread(_z.next_in = _sl.buf + 4096, 1, 4096, _sl.fh); |
|
800 |
} |
|
801 |
||
802 |
// inflate the data |
|
803 |
r = inflate(&_z, 0); |
|
804 |
if (r == Z_STREAM_END) |
|
805 |
break; |
|
806 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
807 |
if (r != Z_OK) |
0 | 808 |
SlError("inflate() failed"); |
809 |
} while (_z.avail_out); |
|
810 |
||
811 |
return 4096 - _z.avail_out; |
|
812 |
} |
|
813 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
814 |
static void UninitReadZlib(void) |
0 | 815 |
{ |
816 |
inflateEnd(&_z); |
|
817 |
free(_sl.buf); |
|
818 |
} |
|
819 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
820 |
static bool InitWriteZlib(void) |
0 | 821 |
{ |
822 |
memset(&_z, 0, sizeof(_z)); |
|
823 |
if (deflateInit(&_z, 6) != Z_OK) return false; |
|
824 |
||
825 |
_sl.bufsize = 4096; |
|
826 |
_sl.buf = (byte*)malloc(4096); // also contains fread buffer |
|
827 |
return true; |
|
828 |
} |
|
829 |
||
830 |
static void WriteZlibLoop(z_streamp z, byte *p, uint len, int mode) |
|
831 |
{ |
|
832 |
char buf[1024]; // output buffer |
|
833 |
int r; |
|
834 |
uint n; |
|
835 |
z->next_in = p; |
|
836 |
z->avail_in = len; |
|
837 |
do { |
|
838 |
z->next_out = buf; |
|
839 |
z->avail_out = sizeof(buf); |
|
840 |
r = deflate(z, mode); |
|
841 |
// bytes were emitted? |
|
842 |
if ((n=sizeof(buf) - z->avail_out) != 0) { |
|
843 |
if (fwrite(buf, n, 1, _sl.fh) != 1) SlError("file write error"); |
|
844 |
} |
|
845 |
if (r == Z_STREAM_END) |
|
846 |
break; |
|
847 |
if (r != Z_OK) SlError("zlib returned error code"); |
|
848 |
} while (z->avail_in || !z->avail_out); |
|
849 |
} |
|
850 |
||
851 |
static void WriteZlib(uint len) |
|
852 |
{ |
|
853 |
WriteZlibLoop(&_z, _sl.buf, len, 0); |
|
854 |
} |
|
855 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
856 |
static void UninitWriteZlib(void) |
0 | 857 |
{ |
858 |
// flush any pending output. |
|
859 |
if (_sl.fh) WriteZlibLoop(&_z, NULL, 0, Z_FINISH); |
|
860 |
deflateEnd(&_z); |
|
861 |
free(_sl.buf); |
|
862 |
} |
|
863 |
||
864 |
#endif //WITH_ZLIB |
|
865 |
||
866 |
//******************************************* |
|
867 |
//************* END OF CODE ***************** |
|
868 |
//******************************************* |
|
869 |
||
870 |
// these define the chunks |
|
871 |
extern const ChunkHandler _misc_chunk_handlers[]; |
|
872 |
extern const ChunkHandler _player_chunk_handlers[]; |
|
873 |
extern const ChunkHandler _veh_chunk_handlers[]; |
|
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
874 |
extern const ChunkHandler _depot_chunk_handlers[]; |
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
950
diff
changeset
|
875 |
extern const ChunkHandler _order_chunk_handlers[]; |
0 | 876 |
extern const ChunkHandler _town_chunk_handlers[]; |
877 |
extern const ChunkHandler _sign_chunk_handlers[]; |
|
878 |
extern const ChunkHandler _station_chunk_handlers[]; |
|
879 |
extern const ChunkHandler _industry_chunk_handlers[]; |
|
880 |
extern const ChunkHandler _engine_chunk_handlers[]; |
|
881 |
extern const ChunkHandler _economy_chunk_handlers[]; |
|
882 |
extern const ChunkHandler _animated_tile_chunk_handlers[]; |
|
883 |
||
884 |
static const ChunkHandler * const _chunk_handlers[] = { |
|
885 |
_misc_chunk_handlers, |
|
886 |
_veh_chunk_handlers, |
|
1313
f1013ec3d318
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1299
diff
changeset
|
887 |
_depot_chunk_handlers, |
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
950
diff
changeset
|
888 |
_order_chunk_handlers, |
0 | 889 |
_industry_chunk_handlers, |
890 |
_economy_chunk_handlers, |
|
891 |
_engine_chunk_handlers, |
|
892 |
_town_chunk_handlers, |
|
893 |
_sign_chunk_handlers, |
|
894 |
_station_chunk_handlers, |
|
895 |
_player_chunk_handlers, |
|
896 |
_animated_tile_chunk_handlers, |
|
897 |
NULL, |
|
898 |
}; |
|
899 |
||
900 |
// used to include a vehicle desc in another desc. |
|
901 |
extern const byte _common_veh_desc[]; |
|
902 |
static const byte * const _desc_includes[] = { |
|
903 |
_common_veh_desc |
|
904 |
}; |
|
905 |
||
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
906 |
/* We can't save pointers to a savegame, so this functions get's |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
907 |
the index of the item, and if not available, it hussles with |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
908 |
pointers (looks really bad :() |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
909 |
Remember that a NULL item has value 0, and all |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
910 |
indexes have + 1, so vehicle 0 is saved as index 1. */ |
410 | 911 |
static uint ReferenceToInt(void *v, uint t) |
0 | 912 |
{ |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
913 |
if (v == NULL) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
914 |
return 0; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
915 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
916 |
switch (t) { |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
917 |
case REF_VEHICLE_OLD: // Old vehicles we save as new onces |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
918 |
case REF_VEHICLE: return ((Vehicle *)v)->index + 1; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
919 |
case REF_STATION: return ((Station *)v)->index + 1; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
920 |
case REF_TOWN: return ((Town *)v)->index + 1; |
1024
5e446b5b3ec5
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
950
diff
changeset
|
921 |
case REF_ORDER: return ((Order *)v)->index + 1; |
1284
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
922 |
case REF_ROADSTOPS: return ((RoadStop *)v)->index + 1; |
1217
59c024cfaf54
(svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents:
1171
diff
changeset
|
923 |
|
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
924 |
default: |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
925 |
NOT_REACHED(); |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
926 |
} |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
927 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
928 |
return 0; |
0 | 929 |
} |
930 |
||
1095 | 931 |
static void *IntToReference(uint r, uint t) |
0 | 932 |
{ |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
933 |
/* From version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE, and should be loaded |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
934 |
like that */ |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
935 |
if (t == REF_VEHICLE_OLD && |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
936 |
_sl.full_version >= 0x404) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
937 |
t = REF_VEHICLE; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
938 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
939 |
if (t != REF_VEHICLE_OLD && r == 0) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
940 |
return NULL; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
941 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
942 |
switch (t) { |
1314
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
943 |
case REF_ORDER: { |
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
944 |
if (!AddBlockIfNeeded(&_order_pool, r - 1)) |
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
945 |
error("Orders: failed loading savegame: too many orders"); |
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
946 |
return GetOrder(r - 1); |
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
947 |
} |
1279
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
948 |
case REF_VEHICLE: { |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
949 |
if (!AddBlockIfNeeded(&_vehicle_pool, r - 1)) |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
950 |
error("Vehicles: failed loading savegame: too many vehicles"); |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
951 |
return GetVehicle(r - 1); |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
952 |
} |
1272
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
953 |
case REF_STATION: { |
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
954 |
if (!AddBlockIfNeeded(&_station_pool, r - 1)) |
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
955 |
error("Stations: failed loading savegame: too many stations"); |
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
956 |
return GetStation(r - 1); |
9aa073c64fc7
(svn r1776) -Add: Dynamic stations. You can now have up to 64k of stations
truelight
parents:
1266
diff
changeset
|
957 |
} |
1260
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
958 |
case REF_TOWN: { |
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
959 |
if (!AddBlockIfNeeded(&_town_pool, r - 1)) |
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
960 |
error("Towns: failed loading savegame: too many towns"); |
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
961 |
return GetTown(r - 1); |
632f63689178
(svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents:
1218
diff
changeset
|
962 |
} |
1284
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
963 |
case REF_ROADSTOPS: { |
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
964 |
if (!AddBlockIfNeeded(&_roadstop_pool, r - 1)) |
1314
10c3417e9c22
(svn r1818) -Add: Dynamic orders (up to 64k orders)
truelight
parents:
1313
diff
changeset
|
965 |
error("RoadStops: failed loading savegame: too many RoadStops"); |
1284
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
966 |
return GetRoadStop(r - 1); |
e1969eb0227b
(svn r1788) -Add: Made RoadStops dynamic. You can now create up to 64k roadstops.
truelight
parents:
1282
diff
changeset
|
967 |
} |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
968 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
969 |
case REF_VEHICLE_OLD: { |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
970 |
/* Old vehicles were saved differently: invalid vehicle was 0xFFFF, |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
971 |
and the index was not - 1.. correct for this */ |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
972 |
if (r == INVALID_VEHICLE) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
973 |
return NULL; |
1279
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
974 |
|
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
975 |
if (!AddBlockIfNeeded(&_vehicle_pool, r)) |
bc761aad52b3
(svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents:
1272
diff
changeset
|
976 |
error("Vehicles: failed loading savegame: too many vehicles"); |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
977 |
return GetVehicle(r); |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
978 |
} |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
979 |
default: |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
980 |
NOT_REACHED(); |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
981 |
} |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
982 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
983 |
return NULL; |
0 | 984 |
} |
985 |
||
986 |
typedef struct { |
|
987 |
const char *name; |
|
988 |
uint32 tag; |
|
989 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
990 |
bool (*init_read)(void); |
0 | 991 |
ReaderProc *reader; |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
992 |
void (*uninit_read)(void); |
0 | 993 |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
994 |
bool (*init_write)(void); |
0 | 995 |
WriterProc *writer; |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
996 |
void (*uninit_write)(void); |
0 | 997 |
|
998 |
} SaveLoadFormat; |
|
999 |
||
1000 |
static const SaveLoadFormat _saveload_formats[] = { |
|
1001 |
{"lzo", TO_BE32X('OTTD'), InitLZO,ReadLZO, UninitLZO, InitLZO, WriteLZO, UninitLZO}, |
|
1002 |
{"none", TO_BE32X('OTTN'), InitNoComp,ReadNoComp, UninitNoComp, InitNoComp, WriteNoComp, UninitNoComp}, |
|
1003 |
#if defined(WITH_ZLIB) |
|
1004 |
{"zlib", TO_BE32X('OTTZ'), InitReadZlib,ReadZlib, UninitReadZlib, InitWriteZlib, WriteZlib, UninitWriteZlib}, |
|
1005 |
#else |
|
1006 |
{"zlib", TO_BE32X('OTTZ'), NULL,NULL,NULL,NULL,NULL,NULL} |
|
1007 |
#endif |
|
1008 |
}; |
|
1009 |
||
1010 |
static const SaveLoadFormat *GetSavegameFormat(const char *s) |
|
1011 |
{ |
|
1012 |
const SaveLoadFormat *def; |
|
1013 |
int i; |
|
1014 |
||
1015 |
// find default savegame format |
|
1016 |
def = endof(_saveload_formats) - 1; |
|
1017 |
while (!def->init_write) def--; |
|
1018 |
||
1019 |
if (_savegame_format[0]) { |
|
1020 |
for(i = 0; i!=lengthof(_saveload_formats); i++) |
|
1021 |
if (_saveload_formats[i].init_write && !strcmp(s, _saveload_formats[i].name)) |
|
1022 |
return _saveload_formats + i; |
|
1023 |
||
1024 |
ShowInfoF("Savegame format '%s' is not available. Reverting to '%s'.", s, def->name); |
|
1025 |
} |
|
1026 |
return def; |
|
1027 |
} |
|
1028 |
||
1029 |
// actual loader/saver function |
|
1218 | 1030 |
void InitializeGame(uint log_x, uint log_y); |
0 | 1031 |
extern bool AfterLoadGame(uint version); |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
1032 |
extern void BeforeSaveGame(void); |
0 | 1033 |
extern bool LoadOldSaveGame(const char *file); |
1034 |
||
1035 |
// Save or Load files SL_LOAD, SL_SAVE, SL_OLD_LOAD |
|
1036 |
int SaveOrLoad(const char *filename, int mode) |
|
1037 |
{ |
|
1038 |
uint32 hdr[2]; |
|
1039 |
const SaveLoadFormat *fmt; |
|
1040 |
uint version; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1041 |
|
0 | 1042 |
// old style load |
1043 |
if (mode == SL_OLD_LOAD) { |
|
1218 | 1044 |
InitializeGame(8, 8); |
0 | 1045 |
if (!LoadOldSaveGame(filename)) return SL_REINIT; |
1046 |
AfterLoadGame(0); |
|
1047 |
return SL_OK; |
|
1048 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1049 |
|
0 | 1050 |
_sl.fh = fopen(filename, mode?"wb":"rb"); |
1051 |
if (_sl.fh == NULL) |
|
1052 |
return SL_ERROR; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1053 |
|
0 | 1054 |
_sl.bufe = _sl.bufp = NULL; |
1055 |
_sl.offs_base = 0; |
|
1056 |
_sl.int_to_ref_proc = IntToReference; |
|
1057 |
_sl.ref_to_int_proc = ReferenceToInt; |
|
1058 |
_sl.save = mode; |
|
1059 |
_sl.includes = _desc_includes; |
|
1060 |
_sl.chs = _chunk_handlers; |
|
1061 |
||
1062 |
// setup setjmp error handler |
|
1063 |
if (setjmp(_sl.excpt)) { |
|
1064 |
// close file handle. |
|
1065 |
fclose(_sl.fh); _sl.fh = NULL; |
|
1066 |
||
1067 |
// deinitialize compressor. |
|
1068 |
_sl.excpt_uninit(); |
|
1069 |
||
1070 |
// a saver/loader exception!! |
|
1071 |
// reinitialize all variables to prevent crash! |
|
1072 |
if (mode == SL_LOAD) { |
|
1073 |
ShowInfoF("Load game failed: %s.", _sl.excpt_msg); |
|
1074 |
return SL_REINIT; |
|
1075 |
} else { |
|
1076 |
ShowInfoF("Save game failed: %s.", _sl.excpt_msg); |
|
1077 |
return SL_ERROR; |
|
1078 |
} |
|
1079 |
} |
|
1080 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1081 |
// we first initialize here to avoid: "warning: variable `version' might |
0 | 1082 |
// be clobbered by `longjmp' or `vfork'" |
1083 |
version = 0; |
|
1084 |
||
1085 |
if (mode != SL_LOAD) { |
|
1086 |
fmt = GetSavegameFormat(_savegame_format); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1087 |
|
0 | 1088 |
_sl.write_bytes = fmt->writer; |
1089 |
_sl.excpt_uninit = fmt->uninit_write; |
|
1090 |
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
|
1091 |
|
0 | 1092 |
hdr[0] = fmt->tag; |
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1093 |
hdr[1] = TO_BE32((SAVEGAME_MAJOR_VERSION << 16) + (SAVEGAME_MINOR_VERSION << 8)); |
0 | 1094 |
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
|
1095 |
|
0 | 1096 |
_sl.version = SAVEGAME_MAJOR_VERSION; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1097 |
|
0 | 1098 |
BeforeSaveGame(); |
1099 |
SlSaveChunks(); |
|
1100 |
SlWriteFill(); // flush the save buffer |
|
1101 |
fmt->uninit_write(); |
|
1102 |
||
1103 |
} else { |
|
1104 |
if (fread(hdr, sizeof(hdr), 1, _sl.fh) != 1) { |
|
1105 |
read_err: |
|
1106 |
printf("Savegame is obsolete or invalid format.\n"); |
|
1107 |
init_err: |
|
1108 |
fclose(_sl.fh); |
|
1109 |
return SL_ERROR; |
|
1110 |
} |
|
1111 |
||
1112 |
// see if we have any loader for this type. |
|
1113 |
for(fmt = _saveload_formats; ; fmt++) { |
|
1114 |
if (fmt == endof(_saveload_formats)) { |
|
1115 |
printf("Unknown savegame type, trying to load it as the buggy format.\n"); |
|
1116 |
rewind(_sl.fh); |
|
1117 |
_sl.version = 0; |
|
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1118 |
_sl.full_version = 0; |
0 | 1119 |
version = 0; |
1120 |
fmt = _saveload_formats + 0; // lzo |
|
1121 |
break; |
|
1122 |
} |
|
1123 |
if (fmt->tag == hdr[0]) { |
|
1124 |
// check version number |
|
1125 |
version = TO_BE32(hdr[1]) >> 8; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
121
diff
changeset
|
1126 |
|
938
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1127 |
/* Is the version higher than the current? */ |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1128 |
if (version > SAVEGAME_LOADABLE_VERSION) |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1129 |
goto read_err; |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1130 |
|
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1131 |
_sl.version = (version >> 8); |
23cc832ac8a2
(svn r1426) -Codechange: cleaned up the reference code in the saveload routines
truelight
parents:
929
diff
changeset
|
1132 |
_sl.full_version = version; |
0 | 1133 |
break; |
1134 |
} |
|
1135 |
} |
|
1136 |
||
1137 |
_sl.read_bytes = fmt->reader; |
|
1138 |
_sl.excpt_uninit = fmt->uninit_read; |
|
1139 |
||
1140 |
// loader for this savegame type is not implemented? |
|
1141 |
if (fmt->init_read == NULL) { |
|
1142 |
ShowInfoF("Loader for '%s' is not available.", fmt->name); |
|
1143 |
fclose(_sl.fh); |
|
1144 |
return SL_ERROR; |
|
1145 |
} |
|
1146 |
||
1147 |
if (!fmt->init_read()) goto init_err; |
|
1148 |
// Clear everything |
|
1218 | 1149 |
/* Set the current map to 256x256, in case of an old map. |
1150 |
* Else MAPS will read the right information */ |
|
1151 |
InitializeGame(8, 8); |
|
1152 |
||
0 | 1153 |
SlLoadChunks(); |
1154 |
fmt->uninit_read(); |
|
1155 |
} |
|
1156 |
||
1157 |
fclose(_sl.fh); |
|
1158 |
||
1159 |
if (mode == SL_LOAD) { |
|
1160 |
if (!AfterLoadGame(version)) |
|
1161 |
return SL_REINIT; |
|
1162 |
} |
|
1163 |
||
1164 |
return SL_OK; |
|
1165 |
} |
|
1166 |
||
1094
9a01482df45a
(svn r1595) EmergencySave() is only used on WIN32, add preprocessor guard to hide it everywhere else
tron
parents:
1093
diff
changeset
|
1167 |
#ifdef WIN32 |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
1168 |
bool EmergencySave(void) |
0 | 1169 |
{ |
1170 |
SaveOrLoad("crash.sav", SL_SAVE); |
|
1171 |
return true; |
|
1172 |
} |
|
1094
9a01482df45a
(svn r1595) EmergencySave() is only used on WIN32, add preprocessor guard to hide it everywhere else
tron
parents:
1093
diff
changeset
|
1173 |
#endif |
0 | 1174 |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1024
diff
changeset
|
1175 |
void DoExitSave(void) |
643
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1176 |
{ |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1177 |
char buf[200]; |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1178 |
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
|
1179 |
SaveOrLoad(buf, SL_SAVE); |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1180 |
} |
a9c84d04eacb
(svn r1076) Feature: Patch setting to autosave the game on exit
dominik
parents:
617
diff
changeset
|
1181 |
|
0 | 1182 |
// not used right now, but could be used if extensions of savegames are garbled |
1183 |
/*int GetSavegameType(char *file) |
|
1184 |
{ |
|
1185 |
const SaveLoadFormat *fmt; |
|
1186 |
uint32 hdr; |
|
1187 |
FILE *f; |
|
1188 |
int mode = SL_OLD_LOAD; |
|
1189 |
||
1190 |
f = fopen(file, "rb"); |
|
1191 |
if (fread(&hdr, sizeof(hdr), 1, f) != 1) { |
|
1192 |
printf("Savegame is obsolete or invalid format.\n"); |
|
1193 |
mode = SL_LOAD; // don't try to get filename, just show name as it is written |
|
1194 |
} |
|
1195 |
else { |
|
1196 |
// see if we have any loader for this type. |
|
1197 |
for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) { |
|
1198 |
if (fmt->tag == hdr) { |
|
1199 |
mode = SL_LOAD; // new type of savegame |
|
1200 |
break; |
|
1201 |
} |
|
1202 |
} |
|
1203 |
} |
|
1204 |
||
1205 |
fclose(f); |
|
1206 |
return mode; |
|
1207 |
}*/ |