OpenTTD Source  12.0-beta2
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 
23 #include "../stdafx.h"
24 #include "../debug.h"
25 #include "../station_base.h"
26 #include "../thread.h"
27 #include "../town.h"
28 #include "../network/network.h"
29 #include "../window_func.h"
30 #include "../strings_func.h"
31 #include "../core/endian_func.hpp"
32 #include "../vehicle_base.h"
33 #include "../company_func.h"
34 #include "../date_func.h"
35 #include "../autoreplace_base.h"
36 #include "../roadstop_base.h"
37 #include "../linkgraph/linkgraph.h"
38 #include "../linkgraph/linkgraphjob.h"
39 #include "../statusbar_gui.h"
40 #include "../fileio_func.h"
41 #include "../gamelog.h"
42 #include "../string_func.h"
43 #include "../fios.h"
44 #include "../error.h"
45 #include <atomic>
46 #include <deque>
47 #include <vector>
48 #include <string>
49 #ifdef __EMSCRIPTEN__
50 # include <emscripten.h>
51 #endif
52 
53 #include "table/strings.h"
54 
55 #include "saveload_internal.h"
56 #include "saveload_filter.h"
57 
58 #include "../safeguards.h"
59 
61 
64 
65 uint32 _ttdp_version;
68 std::string _savegame_format;
70 
78 };
79 
80 enum NeedLength {
81  NL_NONE = 0,
84 };
85 
87 static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
88 
90 struct ReadBuffer {
92  byte *bufp;
93  byte *bufe;
95  size_t read;
96 
101  ReadBuffer(LoadFilter *reader) : bufp(nullptr), bufe(nullptr), reader(reader), read(0)
102  {
103  }
104 
105  inline byte ReadByte()
106  {
107  if (this->bufp == this->bufe) {
108  size_t len = this->reader->Read(this->buf, lengthof(this->buf));
109  if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
110 
111  this->read += len;
112  this->bufp = this->buf;
113  this->bufe = this->buf + len;
114  }
115 
116  return *this->bufp++;
117  }
118 
123  size_t GetSize() const
124  {
125  return this->read - (this->bufe - this->bufp);
126  }
127 };
128 
129 
131 struct MemoryDumper {
132  std::vector<byte *> blocks;
133  byte *buf;
134  byte *bufe;
135 
137  MemoryDumper() : buf(nullptr), bufe(nullptr)
138  {
139  }
140 
141  ~MemoryDumper()
142  {
143  for (auto p : this->blocks) {
144  free(p);
145  }
146  }
147 
152  inline void WriteByte(byte b)
153  {
154  /* Are we at the end of this chunk? */
155  if (this->buf == this->bufe) {
156  this->buf = CallocT<byte>(MEMORY_CHUNK_SIZE);
157  this->blocks.push_back(this->buf);
158  this->bufe = this->buf + MEMORY_CHUNK_SIZE;
159  }
160 
161  *this->buf++ = b;
162  }
163 
168  void Flush(SaveFilter *writer)
169  {
170  uint i = 0;
171  size_t t = this->GetSize();
172 
173  while (t > 0) {
174  size_t to_write = std::min(MEMORY_CHUNK_SIZE, t);
175 
176  writer->Write(this->blocks[i++], to_write);
177  t -= to_write;
178  }
179 
180  writer->Finish();
181  }
182 
187  size_t GetSize() const
188  {
189  return this->blocks.size() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
190  }
191 };
192 
197  byte block_mode;
198  bool error;
199 
200  size_t obj_len;
201  int array_index, last_array_index;
203 
206 
209 
211  char *extra_msg;
212 
213  uint16 game_speed;
215 };
216 
218 
219 static const std::vector<ChunkHandlerRef> &ChunkHandlers()
220 {
221  /* These define the chunks */
222  extern const ChunkHandlerTable _gamelog_chunk_handlers;
223  extern const ChunkHandlerTable _map_chunk_handlers;
224  extern const ChunkHandlerTable _misc_chunk_handlers;
225  extern const ChunkHandlerTable _name_chunk_handlers;
226  extern const ChunkHandlerTable _cheat_chunk_handlers;
227  extern const ChunkHandlerTable _setting_chunk_handlers;
228  extern const ChunkHandlerTable _company_chunk_handlers;
229  extern const ChunkHandlerTable _engine_chunk_handlers;
230  extern const ChunkHandlerTable _veh_chunk_handlers;
231  extern const ChunkHandlerTable _waypoint_chunk_handlers;
232  extern const ChunkHandlerTable _depot_chunk_handlers;
233  extern const ChunkHandlerTable _order_chunk_handlers;
234  extern const ChunkHandlerTable _town_chunk_handlers;
235  extern const ChunkHandlerTable _sign_chunk_handlers;
236  extern const ChunkHandlerTable _station_chunk_handlers;
237  extern const ChunkHandlerTable _industry_chunk_handlers;
238  extern const ChunkHandlerTable _economy_chunk_handlers;
239  extern const ChunkHandlerTable _subsidy_chunk_handlers;
240  extern const ChunkHandlerTable _cargomonitor_chunk_handlers;
241  extern const ChunkHandlerTable _goal_chunk_handlers;
242  extern const ChunkHandlerTable _story_page_chunk_handlers;
243  extern const ChunkHandlerTable _ai_chunk_handlers;
244  extern const ChunkHandlerTable _game_chunk_handlers;
245  extern const ChunkHandlerTable _animated_tile_chunk_handlers;
246  extern const ChunkHandlerTable _newgrf_chunk_handlers;
247  extern const ChunkHandlerTable _group_chunk_handlers;
248  extern const ChunkHandlerTable _cargopacket_chunk_handlers;
249  extern const ChunkHandlerTable _autoreplace_chunk_handlers;
250  extern const ChunkHandlerTable _labelmaps_chunk_handlers;
251  extern const ChunkHandlerTable _linkgraph_chunk_handlers;
252  extern const ChunkHandlerTable _airport_chunk_handlers;
253  extern const ChunkHandlerTable _object_chunk_handlers;
254  extern const ChunkHandlerTable _persistent_storage_chunk_handlers;
255 
257  static const ChunkHandlerTable _chunk_handler_tables[] = {
258  _gamelog_chunk_handlers,
259  _map_chunk_handlers,
260  _misc_chunk_handlers,
261  _name_chunk_handlers,
262  _cheat_chunk_handlers,
263  _setting_chunk_handlers,
264  _veh_chunk_handlers,
265  _waypoint_chunk_handlers,
266  _depot_chunk_handlers,
267  _order_chunk_handlers,
268  _industry_chunk_handlers,
269  _economy_chunk_handlers,
270  _subsidy_chunk_handlers,
271  _cargomonitor_chunk_handlers,
272  _goal_chunk_handlers,
273  _story_page_chunk_handlers,
274  _engine_chunk_handlers,
275  _town_chunk_handlers,
276  _sign_chunk_handlers,
277  _station_chunk_handlers,
278  _company_chunk_handlers,
279  _ai_chunk_handlers,
280  _game_chunk_handlers,
281  _animated_tile_chunk_handlers,
282  _newgrf_chunk_handlers,
283  _group_chunk_handlers,
284  _cargopacket_chunk_handlers,
285  _autoreplace_chunk_handlers,
286  _labelmaps_chunk_handlers,
287  _linkgraph_chunk_handlers,
288  _airport_chunk_handlers,
289  _object_chunk_handlers,
290  _persistent_storage_chunk_handlers,
291  };
292 
293  static std::vector<ChunkHandlerRef> _chunk_handlers;
294 
295  if (_chunk_handlers.empty()) {
296  for (auto &chunk_handler_table : _chunk_handler_tables) {
297  for (auto &chunk_handler : chunk_handler_table) {
298  _chunk_handlers.push_back(chunk_handler);
299  }
300  }
301  }
302 
303  return _chunk_handlers;
304 }
305 
307 static void SlNullPointers()
308 {
309  _sl.action = SLA_NULL;
310 
311  /* We don't want any savegame conversion code to run
312  * during NULLing; especially those that try to get
313  * pointers from other pools. */
315 
316  for (const ChunkHandler &ch : ChunkHandlers()) {
317  Debug(sl, 3, "Nulling pointers for {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id);
318  ch.FixPointers();
319  }
320 
321  assert(_sl.action == SLA_NULL);
322 }
323 
332 void NORETURN SlError(StringID string, const char *extra_msg)
333 {
334  /* Distinguish between loading into _load_check_data vs. normal save/load. */
335  if (_sl.action == SLA_LOAD_CHECK) {
336  _load_check_data.error = string;
338  _load_check_data.error_data = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
339  } else {
340  _sl.error_str = string;
341  free(_sl.extra_msg);
342  _sl.extra_msg = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
343  }
344 
345  /* We have to nullptr all pointers here; we might be in a state where
346  * the pointers are actually filled with indices, which means that
347  * when we access them during cleaning the pool dereferences of
348  * those indices will be made with segmentation faults as result. */
350 
351  /* Logging could be active. */
352  GamelogStopAnyAction();
353 
354  throw std::exception();
355 }
356 
364 void NORETURN SlErrorCorrupt(const char *msg)
365 {
366  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
367 }
368 
376 void NORETURN SlErrorCorruptFmt(const char *format, ...)
377 {
378  va_list ap;
379  char msg[256];
380 
381  va_start(ap, format);
382  vseprintf(msg, lastof(msg), format, ap);
383  va_end(ap);
384 
385  SlErrorCorrupt(msg);
386 }
387 
388 
389 typedef void (*AsyncSaveFinishProc)();
390 static std::atomic<AsyncSaveFinishProc> _async_save_finish;
391 static std::thread _save_thread;
392 
398 {
399  if (_exit_game) return;
400  while (_async_save_finish.load(std::memory_order_acquire) != nullptr) CSleep(10);
401 
402  _async_save_finish.store(proc, std::memory_order_release);
403 }
404 
409 {
410  AsyncSaveFinishProc proc = _async_save_finish.exchange(nullptr, std::memory_order_acq_rel);
411  if (proc == nullptr) return;
412 
413  proc();
414 
415  if (_save_thread.joinable()) {
416  _save_thread.join();
417  }
418 }
419 
425 {
426  return _sl.reader->ReadByte();
427 }
428 
433 void SlWriteByte(byte b)
434 {
435  _sl.dumper->WriteByte(b);
436 }
437 
438 static inline int SlReadUint16()
439 {
440  int x = SlReadByte() << 8;
441  return x | SlReadByte();
442 }
443 
444 static inline uint32 SlReadUint32()
445 {
446  uint32 x = SlReadUint16() << 16;
447  return x | SlReadUint16();
448 }
449 
450 static inline uint64 SlReadUint64()
451 {
452  uint32 x = SlReadUint32();
453  uint32 y = SlReadUint32();
454  return (uint64)x << 32 | y;
455 }
456 
457 static inline void SlWriteUint16(uint16 v)
458 {
459  SlWriteByte(GB(v, 8, 8));
460  SlWriteByte(GB(v, 0, 8));
461 }
462 
463 static inline void SlWriteUint32(uint32 v)
464 {
465  SlWriteUint16(GB(v, 16, 16));
466  SlWriteUint16(GB(v, 0, 16));
467 }
468 
469 static inline void SlWriteUint64(uint64 x)
470 {
471  SlWriteUint32((uint32)(x >> 32));
472  SlWriteUint32((uint32)x);
473 }
474 
484 static uint SlReadSimpleGamma()
485 {
486  uint i = SlReadByte();
487  if (HasBit(i, 7)) {
488  i &= ~0x80;
489  if (HasBit(i, 6)) {
490  i &= ~0x40;
491  if (HasBit(i, 5)) {
492  i &= ~0x20;
493  if (HasBit(i, 4)) {
494  i &= ~0x10;
495  if (HasBit(i, 3)) {
496  SlErrorCorrupt("Unsupported gamma");
497  }
498  i = SlReadByte(); // 32 bits only.
499  }
500  i = (i << 8) | SlReadByte();
501  }
502  i = (i << 8) | SlReadByte();
503  }
504  i = (i << 8) | SlReadByte();
505  }
506  return i;
507 }
508 
526 static void SlWriteSimpleGamma(size_t i)
527 {
528  if (i >= (1 << 7)) {
529  if (i >= (1 << 14)) {
530  if (i >= (1 << 21)) {
531  if (i >= (1 << 28)) {
532  assert(i <= UINT32_MAX); // We can only support 32 bits for now.
533  SlWriteByte((byte)(0xF0));
534  SlWriteByte((byte)(i >> 24));
535  } else {
536  SlWriteByte((byte)(0xE0 | (i >> 24)));
537  }
538  SlWriteByte((byte)(i >> 16));
539  } else {
540  SlWriteByte((byte)(0xC0 | (i >> 16)));
541  }
542  SlWriteByte((byte)(i >> 8));
543  } else {
544  SlWriteByte((byte)(0x80 | (i >> 8)));
545  }
546  }
547  SlWriteByte((byte)i);
548 }
549 
551 static inline uint SlGetGammaLength(size_t i)
552 {
553  return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
554 }
555 
556 static inline uint SlReadSparseIndex()
557 {
558  return SlReadSimpleGamma();
559 }
560 
561 static inline void SlWriteSparseIndex(uint index)
562 {
563  SlWriteSimpleGamma(index);
564 }
565 
566 static inline uint SlReadArrayLength()
567 {
568  return SlReadSimpleGamma();
569 }
570 
571 static inline void SlWriteArrayLength(size_t length)
572 {
573  SlWriteSimpleGamma(length);
574 }
575 
576 static inline uint SlGetArrayLength(size_t length)
577 {
578  return SlGetGammaLength(length);
579 }
580 
584 static uint8 GetSavegameFileType(const SaveLoad &sld)
585 {
586  switch (sld.cmd) {
587  case SL_VAR:
588  return GetVarFileType(sld.conv); break;
589 
590  case SL_STR:
591  case SL_STDSTR:
592  case SL_ARR:
593  case SL_VECTOR:
594  case SL_DEQUE:
595  return GetVarFileType(sld.conv) | SLE_FILE_HAS_LENGTH_FIELD; break;
596 
597  case SL_REF:
598  return IsSavegameVersionBefore(SLV_69) ? SLE_FILE_U16 : SLE_FILE_U32;
599 
600  case SL_REFLIST:
601  return (IsSavegameVersionBefore(SLV_69) ? SLE_FILE_U16 : SLE_FILE_U32) | SLE_FILE_HAS_LENGTH_FIELD;
602 
603  case SL_SAVEBYTE:
604  return SLE_FILE_U8;
605 
606  case SL_STRUCT:
607  case SL_STRUCTLIST:
608  return SLE_FILE_STRUCT | SLE_FILE_HAS_LENGTH_FIELD;
609 
610  default: NOT_REACHED();
611  }
612 }
613 
620 static inline uint SlCalcConvMemLen(VarType conv)
621 {
622  static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
623 
624  switch (GetVarMemType(conv)) {
625  case SLE_VAR_STRB:
626  case SLE_VAR_STR:
627  case SLE_VAR_STRQ:
628  return SlReadArrayLength();
629 
630  default:
631  uint8 type = GetVarMemType(conv) >> 4;
632  assert(type < lengthof(conv_mem_size));
633  return conv_mem_size[type];
634  }
635 }
636 
643 static inline byte SlCalcConvFileLen(VarType conv)
644 {
645  static const byte conv_file_size[] = {0, 1, 1, 2, 2, 4, 4, 8, 8, 2};
646 
647  uint8 type = GetVarFileType(conv);
648  assert(type < lengthof(conv_file_size));
649  return conv_file_size[type];
650 }
651 
653 static inline size_t SlCalcRefLen()
654 {
655  return IsSavegameVersionBefore(SLV_69) ? 2 : 4;
656 }
657 
658 void SlSetArrayIndex(uint index)
659 {
661  _sl.array_index = index;
662 }
663 
664 static size_t _next_offs;
665 
671 {
672  int index;
673 
674  /* After reading in the whole array inside the loop
675  * we must have read in all the data, so we must be at end of current block. */
676  if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) SlErrorCorrupt("Invalid chunk size");
677 
678  for (;;) {
679  uint length = SlReadArrayLength();
680  if (length == 0) {
681  assert(!_sl.expect_table_header);
682  _next_offs = 0;
683  return -1;
684  }
685 
686  _sl.obj_len = --length;
687  _next_offs = _sl.reader->GetSize() + length;
688 
689  if (_sl.expect_table_header) {
690  _sl.expect_table_header = false;
691  return INT32_MAX;
692  }
693 
694  switch (_sl.block_mode) {
695  case CH_SPARSE_TABLE:
696  case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
697  case CH_TABLE:
698  case CH_ARRAY: index = _sl.array_index++; break;
699  default:
700  Debug(sl, 0, "SlIterateArray error");
701  return -1; // error
702  }
703 
704  if (length != 0) return index;
705  }
706 }
707 
712 {
713  while (SlIterateArray() != -1) {
714  SlSkipBytes(_next_offs - _sl.reader->GetSize());
715  }
716 }
717 
723 void SlSetLength(size_t length)
724 {
725  assert(_sl.action == SLA_SAVE);
726 
727  switch (_sl.need_length) {
728  case NL_WANTLENGTH:
730  if ((_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE) && _sl.expect_table_header) {
731  _sl.expect_table_header = false;
732  SlWriteArrayLength(length + 1);
733  break;
734  }
735 
736  switch (_sl.block_mode) {
737  case CH_RIFF:
738  /* Ugly encoding of >16M RIFF chunks
739  * The lower 24 bits are normal
740  * The uppermost 4 bits are bits 24:27 */
741  assert(length < (1 << 28));
742  SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
743  break;
744  case CH_TABLE:
745  case CH_ARRAY:
746  assert(_sl.last_array_index <= _sl.array_index);
747  while (++_sl.last_array_index <= _sl.array_index) {
748  SlWriteArrayLength(1);
749  }
750  SlWriteArrayLength(length + 1);
751  break;
752  case CH_SPARSE_TABLE:
753  case CH_SPARSE_ARRAY:
754  SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
755  SlWriteSparseIndex(_sl.array_index);
756  break;
757  default: NOT_REACHED();
758  }
759  break;
760 
761  case NL_CALCLENGTH:
762  _sl.obj_len += (int)length;
763  break;
764 
765  default: NOT_REACHED();
766  }
767 }
768 
775 static void SlCopyBytes(void *ptr, size_t length)
776 {
777  byte *p = (byte *)ptr;
778 
779  switch (_sl.action) {
780  case SLA_LOAD_CHECK:
781  case SLA_LOAD:
782  for (; length != 0; length--) *p++ = SlReadByte();
783  break;
784  case SLA_SAVE:
785  for (; length != 0; length--) SlWriteByte(*p++);
786  break;
787  default: NOT_REACHED();
788  }
789 }
790 
793 {
794  return _sl.obj_len;
795 }
796 
804 int64 ReadValue(const void *ptr, VarType conv)
805 {
806  switch (GetVarMemType(conv)) {
807  case SLE_VAR_BL: return (*(const bool *)ptr != 0);
808  case SLE_VAR_I8: return *(const int8 *)ptr;
809  case SLE_VAR_U8: return *(const byte *)ptr;
810  case SLE_VAR_I16: return *(const int16 *)ptr;
811  case SLE_VAR_U16: return *(const uint16*)ptr;
812  case SLE_VAR_I32: return *(const int32 *)ptr;
813  case SLE_VAR_U32: return *(const uint32*)ptr;
814  case SLE_VAR_I64: return *(const int64 *)ptr;
815  case SLE_VAR_U64: return *(const uint64*)ptr;
816  case SLE_VAR_NULL:return 0;
817  default: NOT_REACHED();
818  }
819 }
820 
828 void WriteValue(void *ptr, VarType conv, int64 val)
829 {
830  switch (GetVarMemType(conv)) {
831  case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
832  case SLE_VAR_I8: *(int8 *)ptr = val; break;
833  case SLE_VAR_U8: *(byte *)ptr = val; break;
834  case SLE_VAR_I16: *(int16 *)ptr = val; break;
835  case SLE_VAR_U16: *(uint16*)ptr = val; break;
836  case SLE_VAR_I32: *(int32 *)ptr = val; break;
837  case SLE_VAR_U32: *(uint32*)ptr = val; break;
838  case SLE_VAR_I64: *(int64 *)ptr = val; break;
839  case SLE_VAR_U64: *(uint64*)ptr = val; break;
840  case SLE_VAR_NAME: *reinterpret_cast<std::string *>(ptr) = CopyFromOldName(val); break;
841  case SLE_VAR_NULL: break;
842  default: NOT_REACHED();
843  }
844 }
845 
854 static void SlSaveLoadConv(void *ptr, VarType conv)
855 {
856  switch (_sl.action) {
857  case SLA_SAVE: {
858  int64 x = ReadValue(ptr, conv);
859 
860  /* Write the value to the file and check if its value is in the desired range */
861  switch (GetVarFileType(conv)) {
862  case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
863  case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
864  case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
865  case SLE_FILE_STRINGID:
866  case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
867  case SLE_FILE_I32:
868  case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
869  case SLE_FILE_I64:
870  case SLE_FILE_U64: SlWriteUint64(x);break;
871  default: NOT_REACHED();
872  }
873  break;
874  }
875  case SLA_LOAD_CHECK:
876  case SLA_LOAD: {
877  int64 x;
878  /* Read a value from the file */
879  switch (GetVarFileType(conv)) {
880  case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
881  case SLE_FILE_U8: x = (byte )SlReadByte(); break;
882  case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
883  case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
884  case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
885  case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
886  case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
887  case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
888  case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
889  default: NOT_REACHED();
890  }
891 
892  /* Write The value to the struct. These ARE endian safe. */
893  WriteValue(ptr, conv, x);
894  break;
895  }
896  case SLA_PTRS: break;
897  case SLA_NULL: break;
898  default: NOT_REACHED();
899  }
900 }
901 
911 static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
912 {
913  if (ptr == nullptr) return 0;
914  return std::min(strlen(ptr), length - 1);
915 }
916 
926 static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
927 {
928  size_t len;
929  const char *str;
930 
931  switch (GetVarMemType(conv)) {
932  default: NOT_REACHED();
933  case SLE_VAR_STR:
934  case SLE_VAR_STRQ:
935  str = *(const char * const *)ptr;
936  len = SIZE_MAX;
937  break;
938  case SLE_VAR_STRB:
939  str = (const char *)ptr;
940  len = length;
941  break;
942  }
943 
944  len = SlCalcNetStringLen(str, len);
945  return len + SlGetArrayLength(len); // also include the length of the index
946 }
947 
955 static inline size_t SlCalcStdStringLen(const void *ptr)
956 {
957  const std::string *str = reinterpret_cast<const std::string *>(ptr);
958 
959  size_t len = str->length();
960  return len + SlGetArrayLength(len); // also include the length of the index
961 }
962 
969 static void SlString(void *ptr, size_t length, VarType conv)
970 {
971  switch (_sl.action) {
972  case SLA_SAVE: {
973  size_t len;
974  switch (GetVarMemType(conv)) {
975  default: NOT_REACHED();
976  case SLE_VAR_STRB:
977  len = SlCalcNetStringLen((char *)ptr, length);
978  break;
979  case SLE_VAR_STR:
980  case SLE_VAR_STRQ:
981  ptr = *(char **)ptr;
982  len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
983  break;
984  }
985 
986  SlWriteArrayLength(len);
987  SlCopyBytes(ptr, len);
988  break;
989  }
990  case SLA_LOAD_CHECK:
991  case SLA_LOAD: {
992  size_t len = SlReadArrayLength();
993 
994  switch (GetVarMemType(conv)) {
995  default: NOT_REACHED();
996  case SLE_VAR_NULL:
997  SlSkipBytes(len);
998  return;
999  case SLE_VAR_STRB:
1000  if (len >= length) {
1001  Debug(sl, 1, "String length in savegame is bigger than buffer, truncating");
1002  SlCopyBytes(ptr, length);
1003  SlSkipBytes(len - length);
1004  len = length - 1;
1005  } else {
1006  SlCopyBytes(ptr, len);
1007  }
1008  break;
1009  case SLE_VAR_STR:
1010  case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
1011  free(*(char **)ptr);
1012  if (len == 0) {
1013  *(char **)ptr = nullptr;
1014  return;
1015  } else {
1016  *(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
1017  ptr = *(char **)ptr;
1018  SlCopyBytes(ptr, len);
1019  }
1020  break;
1021  }
1022 
1023  ((char *)ptr)[len] = '\0'; // properly terminate the string
1025  if ((conv & SLF_ALLOW_CONTROL) != 0) {
1028  str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
1029  }
1030  }
1031  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
1033  }
1034  StrMakeValidInPlace((char *)ptr, (char *)ptr + len, settings);
1035  break;
1036  }
1037  case SLA_PTRS: break;
1038  case SLA_NULL: break;
1039  default: NOT_REACHED();
1040  }
1041 }
1042 
1048 static void SlStdString(void *ptr, VarType conv)
1049 {
1050  std::string *str = reinterpret_cast<std::string *>(ptr);
1051 
1052  switch (_sl.action) {
1053  case SLA_SAVE: {
1054  size_t len = str->length();
1055  SlWriteArrayLength(len);
1056  SlCopyBytes(const_cast<void *>(static_cast<const void *>(str->c_str())), len);
1057  break;
1058  }
1059 
1060  case SLA_LOAD_CHECK:
1061  case SLA_LOAD: {
1062  size_t len = SlReadArrayLength();
1063  if (GetVarMemType(conv) == SLE_VAR_NULL) {
1064  SlSkipBytes(len);
1065  return;
1066  }
1067 
1068  char *buf = AllocaM(char, len + 1);
1069  SlCopyBytes(buf, len);
1070  buf[len] = '\0'; // properly terminate the string
1071 
1073  if ((conv & SLF_ALLOW_CONTROL) != 0) {
1076  str_fix_scc_encoded(buf, buf + len);
1077  }
1078  }
1079  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
1081  }
1082  StrMakeValidInPlace(buf, buf + len, settings);
1083 
1084  // Store sanitized string.
1085  str->assign(buf);
1086  }
1087 
1088  case SLA_PTRS: break;
1089  case SLA_NULL: break;
1090  default: NOT_REACHED();
1091  }
1092 }
1093 
1102 static void SlCopyInternal(void *object, size_t length, VarType conv)
1103 {
1104  if (GetVarMemType(conv) == SLE_VAR_NULL) {
1105  assert(_sl.action != SLA_SAVE); // Use SL_NULL if you want to write null-bytes
1106  SlSkipBytes(length * SlCalcConvFileLen(conv));
1107  return;
1108  }
1109 
1110  /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1111  * as a byte-type. So detect this, and adjust object size accordingly */
1112  if (_sl.action != SLA_SAVE && _sl_version == 0) {
1113  /* all objects except difficulty settings */
1114  if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1115  conv == SLE_INT32 || conv == SLE_UINT32) {
1116  SlCopyBytes(object, length * SlCalcConvFileLen(conv));
1117  return;
1118  }
1119  /* used for conversion of Money 32bit->64bit */
1120  if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1121  for (uint i = 0; i < length; i++) {
1122  ((int64*)object)[i] = (int32)BSWAP32(SlReadUint32());
1123  }
1124  return;
1125  }
1126  }
1127 
1128  /* If the size of elements is 1 byte both in file and memory, no special
1129  * conversion is needed, use specialized copy-copy function to speed up things */
1130  if (conv == SLE_INT8 || conv == SLE_UINT8) {
1131  SlCopyBytes(object, length);
1132  } else {
1133  byte *a = (byte*)object;
1134  byte mem_size = SlCalcConvMemLen(conv);
1135 
1136  for (; length != 0; length --) {
1137  SlSaveLoadConv(a, conv);
1138  a += mem_size; // get size
1139  }
1140  }
1141 }
1142 
1151 void SlCopy(void *object, size_t length, VarType conv)
1152 {
1153  if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
1154 
1155  /* Automatically calculate the length? */
1156  if (_sl.need_length != NL_NONE) {
1157  SlSetLength(length * SlCalcConvFileLen(conv));
1158  /* Determine length only? */
1159  if (_sl.need_length == NL_CALCLENGTH) return;
1160  }
1161 
1162  SlCopyInternal(object, length, conv);
1163 }
1164 
1170 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
1171 {
1172  return SlCalcConvFileLen(conv) * length + SlGetArrayLength(length);
1173 }
1174 
1181 static void SlArray(void *array, size_t length, VarType conv)
1182 {
1183  switch (_sl.action) {
1184  case SLA_SAVE:
1185  SlWriteArrayLength(length);
1186  SlCopyInternal(array, length, conv);
1187  return;
1188 
1189  case SLA_LOAD_CHECK:
1190  case SLA_LOAD: {
1192  size_t sv_length = SlReadArrayLength();
1193  if (GetVarMemType(conv) == SLE_VAR_NULL) {
1194  /* We don't know this field, so we assume the length in the savegame is correct. */
1195  length = sv_length;
1196  } else if (sv_length != length) {
1197  /* If the SLE_ARR changes size, a savegame bump is required
1198  * and the developer should have written conversion lines.
1199  * Error out to make this more visible. */
1200  SlErrorCorrupt("Fixed-length array is of wrong length");
1201  }
1202  }
1203 
1204  SlCopyInternal(array, length, conv);
1205  return;
1206  }
1207 
1208  case SLA_PTRS:
1209  case SLA_NULL:
1210  return;
1211 
1212  default:
1213  NOT_REACHED();
1214  }
1215 }
1216 
1227 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1228 {
1229  assert(_sl.action == SLA_SAVE);
1230 
1231  if (obj == nullptr) return 0;
1232 
1233  switch (rt) {
1234  case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1235  case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1236  case REF_STATION: return ((const Station*)obj)->index + 1;
1237  case REF_TOWN: return ((const Town*)obj)->index + 1;
1238  case REF_ORDER: return ((const Order*)obj)->index + 1;
1239  case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1240  case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1241  case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1242  case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1243  case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1244  case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1245  case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1246  default: NOT_REACHED();
1247  }
1248 }
1249 
1260 static void *IntToReference(size_t index, SLRefType rt)
1261 {
1262  static_assert(sizeof(size_t) <= sizeof(void *));
1263 
1264  assert(_sl.action == SLA_PTRS);
1265 
1266  /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1267  * and should be loaded like that */
1268  if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) {
1269  rt = REF_VEHICLE;
1270  }
1271 
1272  /* No need to look up nullptr pointers, just return immediately */
1273  if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return nullptr;
1274 
1275  /* Correct index. Old vehicles were saved differently:
1276  * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1277  if (rt != REF_VEHICLE_OLD) index--;
1278 
1279  switch (rt) {
1280  case REF_ORDERLIST:
1281  if (OrderList::IsValidID(index)) return OrderList::Get(index);
1282  SlErrorCorrupt("Referencing invalid OrderList");
1283 
1284  case REF_ORDER:
1285  if (Order::IsValidID(index)) return Order::Get(index);
1286  /* in old versions, invalid order was used to mark end of order list */
1287  if (IsSavegameVersionBefore(SLV_5, 2)) return nullptr;
1288  SlErrorCorrupt("Referencing invalid Order");
1289 
1290  case REF_VEHICLE_OLD:
1291  case REF_VEHICLE:
1292  if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1293  SlErrorCorrupt("Referencing invalid Vehicle");
1294 
1295  case REF_STATION:
1296  if (Station::IsValidID(index)) return Station::Get(index);
1297  SlErrorCorrupt("Referencing invalid Station");
1298 
1299  case REF_TOWN:
1300  if (Town::IsValidID(index)) return Town::Get(index);
1301  SlErrorCorrupt("Referencing invalid Town");
1302 
1303  case REF_ROADSTOPS:
1304  if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1305  SlErrorCorrupt("Referencing invalid RoadStop");
1306 
1307  case REF_ENGINE_RENEWS:
1308  if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1309  SlErrorCorrupt("Referencing invalid EngineRenew");
1310 
1311  case REF_CARGO_PACKET:
1312  if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1313  SlErrorCorrupt("Referencing invalid CargoPacket");
1314 
1315  case REF_STORAGE:
1316  if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1317  SlErrorCorrupt("Referencing invalid PersistentStorage");
1318 
1319  case REF_LINK_GRAPH:
1320  if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1321  SlErrorCorrupt("Referencing invalid LinkGraph");
1322 
1323  case REF_LINK_GRAPH_JOB:
1324  if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1325  SlErrorCorrupt("Referencing invalid LinkGraphJob");
1326 
1327  default: NOT_REACHED();
1328  }
1329 }
1330 
1336 void SlSaveLoadRef(void *ptr, VarType conv)
1337 {
1338  switch (_sl.action) {
1339  case SLA_SAVE:
1340  SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1341  break;
1342  case SLA_LOAD_CHECK:
1343  case SLA_LOAD:
1344  *(size_t *)ptr = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1345  break;
1346  case SLA_PTRS:
1347  *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1348  break;
1349  case SLA_NULL:
1350  *(void **)ptr = nullptr;
1351  break;
1352  default: NOT_REACHED();
1353  }
1354 }
1355 
1359 template <template<typename, typename> typename Tstorage, typename Tvar, typename Tallocator = std::allocator<Tvar>>
1361  typedef Tstorage<Tvar, Tallocator> SlStorageT;
1362 public:
1369  static size_t SlCalcLen(const void *storage, VarType conv, SaveLoadType cmd = SL_VAR)
1370  {
1371  assert(cmd == SL_VAR || cmd == SL_REF);
1372 
1373  const SlStorageT *list = static_cast<const SlStorageT *>(storage);
1374 
1375  int type_size = SlGetArrayLength(list->size());
1376  int item_size = SlCalcConvFileLen(cmd == SL_VAR ? conv : (VarType)SLE_FILE_U32);
1377  return list->size() * item_size + type_size;
1378  }
1379 
1380  static void SlSaveLoadMember(SaveLoadType cmd, Tvar *item, VarType conv)
1381  {
1382  switch (cmd) {
1383  case SL_VAR: SlSaveLoadConv(item, conv); break;
1384  case SL_REF: SlSaveLoadRef(item, conv); break;
1385  default:
1386  NOT_REACHED();
1387  }
1388  }
1389 
1396  static void SlSaveLoad(void *storage, VarType conv, SaveLoadType cmd = SL_VAR)
1397  {
1398  assert(cmd == SL_VAR || cmd == SL_REF);
1399 
1400  SlStorageT *list = static_cast<SlStorageT *>(storage);
1401 
1402  switch (_sl.action) {
1403  case SLA_SAVE:
1404  SlWriteArrayLength(list->size());
1405 
1406  for (auto &item : *list) {
1407  SlSaveLoadMember(cmd, &item, conv);
1408  }
1409  break;
1410 
1411  case SLA_LOAD_CHECK:
1412  case SLA_LOAD: {
1413  size_t length;
1414  switch (cmd) {
1415  case SL_VAR: length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? SlReadUint32() : SlReadArrayLength(); break;
1416  case SL_REF: length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? SlReadUint32() : SlReadArrayLength(); break;
1417  default: NOT_REACHED();
1418  }
1419 
1420  /* Load each value and push to the end of the storage. */
1421  for (size_t i = 0; i < length; i++) {
1422  Tvar &data = list->emplace_back();
1423  SlSaveLoadMember(cmd, &data, conv);
1424  }
1425  break;
1426  }
1427 
1428  case SLA_PTRS:
1429  for (auto &item : *list) {
1430  SlSaveLoadMember(cmd, &item, conv);
1431  }
1432  break;
1433 
1434  case SLA_NULL:
1435  list->clear();
1436  break;
1437 
1438  default: NOT_REACHED();
1439  }
1440  }
1441 };
1442 
1448 static inline size_t SlCalcRefListLen(const void *list, VarType conv)
1449 {
1451 }
1452 
1458 static void SlRefList(void *list, VarType conv)
1459 {
1460  /* Automatically calculate the length? */
1461  if (_sl.need_length != NL_NONE) {
1462  SlSetLength(SlCalcRefListLen(list, conv));
1463  /* Determine length only? */
1464  if (_sl.need_length == NL_CALCLENGTH) return;
1465  }
1466 
1468 }
1469 
1475 static inline size_t SlCalcDequeLen(const void *deque, VarType conv)
1476 {
1477  switch (GetVarMemType(conv)) {
1478  case SLE_VAR_BL: return SlStorageHelper<std::deque, bool>::SlCalcLen(deque, conv);
1479  case SLE_VAR_I8: return SlStorageHelper<std::deque, int8>::SlCalcLen(deque, conv);
1480  case SLE_VAR_U8: return SlStorageHelper<std::deque, uint8>::SlCalcLen(deque, conv);
1481  case SLE_VAR_I16: return SlStorageHelper<std::deque, int16>::SlCalcLen(deque, conv);
1482  case SLE_VAR_U16: return SlStorageHelper<std::deque, uint16>::SlCalcLen(deque, conv);
1483  case SLE_VAR_I32: return SlStorageHelper<std::deque, int32>::SlCalcLen(deque, conv);
1484  case SLE_VAR_U32: return SlStorageHelper<std::deque, uint32>::SlCalcLen(deque, conv);
1485  case SLE_VAR_I64: return SlStorageHelper<std::deque, int64>::SlCalcLen(deque, conv);
1486  case SLE_VAR_U64: return SlStorageHelper<std::deque, uint64>::SlCalcLen(deque, conv);
1487  default: NOT_REACHED();
1488  }
1489 }
1490 
1496 static void SlDeque(void *deque, VarType conv)
1497 {
1498  switch (GetVarMemType(conv)) {
1499  case SLE_VAR_BL: SlStorageHelper<std::deque, bool>::SlSaveLoad(deque, conv); break;
1500  case SLE_VAR_I8: SlStorageHelper<std::deque, int8>::SlSaveLoad(deque, conv); break;
1501  case SLE_VAR_U8: SlStorageHelper<std::deque, uint8>::SlSaveLoad(deque, conv); break;
1502  case SLE_VAR_I16: SlStorageHelper<std::deque, int16>::SlSaveLoad(deque, conv); break;
1503  case SLE_VAR_U16: SlStorageHelper<std::deque, uint16>::SlSaveLoad(deque, conv); break;
1504  case SLE_VAR_I32: SlStorageHelper<std::deque, int32>::SlSaveLoad(deque, conv); break;
1505  case SLE_VAR_U32: SlStorageHelper<std::deque, uint32>::SlSaveLoad(deque, conv); break;
1506  case SLE_VAR_I64: SlStorageHelper<std::deque, int64>::SlSaveLoad(deque, conv); break;
1507  case SLE_VAR_U64: SlStorageHelper<std::deque, uint64>::SlSaveLoad(deque, conv); break;
1508  default: NOT_REACHED();
1509  }
1510 }
1511 
1517 static inline size_t SlCalcVectorLen(const void *vector, VarType conv)
1518 {
1519  switch (GetVarMemType(conv)) {
1520  case SLE_VAR_BL: NOT_REACHED(); // Not supported
1521  case SLE_VAR_I8: return SlStorageHelper<std::vector, int8>::SlCalcLen(vector, conv);
1522  case SLE_VAR_U8: return SlStorageHelper<std::vector, uint8>::SlCalcLen(vector, conv);
1523  case SLE_VAR_I16: return SlStorageHelper<std::vector, int16>::SlCalcLen(vector, conv);
1524  case SLE_VAR_U16: return SlStorageHelper<std::vector, uint16>::SlCalcLen(vector, conv);
1525  case SLE_VAR_I32: return SlStorageHelper<std::vector, int32>::SlCalcLen(vector, conv);
1526  case SLE_VAR_U32: return SlStorageHelper<std::vector, uint32>::SlCalcLen(vector, conv);
1527  case SLE_VAR_I64: return SlStorageHelper<std::vector, int64>::SlCalcLen(vector, conv);
1528  case SLE_VAR_U64: return SlStorageHelper<std::vector, uint64>::SlCalcLen(vector, conv);
1529  default: NOT_REACHED();
1530  }
1531 }
1532 
1538 static void SlVector(void *vector, VarType conv)
1539 {
1540  switch (GetVarMemType(conv)) {
1541  case SLE_VAR_BL: NOT_REACHED(); // Not supported
1542  case SLE_VAR_I8: SlStorageHelper<std::vector, int8>::SlSaveLoad(vector, conv); break;
1543  case SLE_VAR_U8: SlStorageHelper<std::vector, uint8>::SlSaveLoad(vector, conv); break;
1544  case SLE_VAR_I16: SlStorageHelper<std::vector, int16>::SlSaveLoad(vector, conv); break;
1545  case SLE_VAR_U16: SlStorageHelper<std::vector, uint16>::SlSaveLoad(vector, conv); break;
1546  case SLE_VAR_I32: SlStorageHelper<std::vector, int32>::SlSaveLoad(vector, conv); break;
1547  case SLE_VAR_U32: SlStorageHelper<std::vector, uint32>::SlSaveLoad(vector, conv); break;
1548  case SLE_VAR_I64: SlStorageHelper<std::vector, int64>::SlSaveLoad(vector, conv); break;
1549  case SLE_VAR_U64: SlStorageHelper<std::vector, uint64>::SlSaveLoad(vector, conv); break;
1550  default: NOT_REACHED();
1551  }
1552 }
1553 
1555 static inline bool SlIsObjectValidInSavegame(const SaveLoad &sld)
1556 {
1557  return (_sl_version >= sld.version_from && _sl_version < sld.version_to);
1558 }
1559 
1565 static size_t SlCalcTableHeader(const SaveLoadTable &slt)
1566 {
1567  size_t length = 0;
1568 
1569  for (auto &sld : slt) {
1570  if (!SlIsObjectValidInSavegame(sld)) continue;
1571 
1572  length += SlCalcConvFileLen(SLE_UINT8);
1573  length += SlCalcStdStringLen(&sld.name);
1574  }
1575 
1576  length += SlCalcConvFileLen(SLE_UINT8); // End-of-list entry.
1577 
1578  for (auto &sld : slt) {
1579  if (!SlIsObjectValidInSavegame(sld)) continue;
1580  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1581  length += SlCalcTableHeader(sld.handler->GetDescription());
1582  }
1583  }
1584 
1585  return length;
1586 }
1587 
1594 size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
1595 {
1596  size_t length = 0;
1597 
1598  /* Need to determine the length and write a length tag. */
1599  for (auto &sld : slt) {
1600  length += SlCalcObjMemberLength(object, sld);
1601  }
1602  return length;
1603 }
1604 
1605 size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld)
1606 {
1607  assert(_sl.action == SLA_SAVE);
1608 
1609  if (!SlIsObjectValidInSavegame(sld)) return 0;
1610 
1611  switch (sld.cmd) {
1612  case SL_VAR: return SlCalcConvFileLen(sld.conv);
1613  case SL_REF: return SlCalcRefLen();
1614  case SL_ARR: return SlCalcArrayLen(sld.length, sld.conv);
1615  case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld.length, sld.conv);
1616  case SL_REFLIST: return SlCalcRefListLen(GetVariableAddress(object, sld), sld.conv);
1617  case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld.conv);
1618  case SL_VECTOR: return SlCalcVectorLen(GetVariableAddress(object, sld), sld.conv);
1619  case SL_STDSTR: return SlCalcStdStringLen(GetVariableAddress(object, sld));
1620  case SL_SAVEBYTE: return 1; // a byte is logically of size 1
1621  case SL_NULL: return SlCalcConvFileLen(sld.conv) * sld.length;
1622 
1623  case SL_STRUCT:
1624  case SL_STRUCTLIST: {
1625  NeedLength old_need_length = _sl.need_length;
1626  size_t old_obj_len = _sl.obj_len;
1627 
1629  _sl.obj_len = 0;
1630 
1631  /* Pretend that we are saving to collect the object size. Other
1632  * means are difficult, as we don't know the length of the list we
1633  * are about to store. */
1634  sld.handler->Save(const_cast<void *>(object));
1635  size_t length = _sl.obj_len;
1636 
1637  _sl.obj_len = old_obj_len;
1638  _sl.need_length = old_need_length;
1639 
1640  if (sld.cmd == SL_STRUCT) {
1641  length += SlGetArrayLength(1);
1642  }
1643 
1644  return length;
1645  }
1646 
1647  default: NOT_REACHED();
1648  }
1649  return 0;
1650 }
1651 
1657 [[maybe_unused]] static bool IsVariableSizeRight(const SaveLoad &sld)
1658 {
1659  if (GetVarMemType(sld.conv) == SLE_VAR_NULL) return true;
1660 
1661  switch (sld.cmd) {
1662  case SL_VAR:
1663  switch (GetVarMemType(sld.conv)) {
1664  case SLE_VAR_BL:
1665  return sld.size == sizeof(bool);
1666  case SLE_VAR_I8:
1667  case SLE_VAR_U8:
1668  return sld.size == sizeof(int8);
1669  case SLE_VAR_I16:
1670  case SLE_VAR_U16:
1671  return sld.size == sizeof(int16);
1672  case SLE_VAR_I32:
1673  case SLE_VAR_U32:
1674  return sld.size == sizeof(int32);
1675  case SLE_VAR_I64:
1676  case SLE_VAR_U64:
1677  return sld.size == sizeof(int64);
1678  case SLE_VAR_NAME:
1679  return sld.size == sizeof(std::string);
1680  default:
1681  return sld.size == sizeof(void *);
1682  }
1683  case SL_REF:
1684  /* These should all be pointer sized. */
1685  return sld.size == sizeof(void *);
1686 
1687  case SL_STR:
1688  /* These should be pointer sized, or fixed array. */
1689  return sld.size == sizeof(void *) || sld.size == sld.length;
1690 
1691  case SL_STDSTR:
1692  /* These should be all pointers to std::string. */
1693  return sld.size == sizeof(std::string);
1694 
1695  default:
1696  return true;
1697  }
1698 }
1699 
1700 static bool SlObjectMember(void *object, const SaveLoad &sld)
1701 {
1702  assert(IsVariableSizeRight(sld));
1703 
1704  if (!SlIsObjectValidInSavegame(sld)) return false;
1705 
1706  VarType conv = GB(sld.conv, 0, 8);
1707  switch (sld.cmd) {
1708  case SL_VAR:
1709  case SL_REF:
1710  case SL_ARR:
1711  case SL_STR:
1712  case SL_REFLIST:
1713  case SL_DEQUE:
1714  case SL_VECTOR:
1715  case SL_STDSTR: {
1716  void *ptr = GetVariableAddress(object, sld);
1717 
1718  switch (sld.cmd) {
1719  case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1720  case SL_REF: SlSaveLoadRef(ptr, conv); break;
1721  case SL_ARR: SlArray(ptr, sld.length, conv); break;
1722  case SL_STR: SlString(ptr, sld.length, sld.conv); break;
1723  case SL_REFLIST: SlRefList(ptr, conv); break;
1724  case SL_DEQUE: SlDeque(ptr, conv); break;
1725  case SL_VECTOR: SlVector(ptr, conv); break;
1726  case SL_STDSTR: SlStdString(ptr, sld.conv); break;
1727  default: NOT_REACHED();
1728  }
1729  break;
1730  }
1731 
1732  /* SL_SAVEBYTE writes a value to the savegame to identify the type of an object.
1733  * When loading, the value is read explicitly with SlReadByte() to determine which
1734  * object description to use. */
1735  case SL_SAVEBYTE: {
1736  void *ptr = GetVariableAddress(object, sld);
1737 
1738  switch (_sl.action) {
1739  case SLA_SAVE: SlWriteByte(*(uint8 *)ptr); break;
1740  case SLA_LOAD_CHECK:
1741  case SLA_LOAD:
1742  case SLA_PTRS:
1743  case SLA_NULL: break;
1744  default: NOT_REACHED();
1745  }
1746  break;
1747  }
1748 
1749  case SL_NULL: {
1750  assert(GetVarMemType(sld.conv) == SLE_VAR_NULL);
1751 
1752  switch (_sl.action) {
1753  case SLA_LOAD_CHECK:
1754  case SLA_LOAD: SlSkipBytes(SlCalcConvFileLen(sld.conv) * sld.length); break;
1755  case SLA_SAVE: for (int i = 0; i < SlCalcConvFileLen(sld.conv) * sld.length; i++) SlWriteByte(0); break;
1756  case SLA_PTRS:
1757  case SLA_NULL: break;
1758  default: NOT_REACHED();
1759  }
1760  break;
1761  }
1762 
1763  case SL_STRUCT:
1764  case SL_STRUCTLIST:
1765  switch (_sl.action) {
1766  case SLA_SAVE: {
1767  if (sld.cmd == SL_STRUCT) {
1768  /* Store in the savegame if this struct was written or not. */
1769  SlSetStructListLength(SlCalcObjMemberLength(object, sld) > SlGetArrayLength(1) ? 1 : 0);
1770  }
1771  sld.handler->Save(object);
1772  break;
1773  }
1774 
1775  case SLA_LOAD_CHECK: {
1778  }
1779  sld.handler->LoadCheck(object);
1780  break;
1781  }
1782 
1783  case SLA_LOAD: {
1786  }
1787  sld.handler->Load(object);
1788  break;
1789  }
1790 
1791  case SLA_PTRS:
1792  sld.handler->FixPointers(object);
1793  break;
1794 
1795  case SLA_NULL: break;
1796  default: NOT_REACHED();
1797  }
1798  break;
1799 
1800  default: NOT_REACHED();
1801  }
1802  return true;
1803 }
1804 
1809 void SlSetStructListLength(size_t length)
1810 {
1811  /* Automatically calculate the length? */
1812  if (_sl.need_length != NL_NONE) {
1813  SlSetLength(SlGetArrayLength(length));
1814  if (_sl.need_length == NL_CALCLENGTH) return;
1815  }
1816 
1817  SlWriteArrayLength(length);
1818 }
1819 
1825 size_t SlGetStructListLength(size_t limit)
1826 {
1827  size_t length = SlReadArrayLength();
1828  if (length > limit) SlErrorCorrupt("List exceeds storage size");
1829 
1830  return length;
1831 }
1832 
1838 void SlObject(void *object, const SaveLoadTable &slt)
1839 {
1840  /* Automatically calculate the length? */
1841  if (_sl.need_length != NL_NONE) {
1842  SlSetLength(SlCalcObjLength(object, slt));
1843  if (_sl.need_length == NL_CALCLENGTH) return;
1844  }
1845 
1846  for (auto &sld : slt) {
1847  SlObjectMember(object, sld);
1848  }
1849 }
1850 
1856  void Save(void *object) const override
1857  {
1858  NOT_REACHED();
1859  }
1860 
1861  void Load(void *object) const override
1862  {
1863  size_t length = SlGetStructListLength(UINT32_MAX);
1864  for (; length > 0; length--) {
1865  SlObject(object, this->GetLoadDescription());
1866  }
1867  }
1868 
1869  void LoadCheck(void *object) const override
1870  {
1871  this->Load(object);
1872  }
1873 
1874  virtual SaveLoadTable GetDescription() const override
1875  {
1876  return {};
1877  }
1878 
1880  {
1881  NOT_REACHED();
1882  }
1883 };
1884 
1891 std::vector<SaveLoad> SlTableHeader(const SaveLoadTable &slt)
1892 {
1893  /* You can only use SlTableHeader if you are a CH_TABLE. */
1894  assert(_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
1895 
1896  switch (_sl.action) {
1897  case SLA_LOAD_CHECK:
1898  case SLA_LOAD: {
1899  std::vector<SaveLoad> saveloads;
1900 
1901  /* Build a key lookup mapping based on the available fields. */
1902  std::map<std::string, const SaveLoad *> key_lookup;
1903  for (auto &sld : slt) {
1904  if (!SlIsObjectValidInSavegame(sld)) continue;
1905 
1906  /* Check that there is only one active SaveLoad for a given name. */
1907  assert(key_lookup.find(sld.name) == key_lookup.end());
1908  key_lookup[sld.name] = &sld;
1909  }
1910 
1911  while (true) {
1912  uint8 type;
1913  SlSaveLoadConv(&type, SLE_UINT8);
1914  if (type == SLE_FILE_END) break;
1915 
1916  std::string key;
1917  SlStdString(&key, SLE_STR);
1918 
1919  auto sld_it = key_lookup.find(key);
1920  if (sld_it == key_lookup.end()) {
1921  /* SLA_LOADCHECK triggers this debug statement a lot and is perfectly normal. */
1922  Debug(sl, _sl.action == SLA_LOAD ? 2 : 6, "Field '{}' of type 0x{:02x} not found, skipping", key, type);
1923 
1924  std::shared_ptr<SaveLoadHandler> handler = nullptr;
1925  SaveLoadType slt;
1926  switch (type & SLE_FILE_TYPE_MASK) {
1927  case SLE_FILE_STRING:
1928  /* Strings are always marked with SLE_FILE_HAS_LENGTH_FIELD, as they are a list of chars. */
1929  slt = SL_STR;
1930  break;
1931 
1932  case SLE_FILE_STRUCT:
1933  /* Structs are always marked with SLE_FILE_HAS_LENGTH_FIELD as SL_STRUCT is seen as a list of 0/1 in length. */
1934  slt = SL_STRUCTLIST;
1935  handler = std::make_shared<SlSkipHandler>();
1936  break;
1937 
1938  default:
1939  slt = (type & SLE_FILE_HAS_LENGTH_FIELD) ? SL_ARR : SL_VAR;
1940  break;
1941  }
1942 
1943  /* We don't know this field, so read to nothing. */
1944  saveloads.push_back({key, slt, ((VarType)type & SLE_FILE_TYPE_MASK) | SLE_VAR_NULL, 1, SL_MIN_VERSION, SL_MAX_VERSION, 0, nullptr, 0, handler});
1945  continue;
1946  }
1947 
1948  /* Validate the type of the field. If it is changed, the
1949  * savegame should have been bumped so we know how to do the
1950  * conversion. If this error triggers, that clearly didn't
1951  * happen and this is a friendly poke to the developer to bump
1952  * the savegame version and add conversion code. */
1953  uint8 correct_type = GetSavegameFileType(*sld_it->second);
1954  if (correct_type != type) {
1955  Debug(sl, 1, "Field type for '{}' was expected to be 0x{:02x} but 0x{:02x} was found", key, correct_type, type);
1956  SlErrorCorrupt("Field type is different than expected");
1957  }
1958  saveloads.push_back(*sld_it->second);
1959  }
1960 
1961  for (auto &sld : saveloads) {
1962  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1963  sld.handler->load_description = SlTableHeader(sld.handler->GetDescription());
1964  }
1965  }
1966 
1967  return saveloads;
1968  }
1969 
1970  case SLA_SAVE: {
1971  /* Automatically calculate the length? */
1972  if (_sl.need_length != NL_NONE) {
1974  if (_sl.need_length == NL_CALCLENGTH) break;
1975  }
1976 
1977  for (auto &sld : slt) {
1978  if (!SlIsObjectValidInSavegame(sld)) continue;
1979  /* Make sure we are not storing empty keys. */
1980  assert(!sld.name.empty());
1981 
1982  uint8 type = GetSavegameFileType(sld);
1983  assert(type != SLE_FILE_END);
1984 
1985  SlSaveLoadConv(&type, SLE_UINT8);
1986  SlStdString(const_cast<std::string *>(&sld.name), SLE_STR);
1987  }
1988 
1989  /* Add an end-of-header marker. */
1990  uint8 type = SLE_FILE_END;
1991  SlSaveLoadConv(&type, SLE_UINT8);
1992 
1993  /* After the table, write down any sub-tables we might have. */
1994  for (auto &sld : slt) {
1995  if (!SlIsObjectValidInSavegame(sld)) continue;
1996  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1997  /* SlCalcTableHeader already looks in sub-lists, so avoid the length being added twice. */
1998  NeedLength old_need_length = _sl.need_length;
2000 
2001  SlTableHeader(sld.handler->GetDescription());
2002 
2003  _sl.need_length = old_need_length;
2004  }
2005  }
2006 
2007  break;
2008  }
2009 
2010  default: NOT_REACHED();
2011  }
2012 
2013  return std::vector<SaveLoad>();
2014 }
2015 
2029 std::vector<SaveLoad> SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
2030 {
2031  assert(_sl.action == SLA_LOAD || _sl.action == SLA_LOAD_CHECK);
2032  /* CH_TABLE / CH_SPARSE_TABLE always have a header. */
2033  if (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE) return SlTableHeader(slt);
2034 
2035  std::vector<SaveLoad> saveloads;
2036 
2037  /* Build a key lookup mapping based on the available fields. */
2038  std::map<std::string, std::vector<const SaveLoad *>> key_lookup;
2039  for (auto &sld : slt) {
2040  /* All entries should have a name; otherwise the entry should just be removed. */
2041  assert(!sld.name.empty());
2042 
2043  key_lookup[sld.name].push_back(&sld);
2044  }
2045 
2046  for (auto &slc : slct) {
2047  if (slc.name.empty()) {
2048  /* In old savegames there can be data we no longer care for. We
2049  * skip this by simply reading the amount of bytes indicated and
2050  * send those to /dev/null. */
2051  saveloads.push_back({"", SL_NULL, SLE_FILE_U8 | SLE_VAR_NULL, slc.length, slc.version_from, slc.version_to, 0, nullptr, 0, nullptr});
2052  } else {
2053  auto sld_it = key_lookup.find(slc.name);
2054  /* If this branch triggers, it means that an entry in the
2055  * SaveLoadCompat list is not mentioned in the SaveLoad list. Did
2056  * you rename a field in one and not in the other? */
2057  if (sld_it == key_lookup.end()) {
2058  /* This isn't an assert, as that leaves no information what
2059  * field was to blame. This way at least we have breadcrumbs. */
2060  Debug(sl, 0, "internal error: saveload compatibility field '{}' not found", slc.name);
2061  SlErrorCorrupt("Internal error with savegame compatibility");
2062  }
2063  for (auto &sld : sld_it->second) {
2064  saveloads.push_back(*sld);
2065  }
2066  }
2067  }
2068 
2069  for (auto &sld : saveloads) {
2070  if (!SlIsObjectValidInSavegame(sld)) continue;
2071  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
2072  sld.handler->load_description = SlCompatTableHeader(sld.handler->GetDescription(), sld.handler->GetCompatDescription());
2073  }
2074  }
2075 
2076  return saveloads;
2077 }
2078 
2083 void SlGlobList(const SaveLoadTable &slt)
2084 {
2085  SlObject(nullptr, slt);
2086 }
2087 
2093 void SlAutolength(AutolengthProc *proc, void *arg)
2094 {
2095  size_t offs;
2096 
2097  assert(_sl.action == SLA_SAVE);
2098 
2099  /* Tell it to calculate the length */
2101  _sl.obj_len = 0;
2102  proc(arg);
2103 
2104  /* Setup length */
2107 
2108  offs = _sl.dumper->GetSize() + _sl.obj_len;
2109 
2110  /* And write the stuff */
2111  proc(arg);
2112 
2113  if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
2114 }
2115 
2116 void ChunkHandler::LoadCheck(size_t len) const
2117 {
2118  switch (_sl.block_mode) {
2119  case CH_TABLE:
2120  case CH_SPARSE_TABLE:
2121  SlTableHeader({});
2122  FALLTHROUGH;
2123  case CH_ARRAY:
2124  case CH_SPARSE_ARRAY:
2125  SlSkipArray();
2126  break;
2127  case CH_RIFF:
2128  SlSkipBytes(len);
2129  break;
2130  default:
2131  NOT_REACHED();
2132  }
2133 }
2134 
2139 static void SlLoadChunk(const ChunkHandler &ch)
2140 {
2141  byte m = SlReadByte();
2142  size_t len;
2143  size_t endoffs;
2144 
2145  _sl.block_mode = m & CH_TYPE_MASK;
2146  _sl.obj_len = 0;
2147  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2148 
2149  /* The header should always be at the start. Read the length; the
2150  * Load() should as first action process the header. */
2151  if (_sl.expect_table_header) {
2152  SlIterateArray();
2153  }
2154 
2155  switch (_sl.block_mode) {
2156  case CH_TABLE:
2157  case CH_ARRAY:
2158  _sl.array_index = 0;
2159  ch.Load();
2160  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2161  break;
2162  case CH_SPARSE_TABLE:
2163  case CH_SPARSE_ARRAY:
2164  ch.Load();
2165  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2166  break;
2167  case CH_RIFF:
2168  /* Read length */
2169  len = (SlReadByte() << 16) | ((m >> 4) << 24);
2170  len += SlReadUint16();
2171  _sl.obj_len = len;
2172  endoffs = _sl.reader->GetSize() + len;
2173  ch.Load();
2174  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
2175  break;
2176  default:
2177  SlErrorCorrupt("Invalid chunk type");
2178  break;
2179  }
2180 
2181  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2182 }
2183 
2189 static void SlLoadCheckChunk(const ChunkHandler &ch)
2190 {
2191  byte m = SlReadByte();
2192  size_t len;
2193  size_t endoffs;
2194 
2195  _sl.block_mode = m & CH_TYPE_MASK;
2196  _sl.obj_len = 0;
2197  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2198 
2199  /* The header should always be at the start. Read the length; the
2200  * LoadCheck() should as first action process the header. */
2201  if (_sl.expect_table_header) {
2202  SlIterateArray();
2203  }
2204 
2205  switch (_sl.block_mode) {
2206  case CH_TABLE:
2207  case CH_ARRAY:
2208  _sl.array_index = 0;
2209  ch.LoadCheck();
2210  break;
2211  case CH_SPARSE_TABLE:
2212  case CH_SPARSE_ARRAY:
2213  ch.LoadCheck();
2214  break;
2215  case CH_RIFF:
2216  /* Read length */
2217  len = (SlReadByte() << 16) | ((m >> 4) << 24);
2218  len += SlReadUint16();
2219  _sl.obj_len = len;
2220  endoffs = _sl.reader->GetSize() + len;
2221  ch.LoadCheck(len);
2222  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
2223  break;
2224  default:
2225  SlErrorCorrupt("Invalid chunk type");
2226  break;
2227  }
2228 
2229  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2230 }
2231 
2237 static void SlSaveChunk(const ChunkHandler &ch)
2238 {
2239  if (ch.type == CH_READONLY) return;
2240 
2241  SlWriteUint32(ch.id);
2242  Debug(sl, 2, "Saving chunk {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id);
2243 
2244  _sl.block_mode = ch.type;
2245  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2246 
2248 
2249  switch (_sl.block_mode) {
2250  case CH_RIFF:
2251  ch.Save();
2252  break;
2253  case CH_TABLE:
2254  case CH_ARRAY:
2255  _sl.last_array_index = 0;
2257  ch.Save();
2258  SlWriteArrayLength(0); // Terminate arrays
2259  break;
2260  case CH_SPARSE_TABLE:
2261  case CH_SPARSE_ARRAY:
2263  ch.Save();
2264  SlWriteArrayLength(0); // Terminate arrays
2265  break;
2266  default: NOT_REACHED();
2267  }
2268 
2269  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2270 }
2271 
2273 static void SlSaveChunks()
2274 {
2275  for (auto &ch : ChunkHandlers()) {
2276  SlSaveChunk(ch);
2277  }
2278 
2279  /* Terminator */
2280  SlWriteUint32(0);
2281 }
2282 
2289 static const ChunkHandler *SlFindChunkHandler(uint32 id)
2290 {
2291  for (const ChunkHandler &ch : ChunkHandlers()) if (ch.id == id) return &ch;
2292  return nullptr;
2293 }
2294 
2296 static void SlLoadChunks()
2297 {
2298  uint32 id;
2299  const ChunkHandler *ch;
2300 
2301  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
2302  Debug(sl, 2, "Loading chunk {:c}{:c}{:c}{:c}", id >> 24, id >> 16, id >> 8, id);
2303 
2304  ch = SlFindChunkHandler(id);
2305  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2306  SlLoadChunk(*ch);
2307  }
2308 }
2309 
2311 static void SlLoadCheckChunks()
2312 {
2313  uint32 id;
2314  const ChunkHandler *ch;
2315 
2316  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
2317  Debug(sl, 2, "Loading chunk {:c}{:c}{:c}{:c}", id >> 24, id >> 16, id >> 8, id);
2318 
2319  ch = SlFindChunkHandler(id);
2320  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2321  SlLoadCheckChunk(*ch);
2322  }
2323 }
2324 
2326 static void SlFixPointers()
2327 {
2328  _sl.action = SLA_PTRS;
2329 
2330  for (const ChunkHandler &ch : ChunkHandlers()) {
2331  Debug(sl, 3, "Fixing pointers for {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id);
2332  ch.FixPointers();
2333  }
2334 
2335  assert(_sl.action == SLA_PTRS);
2336 }
2337 
2338 
2341  FILE *file;
2342  long begin;
2343 
2348  FileReader(FILE *file) : LoadFilter(nullptr), file(file), begin(ftell(file))
2349  {
2350  }
2351 
2354  {
2355  if (this->file != nullptr) fclose(this->file);
2356  this->file = nullptr;
2357 
2358  /* Make sure we don't double free. */
2359  _sl.sf = nullptr;
2360  }
2361 
2362  size_t Read(byte *buf, size_t size) override
2363  {
2364  /* We're in the process of shutting down, i.e. in "failure" mode. */
2365  if (this->file == nullptr) return 0;
2366 
2367  return fread(buf, 1, size, this->file);
2368  }
2369 
2370  void Reset() override
2371  {
2372  clearerr(this->file);
2373  if (fseek(this->file, this->begin, SEEK_SET)) {
2374  Debug(sl, 1, "Could not reset the file reading");
2375  }
2376  }
2377 };
2378 
2381  FILE *file;
2382 
2387  FileWriter(FILE *file) : SaveFilter(nullptr), file(file)
2388  {
2389  }
2390 
2393  {
2394  this->Finish();
2395 
2396  /* Make sure we don't double free. */
2397  _sl.sf = nullptr;
2398  }
2399 
2400  void Write(byte *buf, size_t size) override
2401  {
2402  /* We're in the process of shutting down, i.e. in "failure" mode. */
2403  if (this->file == nullptr) return;
2404 
2405  if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
2406  }
2407 
2408  void Finish() override
2409  {
2410  if (this->file != nullptr) fclose(this->file);
2411  this->file = nullptr;
2412  }
2413 };
2414 
2415 /*******************************************
2416  ********** START OF LZO CODE **************
2417  *******************************************/
2418 
2419 #ifdef WITH_LZO
2420 #include <lzo/lzo1x.h>
2421 
2423 static const uint LZO_BUFFER_SIZE = 8192;
2424 
2432  {
2433  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2434  }
2435 
2436  size_t Read(byte *buf, size_t ssize) override
2437  {
2438  assert(ssize >= LZO_BUFFER_SIZE);
2439 
2440  /* Buffer size is from the LZO docs plus the chunk header size. */
2441  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
2442  uint32 tmp[2];
2443  uint32 size;
2444  lzo_uint len = ssize;
2445 
2446  /* Read header*/
2447  if (this->chain->Read((byte*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
2448 
2449  /* Check if size is bad */
2450  ((uint32*)out)[0] = size = tmp[1];
2451 
2452  if (_sl_version != SL_MIN_VERSION) {
2453  tmp[0] = TO_BE32(tmp[0]);
2454  size = TO_BE32(size);
2455  }
2456 
2457  if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
2458 
2459  /* Read block */
2460  if (this->chain->Read(out + sizeof(uint32), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2461 
2462  /* Verify checksum */
2463  if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
2464 
2465  /* Decompress */
2466  int ret = lzo1x_decompress_safe(out + sizeof(uint32) * 1, size, buf, &len, nullptr);
2467  if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2468  return len;
2469  }
2470 };
2471 
2479  LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2480  {
2481  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2482  }
2483 
2484  void Write(byte *buf, size_t size) override
2485  {
2486  const lzo_bytep in = buf;
2487  /* Buffer size is from the LZO docs plus the chunk header size. */
2488  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
2489  byte wrkmem[LZO1X_1_MEM_COMPRESS];
2490  lzo_uint outlen;
2491 
2492  do {
2493  /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2494  lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
2495  lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
2496  ((uint32*)out)[1] = TO_BE32((uint32)outlen);
2497  ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
2498  this->chain->Write(out, outlen + sizeof(uint32) * 2);
2499 
2500  /* Move to next data chunk. */
2501  size -= len;
2502  in += len;
2503  } while (size > 0);
2504  }
2505 };
2506 
2507 #endif /* WITH_LZO */
2508 
2509 /*********************************************
2510  ******** START OF NOCOMP CODE (uncompressed)*
2511  *********************************************/
2512 
2520  {
2521  }
2522 
2523  size_t Read(byte *buf, size_t size) override
2524  {
2525  return this->chain->Read(buf, size);
2526  }
2527 };
2528 
2536  NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2537  {
2538  }
2539 
2540  void Write(byte *buf, size_t size) override
2541  {
2542  this->chain->Write(buf, size);
2543  }
2544 };
2545 
2546 /********************************************
2547  ********** START OF ZLIB CODE **************
2548  ********************************************/
2549 
2550 #if defined(WITH_ZLIB)
2551 #include <zlib.h>
2552 
2555  z_stream z;
2563  {
2564  memset(&this->z, 0, sizeof(this->z));
2565  if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2566  }
2567 
2569  ~ZlibLoadFilter()
2570  {
2571  inflateEnd(&this->z);
2572  }
2573 
2574  size_t Read(byte *buf, size_t size) override
2575  {
2576  this->z.next_out = buf;
2577  this->z.avail_out = (uint)size;
2578 
2579  do {
2580  /* read more bytes from the file? */
2581  if (this->z.avail_in == 0) {
2582  this->z.next_in = this->fread_buf;
2583  this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2584  }
2585 
2586  /* inflate the data */
2587  int r = inflate(&this->z, 0);
2588  if (r == Z_STREAM_END) break;
2589 
2590  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2591  } while (this->z.avail_out != 0);
2592 
2593  return size - this->z.avail_out;
2594  }
2595 };
2596 
2599  z_stream z;
2600 
2606  ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2607  {
2608  memset(&this->z, 0, sizeof(this->z));
2609  if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2610  }
2611 
2614  {
2615  deflateEnd(&this->z);
2616  }
2617 
2624  void WriteLoop(byte *p, size_t len, int mode)
2625  {
2626  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2627  uint n;
2628  this->z.next_in = p;
2629  this->z.avail_in = (uInt)len;
2630  do {
2631  this->z.next_out = buf;
2632  this->z.avail_out = sizeof(buf);
2633 
2641  int r = deflate(&this->z, mode);
2642 
2643  /* bytes were emitted? */
2644  if ((n = sizeof(buf) - this->z.avail_out) != 0) {
2645  this->chain->Write(buf, n);
2646  }
2647  if (r == Z_STREAM_END) break;
2648 
2649  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2650  } while (this->z.avail_in || !this->z.avail_out);
2651  }
2652 
2653  void Write(byte *buf, size_t size) override
2654  {
2655  this->WriteLoop(buf, size, 0);
2656  }
2657 
2658  void Finish() override
2659  {
2660  this->WriteLoop(nullptr, 0, Z_FINISH);
2661  this->chain->Finish();
2662  }
2663 };
2664 
2665 #endif /* WITH_ZLIB */
2666 
2667 /********************************************
2668  ********** START OF LZMA CODE **************
2669  ********************************************/
2670 
2671 #if defined(WITH_LIBLZMA)
2672 #include <lzma.h>
2673 
2680 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2681 
2684  lzma_stream lzma;
2686 
2692  {
2693  /* Allow saves up to 256 MB uncompressed */
2694  if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2695  }
2696 
2699  {
2700  lzma_end(&this->lzma);
2701  }
2702 
2703  size_t Read(byte *buf, size_t size) override
2704  {
2705  this->lzma.next_out = buf;
2706  this->lzma.avail_out = size;
2707 
2708  do {
2709  /* read more bytes from the file? */
2710  if (this->lzma.avail_in == 0) {
2711  this->lzma.next_in = this->fread_buf;
2712  this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2713  }
2714 
2715  /* inflate the data */
2716  lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2717  if (r == LZMA_STREAM_END) break;
2718  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2719  } while (this->lzma.avail_out != 0);
2720 
2721  return size - this->lzma.avail_out;
2722  }
2723 };
2724 
2727  lzma_stream lzma;
2728 
2735  {
2736  if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2737  }
2738 
2741  {
2742  lzma_end(&this->lzma);
2743  }
2744 
2751  void WriteLoop(byte *p, size_t len, lzma_action action)
2752  {
2753  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2754  size_t n;
2755  this->lzma.next_in = p;
2756  this->lzma.avail_in = len;
2757  do {
2758  this->lzma.next_out = buf;
2759  this->lzma.avail_out = sizeof(buf);
2760 
2761  lzma_ret r = lzma_code(&this->lzma, action);
2762 
2763  /* bytes were emitted? */
2764  if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
2765  this->chain->Write(buf, n);
2766  }
2767  if (r == LZMA_STREAM_END) break;
2768  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2769  } while (this->lzma.avail_in || !this->lzma.avail_out);
2770  }
2771 
2772  void Write(byte *buf, size_t size) override
2773  {
2774  this->WriteLoop(buf, size, LZMA_RUN);
2775  }
2776 
2777  void Finish() override
2778  {
2779  this->WriteLoop(nullptr, 0, LZMA_FINISH);
2780  this->chain->Finish();
2781  }
2782 };
2783 
2784 #endif /* WITH_LIBLZMA */
2785 
2786 /*******************************************
2787  ************* END OF CODE *****************
2788  *******************************************/
2789 
2792  const char *name;
2793  uint32 tag;
2795  LoadFilter *(*init_load)(LoadFilter *chain);
2796  SaveFilter *(*init_write)(SaveFilter *chain, byte compression);
2799  byte default_compression;
2801 };
2805 #if defined(WITH_LZO)
2806  /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2807  {"lzo", TO_BE32X('OTTD'), CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2808 #else
2809  {"lzo", TO_BE32X('OTTD'), nullptr, nullptr, 0, 0, 0},
2810 #endif
2811  /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2812  {"none", TO_BE32X('OTTN'), CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2813 #if defined(WITH_ZLIB)
2814  /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2815  * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2816  * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2817  {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2818 #else
2819  {"zlib", TO_BE32X('OTTZ'), nullptr, nullptr, 0, 0, 0},
2820 #endif
2821 #if defined(WITH_LIBLZMA)
2822  /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2823  * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2824  * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2825  * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2826  * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2827  {"lzma", TO_BE32X('OTTX'), CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2828 #else
2829  {"lzma", TO_BE32X('OTTX'), nullptr, nullptr, 0, 0, 0},
2830 #endif
2831 };
2832 
2840 static const SaveLoadFormat *GetSavegameFormat(const std::string &full_name, byte *compression_level)
2841 {
2842  const SaveLoadFormat *def = lastof(_saveload_formats);
2843 
2844  /* find default savegame format, the highest one with which files can be written */
2845  while (!def->init_write) def--;
2846 
2847  if (!full_name.empty()) {
2848  /* Get the ":..." of the compression level out of the way */
2849  size_t separator = full_name.find(':');
2850  bool has_comp_level = separator != std::string::npos;
2851  const std::string name(full_name, 0, has_comp_level ? separator : full_name.size());
2852 
2853  for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
2854  if (slf->init_write != nullptr && name.compare(slf->name) == 0) {
2855  *compression_level = slf->default_compression;
2856  if (has_comp_level) {
2857  const std::string complevel(full_name, separator + 1);
2858 
2859  /* Get the level and determine whether all went fine. */
2860  size_t processed;
2861  long level = std::stol(complevel, &processed, 10);
2862  if (processed == 0 || level != Clamp(level, slf->min_compression, slf->max_compression)) {
2863  SetDParamStr(0, complevel);
2864  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2865  } else {
2866  *compression_level = level;
2867  }
2868  }
2869  return slf;
2870  }
2871  }
2872 
2873  SetDParamStr(0, name);
2874  SetDParamStr(1, def->name);
2875  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2876  }
2877  *compression_level = def->default_compression;
2878  return def;
2879 }
2880 
2881 /* actual loader/saver function */
2882 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2883 extern bool AfterLoadGame();
2884 extern bool LoadOldSaveGame(const std::string &file);
2885 
2889 static void ResetSaveloadData()
2890 {
2891  ResetTempEngineData();
2892  ResetLabelMaps();
2893  ResetOldWaypoints();
2894 }
2895 
2899 static inline void ClearSaveLoadState()
2900 {
2901  delete _sl.dumper;
2902  _sl.dumper = nullptr;
2903 
2904  delete _sl.sf;
2905  _sl.sf = nullptr;
2906 
2907  delete _sl.reader;
2908  _sl.reader = nullptr;
2909 
2910  delete _sl.lf;
2911  _sl.lf = nullptr;
2912 }
2913 
2919 static void SaveFileStart()
2920 {
2922  _game_speed = 100;
2923  SetMouseCursorBusy(true);
2924 
2926  _sl.saveinprogress = true;
2927 }
2928 
2930 static void SaveFileDone()
2931 {
2932  if (_game_mode != GM_MENU) _game_speed = _sl.game_speed;
2933  SetMouseCursorBusy(false);
2934 
2936  _sl.saveinprogress = false;
2937 
2938 #ifdef __EMSCRIPTEN__
2939  EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
2940 #endif
2941 }
2942 
2945 {
2946  _sl.error_str = str;
2947 }
2948 
2951 {
2952  SetDParam(0, _sl.error_str);
2954 
2955  static char err_str[512];
2956  GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
2957  return err_str;
2958 }
2959 
2961 static void SaveFileError()
2962 {
2964  ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
2965  SaveFileDone();
2966 }
2967 
2972 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2973 {
2974  try {
2975  byte compression;
2976  const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
2977 
2978  /* We have written our stuff to memory, now write it to file! */
2979  uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
2980  _sl.sf->Write((byte*)hdr, sizeof(hdr));
2981 
2982  _sl.sf = fmt->init_write(_sl.sf, compression);
2983  _sl.dumper->Flush(_sl.sf);
2984 
2986 
2987  if (threaded) SetAsyncSaveFinish(SaveFileDone);
2988 
2989  return SL_OK;
2990  } catch (...) {
2992 
2994 
2995  /* We don't want to shout when saving is just
2996  * cancelled due to a client disconnecting. */
2997  if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2998  /* Skip the "colour" character */
2999  Debug(sl, 0, "{}", GetSaveLoadErrorString() + 3);
3000  asfp = SaveFileError;
3001  }
3002 
3003  if (threaded) {
3004  SetAsyncSaveFinish(asfp);
3005  } else {
3006  asfp();
3007  }
3008  return SL_ERROR;
3009  }
3010 }
3011 
3012 void WaitTillSaved()
3013 {
3014  if (!_save_thread.joinable()) return;
3015 
3016  _save_thread.join();
3017 
3018  /* Make sure every other state is handled properly as well. */
3020 }
3021 
3030 static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
3031 {
3032  assert(!_sl.saveinprogress);
3033 
3034  _sl.dumper = new MemoryDumper();
3035  _sl.sf = writer;
3036 
3038 
3039  SaveViewportBeforeSaveGame();
3040  SlSaveChunks();
3041 
3042  SaveFileStart();
3043 
3044  if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
3045  if (threaded) Debug(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
3046 
3047  SaveOrLoadResult result = SaveFileToDisk(false);
3048  SaveFileDone();
3049 
3050  return result;
3051  }
3052 
3053  return SL_OK;
3054 }
3055 
3063 {
3064  try {
3065  _sl.action = SLA_SAVE;
3066  return DoSave(writer, threaded);
3067  } catch (...) {
3069  return SL_ERROR;
3070  }
3071 }
3072 
3079 static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
3080 {
3081  _sl.lf = reader;
3082 
3083  if (load_check) {
3084  /* Clear previous check data */
3086  /* Mark SL_LOAD_CHECK as supported for this savegame. */
3087  _load_check_data.checkable = true;
3088  }
3089 
3090  uint32 hdr[2];
3091  if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3092 
3093  /* see if we have any loader for this type. */
3094  const SaveLoadFormat *fmt = _saveload_formats;
3095  for (;;) {
3096  /* No loader found, treat as version 0 and use LZO format */
3097  if (fmt == endof(_saveload_formats)) {
3098  Debug(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
3099  _sl.lf->Reset();
3101  _sl_minor_version = 0;
3102 
3103  /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
3104  fmt = _saveload_formats;
3105  for (;;) {
3106  if (fmt == endof(_saveload_formats)) {
3107  /* Who removed LZO support? */
3108  NOT_REACHED();
3109  }
3110  if (fmt->tag == TO_BE32X('OTTD')) break;
3111  fmt++;
3112  }
3113  break;
3114  }
3115 
3116  if (fmt->tag == hdr[0]) {
3117  /* check version number */
3118  _sl_version = (SaveLoadVersion)(TO_BE32(hdr[1]) >> 16);
3119  /* Minor is not used anymore from version 18.0, but it is still needed
3120  * in versions before that (4 cases) which can't be removed easy.
3121  * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
3122  _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
3123 
3124  Debug(sl, 1, "Loading savegame version {}", _sl_version);
3125 
3126  /* Is the version higher than the current? */
3127  if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
3128  if (_sl_version >= SLV_START_PATCHPACKS && _sl_version <= SLV_END_PATCHPACKS) SlError(STR_GAME_SAVELOAD_ERROR_PATCHPACK);
3129  break;
3130  }
3131 
3132  fmt++;
3133  }
3134 
3135  /* loader for this savegame type is not implemented? */
3136  if (fmt->init_load == nullptr) {
3137  char err_str[64];
3138  seprintf(err_str, lastof(err_str), "Loader for '%s' is not available.", fmt->name);
3139  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
3140  }
3141 
3142  _sl.lf = fmt->init_load(_sl.lf);
3143  _sl.reader = new ReadBuffer(_sl.lf);
3144  _next_offs = 0;
3145 
3146  if (!load_check) {
3148 
3149  /* Old maps were hardcoded to 256x256 and thus did not contain
3150  * any mapsize information. Pre-initialize to 256x256 to not to
3151  * confuse old games */
3152  InitializeGame(256, 256, true, true);
3153 
3154  GamelogReset();
3155 
3157  /*
3158  * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
3159  * shared savegame version 4. Anything before that 'obviously'
3160  * does not have any NewGRFs. Between the introduction and
3161  * savegame version 41 (just before 0.5) the NewGRF settings
3162  * were not stored in the savegame and they were loaded by
3163  * using the settings from the main menu.
3164  * So, to recap:
3165  * - savegame version < 4: do not load any NewGRFs.
3166  * - savegame version >= 41: load NewGRFs from savegame, which is
3167  * already done at this stage by
3168  * overwriting the main menu settings.
3169  * - other savegame versions: use main menu settings.
3170  *
3171  * This means that users *can* crash savegame version 4..40
3172  * savegames if they set incompatible NewGRFs in the main menu,
3173  * but can't crash anymore for savegame version < 4 savegames.
3174  *
3175  * Note: this is done here because AfterLoadGame is also called
3176  * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
3177  */
3179  }
3180  }
3181 
3182  if (load_check) {
3183  /* Load chunks into _load_check_data.
3184  * No pools are loaded. References are not possible, and thus do not need resolving. */
3186  } else {
3187  /* Load chunks and resolve references */
3188  SlLoadChunks();
3189  SlFixPointers();
3190  }
3191 
3193 
3195 
3196  if (load_check) {
3197  /* The only part from AfterLoadGame() we need */
3199  } else {
3201 
3202  /* After loading fix up savegame for any internal changes that
3203  * might have occurred since then. If it fails, load back the old game. */
3204  if (!AfterLoadGame()) {
3206  return SL_REINIT;
3207  }
3208 
3210  }
3211 
3212  return SL_OK;
3213 }
3214 
3221 {
3222  try {
3223  _sl.action = SLA_LOAD;
3224  return DoLoad(reader, false);
3225  } catch (...) {
3227  return SL_REINIT;
3228  }
3229 }
3230 
3240 SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
3241 {
3242  /* An instance of saving is already active, so don't go saving again */
3243  if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
3244  /* if not an autosave, but a user action, show error message */
3245  if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
3246  return SL_OK;
3247  }
3248  WaitTillSaved();
3249 
3250  try {
3251  /* Load a TTDLX or TTDPatch game */
3252  if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
3254 
3255  InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
3256 
3257  /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
3258  * and if so a new NewGRF list will be made in LoadOldSaveGame.
3259  * Note: this is done here because AfterLoadGame is also called
3260  * for OTTD savegames which have their own NewGRF logic. */
3262  GamelogReset();
3263  if (!LoadOldSaveGame(filename)) return SL_REINIT;
3265  _sl_minor_version = 0;
3267  if (!AfterLoadGame()) {
3269  return SL_REINIT;
3270  }
3272  return SL_OK;
3273  }
3274 
3275  assert(dft == DFT_GAME_FILE);
3276  switch (fop) {
3277  case SLO_CHECK:
3279  break;
3280 
3281  case SLO_LOAD:
3282  _sl.action = SLA_LOAD;
3283  break;
3284 
3285  case SLO_SAVE:
3286  _sl.action = SLA_SAVE;
3287  break;
3288 
3289  default: NOT_REACHED();
3290  }
3291 
3292  FILE *fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
3293 
3294  /* Make it a little easier to load savegames from the console */
3295  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
3296  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
3297  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
3298 
3299  if (fh == nullptr) {
3300  SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3301  }
3302 
3303  if (fop == SLO_SAVE) { // SAVE game
3304  Debug(desync, 1, "save: {:08x}; {:02x}; {}", _date, _date_fract, filename);
3305  if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
3306 
3307  return DoSave(new FileWriter(fh), threaded);
3308  }
3309 
3310  /* LOAD game */
3311  assert(fop == SLO_LOAD || fop == SLO_CHECK);
3312  Debug(desync, 1, "load: {}", filename);
3313  return DoLoad(new FileReader(fh), fop == SLO_CHECK);
3314  } catch (...) {
3315  /* This code may be executed both for old and new save games. */
3317 
3318  /* Skip the "colour" character */
3319  if (fop != SLO_CHECK) Debug(sl, 0, "{}", GetSaveLoadErrorString());
3320 
3321  /* A saver/loader exception!! reinitialize all variables to prevent crash! */
3322  return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
3323  }
3324 }
3325 
3332 {
3333  char buf[MAX_PATH];
3334 
3336  GenerateDefaultSaveName(buf, lastof(buf));
3337  strecat(buf, counter.Extension().c_str(), lastof(buf));
3338  } else {
3339  strecpy(buf, counter.Filename().c_str(), lastof(buf));
3340  }
3341 
3342  Debug(sl, 2, "Autosaving to '{}'", buf);
3344  ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR);
3345  }
3346 }
3347 
3348 
3351 {
3353 }
3354 
3360 void GenerateDefaultSaveName(char *buf, const char *last)
3361 {
3362  /* Check if we have a name for this map, which is the name of the first
3363  * available company. When there's no company available we'll use
3364  * 'Spectator' as "company" name. */
3365  CompanyID cid = _local_company;
3366  if (!Company::IsValidID(cid)) {
3367  for (const Company *c : Company::Iterate()) {
3368  cid = c->index;
3369  break;
3370  }
3371  }
3372 
3373  SetDParam(0, cid);
3374 
3375  /* Insert current date */
3377  case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
3378  case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
3379  case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
3380  default: NOT_REACHED();
3381  }
3382  SetDParam(2, _date);
3383 
3384  /* Get the correct string (special string for when there's not company) */
3385  GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
3386  SanitizeFilename(buf);
3387 }
3388 
3394 {
3396 }
3397 
3405 {
3406  if (aft == FT_INVALID || aft == FT_NONE) {
3407  this->file_op = SLO_INVALID;
3408  this->detail_ftype = DFT_INVALID;
3409  this->abstract_ftype = FT_INVALID;
3410  return;
3411  }
3412 
3413  this->file_op = fop;
3414  this->detail_ftype = dft;
3415  this->abstract_ftype = aft;
3416 }
3417 
3422 void FileToSaveLoad::SetName(const char *name)
3423 {
3424  this->name = name;
3425 }
3426 
3431 void FileToSaveLoad::SetTitle(const char *title)
3432 {
3433  strecpy(this->title, title, lastof(this->title));
3434 }
3435 
3437 {
3438  assert(this->load_description.has_value());
3439  return *this->load_description;
3440 }
FileToSaveLoad::title
char title[255]
Internal name of the game.
Definition: saveload.h:358
SL_NULL
@ SL_NULL
Save null-bytes and load to nowhere.
Definition: saveload.h:647
ZlibLoadFilter::fread_buf
byte fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
Definition: saveload.cpp:2558
SlLoadChunks
static void SlLoadChunks()
Load all chunks.
Definition: saveload.cpp:2296
SlCalcTableHeader
static size_t SlCalcTableHeader(const SaveLoadTable &slt)
Calculate the size of the table header.
Definition: saveload.cpp:1565
ResetSaveloadData
static void ResetSaveloadData()
Clear temporary data that is passed between various saveload phases.
Definition: saveload.cpp:2889
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:1657
SlStorageHelper::SlSaveLoad
static void SlSaveLoad(void *storage, VarType conv, SaveLoadType cmd=SL_VAR)
Internal templated helper to save/load a list-like type.
Definition: saveload.cpp:1396
SetMouseCursorBusy
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1830
SaveLoad::version_to
SaveLoadVersion version_to
Save/load the variable before this savegame version.
Definition: saveload.h:659
SaveLoadFormat::init_write
SaveFilter *(* init_write)(SaveFilter *chain, byte compression)
Constructor for the save filter.
Definition: saveload.cpp:2798
FileWriter::FileWriter
FileWriter(FILE *file)
Create the file writer, so it writes to a specific file.
Definition: saveload.cpp:2387
SlIsObjectValidInSavegame
static bool SlIsObjectValidInSavegame(const SaveLoad &sld)
Are we going to save this object or not?
Definition: saveload.cpp:1555
LZMASaveFilter::lzma
lzma_stream lzma
Stream state that we are writing to.
Definition: saveload.cpp:2727
SLV_69
@ SLV_69
69 10319
Definition: saveload.h:129
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:3218
LZO_BUFFER_SIZE
static const uint LZO_BUFFER_SIZE
Buffer size for the LZO compressor.
Definition: saveload.cpp:2423
REF_ORDER
@ REF_ORDER
Load/save a reference to an order.
Definition: saveload.h:535
SaveLoadType
SaveLoadType
Type of data saved.
Definition: saveload.h:632
SlDeque
static void SlDeque(void *deque, VarType conv)
Save/load a std::deque.
Definition: saveload.cpp:1496
SLV_169
@ SLV_169
169 23816
Definition: saveload.h:249
SlLoadChunk
static void SlLoadChunk(const ChunkHandler &ch)
Load a chunk of data (eg vehicles, stations, etc.)
Definition: saveload.cpp:2139
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:2944
ZlibLoadFilter::~ZlibLoadFilter
~ZlibLoadFilter()
Clean everything up.
Definition: saveload.cpp:2571
Pool::PoolItem<&_orderlist_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
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:2800
SlArray
static void SlArray(void *array, size_t length, VarType conv)
Save/Load the length of the array followed by the array of SL_VAR elements.
Definition: saveload.cpp:1181
SGT_OTTD
@ SGT_OTTD
OTTD savegame.
Definition: saveload.h:371
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
_save_thread
static std::thread _save_thread
The thread we're using to compress and write a savegame.
Definition: saveload.cpp:391
SLE_VAR_STR
@ SLE_VAR_STR
string pointer
Definition: saveload.h:589
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:2691
GetVarFileType
static VarType GetVarFileType(VarType type)
Get the FileType of a setting.
Definition: saveload.h:1074
LZMASaveFilter::LZMASaveFilter
LZMASaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2734
NoCompLoadFilter
Filter without any compression.
Definition: saveload.cpp:2514
_sl_minor_version
byte _sl_minor_version
the minor savegame version, DO NOT USE!
Definition: saveload.cpp:67
GLAT_LOAD
@ GLAT_LOAD
Game loaded.
Definition: gamelog.h:18
LZMALoadFilter::fread_buf
byte fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
Definition: saveload.cpp:2685
SLA_SAVE
@ SLA_SAVE
saving
Definition: saveload.cpp:74
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:23
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
SaveLoadFormat::init_load
LoadFilter *(* init_load)(LoadFilter *chain)
Constructor for the load filter.
Definition: saveload.cpp:2797
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:538
DoExitSave
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
Definition: saveload.cpp:3350
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:400
NoCompLoadFilter::NoCompLoadFilter
NoCompLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2521
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:137
NoCompSaveFilter::NoCompSaveFilter
NoCompSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2536
SL_STR
@ SL_STR
Save/load a string.
Definition: saveload.h:637
REF_ROADSTOPS
@ REF_ROADSTOPS
Load/save a reference to a bus/truck stop.
Definition: saveload.h:540
FileToSaveLoad::SetTitle
void SetTitle(const char *title)
Set the title of the file.
Definition: saveload.cpp:3431
SLE_FILE_END
@ SLE_FILE_END
Used to mark end-of-header in tables.
Definition: saveload.h:560
FileReader::Read
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2362
Station
Station data structure.
Definition: station_base.h:447
SlSkipArray
void SlSkipArray()
Skip an array or sparse array.
Definition: saveload.cpp:711
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:3240
_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:57
SaveLoadOperation
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:47
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1151
FileToSaveLoad::name
std::string name
Name of the file.
Definition: saveload.h:357
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:2342
SaveLoad::size
size_t size
The sizeof size.
Definition: saveload.h:660
_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:2431
AfterLoadGame
bool AfterLoadGame()
Perform a (large) amount of savegame conversion magic in order to load older savegames and to fill th...
Definition: afterload.cpp:545
SLF_ALLOW_NEWLINE
@ SLF_ALLOW_NEWLINE
Allow new lines in the strings.
Definition: saveload.h:626
ZlibLoadFilter::ZlibLoadFilter
ZlibLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2564
SlSaveChunks
static void SlSaveChunks()
Save all chunks.
Definition: saveload.cpp:2273
SLA_LOAD_CHECK
@ SLA_LOAD_CHECK
partial loading into _load_check_data
Definition: saveload.cpp:77
SLO_CHECK
@ SLO_CHECK
Load file for checking and/or preview.
Definition: fileio_type.h:48
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:798
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
MemoryDumper::MemoryDumper
MemoryDumper()
Initialise our variables.
Definition: saveload.cpp:137
_do_autosave
bool _do_autosave
are we doing an autosave at the moment?
Definition: saveload.cpp:69
LZOSaveFilter::LZOSaveFilter
LZOSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2479
MemoryDumper::GetSize
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:187
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:353
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:3422
SaveLoadFormat::max_compression
byte max_compression
the maximum compression level of this format
Definition: saveload.cpp:2802
SLE_VAR_NULL
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition: saveload.h:587
LZOLoadFilter::Read
size_t Read(byte *buf, size_t ssize) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2436
GetSavegameFileType
static uint8 GetSavegameFileType(const SaveLoad &sld)
Return the type as saved/loaded inside the savegame.
Definition: saveload.cpp:584
SaveLoadHandler::GetLoadDescription
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Definition: saveload.cpp:3436
ChunkHandler::type
ChunkType type
Type of the chunk.
Definition: saveload.h:408
SaveLoad::length
uint16 length
(Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
Definition: saveload.h:657
ReadBuffer
A buffer for reading (and buffering) savegame data.
Definition: saveload.cpp:90
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:52
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:307
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:73
SaveLoadParams::reader
ReadBuffer * reader
Savegame reading buffer.
Definition: saveload.cpp:207
ZlibLoadFilter
Filter using Zlib compression.
Definition: saveload.cpp:2554
LoadWithFilter
SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
Load the game using a (reader) filter.
Definition: saveload.cpp:3220
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:221
SaveLoad::conv
VarType conv
Type of the variable to be saved; this field combines both FileVarType and MemVarType.
Definition: saveload.h:656
LZMASaveFilter::WriteLoop
void WriteLoop(byte *p, size_t len, lzma_action action)
Helper loop for writing the data.
Definition: saveload.cpp:2751
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:643
SaveLoadFormat::default_compression
byte default_compression
the default compression level of this format
Definition: saveload.cpp:2801
SaveLoadAction
SaveLoadAction
What are we currently doing?
Definition: saveload.cpp:72
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:205
SaveLoadHandler
Handler for saving/loading an object to/from disk.
Definition: saveload.h:455
SlSkipHandler::Load
void Load(void *object) const override
Load the object from disk.
Definition: saveload.cpp:1861
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:196
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:955
NL_NONE
@ NL_NONE
not working in NeedLength mode
Definition: saveload.cpp:81
AsyncSaveFinishProc
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition: saveload.cpp:389
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:723
NoCompLoadFilter::Read
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2525
ZlibSaveFilter::z
z_stream z
Stream state we are writing to.
Definition: saveload.cpp:2599
SlReadSimpleGamma
static uint SlReadSimpleGamma()
Read in the header descriptor of an object or an array.
Definition: saveload.cpp:484
SLE_FILE_TYPE_MASK
@ SLE_FILE_TYPE_MASK
Mask to get the file-type (and not any flags).
Definition: saveload.h:574
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:383
saveload_filter.h
ReadBuffer::reader
LoadFilter * reader
The filter used to actually read.
Definition: saveload.cpp:94
MemoryDumper::buf
byte * buf
Buffer we're going to write to.
Definition: saveload.cpp:133
SlGlobList
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:2083
NoCompSaveFilter
Filter without any compression.
Definition: saveload.cpp:2530
REF_STATION
@ REF_STATION
Load/save a reference to a station.
Definition: saveload.h:537
LZMALoadFilter
Filter without any compression.
Definition: saveload.cpp:2683
SLE_VAR_STRB
@ SLE_VAR_STRB
string (with pre-allocated buffer)
Definition: saveload.h:588
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:620
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:1227
SaveLoadFormat
The format for a reader/writer type of a savegame.
Definition: saveload.cpp:2791
FileToSaveLoad::abstract_ftype
AbstractFileType abstract_ftype
Abstract type of file (scenario, heightmap, etc).
Definition: saveload.h:356
ReadBuffer::buf
byte buf[MEMORY_CHUNK_SIZE]
Buffer we're going to read from.
Definition: saveload.cpp:91
SaveLoadParams::block_mode
byte block_mode
???
Definition: saveload.cpp:197
FileWriter::file
FILE * file
The file to write to.
Definition: saveload.cpp:2381
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:47
SL_SAVEBYTE
@ SL_SAVEBYTE
Save (but not load) a byte.
Definition: saveload.h:646
SaveFileDone
static void SaveFileDone()
Update the gui accordingly when saving is done and release locks on saveload.
Definition: saveload.cpp:2930
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:625
SlSkipHandler
Handler that is assigned when there is a struct read in the savegame which is not known to the code.
Definition: saveload.cpp:1855
MemoryDumper::WriteByte
void WriteByte(byte b)
Write a single byte into the dumper.
Definition: saveload.cpp:152
SLE_FILE_HAS_LENGTH_FIELD
@ SLE_FILE_HAS_LENGTH_FIELD
Bit stored in savegame to indicate field has a length field for each entry.
Definition: saveload.h:575
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:2961
IsGoodGRFConfigList
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
Definition: newgrf_config.cpp:513
_sl_version
SaveLoadVersion _sl_version
the major savegame version identifier
Definition: saveload.cpp:66
SlFixPointers
static void SlFixPointers()
Fix all pointers (convert index -> pointer)
Definition: saveload.cpp:2326
SavegameType
SavegameType
Types of save games.
Definition: saveload.h:367
_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:2598
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
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:1260
NeedLength
NeedLength
Definition: saveload.cpp:80
SlSaveLoadRef
void SlSaveLoadRef(void *ptr, VarType conv)
Handle conversion for references.
Definition: saveload.cpp:1336
CH_TYPE_MASK
@ CH_TYPE_MASK
All ChunkType values have to be within this mask.
Definition: saveload.h:401
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
NL_CALCLENGTH
@ NL_CALCLENGTH
need to calculate the length
Definition: saveload.cpp:83
SaveFilter::Finish
virtual void Finish()
Prepare everything to finish writing the savegame.
Definition: saveload_filter.h:88
ChunkHandler::Save
virtual void Save() const
Save the chunk.
Definition: saveload.h:418
CH_READONLY
@ CH_READONLY
Chunk is never saved.
Definition: saveload.h:402
SlCalcDequeLen
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Return the size in bytes of a std::deque.
Definition: saveload.cpp:1475
SlWriteByte
void SlWriteByte(byte b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:433
ChunkHandler::id
uint32 id
Unique ID (4 letters).
Definition: saveload.h:407
SL_VAR
@ SL_VAR
Save/load a variable.
Definition: saveload.h:633
StrMakeValidInPlace
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:255
_savegame_format
std::string _savegame_format
how to compress savegames
Definition: saveload.cpp:68
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:245
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:2772
NL_WANTLENGTH
@ NL_WANTLENGTH
writing length and data
Definition: saveload.cpp:82
GetSaveLoadErrorString
const char * GetSaveLoadErrorString()
Get the string representation of the error message.
Definition: saveload.cpp:2950
ZlibLoadFilter::Read
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2576
SlCopyInternal
static void SlCopyInternal(void *object, size_t length, VarType conv)
Internal function to save/Load a list of SL_VARs.
Definition: saveload.cpp:1102
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:62
SlLoadCheckChunks
static void SlLoadCheckChunks()
Load all chunks for savegame checking.
Definition: saveload.cpp:2311
ZlibSaveFilter::ZlibSaveFilter
ZlibSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2606
CopyFromOldName
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:60
SlVector
static void SlVector(void *vector, VarType conv)
Save/load a std::vector.
Definition: saveload.cpp:1538
FileReader::file
FILE * file
The file to read from.
Definition: saveload.cpp:2341
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:349
SaveLoad::cmd
SaveLoadType cmd
The action to take with the saved/loaded type, All types need different action.
Definition: saveload.h:655
SLV_END_PATCHPACKS
@ SLV_END_PATCHPACKS
286 Last known patchpack to use a version just above ours.
Definition: saveload.h:326
SLRefType
SLRefType
Type of reference (SLE_REF, SLE_CONDREF).
Definition: saveload.h:534
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:46
SlSaveLoadConv
static void SlSaveLoadConv(void *ptr, VarType conv)
Handle all conversion and typechecking of variables here.
Definition: saveload.cpp:854
ZlibSaveFilter::WriteLoop
void WriteLoop(byte *p, size_t len, int mode)
Helper loop for writing the data.
Definition: saveload.cpp:2624
FileWriter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2400
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
GetSavegameFormat
static const SaveLoadFormat * GetSavegameFormat(const std::string &full_name, byte *compression_level)
Return the savegameformat of the game.
Definition: saveload.cpp:2840
SaveLoadParams::lf
LoadFilter * lf
Filter to read the savegame from.
Definition: saveload.cpp:208
NoCompSaveFilter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2540
_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:2972
SaveLoadFormat::name
const char * name
name of the compressor/decompressor (debug-only)
Definition: saveload.cpp:2794
SlCalcRefListLen
static size_t SlCalcRefListLen(const void *list, VarType conv)
Return the size in bytes of a list.
Definition: saveload.cpp:1448
GUISettings::keep_all_autosave
bool keep_all_autosave
name the autosave in a different way
Definition: settings_type.h:134
ChunkHandlers
static const std::vector< ChunkHandlerRef > & ChunkHandlers()
Definition: saveload.cpp:219
SL_ARR
@ SL_ARR
Save/load a fixed-size array of SL_VAR elements.
Definition: saveload.h:640
SLE_FILE_STRINGID
@ SLE_FILE_STRINGID
StringID offset into strings-array.
Definition: saveload.h:569
REF_STORAGE
@ REF_STORAGE
Load/save a reference to a persistent storage.
Definition: saveload.h:544
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:34
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
SLA_PTRS
@ SLA_PTRS
fixing pointers
Definition: saveload.cpp:75
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
MemoryDumper::Flush
void Flush(SaveFilter *writer)
Flush this dumper into a writer.
Definition: saveload.cpp:168
SaveFileStart
static void SaveFileStart()
Update the gui accordingly when starting saving and set locks on saveload.
Definition: saveload.cpp:2919
REF_ENGINE_RENEWS
@ REF_ENGINE_RENEWS
Load/save a reference to an engine renewal (autoreplace).
Definition: saveload.h:541
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
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:536
SL_REF
@ SL_REF
Save/load a reference.
Definition: saveload.h:634
DoAutoOrNetsave
void DoAutoOrNetsave(FiosNumberedSaveName &counter)
Create an autosave or netsave.
Definition: saveload.cpp:3331
REF_CARGO_PACKET
@ REF_CARGO_PACKET
Load/save a reference to a cargo packet.
Definition: saveload.h:542
SL_STRUCT
@ SL_STRUCT
Save/load a struct.
Definition: saveload.h:635
DoSave
static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
Actually perform the saving of the savegame.
Definition: saveload.cpp:3030
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:2684
ReadBuffer::bufp
byte * bufp
Location we're at reading the buffer.
Definition: saveload.cpp:92
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:2093
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:539
SaveLoadParams::error_str
StringID error_str
the translatable error message to show
Definition: saveload.cpp:210
ChunkHandler::Load
virtual void Load() const =0
Load the chunk.
ZlibSaveFilter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2653
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:390
SlSkipHandler::GetDescription
virtual SaveLoadTable GetDescription() const override
Get the description of the fields in the savegame.
Definition: saveload.cpp:1874
SlString
static void SlString(void *ptr, size_t length, VarType conv)
Save/Load a string.
Definition: saveload.cpp:969
GamelogStopAction
void GamelogStopAction()
Stops logging of any changes.
Definition: gamelog.cpp:78
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
SlGetStructListLength
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition: saveload.cpp:1825
SaveLoad::handler
std::shared_ptr< SaveLoadHandler > handler
Custom handler for Save/Load procs.
Definition: saveload.h:663
LoadCheckData::error
StringID error
Error message from loading. INVALID_STRING_ID if no error.
Definition: fios.h:33
GenerateDefaultSaveName
void GenerateDefaultSaveName(char *buf, const char *last)
Fill the buffer with the default name for a savegame or screenshot.
Definition: saveload.cpp:3360
WriteValue
void WriteValue(void *ptr, VarType conv, int64 val)
Write the value of a setting.
Definition: saveload.cpp:828
SL_VECTOR
@ SL_VECTOR
Save/load a vector of SL_VAR elements.
Definition: saveload.h:642
SlCalcVectorLen
static size_t SlCalcVectorLen(const void *vector, VarType conv)
Return the size in bytes of a std::vector.
Definition: saveload.cpp:1517
MemoryDumper::bufe
byte * bufe
End of the buffer we write to.
Definition: saveload.cpp:134
SaveLoadParams::expect_table_header
bool expect_table_header
In the case of a table, if the header is saved/loaded.
Definition: saveload.cpp:202
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:342
SlLoadCheckChunk
static void SlLoadCheckChunk(const ChunkHandler &ch)
Load a chunk of data for checking savegames.
Definition: saveload.cpp:2189
SlError
void NORETURN SlError(StringID string, const char *extra_msg)
Error handler.
Definition: saveload.cpp:332
REF_LINK_GRAPH_JOB
@ REF_LINK_GRAPH_JOB
Load/save a reference to a link graph job.
Definition: saveload.h:546
SlCalcNetStringLen
static size_t SlCalcNetStringLen(const char *ptr, size_t length)
Calculate the net length of a string.
Definition: saveload.cpp:911
ZlibLoadFilter::z
z_stream z
Stream state we are reading from.
Definition: saveload.cpp:2557
LZMALoadFilter::~LZMALoadFilter
~LZMALoadFilter()
Clean everything up.
Definition: saveload.cpp:2698
ReadBuffer::ReadBuffer
ReadBuffer(LoadFilter *reader)
Initialise our variables.
Definition: saveload.cpp:101
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:217
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:196
SlErrorCorruptFmt
void NORETURN SlErrorCorruptFmt(const char *format,...)
Issue an SlErrorCorrupt with a format string.
Definition: saveload.cpp:376
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:386
SlStorageHelper::SlCalcLen
static size_t SlCalcLen(const void *storage, VarType conv, SaveLoadType cmd=SL_VAR)
Internal templated helper to return the size in bytes of a list-like type.
Definition: saveload.cpp:1369
GUISettings::threaded_saves
bool threaded_saves
should we do threaded saves?
Definition: settings_type.h:133
FT_NONE
@ FT_NONE
nothing to do
Definition: fileio_type.h:17
FiosNumberedSaveName
A savegame name automatically numbered.
Definition: fios.h:131
REF_ORDERLIST
@ REF_ORDERLIST
Load/save a reference to an orderlist.
Definition: saveload.h:543
SaveLoadParams
The saveload struct, containing reader-writer functions, buffer, version, etc.
Definition: saveload.cpp:194
LZMALoadFilter::Read
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2703
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:792
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:1170
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
SlStorageHelper
Template class to help with list-like types.
Definition: saveload.cpp:1360
DoLoad
static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
Actually perform the loading of a "non-old" savegame.
Definition: saveload.cpp:3079
_saveload_formats
static const SaveLoadFormat _saveload_formats[]
The different saveload formats known/understood by OpenTTD.
Definition: saveload.cpp:2804
FileReader::Reset
void Reset() override
Reset this filter to read from the beginning of the file.
Definition: saveload.cpp:2370
SL_REFLIST
@ SL_REFLIST
Save/load a list of SL_REF elements.
Definition: saveload.h:643
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:364
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:397
GetVarMemType
static VarType GetVarMemType(VarType type)
Get the NumberType of a setting.
Definition: saveload.h:1063
SaveLoadParams::action
SaveLoadAction action
are we doing a save or a load atm.
Definition: saveload.cpp:195
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
_file_to_saveload
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:63
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:253
SanitizeFilename
void SanitizeFilename(char *filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1089
SaveWithFilter
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
Save the game using a (writer) filter.
Definition: saveload.cpp:3062
SlGetGammaLength
static uint SlGetGammaLength(size_t i)
Return how many bytes used to encode a gamma value.
Definition: saveload.cpp:551
SlReadByte
byte SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:424
ZlibSaveFilter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2658
LZOSaveFilter
Filter using LZO compression.
Definition: saveload.cpp:2473
SaveLoadParams::obj_len
size_t obj_len
the length of the current object we are busy with
Definition: saveload.cpp:200
SlSkipHandler::LoadCheck
void LoadCheck(void *object) const override
Similar to load, but used only to validate savegames.
Definition: saveload.cpp:1869
LZMASaveFilter::~LZMASaveFilter
~LZMASaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2740
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
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:526
SaveLoad::version_from
SaveLoadVersion version_from
Save/load the variable starting from this savegame version.
Definition: saveload.h:658
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:346
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:590
SaveLoadParams::saveinprogress
bool saveinprogress
Whether there is currently a save in progress.
Definition: saveload.cpp:214
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:213
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
SlSkipHandler::Save
void Save(void *object) const override
Save the object to disk.
Definition: saveload.cpp:1856
SlRefList
static void SlRefList(void *list, VarType conv)
Save/Load a list.
Definition: saveload.cpp:1458
ReadBuffer::read
size_t read
The amount of read bytes so far from the filter.
Definition: saveload.cpp:95
_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:545
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:76
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SlStdString
static void SlStdString(void *ptr, VarType conv)
Save/Load a std::string.
Definition: saveload.cpp:1048
SaveLoadFormat::tag
uint32 tag
the 4-letter tag by which it is identified in the savegame
Definition: saveload.cpp:2795
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:378
SaveLoadParams::error
bool error
did an error occur or not
Definition: saveload.cpp:198
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:43
SL_DEQUE
@ SL_DEQUE
Save/load a deque of SL_VAR elements.
Definition: saveload.h:641
SaveLoadParams::dumper
MemoryDumper * dumper
Memory dumper to write the savegame to.
Definition: saveload.cpp:204
FileReader::~FileReader
~FileReader()
Make sure everything is cleaned up.
Definition: saveload.cpp:2353
SLV_SAVELOAD_LIST_LENGTH
@ SLV_SAVELOAD_LIST_LENGTH
293 PR#9374 Consistency in list length with SL_STRUCT / SL_STRUCTLIST / SL_DEQUE / SL_REFLIST.
Definition: saveload.h:335
GetVariableAddress
static void * GetVariableAddress(const void *object, const SaveLoad &sld)
Get the address of the variable.
Definition: saveload.h:1094
GetAbstractFileType
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition: fileio_type.h:90
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
SlCopyBytes
static void SlCopyBytes(void *ptr, size_t length)
Save/Load bytes.
Definition: saveload.cpp:775
LZOLoadFilter
Filter using LZO compression.
Definition: saveload.cpp:2426
MEMORY_CHUNK_SIZE
static const size_t MEMORY_CHUNK_SIZE
Save in chunks of 128 KiB.
Definition: saveload.cpp:87
ProcessAsyncSaveFinish
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition: saveload.cpp:408
MemoryDumper::blocks
std::vector< byte * > blocks
Buffer with blocks of allocated memory.
Definition: saveload.cpp:132
SaveLoadParams::extra_msg
char * extra_msg
the error message
Definition: saveload.cpp:211
FileReader
Yes, simply reading from a file.
Definition: saveload.cpp:2340
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:2289
_ttdp_version
uint32 _ttdp_version
version of TTDP savegame (if applicable)
Definition: saveload.cpp:65
ReadValue
int64 ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:804
SaveLoadParams::last_array_index
int last_array_index
in the case of an array, the current and last positions
Definition: saveload.cpp:201
SlCalcRefLen
static size_t SlCalcRefLen()
Return the size in bytes of a reference (pointer)
Definition: saveload.cpp:653
LZMASaveFilter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2777
_async_save_finish
static std::atomic< AsyncSaveFinishProc > _async_save_finish
Callback to call when the savegame loading is finished.
Definition: saveload.cpp:390
SL_ERROR
@ SL_ERROR
error that was caught before internal structures were modified
Definition: saveload.h:348
FileToSaveLoad::detail_ftype
DetailedFileType detail_ftype
Concrete file type (PNG, BMP, old save, etc).
Definition: saveload.h:355
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:326
WC_STATUS_BAR
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:56
ReadBuffer::GetSize
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:123
RoadStop
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
FiosNumberedSaveName::Extension
std::string Extension()
Generate an extension for a savegame name.
Definition: fios.cpp:801
SaveLoad::name
std::string name
Name of this field (optional, used for tables).
Definition: saveload.h:654
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1838
ZlibSaveFilter::~ZlibSaveFilter
~ZlibSaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2613
ChunkHandler::LoadCheck
virtual void LoadCheck(size_t len=0) const
Load the chunk for game preview.
Definition: saveload.cpp:2116
LZMASaveFilter
Filter using LZMA compression.
Definition: saveload.cpp:2726
SLV_START_PATCHPACKS
@ SLV_START_PATCHPACKS
220 First known patchpack to use a version just above ours.
Definition: saveload.h:325
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
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:460
SlSetStructListLength
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition: saveload.cpp:1809
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:926
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:41
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
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:3393
SlSkipHandler::GetCompatDescription
virtual SaveLoadCompatTable GetCompatDescription() const override
Get the pre-header description of the fields in the savegame.
Definition: saveload.cpp:1879
SaveLoad
SaveLoad type struct.
Definition: saveload.h:653
Company
Definition: company_base.h:115
LZOSaveFilter::Write
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2484
ClearSaveLoadState
static void ClearSaveLoadState()
Clear/free saveload state.
Definition: saveload.cpp:2899
LoadFilter::chain
LoadFilter * chain
Chained to the (savegame) filters.
Definition: saveload_filter.h:16
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:394
SL_OK
@ SL_OK
completed successfully
Definition: saveload.h:347
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
FiosNumberedSaveName::Filename
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition: fios.cpp:791
SL_STDSTR
@ SL_STDSTR
Save/load a std::string.
Definition: saveload.h:638
Order
Definition: order_base.h:33
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:1141
_lzma_init
static const lzma_stream _lzma_init
Have a copy of an initialised LZMA stream.
Definition: saveload.cpp:2680
SLE_VAR_NAME
@ SLE_VAR_NAME
old custom name to be converted to a char pointer
Definition: saveload.h:591
FileWriter::~FileWriter
~FileWriter()
Make sure everything is cleaned up.
Definition: saveload.cpp:2392
SlCalcObjLength
size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
Calculate the size of an object.
Definition: saveload.cpp:1594
_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:670
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:296
FileReader::FileReader
FileReader(FILE *file)
Create the file reader, so it reads from a specific file.
Definition: saveload.cpp:2348
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
SlSaveChunk
static void SlSaveChunk(const ChunkHandler &ch)
Save a chunk of data (eg.
Definition: saveload.cpp:2237
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:593
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:2408
FileToSaveLoad::file_op
SaveLoadOperation file_op
File operation to perform.
Definition: saveload.h:354
FileWriter
Yes, simply writing to a file.
Definition: saveload.cpp:2380
ReadBuffer::bufe
byte * bufe
End of the buffer we can read from.
Definition: saveload.cpp:93
MemoryDumper
Container for dumping the savegame (quickly) to memory.
Definition: saveload.cpp:131
SL_STRUCTLIST
@ SL_STRUCTLIST
Save/load a list of structs.
Definition: saveload.h:644
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