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 
353 
354 typedef void (*AsyncSaveFinishProc)();
357 
363 {
364  if (_exit_game) return;
365  while (_async_save_finish != NULL) CSleep(10);
366 
367  _async_save_finish = proc;
368 }
369 
374 {
375  if (_async_save_finish == NULL) return;
376 
378 
379  _async_save_finish = NULL;
380 
381  if (_save_thread != NULL) {
382  _save_thread->Join();
383  delete _save_thread;
384  _save_thread = NULL;
385  }
386 }
387 
393 {
394  return _sl.reader->ReadByte();
395 }
396 
401 void SlWriteByte(byte b)
402 {
403  _sl.dumper->WriteByte(b);
404 }
405 
406 static inline int SlReadUint16()
407 {
408  int x = SlReadByte() << 8;
409  return x | SlReadByte();
410 }
411 
412 static inline uint32 SlReadUint32()
413 {
414  uint32 x = SlReadUint16() << 16;
415  return x | SlReadUint16();
416 }
417 
418 static inline uint64 SlReadUint64()
419 {
420  uint32 x = SlReadUint32();
421  uint32 y = SlReadUint32();
422  return (uint64)x << 32 | y;
423 }
424 
425 static inline void SlWriteUint16(uint16 v)
426 {
427  SlWriteByte(GB(v, 8, 8));
428  SlWriteByte(GB(v, 0, 8));
429 }
430 
431 static inline void SlWriteUint32(uint32 v)
432 {
433  SlWriteUint16(GB(v, 16, 16));
434  SlWriteUint16(GB(v, 0, 16));
435 }
436 
437 static inline void SlWriteUint64(uint64 x)
438 {
439  SlWriteUint32((uint32)(x >> 32));
440  SlWriteUint32((uint32)x);
441 }
442 
448 static inline void SlSkipBytes(size_t length)
449 {
450  for (; length != 0; length--) SlReadByte();
451 }
452 
462 static uint SlReadSimpleGamma()
463 {
464  uint i = SlReadByte();
465  if (HasBit(i, 7)) {
466  i &= ~0x80;
467  if (HasBit(i, 6)) {
468  i &= ~0x40;
469  if (HasBit(i, 5)) {
470  i &= ~0x20;
471  if (HasBit(i, 4)) {
472  i &= ~0x10;
473  if (HasBit(i, 3)) {
474  SlErrorCorrupt("Unsupported gamma");
475  }
476  i = SlReadByte(); // 32 bits only.
477  }
478  i = (i << 8) | SlReadByte();
479  }
480  i = (i << 8) | SlReadByte();
481  }
482  i = (i << 8) | SlReadByte();
483  }
484  return i;
485 }
486 
504 static void SlWriteSimpleGamma(size_t i)
505 {
506  if (i >= (1 << 7)) {
507  if (i >= (1 << 14)) {
508  if (i >= (1 << 21)) {
509  if (i >= (1 << 28)) {
510  assert(i <= UINT32_MAX); // We can only support 32 bits for now.
511  SlWriteByte((byte)(0xF0));
512  SlWriteByte((byte)(i >> 24));
513  } else {
514  SlWriteByte((byte)(0xE0 | (i >> 24)));
515  }
516  SlWriteByte((byte)(i >> 16));
517  } else {
518  SlWriteByte((byte)(0xC0 | (i >> 16)));
519  }
520  SlWriteByte((byte)(i >> 8));
521  } else {
522  SlWriteByte((byte)(0x80 | (i >> 8)));
523  }
524  }
525  SlWriteByte((byte)i);
526 }
527 
529 static inline uint SlGetGammaLength(size_t i)
530 {
531  return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
532 }
533 
534 static inline uint SlReadSparseIndex()
535 {
536  return SlReadSimpleGamma();
537 }
538 
539 static inline void SlWriteSparseIndex(uint index)
540 {
541  SlWriteSimpleGamma(index);
542 }
543 
544 static inline uint SlReadArrayLength()
545 {
546  return SlReadSimpleGamma();
547 }
548 
549 static inline void SlWriteArrayLength(size_t length)
550 {
551  SlWriteSimpleGamma(length);
552 }
553 
554 static inline uint SlGetArrayLength(size_t length)
555 {
556  return SlGetGammaLength(length);
557 }
558 
565 static inline uint SlCalcConvMemLen(VarType conv)
566 {
567  static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0};
568  byte length = GB(conv, 4, 4);
569 
570  switch (length << 4) {
571  case SLE_VAR_STRB:
572  case SLE_VAR_STRBQ:
573  case SLE_VAR_STR:
574  case SLE_VAR_STRQ:
575  return SlReadArrayLength();
576 
577  default:
578  assert(length < lengthof(conv_mem_size));
579  return conv_mem_size[length];
580  }
581 }
582 
589 static inline byte SlCalcConvFileLen(VarType conv)
590 {
591  static const byte conv_file_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 2};
592  byte length = GB(conv, 0, 4);
593  assert(length < lengthof(conv_file_size));
594  return conv_file_size[length];
595 }
596 
598 static inline size_t SlCalcRefLen()
599 {
600  return IsSavegameVersionBefore(SLV_69) ? 2 : 4;
601 }
602 
603 void SlSetArrayIndex(uint index)
604 {
606  _sl.array_index = index;
607 }
608 
609 static size_t _next_offs;
610 
616 {
617  int index;
618 
619  /* After reading in the whole array inside the loop
620  * we must have read in all the data, so we must be at end of current block. */
621  if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) SlErrorCorrupt("Invalid chunk size");
622 
623  for (;;) {
624  uint length = SlReadArrayLength();
625  if (length == 0) {
626  _next_offs = 0;
627  return -1;
628  }
629 
630  _sl.obj_len = --length;
631  _next_offs = _sl.reader->GetSize() + length;
632 
633  switch (_sl.block_mode) {
634  case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
635  case CH_ARRAY: index = _sl.array_index++; break;
636  default:
637  DEBUG(sl, 0, "SlIterateArray error");
638  return -1; // error
639  }
640 
641  if (length != 0) return index;
642  }
643 }
644 
649 {
650  while (SlIterateArray() != -1) {
651  SlSkipBytes(_next_offs - _sl.reader->GetSize());
652  }
653 }
654 
660 void SlSetLength(size_t length)
661 {
662  assert(_sl.action == SLA_SAVE);
663 
664  switch (_sl.need_length) {
665  case NL_WANTLENGTH:
666  _sl.need_length = NL_NONE;
667  switch (_sl.block_mode) {
668  case CH_RIFF:
669  /* Ugly encoding of >16M RIFF chunks
670  * The lower 24 bits are normal
671  * The uppermost 4 bits are bits 24:27 */
672  assert(length < (1 << 28));
673  SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
674  break;
675  case CH_ARRAY:
676  assert(_sl.last_array_index <= _sl.array_index);
677  while (++_sl.last_array_index <= _sl.array_index) {
678  SlWriteArrayLength(1);
679  }
680  SlWriteArrayLength(length + 1);
681  break;
682  case CH_SPARSE_ARRAY:
683  SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
684  SlWriteSparseIndex(_sl.array_index);
685  break;
686  default: NOT_REACHED();
687  }
688  break;
689 
690  case NL_CALCLENGTH:
691  _sl.obj_len += (int)length;
692  break;
693 
694  default: NOT_REACHED();
695  }
696 }
697 
704 static void SlCopyBytes(void *ptr, size_t length)
705 {
706  byte *p = (byte *)ptr;
707 
708  switch (_sl.action) {
709  case SLA_LOAD_CHECK:
710  case SLA_LOAD:
711  for (; length != 0; length--) *p++ = SlReadByte();
712  break;
713  case SLA_SAVE:
714  for (; length != 0; length--) SlWriteByte(*p++);
715  break;
716  default: NOT_REACHED();
717  }
718 }
719 
722 {
723  return _sl.obj_len;
724 }
725 
733 int64 ReadValue(const void *ptr, VarType conv)
734 {
735  switch (GetVarMemType(conv)) {
736  case SLE_VAR_BL: return (*(const bool *)ptr != 0);
737  case SLE_VAR_I8: return *(const int8 *)ptr;
738  case SLE_VAR_U8: return *(const byte *)ptr;
739  case SLE_VAR_I16: return *(const int16 *)ptr;
740  case SLE_VAR_U16: return *(const uint16*)ptr;
741  case SLE_VAR_I32: return *(const int32 *)ptr;
742  case SLE_VAR_U32: return *(const uint32*)ptr;
743  case SLE_VAR_I64: return *(const int64 *)ptr;
744  case SLE_VAR_U64: return *(const uint64*)ptr;
745  case SLE_VAR_NULL:return 0;
746  default: NOT_REACHED();
747  }
748 }
749 
757 void WriteValue(void *ptr, VarType conv, int64 val)
758 {
759  switch (GetVarMemType(conv)) {
760  case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
761  case SLE_VAR_I8: *(int8 *)ptr = val; break;
762  case SLE_VAR_U8: *(byte *)ptr = val; break;
763  case SLE_VAR_I16: *(int16 *)ptr = val; break;
764  case SLE_VAR_U16: *(uint16*)ptr = val; break;
765  case SLE_VAR_I32: *(int32 *)ptr = val; break;
766  case SLE_VAR_U32: *(uint32*)ptr = val; break;
767  case SLE_VAR_I64: *(int64 *)ptr = val; break;
768  case SLE_VAR_U64: *(uint64*)ptr = val; break;
769  case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
770  case SLE_VAR_NULL: break;
771  default: NOT_REACHED();
772  }
773 }
774 
783 static void SlSaveLoadConv(void *ptr, VarType conv)
784 {
785  switch (_sl.action) {
786  case SLA_SAVE: {
787  int64 x = ReadValue(ptr, conv);
788 
789  /* Write the value to the file and check if its value is in the desired range */
790  switch (GetVarFileType(conv)) {
791  case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
792  case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
793  case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
794  case SLE_FILE_STRINGID:
795  case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
796  case SLE_FILE_I32:
797  case SLE_FILE_U32: SlWriteUint32((uint32)x);break;
798  case SLE_FILE_I64:
799  case SLE_FILE_U64: SlWriteUint64(x);break;
800  default: NOT_REACHED();
801  }
802  break;
803  }
804  case SLA_LOAD_CHECK:
805  case SLA_LOAD: {
806  int64 x;
807  /* Read a value from the file */
808  switch (GetVarFileType(conv)) {
809  case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
810  case SLE_FILE_U8: x = (byte )SlReadByte(); break;
811  case SLE_FILE_I16: x = (int16 )SlReadUint16(); break;
812  case SLE_FILE_U16: x = (uint16)SlReadUint16(); break;
813  case SLE_FILE_I32: x = (int32 )SlReadUint32(); break;
814  case SLE_FILE_U32: x = (uint32)SlReadUint32(); break;
815  case SLE_FILE_I64: x = (int64 )SlReadUint64(); break;
816  case SLE_FILE_U64: x = (uint64)SlReadUint64(); break;
817  case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break;
818  default: NOT_REACHED();
819  }
820 
821  /* Write The value to the struct. These ARE endian safe. */
822  WriteValue(ptr, conv, x);
823  break;
824  }
825  case SLA_PTRS: break;
826  case SLA_NULL: break;
827  default: NOT_REACHED();
828  }
829 }
830 
840 static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
841 {
842  if (ptr == NULL) return 0;
843  return min(strlen(ptr), length - 1);
844 }
845 
855 static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType conv)
856 {
857  size_t len;
858  const char *str;
859 
860  switch (GetVarMemType(conv)) {
861  default: NOT_REACHED();
862  case SLE_VAR_STR:
863  case SLE_VAR_STRQ:
864  str = *(const char * const *)ptr;
865  len = SIZE_MAX;
866  break;
867  case SLE_VAR_STRB:
868  case SLE_VAR_STRBQ:
869  str = (const char *)ptr;
870  len = length;
871  break;
872  }
873 
874  len = SlCalcNetStringLen(str, len);
875  return len + SlGetArrayLength(len); // also include the length of the index
876 }
877 
884 static void SlString(void *ptr, size_t length, VarType conv)
885 {
886  switch (_sl.action) {
887  case SLA_SAVE: {
888  size_t len;
889  switch (GetVarMemType(conv)) {
890  default: NOT_REACHED();
891  case SLE_VAR_STRB:
892  case SLE_VAR_STRBQ:
893  len = SlCalcNetStringLen((char *)ptr, length);
894  break;
895  case SLE_VAR_STR:
896  case SLE_VAR_STRQ:
897  ptr = *(char **)ptr;
898  len = SlCalcNetStringLen((char *)ptr, SIZE_MAX);
899  break;
900  }
901 
902  SlWriteArrayLength(len);
903  SlCopyBytes(ptr, len);
904  break;
905  }
906  case SLA_LOAD_CHECK:
907  case SLA_LOAD: {
908  size_t len = SlReadArrayLength();
909 
910  switch (GetVarMemType(conv)) {
911  default: NOT_REACHED();
912  case SLE_VAR_STRB:
913  case SLE_VAR_STRBQ:
914  if (len >= length) {
915  DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
916  SlCopyBytes(ptr, length);
917  SlSkipBytes(len - length);
918  len = length - 1;
919  } else {
920  SlCopyBytes(ptr, len);
921  }
922  break;
923  case SLE_VAR_STR:
924  case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
925  free(*(char **)ptr);
926  if (len == 0) {
927  *(char **)ptr = NULL;
928  return;
929  } else {
930  *(char **)ptr = MallocT<char>(len + 1); // terminating '\0'
931  ptr = *(char **)ptr;
932  SlCopyBytes(ptr, len);
933  }
934  break;
935  }
936 
937  ((char *)ptr)[len] = '\0'; // properly terminate the string
939  if ((conv & SLF_ALLOW_CONTROL) != 0) {
940  settings = settings | SVS_ALLOW_CONTROL_CODE;
942  str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
943  }
944  }
945  if ((conv & SLF_ALLOW_NEWLINE) != 0) {
946  settings = settings | SVS_ALLOW_NEWLINE;
947  }
948  str_validate((char *)ptr, (char *)ptr + len, settings);
949  break;
950  }
951  case SLA_PTRS: break;
952  case SLA_NULL: break;
953  default: NOT_REACHED();
954  }
955 }
956 
962 static inline size_t SlCalcArrayLen(size_t length, VarType conv)
963 {
964  return SlCalcConvFileLen(conv) * length;
965 }
966 
973 void SlArray(void *array, size_t length, VarType conv)
974 {
975  if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
976 
977  /* Automatically calculate the length? */
978  if (_sl.need_length != NL_NONE) {
979  SlSetLength(SlCalcArrayLen(length, conv));
980  /* Determine length only? */
981  if (_sl.need_length == NL_CALCLENGTH) return;
982  }
983 
984  /* NOTICE - handle some buggy stuff, in really old versions everything was saved
985  * as a byte-type. So detect this, and adjust array size accordingly */
986  if (_sl.action != SLA_SAVE && _sl_version == 0) {
987  /* all arrays except difficulty settings */
988  if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
989  conv == SLE_INT32 || conv == SLE_UINT32) {
990  SlCopyBytes(array, length * SlCalcConvFileLen(conv));
991  return;
992  }
993  /* used for conversion of Money 32bit->64bit */
994  if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
995  for (uint i = 0; i < length; i++) {
996  ((int64*)array)[i] = (int32)BSWAP32(SlReadUint32());
997  }
998  return;
999  }
1000  }
1001 
1002  /* If the size of elements is 1 byte both in file and memory, no special
1003  * conversion is needed, use specialized copy-copy function to speed up things */
1004  if (conv == SLE_INT8 || conv == SLE_UINT8) {
1005  SlCopyBytes(array, length);
1006  } else {
1007  byte *a = (byte*)array;
1008  byte mem_size = SlCalcConvMemLen(conv);
1009 
1010  for (; length != 0; length --) {
1011  SlSaveLoadConv(a, conv);
1012  a += mem_size; // get size
1013  }
1014  }
1015 }
1016 
1017 
1028 static size_t ReferenceToInt(const void *obj, SLRefType rt)
1029 {
1030  assert(_sl.action == SLA_SAVE);
1031 
1032  if (obj == NULL) return 0;
1033 
1034  switch (rt) {
1035  case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1036  case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1037  case REF_STATION: return ((const Station*)obj)->index + 1;
1038  case REF_TOWN: return ((const Town*)obj)->index + 1;
1039  case REF_ORDER: return ((const Order*)obj)->index + 1;
1040  case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1041  case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1042  case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1043  case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1044  case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1045  case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1046  case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1047  default: NOT_REACHED();
1048  }
1049 }
1050 
1061 static void *IntToReference(size_t index, SLRefType rt)
1062 {
1063  assert_compile(sizeof(size_t) <= sizeof(void *));
1064 
1065  assert(_sl.action == SLA_PTRS);
1066 
1067  /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1068  * and should be loaded like that */
1069  if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) {
1070  rt = REF_VEHICLE;
1071  }
1072 
1073  /* No need to look up NULL pointers, just return immediately */
1074  if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return NULL;
1075 
1076  /* Correct index. Old vehicles were saved differently:
1077  * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1078  if (rt != REF_VEHICLE_OLD) index--;
1079 
1080  switch (rt) {
1081  case REF_ORDERLIST:
1082  if (OrderList::IsValidID(index)) return OrderList::Get(index);
1083  SlErrorCorrupt("Referencing invalid OrderList");
1084 
1085  case REF_ORDER:
1086  if (Order::IsValidID(index)) return Order::Get(index);
1087  /* in old versions, invalid order was used to mark end of order list */
1088  if (IsSavegameVersionBefore(SLV_5, 2)) return NULL;
1089  SlErrorCorrupt("Referencing invalid Order");
1090 
1091  case REF_VEHICLE_OLD:
1092  case REF_VEHICLE:
1093  if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1094  SlErrorCorrupt("Referencing invalid Vehicle");
1095 
1096  case REF_STATION:
1097  if (Station::IsValidID(index)) return Station::Get(index);
1098  SlErrorCorrupt("Referencing invalid Station");
1099 
1100  case REF_TOWN:
1101  if (Town::IsValidID(index)) return Town::Get(index);
1102  SlErrorCorrupt("Referencing invalid Town");
1103 
1104  case REF_ROADSTOPS:
1105  if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1106  SlErrorCorrupt("Referencing invalid RoadStop");
1107 
1108  case REF_ENGINE_RENEWS:
1109  if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1110  SlErrorCorrupt("Referencing invalid EngineRenew");
1111 
1112  case REF_CARGO_PACKET:
1113  if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1114  SlErrorCorrupt("Referencing invalid CargoPacket");
1115 
1116  case REF_STORAGE:
1117  if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1118  SlErrorCorrupt("Referencing invalid PersistentStorage");
1119 
1120  case REF_LINK_GRAPH:
1121  if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1122  SlErrorCorrupt("Referencing invalid LinkGraph");
1123 
1124  case REF_LINK_GRAPH_JOB:
1125  if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1126  SlErrorCorrupt("Referencing invalid LinkGraphJob");
1127 
1128  default: NOT_REACHED();
1129  }
1130 }
1131 
1136 static inline size_t SlCalcListLen(const void *list)
1137 {
1138  const std::list<void *> *l = (const std::list<void *> *) list;
1139 
1140  int type_size = IsSavegameVersionBefore(SLV_69) ? 2 : 4;
1141  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1142  * of the list */
1143  return l->size() * type_size + type_size;
1144 }
1145 
1146 
1152 static void SlList(void *list, SLRefType conv)
1153 {
1154  /* Automatically calculate the length? */
1155  if (_sl.need_length != NL_NONE) {
1156  SlSetLength(SlCalcListLen(list));
1157  /* Determine length only? */
1158  if (_sl.need_length == NL_CALCLENGTH) return;
1159  }
1160 
1161  typedef std::list<void *> PtrList;
1162  PtrList *l = (PtrList *)list;
1163 
1164  switch (_sl.action) {
1165  case SLA_SAVE: {
1166  SlWriteUint32((uint32)l->size());
1167 
1168  PtrList::iterator iter;
1169  for (iter = l->begin(); iter != l->end(); ++iter) {
1170  void *ptr = *iter;
1171  SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
1172  }
1173  break;
1174  }
1175  case SLA_LOAD_CHECK:
1176  case SLA_LOAD: {
1177  size_t length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1178 
1179  /* Load each reference and push to the end of the list */
1180  for (size_t i = 0; i < length; i++) {
1181  size_t data = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1182  l->push_back((void *)data);
1183  }
1184  break;
1185  }
1186  case SLA_PTRS: {
1187  PtrList temp = *l;
1188 
1189  l->clear();
1190  PtrList::iterator iter;
1191  for (iter = temp.begin(); iter != temp.end(); ++iter) {
1192  void *ptr = IntToReference((size_t)*iter, conv);
1193  l->push_back(ptr);
1194  }
1195  break;
1196  }
1197  case SLA_NULL:
1198  l->clear();
1199  break;
1200  default: NOT_REACHED();
1201  }
1202 }
1203 
1204 
1208 template <typename T>
1210  typedef std::deque<T> SlDequeT;
1211 public:
1217  static size_t SlCalcDequeLen(const void *deque, VarType conv)
1218  {
1219  const SlDequeT *l = (const SlDequeT *)deque;
1220 
1221  int type_size = 4;
1222  /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
1223  * of the list */
1224  return l->size() * SlCalcConvFileLen(conv) + type_size;
1225  }
1226 
1232  static void SlDeque(void *deque, VarType conv)
1233  {
1234  SlDequeT *l = (SlDequeT *)deque;
1235 
1236  switch (_sl.action) {
1237  case SLA_SAVE: {
1238  SlWriteUint32((uint32)l->size());
1239 
1240  typename SlDequeT::iterator iter;
1241  for (iter = l->begin(); iter != l->end(); ++iter) {
1242  SlSaveLoadConv(&(*iter), conv);
1243  }
1244  break;
1245  }
1246  case SLA_LOAD_CHECK:
1247  case SLA_LOAD: {
1248  size_t length = SlReadUint32();
1249 
1250  /* Load each value and push to the end of the deque */
1251  for (size_t i = 0; i < length; i++) {
1252  T data;
1253  SlSaveLoadConv(&data, conv);
1254  l->push_back(data);
1255  }
1256  break;
1257  }
1258  case SLA_PTRS:
1259  break;
1260  case SLA_NULL:
1261  l->clear();
1262  break;
1263  default: NOT_REACHED();
1264  }
1265  }
1266 };
1267 
1268 
1274 static inline size_t SlCalcDequeLen(const void *deque, VarType conv)
1275 {
1276  switch (GetVarMemType(conv)) {
1277  case SLE_VAR_BL:
1278  return SlDequeHelper<bool>::SlCalcDequeLen(deque, conv);
1279  case SLE_VAR_I8:
1280  case SLE_VAR_U8:
1281  return SlDequeHelper<uint8>::SlCalcDequeLen(deque, conv);
1282  case SLE_VAR_I16:
1283  case SLE_VAR_U16:
1284  return SlDequeHelper<uint16>::SlCalcDequeLen(deque, conv);
1285  case SLE_VAR_I32:
1286  case SLE_VAR_U32:
1287  return SlDequeHelper<uint32>::SlCalcDequeLen(deque, conv);
1288  case SLE_VAR_I64:
1289  case SLE_VAR_U64:
1290  return SlDequeHelper<uint64>::SlCalcDequeLen(deque, conv);
1291  default: NOT_REACHED();
1292  }
1293 }
1294 
1295 
1301 static void SlDeque(void *deque, VarType conv)
1302 {
1303  switch (GetVarMemType(conv)) {
1304  case SLE_VAR_BL:
1305  SlDequeHelper<bool>::SlDeque(deque, conv);
1306  break;
1307  case SLE_VAR_I8:
1308  case SLE_VAR_U8:
1309  SlDequeHelper<uint8>::SlDeque(deque, conv);
1310  break;
1311  case SLE_VAR_I16:
1312  case SLE_VAR_U16:
1313  SlDequeHelper<uint16>::SlDeque(deque, conv);
1314  break;
1315  case SLE_VAR_I32:
1316  case SLE_VAR_U32:
1317  SlDequeHelper<uint32>::SlDeque(deque, conv);
1318  break;
1319  case SLE_VAR_I64:
1320  case SLE_VAR_U64:
1321  SlDequeHelper<uint64>::SlDeque(deque, conv);
1322  break;
1323  default: NOT_REACHED();
1324  }
1325 }
1326 
1327 
1329 static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
1330 {
1331  if (_sl_version < sld->version_from || _sl_version >= sld->version_to) return false;
1332  if (sld->conv & SLF_NOT_IN_SAVE) return false;
1333 
1334  return true;
1335 }
1336 
1342 static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
1343 {
1344  if ((sld->conv & SLF_NO_NETWORK_SYNC) && _sl.action != SLA_SAVE && _networking && !_network_server) {
1345  SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
1346  return true;
1347  }
1348 
1349  return false;
1350 }
1351 
1358 size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
1359 {
1360  size_t length = 0;
1361 
1362  /* Need to determine the length and write a length tag. */
1363  for (; sld->cmd != SL_END; sld++) {
1364  length += SlCalcObjMemberLength(object, sld);
1365  }
1366  return length;
1367 }
1368 
1369 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
1370 {
1371  assert(_sl.action == SLA_SAVE);
1372 
1373  switch (sld->cmd) {
1374  case SL_VAR:
1375  case SL_REF:
1376  case SL_ARR:
1377  case SL_STR:
1378  case SL_LST:
1379  case SL_DEQUE:
1380  /* CONDITIONAL saveload types depend on the savegame version */
1381  if (!SlIsObjectValidInSavegame(sld)) break;
1382 
1383  switch (sld->cmd) {
1384  case SL_VAR: return SlCalcConvFileLen(sld->conv);
1385  case SL_REF: return SlCalcRefLen();
1386  case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
1387  case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
1388  case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
1389  case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld->conv);
1390  default: NOT_REACHED();
1391  }
1392  break;
1393  case SL_WRITEBYTE: return 1; // a byte is logically of size 1
1394  case SL_VEH_INCLUDE: return SlCalcObjLength(object, GetVehicleDescription(VEH_END));
1395  case SL_ST_INCLUDE: return SlCalcObjLength(object, GetBaseStationDescription());
1396  default: NOT_REACHED();
1397  }
1398  return 0;
1399 }
1400 
1401 #ifdef OTTD_ASSERT
1402 
1408 static bool IsVariableSizeRight(const SaveLoad *sld)
1409 {
1410  switch (sld->cmd) {
1411  case SL_VAR:
1412  switch (GetVarMemType(sld->conv)) {
1413  case SLE_VAR_BL:
1414  return sld->size == sizeof(bool);
1415  case SLE_VAR_I8:
1416  case SLE_VAR_U8:
1417  return sld->size == sizeof(int8);
1418  case SLE_VAR_I16:
1419  case SLE_VAR_U16:
1420  return sld->size == sizeof(int16);
1421  case SLE_VAR_I32:
1422  case SLE_VAR_U32:
1423  return sld->size == sizeof(int32);
1424  case SLE_VAR_I64:
1425  case SLE_VAR_U64:
1426  return sld->size == sizeof(int64);
1427  default:
1428  return sld->size == sizeof(void *);
1429  }
1430  case SL_REF:
1431  /* These should all be pointer sized. */
1432  return sld->size == sizeof(void *);
1433 
1434  case SL_STR:
1435  /* These should be pointer sized, or fixed array. */
1436  return sld->size == sizeof(void *) || sld->size == sld->length;
1437 
1438  default:
1439  return true;
1440  }
1441 }
1442 
1443 #endif /* OTTD_ASSERT */
1444 
1445 bool SlObjectMember(void *ptr, const SaveLoad *sld)
1446 {
1447 #ifdef OTTD_ASSERT
1448  assert(IsVariableSizeRight(sld));
1449 #endif
1450 
1451  VarType conv = GB(sld->conv, 0, 8);
1452  switch (sld->cmd) {
1453  case SL_VAR:
1454  case SL_REF:
1455  case SL_ARR:
1456  case SL_STR:
1457  case SL_LST:
1458  case SL_DEQUE:
1459  /* CONDITIONAL saveload types depend on the savegame version */
1460  if (!SlIsObjectValidInSavegame(sld)) return false;
1461  if (SlSkipVariableOnLoad(sld)) return false;
1462 
1463  switch (sld->cmd) {
1464  case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1465  case SL_REF: // Reference variable, translate
1466  switch (_sl.action) {
1467  case SLA_SAVE:
1468  SlWriteUint32((uint32)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1469  break;
1470  case SLA_LOAD_CHECK:
1471  case SLA_LOAD:
1472  *(size_t *)ptr = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1473  break;
1474  case SLA_PTRS:
1475  *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1476  break;
1477  case SLA_NULL:
1478  *(void **)ptr = NULL;
1479  break;
1480  default: NOT_REACHED();
1481  }
1482  break;
1483  case SL_ARR: SlArray(ptr, sld->length, conv); break;
1484  case SL_STR: SlString(ptr, sld->length, sld->conv); break;
1485  case SL_LST: SlList(ptr, (SLRefType)conv); break;
1486  case SL_DEQUE: SlDeque(ptr, conv); break;
1487  default: NOT_REACHED();
1488  }
1489  break;
1490 
1491  /* SL_WRITEBYTE writes a value to the savegame to identify the type of an object.
1492  * When loading, the value is read explictly with SlReadByte() to determine which
1493  * object description to use. */
1494  case SL_WRITEBYTE:
1495  switch (_sl.action) {
1496  case SLA_SAVE: SlWriteByte(*(uint8 *)ptr); break;
1497  case SLA_LOAD_CHECK:
1498  case SLA_LOAD:
1499  case SLA_PTRS:
1500  case SLA_NULL: break;
1501  default: NOT_REACHED();
1502  }
1503  break;
1504 
1505  /* SL_VEH_INCLUDE loads common code for vehicles */
1506  case SL_VEH_INCLUDE:
1507  SlObject(ptr, GetVehicleDescription(VEH_END));
1508  break;
1509 
1510  case SL_ST_INCLUDE:
1512  break;
1513 
1514  default: NOT_REACHED();
1515  }
1516  return true;
1517 }
1518 
1524 void SlObject(void *object, const SaveLoad *sld)
1525 {
1526  /* Automatically calculate the length? */
1527  if (_sl.need_length != NL_NONE) {
1528  SlSetLength(SlCalcObjLength(object, sld));
1529  if (_sl.need_length == NL_CALCLENGTH) return;
1530  }
1531 
1532  for (; sld->cmd != SL_END; sld++) {
1533  void *ptr = sld->global ? sld->address : GetVariableAddress(object, sld);
1534  SlObjectMember(ptr, sld);
1535  }
1536 }
1537 
1543 {
1544  SlObject(NULL, (const SaveLoad*)sldg);
1545 }
1546 
1552 void SlAutolength(AutolengthProc *proc, void *arg)
1553 {
1554  size_t offs;
1555 
1556  assert(_sl.action == SLA_SAVE);
1557 
1558  /* Tell it to calculate the length */
1559  _sl.need_length = NL_CALCLENGTH;
1560  _sl.obj_len = 0;
1561  proc(arg);
1562 
1563  /* Setup length */
1564  _sl.need_length = NL_WANTLENGTH;
1565  SlSetLength(_sl.obj_len);
1566 
1567  offs = _sl.dumper->GetSize() + _sl.obj_len;
1568 
1569  /* And write the stuff */
1570  proc(arg);
1571 
1572  if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
1573 }
1574 
1579 static void SlLoadChunk(const ChunkHandler *ch)
1580 {
1581  byte m = SlReadByte();
1582  size_t len;
1583  size_t endoffs;
1584 
1585  _sl.block_mode = m;
1586  _sl.obj_len = 0;
1587 
1588  switch (m) {
1589  case CH_ARRAY:
1590  _sl.array_index = 0;
1591  ch->load_proc();
1592  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1593  break;
1594  case CH_SPARSE_ARRAY:
1595  ch->load_proc();
1596  if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
1597  break;
1598  default:
1599  if ((m & 0xF) == CH_RIFF) {
1600  /* Read length */
1601  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1602  len += SlReadUint16();
1603  _sl.obj_len = len;
1604  endoffs = _sl.reader->GetSize() + len;
1605  ch->load_proc();
1606  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1607  } else {
1608  SlErrorCorrupt("Invalid chunk type");
1609  }
1610  break;
1611  }
1612 }
1613 
1619 static void SlLoadCheckChunk(const ChunkHandler *ch)
1620 {
1621  byte m = SlReadByte();
1622  size_t len;
1623  size_t endoffs;
1624 
1625  _sl.block_mode = m;
1626  _sl.obj_len = 0;
1627 
1628  switch (m) {
1629  case CH_ARRAY:
1630  _sl.array_index = 0;
1631  if (ch->load_check_proc) {
1632  ch->load_check_proc();
1633  } else {
1634  SlSkipArray();
1635  }
1636  break;
1637  case CH_SPARSE_ARRAY:
1638  if (ch->load_check_proc) {
1639  ch->load_check_proc();
1640  } else {
1641  SlSkipArray();
1642  }
1643  break;
1644  default:
1645  if ((m & 0xF) == CH_RIFF) {
1646  /* Read length */
1647  len = (SlReadByte() << 16) | ((m >> 4) << 24);
1648  len += SlReadUint16();
1649  _sl.obj_len = len;
1650  endoffs = _sl.reader->GetSize() + len;
1651  if (ch->load_check_proc) {
1652  ch->load_check_proc();
1653  } else {
1654  SlSkipBytes(len);
1655  }
1656  if (_sl.reader->GetSize() != endoffs) SlErrorCorrupt("Invalid chunk size");
1657  } else {
1658  SlErrorCorrupt("Invalid chunk type");
1659  }
1660  break;
1661  }
1662 }
1663 
1668 static ChunkSaveLoadProc *_stub_save_proc;
1669 
1675 static inline void SlStubSaveProc2(void *arg)
1676 {
1677  _stub_save_proc();
1678 }
1679 
1685 static void SlStubSaveProc()
1686 {
1688 }
1689 
1695 static void SlSaveChunk(const ChunkHandler *ch)
1696 {
1697  ChunkSaveLoadProc *proc = ch->save_proc;
1698 
1699  /* Don't save any chunk information if there is no save handler. */
1700  if (proc == NULL) return;
1701 
1702  SlWriteUint32(ch->id);
1703  DEBUG(sl, 2, "Saving chunk %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1704 
1705  if (ch->flags & CH_AUTO_LENGTH) {
1706  /* Need to calculate the length. Solve that by calling SlAutoLength in the save_proc. */
1707  _stub_save_proc = proc;
1708  proc = SlStubSaveProc;
1709  }
1710 
1711  _sl.block_mode = ch->flags & CH_TYPE_MASK;
1712  switch (ch->flags & CH_TYPE_MASK) {
1713  case CH_RIFF:
1714  _sl.need_length = NL_WANTLENGTH;
1715  proc();
1716  break;
1717  case CH_ARRAY:
1718  _sl.last_array_index = 0;
1719  SlWriteByte(CH_ARRAY);
1720  proc();
1721  SlWriteArrayLength(0); // Terminate arrays
1722  break;
1723  case CH_SPARSE_ARRAY:
1724  SlWriteByte(CH_SPARSE_ARRAY);
1725  proc();
1726  SlWriteArrayLength(0); // Terminate arrays
1727  break;
1728  default: NOT_REACHED();
1729  }
1730 }
1731 
1733 static void SlSaveChunks()
1734 {
1736  SlSaveChunk(ch);
1737  }
1738 
1739  /* Terminator */
1740  SlWriteUint32(0);
1741 }
1742 
1749 static const ChunkHandler *SlFindChunkHandler(uint32 id)
1750 {
1751  FOR_ALL_CHUNK_HANDLERS(ch) if (ch->id == id) return ch;
1752  return NULL;
1753 }
1754 
1756 static void SlLoadChunks()
1757 {
1758  uint32 id;
1759  const ChunkHandler *ch;
1760 
1761  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1762  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1763 
1764  ch = SlFindChunkHandler(id);
1765  if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
1766  SlLoadChunk(ch);
1767  }
1768 }
1769 
1771 static void SlLoadCheckChunks()
1772 {
1773  uint32 id;
1774  const ChunkHandler *ch;
1775 
1776  for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
1777  DEBUG(sl, 2, "Loading chunk %c%c%c%c", id >> 24, id >> 16, id >> 8, id);
1778 
1779  ch = SlFindChunkHandler(id);
1780  if (ch == NULL) SlErrorCorrupt("Unknown chunk type");
1781  SlLoadCheckChunk(ch);
1782  }
1783 }
1784 
1786 static void SlFixPointers()
1787 {
1788  _sl.action = SLA_PTRS;
1789 
1790  DEBUG(sl, 1, "Fixing pointers");
1791 
1793  if (ch->ptrs_proc != NULL) {
1794  DEBUG(sl, 2, "Fixing pointers for %c%c%c%c", ch->id >> 24, ch->id >> 16, ch->id >> 8, ch->id);
1795  ch->ptrs_proc();
1796  }
1797  }
1798 
1799  DEBUG(sl, 1, "All pointers fixed");
1800 
1801  assert(_sl.action == SLA_PTRS);
1802 }
1803 
1804 
1807  FILE *file;
1808  long begin;
1809 
1814  FileReader(FILE *file) : LoadFilter(NULL), file(file), begin(ftell(file))
1815  {
1816  }
1817 
1820  {
1821  if (this->file != NULL) fclose(this->file);
1822  this->file = NULL;
1823 
1824  /* Make sure we don't double free. */
1825  _sl.sf = NULL;
1826  }
1827 
1828  /* virtual */ size_t Read(byte *buf, size_t size)
1829  {
1830  /* We're in the process of shutting down, i.e. in "failure" mode. */
1831  if (this->file == NULL) return 0;
1832 
1833  return fread(buf, 1, size, this->file);
1834  }
1835 
1836  /* virtual */ void Reset()
1837  {
1838  clearerr(this->file);
1839  if (fseek(this->file, this->begin, SEEK_SET)) {
1840  DEBUG(sl, 1, "Could not reset the file reading");
1841  }
1842  }
1843 };
1844 
1847  FILE *file;
1848 
1853  FileWriter(FILE *file) : SaveFilter(NULL), file(file)
1854  {
1855  }
1856 
1859  {
1860  this->Finish();
1861 
1862  /* Make sure we don't double free. */
1863  _sl.sf = NULL;
1864  }
1865 
1866  /* virtual */ void Write(byte *buf, size_t size)
1867  {
1868  /* We're in the process of shutting down, i.e. in "failure" mode. */
1869  if (this->file == NULL) return;
1870 
1871  if (fwrite(buf, 1, size, this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
1872  }
1873 
1874  /* virtual */ void Finish()
1875  {
1876  if (this->file != NULL) fclose(this->file);
1877  this->file = NULL;
1878  }
1879 };
1880 
1881 /*******************************************
1882  ********** START OF LZO CODE **************
1883  *******************************************/
1884 
1885 #ifdef WITH_LZO
1886 #include <lzo/lzo1x.h>
1887 
1889 static const uint LZO_BUFFER_SIZE = 8192;
1890 
1898  {
1899  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
1900  }
1901 
1902  /* virtual */ size_t Read(byte *buf, size_t ssize)
1903  {
1904  assert(ssize >= LZO_BUFFER_SIZE);
1905 
1906  /* Buffer size is from the LZO docs plus the chunk header size. */
1907  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1908  uint32 tmp[2];
1909  uint32 size;
1910  lzo_uint len = ssize;
1911 
1912  /* Read header*/
1913  if (this->chain->Read((byte*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
1914 
1915  /* Check if size is bad */
1916  ((uint32*)out)[0] = size = tmp[1];
1917 
1918  if (_sl_version != SL_MIN_VERSION) {
1919  tmp[0] = TO_BE32(tmp[0]);
1920  size = TO_BE32(size);
1921  }
1922 
1923  if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
1924 
1925  /* Read block */
1926  if (this->chain->Read(out + sizeof(uint32), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
1927 
1928  /* Verify checksum */
1929  if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32))) SlErrorCorrupt("Bad checksum");
1930 
1931  /* Decompress */
1932  int ret = lzo1x_decompress_safe(out + sizeof(uint32) * 1, size, buf, &len, NULL);
1933  if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
1934  return len;
1935  }
1936 };
1937 
1945  LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
1946  {
1947  if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
1948  }
1949 
1950  /* virtual */ void Write(byte *buf, size_t size)
1951  {
1952  const lzo_bytep in = buf;
1953  /* Buffer size is from the LZO docs plus the chunk header size. */
1954  byte out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32) * 2];
1955  byte wrkmem[LZO1X_1_MEM_COMPRESS];
1956  lzo_uint outlen;
1957 
1958  do {
1959  /* Compress up to LZO_BUFFER_SIZE bytes at once. */
1960  lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
1961  lzo1x_1_compress(in, len, out + sizeof(uint32) * 2, &outlen, wrkmem);
1962  ((uint32*)out)[1] = TO_BE32((uint32)outlen);
1963  ((uint32*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32), outlen + sizeof(uint32)));
1964  this->chain->Write(out, outlen + sizeof(uint32) * 2);
1965 
1966  /* Move to next data chunk. */
1967  size -= len;
1968  in += len;
1969  } while (size > 0);
1970  }
1971 };
1972 
1973 #endif /* WITH_LZO */
1974 
1975 /*********************************************
1976  ******** START OF NOCOMP CODE (uncompressed)*
1977  *********************************************/
1978 
1986  {
1987  }
1988 
1989  /* virtual */ size_t Read(byte *buf, size_t size)
1990  {
1991  return this->chain->Read(buf, size);
1992  }
1993 };
1994 
2002  NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2003  {
2004  }
2005 
2006  /* virtual */ void Write(byte *buf, size_t size)
2007  {
2008  this->chain->Write(buf, size);
2009  }
2010 };
2011 
2012 /********************************************
2013  ********** START OF ZLIB CODE **************
2014  ********************************************/
2015 
2016 #if defined(WITH_ZLIB)
2017 #include <zlib.h>
2018 
2021  z_stream z;
2022  byte fread_buf[MEMORY_CHUNK_SIZE];
2023 
2029  {
2030  memset(&this->z, 0, sizeof(this->z));
2031  if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2032  }
2033 
2036  {
2037  inflateEnd(&this->z);
2038  }
2039 
2040  /* virtual */ size_t Read(byte *buf, size_t size)
2041  {
2042  this->z.next_out = buf;
2043  this->z.avail_out = (uint)size;
2044 
2045  do {
2046  /* read more bytes from the file? */
2047  if (this->z.avail_in == 0) {
2048  this->z.next_in = this->fread_buf;
2049  this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2050  }
2051 
2052  /* inflate the data */
2053  int r = inflate(&this->z, 0);
2054  if (r == Z_STREAM_END) break;
2055 
2056  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2057  } while (this->z.avail_out != 0);
2058 
2059  return size - this->z.avail_out;
2060  }
2061 };
2062 
2065  z_stream z;
2066 
2072  ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
2073  {
2074  memset(&this->z, 0, sizeof(this->z));
2075  if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2076  }
2077 
2080  {
2081  deflateEnd(&this->z);
2082  }
2083 
2090  void WriteLoop(byte *p, size_t len, int mode)
2091  {
2092  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2093  uint n;
2094  this->z.next_in = p;
2095  this->z.avail_in = (uInt)len;
2096  do {
2097  this->z.next_out = buf;
2098  this->z.avail_out = sizeof(buf);
2099 
2107  int r = deflate(&this->z, mode);
2108 
2109  /* bytes were emitted? */
2110  if ((n = sizeof(buf) - this->z.avail_out) != 0) {
2111  this->chain->Write(buf, n);
2112  }
2113  if (r == Z_STREAM_END) break;
2114 
2115  if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2116  } while (this->z.avail_in || !this->z.avail_out);
2117  }
2118 
2119  /* virtual */ void Write(byte *buf, size_t size)
2120  {
2121  this->WriteLoop(buf, size, 0);
2122  }
2123 
2124  /* virtual */ void Finish()
2125  {
2126  this->WriteLoop(NULL, 0, Z_FINISH);
2127  this->chain->Finish();
2128  }
2129 };
2130 
2131 #endif /* WITH_ZLIB */
2132 
2133 /********************************************
2134  ********** START OF LZMA CODE **************
2135  ********************************************/
2136 
2137 #if defined(WITH_LZMA)
2138 #include <lzma.h>
2139 
2146 static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2147 
2150  lzma_stream lzma;
2151  byte fread_buf[MEMORY_CHUNK_SIZE];
2152 
2157  LZMALoadFilter(LoadFilter *chain) : LoadFilter(chain), lzma(_lzma_init)
2158  {
2159  /* Allow saves up to 256 MB uncompressed */
2160  if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2161  }
2162 
2165  {
2166  lzma_end(&this->lzma);
2167  }
2168 
2169  /* virtual */ size_t Read(byte *buf, size_t size)
2170  {
2171  this->lzma.next_out = buf;
2172  this->lzma.avail_out = size;
2173 
2174  do {
2175  /* read more bytes from the file? */
2176  if (this->lzma.avail_in == 0) {
2177  this->lzma.next_in = this->fread_buf;
2178  this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2179  }
2180 
2181  /* inflate the data */
2182  lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2183  if (r == LZMA_STREAM_END) break;
2184  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2185  } while (this->lzma.avail_out != 0);
2186 
2187  return size - this->lzma.avail_out;
2188  }
2189 };
2190 
2193  lzma_stream lzma;
2194 
2200  LZMASaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain), lzma(_lzma_init)
2201  {
2202  if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2203  }
2204 
2207  {
2208  lzma_end(&this->lzma);
2209  }
2210 
2217  void WriteLoop(byte *p, size_t len, lzma_action action)
2218  {
2219  byte buf[MEMORY_CHUNK_SIZE]; // output buffer
2220  size_t n;
2221  this->lzma.next_in = p;
2222  this->lzma.avail_in = len;
2223  do {
2224  this->lzma.next_out = buf;
2225  this->lzma.avail_out = sizeof(buf);
2226 
2227  lzma_ret r = lzma_code(&this->lzma, action);
2228 
2229  /* bytes were emitted? */
2230  if ((n = sizeof(buf) - this->lzma.avail_out) != 0) {
2231  this->chain->Write(buf, n);
2232  }
2233  if (r == LZMA_STREAM_END) break;
2234  if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2235  } while (this->lzma.avail_in || !this->lzma.avail_out);
2236  }
2237 
2238  /* virtual */ void Write(byte *buf, size_t size)
2239  {
2240  this->WriteLoop(buf, size, LZMA_RUN);
2241  }
2242 
2243  /* virtual */ void Finish()
2244  {
2245  this->WriteLoop(NULL, 0, LZMA_FINISH);
2246  this->chain->Finish();
2247  }
2248 };
2249 
2250 #endif /* WITH_LZMA */
2251 
2252 /*******************************************
2253  ************* END OF CODE *****************
2254  *******************************************/
2255 
2258  const char *name;
2259  uint32 tag;
2260 
2261  LoadFilter *(*init_load)(LoadFilter *chain);
2262  SaveFilter *(*init_write)(SaveFilter *chain, byte compression);
2263 
2267 };
2268 
2271 #if defined(WITH_LZO)
2272  /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2273  {"lzo", TO_BE32X('OTTD'), CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2274 #else
2275  {"lzo", TO_BE32X('OTTD'), NULL, NULL, 0, 0, 0},
2276 #endif
2277  /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2278  {"none", TO_BE32X('OTTN'), CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2279 #if defined(WITH_ZLIB)
2280  /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2281  * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2282  * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2283  {"zlib", TO_BE32X('OTTZ'), CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2284 #else
2285  {"zlib", TO_BE32X('OTTZ'), NULL, NULL, 0, 0, 0},
2286 #endif
2287 #if defined(WITH_LZMA)
2288  /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2289  * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2290  * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2291  * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2292  * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2293  {"lzma", TO_BE32X('OTTX'), CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2294 #else
2295  {"lzma", TO_BE32X('OTTX'), NULL, NULL, 0, 0, 0},
2296 #endif
2297 };
2298 
2306 static const SaveLoadFormat *GetSavegameFormat(char *s, byte *compression_level)
2307 {
2308  const SaveLoadFormat *def = lastof(_saveload_formats);
2309 
2310  /* find default savegame format, the highest one with which files can be written */
2311  while (!def->init_write) def--;
2312 
2313  if (!StrEmpty(s)) {
2314  /* Get the ":..." of the compression level out of the way */
2315  char *complevel = strrchr(s, ':');
2316  if (complevel != NULL) *complevel = '\0';
2317 
2318  for (const SaveLoadFormat *slf = &_saveload_formats[0]; slf != endof(_saveload_formats); slf++) {
2319  if (slf->init_write != NULL && strcmp(s, slf->name) == 0) {
2320  *compression_level = slf->default_compression;
2321  if (complevel != NULL) {
2322  /* There is a compression level in the string.
2323  * First restore the : we removed to do proper name matching,
2324  * then move the the begin of the actual version. */
2325  *complevel = ':';
2326  complevel++;
2327 
2328  /* Get the version and determine whether all went fine. */
2329  char *end;
2330  long level = strtol(complevel, &end, 10);
2331  if (end == complevel || level != Clamp(level, slf->min_compression, slf->max_compression)) {
2332  SetDParamStr(0, complevel);
2333  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
2334  } else {
2335  *compression_level = level;
2336  }
2337  }
2338  return slf;
2339  }
2340  }
2341 
2342  SetDParamStr(0, s);
2343  SetDParamStr(1, def->name);
2344  ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
2345 
2346  /* Restore the string by adding the : back */
2347  if (complevel != NULL) *complevel = ':';
2348  }
2349  *compression_level = def->default_compression;
2350  return def;
2351 }
2352 
2353 /* actual loader/saver function */
2354 void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2355 extern bool AfterLoadGame();
2356 extern bool LoadOldSaveGame(const char *file);
2357 
2361 static inline void ClearSaveLoadState()
2362 {
2363  delete _sl.dumper;
2364  _sl.dumper = NULL;
2365 
2366  delete _sl.sf;
2367  _sl.sf = NULL;
2368 
2369  delete _sl.reader;
2370  _sl.reader = NULL;
2371 
2372  delete _sl.lf;
2373  _sl.lf = NULL;
2374 }
2375 
2381 static void SaveFileStart()
2382 {
2383  _sl.ff_state = _fast_forward;
2384  _fast_forward = 0;
2385  SetMouseCursorBusy(true);
2386 
2388  _sl.saveinprogress = true;
2389 }
2390 
2392 static void SaveFileDone()
2393 {
2394  if (_game_mode != GM_MENU) _fast_forward = _sl.ff_state;
2395  SetMouseCursorBusy(false);
2396 
2398  _sl.saveinprogress = false;
2399 }
2400 
2403 {
2404  _sl.error_str = str;
2405 }
2406 
2409 {
2410  SetDParam(0, _sl.error_str);
2411  SetDParamStr(1, _sl.extra_msg);
2412 
2413  static char err_str[512];
2414  GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
2415  return err_str;
2416 }
2417 
2419 static void SaveFileError()
2420 {
2422  ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
2423  SaveFileDone();
2424 }
2425 
2430 static SaveOrLoadResult SaveFileToDisk(bool threaded)
2431 {
2432  try {
2433  byte compression;
2434  const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
2435 
2436  /* We have written our stuff to memory, now write it to file! */
2437  uint32 hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
2438  _sl.sf->Write((byte*)hdr, sizeof(hdr));
2439 
2440  _sl.sf = fmt->init_write(_sl.sf, compression);
2441  _sl.dumper->Flush(_sl.sf);
2442 
2444 
2445  if (threaded) SetAsyncSaveFinish(SaveFileDone);
2446 
2447  return SL_OK;
2448  } catch (...) {
2450 
2452 
2453  /* We don't want to shout when saving is just
2454  * cancelled due to a client disconnecting. */
2455  if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
2456  /* Skip the "colour" character */
2457  DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2458  asfp = SaveFileError;
2459  }
2460 
2461  if (threaded) {
2462  SetAsyncSaveFinish(asfp);
2463  } else {
2464  asfp();
2465  }
2466  return SL_ERROR;
2467  }
2468 }
2469 
2471 static void SaveFileToDiskThread(void *arg)
2472 {
2473  SaveFileToDisk(true);
2474 }
2475 
2476 void WaitTillSaved()
2477 {
2478  if (_save_thread == NULL) return;
2479 
2480  _save_thread->Join();
2481  delete _save_thread;
2482  _save_thread = NULL;
2483 
2484  /* Make sure every other state is handled properly as well. */
2486 }
2487 
2496 static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
2497 {
2498  assert(!_sl.saveinprogress);
2499 
2500  _sl.dumper = new MemoryDumper();
2501  _sl.sf = writer;
2502 
2503  _sl_version = SAVEGAME_VERSION;
2504 
2505  SaveViewportBeforeSaveGame();
2506  SlSaveChunks();
2507 
2508  SaveFileStart();
2509  if (!threaded || !ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread, "ottd:savegame")) {
2510  if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
2511 
2512  SaveOrLoadResult result = SaveFileToDisk(false);
2513  SaveFileDone();
2514 
2515  return result;
2516  }
2517 
2518  return SL_OK;
2519 }
2520 
2528 {
2529  try {
2530  _sl.action = SLA_SAVE;
2531  return DoSave(writer, threaded);
2532  } catch (...) {
2534  return SL_ERROR;
2535  }
2536 }
2537 
2544 static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
2545 {
2546  _sl.lf = reader;
2547 
2548  if (load_check) {
2549  /* Clear previous check data */
2551  /* Mark SL_LOAD_CHECK as supported for this savegame. */
2552  _load_check_data.checkable = true;
2553  }
2554 
2555  uint32 hdr[2];
2556  if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2557 
2558  /* see if we have any loader for this type. */
2559  const SaveLoadFormat *fmt = _saveload_formats;
2560  for (;;) {
2561  /* No loader found, treat as version 0 and use LZO format */
2562  if (fmt == endof(_saveload_formats)) {
2563  DEBUG(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
2564  _sl.lf->Reset();
2565  _sl_version = SL_MIN_VERSION;
2566  _sl_minor_version = 0;
2567 
2568  /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
2569  fmt = _saveload_formats;
2570  for (;;) {
2571  if (fmt == endof(_saveload_formats)) {
2572  /* Who removed LZO support? Bad bad boy! */
2573  NOT_REACHED();
2574  }
2575  if (fmt->tag == TO_BE32X('OTTD')) break;
2576  fmt++;
2577  }
2578  break;
2579  }
2580 
2581  if (fmt->tag == hdr[0]) {
2582  /* check version number */
2583  _sl_version = (SaveLoadVersion)(TO_BE32(hdr[1]) >> 16);
2584  /* Minor is not used anymore from version 18.0, but it is still needed
2585  * in versions before that (4 cases) which can't be removed easy.
2586  * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
2587  _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF;
2588 
2589  DEBUG(sl, 1, "Loading savegame version %d", _sl_version);
2590 
2591  /* Is the version higher than the current? */
2592  if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
2593  break;
2594  }
2595 
2596  fmt++;
2597  }
2598 
2599  /* loader for this savegame type is not implemented? */
2600  if (fmt->init_load == NULL) {
2601  char err_str[64];
2602  seprintf(err_str, lastof(err_str), "Loader for '%s' is not available.", fmt->name);
2603  SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
2604  }
2605 
2606  _sl.lf = fmt->init_load(_sl.lf);
2607  _sl.reader = new ReadBuffer(_sl.lf);
2608  _next_offs = 0;
2609 
2610  if (!load_check) {
2611  /* Old maps were hardcoded to 256x256 and thus did not contain
2612  * any mapsize information. Pre-initialize to 256x256 to not to
2613  * confuse old games */
2614  InitializeGame(256, 256, true, true);
2615 
2616  GamelogReset();
2617 
2619  /*
2620  * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
2621  * shared savegame version 4. Anything before that 'obviously'
2622  * does not have any NewGRFs. Between the introduction and
2623  * savegame version 41 (just before 0.5) the NewGRF settings
2624  * were not stored in the savegame and they were loaded by
2625  * using the settings from the main menu.
2626  * So, to recap:
2627  * - savegame version < 4: do not load any NewGRFs.
2628  * - savegame version >= 41: load NewGRFs from savegame, which is
2629  * already done at this stage by
2630  * overwriting the main menu settings.
2631  * - other savegame versions: use main menu settings.
2632  *
2633  * This means that users *can* crash savegame version 4..40
2634  * savegames if they set incompatible NewGRFs in the main menu,
2635  * but can't crash anymore for savegame version < 4 savegames.
2636  *
2637  * Note: this is done here because AfterLoadGame is also called
2638  * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
2639  */
2641  }
2642  }
2643 
2644  if (load_check) {
2645  /* Load chunks into _load_check_data.
2646  * No pools are loaded. References are not possible, and thus do not need resolving. */
2648  } else {
2649  /* Load chunks and resolve references */
2650  SlLoadChunks();
2651  SlFixPointers();
2652  }
2653 
2655 
2656  _savegame_type = SGT_OTTD;
2657 
2658  if (load_check) {
2659  /* The only part from AfterLoadGame() we need */
2661  } else {
2663 
2664  /* After loading fix up savegame for any internal changes that
2665  * might have occurred since then. If it fails, load back the old game. */
2666  if (!AfterLoadGame()) {
2668  return SL_REINIT;
2669  }
2670 
2672  }
2673 
2674  return SL_OK;
2675 }
2676 
2683 {
2684  try {
2685  _sl.action = SLA_LOAD;
2686  return DoLoad(reader, false);
2687  } catch (...) {
2689  return SL_REINIT;
2690  }
2691 }
2692 
2702 SaveOrLoadResult SaveOrLoad(const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
2703 {
2704  /* An instance of saving is already active, so don't go saving again */
2705  if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
2706  /* if not an autosave, but a user action, show error message */
2707  if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
2708  return SL_OK;
2709  }
2710  WaitTillSaved();
2711 
2712  try {
2713  /* Load a TTDLX or TTDPatch game */
2714  if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
2715  InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
2716 
2717  /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
2718  * and if so a new NewGRF list will be made in LoadOldSaveGame.
2719  * Note: this is done here because AfterLoadGame is also called
2720  * for OTTD savegames which have their own NewGRF logic. */
2722  GamelogReset();
2723  if (!LoadOldSaveGame(filename)) return SL_REINIT;
2724  _sl_version = SL_MIN_VERSION;
2725  _sl_minor_version = 0;
2727  if (!AfterLoadGame()) {
2729  return SL_REINIT;
2730  }
2732  return SL_OK;
2733  }
2734 
2735  assert(dft == DFT_GAME_FILE);
2736  switch (fop) {
2737  case SLO_CHECK:
2738  _sl.action = SLA_LOAD_CHECK;
2739  break;
2740 
2741  case SLO_LOAD:
2742  _sl.action = SLA_LOAD;
2743  break;
2744 
2745  case SLO_SAVE:
2746  _sl.action = SLA_SAVE;
2747  break;
2748 
2749  default: NOT_REACHED();
2750  }
2751 
2752  FILE *fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
2753 
2754  /* Make it a little easier to load savegames from the console */
2755  if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
2756  if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
2757  if (fh == NULL && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
2758 
2759  if (fh == NULL) {
2760  SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2761  }
2762 
2763  if (fop == SLO_SAVE) { // SAVE game
2764  DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
2765  if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
2766 
2767  return DoSave(new FileWriter(fh), threaded);
2768  }
2769 
2770  /* LOAD game */
2771  assert(fop == SLO_LOAD || fop == SLO_CHECK);
2772  DEBUG(desync, 1, "load: %s", filename);
2773  return DoLoad(new FileReader(fh), fop == SLO_CHECK);
2774  } catch (...) {
2775  /* This code may be executed both for old and new save games. */
2777 
2778  /* Skip the "colour" character */
2779  if (fop != SLO_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
2780 
2781  /* A saver/loader exception!! reinitialize all variables to prevent crash! */
2782  return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
2783  }
2784 }
2785 
2788 {
2790 }
2791 
2797 void GenerateDefaultSaveName(char *buf, const char *last)
2798 {
2799  /* Check if we have a name for this map, which is the name of the first
2800  * available company. When there's no company available we'll use
2801  * 'Spectator' as "company" name. */
2802  CompanyID cid = _local_company;
2803  if (!Company::IsValidID(cid)) {
2804  const Company *c;
2805  FOR_ALL_COMPANIES(c) {
2806  cid = c->index;
2807  break;
2808  }
2809  }
2810 
2811  SetDParam(0, cid);
2812 
2813  /* Insert current date */
2815  case 0: SetDParam(1, STR_JUST_DATE_LONG); break;
2816  case 1: SetDParam(1, STR_JUST_DATE_TINY); break;
2817  case 2: SetDParam(1, STR_JUST_DATE_ISO); break;
2818  default: NOT_REACHED();
2819  }
2820  SetDParam(2, _date);
2821 
2822  /* Get the correct string (special string for when there's not company) */
2823  GetString(buf, !Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, last);
2824  SanitizeFilename(buf);
2825 }
2826 
2832 {
2833  this->SetMode(SLO_LOAD, GetAbstractFileType(ft), GetDetailedFileType(ft));
2834 }
2835 
2843 {
2844  if (aft == FT_INVALID || aft == FT_NONE) {
2845  this->file_op = SLO_INVALID;
2846  this->detail_ftype = DFT_INVALID;
2847  this->abstract_ftype = FT_INVALID;
2848  return;
2849  }
2850 
2851  this->file_op = fop;
2852  this->detail_ftype = dft;
2853  this->abstract_ftype = aft;
2854 }
2855 
2860 void FileToSaveLoad::SetName(const char *name)
2861 {
2862  strecpy(this->name, name, lastof(this->name));
2863 }
2864 
2869 void FileToSaveLoad::SetTitle(const char *title)
2870 {
2871  strecpy(this->title, title, lastof(this->title));
2872 }
2873 
2874 #if 0
2875 
2881 int GetSavegameType(char *file)
2882 {
2883  const SaveLoadFormat *fmt;
2884  uint32 hdr;
2885  FILE *f;
2886  int mode = SL_OLD_LOAD;
2887 
2888  f = fopen(file, "rb");
2889  if (fread(&hdr, sizeof(hdr), 1, f) != 1) {
2890  DEBUG(sl, 0, "Savegame is obsolete or invalid format");
2891  mode = SL_LOAD; // don't try to get filename, just show name as it is written
2892  } else {
2893  /* see if we have any loader for this type. */
2894  for (fmt = _saveload_formats; fmt != endof(_saveload_formats); fmt++) {
2895  if (fmt->tag == hdr) {
2896  mode = SL_LOAD; // new type of savegame
2897  break;
2898  }
2899  }
2900  }
2901 
2902  fclose(f);
2903  return mode;
2904 }
2905 #endif
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:69
~FileWriter()
Make sure everything is cleaned up.
Definition: saveload.cpp:1858
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:2544
static void SlFixPointers()
Fix all pointers (convert index -> pointer)
Definition: saveload.cpp:1786
static size_t SlCalcNetStringLen(const char *ptr, size_t length)
Calculate the net length of a string.
Definition: saveload.cpp:840
static void SlLoadCheckChunks()
Load all chunks for savegame checking.
Definition: saveload.cpp:1771
static SaveOrLoadResult SaveFileToDisk(bool threaded)
We have written the whole game into memory, _memory_savegame, now find and appropriate compressor and...
Definition: saveload.cpp:2430
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:1274
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:565
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
LZMALoadFilter(LoadFilter *chain)
Initialise this filter.
Definition: saveload.cpp:2157
LoadFilter *(* init_load)(LoadFilter *chain)
Constructor for the load filter.
Definition: saveload.cpp:2261
Filter using Zlib compression.
Definition: saveload.cpp:2020
void GenerateDefaultSaveName(char *buf, const char *last)
Fill the buffer with the default name for a savegame or screenshot.
Definition: saveload.cpp:2797
NeedLength need_length
working in NeedLength (Autolength) mode?
Definition: saveload.cpp:185
z_stream z
Stream state we are reading from.
Definition: saveload.cpp:2021
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:1602
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:2040
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:348
Yes, simply writing to a file.
Definition: saveload.cpp:1846
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:1342
void Finish()
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2243
string (with pre-allocated buffer)
Definition: saveload.h:418
void SetName(const char *name)
Set the name of the file.
Definition: saveload.cpp:2860
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:2150
lzma_stream lzma
Stream state that we are writing to.
Definition: saveload.cpp:2193
size_t Read(byte *buf, size_t size)
Read a given number of bytes from the savegame.
Definition: saveload.cpp:2169
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:1152
void Finish()
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:1874
void Write(byte *buf, size_t size)
Write a given number of bytes into the savegame.
Definition: saveload.cpp:2119
static size_t SlCalcArrayLen(size_t length, VarType conv)
Return the size in bytes of a certain type of atomic array.
Definition: saveload.cpp:962
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:2028
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:462
uint32 id
Unique ID (4 letters).
Definition: saveload.h:347
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:2200
void Write(byte *buf, size_t size)
Write a given number of bytes into the savegame.
Definition: saveload.cpp:1866
Filter using LZO compression.
Definition: saveload.cpp:1939
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:1808
int64 ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition: saveload.cpp:733
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:2527
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:2270
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:2392
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:2787
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:362
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:2149
size_t Read(byte *buf, size_t size)
Read a given number of bytes from the savegame.
Definition: saveload.cpp:1989
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
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:1329
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:2035
byte default_compression
the default compression level of this format
Definition: saveload.cpp:2265
const char * GetSaveLoadErrorString()
Get the string representation of the error message.
Definition: saveload.cpp:2408
File is being saved.
Definition: fileio_type.h:52
FILE * file
The file to write to.
Definition: saveload.cpp:1847
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:721
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:2471
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:1695
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:973
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition: saveload.cpp:373
z_stream z
Stream state we are writing to.
Definition: saveload.cpp:2065
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:1836
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:2064
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:855
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:279
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:1902
void WriteLoop(byte *p, size_t len, lzma_action action)
Helper loop for writing the data.
Definition: saveload.cpp:2217
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:2869
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:704
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:1807
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:1892
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:1301
Filter without any compression.
Definition: saveload.cpp:1980
Old save game or scenario file.
Definition: fileio_type.h:32
~ZlibSaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2079
allow control codes in the strings
Definition: saveload.h:461
static void SlSaveChunks()
Save all chunks.
Definition: saveload.cpp:1733
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:2266
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:1408
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:1828
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:1232
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:1358
Game loaded.
Definition: gamelog.h:20
Filter without any compression.
Definition: saveload.cpp:1996
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:2257
static void SlLoadCheckChunk(const ChunkHandler *ch)
Load a chunk of data for checking savegames.
Definition: saveload.cpp:1619
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:504
const SaveLoadVersion SAVEGAME_VERSION
Current savegame version of OpenTTD.
ZlibSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2072
SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
Load the game using a (reader) filter.
Definition: saveload.cpp:2682
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:2402
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:2702
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:1756
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:356
#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:1889
static uint SlGetGammaLength(size_t i)
Return how many bytes used to encode a gamma value.
Definition: saveload.cpp:529
byte SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:392
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:2419
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:615
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:1749
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:448
void SlSkipArray()
Skip an array or sparse array.
Definition: saveload.cpp:648
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:1217
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:2361
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:2006
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:2146
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:1675
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:2306
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:355
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:2238
~LZMALoadFilter()
Clean everything up.
Definition: saveload.cpp:2164
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:1819
Struct to store engine replacements.
static void SaveFileStart()
Update the gui accordingly when starting saving and set locks on saveload.
Definition: saveload.cpp:2381
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:1524
uint32 tag
the 4-letter tag by which it is identified in the savegame
Definition: saveload.cpp:2259
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:403
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:589
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:1950
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:1814
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:2262
void Finish()
Prepare everything to finish writing the savegame.
Definition: saveload.cpp:2124
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1542
LZOSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:1945
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:1552
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:2264
static size_t SlCalcListLen(const void *list)
Return the size in bytes of a list.
Definition: saveload.cpp:1136
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:2192
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:1209
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:1853
static void SlStubSaveProc()
Stub Chunk handlers to only calculate length and do nothing else.
Definition: saveload.cpp:1685
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition: saveload.cpp:354
void SlSetLength(size_t length)
Sets the length of either a RIFF object or the number of items in an array.
Definition: saveload.cpp:660
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:2831
static void SlLoadChunk(const ChunkHandler *ch)
Load a chunk of data (eg vehicles, stations, etc.)
Definition: saveload.cpp:1579
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:2496
NoCompSaveFilter(SaveFilter *chain, byte compression_level)
Initialise this filter.
Definition: saveload.cpp:2002
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:884
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:1897
~LZMASaveFilter()
Clean up what we allocated.
Definition: saveload.cpp:2206
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:2258
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:757
void SlWriteByte(byte b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:401
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:1668
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:1985
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:1028
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:783
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:1061
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:3279
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:598
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:2090
#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:1806
error that was caught in the middle of updating game state, need to clear it. (can only happen during...
Definition: saveload.h:302