OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

saveload.cpp
Go to the documentation of this file.
1 /* $Id: saveload.cpp 27518 2016-03-01 20:00:22Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
24 #include "../stdafx.h"
25 #include "../debug.h"
26 #include "../station_base.h"
27 #include "../thread/thread.h"
28 #include "../town.h"
29 #include "../network/network.h"
30 #include "../window_func.h"
31 #include "../strings_func.h"
32 #include "../core/endian_func.hpp"
33 #include "../vehicle_base.h"
34 #include "../company_func.h"
35 #include "../date_func.h"
36 #include "../autoreplace_base.h"
37 #include "../roadstop_base.h"
38 #include "../linkgraph/linkgraph.h"
39 #include "../linkgraph/linkgraphjob.h"
40 #include "../statusbar_gui.h"
41 #include "../fileio_func.h"
42 #include "../gamelog.h"
43 #include "../string_func.h"
44 #include "../fios.h"
45 #include "../error.h"
46
47 #include "table/strings.h"
48
49 #include "saveload_internal.h"
50 #include "saveload_filter.h"
51
52 #include "../safeguards.h"
53
54 /*
55 * Previous savegame versions, the trunk revision where they were
56 * introduced and the released version that had that particular
57 * savegame version.
58 * Up to savegame version 18 there is a minor version as well.
59 *
60 * 1.0 0.1.x, 0.2.x
61 * 2.0 0.3.0
62 * 2.1 0.3.1, 0.3.2
63 * 3.x lost
64 * 4.0 1
65 * 4.1 122 0.3.3, 0.3.4
66 * 4.2 1222 0.3.5
67 * 4.3 1417
68 * 4.4 1426
69 * 5.0 1429
70 * 5.1 1440
71 * 5.2 1525 0.3.6
72 * 6.0 1721
73 * 6.1 1768
74 * 7.0 1770
75 * 8.0 1786
76 * 9.0 1909
77 * 10.0 2030
78 * 11.0 2033
79 * 11.1 2041
80 * 12.1 2046
81 * 13.1 2080 0.4.0, 0.4.0.1
82 * 14.0 2441
83 * 15.0 2499
84 * 16.0 2817
85 * 16.1 3155
86 * 17.0 3212
87 * 17.1 3218
88 * 18 3227
89 * 19 3396
90 * 20 3403
91 * 21 3472 0.4.x
92 * 22 3726
93 * 23 3915
94 * 24 4150
95 * 25 4259
96 * 26 4466
97 * 27 4757
98 * 28 4987
99 * 29 5070
100 * 30 5946
101 * 31 5999
102 * 32 6001
103 * 33 6440
104 * 34 6455
105 * 35 6602
106 * 36 6624
107 * 37 7182
108 * 38 7195
109 * 39 7269
110 * 40 7326
111 * 41 7348 0.5.x
112 * 42 7573
113 * 43 7642
114 * 44 8144
115 * 45 8501
116 * 46 8705
117 * 47 8735
118 * 48 8935
119 * 49 8969
120 * 50 8973
121 * 51 8978
122 * 52 9066
123 * 53 9316
124 * 54 9613
125 * 55 9638
126 * 56 9667
127 * 57 9691
128 * 58 9762
129 * 59 9779
130 * 60 9874
131 * 61 9892
132 * 62 9905
133 * 63 9956
134 * 64 10006
135 * 65 10210
136 * 66 10211
137 * 67 10236
138 * 68 10266
139 * 69 10319
140 * 70 10541
141 * 71 10567
142 * 72 10601
143 * 73 10903
144 * 74 11030
145 * 75 11107
146 * 76 11139
147 * 77 11172
148 * 78 11176
149 * 79 11188
150 * 80 11228
151 * 81 11244
152 * 82 11410
153 * 83 11589
154 * 84 11822
155 * 85 11874
156 * 86 12042
157 * 87 12129
158 * 88 12134
159 * 89 12160
160 * 90 12293
161 * 91 12347
162 * 92 12381 0.6.x
163 * 93 12648
164 * 94 12816
165 * 95 12924
166 * 96 13226
167 * 97 13256
168 * 98 13375
169 * 99 13838
170 * 100 13952
171 * 101 14233
172 * 102 14332
173 * 103 14598
174 * 104 14735
175 * 105 14803
176 * 106 14919
177 * 107 15027
178 * 108 15045
179 * 109 15075
180 * 110 15148
181 * 111 15190
182 * 112 15290
183 * 113 15340
184 * 114 15601
185 * 115 15695
186 * 116 15893 0.7.x
187 * 117 16037
188 * 118 16129
189 * 119 16242
190 * 120 16439
191 * 121 16694
192 * 122 16855
193 * 123 16909
194 * 124 16993
195 * 125 17113
196 * 126 17433
197 * 127 17439
198 * 128 18281
199 * 129 18292
200 * 130 18404
201 * 131 18481
202 * 132 18522
203 * 133 18674
204 * 134 18703
205 * 135 18719
206 * 136 18764
207 * 137 18912
208 * 138 18942 1.0.x
209 * 139 19346
210 * 140 19382
211 * 141 19799
212 * 142 20003
213 * 143 20048
214 * 144 20334
215 * 145 20376
216 * 146 20446
217 * 147 20621
218 * 148 20659
219 * 149 20832
220 * 150 20857
221 * 151 20918
222 * 152 21171
223 * 153 21263
224 * 154 21426
225 * 155 21453
226 * 156 21728
227 * 157 21862
228 * 158 21933
229 * 159 21962
230 * 160 21974 1.1.x
231 * 161 22567
232 * 162 22713
233 * 163 22767
234 * 164 23290
235 * 165 23304
236 * 166 23415
237 * 167 23504
238 * 168 23637
239 * 169 23816
240 * 170 23826
241 * 171 23835
242 * 172 23947
243 * 173 23967 1.2.0-RC1
244 * 174 23973 1.2.x
245 * 175 24136
246 * 176 24446
247 * 177 24619
248 * 178 24789
249 * 179 24810
250 * 180 24998 1.3.x
251 * 181 25012
252 * 182 25296
253 * 183 25363
254 * 184 25508
255 * 185 25620
256 * 186 25833
257 * 187 25899
258 * 188 26169 1.4.x
259 * 189 26450
260 * 190 26547
261 * 191 26646
262 * 192 26700
263 * 193 26802
264 * 194 26881 1.5.x, 1.6.x
265 */
266 extern const uint16 SAVEGAME_VERSION = 194;
267
268 SavegameType _savegame_type;
269
270 uint32 _ttdp_version;
271 uint16 _sl_version;
272 byte _sl_minor_version;
273 char _savegame_format[8];
274 bool _do_autosave;
275
277 enum SaveLoadAction {
278 SLA_LOAD,
279 SLA_SAVE,
280 SLA_PTRS,
281 SLA_NULL,
282 SLA_LOAD_CHECK,
283 };
284
285 enum NeedLength {
286 NL_NONE = 0,
287 NL_WANTLENGTH = 1,
288 NL_CALCLENGTH = 2,
289 };
290
292 static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
293
295 struct ReadBuffer {
296 byte buf[MEMORY_CHUNK_SIZE];
297 byte *bufp;
298 byte *bufe;
299 LoadFilter *reader;
300 size_t read;
301
306 ReadBuffer(LoadFilter *reader) : bufp(NULL), bufe(NULL), reader(reader), read(0)
307 {
308 }
309
310 inline byte ReadByte()
311 {
312 if (this->bufp == this->bufe) {
313 size_t len = this->reader->Read(this->buf, lengthof(this->buf));
314 if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
315
316 this->read += len;
317 this->bufp = this->buf;
318 this->bufe = this->buf + len;
319 }
320
321 return *this->bufp++;
322 }
323
328 size_t GetSize() const
329 {
330 return this->read - (this->bufe - this->bufp);
331 }
332 };
333
334
336 struct MemoryDumper {
337 AutoFreeSmallVector<byte *, 16> blocks;
338 byte *buf;
339 byte *bufe;
340
342 MemoryDumper() : buf(NULL), bufe(NULL)
343 {
344 }
345
350 inline void WriteByte(byte b)
351 {
352 /* Are we at the end of this chunk? */
353 if (this->buf == this->bufe) {
354 this->buf = CallocT<byte>(MEMORY_CHUNK_SIZE);
355 *this->blocks.Append() = this->buf;
356 this->bufe = this->buf + MEMORY_CHUNK_SIZE;
357 }
358
359 *this->buf++ = b;
360 }
361
366 void Flush(SaveFilter *writer)
367 {
368 uint i = 0;
369 size_t t = this->GetSize();
370
371 while (t > 0) {
372 size_t to_write = min(MEMORY_CHUNK_SIZE, t);
373
374 writer->Write(this->blocks[i++], to_write);
375 t -= to_write;
376 }
377
378 writer->Finish();
379 }
380
385 size_t GetSize() const
386 {
387 return this->blocks.Length() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
388 }
389 };
390
392 struct SaveLoadParams {
393 SaveLoadAction action;
394 NeedLength need_length;
395 byte block_mode;
396 bool error;
397
398 size_t obj_len;
399 int array_index, last_array_index;
400
401 MemoryDumper *dumper;
402 SaveFilter *sf;
403
404 ReadBuffer *reader;
405 LoadFilter *lf;
406
407 StringID error_str;
408 char *extra_msg;
409
410 byte ff_state;
411 bool saveinprogress;
412 };
413
414 static SaveLoadParams _sl;
415
416 /* these define the chunks */
417 extern const ChunkHandler _gamelog_chunk_handlers[];
418 extern const ChunkHandler _map_chunk_handlers[];
419 extern const ChunkHandler _misc_chunk_handlers[];
420 extern const ChunkHandler _name_chunk_handlers[];
421 extern const ChunkHandler _cheat_chunk_handlers[] ;
422 extern const ChunkHandler _setting_chunk_handlers[];
423 extern const ChunkHandler _company_chunk_handlers[];
424 extern const ChunkHandler _engine_chunk_handlers[];
425 extern const ChunkHandler _veh_chunk_handlers[];
426 extern const ChunkHandler _waypoint_chunk_handlers[];
427 extern const ChunkHandler _depot_chunk_handlers[];
428 extern const ChunkHandler _order_chunk_handlers[];
429 extern const ChunkHandler _town_chunk_handlers[];
430 extern const ChunkHandler _sign_chunk_handlers[];
431 extern const ChunkHandler _station_chunk_handlers[];
432 extern const ChunkHandler _industry_chunk_handlers[];
433 extern const ChunkHandler _economy_chunk_handlers[];
434 extern const ChunkHandler _subsidy_chunk_handlers[];
435 extern const ChunkHandler _cargomonitor_chunk_handlers[];
436 extern const ChunkHandler _goal_chunk_handlers[];
437 extern const ChunkHandler _story_page_chunk_handlers[];
438 extern const ChunkHandler _ai_chunk_handlers[];
439 extern const ChunkHandler _game_chunk_handlers[];
440 extern const ChunkHandler _animated_tile_chunk_handlers[];
441 extern const ChunkHandler _newgrf_chunk_handlers[];
442 extern const ChunkHandler _group_chunk_handlers[];
443 extern const ChunkHandler _cargopacket_chunk_handlers[];
444 extern const ChunkHandler _autoreplace_chunk_handlers[];
445 extern const ChunkHandler _labelmaps_chunk_handlers[];
446 extern const ChunkHandler _linkgraph_chunk_handlers[];
447 extern const ChunkHandler _airport_chunk_handlers[];
448 extern const ChunkHandler _object_chunk_handlers[];
449 extern const ChunkHandler _persistent_storage_chunk_handlers[];
450
452 static const ChunkHandler * const _chunk_handlers[] = {
453 _gamelog_chunk_handlers,
454 _map_chunk_handlers,
455 _misc_chunk_handlers,
456 _name_chunk_handlers,
457 _cheat_chunk_handlers,
458 _setting_chunk_handlers,
459 _veh_chunk_handlers,
460 _waypoint_chunk_handlers,
461 _depot_chunk_handlers,
462 _order_chunk_handlers,
463 _industry_chunk_handlers,
464 _economy_chunk_handlers,
465 _subsidy_chunk_handlers,
466 _cargomonitor_chunk_handlers,
467 _goal_chunk_handlers,
468 _story_page_chunk_handlers,
469 _engine_chunk_handlers,
470 _town_chunk_handlers,
471 _sign_chunk_handlers,
472 _station_chunk_handlers,
473 _company_chunk_handlers,
474 _ai_chunk_handlers,
475 _game_chunk_handlers,
476 _animated_tile_chunk_handlers,
477 _newgrf_chunk_handlers,
478 _group_chunk_handlers,
479 _cargopacket_chunk_handlers,
480 _autoreplace_chunk_handlers,
481 _labelmaps_chunk_handlers,
482 _linkgraph_chunk_handlers,
483 _airport_chunk_handlers,
484 _object_chunk_handlers,
485 _persistent_storage_chunk_handlers,
486 NULL,
487 };
488
493 #define FOR_ALL_CHUNK_HANDLERS(ch) \
494 for (const ChunkHandler * const *chsc = _chunk_handlers; *chsc != NULL; chsc++) \
495 for (const ChunkHandler *ch = *chsc; ch != NULL; ch = (ch->flags & CH_LAST) ? NULL : ch + 1)
496
498 static void SlNullPointers()
499 {
500 _sl.action = SLA_NULL;
501
502 /* We don't want any savegame conversion code to run
503 * during NULLing; especially those that try to get
504 * pointers from other pools. */
505 _sl_version = SAVEGAME_VERSION;
506
507 DEBUG(sl, 1, "Nulling pointers");
508
509 FOR_ALL_CHUNK_HANDLERS(ch) {
510 if (ch->ptrs_proc != NULL) {
511 DEBUG(sl, 2, "Nulling pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
512 ch->ptrs_proc();
513 }
514 }
515
516 DEBUG(sl, 1, "All pointers nulled");
517
518 assert(_sl.action == SLA_NULL);
519 }
520
529 void NORETURN SlError(StringID string, const char *extra_msg)
530 {
531 /* Distinguish between loading into _load_check_data vs. normal save/load. */
532 if (_sl.action == SLA_LOAD_CHECK) {
533 _load_check_data.error = string;
534 free(_load_check_data.error_data);
535 _load_check_data.error_data = (extra_msg == NULL) ? NULL : stredup(extra_msg);
536 } else {
537 _sl.error_str = string;
538 free(_sl.extra_msg);
539 _sl.extra_msg = (extra_msg == NULL) ? NULL : stredup(extra_msg);
540 }
541
542 /* We have to NULL all pointers here; we might be in a state where
543 * the pointers are actually filled with indices, which means that
544 * when we access them during cleaning the pool dereferences of
545 * those indices will be made with segmentation faults as result. */
546 if (_sl.action == SLA_LOAD || _sl.action == SLA_PTRS) SlNullPointers();
547 throw std::exception();
548 }
549
557 void NORETURN SlErrorCorrupt(const char *msg)
558 {
559 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
560 }
561
562
563 typedef void (*AsyncSaveFinishProc)();
564 static AsyncSaveFinishProc _async_save_finish = NULL;
565 static ThreadObject *_save_thread;
566
571 static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
572 {
573 if (_exit_game) return;
574 while (_async_save_finish != NULL) CSleep(10);
575
576 _async_save_finish = proc;
577 }
578
582 void ProcessAsyncSaveFinish()
583 {
584 if (_async_save_finish == NULL) return;
585
586 _async_save_finish();
587
588 _async_save_finish = NULL;
589
590 if (_save_thread != NULL) {
591 _save_thread->Join();
592 delete _save_thread;
593 _save_thread = NULL;
594 }
595 }
596
601 byte SlReadByte()
602 {
603 return _sl.reader->ReadByte();
604 }
605
610 void SlWriteByte(byte b)
611 {
612 _sl.dumper->WriteByte(b);
613 }
614
615 static inline int SlReadUint16()
616 {
617 int x = SlReadByte() << 8;
618 return x | SlReadByte();
619 }
620
621 static inline uint32 SlReadUint32()
622 {
623 uint32 x = SlReadUint16() << 16;
624 return x | SlReadUint16();
625 }
626
627 static inline uint64 SlReadUint64()
628 {
629 uint32 x = SlReadUint32();
630 uint32 y = SlReadUint32();
631 return (uint64)x << 32 | y;
632 }
633
634 static inline void SlWriteUint16(uint16 v)
635 {
636 SlWriteByte(GB(v, 8, 8));
637 SlWriteByte(GB(v, 0, 8));
638 }
639
640 static inline void SlWriteUint32(uint32 v)
641 {
642 SlWriteUint16(GB(v, 16, 16));
643 SlWriteUint16(GB(v, 0, 16));
644 }
645
646 static inline void SlWriteUint64(uint64 x)
647 {
648 SlWriteUint32((uint32)(x >> 32));
649 SlWriteUint32((uint32)x);
650 }
651
657 static inline void SlSkipBytes(size_t length)
658 {
659 for (; length != 0; length--) SlReadByte();
660 }
661
671 static uint SlReadSimpleGamma()
672 {
673 uint i = SlReadByte();
674 if (HasBit(i, 7)) {
675 i &= ~0x80;
676 if (HasBit(i, 6)) {
677 i &= ~0x40;
678 if (HasBit(i, 5)) {
679 i &= ~0x20;
680 if (HasBit(i, 4)) {
681 i &= ~0x10;
682 if (HasBit(i, 3)) {
683 SlErrorCorrupt("Unsupported gamma");
684 }
685 i = SlReadByte(); // 32 bits only.
686 }
687 i = (i << 8) | SlReadByte();
688 }
689 i = (i << 8) | SlReadByte();
690 }
691 i = (i << 8) | SlReadByte();
692 }
693 return i;
694 }
695
713 static void SlWriteSimpleGamma(size_t i)
714 {
715 if (i >= (1 << 7)) {
716 if (i >= (1 << 14)) {
717 if (i >= (1 << 21)) {
718 if (i >= (1 << 28)) {
719 assert(i <= UINT32_MAX); // We can only support 32 bits for now.
720 SlWriteByte((byte)(0xF0));
721 SlWriteByte((byte)(i >> 24));
722 } else {
723 SlWriteByte((byte)(0xE0 | (i >> 24)));
724 }
725 SlWriteByte((byte)(i >> 16));
726 } else {
727 SlWriteByte((byte)(0xC0 | (i >> 16)));
728 }
729 SlWriteByte((byte)(i >> 8));
730 } else {
731 SlWriteByte((byte)(0x80 | (i >> 8)));
732 }
733 }
734 SlWriteByte((byte)i);
735 }
736
738 static inline uint SlGetGammaLength(size_t i)
739 {
740 return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
741 }
742
743 static inline uint SlReadSparseIndex()
744 {
745 return SlReadSimpleGamma();
746 }
747
748 static inline void SlWriteSparseIndex(uint index)
749 {
750 SlWriteSimpleGamma(index);
751 }
752
753 static inline uint SlReadArrayLength()
754 {
755 return SlReadSimpleGamma();
756 }
757
758 static inline void SlWriteArrayLength(size_t length)
759 {
760 SlWriteSimpleGamma(length);
761 }
762
763 static inline uint SlGetArrayLength(size_t length)
764 {
765 return SlGetGammaLength(length);
766 }
767
774 static inline uint SlCalcConvMemLen(VarType conv)
775 {
776 static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
777 byte length = GB(conv, 4, 4);
778
779 switch (length << 4) {
780 case SLE_VAR_STRB:
781 case SLE_VAR_STRBQ:
782 case SLE_VAR_STR:
783 case SLE_VAR_STRQ:
784 return SlReadArrayLength();
785
786 default:
787 assert(length < lengthof(conv_mem_size));
788 return conv_mem_size[length];
789 }
790 }
791
798 static inline byte SlCalcConvFileLen(VarType conv)
799 {
800 static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
801 byte length = GB(conv, 0, 4);
802 assert(length < lengthof(conv_file_size));
803 return conv_file_size[length];
804 }
805
807 static inline size_t SlCalcRefLen()
808 {
809 return IsSavegameVersionBefore(69) ? 2 : 4;
810 }
811
812 void SlSetArrayIndex(uint index)
813 {
814 _sl.need_length = NL_WANTLENGTH;
815 _sl.array_index = index;
816 }
817
818 static size_t _next_offs;
819
824 int SlIterateArray()
825 {
826 int index;
827
828 /* After reading in the whole array inside the loop
829 * we must have read in all the data, so we must be at end of current block. */
830 if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) SlErrorCorrupt("Invalid chunk size");
831
832 for (;;) {
833 uint length = SlReadArrayLength();
834 if (length == 0) {
835 _next_offs = 0;
836 return -1;
837 }
838
839 _sl.obj_len = --length;
840 _next_offs = _sl.reader->GetSize() + length;
841
842 switch (_sl.block_mode) {
843 case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
844 case CH_ARRAY: index = _sl.array_index++; break;
845 default:
846 DEBUG(sl, 0, "SlIterateArray error");
847 return -1; // error
848 }
849
850 if (length != 0) return index;
851 }
852 }
853
857 void SlSkipArray()
858 {
859 while (SlIterateArray() != -1) {
860 SlSkipBytes(_next_offs - _sl.reader->GetSize());
861 }
862 }
863
869 void SlSetLength(size_t length)
870 {
871 assert(_sl.action == SLA_SAVE);
872
873 switch (_sl.need_length) {
874 case NL_WANTLENGTH:
875 _sl.need_length = NL_NONE;
876 switch (_sl.block_mode) {
877 case CH_RIFF:
878 /* Ugly encoding of >16M RIFF chunks
879 * The lower 24 bits are normal
880 * The uppermost 4 bits are bits 24:27 */
881 assert(length < (1 << 28));
882 SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
883 break;
884 case CH_ARRAY:
885 assert(_sl.last_array_index <= _sl.array_index);
886 while (++_sl.last_array_index <= _sl.array_index) {
887 SlWriteArrayLength(1);
888 }
889 SlWriteArrayLength(length + 1);
890 break;
891 case CH_SPARSE_ARRAY:
892 SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
893 SlWriteSparseIndex(_sl.array_index);
894 break;
895 default: NOT_REACHED();
896 }
897 break;
898
899 case NL_CALCLENGTH:
900 _sl.obj_len += (int)length;
901 break;
902
903 default: NOT_REACHED();
904 }
905 }
906
913 static void SlCopyBytes(void *ptr, size_t length)
914 {
915 byte *p = (byte *)ptr;
916
917 switch (_sl.action) {
918 case SLA_LOAD_CHECK:
919 case SLA_LOAD:
920 for (; length != 0; length--) *p++ = SlReadByte();
921 break;
922 case SLA_SAVE:
923 for (; length != 0; length--) SlWriteByte(*p++);
924 break;
925 default: NOT_REACHED();
926 }
927 }
928
930 size_t SlGetFieldLength()
931 {
932 return _sl.obj_len;
933 }
934
942 int64 ReadValue(const void *ptr, VarType conv)
943 {
944 switch (GetVarMemType(conv)) {
945 case SLE_VAR_BL: return (*(const bool *)ptr != 0);
946 case SLE_VAR_I8: return *(const int8 *)ptr;
947 case SLE_VAR_U8: return *(const byte *)ptr;
948 case SLE_VAR_I16: return *(const int16 *)ptr;
949 case SLE_VAR_U16: return *(const uint16*)ptr;
950 case SLE_VAR_I32: return *(const int32 *)ptr;
951 case SLE_VAR_U32: return *(const uint32*)ptr;
952 case SLE_VAR_I64: return *(const int64 *)ptr;
953 case SLE_VAR_U64: return *(const uint64*)ptr;
954 case SLE_VAR_NULL:return 0;
955 default: NOT_REACHED();
956 }
957 }
958
966 void WriteValue(void *ptr, VarType conv, int64 val)
967 {
968 switch (GetVarMemType(conv)) {
969 case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
970 case SLE_VAR_I8: *(int8 *)ptr = val; break;
971 case SLE_VAR_U8: *(byte *)ptr = val; break;
972 case SLE_VAR_I16: *(int16 *)ptr = val; break;
973 case SLE_VAR_U16: *(uint16*)ptr = val; break;
974 case SLE_VAR_I32: *(int32 *)ptr = val; break;
975 case SLE_VAR_U32: *(uint32*)ptr = val; break;
976 case SLE_VAR_I64: *(int64 *)ptr = val; break;
977 case SLE_VAR_U64: *(uint64*)ptr = val; break;
978 case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
979 case SLE_VAR_NULL: break;
980 default: NOT_REACHED();
981 }
982 }
983
992 static void SlSaveLoadConv(void *ptr, VarType conv)
993 {
994 switch (_sl.action) {
995 case SLA_SAVE: {
996 int64 x = ReadValue(ptr, conv);
997
998 /* Write the value to the file and check if its value is in the desired range */
999 switch (GetVarFileType(conv)) {
1000 case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
1001 case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
1002 case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
1003 case SLE_FILE_STRINGID:
1004 case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
1005 case SLE_FILE_I32:
1006 case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
1007 case SLE_FILE_I64:
1008 case SLE_FILE_U64: SlWriteUint64(x);break;
1009 default: NOT_REACHED();
1010 }
1011 break;
1012 }
1013 case SLA_LOAD_CHECK:
1014 case SLA_LOAD: {
1015 int64 x;
1016 /* Read a value from the file */
1017 switch (GetVarFileType(conv)) {
1018 case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
1019 case SLE_FILE_U8: x = (byte )SlReadByte(); break;
1020 case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
1021 case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
1022 case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
1023 case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
1024 case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
1025 case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
1026 case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
1027 default: NOT_REACHED();
1028 }
1029
1030 /* Write The value to the struct. These ARE endian safe. */
1031 WriteValue(ptr, conv, x);
1032 break;
1033 }
1034 case SLA_PTRS: break;
1035 case SLA_NULL: break;
1036 default: NOT_REACHED();
1037 }
1038 }
1039
1049 static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
1050 {
1051 if (ptr == NULL) return 0;
1052 return min(strlen(ptr), length - 1);
1053 }
1054
1064 static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
1065 {
1066 size_t len;
1067 const char *str;
1068
1069 switch (GetVarMemType(conv)) {
1070 default: NOT_REACHED();
1071 case SLE_VAR_STR:
1072 case SLE_VAR_STRQ:
1073 str = *(const char * const *)ptr;
1074 len = SIZE_MAX;
1075 break;
1076 case SLE_VAR_STRB:
1077 case SLE_VAR_STRBQ:
1078 str = (const char *)ptr;
1079 len = length;
1080 break;
1081 }
1082
1083 len = SlCalcNetStringLen(str, len);
1084 return len + SlGetArrayLength(len); // also include the length of the index
1085 }
1086
1093 static void SlString(void *ptr, size_t length, VarType conv)
1094 {
1095 switch (_sl.action) {
1096 case SLA_SAVE: {
1097 size_t len;
1098 switch (GetVarMemType(conv)) {
1099 default: NOT_REACHED();
1100 case SLE_VAR_STRB:
1101 case SLE_VAR_STRBQ:
1102 len = SlCalcNetStringLen((char *)ptr, length);
1103 break;
1104 case SLE_VAR_STR:
1105 case SLE_VAR_STRQ:
1106 ptr = *(char **)ptr;
1107 len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
1108 break;
1109 }
1110
1111 SlWriteArrayLength(len);
1112 SlCopyBytes(ptr, len);
1113 break;
1114 }
1115 case SLA_LOAD_CHECK:
1116 case SLA_LOAD: {
1117 size_t len = SlReadArrayLength();
1118
1119 switch (GetVarMemType(conv)) {
1120 default: NOT_REACHED();
1121 case SLE_VAR_STRB:
1122 case SLE_VAR_STRBQ:
1123 if (len >= length) {
1124 DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
1125 SlCopyBytes(ptr, length);
1126 SlSkipBytes(len - length);
1127 len = length - 1;
1128 } else {
1129 SlCopyBytes(ptr, len);
1130 }
1131 break;
1132 case SLE_VAR_STR:
1133 case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
1134 free(*(char **)ptr);
1135 if (len == 0) {
1136 *(char **)ptr = NULL;
1137 return;
1138 } else {
1139 *(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
1140 ptr = *(char **)ptr;
1141 SlCopyBytes(ptr, len);
1142 }
1143 break;
1144 }
1145
1146 ((char *)ptr)[len] = '\0'; // properly terminate the string
1147 StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK;
1148 if ((conv & SLF_ALLOW_CONTROL) != 0) {
1149 settings = settings | SVS_ALLOW_CONTROL_CODE;
1150 if (IsSavegameVersionBefore(169)) {
1151 str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
1152 }
1153 }
1154 if ((conv & SLF_ALLOW_NEWLINE) != 0) {
1155 settings = settings | SVS_ALLOW_NEWLINE;
1156 }
1157 str_validate((char *)ptr, (char *)ptr + len, settings);
1158 break;
1159 }
1160 case SLA_PTRS: break;
1161 case SLA_NULL: break;
1162 default: NOT_REACHED();
1163 }
1164 }
1165
1171 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
1172 {
1173 return SlCalcConvFileLen(conv) * length;
1174 }
1175
1182 void SlArray(void *array, size_t length, VarType conv)
1183 {
1184 if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
1185
1186 /* Automatically calculate the length? */
1187 if (_sl.need_length != NL_NONE) {
1188 SlSetLength(SlCalcArrayLen(length, conv));
1189 /* Determine length only? */
1190 if (_sl.need_length == NL_CALCLENGTH) return;
1191 }
1192
1193 /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1194 * as a byte-type. So detect this, and adjust array size accordingly */
1195 if (_sl.action != SLA_SAVE && _sl_version == 0) {
1196 /* all arrays except difficulty settings */
1197 if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1198 conv == SLE_INT32 || conv == SLE_UINT32) {
1199 SlCopyBytes(array, length * SlCalcConvFileLen(conv));
1200 return;
1201 }
1202 /* used for conversion of Money 32bit->64bit */
1203 if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1204 for (uint i = 0; i < length; i++) {
1205 ((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
1206 }
1207 return;
1208 }
1209 }
1210
1211 /* If the size of elements is 1 byte both in file and memory, no special
1212 * conversion is needed, use specialized copy-copy function to speed up things */
1213 if (conv == SLE_INT8 || conv == SLE_UINT8) {
1214 SlCopyBytes(array, length);
1215 } else {
1216 byte *a = (byte*)array;
1217 byte mem_size = SlCalcConvMemLen(conv);
1218
1219 for (; length != 0; length --) {
1220 SlSaveLoadConv(a, conv);
1221 a += mem_size; // get size
1222 }
1223 }
1224 }
1225
1226
1237 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1238 {
1239 assert(_sl.action == SLA_SAVE);
1240
1241 if (obj == NULL) return 0;
1242
1243 switch (rt) {
1244 case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1245 case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1246 case REF_STATION: return ((const Station*)obj)->index + 1;
1247 case REF_TOWN: return ((const Town*)obj)->index + 1;
1248 case REF_ORDER: return ((const Order*)obj)->index + 1;
1249 case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1250 case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1251 case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1252 case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1253 case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1254 case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1255 case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1256 default: NOT_REACHED();
1257 }
1258 }
1259
1270 static void *IntToReference(size_t index, SLRefType rt)
1271 {
1272 assert_compile(sizeof(size_t) <= sizeof(void *));
1273
1274 assert(_sl.action == SLA_PTRS);
1275
1276 /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1277 * and should be loaded like that */
1278 if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(4, 4)) {
1279 rt = REF_VEHICLE;
1280 }
1281
1282 /* No need to look up NULL pointers, just return immediately */
1283 if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return NULL;
1284
1285 /* Correct index. Old vehicles were saved differently:
1286 * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1287 if (rt != REF_VEHICLE_OLD) index--;
1288
1289 switch (rt) {
1290 case REF_ORDERLIST:
1291 if (OrderList::IsValidID(index)) return OrderList::Get(index);
1292 SlErrorCorrupt("Referencing invalid OrderList");
1293
1294 case REF_ORDER:
1295 if (Order::IsValidID(index)) return Order::Get(index);
1296 /* in old versions, invalid order was used to mark end of order list */
1297 if (IsSavegameVersionBefore(5, 2)) return NULL;
1298 SlErrorCorrupt("Referencing invalid Order");
1299
1300 case REF_VEHICLE_OLD:
1301 case REF_VEHICLE:
1302 if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1303 SlErrorCorrupt("Referencing invalid Vehicle");
1304
1305 case REF_STATION:
1306 if (Station::IsValidID(index)) return Station::Get(index);
1307 SlErrorCorrupt("Referencing invalid Station");
1308
1309 case REF_TOWN:
1310 if (Town::IsValidID(index)) return Town::Get(index);
1311 SlErrorCorrupt("Referencing invalid Town");
1312
1313 case REF_ROADSTOPS:
1314 if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1315 SlErrorCorrupt("Referencing invalid RoadStop");
1316
1317 case REF_ENGINE_RENEWS:
1318 if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1319 SlErrorCorrupt("Referencing invalid EngineRenew");
1320
1321 case REF_CARGO_PACKET:
1322 if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1323 SlErrorCorrupt("Referencing invalid CargoPacket");
1324
1325 case REF_STORAGE:
1326 if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1327 SlErrorCorrupt("Referencing invalid PersistentStorage");
1328
1329 case REF_LINK_GRAPH:
1330 if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1331 SlErrorCorrupt("Referencing invalid LinkGraph");
1332
1333 case REF_LINK_GRAPH_JOB:
1334 if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1335 SlErrorCorrupt("Referencing invalid LinkGraphJob");
1336
1337 default: NOT_REACHED();
1338 }
1339 }
1340
1345 static inline size_t SlCalcListLen(const void *list)
1346 {
1347 const std::list<void *> *l = (const std::list<void *> *) list;
1348
1349 int type_size = IsSavegameVersionBefore(69) ? 2 : 4;
1350 /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1351 * of the list */
1352 return l->size() * type_size + type_size;
1353 }
1354
1355
1361 static void SlList(void *list, SLRefType conv)
1362 {
1363 /* Automatically calculate the length? */
1364 if (_sl.need_length != NL_NONE) {
1365 SlSetLength(SlCalcListLen(list));
1366 /* Determine length only? */
1367 if (_sl.need_length == NL_CALCLENGTH) return;
1368 }
1369
1370 typedef std::list<void *> PtrList;
1371 PtrList *l = (PtrList *)list;
1372
1373 switch (_sl.action) {
1374 case SLA_SAVE: {
1375 SlWriteUint32((uint32)l->size());
1376
1377 PtrList::iterator iter;
1378 for (iter = l->begin(); iter != l->end(); ++iter) {
1379 void *ptr = *iter;
1380 SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
1381 }
1382 break;
1383 }
1384 case SLA_LOAD_CHECK:
1385 case SLA_LOAD: {
1386 size_t length = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1387
1388 /* Load each reference and push to the end of the list */
1389 for (size_t i = 0; i < length; i++) {
1390 size_t data = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1391 l->push_back((void *)data);
1392 }
1393 break;
1394 }
1395 case SLA_PTRS: {
1396 PtrList temp = *l;
1397
1398 l->clear();
1399 PtrList::iterator iter;
1400 for (iter = temp.begin(); iter != temp.end(); ++iter) {
1401 void *ptr = IntToReference((size_t)*iter, conv);
1402 l->push_back(ptr);
1403 }
1404 break;
1405 }
1406 case SLA_NULL:
1407 l->clear();
1408 break;
1409 default: NOT_REACHED();
1410 }
1411 }
1412
1413
1415 static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
1416 {
1417 if (_sl_version < sld->version_from || _sl_version > sld->version_to) return false;
1418 if (sld->conv & SLF_NOT_IN_SAVE) return false;
1419
1420 return true;
1421 }
1422
1428 static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
1429 {
1430 if ((sld->conv & SLF_NO_NETWORK_SYNC) && _sl.action != SLA_SAVE && _networking && !_network_server) {
1431 SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
1432 return true;
1433 }
1434
1435 return false;
1436 }
1437
1444 size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
1445 {
1446 size_t length = 0;
1447
1448 /* Need to determine the length and write a length tag. */
1449 for (; sld->cmd != SL_END; sld++) {
1450 length += SlCalcObjMemberLength(object, sld);
1451 }
1452 return length;
1453 }
1454
1455 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
1456 {
1457 assert(_sl.action == SLA_SAVE);
1458
1459 switch (sld->cmd) {
1460 case SL_VAR:
1461 case SL_REF:
1462 case SL_ARR:
1463 case SL_STR:
1464 case SL_LST:
1465 /* CONDITIONAL saveload types depend on the savegame version */
1466 if (!SlIsObjectValidInSavegame(sld)) break;
1467
1468 switch (sld->cmd) {
1469 case SL_VAR: return SlCalcConvFileLen(sld->conv);
1470 case SL_REF: return SlCalcRefLen();
1471 case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
1472 case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
1473 case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
1474 default: NOT_REACHED();
1475 }
1476 break;
1477 case SL_WRITEBYTE: return 1; // a byte is logically of size 1
1478 case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
1479 case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
1480 default: NOT_REACHED();
1481 }
1482 return 0;
1483 }
1484
1490 static bool IsVariableSizeRight(const SaveLoad *sld)
1491 {
1492 switch (sld->cmd) {
1493 case SL_VAR:
1494 switch (GetVarMemType(sld->conv)) {
1495 case SLE_VAR_BL:
1496 return sld->size == sizeof(bool);
1497 case SLE_VAR_I8:
1498 case SLE_VAR_U8:
1499 return sld->size == sizeof(int8);
1500 case SLE_VAR_I16:
1501 case SLE_VAR_U16:
1502 return sld->size == sizeof(int16);
1503 case SLE_VAR_I32:
1504 case SLE_VAR_U32:
1505 return sld->size == sizeof(int32);
1506 case SLE_VAR_I64:
1507 case SLE_VAR_U64:
1508 return sld->size == sizeof(int64);
1509 default:
1510 return sld->size == sizeof(void *);
1511 }
1512 case SL_REF:
1513 /* These should all be pointer sized. */
1514 return sld->size == sizeof(void *);
1515
1516 case SL_STR:
1517 /* These should be pointer sized, or fixed array. */
1518 return sld->size == sizeof(void *) || sld->size == sld->length;
1519
1520 default:
1521 return true;
1522 }
1523 }
1524
1525 bool SlObjectMember(void *ptr, const SaveLoad *sld)
1526 {
1527 assert(IsVariableSizeRight(sld));
1528
1529 VarType conv = GB(sld->conv, 0, 8);
1530 switch (sld->cmd) {
1531 case SL_VAR:
1532 case SL_REF:
1533 case SL_ARR:
1534 case SL_STR:
1535 case SL_LST:
1536 /* CONDITIONAL saveload types depend on the savegame version */
1537 if (!SlIsObjectValidInSavegame(sld)) return false;
1538 if (SlSkipVariableOnLoad(sld)) return false;
1539
1540 switch (sld->cmd) {
1541 case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1542 case SL_REF: // Reference variable, translate
1543 switch (_sl.action) {
1544 case SLA_SAVE:
1545 SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1546 break;
1547 case SLA_LOAD_CHECK:
1548 case SLA_LOAD:
1549 *(size_t *)ptr = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1550 break;
1551 case SLA_PTRS:
1552 *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1553 break;
1554 case SLA_NULL:
1555 *(void **)ptr = NULL;
1556 break;
1557 default: NOT_REACHED();
1558 }
1559 break;
1560 case SL_ARR: SlArray(ptr, sld->length, conv); break;
1561 case SL_STR: SlString(ptr, sld->length, sld->conv); break;
1562 case SL_LST: SlList(ptr, (SLRefType)conv); break;
1563 default: NOT_REACHED();
1564 }
1565 break;
1566
1567 /* SL_WRITEBYTE translates a value of a variable to another one upon
1568 * saving or loading.
1569 * XXX - variable renaming abuse
1570 * game_value: the value of the variable ingame is abused by sld->version_from
1571 * file_value: the value of the variable in the savegame is abused by sld->version_to */
1572 case SL_WRITEBYTE:
1573 switch (_sl.action) {
1574 case SLA_SAVE: SlWriteByte(sld->version_to); break;
1575 case SLA_LOAD_CHECK:
1576 case SLA_LOAD: *(byte *)ptr = sld->version_from; break;
1577 case SLA_PTRS: break;
1578 case SLA_NULL: break;
1579 default: NOT_REACHED();
1580 }
1581 break;
1582
1583 /* SL_VEH_INCLUDE loads common code for vehicles */
1584 case SL_VEH_INCLUDE:
1585 SlObject(ptr, GetVehicleDescription(VEH_END));
1586 break;
1587
1588 case SL_ST_INCLUDE:
1589 SlObject(ptr, GetBaseStationDescription());
1590 break;
1591
1592 default: NOT_REACHED();
1593 }
1594 return true;
1595 }
1596
1602 void SlObject(void *object, const SaveLoad *sld)
1603 {
1604 /* Automatically calculate the length? */
1605 if (_sl.need_length != NL_NONE) {
1606 SlSetLength(SlCalcObjLength(object, sld));
1607 if (_sl.need_length == NL_CALCLENGTH) return;
1608 }
1609
1610 for (; sld->cmd != SL_END; sld++) {
1611 void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld);
1612 SlObjectMember(ptr, sld);
1613 }
1614 }
1615
1620 void SlGlobList(const SaveLoadGlobVarList *sldg)
1621 {
1622 SlObject(NULL, (const SaveLoad*)sldg);
1623 }
1624
1630 void SlAutolength(AutolengthProc *proc, void *arg)
1631 {
1632 size_t offs;
1633
1634 assert(_sl.action == SLA_SAVE);
1635
1636 /* Tell it to calculate the length */
1637 _sl.need_length = NL_CALCLENGTH;
1638 _sl.obj_len = 0;
1639 proc(arg);
1640
1641 /* Setup length */
1642 _sl.need_length = NL_WANTLENGTH;
1643 SlSetLength(_sl.obj_len);
1644
1645 offs = _sl.dumper->GetSize() + _sl.obj_len;
1646
1647 /* And write the stuff */
1648 proc(arg);
1649
1650 if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
1651 }
1652
1657 static void SlLoadChunk(const ChunkHandler *ch)
1658 {
1659 byte m = SlReadByte();
1660 size_t len;
1661 size_t endoffs;
1662
1663 _sl.block_mode = m;
1664 _sl.obj_len = 0;
1665
1666 switch (m) {
1667 case CH_ARRAY:
1668 _sl.array_index = 0;
1669 ch->load_proc();
1670 if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1671 break;
1672 case CH_SPARSE_ARRAY:
1673 ch->load_proc();
1674 if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1675 break;
1676 default:
1677 if ((m & 0xF) == CH_RIFF) {
1678 /* Read length */
1679 len = (SlReadByte() << 16) | ((m >> 4) << 24);
1680 len += SlReadUint16();
1681 _sl.obj_len = len;
1682 endoffs = _sl.reader->GetSize() + len;
1683 ch->load_proc();
1684 if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1685 } else {
1686 SlErrorCorrupt("Invalid chunk type");
1687 }
1688 break;
1689 }
1690 }
1691
1697 static void SlLoadCheckChunk(const ChunkHandler *ch)
1698 {
1699 byte m = SlReadByte();
1700 size_t len;
1701 size_t endoffs;
1702
1703 _sl.block_mode = m;
1704 _sl.obj_len = 0;
1705
1706 switch (m) {
1707 case CH_ARRAY:
1708 _sl.array_index = 0;
1709 if (ch->load_check_proc) {
1710 ch->load_check_proc();
1711 } else {
1712 SlSkipArray();
1713 }
1714 break;
1715 case CH_SPARSE_ARRAY:
1716 if (ch->load_check_proc) {
1717 ch->load_check_proc();
1718 } else {
1719 SlSkipArray();
1720 }
1721 break;
1722 default:
1723 if ((m & 0xF) == CH_RIFF) {
1724 /* Read length */
1725 len = (SlReadByte() << 16) | ((m >> 4) << 24);
1726 len += SlReadUint16();
1727 _sl.obj_len = len;
1728 endoffs = _sl.reader->GetSize() + len;
1729 if (ch->load_check_proc) {
1730 ch->load_check_proc();
1731 } else {
1732 SlSkipBytes(len);
1733 }
1734 if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1735 } else {
1736 SlErrorCorrupt("Invalid chunk type");
1737 }
1738 break;
1739 }
1740 }
1741
1746 static ChunkSaveLoadProc *_stub_save_proc;
1747
1753 static inline void SlStubSaveProc2(void *arg)
1754 {
1755 _stub_save_proc();
1756 }
1757
1763 static void SlStubSaveProc()
1764 {
1765 SlAutolength(SlStubSaveProc2, NULL);
1766 }
1767
1773 static void SlSaveChunk(const ChunkHandler *ch)
1774 {
1775 ChunkSaveLoadProc *proc = ch->save_proc;
1776
1777 /* Don't save any chunk information if there is no save handler. */
1778 if (proc == NULL) return;
1779
1780 SlWriteUint32(ch->id);
1781 DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1782
1783 if (ch->flags & CH_AUTO_LENGTH) {
1784 /* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */
1785 _stub_save_proc = proc;
1786 proc = SlStubSaveProc;
1787 }
1788
1789 _sl.block_mode = ch->flags & CH_TYPE_MASK;
1790 switch (ch->flags & CH_TYPE_MASK) {
1791 case CH_RIFF:
1792 _sl.need_length = NL_WANTLENGTH;
1793 proc();
1794 break;
1795 case CH_ARRAY:
1796 _sl.last_array_index = 0;
1797 SlWriteByte(CH_ARRAY);
1798 proc();
1799 SlWriteArrayLength(0); // Terminate arrays
1800 break;
1801 case CH_SPARSE_ARRAY:
1802 SlWriteByte(CH_SPARSE_ARRAY);
1803 proc();
1804 SlWriteArrayLength(0); // Terminate arrays
1805 break;
1806 default: NOT_REACHED();
1807 }
1808 }
1809
1811 static void SlSaveChunks()
1812 {
1813 FOR_ALL_CHUNK_HANDLERS(ch) {
1814 SlSaveChunk(ch);
1815 }
1816
1817 /* Terminator */
1818 SlWriteUint32(0);
1819 }
1820
1827 static const ChunkHandler *SlFindChunkHandler(uint32 id)
1828 {
1829 FOR_ALL_CHUNK_HANDLERS(ch) if (ch->id == id) return ch;
1830 return NULL;
1831 }
1832
1834 static void SlLoadChunks()
1835 {
1836 uint32 id;
1837 const ChunkHandler *ch;
1838
1839 for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1840 DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1841
1842 ch = SlFindChunkHandler(id);
1843 if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
1844 SlLoadChunk(ch);
1845 }
1846 }
1847
1849 static void SlLoadCheckChunks()
1850 {
1851 uint32 id;
1852 const ChunkHandler *ch;
1853
1854 for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1855 DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1856
1857 ch = SlFindChunkHandler(id);
1858 if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
1859 SlLoadCheckChunk(ch);
1860 }
1861 }
1862
1864 static void SlFixPointers()
1865 {
1866 _sl.action = SLA_PTRS;
1867
1868 DEBUG(sl, 1, "Fixing pointers");
1869
1870 FOR_ALL_CHUNK_HANDLERS(ch) {
1871 if (ch->ptrs_proc != NULL) {
1872 DEBUG(sl, 2, "Fixing pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1873 ch->ptrs_proc();
1874 }
1875 }
1876
1877 DEBUG(sl, 1, "All pointers fixed");
1878
1879 assert(_sl.action == SLA_PTRS);
1880 }
1881
1882
1884 struct FileReader : LoadFilter {
1885 FILE *file;
1886 long begin;
1887
1892 FileReader(FILE *file) : LoadFilter(NULL), file(file), begin(ftell(file))
1893 {
1894 }
1895
1897 ~FileReader()
1898 {
1899 if (this->file != NULL) fclose(this->file);
1900 this->file = NULL;
1901
1902 /* Make sure we don't double free. */
1903 _sl.sf = NULL;
1904 }
1905
1906 /* virtual */ size_t Read(byte *buf, size_t size)
1907 {
1908 /* We're in the process of shutting down, i.e. in "failure" mode. */
1909 if (this->file == NULL) return 0;
1910
1911 return fread(buf, 1, size, this->file);
1912 }
1913
1914 /* virtual */ void Reset()
1915 {
1916 clearerr(this->file);
1917 if (fseek(this->file, this->begin, SEEK_SET)) {
1918 DEBUG(sl, 1, "Could not reset the file reading");
1919 }
1920 }
1921 };
1922
1924 struct FileWriter : SaveFilter {
1925 FILE *file;
1926
1931 FileWriter(FILE *file) : SaveFilter(NULL), file(file)
1932 {
1933 }
1934
1936 ~FileWriter()
1937 {
1938 this->Finish();
1939
1940 /* Make sure we don't double free. */
1941 _sl.sf = NULL;
1942 }
1943
1944 /* virtual */ void Write(byte *buf, size_t size)
1945 {
1946 /* We're in the process of shutting down, i.e. in "failure" mode. */
1947 if (this->file == NULL) return;
1948
1949 if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
1950 }
1951
1952 /* virtual */ void Finish()
1953 {
1954 if (this->file != NULL) fclose(this->file);
1955 this->file = NULL;
1956 }
1957 };
1958
1959 /*******************************************
1960 ********** START OF LZO CODE **************
1961 *******************************************/
1962
1963 #ifdef WITH_LZO
1964 #include <lzo/lzo1x.h>
1965
1967 static const uint LZO_BUFFER_SIZE = 8192;
1968
1970 struct LZOLoadFilter : LoadFilter {
1975 LZOLoadFilter(LoadFilter *chain) : LoadFilter(chain)
1976 {
1977 if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
1978 }
1979
1980 /* virtual */ size_t Read(byte *buf, size_t ssize)
1981 {
1982 assert(ssize >= LZO_BUFFER_SIZE);
1983
1984 /* Buffer size is from the LZO docs plus the chunk header size. */
1985 byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1986 uint32 tmp[2];
1987 uint32 size;
1988 lzo_uint len;
1989
1990 /* Read header*/
1991 if (this->chain->Read((byte*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
1992
1993 /* Check if size is bad */
1994 ((uint32*)out)[0] = size = tmp[1];
1995
1996 if (_sl_version != 0) {
1997 tmp[0] = TO_BE32(tmp[0]);
1998 size = TO_BE32(size);
1999 }
2000
2001 if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
2002
2003 /* Read block */
2004 if (this->chain->Read(out + sizeof(uint32), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2005
2006 /* Verify checksum */
2007 if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
2008
2009 /* Decompress */
2010 lzo1x_decompress_safe(out + sizeof(uint32) * 1, size, buf, &len, NULL);
2011 return len;
2012 }
2013 };
2014
2016 struct LZOSaveFilter : SaveFilter {
2022 LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2023 {
2024 if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2025 }
2026
2027 /* virtual */ void Write(byte *buf, size_t size)
2028 {
2029 const lzo_bytep in = buf;
2030 /* Buffer size is from the LZO docs plus the chunk header size. */
2031 byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
2032 byte wrkmem[LZO1X_1_MEM_COMPRESS];
2033 lzo_uint outlen;
2034
2035 do {
2036 /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2037 lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
2038 lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
2039 ((uint32*)out)[1] = TO_BE32((uint32)outlen);
2040 ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
2041 this->chain->Write(out, outlen + sizeof(uint32) * 2);
2042
2043 /* Move to next data chunk. */
2044 size -= len;
2045 in += len;
2046 } while (size > 0);
2047 }
2048 };
2049
2050 #endif /* WITH_LZO */
2051
2052 /*********************************************
2053 ******** START OF NOCOMP CODE (uncompressed)*
2054 *********************************************/
2055
2057 struct NoCompLoadFilter : LoadFilter {
2062 NoCompLoadFilter(LoadFilter *chain) : LoadFilter(chain)
2063 {
2064 }
2065
2066 /* virtual */ size_t Read(byte *buf, size_t size)
2067 {
2068 return this->chain->Read(buf, size);
2069 }
2070 };
2071
2073 struct NoCompSaveFilter : SaveFilter {
2079 NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2080 {
2081 }
2082
2083 /* virtual */ void Write(byte *buf, size_t size)
2084 {
2085 this->chain->Write(buf, size);
2086 }
2087 };
2088
2089 /********************************************
2090 ********** START OF ZLIB CODE **************
2091 ********************************************/
2092
2093 #if defined(WITH_ZLIB)
2094 #include <zlib.h>
2095
2097 struct ZlibLoadFilter : LoadFilter {
2098 z_stream z;
2099 byte fread_buf[MEMORY_CHUNK_SIZE];
2100
2105 ZlibLoadFilter(LoadFilter *chain) : LoadFilter(chain)
2106 {
2107 memset(&this->z, 0, sizeof(this->z));
2108 if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2109 }
2110
2112 ~ZlibLoadFilter()
2113 {
2114 inflateEnd(&this->z);
2115 }
2116
2117 /* virtual */ size_t Read(byte *buf, size_t size)
2118 {
2119 this->z.next_out = buf;
2120 this->z.avail_out = (uint)size;
2121
2122 do {
2123 /* read more bytes from the file? */
2124 if (this->z.avail_in == 0) {
2125 this->z.next_in = this->fread_buf;
2126 this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2127 }
2128
2129 /* inflate the data */
2130 int r = inflate(&this->z, 0);
2131 if (r == Z_STREAM_END) break;
2132
2133 if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2134 } while (this->z.avail_out != 0);
2135
2136 return size - this->z.avail_out;
2137 }
2138 };
2139
2141 struct ZlibSaveFilter : SaveFilter {
2142 z_stream z;
2143
2149 ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2150 {
2151 memset(&this->z, 0, sizeof(this->z));
2152 if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2153 }
2154
2156 ~ZlibSaveFilter()
2157 {
2158 deflateEnd(&this->z);
2159 }
2160
2167 void WriteLoop(byte *p, size_t len, int mode)
2168 {
2169 byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2170 uint n;
2171 this->z.next_in = p;
2172 this->z.avail_in = (uInt)len;
2173 do {
2174 this->z.next_out = buf;
2175 this->z.avail_out = sizeof(buf);
2176
2184 int r = deflate(&this->z, mode);
2185
2186 /* bytes were emitted? */
2187 if ((n = sizeof(buf) - this->z.avail_out) != 0) {
2188 this->chain->Write(buf, n);
2189 }
2190 if (r == Z_STREAM_END) break;
2191
2192 if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2193 } while (this->z.avail_in || !this->z.avail_out);
2194 }
2195
2196 /* virtual */ void Write(byte *buf, size_t size)
2197 {
2198 this->WriteLoop(buf, size, 0);
2199 }
2200
2201 /* virtual */ void Finish()
2202 {
2203 this->WriteLoop(NULL, 0, Z_FINISH);
2204 this->chain->Finish();
2205 }
2206 };
2207
2208 #endif /* WITH_ZLIB */
2209
2210 /********************************************
2211 ********** START OF LZMA CODE **************
2212 ********************************************/
2213
2214 #if defined(WITH_LZMA)
2215 #include <lzma.h>
2216
2223 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2224
2226 struct LZMALoadFilter : LoadFilter {
2227 lzma_stream lzma;
2228 byte fread_buf[MEMORY_CHUNK_SIZE];
2229
2234 LZMALoadFilter(LoadFilter *chain) : LoadFilter(chain), lzma(_lzma_init)
2235 {
2236 /* Allow saves up to 256 MB uncompressed */
2237 if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2238 }
2239
2241 ~LZMALoadFilter()
2242 {
2243 lzma_end(&this->lzma);
2244 }
2245
2246 /* virtual */ size_t Read(byte *buf, size_t size)
2247 {
2248 this->lzma.next_out = buf;
2249 this->lzma.avail_out = size;
2250
2251 do {
2252 /* read more bytes from the file? */
2253 if (this->lzma.avail_in == 0) {
2254 this->lzma.next_in = this->fread_buf;
2255 this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2256 }
2257
2258 /* inflate the data */
2259 lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2260 if (r == LZMA_STREAM_END) break;
2261 if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2262 } while (this->lzma.avail_out != 0);
2263
2264 return size - this->lzma.avail_out;
2265 }
2266 };
2267
2269 struct LZMASaveFilter : SaveFilter {
2270 lzma_stream lzma;
2271
2277 LZMASaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain), lzma(_lzma_init)
2278 {
2279 if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2280 }
2281
2283 ~LZMASaveFilter()
2284 {
2285 lzma_end(&this->lzma);
2286 }
2287
2294 void WriteLoop(byte *p, size_t len, lzma_action action)
2295 {
2296 byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2297 size_t n;
2298 this->lzma.next_in = p;
2299 this->lzma.avail_in = len;
2300 do {
2301 this->lzma.next_out = buf;
2302 this->lzma.avail_out = sizeof(buf);
2303
2304 lzma_ret r = lzma_code(&this->lzma, action);
2305
2306 /* bytes were emitted? */
2307 if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
2308 this->chain->Write(buf, n);
2309 }
2310 if (r == LZMA_STREAM_END) break;
2311 if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2312 } while (this->lzma.avail_in || !this->lzma.avail_out);
2313 }
2314
2315 /* virtual */ void Write(byte *buf, size_t size)
2316 {
2317 this->WriteLoop(buf, size, LZMA_RUN);
2318 }
2319
2320 /* virtual */ void Finish()
2321 {
2322 this->WriteLoop(NULL, 0, LZMA_FINISH);
2323 this->chain->Finish();
2324 }
2325 };
2326
2327 #endif /* WITH_LZMA */
2328
2329 /*******************************************
2330 ************* END OF CODE *****************
2331 *******************************************/
2332
2334 struct SaveLoadFormat {
2335 const char *name;
2336 uint32 tag;
2337
2338 LoadFilter *(*init_load)(LoadFilter *chain);
2339 SaveFilter *(*init_write)(SaveFilter *chain, byte compression);
2340
2341 byte min_compression;
2342 byte default_compression;
2343 byte max_compression;
2344 };
2345
2347 static const SaveLoadFormat _saveload_formats[] = {
2348 #if defined(WITH_LZO)
2349 /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2350 {"lzo", TO_BE32X('OTTD'), CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2351 #else
2352 {"lzo", TO_BE32X('OTTD'), NULL, NULL, 0, 0, 0},
2353 #endif
2354 /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2355 {"none", TO_BE32X('OTTN'), CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2356 #if defined(WITH_ZLIB)
2357 /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2358 * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2359 * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2360 {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2361 #else
2362 {"zlib", TO_BE32X('OTTZ'), NULL, NULL, 0, 0, 0},
2363 #endif
2364 #if defined(WITH_LZMA)
2365 /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2366 * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2367 * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2368 * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2369 * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2370 {"lzma", TO_BE32X('OTTX'), CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2371 #else
2372 {"lzma", TO_BE32X('OTTX'), NULL, NULL, 0, 0, 0},
2373 #endif
2374 };
2375
2383 static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
2384 {
2385 const SaveLoadFormat *def = lastof(_saveload_formats);
2386
2387 /* find default savegame format, the highest one with which files can be written */
2388 while (!def->init_write) def--;
2389
2390 if (!StrEmpty(s)) {
2391 /* Get the ":..." of the compression level out of the way */
2392 char *complevel = strrchr(s, ':');
2393 if (complevel != NULL) *complevel = '\0';
2394
2395 for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
2396 if (slf->init_write != NULL && strcmp(s, slf->name) == 0) {
2397 *compression_level = slf->default_compression;
2398 if (complevel != NULL) {
2399 /* There is a compression level in the string.
2400 * First restore the : we removed to do proper name matching,
2401 * then move the the begin of the actual version. */
2402 *complevel = ':';
2403 complevel++;
2404
2405 /* Get the version and determine whether all went fine. */
2406 char *end;
2407 long level = strtol(complevel, &end, 10);
2408 if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) {
2409 SetDParamStr(0, complevel);
2410 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2411 } else {
2412 *compression_level = level;
2413 }
2414 }
2415 return slf;
2416 }
2417 }
2418
2419 SetDParamStr(0, s);
2420 SetDParamStr(1, def->name);
2421 ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2422
2423 /* Restore the string by adding the : back */
2424 if (complevel != NULL) *complevel = ':';
2425 }
2426 *compression_level = def->default_compression;
2427 return def;
2428 }
2429
2430 /* actual loader/saver function */
2431 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2432 extern bool AfterLoadGame();
2433 extern bool LoadOldSaveGame(const char *file);
2434
2438 static inline void ClearSaveLoadState()
2439 {
2440 delete _sl.dumper;
2441 _sl.dumper = NULL;
2442
2443 delete _sl.sf;
2444 _sl.sf = NULL;
2445
2446 delete _sl.reader;
2447 _sl.reader = NULL;
2448
2449 delete _sl.lf;
2450 _sl.lf = NULL;
2451 }
2452
2458 static void SaveFileStart()
2459 {
2460 _sl.ff_state = _fast_forward;
2461 _fast_forward = 0;
2462 if (_cursor.sprite == SPR_CURSOR_MOUSE) SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
2463
2464 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_START);
2465 _sl.saveinprogress = true;
2466 }
2467
2469 static void SaveFileDone()
2470 {
2471 if (_game_mode != GM_MENU) _fast_forward = _sl.ff_state;
2472 if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
2473
2474 InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SAVELOAD_FINISH);
2475 _sl.saveinprogress = false;
2476 }
2477
2479 void SetSaveLoadError(StringID str)
2480 {
2481 _sl.error_str = str;
2482 }
2483
2485 const char *GetSaveLoadErrorString()
2486 {
2487 SetDParam(0, _sl.error_str);
2488 SetDParamStr(1, _sl.extra_msg);
2489
2490 static char err_str[512];
2491 GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
2492 return err_str;
2493 }
2494
2496 static void SaveFileError()
2497 {
2498 SetDParamStr(0, GetSaveLoadErrorString());
2499 ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
2500 SaveFileDone();
2501 }
2502
2507 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2508 {
2509 try {
2510 byte compression;
2511 const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
2512
2513 /* We have written our stuff to memory, now write it to file! */
2514 uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
2515 _sl.sf->Write((byte*)hdr, sizeof(hdr));
2516
2517 _sl.sf = fmt->init_write(_sl.sf, compression);
2518 _sl.dumper->Flush(_sl.sf);
2519
2520 ClearSaveLoadState();
2521
2522 if (threaded) SetAsyncSaveFinish(SaveFileDone);
2523
2524 return SL_OK;
2525 } catch (...) {
2526 ClearSaveLoadState();
2527
2528 AsyncSaveFinishProc asfp = SaveFileDone;
2529
2530 /* We don't want to shout when saving is just
2531 * cancelled due to a client disconnecting. */
2532 if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2533 /* Skip the "colour" character */
2534 DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2535 asfp = SaveFileError;
2536 }
2537
2538 if (threaded) {
2539 SetAsyncSaveFinish(asfp);
2540 } else {
2541 asfp();
2542 }
2543 return SL_ERROR;
2544 }
2545 }
2546
2548 static void SaveFileToDiskThread(void *arg)
2549 {
2550 SaveFileToDisk(true);
2551 }
2552
2553 void WaitTillSaved()
2554 {
2555 if (_save_thread == NULL) return;
2556
2557 _save_thread->Join();
2558 delete _save_thread;
2559 _save_thread = NULL;
2560
2561 /* Make sure every other state is handled properly as well. */
2562 ProcessAsyncSaveFinish();
2563 }
2564
2573 static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
2574 {
2575 assert(!_sl.saveinprogress);
2576
2577 _sl.dumper = new MemoryDumper();
2578 _sl.sf = writer;
2579
2580 _sl_version = SAVEGAME_VERSION;
2581
2582 SaveViewportBeforeSaveGame();
2583 SlSaveChunks();
2584
2585 SaveFileStart();
2586 if (!threaded || !ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread)) {
2587 if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2588
2589 SaveOrLoadResult result = SaveFileToDisk(false);
2590 SaveFileDone();
2591
2592 return result;
2593 }
2594
2595 return SL_OK;
2596 }
2597
2604 SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
2605 {
2606 try {
2607 _sl.action = SLA_SAVE;
2608 return DoSave(writer, threaded);
2609 } catch (...) {
2610 ClearSaveLoadState();
2611 return SL_ERROR;
2612 }
2613 }
2614
2621 static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
2622 {
2623 _sl.lf = reader;
2624
2625 if (load_check) {
2626 /* Clear previous check data */
2627 _load_check_data.Clear();
2628 /* Mark SL_LOAD_CHECK as supported for this savegame. */
2629 _load_check_data.checkable = true;
2630 }
2631
2632 uint32 hdr[2];
2633 if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2634
2635 /* see if we have any loader for this type. */
2636 const SaveLoadFormat *fmt = _saveload_formats;
2637 for (;;) {
2638 /* No loader found, treat as version 0 and use LZO format */
2639 if (fmt == endof(_saveload_formats)) {
2640 DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2641 _sl.lf->Reset();
2642 _sl_version = 0;
2643 _sl_minor_version = 0;
2644
2645 /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2646 fmt = _saveload_formats;
2647 for (;;) {
2648 if (fmt == endof(_saveload_formats)) {
2649 /* Who removed LZO support? Bad bad boy! */
2650 NOT_REACHED();
2651 }
2652 if (fmt->tag == TO_BE32X('OTTD')) break;
2653 fmt++;
2654 }
2655 break;
2656 }
2657
2658 if (fmt->tag == hdr[0]) {
2659 /* check version number */
2660 _sl_version = TO_BE32(hdr[1]) >> 16;
2661 /* Minor is not used anymore from version 18.0, but it is still needed
2662 * in versions before that (4 cases) which can't be removed easy.
2663 * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2664 _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
2665
2666 DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
2667
2668 /* Is the version higher than the current? */
2669 if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2670 break;
2671 }
2672
2673 fmt++;
2674 }
2675
2676 /* loader for this savegame type is not implemented? */
2677 if (fmt->init_load == NULL) {
2678 char err_str[64];
2679 seprintf(err_str, lastof(err_str), "Loader for '%s' is not available.", fmt->name);
2680 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
2681 }
2682
2683 _sl.lf = fmt->init_load(_sl.lf);
2684 _sl.reader = new ReadBuffer(_sl.lf);
2685 _next_offs = 0;
2686
2687 if (!load_check) {
2688 /* Old maps were hardcoded to 256x256 and thus did not contain
2689 * any mapsize information. Pre-initialize to 256x256 to not to
2690 * confuse old games */
2691 InitializeGame(256, 256, true, true);
2692
2693 GamelogReset();
2694
2695 if (IsSavegameVersionBefore(4)) {
2696 /*
2697 * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
2698 * shared savegame version 4. Anything before that 'obviously'
2699 * does not have any NewGRFs. Between the introduction and
2700 * savegame version 41 (just before 0.5) the NewGRF settings
2701 * were not stored in the savegame and they were loaded by
2702 * using the settings from the main menu.
2703 * So, to recap:
2704 * - savegame version < 4: do not load any NewGRFs.
2705 * - savegame version >= 41: load NewGRFs from savegame, which is
2706 * already done at this stage by
2707 * overwriting the main menu settings.
2708 * - other savegame versions: use main menu settings.
2709 *
2710 * This means that users *can* crash savegame version 4..40
2711 * savegames if they set incompatible NewGRFs in the main menu,
2712 * but can't crash anymore for savegame version < 4 savegames.
2713 *
2714 * Note: this is done here because AfterLoadGame is also called
2715 * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
2716 */
2717 ClearGRFConfigList(&_grfconfig);
2718 }
2719 }
2720
2721 if (load_check) {
2722 /* Load chunks into _load_check_data.
2723 * No pools are loaded. References are not possible, and thus do not need resolving. */
2724 SlLoadCheckChunks();
2725 } else {
2726 /* Load chunks and resolve references */
2727 SlLoadChunks();
2728 SlFixPointers();
2729 }
2730
2731 ClearSaveLoadState();
2732
2733 _savegame_type = SGT_OTTD;
2734
2735 if (load_check) {
2736 /* The only part from AfterLoadGame() we need */
2737 _load_check_data.grf_compatibility = IsGoodGRFConfigList(_load_check_data.grfconfig);
2738 } else {
2739 GamelogStartAction(GLAT_LOAD);
2740
2741 /* After loading fix up savegame for any internal changes that
2742 * might have occurred since then. If it fails, load back the old game. */
2743 if (!AfterLoadGame()) {
2744 GamelogStopAction();
2745 return SL_REINIT;
2746 }
2747
2748 GamelogStopAction();
2749 }
2750
2751 return SL_OK;
2752 }
2753
2759 SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
2760 {
2761 try {
2762 _sl.action = SLA_LOAD;
2763 return DoLoad(reader, false);
2764 } catch (...) {
2765 ClearSaveLoadState();
2766 return SL_REINIT;
2767 }
2768 }
2769
2779 SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded)
2780 {
2781 /* An instance of saving is already active, so don't go saving again */
2782 if (_sl.saveinprogress && mode == SL_SAVE && threaded) {
2783 /* if not an autosave, but a user action, show error message */
2784 if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
2785 return SL_OK;
2786 }
2787 WaitTillSaved();
2788
2789 try {
2790 /* Load a TTDLX or TTDPatch game */
2791 if (mode == SL_OLD_LOAD) {
2792 InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
2793
2794 /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
2795 * and if so a new NewGRF list will be made in LoadOldSaveGame.
2796 * Note: this is done here because AfterLoadGame is also called
2797 * for OTTD savegames which have their own NewGRF logic. */
2798 ClearGRFConfigList(&_grfconfig);
2799 GamelogReset();
2800 if (!LoadOldSaveGame(filename)) return SL_REINIT;
2801 _sl_version = 0;
2802 _sl_minor_version = 0;
2803 GamelogStartAction(GLAT_LOAD);
2804 if (!AfterLoadGame()) {
2805 GamelogStopAction();
2806 return SL_REINIT;
2807 }
2808 GamelogStopAction();
2809 return SL_OK;
2810 }
2811
2812 switch (mode) {
2813 case SL_LOAD_CHECK: _sl.action = SLA_LOAD_CHECK; break;
2814 case SL_LOAD: _sl.action = SLA_LOAD; break;
2815 case SL_SAVE: _sl.action = SLA_SAVE; break;
2816 default: NOT_REACHED();
2817 }
2818
2819 FILE *fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
2820
2821 /* Make it a little easier to load savegames from the console */
2822 if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
2823 if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
2824 if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
2825
2826 if (fh == NULL) {
2827 SlError(mode == SL_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2828 }
2829
2830 if (mode == SL_SAVE) { // SAVE game
2831 DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
2832 if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
2833
2834 return DoSave(new FileWriter(fh), threaded);
2835 }
2836
2837 /* LOAD game */
2838 assert(mode == SL_LOAD || mode == SL_LOAD_CHECK);
2839 DEBUG(desync, 1, "load: %s", filename);
2840 return DoLoad(new FileReader(fh), mode == SL_LOAD_CHECK);
2841 } catch (...) {
2842 ClearSaveLoadState();
2843
2844 /* Skip the "colour" character */
2845 if (mode != SL_LOAD_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2846
2847 /* A saver/loader exception!! reinitialize all variables to prevent crash! */
2848 return (mode == SL_LOAD || mode == SL_OLD_LOAD) ? SL_REINIT : SL_ERROR;
2849 }
2850 }
2851
2853 void DoExitSave()
2854 {
2855 SaveOrLoad("exit.sav", SL_SAVE, AUTOSAVE_DIR);
2856 }
2857
2863 void GenerateDefaultSaveName(char *buf, const char *last)
2864 {
2865 /* Check if we have a name for this map, which is the name of the first
2866 * available company. When there's no company available we'll use
2867 * 'Spectator' as "company" name. */
2868 CompanyID cid = _local_company;
2869 if (!Company::IsValidID(cid)) {
2870 const Company *c;
2871 FOR_ALL_COMPANIES(c) {
2872 cid = c->index;
2873 break;
2874 }
2875 }
2876
2877 SetDParam(0, cid);
2878
2879 /* Insert current date */
2880 switch (_settings_client.gui.date_format_in_default_names) {
2881 case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
2882 case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
2883 case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
2884 default: NOT_REACHED();
2885 }
2886 SetDParam(2, _date);
2887
2888 /* Get the correct string (special string for when there's not company) */
2889 GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
2890 SanitizeFilename(buf);
2891 }
2892
2893 #if 0
2894
2900 int GetSavegameType(char *file)
2901 {
2902 const SaveLoadFormat *fmt;
2903 uint32 hdr;
2904 FILE *f;
2905 int mode = SL_OLD_LOAD;
2906
2907 f = fopen(file, "rb");
2908 if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
2909 DEBUG(sl, 0, "Savegame is obsolete or invalid format");
2910 mode = SL_LOAD; // don't try to get filename, just show name as it is written
2911 } else {
2912 /* see if we have any loader for this type. */
2913 for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
2914 if (fmt->tag == hdr) {
2915 mode = SL_LOAD; // new type of savegame
2916 break;
2917 }
2918 }
2919 }
2920
2921 fclose(f);
2922 return mode;
2923 }
2924 #endif