OpenTTD Source  20240917-master-g9ab0a47812
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 "../timer/timer_game_economy.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 #ifdef __EMSCRIPTEN__
47 # include <emscripten.h>
48 #endif
49 
50 #include "table/strings.h"
51 
52 #include "saveload_internal.h"
53 #include "saveload_filter.h"
54 
55 #include "../safeguards.h"
56 
58 
61 
62 uint32_t _ttdp_version;
65 std::string _savegame_format;
67 
75 };
76 
77 enum NeedLength {
78  NL_NONE = 0,
81 };
82 
84 static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
85 
87 struct ReadBuffer {
89  uint8_t *bufp;
90  uint8_t *bufe;
91  std::shared_ptr<LoadFilter> reader;
92  size_t read;
93 
98  ReadBuffer(std::shared_ptr<LoadFilter> reader) : bufp(nullptr), bufe(nullptr), reader(reader), read(0)
99  {
100  }
101 
102  inline uint8_t ReadByte()
103  {
104  if (this->bufp == this->bufe) {
105  size_t len = this->reader->Read(this->buf, lengthof(this->buf));
106  if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
107 
108  this->read += len;
109  this->bufp = this->buf;
110  this->bufe = this->buf + len;
111  }
112 
113  return *this->bufp++;
114  }
115 
120  size_t GetSize() const
121  {
122  return this->read - (this->bufe - this->bufp);
123  }
124 };
125 
126 
128 struct MemoryDumper {
129  std::vector<std::unique_ptr<uint8_t[]>> blocks{};
130  uint8_t *buf = nullptr;
131  uint8_t *bufe = nullptr;
132 
137  inline void WriteByte(uint8_t b)
138  {
139  /* Are we at the end of this chunk? */
140  if (this->buf == this->bufe) {
141  this->buf = this->blocks.emplace_back(std::make_unique<uint8_t[]>(MEMORY_CHUNK_SIZE)).get();
142  this->bufe = this->buf + MEMORY_CHUNK_SIZE;
143  }
144 
145  *this->buf++ = b;
146  }
147 
152  void Flush(std::shared_ptr<SaveFilter> writer)
153  {
154  uint i = 0;
155  size_t t = this->GetSize();
156 
157  while (t > 0) {
158  size_t to_write = std::min(MEMORY_CHUNK_SIZE, t);
159 
160  writer->Write(this->blocks[i++].get(), to_write);
161  t -= to_write;
162  }
163 
164  writer->Finish();
165  }
166 
171  size_t GetSize() const
172  {
173  return this->blocks.size() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
174  }
175 };
176 
181  uint8_t block_mode;
182  bool error;
183 
184  size_t obj_len;
185  int array_index, last_array_index;
187 
188  std::unique_ptr<MemoryDumper> dumper;
189  std::shared_ptr<SaveFilter> sf;
190 
191  std::unique_ptr<ReadBuffer> reader;
192  std::shared_ptr<LoadFilter> lf;
193 
195  std::string extra_msg;
196 
198 };
199 
201 
202 static const std::vector<ChunkHandlerRef> &ChunkHandlers()
203 {
204  /* These define the chunks */
205  extern const ChunkHandlerTable _gamelog_chunk_handlers;
206  extern const ChunkHandlerTable _map_chunk_handlers;
207  extern const ChunkHandlerTable _misc_chunk_handlers;
208  extern const ChunkHandlerTable _name_chunk_handlers;
209  extern const ChunkHandlerTable _cheat_chunk_handlers;
210  extern const ChunkHandlerTable _setting_chunk_handlers;
211  extern const ChunkHandlerTable _company_chunk_handlers;
212  extern const ChunkHandlerTable _engine_chunk_handlers;
213  extern const ChunkHandlerTable _veh_chunk_handlers;
214  extern const ChunkHandlerTable _waypoint_chunk_handlers;
215  extern const ChunkHandlerTable _depot_chunk_handlers;
216  extern const ChunkHandlerTable _order_chunk_handlers;
217  extern const ChunkHandlerTable _town_chunk_handlers;
218  extern const ChunkHandlerTable _sign_chunk_handlers;
219  extern const ChunkHandlerTable _station_chunk_handlers;
220  extern const ChunkHandlerTable _industry_chunk_handlers;
221  extern const ChunkHandlerTable _economy_chunk_handlers;
222  extern const ChunkHandlerTable _subsidy_chunk_handlers;
223  extern const ChunkHandlerTable _cargomonitor_chunk_handlers;
224  extern const ChunkHandlerTable _goal_chunk_handlers;
225  extern const ChunkHandlerTable _story_page_chunk_handlers;
226  extern const ChunkHandlerTable _league_chunk_handlers;
227  extern const ChunkHandlerTable _ai_chunk_handlers;
228  extern const ChunkHandlerTable _game_chunk_handlers;
229  extern const ChunkHandlerTable _animated_tile_chunk_handlers;
230  extern const ChunkHandlerTable _newgrf_chunk_handlers;
231  extern const ChunkHandlerTable _group_chunk_handlers;
232  extern const ChunkHandlerTable _cargopacket_chunk_handlers;
233  extern const ChunkHandlerTable _autoreplace_chunk_handlers;
234  extern const ChunkHandlerTable _labelmaps_chunk_handlers;
235  extern const ChunkHandlerTable _linkgraph_chunk_handlers;
236  extern const ChunkHandlerTable _airport_chunk_handlers;
237  extern const ChunkHandlerTable _object_chunk_handlers;
238  extern const ChunkHandlerTable _persistent_storage_chunk_handlers;
239  extern const ChunkHandlerTable _water_region_chunk_handlers;
240  extern const ChunkHandlerTable _randomizer_chunk_handlers;
241 
243  static const ChunkHandlerTable _chunk_handler_tables[] = {
244  _gamelog_chunk_handlers,
245  _map_chunk_handlers,
246  _misc_chunk_handlers,
247  _name_chunk_handlers,
248  _cheat_chunk_handlers,
249  _setting_chunk_handlers,
250  _veh_chunk_handlers,
251  _waypoint_chunk_handlers,
252  _depot_chunk_handlers,
253  _order_chunk_handlers,
254  _industry_chunk_handlers,
255  _economy_chunk_handlers,
256  _subsidy_chunk_handlers,
257  _cargomonitor_chunk_handlers,
258  _goal_chunk_handlers,
259  _story_page_chunk_handlers,
260  _league_chunk_handlers,
261  _engine_chunk_handlers,
262  _town_chunk_handlers,
263  _sign_chunk_handlers,
264  _station_chunk_handlers,
265  _company_chunk_handlers,
266  _ai_chunk_handlers,
267  _game_chunk_handlers,
268  _animated_tile_chunk_handlers,
269  _newgrf_chunk_handlers,
270  _group_chunk_handlers,
271  _cargopacket_chunk_handlers,
272  _autoreplace_chunk_handlers,
273  _labelmaps_chunk_handlers,
274  _linkgraph_chunk_handlers,
275  _airport_chunk_handlers,
276  _object_chunk_handlers,
277  _persistent_storage_chunk_handlers,
278  _water_region_chunk_handlers,
279  _randomizer_chunk_handlers,
280  };
281 
282  static std::vector<ChunkHandlerRef> _chunk_handlers;
283 
284  if (_chunk_handlers.empty()) {
285  for (auto &chunk_handler_table : _chunk_handler_tables) {
286  for (auto &chunk_handler : chunk_handler_table) {
287  _chunk_handlers.push_back(chunk_handler);
288  }
289  }
290  }
291 
292  return _chunk_handlers;
293 }
294 
296 static void SlNullPointers()
297 {
298  _sl.action = SLA_NULL;
299 
300  /* We don't want any savegame conversion code to run
301  * during NULLing; especially those that try to get
302  * pointers from other pools. */
304 
305  for (const ChunkHandler &ch : ChunkHandlers()) {
306  Debug(sl, 3, "Nulling pointers for {}", ch.GetName());
307  ch.FixPointers();
308  }
309 
310  assert(_sl.action == SLA_NULL);
311 }
312 
321 [[noreturn]] void SlError(StringID string, const std::string &extra_msg)
322 {
323  /* Distinguish between loading into _load_check_data vs. normal save/load. */
324  if (_sl.action == SLA_LOAD_CHECK) {
325  _load_check_data.error = string;
326  _load_check_data.error_msg = extra_msg;
327  } else {
328  _sl.error_str = string;
329  _sl.extra_msg = extra_msg;
330  }
331 
332  /* We have to nullptr all pointers here; we might be in a state where
333  * the pointers are actually filled with indices, which means that
334  * when we access them during cleaning the pool dereferences of
335  * those indices will be made with segmentation faults as result. */
337 
338  /* Logging could be active. */
339  _gamelog.StopAnyAction();
340 
341  throw std::exception();
342 }
343 
351 [[noreturn]] void SlErrorCorrupt(const std::string &msg)
352 {
353  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
354 }
355 
356 
357 typedef void (*AsyncSaveFinishProc)();
358 static std::atomic<AsyncSaveFinishProc> _async_save_finish;
359 static std::thread _save_thread;
360 
366 {
367  if (_exit_game) return;
368  while (_async_save_finish.load(std::memory_order_acquire) != nullptr) CSleep(10);
369 
370  _async_save_finish.store(proc, std::memory_order_release);
371 }
372 
377 {
378  AsyncSaveFinishProc proc = _async_save_finish.exchange(nullptr, std::memory_order_acq_rel);
379  if (proc == nullptr) return;
380 
381  proc();
382 
383  if (_save_thread.joinable()) {
384  _save_thread.join();
385  }
386 }
387 
392 uint8_t SlReadByte()
393 {
394  return _sl.reader->ReadByte();
395 }
396 
401 void SlWriteByte(uint8_t b)
402 {
403  _sl.dumper->WriteByte(b);
404 }
405 
406 static inline int SlReadUint16()
407 {
408  int x = SlReadByte() << 8;
409  return x | SlReadByte();
410 }
411 
412 static inline uint32_t SlReadUint32()
413 {
414  uint32_t x = SlReadUint16() << 16;
415  return x | SlReadUint16();
416 }
417 
418 static inline uint64_t SlReadUint64()
419 {
420  uint32_t x = SlReadUint32();
421  uint32_t y = SlReadUint32();
422  return (uint64_t)x << 32 | y;
423 }
424 
425 static inline void SlWriteUint16(uint16_t v)
426 {
427  SlWriteByte(GB(v, 8, 8));
428  SlWriteByte(GB(v, 0, 8));
429 }
430 
431 static inline void SlWriteUint32(uint32_t v)
432 {
433  SlWriteUint16(GB(v, 16, 16));
434  SlWriteUint16(GB(v, 0, 16));
435 }
436 
437 static inline void SlWriteUint64(uint64_t x)
438 {
439  SlWriteUint32((uint32_t)(x >> 32));
440  SlWriteUint32((uint32_t)x);
441 }
442 
452 static uint SlReadSimpleGamma()
453 {
454  uint i = SlReadByte();
455  if (HasBit(i, 7)) {
456  i &= ~0x80;
457  if (HasBit(i, 6)) {
458  i &= ~0x40;
459  if (HasBit(i, 5)) {
460  i &= ~0x20;
461  if (HasBit(i, 4)) {
462  i &= ~0x10;
463  if (HasBit(i, 3)) {
464  SlErrorCorrupt("Unsupported gamma");
465  }
466  i = SlReadByte(); // 32 bits only.
467  }
468  i = (i << 8) | SlReadByte();
469  }
470  i = (i << 8) | SlReadByte();
471  }
472  i = (i << 8) | SlReadByte();
473  }
474  return i;
475 }
476 
494 static void SlWriteSimpleGamma(size_t i)
495 {
496  if (i >= (1 << 7)) {
497  if (i >= (1 << 14)) {
498  if (i >= (1 << 21)) {
499  if (i >= (1 << 28)) {
500  assert(i <= UINT32_MAX); // We can only support 32 bits for now.
501  SlWriteByte((uint8_t)(0xF0));
502  SlWriteByte((uint8_t)(i >> 24));
503  } else {
504  SlWriteByte((uint8_t)(0xE0 | (i >> 24)));
505  }
506  SlWriteByte((uint8_t)(i >> 16));
507  } else {
508  SlWriteByte((uint8_t)(0xC0 | (i >> 16)));
509  }
510  SlWriteByte((uint8_t)(i >> 8));
511  } else {
512  SlWriteByte((uint8_t)(0x80 | (i >> 8)));
513  }
514  }
515  SlWriteByte((uint8_t)i);
516 }
517 
519 static inline uint SlGetGammaLength(size_t i)
520 {
521  return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
522 }
523 
524 static inline uint SlReadSparseIndex()
525 {
526  return SlReadSimpleGamma();
527 }
528 
529 static inline void SlWriteSparseIndex(uint index)
530 {
531  SlWriteSimpleGamma(index);
532 }
533 
534 static inline uint SlReadArrayLength()
535 {
536  return SlReadSimpleGamma();
537 }
538 
539 static inline void SlWriteArrayLength(size_t length)
540 {
541  SlWriteSimpleGamma(length);
542 }
543 
544 static inline uint SlGetArrayLength(size_t length)
545 {
546  return SlGetGammaLength(length);
547 }
548 
552 static uint8_t GetSavegameFileType(const SaveLoad &sld)
553 {
554  switch (sld.cmd) {
555  case SL_VAR:
556  return GetVarFileType(sld.conv); break;
557 
558  case SL_STDSTR:
559  case SL_ARR:
560  case SL_VECTOR:
561  case SL_DEQUE:
562  return GetVarFileType(sld.conv) | SLE_FILE_HAS_LENGTH_FIELD; break;
563 
564  case SL_REF:
565  return IsSavegameVersionBefore(SLV_69) ? SLE_FILE_U16 : SLE_FILE_U32;
566 
567  case SL_REFLIST:
568  return (IsSavegameVersionBefore(SLV_69) ? SLE_FILE_U16 : SLE_FILE_U32) | SLE_FILE_HAS_LENGTH_FIELD;
569 
570  case SL_SAVEBYTE:
571  return SLE_FILE_U8;
572 
573  case SL_STRUCT:
574  case SL_STRUCTLIST:
575  return SLE_FILE_STRUCT | SLE_FILE_HAS_LENGTH_FIELD;
576 
577  default: NOT_REACHED();
578  }
579 }
580 
587 static inline uint SlCalcConvMemLen(VarType conv)
588 {
589  switch (GetVarMemType(conv)) {
590  case SLE_VAR_BL: return sizeof(bool);
591  case SLE_VAR_I8: return sizeof(int8_t);
592  case SLE_VAR_U8: return sizeof(uint8_t);
593  case SLE_VAR_I16: return sizeof(int16_t);
594  case SLE_VAR_U16: return sizeof(uint16_t);
595  case SLE_VAR_I32: return sizeof(int32_t);
596  case SLE_VAR_U32: return sizeof(uint32_t);
597  case SLE_VAR_I64: return sizeof(int64_t);
598  case SLE_VAR_U64: return sizeof(uint64_t);
599  case SLE_VAR_NULL: return 0;
600 
601  case SLE_VAR_STR:
602  case SLE_VAR_STRQ:
603  return SlReadArrayLength();
604 
605  case SLE_VAR_NAME:
606  default:
607  NOT_REACHED();
608  }
609 }
610 
617 static inline uint8_t SlCalcConvFileLen(VarType conv)
618 {
619  switch (GetVarFileType(conv)) {
620  case SLE_FILE_END: return 0;
621  case SLE_FILE_I8: return sizeof(int8_t);
622  case SLE_FILE_U8: return sizeof(uint8_t);
623  case SLE_FILE_I16: return sizeof(int16_t);
624  case SLE_FILE_U16: return sizeof(uint16_t);
625  case SLE_FILE_I32: return sizeof(int32_t);
626  case SLE_FILE_U32: return sizeof(uint32_t);
627  case SLE_FILE_I64: return sizeof(int64_t);
628  case SLE_FILE_U64: return sizeof(uint64_t);
629  case SLE_FILE_STRINGID: return sizeof(uint16_t);
630 
631  case SLE_FILE_STRING:
632  return SlReadArrayLength();
633 
634  case SLE_FILE_STRUCT:
635  default:
636  NOT_REACHED();
637  }
638 }
639 
641 static inline size_t SlCalcRefLen()
642 {
643  return IsSavegameVersionBefore(SLV_69) ? 2 : 4;
644 }
645 
646 void SlSetArrayIndex(uint index)
647 {
649  _sl.array_index = index;
650 }
651 
652 static size_t _next_offs;
653 
659 {
660  /* After reading in the whole array inside the loop
661  * we must have read in all the data, so we must be at end of current block. */
662  if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) {
663  SlErrorCorruptFmt("Invalid chunk size iterating array - expected to be at position {}, actually at {}", _next_offs, _sl.reader->GetSize());
664  }
665 
666  for (;;) {
667  uint length = SlReadArrayLength();
668  if (length == 0) {
669  assert(!_sl.expect_table_header);
670  _next_offs = 0;
671  return -1;
672  }
673 
674  _sl.obj_len = --length;
675  _next_offs = _sl.reader->GetSize() + length;
676 
677  if (_sl.expect_table_header) {
678  _sl.expect_table_header = false;
679  return INT32_MAX;
680  }
681 
682  int index;
683  switch (_sl.block_mode) {
684  case CH_SPARSE_TABLE:
685  case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
686  case CH_TABLE:
687  case CH_ARRAY: index = _sl.array_index++; break;
688  default:
689  Debug(sl, 0, "SlIterateArray error");
690  return -1; // error
691  }
692 
693  if (length != 0) return index;
694  }
695 }
696 
701 {
702  while (SlIterateArray() != -1) {
703  SlSkipBytes(_next_offs - _sl.reader->GetSize());
704  }
705 }
706 
712 void SlSetLength(size_t length)
713 {
714  assert(_sl.action == SLA_SAVE);
715 
716  switch (_sl.need_length) {
717  case NL_WANTLENGTH:
719  if ((_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE) && _sl.expect_table_header) {
720  _sl.expect_table_header = false;
721  SlWriteArrayLength(length + 1);
722  break;
723  }
724 
725  switch (_sl.block_mode) {
726  case CH_RIFF:
727  /* Ugly encoding of >16M RIFF chunks
728  * The lower 24 bits are normal
729  * The uppermost 4 bits are bits 24:27 */
730  assert(length < (1 << 28));
731  SlWriteUint32((uint32_t)((length & 0xFFFFFF) | ((length >> 24) << 28)));
732  break;
733  case CH_TABLE:
734  case CH_ARRAY:
735  assert(_sl.last_array_index <= _sl.array_index);
736  while (++_sl.last_array_index <= _sl.array_index) {
737  SlWriteArrayLength(1);
738  }
739  SlWriteArrayLength(length + 1);
740  break;
741  case CH_SPARSE_TABLE:
742  case CH_SPARSE_ARRAY:
743  SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
744  SlWriteSparseIndex(_sl.array_index);
745  break;
746  default: NOT_REACHED();
747  }
748  break;
749 
750  case NL_CALCLENGTH:
751  _sl.obj_len += (int)length;
752  break;
753 
754  default: NOT_REACHED();
755  }
756 }
757 
764 static void SlCopyBytes(void *ptr, size_t length)
765 {
766  uint8_t *p = (uint8_t *)ptr;
767 
768  switch (_sl.action) {
769  case SLA_LOAD_CHECK:
770  case SLA_LOAD:
771  for (; length != 0; length--) *p++ = SlReadByte();
772  break;
773  case SLA_SAVE:
774  for (; length != 0; length--) SlWriteByte(*p++);
775  break;
776  default: NOT_REACHED();
777  }
778 }
779 
782 {
783  return _sl.obj_len;
784 }
785 
793 int64_t ReadValue(const void *ptr, VarType conv)
794 {
795  switch (GetVarMemType(conv)) {
796  case SLE_VAR_BL: return (*(const bool *)ptr != 0);
797  case SLE_VAR_I8: return *(const int8_t *)ptr;
798  case SLE_VAR_U8: return *(const uint8_t *)ptr;
799  case SLE_VAR_I16: return *(const int16_t *)ptr;
800  case SLE_VAR_U16: return *(const uint16_t*)ptr;
801  case SLE_VAR_I32: return *(const int32_t *)ptr;
802  case SLE_VAR_U32: return *(const uint32_t*)ptr;
803  case SLE_VAR_I64: return *(const int64_t *)ptr;
804  case SLE_VAR_U64: return *(const uint64_t*)ptr;
805  case SLE_VAR_NULL:return 0;
806  default: NOT_REACHED();
807  }
808 }
809 
817 void WriteValue(void *ptr, VarType conv, int64_t val)
818 {
819  switch (GetVarMemType(conv)) {
820  case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
821  case SLE_VAR_I8: *(int8_t *)ptr = val; break;
822  case SLE_VAR_U8: *(uint8_t *)ptr = val; break;
823  case SLE_VAR_I16: *(int16_t *)ptr = val; break;
824  case SLE_VAR_U16: *(uint16_t*)ptr = val; break;
825  case SLE_VAR_I32: *(int32_t *)ptr = val; break;
826  case SLE_VAR_U32: *(uint32_t*)ptr = val; break;
827  case SLE_VAR_I64: *(int64_t *)ptr = val; break;
828  case SLE_VAR_U64: *(uint64_t*)ptr = val; break;
829  case SLE_VAR_NAME: *reinterpret_cast<std::string *>(ptr) = CopyFromOldName(val); break;
830  case SLE_VAR_NULL: break;
831  default: NOT_REACHED();
832  }
833 }
834 
843 static void SlSaveLoadConv(void *ptr, VarType conv)
844 {
845  switch (_sl.action) {
846  case SLA_SAVE: {
847  int64_t x = ReadValue(ptr, conv);
848 
849  /* Write the value to the file and check if its value is in the desired range */
850  switch (GetVarFileType(conv)) {
851  case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
852  case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
853  case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
854  case SLE_FILE_STRINGID:
855  case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
856  case SLE_FILE_I32:
857  case SLE_FILE_U32: SlWriteUint32((uint32_t)x);break;
858  case SLE_FILE_I64:
859  case SLE_FILE_U64: SlWriteUint64(x);break;
860  default: NOT_REACHED();
861  }
862  break;
863  }
864  case SLA_LOAD_CHECK:
865  case SLA_LOAD: {
866  int64_t x;
867  /* Read a value from the file */
868  switch (GetVarFileType(conv)) {
869  case SLE_FILE_I8: x = (int8_t )SlReadByte(); break;
870  case SLE_FILE_U8: x = (uint8_t )SlReadByte(); break;
871  case SLE_FILE_I16: x = (int16_t )SlReadUint16(); break;
872  case SLE_FILE_U16: x = (uint16_t)SlReadUint16(); break;
873  case SLE_FILE_I32: x = (int32_t )SlReadUint32(); break;
874  case SLE_FILE_U32: x = (uint32_t)SlReadUint32(); break;
875  case SLE_FILE_I64: x = (int64_t )SlReadUint64(); break;
876  case SLE_FILE_U64: x = (uint64_t)SlReadUint64(); break;
877  case SLE_FILE_STRINGID: x = RemapOldStringID((uint16_t)SlReadUint16()); break;
878  default: NOT_REACHED();
879  }
880 
881  /* Write The value to the struct. These ARE endian safe. */
882  WriteValue(ptr, conv, x);
883  break;
884  }
885  case SLA_PTRS: break;
886  case SLA_NULL: break;
887  default: NOT_REACHED();
888  }
889 }
890 
898 static inline size_t SlCalcStdStringLen(const void *ptr)
899 {
900  const std::string *str = reinterpret_cast<const std::string *>(ptr);
901 
902  size_t len = str->length();
903  return len + SlGetArrayLength(len); // also include the length of the index
904 }
905 
906 
914 static void FixSCCEncoded(std::string &str)
915 {
916  for (size_t i = 0; i < str.size(); /* nothing. */) {
917  size_t len = Utf8EncodedCharLen(str[i]);
918  if (len == 0 || i + len > str.size()) break;
919 
920  char32_t c;
921  Utf8Decode(&c, &str[i]);
922  if (c == 0xE028 || c == 0xE02A) Utf8Encode(&str[i], SCC_ENCODED);
923  i += len;
924  }
925 }
926 
932 static void SlStdString(void *ptr, VarType conv)
933 {
934  std::string *str = reinterpret_cast<std::string *>(ptr);
935 
936  switch (_sl.action) {
937  case SLA_SAVE: {
938  size_t len = str->length();
939  SlWriteArrayLength(len);
940  SlCopyBytes(const_cast<void *>(static_cast<const void *>(str->c_str())), len);
941  break;
942  }
943 
944  case SLA_LOAD_CHECK:
945  case SLA_LOAD: {
946  size_t len = SlReadArrayLength();
947  if (GetVarMemType(conv) == SLE_VAR_NULL) {
948  SlSkipBytes(len);
949  return;
950  }
951 
952  str->resize(len);
953  SlCopyBytes(str->data(), len);
954 
956  if ((conv & SLF_ALLOW_CONTROL) != 0) {
959  }
960  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
962  }
963  *str = StrMakeValid(*str, settings);
964  }
965 
966  case SLA_PTRS: break;
967  case SLA_NULL: break;
968  default: NOT_REACHED();
969  }
970 }
971 
980 static void SlCopyInternal(void *object, size_t length, VarType conv)
981 {
982  if (GetVarMemType(conv) == SLE_VAR_NULL) {
983  assert(_sl.action != SLA_SAVE); // Use SL_NULL if you want to write null-bytes
984  SlSkipBytes(length * SlCalcConvFileLen(conv));
985  return;
986  }
987 
988  /* NOTICE - handle some buggy stuff, in really old versions everything was saved
989  * as a byte-type. So detect this, and adjust object size accordingly */
990  if (_sl.action != SLA_SAVE && _sl_version == 0) {
991  /* all objects except difficulty settings */
992  if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
993  conv == SLE_INT32 || conv == SLE_UINT32) {
994  SlCopyBytes(object, length * SlCalcConvFileLen(conv));
995  return;
996  }
997  /* used for conversion of Money 32bit->64bit */
998  if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
999  for (uint i = 0; i < length; i++) {
1000  ((int64_t*)object)[i] = (int32_t)BSWAP32(SlReadUint32());
1001  }
1002  return;
1003  }
1004  }
1005 
1006  /* If the size of elements is 1 byte both in file and memory, no special
1007  * conversion is needed, use specialized copy-copy function to speed up things */
1008  if (conv == SLE_INT8 || conv == SLE_UINT8) {
1009  SlCopyBytes(object, length);
1010  } else {
1011  uint8_t *a = (uint8_t*)object;
1012  uint8_t mem_size = SlCalcConvMemLen(conv);
1013 
1014  for (; length != 0; length --) {
1015  SlSaveLoadConv(a, conv);
1016  a += mem_size; // get size
1017  }
1018  }
1019 }
1020 
1029 void SlCopy(void *object, size_t length, VarType conv)
1030 {
1031  if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
1032 
1033  /* Automatically calculate the length? */
1034  if (_sl.need_length != NL_NONE) {
1035  SlSetLength(length * SlCalcConvFileLen(conv));
1036  /* Determine length only? */
1037  if (_sl.need_length == NL_CALCLENGTH) return;
1038  }
1039 
1040  SlCopyInternal(object, length, conv);
1041 }
1042 
1048 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
1049 {
1050  return SlCalcConvFileLen(conv) * length + SlGetArrayLength(length);
1051 }
1052 
1059 static void SlArray(void *array, size_t length, VarType conv)
1060 {
1061  switch (_sl.action) {
1062  case SLA_SAVE:
1063  SlWriteArrayLength(length);
1064  SlCopyInternal(array, length, conv);
1065  return;
1066 
1067  case SLA_LOAD_CHECK:
1068  case SLA_LOAD: {
1070  size_t sv_length = SlReadArrayLength();
1071  if (GetVarMemType(conv) == SLE_VAR_NULL) {
1072  /* We don't know this field, so we assume the length in the savegame is correct. */
1073  length = sv_length;
1074  } else if (sv_length != length) {
1075  /* If the SLE_ARR changes size, a savegame bump is required
1076  * and the developer should have written conversion lines.
1077  * Error out to make this more visible. */
1078  SlErrorCorrupt("Fixed-length array is of wrong length");
1079  }
1080  }
1081 
1082  SlCopyInternal(array, length, conv);
1083  return;
1084  }
1085 
1086  case SLA_PTRS:
1087  case SLA_NULL:
1088  return;
1089 
1090  default:
1091  NOT_REACHED();
1092  }
1093 }
1094 
1105 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1106 {
1107  assert(_sl.action == SLA_SAVE);
1108 
1109  if (obj == nullptr) return 0;
1110 
1111  switch (rt) {
1112  case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1113  case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1114  case REF_STATION: return ((const Station*)obj)->index + 1;
1115  case REF_TOWN: return ((const Town*)obj)->index + 1;
1116  case REF_ORDER: return ((const Order*)obj)->index + 1;
1117  case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1118  case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1119  case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1120  case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1121  case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1122  case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1123  case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1124  default: NOT_REACHED();
1125  }
1126 }
1127 
1138 static void *IntToReference(size_t index, SLRefType rt)
1139 {
1140  static_assert(sizeof(size_t) <= sizeof(void *));
1141 
1142  assert(_sl.action == SLA_PTRS);
1143 
1144  /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1145  * and should be loaded like that */
1146  if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) {
1147  rt = REF_VEHICLE;
1148  }
1149 
1150  /* No need to look up nullptr pointers, just return immediately */
1151  if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return nullptr;
1152 
1153  /* Correct index. Old vehicles were saved differently:
1154  * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1155  if (rt != REF_VEHICLE_OLD) index--;
1156 
1157  switch (rt) {
1158  case REF_ORDERLIST:
1159  if (OrderList::IsValidID(index)) return OrderList::Get(index);
1160  SlErrorCorrupt("Referencing invalid OrderList");
1161 
1162  case REF_ORDER:
1163  if (Order::IsValidID(index)) return Order::Get(index);
1164  /* in old versions, invalid order was used to mark end of order list */
1165  if (IsSavegameVersionBefore(SLV_5, 2)) return nullptr;
1166  SlErrorCorrupt("Referencing invalid Order");
1167 
1168  case REF_VEHICLE_OLD:
1169  case REF_VEHICLE:
1170  if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1171  SlErrorCorrupt("Referencing invalid Vehicle");
1172 
1173  case REF_STATION:
1174  if (Station::IsValidID(index)) return Station::Get(index);
1175  SlErrorCorrupt("Referencing invalid Station");
1176 
1177  case REF_TOWN:
1178  if (Town::IsValidID(index)) return Town::Get(index);
1179  SlErrorCorrupt("Referencing invalid Town");
1180 
1181  case REF_ROADSTOPS:
1182  if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1183  SlErrorCorrupt("Referencing invalid RoadStop");
1184 
1185  case REF_ENGINE_RENEWS:
1186  if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1187  SlErrorCorrupt("Referencing invalid EngineRenew");
1188 
1189  case REF_CARGO_PACKET:
1190  if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1191  SlErrorCorrupt("Referencing invalid CargoPacket");
1192 
1193  case REF_STORAGE:
1194  if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1195  SlErrorCorrupt("Referencing invalid PersistentStorage");
1196 
1197  case REF_LINK_GRAPH:
1198  if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1199  SlErrorCorrupt("Referencing invalid LinkGraph");
1200 
1201  case REF_LINK_GRAPH_JOB:
1202  if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1203  SlErrorCorrupt("Referencing invalid LinkGraphJob");
1204 
1205  default: NOT_REACHED();
1206  }
1207 }
1208 
1214 void SlSaveLoadRef(void *ptr, VarType conv)
1215 {
1216  switch (_sl.action) {
1217  case SLA_SAVE:
1218  SlWriteUint32((uint32_t)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1219  break;
1220  case SLA_LOAD_CHECK:
1221  case SLA_LOAD:
1222  *(size_t *)ptr = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1223  break;
1224  case SLA_PTRS:
1225  *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1226  break;
1227  case SLA_NULL:
1228  *(void **)ptr = nullptr;
1229  break;
1230  default: NOT_REACHED();
1231  }
1232 }
1233 
1237 template <template<typename, typename> typename Tstorage, typename Tvar, typename Tallocator = std::allocator<Tvar>>
1239  typedef Tstorage<Tvar, Tallocator> SlStorageT;
1240 public:
1247  static size_t SlCalcLen(const void *storage, VarType conv, SaveLoadType cmd = SL_VAR)
1248  {
1249  assert(cmd == SL_VAR || cmd == SL_REF);
1250 
1251  const SlStorageT *list = static_cast<const SlStorageT *>(storage);
1252 
1253  int type_size = SlGetArrayLength(list->size());
1254  int item_size = SlCalcConvFileLen(cmd == SL_VAR ? conv : (VarType)SLE_FILE_U32);
1255  return list->size() * item_size + type_size;
1256  }
1257 
1258  static void SlSaveLoadMember(SaveLoadType cmd, Tvar *item, VarType conv)
1259  {
1260  switch (cmd) {
1261  case SL_VAR: SlSaveLoadConv(item, conv); break;
1262  case SL_REF: SlSaveLoadRef(item, conv); break;
1263  case SL_STDSTR: SlStdString(item, conv); break;
1264  default:
1265  NOT_REACHED();
1266  }
1267  }
1268 
1275  static void SlSaveLoad(void *storage, VarType conv, SaveLoadType cmd = SL_VAR)
1276  {
1277  assert(cmd == SL_VAR || cmd == SL_REF || cmd == SL_STDSTR);
1278 
1279  SlStorageT *list = static_cast<SlStorageT *>(storage);
1280 
1281  switch (_sl.action) {
1282  case SLA_SAVE:
1283  SlWriteArrayLength(list->size());
1284 
1285  for (auto &item : *list) {
1286  SlSaveLoadMember(cmd, &item, conv);
1287  }
1288  break;
1289 
1290  case SLA_LOAD_CHECK:
1291  case SLA_LOAD: {
1292  size_t length;
1293  switch (cmd) {
1294  case SL_VAR: length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? SlReadUint32() : SlReadArrayLength(); break;
1295  case SL_REF: length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? SlReadUint32() : SlReadArrayLength(); break;
1296  case SL_STDSTR: length = SlReadArrayLength(); break;
1297  default: NOT_REACHED();
1298  }
1299 
1300  /* Load each value and push to the end of the storage. */
1301  for (size_t i = 0; i < length; i++) {
1302  Tvar &data = list->emplace_back();
1303  SlSaveLoadMember(cmd, &data, conv);
1304  }
1305  break;
1306  }
1307 
1308  case SLA_PTRS:
1309  for (auto &item : *list) {
1310  SlSaveLoadMember(cmd, &item, conv);
1311  }
1312  break;
1313 
1314  case SLA_NULL:
1315  list->clear();
1316  break;
1317 
1318  default: NOT_REACHED();
1319  }
1320  }
1321 };
1322 
1328 static inline size_t SlCalcRefListLen(const void *list, VarType conv)
1329 {
1331 }
1332 
1338 static void SlRefList(void *list, VarType conv)
1339 {
1340  /* Automatically calculate the length? */
1341  if (_sl.need_length != NL_NONE) {
1342  SlSetLength(SlCalcRefListLen(list, conv));
1343  /* Determine length only? */
1344  if (_sl.need_length == NL_CALCLENGTH) return;
1345  }
1346 
1348 }
1349 
1355 static inline size_t SlCalcDequeLen(const void *deque, VarType conv)
1356 {
1357  switch (GetVarMemType(conv)) {
1358  case SLE_VAR_BL: return SlStorageHelper<std::deque, bool>::SlCalcLen(deque, conv);
1359  case SLE_VAR_I8: return SlStorageHelper<std::deque, int8_t>::SlCalcLen(deque, conv);
1360  case SLE_VAR_U8: return SlStorageHelper<std::deque, uint8_t>::SlCalcLen(deque, conv);
1361  case SLE_VAR_I16: return SlStorageHelper<std::deque, int16_t>::SlCalcLen(deque, conv);
1362  case SLE_VAR_U16: return SlStorageHelper<std::deque, uint16_t>::SlCalcLen(deque, conv);
1363  case SLE_VAR_I32: return SlStorageHelper<std::deque, int32_t>::SlCalcLen(deque, conv);
1364  case SLE_VAR_U32: return SlStorageHelper<std::deque, uint32_t>::SlCalcLen(deque, conv);
1365  case SLE_VAR_I64: return SlStorageHelper<std::deque, int64_t>::SlCalcLen(deque, conv);
1366  case SLE_VAR_U64: return SlStorageHelper<std::deque, uint64_t>::SlCalcLen(deque, conv);
1367 
1368  case SLE_VAR_STR:
1369  /* Strings are a length-prefixed field type in the savegame table format,
1370  * these may not be directly stored in another length-prefixed container type. */
1371  NOT_REACHED();
1372 
1373  default: NOT_REACHED();
1374  }
1375 }
1376 
1382 static void SlDeque(void *deque, VarType conv)
1383 {
1384  switch (GetVarMemType(conv)) {
1385  case SLE_VAR_BL: SlStorageHelper<std::deque, bool>::SlSaveLoad(deque, conv); break;
1386  case SLE_VAR_I8: SlStorageHelper<std::deque, int8_t>::SlSaveLoad(deque, conv); break;
1387  case SLE_VAR_U8: SlStorageHelper<std::deque, uint8_t>::SlSaveLoad(deque, conv); break;
1388  case SLE_VAR_I16: SlStorageHelper<std::deque, int16_t>::SlSaveLoad(deque, conv); break;
1389  case SLE_VAR_U16: SlStorageHelper<std::deque, uint16_t>::SlSaveLoad(deque, conv); break;
1390  case SLE_VAR_I32: SlStorageHelper<std::deque, int32_t>::SlSaveLoad(deque, conv); break;
1391  case SLE_VAR_U32: SlStorageHelper<std::deque, uint32_t>::SlSaveLoad(deque, conv); break;
1392  case SLE_VAR_I64: SlStorageHelper<std::deque, int64_t>::SlSaveLoad(deque, conv); break;
1393  case SLE_VAR_U64: SlStorageHelper<std::deque, uint64_t>::SlSaveLoad(deque, conv); break;
1394 
1395  case SLE_VAR_STR:
1396  /* Strings are a length-prefixed field type in the savegame table format,
1397  * these may not be directly stored in another length-prefixed container type.
1398  * This is permitted for load-related actions, because invalid fields of this type are present
1399  * from SLV_COMPANY_ALLOW_LIST up to SLV_COMPANY_ALLOW_LIST_V2. */
1400  assert(_sl.action != SLA_SAVE);
1402  break;
1403 
1404  default: NOT_REACHED();
1405  }
1406 }
1407 
1413 static inline size_t SlCalcVectorLen(const void *vector, VarType conv)
1414 {
1415  switch (GetVarMemType(conv)) {
1416  case SLE_VAR_BL: NOT_REACHED(); // Not supported
1417  case SLE_VAR_I8: return SlStorageHelper<std::vector, int8_t>::SlCalcLen(vector, conv);
1418  case SLE_VAR_U8: return SlStorageHelper<std::vector, uint8_t>::SlCalcLen(vector, conv);
1419  case SLE_VAR_I16: return SlStorageHelper<std::vector, int16_t>::SlCalcLen(vector, conv);
1420  case SLE_VAR_U16: return SlStorageHelper<std::vector, uint16_t>::SlCalcLen(vector, conv);
1421  case SLE_VAR_I32: return SlStorageHelper<std::vector, int32_t>::SlCalcLen(vector, conv);
1422  case SLE_VAR_U32: return SlStorageHelper<std::vector, uint32_t>::SlCalcLen(vector, conv);
1423  case SLE_VAR_I64: return SlStorageHelper<std::vector, int64_t>::SlCalcLen(vector, conv);
1424  case SLE_VAR_U64: return SlStorageHelper<std::vector, uint64_t>::SlCalcLen(vector, conv);
1425 
1426  case SLE_VAR_STR:
1427  /* Strings are a length-prefixed field type in the savegame table format,
1428  * these may not be directly stored in another length-prefixed container type. */
1429  NOT_REACHED();
1430 
1431  default: NOT_REACHED();
1432  }
1433 }
1434 
1440 static void SlVector(void *vector, VarType conv)
1441 {
1442  switch (GetVarMemType(conv)) {
1443  case SLE_VAR_BL: NOT_REACHED(); // Not supported
1444  case SLE_VAR_I8: SlStorageHelper<std::vector, int8_t>::SlSaveLoad(vector, conv); break;
1445  case SLE_VAR_U8: SlStorageHelper<std::vector, uint8_t>::SlSaveLoad(vector, conv); break;
1446  case SLE_VAR_I16: SlStorageHelper<std::vector, int16_t>::SlSaveLoad(vector, conv); break;
1447  case SLE_VAR_U16: SlStorageHelper<std::vector, uint16_t>::SlSaveLoad(vector, conv); break;
1448  case SLE_VAR_I32: SlStorageHelper<std::vector, int32_t>::SlSaveLoad(vector, conv); break;
1449  case SLE_VAR_U32: SlStorageHelper<std::vector, uint32_t>::SlSaveLoad(vector, conv); break;
1450  case SLE_VAR_I64: SlStorageHelper<std::vector, int64_t>::SlSaveLoad(vector, conv); break;
1451  case SLE_VAR_U64: SlStorageHelper<std::vector, uint64_t>::SlSaveLoad(vector, conv); break;
1452 
1453  case SLE_VAR_STR:
1454  /* Strings are a length-prefixed field type in the savegame table format,
1455  * these may not be directly stored in another length-prefixed container type.
1456  * This is permitted for load-related actions, because invalid fields of this type are present
1457  * from SLV_COMPANY_ALLOW_LIST up to SLV_COMPANY_ALLOW_LIST_V2. */
1458  assert(_sl.action != SLA_SAVE);
1460  break;
1461 
1462  default: NOT_REACHED();
1463  }
1464 }
1465 
1467 static inline bool SlIsObjectValidInSavegame(const SaveLoad &sld)
1468 {
1469  return (_sl_version >= sld.version_from && _sl_version < sld.version_to);
1470 }
1471 
1477 static size_t SlCalcTableHeader(const SaveLoadTable &slt)
1478 {
1479  size_t length = 0;
1480 
1481  for (auto &sld : slt) {
1482  if (!SlIsObjectValidInSavegame(sld)) continue;
1483 
1484  length += SlCalcConvFileLen(SLE_UINT8);
1485  length += SlCalcStdStringLen(&sld.name);
1486  }
1487 
1488  length += SlCalcConvFileLen(SLE_UINT8); // End-of-list entry.
1489 
1490  for (auto &sld : slt) {
1491  if (!SlIsObjectValidInSavegame(sld)) continue;
1492  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1493  length += SlCalcTableHeader(sld.handler->GetDescription());
1494  }
1495  }
1496 
1497  return length;
1498 }
1499 
1506 size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
1507 {
1508  size_t length = 0;
1509 
1510  /* Need to determine the length and write a length tag. */
1511  for (auto &sld : slt) {
1512  length += SlCalcObjMemberLength(object, sld);
1513  }
1514  return length;
1515 }
1516 
1517 size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld)
1518 {
1519  assert(_sl.action == SLA_SAVE);
1520 
1521  if (!SlIsObjectValidInSavegame(sld)) return 0;
1522 
1523  switch (sld.cmd) {
1524  case SL_VAR: return SlCalcConvFileLen(sld.conv);
1525  case SL_REF: return SlCalcRefLen();
1526  case SL_ARR: return SlCalcArrayLen(sld.length, sld.conv);
1527  case SL_REFLIST: return SlCalcRefListLen(GetVariableAddress(object, sld), sld.conv);
1528  case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld.conv);
1529  case SL_VECTOR: return SlCalcVectorLen(GetVariableAddress(object, sld), sld.conv);
1530  case SL_STDSTR: return SlCalcStdStringLen(GetVariableAddress(object, sld));
1531  case SL_SAVEBYTE: return 1; // a byte is logically of size 1
1532  case SL_NULL: return SlCalcConvFileLen(sld.conv) * sld.length;
1533 
1534  case SL_STRUCT:
1535  case SL_STRUCTLIST: {
1536  NeedLength old_need_length = _sl.need_length;
1537  size_t old_obj_len = _sl.obj_len;
1538 
1540  _sl.obj_len = 0;
1541 
1542  /* Pretend that we are saving to collect the object size. Other
1543  * means are difficult, as we don't know the length of the list we
1544  * are about to store. */
1545  sld.handler->Save(const_cast<void *>(object));
1546  size_t length = _sl.obj_len;
1547 
1548  _sl.obj_len = old_obj_len;
1549  _sl.need_length = old_need_length;
1550 
1551  if (sld.cmd == SL_STRUCT) {
1552  length += SlGetArrayLength(1);
1553  }
1554 
1555  return length;
1556  }
1557 
1558  default: NOT_REACHED();
1559  }
1560  return 0;
1561 }
1562 
1563 static bool SlObjectMember(void *object, const SaveLoad &sld)
1564 {
1565  if (!SlIsObjectValidInSavegame(sld)) return false;
1566 
1567  VarType conv = GB(sld.conv, 0, 8);
1568  switch (sld.cmd) {
1569  case SL_VAR:
1570  case SL_REF:
1571  case SL_ARR:
1572  case SL_REFLIST:
1573  case SL_DEQUE:
1574  case SL_VECTOR:
1575  case SL_STDSTR: {
1576  void *ptr = GetVariableAddress(object, sld);
1577 
1578  switch (sld.cmd) {
1579  case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1580  case SL_REF: SlSaveLoadRef(ptr, conv); break;
1581  case SL_ARR: SlArray(ptr, sld.length, conv); break;
1582  case SL_REFLIST: SlRefList(ptr, conv); break;
1583  case SL_DEQUE: SlDeque(ptr, conv); break;
1584  case SL_VECTOR: SlVector(ptr, conv); break;
1585  case SL_STDSTR: SlStdString(ptr, sld.conv); break;
1586  default: NOT_REACHED();
1587  }
1588  break;
1589  }
1590 
1591  /* SL_SAVEBYTE writes a value to the savegame to identify the type of an object.
1592  * When loading, the value is read explicitly with SlReadByte() to determine which
1593  * object description to use. */
1594  case SL_SAVEBYTE: {
1595  void *ptr = GetVariableAddress(object, sld);
1596 
1597  switch (_sl.action) {
1598  case SLA_SAVE: SlWriteByte(*(uint8_t *)ptr); break;
1599  case SLA_LOAD_CHECK:
1600  case SLA_LOAD:
1601  case SLA_PTRS:
1602  case SLA_NULL: break;
1603  default: NOT_REACHED();
1604  }
1605  break;
1606  }
1607 
1608  case SL_NULL: {
1609  assert(GetVarMemType(sld.conv) == SLE_VAR_NULL);
1610 
1611  switch (_sl.action) {
1612  case SLA_LOAD_CHECK:
1613  case SLA_LOAD: SlSkipBytes(SlCalcConvFileLen(sld.conv) * sld.length); break;
1614  case SLA_SAVE: for (int i = 0; i < SlCalcConvFileLen(sld.conv) * sld.length; i++) SlWriteByte(0); break;
1615  case SLA_PTRS:
1616  case SLA_NULL: break;
1617  default: NOT_REACHED();
1618  }
1619  break;
1620  }
1621 
1622  case SL_STRUCT:
1623  case SL_STRUCTLIST:
1624  switch (_sl.action) {
1625  case SLA_SAVE: {
1626  if (sld.cmd == SL_STRUCT) {
1627  /* Store in the savegame if this struct was written or not. */
1628  SlSetStructListLength(SlCalcObjMemberLength(object, sld) > SlGetArrayLength(1) ? 1 : 0);
1629  }
1630  sld.handler->Save(object);
1631  break;
1632  }
1633 
1634  case SLA_LOAD_CHECK: {
1637  }
1638  sld.handler->LoadCheck(object);
1639  break;
1640  }
1641 
1642  case SLA_LOAD: {
1645  }
1646  sld.handler->Load(object);
1647  break;
1648  }
1649 
1650  case SLA_PTRS:
1651  sld.handler->FixPointers(object);
1652  break;
1653 
1654  case SLA_NULL: break;
1655  default: NOT_REACHED();
1656  }
1657  break;
1658 
1659  default: NOT_REACHED();
1660  }
1661  return true;
1662 }
1663 
1668 void SlSetStructListLength(size_t length)
1669 {
1670  /* Automatically calculate the length? */
1671  if (_sl.need_length != NL_NONE) {
1672  SlSetLength(SlGetArrayLength(length));
1673  if (_sl.need_length == NL_CALCLENGTH) return;
1674  }
1675 
1676  SlWriteArrayLength(length);
1677 }
1678 
1684 size_t SlGetStructListLength(size_t limit)
1685 {
1686  size_t length = SlReadArrayLength();
1687  if (length > limit) SlErrorCorrupt("List exceeds storage size");
1688 
1689  return length;
1690 }
1691 
1697 void SlObject(void *object, const SaveLoadTable &slt)
1698 {
1699  /* Automatically calculate the length? */
1700  if (_sl.need_length != NL_NONE) {
1701  SlSetLength(SlCalcObjLength(object, slt));
1702  if (_sl.need_length == NL_CALCLENGTH) return;
1703  }
1704 
1705  for (auto &sld : slt) {
1706  SlObjectMember(object, sld);
1707  }
1708 }
1709 
1715  void Save(void *) const override
1716  {
1717  NOT_REACHED();
1718  }
1719 
1720  void Load(void *object) const override
1721  {
1722  size_t length = SlGetStructListLength(UINT32_MAX);
1723  for (; length > 0; length--) {
1724  SlObject(object, this->GetLoadDescription());
1725  }
1726  }
1727 
1728  void LoadCheck(void *object) const override
1729  {
1730  this->Load(object);
1731  }
1732 
1733  virtual SaveLoadTable GetDescription() const override
1734  {
1735  return {};
1736  }
1737 
1739  {
1740  NOT_REACHED();
1741  }
1742 };
1743 
1750 std::vector<SaveLoad> SlTableHeader(const SaveLoadTable &slt)
1751 {
1752  /* You can only use SlTableHeader if you are a CH_TABLE. */
1753  assert(_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
1754 
1755  switch (_sl.action) {
1756  case SLA_LOAD_CHECK:
1757  case SLA_LOAD: {
1758  std::vector<SaveLoad> saveloads;
1759 
1760  /* Build a key lookup mapping based on the available fields. */
1761  std::map<std::string, const SaveLoad *> key_lookup;
1762  for (auto &sld : slt) {
1763  if (!SlIsObjectValidInSavegame(sld)) continue;
1764 
1765  /* Check that there is only one active SaveLoad for a given name. */
1766  assert(key_lookup.find(sld.name) == key_lookup.end());
1767  key_lookup[sld.name] = &sld;
1768  }
1769 
1770  while (true) {
1771  uint8_t type = 0;
1772  SlSaveLoadConv(&type, SLE_UINT8);
1773  if (type == SLE_FILE_END) break;
1774 
1775  std::string key;
1776  SlStdString(&key, SLE_STR);
1777 
1778  auto sld_it = key_lookup.find(key);
1779  if (sld_it == key_lookup.end()) {
1780  /* SLA_LOADCHECK triggers this debug statement a lot and is perfectly normal. */
1781  Debug(sl, _sl.action == SLA_LOAD ? 2 : 6, "Field '{}' of type 0x{:02x} not found, skipping", key, type);
1782 
1783  std::shared_ptr<SaveLoadHandler> handler = nullptr;
1784  SaveLoadType saveload_type;
1785  switch (type & SLE_FILE_TYPE_MASK) {
1786  case SLE_FILE_STRING:
1787  /* Strings are always marked with SLE_FILE_HAS_LENGTH_FIELD, as they are a list of chars. */
1788  saveload_type = SL_STDSTR;
1789  break;
1790 
1791  case SLE_FILE_STRUCT:
1792  /* Structs are always marked with SLE_FILE_HAS_LENGTH_FIELD as SL_STRUCT is seen as a list of 0/1 in length. */
1793  saveload_type = SL_STRUCTLIST;
1794  handler = std::make_shared<SlSkipHandler>();
1795  break;
1796 
1797  default:
1798  saveload_type = (type & SLE_FILE_HAS_LENGTH_FIELD) ? SL_ARR : SL_VAR;
1799  break;
1800  }
1801 
1802  /* We don't know this field, so read to nothing. */
1803  saveloads.push_back({key, saveload_type, ((VarType)type & SLE_FILE_TYPE_MASK) | SLE_VAR_NULL, 1, SL_MIN_VERSION, SL_MAX_VERSION, nullptr, 0, handler});
1804  continue;
1805  }
1806 
1807  /* Validate the type of the field. If it is changed, the
1808  * savegame should have been bumped so we know how to do the
1809  * conversion. If this error triggers, that clearly didn't
1810  * happen and this is a friendly poke to the developer to bump
1811  * the savegame version and add conversion code. */
1812  uint8_t correct_type = GetSavegameFileType(*sld_it->second);
1813  if (correct_type != type) {
1814  Debug(sl, 1, "Field type for '{}' was expected to be 0x{:02x} but 0x{:02x} was found", key, correct_type, type);
1815  SlErrorCorrupt("Field type is different than expected");
1816  }
1817  saveloads.push_back(*sld_it->second);
1818  }
1819 
1820  for (auto &sld : saveloads) {
1821  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1822  sld.handler->load_description = SlTableHeader(sld.handler->GetDescription());
1823  }
1824  }
1825 
1826  return saveloads;
1827  }
1828 
1829  case SLA_SAVE: {
1830  /* Automatically calculate the length? */
1831  if (_sl.need_length != NL_NONE) {
1833  if (_sl.need_length == NL_CALCLENGTH) break;
1834  }
1835 
1836  for (auto &sld : slt) {
1837  if (!SlIsObjectValidInSavegame(sld)) continue;
1838  /* Make sure we are not storing empty keys. */
1839  assert(!sld.name.empty());
1840 
1841  uint8_t type = GetSavegameFileType(sld);
1842  assert(type != SLE_FILE_END);
1843 
1844  SlSaveLoadConv(&type, SLE_UINT8);
1845  SlStdString(const_cast<std::string *>(&sld.name), SLE_STR);
1846  }
1847 
1848  /* Add an end-of-header marker. */
1849  uint8_t type = SLE_FILE_END;
1850  SlSaveLoadConv(&type, SLE_UINT8);
1851 
1852  /* After the table, write down any sub-tables we might have. */
1853  for (auto &sld : slt) {
1854  if (!SlIsObjectValidInSavegame(sld)) continue;
1855  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1856  /* SlCalcTableHeader already looks in sub-lists, so avoid the length being added twice. */
1857  NeedLength old_need_length = _sl.need_length;
1859 
1860  SlTableHeader(sld.handler->GetDescription());
1861 
1862  _sl.need_length = old_need_length;
1863  }
1864  }
1865 
1866  break;
1867  }
1868 
1869  default: NOT_REACHED();
1870  }
1871 
1872  return std::vector<SaveLoad>();
1873 }
1874 
1888 std::vector<SaveLoad> SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
1889 {
1890  assert(_sl.action == SLA_LOAD || _sl.action == SLA_LOAD_CHECK);
1891  /* CH_TABLE / CH_SPARSE_TABLE always have a header. */
1892  if (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE) return SlTableHeader(slt);
1893 
1894  std::vector<SaveLoad> saveloads;
1895 
1896  /* Build a key lookup mapping based on the available fields. */
1897  std::map<std::string, std::vector<const SaveLoad *>> key_lookup;
1898  for (auto &sld : slt) {
1899  /* All entries should have a name; otherwise the entry should just be removed. */
1900  assert(!sld.name.empty());
1901 
1902  key_lookup[sld.name].push_back(&sld);
1903  }
1904 
1905  for (auto &slc : slct) {
1906  if (slc.name.empty()) {
1907  /* In old savegames there can be data we no longer care for. We
1908  * skip this by simply reading the amount of bytes indicated and
1909  * send those to /dev/null. */
1910  saveloads.push_back({"", SL_NULL, GetVarFileType(slc.null_type) | SLE_VAR_NULL, slc.null_length, slc.version_from, slc.version_to, nullptr, 0, nullptr});
1911  } else {
1912  auto sld_it = key_lookup.find(slc.name);
1913  /* If this branch triggers, it means that an entry in the
1914  * SaveLoadCompat list is not mentioned in the SaveLoad list. Did
1915  * you rename a field in one and not in the other? */
1916  if (sld_it == key_lookup.end()) {
1917  /* This isn't an assert, as that leaves no information what
1918  * field was to blame. This way at least we have breadcrumbs. */
1919  Debug(sl, 0, "internal error: saveload compatibility field '{}' not found", slc.name);
1920  SlErrorCorrupt("Internal error with savegame compatibility");
1921  }
1922  for (auto &sld : sld_it->second) {
1923  saveloads.push_back(*sld);
1924  }
1925  }
1926  }
1927 
1928  for (auto &sld : saveloads) {
1929  if (!SlIsObjectValidInSavegame(sld)) continue;
1930  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1931  sld.handler->load_description = SlCompatTableHeader(sld.handler->GetDescription(), sld.handler->GetCompatDescription());
1932  }
1933  }
1934 
1935  return saveloads;
1936 }
1937 
1942 void SlGlobList(const SaveLoadTable &slt)
1943 {
1944  SlObject(nullptr, slt);
1945 }
1946 
1952 void SlAutolength(AutolengthProc *proc, int arg)
1953 {
1954  assert(_sl.action == SLA_SAVE);
1955 
1956  /* Tell it to calculate the length */
1958  _sl.obj_len = 0;
1959  proc(arg);
1960 
1961  /* Setup length */
1964 
1965  size_t start_pos = _sl.dumper->GetSize();
1966  size_t expected_offs = start_pos + _sl.obj_len;
1967 
1968  /* And write the stuff */
1969  proc(arg);
1970 
1971  if (expected_offs != _sl.dumper->GetSize()) {
1972  SlErrorCorruptFmt("Invalid chunk size when writing autolength block, expected {}, got {}", _sl.obj_len, _sl.dumper->GetSize() - start_pos);
1973  }
1974 }
1975 
1976 void ChunkHandler::LoadCheck(size_t len) const
1977 {
1978  switch (_sl.block_mode) {
1979  case CH_TABLE:
1980  case CH_SPARSE_TABLE:
1981  SlTableHeader({});
1982  [[fallthrough]];
1983  case CH_ARRAY:
1984  case CH_SPARSE_ARRAY:
1985  SlSkipArray();
1986  break;
1987  case CH_RIFF:
1988  SlSkipBytes(len);
1989  break;
1990  default:
1991  NOT_REACHED();
1992  }
1993 }
1994 
1999 static void SlLoadChunk(const ChunkHandler &ch)
2000 {
2001  uint8_t m = SlReadByte();
2002 
2003  _sl.block_mode = m & CH_TYPE_MASK;
2004  _sl.obj_len = 0;
2005  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2006 
2007  /* The header should always be at the start. Read the length; the
2008  * Load() should as first action process the header. */
2009  if (_sl.expect_table_header) {
2010  SlIterateArray();
2011  }
2012 
2013  switch (_sl.block_mode) {
2014  case CH_TABLE:
2015  case CH_ARRAY:
2016  _sl.array_index = 0;
2017  ch.Load();
2018  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2019  break;
2020  case CH_SPARSE_TABLE:
2021  case CH_SPARSE_ARRAY:
2022  ch.Load();
2023  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2024  break;
2025  case CH_RIFF: {
2026  /* Read length */
2027  size_t len = (SlReadByte() << 16) | ((m >> 4) << 24);
2028  len += SlReadUint16();
2029  _sl.obj_len = len;
2030  size_t start_pos = _sl.reader->GetSize();
2031  size_t endoffs = start_pos + len;
2032  ch.Load();
2033 
2034  if (_sl.reader->GetSize() != endoffs) {
2035  SlErrorCorruptFmt("Invalid chunk size in RIFF in {} - expected {}, got {}", ch.GetName(), len, _sl.reader->GetSize() - start_pos);
2036  }
2037  break;
2038  }
2039  default:
2040  SlErrorCorrupt("Invalid chunk type");
2041  break;
2042  }
2043 
2044  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2045 }
2046 
2052 static void SlLoadCheckChunk(const ChunkHandler &ch)
2053 {
2054  uint8_t m = SlReadByte();
2055 
2056  _sl.block_mode = m & CH_TYPE_MASK;
2057  _sl.obj_len = 0;
2058  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2059 
2060  /* The header should always be at the start. Read the length; the
2061  * LoadCheck() should as first action process the header. */
2062  if (_sl.expect_table_header) {
2063  SlIterateArray();
2064  }
2065 
2066  switch (_sl.block_mode) {
2067  case CH_TABLE:
2068  case CH_ARRAY:
2069  _sl.array_index = 0;
2070  ch.LoadCheck();
2071  break;
2072  case CH_SPARSE_TABLE:
2073  case CH_SPARSE_ARRAY:
2074  ch.LoadCheck();
2075  break;
2076  case CH_RIFF: {
2077  /* Read length */
2078  size_t len = (SlReadByte() << 16) | ((m >> 4) << 24);
2079  len += SlReadUint16();
2080  _sl.obj_len = len;
2081  size_t start_pos = _sl.reader->GetSize();
2082  size_t endoffs = start_pos + len;
2083  ch.LoadCheck(len);
2084 
2085  if (_sl.reader->GetSize() != endoffs) {
2086  SlErrorCorruptFmt("Invalid chunk size in RIFF in {} - expected {}, got {}", ch.GetName(), len, _sl.reader->GetSize() - start_pos);
2087  }
2088  break;
2089  }
2090  default:
2091  SlErrorCorrupt("Invalid chunk type");
2092  break;
2093  }
2094 
2095  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2096 }
2097 
2103 static void SlSaveChunk(const ChunkHandler &ch)
2104 {
2105  if (ch.type == CH_READONLY) return;
2106 
2107  SlWriteUint32(ch.id);
2108  Debug(sl, 2, "Saving chunk {}", ch.GetName());
2109 
2110  _sl.block_mode = ch.type;
2111  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2112 
2114 
2115  switch (_sl.block_mode) {
2116  case CH_RIFF:
2117  ch.Save();
2118  break;
2119  case CH_TABLE:
2120  case CH_ARRAY:
2121  _sl.last_array_index = 0;
2123  ch.Save();
2124  SlWriteArrayLength(0); // Terminate arrays
2125  break;
2126  case CH_SPARSE_TABLE:
2127  case CH_SPARSE_ARRAY:
2129  ch.Save();
2130  SlWriteArrayLength(0); // Terminate arrays
2131  break;
2132  default: NOT_REACHED();
2133  }
2134 
2135  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2136 }
2137 
2139 static void SlSaveChunks()
2140 {
2141  for (auto &ch : ChunkHandlers()) {
2142  SlSaveChunk(ch);
2143  }
2144 
2145  /* Terminator */
2146  SlWriteUint32(0);
2147 }
2148 
2155 static const ChunkHandler *SlFindChunkHandler(uint32_t id)
2156 {
2157  for (const ChunkHandler &ch : ChunkHandlers()) if (ch.id == id) return &ch;
2158  return nullptr;
2159 }
2160 
2162 static void SlLoadChunks()
2163 {
2164  uint32_t id;
2165  const ChunkHandler *ch;
2166 
2167  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
2168  Debug(sl, 2, "Loading chunk {:c}{:c}{:c}{:c}", id >> 24, id >> 16, id >> 8, id);
2169 
2170  ch = SlFindChunkHandler(id);
2171  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2172  SlLoadChunk(*ch);
2173  }
2174 }
2175 
2177 static void SlLoadCheckChunks()
2178 {
2179  uint32_t id;
2180  const ChunkHandler *ch;
2181 
2182  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
2183  Debug(sl, 2, "Loading chunk {:c}{:c}{:c}{:c}", id >> 24, id >> 16, id >> 8, id);
2184 
2185  ch = SlFindChunkHandler(id);
2186  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2187  SlLoadCheckChunk(*ch);
2188  }
2189 }
2190 
2192 static void SlFixPointers()
2193 {
2194  _sl.action = SLA_PTRS;
2195 
2196  for (const ChunkHandler &ch : ChunkHandlers()) {
2197  Debug(sl, 3, "Fixing pointers for {}", ch.GetName());
2198  ch.FixPointers();
2199  }
2200 
2201  assert(_sl.action == SLA_PTRS);
2202 }
2203 
2204 
2207  std::optional<FileHandle> file;
2208  long begin;
2209 
2214  FileReader(FileHandle &&file) : LoadFilter(nullptr), file(std::move(file)), begin(ftell(*this->file))
2215  {
2216  }
2217 
2220  {
2221  if (this->file.has_value()) {
2222  _game_session_stats.savegame_size = ftell(*this->file) - this->begin;
2223  }
2224  }
2225 
2226  size_t Read(uint8_t *buf, size_t size) override
2227  {
2228  /* We're in the process of shutting down, i.e. in "failure" mode. */
2229  if (!this->file.has_value()) return 0;
2230 
2231  return fread(buf, 1, size, *this->file);
2232  }
2233 
2234  void Reset() override
2235  {
2236  clearerr(*this->file);
2237  if (fseek(*this->file, this->begin, SEEK_SET)) {
2238  Debug(sl, 1, "Could not reset the file reading");
2239  }
2240  }
2241 };
2242 
2245  std::optional<FileHandle> file;
2246 
2251  FileWriter(FileHandle &&file) : SaveFilter(nullptr), file(std::move(file))
2252  {
2253  }
2254 
2257  {
2258  this->Finish();
2259  }
2260 
2261  void Write(uint8_t *buf, size_t size) override
2262  {
2263  /* We're in the process of shutting down, i.e. in "failure" mode. */
2264  if (!this->file.has_value()) return;
2265 
2266  if (fwrite(buf, 1, size, *this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
2267  }
2268 
2269  void Finish() override
2270  {
2271  if (this->file.has_value()) {
2272  _game_session_stats.savegame_size = ftell(*this->file);
2273  this->file.reset();
2274  }
2275  }
2276 };
2277 
2278 /*******************************************
2279  ********** START OF LZO CODE **************
2280  *******************************************/
2281 
2282 #ifdef WITH_LZO
2283 #include <lzo/lzo1x.h>
2284 
2286 static const uint LZO_BUFFER_SIZE = 8192;
2287 
2294  LZOLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(chain)
2295  {
2296  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2297  }
2298 
2299  size_t Read(uint8_t *buf, size_t ssize) override
2300  {
2301  assert(ssize >= LZO_BUFFER_SIZE);
2302 
2303  /* Buffer size is from the LZO docs plus the chunk header size. */
2304  uint8_t out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32_t) * 2];
2305  uint32_t tmp[2];
2306  uint32_t size;
2307  lzo_uint len = ssize;
2308 
2309  /* Read header*/
2310  if (this->chain->Read((uint8_t*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
2311 
2312  /* Check if size is bad */
2313  ((uint32_t*)out)[0] = size = tmp[1];
2314 
2315  if (_sl_version != SL_MIN_VERSION) {
2316  tmp[0] = TO_BE32(tmp[0]);
2317  size = TO_BE32(size);
2318  }
2319 
2320  if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
2321 
2322  /* Read block */
2323  if (this->chain->Read(out + sizeof(uint32_t), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2324 
2325  /* Verify checksum */
2326  if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32_t))) SlErrorCorrupt("Bad checksum");
2327 
2328  /* Decompress */
2329  int ret = lzo1x_decompress_safe(out + sizeof(uint32_t) * 1, size, buf, &len, nullptr);
2330  if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2331  return len;
2332  }
2333 };
2334 
2341  LZOSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t) : SaveFilter(chain)
2342  {
2343  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2344  }
2345 
2346  void Write(uint8_t *buf, size_t size) override
2347  {
2348  const lzo_bytep in = buf;
2349  /* Buffer size is from the LZO docs plus the chunk header size. */
2350  uint8_t out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32_t) * 2];
2351  uint8_t wrkmem[LZO1X_1_MEM_COMPRESS];
2352  lzo_uint outlen;
2353 
2354  do {
2355  /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2356  lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
2357  lzo1x_1_compress(in, len, out + sizeof(uint32_t) * 2, &outlen, wrkmem);
2358  ((uint32_t*)out)[1] = TO_BE32((uint32_t)outlen);
2359  ((uint32_t*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32_t), outlen + sizeof(uint32_t)));
2360  this->chain->Write(out, outlen + sizeof(uint32_t) * 2);
2361 
2362  /* Move to next data chunk. */
2363  size -= len;
2364  in += len;
2365  } while (size > 0);
2366  }
2367 };
2368 
2369 #endif /* WITH_LZO */
2370 
2371 /*********************************************
2372  ******** START OF NOCOMP CODE (uncompressed)*
2373  *********************************************/
2374 
2381  NoCompLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(chain)
2382  {
2383  }
2384 
2385  size_t Read(uint8_t *buf, size_t size) override
2386  {
2387  return this->chain->Read(buf, size);
2388  }
2389 };
2390 
2397  NoCompSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t) : SaveFilter(chain)
2398  {
2399  }
2400 
2401  void Write(uint8_t *buf, size_t size) override
2402  {
2403  this->chain->Write(buf, size);
2404  }
2405 };
2406 
2407 /********************************************
2408  ********** START OF ZLIB CODE **************
2409  ********************************************/
2410 
2411 #if defined(WITH_ZLIB)
2412 #include <zlib.h>
2413 
2416  z_stream z;
2417  uint8_t fread_buf[MEMORY_CHUNK_SIZE];
2423  ZlibLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(chain)
2424  {
2425  memset(&this->z, 0, sizeof(this->z));
2426  if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2427  }
2428 
2430  ~ZlibLoadFilter()
2431  {
2432  inflateEnd(&this->z);
2433  }
2434 
2435  size_t Read(uint8_t *buf, size_t size) override
2436  {
2437  this->z.next_out = buf;
2438  this->z.avail_out = (uint)size;
2439 
2440  do {
2441  /* read more bytes from the file? */
2442  if (this->z.avail_in == 0) {
2443  this->z.next_in = this->fread_buf;
2444  this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2445  }
2446 
2447  /* inflate the data */
2448  int r = inflate(&this->z, 0);
2449  if (r == Z_STREAM_END) break;
2450 
2451  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2452  } while (this->z.avail_out != 0);
2453 
2454  return size - this->z.avail_out;
2455  }
2456 };
2457 
2460  z_stream z;
2462 
2468  ZlibSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level) : SaveFilter(chain)
2469  {
2470  memset(&this->z, 0, sizeof(this->z));
2471  if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2472  }
2473 
2476  {
2477  deflateEnd(&this->z);
2478  }
2479 
2486  void WriteLoop(uint8_t *p, size_t len, int mode)
2487  {
2488  uint n;
2489  this->z.next_in = p;
2490  this->z.avail_in = (uInt)len;
2491  do {
2492  this->z.next_out = this->fwrite_buf;
2493  this->z.avail_out = sizeof(this->fwrite_buf);
2494 
2502  int r = deflate(&this->z, mode);
2503 
2504  /* bytes were emitted? */
2505  if ((n = sizeof(this->fwrite_buf) - this->z.avail_out) != 0) {
2506  this->chain->Write(this->fwrite_buf, n);
2507  }
2508  if (r == Z_STREAM_END) break;
2509 
2510  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2511  } while (this->z.avail_in || !this->z.avail_out);
2512  }
2513 
2514  void Write(uint8_t *buf, size_t size) override
2515  {
2516  this->WriteLoop(buf, size, 0);
2517  }
2518 
2519  void Finish() override
2520  {
2521  this->WriteLoop(nullptr, 0, Z_FINISH);
2522  this->chain->Finish();
2523  }
2524 };
2525 
2526 #endif /* WITH_ZLIB */
2527 
2528 /********************************************
2529  ********** START OF LZMA CODE **************
2530  ********************************************/
2531 
2532 #if defined(WITH_LIBLZMA)
2533 #include <lzma.h>
2534 
2541 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2542 
2545  lzma_stream lzma;
2547 
2552  LZMALoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(chain), lzma(_lzma_init)
2553  {
2554  /* Allow saves up to 256 MB uncompressed */
2555  if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2556  }
2557 
2560  {
2561  lzma_end(&this->lzma);
2562  }
2563 
2564  size_t Read(uint8_t *buf, size_t size) override
2565  {
2566  this->lzma.next_out = buf;
2567  this->lzma.avail_out = size;
2568 
2569  do {
2570  /* read more bytes from the file? */
2571  if (this->lzma.avail_in == 0) {
2572  this->lzma.next_in = this->fread_buf;
2573  this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2574  }
2575 
2576  /* inflate the data */
2577  lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2578  if (r == LZMA_STREAM_END) break;
2579  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2580  } while (this->lzma.avail_out != 0);
2581 
2582  return size - this->lzma.avail_out;
2583  }
2584 };
2585 
2588  lzma_stream lzma;
2590 
2596  LZMASaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level) : SaveFilter(chain), lzma(_lzma_init)
2597  {
2598  if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2599  }
2600 
2603  {
2604  lzma_end(&this->lzma);
2605  }
2606 
2613  void WriteLoop(uint8_t *p, size_t len, lzma_action action)
2614  {
2615  size_t n;
2616  this->lzma.next_in = p;
2617  this->lzma.avail_in = len;
2618  do {
2619  this->lzma.next_out = this->fwrite_buf;
2620  this->lzma.avail_out = sizeof(this->fwrite_buf);
2621 
2622  lzma_ret r = lzma_code(&this->lzma, action);
2623 
2624  /* bytes were emitted? */
2625  if ((n = sizeof(this->fwrite_buf) - this->lzma.avail_out) != 0) {
2626  this->chain->Write(this->fwrite_buf, n);
2627  }
2628  if (r == LZMA_STREAM_END) break;
2629  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2630  } while (this->lzma.avail_in || !this->lzma.avail_out);
2631  }
2632 
2633  void Write(uint8_t *buf, size_t size) override
2634  {
2635  this->WriteLoop(buf, size, LZMA_RUN);
2636  }
2637 
2638  void Finish() override
2639  {
2640  this->WriteLoop(nullptr, 0, LZMA_FINISH);
2641  this->chain->Finish();
2642  }
2643 };
2644 
2645 #endif /* WITH_LIBLZMA */
2646 
2647 /*******************************************
2648  ************* END OF CODE *****************
2649  *******************************************/
2650 
2653  const char *name;
2654  uint32_t tag;
2656  std::shared_ptr<LoadFilter> (*init_load)(std::shared_ptr<LoadFilter> chain);
2657  std::shared_ptr<SaveFilter> (*init_write)(std::shared_ptr<SaveFilter> chain, uint8_t compression);
2660  uint8_t default_compression;
2662 };
2664 static const uint32_t SAVEGAME_TAG_LZO = TO_BE32X('OTTD');
2665 static const uint32_t SAVEGAME_TAG_NONE = TO_BE32X('OTTN');
2666 static const uint32_t SAVEGAME_TAG_ZLIB = TO_BE32X('OTTZ');
2667 static const uint32_t SAVEGAME_TAG_LZMA = TO_BE32X('OTTX');
2668 
2671 #if defined(WITH_LZO)
2672  /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2673  {"lzo", SAVEGAME_TAG_LZO, CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2674 #else
2675  {"lzo", SAVEGAME_TAG_LZO, nullptr, nullptr, 0, 0, 0},
2676 #endif
2677  /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2678  {"none", SAVEGAME_TAG_NONE, CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2679 #if defined(WITH_ZLIB)
2680  /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2681  * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2682  * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2683  {"zlib", SAVEGAME_TAG_ZLIB, CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2684 #else
2685  {"zlib", SAVEGAME_TAG_ZLIB, nullptr, nullptr, 0, 0, 0},
2686 #endif
2687 #if defined(WITH_LIBLZMA)
2688  /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2689  * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2690  * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2691  * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2692  * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2693  {"lzma", SAVEGAME_TAG_LZMA, CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2694 #else
2695  {"lzma", SAVEGAME_TAG_LZMA, nullptr, nullptr, 0, 0, 0},
2696 #endif
2697 };
2698 
2705 static std::pair<const SaveLoadFormat &, uint8_t> GetSavegameFormat(const std::string &full_name)
2706 {
2707  /* Find default savegame format, the highest one with which files can be written. */
2708  auto it = std::find_if(std::rbegin(_saveload_formats), std::rend(_saveload_formats), [](const auto &slf) { return slf.init_write != nullptr; });
2709  if (it == std::rend(_saveload_formats)) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "no writeable savegame formats");
2710 
2711  const SaveLoadFormat &def = *it;
2712 
2713  if (!full_name.empty()) {
2714  /* Get the ":..." of the compression level out of the way */
2715  size_t separator = full_name.find(':');
2716  bool has_comp_level = separator != std::string::npos;
2717  const std::string name(full_name, 0, has_comp_level ? separator : full_name.size());
2718 
2719  for (const auto &slf : _saveload_formats) {
2720  if (slf.init_write != nullptr && name.compare(slf.name) == 0) {
2721  if (has_comp_level) {
2722  const std::string complevel(full_name, separator + 1);
2723 
2724  /* Get the level and determine whether all went fine. */
2725  size_t processed;
2726  long level = std::stol(complevel, &processed, 10);
2727  if (processed == 0 || level != Clamp(level, slf.min_compression, slf.max_compression)) {
2728  SetDParamStr(0, complevel);
2729  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2730  } else {
2731  return {slf, ClampTo<uint8_t>(level)};
2732  }
2733  }
2734  return {slf, slf.default_compression};
2735  }
2736  }
2737 
2738  SetDParamStr(0, name);
2739  SetDParamStr(1, def.name);
2740  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2741  }
2742  return {def, def.default_compression};
2743 }
2744 
2745 /* actual loader/saver function */
2746 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2747 extern bool AfterLoadGame();
2748 extern bool LoadOldSaveGame(const std::string &file);
2749 
2753 static void ResetSaveloadData()
2754 {
2755  ResetTempEngineData();
2756  ResetLabelMaps();
2757  ResetOldWaypoints();
2758 }
2759 
2763 static inline void ClearSaveLoadState()
2764 {
2765  _sl.dumper = nullptr;
2766  _sl.sf = nullptr;
2767  _sl.reader = nullptr;
2768  _sl.lf = nullptr;
2769 }
2770 
2772 static void SaveFileStart()
2773 {
2774  SetMouseCursorBusy(true);
2775 
2777  _sl.saveinprogress = true;
2778 }
2779 
2781 static void SaveFileDone()
2782 {
2783  SetMouseCursorBusy(false);
2784 
2786  _sl.saveinprogress = false;
2787 
2788 #ifdef __EMSCRIPTEN__
2789  EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
2790 #endif
2791 }
2792 
2795 {
2796  _sl.error_str = str;
2797 }
2798 
2801 {
2802  return _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED;
2803 }
2804 
2807 {
2809  return _sl.error_str;
2810 }
2811 
2813 static void SaveFileError()
2814 {
2816  SaveFileDone();
2817 }
2818 
2823 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2824 {
2825  try {
2826  auto [fmt, compression] = GetSavegameFormat(_savegame_format);
2827 
2828  /* We have written our stuff to memory, now write it to file! */
2829  uint32_t hdr[2] = { fmt.tag, TO_BE32(SAVEGAME_VERSION << 16) };
2830  _sl.sf->Write((uint8_t*)hdr, sizeof(hdr));
2831 
2832  _sl.sf = fmt.init_write(_sl.sf, compression);
2833  _sl.dumper->Flush(_sl.sf);
2834 
2836 
2837  if (threaded) SetAsyncSaveFinish(SaveFileDone);
2838 
2839  return SL_OK;
2840  } catch (...) {
2842 
2844 
2845  /* We don't want to shout when saving is just
2846  * cancelled due to a client disconnecting. */
2847  if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2848  /* Skip the "colour" character */
2849  Debug(sl, 0, "{}", GetString(GetSaveLoadErrorType()).substr(3) + GetString(GetSaveLoadErrorMessage()));
2850  asfp = SaveFileError;
2851  }
2852 
2853  if (threaded) {
2854  SetAsyncSaveFinish(asfp);
2855  } else {
2856  asfp();
2857  }
2858  return SL_ERROR;
2859  }
2860 }
2861 
2862 void WaitTillSaved()
2863 {
2864  if (!_save_thread.joinable()) return;
2865 
2866  _save_thread.join();
2867 
2868  /* Make sure every other state is handled properly as well. */
2870 }
2871 
2880 static SaveOrLoadResult DoSave(std::shared_ptr<SaveFilter> writer, bool threaded)
2881 {
2882  assert(!_sl.saveinprogress);
2883 
2884  _sl.dumper = std::make_unique<MemoryDumper>();
2885  _sl.sf = writer;
2886 
2888 
2889  SaveViewportBeforeSaveGame();
2890  SlSaveChunks();
2891 
2892  SaveFileStart();
2893 
2894  if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
2895  if (threaded) Debug(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2896 
2897  SaveOrLoadResult result = SaveFileToDisk(false);
2898  SaveFileDone();
2899 
2900  return result;
2901  }
2902 
2903  return SL_OK;
2904 }
2905 
2912 SaveOrLoadResult SaveWithFilter(std::shared_ptr<SaveFilter> writer, bool threaded)
2913 {
2914  try {
2915  _sl.action = SLA_SAVE;
2916  return DoSave(writer, threaded);
2917  } catch (...) {
2919  return SL_ERROR;
2920  }
2921 }
2922 
2931 static const SaveLoadFormat *DetermineSaveLoadFormat(uint32_t tag, uint32_t raw_version)
2932 {
2933  auto fmt = std::find_if(std::begin(_saveload_formats), std::end(_saveload_formats), [tag](const auto &fmt) { return fmt.tag == tag; });
2934  if (fmt != std::end(_saveload_formats)) {
2935  /* Check version number */
2936  _sl_version = (SaveLoadVersion)(TO_BE32(raw_version) >> 16);
2937  /* Minor is not used anymore from version 18.0, but it is still needed
2938  * in versions before that (4 cases) which can't be removed easy.
2939  * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2940  _sl_minor_version = (TO_BE32(raw_version) >> 8) & 0xFF;
2941 
2942  Debug(sl, 1, "Loading savegame version {}", _sl_version);
2943 
2944  /* Is the version higher than the current? */
2945  if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2946  if (_sl_version >= SLV_START_PATCHPACKS && _sl_version <= SLV_END_PATCHPACKS) SlError(STR_GAME_SAVELOAD_ERROR_PATCHPACK);
2947  return fmt;
2948  }
2949 
2950  Debug(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2951  _sl.lf->Reset();
2953  _sl_minor_version = 0;
2954 
2955  /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2956  fmt = std::find_if(std::begin(_saveload_formats), std::end(_saveload_formats), [](const auto &fmt) { return fmt.tag == SAVEGAME_TAG_LZO; });
2957  if (fmt == std::end(_saveload_formats)) {
2958  /* Who removed the LZO savegame format definition? When built without LZO support,
2959  * the formats must still list it just without a method to read the file.
2960  * The caller of this function has to check for the existence of load function. */
2961  NOT_REACHED();
2962  }
2963  return fmt;
2964 }
2965 
2972 static SaveOrLoadResult DoLoad(std::shared_ptr<LoadFilter> reader, bool load_check)
2973 {
2974  _sl.lf = reader;
2975 
2976  if (load_check) {
2977  /* Clear previous check data */
2979  /* Mark SL_LOAD_CHECK as supported for this savegame. */
2980  _load_check_data.checkable = true;
2981  }
2982 
2983  uint32_t hdr[2];
2984  if (_sl.lf->Read((uint8_t*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2985 
2986  /* see if we have any loader for this type. */
2987  const SaveLoadFormat *fmt = DetermineSaveLoadFormat(hdr[0], hdr[1]);
2988 
2989  /* loader for this savegame type is not implemented? */
2990  if (fmt->init_load == nullptr) {
2991  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, fmt::format("Loader for '{}' is not available.", fmt->name));
2992  }
2993 
2994  _sl.lf = fmt->init_load(_sl.lf);
2995  _sl.reader = std::make_unique<ReadBuffer>(_sl.lf);
2996  _next_offs = 0;
2997 
2998  if (!load_check) {
3000 
3001  /* Old maps were hardcoded to 256x256 and thus did not contain
3002  * any mapsize information. Pre-initialize to 256x256 to not to
3003  * confuse old games */
3004  InitializeGame(256, 256, true, true);
3005 
3006  _gamelog.Reset();
3007 
3009  /*
3010  * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
3011  * shared savegame version 4. Anything before that 'obviously'
3012  * does not have any NewGRFs. Between the introduction and
3013  * savegame version 41 (just before 0.5) the NewGRF settings
3014  * were not stored in the savegame and they were loaded by
3015  * using the settings from the main menu.
3016  * So, to recap:
3017  * - savegame version < 4: do not load any NewGRFs.
3018  * - savegame version >= 41: load NewGRFs from savegame, which is
3019  * already done at this stage by
3020  * overwriting the main menu settings.
3021  * - other savegame versions: use main menu settings.
3022  *
3023  * This means that users *can* crash savegame version 4..40
3024  * savegames if they set incompatible NewGRFs in the main menu,
3025  * but can't crash anymore for savegame version < 4 savegames.
3026  *
3027  * Note: this is done here because AfterLoadGame is also called
3028  * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
3029  */
3031  }
3032  }
3033 
3034  if (load_check) {
3035  /* Load chunks into _load_check_data.
3036  * No pools are loaded. References are not possible, and thus do not need resolving. */
3038  } else {
3039  /* Load chunks and resolve references */
3040  SlLoadChunks();
3041  SlFixPointers();
3042  }
3043 
3045 
3047 
3048  if (load_check) {
3049  /* The only part from AfterLoadGame() we need */
3051  } else {
3053 
3054  /* After loading fix up savegame for any internal changes that
3055  * might have occurred since then. If it fails, load back the old game. */
3056  if (!AfterLoadGame()) {
3057  _gamelog.StopAction();
3058  return SL_REINIT;
3059  }
3060 
3061  _gamelog.StopAction();
3062  }
3063 
3064  return SL_OK;
3065 }
3066 
3072 SaveOrLoadResult LoadWithFilter(std::shared_ptr<LoadFilter> reader)
3073 {
3074  try {
3075  _sl.action = SLA_LOAD;
3076  return DoLoad(reader, false);
3077  } catch (...) {
3079  return SL_REINIT;
3080  }
3081 }
3082 
3092 SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
3093 {
3094  /* An instance of saving is already active, so don't go saving again */
3095  if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
3096  /* if not an autosave, but a user action, show error message */
3097  if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
3098  return SL_OK;
3099  }
3100  WaitTillSaved();
3101 
3102  try {
3103  /* Load a TTDLX or TTDPatch game */
3104  if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
3106 
3107  InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
3108 
3109  /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
3110  * and if so a new NewGRF list will be made in LoadOldSaveGame.
3111  * Note: this is done here because AfterLoadGame is also called
3112  * for OTTD savegames which have their own NewGRF logic. */
3114  _gamelog.Reset();
3115  if (!LoadOldSaveGame(filename)) return SL_REINIT;
3117  _sl_minor_version = 0;
3119  if (!AfterLoadGame()) {
3120  _gamelog.StopAction();
3121  return SL_REINIT;
3122  }
3123  _gamelog.StopAction();
3124  return SL_OK;
3125  }
3126 
3127  assert(dft == DFT_GAME_FILE);
3128  switch (fop) {
3129  case SLO_CHECK:
3131  break;
3132 
3133  case SLO_LOAD:
3134  _sl.action = SLA_LOAD;
3135  break;
3136 
3137  case SLO_SAVE:
3138  _sl.action = SLA_SAVE;
3139  break;
3140 
3141  default: NOT_REACHED();
3142  }
3143 
3144  auto fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
3145 
3146  /* Make it a little easier to load savegames from the console */
3147  if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
3148  if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
3149  if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
3150 
3151  if (!fh.has_value()) {
3152  SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3153  }
3154 
3155  if (fop == SLO_SAVE) { // SAVE game
3156  Debug(desync, 1, "save: {:08x}; {:02x}; {}", TimerGameEconomy::date, TimerGameEconomy::date_fract, filename);
3157  if (!_settings_client.gui.threaded_saves) threaded = false;
3158 
3159  return DoSave(std::make_shared<FileWriter>(std::move(*fh)), threaded);
3160  }
3161 
3162  /* LOAD game */
3163  assert(fop == SLO_LOAD || fop == SLO_CHECK);
3164  Debug(desync, 1, "load: {}", filename);
3165  return DoLoad(std::make_shared<FileReader>(std::move(*fh)), fop == SLO_CHECK);
3166  } catch (...) {
3167  /* This code may be executed both for old and new save games. */
3169 
3170  /* Skip the "colour" character */
3171  if (fop != SLO_CHECK) Debug(sl, 0, "{}", GetString(GetSaveLoadErrorType()).substr(3) + GetString(GetSaveLoadErrorMessage()));
3172 
3173  /* A saver/loader exception!! reinitialize all variables to prevent crash! */
3174  return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
3175  }
3176 }
3177 
3184 {
3185  std::string filename;
3186 
3188  filename = GenerateDefaultSaveName() + counter.Extension();
3189  } else {
3190  filename = counter.Filename();
3191  }
3192 
3193  Debug(sl, 2, "Autosaving to '{}'", filename);
3194  if (SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) {
3195  ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR);
3196  }
3197 }
3198 
3199 
3202 {
3204 }
3205 
3210 {
3211  /* Check if we have a name for this map, which is the name of the first
3212  * available company. When there's no company available we'll use
3213  * 'Spectator' as "company" name. */
3214  CompanyID cid = _local_company;
3215  if (!Company::IsValidID(cid)) {
3216  for (const Company *c : Company::Iterate()) {
3217  cid = c->index;
3218  break;
3219  }
3220  }
3221 
3222  SetDParam(0, cid);
3223 
3224  /* We show the current game time differently depending on the timekeeping units used by this game. */
3226  /* Insert time played. */
3227  const auto play_time = TimerGameTick::counter / Ticks::TICKS_PER_SECOND;
3228  SetDParam(1, STR_SAVEGAME_DURATION_REALTIME);
3229  SetDParam(2, play_time / 60 / 60);
3230  SetDParam(3, (play_time / 60) % 60);
3231  } else {
3232  /* Insert current date */
3234  case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
3235  case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
3236  case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
3237  default: NOT_REACHED();
3238  }
3240  }
3241 
3242  /* Get the correct string (special string for when there's not company) */
3243  std::string filename = GetString(!Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT);
3244  SanitizeFilename(filename);
3245  return filename;
3246 }
3247 
3253 {
3255 }
3256 
3264 {
3265  if (aft == FT_INVALID || aft == FT_NONE) {
3266  this->file_op = SLO_INVALID;
3267  this->detail_ftype = DFT_INVALID;
3268  this->abstract_ftype = FT_INVALID;
3269  return;
3270  }
3271 
3272  this->file_op = fop;
3273  this->detail_ftype = dft;
3274  this->abstract_ftype = aft;
3275 }
3276 
3282 {
3283  this->SetMode(item.type);
3284  this->name = item.name;
3285  this->title = item.title;
3286 }
3287 
3289 {
3290  assert(this->load_description.has_value());
3291  return *this->load_description;
3292 }
SlLoadChunks
static void SlLoadChunks()
Load all chunks.
Definition: saveload.cpp:2162
SlCalcTableHeader
static size_t SlCalcTableHeader(const SaveLoadTable &slt)
Calculate the size of the table header.
Definition: saveload.cpp:1477
ResetSaveloadData
static void ResetSaveloadData()
Clear temporary data that is passed between various saveload phases.
Definition: saveload.cpp:2753
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:1275
SaveLoadVersion
SaveLoadVersion
SaveLoad versions Previous savegame versions, the trunk revision where they were introduced and the r...
Definition: saveload.h:30
SetMouseCursorBusy
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1670
SaveLoad::version_to
SaveLoadVersion version_to
Save/load the variable before this savegame version.
Definition: saveload.h:713
SlIsObjectValidInSavegame
static bool SlIsObjectValidInSavegame(const SaveLoad &sld)
Are we going to save this object or not?
Definition: saveload.cpp:1467
LZMASaveFilter::lzma
lzma_stream lzma
Stream state that we are writing to.
Definition: saveload.cpp:2588
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:3208
LZO_BUFFER_SIZE
static const uint LZO_BUFFER_SIZE
Buffer size for the LZO compressor.
Definition: saveload.cpp:2286
SL_VAR
@ SL_VAR
Save/load a variable.
Definition: saveload.h:688
REF_ORDER
@ REF_ORDER
Load/save a reference to an order.
Definition: saveload.h:593
SlDeque
static void SlDeque(void *deque, VarType conv)
Save/load a std::deque.
Definition: saveload.cpp:1382
SlLoadChunk
static void SlLoadChunk(const ChunkHandler &ch)
Load a chunk of data (eg vehicles, stations, etc.)
Definition: saveload.cpp:1999
LoadCheckData::checkable
bool checkable
True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable....
Definition: fios.h:34
SLV_5
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:43
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:2794
ZlibLoadFilter::~ZlibLoadFilter
~ZlibLoadFilter()
Clean everything up.
Definition: saveload.cpp:2432
Pool::PoolItem<&_orderlist_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
LoadCheckData::error_msg
std::string error_msg
Data to pass to SetDParamStr when displaying error.
Definition: fios.h:36
SAVE_DIR
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:117
TimerGameTick::counter
static TickCounter counter
Monotonic counter, in ticks, since start of game.
Definition: timer_game_tick.h:60
SL_NULL
@ SL_NULL
Save null-bytes and load to nowhere.
Definition: saveload.h:701
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:1059
SGT_OTTD
@ SGT_OTTD
OTTD savegame.
Definition: saveload.h:419
SVS_ALLOW_NEWLINE
@ SVS_ALLOW_NEWLINE
Allow newlines; replaces '\r ' with ' ' during processing.
Definition: string_type.h:47
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:37
_save_thread
static std::thread _save_thread
The thread we're using to compress and write a savegame.
Definition: saveload.cpp:359
SLE_VAR_STR
@ SLE_VAR_STR
string pointer
Definition: saveload.h:646
LZMASaveFilter::Write
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2633
NoCompLoadFilter
Filter without any compression.
Definition: saveload.cpp:2376
SlCalcConvFileLen
static uint8_t 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:617
SLA_SAVE
@ SLA_SAVE
saving
Definition: saveload.cpp:71
RemapOldStringID
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
Definition: strings_sl.cpp:30
CSleep
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:24
ShowErrorMessage
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
Definition: error_gui.cpp:367
SaveLoadTable
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Definition: saveload.h:507
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:596
DoExitSave
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
Definition: saveload.cpp:3201
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:352
GLAT_LOAD
@ GLAT_LOAD
Game loaded.
Definition: gamelog.h:18
SL_REF
@ SL_REF
Save/load a reference.
Definition: saveload.h:689
ZlibSaveFilter::Write
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2514
REF_ROADSTOPS
@ REF_ROADSTOPS
Load/save a reference to a bus/truck stop.
Definition: saveload.h:598
FileToSaveLoad::title
std::string title
Internal name of the game.
Definition: saveload.h:407
SL_STDSTR
@ SL_STDSTR
Save/load a std::string.
Definition: saveload.h:692
LZMALoadFilter::fread_buf
uint8_t fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
Definition: saveload.cpp:2546
SLE_FILE_END
@ SLE_FILE_END
Used to mark end-of-header in tables.
Definition: saveload.h:618
_gamelog
Gamelog _gamelog
Gamelog instance.
Definition: gamelog.cpp:31
Station
Station data structure.
Definition: station_base.h:439
SlSkipArray
void SlSkipArray()
Skip an array or sparse array.
Definition: saveload.cpp:700
LinkGraphJob
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:29
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:3092
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
SaveLoadOperation
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:53
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1029
NoCompLoadFilter::NoCompLoadFilter
NoCompLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload.cpp:2383
_ttdp_version
uint32_t _ttdp_version
version of TTDP savegame (if applicable)
Definition: saveload.cpp:62
FileToSaveLoad::name
std::string name
Name of the file.
Definition: saveload.h:406
SL_ARR
@ SL_ARR
Save/load a fixed-size array of SL_VAR elements.
Definition: saveload.h:694
FileReader::begin
long begin
The begin of the file.
Definition: saveload.cpp:2208
LZOSaveFilter::Write
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2346
MemoryDumper::WriteByte
void WriteByte(uint8_t b)
Write a single byte into the dumper.
Definition: saveload.cpp:137
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:41
AfterLoadGame
bool AfterLoadGame()
Perform a (large) amount of savegame conversion magic in order to load older savegames and to fill th...
Definition: afterload.cpp:564
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
SLF_ALLOW_NEWLINE
@ SLF_ALLOW_NEWLINE
Allow new lines in the strings.
Definition: saveload.h:681
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:351
SlSaveChunks
static void SlSaveChunks()
Save all chunks.
Definition: saveload.cpp:2139
SaveLoadFormat::max_compression
uint8_t max_compression
the maximum compression level of this format
Definition: saveload.cpp:2663
SLA_LOAD_CHECK
@ SLA_LOAD_CHECK
partial loading into _load_check_data
Definition: saveload.cpp:74
SLV_END_PATCHPACKS
@ SLV_END_PATCHPACKS
286 Last known patchpack to use a version just above ours.
Definition: saveload.h:322
SLO_CHECK
@ SLO_CHECK
Load file for checking and/or preview.
Definition: fileio_type.h:54
SaveLoadParams::extra_msg
std::string extra_msg
the error message
Definition: saveload.cpp:195
FixSCCEncoded
static void FixSCCEncoded(std::string &str)
Scan the string for old values of SCC_ENCODED and fix it to it's new, value.
Definition: saveload.cpp:914
StrMakeValid
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition: string.cpp:107
_do_autosave
bool _do_autosave
are we doing an autosave at the moment?
Definition: saveload.cpp:66
MemoryDumper::GetSize
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:171
DFT_GAME_FILE
@ DFT_GAME_FILE
Save game or scenario file.
Definition: fileio_type.h:32
FileToSaveLoad
Deals with the type of the savegame, independent of extension.
Definition: saveload.h:402
LoadCheckData::grfconfig
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:45
SLE_VAR_NULL
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition: saveload.h:645
SaveLoadHandler::GetLoadDescription
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Definition: saveload.cpp:3288
ChunkHandler::type
ChunkType type
Type of the chunk.
Definition: saveload.h:457
ReadBuffer
A buffer for reading (and buffering) savegame data.
Definition: saveload.cpp:87
TimerGameEconomy::date_fract
static DateFract date_fract
Fractional part of the day.
Definition: timer_game_economy.h:38
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:254
AUTOSAVE_DIR
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:118
SaveLoadCompatTable
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition: saveload.h:510
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
GetDetailedFileType
DetailedFileType GetDetailedFileType(FiosType fios_type)
Extract the detailed file type from a FiosType.
Definition: fileio_type.h:107
SlNullPointers
static void SlNullPointers()
Null all pointers (convert index -> nullptr)
Definition: saveload.cpp:296
MemoryDumper::bufe
uint8_t * bufe
End of the buffer we write to.
Definition: saveload.cpp:131
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:245
SaveFilter::chain
std::shared_ptr< SaveFilter > chain
Chained to the (savegame) filters.
Definition: saveload_filter.h:61
TimerGameEconomy::UsingWallclockUnits
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
Definition: timer_game_economy.cpp:97
Gamelog::Reset
void Reset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition: gamelog.cpp:94
SLA_LOAD
@ SLA_LOAD
loading
Definition: saveload.cpp:70
ZlibLoadFilter
Filter using Zlib compression.
Definition: saveload.cpp:2415
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:455
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:244
SaveLoad::conv
VarType conv
Type of the variable to be saved; this field combines both FileVarType and MemVarType.
Definition: saveload.h:710
SL_DEQUE
@ SL_DEQUE
Save/load a deque of SL_VAR elements.
Definition: saveload.h:695
SaveLoadAction
SaveLoadAction
What are we currently doing?
Definition: saveload.cpp:69
SL_SAVEBYTE
@ SL_SAVEBYTE
Save (but not load) a byte.
Definition: saveload.h:700
SaveLoadHandler
Handler for saving/loading an object to/from disk.
Definition: saveload.h:513
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:898
NL_NONE
@ NL_NONE
not working in NeedLength mode
Definition: saveload.cpp:78
SLV_169
@ SLV_169
169 23816
Definition: saveload.h:245
AsyncSaveFinishProc
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition: saveload.cpp:357
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:712
FileWriter::Write
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2261
ZlibSaveFilter::z
z_stream z
Stream state we are writing to.
Definition: saveload.cpp:2460
ZlibLoadFilter::ZlibLoadFilter
ZlibLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload.cpp:2425
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SlReadSimpleGamma
static uint SlReadSimpleGamma()
Read in the header descriptor of an object or an array.
Definition: saveload.cpp:452
SaveLoadFormat::init_load
std::shared_ptr< LoadFilter >(* init_load)(std::shared_ptr< LoadFilter > chain)
Constructor for the load filter.
Definition: saveload.cpp:2658
SLE_FILE_TYPE_MASK
@ SLE_FILE_TYPE_MASK
Mask to get the file-type (and not any flags).
Definition: saveload.h:632
DetermineSaveLoadFormat
static const SaveLoadFormat * DetermineSaveLoadFormat(uint32_t tag, uint32_t raw_version)
Determines the SaveLoadFormat that is connected to the given tag.
Definition: saveload.cpp:2931
saveload_filter.h
ReadValue
int64_t ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:793
SlGlobList
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1942
SlWriteByte
void SlWriteByte(uint8_t b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:401
LZMASaveFilter::WriteLoop
void WriteLoop(uint8_t *p, size_t len, lzma_action action)
Helper loop for writing the data.
Definition: saveload.cpp:2613
NoCompSaveFilter
Filter without any compression.
Definition: saveload.cpp:2392
REF_STATION
@ REF_STATION
Load/save a reference to a station.
Definition: saveload.h:595
LZMALoadFilter
Filter without any compression.
Definition: saveload.cpp:2544
NoCompLoadFilter::Read
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2387
SLV_69
@ SLV_69
69 10319
Definition: saveload.h:125
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:587
SaveLoadParams::lf
std::shared_ptr< LoadFilter > lf
Filter to read the savegame from.
Definition: saveload.cpp:192
AbstractFileType
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:16
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:1105
SaveLoadFormat
The format for a reader/writer type of a savegame.
Definition: saveload.cpp:2652
FileToSaveLoad::abstract_ftype
AbstractFileType abstract_ftype
Abstract type of file (scenario, heightmap, etc).
Definition: saveload.h:405
SVS_ALLOW_CONTROL_CODE
@ SVS_ALLOW_CONTROL_CODE
Allow the special control codes.
Definition: string_type.h:48
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition: fileio_type.h:116
LoadFilter::LoadFilter
LoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload_filter.h:22
NoCompSaveFilter::Write
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2401
FileReader::FileReader
FileReader(FileHandle &&file)
Create the file reader, so it reads from a specific file.
Definition: saveload.cpp:2214
GetVarMemType
constexpr VarType GetVarMemType(VarType type)
Get the NumberType of a setting.
Definition: saveload.h:741
SaveFileDone
static void SaveFileDone()
Update the gui accordingly when saving is done and release locks on saveload.
Definition: saveload.cpp:2781
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:680
SlSkipHandler
Handler that is assigned when there is a struct read in the savegame which is not known to the code.
Definition: saveload.cpp:1714
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:633
SLO_LOAD
@ SLO_LOAD
File is being loaded.
Definition: fileio_type.h:55
SaveFileError
static void SaveFileError()
Show a gui message when saving has failed.
Definition: saveload.cpp:2813
IsGoodGRFConfigList
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
Definition: newgrf_config.cpp:465
_sl_version
SaveLoadVersion _sl_version
the major savegame version identifier
Definition: saveload.cpp:63
SlFixPointers
static void SlFixPointers()
Fix all pointers (convert index -> pointer)
Definition: saveload.cpp:2192
BSWAP32
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:364
SavegameType
SavegameType
Types of save games.
Definition: saveload.h:415
SAVEGAME_VERSION
const SaveLoadVersion SAVEGAME_VERSION
Current savegame version of OpenTTD.
ZlibSaveFilter
Filter using Zlib compression.
Definition: saveload.cpp:2459
SLO_SAVE
@ SLO_SAVE
File is being saved.
Definition: fileio_type.h:56
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:1138
NeedLength
NeedLength
Definition: saveload.cpp:77
SlSaveLoadRef
void SlSaveLoadRef(void *ptr, VarType conv)
Handle conversion for references.
Definition: saveload.cpp:1214
CH_TYPE_MASK
@ CH_TYPE_MASK
All ChunkType values have to be within this mask.
Definition: saveload.h:450
LZOSaveFilter::LZOSaveFilter
LZOSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t)
Initialise this filter.
Definition: saveload.cpp:2341
NL_CALCLENGTH
@ NL_CALCLENGTH
need to calculate the length
Definition: saveload.cpp:80
ChunkHandler::Save
virtual void Save() const
Save the chunk.
Definition: saveload.h:467
CH_READONLY
@ CH_READONLY
Chunk is never saved.
Definition: saveload.h:451
SlCalcDequeLen
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Return the size in bytes of a std::deque.
Definition: saveload.cpp:1355
_savegame_format
std::string _savegame_format
how to compress savegames
Definition: saveload.cpp:65
SaveLoadParams::sf
std::shared_ptr< SaveFilter > sf
Filter to write the savegame to.
Definition: saveload.cpp:189
SaveLoadType
SaveLoadType
Type of data saved.
Definition: saveload.h:687
NL_WANTLENGTH
@ NL_WANTLENGTH
writing length and data
Definition: saveload.cpp:79
ReadBuffer::buf
uint8_t buf[MEMORY_CHUNK_SIZE]
Buffer we're going to read from.
Definition: saveload.cpp:88
LZOLoadFilter::Read
size_t Read(uint8_t *buf, size_t ssize) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2299
SanitizeFilename
void SanitizeFilename(std::string &filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1003
SlCopyInternal
static void SlCopyInternal(void *object, size_t length, VarType conv)
Internal function to save/Load a list of SL_VARs.
Definition: saveload.cpp:980
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:199
GetSavegameFormat
static std::pair< const SaveLoadFormat &, uint8_t > GetSavegameFormat(const std::string &full_name)
Return the savegameformat of the game.
Definition: saveload.cpp:2705
_savegame_type
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:59
SlLoadCheckChunks
static void SlLoadCheckChunks()
Load all chunks for savegame checking.
Definition: saveload.cpp:2177
MemoryDumper::Flush
void Flush(std::shared_ptr< SaveFilter > writer)
Flush this dumper into a writer.
Definition: saveload.cpp:152
CopyFromOldName
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:61
SlVector
static void SlVector(void *vector, VarType conv)
Save/load a std::vector.
Definition: saveload.cpp:1440
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:398
SaveLoad::cmd
SaveLoadType cmd
The action to take with the saved/loaded type, All types need different action.
Definition: saveload.h:709
FiosItem
Deals with finding savegames.
Definition: fios.h:79
ReadBuffer::ReadBuffer
ReadBuffer(std::shared_ptr< LoadFilter > reader)
Initialise our variables.
Definition: saveload.cpp:98
LZMALoadFilter::Read
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2564
SLRefType
SLRefType
Type of reference (SLE_REF, SLE_CONDREF).
Definition: saveload.h:592
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:47
SlSaveLoadConv
static void SlSaveLoadConv(void *ptr, VarType conv)
Handle all conversion and typechecking of variables here.
Definition: saveload.cpp:843
GetVariableAddress
void * GetVariableAddress(const void *object, const SaveLoad &sld)
Get the address of the variable.
Definition: saveload.h:1272
GameSessionStats::savegame_size
std::optional< size_t > savegame_size
Size of the last saved savegame in bytes, or std::nullopt if not saved yet.
Definition: openttd.h:58
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:52
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:2823
SaveLoadFormat::name
const char * name
name of the compressor/decompressor (debug-only)
Definition: saveload.cpp:2655
SlCalcRefListLen
static size_t SlCalcRefListLen(const void *list, VarType conv)
Return the size in bytes of a list.
Definition: saveload.cpp:1328
GenerateDefaultSaveName
std::string GenerateDefaultSaveName()
Get the default name for a savegame or screenshot.
Definition: saveload.cpp:3209
GUISettings::keep_all_autosave
bool keep_all_autosave
name the autosave in a different way
Definition: settings_type.h:170
ChunkHandlers
static const std::vector< ChunkHandlerRef > & ChunkHandlers()
Definition: saveload.cpp:202
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:280
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:331
SLE_FILE_STRINGID
@ SLE_FILE_STRINGID
StringID offset into strings-array.
Definition: saveload.h:627
REF_STORAGE
@ REF_STORAGE
Load/save a reference to a persistent storage.
Definition: saveload.h:602
SCENARIO_DIR
@ SCENARIO_DIR
Base directory for all scenarios.
Definition: fileio_type.h:119
FT_INVALID
@ FT_INVALID
Invalid or unknown file type.
Definition: fileio_type.h:23
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
FileHandle
Definition: fileio_type.h:158
SLA_PTRS
@ SLA_PTRS
fixing pointers
Definition: saveload.cpp:72
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:391
SaveFileStart
static void SaveFileStart()
Update the gui accordingly when starting saving and set locks on saveload.
Definition: saveload.cpp:2772
REF_ENGINE_RENEWS
@ REF_ENGINE_RENEWS
Load/save a reference to an engine renewal (autoreplace).
Definition: saveload.h:599
FileWriter::file
std::optional< FileHandle > file
The file to write to.
Definition: saveload.cpp:2245
SL_VECTOR
@ SL_VECTOR
Save/load a vector of SL_VAR elements.
Definition: saveload.h:696
DFT_OLD_GAME_FILE
@ DFT_OLD_GAME_FILE
Old save game or scenario file.
Definition: fileio_type.h:31
ReadBuffer::reader
std::shared_ptr< LoadFilter > reader
The filter used to actually read.
Definition: saveload.cpp:91
SaveLoadFormat::min_compression
uint8_t min_compression
the minimum compression level of this format
Definition: saveload.cpp:2661
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:594
SLV_4
@ SLV_4
4.0 1 4.1 122 0.3.3, 0.3.4 4.2 1222 0.3.5 4.3 1417 4.4 1426
Definition: saveload.h:37
DoAutoOrNetsave
void DoAutoOrNetsave(FiosNumberedSaveName &counter)
Create an autosave or netsave.
Definition: saveload.cpp:3183
REF_CARGO_PACKET
@ REF_CARGO_PACKET
Load/save a reference to a cargo packet.
Definition: saveload.h:600
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:2545
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:597
LZOLoadFilter::LZOLoadFilter
LZOLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload.cpp:2294
SaveLoadParams::error_str
StringID error_str
the translatable error message to show
Definition: saveload.cpp:194
SlAutolength
void SlAutolength(AutolengthProc *proc, int arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1952
ChunkHandler::Load
virtual void Load() const =0
Load the chunk.
_game_session_stats
GameSessionStats _game_session_stats
Statistics about the current session.
Definition: gfx.cpp:51
SlSkipHandler::GetDescription
virtual SaveLoadTable GetDescription() const override
Get the description of the fields in the savegame.
Definition: saveload.cpp:1733
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:44
SlGetStructListLength
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition: saveload.cpp:1684
SaveLoad::handler
std::shared_ptr< SaveLoadHandler > handler
Custom handler for Save/Load procs.
Definition: saveload.h:716
LoadCheckData::error
StringID error
Error message from loading. INVALID_STRING_ID if no error.
Definition: fios.h:35
FileReader::file
std::optional< FileHandle > file
The file to read from.
Definition: saveload.cpp:2207
ZlibSaveFilter::ZlibSaveFilter
ZlibSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Initialise this filter.
Definition: saveload.cpp:2468
SlCalcVectorLen
static size_t SlCalcVectorLen(const void *vector, VarType conv)
Return the size in bytes of a std::vector.
Definition: saveload.cpp:1413
SaveLoadParams::expect_table_header
bool expect_table_header
In the case of a table, if the header is saved/loaded.
Definition: saveload.cpp:186
SlLoadCheckChunk
static void SlLoadCheckChunk(const ChunkHandler &ch)
Load a chunk of data for checking savegames.
Definition: saveload.cpp:2052
LoadFilter::chain
std::shared_ptr< LoadFilter > chain
Chained to the (savegame) filters.
Definition: saveload_filter.h:16
REF_LINK_GRAPH_JOB
@ REF_LINK_GRAPH_JOB
Load/save a reference to a link graph job.
Definition: saveload.h:604
ZlibLoadFilter::z
z_stream z
Stream state we are reading from.
Definition: saveload.cpp:2418
LZMASaveFilter::fwrite_buf
uint8_t fwrite_buf[MEMORY_CHUNK_SIZE]
Buffer for writing to the file.
Definition: saveload.cpp:2589
LZMALoadFilter::~LZMALoadFilter
~LZMALoadFilter()
Clean everything up.
Definition: saveload.cpp:2559
DFT_INVALID
@ DFT_INVALID
Unknown or invalid file.
Definition: fileio_type.h:49
_sl
static SaveLoadParams _sl
Parameters used for/at saveload.
Definition: saveload.cpp:200
Gamelog::StartAction
void StartAction(GamelogActionType at)
Stores information about new action, but doesn't allocate it Action is allocated only when there is a...
Definition: gamelog.cpp:65
Gamelog::StopAction
void StopAction()
Stops logging of any changes.
Definition: gamelog.cpp:74
SaveLoadParams::need_length
NeedLength need_length
working in NeedLength (Autolength) mode?
Definition: saveload.cpp:180
Utf8EncodedCharLen
int8_t Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
Definition: string_func.h:124
_sl_minor_version
uint8_t _sl_minor_version
the minor savegame version, DO NOT USE!
Definition: saveload.cpp:64
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:388
SaveWithFilter
SaveOrLoadResult SaveWithFilter(std::shared_ptr< SaveFilter > writer, bool threaded)
Save the game using a (writer) filter.
Definition: saveload.cpp:2912
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:1247
GetSavegameFileType
static uint8_t GetSavegameFileType(const SaveLoad &sld)
Return the type as saved/loaded inside the savegame.
Definition: saveload.cpp:552
GUISettings::threaded_saves
bool threaded_saves
should we do threaded saves?
Definition: settings_type.h:169
FT_NONE
@ FT_NONE
nothing to do
Definition: fileio_type.h:17
FiosNumberedSaveName
A savegame name automatically numbered.
Definition: fios.h:130
REF_ORDERLIST
@ REF_ORDERLIST
Load/save a reference to an orderlist.
Definition: saveload.h:601
SaveLoadParams
The saveload struct, containing reader-writer functions, buffer, version, etc.
Definition: saveload.cpp:178
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:781
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:1048
DetailedFileType
DetailedFileType
Kinds of files in each AbstractFileType.
Definition: fileio_type.h:29
LoadCheckData::Clear
void Clear()
Reset read data.
Definition: fios_gui.cpp:49
SlStorageHelper
Template class to help with list-like types.
Definition: saveload.cpp:1238
_saveload_formats
static const SaveLoadFormat _saveload_formats[]
The different saveload formats known/understood by OpenTTD.
Definition: saveload.cpp:2670
SLV_START_PATCHPACKS
@ SLV_START_PATCHPACKS
220 First known patchpack to use a version just above ours.
Definition: saveload.h:321
FileReader::Reset
void Reset() override
Reset this filter to read from the beginning of the file.
Definition: saveload.cpp:2234
SlReadByte
uint8_t SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:392
FileToSaveLoad::Set
void Set(const FiosItem &item)
Set the title of the file.
Definition: saveload.cpp:3281
GUISettings::date_format_in_default_names
uint8_t 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:173
SaveFilter
Interface for filtering a savegame till it is written.
Definition: saveload_filter.h:59
SetAsyncSaveFinish
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
Called by save thread to tell we finished saving.
Definition: saveload.cpp:365
GetSaveLoadErrorType
StringID GetSaveLoadErrorType()
Return the appropriate initial string for an error depending on whether we are saving or loading.
Definition: saveload.cpp:2800
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
SaveLoadParams::action
SaveLoadAction action
are we doing a save or a load atm.
Definition: saveload.cpp:179
_file_to_saveload
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:60
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:259
SlGetGammaLength
static uint SlGetGammaLength(size_t i)
Return how many bytes used to encode a gamma value.
Definition: saveload.cpp:519
ZlibSaveFilter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2519
SaveLoadParams::dumper
std::unique_ptr< MemoryDumper > dumper
Memory dumper to write the savegame to.
Definition: saveload.cpp:188
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:319
LZOSaveFilter
Filter using LZO compression.
Definition: saveload.cpp:2336
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:504
SaveLoadParams::obj_len
size_t obj_len
the length of the current object we are busy with
Definition: saveload.cpp:184
LZMASaveFilter::~LZMASaveFilter
~LZMASaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2602
SlSkipBytes
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:1316
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
SetDParamStr
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:344
SaveLoadFormat::tag
uint32_t tag
the 4-letter tag by which it is identified in the savegame
Definition: saveload.cpp:2656
SlWriteSimpleGamma
static void SlWriteSimpleGamma(size_t i)
Write the header descriptor of an object or an array.
Definition: saveload.cpp:494
SaveLoad::version_from
SaveLoadVersion version_from
Save/load the variable starting from this savegame version.
Definition: saveload.h:712
LoadCheckData::grf_compatibility
GRFListCompatibility grf_compatibility
Summary state of NewGrfs, whether missing files or only compatible found.
Definition: fios.h:46
SaveOrLoadResult
SaveOrLoadResult
Save or load result codes.
Definition: saveload.h:395
FileWriter::FileWriter
FileWriter(FileHandle &&file)
Create the file writer, so it writes to a specific file.
Definition: saveload.cpp:2251
FioFOpenFile
std::optional< FileHandle > 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:242
WL_ERROR
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:26
FiosType
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:73
SLE_VAR_STRQ
@ SLE_VAR_STRQ
string pointer enclosed in quotes
Definition: saveload.h:647
SaveLoadParams::saveinprogress
bool saveinprogress
Whether there is currently a save in progress.
Definition: saveload.cpp:197
ZlibLoadFilter::Read
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2437
FileReader::Read
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2226
SaveLoadParams::reader
std::unique_ptr< ReadBuffer > reader
Savegame reading buffer.
Definition: saveload.cpp:191
Ticks::TICKS_PER_SECOND
static constexpr TimerGameTick::Ticks TICKS_PER_SECOND
Estimation of how many ticks fit in a single second.
Definition: timer_game_tick.h:76
SlRefList
static void SlRefList(void *list, VarType conv)
Save/Load a list.
Definition: saveload.cpp:1338
ZlibSaveFilter::fwrite_buf
uint8_t fwrite_buf[MEMORY_CHUNK_SIZE]
Buffer for writing to the file.
Definition: saveload.cpp:2461
ReadBuffer::read
size_t read
The amount of read bytes so far from the filter.
Definition: saveload.cpp:92
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:164
REF_LINK_GRAPH
@ REF_LINK_GRAPH
Load/save a reference to a link graph.
Definition: saveload.h:603
saveload_internal.h
SLO_INVALID
@ SLO_INVALID
Unknown file operation.
Definition: fileio_type.h:58
SLA_NULL
@ SLA_NULL
null all pointers (on loading error)
Definition: saveload.cpp:73
SlStdString
static void SlStdString(void *ptr, VarType conv)
Save/Load a std::string.
Definition: saveload.cpp:932
LoadFilter
Interface for filtering a savegame till it is loaded.
Definition: saveload_filter.h:14
Town
Town data structure.
Definition: town.h:54
SaveLoadParams::error
bool error
did an error occur or not
Definition: saveload.cpp:182
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:40
LoadWithFilter
SaveOrLoadResult LoadWithFilter(std::shared_ptr< LoadFilter > reader)
Load the game using a (reader) filter.
Definition: saveload.cpp:3072
ChunkHandler::id
uint32_t id
Unique ID (4 letters).
Definition: saveload.h:456
SaveLoadFormat::init_write
std::shared_ptr< SaveFilter >(* init_write)(std::shared_ptr< SaveFilter > chain, uint8_t compression)
Constructor for the save filter.
Definition: saveload.cpp:2659
FileReader::~FileReader
~FileReader()
Make sure everything is cleaned up.
Definition: saveload.cpp:2219
SlError
void SlError(StringID string, const std::string &extra_msg)
Error handler.
Definition: saveload.cpp:321
SaveLoadParams::block_mode
uint8_t block_mode
???
Definition: saveload.cpp:181
GetAbstractFileType
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition: fileio_type.h:97
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1888
SlCopyBytes
static void SlCopyBytes(void *ptr, size_t length)
Save/Load bytes.
Definition: saveload.cpp:764
LZOLoadFilter
Filter using LZO compression.
Definition: saveload.cpp:2289
MEMORY_CHUNK_SIZE
static const size_t MEMORY_CHUNK_SIZE
Save in chunks of 128 KiB.
Definition: saveload.cpp:84
ProcessAsyncSaveFinish
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition: saveload.cpp:376
FileReader
Yes, simply reading from a file.
Definition: saveload.cpp:2206
SL_REFLIST
@ SL_REFLIST
Save/load a list of SL_REF elements.
Definition: saveload.h:697
ReadBuffer::bufe
uint8_t * bufe
End of the buffer we can read from.
Definition: saveload.cpp:90
SaveLoadParams::last_array_index
int last_array_index
in the case of an array, the current and last positions
Definition: saveload.cpp:185
SlCalcRefLen
static size_t SlCalcRefLen()
Return the size in bytes of a reference (pointer)
Definition: saveload.cpp:641
GetSaveLoadErrorMessage
StringID GetSaveLoadErrorMessage()
Return the description of the error.
Definition: saveload.cpp:2806
LZMASaveFilter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2638
Utf8Encode
size_t Utf8Encode(T buf, char32_t c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:460
_async_save_finish
static std::atomic< AsyncSaveFinishProc > _async_save_finish
Callback to call when the savegame loading is finished.
Definition: saveload.cpp:358
SL_ERROR
@ SL_ERROR
error that was caught before internal structures were modified
Definition: saveload.h:397
FileToSaveLoad::detail_ftype
DetailedFileType detail_ftype
Concrete file type (PNG, BMP, old save, etc).
Definition: saveload.h:404
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:328
SaveLoad::length
uint16_t length
(Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
Definition: saveload.h:711
WC_STATUS_BAR
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:64
ReadBuffer::GetSize
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:120
RoadStop
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
FiosNumberedSaveName::Extension
std::string Extension()
Generate an extension for a savegame name.
Definition: fios.cpp:773
SaveLoad::name
std::string name
Name of this field (optional, used for tables).
Definition: saveload.h:708
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
ZlibSaveFilter::~ZlibSaveFilter
~ZlibSaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2475
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1234
ChunkHandler::LoadCheck
virtual void LoadCheck(size_t len=0) const
Load the chunk for game preview.
Definition: saveload.cpp:1976
Utf8Decode
size_t Utf8Decode(char32_t *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:419
LZMASaveFilter
Filter using LZMA compression.
Definition: saveload.cpp:2587
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
SL_STRUCTLIST
@ SL_STRUCTLIST
Save/load a list of structs.
Definition: saveload.h:698
SlSetStructListLength
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition: saveload.cpp:1668
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:3252
SlSkipHandler::GetCompatDescription
virtual SaveLoadCompatTable GetCompatDescription() const override
Get the pre-header description of the fields in the savegame.
Definition: saveload.cpp:1738
ReadBuffer::bufp
uint8_t * bufp
Location we're at reading the buffer.
Definition: saveload.cpp:89
SaveLoad
SaveLoad type struct.
Definition: saveload.h:707
Company
Definition: company_base.h:133
ClearSaveLoadState
static void ClearSaveLoadState()
Clear/free saveload state.
Definition: saveload.cpp:2763
DoSave
static SaveOrLoadResult DoSave(std::shared_ptr< SaveFilter > writer, bool threaded)
Actually perform the saving of the savegame.
Definition: saveload.cpp:2880
ZlibLoadFilter::fread_buf
uint8_t fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
Definition: saveload.cpp:2419
MemoryDumper::blocks
std::vector< std::unique_ptr< uint8_t[]> > blocks
Buffer with blocks of allocated memory.
Definition: saveload.cpp:129
SL_OK
@ SL_OK
completed successfully
Definition: saveload.h:396
SaveLoadFormat::default_compression
uint8_t default_compression
the default compression level of this format
Definition: saveload.cpp:2662
SVS_REPLACE_WITH_QUESTION_MARK
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:46
FiosNumberedSaveName::Filename
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition: fios.cpp:763
Order
Definition: order_base.h:36
_lzma_init
static const lzma_stream _lzma_init
Have a copy of an initialised LZMA stream.
Definition: saveload.cpp:2541
ZlibSaveFilter::WriteLoop
void WriteLoop(uint8_t *p, size_t len, int mode)
Helper loop for writing the data.
Definition: saveload.cpp:2486
SLE_VAR_NAME
@ SLE_VAR_NAME
old custom name to be converted to a char pointer
Definition: saveload.h:648
LZMALoadFilter::LZMALoadFilter
LZMALoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload.cpp:2552
FileWriter::~FileWriter
~FileWriter()
Make sure everything is cleaned up.
Definition: saveload.cpp:2256
SlCalcObjLength
size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
Calculate the size of an object.
Definition: saveload.cpp:1506
SL_STRUCT
@ SL_STRUCT
Save/load a struct.
Definition: saveload.h:690
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
WriteValue
void WriteValue(void *ptr, VarType conv, int64_t val)
Write the value of a setting.
Definition: saveload.cpp:817
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:2103
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:611
WL_CRITICAL
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:27
SlFindChunkHandler
static const ChunkHandler * SlFindChunkHandler(uint32_t id)
Find the ChunkHandler that will be used for processing the found chunk in the savegame or in memory.
Definition: saveload.cpp:2155
FileWriter::Finish
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2269
FileToSaveLoad::file_op
SaveLoadOperation file_op
File operation to perform.
Definition: saveload.h:403
FileWriter
Yes, simply writing to a file.
Definition: saveload.cpp:2244
GetVarFileType
constexpr VarType GetVarFileType(VarType type)
Get the FileType of a setting.
Definition: saveload.h:752
MemoryDumper::buf
uint8_t * buf
Buffer we're going to write to.
Definition: saveload.cpp:130
MemoryDumper
Container for dumping the savegame (quickly) to memory.
Definition: saveload.cpp:128
DoLoad
static SaveOrLoadResult DoLoad(std::shared_ptr< LoadFilter > reader, bool load_check)
Actually perform the loading of a "non-old" savegame.
Definition: saveload.cpp:2972
LZMASaveFilter::LZMASaveFilter
LZMASaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Initialise this filter.
Definition: saveload.cpp:2596
TimerGameEconomy::date
static Date date
Current date in days (day counter).
Definition: timer_game_economy.h:37
NoCompSaveFilter::NoCompSaveFilter
NoCompSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t)
Initialise this filter.
Definition: saveload.cpp:2397
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103