OpenTTD Source  1.11.2
saveload.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
22 #include <deque>
23 
24 #include "../stdafx.h"
25 #include "../debug.h"
26 #include "../station_base.h"
27 #include "../thread.h"
28 #include "../town.h"
29 #include "../network/network.h"
30 #include "../window_func.h"
31 #include "../strings_func.h"
32 #include "../core/endian_func.hpp"
33 #include "../vehicle_base.h"
34 #include "../company_func.h"
35 #include "../date_func.h"
36 #include "../autoreplace_base.h"
37 #include "../roadstop_base.h"
38 #include "../linkgraph/linkgraph.h"
39 #include "../linkgraph/linkgraphjob.h"
40 #include "../statusbar_gui.h"
41 #include "../fileio_func.h"
42 #include "../gamelog.h"
43 #include "../string_func.h"
44 #include "../fios.h"
45 #include "../error.h"
46 #include <atomic>
47 #include <string>
48 #ifdef __EMSCRIPTEN__
49 # include <emscripten.h>
50 #endif
51 
52 #include "table/strings.h"
53 
54 #include "saveload_internal.h"
55 #include "saveload_filter.h"
56 
57 #include "../safeguards.h"
58 
60 
63 
64 uint32 _ttdp_version;
69 
77 };
78 
79 enum NeedLength {
80  NL_NONE = 0,
83 };
84 
86 static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
87 
89 struct ReadBuffer {
91  byte *bufp;
92  byte *bufe;
94  size_t read;
95 
100  ReadBuffer(LoadFilter *reader) : bufp(nullptr), bufe(nullptr), reader(reader), read(0)
101  {
102  }
103 
104  inline byte ReadByte()
105  {
106  if (this->bufp == this->bufe) {
107  size_t len = this->reader->Read(this->buf, lengthof(this->buf));
108  if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
109 
110  this->read += len;
111  this->bufp = this->buf;
112  this->bufe = this->buf + len;
113  }
114 
115  return *this->bufp++;
116  }
117 
122  size_t GetSize() const
123  {
124  return this->read - (this->bufe - this->bufp);
125  }
126 };
127 
128 
130 struct MemoryDumper {
131  std::vector<byte *> blocks;
132  byte *buf;
133  byte *bufe;
134 
136  MemoryDumper() : buf(nullptr), bufe(nullptr)
137  {
138  }
139 
140  ~MemoryDumper()
141  {
142  for (auto p : this->blocks) {
143  free(p);
144  }
145  }
146 
151  inline void WriteByte(byte b)
152  {
153  /* Are we at the end of this chunk? */
154  if (this->buf == this->bufe) {
155  this->buf = CallocT<byte>(MEMORY_CHUNK_SIZE);
156  this->blocks.push_back(this->buf);
157  this->bufe = this->buf + MEMORY_CHUNK_SIZE;
158  }
159 
160  *this->buf++ = b;
161  }
162 
167  void Flush(SaveFilter *writer)
168  {
169  uint i = 0;
170  size_t t = this->GetSize();
171 
172  while (t > 0) {
173  size_t to_write = std::min(MEMORY_CHUNK_SIZE, t);
174 
175  writer->Write(this->blocks[i++], to_write);
176  t -= to_write;
177  }
178 
179  writer->Finish();
180  }
181 
186  size_t GetSize() const
187  {
188  return this->blocks.size() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
189  }
190 };
191 
196  byte block_mode;
197  bool error;
198 
199  size_t obj_len;
200  int array_index, last_array_index;
201 
204 
207 
209  char *extra_msg;
210 
211  uint16 game_speed;
213 };
214 
216 
217 /* these define the chunks */
218 extern const ChunkHandler _gamelog_chunk_handlers[];
219 extern const ChunkHandler _map_chunk_handlers[];
220 extern const ChunkHandler _misc_chunk_handlers[];
221 extern const ChunkHandler _name_chunk_handlers[];
222 extern const ChunkHandler _cheat_chunk_handlers[] ;
223 extern const ChunkHandler _setting_chunk_handlers[];
224 extern const ChunkHandler _company_chunk_handlers[];
225 extern const ChunkHandler _engine_chunk_handlers[];
226 extern const ChunkHandler _veh_chunk_handlers[];
227 extern const ChunkHandler _waypoint_chunk_handlers[];
228 extern const ChunkHandler _depot_chunk_handlers[];
229 extern const ChunkHandler _order_chunk_handlers[];
230 extern const ChunkHandler _town_chunk_handlers[];
231 extern const ChunkHandler _sign_chunk_handlers[];
232 extern const ChunkHandler _station_chunk_handlers[];
233 extern const ChunkHandler _industry_chunk_handlers[];
234 extern const ChunkHandler _economy_chunk_handlers[];
235 extern const ChunkHandler _subsidy_chunk_handlers[];
237 extern const ChunkHandler _goal_chunk_handlers[];
238 extern const ChunkHandler _story_page_chunk_handlers[];
239 extern const ChunkHandler _ai_chunk_handlers[];
240 extern const ChunkHandler _game_chunk_handlers[];
242 extern const ChunkHandler _newgrf_chunk_handlers[];
243 extern const ChunkHandler _group_chunk_handlers[];
245 extern const ChunkHandler _autoreplace_chunk_handlers[];
246 extern const ChunkHandler _labelmaps_chunk_handlers[];
247 extern const ChunkHandler _linkgraph_chunk_handlers[];
248 extern const ChunkHandler _airport_chunk_handlers[];
249 extern const ChunkHandler _object_chunk_handlers[];
251 
253 static const ChunkHandler * const _chunk_handlers[] = {
254  _gamelog_chunk_handlers,
255  _map_chunk_handlers,
256  _misc_chunk_handlers,
259  _setting_chunk_handlers,
260  _veh_chunk_handlers,
261  _waypoint_chunk_handlers,
262  _depot_chunk_handlers,
263  _order_chunk_handlers,
264  _industry_chunk_handlers,
265  _economy_chunk_handlers,
266  _subsidy_chunk_handlers,
268  _goal_chunk_handlers,
269  _story_page_chunk_handlers,
270  _engine_chunk_handlers,
273  _station_chunk_handlers,
274  _company_chunk_handlers,
275  _ai_chunk_handlers,
276  _game_chunk_handlers,
278  _newgrf_chunk_handlers,
279  _group_chunk_handlers,
281  _autoreplace_chunk_handlers,
282  _labelmaps_chunk_handlers,
283  _linkgraph_chunk_handlers,
284  _airport_chunk_handlers,
285  _object_chunk_handlers,
287  nullptr,
288 };
289 
294 #define FOR_ALL_CHUNK_HANDLERS(ch) \
295  for (const ChunkHandler * const *chsc = _chunk_handlers; *chsc != nullptr; chsc++) \
296  for (const ChunkHandler *ch = *chsc; ch != nullptr; ch = (ch->flags & CH_LAST) ? nullptr : ch + 1)
297 
299 static void SlNullPointers()
300 {
301  _sl.action = SLA_NULL;
302 
303  /* We don't want any savegame conversion code to run
304  * during NULLing; especially those that try to get
305  * pointers from other pools. */
307 
309  if (ch->ptrs_proc != nullptr) {
310  DEBUG(sl, 3, "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  assert(_sl.action == SLA_NULL);
316 }
317 
326 void NORETURN SlError(StringID string, const char *extra_msg)
327 {
328  /* Distinguish between loading into _load_check_data vs. normal save/load. */
329  if (_sl.action == SLA_LOAD_CHECK) {
330  _load_check_data.error = string;
332  _load_check_data.error_data = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
333  } else {
334  _sl.error_str = string;
335  free(_sl.extra_msg);
336  _sl.extra_msg = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
337  }
338 
339  /* We have to nullptr all pointers here; we might be in a state where
340  * the pointers are actually filled with indices, which means that
341  * when we access them during cleaning the pool dereferences of
342  * those indices will be made with segmentation faults as result. */
344 
345  /* Logging could be active. */
346  GamelogStopAnyAction();
347 
348  throw std::exception();
349 }
350 
358 void NORETURN SlErrorCorrupt(const char *msg)
359 {
360  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
361 }
362 
370 void NORETURN SlErrorCorruptFmt(const char *format, ...)
371 {
372  va_list ap;
373  char msg[256];
374 
375  va_start(ap, format);
376  vseprintf(msg, lastof(msg), format, ap);
377  va_end(ap);
378 
379  SlErrorCorrupt(msg);
380 }
381 
382 
383 typedef void (*AsyncSaveFinishProc)();
384 static std::atomic<AsyncSaveFinishProc> _async_save_finish;
385 static std::thread _save_thread;
386 
392 {
393  if (_exit_game) return;
394  while (_async_save_finish.load(std::memory_order_acquire) != nullptr) CSleep(10);
395 
396  _async_save_finish.store(proc, std::memory_order_release);
397 }
398 
403 {
404  AsyncSaveFinishProc proc = _async_save_finish.exchange(nullptr, std::memory_order_acq_rel);
405  if (proc == nullptr) return;
406 
407  proc();
408 
409  if (_save_thread.joinable()) {
410  _save_thread.join();
411  }
412 }
413 
419 {
420  return _sl.reader->ReadByte();
421 }
422 
427 void SlWriteByte(byte b)
428 {
429  _sl.dumper->WriteByte(b);
430 }
431 
432 static inline int SlReadUint16()
433 {
434  int x = SlReadByte() << 8;
435  return x | SlReadByte();
436 }
437 
438 static inline uint32 SlReadUint32()
439 {
440  uint32 x = SlReadUint16() << 16;
441  return x | SlReadUint16();
442 }
443 
444 static inline uint64 SlReadUint64()
445 {
446  uint32 x = SlReadUint32();
447  uint32 y = SlReadUint32();
448  return (uint64)x << 32 | y;
449 }
450 
451 static inline void SlWriteUint16(uint16 v)
452 {
453  SlWriteByte(GB(v, 8, 8));
454  SlWriteByte(GB(v, 0, 8));
455 }
456 
457 static inline void SlWriteUint32(uint32 v)
458 {
459  SlWriteUint16(GB(v, 16, 16));
460  SlWriteUint16(GB(v, 0, 16));
461 }
462 
463 static inline void SlWriteUint64(uint64 x)
464 {
465  SlWriteUint32((uint32)(x >> 32));
466  SlWriteUint32((uint32)x);
467 }
468 
478 static uint SlReadSimpleGamma()
479 {
480  uint i = SlReadByte();
481  if (HasBit(i, 7)) {
482  i &= ~0x80;
483  if (HasBit(i, 6)) {
484  i &= ~0x40;
485  if (HasBit(i, 5)) {
486  i &= ~0x20;
487  if (HasBit(i, 4)) {
488  i &= ~0x10;
489  if (HasBit(i, 3)) {
490  SlErrorCorrupt("Unsupported gamma");
491  }
492  i = SlReadByte(); // 32 bits only.
493  }
494  i = (i << 8) | SlReadByte();
495  }
496  i = (i << 8) | SlReadByte();
497  }
498  i = (i << 8) | SlReadByte();
499  }
500  return i;
501 }
502 
520 static void SlWriteSimpleGamma(size_t i)
521 {
522  if (i >= (1 << 7)) {
523  if (i >= (1 << 14)) {
524  if (i >= (1 << 21)) {
525  if (i >= (1 << 28)) {
526  assert(i <= UINT32_MAX); // We can only support 32 bits for now.
527  SlWriteByte((byte)(0xF0));
528  SlWriteByte((byte)(i >> 24));
529  } else {
530  SlWriteByte((byte)(0xE0 | (i >> 24)));
531  }
532  SlWriteByte((byte)(i >> 16));
533  } else {
534  SlWriteByte((byte)(0xC0 | (i >> 16)));
535  }
536  SlWriteByte((byte)(i >> 8));
537  } else {
538  SlWriteByte((byte)(0x80 | (i >> 8)));
539  }
540  }
541  SlWriteByte((byte)i);
542 }
543 
545 static inline uint SlGetGammaLength(size_t i)
546 {
547  return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
548 }
549 
550 static inline uint SlReadSparseIndex()
551 {
552  return SlReadSimpleGamma();
553 }
554 
555 static inline void SlWriteSparseIndex(uint index)
556 {
557  SlWriteSimpleGamma(index);
558 }
559 
560 static inline uint SlReadArrayLength()
561 {
562  return SlReadSimpleGamma();
563 }
564 
565 static inline void SlWriteArrayLength(size_t length)
566 {
567  SlWriteSimpleGamma(length);
568 }
569 
570 static inline uint SlGetArrayLength(size_t length)
571 {
572  return SlGetGammaLength(length);
573 }
574 
581 static inline uint SlCalcConvMemLen(VarType conv)
582 {
583  static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
584  byte length = GB(conv, 4, 4);
585 
586  switch (length << 4) {
587  case SLE_VAR_STRB:
588  case SLE_VAR_STRBQ:
589  case SLE_VAR_STR:
590  case SLE_VAR_STRQ:
591  return SlReadArrayLength();
592 
593  default:
594  assert(length < lengthof(conv_mem_size));
595  return conv_mem_size[length];
596  }
597 }
598 
605 static inline byte SlCalcConvFileLen(VarType conv)
606 {
607  static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
608  byte length = GB(conv, 0, 4);
609  assert(length < lengthof(conv_file_size));
610  return conv_file_size[length];
611 }
612 
614 static inline size_t SlCalcRefLen()
615 {
616  return IsSavegameVersionBefore(SLV_69) ? 2 : 4;
617 }
618 
619 void SlSetArrayIndex(uint index)
620 {
622  _sl.array_index = index;
623 }
624 
625 static size_t _next_offs;
626 
632 {
633  int index;
634 
635  /* After reading in the whole array inside the loop
636  * we must have read in all the data, so we must be at end of current block. */
637  if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) SlErrorCorrupt("Invalid chunk size");
638 
639  for (;;) {
640  uint length = SlReadArrayLength();
641  if (length == 0) {
642  _next_offs = 0;
643  return -1;
644  }
645 
646  _sl.obj_len = --length;
647  _next_offs = _sl.reader->GetSize() + length;
648 
649  switch (_sl.block_mode) {
650  case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
651  case CH_ARRAY: index = _sl.array_index++; break;
652  default:
653  DEBUG(sl, 0, "SlIterateArray error");
654  return -1; // error
655  }
656 
657  if (length != 0) return index;
658  }
659 }
660 
665 {
666  while (SlIterateArray() != -1) {
667  SlSkipBytes(_next_offs - _sl.reader->GetSize());
668  }
669 }
670 
676 void SlSetLength(size_t length)
677 {
678  assert(_sl.action == SLA_SAVE);
679 
680  switch (_sl.need_length) {
681  case NL_WANTLENGTH:
683  switch (_sl.block_mode) {
684  case CH_RIFF:
685  /* Ugly encoding of >16M RIFF chunks
686  * The lower 24 bits are normal
687  * The uppermost 4 bits are bits 24:27 */
688  assert(length < (1 << 28));
689  SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
690  break;
691  case CH_ARRAY:
692  assert(_sl.last_array_index <= _sl.array_index);
693  while (++_sl.last_array_index <= _sl.array_index) {
694  SlWriteArrayLength(1);
695  }
696  SlWriteArrayLength(length + 1);
697  break;
698  case CH_SPARSE_ARRAY:
699  SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
700  SlWriteSparseIndex(_sl.array_index);
701  break;
702  default: NOT_REACHED();
703  }
704  break;
705 
706  case NL_CALCLENGTH:
707  _sl.obj_len += (int)length;
708  break;
709 
710  default: NOT_REACHED();
711  }
712 }
713 
720 static void SlCopyBytes(void *ptr, size_t length)
721 {
722  byte *p = (byte *)ptr;
723 
724  switch (_sl.action) {
725  case SLA_LOAD_CHECK:
726  case SLA_LOAD:
727  for (; length != 0; length--) *p++ = SlReadByte();
728  break;
729  case SLA_SAVE:
730  for (; length != 0; length--) SlWriteByte(*p++);
731  break;
732  default: NOT_REACHED();
733  }
734 }
735 
738 {
739  return _sl.obj_len;
740 }
741 
749 int64 ReadValue(const void *ptr, VarType conv)
750 {
751  switch (GetVarMemType(conv)) {
752  case SLE_VAR_BL: return (*(const bool *)ptr != 0);
753  case SLE_VAR_I8: return *(const int8 *)ptr;
754  case SLE_VAR_U8: return *(const byte *)ptr;
755  case SLE_VAR_I16: return *(const int16 *)ptr;
756  case SLE_VAR_U16: return *(const uint16*)ptr;
757  case SLE_VAR_I32: return *(const int32 *)ptr;
758  case SLE_VAR_U32: return *(const uint32*)ptr;
759  case SLE_VAR_I64: return *(const int64 *)ptr;
760  case SLE_VAR_U64: return *(const uint64*)ptr;
761  case SLE_VAR_NULL:return 0;
762  default: NOT_REACHED();
763  }
764 }
765 
773 void WriteValue(void *ptr, VarType conv, int64 val)
774 {
775  switch (GetVarMemType(conv)) {
776  case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
777  case SLE_VAR_I8: *(int8 *)ptr = val; break;
778  case SLE_VAR_U8: *(byte *)ptr = val; break;
779  case SLE_VAR_I16: *(int16 *)ptr = val; break;
780  case SLE_VAR_U16: *(uint16*)ptr = val; break;
781  case SLE_VAR_I32: *(int32 *)ptr = val; break;
782  case SLE_VAR_U32: *(uint32*)ptr = val; break;
783  case SLE_VAR_I64: *(int64 *)ptr = val; break;
784  case SLE_VAR_U64: *(uint64*)ptr = val; break;
785  case SLE_VAR_NAME: *reinterpret_cast<std::string *>(ptr) = CopyFromOldName(val); break;
786  case SLE_VAR_NULL: break;
787  default: NOT_REACHED();
788  }
789 }
790 
799 static void SlSaveLoadConv(void *ptr, VarType conv)
800 {
801  switch (_sl.action) {
802  case SLA_SAVE: {
803  int64 x = ReadValue(ptr, conv);
804 
805  /* Write the value to the file and check if its value is in the desired range */
806  switch (GetVarFileType(conv)) {
807  case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
808  case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
809  case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
810  case SLE_FILE_STRINGID:
811  case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
812  case SLE_FILE_I32:
813  case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
814  case SLE_FILE_I64:
815  case SLE_FILE_U64: SlWriteUint64(x);break;
816  default: NOT_REACHED();
817  }
818  break;
819  }
820  case SLA_LOAD_CHECK:
821  case SLA_LOAD: {
822  int64 x;
823  /* Read a value from the file */
824  switch (GetVarFileType(conv)) {
825  case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
826  case SLE_FILE_U8: x = (byte )SlReadByte(); break;
827  case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
828  case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
829  case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
830  case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
831  case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
832  case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
833  case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
834  default: NOT_REACHED();
835  }
836 
837  /* Write The value to the struct. These ARE endian safe. */
838  WriteValue(ptr, conv, x);
839  break;
840  }
841  case SLA_PTRS: break;
842  case SLA_NULL: break;
843  default: NOT_REACHED();
844  }
845 }
846 
856 static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
857 {
858  if (ptr == nullptr) return 0;
859  return std::min(strlen(ptr), length - 1);
860 }
861 
871 static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
872 {
873  size_t len;
874  const char *str;
875 
876  switch (GetVarMemType(conv)) {
877  default: NOT_REACHED();
878  case SLE_VAR_STR:
879  case SLE_VAR_STRQ:
880  str = *(const char * const *)ptr;
881  len = SIZE_MAX;
882  break;
883  case SLE_VAR_STRB:
884  case SLE_VAR_STRBQ:
885  str = (const char *)ptr;
886  len = length;
887  break;
888  }
889 
890  len = SlCalcNetStringLen(str, len);
891  return len + SlGetArrayLength(len); // also include the length of the index
892 }
893 
901 static inline size_t SlCalcStdStringLen(const void *ptr)
902 {
903  const std::string *str = reinterpret_cast<const std::string *>(ptr);
904 
905  size_t len = str->length();
906  return len + SlGetArrayLength(len); // also include the length of the index
907 }
908 
915 static void SlString(void *ptr, size_t length, VarType conv)
916 {
917  switch (_sl.action) {
918  case SLA_SAVE: {
919  size_t len;
920  switch (GetVarMemType(conv)) {
921  default: NOT_REACHED();
922  case SLE_VAR_STRB:
923  case SLE_VAR_STRBQ:
924  len = SlCalcNetStringLen((char *)ptr, length);
925  break;
926  case SLE_VAR_STR:
927  case SLE_VAR_STRQ:
928  ptr = *(char **)ptr;
929  len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
930  break;
931  }
932 
933  SlWriteArrayLength(len);
934  SlCopyBytes(ptr, len);
935  break;
936  }
937  case SLA_LOAD_CHECK:
938  case SLA_LOAD: {
939  size_t len = SlReadArrayLength();
940 
941  switch (GetVarMemType(conv)) {
942  default: NOT_REACHED();
943  case SLE_VAR_STRB:
944  case SLE_VAR_STRBQ:
945  if (len >= length) {
946  DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
947  SlCopyBytes(ptr, length);
948  SlSkipBytes(len - length);
949  len = length - 1;
950  } else {
951  SlCopyBytes(ptr, len);
952  }
953  break;
954  case SLE_VAR_STR:
955  case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
956  free(*(char **)ptr);
957  if (len == 0) {
958  *(char **)ptr = nullptr;
959  return;
960  } else {
961  *(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
962  ptr = *(char **)ptr;
963  SlCopyBytes(ptr, len);
964  }
965  break;
966  }
967 
968  ((char *)ptr)[len] = '\0'; // properly terminate the string
970  if ((conv & SLF_ALLOW_CONTROL) != 0) {
973  str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
974  }
975  }
976  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
978  }
979  str_validate((char *)ptr, (char *)ptr + len, settings);
980  break;
981  }
982  case SLA_PTRS: break;
983  case SLA_NULL: break;
984  default: NOT_REACHED();
985  }
986 }
987 
993 static void SlStdString(void *ptr, VarType conv)
994 {
995  std::string *str = reinterpret_cast<std::string *>(ptr);
996 
997  switch (_sl.action) {
998  case SLA_SAVE: {
999  size_t len = str->length();
1000  SlWriteArrayLength(len);
1001  SlCopyBytes(const_cast<void *>(static_cast<const void *>(str->c_str())), len);
1002  break;
1003  }
1004 
1005  case SLA_LOAD_CHECK:
1006  case SLA_LOAD: {
1007  size_t len = SlReadArrayLength();
1008  char *buf = AllocaM(char, len + 1);
1009 
1010  SlCopyBytes(buf, len);
1011  buf[len] = '\0'; // properly terminate the string
1012 
1014  if ((conv & SLF_ALLOW_CONTROL) != 0) {
1017  str_fix_scc_encoded(buf, buf + len);
1018  }
1019  }
1020  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
1022  }
1023  str_validate(buf, buf + len, settings);
1024 
1025  // Store sanitized string.
1026  str->assign(buf);
1027  }
1028 
1029  case SLA_PTRS: break;
1030  case SLA_NULL: break;
1031  default: NOT_REACHED();
1032  }
1033 }
1034 
1040 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
1041 {
1042  return SlCalcConvFileLen(conv) * length;
1043 }
1044 
1051 void SlArray(void *array, size_t length, VarType conv)
1052 {
1053  if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
1054 
1055  /* Automatically calculate the length? */
1056  if (_sl.need_length != NL_NONE) {
1057  SlSetLength(SlCalcArrayLen(length, conv));
1058  /* Determine length only? */
1059  if (_sl.need_length == NL_CALCLENGTH) return;
1060  }
1061 
1062  /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1063  * as a byte-type. So detect this, and adjust array size accordingly */
1064  if (_sl.action != SLA_SAVE && _sl_version == 0) {
1065  /* all arrays except difficulty settings */
1066  if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1067  conv == SLE_INT32 || conv == SLE_UINT32) {
1068  SlCopyBytes(array, length * SlCalcConvFileLen(conv));
1069  return;
1070  }
1071  /* used for conversion of Money 32bit->64bit */
1072  if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1073  for (uint i = 0; i < length; i++) {
1074  ((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
1075  }
1076  return;
1077  }
1078  }
1079 
1080  /* If the size of elements is 1 byte both in file and memory, no special
1081  * conversion is needed, use specialized copy-copy function to speed up things */
1082  if (conv == SLE_INT8 || conv == SLE_UINT8) {
1083  SlCopyBytes(array, length);
1084  } else {
1085  byte *a = (byte*)array;
1086  byte mem_size = SlCalcConvMemLen(conv);
1087 
1088  for (; length != 0; length --) {
1089  SlSaveLoadConv(a, conv);
1090  a += mem_size; // get size
1091  }
1092  }
1093 }
1094 
1095 
1106 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1107 {
1108  assert(_sl.action == SLA_SAVE);
1109 
1110  if (obj == nullptr) return 0;
1111 
1112  switch (rt) {
1113  case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1114  case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1115  case REF_STATION: return ((const Station*)obj)->index + 1;
1116  case REF_TOWN: return ((const Town*)obj)->index + 1;
1117  case REF_ORDER: return ((const Order*)obj)->index + 1;
1118  case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1119  case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1120  case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1121  case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1122  case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1123  case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1124  case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1125  default: NOT_REACHED();
1126  }
1127 }
1128 
1139 static void *IntToReference(size_t index, SLRefType rt)
1140 {
1141  static_assert(sizeof(size_t) <= sizeof(void *));
1142 
1143  assert(_sl.action == SLA_PTRS);
1144 
1145  /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1146  * and should be loaded like that */
1147  if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) {
1148  rt = REF_VEHICLE;
1149  }
1150 
1151  /* No need to look up nullptr pointers, just return immediately */
1152  if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return nullptr;
1153 
1154  /* Correct index. Old vehicles were saved differently:
1155  * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1156  if (rt != REF_VEHICLE_OLD) index--;
1157 
1158  switch (rt) {
1159  case REF_ORDERLIST:
1160  if (OrderList::IsValidID(index)) return OrderList::Get(index);
1161  SlErrorCorrupt("Referencing invalid OrderList");
1162 
1163  case REF_ORDER:
1164  if (Order::IsValidID(index)) return Order::Get(index);
1165  /* in old versions, invalid order was used to mark end of order list */
1166  if (IsSavegameVersionBefore(SLV_5, 2)) return nullptr;
1167  SlErrorCorrupt("Referencing invalid Order");
1168 
1169  case REF_VEHICLE_OLD:
1170  case REF_VEHICLE:
1171  if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1172  SlErrorCorrupt("Referencing invalid Vehicle");
1173 
1174  case REF_STATION:
1175  if (Station::IsValidID(index)) return Station::Get(index);
1176  SlErrorCorrupt("Referencing invalid Station");
1177 
1178  case REF_TOWN:
1179  if (Town::IsValidID(index)) return Town::Get(index);
1180  SlErrorCorrupt("Referencing invalid Town");
1181 
1182  case REF_ROADSTOPS:
1183  if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1184  SlErrorCorrupt("Referencing invalid RoadStop");
1185 
1186  case REF_ENGINE_RENEWS:
1187  if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1188  SlErrorCorrupt("Referencing invalid EngineRenew");
1189 
1190  case REF_CARGO_PACKET:
1191  if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1192  SlErrorCorrupt("Referencing invalid CargoPacket");
1193 
1194  case REF_STORAGE:
1195  if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1196  SlErrorCorrupt("Referencing invalid PersistentStorage");
1197 
1198  case REF_LINK_GRAPH:
1199  if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1200  SlErrorCorrupt("Referencing invalid LinkGraph");
1201 
1202  case REF_LINK_GRAPH_JOB:
1203  if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1204  SlErrorCorrupt("Referencing invalid LinkGraphJob");
1205 
1206  default: NOT_REACHED();
1207  }
1208 }
1209 
1214 static inline size_t SlCalcListLen(const void *list)
1215 {
1216  const std::list<void *> *l = (const std::list<void *> *) list;
1217 
1218  int type_size = IsSavegameVersionBefore(SLV_69) ? 2 : 4;
1219  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1220  * of the list */
1221  return l->size() * type_size + type_size;
1222 }
1223 
1224 
1230 static void SlList(void *list, SLRefType conv)
1231 {
1232  /* Automatically calculate the length? */
1233  if (_sl.need_length != NL_NONE) {
1234  SlSetLength(SlCalcListLen(list));
1235  /* Determine length only? */
1236  if (_sl.need_length == NL_CALCLENGTH) return;
1237  }
1238 
1239  typedef std::list<void *> PtrList;
1240  PtrList *l = (PtrList *)list;
1241 
1242  switch (_sl.action) {
1243  case SLA_SAVE: {
1244  SlWriteUint32((uint32)l->size());
1245 
1246  PtrList::iterator iter;
1247  for (iter = l->begin(); iter != l->end(); ++iter) {
1248  void *ptr = *iter;
1249  SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
1250  }
1251  break;
1252  }
1253  case SLA_LOAD_CHECK:
1254  case SLA_LOAD: {
1255  size_t length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1256 
1257  /* Load each reference and push to the end of the list */
1258  for (size_t i = 0; i < length; i++) {
1259  size_t data = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1260  l->push_back((void *)data);
1261  }
1262  break;
1263  }
1264  case SLA_PTRS: {
1265  PtrList temp = *l;
1266 
1267  l->clear();
1268  PtrList::iterator iter;
1269  for (iter = temp.begin(); iter != temp.end(); ++iter) {
1270  void *ptr = IntToReference((size_t)*iter, conv);
1271  l->push_back(ptr);
1272  }
1273  break;
1274  }
1275  case SLA_NULL:
1276  l->clear();
1277  break;
1278  default: NOT_REACHED();
1279  }
1280 }
1281 
1282 
1286 template <typename T>
1288  typedef std::deque<T> SlDequeT;
1289 public:
1295  static size_t SlCalcDequeLen(const void *deque, VarType conv)
1296  {
1297  const SlDequeT *l = (const SlDequeT *)deque;
1298 
1299  int type_size = 4;
1300  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1301  * of the list */
1302  return l->size() * SlCalcConvFileLen(conv) + type_size;
1303  }
1304 
1310  static void SlDeque(void *deque, VarType conv)
1311  {
1312  SlDequeT *l = (SlDequeT *)deque;
1313 
1314  switch (_sl.action) {
1315  case SLA_SAVE: {
1316  SlWriteUint32((uint32)l->size());
1317 
1318  typename SlDequeT::iterator iter;
1319  for (iter = l->begin(); iter != l->end(); ++iter) {
1320  SlSaveLoadConv(&(*iter), conv);
1321  }
1322  break;
1323  }
1324  case SLA_LOAD_CHECK:
1325  case SLA_LOAD: {
1326  size_t length = SlReadUint32();
1327 
1328  /* Load each value and push to the end of the deque */
1329  for (size_t i = 0; i < length; i++) {
1330  T data;
1331  SlSaveLoadConv(&data, conv);
1332  l->push_back(data);
1333  }
1334  break;
1335  }
1336  case SLA_PTRS:
1337  break;
1338  case SLA_NULL:
1339  l->clear();
1340  break;
1341  default: NOT_REACHED();
1342  }
1343  }
1344 };
1345 
1346 
1352 static inline size_t SlCalcDequeLen(const void *deque, VarType conv)
1353 {
1354  switch (GetVarMemType(conv)) {
1355  case SLE_VAR_BL:
1356  return SlDequeHelper<bool>::SlCalcDequeLen(deque, conv);
1357  case SLE_VAR_I8:
1358  case SLE_VAR_U8:
1359  return SlDequeHelper<uint8>::SlCalcDequeLen(deque, conv);
1360  case SLE_VAR_I16:
1361  case SLE_VAR_U16:
1362  return SlDequeHelper<uint16>::SlCalcDequeLen(deque, conv);
1363  case SLE_VAR_I32:
1364  case SLE_VAR_U32:
1365  return SlDequeHelper<uint32>::SlCalcDequeLen(deque, conv);
1366  case SLE_VAR_I64:
1367  case SLE_VAR_U64:
1368  return SlDequeHelper<uint64>::SlCalcDequeLen(deque, conv);
1369  default: NOT_REACHED();
1370  }
1371 }
1372 
1373 
1379 static void SlDeque(void *deque, VarType conv)
1380 {
1381  switch (GetVarMemType(conv)) {
1382  case SLE_VAR_BL:
1383  SlDequeHelper<bool>::SlDeque(deque, conv);
1384  break;
1385  case SLE_VAR_I8:
1386  case SLE_VAR_U8:
1387  SlDequeHelper<uint8>::SlDeque(deque, conv);
1388  break;
1389  case SLE_VAR_I16:
1390  case SLE_VAR_U16:
1391  SlDequeHelper<uint16>::SlDeque(deque, conv);
1392  break;
1393  case SLE_VAR_I32:
1394  case SLE_VAR_U32:
1395  SlDequeHelper<uint32>::SlDeque(deque, conv);
1396  break;
1397  case SLE_VAR_I64:
1398  case SLE_VAR_U64:
1399  SlDequeHelper<uint64>::SlDeque(deque, conv);
1400  break;
1401  default: NOT_REACHED();
1402  }
1403 }
1404 
1405 
1407 static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
1408 {
1409  if (_sl_version < sld->version_from || _sl_version >= sld->version_to) return false;
1410  if (sld->conv & SLF_NOT_IN_SAVE) return false;
1411 
1412  return true;
1413 }
1414 
1420 static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
1421 {
1423  SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
1424  return true;
1425  }
1426 
1427  return false;
1428 }
1429 
1436 size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
1437 {
1438  size_t length = 0;
1439 
1440  /* Need to determine the length and write a length tag. */
1441  for (; sld->cmd != SL_END; sld++) {
1442  length += SlCalcObjMemberLength(object, sld);
1443  }
1444  return length;
1445 }
1446 
1447 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
1448 {
1449  assert(_sl.action == SLA_SAVE);
1450 
1451  switch (sld->cmd) {
1452  case SL_VAR:
1453  case SL_REF:
1454  case SL_ARR:
1455  case SL_STR:
1456  case SL_LST:
1457  case SL_DEQUE:
1458  case SL_STDSTR:
1459  /* CONDITIONAL saveload types depend on the savegame version */
1460  if (!SlIsObjectValidInSavegame(sld)) break;
1461 
1462  switch (sld->cmd) {
1463  case SL_VAR: return SlCalcConvFileLen(sld->conv);
1464  case SL_REF: return SlCalcRefLen();
1465  case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
1466  case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
1467  case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
1468  case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld->conv);
1469  case SL_STDSTR: return SlCalcStdStringLen(GetVariableAddress(object, sld));
1470  default: NOT_REACHED();
1471  }
1472  break;
1473  case SL_WRITEBYTE: return 1; // a byte is logically of size 1
1474  case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
1475  case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
1476  default: NOT_REACHED();
1477  }
1478  return 0;
1479 }
1480 
1481 #ifdef OTTD_ASSERT
1482 
1488 static bool IsVariableSizeRight(const SaveLoad *sld)
1489 {
1490  switch (sld->cmd) {
1491  case SL_VAR:
1492  switch (GetVarMemType(sld->conv)) {
1493  case SLE_VAR_BL:
1494  return sld->size == sizeof(bool);
1495  case SLE_VAR_I8:
1496  case SLE_VAR_U8:
1497  return sld->size == sizeof(int8);
1498  case SLE_VAR_I16:
1499  case SLE_VAR_U16:
1500  return sld->size == sizeof(int16);
1501  case SLE_VAR_I32:
1502  case SLE_VAR_U32:
1503  return sld->size == sizeof(int32);
1504  case SLE_VAR_I64:
1505  case SLE_VAR_U64:
1506  return sld->size == sizeof(int64);
1507  case SLE_VAR_NAME:
1508  return sld->size == sizeof(std::string);
1509  default:
1510  return sld->size == sizeof(void *);
1511  }
1512  case SL_REF:
1513  /* These should all be pointer sized. */
1514  return sld->size == sizeof(void *);
1515 
1516  case SL_STR:
1517  /* These should be pointer sized, or fixed array. */
1518  return sld->size == sizeof(void *) || sld->size == sld->length;
1519 
1520  case SL_STDSTR:
1521  /* These should be all pointers to std::string. */
1522  return sld->size == sizeof(std::string);
1523 
1524  default:
1525  return true;
1526  }
1527 }
1528 
1529 #endif /* OTTD_ASSERT */
1530 
1531 bool SlObjectMember(void *ptr, const SaveLoad *sld)
1532 {
1533 #ifdef OTTD_ASSERT
1534  assert(IsVariableSizeRight(sld));
1535 #endif
1536 
1537  VarType conv = GB(sld->conv, 0, 8);
1538  switch (sld->cmd) {
1539  case SL_VAR:
1540  case SL_REF:
1541  case SL_ARR:
1542  case SL_STR:
1543  case SL_LST:
1544  case SL_DEQUE:
1545  case SL_STDSTR:
1546  /* CONDITIONAL saveload types depend on the savegame version */
1547  if (!SlIsObjectValidInSavegame(sld)) return false;
1548  if (SlSkipVariableOnLoad(sld)) return false;
1549 
1550  switch (sld->cmd) {
1551  case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1552  case SL_REF: // Reference variable, translate
1553  switch (_sl.action) {
1554  case SLA_SAVE:
1555  SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1556  break;
1557  case SLA_LOAD_CHECK:
1558  case SLA_LOAD:
1559  *(size_t *)ptr = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1560  break;
1561  case SLA_PTRS:
1562  *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1563  break;
1564  case SLA_NULL:
1565  *(void **)ptr = nullptr;
1566  break;
1567  default: NOT_REACHED();
1568  }
1569  break;
1570  case SL_ARR: SlArray(ptr, sld->length, conv); break;
1571  case SL_STR: SlString(ptr, sld->length, sld->conv); break;
1572  case SL_LST: SlList(ptr, (SLRefType)conv); break;
1573  case SL_DEQUE: SlDeque(ptr, conv); break;
1574  case SL_STDSTR: SlStdString(ptr, sld->conv); break;
1575  default: NOT_REACHED();
1576  }
1577  break;
1578 
1579  /* SL_WRITEBYTE writes a value to the savegame to identify the type of an object.
1580  * When loading, the value is read explicitly with SlReadByte() to determine which
1581  * object description to use. */
1582  case SL_WRITEBYTE:
1583  switch (_sl.action) {
1584  case SLA_SAVE: SlWriteByte(*(uint8 *)ptr); break;
1585  case SLA_LOAD_CHECK:
1586  case SLA_LOAD:
1587  case SLA_PTRS:
1588  case SLA_NULL: break;
1589  default: NOT_REACHED();
1590  }
1591  break;
1592 
1593  /* SL_VEH_INCLUDE loads common code for vehicles */
1594  case SL_VEH_INCLUDE:
1595  SlObject(ptr, GetVehicleDescription(VEH_END));
1596  break;
1597 
1598  case SL_ST_INCLUDE:
1600  break;
1601 
1602  default: NOT_REACHED();
1603  }
1604  return true;
1605 }
1606 
1612 void SlObject(void *object, const SaveLoad *sld)
1613 {
1614  /* Automatically calculate the length? */
1615  if (_sl.need_length != NL_NONE) {
1616  SlSetLength(SlCalcObjLength(object, sld));
1617  if (_sl.need_length == NL_CALCLENGTH) return;
1618  }
1619 
1620  for (; sld->cmd != SL_END; sld++) {
1621  void *ptr = GetVariableAddress(object, sld);
1622  SlObjectMember(ptr, sld);
1623  }
1624 }
1625 
1631 {
1632  SlObject(nullptr, (const SaveLoad*)sldg);
1633 }
1634 
1640 void SlAutolength(AutolengthProc *proc, void *arg)
1641 {
1642  size_t offs;
1643 
1644  assert(_sl.action == SLA_SAVE);
1645 
1646  /* Tell it to calculate the length */
1648  _sl.obj_len = 0;
1649  proc(arg);
1650 
1651  /* Setup length */
1654 
1655  offs = _sl.dumper->GetSize() + _sl.obj_len;
1656 
1657  /* And write the stuff */
1658  proc(arg);
1659 
1660  if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
1661 }
1662 
1667 static void SlLoadChunk(const ChunkHandler *ch)
1668 {
1669  byte m = SlReadByte();
1670  size_t len;
1671  size_t endoffs;
1672 
1673  _sl.block_mode = m;
1674  _sl.obj_len = 0;
1675 
1676  switch (m) {
1677  case CH_ARRAY:
1678  _sl.array_index = 0;
1679  ch->load_proc();
1680  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1681  break;
1682  case CH_SPARSE_ARRAY:
1683  ch->load_proc();
1684  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1685  break;
1686  default:
1687  if ((m & 0xF) == CH_RIFF) {
1688  /* Read length */
1689  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1690  len += SlReadUint16();
1691  _sl.obj_len = len;
1692  endoffs = _sl.reader->GetSize() + len;
1693  ch->load_proc();
1694  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1695  } else {
1696  SlErrorCorrupt("Invalid chunk type");
1697  }
1698  break;
1699  }
1700 }
1701 
1707 static void SlLoadCheckChunk(const ChunkHandler *ch)
1708 {
1709  byte m = SlReadByte();
1710  size_t len;
1711  size_t endoffs;
1712 
1713  _sl.block_mode = m;
1714  _sl.obj_len = 0;
1715 
1716  switch (m) {
1717  case CH_ARRAY:
1718  _sl.array_index = 0;
1719  if (ch->load_check_proc) {
1720  ch->load_check_proc();
1721  } else {
1722  SlSkipArray();
1723  }
1724  break;
1725  case CH_SPARSE_ARRAY:
1726  if (ch->load_check_proc) {
1727  ch->load_check_proc();
1728  } else {
1729  SlSkipArray();
1730  }
1731  break;
1732  default:
1733  if ((m & 0xF) == CH_RIFF) {
1734  /* Read length */
1735  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1736  len += SlReadUint16();
1737  _sl.obj_len = len;
1738  endoffs = _sl.reader->GetSize() + len;
1739  if (ch->load_check_proc) {
1740  ch->load_check_proc();
1741  } else {
1742  SlSkipBytes(len);
1743  }
1744  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1745  } else {
1746  SlErrorCorrupt("Invalid chunk type");
1747  }
1748  break;
1749  }
1750 }
1751 
1757 static void SlSaveChunk(const ChunkHandler *ch)
1758 {
1759  ChunkSaveLoadProc *proc = ch->save_proc;
1760 
1761  /* Don't save any chunk information if there is no save handler. */
1762  if (proc == nullptr) return;
1763 
1764  SlWriteUint32(ch->id);
1765  DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1766 
1767  _sl.block_mode = ch->flags & CH_TYPE_MASK;
1768  switch (ch->flags & CH_TYPE_MASK) {
1769  case CH_RIFF:
1771  proc();
1772  break;
1773  case CH_ARRAY:
1774  _sl.last_array_index = 0;
1775  SlWriteByte(CH_ARRAY);
1776  proc();
1777  SlWriteArrayLength(0); // Terminate arrays
1778  break;
1779  case CH_SPARSE_ARRAY:
1780  SlWriteByte(CH_SPARSE_ARRAY);
1781  proc();
1782  SlWriteArrayLength(0); // Terminate arrays
1783  break;
1784  default: NOT_REACHED();
1785  }
1786 }
1787 
1789 static void SlSaveChunks()
1790 {
1792  SlSaveChunk(ch);
1793  }
1794 
1795  /* Terminator */
1796  SlWriteUint32(0);
1797 }
1798 
1805 static const ChunkHandler *SlFindChunkHandler(uint32 id)
1806 {
1807  FOR_ALL_CHUNK_HANDLERS(ch) if (ch->id == id) return ch;
1808  return nullptr;
1809 }
1810 
1812 static void SlLoadChunks()
1813 {
1814  uint32 id;
1815  const ChunkHandler *ch;
1816 
1817  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1818  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1819 
1820  ch = SlFindChunkHandler(id);
1821  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
1822  SlLoadChunk(ch);
1823  }
1824 }
1825 
1827 static void SlLoadCheckChunks()
1828 {
1829  uint32 id;
1830  const ChunkHandler *ch;
1831 
1832  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1833  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1834 
1835  ch = SlFindChunkHandler(id);
1836  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
1837  SlLoadCheckChunk(ch);
1838  }
1839 }
1840 
1842 static void SlFixPointers()
1843 {
1844  _sl.action = SLA_PTRS;
1845 
1847  if (ch->ptrs_proc != nullptr) {
1848  DEBUG(sl, 3, "Fixing pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1849  ch->ptrs_proc();
1850  }
1851  }
1852 
1853  assert(_sl.action == SLA_PTRS);
1854 }
1855 
1856 
1859  FILE *file;
1860  long begin;
1861 
1866  FileReader(FILE *file) : LoadFilter(nullptr), file(file), begin(ftell(file))
1867  {
1868  }
1869 
1872  {
1873  if (this->file != nullptr) fclose(this->file);
1874  this->file = nullptr;
1875 
1876  /* Make sure we don't double free. */
1877  _sl.sf = nullptr;
1878  }
1879 
1880  size_t Read(byte *buf, size_t size) override
1881  {
1882  /* We're in the process of shutting down, i.e. in "failure" mode. */
1883  if (this->file == nullptr) return 0;
1884 
1885  return fread(buf, 1, size, this->file);
1886  }
1887 
1888  void Reset() override
1889  {
1890  clearerr(this->file);
1891  if (fseek(this->file, this->begin, SEEK_SET)) {
1892  DEBUG(sl, 1, "Could not reset the file reading");
1893  }
1894  }
1895 };
1896 
1899  FILE *file;
1900 
1905  FileWriter(FILE *file) : SaveFilter(nullptr), file(file)
1906  {
1907  }
1908 
1911  {
1912  this->Finish();
1913 
1914  /* Make sure we don't double free. */
1915  _sl.sf = nullptr;
1916  }
1917 
1918  void Write(byte *buf, size_t size) override
1919  {
1920  /* We're in the process of shutting down, i.e. in "failure" mode. */
1921  if (this->file == nullptr) return;
1922 
1923  if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
1924  }
1925 
1926  void Finish() override
1927  {
1928  if (this->file != nullptr) fclose(this->file);
1929  this->file = nullptr;
1930  }
1931 };
1932 
1933 /*******************************************
1934  ********** START OF LZO CODE **************
1935  *******************************************/
1936 
1937 #ifdef WITH_LZO
1938 #include <lzo/lzo1x.h>
1939 
1941 static const uint LZO_BUFFER_SIZE = 8192;
1942 
1950  {
1951  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
1952  }
1953 
1954  size_t Read(byte *buf, size_t ssize) override
1955  {
1956  assert(ssize >= LZO_BUFFER_SIZE);
1957 
1958  /* Buffer size is from the LZO docs plus the chunk header size. */
1959  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1960  uint32 tmp[2];
1961  uint32 size;
1962  lzo_uint len = ssize;
1963 
1964  /* Read header*/
1965  if (this->chain->Read((byte*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
1966 
1967  /* Check if size is bad */
1968  ((uint32*)out)[0] = size = tmp[1];
1969 
1970  if (_sl_version != SL_MIN_VERSION) {
1971  tmp[0] = TO_BE32(tmp[0]);
1972  size = TO_BE32(size);
1973  }
1974 
1975  if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
1976 
1977  /* Read block */
1978  if (this->chain->Read(out + sizeof(uint32), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
1979 
1980  /* Verify checksum */
1981  if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
1982 
1983  /* Decompress */
1984  int ret = lzo1x_decompress_safe(out + sizeof(uint32) * 1, size, buf, &len, nullptr);
1985  if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
1986  return len;
1987  }
1988 };
1989 
1997  LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
1998  {
1999  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2000  }
2001 
2002  void Write(byte *buf, size_t size) override
2003  {
2004  const lzo_bytep in = buf;
2005  /* Buffer size is from the LZO docs plus the chunk header size. */
2006  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
2007  byte wrkmem[LZO1X_1_MEM_COMPRESS];
2008  lzo_uint outlen;
2009 
2010  do {
2011  /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2012  lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
2013  lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
2014  ((uint32*)out)[1] = TO_BE32((uint32)outlen);
2015  ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
2016  this->chain->Write(out, outlen + sizeof(uint32) * 2);
2017 
2018  /* Move to next data chunk. */
2019  size -= len;
2020  in += len;
2021  } while (size > 0);
2022  }
2023 };
2024 
2025 #endif /* WITH_LZO */
2026 
2027 /*********************************************
2028  ******** START OF NOCOMP CODE (uncompressed)*
2029  *********************************************/
2030 
2038  {
2039  }
2040 
2041  size_t Read(byte *buf, size_t size) override
2042  {
2043  return this->chain->Read(buf, size);
2044  }
2045 };
2046 
2054  NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2055  {
2056  }
2057 
2058  void Write(byte *buf, size_t size) override
2059  {
2060  this->chain->Write(buf, size);
2061  }
2062 };
2063 
2064 /********************************************
2065  ********** START OF ZLIB CODE **************
2066  ********************************************/
2067 
2068 #if defined(WITH_ZLIB)
2069 #include <zlib.h>
2070 
2073  z_stream z;
2081  {
2082  memset(&this->z, 0, sizeof(this->z));
2083  if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2084  }
2085 
2087  ~ZlibLoadFilter()
2088  {
2089  inflateEnd(&this->z);
2090  }
2091 
2092  size_t Read(byte *buf, size_t size) override
2093  {
2094  this->z.next_out = buf;
2095  this->z.avail_out = (uint)size;
2096 
2097  do {
2098  /* read more bytes from the file? */
2099  if (this->z.avail_in == 0) {
2100  this->z.next_in = this->fread_buf;
2101  this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2102  }
2103 
2104  /* inflate the data */
2105  int r = inflate(&this->z, 0);
2106  if (r == Z_STREAM_END) break;
2107 
2108  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2109  } while (this->z.avail_out != 0);
2110 
2111  return size - this->z.avail_out;
2112  }
2113 };
2114 
2117  z_stream z;
2118 
2124  ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2125  {
2126  memset(&this->z, 0, sizeof(this->z));
2127  if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2128  }
2129 
2132  {
2133  deflateEnd(&this->z);
2134  }
2135 
2142  void WriteLoop(byte *p, size_t len, int mode)
2143  {
2144  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2145  uint n;
2146  this->z.next_in = p;
2147  this->z.avail_in = (uInt)len;
2148  do {
2149  this->z.next_out = buf;
2150  this->z.avail_out = sizeof(buf);
2151 
2159  int r = deflate(&this->z, mode);
2160 
2161  /* bytes were emitted? */
2162  if ((n = sizeof(buf) - this->z.avail_out) != 0) {
2163  this->chain->Write(buf, n);
2164  }
2165  if (r == Z_STREAM_END) break;
2166 
2167  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2168  } while (this->z.avail_in || !this->z.avail_out);
2169  }
2170 
2171  void Write(byte *buf, size_t size) override
2172  {
2173  this->WriteLoop(buf, size, 0);
2174  }
2175 
2176  void Finish() override
2177  {
2178  this->WriteLoop(nullptr, 0, Z_FINISH);
2179  this->chain->Finish();
2180  }
2181 };
2182 
2183 #endif /* WITH_ZLIB */
2184 
2185 /********************************************
2186  ********** START OF LZMA CODE **************
2187  ********************************************/
2188 
2189 #if defined(WITH_LIBLZMA)
2190 #include <lzma.h>
2191 
2198 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2199 
2202  lzma_stream lzma;
2204 
2210  {
2211  /* Allow saves up to 256 MB uncompressed */
2212  if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2213  }
2214 
2217  {
2218  lzma_end(&this->lzma);
2219  }
2220 
2221  size_t Read(byte *buf, size_t size) override
2222  {
2223  this->lzma.next_out = buf;
2224  this->lzma.avail_out = size;
2225 
2226  do {
2227  /* read more bytes from the file? */
2228  if (this->lzma.avail_in == 0) {
2229  this->lzma.next_in = this->fread_buf;
2230  this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2231  }
2232 
2233  /* inflate the data */
2234  lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2235  if (r == LZMA_STREAM_END) break;
2236  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2237  } while (this->lzma.avail_out != 0);
2238 
2239  return size - this->lzma.avail_out;
2240  }
2241 };
2242 
2245  lzma_stream lzma;
2246 
2253  {
2254  if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2255  }
2256 
2259  {
2260  lzma_end(&this->lzma);
2261  }
2262 
2269  void WriteLoop(byte *p, size_t len, lzma_action action)
2270  {
2271  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2272  size_t n;
2273  this->lzma.next_in = p;
2274  this->lzma.avail_in = len;
2275  do {
2276  this->lzma.next_out = buf;
2277  this->lzma.avail_out = sizeof(buf);
2278 
2279  lzma_ret r = lzma_code(&this->lzma, action);
2280 
2281  /* bytes were emitted? */
2282  if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
2283  this->chain->Write(buf, n);
2284  }
2285  if (r == LZMA_STREAM_END) break;
2286  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2287  } while (this->lzma.avail_in || !this->lzma.avail_out);
2288  }
2289 
2290  void Write(byte *buf, size_t size) override
2291  {
2292  this->WriteLoop(buf, size, LZMA_RUN);
2293  }
2294 
2295  void Finish() override
2296  {
2297  this->WriteLoop(nullptr, 0, LZMA_FINISH);
2298  this->chain->Finish();
2299  }
2300 };
2301 
2302 #endif /* WITH_LIBLZMA */
2303 
2304 /*******************************************
2305  ************* END OF CODE *****************
2306  *******************************************/
2307 
2310  const char *name;
2311  uint32 tag;
2313  LoadFilter *(*init_load)(LoadFilter *chain);
2314  SaveFilter *(*init_write)(SaveFilter *chain, byte compression);
2317  byte default_compression;
2319 };
2323 #if defined(WITH_LZO)
2324  /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2325  {"lzo", TO_BE32X('OTTD'), CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2326 #else
2327  {"lzo", TO_BE32X('OTTD'), nullptr, nullptr, 0, 0, 0},
2328 #endif
2329  /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2330  {"none", TO_BE32X('OTTN'), CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2331 #if defined(WITH_ZLIB)
2332  /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2333  * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2334  * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2335  {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2336 #else
2337  {"zlib", TO_BE32X('OTTZ'), nullptr, nullptr, 0, 0, 0},
2338 #endif
2339 #if defined(WITH_LIBLZMA)
2340  /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2341  * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2342  * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2343  * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2344  * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2345  {"lzma", TO_BE32X('OTTX'), CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2346 #else
2347  {"lzma", TO_BE32X('OTTX'), nullptr, nullptr, 0, 0, 0},
2348 #endif
2349 };
2350 
2358 static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
2359 {
2360  const SaveLoadFormat *def = lastof(_saveload_formats);
2361 
2362  /* find default savegame format, the highest one with which files can be written */
2363  while (!def->init_write) def--;
2364 
2365  if (!StrEmpty(s)) {
2366  /* Get the ":..." of the compression level out of the way */
2367  char *complevel = strrchr(s, ':');
2368  if (complevel != nullptr) *complevel = '\0';
2369 
2370  for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
2371  if (slf->init_write != nullptr && strcmp(s, slf->name) == 0) {
2372  *compression_level = slf->default_compression;
2373  if (complevel != nullptr) {
2374  /* There is a compression level in the string.
2375  * First restore the : we removed to do proper name matching,
2376  * then move the the begin of the actual version. */
2377  *complevel = ':';
2378  complevel++;
2379 
2380  /* Get the version and determine whether all went fine. */
2381  char *end;
2382  long level = strtol(complevel, &end, 10);
2383  if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) {
2384  SetDParamStr(0, complevel);
2385  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2386  } else {
2387  *compression_level = level;
2388  }
2389  }
2390  return slf;
2391  }
2392  }
2393 
2394  SetDParamStr(0, s);
2395  SetDParamStr(1, def->name);
2396  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2397 
2398  /* Restore the string by adding the : back */
2399  if (complevel != nullptr) *complevel = ':';
2400  }
2401  *compression_level = def->default_compression;
2402  return def;
2403 }
2404 
2405 /* actual loader/saver function */
2406 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2407 extern bool AfterLoadGame();
2408 extern bool LoadOldSaveGame(const std::string &file);
2409 
2413 static void ResetSaveloadData()
2414 {
2415  ResetTempEngineData();
2416  ResetLabelMaps();
2417  ResetOldWaypoints();
2418 }
2419 
2423 static inline void ClearSaveLoadState()
2424 {
2425  delete _sl.dumper;
2426  _sl.dumper = nullptr;
2427 
2428  delete _sl.sf;
2429  _sl.sf = nullptr;
2430 
2431  delete _sl.reader;
2432  _sl.reader = nullptr;
2433 
2434  delete _sl.lf;
2435  _sl.lf = nullptr;
2436 }
2437 
2443 static void SaveFileStart()
2444 {
2446  _game_speed = 100;
2447  SetMouseCursorBusy(true);
2448 
2450  _sl.saveinprogress = true;
2451 }
2452 
2454 static void SaveFileDone()
2455 {
2456  if (_game_mode != GM_MENU) _game_speed = _sl.game_speed;
2457  SetMouseCursorBusy(false);
2458 
2460  _sl.saveinprogress = false;
2461 
2462 #ifdef __EMSCRIPTEN__
2463  EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
2464 #endif
2465 }
2466 
2469 {
2470  _sl.error_str = str;
2471 }
2472 
2475 {
2476  SetDParam(0, _sl.error_str);
2478 
2479  static char err_str[512];
2480  GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
2481  return err_str;
2482 }
2483 
2485 static void SaveFileError()
2486 {
2488  ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
2489  SaveFileDone();
2490 }
2491 
2496 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2497 {
2498  try {
2499  byte compression;
2500  const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
2501 
2502  /* We have written our stuff to memory, now write it to file! */
2503  uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
2504  _sl.sf->Write((byte*)hdr, sizeof(hdr));
2505 
2506  _sl.sf = fmt->init_write(_sl.sf, compression);
2507  _sl.dumper->Flush(_sl.sf);
2508 
2510 
2511  if (threaded) SetAsyncSaveFinish(SaveFileDone);
2512 
2513  return SL_OK;
2514  } catch (...) {
2516 
2518 
2519  /* We don't want to shout when saving is just
2520  * cancelled due to a client disconnecting. */
2521  if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2522  /* Skip the "colour" character */
2523  DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2524  asfp = SaveFileError;
2525  }
2526 
2527  if (threaded) {
2528  SetAsyncSaveFinish(asfp);
2529  } else {
2530  asfp();
2531  }
2532  return SL_ERROR;
2533  }
2534 }
2535 
2536 void WaitTillSaved()
2537 {
2538  if (!_save_thread.joinable()) return;
2539 
2540  _save_thread.join();
2541 
2542  /* Make sure every other state is handled properly as well. */
2544 }
2545 
2554 static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
2555 {
2556  assert(!_sl.saveinprogress);
2557 
2558  _sl.dumper = new MemoryDumper();
2559  _sl.sf = writer;
2560 
2562 
2563  SaveViewportBeforeSaveGame();
2564  SlSaveChunks();
2565 
2566  SaveFileStart();
2567 
2568  if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
2569  if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2570 
2571  SaveOrLoadResult result = SaveFileToDisk(false);
2572  SaveFileDone();
2573 
2574  return result;
2575  }
2576 
2577  return SL_OK;
2578 }
2579 
2587 {
2588  try {
2589  _sl.action = SLA_SAVE;
2590  return DoSave(writer, threaded);
2591  } catch (...) {
2593  return SL_ERROR;
2594  }
2595 }
2596 
2603 static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
2604 {
2605  _sl.lf = reader;
2606 
2607  if (load_check) {
2608  /* Clear previous check data */
2610  /* Mark SL_LOAD_CHECK as supported for this savegame. */
2611  _load_check_data.checkable = true;
2612  }
2613 
2614  uint32 hdr[2];
2615  if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2616 
2617  /* see if we have any loader for this type. */
2618  const SaveLoadFormat *fmt = _saveload_formats;
2619  for (;;) {
2620  /* No loader found, treat as version 0 and use LZO format */
2621  if (fmt == endof(_saveload_formats)) {
2622  DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2623  _sl.lf->Reset();
2625  _sl_minor_version = 0;
2626 
2627  /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2628  fmt = _saveload_formats;
2629  for (;;) {
2630  if (fmt == endof(_saveload_formats)) {
2631  /* Who removed LZO support? Bad bad boy! */
2632  NOT_REACHED();
2633  }
2634  if (fmt->tag == TO_BE32X('OTTD')) break;
2635  fmt++;
2636  }
2637  break;
2638  }
2639 
2640  if (fmt->tag == hdr[0]) {
2641  /* check version number */
2642  _sl_version = (SaveLoadVersion)(TO_BE32(hdr[1]) >> 16);
2643  /* Minor is not used anymore from version 18.0, but it is still needed
2644  * in versions before that (4 cases) which can't be removed easy.
2645  * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2646  _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
2647 
2648  DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
2649 
2650  /* Is the version higher than the current? */
2651  if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2652  if (_sl_version >= SLV_START_PATCHPACKS && _sl_version <= SLV_END_PATCHPACKS) SlError(STR_GAME_SAVELOAD_ERROR_PATCHPACK);
2653  break;
2654  }
2655 
2656  fmt++;
2657  }
2658 
2659  /* loader for this savegame type is not implemented? */
2660  if (fmt->init_load == nullptr) {
2661  char err_str[64];
2662  seprintf(err_str, lastof(err_str), "Loader for '%s' is not available.", fmt->name);
2663  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
2664  }
2665 
2666  _sl.lf = fmt->init_load(_sl.lf);
2667  _sl.reader = new ReadBuffer(_sl.lf);
2668  _next_offs = 0;
2669 
2670  if (!load_check) {
2672 
2673  /* Old maps were hardcoded to 256x256 and thus did not contain
2674  * any mapsize information. Pre-initialize to 256x256 to not to
2675  * confuse old games */
2676  InitializeGame(256, 256, true, true);
2677 
2678  GamelogReset();
2679 
2681  /*
2682  * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
2683  * shared savegame version 4. Anything before that 'obviously'
2684  * does not have any NewGRFs. Between the introduction and
2685  * savegame version 41 (just before 0.5) the NewGRF settings
2686  * were not stored in the savegame and they were loaded by
2687  * using the settings from the main menu.
2688  * So, to recap:
2689  * - savegame version < 4: do not load any NewGRFs.
2690  * - savegame version >= 41: load NewGRFs from savegame, which is
2691  * already done at this stage by
2692  * overwriting the main menu settings.
2693  * - other savegame versions: use main menu settings.
2694  *
2695  * This means that users *can* crash savegame version 4..40
2696  * savegames if they set incompatible NewGRFs in the main menu,
2697  * but can't crash anymore for savegame version < 4 savegames.
2698  *
2699  * Note: this is done here because AfterLoadGame is also called
2700  * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
2701  */
2703  }
2704  }
2705 
2706  if (load_check) {
2707  /* Load chunks into _load_check_data.
2708  * No pools are loaded. References are not possible, and thus do not need resolving. */
2710  } else {
2711  /* Load chunks and resolve references */
2712  SlLoadChunks();
2713  SlFixPointers();
2714  }
2715 
2717 
2719 
2720  if (load_check) {
2721  /* The only part from AfterLoadGame() we need */
2723  } else {
2725 
2726  /* After loading fix up savegame for any internal changes that
2727  * might have occurred since then. If it fails, load back the old game. */
2728  if (!AfterLoadGame()) {
2730  return SL_REINIT;
2731  }
2732 
2734  }
2735 
2736  return SL_OK;
2737 }
2738 
2745 {
2746  try {
2747  _sl.action = SLA_LOAD;
2748  return DoLoad(reader, false);
2749  } catch (...) {
2751  return SL_REINIT;
2752  }
2753 }
2754 
2764 SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
2765 {
2766  /* An instance of saving is already active, so don't go saving again */
2767  if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
2768  /* if not an autosave, but a user action, show error message */
2769  if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
2770  return SL_OK;
2771  }
2772  WaitTillSaved();
2773 
2774  try {
2775  /* Load a TTDLX or TTDPatch game */
2776  if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
2778 
2779  InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
2780 
2781  /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
2782  * and if so a new NewGRF list will be made in LoadOldSaveGame.
2783  * Note: this is done here because AfterLoadGame is also called
2784  * for OTTD savegames which have their own NewGRF logic. */
2786  GamelogReset();
2787  if (!LoadOldSaveGame(filename)) return SL_REINIT;
2789  _sl_minor_version = 0;
2791  if (!AfterLoadGame()) {
2793  return SL_REINIT;
2794  }
2796  return SL_OK;
2797  }
2798 
2799  assert(dft == DFT_GAME_FILE);
2800  switch (fop) {
2801  case SLO_CHECK:
2803  break;
2804 
2805  case SLO_LOAD:
2806  _sl.action = SLA_LOAD;
2807  break;
2808 
2809  case SLO_SAVE:
2810  _sl.action = SLA_SAVE;
2811  break;
2812 
2813  default: NOT_REACHED();
2814  }
2815 
2816  FILE *fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
2817 
2818  /* Make it a little easier to load savegames from the console */
2819  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
2820  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
2821  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
2822 
2823  if (fh == nullptr) {
2824  SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2825  }
2826 
2827  if (fop == SLO_SAVE) { // SAVE game
2828  DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename.c_str());
2829  if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
2830 
2831  return DoSave(new FileWriter(fh), threaded);
2832  }
2833 
2834  /* LOAD game */
2835  assert(fop == SLO_LOAD || fop == SLO_CHECK);
2836  DEBUG(desync, 1, "load: %s", filename.c_str());
2837  return DoLoad(new FileReader(fh), fop == SLO_CHECK);
2838  } catch (...) {
2839  /* This code may be executed both for old and new save games. */
2841 
2842  /* Skip the "colour" character */
2843  if (fop != SLO_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2844 
2845  /* A saver/loader exception!! reinitialize all variables to prevent crash! */
2846  return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
2847  }
2848 }
2849 
2852 {
2854 }
2855 
2861 void GenerateDefaultSaveName(char *buf, const char *last)
2862 {
2863  /* Check if we have a name for this map, which is the name of the first
2864  * available company. When there's no company available we'll use
2865  * 'Spectator' as "company" name. */
2866  CompanyID cid = _local_company;
2867  if (!Company::IsValidID(cid)) {
2868  for (const Company *c : Company::Iterate()) {
2869  cid = c->index;
2870  break;
2871  }
2872  }
2873 
2874  SetDParam(0, cid);
2875 
2876  /* Insert current date */
2878  case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
2879  case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
2880  case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
2881  default: NOT_REACHED();
2882  }
2883  SetDParam(2, _date);
2884 
2885  /* Get the correct string (special string for when there's not company) */
2886  GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
2887  SanitizeFilename(buf);
2888 }
2889 
2895 {
2897 }
2898 
2906 {
2907  if (aft == FT_INVALID || aft == FT_NONE) {
2908  this->file_op = SLO_INVALID;
2909  this->detail_ftype = DFT_INVALID;
2910  this->abstract_ftype = FT_INVALID;
2911  return;
2912  }
2913 
2914  this->file_op = fop;
2915  this->detail_ftype = dft;
2916  this->abstract_ftype = aft;
2917 }
2918 
2923 void FileToSaveLoad::SetName(const char *name)
2924 {
2925  this->name = name;
2926 }
2927 
2932 void FileToSaveLoad::SetTitle(const char *title)
2933 {
2934  strecpy(this->title, title, lastof(this->title));
2935 }
FileToSaveLoad::title
char title[255]
Internal name of the game.
Definition: saveload.h:345
ZlibLoadFilter::fread_buf
byte fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
Definition: saveload.cpp:2076
SlLoadChunks
static void SlLoadChunks()
Load all chunks.
Definition: saveload.cpp:1812
ResetSaveloadData
static void ResetSaveloadData()
Clear temporary data that is passed between various saveload phases.
Definition: saveload.cpp:2413
SetMouseCursorBusy
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1738
SaveLoad::version_to
SaveLoadVersion version_to
save/load the variable until this savegame version
Definition: saveload.h:522
SaveLoadFormat::init_write
SaveFilter *(* init_write)(SaveFilter *chain, byte compression)
Constructor for the save filter.
Definition: saveload.cpp:2316
FileWriter::FileWriter
FileWriter(FILE *file)
Create the file writer, so it writes to a specific file.
Definition: saveload.cpp:1905
LZMASaveFilter::lzma
lzma_stream lzma
Stream state that we are writing to.
Definition: saveload.cpp:2245
SLV_69
@ SLV_69
69 10319
Definition: saveload.h:125
SlLoadChunk
static void SlLoadChunk(const ChunkHandler *ch)
Load a chunk of data (eg vehicles, stations, etc.)
Definition: saveload.cpp:1667
InvalidateWindowData
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:3321
LZO_BUFFER_SIZE
static const uint LZO_BUFFER_SIZE
Buffer size for the LZO compressor.
Definition: saveload.cpp:1941
REF_ORDER
@ REF_ORDER
Load/save a reference to an order.
Definition: saveload.h:391
SlDeque
static void SlDeque(void *deque, VarType conv)
Save/load a std::deque.
Definition: saveload.cpp:1379
SLV_169
@ SLV_169
169 23816
Definition: saveload.h:245
LoadCheckData::checkable
bool checkable
True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable....
Definition: fios.h:32
SetSaveLoadError
void SetSaveLoadError(StringID str)
Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friend...
Definition: saveload.cpp:2468
ZlibLoadFilter::~ZlibLoadFilter
~ZlibLoadFilter()
Clean everything up.
Definition: saveload.cpp:2089
Pool::PoolItem<&_orderlist_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
SAVE_DIR
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:110
SaveLoadFormat::min_compression
byte min_compression
the minimum compression level of this format
Definition: saveload.cpp:2318
SGT_OTTD
@ SGT_OTTD
OTTD savegame.
Definition: saveload.h:358
SVS_ALLOW_NEWLINE
@ SVS_ALLOW_NEWLINE
Allow newlines.
Definition: string_type.h:51
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:39
SLF_NOT_IN_SAVE
@ SLF_NOT_IN_SAVE
do not save with savegame, basically client-based
Definition: saveload.h:487
_save_thread
static std::thread _save_thread
The thread we're using to compress and write a savegame.
Definition: saveload.cpp:385
_name_chunk_handlers
const ChunkHandler _name_chunk_handlers[]
Chunk handlers related to strings.
SLE_VAR_STR
@ SLE_VAR_STR
string pointer
Definition: saveload.h:449
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
LZMALoadFilter::LZMALoadFilter
LZMALoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2209
GetVarFileType
static VarType GetVarFileType(VarType type)
Get the FileType of a setting.
Definition: saveload.h:868
LZMASaveFilter::LZMASaveFilter
LZMASaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2252
NoCompLoadFilter
Filter without any compression.
Definition: saveload.cpp:2032
_sl_minor_version
byte _sl_minor_version
the minor savegame version, DO NOT USE!
Definition: saveload.cpp:66
LZMALoadFilter::fread_buf
byte fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
Definition: saveload.cpp:2203
SLA_SAVE
@ SLA_SAVE
saving
Definition: saveload.cpp:73
RemapOldStringID
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
Definition: strings_sl.cpp:29
CSleep
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:22
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
SaveLoadFormat::init_load
LoadFilter *(* init_load)(LoadFilter *chain)
Constructor for the load filter.
Definition: saveload.cpp:2315
str_validate
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:255
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:394
DoExitSave
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
Definition: saveload.cpp:2851
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:402
NoCompLoadFilter::NoCompLoadFilter
NoCompLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2039
SlLoadCheckChunk
static void SlLoadCheckChunk(const ChunkHandler *ch)
Load a chunk of data for checking savegames.
Definition: saveload.cpp:1707
GUISettings::date_format_in_default_names
uint8 date_format_in_default_names
should the default savegame/screenshot name use long dates (31th Dec 2008), short dates (31-12-2008) ...
Definition: settings_type.h:126
NoCompSaveFilter::NoCompSaveFilter
NoCompSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2054
SL_STR
@ SL_STR
Save/load a string.
Definition: saveload.h:503
REF_ROADSTOPS
@ REF_ROADSTOPS
Load/save a reference to a bus/truck stop.
Definition: saveload.h:396
FileToSaveLoad::SetTitle
void SetTitle(const char *title)
Set the title of the file.
Definition: saveload.cpp:2932
_town_chunk_handlers
const ChunkHandler _town_chunk_handlers[]
Chunk handler for towns.
FileReader::Read
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:1880
Station
Station data structure.
Definition: station_base.h:450
SlSkipArray
void SlSkipArray()
Skip an array or sparse array.
Definition: saveload.cpp:664
LinkGraphJob
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:30
SaveOrLoad
SaveOrLoadResult SaveOrLoad(const std::string &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:2764
_date_fract
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:29
_network_server
bool _network_server
network-server is active
Definition: network.cpp:53
SaveLoadOperation
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:47
FileToSaveLoad::name
std::string name
Name of the file.
Definition: saveload.h:344
str_fix_scc_encoded
void str_fix_scc_encoded(char *str, const char *last)
Scan the string for old values of SCC_ENCODED and fix it to it's new, static value.
Definition: string.cpp:169
FileReader::begin
long begin
The begin of the file.
Definition: saveload.cpp:1860
SaveLoad::size
size_t size
the sizeof size.
Definition: saveload.h:523
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
LZOLoadFilter::LZOLoadFilter
LZOLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:1949
AfterLoadGame
bool AfterLoadGame()
Perform a (large) amount of savegame conversion magic in order to load older savegames and to fill th...
Definition: afterload.cpp:543
SLF_ALLOW_NEWLINE
@ SLF_ALLOW_NEWLINE
allow new lines in the strings
Definition: saveload.h:491
ZlibLoadFilter::ZlibLoadFilter
ZlibLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2082
SlSaveChunks
static void SlSaveChunks()
Save all chunks.
Definition: saveload.cpp:1789
SLA_LOAD_CHECK
@ SLA_LOAD_CHECK
partial loading into _load_check_data
Definition: saveload.cpp:76
SLO_CHECK
@ SLO_CHECK
Load file for checking and/or preview.
Definition: fileio_type.h:48
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
SLE_VAR_STRBQ
@ SLE_VAR_STRBQ
string enclosed in quotes (with pre-allocated buffer)
Definition: saveload.h:448
MemoryDumper::MemoryDumper
MemoryDumper()
Initialise our variables.
Definition: saveload.cpp:136
_do_autosave
bool _do_autosave
are we doing an autosave at the moment?
Definition: saveload.cpp:68
LZOSaveFilter::LZOSaveFilter
LZOSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:1997
MemoryDumper::GetSize
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:186
DFT_GAME_FILE
@ DFT_GAME_FILE
Save game or scenario file.
Definition: fileio_type.h:31
FileToSaveLoad
Deals with the type of the savegame, independent of extension.
Definition: saveload.h:340
LoadCheckData::grfconfig
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:43
FileToSaveLoad::SetName
void SetName(const char *name)
Set the name of the file.
Definition: saveload.cpp:2923
SaveLoadFormat::max_compression
byte max_compression
the maximum compression level of this format
Definition: saveload.cpp:2320
SLE_VAR_NULL
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition: saveload.h:446
LZOLoadFilter::Read
size_t Read(byte *buf, size_t ssize) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:1954
SaveLoad::length
uint16 length
(conditional) length of the variable (eg. arrays) (max array size is 65536 elements)
Definition: saveload.h:520
ReadBuffer
A buffer for reading (and buffering) savegame data.
Definition: saveload.cpp:89
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:219
AUTOSAVE_DIR
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:111
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
GetDetailedFileType
DetailedFileType GetDetailedFileType(FiosType fios_type)
Extract the detailed file type from a FiosType.
Definition: fileio_type.h:100
SlNullPointers
static void SlNullPointers()
Null all pointers (convert index -> nullptr)
Definition: saveload.cpp:299
_savegame_format
char _savegame_format[8]
how to compress savegames
Definition: saveload.cpp:67
SpecializedStation< Station, false >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
Definition: base_station_base.h:210
SLA_LOAD
@ SLA_LOAD
loading
Definition: saveload.cpp:72
SaveLoadParams::reader
ReadBuffer * reader
Savegame reading buffer.
Definition: saveload.cpp:205
ZlibLoadFilter
Filter using Zlib compression.
Definition: saveload.cpp:2072
LoadWithFilter
SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
Load the game using a (reader) filter.
Definition: saveload.cpp:2744
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:380
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
SaveLoad::conv
VarType conv
type of the variable to be saved, int
Definition: saveload.h:519
LZMASaveFilter::WriteLoop
void WriteLoop(byte *p, size_t len, lzma_action action)
Helper loop for writing the data.
Definition: saveload.cpp:2269
SLF_NO_NETWORK_SYNC
@ SLF_NO_NETWORK_SYNC
do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
Definition: saveload.h:489
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
SlCalcConvFileLen
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:605
SaveLoadFormat::default_compression
byte default_compression
the default compression level of this format
Definition: saveload.cpp:2319
SaveLoadAction
SaveLoadAction
What are we currently doing?
Definition: saveload.cpp:71
LoadFilter::Reset
virtual void Reset()
Reset this filter to read from the beginning of the file.
Definition: saveload_filter.h:43
SaveLoadParams::sf
SaveFilter * sf
Filter to write the savegame to.
Definition: saveload.cpp:203
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
SlCalcStdStringLen
static size_t SlCalcStdStringLen(const void *ptr)
Calculate the gross length of the string that it will occupy in the savegame.
Definition: saveload.cpp:901
NL_NONE
@ NL_NONE
not working in NeedLength mode
Definition: saveload.cpp:80
AsyncSaveFinishProc
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition: saveload.cpp:383
SlSetLength
void SlSetLength(size_t length)
Sets the length of either a RIFF object or the number of items in an array.
Definition: saveload.cpp:676
NoCompLoadFilter::Read
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2043
ZlibSaveFilter::z
z_stream z
Stream state we are writing to.
Definition: saveload.cpp:2117
SlReadSimpleGamma
static uint SlReadSimpleGamma()
Read in the header descriptor of an object or an array.
Definition: saveload.cpp:478
ShowErrorMessage
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:372
saveload_filter.h
ReadBuffer::reader
LoadFilter * reader
The filter used to actually read.
Definition: saveload.cpp:93
MemoryDumper::buf
byte * buf
Buffer we're going to write to.
Definition: saveload.cpp:132
NoCompSaveFilter
Filter without any compression.
Definition: saveload.cpp:2048
GetSavegameFormat
static const SaveLoadFormat * GetSavegameFormat(char *s, byte *compression_level)
Return the savegameformat of the game.
Definition: saveload.cpp:2358
REF_STATION
@ REF_STATION
Load/save a reference to a station.
Definition: saveload.h:393
LZMALoadFilter
Filter without any compression.
Definition: saveload.cpp:2201
SLE_VAR_STRB
@ SLE_VAR_STRB
string (with pre-allocated buffer)
Definition: saveload.h:447
SlCalcConvMemLen
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:581
AbstractFileType
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:16
LoadFilter::Read
virtual size_t Read(byte *buf, size_t len)=0
Read a given number of bytes from the savegame.
ReferenceToInt
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,...
Definition: saveload.cpp:1106
SaveLoadFormat
The format for a reader/writer type of a savegame.
Definition: saveload.cpp:2309
FileToSaveLoad::abstract_ftype
AbstractFileType abstract_ftype
Abstract type of file (scenario, heightmap, etc).
Definition: saveload.h:343
ReadBuffer::buf
byte buf[MEMORY_CHUNK_SIZE]
Buffer we're going to read from.
Definition: saveload.cpp:90
SaveLoadParams::block_mode
byte block_mode
???
Definition: saveload.cpp:196
FileWriter::file
FILE * file
The file to write to.
Definition: saveload.cpp:1899
SVS_ALLOW_CONTROL_CODE
@ SVS_ALLOW_CONTROL_CODE
Allow the special control codes.
Definition: string_type.h:52
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition: fileio_type.h:109
SLV_5
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:43
GLAT_LOAD
@ GLAT_LOAD
Game loaded.
Definition: gamelog.h:18
SaveFileDone
static void SaveFileDone()
Update the gui accordingly when saving is done and release locks on saveload.
Definition: saveload.cpp:2454
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
allow control codes in the strings
Definition: saveload.h:490
MemoryDumper::WriteByte
void WriteByte(byte b)
Write a single byte into the dumper.
Definition: saveload.cpp:151
SLO_LOAD
@ SLO_LOAD
File is being loaded.
Definition: fileio_type.h:49
SaveFileError
static void SaveFileError()
Show a gui message when saving has failed.
Definition: saveload.cpp:2485
IsGoodGRFConfigList
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
Definition: newgrf_config.cpp:515
_sl_version
SaveLoadVersion _sl_version
the major savegame version identifier
Definition: saveload.cpp:65
SlFixPointers
static void SlFixPointers()
Fix all pointers (convert index -> pointer)
Definition: saveload.cpp:1842
SavegameType
SavegameType
Types of save games.
Definition: saveload.h:354
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
SAVEGAME_VERSION
const SaveLoadVersion SAVEGAME_VERSION
Current savegame version of OpenTTD.
ZlibSaveFilter
Filter using Zlib compression.
Definition: saveload.cpp:2116
SLO_SAVE
@ SLO_SAVE
File is being saved.
Definition: fileio_type.h:50
SBI_SAVELOAD_FINISH
@ SBI_SAVELOAD_FINISH
finished saving
Definition: statusbar_gui.h:16
ChunkHandler::load_check_proc
ChunkSaveLoadProc * load_check_proc
Load procedure for game preview.
Definition: saveload.h:385
IntToReference
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:1139
NeedLength
NeedLength
Definition: saveload.cpp:79
NL_CALCLENGTH
@ NL_CALCLENGTH
need to calculate the length
Definition: saveload.cpp:82
SaveFilter::Finish
virtual void Finish()
Prepare everything to finish writing the savegame.
Definition: saveload_filter.h:88
SlCalcDequeLen
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Return the size in bytes of a std::deque.
Definition: saveload.cpp:1352
SlWriteByte
void SlWriteByte(byte b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:427
ChunkHandler::id
uint32 id
Unique ID (4 letters).
Definition: saveload.h:381
SL_VAR
@ SL_VAR
Save/load a variable.
Definition: saveload.h:500
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:406
LoadCheckData::error_data
char * error_data
Data to pass to SetDParamStr when displaying error.
Definition: fios.h:34
LZMASaveFilter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2290
NL_WANTLENGTH
@ NL_WANTLENGTH
writing length and data
Definition: saveload.cpp:81
GetSaveLoadErrorString
const char * GetSaveLoadErrorString()
Get the string representation of the error message.
Definition: saveload.cpp:2474
ZlibLoadFilter::Read
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2094
_cheat_chunk_handlers
const ChunkHandler _cheat_chunk_handlers[]
Chunk handlers related to cheats.
SaveFilter::Write
virtual void Write(byte *buf, size_t len)=0
Write a given number of bytes into the savegame.
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:221
_savegame_type
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:61
SlLoadCheckChunks
static void SlLoadCheckChunks()
Load all chunks for savegame checking.
Definition: saveload.cpp:1827
ZlibSaveFilter::ZlibSaveFilter
ZlibSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2124
CopyFromOldName
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:60
_persistent_storage_chunk_handlers
const ChunkHandler _persistent_storage_chunk_handlers[]
Chunk handler for persistent storages.
FileReader::file
FILE * file
The file to read from.
Definition: saveload.cpp:1859
SL_REINIT
@ SL_REINIT
error that was caught in the middle of updating game state, need to clear it. (can only happen during...
Definition: saveload.h:336
SaveLoad::cmd
SaveLoadType cmd
the action to take with the saved/loaded type, All types need different action
Definition: saveload.h:518
SLV_END_PATCHPACKS
@ SLV_END_PATCHPACKS
286 Last known patchpack to use a version just above ours.
Definition: saveload.h:322
SLRefType
SLRefType
Type of reference (SLE_REF, SLE_CONDREF).
Definition: saveload.h:390
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:45
SlSaveLoadConv
static void SlSaveLoadConv(void *ptr, VarType conv)
Handle all conversion and typechecking of variables here.
Definition: saveload.cpp:799
ZlibSaveFilter::WriteLoop
void WriteLoop(byte *p, size_t len, int mode)
Helper loop for writing the data.
Definition: saveload.cpp:2142
FileWriter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:1918
GamelogStartAction
void GamelogStartAction(GamelogActionType at)
Stores information about new action, but doesn't allocate it Action is allocated only when there is a...
Definition: gamelog.cpp:69
SaveLoadParams::lf
LoadFilter * lf
Filter to read the savegame from.
Definition: saveload.cpp:206
NoCompSaveFilter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2058
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
SaveFileToDisk
static SaveOrLoadResult SaveFileToDisk(bool threaded)
We have written the whole game into memory, _memory_savegame, now find and appropriate compressor and...
Definition: saveload.cpp:2496
SaveLoadFormat::name
const char * name
name of the compressor/decompressor (debug-only)
Definition: saveload.cpp:2312
ChunkHandler::flags
uint32 flags
Flags of the chunk.
Definition: saveload.h:386
SL_ARR
@ SL_ARR
Save/load an array.
Definition: saveload.h:502
SLE_FILE_STRINGID
@ SLE_FILE_STRINGID
StringID offset into strings-array.
Definition: saveload.h:432
REF_STORAGE
@ REF_STORAGE
Load/save a reference to a persistent storage.
Definition: saveload.h:400
SCENARIO_DIR
@ SCENARIO_DIR
Base directory for all scenarios.
Definition: fileio_type.h:112
FT_INVALID
@ FT_INVALID
Invalid or unknown file type.
Definition: fileio_type.h:22
SaveLoadVersion
SaveLoadVersion
SaveLoad versions Previous savegame versions, the trunk revision where they were introduced and the r...
Definition: saveload.h:30
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
SLA_PTRS
@ SLA_PTRS
fixing pointers
Definition: saveload.cpp:74
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:816
MemoryDumper::Flush
void Flush(SaveFilter *writer)
Flush this dumper into a writer.
Definition: saveload.cpp:167
_sign_chunk_handlers
const ChunkHandler _sign_chunk_handlers[]
Chunk handlers related to signs.
SlList
static void SlList(void *list, SLRefType conv)
Save/Load a list.
Definition: saveload.cpp:1230
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
SaveFileStart
static void SaveFileStart()
Update the gui accordingly when starting saving and set locks on saveload.
Definition: saveload.cpp:2443
REF_ENGINE_RENEWS
@ REF_ENGINE_RENEWS
Load/save a reference to an engine renewal (autoreplace).
Definition: saveload.h:397
DFT_OLD_GAME_FILE
@ DFT_OLD_GAME_FILE
Old save game or scenario file.
Definition: fileio_type.h:30
vseprintf
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:61
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:392
SL_REF
@ SL_REF
Save/load a reference.
Definition: saveload.h:501
REF_CARGO_PACKET
@ REF_CARGO_PACKET
Load/save a reference to a cargo packet.
Definition: saveload.h:398
_chunk_handlers
static const ChunkHandler *const _chunk_handlers[]
Array of all chunks in a savegame, nullptr terminated.
Definition: saveload.cpp:253
DoSave
static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
Actually perform the saving of the savegame.
Definition: saveload.cpp:2554
SBI_SAVELOAD_START
@ SBI_SAVELOAD_START
started saving
Definition: statusbar_gui.h:15
LZMALoadFilter::lzma
lzma_stream lzma
Stream state that we are reading from.
Definition: saveload.cpp:2202
ReadBuffer::bufp
byte * bufp
Location we're at reading the buffer.
Definition: saveload.cpp:91
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1640
EngineRenew
Struct to store engine replacements.
Definition: autoreplace_base.h:33
REF_VEHICLE_OLD
@ REF_VEHICLE_OLD
Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
Definition: saveload.h:395
ChunkHandler::load_proc
ChunkSaveLoadProc * load_proc
Load procedure of the chunk.
Definition: saveload.h:383
SlCalcListLen
static size_t SlCalcListLen(const void *list)
Return the size in bytes of a list.
Definition: saveload.cpp:1214
SaveLoadParams::error_str
StringID error_str
the translatable error message to show
Definition: saveload.cpp:208
ZlibSaveFilter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2171
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:380
SlString
static void SlString(void *ptr, size_t length, VarType conv)
Save/Load a string.
Definition: saveload.cpp:915
SlDequeHelper::SlCalcDequeLen
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:1295
GamelogStopAction
void GamelogStopAction()
Stops logging of any changes.
Definition: gamelog.cpp:78
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
FOR_ALL_CHUNK_HANDLERS
#define FOR_ALL_CHUNK_HANDLERS(ch)
Iterate over all chunk handlers.
Definition: saveload.cpp:294
LoadCheckData::error
StringID error
Error message from loading. INVALID_STRING_ID if no error.
Definition: fios.h:33
SlGlobList
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1630
_animated_tile_chunk_handlers
const ChunkHandler _animated_tile_chunk_handlers[]
"Definition" imported by the saveload code to be able to load and save the animated tile table.
GenerateDefaultSaveName
void GenerateDefaultSaveName(char *buf, const char *last)
Fill the buffer with the default name for a savegame or screenshot.
Definition: saveload.cpp:2861
WriteValue
void WriteValue(void *ptr, VarType conv, int64 val)
Write the value of a setting.
Definition: saveload.cpp:773
MemoryDumper::bufe
byte * bufe
End of the buffer we write to.
Definition: saveload.cpp:133
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:329
SlError
void NORETURN SlError(StringID string, const char *extra_msg)
Error handler.
Definition: saveload.cpp:326
SlCalcObjLength
size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
Calculate the size of an object.
Definition: saveload.cpp:1436
REF_LINK_GRAPH_JOB
@ REF_LINK_GRAPH_JOB
Load/save a reference to a link graph job.
Definition: saveload.h:402
SlCalcNetStringLen
static size_t SlCalcNetStringLen(const char *ptr, size_t length)
Calculate the net length of a string.
Definition: saveload.cpp:856
ZlibLoadFilter::z
z_stream z
Stream state we are reading from.
Definition: saveload.cpp:2075
LZMALoadFilter::~LZMALoadFilter
~LZMALoadFilter()
Clean everything up.
Definition: saveload.cpp:2216
ReadBuffer::ReadBuffer
ReadBuffer(LoadFilter *reader)
Initialise our variables.
Definition: saveload.cpp:100
DFT_INVALID
@ DFT_INVALID
Unknown or invalid file.
Definition: fileio_type.h:43
_sl
static SaveLoadParams _sl
Parameters used for/at saveload.
Definition: saveload.cpp:215
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
SaveLoadParams::need_length
NeedLength need_length
working in NeedLength (Autolength) mode?
Definition: saveload.cpp:195
SlErrorCorruptFmt
void NORETURN SlErrorCorruptFmt(const char *format,...)
Issue an SlErrorCorrupt with a format string.
Definition: saveload.cpp:370
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
Pool::PoolItem<&_company_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
SlSkipVariableOnLoad
static bool SlSkipVariableOnLoad(const SaveLoad *sld)
Are we going to load this variable when loading a savegame or not?
Definition: saveload.cpp:1420
GUISettings::threaded_saves
bool threaded_saves
should we do threaded saves?
Definition: settings_type.h:122
FT_NONE
@ FT_NONE
nothing to do
Definition: fileio_type.h:17
REF_ORDERLIST
@ REF_ORDERLIST
Load/save a reference to an orderlist.
Definition: saveload.h:399
SaveLoadParams
The saveload struct, containing reader-writer functions, buffer, version, etc.
Definition: saveload.cpp:193
LZMALoadFilter::Read
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2221
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:737
SlCalcArrayLen
static size_t SlCalcArrayLen(size_t length, VarType conv)
Return the size in bytes of a certain type of atomic array.
Definition: saveload.cpp:1040
DetailedFileType
DetailedFileType
Kinds of files in each AbstractFileType.
Definition: fileio_type.h:28
LoadCheckData::Clear
void Clear()
Reset read data.
Definition: fios_gui.cpp:47
SlDequeHelper::SlDeque
static void SlDeque(void *deque, VarType conv)
Internal templated helper to save/load a std::deque.
Definition: saveload.cpp:1310
DoLoad
static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
Actually perform the loading of a "non-old" savegame.
Definition: saveload.cpp:2603
_saveload_formats
static const SaveLoadFormat _saveload_formats[]
The different saveload formats known/understood by OpenTTD.
Definition: saveload.cpp:2322
ChunkHandler::save_proc
ChunkSaveLoadProc * save_proc
Save procedure of the chunk.
Definition: saveload.h:382
FileReader::Reset
void Reset() override
Reset this filter to read from the beginning of the file.
Definition: saveload.cpp:1888
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:358
SaveFilter
Interface for filtering a savegame till it is written.
Definition: saveload_filter.h:60
SetAsyncSaveFinish
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
Called by save thread to tell we finished saving.
Definition: saveload.cpp:391
GetVarMemType
static VarType GetVarMemType(VarType type)
Get the NumberType of a setting.
Definition: saveload.h:857
SaveLoadParams::action
SaveLoadAction action
are we doing a save or a load atm.
Definition: saveload.cpp:194
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:377
_file_to_saveload
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:62
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:250
SanitizeFilename
void SanitizeFilename(char *filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1240
SaveWithFilter
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
Save the game using a (writer) filter.
Definition: saveload.cpp:2586
SlGetGammaLength
static uint SlGetGammaLength(size_t i)
Return how many bytes used to encode a gamma value.
Definition: saveload.cpp:545
SlReadByte
byte SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:418
SL_LST
@ SL_LST
Save/load a list.
Definition: saveload.h:504
ZlibSaveFilter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2176
LZOSaveFilter
Filter using LZO compression.
Definition: saveload.cpp:1991
SaveLoadParams::obj_len
size_t obj_len
the length of the current object we are busy with
Definition: saveload.cpp:199
LZMASaveFilter::~LZMASaveFilter
~LZMASaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2258
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:460
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
SlWriteSimpleGamma
static void SlWriteSimpleGamma(size_t i)
Write the header descriptor of an object or an array.
Definition: saveload.cpp:520
SlIsObjectValidInSavegame
static bool SlIsObjectValidInSavegame(const SaveLoad *sld)
Are we going to save this object or not?
Definition: saveload.cpp:1407
LoadCheckData::grf_compatibility
GRFListCompatibility grf_compatibility
Summary state of NewGrfs, whether missing files or only compatible found.
Definition: fios.h:44
SaveOrLoadResult
SaveOrLoadResult
Save or load result codes.
Definition: saveload.h:333
GamelogReset
void GamelogReset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition: gamelog.cpp:115
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:24
FiosType
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:67
SLE_VAR_STRQ
@ SLE_VAR_STRQ
string pointer enclosed in quotes
Definition: saveload.h:450
SaveLoadParams::saveinprogress
bool saveinprogress
Whether there is currently a save in progress.
Definition: saveload.cpp:212
SaveFilter::chain
SaveFilter * chain
Chained to the (savegame) filters.
Definition: saveload_filter.h:62
SaveLoadParams::game_speed
uint16 game_speed
The game speed when saving started.
Definition: saveload.cpp:211
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
_cargomonitor_chunk_handlers
const ChunkHandler _cargomonitor_chunk_handlers[]
Chunk definition of the cargomonitoring maps.
ReadBuffer::read
size_t read
The amount of read bytes so far from the filter.
Definition: saveload.cpp:94
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:171
REF_LINK_GRAPH
@ REF_LINK_GRAPH
Load/save a reference to a link graph.
Definition: saveload.h:401
_cargopacket_chunk_handlers
const ChunkHandler _cargopacket_chunk_handlers[]
Chunk handlers related to cargo packets.
saveload_internal.h
SLO_INVALID
@ SLO_INVALID
Unknown file operation.
Definition: fileio_type.h:52
SLA_NULL
@ SLA_NULL
null all pointers (on loading error)
Definition: saveload.cpp:75
SlStdString
static void SlStdString(void *ptr, VarType conv)
Save/Load a std::string.
Definition: saveload.cpp:993
SaveLoadFormat::tag
uint32 tag
the 4-letter tag by which it is identified in the savegame
Definition: saveload.cpp:2313
LoadFilter
Interface for filtering a savegame till it is loaded.
Definition: saveload_filter.h:14
Town
Town data structure.
Definition: town.h:50
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:369
SaveLoadParams::error
bool error
did an error occur or not
Definition: saveload.cpp:197
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:42
SL_DEQUE
@ SL_DEQUE
Save/load a deque.
Definition: saveload.h:505
SaveLoadParams::dumper
MemoryDumper * dumper
Memory dumper to write the savegame to.
Definition: saveload.cpp:202
GetVehicleDescription
const SaveLoad * GetVehicleDescription(VehicleType vt)
Make it possible to make the saveload tables "friends" of other classes.
Definition: vehicle_sl.cpp:581
FileReader::~FileReader
~FileReader()
Make sure everything is cleaned up.
Definition: saveload.cpp:1871
GetAbstractFileType
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition: fileio_type.h:90
SlCopyBytes
static void SlCopyBytes(void *ptr, size_t length)
Save/Load bytes.
Definition: saveload.cpp:720
LZOLoadFilter
Filter using LZO compression.
Definition: saveload.cpp:1944
MEMORY_CHUNK_SIZE
static const size_t MEMORY_CHUNK_SIZE
Save in chunks of 128 KiB.
Definition: saveload.cpp:86
ProcessAsyncSaveFinish
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition: saveload.cpp:402
MemoryDumper::blocks
std::vector< byte * > blocks
Buffer with blocks of allocated memory.
Definition: saveload.cpp:131
SaveLoadParams::extra_msg
char * extra_msg
the error message
Definition: saveload.cpp:209
FileReader
Yes, simply reading from a file.
Definition: saveload.cpp:1858
SlFindChunkHandler
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:1805
_ttdp_version
uint32 _ttdp_version
version of TTDP savegame (if applicable)
Definition: saveload.cpp:64
ReadValue
int64 ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:749
SaveLoadParams::last_array_index
int last_array_index
in the case of an array, the current and last positions
Definition: saveload.cpp:200
SlCalcRefLen
static size_t SlCalcRefLen()
Return the size in bytes of a reference (pointer)
Definition: saveload.cpp:614
LZMASaveFilter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2295
_async_save_finish
static std::atomic< AsyncSaveFinishProc > _async_save_finish
Callback to call when the savegame loading is finished.
Definition: saveload.cpp:384
SL_ERROR
@ SL_ERROR
error that was caught before internal structures were modified
Definition: saveload.h:335
FileToSaveLoad::detail_ftype
DetailedFileType detail_ftype
Concrete file type (PNG, BMP, old save, etc).
Definition: saveload.h:342
Pool::PoolItem<&_orderlist_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:318
WC_STATUS_BAR
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:57
ReadBuffer::GetSize
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:122
RoadStop
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
SlSaveChunk
static void SlSaveChunk(const ChunkHandler *ch)
Save a chunk of data (eg.
Definition: saveload.cpp:1757
ZlibSaveFilter::~ZlibSaveFilter
~ZlibSaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2131
LZMASaveFilter
Filter using LZMA compression.
Definition: saveload.cpp:2244
SLV_START_PATCHPACKS
@ SLV_START_PATCHPACKS
220 First known patchpack to use a version just above ours.
Definition: saveload.h:321
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:456
SlCalcStringLen
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:871
SLV_4
@ SLV_4
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:37
FileToSaveLoad::SetMode
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:2894
SaveLoad
SaveLoad type struct.
Definition: saveload.h:517
Company
Definition: company_base.h:110
LZOSaveFilter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2002
ClearSaveLoadState
static void ClearSaveLoadState()
Clear/free saveload state.
Definition: saveload.cpp:2423
SlArray
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:1051
LoadFilter::chain
LoadFilter * chain
Chained to the (savegame) filters.
Definition: saveload_filter.h:16
GetBaseStationDescription
const SaveLoad * GetBaseStationDescription()
Get the base station description to be used for SL_ST_INCLUDE.
Definition: station_sl.cpp:464
GetVariableAddress
static void * GetVariableAddress(const void *object, const SaveLoad *sld)
Get the address of the variable.
Definition: saveload.h:888
LoadFilter::LoadFilter
LoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload_filter.h:22
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:385
SlDequeHelper
Template class to help with std::deque.
Definition: saveload.cpp:1287
SL_OK
@ SL_OK
completed successfully
Definition: saveload.h:334
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
SL_STDSTR
@ SL_STDSTR
Save/load a std::string.
Definition: saveload.h:506
Order
Definition: order_base.h:32
SlSkipBytes
static void SlSkipBytes(size_t length)
Read in bytes from the file/data structure but don't do anything with them, discarding them in effect...
Definition: saveload.h:931
_lzma_init
static const lzma_stream _lzma_init
Have a copy of an initialised LZMA stream.
Definition: saveload.cpp:2198
SLE_VAR_NAME
@ SLE_VAR_NAME
old custom name to be converted to a char pointer
Definition: saveload.h:451
FileWriter::~FileWriter
~FileWriter()
Make sure everything is cleaned up.
Definition: saveload.cpp:1910
_game_speed
uint16 _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition: gfx.cpp:37
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:286
FileReader::FileReader
FileReader(FILE *file)
Create the file reader, so it reads from a specific file.
Definition: saveload.cpp:1866
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:581
WL_CRITICAL
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:25
FileWriter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:1926
FileToSaveLoad::file_op
SaveLoadOperation file_op
File operation to perform.
Definition: saveload.h:341
FileWriter
Yes, simply writing to a file.
Definition: saveload.cpp:1898
ReadBuffer::bufe
byte * bufe
End of the buffer we can read from.
Definition: saveload.cpp:92
MemoryDumper
Container for dumping the savegame (quickly) to memory.
Definition: saveload.cpp:130
IsVariableSizeRight
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:1488
AllocaM
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
Definition: alloc_func.hpp:132