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