OpenTTD
saveload.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
22 #include <deque>
23 
24 #include "../stdafx.h"
25 #include "../debug.h"
26 #include "../station_base.h"
27 #include "../thread.h"
28 #include "../town.h"
29 #include "../network/network.h"
30 #include "../window_func.h"
31 #include "../strings_func.h"
32 #include "../core/endian_func.hpp"
33 #include "../vehicle_base.h"
34 #include "../company_func.h"
35 #include "../date_func.h"
36 #include "../autoreplace_base.h"
37 #include "../roadstop_base.h"
38 #include "../linkgraph/linkgraph.h"
39 #include "../linkgraph/linkgraphjob.h"
40 #include "../statusbar_gui.h"
41 #include "../fileio_func.h"
42 #include "../gamelog.h"
43 #include "../string_func.h"
44 #include "../fios.h"
45 #include "../error.h"
46 #include <atomic>
47 
48 #include "table/strings.h"
49 
50 #include "saveload_internal.h"
51 #include "saveload_filter.h"
52 
53 #include "../safeguards.h"
54 
56 
59 
60 uint32 _ttdp_version;
65 
73 };
74 
75 enum NeedLength {
76  NL_NONE = 0,
79 };
80 
82 static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
83 
85 struct ReadBuffer {
87  byte *bufp;
88  byte *bufe;
90  size_t read;
91 
96  ReadBuffer(LoadFilter *reader) : bufp(nullptr), bufe(nullptr), reader(reader), read(0)
97  {
98  }
99 
100  inline byte ReadByte()
101  {
102  if (this->bufp == this->bufe) {
103  size_t len = this->reader->Read(this->buf, lengthof(this->buf));
104  if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
105 
106  this->read += len;
107  this->bufp = this->buf;
108  this->bufe = this->buf + len;
109  }
110 
111  return *this->bufp++;
112  }
113 
118  size_t GetSize() const
119  {
120  return this->read - (this->bufe - this->bufp);
121  }
122 };
123 
124 
126 struct MemoryDumper {
127  std::vector<byte *> blocks;
128  byte *buf;
129  byte *bufe;
130 
132  MemoryDumper() : buf(nullptr), bufe(nullptr)
133  {
134  }
135 
136  ~MemoryDumper()
137  {
138  for (auto p : this->blocks) {
139  free(p);
140  }
141  }
142 
147  inline void WriteByte(byte b)
148  {
149  /* Are we at the end of this chunk? */
150  if (this->buf == this->bufe) {
151  this->buf = CallocT<byte>(MEMORY_CHUNK_SIZE);
152  this->blocks.push_back(this->buf);
153  this->bufe = this->buf + MEMORY_CHUNK_SIZE;
154  }
155 
156  *this->buf++ = b;
157  }
158 
163  void Flush(SaveFilter *writer)
164  {
165  uint i = 0;
166  size_t t = this->GetSize();
167 
168  while (t > 0) {
169  size_t to_write = min(MEMORY_CHUNK_SIZE, t);
170 
171  writer->Write(this->blocks[i++], to_write);
172  t -= to_write;
173  }
174 
175  writer->Finish();
176  }
177 
182  size_t GetSize() const
183  {
184  return this->blocks.size() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
185  }
186 };
187 
192  byte block_mode;
193  bool error;
194 
195  size_t obj_len;
196  int array_index, last_array_index;
197 
200 
203 
205  char *extra_msg;
206 
207  byte ff_state;
209 };
210 
212 
213 /* these define the chunks */
214 extern const ChunkHandler _gamelog_chunk_handlers[];
215 extern const ChunkHandler _map_chunk_handlers[];
216 extern const ChunkHandler _misc_chunk_handlers[];
217 extern const ChunkHandler _name_chunk_handlers[];
218 extern const ChunkHandler _cheat_chunk_handlers[] ;
219 extern const ChunkHandler _setting_chunk_handlers[];
220 extern const ChunkHandler _company_chunk_handlers[];
221 extern const ChunkHandler _engine_chunk_handlers[];
222 extern const ChunkHandler _veh_chunk_handlers[];
223 extern const ChunkHandler _waypoint_chunk_handlers[];
224 extern const ChunkHandler _depot_chunk_handlers[];
225 extern const ChunkHandler _order_chunk_handlers[];
226 extern const ChunkHandler _town_chunk_handlers[];
227 extern const ChunkHandler _sign_chunk_handlers[];
228 extern const ChunkHandler _station_chunk_handlers[];
229 extern const ChunkHandler _industry_chunk_handlers[];
230 extern const ChunkHandler _economy_chunk_handlers[];
231 extern const ChunkHandler _subsidy_chunk_handlers[];
233 extern const ChunkHandler _goal_chunk_handlers[];
234 extern const ChunkHandler _story_page_chunk_handlers[];
235 extern const ChunkHandler _ai_chunk_handlers[];
236 extern const ChunkHandler _game_chunk_handlers[];
238 extern const ChunkHandler _newgrf_chunk_handlers[];
239 extern const ChunkHandler _group_chunk_handlers[];
241 extern const ChunkHandler _autoreplace_chunk_handlers[];
242 extern const ChunkHandler _labelmaps_chunk_handlers[];
243 extern const ChunkHandler _linkgraph_chunk_handlers[];
244 extern const ChunkHandler _airport_chunk_handlers[];
245 extern const ChunkHandler _object_chunk_handlers[];
247 
249 static const ChunkHandler * const _chunk_handlers[] = {
250  _gamelog_chunk_handlers,
251  _map_chunk_handlers,
252  _misc_chunk_handlers,
255  _setting_chunk_handlers,
256  _veh_chunk_handlers,
257  _waypoint_chunk_handlers,
258  _depot_chunk_handlers,
259  _order_chunk_handlers,
260  _industry_chunk_handlers,
261  _economy_chunk_handlers,
262  _subsidy_chunk_handlers,
264  _goal_chunk_handlers,
265  _story_page_chunk_handlers,
266  _engine_chunk_handlers,
269  _station_chunk_handlers,
270  _company_chunk_handlers,
271  _ai_chunk_handlers,
272  _game_chunk_handlers,
274  _newgrf_chunk_handlers,
275  _group_chunk_handlers,
277  _autoreplace_chunk_handlers,
278  _labelmaps_chunk_handlers,
279  _linkgraph_chunk_handlers,
280  _airport_chunk_handlers,
281  _object_chunk_handlers,
283  nullptr,
284 };
285 
290 #define FOR_ALL_CHUNK_HANDLERS(ch) \
291  for (const ChunkHandler * const *chsc = _chunk_handlers; *chsc != nullptr; chsc++) \
292  for (const ChunkHandler *ch = *chsc; ch != nullptr; ch = (ch->flags & CH_LAST) ? nullptr : ch + 1)
293 
295 static void SlNullPointers()
296 {
297  _sl.action = SLA_NULL;
298 
299  /* We don't want any savegame conversion code to run
300  * during NULLing; especially those that try to get
301  * pointers from other pools. */
302  _sl_version = SAVEGAME_VERSION;
303 
304  DEBUG(sl, 1, "Nulling pointers");
305 
307  if (ch->ptrs_proc != nullptr) {
308  DEBUG(sl, 2, "Nulling pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
309  ch->ptrs_proc();
310  }
311  }
312 
313  DEBUG(sl, 1, "All pointers nulled");
314 
315  assert(_sl.action == SLA_NULL);
316 }
317 
326 void NORETURN SlError(StringID string, const char *extra_msg)
327 {
328  /* Distinguish between loading into _load_check_data vs. normal save/load. */
329  if (_sl.action == SLA_LOAD_CHECK) {
330  _load_check_data.error = string;
332  _load_check_data.error_data = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
333  } else {
334  _sl.error_str = string;
335  free(_sl.extra_msg);
336  _sl.extra_msg = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
337  }
338 
339  /* We have to nullptr all pointers here; we might be in a state where
340  * the pointers are actually filled with indices, which means that
341  * when we access them during cleaning the pool dereferences of
342  * those indices will be made with segmentation faults as result. */
343  if (_sl.action == SLA_LOAD || _sl.action == SLA_PTRS) SlNullPointers();
344  throw std::exception();
345 }
346 
354 void NORETURN SlErrorCorrupt(const char *msg)
355 {
356  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
357 }
358 
366 void NORETURN SlErrorCorruptFmt(const char *format, ...)
367 {
368  va_list ap;
369  char msg[256];
370 
371  va_start(ap, format);
372  vseprintf(msg, lastof(msg), format, ap);
373  va_end(ap);
374 
375  SlErrorCorrupt(msg);
376 }
377 
378 
379 typedef void (*AsyncSaveFinishProc)();
380 static std::atomic<AsyncSaveFinishProc> _async_save_finish;
381 static std::thread _save_thread;
382 
388 {
389  if (_exit_game) return;
390  while (_async_save_finish.load(std::memory_order_acquire) != nullptr) CSleep(10);
391 
392  _async_save_finish.store(proc, std::memory_order_release);
393 }
394 
399 {
400  AsyncSaveFinishProc proc = _async_save_finish.exchange(nullptr, std::memory_order_acq_rel);
401  if (proc == nullptr) return;
402 
403  proc();
404 
405  if (_save_thread.joinable()) {
406  _save_thread.join();
407  }
408 }
409 
415 {
416  return _sl.reader->ReadByte();
417 }
418 
423 void SlWriteByte(byte b)
424 {
425  _sl.dumper->WriteByte(b);
426 }
427 
428 static inline int SlReadUint16()
429 {
430  int x = SlReadByte() << 8;
431  return x | SlReadByte();
432 }
433 
434 static inline uint32 SlReadUint32()
435 {
436  uint32 x = SlReadUint16() << 16;
437  return x | SlReadUint16();
438 }
439 
440 static inline uint64 SlReadUint64()
441 {
442  uint32 x = SlReadUint32();
443  uint32 y = SlReadUint32();
444  return (uint64)x << 32 | y;
445 }
446 
447 static inline void SlWriteUint16(uint16 v)
448 {
449  SlWriteByte(GB(v, 8, 8));
450  SlWriteByte(GB(v, 0, 8));
451 }
452 
453 static inline void SlWriteUint32(uint32 v)
454 {
455  SlWriteUint16(GB(v, 16, 16));
456  SlWriteUint16(GB(v, 0, 16));
457 }
458 
459 static inline void SlWriteUint64(uint64 x)
460 {
461  SlWriteUint32((uint32)(x >> 32));
462  SlWriteUint32((uint32)x);
463 }
464 
470 static inline void SlSkipBytes(size_t length)
471 {
472  for (; length != 0; length--) SlReadByte();
473 }
474 
484 static uint SlReadSimpleGamma()
485 {
486  uint i = SlReadByte();
487  if (HasBit(i, 7)) {
488  i &= ~0x80;
489  if (HasBit(i, 6)) {
490  i &= ~0x40;
491  if (HasBit(i, 5)) {
492  i &= ~0x20;
493  if (HasBit(i, 4)) {
494  i &= ~0x10;
495  if (HasBit(i, 3)) {
496  SlErrorCorrupt("Unsupported gamma");
497  }
498  i = SlReadByte(); // 32 bits only.
499  }
500  i = (i << 8) | SlReadByte();
501  }
502  i = (i << 8) | SlReadByte();
503  }
504  i = (i << 8) | SlReadByte();
505  }
506  return i;
507 }
508 
526 static void SlWriteSimpleGamma(size_t i)
527 {
528  if (i >= (1 << 7)) {
529  if (i >= (1 << 14)) {
530  if (i >= (1 << 21)) {
531  if (i >= (1 << 28)) {
532  assert(i <= UINT32_MAX); // We can only support 32 bits for now.
533  SlWriteByte((byte)(0xF0));
534  SlWriteByte((byte)(i >> 24));
535  } else {
536  SlWriteByte((byte)(0xE0 | (i >> 24)));
537  }
538  SlWriteByte((byte)(i >> 16));
539  } else {
540  SlWriteByte((byte)(0xC0 | (i >> 16)));
541  }
542  SlWriteByte((byte)(i >> 8));
543  } else {
544  SlWriteByte((byte)(0x80 | (i >> 8)));
545  }
546  }
547  SlWriteByte((byte)i);
548 }
549 
551 static inline uint SlGetGammaLength(size_t i)
552 {
553  return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
554 }
555 
556 static inline uint SlReadSparseIndex()
557 {
558  return SlReadSimpleGamma();
559 }
560 
561 static inline void SlWriteSparseIndex(uint index)
562 {
563  SlWriteSimpleGamma(index);
564 }
565 
566 static inline uint SlReadArrayLength()
567 {
568  return SlReadSimpleGamma();
569 }
570 
571 static inline void SlWriteArrayLength(size_t length)
572 {
573  SlWriteSimpleGamma(length);
574 }
575 
576 static inline uint SlGetArrayLength(size_t length)
577 {
578  return SlGetGammaLength(length);
579 }
580 
587 static inline uint SlCalcConvMemLen(VarType conv)
588 {
589  static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
590  byte length = GB(conv, 4, 4);
591 
592  switch (length << 4) {
593  case SLE_VAR_STRB:
594  case SLE_VAR_STRBQ:
595  case SLE_VAR_STR:
596  case SLE_VAR_STRQ:
597  return SlReadArrayLength();
598 
599  default:
600  assert(length < lengthof(conv_mem_size));
601  return conv_mem_size[length];
602  }
603 }
604 
611 static inline byte SlCalcConvFileLen(VarType conv)
612 {
613  static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
614  byte length = GB(conv, 0, 4);
615  assert(length < lengthof(conv_file_size));
616  return conv_file_size[length];
617 }
618 
620 static inline size_t SlCalcRefLen()
621 {
622  return IsSavegameVersionBefore(SLV_69) ? 2 : 4;
623 }
624 
625 void SlSetArrayIndex(uint index)
626 {
628  _sl.array_index = index;
629 }
630 
631 static size_t _next_offs;
632 
638 {
639  int index;
640 
641  /* After reading in the whole array inside the loop
642  * we must have read in all the data, so we must be at end of current block. */
643  if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) SlErrorCorrupt("Invalid chunk size");
644 
645  for (;;) {
646  uint length = SlReadArrayLength();
647  if (length == 0) {
648  _next_offs = 0;
649  return -1;
650  }
651 
652  _sl.obj_len = --length;
653  _next_offs = _sl.reader->GetSize() + length;
654 
655  switch (_sl.block_mode) {
656  case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
657  case CH_ARRAY: index = _sl.array_index++; break;
658  default:
659  DEBUG(sl, 0, "SlIterateArray error");
660  return -1; // error
661  }
662 
663  if (length != 0) return index;
664  }
665 }
666 
671 {
672  while (SlIterateArray() != -1) {
673  SlSkipBytes(_next_offs - _sl.reader->GetSize());
674  }
675 }
676 
682 void SlSetLength(size_t length)
683 {
684  assert(_sl.action == SLA_SAVE);
685 
686  switch (_sl.need_length) {
687  case NL_WANTLENGTH:
688  _sl.need_length = NL_NONE;
689  switch (_sl.block_mode) {
690  case CH_RIFF:
691  /* Ugly encoding of >16M RIFF chunks
692  * The lower 24 bits are normal
693  * The uppermost 4 bits are bits 24:27 */
694  assert(length < (1 << 28));
695  SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
696  break;
697  case CH_ARRAY:
698  assert(_sl.last_array_index <= _sl.array_index);
699  while (++_sl.last_array_index <= _sl.array_index) {
700  SlWriteArrayLength(1);
701  }
702  SlWriteArrayLength(length + 1);
703  break;
704  case CH_SPARSE_ARRAY:
705  SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
706  SlWriteSparseIndex(_sl.array_index);
707  break;
708  default: NOT_REACHED();
709  }
710  break;
711 
712  case NL_CALCLENGTH:
713  _sl.obj_len += (int)length;
714  break;
715 
716  default: NOT_REACHED();
717  }
718 }
719 
726 static void SlCopyBytes(void *ptr, size_t length)
727 {
728  byte *p = (byte *)ptr;
729 
730  switch (_sl.action) {
731  case SLA_LOAD_CHECK:
732  case SLA_LOAD:
733  for (; length != 0; length--) *p++ = SlReadByte();
734  break;
735  case SLA_SAVE:
736  for (; length != 0; length--) SlWriteByte(*p++);
737  break;
738  default: NOT_REACHED();
739  }
740 }
741 
744 {
745  return _sl.obj_len;
746 }
747 
755 int64 ReadValue(const void *ptr, VarType conv)
756 {
757  switch (GetVarMemType(conv)) {
758  case SLE_VAR_BL: return (*(const bool *)ptr != 0);
759  case SLE_VAR_I8: return *(const int8 *)ptr;
760  case SLE_VAR_U8: return *(const byte *)ptr;
761  case SLE_VAR_I16: return *(const int16 *)ptr;
762  case SLE_VAR_U16: return *(const uint16*)ptr;
763  case SLE_VAR_I32: return *(const int32 *)ptr;
764  case SLE_VAR_U32: return *(const uint32*)ptr;
765  case SLE_VAR_I64: return *(const int64 *)ptr;
766  case SLE_VAR_U64: return *(const uint64*)ptr;
767  case SLE_VAR_NULL:return 0;
768  default: NOT_REACHED();
769  }
770 }
771 
779 void WriteValue(void *ptr, VarType conv, int64 val)
780 {
781  switch (GetVarMemType(conv)) {
782  case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
783  case SLE_VAR_I8: *(int8 *)ptr = val; break;
784  case SLE_VAR_U8: *(byte *)ptr = val; break;
785  case SLE_VAR_I16: *(int16 *)ptr = val; break;
786  case SLE_VAR_U16: *(uint16*)ptr = val; break;
787  case SLE_VAR_I32: *(int32 *)ptr = val; break;
788  case SLE_VAR_U32: *(uint32*)ptr = val; break;
789  case SLE_VAR_I64: *(int64 *)ptr = val; break;
790  case SLE_VAR_U64: *(uint64*)ptr = val; break;
791  case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
792  case SLE_VAR_NULL: break;
793  default: NOT_REACHED();
794  }
795 }
796 
805 static void SlSaveLoadConv(void *ptr, VarType conv)
806 {
807  switch (_sl.action) {
808  case SLA_SAVE: {
809  int64 x = ReadValue(ptr, conv);
810 
811  /* Write the value to the file and check if its value is in the desired range */
812  switch (GetVarFileType(conv)) {
813  case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
814  case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
815  case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
816  case SLE_FILE_STRINGID:
817  case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
818  case SLE_FILE_I32:
819  case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
820  case SLE_FILE_I64:
821  case SLE_FILE_U64: SlWriteUint64(x);break;
822  default: NOT_REACHED();
823  }
824  break;
825  }
826  case SLA_LOAD_CHECK:
827  case SLA_LOAD: {
828  int64 x;
829  /* Read a value from the file */
830  switch (GetVarFileType(conv)) {
831  case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
832  case SLE_FILE_U8: x = (byte )SlReadByte(); break;
833  case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
834  case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
835  case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
836  case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
837  case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
838  case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
839  case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
840  default: NOT_REACHED();
841  }
842 
843  /* Write The value to the struct. These ARE endian safe. */
844  WriteValue(ptr, conv, x);
845  break;
846  }
847  case SLA_PTRS: break;
848  case SLA_NULL: break;
849  default: NOT_REACHED();
850  }
851 }
852 
862 static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
863 {
864  if (ptr == nullptr) return 0;
865  return min(strlen(ptr), length - 1);
866 }
867 
877 static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
878 {
879  size_t len;
880  const char *str;
881 
882  switch (GetVarMemType(conv)) {
883  default: NOT_REACHED();
884  case SLE_VAR_STR:
885  case SLE_VAR_STRQ:
886  str = *(const char * const *)ptr;
887  len = SIZE_MAX;
888  break;
889  case SLE_VAR_STRB:
890  case SLE_VAR_STRBQ:
891  str = (const char *)ptr;
892  len = length;
893  break;
894  }
895 
896  len = SlCalcNetStringLen(str, len);
897  return len + SlGetArrayLength(len); // also include the length of the index
898 }
899 
906 static void SlString(void *ptr, size_t length, VarType conv)
907 {
908  switch (_sl.action) {
909  case SLA_SAVE: {
910  size_t len;
911  switch (GetVarMemType(conv)) {
912  default: NOT_REACHED();
913  case SLE_VAR_STRB:
914  case SLE_VAR_STRBQ:
915  len = SlCalcNetStringLen((char *)ptr, length);
916  break;
917  case SLE_VAR_STR:
918  case SLE_VAR_STRQ:
919  ptr = *(char **)ptr;
920  len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
921  break;
922  }
923 
924  SlWriteArrayLength(len);
925  SlCopyBytes(ptr, len);
926  break;
927  }
928  case SLA_LOAD_CHECK:
929  case SLA_LOAD: {
930  size_t len = SlReadArrayLength();
931 
932  switch (GetVarMemType(conv)) {
933  default: NOT_REACHED();
934  case SLE_VAR_STRB:
935  case SLE_VAR_STRBQ:
936  if (len >= length) {
937  DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
938  SlCopyBytes(ptr, length);
939  SlSkipBytes(len - length);
940  len = length - 1;
941  } else {
942  SlCopyBytes(ptr, len);
943  }
944  break;
945  case SLE_VAR_STR:
946  case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
947  free(*(char **)ptr);
948  if (len == 0) {
949  *(char **)ptr = nullptr;
950  return;
951  } else {
952  *(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
953  ptr = *(char **)ptr;
954  SlCopyBytes(ptr, len);
955  }
956  break;
957  }
958 
959  ((char *)ptr)[len] = '\0'; // properly terminate the string
961  if ((conv & SLF_ALLOW_CONTROL) != 0) {
962  settings = settings | SVS_ALLOW_CONTROL_CODE;
964  str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
965  }
966  }
967  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
968  settings = settings | SVS_ALLOW_NEWLINE;
969  }
970  str_validate((char *)ptr, (char *)ptr + len, settings);
971  break;
972  }
973  case SLA_PTRS: break;
974  case SLA_NULL: break;
975  default: NOT_REACHED();
976  }
977 }
978 
984 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
985 {
986  return SlCalcConvFileLen(conv) * length;
987 }
988 
995 void SlArray(void *array, size_t length, VarType conv)
996 {
997  if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
998 
999  /* Automatically calculate the length? */
1000  if (_sl.need_length != NL_NONE) {
1001  SlSetLength(SlCalcArrayLen(length, conv));
1002  /* Determine length only? */
1003  if (_sl.need_length == NL_CALCLENGTH) return;
1004  }
1005 
1006  /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1007  * as a byte-type. So detect this, and adjust array size accordingly */
1008  if (_sl.action != SLA_SAVE && _sl_version == 0) {
1009  /* all arrays except difficulty settings */
1010  if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1011  conv == SLE_INT32 || conv == SLE_UINT32) {
1012  SlCopyBytes(array, length * SlCalcConvFileLen(conv));
1013  return;
1014  }
1015  /* used for conversion of Money 32bit->64bit */
1016  if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1017  for (uint i = 0; i < length; i++) {
1018  ((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
1019  }
1020  return;
1021  }
1022  }
1023 
1024  /* If the size of elements is 1 byte both in file and memory, no special
1025  * conversion is needed, use specialized copy-copy function to speed up things */
1026  if (conv == SLE_INT8 || conv == SLE_UINT8) {
1027  SlCopyBytes(array, length);
1028  } else {
1029  byte *a = (byte*)array;
1030  byte mem_size = SlCalcConvMemLen(conv);
1031 
1032  for (; length != 0; length --) {
1033  SlSaveLoadConv(a, conv);
1034  a += mem_size; // get size
1035  }
1036  }
1037 }
1038 
1039 
1050 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1051 {
1052  assert(_sl.action == SLA_SAVE);
1053 
1054  if (obj == nullptr) return 0;
1055 
1056  switch (rt) {
1057  case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1058  case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1059  case REF_STATION: return ((const Station*)obj)->index + 1;
1060  case REF_TOWN: return ((const Town*)obj)->index + 1;
1061  case REF_ORDER: return ((const Order*)obj)->index + 1;
1062  case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1063  case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1064  case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1065  case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1066  case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1067  case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1068  case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1069  default: NOT_REACHED();
1070  }
1071 }
1072 
1083 static void *IntToReference(size_t index, SLRefType rt)
1084 {
1085  assert_compile(sizeof(size_t) <= sizeof(void *));
1086 
1087  assert(_sl.action == SLA_PTRS);
1088 
1089  /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1090  * and should be loaded like that */
1091  if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) {
1092  rt = REF_VEHICLE;
1093  }
1094 
1095  /* No need to look up nullptr pointers, just return immediately */
1096  if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return nullptr;
1097 
1098  /* Correct index. Old vehicles were saved differently:
1099  * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1100  if (rt != REF_VEHICLE_OLD) index--;
1101 
1102  switch (rt) {
1103  case REF_ORDERLIST:
1104  if (OrderList::IsValidID(index)) return OrderList::Get(index);
1105  SlErrorCorrupt("Referencing invalid OrderList");
1106 
1107  case REF_ORDER:
1108  if (Order::IsValidID(index)) return Order::Get(index);
1109  /* in old versions, invalid order was used to mark end of order list */
1110  if (IsSavegameVersionBefore(SLV_5, 2)) return nullptr;
1111  SlErrorCorrupt("Referencing invalid Order");
1112 
1113  case REF_VEHICLE_OLD:
1114  case REF_VEHICLE:
1115  if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1116  SlErrorCorrupt("Referencing invalid Vehicle");
1117 
1118  case REF_STATION:
1119  if (Station::IsValidID(index)) return Station::Get(index);
1120  SlErrorCorrupt("Referencing invalid Station");
1121 
1122  case REF_TOWN:
1123  if (Town::IsValidID(index)) return Town::Get(index);
1124  SlErrorCorrupt("Referencing invalid Town");
1125 
1126  case REF_ROADSTOPS:
1127  if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1128  SlErrorCorrupt("Referencing invalid RoadStop");
1129 
1130  case REF_ENGINE_RENEWS:
1131  if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1132  SlErrorCorrupt("Referencing invalid EngineRenew");
1133 
1134  case REF_CARGO_PACKET:
1135  if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1136  SlErrorCorrupt("Referencing invalid CargoPacket");
1137 
1138  case REF_STORAGE:
1139  if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1140  SlErrorCorrupt("Referencing invalid PersistentStorage");
1141 
1142  case REF_LINK_GRAPH:
1143  if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1144  SlErrorCorrupt("Referencing invalid LinkGraph");
1145 
1146  case REF_LINK_GRAPH_JOB:
1147  if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1148  SlErrorCorrupt("Referencing invalid LinkGraphJob");
1149 
1150  default: NOT_REACHED();
1151  }
1152 }
1153 
1158 static inline size_t SlCalcListLen(const void *list)
1159 {
1160  const std::list<void *> *l = (const std::list<void *> *) list;
1161 
1162  int type_size = IsSavegameVersionBefore(SLV_69) ? 2 : 4;
1163  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1164  * of the list */
1165  return l->size() * type_size + type_size;
1166 }
1167 
1168 
1174 static void SlList(void *list, SLRefType conv)
1175 {
1176  /* Automatically calculate the length? */
1177  if (_sl.need_length != NL_NONE) {
1178  SlSetLength(SlCalcListLen(list));
1179  /* Determine length only? */
1180  if (_sl.need_length == NL_CALCLENGTH) return;
1181  }
1182 
1183  typedef std::list<void *> PtrList;
1184  PtrList *l = (PtrList *)list;
1185 
1186  switch (_sl.action) {
1187  case SLA_SAVE: {
1188  SlWriteUint32((uint32)l->size());
1189 
1190  PtrList::iterator iter;
1191  for (iter = l->begin(); iter != l->end(); ++iter) {
1192  void *ptr = *iter;
1193  SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
1194  }
1195  break;
1196  }
1197  case SLA_LOAD_CHECK:
1198  case SLA_LOAD: {
1199  size_t length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1200 
1201  /* Load each reference and push to the end of the list */
1202  for (size_t i = 0; i < length; i++) {
1203  size_t data = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1204  l->push_back((void *)data);
1205  }
1206  break;
1207  }
1208  case SLA_PTRS: {
1209  PtrList temp = *l;
1210 
1211  l->clear();
1212  PtrList::iterator iter;
1213  for (iter = temp.begin(); iter != temp.end(); ++iter) {
1214  void *ptr = IntToReference((size_t)*iter, conv);
1215  l->push_back(ptr);
1216  }
1217  break;
1218  }
1219  case SLA_NULL:
1220  l->clear();
1221  break;
1222  default: NOT_REACHED();
1223  }
1224 }
1225 
1226 
1230 template <typename T>
1232  typedef std::deque<T> SlDequeT;
1233 public:
1239  static size_t SlCalcDequeLen(const void *deque, VarType conv)
1240  {
1241  const SlDequeT *l = (const SlDequeT *)deque;
1242 
1243  int type_size = 4;
1244  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1245  * of the list */
1246  return l->size() * SlCalcConvFileLen(conv) + type_size;
1247  }
1248 
1254  static void SlDeque(void *deque, VarType conv)
1255  {
1256  SlDequeT *l = (SlDequeT *)deque;
1257 
1258  switch (_sl.action) {
1259  case SLA_SAVE: {
1260  SlWriteUint32((uint32)l->size());
1261 
1262  typename SlDequeT::iterator iter;
1263  for (iter = l->begin(); iter != l->end(); ++iter) {
1264  SlSaveLoadConv(&(*iter), conv);
1265  }
1266  break;
1267  }
1268  case SLA_LOAD_CHECK:
1269  case SLA_LOAD: {
1270  size_t length = SlReadUint32();
1271 
1272  /* Load each value and push to the end of the deque */
1273  for (size_t i = 0; i < length; i++) {
1274  T data;
1275  SlSaveLoadConv(&data, conv);
1276  l->push_back(data);
1277  }
1278  break;
1279  }
1280  case SLA_PTRS:
1281  break;
1282  case SLA_NULL:
1283  l->clear();
1284  break;
1285  default: NOT_REACHED();
1286  }
1287  }
1288 };
1289 
1290 
1296 static inline size_t SlCalcDequeLen(const void *deque, VarType conv)
1297 {
1298  switch (GetVarMemType(conv)) {
1299  case SLE_VAR_BL:
1300  return SlDequeHelper<bool>::SlCalcDequeLen(deque, conv);
1301  case SLE_VAR_I8:
1302  case SLE_VAR_U8:
1303  return SlDequeHelper<uint8>::SlCalcDequeLen(deque, conv);
1304  case SLE_VAR_I16:
1305  case SLE_VAR_U16:
1306  return SlDequeHelper<uint16>::SlCalcDequeLen(deque, conv);
1307  case SLE_VAR_I32:
1308  case SLE_VAR_U32:
1309  return SlDequeHelper<uint32>::SlCalcDequeLen(deque, conv);
1310  case SLE_VAR_I64:
1311  case SLE_VAR_U64:
1312  return SlDequeHelper<uint64>::SlCalcDequeLen(deque, conv);
1313  default: NOT_REACHED();
1314  }
1315 }
1316 
1317 
1323 static void SlDeque(void *deque, VarType conv)
1324 {
1325  switch (GetVarMemType(conv)) {
1326  case SLE_VAR_BL:
1327  SlDequeHelper<bool>::SlDeque(deque, conv);
1328  break;
1329  case SLE_VAR_I8:
1330  case SLE_VAR_U8:
1331  SlDequeHelper<uint8>::SlDeque(deque, conv);
1332  break;
1333  case SLE_VAR_I16:
1334  case SLE_VAR_U16:
1335  SlDequeHelper<uint16>::SlDeque(deque, conv);
1336  break;
1337  case SLE_VAR_I32:
1338  case SLE_VAR_U32:
1339  SlDequeHelper<uint32>::SlDeque(deque, conv);
1340  break;
1341  case SLE_VAR_I64:
1342  case SLE_VAR_U64:
1343  SlDequeHelper<uint64>::SlDeque(deque, conv);
1344  break;
1345  default: NOT_REACHED();
1346  }
1347 }
1348 
1349 
1351 static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
1352 {
1353  if (_sl_version < sld->version_from || _sl_version >= sld->version_to) return false;
1354  if (sld->conv & SLF_NOT_IN_SAVE) return false;
1355 
1356  return true;
1357 }
1358 
1364 static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
1365 {
1366  if ((sld->conv & SLF_NO_NETWORK_SYNC) && _sl.action != SLA_SAVE && _networking && !_network_server) {
1367  SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
1368  return true;
1369  }
1370 
1371  return false;
1372 }
1373 
1380 size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
1381 {
1382  size_t length = 0;
1383 
1384  /* Need to determine the length and write a length tag. */
1385  for (; sld->cmd != SL_END; sld++) {
1386  length += SlCalcObjMemberLength(object, sld);
1387  }
1388  return length;
1389 }
1390 
1391 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
1392 {
1393  assert(_sl.action == SLA_SAVE);
1394 
1395  switch (sld->cmd) {
1396  case SL_VAR:
1397  case SL_REF:
1398  case SL_ARR:
1399  case SL_STR:
1400  case SL_LST:
1401  case SL_DEQUE:
1402  /* CONDITIONAL saveload types depend on the savegame version */
1403  if (!SlIsObjectValidInSavegame(sld)) break;
1404 
1405  switch (sld->cmd) {
1406  case SL_VAR: return SlCalcConvFileLen(sld->conv);
1407  case SL_REF: return SlCalcRefLen();
1408  case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
1409  case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
1410  case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
1411  case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld->conv);
1412  default: NOT_REACHED();
1413  }
1414  break;
1415  case SL_WRITEBYTE: return 1; // a byte is logically of size 1
1416  case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
1417  case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
1418  default: NOT_REACHED();
1419  }
1420  return 0;
1421 }
1422 
1423 #ifdef OTTD_ASSERT
1424 
1430 static bool IsVariableSizeRight(const SaveLoad *sld)
1431 {
1432  switch (sld->cmd) {
1433  case SL_VAR:
1434  switch (GetVarMemType(sld->conv)) {
1435  case SLE_VAR_BL:
1436  return sld->size == sizeof(bool);
1437  case SLE_VAR_I8:
1438  case SLE_VAR_U8:
1439  return sld->size == sizeof(int8);
1440  case SLE_VAR_I16:
1441  case SLE_VAR_U16:
1442  return sld->size == sizeof(int16);
1443  case SLE_VAR_I32:
1444  case SLE_VAR_U32:
1445  return sld->size == sizeof(int32);
1446  case SLE_VAR_I64:
1447  case SLE_VAR_U64:
1448  return sld->size == sizeof(int64);
1449  default:
1450  return sld->size == sizeof(void *);
1451  }
1452  case SL_REF:
1453  /* These should all be pointer sized. */
1454  return sld->size == sizeof(void *);
1455 
1456  case SL_STR:
1457  /* These should be pointer sized, or fixed array. */
1458  return sld->size == sizeof(void *) || sld->size == sld->length;
1459 
1460  default:
1461  return true;
1462  }
1463 }
1464 
1465 #endif /* OTTD_ASSERT */
1466 
1467 bool SlObjectMember(void *ptr, const SaveLoad *sld)
1468 {
1469 #ifdef OTTD_ASSERT
1470  assert(IsVariableSizeRight(sld));
1471 #endif
1472 
1473  VarType conv = GB(sld->conv, 0, 8);
1474  switch (sld->cmd) {
1475  case SL_VAR:
1476  case SL_REF:
1477  case SL_ARR:
1478  case SL_STR:
1479  case SL_LST:
1480  case SL_DEQUE:
1481  /* CONDITIONAL saveload types depend on the savegame version */
1482  if (!SlIsObjectValidInSavegame(sld)) return false;
1483  if (SlSkipVariableOnLoad(sld)) return false;
1484 
1485  switch (sld->cmd) {
1486  case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1487  case SL_REF: // Reference variable, translate
1488  switch (_sl.action) {
1489  case SLA_SAVE:
1490  SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1491  break;
1492  case SLA_LOAD_CHECK:
1493  case SLA_LOAD:
1494  *(size_t *)ptr = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1495  break;
1496  case SLA_PTRS:
1497  *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1498  break;
1499  case SLA_NULL:
1500  *(void **)ptr = nullptr;
1501  break;
1502  default: NOT_REACHED();
1503  }
1504  break;
1505  case SL_ARR: SlArray(ptr, sld->length, conv); break;
1506  case SL_STR: SlString(ptr, sld->length, sld->conv); break;
1507  case SL_LST: SlList(ptr, (SLRefType)conv); break;
1508  case SL_DEQUE: SlDeque(ptr, conv); break;
1509  default: NOT_REACHED();
1510  }
1511  break;
1512 
1513  /* SL_WRITEBYTE writes a value to the savegame to identify the type of an object.
1514  * When loading, the value is read explicitly with SlReadByte() to determine which
1515  * object description to use. */
1516  case SL_WRITEBYTE:
1517  switch (_sl.action) {
1518  case SLA_SAVE: SlWriteByte(*(uint8 *)ptr); break;
1519  case SLA_LOAD_CHECK:
1520  case SLA_LOAD:
1521  case SLA_PTRS:
1522  case SLA_NULL: break;
1523  default: NOT_REACHED();
1524  }
1525  break;
1526 
1527  /* SL_VEH_INCLUDE loads common code for vehicles */
1528  case SL_VEH_INCLUDE:
1529  SlObject(ptr, GetVehicleDescription(VEH_END));
1530  break;
1531 
1532  case SL_ST_INCLUDE:
1534  break;
1535 
1536  default: NOT_REACHED();
1537  }
1538  return true;
1539 }
1540 
1546 void SlObject(void *object, const SaveLoad *sld)
1547 {
1548  /* Automatically calculate the length? */
1549  if (_sl.need_length != NL_NONE) {
1550  SlSetLength(SlCalcObjLength(object, sld));
1551  if (_sl.need_length == NL_CALCLENGTH) return;
1552  }
1553 
1554  for (; sld->cmd != SL_END; sld++) {
1555  void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld);
1556  SlObjectMember(ptr, sld);
1557  }
1558 }
1559 
1565 {
1566  SlObject(nullptr, (const SaveLoad*)sldg);
1567 }
1568 
1574 void SlAutolength(AutolengthProc *proc, void *arg)
1575 {
1576  size_t offs;
1577 
1578  assert(_sl.action == SLA_SAVE);
1579 
1580  /* Tell it to calculate the length */
1581  _sl.need_length = NL_CALCLENGTH;
1582  _sl.obj_len = 0;
1583  proc(arg);
1584 
1585  /* Setup length */
1586  _sl.need_length = NL_WANTLENGTH;
1587  SlSetLength(_sl.obj_len);
1588 
1589  offs = _sl.dumper->GetSize() + _sl.obj_len;
1590 
1591  /* And write the stuff */
1592  proc(arg);
1593 
1594  if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
1595 }
1596 
1601 static void SlLoadChunk(const ChunkHandler *ch)
1602 {
1603  byte m = SlReadByte();
1604  size_t len;
1605  size_t endoffs;
1606 
1607  _sl.block_mode = m;
1608  _sl.obj_len = 0;
1609 
1610  switch (m) {
1611  case CH_ARRAY:
1612  _sl.array_index = 0;
1613  ch->load_proc();
1614  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1615  break;
1616  case CH_SPARSE_ARRAY:
1617  ch->load_proc();
1618  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1619  break;
1620  default:
1621  if ((m & 0xF) == CH_RIFF) {
1622  /* Read length */
1623  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1624  len += SlReadUint16();
1625  _sl.obj_len = len;
1626  endoffs = _sl.reader->GetSize() + len;
1627  ch->load_proc();
1628  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1629  } else {
1630  SlErrorCorrupt("Invalid chunk type");
1631  }
1632  break;
1633  }
1634 }
1635 
1641 static void SlLoadCheckChunk(const ChunkHandler *ch)
1642 {
1643  byte m = SlReadByte();
1644  size_t len;
1645  size_t endoffs;
1646 
1647  _sl.block_mode = m;
1648  _sl.obj_len = 0;
1649 
1650  switch (m) {
1651  case CH_ARRAY:
1652  _sl.array_index = 0;
1653  if (ch->load_check_proc) {
1654  ch->load_check_proc();
1655  } else {
1656  SlSkipArray();
1657  }
1658  break;
1659  case CH_SPARSE_ARRAY:
1660  if (ch->load_check_proc) {
1661  ch->load_check_proc();
1662  } else {
1663  SlSkipArray();
1664  }
1665  break;
1666  default:
1667  if ((m & 0xF) == CH_RIFF) {
1668  /* Read length */
1669  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1670  len += SlReadUint16();
1671  _sl.obj_len = len;
1672  endoffs = _sl.reader->GetSize() + len;
1673  if (ch->load_check_proc) {
1674  ch->load_check_proc();
1675  } else {
1676  SlSkipBytes(len);
1677  }
1678  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1679  } else {
1680  SlErrorCorrupt("Invalid chunk type");
1681  }
1682  break;
1683  }
1684 }
1685 
1690 static ChunkSaveLoadProc *_stub_save_proc;
1691 
1697 static inline void SlStubSaveProc2(void *arg)
1698 {
1699  _stub_save_proc();
1700 }
1701 
1707 static void SlStubSaveProc()
1708 {
1709  SlAutolength(SlStubSaveProc2, nullptr);
1710 }
1711 
1717 static void SlSaveChunk(const ChunkHandler *ch)
1718 {
1719  ChunkSaveLoadProc *proc = ch->save_proc;
1720 
1721  /* Don't save any chunk information if there is no save handler. */
1722  if (proc == nullptr) return;
1723 
1724  SlWriteUint32(ch->id);
1725  DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1726 
1727  if (ch->flags & CH_AUTO_LENGTH) {
1728  /* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */
1729  _stub_save_proc = proc;
1730  proc = SlStubSaveProc;
1731  }
1732 
1733  _sl.block_mode = ch->flags & CH_TYPE_MASK;
1734  switch (ch->flags & CH_TYPE_MASK) {
1735  case CH_RIFF:
1736  _sl.need_length = NL_WANTLENGTH;
1737  proc();
1738  break;
1739  case CH_ARRAY:
1740  _sl.last_array_index = 0;
1741  SlWriteByte(CH_ARRAY);
1742  proc();
1743  SlWriteArrayLength(0); // Terminate arrays
1744  break;
1745  case CH_SPARSE_ARRAY:
1746  SlWriteByte(CH_SPARSE_ARRAY);
1747  proc();
1748  SlWriteArrayLength(0); // Terminate arrays
1749  break;
1750  default: NOT_REACHED();
1751  }
1752 }
1753 
1755 static void SlSaveChunks()
1756 {
1758  SlSaveChunk(ch);
1759  }
1760 
1761  /* Terminator */
1762  SlWriteUint32(0);
1763 }
1764 
1771 static const ChunkHandler *SlFindChunkHandler(uint32 id)
1772 {
1773  FOR_ALL_CHUNK_HANDLERS(ch) if (ch->id == id) return ch;
1774  return nullptr;
1775 }
1776 
1778 static void SlLoadChunks()
1779 {
1780  uint32 id;
1781  const ChunkHandler *ch;
1782 
1783  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1784  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1785 
1786  ch = SlFindChunkHandler(id);
1787  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
1788  SlLoadChunk(ch);
1789  }
1790 }
1791 
1793 static void SlLoadCheckChunks()
1794 {
1795  uint32 id;
1796  const ChunkHandler *ch;
1797 
1798  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1799  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1800 
1801  ch = SlFindChunkHandler(id);
1802  if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
1803  SlLoadCheckChunk(ch);
1804  }
1805 }
1806 
1808 static void SlFixPointers()
1809 {
1810  _sl.action = SLA_PTRS;
1811 
1812  DEBUG(sl, 1, "Fixing pointers");
1813 
1815  if (ch->ptrs_proc != nullptr) {
1816  DEBUG(sl, 2, "Fixing pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1817  ch->ptrs_proc();
1818  }
1819  }
1820 
1821  DEBUG(sl, 1, "All pointers fixed");
1822 
1823  assert(_sl.action == SLA_PTRS);
1824 }
1825 
1826 
1829  FILE *file;
1830  long begin;
1831 
1836  FileReader(FILE *file) : LoadFilter(nullptr), file(file), begin(ftell(file))
1837  {
1838  }
1839 
1842  {
1843  if (this->file != nullptr) fclose(this->file);
1844  this->file = nullptr;
1845 
1846  /* Make sure we don't double free. */
1847  _sl.sf = nullptr;
1848  }
1849 
1850  size_t Read(byte *buf, size_t size) override
1851  {
1852  /* We're in the process of shutting down, i.e. in "failure" mode. */
1853  if (this->file == nullptr) return 0;
1854 
1855  return fread(buf, 1, size, this->file);
1856  }
1857 
1858  void Reset() override
1859  {
1860  clearerr(this->file);
1861  if (fseek(this->file, this->begin, SEEK_SET)) {
1862  DEBUG(sl, 1, "Could not reset the file reading");
1863  }
1864  }
1865 };
1866 
1869  FILE *file;
1870 
1875  FileWriter(FILE *file) : SaveFilter(nullptr), file(file)
1876  {
1877  }
1878 
1881  {
1882  this->Finish();
1883 
1884  /* Make sure we don't double free. */
1885  _sl.sf = nullptr;
1886  }
1887 
1888  void Write(byte *buf, size_t size) override
1889  {
1890  /* We're in the process of shutting down, i.e. in "failure" mode. */
1891  if (this->file == nullptr) return;
1892 
1893  if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
1894  }
1895 
1896  void Finish() override
1897  {
1898  if (this->file != nullptr) fclose(this->file);
1899  this->file = nullptr;
1900  }
1901 };
1902 
1903 /*******************************************
1904  ********** START OF LZO CODE **************
1905  *******************************************/
1906 
1907 #ifdef WITH_LZO
1908 #include <lzo/lzo1x.h>
1909 
1911 static const uint LZO_BUFFER_SIZE = 8192;
1912 
1920  {
1921  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
1922  }
1923 
1924  size_t Read(byte *buf, size_t ssize) override
1925  {
1926  assert(ssize >= LZO_BUFFER_SIZE);
1927 
1928  /* Buffer size is from the LZO docs plus the chunk header size. */
1929  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1930  uint32 tmp[2];
1931  uint32 size;
1932  lzo_uint len = ssize;
1933 
1934  /* Read header*/
1935  if (this->chain->Read((byte*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
1936 
1937  /* Check if size is bad */
1938  ((uint32*)out)[0] = size = tmp[1];
1939 
1940  if (_sl_version != SL_MIN_VERSION) {
1941  tmp[0] = TO_BE32(tmp[0]);
1942  size = TO_BE32(size);
1943  }
1944 
1945  if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
1946 
1947  /* Read block */
1948  if (this->chain->Read(out + sizeof(uint32), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
1949 
1950  /* Verify checksum */
1951  if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
1952 
1953  /* Decompress */
1954  int ret = lzo1x_decompress_safe(out + sizeof(uint32) * 1, size, buf, &len, nullptr);
1955  if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
1956  return len;
1957  }
1958 };
1959 
1967  LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
1968  {
1969  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
1970  }
1971 
1972  void Write(byte *buf, size_t size) override
1973  {
1974  const lzo_bytep in = buf;
1975  /* Buffer size is from the LZO docs plus the chunk header size. */
1976  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1977  byte wrkmem[LZO1X_1_MEM_COMPRESS];
1978  lzo_uint outlen;
1979 
1980  do {
1981  /* Compress up to LZO_BUFFER_SIZE bytes at once. */
1982  lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
1983  lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
1984  ((uint32*)out)[1] = TO_BE32((uint32)outlen);
1985  ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
1986  this->chain->Write(out, outlen + sizeof(uint32) * 2);
1987 
1988  /* Move to next data chunk. */
1989  size -= len;
1990  in += len;
1991  } while (size > 0);
1992  }
1993 };
1994 
1995 #endif /* WITH_LZO */
1996 
1997 /*********************************************
1998  ******** START OF NOCOMP CODE (uncompressed)*
1999  *********************************************/
2000 
2008  {
2009  }
2010 
2011  size_t Read(byte *buf, size_t size) override
2012  {
2013  return this->chain->Read(buf, size);
2014  }
2015 };
2016 
2024  NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2025  {
2026  }
2027 
2028  void Write(byte *buf, size_t size) override
2029  {
2030  this->chain->Write(buf, size);
2031  }
2032 };
2033 
2034 /********************************************
2035  ********** START OF ZLIB CODE **************
2036  ********************************************/
2037 
2038 #if defined(WITH_ZLIB)
2039 #include <zlib.h>
2040 
2043  z_stream z;
2044  byte fread_buf[MEMORY_CHUNK_SIZE];
2045 
2051  {
2052  memset(&this->z, 0, sizeof(this->z));
2053  if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2054  }
2055 
2058  {
2059  inflateEnd(&this->z);
2060  }
2061 
2062  size_t Read(byte *buf, size_t size) override
2063  {
2064  this->z.next_out = buf;
2065  this->z.avail_out = (uint)size;
2066 
2067  do {
2068  /* read more bytes from the file? */
2069  if (this->z.avail_in == 0) {
2070  this->z.next_in = this->fread_buf;
2071  this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2072  }
2073 
2074  /* inflate the data */
2075  int r = inflate(&this->z, 0);
2076  if (r == Z_STREAM_END) break;
2077 
2078  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2079  } while (this->z.avail_out != 0);
2080 
2081  return size - this->z.avail_out;
2082  }
2083 };
2084 
2087  z_stream z;
2088 
2094  ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2095  {
2096  memset(&this->z, 0, sizeof(this->z));
2097  if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2098  }
2099 
2102  {
2103  deflateEnd(&this->z);
2104  }
2105 
2112  void WriteLoop(byte *p, size_t len, int mode)
2113  {
2114  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2115  uint n;
2116  this->z.next_in = p;
2117  this->z.avail_in = (uInt)len;
2118  do {
2119  this->z.next_out = buf;
2120  this->z.avail_out = sizeof(buf);
2121 
2129  int r = deflate(&this->z, mode);
2130 
2131  /* bytes were emitted? */
2132  if ((n = sizeof(buf) - this->z.avail_out) != 0) {
2133  this->chain->Write(buf, n);
2134  }
2135  if (r == Z_STREAM_END) break;
2136 
2137  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2138  } while (this->z.avail_in || !this->z.avail_out);
2139  }
2140 
2141  void Write(byte *buf, size_t size) override
2142  {
2143  this->WriteLoop(buf, size, 0);
2144  }
2145 
2146  void Finish() override
2147  {
2148  this->WriteLoop(nullptr, 0, Z_FINISH);
2149  this->chain->Finish();
2150  }
2151 };
2152 
2153 #endif /* WITH_ZLIB */
2154 
2155 /********************************************
2156  ********** START OF LZMA CODE **************
2157  ********************************************/
2158 
2159 #if defined(WITH_LIBLZMA)
2160 #include <lzma.h>
2161 
2168 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2169 
2172  lzma_stream lzma;
2173  byte fread_buf[MEMORY_CHUNK_SIZE];
2174 
2179  LZMALoadFilter(LoadFilter *chain) : LoadFilter(chain), lzma(_lzma_init)
2180  {
2181  /* Allow saves up to 256 MB uncompressed */
2182  if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2183  }
2184 
2187  {
2188  lzma_end(&this->lzma);
2189  }
2190 
2191  size_t Read(byte *buf, size_t size) override
2192  {
2193  this->lzma.next_out = buf;
2194  this->lzma.avail_out = size;
2195 
2196  do {
2197  /* read more bytes from the file? */
2198  if (this->lzma.avail_in == 0) {
2199  this->lzma.next_in = this->fread_buf;
2200  this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2201  }
2202 
2203  /* inflate the data */
2204  lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2205  if (r == LZMA_STREAM_END) break;
2206  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2207  } while (this->lzma.avail_out != 0);
2208 
2209  return size - this->lzma.avail_out;
2210  }
2211 };
2212 
2215  lzma_stream lzma;
2216 
2222  LZMASaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain), lzma(_lzma_init)
2223  {
2224  if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2225  }
2226 
2229  {
2230  lzma_end(&this->lzma);
2231  }
2232 
2239  void WriteLoop(byte *p, size_t len, lzma_action action)
2240  {
2241  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2242  size_t n;
2243  this->lzma.next_in = p;
2244  this->lzma.avail_in = len;
2245  do {
2246  this->lzma.next_out = buf;
2247  this->lzma.avail_out = sizeof(buf);
2248 
2249  lzma_ret r = lzma_code(&this->lzma, action);
2250 
2251  /* bytes were emitted? */
2252  if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
2253  this->chain->Write(buf, n);
2254  }
2255  if (r == LZMA_STREAM_END) break;
2256  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2257  } while (this->lzma.avail_in || !this->lzma.avail_out);
2258  }
2259 
2260  void Write(byte *buf, size_t size) override
2261  {
2262  this->WriteLoop(buf, size, LZMA_RUN);
2263  }
2264 
2265  void Finish() override
2266  {
2267  this->WriteLoop(nullptr, 0, LZMA_FINISH);
2268  this->chain->Finish();
2269  }
2270 };
2271 
2272 #endif /* WITH_LIBLZMA */
2273 
2274 /*******************************************
2275  ************* END OF CODE *****************
2276  *******************************************/
2277 
2280  const char *name;
2281  uint32 tag;
2282 
2283  LoadFilter *(*init_load)(LoadFilter *chain);
2284  SaveFilter *(*init_write)(SaveFilter *chain, byte compression);
2285 
2289 };
2290 
2293 #if defined(WITH_LZO)
2294  /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2295  {"lzo", TO_BE32X('OTTD'), CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2296 #else
2297  {"lzo", TO_BE32X('OTTD'), nullptr, nullptr, 0, 0, 0},
2298 #endif
2299  /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2300  {"none", TO_BE32X('OTTN'), CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2301 #if defined(WITH_ZLIB)
2302  /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2303  * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2304  * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2305  {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2306 #else
2307  {"zlib", TO_BE32X('OTTZ'), nullptr, nullptr, 0, 0, 0},
2308 #endif
2309 #if defined(WITH_LIBLZMA)
2310  /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2311  * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2312  * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2313  * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2314  * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2315  {"lzma", TO_BE32X('OTTX'), CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2316 #else
2317  {"lzma", TO_BE32X('OTTX'), nullptr, nullptr, 0, 0, 0},
2318 #endif
2319 };
2320 
2328 static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
2329 {
2330  const SaveLoadFormat *def = lastof(_saveload_formats);
2331 
2332  /* find default savegame format, the highest one with which files can be written */
2333  while (!def->init_write) def--;
2334 
2335  if (!StrEmpty(s)) {
2336  /* Get the ":..." of the compression level out of the way */
2337  char *complevel = strrchr(s, ':');
2338  if (complevel != nullptr) *complevel = '\0';
2339 
2340  for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
2341  if (slf->init_write != nullptr && strcmp(s, slf->name) == 0) {
2342  *compression_level = slf->default_compression;
2343  if (complevel != nullptr) {
2344  /* There is a compression level in the string.
2345  * First restore the : we removed to do proper name matching,
2346  * then move the the begin of the actual version. */
2347  *complevel = ':';
2348  complevel++;
2349 
2350  /* Get the version and determine whether all went fine. */
2351  char *end;
2352  long level = strtol(complevel, &end, 10);
2353  if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) {
2354  SetDParamStr(0, complevel);
2355  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2356  } else {
2357  *compression_level = level;
2358  }
2359  }
2360  return slf;
2361  }
2362  }
2363 
2364  SetDParamStr(0, s);
2365  SetDParamStr(1, def->name);
2366  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2367 
2368  /* Restore the string by adding the : back */
2369  if (complevel != nullptr) *complevel = ':';
2370  }
2371  *compression_level = def->default_compression;
2372  return def;
2373 }
2374 
2375 /* actual loader/saver function */
2376 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2377 extern bool AfterLoadGame();
2378 extern bool LoadOldSaveGame(const char *file);
2379 
2383 static inline void ClearSaveLoadState()
2384 {
2385  delete _sl.dumper;
2386  _sl.dumper = nullptr;
2387 
2388  delete _sl.sf;
2389  _sl.sf = nullptr;
2390 
2391  delete _sl.reader;
2392  _sl.reader = nullptr;
2393 
2394  delete _sl.lf;
2395  _sl.lf = nullptr;
2396 }
2397 
2403 static void SaveFileStart()
2404 {
2405  _sl.ff_state = _fast_forward;
2406  _fast_forward = 0;
2407  SetMouseCursorBusy(true);
2408 
2410  _sl.saveinprogress = true;
2411 }
2412 
2414 static void SaveFileDone()
2415 {
2416  if (_game_mode != GM_MENU) _fast_forward = _sl.ff_state;
2417  SetMouseCursorBusy(false);
2418 
2420  _sl.saveinprogress = false;
2421 }
2422 
2425 {
2426  _sl.error_str = str;
2427 }
2428 
2431 {
2432  SetDParam(0, _sl.error_str);
2433  SetDParamStr(1, _sl.extra_msg);
2434 
2435  static char err_str[512];
2436  GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
2437  return err_str;
2438 }
2439 
2441 static void SaveFileError()
2442 {
2444  ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
2445  SaveFileDone();
2446 }
2447 
2452 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2453 {
2454  try {
2455  byte compression;
2456  const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
2457 
2458  /* We have written our stuff to memory, now write it to file! */
2459  uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
2460  _sl.sf->Write((byte*)hdr, sizeof(hdr));
2461 
2462  _sl.sf = fmt->init_write(_sl.sf, compression);
2463  _sl.dumper->Flush(_sl.sf);
2464 
2466 
2467  if (threaded) SetAsyncSaveFinish(SaveFileDone);
2468 
2469  return SL_OK;
2470  } catch (...) {
2472 
2474 
2475  /* We don't want to shout when saving is just
2476  * cancelled due to a client disconnecting. */
2477  if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2478  /* Skip the "colour" character */
2479  DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2480  asfp = SaveFileError;
2481  }
2482 
2483  if (threaded) {
2484  SetAsyncSaveFinish(asfp);
2485  } else {
2486  asfp();
2487  }
2488  return SL_ERROR;
2489  }
2490 }
2491 
2492 void WaitTillSaved()
2493 {
2494  if (!_save_thread.joinable()) return;
2495 
2496  _save_thread.join();
2497 
2498  /* Make sure every other state is handled properly as well. */
2500 }
2501 
2510 static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
2511 {
2512  assert(!_sl.saveinprogress);
2513 
2514  _sl.dumper = new MemoryDumper();
2515  _sl.sf = writer;
2516 
2517  _sl_version = SAVEGAME_VERSION;
2518 
2519  SaveViewportBeforeSaveGame();
2520  SlSaveChunks();
2521 
2522  SaveFileStart();
2523 
2524  if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
2525  if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2526 
2527  SaveOrLoadResult result = SaveFileToDisk(false);
2528  SaveFileDone();
2529 
2530  return result;
2531  }
2532 
2533  return SL_OK;
2534 }
2535 
2543 {
2544  try {
2545  _sl.action = SLA_SAVE;
2546  return DoSave(writer, threaded);
2547  } catch (...) {
2549  return SL_ERROR;
2550  }
2551 }
2552 
2559 static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
2560 {
2561  _sl.lf = reader;
2562 
2563  if (load_check) {
2564  /* Clear previous check data */
2566  /* Mark SL_LOAD_CHECK as supported for this savegame. */
2567  _load_check_data.checkable = true;
2568  }
2569 
2570  uint32 hdr[2];
2571  if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2572 
2573  /* see if we have any loader for this type. */
2574  const SaveLoadFormat *fmt = _saveload_formats;
2575  for (;;) {
2576  /* No loader found, treat as version 0 and use LZO format */
2577  if (fmt == endof(_saveload_formats)) {
2578  DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2579  _sl.lf->Reset();
2580  _sl_version = SL_MIN_VERSION;
2581  _sl_minor_version = 0;
2582 
2583  /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2584  fmt = _saveload_formats;
2585  for (;;) {
2586  if (fmt == endof(_saveload_formats)) {
2587  /* Who removed LZO support? Bad bad boy! */
2588  NOT_REACHED();
2589  }
2590  if (fmt->tag == TO_BE32X('OTTD')) break;
2591  fmt++;
2592  }
2593  break;
2594  }
2595 
2596  if (fmt->tag == hdr[0]) {
2597  /* check version number */
2598  _sl_version = (SaveLoadVersion)(TO_BE32(hdr[1]) >> 16);
2599  /* Minor is not used anymore from version 18.0, but it is still needed
2600  * in versions before that (4 cases) which can't be removed easy.
2601  * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2602  _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
2603 
2604  DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
2605 
2606  /* Is the version higher than the current? */
2607  if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2608  break;
2609  }
2610 
2611  fmt++;
2612  }
2613 
2614  /* loader for this savegame type is not implemented? */
2615  if (fmt->init_load == nullptr) {
2616  char err_str[64];
2617  seprintf(err_str, lastof(err_str), "Loader for '%s' is not available.", fmt->name);
2618  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
2619  }
2620 
2621  _sl.lf = fmt->init_load(_sl.lf);
2622  _sl.reader = new ReadBuffer(_sl.lf);
2623  _next_offs = 0;
2624 
2625  if (!load_check) {
2626  /* Old maps were hardcoded to 256x256 and thus did not contain
2627  * any mapsize information. Pre-initialize to 256x256 to not to
2628  * confuse old games */
2629  InitializeGame(256, 256, true, true);
2630 
2631  GamelogReset();
2632 
2634  /*
2635  * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
2636  * shared savegame version 4. Anything before that 'obviously'
2637  * does not have any NewGRFs. Between the introduction and
2638  * savegame version 41 (just before 0.5) the NewGRF settings
2639  * were not stored in the savegame and they were loaded by
2640  * using the settings from the main menu.
2641  * So, to recap:
2642  * - savegame version < 4: do not load any NewGRFs.
2643  * - savegame version >= 41: load NewGRFs from savegame, which is
2644  * already done at this stage by
2645  * overwriting the main menu settings.
2646  * - other savegame versions: use main menu settings.
2647  *
2648  * This means that users *can* crash savegame version 4..40
2649  * savegames if they set incompatible NewGRFs in the main menu,
2650  * but can't crash anymore for savegame version < 4 savegames.
2651  *
2652  * Note: this is done here because AfterLoadGame is also called
2653  * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
2654  */
2656  }
2657  }
2658 
2659  if (load_check) {
2660  /* Load chunks into _load_check_data.
2661  * No pools are loaded. References are not possible, and thus do not need resolving. */
2663  } else {
2664  /* Load chunks and resolve references */
2665  SlLoadChunks();
2666  SlFixPointers();
2667  }
2668 
2670 
2671  _savegame_type = SGT_OTTD;
2672 
2673  if (load_check) {
2674  /* The only part from AfterLoadGame() we need */
2676  } else {
2678 
2679  /* After loading fix up savegame for any internal changes that
2680  * might have occurred since then. If it fails, load back the old game. */
2681  if (!AfterLoadGame()) {
2683  return SL_REINIT;
2684  }
2685 
2687  }
2688 
2689  return SL_OK;
2690 }
2691 
2698 {
2699  try {
2700  _sl.action = SLA_LOAD;
2701  return DoLoad(reader, false);
2702  } catch (...) {
2704  return SL_REINIT;
2705  }
2706 }
2707 
2717 SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
2718 {
2719  /* An instance of saving is already active, so don't go saving again */
2720  if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
2721  /* if not an autosave, but a user action, show error message */
2722  if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
2723  return SL_OK;
2724  }
2725  WaitTillSaved();
2726 
2727  try {
2728  /* Load a TTDLX or TTDPatch game */
2729  if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
2730  InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
2731 
2732  /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
2733  * and if so a new NewGRF list will be made in LoadOldSaveGame.
2734  * Note: this is done here because AfterLoadGame is also called
2735  * for OTTD savegames which have their own NewGRF logic. */
2737  GamelogReset();
2738  if (!LoadOldSaveGame(filename)) return SL_REINIT;
2739  _sl_version = SL_MIN_VERSION;
2740  _sl_minor_version = 0;
2742  if (!AfterLoadGame()) {
2744  return SL_REINIT;
2745  }
2747  return SL_OK;
2748  }
2749 
2750  assert(dft == DFT_GAME_FILE);
2751  switch (fop) {
2752  case SLO_CHECK:
2753  _sl.action = SLA_LOAD_CHECK;
2754  break;
2755 
2756  case SLO_LOAD:
2757  _sl.action = SLA_LOAD;
2758  break;
2759 
2760  case SLO_SAVE:
2761  _sl.action = SLA_SAVE;
2762  break;
2763 
2764  default: NOT_REACHED();
2765  }
2766 
2767  FILE *fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
2768 
2769  /* Make it a little easier to load savegames from the console */
2770  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
2771  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
2772  if (fh == nullptr && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
2773 
2774  if (fh == nullptr) {
2775  SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2776  }
2777 
2778  if (fop == SLO_SAVE) { // SAVE game
2779  DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
2780  if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
2781 
2782  return DoSave(new FileWriter(fh), threaded);
2783  }
2784 
2785  /* LOAD game */
2786  assert(fop == SLO_LOAD || fop == SLO_CHECK);
2787  DEBUG(desync, 1, "load: %s", filename);
2788  return DoLoad(new FileReader(fh), fop == SLO_CHECK);
2789  } catch (...) {
2790  /* This code may be executed both for old and new save games. */
2792 
2793  /* Skip the "colour" character */
2794  if (fop != SLO_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2795 
2796  /* A saver/loader exception!! reinitialize all variables to prevent crash! */
2797  return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
2798  }
2799 }
2800 
2803 {
2805 }
2806 
2812 void GenerateDefaultSaveName(char *buf, const char *last)
2813 {
2814  /* Check if we have a name for this map, which is the name of the first
2815  * available company. When there's no company available we'll use
2816  * 'Spectator' as "company" name. */
2817  CompanyID cid = _local_company;
2818  if (!Company::IsValidID(cid)) {
2819  for (const Company *c : Company::Iterate()) {
2820  cid = c->index;
2821  break;
2822  }
2823  }
2824 
2825  SetDParam(0, cid);
2826 
2827  /* Insert current date */
2829  case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
2830  case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
2831  case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
2832  default: NOT_REACHED();
2833  }
2834  SetDParam(2, _date);
2835 
2836  /* Get the correct string (special string for when there's not company) */
2837  GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
2838  SanitizeFilename(buf);
2839 }
2840 
2846 {
2847  this->SetMode(SLO_LOAD, GetAbstractFileType(ft), GetDetailedFileType(ft));
2848 }
2849 
2857 {
2858  if (aft == FT_INVALID || aft == FT_NONE) {
2859  this->file_op = SLO_INVALID;
2860  this->detail_ftype = DFT_INVALID;
2861  this->abstract_ftype = FT_INVALID;
2862  return;
2863  }
2864 
2865  this->file_op = fop;
2866  this->detail_ftype = dft;
2867  this->abstract_ftype = aft;
2868 }
2869 
2874 void FileToSaveLoad::SetName(const char *name)
2875 {
2876  strecpy(this->name, name, lastof(this->name));
2877 }
2878 
2883 void FileToSaveLoad::SetTitle(const char *title)
2884 {
2885  strecpy(this->title, title, lastof(this->title));
2886 }
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:67
~FileWriter()
Make sure everything is cleaned up.
Definition: saveload.cpp:1880
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:1972
Owner
Enum for all companies/owners.
Definition: company_type.h:18
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:16
const ChunkHandler _name_chunk_handlers[]
Chunk handlers related to strings.
static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
Actually perform the loading of a "non-old" savegame.
Definition: saveload.cpp:2559
static void SlFixPointers()
Fix all pointers (convert index -> pointer)
Definition: saveload.cpp:1808
static size_t SlCalcNetStringLen(const char *ptr, size_t length)
Calculate the net length of a string.
Definition: saveload.cpp:862
static void SlLoadCheckChunks()
Load all chunks for savegame checking.
Definition: saveload.cpp:1793
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:1896
static SaveOrLoadResult SaveFileToDisk(bool threaded)
We have written the whole game into memory, _memory_savegame, now find and appropriate compressor and...
Definition: saveload.cpp:2452
bool _networking
are we in networking mode?
Definition: network.cpp:52
static SaveLoadParams _sl
Parameters used for/at saveload.
Definition: saveload.cpp:211
ChunkSaveLoadProc * load_check_proc
Load procedure for game preview.
Definition: saveload.h:361
const SaveLoad * GetVehicleDescription(VehicleType vt)
Make it possible to make the saveload tables "friends" of other classes.
Definition: vehicle_sl.cpp:580
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Return the size in bytes of a std::deque.
Definition: saveload.cpp:1296
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:763
byte * bufe
End of the buffer we can read from.
Definition: saveload.cpp:88
Save/load a deque.
Definition: saveload.h:485
GRFConfig * _grfconfig
First item in list of current GRF set up.
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
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
LZMALoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2179
LoadFilter *(* init_load)(LoadFilter *chain)
Constructor for the load filter.
Definition: saveload.cpp:2283
Filter using Zlib compression.
Definition: saveload.cpp:2042
void GenerateDefaultSaveName(char *buf, const char *last)
Fill the buffer with the default name for a savegame or screenshot.
Definition: saveload.cpp:2812
NeedLength need_length
working in NeedLength (Autolength) mode?
Definition: saveload.cpp:191
z_stream z
Stream state we are reading from.
Definition: saveload.cpp:2043
void WriteByte(byte b)
Write a single byte into the dumper.
Definition: saveload.cpp:147
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1603
SaveLoadVersion
SaveLoad versions Previous savegame versions, the trunk revision where they were introduced and the r...
Definition: saveload.h:29
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:354
Yes, simply writing to a file.
Definition: saveload.cpp:1868
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
static bool SlSkipVariableOnLoad(const SaveLoad *sld)
Are we going to load this variable when loading a savegame or not?
Definition: saveload.cpp:1364
string (with pre-allocated buffer)
Definition: saveload.h:428
void SetName(const char *name)
Set the name of the file.
Definition: saveload.cpp:2874
uint32 flags
Flags of the chunk.
Definition: saveload.h:362
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
lzma_stream lzma
Stream state that we are reading from.
Definition: saveload.cpp:2172
lzma_stream lzma
Stream state that we are writing to.
Definition: saveload.cpp:2215
do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
Definition: saveload.h:470
static void SlList(void *list, SLRefType conv)
Save/Load a list.
Definition: saveload.cpp:1174
static size_t SlCalcArrayLen(size_t length, VarType conv)
Return the size in bytes of a certain type of atomic array.
Definition: saveload.cpp:984
void NORETURN SlError(StringID string, const char *extra_msg)
Error handler.
Definition: saveload.cpp:326
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition: saveload.cpp:58
ZlibLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2050
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:20
void GamelogStartAction(GamelogActionType at)
Stores information about new action, but doesn&#39;t allocate it Action is allocated only when there is a...
Definition: gamelog.cpp:69
static uint SlReadSimpleGamma()
Read in the header descriptor of an object or an array.
Definition: saveload.cpp:484
uint32 id
Unique ID (4 letters).
Definition: saveload.h:357
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:60
char * CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:59
LZMASaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2222
Filter using LZO compression.
Definition: saveload.cpp:1961
bool saveinprogress
Whether there is currently a save in progress.
Definition: saveload.cpp:208
Vehicle data structure.
Definition: vehicle_base.h:210
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
Load/save a reference to a link graph job.
Definition: saveload.h:382
Declaration of filters used for saving and loading savegames.
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:43
long begin
The begin of the file.
Definition: saveload.cpp:1830
int64 ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:755
byte buf[MEMORY_CHUNK_SIZE]
Buffer we&#39;re going to read from.
Definition: saveload.cpp:86
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
Save the game using a (writer) filter.
Definition: saveload.cpp:2542
Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
Definition: saveload.h:375
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:380
std::vector< byte * > blocks
Buffer with blocks of allocated memory.
Definition: saveload.cpp:127
static const SaveLoadFormat _saveload_formats[]
The different saveload formats known/understood by OpenTTD.
Definition: saveload.cpp:2292
partial loading into _load_check_data
Definition: saveload.cpp:72
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:25
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2141
void * address
address of variable OR offset of variable in the struct (max offset is 65536)
Definition: saveload.h:507
static void SaveFileDone()
Update the gui accordingly when saving is done and release locks on saveload.
Definition: saveload.cpp:2414
DetailedFileType GetDetailedFileType(FiosType fios_type)
Extract the detailed file type from a FiosType.
Definition: fileio_type.h:100
const ChunkHandler _town_chunk_handlers[]
Chunk handler for towns.
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
Definition: saveload.cpp:2802
SaveLoadVersion _sl_version
the major savegame version identifier
Definition: saveload.cpp:61
Load/save a reference to a town.
Definition: saveload.h:374
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
Called by save thread to tell we finished saving.
Definition: saveload.cpp:387
const ChunkHandler _sign_chunk_handlers[]
Chunk handlers related to signs.
LoadFilter * reader
The filter used to actually read.
Definition: saveload.cpp:89
Filter without any compression.
Definition: saveload.cpp:2171
virtual void Write(byte *buf, size_t len)=0
Write a given number of bytes into the savegame.
SavegameType
Types of save games.
Definition: saveload.h:330
void NORETURN SlErrorCorruptFmt(const char *format,...)
Issue an SlErrorCorrupt with a format string.
Definition: saveload.cpp:366
byte ff_state
The state of fast-forward when saving started.
Definition: saveload.cpp:207
static bool SlIsObjectValidInSavegame(const SaveLoad *sld)
Are we going to save this object or not?
Definition: saveload.cpp:1351
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2011
Deals with the type of the savegame, independent of extension.
Definition: saveload.h:316
size_t size
the sizeof size.
Definition: saveload.h:508
~ZlibLoadFilter()
Clean everything up.
Definition: saveload.cpp:2057
byte default_compression
the default compression level of this format
Definition: saveload.cpp:2287
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:1850
const char * GetSaveLoadErrorString()
Get the string representation of the error message.
Definition: saveload.cpp:2430
File is being saved.
Definition: fileio_type.h:50
FILE * file
The file to write to.
Definition: saveload.cpp:1869
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:743
Load file for checking and/or preview.
Definition: fileio_type.h:48
loading
Definition: saveload.cpp:68
Save/load a reference.
Definition: saveload.h:481
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2260
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
not working in NeedLength mode
Definition: saveload.cpp:76
A connected component of a link graph.
Definition: linkgraph.h:38
static void SlSaveChunk(const ChunkHandler *ch)
Save a chunk of data (eg.
Definition: saveload.cpp:1717
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:995
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition: saveload.cpp:398
z_stream z
Stream state we are writing to.
Definition: saveload.cpp:2087
Save game or scenario file.
Definition: fileio_type.h:31
Interface for filtering a savegame till it is loaded.
bool checkable
True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.)
Definition: fios.h:32
uint16 length
(conditional) length of the variable (eg. arrays) (max array size is 65536 elements) ...
Definition: saveload.h:500
Load/save a reference to a bus/truck stop.
Definition: saveload.h:376
virtual void Finish()
Prepare everything to finish writing the savegame.
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:24
fixing pointers
Definition: saveload.cpp:70
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:48
Save/load a variable.
Definition: saveload.h:480
Filter using Zlib compression.
Definition: saveload.cpp:2086
static size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
Calculate the gross length of the string that it will occupy in the savegame.
Definition: saveload.cpp:877
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition: order_base.h:250
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:279
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
void WriteLoop(byte *p, size_t len, lzma_action action)
Helper loop for writing the data.
Definition: saveload.cpp:2239
Base directory for all scenarios.
Definition: fileio_type.h:112
bool global
should we load a global variable or a non-global one
Definition: saveload.h:497
char _savegame_format[8]
how to compress savegames
Definition: saveload.cpp:63
void GamelogReset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition: gamelog.cpp:110
void SetTitle(const char *title)
Set the title of the file.
Definition: saveload.cpp:2883
Load/save a reference to an engine renewal (autoreplace).
Definition: saveload.h:377
ReadBuffer * reader
Savegame reading buffer.
Definition: saveload.cpp:201
VarType conv
type of the variable to be saved, int
Definition: saveload.h:499
static void SlCopyBytes(void *ptr, size_t length)
Save/Load bytes.
Definition: saveload.cpp:726
writing length and data
Definition: saveload.cpp:77
nothing to do
Definition: fileio_type.h:17
SLRefType
Type of reference (SLE_REF, SLE_CONDREF).
Definition: saveload.h:370
FILE * file
The file to read from.
Definition: saveload.cpp:1829
const ChunkHandler _persistent_storage_chunk_handlers[]
Chunk handler for persistent storages.
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:27
allow new lines in the strings
Definition: saveload.h:472
Highest possible saveload version.
Definition: saveload.h:305
SaveOrLoadResult
Save or load result codes.
Definition: saveload.h:309
Filter using LZO compression.
Definition: saveload.cpp:1914
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:194
do not save with savegame, basically client-based
Definition: saveload.h:468
static void SlDeque(void *deque, VarType conv)
Save/load a std::deque.
Definition: saveload.cpp:1323
Filter without any compression.
Definition: saveload.cpp:2002
Old save game or scenario file.
Definition: fileio_type.h:30
~ZlibSaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2101
allow control codes in the strings
Definition: saveload.h:471
static void SlSaveChunks()
Save all chunks.
Definition: saveload.cpp:1755
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition: saveload.h:42
First savegame version.
Definition: saveload.h:30
byte _sl_minor_version
the minor savegame version, DO NOT USE!
Definition: saveload.cpp:62
byte max_compression
the maximum compression level of this format
Definition: saveload.cpp:2288
StringID offset into strings-array.
Definition: saveload.h:413
need to calculate the length
Definition: saveload.cpp:78
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
static bool IsVariableSizeRight(const SaveLoad *sld)
Check whether the variable size of the variable in the saveload configuration matches with the actual...
Definition: saveload.cpp:1430
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:463
byte * bufe
End of the buffer we write to.
Definition: saveload.cpp:129
static std::atomic< AsyncSaveFinishProc > _async_save_finish
Callback to call when the savegame loading is finished.
Definition: saveload.cpp:380
Container for cargo from the same location and time.
Definition: cargopacket.h:42
static void SlDeque(void *deque, VarType conv)
Internal templated helper to save/load a std::deque.
Definition: saveload.cpp:1254
uint8 date_format_in_default_names
should the default savegame/screenshot name use long dates (31th Dec 2008), short dates (31-12-2008) ...
void Clear()
Reset read data.
Definition: fios_gui.cpp:47
Allow newlines.
Definition: string_type.h:51
Save/load a list.
Definition: saveload.h:484
size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
Calculate the size of an object.
Definition: saveload.cpp:1380
Game loaded.
Definition: gamelog.h:18
Filter without any compression.
Definition: saveload.cpp:2018
virtual void Reset()
Reset this filter to read from the beginning of the file.
const SaveLoad * GetBaseStationDescription()
Get the base station description to be used for SL_ST_INCLUDE.
Definition: station_sl.cpp:465
Load/save a reference to a station.
Definition: saveload.h:373
const ChunkHandler _animated_tile_chunk_handlers[]
"Definition" imported by the saveload code to be able to load and save the animated tile table...
size_t obj_len
the length of the current object we are busy with
Definition: saveload.cpp:195
Base directory for all savegames.
Definition: fileio_type.h:110
Subdirectory of save for autosaves.
Definition: fileio_type.h:111
ReadBuffer(LoadFilter *reader)
Initialise our variables.
Definition: saveload.cpp:96
void SanitizeFilename(char *filename)
Sanitizes a filename, i.e.
Definition: fileio.cpp:1242
Base directory for all subdirectories.
Definition: fileio_type.h:109
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
Class for pooled persistent storage of data.
The format for a reader/writer type of a savegame.
Definition: saveload.cpp:2279
static void SlLoadCheckChunk(const ChunkHandler *ch)
Load a chunk of data for checking savegames.
Definition: saveload.cpp:1641
char * error_data
Data to pass to SetDParamStr when displaying error.
Definition: fios.h:34
Load/save a reference to an order.
Definition: saveload.h:371
static std::thread _save_thread
The thread we&#39;re using to compress and write a savegame.
Definition: saveload.cpp:381
static void SlWriteSimpleGamma(size_t i)
Write the header descriptor of an object or an array.
Definition: saveload.cpp:526
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:1888
const SaveLoadVersion SAVEGAME_VERSION
Current savegame version of OpenTTD.
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2265
ZlibSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2094
SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
Load the game using a (reader) filter.
Definition: saveload.cpp:2697
void SetSaveLoadError(StringID str)
Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friend...
Definition: saveload.cpp:2424
static VarType GetVarFileType(VarType type)
Get the FileType of a setting.
Definition: saveload.h:802
AbstractFileType GetAbstractFileType(FiosType fios_type)
Extract the abstract file type from a FiosType.
Definition: fileio_type.h:90
MemoryDumper * dumper
Memory dumper to write the savegame to.
Definition: saveload.cpp:198
SaveOrLoadResult SaveOrLoad(const char *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:2717
OTTD savegame.
Definition: saveload.h:334
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:182
static void SlLoadChunks()
Load all chunks.
Definition: saveload.cpp:1778
void Write(byte *buf, size_t size) override
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2028
A buffer for reading (and buffering) savegame data.
Definition: saveload.cpp:85
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
byte * buf
Buffer we&#39;re going to write to.
Definition: saveload.cpp:128
virtual size_t Read(byte *buf, size_t len)=0
Read a given number of bytes from the savegame.
File is being loaded.
Definition: fileio_type.h:49
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
static const uint LZO_BUFFER_SIZE
Buffer size for the LZO compressor.
Definition: saveload.cpp:1911
static uint SlGetGammaLength(size_t i)
Return how many bytes used to encode a gamma value.
Definition: saveload.cpp:551
byte SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:414
StringID error
Error message from loading. INVALID_STRING_ID if no error.
Definition: fios.h:33
static VarType GetVarMemType(VarType type)
Get the NumberType of a setting.
Definition: saveload.h:791
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
static void SaveFileError()
Show a gui message when saving has failed.
Definition: saveload.cpp:2441
ChunkSaveLoadProc * save_proc
Save procedure of the chunk.
Definition: saveload.h:358
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:47
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:637
ChunkSaveLoadProc * load_proc
Load procedure of the chunk.
Definition: saveload.h:359
Load/save a reference to a vehicle.
Definition: saveload.h:372
static const ChunkHandler * SlFindChunkHandler(uint32 id)
Find the ChunkHandler that will be used for processing the found chunk in the savegame or in memory...
Definition: saveload.cpp:1771
void Reset() override
Reset this filter to read from the beginning of the file.
Definition: saveload.cpp:1858
Handlers and description of chunk.
Definition: saveload.h:356
static void SlSkipBytes(size_t length)
Read in bytes from the file/data structure but don&#39;t do anything with them, discarding them in effect...
Definition: saveload.cpp:470
void SlSkipArray()
Skip an array or sparse array.
Definition: saveload.cpp:670
The saveload struct, containing reader-writer functions, buffer, version, etc.
Definition: saveload.cpp:189
byte * bufp
Location we&#39;re at reading the buffer.
Definition: saveload.cpp:87
Save/load an array.
Definition: saveload.h:482
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Internal templated helper to return the size in bytes of a std::deque.
Definition: saveload.cpp:1239
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:137
void GamelogStopAction()
Stops logging of any changes.
Definition: gamelog.cpp:78
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
Definition: strings_sl.cpp:28
string enclosed in quotes (with pre-allocated buffer)
Definition: saveload.h:429
static void ClearSaveLoadState()
Clear/free saveload state.
Definition: saveload.cpp:2383
Unknown or invalid file.
Definition: fileio_type.h:43
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2062
static void * GetVariableAddress(const void *object, const SaveLoad *sld)
Get the address of the variable.
Definition: saveload.h:823
MemoryDumper()
Initialise our variables.
Definition: saveload.cpp:132
static const lzma_stream _lzma_init
Have a copy of an initialised LZMA stream.
Definition: saveload.cpp:2168
bool AfterLoadGame()
Perform a (large) amount of savegame conversion magic in order to load older savegames and to fill th...
Definition: afterload.cpp:534
static void SlStubSaveProc2(void *arg)
Stub Chunk handlers to only calculate length and do nothing else.
Definition: saveload.cpp:1697
SaveLoadAction action
are we doing a save or a load atm.
Definition: saveload.cpp:190
static const SaveLoadFormat * GetSavegameFormat(char *s, byte *compression_level)
Return the savegameformat of the game.
Definition: saveload.cpp:2328
Load/save a reference to a cargo packet.
Definition: saveload.h:378
bool error
did an error occur or not
Definition: saveload.cpp:193
GUISettings gui
settings related to the GUI
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
const ChunkHandler _cargopacket_chunk_handlers[]
Chunk handlers related to cargo packets.
static const size_t MEMORY_CHUNK_SIZE
Save in chunks of 128 KiB.
Definition: saveload.cpp:82
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:57
SaveLoadVersion version_to
save/load the variable until this savegame version
Definition: saveload.h:502
169 23816
Definition: saveload.h:244
const ChunkHandler _cargomonitor_chunk_handlers[]
Chunk definition of the cargomonitoring maps.
static void SlNullPointers()
Null all pointers (convert index -> nullptr)
Definition: saveload.cpp:295
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
~LZMALoadFilter()
Clean everything up.
Definition: saveload.cpp:2186
useful to write zeros in savegame.
Definition: saveload.h:427
string pointer enclosed in quotes
Definition: saveload.h:431
Invalid or unknown file type.
Definition: fileio_type.h:22
~FileReader()
Make sure everything is cleaned up.
Definition: saveload.cpp:1841
Struct to store engine replacements.
static void SaveFileStart()
Update the gui accordingly when starting saving and set locks on saveload.
Definition: saveload.cpp:2403
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
SaveLoadType cmd
the action to take with the saved/loaded type, All types need different action
Definition: saveload.h:498
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1546
uint32 tag
the 4-letter tag by which it is identified in the savegame
Definition: saveload.cpp:2281
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:384
static byte SlCalcConvFileLen(VarType conv)
Return the size in bytes of a certain type of normal/atomic variable as it appears in a saved game...
Definition: saveload.cpp:611
Town data structure.
Definition: town.h:53
byte block_mode
???
Definition: saveload.cpp:192
size_t Read(byte *buf, size_t size) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2191
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:57
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
FileReader(FILE *file)
Create the file reader, so it reads from a specific file.
Definition: saveload.cpp:1836
bool _network_server
network-server is active
Definition: network.cpp:53
A Stop for a Road Vehicle.
Definition: roadstop_base.h:22
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
StringID error_str
the translatable error message to show
Definition: saveload.cpp:204
SaveFilter *(* init_write)(SaveFilter *chain, byte compression)
Constructor for the save filter.
Definition: saveload.cpp:2284
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1564
LZOSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:1967
char * extra_msg
the error message
Definition: saveload.cpp:205
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1574
const ChunkHandler _cheat_chunk_handlers[]
Chunk handlers related to cheats.
void str_fix_scc_encoded(char *str, const char *last)
Scan the string for old values of SCC_ENCODED and fix it to it&#39;s new, static value.
Definition: string.cpp:168
Allow the special control codes.
Definition: string_type.h:52
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
byte min_compression
the minimum compression level of this format
Definition: saveload.cpp:2286
static size_t SlCalcListLen(const void *list)
Return the size in bytes of a list.
Definition: saveload.cpp:1158
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
void Flush(SaveFilter *writer)
Flush this dumper into a writer.
Definition: saveload.cpp:163
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:57
SaveLoad type struct.
Definition: saveload.h:496
69 10319
Definition: saveload.h:124
SaveLoadAction
What are we currently doing?
Definition: saveload.cpp:67
SaveFilter * sf
Filter to write the savegame to.
Definition: saveload.cpp:199
bool threaded_saves
should we do threaded saves?
Load/save a reference to an orderlist.
Definition: saveload.h:379
Filter using LZMA compression.
Definition: saveload.cpp:2214
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Template class to help with std::deque.
Definition: saveload.cpp:1231
completed successfully
Definition: saveload.h:310
Load/save a reference to a link graph.
Definition: saveload.h:381
string pointer
Definition: saveload.h:430
FileWriter(FILE *file)
Create the file writer, so it writes to a specific file.
Definition: saveload.cpp:1875
static void SlStubSaveProc()
Stub Chunk handlers to only calculate length and do nothing else.
Definition: saveload.cpp:1707
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition: saveload.cpp:379
void SlSetLength(size_t length)
Sets the length of either a RIFF object or the number of items in an array.
Definition: saveload.cpp:682
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:2845
static void SlLoadChunk(const ChunkHandler *ch)
Load a chunk of data (eg vehicles, stations, etc.)
Definition: saveload.cpp:1601
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
NeedLength
Definition: saveload.cpp:75
size_t GetSize() const
Get the size of the memory dump made so far.
Definition: saveload.cpp:118
finished saving
Definition: statusbar_gui.h:16
Interface for filtering a savegame till it is written.
saving
Definition: saveload.cpp:69
static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
Actually perform the saving of the savegame.
Definition: saveload.cpp:2510
NoCompSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2024
LoadFilter * lf
Filter to read the savegame from.
Definition: saveload.cpp:202
Errors (eg. saving/loading failed)
Definition: error.h:23
static void SlString(void *ptr, size_t length, VarType conv)
Save/Load a string.
Definition: saveload.cpp:906
error that was caught before internal structures were modified
Definition: saveload.h:311
static Station * Get(size_t index)
Gets station with given index.
Date _date
Current date in days (day counter)
Definition: date.cpp:26
void Finish() override
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2146
null all pointers (on loading error)
Definition: saveload.cpp:71
started saving
Definition: statusbar_gui.h:15
LZOLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:1919
~LZMASaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2228
size_t read
The amount of read bytes so far from the filter.
Definition: saveload.cpp:90
const char * name
name of the compressor/decompressor (debug-only)
Definition: saveload.cpp:2280
Declaration of functions used in more save/load files.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:44
DetailedFileType
Kinds of files in each AbstractFileType.
Definition: fileio_type.h:28
size_t Read(byte *buf, size_t ssize) override
Read a given number of bytes from the savegame.
Definition: saveload.cpp:1924
Container for dumping the savegame (quickly) to memory.
Definition: saveload.cpp:126
void WriteValue(void *ptr, VarType conv, int64 val)
Write the value of a setting.
Definition: saveload.cpp:779
void SlWriteByte(byte b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:423
GRFListCompatibility grf_compatibility
Summary state of NewGrfs, whether missing files or only compatible found.
Definition: fios.h:44
static const ChunkHandler *const _chunk_handlers[]
Array of all chunks in a savegame, nullptr terminated.
Definition: saveload.cpp:249
static ChunkSaveLoadProc * _stub_save_proc
Stub Chunk handlers to only calculate length and do nothing else.
Definition: saveload.cpp:1690
bool _do_autosave
are we doing an autosave at the moment?
Definition: saveload.cpp:64
Station data structure.
Definition: station_base.h:450
Unknown file operation.
Definition: fileio_type.h:52
NoCompLoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2007
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, and if not available, it hussles with pointers (looks really bad :() Remember that a nullptr item has value 0, and all indices have +1, so vehicle 0 is saved as index 1.
Definition: saveload.cpp:1050
int last_array_index
in the case of an array, the current and last positions
Definition: saveload.cpp:196
static void SlSaveLoadConv(void *ptr, VarType conv)
Handle all conversion and typechecking of variables here.
Definition: saveload.cpp:805
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:29
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:1083
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:3316
old custom name to be converted to a char pointer
Definition: saveload.h:432
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:36
uint32 _ttdp_version
version of TTDP savegame (if applicable)
Definition: saveload.cpp:60
static size_t SlCalcRefLen()
Return the size in bytes of a reference (pointer)
Definition: saveload.cpp:620
Save/load a string.
Definition: saveload.h:483
Load/save a reference to a persistent storage.
Definition: saveload.h:380
void WriteLoop(byte *p, size_t len, int mode)
Helper loop for writing the data.
Definition: saveload.cpp:2112
#define FOR_ALL_CHUNK_HANDLERS(ch)
Iterate over all chunk handlers.
Definition: saveload.cpp:290
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
Yes, simply reading from a file.
Definition: saveload.cpp:1828
error that was caught in the middle of updating game state, need to clear it. (can only happen during...
Definition: saveload.h:312