OpenTTD Source  1.10.2
saveload.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
22 #include <deque>
23 
24 #include "../stdafx.h"
25 #include "../debug.h"
26 #include "../station_base.h"
27 #include "../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 #include <atomic>
47 
48 #include "table/strings.h"
49 
50 #include "saveload_internal.h"
51 #include "saveload_filter.h"
52 
53 #include "../safeguards.h"
54 
56 
59 
60 uint32 _ttdp_version;
65 
73 };
74 
75 enum NeedLength {
76  NL_NONE = 0,
79 };
80 
82 static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
83 
85 struct ReadBuffer {
87  byte *bufp;
88  byte *bufe;
90  size_t read;
91 
96  ReadBuffer(LoadFilter *reader) : bufp(nullptr), bufe(nullptr), reader(reader), read(0)
97  {
98  }
99 
100  inline byte ReadByte()
101  {
102  if (this->bufp == this->bufe) {
103  size_t len = this->reader->Read(this->buf, lengthof(this->buf));
104  if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
105 
106  this->read += len;
107  this->bufp = this->buf;
108  this->bufe = this->buf + len;
109  }
110 
111  return *this->bufp++;
112  }
113 
118  size_t GetSize() const
119  {
120  return this->read - (this->bufe - this->bufp);
121  }
122 };
123 
124 
126 struct MemoryDumper {
127  std::vector<byte *> blocks;
128  byte *buf;
129  byte *bufe;
130 
132  MemoryDumper() : buf(nullptr), bufe(nullptr)
133  {
134  }
135 
136  ~MemoryDumper()
137  {
138  for (auto p : this->blocks) {
139  free(p);
140  }
141  }
142 
147  inline void WriteByte(byte b)
148  {
149  /* Are we at the end of this chunk? */
150  if (this->buf == this->bufe) {
151  this->buf = CallocT<byte>(MEMORY_CHUNK_SIZE);
152  this->blocks.push_back(this->buf);
153  this->bufe = this->buf + MEMORY_CHUNK_SIZE;
154  }
155 
156  *this->buf++ = b;
157  }
158 
163  void Flush(SaveFilter *writer)
164  {
165  uint i = 0;
166  size_t t = this->GetSize();
167 
168  while (t > 0) {
169  size_t to_write = min(MEMORY_CHUNK_SIZE, t);
170 
171  writer->Write(this->blocks[i++], to_write);
172  t -= to_write;
173  }
174 
175  writer->Finish();
176  }
177 
182  size_t GetSize() const
183  {
184  return this->blocks.size() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
185  }
186 };
187 
192  byte block_mode;
193  bool error;
194 
195  size_t obj_len;
196  int array_index, last_array_index;
197 
200 
203 
205  char *extra_msg;
206 
207  byte ff_state;
209 };
210 
212 
213 /* these define the chunks */
214 extern const ChunkHandler _gamelog_chunk_handlers[];
215 extern const ChunkHandler _map_chunk_handlers[];
216 extern const ChunkHandler _misc_chunk_handlers[];
217 extern const ChunkHandler _name_chunk_handlers[];
218 extern const ChunkHandler _cheat_chunk_handlers[] ;
219 extern const ChunkHandler _setting_chunk_handlers[];
220 extern const ChunkHandler _company_chunk_handlers[];
221 extern const ChunkHandler _engine_chunk_handlers[];
222 extern const ChunkHandler _veh_chunk_handlers[];
223 extern const ChunkHandler _waypoint_chunk_handlers[];
224 extern const ChunkHandler _depot_chunk_handlers[];
225 extern const ChunkHandler _order_chunk_handlers[];
226 extern const ChunkHandler _town_chunk_handlers[];
227 extern const ChunkHandler _sign_chunk_handlers[];
228 extern const ChunkHandler _station_chunk_handlers[];
229 extern const ChunkHandler _industry_chunk_handlers[];
230 extern const ChunkHandler _economy_chunk_handlers[];
231 extern const ChunkHandler _subsidy_chunk_handlers[];
233 extern const ChunkHandler _goal_chunk_handlers[];
234 extern const ChunkHandler _story_page_chunk_handlers[];
235 extern const ChunkHandler _ai_chunk_handlers[];
236 extern const ChunkHandler _game_chunk_handlers[];
238 extern const ChunkHandler _newgrf_chunk_handlers[];
239 extern const ChunkHandler _group_chunk_handlers[];
241 extern const ChunkHandler _autoreplace_chunk_handlers[];
242 extern const ChunkHandler _labelmaps_chunk_handlers[];
243 extern const ChunkHandler _linkgraph_chunk_handlers[];
244 extern const ChunkHandler _airport_chunk_handlers[];
245 extern const ChunkHandler _object_chunk_handlers[];
247 
249 static const ChunkHandler * const _chunk_handlers[] = {
250  _gamelog_chunk_handlers,
251  _map_chunk_handlers,
252  _misc_chunk_handlers,
255  _setting_chunk_handlers,
256  _veh_chunk_handlers,
257  _waypoint_chunk_handlers,
258  _depot_chunk_handlers,
259  _order_chunk_handlers,
260  _industry_chunk_handlers,
261  _economy_chunk_handlers,
262  _subsidy_chunk_handlers,
264  _goal_chunk_handlers,
265  _story_page_chunk_handlers,
266  _engine_chunk_handlers,
269  _station_chunk_handlers,
270  _company_chunk_handlers,
271  _ai_chunk_handlers,
272  _game_chunk_handlers,
274  _newgrf_chunk_handlers,
275  _group_chunk_handlers,
277  _autoreplace_chunk_handlers,
278  _labelmaps_chunk_handlers,
279  _linkgraph_chunk_handlers,
280  _airport_chunk_handlers,
281  _object_chunk_handlers,
283  nullptr,
284 };
285 
290 #define FOR_ALL_CHUNK_HANDLERS(ch) \
291  for (const ChunkHandler * const *chsc = _chunk_handlers; *chsc != nullptr; chsc++) \
292  for (const ChunkHandler *ch = *chsc; ch != nullptr; ch = (ch->flags & CH_LAST) ? nullptr : ch + 1)
293 
295 static void SlNullPointers()
296 {
297  _sl.action = SLA_NULL;
298 
299  /* We don't want any savegame conversion code to run
300  * during NULLing; especially those that try to get
301  * pointers from other pools. */
302  _sl_version = SAVEGAME_VERSION;
303 
304  DEBUG(sl, 1, "Nulling pointers");
305 
307  if (ch->ptrs_proc != nullptr) {
308  DEBUG(sl, 2, "Nulling pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
309  ch->ptrs_proc();
310  }
311  }
312 
313  DEBUG(sl, 1, "All pointers nulled");
314 
315  assert(_sl.action == SLA_NULL);
316 }
317 
326 void NORETURN SlError(StringID string, const char *extra_msg)
327 {
328  /* Distinguish between loading into _load_check_data vs. normal save/load. */
329  if (_sl.action == SLA_LOAD_CHECK) {
330  _load_check_data.error = string;
332  _load_check_data.error_data = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
333  } else {
334  _sl.error_str = string;
335  free(_sl.extra_msg);
336  _sl.extra_msg = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
337  }
338 
339  /* We have to nullptr all pointers here; we might be in a state where
340  * the pointers are actually filled with indices, which means that
341  * when we access them during cleaning the pool dereferences of
342  * those indices will be made with segmentation faults as result. */
343  if (_sl.action == SLA_LOAD || _sl.action == SLA_PTRS) SlNullPointers();
344 
345  /* Logging could be active. */
346  GamelogStopAnyAction();
347 
348  throw std::exception();
349 }
350 
358 void NORETURN SlErrorCorrupt(const char *msg)
359 {
360  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
361 }
362 
370 void NORETURN SlErrorCorruptFmt(const char *format, ...)
371 {
372  va_list ap;
373  char msg[256];
374 
375  va_start(ap, format);
376  vseprintf(msg, lastof(msg), format, ap);
377  va_end(ap);
378 
379  SlErrorCorrupt(msg);
380 }
381 
382 
383 typedef void (*AsyncSaveFinishProc)();
384 static std::atomic<AsyncSaveFinishProc> _async_save_finish;
385 static std::thread _save_thread;
386 
392 {
393  if (_exit_game) return;
394  while (_async_save_finish.load(std::memory_order_acquire) != nullptr) CSleep(10);
395 
396  _async_save_finish.store(proc, std::memory_order_release);
397 }
398 
403 {
404  AsyncSaveFinishProc proc = _async_save_finish.exchange(nullptr, std::memory_order_acq_rel);
405  if (proc == nullptr) return;
406 
407  proc();
408 
409  if (_save_thread.joinable()) {
410  _save_thread.join();
411  }
412 }
413 
419 {
420  return _sl.reader->ReadByte();
421 }
422 
427 void SlWriteByte(byte b)
428 {
429  _sl.dumper->WriteByte(b);
430 }
431 
432 static inline int SlReadUint16()
433 {
434  int x = SlReadByte() << 8;
435  return x | SlReadByte();
436 }
437 
438 static inline uint32 SlReadUint32()
439 {
440  uint32 x = SlReadUint16() << 16;
441  return x | SlReadUint16();
442 }
443 
444 static inline uint64 SlReadUint64()
445 {
446  uint32 x = SlReadUint32();
447  uint32 y = SlReadUint32();
448  return (uint64)x << 32 | y;
449 }
450 
451 static inline void SlWriteUint16(uint16 v)
452 {
453  SlWriteByte(GB(v, 8, 8));
454  SlWriteByte(GB(v, 0, 8));
455 }
456 
457 static inline void SlWriteUint32(uint32 v)
458 {
459  SlWriteUint16(GB(v, 16, 16));
460  SlWriteUint16(GB(v, 0, 16));
461 }
462 
463 static inline void SlWriteUint64(uint64 x)
464 {
465  SlWriteUint32((uint32)(x >> 32));
466  SlWriteUint32((uint32)x);
467 }
468 
474 static inline void SlSkipBytes(size_t length)
475 {
476  for (; length != 0; length--) SlReadByte();
477 }
478 
488 static uint SlReadSimpleGamma()
489 {
490  uint i = SlReadByte();
491  if (HasBit(i, 7)) {
492  i &= ~0x80;
493  if (HasBit(i, 6)) {
494  i &= ~0x40;
495  if (HasBit(i, 5)) {
496  i &= ~0x20;
497  if (HasBit(i, 4)) {
498  i &= ~0x10;
499  if (HasBit(i, 3)) {
500  SlErrorCorrupt("Unsupported gamma");
501  }
502  i = SlReadByte(); // 32 bits only.
503  }
504  i = (i << 8) | SlReadByte();
505  }
506  i = (i << 8) | SlReadByte();
507  }
508  i = (i << 8) | SlReadByte();
509  }
510  return i;
511 }
512 
530 static void SlWriteSimpleGamma(size_t i)
531 {
532  if (i >= (1 << 7)) {
533  if (i >= (1 << 14)) {
534  if (i >= (1 << 21)) {
535  if (i >= (1 << 28)) {
536  assert(i <= UINT32_MAX); // We can only support 32 bits for now.
537  SlWriteByte((byte)(0xF0));
538  SlWriteByte((byte)(i >> 24));
539  } else {
540  SlWriteByte((byte)(0xE0 | (i >> 24)));
541  }
542  SlWriteByte((byte)(i >> 16));
543  } else {
544  SlWriteByte((byte)(0xC0 | (i >> 16)));
545  }
546  SlWriteByte((byte)(i >> 8));
547  } else {
548  SlWriteByte((byte)(0x80 | (i >> 8)));
549  }
550  }
551  SlWriteByte((byte)i);
552 }
553 
555 static inline uint SlGetGammaLength(size_t i)
556 {
557  return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
558 }
559 
560 static inline uint SlReadSparseIndex()
561 {
562  return SlReadSimpleGamma();
563 }
564 
565 static inline void SlWriteSparseIndex(uint index)
566 {
567  SlWriteSimpleGamma(index);
568 }
569 
570 static inline uint SlReadArrayLength()
571 {
572  return SlReadSimpleGamma();
573 }
574 
575 static inline void SlWriteArrayLength(size_t length)
576 {
577  SlWriteSimpleGamma(length);
578 }
579 
580 static inline uint SlGetArrayLength(size_t length)
581 {
582  return SlGetGammaLength(length);
583 }
584 
591 static inline uint SlCalcConvMemLen(VarType conv)
592 {
593  static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
594  byte length = GB(conv, 4, 4);
595 
596  switch (length << 4) {
597  case SLE_VAR_STRB:
598  case SLE_VAR_STRBQ:
599  case SLE_VAR_STR:
600  case SLE_VAR_STRQ:
601  return SlReadArrayLength();
602 
603  default:
604  assert(length < lengthof(conv_mem_size));
605  return conv_mem_size[length];
606  }
607 }
608 
615 static inline byte SlCalcConvFileLen(VarType conv)
616 {
617  static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
618  byte length = GB(conv, 0, 4);
619  assert(length < lengthof(conv_file_size));
620  return conv_file_size[length];
621 }
622 
624 static inline size_t SlCalcRefLen()
625 {
626  return IsSavegameVersionBefore(SLV_69) ? 2 : 4;
627 }
628 
629 void SlSetArrayIndex(uint index)
630 {
632  _sl.array_index = index;
633 }
634 
635 static size_t _next_offs;
636 
642 {
643  int index;
644 
645  /* After reading in the whole array inside the loop
646  * we must have read in all the data, so we must be at end of current block. */
647  if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) SlErrorCorrupt("Invalid chunk size");
648 
649  for (;;) {
650  uint length = SlReadArrayLength();
651  if (length == 0) {
652  _next_offs = 0;
653  return -1;
654  }
655 
656  _sl.obj_len = --length;
657  _next_offs = _sl.reader->GetSize() + length;
658 
659  switch (_sl.block_mode) {
660  case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
661  case CH_ARRAY: index = _sl.array_index++; break;
662  default:
663  DEBUG(sl, 0, "SlIterateArray error");
664  return -1; // error
665  }
666 
667  if (length != 0) return index;
668  }
669 }
670 
675 {
676  while (SlIterateArray() != -1) {
677  SlSkipBytes(_next_offs - _sl.reader->GetSize());
678  }
679 }
680 
686 void SlSetLength(size_t length)
687 {
688  assert(_sl.action == SLA_SAVE);
689 
690  switch (_sl.need_length) {
691  case NL_WANTLENGTH:
692  _sl.need_length = NL_NONE;
693  switch (_sl.block_mode) {
694  case CH_RIFF:
695  /* Ugly encoding of >16M RIFF chunks
696  * The lower 24 bits are normal
697  * The uppermost 4 bits are bits 24:27 */
698  assert(length < (1 << 28));
699  SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
700  break;
701  case CH_ARRAY:
702  assert(_sl.last_array_index <= _sl.array_index);
703  while (++_sl.last_array_index <= _sl.array_index) {
704  SlWriteArrayLength(1);
705  }
706  SlWriteArrayLength(length + 1);
707  break;
708  case CH_SPARSE_ARRAY:
709  SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
710  SlWriteSparseIndex(_sl.array_index);
711  break;
712  default: NOT_REACHED();
713  }
714  break;
715 
716  case NL_CALCLENGTH:
717  _sl.obj_len += (int)length;
718  break;
719 
720  default: NOT_REACHED();
721  }
722 }
723 
730 static void SlCopyBytes(void *ptr, size_t length)
731 {
732  byte *p = (byte *)ptr;
733 
734  switch (_sl.action) {
735  case SLA_LOAD_CHECK:
736  case SLA_LOAD:
737  for (; length != 0; length--) *p++ = SlReadByte();
738  break;
739  case SLA_SAVE:
740  for (; length != 0; length--) SlWriteByte(*p++);
741  break;
742  default: NOT_REACHED();
743  }
744 }
745 
748 {
749  return _sl.obj_len;
750 }
751 
759 int64 ReadValue(const void *ptr, VarType conv)
760 {
761  switch (GetVarMemType(conv)) {
762  case SLE_VAR_BL: return (*(const bool *)ptr != 0);
763  case SLE_VAR_I8: return *(const int8 *)ptr;
764  case SLE_VAR_U8: return *(const byte *)ptr;
765  case SLE_VAR_I16: return *(const int16 *)ptr;
766  case SLE_VAR_U16: return *(const uint16*)ptr;
767  case SLE_VAR_I32: return *(const int32 *)ptr;
768  case SLE_VAR_U32: return *(const uint32*)ptr;
769  case SLE_VAR_I64: return *(const int64 *)ptr;
770  case SLE_VAR_U64: return *(const uint64*)ptr;
771  case SLE_VAR_NULL:return 0;
772  default: NOT_REACHED();
773  }
774 }
775 
783 void WriteValue(void *ptr, VarType conv, int64 val)
784 {
785  switch (GetVarMemType(conv)) {
786  case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
787  case SLE_VAR_I8: *(int8 *)ptr = val; break;
788  case SLE_VAR_U8: *(byte *)ptr = val; break;
789  case SLE_VAR_I16: *(int16 *)ptr = val; break;
790  case SLE_VAR_U16: *(uint16*)ptr = val; break;
791  case SLE_VAR_I32: *(int32 *)ptr = val; break;
792  case SLE_VAR_U32: *(uint32*)ptr = val; break;
793  case SLE_VAR_I64: *(int64 *)ptr = val; break;
794  case SLE_VAR_U64: *(uint64*)ptr = val; break;
795  case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
796  case SLE_VAR_NULL: break;
797  default: NOT_REACHED();
798  }
799 }
800 
809 static void SlSaveLoadConv(void *ptr, VarType conv)
810 {
811  switch (_sl.action) {
812  case SLA_SAVE: {
813  int64 x = ReadValue(ptr, conv);
814 
815  /* Write the value to the file and check if its value is in the desired range */
816  switch (GetVarFileType(conv)) {
817  case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
818  case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
819  case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
820  case SLE_FILE_STRINGID:
821  case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
822  case SLE_FILE_I32:
823  case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
824  case SLE_FILE_I64:
825  case SLE_FILE_U64: SlWriteUint64(x);break;
826  default: NOT_REACHED();
827  }
828  break;
829  }
830  case SLA_LOAD_CHECK:
831  case SLA_LOAD: {
832  int64 x;
833  /* Read a value from the file */
834  switch (GetVarFileType(conv)) {
835  case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
836  case SLE_FILE_U8: x = (byte )SlReadByte(); break;
837  case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
838  case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
839  case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
840  case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
841  case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
842  case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
843  case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
844  default: NOT_REACHED();
845  }
846 
847  /* Write The value to the struct. These ARE endian safe. */
848  WriteValue(ptr, conv, x);
849  break;
850  }
851  case SLA_PTRS: break;
852  case SLA_NULL: break;
853  default: NOT_REACHED();
854  }
855 }
856 
866 static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
867 {
868  if (ptr == nullptr) return 0;
869  return min(strlen(ptr), length - 1);
870 }
871 
881 static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
882 {
883  size_t len;
884  const char *str;
885 
886  switch (GetVarMemType(conv)) {
887  default: NOT_REACHED();
888  case SLE_VAR_STR:
889  case SLE_VAR_STRQ:
890  str = *(const char * const *)ptr;
891  len = SIZE_MAX;
892  break;
893  case SLE_VAR_STRB:
894  case SLE_VAR_STRBQ:
895  str = (const char *)ptr;
896  len = length;
897  break;
898  }
899 
900  len = SlCalcNetStringLen(str, len);
901  return len + SlGetArrayLength(len); // also include the length of the index
902 }
903 
910 static void SlString(void *ptr, size_t length, VarType conv)
911 {
912  switch (_sl.action) {
913  case SLA_SAVE: {
914  size_t len;
915  switch (GetVarMemType(conv)) {
916  default: NOT_REACHED();
917  case SLE_VAR_STRB:
918  case SLE_VAR_STRBQ:
919  len = SlCalcNetStringLen((char *)ptr, length);
920  break;
921  case SLE_VAR_STR:
922  case SLE_VAR_STRQ:
923  ptr = *(char **)ptr;
924  len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
925  break;
926  }
927 
928  SlWriteArrayLength(len);
929  SlCopyBytes(ptr, len);
930  break;
931  }
932  case SLA_LOAD_CHECK:
933  case SLA_LOAD: {
934  size_t len = SlReadArrayLength();
935 
936  switch (GetVarMemType(conv)) {
937  default: NOT_REACHED();
938  case SLE_VAR_STRB:
939  case SLE_VAR_STRBQ:
940  if (len >= length) {
941  DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
942  SlCopyBytes(ptr, length);
943  SlSkipBytes(len - length);
944  len = length - 1;
945  } else {
946  SlCopyBytes(ptr, len);
947  }
948  break;
949  case SLE_VAR_STR:
950  case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
951  free(*(char **)ptr);
952  if (len == 0) {
953  *(char **)ptr = nullptr;
954  return;
955  } else {
956  *(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
957  ptr = *(char **)ptr;
958  SlCopyBytes(ptr, len);
959  }
960  break;
961  }
962 
963  ((char *)ptr)[len] = '\0'; // properly terminate the string
965  if ((conv & SLF_ALLOW_CONTROL) != 0) {
966  settings = settings | SVS_ALLOW_CONTROL_CODE;
968  str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
969  }
970  }
971  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
972  settings = settings | SVS_ALLOW_NEWLINE;
973  }
974  str_validate((char *)ptr, (char *)ptr + len, settings);
975  break;
976  }
977  case SLA_PTRS: break;
978  case SLA_NULL: break;
979  default: NOT_REACHED();
980  }
981 }
982 
988 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
989 {
990  return SlCalcConvFileLen(conv) * length;
991 }
992 
999 void SlArray(void *array, size_t length, VarType conv)
1000 {
1001  if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
1002 
1003  /* Automatically calculate the length? */
1004  if (_sl.need_length != NL_NONE) {
1005  SlSetLength(SlCalcArrayLen(length, conv));
1006  /* Determine length only? */
1007  if (_sl.need_length == NL_CALCLENGTH) return;
1008  }
1009 
1010  /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1011  * as a byte-type. So detect this, and adjust array size accordingly */
1012  if (_sl.action != SLA_SAVE && _sl_version == 0) {
1013  /* all arrays except difficulty settings */
1014  if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1015  conv == SLE_INT32 || conv == SLE_UINT32) {
1016  SlCopyBytes(array, length * SlCalcConvFileLen(conv));
1017  return;
1018  }
1019  /* used for conversion of Money 32bit->64bit */
1020  if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1021  for (uint i = 0; i < length; i++) {
1022  ((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
1023  }
1024  return;
1025  }
1026  }
1027 
1028  /* If the size of elements is 1 byte both in file and memory, no special
1029  * conversion is needed, use specialized copy-copy function to speed up things */
1030  if (conv == SLE_INT8 || conv == SLE_UINT8) {
1031  SlCopyBytes(array, length);
1032  } else {
1033  byte *a = (byte*)array;
1034  byte mem_size = SlCalcConvMemLen(conv);
1035 
1036  for (; length != 0; length --) {
1037  SlSaveLoadConv(a, conv);
1038  a += mem_size; // get size
1039  }
1040  }
1041 }
1042 
1043 
1054 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1055 {
1056  assert(_sl.action == SLA_SAVE);
1057 
1058  if (obj == nullptr) return 0;
1059 
1060  switch (rt) {
1061  case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1062  case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1063  case REF_STATION: return ((const Station*)obj)->index + 1;
1064  case REF_TOWN: return ((const Town*)obj)->index + 1;
1065  case REF_ORDER: return ((const Order*)obj)->index + 1;
1066  case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1067  case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1068  case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1069  case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1070  case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1071  case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1072  case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1073  default: NOT_REACHED();
1074  }
1075 }
1076 
1087 static void *IntToReference(size_t index, SLRefType rt)
1088 {
1089  assert_compile(sizeof(size_t) <= sizeof(void *));
1090 
1091  assert(_sl.action == SLA_PTRS);
1092 
1093  /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1094  * and should be loaded like that */
1095  if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) {
1096  rt = REF_VEHICLE;
1097  }
1098 
1099  /* No need to look up nullptr pointers, just return immediately */
1100  if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return nullptr;
1101 
1102  /* Correct index. Old vehicles were saved differently:
1103  * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1104  if (rt != REF_VEHICLE_OLD) index--;
1105 
1106  switch (rt) {
1107  case REF_ORDERLIST:
1108  if (OrderList::IsValidID(index)) return OrderList::Get(index);
1109  SlErrorCorrupt("Referencing invalid OrderList");
1110 
1111  case REF_ORDER:
1112  if (Order::IsValidID(index)) return Order::Get(index);
1113  /* in old versions, invalid order was used to mark end of order list */
1114  if (IsSavegameVersionBefore(SLV_5, 2)) return nullptr;
1115  SlErrorCorrupt("Referencing invalid Order");
1116 
1117  case REF_VEHICLE_OLD:
1118  case REF_VEHICLE:
1119  if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1120  SlErrorCorrupt("Referencing invalid Vehicle");
1121 
1122  case REF_STATION:
1123  if (Station::IsValidID(index)) return Station::Get(index);
1124  SlErrorCorrupt("Referencing invalid Station");
1125 
1126  case REF_TOWN:
1127  if (Town::IsValidID(index)) return Town::Get(index);
1128  SlErrorCorrupt("Referencing invalid Town");
1129 
1130  case REF_ROADSTOPS:
1131  if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1132  SlErrorCorrupt("Referencing invalid RoadStop");
1133 
1134  case REF_ENGINE_RENEWS:
1135  if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1136  SlErrorCorrupt("Referencing invalid EngineRenew");
1137 
1138  case REF_CARGO_PACKET:
1139  if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1140  SlErrorCorrupt("Referencing invalid CargoPacket");
1141 
1142  case REF_STORAGE:
1143  if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1144  SlErrorCorrupt("Referencing invalid PersistentStorage");
1145 
1146  case REF_LINK_GRAPH:
1147  if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1148  SlErrorCorrupt("Referencing invalid LinkGraph");
1149 
1150  case REF_LINK_GRAPH_JOB:
1151  if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1152  SlErrorCorrupt("Referencing invalid LinkGraphJob");
1153 
1154  default: NOT_REACHED();
1155  }
1156 }
1157 
1162 static inline size_t SlCalcListLen(const void *list)
1163 {
1164  const std::list<void *> *l = (const std::list<void *> *) list;
1165 
1166  int type_size = IsSavegameVersionBefore(SLV_69) ? 2 : 4;
1167  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1168  * of the list */
1169  return l->size() * type_size + type_size;
1170 }
1171 
1172 
1178 static void SlList(void *list, SLRefType conv)
1179 {
1180  /* Automatically calculate the length? */
1181  if (_sl.need_length != NL_NONE) {
1182  SlSetLength(SlCalcListLen(list));
1183  /* Determine length only? */
1184  if (_sl.need_length == NL_CALCLENGTH) return;
1185  }
1186 
1187  typedef std::list<void *> PtrList;
1188  PtrList *l = (PtrList *)list;
1189 
1190  switch (_sl.action) {
1191  case SLA_SAVE: {
1192  SlWriteUint32((uint32)l->size());
1193 
1194  PtrList::iterator iter;
1195  for (iter = l->begin(); iter != l->end(); ++iter) {
1196  void *ptr = *iter;
1197  SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
1198  }
1199  break;
1200  }
1201  case SLA_LOAD_CHECK:
1202  case SLA_LOAD: {
1203  size_t length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1204 
1205  /* Load each reference and push to the end of the list */
1206  for (size_t i = 0; i < length; i++) {
1207  size_t data = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1208  l->push_back((void *)data);
1209  }
1210  break;
1211  }
1212  case SLA_PTRS: {
1213  PtrList temp = *l;
1214 
1215  l->clear();
1216  PtrList::iterator iter;
1217  for (iter = temp.begin(); iter != temp.end(); ++iter) {
1218  void *ptr = IntToReference((size_t)*iter, conv);
1219  l->push_back(ptr);
1220  }
1221  break;
1222  }
1223  case SLA_NULL:
1224  l->clear();
1225  break;
1226  default: NOT_REACHED();
1227  }
1228 }
1229 
1230 
1234 template <typename T>
1236  typedef std::deque<T> SlDequeT;
1237 public:
1243  static size_t SlCalcDequeLen(const void *deque, VarType conv)
1244  {
1245  const SlDequeT *l = (const SlDequeT *)deque;
1246 
1247  int type_size = 4;
1248  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1249  * of the list */
1250  return l->size() * SlCalcConvFileLen(conv) + type_size;
1251  }
1252 
1258  static void SlDeque(void *deque, VarType conv)
1259  {
1260  SlDequeT *l = (SlDequeT *)deque;
1261 
1262  switch (_sl.action) {
1263  case SLA_SAVE: {
1264  SlWriteUint32((uint32)l->size());
1265 
1266  typename SlDequeT::iterator iter;
1267  for (iter = l->begin(); iter != l->end(); ++iter) {
1268  SlSaveLoadConv(&(*iter), conv);
1269  }
1270  break;
1271  }
1272  case SLA_LOAD_CHECK:
1273  case SLA_LOAD: {
1274  size_t length = SlReadUint32();
1275 
1276  /* Load each value and push to the end of the deque */
1277  for (size_t i = 0; i < length; i++) {
1278  T data;
1279  SlSaveLoadConv(&data, conv);
1280  l->push_back(data);
1281  }
1282  break;
1283  }
1284  case SLA_PTRS:
1285  break;
1286  case SLA_NULL:
1287  l->clear();
1288  break;
1289  default: NOT_REACHED();
1290  }
1291  }
1292 };
1293 
1294 
1300 static inline size_t SlCalcDequeLen(const void *deque, VarType conv)
1301 {
1302  switch (GetVarMemType(conv)) {
1303  case SLE_VAR_BL:
1304  return SlDequeHelper<bool>::SlCalcDequeLen(deque, conv);
1305  case SLE_VAR_I8:
1306  case SLE_VAR_U8:
1307  return SlDequeHelper<uint8>::SlCalcDequeLen(deque, conv);
1308  case SLE_VAR_I16:
1309  case SLE_VAR_U16:
1310  return SlDequeHelper<uint16>::SlCalcDequeLen(deque, conv);
1311  case SLE_VAR_I32:
1312  case SLE_VAR_U32:
1313  return SlDequeHelper<uint32>::SlCalcDequeLen(deque, conv);
1314  case SLE_VAR_I64:
1315  case SLE_VAR_U64:
1316  return SlDequeHelper<uint64>::SlCalcDequeLen(deque, conv);
1317  default: NOT_REACHED();
1318  }
1319 }
1320 
1321 
1327 static void SlDeque(void *deque, VarType conv)
1328 {
1329  switch (GetVarMemType(conv)) {
1330  case SLE_VAR_BL:
1331  SlDequeHelper<bool>::SlDeque(deque, conv);
1332  break;
1333  case SLE_VAR_I8:
1334  case SLE_VAR_U8:
1335  SlDequeHelper<uint8>::SlDeque(deque, conv);
1336  break;
1337  case SLE_VAR_I16:
1338  case SLE_VAR_U16:
1339  SlDequeHelper<uint16>::SlDeque(deque, conv);
1340  break;
1341  case SLE_VAR_I32:
1342  case SLE_VAR_U32:
1343  SlDequeHelper<uint32>::SlDeque(deque, conv);
1344  break;
1345  case SLE_VAR_I64:
1346  case SLE_VAR_U64:
1347  SlDequeHelper<uint64>::SlDeque(deque, conv);
1348  break;
1349  default: NOT_REACHED();
1350  }
1351 }
1352 
1353 
1355 static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
1356 {
1357  if (_sl_version < sld->version_from || _sl_version >= sld->version_to) return false;
1358  if (sld->conv & SLF_NOT_IN_SAVE) return false;
1359 
1360  return true;
1361 }
1362 
1368 static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
1369 {
1370  if ((sld->conv & SLF_NO_NETWORK_SYNC) && _sl.action != SLA_SAVE && _networking && !_network_server) {
1371  SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
1372  return true;
1373  }
1374 
1375  return false;
1376 }
1377 
1384 size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
1385 {
1386  size_t length = 0;
1387 
1388  /* Need to determine the length and write a length tag. */
1389  for (; sld->cmd != SL_END; sld++) {
1390  length += SlCalcObjMemberLength(object, sld);
1391  }
1392  return length;
1393 }
1394 
1395 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
1396 {
1397  assert(_sl.action == SLA_SAVE);
1398 
1399  switch (sld->cmd) {
1400  case SL_VAR:
1401  case SL_REF:
1402  case SL_ARR:
1403  case SL_STR:
1404  case SL_LST:
1405  case SL_DEQUE:
1406  /* CONDITIONAL saveload types depend on the savegame version */
1407  if (!SlIsObjectValidInSavegame(sld)) break;
1408 
1409  switch (sld->cmd) {
1410  case SL_VAR: return SlCalcConvFileLen(sld->conv);
1411  case SL_REF: return SlCalcRefLen();
1412  case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
1413  case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
1414  case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
1415  case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld->conv);
1416  default: NOT_REACHED();
1417  }
1418  break;
1419  case SL_WRITEBYTE: return 1; // a byte is logically of size 1
1420  case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
1421  case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
1422  default: NOT_REACHED();
1423  }
1424  return 0;
1425 }
1426 
1427 #ifdef OTTD_ASSERT
1428 
1434 static bool IsVariableSizeRight(const SaveLoad *sld)
1435 {
1436  switch (sld->cmd) {
1437  case SL_VAR:
1438  switch (GetVarMemType(sld->conv)) {
1439  case SLE_VAR_BL:
1440  return sld->size == sizeof(bool);
1441  case SLE_VAR_I8:
1442  case SLE_VAR_U8:
1443  return sld->size == sizeof(int8);
1444  case SLE_VAR_I16:
1445  case SLE_VAR_U16:
1446  return sld->size == sizeof(int16);
1447  case SLE_VAR_I32:
1448  case SLE_VAR_U32:
1449  return sld->size == sizeof(int32);
1450  case SLE_VAR_I64:
1451  case SLE_VAR_U64:
1452  return sld->size == sizeof(int64);
1453  default:
1454  return sld->size == sizeof(void *);
1455  }
1456  case SL_REF:
1457  /* These should all be pointer sized. */
1458  return sld->size == sizeof(void *);
1459 
1460  case SL_STR:
1461  /* These should be pointer sized, or fixed array. */
1462  return sld->size == sizeof(void *) || sld->size == sld->length;
1463 
1464  default:
1465  return true;
1466  }
1467 }
1468 
1469 #endif /* OTTD_ASSERT */
1470 
1471 bool SlObjectMember(void *ptr, const SaveLoad *sld)
1472 {
1473 #ifdef OTTD_ASSERT
1474  assert(IsVariableSizeRight(sld));
1475 #endif
1476 
1477  VarType conv = GB(sld->conv, 0, 8);
1478  switch (sld->cmd) {
1479  case SL_VAR:
1480  case SL_REF:
1481  case SL_ARR:
1482  case SL_STR:
1483  case SL_LST:
1484  case SL_DEQUE:
1485  /* CONDITIONAL saveload types depend on the savegame version */
1486  if (!SlIsObjectValidInSavegame(sld)) return false;
1487  if (SlSkipVariableOnLoad(sld)) return false;
1488 
1489  switch (sld->cmd) {
1490  case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1491  case SL_REF: // Reference variable, translate
1492  switch (_sl.action) {
1493  case SLA_SAVE:
1494  SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1495  break;
1496  case SLA_LOAD_CHECK:
1497  case SLA_LOAD:
1498  *(size_t *)ptr = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1499  break;
1500  case SLA_PTRS:
1501  *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1502  break;
1503  case SLA_NULL:
1504  *(void **)ptr = nullptr;
1505  break;
1506  default: NOT_REACHED();
1507  }
1508  break;
1509  case SL_ARR: SlArray(ptr, sld->length, conv); break;
1510  case SL_STR: SlString(ptr, sld->length, sld->conv); break;
1511  case SL_LST: SlList(ptr, (SLRefType)conv); break;
1512  case SL_DEQUE: SlDeque(ptr, conv); break;
1513  default: NOT_REACHED();
1514  }
1515  break;
1516 
1517  /* SL_WRITEBYTE writes a value to the savegame to identify the type of an object.
1518  * When loading, the value is read explicitly with SlReadByte() to determine which
1519  * object description to use. */
1520  case SL_WRITEBYTE:
1521  switch (_sl.action) {
1522  case SLA_SAVE: SlWriteByte(*(uint8 *)ptr); break;
1523  case SLA_LOAD_CHECK:
1524  case SLA_LOAD:
1525  case SLA_PTRS:
1526  case SLA_NULL: break;
1527  default: NOT_REACHED();
1528  }
1529  break;
1530 
1531  /* SL_VEH_INCLUDE loads common code for vehicles */
1532  case SL_VEH_INCLUDE:
1533  SlObject(ptr, GetVehicleDescription(VEH_END));
1534  break;
1535 
1536  case SL_ST_INCLUDE:
1538  break;
1539 
1540  default: NOT_REACHED();
1541  }
1542  return true;
1543 }
1544 
1550 void SlObject(void *object, const SaveLoad *sld)
1551 {
1552  /* Automatically calculate the length? */
1553  if (_sl.need_length != NL_NONE) {
1554  SlSetLength(SlCalcObjLength(object, sld));
1555  if (_sl.need_length == NL_CALCLENGTH) return;
1556  }
1557 
1558  for (; sld->cmd != SL_END; sld++) {
1559  void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld);
1560  SlObjectMember(ptr, sld);
1561  }
1562 }
1563 
1569 {
1570  SlObject(nullptr, (const SaveLoad*)sldg);
1571 }
1572 
1578 void SlAutolength(AutolengthProc *proc, void *arg)
1579 {
1580  size_t offs;
1581 
1582  assert(_sl.action == SLA_SAVE);
1583 
1584  /* Tell it to calculate the length */
1585  _sl.need_length = NL_CALCLENGTH;
1586  _sl.obj_len = 0;
1587  proc(arg);
1588 
1589  /* Setup length */
1590  _sl.need_length = NL_WANTLENGTH;
1591  SlSetLength(_sl.obj_len);
1592 
1593  offs = _sl.dumper->GetSize() + _sl.obj_len;
1594 
1595  /* And write the stuff */
1596  proc(arg);
1597 
1598  if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
1599 }
1600 
1605 static void SlLoadChunk(const ChunkHandler *ch)
1606 {
1607  byte m = SlReadByte();
1608  size_t len;
1609  size_t endoffs;
1610 
1611  _sl.block_mode = m;
1612  _sl.obj_len = 0;
1613 
1614  switch (m) {
1615  case CH_ARRAY:
1616  _sl.array_index = 0;
1617  ch->load_proc();
1618  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1619  break;
1620  case CH_SPARSE_ARRAY:
1621  ch->load_proc();
1622  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1623  break;
1624  default:
1625  if ((m & 0xF) == CH_RIFF) {
1626  /* Read length */
1627  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1628  len += SlReadUint16();
1629  _sl.obj_len = len;
1630  endoffs = _sl.reader->GetSize() + len;
1631  ch->load_proc();
1632  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1633  } else {
1634  SlErrorCorrupt("Invalid chunk type");
1635  }
1636  break;
1637  }
1638 }
1639 
1645 static void SlLoadCheckChunk(const ChunkHandler *ch)
1646 {
1647  byte m = SlReadByte();
1648  size_t len;
1649  size_t endoffs;
1650 
1651  _sl.block_mode = m;
1652  _sl.obj_len = 0;
1653 
1654  switch (m) {
1655  case CH_ARRAY:
1656  _sl.array_index = 0;
1657  if (ch->load_check_proc) {
1658  ch->load_check_proc();
1659  } else {
1660  SlSkipArray();
1661  }
1662  break;
1663  case CH_SPARSE_ARRAY:
1664  if (ch->load_check_proc) {
1665  ch->load_check_proc();
1666  } else {
1667  SlSkipArray();
1668  }
1669  break;
1670  default:
1671  if ((m & 0xF) == CH_RIFF) {
1672  /* Read length */
1673  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1674  len += SlReadUint16();
1675  _sl.obj_len = len;
1676  endoffs = _sl.reader->GetSize() + len;
1677  if (ch->load_check_proc) {
1678  ch->load_check_proc();
1679  } else {
1680  SlSkipBytes(len);
1681  }
1682  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1683  } else {
1684  SlErrorCorrupt("Invalid chunk type");
1685  }
1686  break;
1687  }
1688 }
1689 
1694 static ChunkSaveLoadProc *_stub_save_proc;
1695 
1701 static inline void SlStubSaveProc2(void *arg)
1702 {
1703  _stub_save_proc();
1704 }
1705 
1711 static void SlStubSaveProc()
1712 {
1713  SlAutolength(SlStubSaveProc2, nullptr);
1714 }
1715 
1721 static void SlSaveChunk(const ChunkHandler *ch)
1722 {
1723  ChunkSaveLoadProc *proc = ch->save_proc;
1724 
1725  /* Don't save any chunk information if there is no save handler. */
1726  if (proc == nullptr) return;
1727 
1728  SlWriteUint32(ch->id);
1729  DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1730 
1731  if (ch->flags & CH_AUTO_LENGTH) {
1732  /* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */
1733  _stub_save_proc = proc;
1734  proc = SlStubSaveProc;
1735  }
1736 
1737  _sl.block_mode = ch->flags & CH_TYPE_MASK;
1738  switch (ch->flags & CH_TYPE_MASK) {
1739  case CH_RIFF:
1740  _sl.need_length = NL_WANTLENGTH;
1741  proc();
1742  break;
1743  case CH_ARRAY:
1744  _sl.last_array_index = 0;
1745  SlWriteByte(CH_ARRAY);
1746  proc();
1747  SlWriteArrayLength(0); // Terminate arrays
1748  break;
1749  case CH_SPARSE_ARRAY:
1750  SlWriteByte(CH_SPARSE_ARRAY);
1751  proc();
1752  SlWriteArrayLength(0); // Terminate arrays
1753  break;
1754  default: NOT_REACHED();
1755  }
1756 }
1757 
1759 static void SlSaveChunks()
1760 {
1762  SlSaveChunk(ch);
1763  }
1764 
1765  /* Terminator */
1766  SlWriteUint32(0);
1767 }
1768 
1775 static const ChunkHandler *SlFindChunkHandler(uint32 id)
1776 {
1777  FOR_ALL_CHUNK_HANDLERS(ch) if (ch->id == id) return ch;
1778  return nullptr;
1779 }
1780 
1782 static void SlLoadChunks()
1783 {
1784  uint32 id;
1785  const ChunkHandler *ch;
1786 
1787  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1788  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1789 
1790  ch = SlFindChunkHandler(id);
1791  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
1792  SlLoadChunk(ch);
1793  }
1794 }
1795 
1797 static void SlLoadCheckChunks()
1798 {
1799  uint32 id;
1800  const ChunkHandler *ch;
1801 
1802  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1803  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1804 
1805  ch = SlFindChunkHandler(id);
1806  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
1807  SlLoadCheckChunk(ch);
1808  }
1809 }
1810 
1812 static void SlFixPointers()
1813 {
1814  _sl.action = SLA_PTRS;
1815 
1816  DEBUG(sl, 1, "Fixing pointers");
1817 
1819  if (ch->ptrs_proc != nullptr) {
1820  DEBUG(sl, 2, "Fixing pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1821  ch->ptrs_proc();
1822  }
1823  }
1824 
1825  DEBUG(sl, 1, "All pointers fixed");
1826 
1827  assert(_sl.action == SLA_PTRS);
1828 }
1829 
1830 
1833  FILE *file;
1834  long begin;
1835 
1840  FileReader(FILE *file) : LoadFilter(nullptr), file(file), begin(ftell(file))
1841  {
1842  }
1843 
1846  {
1847  if (this->file != nullptr) fclose(this->file);
1848  this->file = nullptr;
1849 
1850  /* Make sure we don't double free. */
1851  _sl.sf = nullptr;
1852  }
1853 
1854  size_t Read(byte *buf, size_t size) override
1855  {
1856  /* We're in the process of shutting down, i.e. in "failure" mode. */
1857  if (this->file == nullptr) return 0;
1858 
1859  return fread(buf, 1, size, this->file);
1860  }
1861 
1862  void Reset() override
1863  {
1864  clearerr(this->file);
1865  if (fseek(this->file, this->begin, SEEK_SET)) {
1866  DEBUG(sl, 1, "Could not reset the file reading");
1867  }
1868  }
1869 };
1870 
1873  FILE *file;
1874 
1879  FileWriter(FILE *file) : SaveFilter(nullptr), file(file)
1880  {
1881  }
1882 
1885  {
1886  this->Finish();
1887 
1888  /* Make sure we don't double free. */
1889  _sl.sf = nullptr;
1890  }
1891 
1892  void Write(byte *buf, size_t size) override
1893  {
1894  /* We're in the process of shutting down, i.e. in "failure" mode. */
1895  if (this->file == nullptr) return;
1896 
1897  if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
1898  }
1899 
1900  void Finish() override
1901  {
1902  if (this->file != nullptr) fclose(this->file);
1903  this->file = nullptr;
1904  }
1905 };
1906 
1907 /*******************************************
1908  ********** START OF LZO CODE **************
1909  *******************************************/
1910 
1911 #ifdef WITH_LZO
1912 #include <lzo/lzo1x.h>
1913 
1915 static const uint LZO_BUFFER_SIZE = 8192;
1916 
1924  {
1925  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
1926  }
1927 
1928  size_t Read(byte *buf, size_t ssize) override
1929  {
1930  assert(ssize >= LZO_BUFFER_SIZE);
1931 
1932  /* Buffer size is from the LZO docs plus the chunk header size. */
1933  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1934  uint32 tmp[2];
1935  uint32 size;
1936  lzo_uint len = ssize;
1937 
1938  /* Read header*/
1939  if (this->chain->Read((byte*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
1940 
1941  /* Check if size is bad */
1942  ((uint32*)out)[0] = size = tmp[1];
1943 
1944  if (_sl_version != SL_MIN_VERSION) {
1945  tmp[0] = TO_BE32(tmp[0]);
1946  size = TO_BE32(size);
1947  }
1948 
1949  if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
1950 
1951  /* Read block */
1952  if (this->chain->Read(out + sizeof(uint32), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
1953 
1954  /* Verify checksum */
1955  if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
1956 
1957  /* Decompress */
1958  int ret = lzo1x_decompress_safe(out + sizeof(uint32) * 1, size, buf, &len, nullptr);
1959  if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
1960  return len;
1961  }
1962 };
1963 
1971  LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
1972  {
1973  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
1974  }
1975 
1976  void Write(byte *buf, size_t size) override
1977  {
1978  const lzo_bytep in = buf;
1979  /* Buffer size is from the LZO docs plus the chunk header size. */
1980  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1981  byte wrkmem[LZO1X_1_MEM_COMPRESS];
1982  lzo_uint outlen;
1983 
1984  do {
1985  /* Compress up to LZO_BUFFER_SIZE bytes at once. */
1986  lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
1987  lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
1988  ((uint32*)out)[1] = TO_BE32((uint32)outlen);
1989  ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
1990  this->chain->Write(out, outlen + sizeof(uint32) * 2);
1991 
1992  /* Move to next data chunk. */
1993  size -= len;
1994  in += len;
1995  } while (size > 0);
1996  }
1997 };
1998 
1999 #endif /* WITH_LZO */
2000 
2001 /*********************************************
2002  ******** START OF NOCOMP CODE (uncompressed)*
2003  *********************************************/
2004 
2012  {
2013  }
2014 
2015  size_t Read(byte *buf, size_t size) override
2016  {
2017  return this->chain->Read(buf, size);
2018  }
2019 };
2020 
2028  NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2029  {
2030  }
2031 
2032  void Write(byte *buf, size_t size) override
2033  {
2034  this->chain->Write(buf, size);
2035  }
2036 };
2037 
2038 /********************************************
2039  ********** START OF ZLIB CODE **************
2040  ********************************************/
2041 
2042 #if defined(WITH_ZLIB)
2043 #include <zlib.h>
2044 
2047  z_stream z;
2048  byte fread_buf[MEMORY_CHUNK_SIZE];
2049 
2055  {
2056  memset(&this->z, 0, sizeof(this->z));
2057  if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2058  }
2059 
2062  {
2063  inflateEnd(&this->z);
2064  }
2065 
2066  size_t Read(byte *buf, size_t size) override
2067  {
2068  this->z.next_out = buf;
2069  this->z.avail_out = (uint)size;
2070 
2071  do {
2072  /* read more bytes from the file? */
2073  if (this->z.avail_in == 0) {
2074  this->z.next_in = this->fread_buf;
2075  this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2076  }
2077 
2078  /* inflate the data */
2079  int r = inflate(&this->z, 0);
2080  if (r == Z_STREAM_END) break;
2081 
2082  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2083  } while (this->z.avail_out != 0);
2084 
2085  return size - this->z.avail_out;
2086  }
2087 };
2088 
2091  z_stream z;
2092 
2098  ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2099  {
2100  memset(&this->z, 0, sizeof(this->z));
2101  if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2102  }
2103 
2106  {
2107  deflateEnd(&this->z);
2108  }
2109 
2116  void WriteLoop(byte *p, size_t len, int mode)
2117  {
2118  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2119  uint n;
2120  this->z.next_in = p;
2121  this->z.avail_in = (uInt)len;
2122  do {
2123  this->z.next_out = buf;
2124  this->z.avail_out = sizeof(buf);
2125 
2133  int r = deflate(&this->z, mode);
2134 
2135  /* bytes were emitted? */
2136  if ((n = sizeof(buf) - this->z.avail_out) != 0) {
2137  this->chain->Write(buf, n);
2138  }
2139  if (r == Z_STREAM_END) break;
2140 
2141  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2142  } while (this->z.avail_in || !this->z.avail_out);
2143  }
2144 
2145  void Write(byte *buf, size_t size) override
2146  {
2147  this->WriteLoop(buf, size, 0);
2148  }
2149 
2150  void Finish() override
2151  {
2152  this->WriteLoop(nullptr, 0, Z_FINISH);
2153  this->chain->Finish();
2154  }
2155 };
2156 
2157 #endif /* WITH_ZLIB */
2158 
2159 /********************************************
2160  ********** START OF LZMA CODE **************
2161  ********************************************/
2162 
2163 #if defined(WITH_LIBLZMA)
2164 #include <lzma.h>
2165 
2172 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2173 
2176  lzma_stream lzma;
2177  byte fread_buf[MEMORY_CHUNK_SIZE];
2178 
2183  LZMALoadFilter(LoadFilter *chain) : LoadFilter(chain), lzma(_lzma_init)
2184  {
2185  /* Allow saves up to 256 MB uncompressed */
2186  if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2187  }
2188 
2191  {
2192  lzma_end(&this->lzma);
2193  }
2194 
2195  size_t Read(byte *buf, size_t size) override
2196  {
2197  this->lzma.next_out = buf;
2198  this->lzma.avail_out = size;
2199 
2200  do {
2201  /* read more bytes from the file? */
2202  if (this->lzma.avail_in == 0) {
2203  this->lzma.next_in = this->fread_buf;
2204  this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2205  }
2206 
2207  /* inflate the data */
2208  lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2209  if (r == LZMA_STREAM_END) break;
2210  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2211  } while (this->lzma.avail_out != 0);
2212 
2213  return size - this->lzma.avail_out;
2214  }
2215 };
2216 
2219  lzma_stream lzma;
2220 
2226  LZMASaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain), lzma(_lzma_init)
2227  {
2228  if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2229  }
2230 
2233  {
2234  lzma_end(&this->lzma);
2235  }
2236 
2243  void WriteLoop(byte *p, size_t len, lzma_action action)
2244  {
2245  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2246  size_t n;
2247  this->lzma.next_in = p;
2248  this->lzma.avail_in = len;
2249  do {
2250  this->lzma.next_out = buf;
2251  this->lzma.avail_out = sizeof(buf);
2252 
2253  lzma_ret r = lzma_code(&this->lzma, action);
2254 
2255  /* bytes were emitted? */
2256  if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
2257  this->chain->Write(buf, n);
2258  }
2259  if (r == LZMA_STREAM_END) break;
2260  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2261  } while (this->lzma.avail_in || !this->lzma.avail_out);
2262  }
2263 
2264  void Write(byte *buf, size_t size) override
2265  {
2266  this->WriteLoop(buf, size, LZMA_RUN);
2267  }
2268 
2269  void Finish() override
2270  {
2271  this->WriteLoop(nullptr, 0, LZMA_FINISH);
2272  this->chain->Finish();
2273  }
2274 };
2275 
2276 #endif /* WITH_LIBLZMA */
2277 
2278 /*******************************************
2279  ************* END OF CODE *****************
2280  *******************************************/
2281 
2284  const char *name;
2285  uint32 tag;
2286 
2287  LoadFilter *(*init_load)(LoadFilter *chain);
2288  SaveFilter *(*init_write)(SaveFilter *chain, byte compression);
2289 
2293 };
2294 
2297 #if defined(WITH_LZO)
2298  /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2299  {"lzo", TO_BE32X('OTTD'), CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2300 #else
2301  {"lzo", TO_BE32X('OTTD'), nullptr, nullptr, 0, 0, 0},
2302 #endif
2303  /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2304  {"none", TO_BE32X('OTTN'), CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2305 #if defined(WITH_ZLIB)
2306  /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2307  * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2308  * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2309  {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2310 #else
2311  {"zlib", TO_BE32X('OTTZ'), nullptr, nullptr, 0, 0, 0},
2312 #endif
2313 #if defined(WITH_LIBLZMA)
2314  /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2315  * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2316  * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2317  * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2318  * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2319  {"lzma", TO_BE32X('OTTX'), CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2320 #else
2321  {"lzma", TO_BE32X('OTTX'), nullptr, nullptr, 0, 0, 0},
2322 #endif
2323 };
2324 
2332 static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
2333 {
2334  const SaveLoadFormat *def = lastof(_saveload_formats);
2335 
2336  /* find default savegame format, the highest one with which files can be written */
2337  while (!def->init_write) def--;
2338 
2339  if (!StrEmpty(s)) {
2340  /* Get the ":..." of the compression level out of the way */
2341  char *complevel = strrchr(s, ':');
2342  if (complevel != nullptr) *complevel = '\0';
2343 
2344  for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
2345  if (slf->init_write != nullptr && strcmp(s, slf->name) == 0) {
2346  *compression_level = slf->default_compression;
2347  if (complevel != nullptr) {
2348  /* There is a compression level in the string.
2349  * First restore the : we removed to do proper name matching,
2350  * then move the the begin of the actual version. */
2351  *complevel = ':';
2352  complevel++;
2353 
2354  /* Get the version and determine whether all went fine. */
2355  char *end;
2356  long level = strtol(complevel, &end, 10);
2357  if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) {
2358  SetDParamStr(0, complevel);
2359  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2360  } else {
2361  *compression_level = level;
2362  }
2363  }
2364  return slf;
2365  }
2366  }
2367 
2368  SetDParamStr(0, s);
2369  SetDParamStr(1, def->name);
2370  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2371 
2372  /* Restore the string by adding the : back */
2373  if (complevel != nullptr) *complevel = ':';
2374  }
2375  *compression_level = def->default_compression;
2376  return def;
2377 }
2378 
2379 /* actual loader/saver function */
2380 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2381 extern bool AfterLoadGame();
2382 extern bool LoadOldSaveGame(const char *file);
2383 
2387 static void ResetSaveloadData()
2388 {
2389  ResetTempEngineData();
2390  ResetLabelMaps();
2391  ResetOldWaypoints();
2392 }
2393 
2397 static inline void ClearSaveLoadState()
2398 {
2399  delete _sl.dumper;
2400  _sl.dumper = nullptr;
2401 
2402  delete _sl.sf;
2403  _sl.sf = nullptr;
2404 
2405  delete _sl.reader;
2406  _sl.reader = nullptr;
2407 
2408  delete _sl.lf;
2409  _sl.lf = nullptr;
2410 }
2411 
2417 static void SaveFileStart()
2418 {
2419  _sl.ff_state = _fast_forward;
2420  _fast_forward = 0;
2421  SetMouseCursorBusy(true);
2422 
2424  _sl.saveinprogress = true;
2425 }
2426 
2428 static void SaveFileDone()
2429 {
2430  if (_game_mode != GM_MENU) _fast_forward = _sl.ff_state;
2431  SetMouseCursorBusy(false);
2432 
2434  _sl.saveinprogress = false;
2435 }
2436 
2439 {
2440  _sl.error_str = str;
2441 }
2442 
2445 {
2446  SetDParam(0, _sl.error_str);
2447  SetDParamStr(1, _sl.extra_msg);
2448 
2449  static char err_str[512];
2450  GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
2451  return err_str;
2452 }
2453 
2455 static void SaveFileError()
2456 {
2458  ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
2459  SaveFileDone();
2460 }
2461 
2466 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2467 {
2468  try {
2469  byte compression;
2470  const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
2471 
2472  /* We have written our stuff to memory, now write it to file! */
2473  uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
2474  _sl.sf->Write((byte*)hdr, sizeof(hdr));
2475 
2476  _sl.sf = fmt->init_write(_sl.sf, compression);
2477  _sl.dumper->Flush(_sl.sf);
2478 
2480 
2481  if (threaded) SetAsyncSaveFinish(SaveFileDone);
2482 
2483  return SL_OK;
2484  } catch (...) {
2486 
2488 
2489  /* We don't want to shout when saving is just
2490  * cancelled due to a client disconnecting. */
2491  if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2492  /* Skip the "colour" character */
2493  DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2494  asfp = SaveFileError;
2495  }
2496 
2497  if (threaded) {
2498  SetAsyncSaveFinish(asfp);
2499  } else {
2500  asfp();
2501  }
2502  return SL_ERROR;
2503  }
2504 }
2505 
2506 void WaitTillSaved()
2507 {
2508  if (!_save_thread.joinable()) return;
2509 
2510  _save_thread.join();
2511 
2512  /* Make sure every other state is handled properly as well. */
2514 }
2515 
2524 static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
2525 {
2526  assert(!_sl.saveinprogress);
2527 
2528  _sl.dumper = new MemoryDumper();
2529  _sl.sf = writer;
2530 
2531  _sl_version = SAVEGAME_VERSION;
2532 
2533  SaveViewportBeforeSaveGame();
2534  SlSaveChunks();
2535 
2536  SaveFileStart();
2537 
2538  if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
2539  if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2540 
2541  SaveOrLoadResult result = SaveFileToDisk(false);
2542  SaveFileDone();
2543 
2544  return result;
2545  }
2546 
2547  return SL_OK;
2548 }
2549 
2557 {
2558  try {
2559  _sl.action = SLA_SAVE;
2560  return DoSave(writer, threaded);
2561  } catch (...) {
2563  return SL_ERROR;
2564  }
2565 }
2566 
2573 static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
2574 {
2575  _sl.lf = reader;
2576 
2577  if (load_check) {
2578  /* Clear previous check data */
2580  /* Mark SL_LOAD_CHECK as supported for this savegame. */
2581  _load_check_data.checkable = true;
2582  }
2583 
2584  uint32 hdr[2];
2585  if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2586 
2587  /* see if we have any loader for this type. */
2588  const SaveLoadFormat *fmt = _saveload_formats;
2589  for (;;) {
2590  /* No loader found, treat as version 0 and use LZO format */
2591  if (fmt == endof(_saveload_formats)) {
2592  DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2593  _sl.lf->Reset();
2594  _sl_version = SL_MIN_VERSION;
2595  _sl_minor_version = 0;
2596 
2597  /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2598  fmt = _saveload_formats;
2599  for (;;) {
2600  if (fmt == endof(_saveload_formats)) {
2601  /* Who removed LZO support? Bad bad boy! */
2602  NOT_REACHED();
2603  }
2604  if (fmt->tag == TO_BE32X('OTTD')) break;
2605  fmt++;
2606  }
2607  break;
2608  }
2609 
2610  if (fmt->tag == hdr[0]) {
2611  /* check version number */
2612  _sl_version = (SaveLoadVersion)(TO_BE32(hdr[1]) >> 16);
2613  /* Minor is not used anymore from version 18.0, but it is still needed
2614  * in versions before that (4 cases) which can't be removed easy.
2615  * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2616  _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
2617 
2618  DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
2619 
2620  /* Is the version higher than the current? */
2621  if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2622  break;
2623  }
2624 
2625  fmt++;
2626  }
2627 
2628  /* loader for this savegame type is not implemented? */
2629  if (fmt->init_load == nullptr) {
2630  char err_str[64];
2631  seprintf(err_str, lastof(err_str), "Loader for '%s' is not available.", fmt->name);
2632  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
2633  }
2634 
2635  _sl.lf = fmt->init_load(_sl.lf);
2636  _sl.reader = new ReadBuffer(_sl.lf);
2637  _next_offs = 0;
2638 
2639  if (!load_check) {
2641 
2642  /* Old maps were hardcoded to 256x256 and thus did not contain
2643  * any mapsize information. Pre-initialize to 256x256 to not to
2644  * confuse old games */
2645  InitializeGame(256, 256, true, true);
2646 
2647  GamelogReset();
2648 
2650  /*
2651  * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
2652  * shared savegame version 4. Anything before that 'obviously'
2653  * does not have any NewGRFs. Between the introduction and
2654  * savegame version 41 (just before 0.5) the NewGRF settings
2655  * were not stored in the savegame and they were loaded by
2656  * using the settings from the main menu.
2657  * So, to recap:
2658  * - savegame version < 4: do not load any NewGRFs.
2659  * - savegame version >= 41: load NewGRFs from savegame, which is
2660  * already done at this stage by
2661  * overwriting the main menu settings.
2662  * - other savegame versions: use main menu settings.
2663  *
2664  * This means that users *can* crash savegame version 4..40
2665  * savegames if they set incompatible NewGRFs in the main menu,
2666  * but can't crash anymore for savegame version < 4 savegames.
2667  *
2668  * Note: this is done here because AfterLoadGame is also called
2669  * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
2670  */
2672  }
2673  }
2674 
2675  if (load_check) {
2676  /* Load chunks into _load_check_data.
2677  * No pools are loaded. References are not possible, and thus do not need resolving. */
2679  } else {
2680  /* Load chunks and resolve references */
2681  SlLoadChunks();
2682  SlFixPointers();
2683  }
2684 
2686 
2687  _savegame_type = SGT_OTTD;
2688 
2689  if (load_check) {
2690  /* The only part from AfterLoadGame() we need */
2692  } else {
2694 
2695  /* After loading fix up savegame for any internal changes that
2696  * might have occurred since then. If it fails, load back the old game. */
2697  if (!AfterLoadGame()) {
2699  return SL_REINIT;
2700  }
2701 
2703  }
2704 
2705  return SL_OK;
2706 }
2707 
2714 {
2715  try {
2716  _sl.action = SLA_LOAD;
2717  return DoLoad(reader, false);
2718  } catch (...) {
2720  return SL_REINIT;
2721  }
2722 }
2723 
2733 SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
2734 {
2735  /* An instance of saving is already active, so don't go saving again */
2736  if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
2737  /* if not an autosave, but a user action, show error message */
2738  if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
2739  return SL_OK;
2740  }
2741  WaitTillSaved();
2742 
2743  try {
2744  /* Load a TTDLX or TTDPatch game */
2745  if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
2747 
2748  InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
2749 
2750  /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
2751  * and if so a new NewGRF list will be made in LoadOldSaveGame.
2752  * Note: this is done here because AfterLoadGame is also called
2753  * for OTTD savegames which have their own NewGRF logic. */
2755  GamelogReset();
2756  if (!LoadOldSaveGame(filename)) return SL_REINIT;
2757  _sl_version = SL_MIN_VERSION;
2758  _sl_minor_version = 0;
2760  if (!AfterLoadGame()) {
2762  return SL_REINIT;
2763  }
2765  return SL_OK;
2766  }
2767 
2768  assert(dft == DFT_GAME_FILE);
2769  switch (fop) {
2770  case SLO_CHECK:
2771  _sl.action = SLA_LOAD_CHECK;
2772  break;
2773 
2774  case SLO_LOAD:
2775  _sl.action = SLA_LOAD;
2776  break;
2777 
2778  case SLO_SAVE:
2779  _sl.action = SLA_SAVE;
2780  break;
2781 
2782  default: NOT_REACHED();
2783  }
2784 
2785  FILE *fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
2786 
2787  /* Make it a little easier to load savegames from the console */
2788  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
2789  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
2790  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
2791 
2792  if (fh == nullptr) {
2793  SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2794  }
2795 
2796  if (fop == SLO_SAVE) { // SAVE game
2797  DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
2798  if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
2799 
2800  return DoSave(new FileWriter(fh), threaded);
2801  }
2802 
2803  /* LOAD game */
2804  assert(fop == SLO_LOAD || fop == SLO_CHECK);
2805  DEBUG(desync, 1, "load: %s", filename);
2806  return DoLoad(new FileReader(fh), fop == SLO_CHECK);
2807  } catch (...) {
2808  /* This code may be executed both for old and new save games. */
2810 
2811  /* Skip the "colour" character */
2812  if (fop != SLO_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2813 
2814  /* A saver/loader exception!! reinitialize all variables to prevent crash! */
2815  return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
2816  }
2817 }
2818 
2821 {
2823 }
2824 
2830 void GenerateDefaultSaveName(char *buf, const char *last)
2831 {
2832  /* Check if we have a name for this map, which is the name of the first
2833  * available company. When there's no company available we'll use
2834  * 'Spectator' as "company" name. */
2835  CompanyID cid = _local_company;
2836  if (!Company::IsValidID(cid)) {
2837  for (const Company *c : Company::Iterate()) {
2838  cid = c->index;
2839  break;
2840  }
2841  }
2842 
2843  SetDParam(0, cid);
2844 
2845  /* Insert current date */
2847  case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
2848  case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
2849  case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
2850  default: NOT_REACHED();
2851  }
2852  SetDParam(2, _date);
2853 
2854  /* Get the correct string (special string for when there's not company) */
2855  GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
2856  SanitizeFilename(buf);
2857 }
2858 
2864 {
2865  this->SetMode(SLO_LOAD, GetAbstractFileType(ft), GetDetailedFileType(ft));
2866 }
2867 
2875 {
2876  if (aft == FT_INVALID || aft == FT_NONE) {
2877  this->file_op = SLO_INVALID;
2878  this->detail_ftype = DFT_INVALID;
2879  this->abstract_ftype = FT_INVALID;
2880  return;
2881  }
2882 
2883  this->file_op = fop;
2884  this->detail_ftype = dft;
2885  this->abstract_ftype = aft;
2886 }
2887 
2892 void FileToSaveLoad::SetName(const char *name)
2893 {
2894  strecpy(this->name, name, lastof(this->name));
2895 }
2896 
2901 void FileToSaveLoad::SetTitle(const char *title)
2902 {
2903  strecpy(this->title, title, lastof(this->title));
2904 }
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:67
~FileWriter()
Make sure everything is cleaned up.
Definition: saveload.cpp:1884
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:1976
Owner
Enum for all companies/owners.
Definition: company_type.h:18
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:16
const ChunkHandler _name_chunk_handlers[]
Chunk handlers related to strings.
static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
Actually perform the loading of a "non-old" savegame.
Definition: saveload.cpp:2573
static void SlFixPointers()
Fix all pointers (convert index -> pointer)
Definition: saveload.cpp:1812
static size_t SlCalcNetStringLen(const char *ptr, size_t length)
Calculate the net length of a string.
Definition: saveload.cpp:866
static void SlLoadCheckChunks()
Load all chunks for savegame checking.
Definition: saveload.cpp:1797
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:1900
static SaveOrLoadResult SaveFileToDisk(bool threaded)
We have written the whole game into memory, _memory_savegame, now find and appropriate compressor and...
Definition: saveload.cpp:2466
bool _networking
are we in networking mode?
Definition: network.cpp:52
static SaveLoadParams _sl
Parameters used for/at saveload.
Definition: saveload.cpp:211
ChunkSaveLoadProc * load_check_proc
Load procedure for game preview.
Definition: saveload.h:362
const SaveLoad * GetVehicleDescription(VehicleType vt)
Make it possible to make the saveload tables "friends" of other classes.
Definition: vehicle_sl.cpp:580
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Return the size in bytes of a std::deque.
Definition: saveload.cpp:1300
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:765
byte * bufe
End of the buffer we can read from.
Definition: saveload.cpp:88
Save/load a deque.
Definition: saveload.h:487
GRFConfig * _grfconfig
First item in list of current GRF set up.
static uint SlCalcConvMemLen(VarType conv)
Return the size in bytes of a certain type of normal/atomic variable as it appears in memory...
Definition: saveload.cpp:591
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
LZMALoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2183
LoadFilter *(* init_load)(LoadFilter *chain)
Constructor for the load filter.
Definition: saveload.cpp:2287
Filter using Zlib compression.
Definition: saveload.cpp:2046
void GenerateDefaultSaveName(char *buf, const char *last)
Fill the buffer with the default name for a savegame or screenshot.
Definition: saveload.cpp:2830
NeedLength need_length
working in NeedLength (Autolength) mode?
Definition: saveload.cpp:191
z_stream z
Stream state we are reading from.
Definition: saveload.cpp:2047
void WriteByte(byte b)
Write a single byte into the dumper.
Definition: saveload.cpp:147
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1741
SaveLoadVersion
SaveLoad versions Previous savegame versions, the trunk revision where they were introduced and the r...
Definition: saveload.h:29
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:358
Yes, simply writing to a file.
Definition: saveload.cpp:1872
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
static bool SlSkipVariableOnLoad(const SaveLoad *sld)
Are we going to load this variable when loading a savegame or not?
Definition: saveload.cpp:1368
string (with pre-allocated buffer)
Definition: saveload.h:429
void SetName(const char *name)
Set the name of the file.
Definition: saveload.cpp:2892
uint32 flags
Flags of the chunk.
Definition: saveload.h:363
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
lzma_stream lzma
Stream state that we are reading from.
Definition: saveload.cpp:2176
lzma_stream lzma
Stream state that we are writing to.
Definition: saveload.cpp:2219
do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
Definition: saveload.h:471
static void SlList(void *list, SLRefType conv)
Save/Load a list.
Definition: saveload.cpp:1178
static size_t SlCalcArrayLen(size_t length, VarType conv)
Return the size in bytes of a certain type of atomic array.
Definition: saveload.cpp:988
void NORETURN SlError(StringID string, const char *extra_msg)
Error handler.
Definition: saveload.cpp:326
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:58
ZlibLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2054
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
void GamelogStartAction(GamelogActionType at)
Stores information about new action, but doesn&#39;t allocate it Action is allocated only when there is a...
Definition: gamelog.cpp:69
static uint SlReadSimpleGamma()
Read in the header descriptor of an object or an array.
Definition: saveload.cpp:488
uint32 id
Unique ID (4 letters).
Definition: saveload.h:358
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:60
char * CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:59
LZMASaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2226
Filter using LZO compression.
Definition: saveload.cpp:1965
bool saveinprogress
Whether there is currently a save in progress.
Definition: saveload.cpp:208
Vehicle data structure.
Definition: vehicle_base.h:210
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
Load/save a reference to a link graph job.
Definition: saveload.h:383
Declaration of filters used for saving and loading savegames.
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:43
long begin
The begin of the file.
Definition: saveload.cpp:1834
int64 ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:759
byte buf[MEMORY_CHUNK_SIZE]
Buffer we&#39;re going to read from.
Definition: saveload.cpp:86
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
Save the game using a (writer) filter.
Definition: saveload.cpp:2556
Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
Definition: saveload.h:376
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:380
std::vector< byte * > blocks
Buffer with blocks of allocated memory.
Definition: saveload.cpp:127
static const SaveLoadFormat _saveload_formats[]
The different saveload formats known/understood by OpenTTD.
Definition: saveload.cpp:2296
partial loading into _load_check_data
Definition: saveload.cpp:72
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:25
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2145
void * address
address of variable OR offset of variable in the struct (max offset is 65536)
Definition: saveload.h:509
static void SaveFileDone()
Update the gui accordingly when saving is done and release locks on saveload.
Definition: saveload.cpp:2428
DetailedFileType GetDetailedFileType(FiosType fios_type)
Extract the detailed file type from a FiosType.
Definition: fileio_type.h:100
const ChunkHandler _town_chunk_handlers[]
Chunk handler for towns.
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
Definition: saveload.cpp:2820
SaveLoadVersion _sl_version
the major savegame version identifier
Definition: saveload.cpp:61
Load/save a reference to a town.
Definition: saveload.h:375
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
Called by save thread to tell we finished saving.
Definition: saveload.cpp:391
const ChunkHandler _sign_chunk_handlers[]
Chunk handlers related to signs.
LoadFilter * reader
The filter used to actually read.
Definition: saveload.cpp:89
Filter without any compression.
Definition: saveload.cpp:2175
virtual void Write(byte *buf, size_t len)=0
Write a given number of bytes into the savegame.
SavegameType
Types of save games.
Definition: saveload.h:331
void NORETURN SlErrorCorruptFmt(const char *format,...)
Issue an SlErrorCorrupt with a format string.
Definition: saveload.cpp:370
byte ff_state
The state of fast-forward when saving started.
Definition: saveload.cpp:207
static bool SlIsObjectValidInSavegame(const SaveLoad *sld)
Are we going to save this object or not?
Definition: saveload.cpp:1355
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2015
Deals with the type of the savegame, independent of extension.
Definition: saveload.h:317
size_t size
the sizeof size.
Definition: saveload.h:510
~ZlibLoadFilter()
Clean everything up.
Definition: saveload.cpp:2061
byte default_compression
the default compression level of this format
Definition: saveload.cpp:2291
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:1854
const char * GetSaveLoadErrorString()
Get the string representation of the error message.
Definition: saveload.cpp:2444
File is being saved.
Definition: fileio_type.h:50
FILE * file
The file to write to.
Definition: saveload.cpp:1873
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:747
Load file for checking and/or preview.
Definition: fileio_type.h:48
loading
Definition: saveload.cpp:68
Save/load a reference.
Definition: saveload.h:483
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2264
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
not working in NeedLength mode
Definition: saveload.cpp:76
A connected component of a link graph.
Definition: linkgraph.h:38
static void SlSaveChunk(const ChunkHandler *ch)
Save a chunk of data (eg.
Definition: saveload.cpp:1721
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:999
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition: saveload.cpp:402
z_stream z
Stream state we are writing to.
Definition: saveload.cpp:2091
Save game or scenario file.
Definition: fileio_type.h:31
Interface for filtering a savegame till it is loaded.
bool checkable
True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.)
Definition: fios.h:32
uint16 length
(conditional) length of the variable (eg. arrays) (max array size is 65536 elements) ...
Definition: saveload.h:502
Load/save a reference to a bus/truck stop.
Definition: saveload.h:377
virtual void Finish()
Prepare everything to finish writing the savegame.
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:24
fixing pointers
Definition: saveload.cpp:70
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:48
Save/load a variable.
Definition: saveload.h:482
Filter using Zlib compression.
Definition: saveload.cpp:2090
static size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
Calculate the gross length of the string that it will occupy in the savegame.
Definition: saveload.cpp:881
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:250
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:279
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
void WriteLoop(byte *p, size_t len, lzma_action action)
Helper loop for writing the data.
Definition: saveload.cpp:2243
Base directory for all scenarios.
Definition: fileio_type.h:112
bool global
should we load a global variable or a non-global one
Definition: saveload.h:499
char _savegame_format[8]
how to compress savegames
Definition: saveload.cpp:63
void GamelogReset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition: gamelog.cpp:115
void SetTitle(const char *title)
Set the title of the file.
Definition: saveload.cpp:2901
Load/save a reference to an engine renewal (autoreplace).
Definition: saveload.h:378
ReadBuffer * reader
Savegame reading buffer.
Definition: saveload.cpp:201
VarType conv
type of the variable to be saved, int
Definition: saveload.h:501
static void SlCopyBytes(void *ptr, size_t length)
Save/Load bytes.
Definition: saveload.cpp:730
writing length and data
Definition: saveload.cpp:77
nothing to do
Definition: fileio_type.h:17
SLRefType
Type of reference (SLE_REF, SLE_CONDREF).
Definition: saveload.h:371
FILE * file
The file to read from.
Definition: saveload.cpp:1833
const ChunkHandler _persistent_storage_chunk_handlers[]
Chunk handler for persistent storages.
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:28
allow new lines in the strings
Definition: saveload.h:473
Highest possible saveload version.
Definition: saveload.h:306
SaveOrLoadResult
Save or load result codes.
Definition: saveload.h:310
Filter using LZO compression.
Definition: saveload.cpp:1918
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:194
do not save with savegame, basically client-based
Definition: saveload.h:469
static void SlDeque(void *deque, VarType conv)
Save/load a std::deque.
Definition: saveload.cpp:1327
Filter without any compression.
Definition: saveload.cpp:2006
Old save game or scenario file.
Definition: fileio_type.h:30
~ZlibSaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2105
allow control codes in the strings
Definition: saveload.h:472
static void SlSaveChunks()
Save all chunks.
Definition: saveload.cpp:1759
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:42
First savegame version.
Definition: saveload.h:30
byte _sl_minor_version
the minor savegame version, DO NOT USE!
Definition: saveload.cpp:62
byte max_compression
the maximum compression level of this format
Definition: saveload.cpp:2292
StringID offset into strings-array.
Definition: saveload.h:414
need to calculate the length
Definition: saveload.cpp:78
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
static bool IsVariableSizeRight(const SaveLoad *sld)
Check whether the variable size of the variable in the saveload configuration matches with the actual...
Definition: saveload.cpp:1434
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:463
byte * bufe
End of the buffer we write to.
Definition: saveload.cpp:129
static std::atomic< AsyncSaveFinishProc > _async_save_finish
Callback to call when the savegame loading is finished.
Definition: saveload.cpp:384
Container for cargo from the same location and time.
Definition: cargopacket.h:42
static void SlDeque(void *deque, VarType conv)
Internal templated helper to save/load a std::deque.
Definition: saveload.cpp:1258
uint8 date_format_in_default_names
should the default savegame/screenshot name use long dates (31th Dec 2008), short dates (31-12-2008) ...
void Clear()
Reset read data.
Definition: fios_gui.cpp:47
Allow newlines.
Definition: string_type.h:51
Save/load a list.
Definition: saveload.h:486
size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
Calculate the size of an object.
Definition: saveload.cpp:1384
Game loaded.
Definition: gamelog.h:18
Filter without any compression.
Definition: saveload.cpp:2022
virtual void Reset()
Reset this filter to read from the beginning of the file.
const SaveLoad * GetBaseStationDescription()
Get the base station description to be used for SL_ST_INCLUDE.
Definition: station_sl.cpp:465
Load/save a reference to a station.
Definition: saveload.h:374
const ChunkHandler _animated_tile_chunk_handlers[]
"Definition" imported by the saveload code to be able to load and save the animated tile table...
size_t obj_len
the length of the current object we are busy with
Definition: saveload.cpp:195
Base directory for all savegames.
Definition: fileio_type.h:110
Subdirectory of save for autosaves.
Definition: fileio_type.h:111
ReadBuffer(LoadFilter *reader)
Initialise our variables.
Definition: saveload.cpp:96
void SanitizeFilename(char *filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1256
Base directory for all subdirectories.
Definition: fileio_type.h:109
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
Class for pooled persistent storage of data.
The format for a reader/writer type of a savegame.
Definition: saveload.cpp:2283
static void SlLoadCheckChunk(const ChunkHandler *ch)
Load a chunk of data for checking savegames.
Definition: saveload.cpp:1645
char * error_data
Data to pass to SetDParamStr when displaying error.
Definition: fios.h:34
Load/save a reference to an order.
Definition: saveload.h:372
static std::thread _save_thread
The thread we&#39;re using to compress and write a savegame.
Definition: saveload.cpp:385
static void SlWriteSimpleGamma(size_t i)
Write the header descriptor of an object or an array.
Definition: saveload.cpp:530
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:1892
const SaveLoadVersion SAVEGAME_VERSION
Current savegame version of OpenTTD.
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2269
ZlibSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2098
SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
Load the game using a (reader) filter.
Definition: saveload.cpp:2713
void SetSaveLoadError(StringID str)
Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friend...
Definition: saveload.cpp:2438
static VarType GetVarFileType(VarType type)
Get the FileType of a setting.
Definition: saveload.h:817
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition: fileio_type.h:90
MemoryDumper * dumper
Memory dumper to write the savegame to.
Definition: saveload.cpp:198
SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
Main Save or Load function where the high-level saveload functions are handled.
Definition: saveload.cpp:2733
OTTD savegame.
Definition: saveload.h:335
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:182
static void SlLoadChunks()
Load all chunks.
Definition: saveload.cpp:1782
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2032
A buffer for reading (and buffering) savegame data.
Definition: saveload.cpp:85
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
byte * buf
Buffer we&#39;re going to write to.
Definition: saveload.cpp:128
virtual size_t Read(byte *buf, size_t len)=0
Read a given number of bytes from the savegame.
File is being loaded.
Definition: fileio_type.h:49
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
static const uint LZO_BUFFER_SIZE
Buffer size for the LZO compressor.
Definition: saveload.cpp:1915
static uint SlGetGammaLength(size_t i)
Return how many bytes used to encode a gamma value.
Definition: saveload.cpp:555
byte SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:418
StringID error
Error message from loading. INVALID_STRING_ID if no error.
Definition: fios.h:33
static VarType GetVarMemType(VarType type)
Get the NumberType of a setting.
Definition: saveload.h:806
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
static void ResetSaveloadData()
Clear temporary data that is passed between various saveload phases.
Definition: saveload.cpp:2387
static void SaveFileError()
Show a gui message when saving has failed.
Definition: saveload.cpp:2455
ChunkSaveLoadProc * save_proc
Save procedure of the chunk.
Definition: saveload.h:359
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:47
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:641
ChunkSaveLoadProc * load_proc
Load procedure of the chunk.
Definition: saveload.h:360
Load/save a reference to a vehicle.
Definition: saveload.h:373
static const ChunkHandler * SlFindChunkHandler(uint32 id)
Find the ChunkHandler that will be used for processing the found chunk in the savegame or in memory...
Definition: saveload.cpp:1775
void Reset() override
Reset this filter to read from the beginning of the file.
Definition: saveload.cpp:1862
Handlers and description of chunk.
Definition: saveload.h:357
static void SlSkipBytes(size_t length)
Read in bytes from the file/data structure but don&#39;t do anything with them, discarding them in effect...
Definition: saveload.cpp:474
void SlSkipArray()
Skip an array or sparse array.
Definition: saveload.cpp:674
The saveload struct, containing reader-writer functions, buffer, version, etc.
Definition: saveload.cpp:189
byte * bufp
Location we&#39;re at reading the buffer.
Definition: saveload.cpp:87
Save/load an array.
Definition: saveload.h:484
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Internal templated helper to return the size in bytes of a std::deque.
Definition: saveload.cpp:1243
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:137
void GamelogStopAction()
Stops logging of any changes.
Definition: gamelog.cpp:78
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
Definition: strings_sl.cpp:28
string enclosed in quotes (with pre-allocated buffer)
Definition: saveload.h:430
static void ClearSaveLoadState()
Clear/free saveload state.
Definition: saveload.cpp:2397
Unknown or invalid file.
Definition: fileio_type.h:43
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2066
static void * GetVariableAddress(const void *object, const SaveLoad *sld)
Get the address of the variable.
Definition: saveload.h:838
MemoryDumper()
Initialise our variables.
Definition: saveload.cpp:132
static const lzma_stream _lzma_init
Have a copy of an initialised LZMA stream.
Definition: saveload.cpp:2172
bool AfterLoadGame()
Perform a (large) amount of savegame conversion magic in order to load older savegames and to fill th...
Definition: afterload.cpp:542
static void SlStubSaveProc2(void *arg)
Stub Chunk handlers to only calculate length and do nothing else.
Definition: saveload.cpp:1701
SaveLoadAction action
are we doing a save or a load atm.
Definition: saveload.cpp:190
static const SaveLoadFormat * GetSavegameFormat(char *s, byte *compression_level)
Return the savegameformat of the game.
Definition: saveload.cpp:2332
Load/save a reference to a cargo packet.
Definition: saveload.h:379
bool error
did an error occur or not
Definition: saveload.cpp:193
GUISettings gui
settings related to the GUI
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
const ChunkHandler _cargopacket_chunk_handlers[]
Chunk handlers related to cargo packets.
static const size_t MEMORY_CHUNK_SIZE
Save in chunks of 128 KiB.
Definition: saveload.cpp:82
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
SaveLoadVersion version_to
save/load the variable until this savegame version
Definition: saveload.h:504
169 23816
Definition: saveload.h:244
const ChunkHandler _cargomonitor_chunk_handlers[]
Chunk definition of the cargomonitoring maps.
static void SlNullPointers()
Null all pointers (convert index -> nullptr)
Definition: saveload.cpp:295
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
~LZMALoadFilter()
Clean everything up.
Definition: saveload.cpp:2190
useful to write zeros in savegame.
Definition: saveload.h:428
string pointer enclosed in quotes
Definition: saveload.h:432
Invalid or unknown file type.
Definition: fileio_type.h:22
~FileReader()
Make sure everything is cleaned up.
Definition: saveload.cpp:1845
Struct to store engine replacements.
static void SaveFileStart()
Update the gui accordingly when starting saving and set locks on saveload.
Definition: saveload.cpp:2417
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
SaveLoadType cmd
the action to take with the saved/loaded type, All types need different action
Definition: saveload.h:500
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1550
uint32 tag
the 4-letter tag by which it is identified in the savegame
Definition: saveload.cpp:2285
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:384
static byte SlCalcConvFileLen(VarType conv)
Return the size in bytes of a certain type of normal/atomic variable as it appears in a saved game...
Definition: saveload.cpp:615
Town data structure.
Definition: town.h:53
byte block_mode
???
Definition: saveload.cpp:192
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2195
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:57
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
FileReader(FILE *file)
Create the file reader, so it reads from a specific file.
Definition: saveload.cpp:1840
bool _network_server
network-server is active
Definition: network.cpp:53
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:318
StringID error_str
the translatable error message to show
Definition: saveload.cpp:204
SaveFilter *(* init_write)(SaveFilter *chain, byte compression)
Constructor for the save filter.
Definition: saveload.cpp:2288
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1568
LZOSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:1971
char * extra_msg
the error message
Definition: saveload.cpp:205
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1578
const ChunkHandler _cheat_chunk_handlers[]
Chunk handlers related to cheats.
void str_fix_scc_encoded(char *str, const char *last)
Scan the string for old values of SCC_ENCODED and fix it to it&#39;s new, static value.
Definition: string.cpp:168
Allow the special control codes.
Definition: string_type.h:52
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
byte min_compression
the minimum compression level of this format
Definition: saveload.cpp:2290
static size_t SlCalcListLen(const void *list)
Return the size in bytes of a list.
Definition: saveload.cpp:1162
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
void Flush(SaveFilter *writer)
Flush this dumper into a writer.
Definition: saveload.cpp:163
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:57
SaveLoad type struct.
Definition: saveload.h:498
69 10319
Definition: saveload.h:124
SaveLoadAction
What are we currently doing?
Definition: saveload.cpp:67
SaveFilter * sf
Filter to write the savegame to.
Definition: saveload.cpp:199
bool threaded_saves
should we do threaded saves?
Load/save a reference to an orderlist.
Definition: saveload.h:380
Filter using LZMA compression.
Definition: saveload.cpp:2218
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Template class to help with std::deque.
Definition: saveload.cpp:1235
completed successfully
Definition: saveload.h:311
Load/save a reference to a link graph.
Definition: saveload.h:382
string pointer
Definition: saveload.h:431
FileWriter(FILE *file)
Create the file writer, so it writes to a specific file.
Definition: saveload.cpp:1879
static void SlStubSaveProc()
Stub Chunk handlers to only calculate length and do nothing else.
Definition: saveload.cpp:1711
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition: saveload.cpp:383
void SlSetLength(size_t length)
Sets the length of either a RIFF object or the number of items in an array.
Definition: saveload.cpp:686
void SetMode(FiosType ft)
Set the mode and file type of the file to save or load based on the type of file entry at the file sy...
Definition: saveload.cpp:2863
static void SlLoadChunk(const ChunkHandler *ch)
Load a chunk of data (eg vehicles, stations, etc.)
Definition: saveload.cpp:1605
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
NeedLength
Definition: saveload.cpp:75
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:118
finished saving
Definition: statusbar_gui.h:16
Interface for filtering a savegame till it is written.
saving
Definition: saveload.cpp:69
static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
Actually perform the saving of the savegame.
Definition: saveload.cpp:2524
NoCompSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2028
LoadFilter * lf
Filter to read the savegame from.
Definition: saveload.cpp:202
Errors (eg. saving/loading failed)
Definition: error.h:23
static void SlString(void *ptr, size_t length, VarType conv)
Save/Load a string.
Definition: saveload.cpp:910
error that was caught before internal structures were modified
Definition: saveload.h:312
static Station * Get(size_t index)
Gets station with given index.
Date _date
Current date in days (day counter)
Definition: date.cpp:27
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2150
null all pointers (on loading error)
Definition: saveload.cpp:71
started saving
Definition: statusbar_gui.h:15
LZOLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:1923
~LZMASaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2232
size_t read
The amount of read bytes so far from the filter.
Definition: saveload.cpp:90
const char * name
name of the compressor/decompressor (debug-only)
Definition: saveload.cpp:2284
Declaration of functions used in more save/load files.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:44
DetailedFileType
Kinds of files in each AbstractFileType.
Definition: fileio_type.h:28
size_t Read(byte *buf, size_t ssize) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:1928
Container for dumping the savegame (quickly) to memory.
Definition: saveload.cpp:126
void WriteValue(void *ptr, VarType conv, int64 val)
Write the value of a setting.
Definition: saveload.cpp:783
void SlWriteByte(byte b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:427
GRFListCompatibility grf_compatibility
Summary state of NewGrfs, whether missing files or only compatible found.
Definition: fios.h:44
static const ChunkHandler *const _chunk_handlers[]
Array of all chunks in a savegame, nullptr terminated.
Definition: saveload.cpp:249
static ChunkSaveLoadProc * _stub_save_proc
Stub Chunk handlers to only calculate length and do nothing else.
Definition: saveload.cpp:1694
bool _do_autosave
are we doing an autosave at the moment?
Definition: saveload.cpp:64
Station data structure.
Definition: station_base.h:450
Unknown file operation.
Definition: fileio_type.h:52
NoCompLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2011
static size_t ReferenceToInt(const void *obj, SLRefType rt)
Pointers cannot be saved to a savegame, so this functions gets the index of the item, and if not available, it hussles with pointers (looks really bad :() Remember that a nullptr item has value 0, and all indices have +1, so vehicle 0 is saved as index 1.
Definition: saveload.cpp:1054
int last_array_index
in the case of an array, the current and last positions
Definition: saveload.cpp:196
static void SlSaveLoadConv(void *ptr, VarType conv)
Handle all conversion and typechecking of variables here.
Definition: saveload.cpp:809
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:29
static void * IntToReference(size_t index, SLRefType rt)
Pointers cannot be loaded from a savegame, so this function gets the index from the savegame and retu...
Definition: saveload.cpp:1087
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3319
old custom name to be converted to a char pointer
Definition: saveload.h:433
4.0 1 4.1 122 0.3.3, 0.3.4 4.2 1222 0.3.5 4.3 1417 4.4 1426
Definition: saveload.h:36
uint32 _ttdp_version
version of TTDP savegame (if applicable)
Definition: saveload.cpp:60
static size_t SlCalcRefLen()
Return the size in bytes of a reference (pointer)
Definition: saveload.cpp:624
Save/load a string.
Definition: saveload.h:485
Load/save a reference to a persistent storage.
Definition: saveload.h:381
void WriteLoop(byte *p, size_t len, int mode)
Helper loop for writing the data.
Definition: saveload.cpp:2116
#define FOR_ALL_CHUNK_HANDLERS(ch)
Iterate over all chunk handlers.
Definition: saveload.cpp:290
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
Yes, simply reading from a file.
Definition: saveload.cpp:1832
error that was caught in the middle of updating game state, need to clear it. (can only happen during...
Definition: saveload.h:313