OpenTTD Source  20241120-master-g6d3adc6169
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  list->clear();
1301  if constexpr (std::is_same_v<SlStorageT, std::vector<Tvar, Tallocator>>) {
1302  list->reserve(length);
1303  }
1304 
1305  /* Load each value and push to the end of the storage. */
1306  for (size_t i = 0; i < length; i++) {
1307  Tvar &data = list->emplace_back();
1308  SlSaveLoadMember(cmd, &data, conv);
1309  }
1310  break;
1311  }
1312 
1313  case SLA_PTRS:
1314  for (auto &item : *list) {
1315  SlSaveLoadMember(cmd, &item, conv);
1316  }
1317  break;
1318 
1319  case SLA_NULL:
1320  list->clear();
1321  break;
1322 
1323  default: NOT_REACHED();
1324  }
1325  }
1326 };
1327 
1333 static inline size_t SlCalcRefListLen(const void *list, VarType conv)
1334 {
1336 }
1337 
1343 static void SlRefList(void *list, VarType conv)
1344 {
1345  /* Automatically calculate the length? */
1346  if (_sl.need_length != NL_NONE) {
1347  SlSetLength(SlCalcRefListLen(list, conv));
1348  /* Determine length only? */
1349  if (_sl.need_length == NL_CALCLENGTH) return;
1350  }
1351 
1353 }
1354 
1360 static inline size_t SlCalcDequeLen(const void *deque, VarType conv)
1361 {
1362  switch (GetVarMemType(conv)) {
1363  case SLE_VAR_BL: return SlStorageHelper<std::deque, bool>::SlCalcLen(deque, conv);
1364  case SLE_VAR_I8: return SlStorageHelper<std::deque, int8_t>::SlCalcLen(deque, conv);
1365  case SLE_VAR_U8: return SlStorageHelper<std::deque, uint8_t>::SlCalcLen(deque, conv);
1366  case SLE_VAR_I16: return SlStorageHelper<std::deque, int16_t>::SlCalcLen(deque, conv);
1367  case SLE_VAR_U16: return SlStorageHelper<std::deque, uint16_t>::SlCalcLen(deque, conv);
1368  case SLE_VAR_I32: return SlStorageHelper<std::deque, int32_t>::SlCalcLen(deque, conv);
1369  case SLE_VAR_U32: return SlStorageHelper<std::deque, uint32_t>::SlCalcLen(deque, conv);
1370  case SLE_VAR_I64: return SlStorageHelper<std::deque, int64_t>::SlCalcLen(deque, conv);
1371  case SLE_VAR_U64: return SlStorageHelper<std::deque, uint64_t>::SlCalcLen(deque, conv);
1372 
1373  case SLE_VAR_STR:
1374  /* Strings are a length-prefixed field type in the savegame table format,
1375  * these may not be directly stored in another length-prefixed container type. */
1376  NOT_REACHED();
1377 
1378  default: NOT_REACHED();
1379  }
1380 }
1381 
1387 static void SlDeque(void *deque, VarType conv)
1388 {
1389  switch (GetVarMemType(conv)) {
1390  case SLE_VAR_BL: SlStorageHelper<std::deque, bool>::SlSaveLoad(deque, conv); break;
1391  case SLE_VAR_I8: SlStorageHelper<std::deque, int8_t>::SlSaveLoad(deque, conv); break;
1392  case SLE_VAR_U8: SlStorageHelper<std::deque, uint8_t>::SlSaveLoad(deque, conv); break;
1393  case SLE_VAR_I16: SlStorageHelper<std::deque, int16_t>::SlSaveLoad(deque, conv); break;
1394  case SLE_VAR_U16: SlStorageHelper<std::deque, uint16_t>::SlSaveLoad(deque, conv); break;
1395  case SLE_VAR_I32: SlStorageHelper<std::deque, int32_t>::SlSaveLoad(deque, conv); break;
1396  case SLE_VAR_U32: SlStorageHelper<std::deque, uint32_t>::SlSaveLoad(deque, conv); break;
1397  case SLE_VAR_I64: SlStorageHelper<std::deque, int64_t>::SlSaveLoad(deque, conv); break;
1398  case SLE_VAR_U64: SlStorageHelper<std::deque, uint64_t>::SlSaveLoad(deque, conv); break;
1399 
1400  case SLE_VAR_STR:
1401  /* Strings are a length-prefixed field type in the savegame table format,
1402  * these may not be directly stored in another length-prefixed container type.
1403  * This is permitted for load-related actions, because invalid fields of this type are present
1404  * from SLV_COMPANY_ALLOW_LIST up to SLV_COMPANY_ALLOW_LIST_V2. */
1405  assert(_sl.action != SLA_SAVE);
1407  break;
1408 
1409  default: NOT_REACHED();
1410  }
1411 }
1412 
1418 static inline size_t SlCalcVectorLen(const void *vector, VarType conv)
1419 {
1420  switch (GetVarMemType(conv)) {
1421  case SLE_VAR_BL: NOT_REACHED(); // Not supported
1422  case SLE_VAR_I8: return SlStorageHelper<std::vector, int8_t>::SlCalcLen(vector, conv);
1423  case SLE_VAR_U8: return SlStorageHelper<std::vector, uint8_t>::SlCalcLen(vector, conv);
1424  case SLE_VAR_I16: return SlStorageHelper<std::vector, int16_t>::SlCalcLen(vector, conv);
1425  case SLE_VAR_U16: return SlStorageHelper<std::vector, uint16_t>::SlCalcLen(vector, conv);
1426  case SLE_VAR_I32: return SlStorageHelper<std::vector, int32_t>::SlCalcLen(vector, conv);
1427  case SLE_VAR_U32: return SlStorageHelper<std::vector, uint32_t>::SlCalcLen(vector, conv);
1428  case SLE_VAR_I64: return SlStorageHelper<std::vector, int64_t>::SlCalcLen(vector, conv);
1429  case SLE_VAR_U64: return SlStorageHelper<std::vector, uint64_t>::SlCalcLen(vector, conv);
1430 
1431  case SLE_VAR_STR:
1432  /* Strings are a length-prefixed field type in the savegame table format,
1433  * these may not be directly stored in another length-prefixed container type. */
1434  NOT_REACHED();
1435 
1436  default: NOT_REACHED();
1437  }
1438 }
1439 
1445 static void SlVector(void *vector, VarType conv)
1446 {
1447  switch (GetVarMemType(conv)) {
1448  case SLE_VAR_BL: NOT_REACHED(); // Not supported
1449  case SLE_VAR_I8: SlStorageHelper<std::vector, int8_t>::SlSaveLoad(vector, conv); break;
1450  case SLE_VAR_U8: SlStorageHelper<std::vector, uint8_t>::SlSaveLoad(vector, conv); break;
1451  case SLE_VAR_I16: SlStorageHelper<std::vector, int16_t>::SlSaveLoad(vector, conv); break;
1452  case SLE_VAR_U16: SlStorageHelper<std::vector, uint16_t>::SlSaveLoad(vector, conv); break;
1453  case SLE_VAR_I32: SlStorageHelper<std::vector, int32_t>::SlSaveLoad(vector, conv); break;
1454  case SLE_VAR_U32: SlStorageHelper<std::vector, uint32_t>::SlSaveLoad(vector, conv); break;
1455  case SLE_VAR_I64: SlStorageHelper<std::vector, int64_t>::SlSaveLoad(vector, conv); break;
1456  case SLE_VAR_U64: SlStorageHelper<std::vector, uint64_t>::SlSaveLoad(vector, conv); break;
1457 
1458  case SLE_VAR_STR:
1459  /* Strings are a length-prefixed field type in the savegame table format,
1460  * these may not be directly stored in another length-prefixed container type.
1461  * This is permitted for load-related actions, because invalid fields of this type are present
1462  * from SLV_COMPANY_ALLOW_LIST up to SLV_COMPANY_ALLOW_LIST_V2. */
1463  assert(_sl.action != SLA_SAVE);
1465  break;
1466 
1467  default: NOT_REACHED();
1468  }
1469 }
1470 
1472 static inline bool SlIsObjectValidInSavegame(const SaveLoad &sld)
1473 {
1474  return (_sl_version >= sld.version_from && _sl_version < sld.version_to);
1475 }
1476 
1482 static size_t SlCalcTableHeader(const SaveLoadTable &slt)
1483 {
1484  size_t length = 0;
1485 
1486  for (auto &sld : slt) {
1487  if (!SlIsObjectValidInSavegame(sld)) continue;
1488 
1489  length += SlCalcConvFileLen(SLE_UINT8);
1490  length += SlCalcStdStringLen(&sld.name);
1491  }
1492 
1493  length += SlCalcConvFileLen(SLE_UINT8); // End-of-list entry.
1494 
1495  for (auto &sld : slt) {
1496  if (!SlIsObjectValidInSavegame(sld)) continue;
1497  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1498  length += SlCalcTableHeader(sld.handler->GetDescription());
1499  }
1500  }
1501 
1502  return length;
1503 }
1504 
1511 size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
1512 {
1513  size_t length = 0;
1514 
1515  /* Need to determine the length and write a length tag. */
1516  for (auto &sld : slt) {
1517  length += SlCalcObjMemberLength(object, sld);
1518  }
1519  return length;
1520 }
1521 
1522 size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld)
1523 {
1524  assert(_sl.action == SLA_SAVE);
1525 
1526  if (!SlIsObjectValidInSavegame(sld)) return 0;
1527 
1528  switch (sld.cmd) {
1529  case SL_VAR: return SlCalcConvFileLen(sld.conv);
1530  case SL_REF: return SlCalcRefLen();
1531  case SL_ARR: return SlCalcArrayLen(sld.length, sld.conv);
1532  case SL_REFLIST: return SlCalcRefListLen(GetVariableAddress(object, sld), sld.conv);
1533  case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld.conv);
1534  case SL_VECTOR: return SlCalcVectorLen(GetVariableAddress(object, sld), sld.conv);
1535  case SL_STDSTR: return SlCalcStdStringLen(GetVariableAddress(object, sld));
1536  case SL_SAVEBYTE: return 1; // a byte is logically of size 1
1537  case SL_NULL: return SlCalcConvFileLen(sld.conv) * sld.length;
1538 
1539  case SL_STRUCT:
1540  case SL_STRUCTLIST: {
1541  NeedLength old_need_length = _sl.need_length;
1542  size_t old_obj_len = _sl.obj_len;
1543 
1545  _sl.obj_len = 0;
1546 
1547  /* Pretend that we are saving to collect the object size. Other
1548  * means are difficult, as we don't know the length of the list we
1549  * are about to store. */
1550  sld.handler->Save(const_cast<void *>(object));
1551  size_t length = _sl.obj_len;
1552 
1553  _sl.obj_len = old_obj_len;
1554  _sl.need_length = old_need_length;
1555 
1556  if (sld.cmd == SL_STRUCT) {
1557  length += SlGetArrayLength(1);
1558  }
1559 
1560  return length;
1561  }
1562 
1563  default: NOT_REACHED();
1564  }
1565  return 0;
1566 }
1567 
1568 static bool SlObjectMember(void *object, const SaveLoad &sld)
1569 {
1570  if (!SlIsObjectValidInSavegame(sld)) return false;
1571 
1572  VarType conv = GB(sld.conv, 0, 8);
1573  switch (sld.cmd) {
1574  case SL_VAR:
1575  case SL_REF:
1576  case SL_ARR:
1577  case SL_REFLIST:
1578  case SL_DEQUE:
1579  case SL_VECTOR:
1580  case SL_STDSTR: {
1581  void *ptr = GetVariableAddress(object, sld);
1582 
1583  switch (sld.cmd) {
1584  case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1585  case SL_REF: SlSaveLoadRef(ptr, conv); break;
1586  case SL_ARR: SlArray(ptr, sld.length, conv); break;
1587  case SL_REFLIST: SlRefList(ptr, conv); break;
1588  case SL_DEQUE: SlDeque(ptr, conv); break;
1589  case SL_VECTOR: SlVector(ptr, conv); break;
1590  case SL_STDSTR: SlStdString(ptr, sld.conv); break;
1591  default: NOT_REACHED();
1592  }
1593  break;
1594  }
1595 
1596  /* SL_SAVEBYTE writes a value to the savegame to identify the type of an object.
1597  * When loading, the value is read explicitly with SlReadByte() to determine which
1598  * object description to use. */
1599  case SL_SAVEBYTE: {
1600  void *ptr = GetVariableAddress(object, sld);
1601 
1602  switch (_sl.action) {
1603  case SLA_SAVE: SlWriteByte(*(uint8_t *)ptr); break;
1604  case SLA_LOAD_CHECK:
1605  case SLA_LOAD:
1606  case SLA_PTRS:
1607  case SLA_NULL: break;
1608  default: NOT_REACHED();
1609  }
1610  break;
1611  }
1612 
1613  case SL_NULL: {
1614  assert(GetVarMemType(sld.conv) == SLE_VAR_NULL);
1615 
1616  switch (_sl.action) {
1617  case SLA_LOAD_CHECK:
1618  case SLA_LOAD: SlSkipBytes(SlCalcConvFileLen(sld.conv) * sld.length); break;
1619  case SLA_SAVE: for (int i = 0; i < SlCalcConvFileLen(sld.conv) * sld.length; i++) SlWriteByte(0); break;
1620  case SLA_PTRS:
1621  case SLA_NULL: break;
1622  default: NOT_REACHED();
1623  }
1624  break;
1625  }
1626 
1627  case SL_STRUCT:
1628  case SL_STRUCTLIST:
1629  switch (_sl.action) {
1630  case SLA_SAVE: {
1631  if (sld.cmd == SL_STRUCT) {
1632  /* Store in the savegame if this struct was written or not. */
1633  SlSetStructListLength(SlCalcObjMemberLength(object, sld) > SlGetArrayLength(1) ? 1 : 0);
1634  }
1635  sld.handler->Save(object);
1636  break;
1637  }
1638 
1639  case SLA_LOAD_CHECK: {
1642  }
1643  sld.handler->LoadCheck(object);
1644  break;
1645  }
1646 
1647  case SLA_LOAD: {
1650  }
1651  sld.handler->Load(object);
1652  break;
1653  }
1654 
1655  case SLA_PTRS:
1656  sld.handler->FixPointers(object);
1657  break;
1658 
1659  case SLA_NULL: break;
1660  default: NOT_REACHED();
1661  }
1662  break;
1663 
1664  default: NOT_REACHED();
1665  }
1666  return true;
1667 }
1668 
1673 void SlSetStructListLength(size_t length)
1674 {
1675  /* Automatically calculate the length? */
1676  if (_sl.need_length != NL_NONE) {
1677  SlSetLength(SlGetArrayLength(length));
1678  if (_sl.need_length == NL_CALCLENGTH) return;
1679  }
1680 
1681  SlWriteArrayLength(length);
1682 }
1683 
1689 size_t SlGetStructListLength(size_t limit)
1690 {
1691  size_t length = SlReadArrayLength();
1692  if (length > limit) SlErrorCorrupt("List exceeds storage size");
1693 
1694  return length;
1695 }
1696 
1702 void SlObject(void *object, const SaveLoadTable &slt)
1703 {
1704  /* Automatically calculate the length? */
1705  if (_sl.need_length != NL_NONE) {
1706  SlSetLength(SlCalcObjLength(object, slt));
1707  if (_sl.need_length == NL_CALCLENGTH) return;
1708  }
1709 
1710  for (auto &sld : slt) {
1711  SlObjectMember(object, sld);
1712  }
1713 }
1714 
1720  void Save(void *) const override
1721  {
1722  NOT_REACHED();
1723  }
1724 
1725  void Load(void *object) const override
1726  {
1727  size_t length = SlGetStructListLength(UINT32_MAX);
1728  for (; length > 0; length--) {
1729  SlObject(object, this->GetLoadDescription());
1730  }
1731  }
1732 
1733  void LoadCheck(void *object) const override
1734  {
1735  this->Load(object);
1736  }
1737 
1738  virtual SaveLoadTable GetDescription() const override
1739  {
1740  return {};
1741  }
1742 
1744  {
1745  NOT_REACHED();
1746  }
1747 };
1748 
1755 std::vector<SaveLoad> SlTableHeader(const SaveLoadTable &slt)
1756 {
1757  /* You can only use SlTableHeader if you are a CH_TABLE. */
1758  assert(_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
1759 
1760  switch (_sl.action) {
1761  case SLA_LOAD_CHECK:
1762  case SLA_LOAD: {
1763  std::vector<SaveLoad> saveloads;
1764 
1765  /* Build a key lookup mapping based on the available fields. */
1766  std::map<std::string, const SaveLoad *> key_lookup;
1767  for (auto &sld : slt) {
1768  if (!SlIsObjectValidInSavegame(sld)) continue;
1769 
1770  /* Check that there is only one active SaveLoad for a given name. */
1771  assert(key_lookup.find(sld.name) == key_lookup.end());
1772  key_lookup[sld.name] = &sld;
1773  }
1774 
1775  while (true) {
1776  uint8_t type = 0;
1777  SlSaveLoadConv(&type, SLE_UINT8);
1778  if (type == SLE_FILE_END) break;
1779 
1780  std::string key;
1781  SlStdString(&key, SLE_STR);
1782 
1783  auto sld_it = key_lookup.find(key);
1784  if (sld_it == key_lookup.end()) {
1785  /* SLA_LOADCHECK triggers this debug statement a lot and is perfectly normal. */
1786  Debug(sl, _sl.action == SLA_LOAD ? 2 : 6, "Field '{}' of type 0x{:02x} not found, skipping", key, type);
1787 
1788  std::shared_ptr<SaveLoadHandler> handler = nullptr;
1789  SaveLoadType saveload_type;
1790  switch (type & SLE_FILE_TYPE_MASK) {
1791  case SLE_FILE_STRING:
1792  /* Strings are always marked with SLE_FILE_HAS_LENGTH_FIELD, as they are a list of chars. */
1793  saveload_type = SL_STDSTR;
1794  break;
1795 
1796  case SLE_FILE_STRUCT:
1797  /* Structs are always marked with SLE_FILE_HAS_LENGTH_FIELD as SL_STRUCT is seen as a list of 0/1 in length. */
1798  saveload_type = SL_STRUCTLIST;
1799  handler = std::make_shared<SlSkipHandler>();
1800  break;
1801 
1802  default:
1803  saveload_type = (type & SLE_FILE_HAS_LENGTH_FIELD) ? SL_ARR : SL_VAR;
1804  break;
1805  }
1806 
1807  /* We don't know this field, so read to nothing. */
1808  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});
1809  continue;
1810  }
1811 
1812  /* Validate the type of the field. If it is changed, the
1813  * savegame should have been bumped so we know how to do the
1814  * conversion. If this error triggers, that clearly didn't
1815  * happen and this is a friendly poke to the developer to bump
1816  * the savegame version and add conversion code. */
1817  uint8_t correct_type = GetSavegameFileType(*sld_it->second);
1818  if (correct_type != type) {
1819  Debug(sl, 1, "Field type for '{}' was expected to be 0x{:02x} but 0x{:02x} was found", key, correct_type, type);
1820  SlErrorCorrupt("Field type is different than expected");
1821  }
1822  saveloads.push_back(*sld_it->second);
1823  }
1824 
1825  for (auto &sld : saveloads) {
1826  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1827  sld.handler->load_description = SlTableHeader(sld.handler->GetDescription());
1828  }
1829  }
1830 
1831  return saveloads;
1832  }
1833 
1834  case SLA_SAVE: {
1835  /* Automatically calculate the length? */
1836  if (_sl.need_length != NL_NONE) {
1838  if (_sl.need_length == NL_CALCLENGTH) break;
1839  }
1840 
1841  for (auto &sld : slt) {
1842  if (!SlIsObjectValidInSavegame(sld)) continue;
1843  /* Make sure we are not storing empty keys. */
1844  assert(!sld.name.empty());
1845 
1846  uint8_t type = GetSavegameFileType(sld);
1847  assert(type != SLE_FILE_END);
1848 
1849  SlSaveLoadConv(&type, SLE_UINT8);
1850  SlStdString(const_cast<std::string *>(&sld.name), SLE_STR);
1851  }
1852 
1853  /* Add an end-of-header marker. */
1854  uint8_t type = SLE_FILE_END;
1855  SlSaveLoadConv(&type, SLE_UINT8);
1856 
1857  /* After the table, write down any sub-tables we might have. */
1858  for (auto &sld : slt) {
1859  if (!SlIsObjectValidInSavegame(sld)) continue;
1860  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1861  /* SlCalcTableHeader already looks in sub-lists, so avoid the length being added twice. */
1862  NeedLength old_need_length = _sl.need_length;
1864 
1865  SlTableHeader(sld.handler->GetDescription());
1866 
1867  _sl.need_length = old_need_length;
1868  }
1869  }
1870 
1871  break;
1872  }
1873 
1874  default: NOT_REACHED();
1875  }
1876 
1877  return std::vector<SaveLoad>();
1878 }
1879 
1893 std::vector<SaveLoad> SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
1894 {
1895  assert(_sl.action == SLA_LOAD || _sl.action == SLA_LOAD_CHECK);
1896  /* CH_TABLE / CH_SPARSE_TABLE always have a header. */
1897  if (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE) return SlTableHeader(slt);
1898 
1899  std::vector<SaveLoad> saveloads;
1900 
1901  /* Build a key lookup mapping based on the available fields. */
1902  std::map<std::string, std::vector<const SaveLoad *>> key_lookup;
1903  for (auto &sld : slt) {
1904  /* All entries should have a name; otherwise the entry should just be removed. */
1905  assert(!sld.name.empty());
1906 
1907  key_lookup[sld.name].push_back(&sld);
1908  }
1909 
1910  for (auto &slc : slct) {
1911  if (slc.name.empty()) {
1912  /* In old savegames there can be data we no longer care for. We
1913  * skip this by simply reading the amount of bytes indicated and
1914  * send those to /dev/null. */
1915  saveloads.push_back({"", SL_NULL, GetVarFileType(slc.null_type) | SLE_VAR_NULL, slc.null_length, slc.version_from, slc.version_to, nullptr, 0, nullptr});
1916  } else {
1917  auto sld_it = key_lookup.find(slc.name);
1918  /* If this branch triggers, it means that an entry in the
1919  * SaveLoadCompat list is not mentioned in the SaveLoad list. Did
1920  * you rename a field in one and not in the other? */
1921  if (sld_it == key_lookup.end()) {
1922  /* This isn't an assert, as that leaves no information what
1923  * field was to blame. This way at least we have breadcrumbs. */
1924  Debug(sl, 0, "internal error: saveload compatibility field '{}' not found", slc.name);
1925  SlErrorCorrupt("Internal error with savegame compatibility");
1926  }
1927  for (auto &sld : sld_it->second) {
1928  saveloads.push_back(*sld);
1929  }
1930  }
1931  }
1932 
1933  for (auto &sld : saveloads) {
1934  if (!SlIsObjectValidInSavegame(sld)) continue;
1935  if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1936  sld.handler->load_description = SlCompatTableHeader(sld.handler->GetDescription(), sld.handler->GetCompatDescription());
1937  }
1938  }
1939 
1940  return saveloads;
1941 }
1942 
1947 void SlGlobList(const SaveLoadTable &slt)
1948 {
1949  SlObject(nullptr, slt);
1950 }
1951 
1957 void SlAutolength(AutolengthProc *proc, int arg)
1958 {
1959  assert(_sl.action == SLA_SAVE);
1960 
1961  /* Tell it to calculate the length */
1963  _sl.obj_len = 0;
1964  proc(arg);
1965 
1966  /* Setup length */
1969 
1970  size_t start_pos = _sl.dumper->GetSize();
1971  size_t expected_offs = start_pos + _sl.obj_len;
1972 
1973  /* And write the stuff */
1974  proc(arg);
1975 
1976  if (expected_offs != _sl.dumper->GetSize()) {
1977  SlErrorCorruptFmt("Invalid chunk size when writing autolength block, expected {}, got {}", _sl.obj_len, _sl.dumper->GetSize() - start_pos);
1978  }
1979 }
1980 
1981 void ChunkHandler::LoadCheck(size_t len) const
1982 {
1983  switch (_sl.block_mode) {
1984  case CH_TABLE:
1985  case CH_SPARSE_TABLE:
1986  SlTableHeader({});
1987  [[fallthrough]];
1988  case CH_ARRAY:
1989  case CH_SPARSE_ARRAY:
1990  SlSkipArray();
1991  break;
1992  case CH_RIFF:
1993  SlSkipBytes(len);
1994  break;
1995  default:
1996  NOT_REACHED();
1997  }
1998 }
1999 
2004 static void SlLoadChunk(const ChunkHandler &ch)
2005 {
2006  uint8_t m = SlReadByte();
2007 
2008  _sl.block_mode = m & CH_TYPE_MASK;
2009  _sl.obj_len = 0;
2010  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2011 
2012  /* The header should always be at the start. Read the length; the
2013  * Load() should as first action process the header. */
2014  if (_sl.expect_table_header) {
2015  SlIterateArray();
2016  }
2017 
2018  switch (_sl.block_mode) {
2019  case CH_TABLE:
2020  case CH_ARRAY:
2021  _sl.array_index = 0;
2022  ch.Load();
2023  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2024  break;
2025  case CH_SPARSE_TABLE:
2026  case CH_SPARSE_ARRAY:
2027  ch.Load();
2028  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2029  break;
2030  case CH_RIFF: {
2031  /* Read length */
2032  size_t len = (SlReadByte() << 16) | ((m >> 4) << 24);
2033  len += SlReadUint16();
2034  _sl.obj_len = len;
2035  size_t start_pos = _sl.reader->GetSize();
2036  size_t endoffs = start_pos + len;
2037  ch.Load();
2038 
2039  if (_sl.reader->GetSize() != endoffs) {
2040  SlErrorCorruptFmt("Invalid chunk size in RIFF in {} - expected {}, got {}", ch.GetName(), len, _sl.reader->GetSize() - start_pos);
2041  }
2042  break;
2043  }
2044  default:
2045  SlErrorCorrupt("Invalid chunk type");
2046  break;
2047  }
2048 
2049  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2050 }
2051 
2057 static void SlLoadCheckChunk(const ChunkHandler &ch)
2058 {
2059  uint8_t m = SlReadByte();
2060 
2061  _sl.block_mode = m & CH_TYPE_MASK;
2062  _sl.obj_len = 0;
2063  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2064 
2065  /* The header should always be at the start. Read the length; the
2066  * LoadCheck() should as first action process the header. */
2067  if (_sl.expect_table_header) {
2068  SlIterateArray();
2069  }
2070 
2071  switch (_sl.block_mode) {
2072  case CH_TABLE:
2073  case CH_ARRAY:
2074  _sl.array_index = 0;
2075  ch.LoadCheck();
2076  break;
2077  case CH_SPARSE_TABLE:
2078  case CH_SPARSE_ARRAY:
2079  ch.LoadCheck();
2080  break;
2081  case CH_RIFF: {
2082  /* Read length */
2083  size_t len = (SlReadByte() << 16) | ((m >> 4) << 24);
2084  len += SlReadUint16();
2085  _sl.obj_len = len;
2086  size_t start_pos = _sl.reader->GetSize();
2087  size_t endoffs = start_pos + len;
2088  ch.LoadCheck(len);
2089 
2090  if (_sl.reader->GetSize() != endoffs) {
2091  SlErrorCorruptFmt("Invalid chunk size in RIFF in {} - expected {}, got {}", ch.GetName(), len, _sl.reader->GetSize() - start_pos);
2092  }
2093  break;
2094  }
2095  default:
2096  SlErrorCorrupt("Invalid chunk type");
2097  break;
2098  }
2099 
2100  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2101 }
2102 
2108 static void SlSaveChunk(const ChunkHandler &ch)
2109 {
2110  if (ch.type == CH_READONLY) return;
2111 
2112  SlWriteUint32(ch.id);
2113  Debug(sl, 2, "Saving chunk {}", ch.GetName());
2114 
2115  _sl.block_mode = ch.type;
2116  _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2117 
2119 
2120  switch (_sl.block_mode) {
2121  case CH_RIFF:
2122  ch.Save();
2123  break;
2124  case CH_TABLE:
2125  case CH_ARRAY:
2126  _sl.last_array_index = 0;
2128  ch.Save();
2129  SlWriteArrayLength(0); // Terminate arrays
2130  break;
2131  case CH_SPARSE_TABLE:
2132  case CH_SPARSE_ARRAY:
2134  ch.Save();
2135  SlWriteArrayLength(0); // Terminate arrays
2136  break;
2137  default: NOT_REACHED();
2138  }
2139 
2140  if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2141 }
2142 
2144 static void SlSaveChunks()
2145 {
2146  for (auto &ch : ChunkHandlers()) {
2147  SlSaveChunk(ch);
2148  }
2149 
2150  /* Terminator */
2151  SlWriteUint32(0);
2152 }
2153 
2160 static const ChunkHandler *SlFindChunkHandler(uint32_t id)
2161 {
2162  for (const ChunkHandler &ch : ChunkHandlers()) if (ch.id == id) return &ch;
2163  return nullptr;
2164 }
2165 
2167 static void SlLoadChunks()
2168 {
2169  uint32_t id;
2170  const ChunkHandler *ch;
2171 
2172  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
2173  Debug(sl, 2, "Loading chunk {:c}{:c}{:c}{:c}", id >> 24, id >> 16, id >> 8, id);
2174 
2175  ch = SlFindChunkHandler(id);
2176  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2177  SlLoadChunk(*ch);
2178  }
2179 }
2180 
2182 static void SlLoadCheckChunks()
2183 {
2184  uint32_t id;
2185  const ChunkHandler *ch;
2186 
2187  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
2188  Debug(sl, 2, "Loading chunk {:c}{:c}{:c}{:c}", id >> 24, id >> 16, id >> 8, id);
2189 
2190  ch = SlFindChunkHandler(id);
2191  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2192  SlLoadCheckChunk(*ch);
2193  }
2194 }
2195 
2197 static void SlFixPointers()
2198 {
2199  _sl.action = SLA_PTRS;
2200 
2201  for (const ChunkHandler &ch : ChunkHandlers()) {
2202  Debug(sl, 3, "Fixing pointers for {}", ch.GetName());
2203  ch.FixPointers();
2204  }
2205 
2206  assert(_sl.action == SLA_PTRS);
2207 }
2208 
2209 
2212  std::optional<FileHandle> file;
2213  long begin;
2214 
2219  FileReader(FileHandle &&file) : LoadFilter(nullptr), file(std::move(file)), begin(ftell(*this->file))
2220  {
2221  }
2222 
2225  {
2226  if (this->file.has_value()) {
2227  _game_session_stats.savegame_size = ftell(*this->file) - this->begin;
2228  }
2229  }
2230 
2231  size_t Read(uint8_t *buf, size_t size) override
2232  {
2233  /* We're in the process of shutting down, i.e. in "failure" mode. */
2234  if (!this->file.has_value()) return 0;
2235 
2236  return fread(buf, 1, size, *this->file);
2237  }
2238 
2239  void Reset() override
2240  {
2241  clearerr(*this->file);
2242  if (fseek(*this->file, this->begin, SEEK_SET)) {
2243  Debug(sl, 1, "Could not reset the file reading");
2244  }
2245  }
2246 };
2247 
2250  std::optional<FileHandle> file;
2251 
2256  FileWriter(FileHandle &&file) : SaveFilter(nullptr), file(std::move(file))
2257  {
2258  }
2259 
2262  {
2263  this->Finish();
2264  }
2265 
2266  void Write(uint8_t *buf, size_t size) override
2267  {
2268  /* We're in the process of shutting down, i.e. in "failure" mode. */
2269  if (!this->file.has_value()) return;
2270 
2271  if (fwrite(buf, 1, size, *this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
2272  }
2273 
2274  void Finish() override
2275  {
2276  if (this->file.has_value()) {
2277  _game_session_stats.savegame_size = ftell(*this->file);
2278  this->file.reset();
2279  }
2280  }
2281 };
2282 
2283 /*******************************************
2284  ********** START OF LZO CODE **************
2285  *******************************************/
2286 
2287 #ifdef WITH_LZO
2288 #include <lzo/lzo1x.h>
2289 
2291 static const uint LZO_BUFFER_SIZE = 8192;
2292 
2299  LZOLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(chain)
2300  {
2301  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2302  }
2303 
2304  size_t Read(uint8_t *buf, size_t ssize) override
2305  {
2306  assert(ssize >= LZO_BUFFER_SIZE);
2307 
2308  /* Buffer size is from the LZO docs plus the chunk header size. */
2309  uint8_t out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32_t) * 2];
2310  uint32_t tmp[2];
2311  uint32_t size;
2312  lzo_uint len = ssize;
2313 
2314  /* Read header*/
2315  if (this->chain->Read((uint8_t*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
2316 
2317  /* Check if size is bad */
2318  ((uint32_t*)out)[0] = size = tmp[1];
2319 
2320  if (_sl_version != SL_MIN_VERSION) {
2321  tmp[0] = TO_BE32(tmp[0]);
2322  size = TO_BE32(size);
2323  }
2324 
2325  if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
2326 
2327  /* Read block */
2328  if (this->chain->Read(out + sizeof(uint32_t), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2329 
2330  /* Verify checksum */
2331  if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32_t))) SlErrorCorrupt("Bad checksum");
2332 
2333  /* Decompress */
2334  int ret = lzo1x_decompress_safe(out + sizeof(uint32_t) * 1, size, buf, &len, nullptr);
2335  if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2336  return len;
2337  }
2338 };
2339 
2346  LZOSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t) : SaveFilter(chain)
2347  {
2348  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2349  }
2350 
2351  void Write(uint8_t *buf, size_t size) override
2352  {
2353  const lzo_bytep in = buf;
2354  /* Buffer size is from the LZO docs plus the chunk header size. */
2355  uint8_t out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32_t) * 2];
2356  uint8_t wrkmem[LZO1X_1_MEM_COMPRESS];
2357  lzo_uint outlen;
2358 
2359  do {
2360  /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2361  lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
2362  lzo1x_1_compress(in, len, out + sizeof(uint32_t) * 2, &outlen, wrkmem);
2363  ((uint32_t*)out)[1] = TO_BE32((uint32_t)outlen);
2364  ((uint32_t*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32_t), outlen + sizeof(uint32_t)));
2365  this->chain->Write(out, outlen + sizeof(uint32_t) * 2);
2366 
2367  /* Move to next data chunk. */
2368  size -= len;
2369  in += len;
2370  } while (size > 0);
2371  }
2372 };
2373 
2374 #endif /* WITH_LZO */
2375 
2376 /*********************************************
2377  ******** START OF NOCOMP CODE (uncompressed)*
2378  *********************************************/
2379 
2386  NoCompLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(chain)
2387  {
2388  }
2389 
2390  size_t Read(uint8_t *buf, size_t size) override
2391  {
2392  return this->chain->Read(buf, size);
2393  }
2394 };
2395 
2402  NoCompSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t) : SaveFilter(chain)
2403  {
2404  }
2405 
2406  void Write(uint8_t *buf, size_t size) override
2407  {
2408  this->chain->Write(buf, size);
2409  }
2410 };
2411 
2412 /********************************************
2413  ********** START OF ZLIB CODE **************
2414  ********************************************/
2415 
2416 #if defined(WITH_ZLIB)
2417 #include <zlib.h>
2418 
2421  z_stream z;
2423 
2428  ZlibLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(chain)
2429  {
2430  memset(&this->z, 0, sizeof(this->z));
2431  if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2432  }
2433 
2436  {
2437  inflateEnd(&this->z);
2438  }
2439 
2440  size_t Read(uint8_t *buf, size_t size) override
2441  {
2442  this->z.next_out = buf;
2443  this->z.avail_out = (uint)size;
2444 
2445  do {
2446  /* read more bytes from the file? */
2447  if (this->z.avail_in == 0) {
2448  this->z.next_in = this->fread_buf;
2449  this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2450  }
2451 
2452  /* inflate the data */
2453  int r = inflate(&this->z, 0);
2454  if (r == Z_STREAM_END) break;
2455 
2456  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2457  } while (this->z.avail_out != 0);
2458 
2459  return size - this->z.avail_out;
2460  }
2461 };
2462 
2465  z_stream z;
2467 
2473  ZlibSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level) : SaveFilter(chain)
2474  {
2475  memset(&this->z, 0, sizeof(this->z));
2476  if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2477  }
2478 
2481  {
2482  deflateEnd(&this->z);
2483  }
2484 
2491  void WriteLoop(uint8_t *p, size_t len, int mode)
2492  {
2493  uint n;
2494  this->z.next_in = p;
2495  this->z.avail_in = (uInt)len;
2496  do {
2497  this->z.next_out = this->fwrite_buf;
2498  this->z.avail_out = sizeof(this->fwrite_buf);
2499 
2507  int r = deflate(&this->z, mode);
2508 
2509  /* bytes were emitted? */
2510  if ((n = sizeof(this->fwrite_buf) - this->z.avail_out) != 0) {
2511  this->chain->Write(this->fwrite_buf, n);
2512  }
2513  if (r == Z_STREAM_END) break;
2514 
2515  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2516  } while (this->z.avail_in || !this->z.avail_out);
2517  }
2518 
2519  void Write(uint8_t *buf, size_t size) override
2520  {
2521  this->WriteLoop(buf, size, 0);
2522  }
2523 
2524  void Finish() override
2525  {
2526  this->WriteLoop(nullptr, 0, Z_FINISH);
2527  this->chain->Finish();
2528  }
2529 };
2530 
2531 #endif /* WITH_ZLIB */
2532 
2533 /********************************************
2534  ********** START OF LZMA CODE **************
2535  ********************************************/
2536 
2537 #if defined(WITH_LIBLZMA)
2538 #include <lzma.h>
2539 
2546 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2547 
2550  lzma_stream lzma;
2552 
2557  LZMALoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(chain), lzma(_lzma_init)
2558  {
2559  /* Allow saves up to 256 MB uncompressed */
2560  if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2561  }
2562 
2565  {
2566  lzma_end(&this->lzma);
2567  }
2568 
2569  size_t Read(uint8_t *buf, size_t size) override
2570  {
2571  this->lzma.next_out = buf;
2572  this->lzma.avail_out = size;
2573 
2574  do {
2575  /* read more bytes from the file? */
2576  if (this->lzma.avail_in == 0) {
2577  this->lzma.next_in = this->fread_buf;
2578  this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2579  }
2580 
2581  /* inflate the data */
2582  lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2583  if (r == LZMA_STREAM_END) break;
2584  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2585  } while (this->lzma.avail_out != 0);
2586 
2587  return size - this->lzma.avail_out;
2588  }
2589 };
2590 
2593  lzma_stream lzma;
2595 
2601  LZMASaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level) : SaveFilter(chain), lzma(_lzma_init)
2602  {
2603  if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2604  }
2605 
2608  {
2609  lzma_end(&this->lzma);
2610  }
2611 
2618  void WriteLoop(uint8_t *p, size_t len, lzma_action action)
2619  {
2620  size_t n;
2621  this->lzma.next_in = p;
2622  this->lzma.avail_in = len;
2623  do {
2624  this->lzma.next_out = this->fwrite_buf;
2625  this->lzma.avail_out = sizeof(this->fwrite_buf);
2626 
2627  lzma_ret r = lzma_code(&this->lzma, action);
2628 
2629  /* bytes were emitted? */
2630  if ((n = sizeof(this->fwrite_buf) - this->lzma.avail_out) != 0) {
2631  this->chain->Write(this->fwrite_buf, n);
2632  }
2633  if (r == LZMA_STREAM_END) break;
2634  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2635  } while (this->lzma.avail_in || !this->lzma.avail_out);
2636  }
2637 
2638  void Write(uint8_t *buf, size_t size) override
2639  {
2640  this->WriteLoop(buf, size, LZMA_RUN);
2641  }
2642 
2643  void Finish() override
2644  {
2645  this->WriteLoop(nullptr, 0, LZMA_FINISH);
2646  this->chain->Finish();
2647  }
2648 };
2649 
2650 #endif /* WITH_LIBLZMA */
2651 
2652 /*******************************************
2653  ************* END OF CODE *****************
2654  *******************************************/
2655 
2658  const char *name;
2659  uint32_t tag;
2660 
2661  std::shared_ptr<LoadFilter> (*init_load)(std::shared_ptr<LoadFilter> chain);
2662  std::shared_ptr<SaveFilter> (*init_write)(std::shared_ptr<SaveFilter> chain, uint8_t compression);
2663 
2667 };
2668 
2669 static const uint32_t SAVEGAME_TAG_LZO = TO_BE32X('OTTD');
2670 static const uint32_t SAVEGAME_TAG_NONE = TO_BE32X('OTTN');
2671 static const uint32_t SAVEGAME_TAG_ZLIB = TO_BE32X('OTTZ');
2672 static const uint32_t SAVEGAME_TAG_LZMA = TO_BE32X('OTTX');
2673 
2676 #if defined(WITH_LZO)
2677  /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2678  {"lzo", SAVEGAME_TAG_LZO, CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2679 #else
2680  {"lzo", SAVEGAME_TAG_LZO, nullptr, nullptr, 0, 0, 0},
2681 #endif
2682  /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2683  {"none", SAVEGAME_TAG_NONE, CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2684 #if defined(WITH_ZLIB)
2685  /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2686  * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2687  * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2688  {"zlib", SAVEGAME_TAG_ZLIB, CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2689 #else
2690  {"zlib", SAVEGAME_TAG_ZLIB, nullptr, nullptr, 0, 0, 0},
2691 #endif
2692 #if defined(WITH_LIBLZMA)
2693  /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2694  * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2695  * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2696  * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2697  * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2698  {"lzma", SAVEGAME_TAG_LZMA, CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2699 #else
2700  {"lzma", SAVEGAME_TAG_LZMA, nullptr, nullptr, 0, 0, 0},
2701 #endif
2702 };
2703 
2710 static std::pair<const SaveLoadFormat &, uint8_t> GetSavegameFormat(const std::string &full_name)
2711 {
2712  /* Find default savegame format, the highest one with which files can be written. */
2713  auto it = std::find_if(std::rbegin(_saveload_formats), std::rend(_saveload_formats), [](const auto &slf) { return slf.init_write != nullptr; });
2714  if (it == std::rend(_saveload_formats)) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "no writeable savegame formats");
2715 
2716  const SaveLoadFormat &def = *it;
2717 
2718  if (!full_name.empty()) {
2719  /* Get the ":..." of the compression level out of the way */
2720  size_t separator = full_name.find(':');
2721  bool has_comp_level = separator != std::string::npos;
2722  const std::string name(full_name, 0, has_comp_level ? separator : full_name.size());
2723 
2724  for (const auto &slf : _saveload_formats) {
2725  if (slf.init_write != nullptr && name.compare(slf.name) == 0) {
2726  if (has_comp_level) {
2727  const std::string complevel(full_name, separator + 1);
2728 
2729  /* Get the level and determine whether all went fine. */
2730  size_t processed;
2731  long level = std::stol(complevel, &processed, 10);
2732  if (processed == 0 || level != Clamp(level, slf.min_compression, slf.max_compression)) {
2733  SetDParamStr(0, complevel);
2734  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2735  } else {
2736  return {slf, ClampTo<uint8_t>(level)};
2737  }
2738  }
2739  return {slf, slf.default_compression};
2740  }
2741  }
2742 
2743  SetDParamStr(0, name);
2744  SetDParamStr(1, def.name);
2745  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2746  }
2747  return {def, def.default_compression};
2748 }
2749 
2750 /* actual loader/saver function */
2751 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2752 extern bool AfterLoadGame();
2753 extern bool LoadOldSaveGame(const std::string &file);
2754 
2758 static void ResetSaveloadData()
2759 {
2760  ResetTempEngineData();
2761  ResetLabelMaps();
2762  ResetOldWaypoints();
2763 }
2764 
2768 static inline void ClearSaveLoadState()
2769 {
2770  _sl.dumper = nullptr;
2771  _sl.sf = nullptr;
2772  _sl.reader = nullptr;
2773  _sl.lf = nullptr;
2774 }
2775 
2777 static void SaveFileStart()
2778 {
2779  SetMouseCursorBusy(true);
2780 
2782  _sl.saveinprogress = true;
2783 }
2784 
2786 static void SaveFileDone()
2787 {
2788  SetMouseCursorBusy(false);
2789 
2791  _sl.saveinprogress = false;
2792 
2793 #ifdef __EMSCRIPTEN__
2794  EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
2795 #endif
2796 }
2797 
2800 {
2801  _sl.error_str = str;
2802 }
2803 
2806 {
2807  return _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED;
2808 }
2809 
2812 {
2814  return _sl.error_str;
2815 }
2816 
2818 static void SaveFileError()
2819 {
2821  SaveFileDone();
2822 }
2823 
2828 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2829 {
2830  try {
2831  auto [fmt, compression] = GetSavegameFormat(_savegame_format);
2832 
2833  /* We have written our stuff to memory, now write it to file! */
2834  uint32_t hdr[2] = { fmt.tag, TO_BE32(SAVEGAME_VERSION << 16) };
2835  _sl.sf->Write((uint8_t*)hdr, sizeof(hdr));
2836 
2837  _sl.sf = fmt.init_write(_sl.sf, compression);
2838  _sl.dumper->Flush(_sl.sf);
2839 
2841 
2842  if (threaded) SetAsyncSaveFinish(SaveFileDone);
2843 
2844  return SL_OK;
2845  } catch (...) {
2847 
2849 
2850  /* We don't want to shout when saving is just
2851  * cancelled due to a client disconnecting. */
2852  if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2853  /* Skip the "colour" character */
2854  Debug(sl, 0, "{}", GetString(GetSaveLoadErrorType()).substr(3) + GetString(GetSaveLoadErrorMessage()));
2855  asfp = SaveFileError;
2856  }
2857 
2858  if (threaded) {
2859  SetAsyncSaveFinish(asfp);
2860  } else {
2861  asfp();
2862  }
2863  return SL_ERROR;
2864  }
2865 }
2866 
2867 void WaitTillSaved()
2868 {
2869  if (!_save_thread.joinable()) return;
2870 
2871  _save_thread.join();
2872 
2873  /* Make sure every other state is handled properly as well. */
2875 }
2876 
2885 static SaveOrLoadResult DoSave(std::shared_ptr<SaveFilter> writer, bool threaded)
2886 {
2887  assert(!_sl.saveinprogress);
2888 
2889  _sl.dumper = std::make_unique<MemoryDumper>();
2890  _sl.sf = writer;
2891 
2893 
2894  SaveViewportBeforeSaveGame();
2895  SlSaveChunks();
2896 
2897  SaveFileStart();
2898 
2899  if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
2900  if (threaded) Debug(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2901 
2902  SaveOrLoadResult result = SaveFileToDisk(false);
2903  SaveFileDone();
2904 
2905  return result;
2906  }
2907 
2908  return SL_OK;
2909 }
2910 
2917 SaveOrLoadResult SaveWithFilter(std::shared_ptr<SaveFilter> writer, bool threaded)
2918 {
2919  try {
2920  _sl.action = SLA_SAVE;
2921  return DoSave(writer, threaded);
2922  } catch (...) {
2924  return SL_ERROR;
2925  }
2926 }
2927 
2936 static const SaveLoadFormat *DetermineSaveLoadFormat(uint32_t tag, uint32_t raw_version)
2937 {
2938  auto fmt = std::find_if(std::begin(_saveload_formats), std::end(_saveload_formats), [tag](const auto &fmt) { return fmt.tag == tag; });
2939  if (fmt != std::end(_saveload_formats)) {
2940  /* Check version number */
2941  _sl_version = (SaveLoadVersion)(TO_BE32(raw_version) >> 16);
2942  /* Minor is not used anymore from version 18.0, but it is still needed
2943  * in versions before that (4 cases) which can't be removed easy.
2944  * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2945  _sl_minor_version = (TO_BE32(raw_version) >> 8) & 0xFF;
2946 
2947  Debug(sl, 1, "Loading savegame version {}", _sl_version);
2948 
2949  /* Is the version higher than the current? */
2950  if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2951  if (_sl_version >= SLV_START_PATCHPACKS && _sl_version <= SLV_END_PATCHPACKS) SlError(STR_GAME_SAVELOAD_ERROR_PATCHPACK);
2952  return fmt;
2953  }
2954 
2955  Debug(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2956  _sl.lf->Reset();
2958  _sl_minor_version = 0;
2959 
2960  /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2961  fmt = std::find_if(std::begin(_saveload_formats), std::end(_saveload_formats), [](const auto &fmt) { return fmt.tag == SAVEGAME_TAG_LZO; });
2962  if (fmt == std::end(_saveload_formats)) {
2963  /* Who removed the LZO savegame format definition? When built without LZO support,
2964  * the formats must still list it just without a method to read the file.
2965  * The caller of this function has to check for the existence of load function. */
2966  NOT_REACHED();
2967  }
2968  return fmt;
2969 }
2970 
2977 static SaveOrLoadResult DoLoad(std::shared_ptr<LoadFilter> reader, bool load_check)
2978 {
2979  _sl.lf = reader;
2980 
2981  if (load_check) {
2982  /* Clear previous check data */
2984  /* Mark SL_LOAD_CHECK as supported for this savegame. */
2985  _load_check_data.checkable = true;
2986  }
2987 
2988  uint32_t hdr[2];
2989  if (_sl.lf->Read((uint8_t*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2990 
2991  /* see if we have any loader for this type. */
2992  const SaveLoadFormat *fmt = DetermineSaveLoadFormat(hdr[0], hdr[1]);
2993 
2994  /* loader for this savegame type is not implemented? */
2995  if (fmt->init_load == nullptr) {
2996  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, fmt::format("Loader for '{}' is not available.", fmt->name));
2997  }
2998 
2999  _sl.lf = fmt->init_load(_sl.lf);
3000  _sl.reader = std::make_unique<ReadBuffer>(_sl.lf);
3001  _next_offs = 0;
3002 
3003  if (!load_check) {
3005 
3006  /* Old maps were hardcoded to 256x256 and thus did not contain
3007  * any mapsize information. Pre-initialize to 256x256 to not to
3008  * confuse old games */
3009  InitializeGame(256, 256, true, true);
3010 
3011  _gamelog.Reset();
3012 
3014  /*
3015  * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
3016  * shared savegame version 4. Anything before that 'obviously'
3017  * does not have any NewGRFs. Between the introduction and
3018  * savegame version 41 (just before 0.5) the NewGRF settings
3019  * were not stored in the savegame and they were loaded by
3020  * using the settings from the main menu.
3021  * So, to recap:
3022  * - savegame version < 4: do not load any NewGRFs.
3023  * - savegame version >= 41: load NewGRFs from savegame, which is
3024  * already done at this stage by
3025  * overwriting the main menu settings.
3026  * - other savegame versions: use main menu settings.
3027  *
3028  * This means that users *can* crash savegame version 4..40
3029  * savegames if they set incompatible NewGRFs in the main menu,
3030  * but can't crash anymore for savegame version < 4 savegames.
3031  *
3032  * Note: this is done here because AfterLoadGame is also called
3033  * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
3034  */
3036  }
3037  }
3038 
3039  if (load_check) {
3040  /* Load chunks into _load_check_data.
3041  * No pools are loaded. References are not possible, and thus do not need resolving. */
3043  } else {
3044  /* Load chunks and resolve references */
3045  SlLoadChunks();
3046  SlFixPointers();
3047  }
3048 
3050 
3052 
3053  if (load_check) {
3054  /* The only part from AfterLoadGame() we need */
3056  } else {
3058 
3059  /* After loading fix up savegame for any internal changes that
3060  * might have occurred since then. If it fails, load back the old game. */
3061  if (!AfterLoadGame()) {
3062  _gamelog.StopAction();
3063  return SL_REINIT;
3064  }
3065 
3066  _gamelog.StopAction();
3067  }
3068 
3069  return SL_OK;
3070 }
3071 
3077 SaveOrLoadResult LoadWithFilter(std::shared_ptr<LoadFilter> reader)
3078 {
3079  try {
3080  _sl.action = SLA_LOAD;
3081  return DoLoad(reader, false);
3082  } catch (...) {
3084  return SL_REINIT;
3085  }
3086 }
3087 
3097 SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
3098 {
3099  /* An instance of saving is already active, so don't go saving again */
3100  if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
3101  /* if not an autosave, but a user action, show error message */
3102  if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
3103  return SL_OK;
3104  }
3105  WaitTillSaved();
3106 
3107  try {
3108  /* Load a TTDLX or TTDPatch game */
3109  if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
3111 
3112  InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
3113 
3114  /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
3115  * and if so a new NewGRF list will be made in LoadOldSaveGame.
3116  * Note: this is done here because AfterLoadGame is also called
3117  * for OTTD savegames which have their own NewGRF logic. */
3119  _gamelog.Reset();
3120  if (!LoadOldSaveGame(filename)) return SL_REINIT;
3122  _sl_minor_version = 0;
3124  if (!AfterLoadGame()) {
3125  _gamelog.StopAction();
3126  return SL_REINIT;
3127  }
3128  _gamelog.StopAction();
3129  return SL_OK;
3130  }
3131 
3132  assert(dft == DFT_GAME_FILE);
3133  switch (fop) {
3134  case SLO_CHECK:
3136  break;
3137 
3138  case SLO_LOAD:
3139  _sl.action = SLA_LOAD;
3140  break;
3141 
3142  case SLO_SAVE:
3143  _sl.action = SLA_SAVE;
3144  break;
3145 
3146  default: NOT_REACHED();
3147  }
3148 
3149  auto fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
3150 
3151  /* Make it a little easier to load savegames from the console */
3152  if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
3153  if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
3154  if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
3155 
3156  if (!fh.has_value()) {
3157  SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3158  }
3159 
3160  if (fop == SLO_SAVE) { // SAVE game
3161  Debug(desync, 1, "save: {:08x}; {:02x}; {}", TimerGameEconomy::date, TimerGameEconomy::date_fract, filename);
3162  if (!_settings_client.gui.threaded_saves) threaded = false;
3163 
3164  return DoSave(std::make_shared<FileWriter>(std::move(*fh)), threaded);
3165  }
3166 
3167  /* LOAD game */
3168  assert(fop == SLO_LOAD || fop == SLO_CHECK);
3169  Debug(desync, 1, "load: {}", filename);
3170  return DoLoad(std::make_shared<FileReader>(std::move(*fh)), fop == SLO_CHECK);
3171  } catch (...) {
3172  /* This code may be executed both for old and new save games. */
3174 
3175  /* Skip the "colour" character */
3176  if (fop != SLO_CHECK) Debug(sl, 0, "{}", GetString(GetSaveLoadErrorType()).substr(3) + GetString(GetSaveLoadErrorMessage()));
3177 
3178  /* A saver/loader exception!! reinitialize all variables to prevent crash! */
3179  return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
3180  }
3181 }
3182 
3189 {
3190  std::string filename;
3191 
3193  filename = GenerateDefaultSaveName() + counter.Extension();
3194  } else {
3195  filename = counter.Filename();
3196  }
3197 
3198  Debug(sl, 2, "Autosaving to '{}'", filename);
3199  if (SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) {
3200  ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR);
3201  }
3202 }
3203 
3204 
3207 {
3209 }
3210 
3215 {
3216  /* Check if we have a name for this map, which is the name of the first
3217  * available company. When there's no company available we'll use
3218  * 'Spectator' as "company" name. */
3219  CompanyID cid = _local_company;
3220  if (!Company::IsValidID(cid)) {
3221  for (const Company *c : Company::Iterate()) {
3222  cid = c->index;
3223  break;
3224  }
3225  }
3226 
3227  SetDParam(0, cid);
3228 
3229  /* We show the current game time differently depending on the timekeeping units used by this game. */
3231  /* Insert time played. */
3232  const auto play_time = TimerGameTick::counter / Ticks::TICKS_PER_SECOND;
3233  SetDParam(1, STR_SAVEGAME_DURATION_REALTIME);
3234  SetDParam(2, play_time / 60 / 60);
3235  SetDParam(3, (play_time / 60) % 60);
3236  } else {
3237  /* Insert current date */
3239  case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
3240  case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
3241  case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
3242  default: NOT_REACHED();
3243  }
3245  }
3246 
3247  /* Get the correct string (special string for when there's not company) */
3248  std::string filename = GetString(!Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT);
3249  SanitizeFilename(filename);
3250  return filename;
3251 }
3252 
3258 {
3260 }
3261 
3269 {
3270  if (aft == FT_INVALID || aft == FT_NONE) {
3271  this->file_op = SLO_INVALID;
3272  this->detail_ftype = DFT_INVALID;
3273  this->abstract_ftype = FT_INVALID;
3274  return;
3275  }
3276 
3277  this->file_op = fop;
3278  this->detail_ftype = dft;
3279  this->abstract_ftype = aft;
3280 }
3281 
3287 {
3288  this->SetMode(item.type);
3289  this->name = item.name;
3290  this->title = item.title;
3291 }
3292 
3294 {
3295  assert(this->load_description.has_value());
3296  return *this->load_description;
3297 }
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
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.
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
void Reset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition: gamelog.cpp:94
void StopAction()
Stops logging of any changes.
Definition: gamelog.cpp:74
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:29
A connected component of a link graph.
Definition: linkgraph.h:37
Handler for saving/loading an object to/from disk.
Definition: saveload.h:519
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Definition: saveload.cpp:3293
Handler that is assigned when there is a struct read in the savegame which is not known to the code.
Definition: saveload.cpp:1719
virtual SaveLoadTable GetDescription() const override
Get the description of the fields in the savegame.
Definition: saveload.cpp:1738
virtual SaveLoadCompatTable GetCompatDescription() const override
Get the pre-header description of the fields in the savegame.
Definition: saveload.cpp:1743
Template class to help with list-like types.
Definition: saveload.cpp:1238
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
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
static constexpr TimerGameTick::Ticks TICKS_PER_SECOND
Estimation of how many ticks fit in a single second.
static Date date
Current date in days (day counter).
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
static DateFract date_fract
Fractional part of the day.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:52
Owner
Enum for all companies/owners.
Definition: company_type.h:18
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
Definition: error_gui.cpp:367
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:26
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:27
void SanitizeFilename(std::string &filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1003
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
DetailedFileType GetDetailedFileType(FiosType fios_type)
Extract the detailed file type from a FiosType.
Definition: fileio_type.h:107
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition: fileio_type.h:97
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:16
@ FT_NONE
nothing to do
Definition: fileio_type.h:17
@ FT_INVALID
Invalid or unknown file type.
Definition: fileio_type.h:23
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:73
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:53
@ SLO_CHECK
Load file for checking and/or preview.
Definition: fileio_type.h:54
@ SLO_SAVE
File is being saved.
Definition: fileio_type.h:56
@ SLO_LOAD
File is being loaded.
Definition: fileio_type.h:55
@ SLO_INVALID
Unknown file operation.
Definition: fileio_type.h:58
DetailedFileType
Kinds of files in each AbstractFileType.
Definition: fileio_type.h:29
@ DFT_GAME_FILE
Save game or scenario file.
Definition: fileio_type.h:32
@ DFT_INVALID
Unknown or invalid file.
Definition: fileio_type.h:49
@ DFT_OLD_GAME_FILE
Old save game or scenario file.
Definition: fileio_type.h:31
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
@ SCENARIO_DIR
Base directory for all scenarios.
Definition: fileio_type.h:119
@ BASE_DIR
Base directory for all subdirectories.
Definition: fileio_type.h:116
@ SAVE_DIR
Base directory for all savegames.
Definition: fileio_type.h:117
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition: fileio_type.h:118
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:41
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
Gamelog _gamelog
Gamelog instance.
Definition: gamelog.cpp:31
@ GLAT_LOAD
Game loaded.
Definition: gamelog.h:18
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1670
GameSessionStats _game_session_stats
Statistics about the current session.
Definition: gfx.cpp:51
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
GRFConfig * _grfconfig
First item in list of current GRF set up.
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
static const uint LZO_BUFFER_SIZE
Buffer size for the LZO compressor.
Definition: saveload.cpp:2291
void SlError(StringID string, const std::string &extra_msg)
Error handler.
Definition: saveload.cpp:321
static uint8_t GetSavegameFileType(const SaveLoad &sld)
Return the type as saved/loaded inside the savegame.
Definition: saveload.cpp:552
static SaveOrLoadResult DoSave(std::shared_ptr< SaveFilter > writer, bool threaded)
Actually perform the saving of the savegame.
Definition: saveload.cpp:2885
static std::pair< const SaveLoadFormat &, uint8_t > GetSavegameFormat(const std::string &full_name)
Return the savegameformat of the game.
Definition: saveload.cpp:2710
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition: saveload.cpp:376
static const lzma_stream _lzma_init
Have a copy of an initialised LZMA stream.
Definition: saveload.cpp:2546
uint32_t _ttdp_version
version of TTDP savegame (if applicable)
Definition: saveload.cpp:62
static const SaveLoadFormat _saveload_formats[]
The different saveload formats known/understood by OpenTTD.
Definition: saveload.cpp:2675
std::string _savegame_format
how to compress savegames
Definition: saveload.cpp:65
static const SaveLoadFormat * DetermineSaveLoadFormat(uint32_t tag, uint32_t raw_version)
Determines the SaveLoadFormat that is connected to the given tag.
Definition: saveload.cpp:2936
static void SaveFileDone()
Update the gui accordingly when saving is done and release locks on saveload.
Definition: saveload.cpp:2786
StringID GetSaveLoadErrorMessage()
Return the description of the error.
Definition: saveload.cpp:2811
SaveLoadVersion _sl_version
the major savegame version identifier
Definition: saveload.cpp:63
uint8_t _sl_minor_version
the minor savegame version, DO NOT USE!
Definition: saveload.cpp:64
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
SaveOrLoadResult LoadWithFilter(std::shared_ptr< LoadFilter > reader)
Load the game using a (reader) filter.
Definition: saveload.cpp:3077
static SaveOrLoadResult SaveFileToDisk(bool threaded)
We have written the whole game into memory, _memory_savegame, now find and appropriate compressor and...
Definition: saveload.cpp:2828
static void ResetSaveloadData()
Clear temporary data that is passed between various saveload phases.
Definition: saveload.cpp:2758
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
SaveOrLoadResult SaveWithFilter(std::shared_ptr< SaveFilter > writer, bool threaded)
Save the game using a (writer) filter.
Definition: saveload.cpp:2917
static void SlWriteSimpleGamma(size_t i)
Write the header descriptor of an object or an array.
Definition: saveload.cpp:494
static size_t SlCalcTableHeader(const SaveLoadTable &slt)
Calculate the size of the table header.
Definition: saveload.cpp:1482
static void ClearSaveLoadState()
Clear/free saveload state.
Definition: saveload.cpp:2768
bool _do_autosave
are we doing an autosave at the moment?
Definition: saveload.cpp:66
StringID GetSaveLoadErrorType()
Return the appropriate initial string for an error depending on whether we are saving or loading.
Definition: saveload.cpp:2805
static std::atomic< AsyncSaveFinishProc > _async_save_finish
Callback to call when the savegame loading is finished.
Definition: saveload.cpp:358
static std::thread _save_thread
The thread we're using to compress and write a savegame.
Definition: saveload.cpp:359
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
void SlWriteByte(uint8_t b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:401
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition: saveload.cpp:1689
SaveLoadAction
What are we currently doing?
Definition: saveload.cpp:69
@ SLA_LOAD
loading
Definition: saveload.cpp:70
@ SLA_NULL
null all pointers (on loading error)
Definition: saveload.cpp:73
@ SLA_SAVE
saving
Definition: saveload.cpp:71
@ SLA_LOAD_CHECK
partial loading into _load_check_data
Definition: saveload.cpp:74
@ SLA_PTRS
fixing pointers
Definition: saveload.cpp:72
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
void WriteValue(void *ptr, VarType conv, int64_t val)
Write the value of a setting.
Definition: saveload.cpp:817
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition: saveload.cpp:357
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
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:2160
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:3097
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
Called by save thread to tell we finished saving.
Definition: saveload.cpp:365
void SetSaveLoadError(StringID str)
Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friend...
Definition: saveload.cpp:2799
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1029
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:781
void DoAutoOrNetsave(FiosNumberedSaveName &counter)
Create an autosave or netsave.
Definition: saveload.cpp:3188
static size_t SlCalcRefLen()
Return the size in bytes of a reference (pointer)
Definition: saveload.cpp:641
static void SaveFileStart()
Update the gui accordingly when starting saving and set locks on saveload.
Definition: saveload.cpp:2777
static void SlNullPointers()
Null all pointers (convert index -> nullptr)
Definition: saveload.cpp:296
static void SlStdString(void *ptr, VarType conv)
Save/Load a std::string.
Definition: saveload.cpp:932
static SaveOrLoadResult DoLoad(std::shared_ptr< LoadFilter > reader, bool load_check)
Actually perform the loading of a "non-old" savegame.
Definition: saveload.cpp:2977
static size_t SlCalcRefListLen(const void *list, VarType conv)
Return the size in bytes of a list.
Definition: saveload.cpp:1333
static bool SlIsObjectValidInSavegame(const SaveLoad &sld)
Are we going to save this object or not?
Definition: saveload.cpp:1472
void SlSaveLoadRef(void *ptr, VarType conv)
Handle conversion for references.
Definition: saveload.cpp:1214
static void SlFixPointers()
Fix all pointers (convert index -> pointer)
Definition: saveload.cpp:2197
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:351
void SlSkipArray()
Skip an array or sparse array.
Definition: saveload.cpp:700
static void SlLoadChunk(const ChunkHandler &ch)
Load a chunk of data (eg vehicles, stations, etc.)
Definition: saveload.cpp:2004
static void SlLoadCheckChunks()
Load all chunks for savegame checking.
Definition: saveload.cpp:2182
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1893
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
static uint SlReadSimpleGamma()
Read in the header descriptor of an object or an array.
Definition: saveload.cpp:452
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1755
static void SlCopyBytes(void *ptr, size_t length)
Save/Load bytes.
Definition: saveload.cpp:764
NeedLength
Definition: saveload.cpp:77
@ NL_WANTLENGTH
writing length and data
Definition: saveload.cpp:79
@ NL_NONE
not working in NeedLength mode
Definition: saveload.cpp:78
@ NL_CALCLENGTH
need to calculate the length
Definition: saveload.cpp:80
static void SlCopyInternal(void *object, size_t length, VarType conv)
Internal function to save/Load a list of SL_VARs.
Definition: saveload.cpp:980
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
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:59
static void SlSaveLoadConv(void *ptr, VarType conv)
Handle all conversion and typechecking of variables here.
Definition: saveload.cpp:843
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:60
static void SlLoadCheckChunk(const ChunkHandler &ch)
Load a chunk of data for checking savegames.
Definition: saveload.cpp:2057
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
uint8_t SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:392
static SaveLoadParams _sl
Parameters used for/at saveload.
Definition: saveload.cpp:200
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
Definition: saveload.cpp:3206
static void SlLoadChunks()
Load all chunks.
Definition: saveload.cpp:2167
static void SlDeque(void *deque, VarType conv)
Save/load a std::deque.
Definition: saveload.cpp:1387
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
static void SlSaveChunk(const ChunkHandler &ch)
Save a chunk of data (eg.
Definition: saveload.cpp:2108
const SaveLoadVersion SAVEGAME_VERSION
Current savegame version of OpenTTD.
size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
Calculate the size of an object.
Definition: saveload.cpp:1511
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1702
static const std::vector< ChunkHandlerRef > & ChunkHandlers()
Definition: saveload.cpp:202
bool AfterLoadGame()
Perform a (large) amount of savegame conversion magic in order to load older savegames and to fill th...
Definition: afterload.cpp:565
int64_t ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:793
static void SlVector(void *vector, VarType conv)
Save/load a std::vector.
Definition: saveload.cpp:1445
static void SaveFileError()
Show a gui message when saving has failed.
Definition: saveload.cpp:2818
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1947
static void SlSaveChunks()
Save all chunks.
Definition: saveload.cpp:2144
std::string GenerateDefaultSaveName()
Get the default name for a savegame or screenshot.
Definition: saveload.cpp:3214
static const size_t MEMORY_CHUNK_SIZE
Save in chunks of 128 KiB.
Definition: saveload.cpp:84
void SlAutolength(AutolengthProc *proc, int arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1957
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
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition: saveload.cpp:1673
static uint SlGetGammaLength(size_t i)
Return how many bytes used to encode a gamma value.
Definition: saveload.cpp:519
static size_t SlCalcVectorLen(const void *vector, VarType conv)
Return the size in bytes of a std::vector.
Definition: saveload.cpp:1418
static void SlRefList(void *list, VarType conv)
Save/Load a list.
Definition: saveload.cpp:1343
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Return the size in bytes of a std::deque.
Definition: saveload.cpp:1360
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition: saveload.h:651
@ SLE_FILE_END
Used to mark end-of-header in tables.
Definition: saveload.h:624
@ SLE_FILE_TYPE_MASK
Mask to get the file-type (and not any flags).
Definition: saveload.h:638
@ SLE_FILE_HAS_LENGTH_FIELD
Bit stored in savegame to indicate field has a length field for each entry.
Definition: saveload.h:639
@ SLF_ALLOW_NEWLINE
Allow new lines in the strings.
Definition: saveload.h:687
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:686
@ SLE_VAR_STR
string pointer
Definition: saveload.h:652
@ SLE_VAR_NAME
old custom name to be converted to a char pointer
Definition: saveload.h:654
@ SLE_VAR_STRQ
string pointer enclosed in quotes
Definition: saveload.h:653
@ SLE_FILE_STRINGID
StringID offset into strings-array.
Definition: saveload.h:633
void * GetVariableAddress(const void *object, const SaveLoad &sld)
Get the address of the variable.
Definition: saveload.h:1277
SaveOrLoadResult
Save or load result codes.
Definition: saveload.h:401
@ SL_ERROR
error that was caught before internal structures were modified
Definition: saveload.h:403
@ SL_OK
completed successfully
Definition: saveload.h:402
@ SL_REINIT
error that was caught in the middle of updating game state, need to clear it. (can only happen during...
Definition: saveload.h:404
@ CH_TYPE_MASK
All ChunkType values have to be within this mask.
Definition: saveload.h:456
@ CH_READONLY
Chunk is never saved.
Definition: saveload.h:457
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:1321
SavegameType
Types of save games.
Definition: saveload.h:421
@ SGT_OTTD
OTTD savegame.
Definition: saveload.h:425
constexpr VarType GetVarFileType(VarType type)
Get the FileType of a setting.
Definition: saveload.h:758
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:510
SaveLoadType
Type of data saved.
Definition: saveload.h:693
@ SL_NULL
Save null-bytes and load to nowhere.
Definition: saveload.h:707
@ SL_STRUCTLIST
Save/load a list of structs.
Definition: saveload.h:704
@ SL_STDSTR
Save/load a std::string.
Definition: saveload.h:698
@ SL_REF
Save/load a reference.
Definition: saveload.h:695
@ SL_SAVEBYTE
Save (but not load) a byte.
Definition: saveload.h:706
@ SL_DEQUE
Save/load a deque of SL_VAR elements.
Definition: saveload.h:701
@ SL_STRUCT
Save/load a struct.
Definition: saveload.h:696
@ SL_VECTOR
Save/load a vector of SL_VAR elements.
Definition: saveload.h:702
@ SL_REFLIST
Save/load a list of SL_REF elements.
Definition: saveload.h:703
@ SL_ARR
Save/load a fixed-size array of SL_VAR elements.
Definition: saveload.h:700
@ SL_VAR
Save/load a variable.
Definition: saveload.h:694
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition: saveload.h:516
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1239
constexpr VarType GetVarMemType(VarType type)
Get the NumberType of a setting.
Definition: saveload.h:747
SaveLoadVersion
SaveLoad versions Previous savegame versions, the trunk revision where they were introduced and the r...
Definition: saveload.h:30
@ SLV_69
69 10319
Definition: saveload.h:125
@ 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
@ SLV_SAVELOAD_LIST_LENGTH
293 PR#9374 Consistency in list length with SL_STRUCT / SL_STRUCTLIST / SL_DEQUE / SL_REFLIST.
Definition: saveload.h:331
@ SLV_START_PATCHPACKS
220 First known patchpack to use a version just above ours.
Definition: saveload.h:321
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:397
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
@ SLV_END_PATCHPACKS
286 Last known patchpack to use a version just above ours.
Definition: saveload.h:322
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:43
@ SLV_169
169 23816
Definition: saveload.h:245
SLRefType
Type of reference (SLE_REF, SLE_CONDREF).
Definition: saveload.h:598
@ REF_VEHICLE_OLD
Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
Definition: saveload.h:603
@ REF_LINK_GRAPH_JOB
Load/save a reference to a link graph job.
Definition: saveload.h:610
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:602
@ REF_LINK_GRAPH
Load/save a reference to a link graph.
Definition: saveload.h:609
@ REF_CARGO_PACKET
Load/save a reference to a cargo packet.
Definition: saveload.h:606
@ REF_ENGINE_RENEWS
Load/save a reference to an engine renewal (autoreplace).
Definition: saveload.h:605
@ REF_STATION
Load/save a reference to a station.
Definition: saveload.h:601
@ REF_ORDER
Load/save a reference to an order.
Definition: saveload.h:599
@ REF_ORDERLIST
Load/save a reference to an orderlist.
Definition: saveload.h:607
@ REF_STORAGE
Load/save a reference to a persistent storage.
Definition: saveload.h:608
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition: saveload.h:600
@ REF_ROADSTOPS
Load/save a reference to a bus/truck stop.
Definition: saveload.h:604
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Definition: saveload.h:513
Declaration of filters used for saving and loading savegames.
Declaration of functions used in more save/load files.
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
Definition: strings_sl.cpp:30
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:61
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
@ SBI_SAVELOAD_FINISH
finished saving
Definition: statusbar_gui.h:16
@ SBI_SAVELOAD_START
started saving
Definition: statusbar_gui.h:15
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:280
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
size_t Utf8Decode(char32_t *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:419
size_t Utf8Encode(T buf, char32_t c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:460
int8_t Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
Definition: string_func.h:124
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:44
@ SVS_ALLOW_CONTROL_CODE
Allow the special control codes.
Definition: string_type.h:48
@ SVS_ALLOW_NEWLINE
Allow newlines; replaces '\r ' with ' ' during processing.
Definition: string_type.h:47
@ SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:46
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
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
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:357
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
Container for cargo from the same location and time.
Definition: cargopacket.h:40
Handlers and description of chunk.
Definition: saveload.h:461
ChunkType type
Type of the chunk.
Definition: saveload.h:463
virtual void LoadCheck(size_t len=0) const
Load the chunk for game preview.
Definition: saveload.cpp:1981
virtual void Load() const =0
Load the chunk.
uint32_t id
Unique ID (4 letters).
Definition: saveload.h:462
virtual void Save() const
Save the chunk.
Definition: saveload.h:473
GUISettings gui
settings related to the GUI
Struct to store engine replacements.
Yes, simply reading from a file.
Definition: saveload.cpp:2211
~FileReader()
Make sure everything is cleaned up.
Definition: saveload.cpp:2224
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2231
void Reset() override
Reset this filter to read from the beginning of the file.
Definition: saveload.cpp:2239
FileReader(FileHandle &&file)
Create the file reader, so it reads from a specific file.
Definition: saveload.cpp:2219
long begin
The begin of the file.
Definition: saveload.cpp:2213
std::optional< FileHandle > file
The file to read from.
Definition: saveload.cpp:2212
Deals with the type of the savegame, independent of extension.
Definition: saveload.h:408
AbstractFileType abstract_ftype
Abstract type of file (scenario, heightmap, etc).
Definition: saveload.h:411
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:3257
DetailedFileType detail_ftype
Concrete file type (PNG, BMP, old save, etc).
Definition: saveload.h:410
std::string title
Internal name of the game.
Definition: saveload.h:413
SaveLoadOperation file_op
File operation to perform.
Definition: saveload.h:409
std::string name
Name of the file.
Definition: saveload.h:412
void Set(const FiosItem &item)
Set the title of the file.
Definition: saveload.cpp:3286
Yes, simply writing to a file.
Definition: saveload.cpp:2249
std::optional< FileHandle > file
The file to write to.
Definition: saveload.cpp:2250
FileWriter(FileHandle &&file)
Create the file writer, so it writes to a specific file.
Definition: saveload.cpp:2256
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2274
~FileWriter()
Make sure everything is cleaned up.
Definition: saveload.cpp:2261
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2266
Deals with finding savegames.
Definition: fios.h:79
A savegame name automatically numbered.
Definition: fios.h:130
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition: fios.cpp:764
std::string Extension()
Generate an extension for a savegame name.
Definition: fios.cpp:774
bool keep_all_autosave
name the autosave in a different way
uint8_t date_format_in_default_names
should the default savegame/screenshot name use long dates (31th Dec 2008), short dates (31-12-2008) ...
bool threaded_saves
should we do threaded saves?
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
Filter without any compression.
Definition: saveload.cpp:2549
~LZMALoadFilter()
Clean everything up.
Definition: saveload.cpp:2564
lzma_stream lzma
Stream state that we are reading from.
Definition: saveload.cpp:2550
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2569
uint8_t fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
Definition: saveload.cpp:2551
LZMALoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload.cpp:2557
Filter using LZMA compression.
Definition: saveload.cpp:2592
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2638
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2643
void WriteLoop(uint8_t *p, size_t len, lzma_action action)
Helper loop for writing the data.
Definition: saveload.cpp:2618
LZMASaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Initialise this filter.
Definition: saveload.cpp:2601
lzma_stream lzma
Stream state that we are writing to.
Definition: saveload.cpp:2593
uint8_t fwrite_buf[MEMORY_CHUNK_SIZE]
Buffer for writing to the file.
Definition: saveload.cpp:2594
~LZMASaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2607
Filter using LZO compression.
Definition: saveload.cpp:2294
LZOLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload.cpp:2299
size_t Read(uint8_t *buf, size_t ssize) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2304
Filter using LZO compression.
Definition: saveload.cpp:2341
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2351
LZOSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t)
Initialise this filter.
Definition: saveload.cpp:2346
bool checkable
True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable....
Definition: fios.h:34
std::string error_msg
Data to pass to SetDParamStr when displaying error.
Definition: fios.h:36
StringID error
Error message from loading. INVALID_STRING_ID if no error.
Definition: fios.h:35
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:45
void Clear()
Reset read data.
Definition: fios_gui.cpp:49
GRFListCompatibility grf_compatibility
Summary state of NewGrfs, whether missing files or only compatible found.
Definition: fios.h:46
Interface for filtering a savegame till it is loaded.
std::shared_ptr< LoadFilter > chain
Chained to the (savegame) filters.
Container for dumping the savegame (quickly) to memory.
Definition: saveload.cpp:128
uint8_t * buf
Buffer we're going to write to.
Definition: saveload.cpp:130
void WriteByte(uint8_t b)
Write a single byte into the dumper.
Definition: saveload.cpp:137
std::vector< std::unique_ptr< uint8_t[]> > blocks
Buffer with blocks of allocated memory.
Definition: saveload.cpp:129
uint8_t * bufe
End of the buffer we write to.
Definition: saveload.cpp:131
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:171
void Flush(std::shared_ptr< SaveFilter > writer)
Flush this dumper into a writer.
Definition: saveload.cpp:152
Filter without any compression.
Definition: saveload.cpp:2381
NoCompLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload.cpp:2386
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2390
Filter without any compression.
Definition: saveload.cpp:2397
NoCompSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t)
Initialise this filter.
Definition: saveload.cpp:2402
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2406
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:259
Class for pooled persistent storage of data.
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:328
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
A buffer for reading (and buffering) savegame data.
Definition: saveload.cpp:87
uint8_t * bufp
Location we're at reading the buffer.
Definition: saveload.cpp:89
ReadBuffer(std::shared_ptr< LoadFilter > reader)
Initialise our variables.
Definition: saveload.cpp:98
size_t read
The amount of read bytes so far from the filter.
Definition: saveload.cpp:92
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:120
std::shared_ptr< LoadFilter > reader
The filter used to actually read.
Definition: saveload.cpp:91
uint8_t buf[MEMORY_CHUNK_SIZE]
Buffer we're going to read from.
Definition: saveload.cpp:88
uint8_t * bufe
End of the buffer we can read from.
Definition: saveload.cpp:90
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
Interface for filtering a savegame till it is written.
std::shared_ptr< SaveFilter > chain
Chained to the (savegame) filters.
The format for a reader/writer type of a savegame.
Definition: saveload.cpp:2657
std::shared_ptr< LoadFilter >(* init_load)(std::shared_ptr< LoadFilter > chain)
Constructor for the load filter.
Definition: saveload.cpp:2661
std::shared_ptr< SaveFilter >(* init_write)(std::shared_ptr< SaveFilter > chain, uint8_t compression)
Constructor for the save filter.
Definition: saveload.cpp:2662
const char * name
name of the compressor/decompressor (debug-only)
Definition: saveload.cpp:2658
uint32_t tag
the 4-letter tag by which it is identified in the savegame
Definition: saveload.cpp:2659
uint8_t min_compression
the minimum compression level of this format
Definition: saveload.cpp:2664
uint8_t default_compression
the default compression level of this format
Definition: saveload.cpp:2665
uint8_t max_compression
the maximum compression level of this format
Definition: saveload.cpp:2666
The saveload struct, containing reader-writer functions, buffer, version, etc.
Definition: saveload.cpp:178
std::unique_ptr< ReadBuffer > reader
Savegame reading buffer.
Definition: saveload.cpp:191
std::shared_ptr< SaveFilter > sf
Filter to write the savegame to.
Definition: saveload.cpp:189
std::unique_ptr< MemoryDumper > dumper
Memory dumper to write the savegame to.
Definition: saveload.cpp:188
StringID error_str
the translatable error message to show
Definition: saveload.cpp:194
SaveLoadAction action
are we doing a save or a load atm.
Definition: saveload.cpp:179
std::string extra_msg
the error message
Definition: saveload.cpp:195
NeedLength need_length
working in NeedLength (Autolength) mode?
Definition: saveload.cpp:180
uint8_t block_mode
???
Definition: saveload.cpp:181
bool saveinprogress
Whether there is currently a save in progress.
Definition: saveload.cpp:197
std::shared_ptr< LoadFilter > lf
Filter to read the savegame from.
Definition: saveload.cpp:192
bool expect_table_header
In the case of a table, if the header is saved/loaded.
Definition: saveload.cpp:186
size_t obj_len
the length of the current object we are busy with
Definition: saveload.cpp:184
bool error
did an error occur or not
Definition: saveload.cpp:182
int last_array_index
in the case of an array, the current and last positions
Definition: saveload.cpp:185
SaveLoad type struct.
Definition: saveload.h:713
uint16_t length
(Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
Definition: saveload.h:717
std::shared_ptr< SaveLoadHandler > handler
Custom handler for Save/Load procs.
Definition: saveload.h:722
SaveLoadVersion version_to
Save/load the variable before this savegame version.
Definition: saveload.h:719
SaveLoadType cmd
The action to take with the saved/loaded type, All types need different action.
Definition: saveload.h:715
std::string name
Name of this field (optional, used for tables).
Definition: saveload.h:714
VarType conv
Type of the variable to be saved; this field combines both FileVarType and MemVarType.
Definition: saveload.h:716
SaveLoadVersion version_from
Save/load the variable starting from this savegame version.
Definition: saveload.h:718
static Station * Get(size_t index)
Gets station with given index.
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
Station data structure.
Definition: station_base.h:439
Town data structure.
Definition: town.h:54
Vehicle data structure.
Definition: vehicle_base.h:244
Filter using Zlib compression.
Definition: saveload.cpp:2420
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2440
ZlibLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Definition: saveload.cpp:2428
uint8_t fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
Definition: saveload.cpp:2422
~ZlibLoadFilter()
Clean everything up.
Definition: saveload.cpp:2435
z_stream z
Stream state we are reading from.
Definition: saveload.cpp:2421
Filter using Zlib compression.
Definition: saveload.cpp:2464
void WriteLoop(uint8_t *p, size_t len, int mode)
Helper loop for writing the data.
Definition: saveload.cpp:2491
z_stream z
Stream state we are writing to.
Definition: saveload.cpp:2465
uint8_t fwrite_buf[MEMORY_CHUNK_SIZE]
Buffer for writing to the file.
Definition: saveload.cpp:2466
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2524
~ZlibSaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2480
ZlibSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Initialise this filter.
Definition: saveload.cpp:2473
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2519
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:24
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:47
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:3211
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:64