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