OpenTTD
saveload.cpp
Go to the documentation of this file.
1 /* $Id: saveload.cpp 27778 2017-03-11 20:37:32Z 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.0
265  * 195 27572 1.6.x
266  * 196 27778 1.7.x
267  */
268 extern const uint16 SAVEGAME_VERSION = 196;
269 
272 
274 uint16 _sl_version;
278 
286 };
287 
289  NL_NONE = 0,
292 };
293 
295 static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
296 
298 struct ReadBuffer {
300  byte *bufp;
301  byte *bufe;
303  size_t read;
304 
309  ReadBuffer(LoadFilter *reader) : bufp(NULL), bufe(NULL), reader(reader), read(0)
310  {
311  }
312 
313  inline byte ReadByte()
314  {
315  if (this->bufp == this->bufe) {
316  size_t len = this->reader->Read(this->buf, lengthof(this->buf));
317  if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
318 
319  this->read += len;
320  this->bufp = this->buf;
321  this->bufe = this->buf + len;
322  }
323 
324  return *this->bufp++;
325  }
326 
331  size_t GetSize() const
332  {
333  return this->read - (this->bufe - this->bufp);
334  }
335 };
336 
337 
339 struct MemoryDumper {
341  byte *buf;
342  byte *bufe;
343 
345  MemoryDumper() : buf(NULL), bufe(NULL)
346  {
347  }
348 
353  inline void WriteByte(byte b)
354  {
355  /* Are we at the end of this chunk? */
356  if (this->buf == this->bufe) {
357  this->buf = CallocT<byte>(MEMORY_CHUNK_SIZE);
358  *this->blocks.Append() = this->buf;
359  this->bufe = this->buf + MEMORY_CHUNK_SIZE;
360  }
361 
362  *this->buf++ = b;
363  }
364 
369  void Flush(SaveFilter *writer)
370  {
371  uint i = 0;
372  size_t t = this->GetSize();
373 
374  while (t > 0) {
375  size_t to_write = min(MEMORY_CHUNK_SIZE, t);
376 
377  writer->Write(this->blocks[i++], to_write);
378  t -= to_write;
379  }
380 
381  writer->Finish();
382  }
383 
388  size_t GetSize() const
389  {
390  return this->blocks.Length() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
391  }
392 };
393 
398  byte block_mode;
399  bool error;
400 
401  size_t obj_len;
402  int array_index, last_array_index;
403 
406 
409 
411  char *extra_msg;
412 
413  byte ff_state;
415 };
416 
418 
419 /* these define the chunks */
420 extern const ChunkHandler _gamelog_chunk_handlers[];
421 extern const ChunkHandler _map_chunk_handlers[];
422 extern const ChunkHandler _misc_chunk_handlers[];
423 extern const ChunkHandler _name_chunk_handlers[];
424 extern const ChunkHandler _cheat_chunk_handlers[] ;
425 extern const ChunkHandler _setting_chunk_handlers[];
426 extern const ChunkHandler _company_chunk_handlers[];
427 extern const ChunkHandler _engine_chunk_handlers[];
428 extern const ChunkHandler _veh_chunk_handlers[];
429 extern const ChunkHandler _waypoint_chunk_handlers[];
430 extern const ChunkHandler _depot_chunk_handlers[];
431 extern const ChunkHandler _order_chunk_handlers[];
432 extern const ChunkHandler _town_chunk_handlers[];
433 extern const ChunkHandler _sign_chunk_handlers[];
434 extern const ChunkHandler _station_chunk_handlers[];
435 extern const ChunkHandler _industry_chunk_handlers[];
436 extern const ChunkHandler _economy_chunk_handlers[];
437 extern const ChunkHandler _subsidy_chunk_handlers[];
439 extern const ChunkHandler _goal_chunk_handlers[];
440 extern const ChunkHandler _story_page_chunk_handlers[];
441 extern const ChunkHandler _ai_chunk_handlers[];
442 extern const ChunkHandler _game_chunk_handlers[];
444 extern const ChunkHandler _newgrf_chunk_handlers[];
445 extern const ChunkHandler _group_chunk_handlers[];
447 extern const ChunkHandler _autoreplace_chunk_handlers[];
448 extern const ChunkHandler _labelmaps_chunk_handlers[];
449 extern const ChunkHandler _linkgraph_chunk_handlers[];
450 extern const ChunkHandler _airport_chunk_handlers[];
451 extern const ChunkHandler _object_chunk_handlers[];
452 extern const ChunkHandler _persistent_storage_chunk_handlers[];
453 
455 static const ChunkHandler * const _chunk_handlers[] = {
456  _gamelog_chunk_handlers,
457  _map_chunk_handlers,
458  _misc_chunk_handlers,
459  _name_chunk_handlers,
460  _cheat_chunk_handlers,
461  _setting_chunk_handlers,
462  _veh_chunk_handlers,
463  _waypoint_chunk_handlers,
464  _depot_chunk_handlers,
465  _order_chunk_handlers,
466  _industry_chunk_handlers,
467  _economy_chunk_handlers,
468  _subsidy_chunk_handlers,
469  _cargomonitor_chunk_handlers,
470  _goal_chunk_handlers,
471  _story_page_chunk_handlers,
472  _engine_chunk_handlers,
473  _town_chunk_handlers,
474  _sign_chunk_handlers,
475  _station_chunk_handlers,
476  _company_chunk_handlers,
477  _ai_chunk_handlers,
478  _game_chunk_handlers,
479  _animated_tile_chunk_handlers,
480  _newgrf_chunk_handlers,
481  _group_chunk_handlers,
482  _cargopacket_chunk_handlers,
483  _autoreplace_chunk_handlers,
484  _labelmaps_chunk_handlers,
485  _linkgraph_chunk_handlers,
486  _airport_chunk_handlers,
487  _object_chunk_handlers,
488  _persistent_storage_chunk_handlers,
489  NULL,
490 };
491 
496 #define FOR_ALL_CHUNK_HANDLERS(ch) \
497  for (const ChunkHandler * const *chsc = _chunk_handlers; *chsc != NULL; chsc++) \
498  for (const ChunkHandler *ch = *chsc; ch != NULL; ch = (ch->flags & CH_LAST) ? NULL : ch + 1)
499 
501 static void SlNullPointers()
502 {
503  _sl.action = SLA_NULL;
504 
505  /* We don't want any savegame conversion code to run
506  * during NULLing; especially those that try to get
507  * pointers from other pools. */
509 
510  DEBUG(sl, 1, "Nulling pointers");
511 
513  if (ch->ptrs_proc != NULL) {
514  DEBUG(sl, 2, "Nulling pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
515  ch->ptrs_proc();
516  }
517  }
518 
519  DEBUG(sl, 1, "All pointers nulled");
520 
521  assert(_sl.action == SLA_NULL);
522 }
523 
532 void NORETURN SlError(StringID string, const char *extra_msg)
533 {
534  /* Distinguish between loading into _load_check_data vs. normal save/load. */
535  if (_sl.action == SLA_LOAD_CHECK) {
536  _load_check_data.error = string;
538  _load_check_data.error_data = (extra_msg == NULL) ? NULL : stredup(extra_msg);
539  } else {
540  _sl.error_str = string;
541  free(_sl.extra_msg);
542  _sl.extra_msg = (extra_msg == NULL) ? NULL : stredup(extra_msg);
543  }
544 
545  /* We have to NULL all pointers here; we might be in a state where
546  * the pointers are actually filled with indices, which means that
547  * when we access them during cleaning the pool dereferences of
548  * those indices will be made with segmentation faults as result. */
549  if (_sl.action == SLA_LOAD || _sl.action == SLA_PTRS) SlNullPointers();
550  throw std::exception();
551 }
552 
560 void NORETURN SlErrorCorrupt(const char *msg)
561 {
562  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
563 }
564 
565 
566 typedef void (*AsyncSaveFinishProc)();
569 
575 {
576  if (_exit_game) return;
577  while (_async_save_finish != NULL) CSleep(10);
578 
579  _async_save_finish = proc;
580 }
581 
586 {
587  if (_async_save_finish == NULL) return;
588 
590 
591  _async_save_finish = NULL;
592 
593  if (_save_thread != NULL) {
594  _save_thread->Join();
595  delete _save_thread;
596  _save_thread = NULL;
597  }
598 }
599 
605 {
606  return _sl.reader->ReadByte();
607 }
608 
613 void SlWriteByte(byte b)
614 {
615  _sl.dumper->WriteByte(b);
616 }
617 
618 static inline int SlReadUint16()
619 {
620  int x = SlReadByte() << 8;
621  return x | SlReadByte();
622 }
623 
624 static inline uint32 SlReadUint32()
625 {
626  uint32 x = SlReadUint16() << 16;
627  return x | SlReadUint16();
628 }
629 
630 static inline uint64 SlReadUint64()
631 {
632  uint32 x = SlReadUint32();
633  uint32 y = SlReadUint32();
634  return (uint64)x << 32 | y;
635 }
636 
637 static inline void SlWriteUint16(uint16 v)
638 {
639  SlWriteByte(GB(v, 8, 8));
640  SlWriteByte(GB(v, 0, 8));
641 }
642 
643 static inline void SlWriteUint32(uint32 v)
644 {
645  SlWriteUint16(GB(v, 16, 16));
646  SlWriteUint16(GB(v, 0, 16));
647 }
648 
649 static inline void SlWriteUint64(uint64 x)
650 {
651  SlWriteUint32((uint32)(x >> 32));
652  SlWriteUint32((uint32)x);
653 }
654 
660 static inline void SlSkipBytes(size_t length)
661 {
662  for (; length != 0; length--) SlReadByte();
663 }
664 
674 static uint SlReadSimpleGamma()
675 {
676  uint i = SlReadByte();
677  if (HasBit(i, 7)) {
678  i &= ~0x80;
679  if (HasBit(i, 6)) {
680  i &= ~0x40;
681  if (HasBit(i, 5)) {
682  i &= ~0x20;
683  if (HasBit(i, 4)) {
684  i &= ~0x10;
685  if (HasBit(i, 3)) {
686  SlErrorCorrupt("Unsupported gamma");
687  }
688  i = SlReadByte(); // 32 bits only.
689  }
690  i = (i << 8) | SlReadByte();
691  }
692  i = (i << 8) | SlReadByte();
693  }
694  i = (i << 8) | SlReadByte();
695  }
696  return i;
697 }
698 
716 static void SlWriteSimpleGamma(size_t i)
717 {
718  if (i >= (1 << 7)) {
719  if (i >= (1 << 14)) {
720  if (i >= (1 << 21)) {
721  if (i >= (1 << 28)) {
722  assert(i <= UINT32_MAX); // We can only support 32 bits for now.
723  SlWriteByte((byte)(0xF0));
724  SlWriteByte((byte)(i >> 24));
725  } else {
726  SlWriteByte((byte)(0xE0 | (i >> 24)));
727  }
728  SlWriteByte((byte)(i >> 16));
729  } else {
730  SlWriteByte((byte)(0xC0 | (i >> 16)));
731  }
732  SlWriteByte((byte)(i >> 8));
733  } else {
734  SlWriteByte((byte)(0x80 | (i >> 8)));
735  }
736  }
737  SlWriteByte((byte)i);
738 }
739 
741 static inline uint SlGetGammaLength(size_t i)
742 {
743  return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
744 }
745 
746 static inline uint SlReadSparseIndex()
747 {
748  return SlReadSimpleGamma();
749 }
750 
751 static inline void SlWriteSparseIndex(uint index)
752 {
753  SlWriteSimpleGamma(index);
754 }
755 
756 static inline uint SlReadArrayLength()
757 {
758  return SlReadSimpleGamma();
759 }
760 
761 static inline void SlWriteArrayLength(size_t length)
762 {
763  SlWriteSimpleGamma(length);
764 }
765 
766 static inline uint SlGetArrayLength(size_t length)
767 {
768  return SlGetGammaLength(length);
769 }
770 
777 static inline uint SlCalcConvMemLen(VarType conv)
778 {
779  static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
780  byte length = GB(conv, 4, 4);
781 
782  switch (length << 4) {
783  case SLE_VAR_STRB:
784  case SLE_VAR_STRBQ:
785  case SLE_VAR_STR:
786  case SLE_VAR_STRQ:
787  return SlReadArrayLength();
788 
789  default:
790  assert(length < lengthof(conv_mem_size));
791  return conv_mem_size[length];
792  }
793 }
794 
801 static inline byte SlCalcConvFileLen(VarType conv)
802 {
803  static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
804  byte length = GB(conv, 0, 4);
805  assert(length < lengthof(conv_file_size));
806  return conv_file_size[length];
807 }
808 
810 static inline size_t SlCalcRefLen()
811 {
812  return IsSavegameVersionBefore(69) ? 2 : 4;
813 }
814 
815 void SlSetArrayIndex(uint index)
816 {
818  _sl.array_index = index;
819 }
820 
821 static size_t _next_offs;
822 
828 {
829  int index;
830 
831  /* After reading in the whole array inside the loop
832  * we must have read in all the data, so we must be at end of current block. */
833  if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) SlErrorCorrupt("Invalid chunk size");
834 
835  for (;;) {
836  uint length = SlReadArrayLength();
837  if (length == 0) {
838  _next_offs = 0;
839  return -1;
840  }
841 
842  _sl.obj_len = --length;
843  _next_offs = _sl.reader->GetSize() + length;
844 
845  switch (_sl.block_mode) {
846  case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
847  case CH_ARRAY: index = _sl.array_index++; break;
848  default:
849  DEBUG(sl, 0, "SlIterateArray error");
850  return -1; // error
851  }
852 
853  if (length != 0) return index;
854  }
855 }
856 
861 {
862  while (SlIterateArray() != -1) {
863  SlSkipBytes(_next_offs - _sl.reader->GetSize());
864  }
865 }
866 
872 void SlSetLength(size_t length)
873 {
874  assert(_sl.action == SLA_SAVE);
875 
876  switch (_sl.need_length) {
877  case NL_WANTLENGTH:
878  _sl.need_length = NL_NONE;
879  switch (_sl.block_mode) {
880  case CH_RIFF:
881  /* Ugly encoding of >16M RIFF chunks
882  * The lower 24 bits are normal
883  * The uppermost 4 bits are bits 24:27 */
884  assert(length < (1 << 28));
885  SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
886  break;
887  case CH_ARRAY:
888  assert(_sl.last_array_index <= _sl.array_index);
889  while (++_sl.last_array_index <= _sl.array_index) {
890  SlWriteArrayLength(1);
891  }
892  SlWriteArrayLength(length + 1);
893  break;
894  case CH_SPARSE_ARRAY:
895  SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
896  SlWriteSparseIndex(_sl.array_index);
897  break;
898  default: NOT_REACHED();
899  }
900  break;
901 
902  case NL_CALCLENGTH:
903  _sl.obj_len += (int)length;
904  break;
905 
906  default: NOT_REACHED();
907  }
908 }
909 
916 static void SlCopyBytes(void *ptr, size_t length)
917 {
918  byte *p = (byte *)ptr;
919 
920  switch (_sl.action) {
921  case SLA_LOAD_CHECK:
922  case SLA_LOAD:
923  for (; length != 0; length--) *p++ = SlReadByte();
924  break;
925  case SLA_SAVE:
926  for (; length != 0; length--) SlWriteByte(*p++);
927  break;
928  default: NOT_REACHED();
929  }
930 }
931 
934 {
935  return _sl.obj_len;
936 }
937 
945 int64 ReadValue(const void *ptr, VarType conv)
946 {
947  switch (GetVarMemType(conv)) {
948  case SLE_VAR_BL: return (*(const bool *)ptr != 0);
949  case SLE_VAR_I8: return *(const int8 *)ptr;
950  case SLE_VAR_U8: return *(const byte *)ptr;
951  case SLE_VAR_I16: return *(const int16 *)ptr;
952  case SLE_VAR_U16: return *(const uint16*)ptr;
953  case SLE_VAR_I32: return *(const int32 *)ptr;
954  case SLE_VAR_U32: return *(const uint32*)ptr;
955  case SLE_VAR_I64: return *(const int64 *)ptr;
956  case SLE_VAR_U64: return *(const uint64*)ptr;
957  case SLE_VAR_NULL:return 0;
958  default: NOT_REACHED();
959  }
960 }
961 
969 void WriteValue(void *ptr, VarType conv, int64 val)
970 {
971  switch (GetVarMemType(conv)) {
972  case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
973  case SLE_VAR_I8: *(int8 *)ptr = val; break;
974  case SLE_VAR_U8: *(byte *)ptr = val; break;
975  case SLE_VAR_I16: *(int16 *)ptr = val; break;
976  case SLE_VAR_U16: *(uint16*)ptr = val; break;
977  case SLE_VAR_I32: *(int32 *)ptr = val; break;
978  case SLE_VAR_U32: *(uint32*)ptr = val; break;
979  case SLE_VAR_I64: *(int64 *)ptr = val; break;
980  case SLE_VAR_U64: *(uint64*)ptr = val; break;
981  case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
982  case SLE_VAR_NULL: break;
983  default: NOT_REACHED();
984  }
985 }
986 
995 static void SlSaveLoadConv(void *ptr, VarType conv)
996 {
997  switch (_sl.action) {
998  case SLA_SAVE: {
999  int64 x = ReadValue(ptr, conv);
1000 
1001  /* Write the value to the file and check if its value is in the desired range */
1002  switch (GetVarFileType(conv)) {
1003  case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
1004  case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
1005  case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
1006  case SLE_FILE_STRINGID:
1007  case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
1008  case SLE_FILE_I32:
1009  case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
1010  case SLE_FILE_I64:
1011  case SLE_FILE_U64: SlWriteUint64(x);break;
1012  default: NOT_REACHED();
1013  }
1014  break;
1015  }
1016  case SLA_LOAD_CHECK:
1017  case SLA_LOAD: {
1018  int64 x;
1019  /* Read a value from the file */
1020  switch (GetVarFileType(conv)) {
1021  case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
1022  case SLE_FILE_U8: x = (byte )SlReadByte(); break;
1023  case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
1024  case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
1025  case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
1026  case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
1027  case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
1028  case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
1029  case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
1030  default: NOT_REACHED();
1031  }
1032 
1033  /* Write The value to the struct. These ARE endian safe. */
1034  WriteValue(ptr, conv, x);
1035  break;
1036  }
1037  case SLA_PTRS: break;
1038  case SLA_NULL: break;
1039  default: NOT_REACHED();
1040  }
1041 }
1042 
1052 static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
1053 {
1054  if (ptr == NULL) return 0;
1055  return min(strlen(ptr), length - 1);
1056 }
1057 
1067 static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
1068 {
1069  size_t len;
1070  const char *str;
1071 
1072  switch (GetVarMemType(conv)) {
1073  default: NOT_REACHED();
1074  case SLE_VAR_STR:
1075  case SLE_VAR_STRQ:
1076  str = *(const char * const *)ptr;
1077  len = SIZE_MAX;
1078  break;
1079  case SLE_VAR_STRB:
1080  case SLE_VAR_STRBQ:
1081  str = (const char *)ptr;
1082  len = length;
1083  break;
1084  }
1085 
1086  len = SlCalcNetStringLen(str, len);
1087  return len + SlGetArrayLength(len); // also include the length of the index
1088 }
1089 
1096 static void SlString(void *ptr, size_t length, VarType conv)
1097 {
1098  switch (_sl.action) {
1099  case SLA_SAVE: {
1100  size_t len;
1101  switch (GetVarMemType(conv)) {
1102  default: NOT_REACHED();
1103  case SLE_VAR_STRB:
1104  case SLE_VAR_STRBQ:
1105  len = SlCalcNetStringLen((char *)ptr, length);
1106  break;
1107  case SLE_VAR_STR:
1108  case SLE_VAR_STRQ:
1109  ptr = *(char **)ptr;
1110  len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
1111  break;
1112  }
1113 
1114  SlWriteArrayLength(len);
1115  SlCopyBytes(ptr, len);
1116  break;
1117  }
1118  case SLA_LOAD_CHECK:
1119  case SLA_LOAD: {
1120  size_t len = SlReadArrayLength();
1121 
1122  switch (GetVarMemType(conv)) {
1123  default: NOT_REACHED();
1124  case SLE_VAR_STRB:
1125  case SLE_VAR_STRBQ:
1126  if (len >= length) {
1127  DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
1128  SlCopyBytes(ptr, length);
1129  SlSkipBytes(len - length);
1130  len = length - 1;
1131  } else {
1132  SlCopyBytes(ptr, len);
1133  }
1134  break;
1135  case SLE_VAR_STR:
1136  case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
1137  free(*(char **)ptr);
1138  if (len == 0) {
1139  *(char **)ptr = NULL;
1140  return;
1141  } else {
1142  *(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
1143  ptr = *(char **)ptr;
1144  SlCopyBytes(ptr, len);
1145  }
1146  break;
1147  }
1148 
1149  ((char *)ptr)[len] = '\0'; // properly terminate the string
1151  if ((conv & SLF_ALLOW_CONTROL) != 0) {
1152  settings = settings | SVS_ALLOW_CONTROL_CODE;
1153  if (IsSavegameVersionBefore(169)) {
1154  str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
1155  }
1156  }
1157  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
1158  settings = settings | SVS_ALLOW_NEWLINE;
1159  }
1160  str_validate((char *)ptr, (char *)ptr + len, settings);
1161  break;
1162  }
1163  case SLA_PTRS: break;
1164  case SLA_NULL: break;
1165  default: NOT_REACHED();
1166  }
1167 }
1168 
1174 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
1175 {
1176  return SlCalcConvFileLen(conv) * length;
1177 }
1178 
1185 void SlArray(void *array, size_t length, VarType conv)
1186 {
1187  if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
1188 
1189  /* Automatically calculate the length? */
1190  if (_sl.need_length != NL_NONE) {
1191  SlSetLength(SlCalcArrayLen(length, conv));
1192  /* Determine length only? */
1193  if (_sl.need_length == NL_CALCLENGTH) return;
1194  }
1195 
1196  /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1197  * as a byte-type. So detect this, and adjust array size accordingly */
1198  if (_sl.action != SLA_SAVE && _sl_version == 0) {
1199  /* all arrays except difficulty settings */
1200  if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1201  conv == SLE_INT32 || conv == SLE_UINT32) {
1202  SlCopyBytes(array, length * SlCalcConvFileLen(conv));
1203  return;
1204  }
1205  /* used for conversion of Money 32bit->64bit */
1206  if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1207  for (uint i = 0; i < length; i++) {
1208  ((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
1209  }
1210  return;
1211  }
1212  }
1213 
1214  /* If the size of elements is 1 byte both in file and memory, no special
1215  * conversion is needed, use specialized copy-copy function to speed up things */
1216  if (conv == SLE_INT8 || conv == SLE_UINT8) {
1217  SlCopyBytes(array, length);
1218  } else {
1219  byte *a = (byte*)array;
1220  byte mem_size = SlCalcConvMemLen(conv);
1221 
1222  for (; length != 0; length --) {
1223  SlSaveLoadConv(a, conv);
1224  a += mem_size; // get size
1225  }
1226  }
1227 }
1228 
1229 
1240 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1241 {
1242  assert(_sl.action == SLA_SAVE);
1243 
1244  if (obj == NULL) return 0;
1245 
1246  switch (rt) {
1247  case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1248  case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1249  case REF_STATION: return ((const Station*)obj)->index + 1;
1250  case REF_TOWN: return ((const Town*)obj)->index + 1;
1251  case REF_ORDER: return ((const Order*)obj)->index + 1;
1252  case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1253  case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1254  case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1255  case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1256  case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1257  case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1258  case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1259  default: NOT_REACHED();
1260  }
1261 }
1262 
1273 static void *IntToReference(size_t index, SLRefType rt)
1274 {
1275  assert_compile(sizeof(size_t) <= sizeof(void *));
1276 
1277  assert(_sl.action == SLA_PTRS);
1278 
1279  /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1280  * and should be loaded like that */
1281  if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(4, 4)) {
1282  rt = REF_VEHICLE;
1283  }
1284 
1285  /* No need to look up NULL pointers, just return immediately */
1286  if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return NULL;
1287 
1288  /* Correct index. Old vehicles were saved differently:
1289  * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1290  if (rt != REF_VEHICLE_OLD) index--;
1291 
1292  switch (rt) {
1293  case REF_ORDERLIST:
1294  if (OrderList::IsValidID(index)) return OrderList::Get(index);
1295  SlErrorCorrupt("Referencing invalid OrderList");
1296 
1297  case REF_ORDER:
1298  if (Order::IsValidID(index)) return Order::Get(index);
1299  /* in old versions, invalid order was used to mark end of order list */
1300  if (IsSavegameVersionBefore(5, 2)) return NULL;
1301  SlErrorCorrupt("Referencing invalid Order");
1302 
1303  case REF_VEHICLE_OLD:
1304  case REF_VEHICLE:
1305  if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1306  SlErrorCorrupt("Referencing invalid Vehicle");
1307 
1308  case REF_STATION:
1309  if (Station::IsValidID(index)) return Station::Get(index);
1310  SlErrorCorrupt("Referencing invalid Station");
1311 
1312  case REF_TOWN:
1313  if (Town::IsValidID(index)) return Town::Get(index);
1314  SlErrorCorrupt("Referencing invalid Town");
1315 
1316  case REF_ROADSTOPS:
1317  if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1318  SlErrorCorrupt("Referencing invalid RoadStop");
1319 
1320  case REF_ENGINE_RENEWS:
1321  if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1322  SlErrorCorrupt("Referencing invalid EngineRenew");
1323 
1324  case REF_CARGO_PACKET:
1325  if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1326  SlErrorCorrupt("Referencing invalid CargoPacket");
1327 
1328  case REF_STORAGE:
1329  if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1330  SlErrorCorrupt("Referencing invalid PersistentStorage");
1331 
1332  case REF_LINK_GRAPH:
1333  if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1334  SlErrorCorrupt("Referencing invalid LinkGraph");
1335 
1336  case REF_LINK_GRAPH_JOB:
1337  if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1338  SlErrorCorrupt("Referencing invalid LinkGraphJob");
1339 
1340  default: NOT_REACHED();
1341  }
1342 }
1343 
1348 static inline size_t SlCalcListLen(const void *list)
1349 {
1350  const std::list<void *> *l = (const std::list<void *> *) list;
1351 
1352  int type_size = IsSavegameVersionBefore(69) ? 2 : 4;
1353  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1354  * of the list */
1355  return l->size() * type_size + type_size;
1356 }
1357 
1358 
1364 static void SlList(void *list, SLRefType conv)
1365 {
1366  /* Automatically calculate the length? */
1367  if (_sl.need_length != NL_NONE) {
1368  SlSetLength(SlCalcListLen(list));
1369  /* Determine length only? */
1370  if (_sl.need_length == NL_CALCLENGTH) return;
1371  }
1372 
1373  typedef std::list<void *> PtrList;
1374  PtrList *l = (PtrList *)list;
1375 
1376  switch (_sl.action) {
1377  case SLA_SAVE: {
1378  SlWriteUint32((uint32)l->size());
1379 
1380  PtrList::iterator iter;
1381  for (iter = l->begin(); iter != l->end(); ++iter) {
1382  void *ptr = *iter;
1383  SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
1384  }
1385  break;
1386  }
1387  case SLA_LOAD_CHECK:
1388  case SLA_LOAD: {
1389  size_t length = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1390 
1391  /* Load each reference and push to the end of the list */
1392  for (size_t i = 0; i < length; i++) {
1393  size_t data = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1394  l->push_back((void *)data);
1395  }
1396  break;
1397  }
1398  case SLA_PTRS: {
1399  PtrList temp = *l;
1400 
1401  l->clear();
1402  PtrList::iterator iter;
1403  for (iter = temp.begin(); iter != temp.end(); ++iter) {
1404  void *ptr = IntToReference((size_t)*iter, conv);
1405  l->push_back(ptr);
1406  }
1407  break;
1408  }
1409  case SLA_NULL:
1410  l->clear();
1411  break;
1412  default: NOT_REACHED();
1413  }
1414 }
1415 
1416 
1418 static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
1419 {
1420  if (_sl_version < sld->version_from || _sl_version > sld->version_to) return false;
1421  if (sld->conv & SLF_NOT_IN_SAVE) return false;
1422 
1423  return true;
1424 }
1425 
1431 static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
1432 {
1433  if ((sld->conv & SLF_NO_NETWORK_SYNC) && _sl.action != SLA_SAVE && _networking && !_network_server) {
1434  SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
1435  return true;
1436  }
1437 
1438  return false;
1439 }
1440 
1447 size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
1448 {
1449  size_t length = 0;
1450 
1451  /* Need to determine the length and write a length tag. */
1452  for (; sld->cmd != SL_END; sld++) {
1453  length += SlCalcObjMemberLength(object, sld);
1454  }
1455  return length;
1456 }
1457 
1458 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
1459 {
1460  assert(_sl.action == SLA_SAVE);
1461 
1462  switch (sld->cmd) {
1463  case SL_VAR:
1464  case SL_REF:
1465  case SL_ARR:
1466  case SL_STR:
1467  case SL_LST:
1468  /* CONDITIONAL saveload types depend on the savegame version */
1469  if (!SlIsObjectValidInSavegame(sld)) break;
1470 
1471  switch (sld->cmd) {
1472  case SL_VAR: return SlCalcConvFileLen(sld->conv);
1473  case SL_REF: return SlCalcRefLen();
1474  case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
1475  case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
1476  case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
1477  default: NOT_REACHED();
1478  }
1479  break;
1480  case SL_WRITEBYTE: return 1; // a byte is logically of size 1
1481  case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
1482  case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
1483  default: NOT_REACHED();
1484  }
1485  return 0;
1486 }
1487 
1488 #ifdef OTTD_ASSERT
1489 
1495 static bool IsVariableSizeRight(const SaveLoad *sld)
1496 {
1497  switch (sld->cmd) {
1498  case SL_VAR:
1499  switch (GetVarMemType(sld->conv)) {
1500  case SLE_VAR_BL:
1501  return sld->size == sizeof(bool);
1502  case SLE_VAR_I8:
1503  case SLE_VAR_U8:
1504  return sld->size == sizeof(int8);
1505  case SLE_VAR_I16:
1506  case SLE_VAR_U16:
1507  return sld->size == sizeof(int16);
1508  case SLE_VAR_I32:
1509  case SLE_VAR_U32:
1510  return sld->size == sizeof(int32);
1511  case SLE_VAR_I64:
1512  case SLE_VAR_U64:
1513  return sld->size == sizeof(int64);
1514  default:
1515  return sld->size == sizeof(void *);
1516  }
1517  case SL_REF:
1518  /* These should all be pointer sized. */
1519  return sld->size == sizeof(void *);
1520 
1521  case SL_STR:
1522  /* These should be pointer sized, or fixed array. */
1523  return sld->size == sizeof(void *) || sld->size == sld->length;
1524 
1525  default:
1526  return true;
1527  }
1528 }
1529 
1530 #endif /* OTTD_ASSERT */
1531 
1532 bool SlObjectMember(void *ptr, const SaveLoad *sld)
1533 {
1534 #ifdef OTTD_ASSERT
1535  assert(IsVariableSizeRight(sld));
1536 #endif
1537 
1538  VarType conv = GB(sld->conv, 0, 8);
1539  switch (sld->cmd) {
1540  case SL_VAR:
1541  case SL_REF:
1542  case SL_ARR:
1543  case SL_STR:
1544  case SL_LST:
1545  /* CONDITIONAL saveload types depend on the savegame version */
1546  if (!SlIsObjectValidInSavegame(sld)) return false;
1547  if (SlSkipVariableOnLoad(sld)) return false;
1548 
1549  switch (sld->cmd) {
1550  case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1551  case SL_REF: // Reference variable, translate
1552  switch (_sl.action) {
1553  case SLA_SAVE:
1554  SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1555  break;
1556  case SLA_LOAD_CHECK:
1557  case SLA_LOAD:
1558  *(size_t *)ptr = IsSavegameVersionBefore(69) ? SlReadUint16() : SlReadUint32();
1559  break;
1560  case SLA_PTRS:
1561  *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1562  break;
1563  case SLA_NULL:
1564  *(void **)ptr = NULL;
1565  break;
1566  default: NOT_REACHED();
1567  }
1568  break;
1569  case SL_ARR: SlArray(ptr, sld->length, conv); break;
1570  case SL_STR: SlString(ptr, sld->length, sld->conv); break;
1571  case SL_LST: SlList(ptr, (SLRefType)conv); break;
1572  default: NOT_REACHED();
1573  }
1574  break;
1575 
1576  /* SL_WRITEBYTE translates a value of a variable to another one upon
1577  * saving or loading.
1578  * XXX - variable renaming abuse
1579  * game_value: the value of the variable ingame is abused by sld->version_from
1580  * file_value: the value of the variable in the savegame is abused by sld->version_to */
1581  case SL_WRITEBYTE:
1582  switch (_sl.action) {
1583  case SLA_SAVE: SlWriteByte(sld->version_to); break;
1584  case SLA_LOAD_CHECK:
1585  case SLA_LOAD: *(byte *)ptr = sld->version_from; break;
1586  case SLA_PTRS: break;
1587  case SLA_NULL: break;
1588  default: NOT_REACHED();
1589  }
1590  break;
1591 
1592  /* SL_VEH_INCLUDE loads common code for vehicles */
1593  case SL_VEH_INCLUDE:
1594  SlObject(ptr, GetVehicleDescription(VEH_END));
1595  break;
1596 
1597  case SL_ST_INCLUDE:
1599  break;
1600 
1601  default: NOT_REACHED();
1602  }
1603  return true;
1604 }
1605 
1611 void SlObject(void *object, const SaveLoad *sld)
1612 {
1613  /* Automatically calculate the length? */
1614  if (_sl.need_length != NL_NONE) {
1615  SlSetLength(SlCalcObjLength(object, sld));
1616  if (_sl.need_length == NL_CALCLENGTH) return;
1617  }
1618 
1619  for (; sld->cmd != SL_END; sld++) {
1620  void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld);
1621  SlObjectMember(ptr, sld);
1622  }
1623 }
1624 
1630 {
1631  SlObject(NULL, (const SaveLoad*)sldg);
1632 }
1633 
1639 void SlAutolength(AutolengthProc *proc, void *arg)
1640 {
1641  size_t offs;
1642 
1643  assert(_sl.action == SLA_SAVE);
1644 
1645  /* Tell it to calculate the length */
1646  _sl.need_length = NL_CALCLENGTH;
1647  _sl.obj_len = 0;
1648  proc(arg);
1649 
1650  /* Setup length */
1651  _sl.need_length = NL_WANTLENGTH;
1652  SlSetLength(_sl.obj_len);
1653 
1654  offs = _sl.dumper->GetSize() + _sl.obj_len;
1655 
1656  /* And write the stuff */
1657  proc(arg);
1658 
1659  if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
1660 }
1661 
1666 static void SlLoadChunk(const ChunkHandler *ch)
1667 {
1668  byte m = SlReadByte();
1669  size_t len;
1670  size_t endoffs;
1671 
1672  _sl.block_mode = m;
1673  _sl.obj_len = 0;
1674 
1675  switch (m) {
1676  case CH_ARRAY:
1677  _sl.array_index = 0;
1678  ch->load_proc();
1679  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1680  break;
1681  case CH_SPARSE_ARRAY:
1682  ch->load_proc();
1683  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1684  break;
1685  default:
1686  if ((m & 0xF) == CH_RIFF) {
1687  /* Read length */
1688  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1689  len += SlReadUint16();
1690  _sl.obj_len = len;
1691  endoffs = _sl.reader->GetSize() + len;
1692  ch->load_proc();
1693  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1694  } else {
1695  SlErrorCorrupt("Invalid chunk type");
1696  }
1697  break;
1698  }
1699 }
1700 
1706 static void SlLoadCheckChunk(const ChunkHandler *ch)
1707 {
1708  byte m = SlReadByte();
1709  size_t len;
1710  size_t endoffs;
1711 
1712  _sl.block_mode = m;
1713  _sl.obj_len = 0;
1714 
1715  switch (m) {
1716  case CH_ARRAY:
1717  _sl.array_index = 0;
1718  if (ch->load_check_proc) {
1719  ch->load_check_proc();
1720  } else {
1721  SlSkipArray();
1722  }
1723  break;
1724  case CH_SPARSE_ARRAY:
1725  if (ch->load_check_proc) {
1726  ch->load_check_proc();
1727  } else {
1728  SlSkipArray();
1729  }
1730  break;
1731  default:
1732  if ((m & 0xF) == CH_RIFF) {
1733  /* Read length */
1734  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1735  len += SlReadUint16();
1736  _sl.obj_len = len;
1737  endoffs = _sl.reader->GetSize() + len;
1738  if (ch->load_check_proc) {
1739  ch->load_check_proc();
1740  } else {
1741  SlSkipBytes(len);
1742  }
1743  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1744  } else {
1745  SlErrorCorrupt("Invalid chunk type");
1746  }
1747  break;
1748  }
1749 }
1750 
1755 static ChunkSaveLoadProc *_stub_save_proc;
1756 
1762 static inline void SlStubSaveProc2(void *arg)
1763 {
1764  _stub_save_proc();
1765 }
1766 
1772 static void SlStubSaveProc()
1773 {
1775 }
1776 
1782 static void SlSaveChunk(const ChunkHandler *ch)
1783 {
1784  ChunkSaveLoadProc *proc = ch->save_proc;
1785 
1786  /* Don't save any chunk information if there is no save handler. */
1787  if (proc == NULL) return;
1788 
1789  SlWriteUint32(ch->id);
1790  DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1791 
1792  if (ch->flags & CH_AUTO_LENGTH) {
1793  /* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */
1794  _stub_save_proc = proc;
1795  proc = SlStubSaveProc;
1796  }
1797 
1798  _sl.block_mode = ch->flags & CH_TYPE_MASK;
1799  switch (ch->flags & CH_TYPE_MASK) {
1800  case CH_RIFF:
1801  _sl.need_length = NL_WANTLENGTH;
1802  proc();
1803  break;
1804  case CH_ARRAY:
1805  _sl.last_array_index = 0;
1806  SlWriteByte(CH_ARRAY);
1807  proc();
1808  SlWriteArrayLength(0); // Terminate arrays
1809  break;
1810  case CH_SPARSE_ARRAY:
1811  SlWriteByte(CH_SPARSE_ARRAY);
1812  proc();
1813  SlWriteArrayLength(0); // Terminate arrays
1814  break;
1815  default: NOT_REACHED();
1816  }
1817 }
1818 
1820 static void SlSaveChunks()
1821 {
1823  SlSaveChunk(ch);
1824  }
1825 
1826  /* Terminator */
1827  SlWriteUint32(0);
1828 }
1829 
1836 static const ChunkHandler *SlFindChunkHandler(uint32 id)
1837 {
1838  FOR_ALL_CHUNK_HANDLERS(ch) if (ch->id == id) return ch;
1839  return NULL;
1840 }
1841 
1843 static void SlLoadChunks()
1844 {
1845  uint32 id;
1846  const ChunkHandler *ch;
1847 
1848  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1849  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1850 
1851  ch = SlFindChunkHandler(id);
1852  if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
1853  SlLoadChunk(ch);
1854  }
1855 }
1856 
1858 static void SlLoadCheckChunks()
1859 {
1860  uint32 id;
1861  const ChunkHandler *ch;
1862 
1863  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1864  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1865 
1866  ch = SlFindChunkHandler(id);
1867  if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
1868  SlLoadCheckChunk(ch);
1869  }
1870 }
1871 
1873 static void SlFixPointers()
1874 {
1875  _sl.action = SLA_PTRS;
1876 
1877  DEBUG(sl, 1, "Fixing pointers");
1878 
1880  if (ch->ptrs_proc != NULL) {
1881  DEBUG(sl, 2, "Fixing pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1882  ch->ptrs_proc();
1883  }
1884  }
1885 
1886  DEBUG(sl, 1, "All pointers fixed");
1887 
1888  assert(_sl.action == SLA_PTRS);
1889 }
1890 
1891 
1894  FILE *file;
1895  long begin;
1896 
1901  FileReader(FILE *file) : LoadFilter(NULL), file(file), begin(ftell(file))
1902  {
1903  }
1904 
1907  {
1908  if (this->file != NULL) fclose(this->file);
1909  this->file = NULL;
1910 
1911  /* Make sure we don't double free. */
1912  _sl.sf = NULL;
1913  }
1914 
1915  /* virtual */ size_t Read(byte *buf, size_t size)
1916  {
1917  /* We're in the process of shutting down, i.e. in "failure" mode. */
1918  if (this->file == NULL) return 0;
1919 
1920  return fread(buf, 1, size, this->file);
1921  }
1922 
1923  /* virtual */ void Reset()
1924  {
1925  clearerr(this->file);
1926  if (fseek(this->file, this->begin, SEEK_SET)) {
1927  DEBUG(sl, 1, "Could not reset the file reading");
1928  }
1929  }
1930 };
1931 
1934  FILE *file;
1935 
1940  FileWriter(FILE *file) : SaveFilter(NULL), file(file)
1941  {
1942  }
1943 
1946  {
1947  this->Finish();
1948 
1949  /* Make sure we don't double free. */
1950  _sl.sf = NULL;
1951  }
1952 
1953  /* virtual */ void Write(byte *buf, size_t size)
1954  {
1955  /* We're in the process of shutting down, i.e. in "failure" mode. */
1956  if (this->file == NULL) return;
1957 
1958  if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
1959  }
1960 
1961  /* virtual */ void Finish()
1962  {
1963  if (this->file != NULL) fclose(this->file);
1964  this->file = NULL;
1965  }
1966 };
1967 
1968 /*******************************************
1969  ********** START OF LZO CODE **************
1970  *******************************************/
1971 
1972 #ifdef WITH_LZO
1973 #include <lzo/lzo1x.h>
1974 
1976 static const uint LZO_BUFFER_SIZE = 8192;
1977 
1985  {
1986  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
1987  }
1988 
1989  /* virtual */ size_t Read(byte *buf, size_t ssize)
1990  {
1991  assert(ssize >= LZO_BUFFER_SIZE);
1992 
1993  /* Buffer size is from the LZO docs plus the chunk header size. */
1994  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1995  uint32 tmp[2];
1996  uint32 size;
1997  lzo_uint len;
1998 
1999  /* Read header*/
2000  if (this->chain->Read((byte*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
2001 
2002  /* Check if size is bad */
2003  ((uint32*)out)[0] = size = tmp[1];
2004 
2005  if (_sl_version != 0) {
2006  tmp[0] = TO_BE32(tmp[0]);
2007  size = TO_BE32(size);
2008  }
2009 
2010  if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
2011 
2012  /* Read block */
2013  if (this->chain->Read(out + sizeof(uint32), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2014 
2015  /* Verify checksum */
2016  if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
2017 
2018  /* Decompress */
2019  lzo1x_decompress_safe(out + sizeof(uint32) * 1, size, buf, &len, NULL);
2020  return len;
2021  }
2022 };
2023 
2031  LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2032  {
2033  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2034  }
2035 
2036  /* virtual */ void Write(byte *buf, size_t size)
2037  {
2038  const lzo_bytep in = buf;
2039  /* Buffer size is from the LZO docs plus the chunk header size. */
2040  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
2041  byte wrkmem[LZO1X_1_MEM_COMPRESS];
2042  lzo_uint outlen;
2043 
2044  do {
2045  /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2046  lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
2047  lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
2048  ((uint32*)out)[1] = TO_BE32((uint32)outlen);
2049  ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
2050  this->chain->Write(out, outlen + sizeof(uint32) * 2);
2051 
2052  /* Move to next data chunk. */
2053  size -= len;
2054  in += len;
2055  } while (size > 0);
2056  }
2057 };
2058 
2059 #endif /* WITH_LZO */
2060 
2061 /*********************************************
2062  ******** START OF NOCOMP CODE (uncompressed)*
2063  *********************************************/
2064 
2072  {
2073  }
2074 
2075  /* virtual */ size_t Read(byte *buf, size_t size)
2076  {
2077  return this->chain->Read(buf, size);
2078  }
2079 };
2080 
2088  NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2089  {
2090  }
2091 
2092  /* virtual */ void Write(byte *buf, size_t size)
2093  {
2094  this->chain->Write(buf, size);
2095  }
2096 };
2097 
2098 /********************************************
2099  ********** START OF ZLIB CODE **************
2100  ********************************************/
2101 
2102 #if defined(WITH_ZLIB)
2103 #include <zlib.h>
2104 
2107  z_stream z;
2109 
2115  {
2116  memset(&this->z, 0, sizeof(this->z));
2117  if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2118  }
2119 
2122  {
2123  inflateEnd(&this->z);
2124  }
2125 
2126  /* virtual */ size_t Read(byte *buf, size_t size)
2127  {
2128  this->z.next_out = buf;
2129  this->z.avail_out = (uint)size;
2130 
2131  do {
2132  /* read more bytes from the file? */
2133  if (this->z.avail_in == 0) {
2134  this->z.next_in = this->fread_buf;
2135  this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2136  }
2137 
2138  /* inflate the data */
2139  int r = inflate(&this->z, 0);
2140  if (r == Z_STREAM_END) break;
2141 
2142  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2143  } while (this->z.avail_out != 0);
2144 
2145  return size - this->z.avail_out;
2146  }
2147 };
2148 
2151  z_stream z;
2152 
2158  ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2159  {
2160  memset(&this->z, 0, sizeof(this->z));
2161  if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2162  }
2163 
2166  {
2167  deflateEnd(&this->z);
2168  }
2169 
2176  void WriteLoop(byte *p, size_t len, int mode)
2177  {
2178  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2179  uint n;
2180  this->z.next_in = p;
2181  this->z.avail_in = (uInt)len;
2182  do {
2183  this->z.next_out = buf;
2184  this->z.avail_out = sizeof(buf);
2185 
2193  int r = deflate(&this->z, mode);
2194 
2195  /* bytes were emitted? */
2196  if ((n = sizeof(buf) - this->z.avail_out) != 0) {
2197  this->chain->Write(buf, n);
2198  }
2199  if (r == Z_STREAM_END) break;
2200 
2201  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2202  } while (this->z.avail_in || !this->z.avail_out);
2203  }
2204 
2205  /* virtual */ void Write(byte *buf, size_t size)
2206  {
2207  this->WriteLoop(buf, size, 0);
2208  }
2209 
2210  /* virtual */ void Finish()
2211  {
2212  this->WriteLoop(NULL, 0, Z_FINISH);
2213  this->chain->Finish();
2214  }
2215 };
2216 
2217 #endif /* WITH_ZLIB */
2218 
2219 /********************************************
2220  ********** START OF LZMA CODE **************
2221  ********************************************/
2222 
2223 #if defined(WITH_LZMA)
2224 #include <lzma.h>
2225 
2232 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2233 
2236  lzma_stream lzma;
2238 
2244  {
2245  /* Allow saves up to 256 MB uncompressed */
2246  if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2247  }
2248 
2251  {
2252  lzma_end(&this->lzma);
2253  }
2254 
2255  /* virtual */ size_t Read(byte *buf, size_t size)
2256  {
2257  this->lzma.next_out = buf;
2258  this->lzma.avail_out = size;
2259 
2260  do {
2261  /* read more bytes from the file? */
2262  if (this->lzma.avail_in == 0) {
2263  this->lzma.next_in = this->fread_buf;
2264  this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2265  }
2266 
2267  /* inflate the data */
2268  lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2269  if (r == LZMA_STREAM_END) break;
2270  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2271  } while (this->lzma.avail_out != 0);
2272 
2273  return size - this->lzma.avail_out;
2274  }
2275 };
2276 
2279  lzma_stream lzma;
2280 
2286  LZMASaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain), lzma(_lzma_init)
2287  {
2288  if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2289  }
2290 
2293  {
2294  lzma_end(&this->lzma);
2295  }
2296 
2303  void WriteLoop(byte *p, size_t len, lzma_action action)
2304  {
2305  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2306  size_t n;
2307  this->lzma.next_in = p;
2308  this->lzma.avail_in = len;
2309  do {
2310  this->lzma.next_out = buf;
2311  this->lzma.avail_out = sizeof(buf);
2312 
2313  lzma_ret r = lzma_code(&this->lzma, action);
2314 
2315  /* bytes were emitted? */
2316  if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
2317  this->chain->Write(buf, n);
2318  }
2319  if (r == LZMA_STREAM_END) break;
2320  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2321  } while (this->lzma.avail_in || !this->lzma.avail_out);
2322  }
2323 
2324  /* virtual */ void Write(byte *buf, size_t size)
2325  {
2326  this->WriteLoop(buf, size, LZMA_RUN);
2327  }
2328 
2329  /* virtual */ void Finish()
2330  {
2331  this->WriteLoop(NULL, 0, LZMA_FINISH);
2332  this->chain->Finish();
2333  }
2334 };
2335 
2336 #endif /* WITH_LZMA */
2337 
2338 /*******************************************
2339  ************* END OF CODE *****************
2340  *******************************************/
2341 
2344  const char *name;
2345  uint32 tag;
2346 
2347  LoadFilter *(*init_load)(LoadFilter *chain);
2348  SaveFilter *(*init_write)(SaveFilter *chain, byte compression);
2349 
2353 };
2354 
2357 #if defined(WITH_LZO)
2358  /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2359  {"lzo", TO_BE32X('OTTD'), CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2360 #else
2361  {"lzo", TO_BE32X('OTTD'), NULL, NULL, 0, 0, 0},
2362 #endif
2363  /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2364  {"none", TO_BE32X('OTTN'), CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2365 #if defined(WITH_ZLIB)
2366  /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2367  * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2368  * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2369  {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2370 #else
2371  {"zlib", TO_BE32X('OTTZ'), NULL, NULL, 0, 0, 0},
2372 #endif
2373 #if defined(WITH_LZMA)
2374  /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2375  * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2376  * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2377  * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2378  * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2379  {"lzma", TO_BE32X('OTTX'), CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2380 #else
2381  {"lzma", TO_BE32X('OTTX'), NULL, NULL, 0, 0, 0},
2382 #endif
2383 };
2384 
2392 static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
2393 {
2394  const SaveLoadFormat *def = lastof(_saveload_formats);
2395 
2396  /* find default savegame format, the highest one with which files can be written */
2397  while (!def->init_write) def--;
2398 
2399  if (!StrEmpty(s)) {
2400  /* Get the ":..." of the compression level out of the way */
2401  char *complevel = strrchr(s, ':');
2402  if (complevel != NULL) *complevel = '\0';
2403 
2404  for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
2405  if (slf->init_write != NULL && strcmp(s, slf->name) == 0) {
2406  *compression_level = slf->default_compression;
2407  if (complevel != NULL) {
2408  /* There is a compression level in the string.
2409  * First restore the : we removed to do proper name matching,
2410  * then move the the begin of the actual version. */
2411  *complevel = ':';
2412  complevel++;
2413 
2414  /* Get the version and determine whether all went fine. */
2415  char *end;
2416  long level = strtol(complevel, &end, 10);
2417  if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) {
2418  SetDParamStr(0, complevel);
2419  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2420  } else {
2421  *compression_level = level;
2422  }
2423  }
2424  return slf;
2425  }
2426  }
2427 
2428  SetDParamStr(0, s);
2429  SetDParamStr(1, def->name);
2430  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2431 
2432  /* Restore the string by adding the : back */
2433  if (complevel != NULL) *complevel = ':';
2434  }
2435  *compression_level = def->default_compression;
2436  return def;
2437 }
2438 
2439 /* actual loader/saver function */
2440 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2441 extern bool AfterLoadGame();
2442 extern bool LoadOldSaveGame(const char *file);
2443 
2447 static inline void ClearSaveLoadState()
2448 {
2449  delete _sl.dumper;
2450  _sl.dumper = NULL;
2451 
2452  delete _sl.sf;
2453  _sl.sf = NULL;
2454 
2455  delete _sl.reader;
2456  _sl.reader = NULL;
2457 
2458  delete _sl.lf;
2459  _sl.lf = NULL;
2460 }
2461 
2467 static void SaveFileStart()
2468 {
2469  _sl.ff_state = _fast_forward;
2470  _fast_forward = 0;
2471  SetMouseCursorBusy(true);
2472 
2474  _sl.saveinprogress = true;
2475 }
2476 
2478 static void SaveFileDone()
2479 {
2480  if (_game_mode != GM_MENU) _fast_forward = _sl.ff_state;
2481  SetMouseCursorBusy(false);
2482 
2484  _sl.saveinprogress = false;
2485 }
2486 
2489 {
2490  _sl.error_str = str;
2491 }
2492 
2495 {
2496  SetDParam(0, _sl.error_str);
2497  SetDParamStr(1, _sl.extra_msg);
2498 
2499  static char err_str[512];
2500  GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
2501  return err_str;
2502 }
2503 
2505 static void SaveFileError()
2506 {
2508  ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
2509  SaveFileDone();
2510 }
2511 
2516 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2517 {
2518  try {
2519  byte compression;
2520  const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
2521 
2522  /* We have written our stuff to memory, now write it to file! */
2523  uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
2524  _sl.sf->Write((byte*)hdr, sizeof(hdr));
2525 
2526  _sl.sf = fmt->init_write(_sl.sf, compression);
2527  _sl.dumper->Flush(_sl.sf);
2528 
2530 
2531  if (threaded) SetAsyncSaveFinish(SaveFileDone);
2532 
2533  return SL_OK;
2534  } catch (...) {
2536 
2538 
2539  /* We don't want to shout when saving is just
2540  * cancelled due to a client disconnecting. */
2541  if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2542  /* Skip the "colour" character */
2543  DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2544  asfp = SaveFileError;
2545  }
2546 
2547  if (threaded) {
2548  SetAsyncSaveFinish(asfp);
2549  } else {
2550  asfp();
2551  }
2552  return SL_ERROR;
2553  }
2554 }
2555 
2557 static void SaveFileToDiskThread(void *arg)
2558 {
2559  SaveFileToDisk(true);
2560 }
2561 
2562 void WaitTillSaved()
2563 {
2564  if (_save_thread == NULL) return;
2565 
2566  _save_thread->Join();
2567  delete _save_thread;
2568  _save_thread = NULL;
2569 
2570  /* Make sure every other state is handled properly as well. */
2572 }
2573 
2582 static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
2583 {
2584  assert(!_sl.saveinprogress);
2585 
2586  _sl.dumper = new MemoryDumper();
2587  _sl.sf = writer;
2588 
2590 
2591  SaveViewportBeforeSaveGame();
2592  SlSaveChunks();
2593 
2594  SaveFileStart();
2595  if (!threaded || !ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread, "ottd:savegame")) {
2596  if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2597 
2598  SaveOrLoadResult result = SaveFileToDisk(false);
2599  SaveFileDone();
2600 
2601  return result;
2602  }
2603 
2604  return SL_OK;
2605 }
2606 
2614 {
2615  try {
2616  _sl.action = SLA_SAVE;
2617  return DoSave(writer, threaded);
2618  } catch (...) {
2620  return SL_ERROR;
2621  }
2622 }
2623 
2630 static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
2631 {
2632  _sl.lf = reader;
2633 
2634  if (load_check) {
2635  /* Clear previous check data */
2637  /* Mark SL_LOAD_CHECK as supported for this savegame. */
2638  _load_check_data.checkable = true;
2639  }
2640 
2641  uint32 hdr[2];
2642  if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2643 
2644  /* see if we have any loader for this type. */
2645  const SaveLoadFormat *fmt = _saveload_formats;
2646  for (;;) {
2647  /* No loader found, treat as version 0 and use LZO format */
2648  if (fmt == endof(_saveload_formats)) {
2649  DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2650  _sl.lf->Reset();
2651  _sl_version = 0;
2652  _sl_minor_version = 0;
2653 
2654  /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2655  fmt = _saveload_formats;
2656  for (;;) {
2657  if (fmt == endof(_saveload_formats)) {
2658  /* Who removed LZO support? Bad bad boy! */
2659  NOT_REACHED();
2660  }
2661  if (fmt->tag == TO_BE32X('OTTD')) break;
2662  fmt++;
2663  }
2664  break;
2665  }
2666 
2667  if (fmt->tag == hdr[0]) {
2668  /* check version number */
2669  _sl_version = TO_BE32(hdr[1]) >> 16;
2670  /* Minor is not used anymore from version 18.0, but it is still needed
2671  * in versions before that (4 cases) which can't be removed easy.
2672  * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2673  _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
2674 
2675  DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
2676 
2677  /* Is the version higher than the current? */
2678  if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2679  break;
2680  }
2681 
2682  fmt++;
2683  }
2684 
2685  /* loader for this savegame type is not implemented? */
2686  if (fmt->init_load == NULL) {
2687  char err_str[64];
2688  seprintf(err_str, lastof(err_str), "Loader for '%s' is not available.", fmt->name);
2689  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
2690  }
2691 
2692  _sl.lf = fmt->init_load(_sl.lf);
2693  _sl.reader = new ReadBuffer(_sl.lf);
2694  _next_offs = 0;
2695 
2696  if (!load_check) {
2697  /* Old maps were hardcoded to 256x256 and thus did not contain
2698  * any mapsize information. Pre-initialize to 256x256 to not to
2699  * confuse old games */
2700  InitializeGame(256, 256, true, true);
2701 
2702  GamelogReset();
2703 
2704  if (IsSavegameVersionBefore(4)) {
2705  /*
2706  * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
2707  * shared savegame version 4. Anything before that 'obviously'
2708  * does not have any NewGRFs. Between the introduction and
2709  * savegame version 41 (just before 0.5) the NewGRF settings
2710  * were not stored in the savegame and they were loaded by
2711  * using the settings from the main menu.
2712  * So, to recap:
2713  * - savegame version < 4: do not load any NewGRFs.
2714  * - savegame version >= 41: load NewGRFs from savegame, which is
2715  * already done at this stage by
2716  * overwriting the main menu settings.
2717  * - other savegame versions: use main menu settings.
2718  *
2719  * This means that users *can* crash savegame version 4..40
2720  * savegames if they set incompatible NewGRFs in the main menu,
2721  * but can't crash anymore for savegame version < 4 savegames.
2722  *
2723  * Note: this is done here because AfterLoadGame is also called
2724  * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
2725  */
2727  }
2728  }
2729 
2730  if (load_check) {
2731  /* Load chunks into _load_check_data.
2732  * No pools are loaded. References are not possible, and thus do not need resolving. */
2734  } else {
2735  /* Load chunks and resolve references */
2736  SlLoadChunks();
2737  SlFixPointers();
2738  }
2739 
2741 
2743 
2744  if (load_check) {
2745  /* The only part from AfterLoadGame() we need */
2747  } else {
2749 
2750  /* After loading fix up savegame for any internal changes that
2751  * might have occurred since then. If it fails, load back the old game. */
2752  if (!AfterLoadGame()) {
2754  return SL_REINIT;
2755  }
2756 
2758  }
2759 
2760  return SL_OK;
2761 }
2762 
2769 {
2770  try {
2771  _sl.action = SLA_LOAD;
2772  return DoLoad(reader, false);
2773  } catch (...) {
2775  return SL_REINIT;
2776  }
2777 }
2778 
2788 SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
2789 {
2790  /* An instance of saving is already active, so don't go saving again */
2791  if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
2792  /* if not an autosave, but a user action, show error message */
2793  if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
2794  return SL_OK;
2795  }
2796  WaitTillSaved();
2797 
2798  try {
2799  /* Load a TTDLX or TTDPatch game */
2800  if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
2801  InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
2802 
2803  /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
2804  * and if so a new NewGRF list will be made in LoadOldSaveGame.
2805  * Note: this is done here because AfterLoadGame is also called
2806  * for OTTD savegames which have their own NewGRF logic. */
2808  GamelogReset();
2809  if (!LoadOldSaveGame(filename)) return SL_REINIT;
2810  _sl_version = 0;
2811  _sl_minor_version = 0;
2813  if (!AfterLoadGame()) {
2815  return SL_REINIT;
2816  }
2818  return SL_OK;
2819  }
2820 
2821  assert(dft == DFT_GAME_FILE);
2822  switch (fop) {
2823  case SLO_CHECK:
2824  _sl.action = SLA_LOAD_CHECK;
2825  break;
2826 
2827  case SLO_LOAD:
2828  _sl.action = SLA_LOAD;
2829  break;
2830 
2831  case SLO_SAVE:
2832  _sl.action = SLA_SAVE;
2833  break;
2834 
2835  default: NOT_REACHED();
2836  }
2837 
2838  FILE *fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
2839 
2840  /* Make it a little easier to load savegames from the console */
2841  if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
2842  if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
2843  if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
2844 
2845  if (fh == NULL) {
2846  SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2847  }
2848 
2849  if (fop == SLO_SAVE) { // SAVE game
2850  DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
2851  if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
2852 
2853  return DoSave(new FileWriter(fh), threaded);
2854  }
2855 
2856  /* LOAD game */
2857  assert(fop == SLO_LOAD || fop == SLO_CHECK);
2858  DEBUG(desync, 1, "load: %s", filename);
2859  return DoLoad(new FileReader(fh), fop == SLO_CHECK);
2860  } catch (...) {
2861  /* This code may be executed both for old and new save games. */
2863 
2864  /* Skip the "colour" character */
2865  if (fop != SLO_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2866 
2867  /* A saver/loader exception!! reinitialize all variables to prevent crash! */
2868  return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
2869  }
2870 }
2871 
2874 {
2876 }
2877 
2883 void GenerateDefaultSaveName(char *buf, const char *last)
2884 {
2885  /* Check if we have a name for this map, which is the name of the first
2886  * available company. When there's no company available we'll use
2887  * 'Spectator' as "company" name. */
2888  CompanyID cid = _local_company;
2889  if (!Company::IsValidID(cid)) {
2890  const Company *c;
2891  FOR_ALL_COMPANIES(c) {
2892  cid = c->index;
2893  break;
2894  }
2895  }
2896 
2897  SetDParam(0, cid);
2898 
2899  /* Insert current date */
2901  case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
2902  case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
2903  case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
2904  default: NOT_REACHED();
2905  }
2906  SetDParam(2, _date);
2907 
2908  /* Get the correct string (special string for when there's not company) */
2909  GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
2910  SanitizeFilename(buf);
2911 }
2912 
2918 {
2920 }
2921 
2929 {
2930  if (aft == FT_INVALID || aft == FT_NONE) {
2931  this->file_op = SLO_INVALID;
2932  this->detail_ftype = DFT_INVALID;
2933  this->abstract_ftype = FT_INVALID;
2934  return;
2935  }
2936 
2937  this->file_op = fop;
2938  this->detail_ftype = dft;
2939  this->abstract_ftype = aft;
2940 }
2941 
2946 void FileToSaveLoad::SetName(const char *name)
2947 {
2948  strecpy(this->name, name, lastof(this->name));
2949 }
2950 
2955 void FileToSaveLoad::SetTitle(const char *title)
2956 {
2957  strecpy(this->title, title, lastof(this->title));
2958 }
2959 
2960 #if 0
2961 
2967 int GetSavegameType(char *file)
2968 {
2969  const SaveLoadFormat *fmt;
2970  uint32 hdr;
2971  FILE *f;
2972  int mode = SL_OLD_LOAD;
2973 
2974  f = fopen(file, "rb");
2975  if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
2976  DEBUG(sl, 0, "Savegame is obsolete or invalid format");
2977  mode = SL_LOAD; // don't try to get filename, just show name as it is written
2978  } else {
2979  /* see if we have any loader for this type. */
2980  for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
2981  if (fmt->tag == hdr) {
2982  mode = SL_LOAD; // new type of savegame
2983  break;
2984  }
2985  }
2986  }
2987 
2988  fclose(f);
2989  return mode;
2990 }
2991 #endif