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