OpenTTD Source 20250529-master-g10c159a79f
saveload.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
23#include "../stdafx.h"
24#include "../debug.h"
25#include "../station_base.h"
26#include "../thread.h"
27#include "../town.h"
28#include "../network/network.h"
29#include "../window_func.h"
30#include "../strings_func.h"
31#include "../core/endian_func.hpp"
32#include "../core/string_builder.hpp"
33#include "../core/string_consumer.hpp"
34#include "../vehicle_base.h"
35#include "../company_func.h"
36#include "../timer/timer_game_economy.h"
37#include "../autoreplace_base.h"
38#include "../roadstop_base.h"
39#include "../linkgraph/linkgraph.h"
40#include "../linkgraph/linkgraphjob.h"
41#include "../statusbar_gui.h"
42#include "../fileio_func.h"
43#include "../gamelog.h"
44#include "../string_func.h"
45#include "../fios.h"
46#include "../error.h"
47#include "../strings_type.h"
48#include "../newgrf_railtype.h"
49#include "../newgrf_roadtype.h"
50#include "../settings_internal.h"
51#include "saveload_internal.h"
52#include "saveload_filter.h"
53
54#include <atomic>
55#ifdef __EMSCRIPTEN__
56# include <emscripten.h>
57#endif
58
59#include "table/strings.h"
60
61#include "../safeguards.h"
62
64
67
68uint32_t _ttdp_version;
71std::string _savegame_format;
73
82
83enum NeedLength : uint8_t {
84 NL_NONE = 0,
87};
88
90static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
91
93struct ReadBuffer {
95 uint8_t *bufp = nullptr;
96 uint8_t *bufe = nullptr;
97 std::shared_ptr<LoadFilter> reader{};
98 size_t read = 0;
99
104 ReadBuffer(std::shared_ptr<LoadFilter> reader) : reader(std::move(reader))
105 {
106 }
107
108 inline uint8_t ReadByte()
109 {
110 if (this->bufp == this->bufe) {
111 size_t len = this->reader->Read(this->buf, lengthof(this->buf));
112 if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
113
114 this->read += len;
115 this->bufp = this->buf;
116 this->bufe = this->buf + len;
117 }
118
119 return *this->bufp++;
120 }
121
126 size_t GetSize() const
127 {
128 return this->read - (this->bufe - this->bufp);
129 }
130};
131
132
135 std::vector<std::unique_ptr<uint8_t[]>> blocks{};
136 uint8_t *buf = nullptr;
137 uint8_t *bufe = nullptr;
138
143 inline void WriteByte(uint8_t b)
144 {
145 /* Are we at the end of this chunk? */
146 if (this->buf == this->bufe) {
147 this->buf = this->blocks.emplace_back(std::make_unique<uint8_t[]>(MEMORY_CHUNK_SIZE)).get();
148 this->bufe = this->buf + MEMORY_CHUNK_SIZE;
149 }
150
151 *this->buf++ = b;
152 }
153
158 void Flush(std::shared_ptr<SaveFilter> writer)
159 {
160 uint i = 0;
161 size_t t = this->GetSize();
162
163 while (t > 0) {
164 size_t to_write = std::min(MEMORY_CHUNK_SIZE, t);
165
166 writer->Write(this->blocks[i++].get(), to_write);
167 t -= to_write;
168 }
169
170 writer->Finish();
171 }
172
177 size_t GetSize() const
178 {
179 return this->blocks.size() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
180 }
181};
182
187 uint8_t block_mode;
188 bool error;
189
190 size_t obj_len;
191 int array_index, last_array_index;
193
194 std::unique_ptr<MemoryDumper> dumper;
195 std::shared_ptr<SaveFilter> sf;
196
197 std::unique_ptr<ReadBuffer> reader;
198 std::shared_ptr<LoadFilter> lf;
199
201 std::string extra_msg;
202
204};
205
207
208static const std::vector<ChunkHandlerRef> &ChunkHandlers()
209{
210 /* These define the chunks */
211 extern const ChunkHandlerTable _gamelog_chunk_handlers;
212 extern const ChunkHandlerTable _map_chunk_handlers;
213 extern const ChunkHandlerTable _misc_chunk_handlers;
214 extern const ChunkHandlerTable _name_chunk_handlers;
215 extern const ChunkHandlerTable _cheat_chunk_handlers;
216 extern const ChunkHandlerTable _setting_chunk_handlers;
217 extern const ChunkHandlerTable _company_chunk_handlers;
218 extern const ChunkHandlerTable _engine_chunk_handlers;
219 extern const ChunkHandlerTable _veh_chunk_handlers;
220 extern const ChunkHandlerTable _waypoint_chunk_handlers;
221 extern const ChunkHandlerTable _depot_chunk_handlers;
222 extern const ChunkHandlerTable _order_chunk_handlers;
223 extern const ChunkHandlerTable _town_chunk_handlers;
224 extern const ChunkHandlerTable _sign_chunk_handlers;
225 extern const ChunkHandlerTable _station_chunk_handlers;
226 extern const ChunkHandlerTable _industry_chunk_handlers;
227 extern const ChunkHandlerTable _economy_chunk_handlers;
228 extern const ChunkHandlerTable _subsidy_chunk_handlers;
229 extern const ChunkHandlerTable _cargomonitor_chunk_handlers;
230 extern const ChunkHandlerTable _goal_chunk_handlers;
231 extern const ChunkHandlerTable _story_page_chunk_handlers;
232 extern const ChunkHandlerTable _league_chunk_handlers;
233 extern const ChunkHandlerTable _ai_chunk_handlers;
234 extern const ChunkHandlerTable _game_chunk_handlers;
235 extern const ChunkHandlerTable _animated_tile_chunk_handlers;
236 extern const ChunkHandlerTable _newgrf_chunk_handlers;
237 extern const ChunkHandlerTable _group_chunk_handlers;
238 extern const ChunkHandlerTable _cargopacket_chunk_handlers;
239 extern const ChunkHandlerTable _autoreplace_chunk_handlers;
240 extern const ChunkHandlerTable _labelmaps_chunk_handlers;
241 extern const ChunkHandlerTable _linkgraph_chunk_handlers;
242 extern const ChunkHandlerTable _airport_chunk_handlers;
243 extern const ChunkHandlerTable _object_chunk_handlers;
244 extern const ChunkHandlerTable _persistent_storage_chunk_handlers;
245 extern const ChunkHandlerTable _water_region_chunk_handlers;
246 extern const ChunkHandlerTable _randomizer_chunk_handlers;
247
249 static const ChunkHandlerTable _chunk_handler_tables[] = {
250 _gamelog_chunk_handlers,
251 _map_chunk_handlers,
252 _misc_chunk_handlers,
253 _name_chunk_handlers,
254 _cheat_chunk_handlers,
255 _setting_chunk_handlers,
256 _veh_chunk_handlers,
257 _waypoint_chunk_handlers,
258 _depot_chunk_handlers,
259 _order_chunk_handlers,
260 _industry_chunk_handlers,
261 _economy_chunk_handlers,
262 _subsidy_chunk_handlers,
263 _cargomonitor_chunk_handlers,
264 _goal_chunk_handlers,
265 _story_page_chunk_handlers,
266 _league_chunk_handlers,
267 _engine_chunk_handlers,
268 _town_chunk_handlers,
269 _sign_chunk_handlers,
270 _station_chunk_handlers,
271 _company_chunk_handlers,
272 _ai_chunk_handlers,
273 _game_chunk_handlers,
274 _animated_tile_chunk_handlers,
275 _newgrf_chunk_handlers,
276 _group_chunk_handlers,
277 _cargopacket_chunk_handlers,
278 _autoreplace_chunk_handlers,
279 _labelmaps_chunk_handlers,
280 _linkgraph_chunk_handlers,
281 _airport_chunk_handlers,
282 _object_chunk_handlers,
283 _persistent_storage_chunk_handlers,
284 _water_region_chunk_handlers,
285 _randomizer_chunk_handlers,
286 };
287
288 static std::vector<ChunkHandlerRef> _chunk_handlers;
289
290 if (_chunk_handlers.empty()) {
291 for (auto &chunk_handler_table : _chunk_handler_tables) {
292 for (auto &chunk_handler : chunk_handler_table) {
293 _chunk_handlers.push_back(chunk_handler);
294 }
295 }
296 }
297
298 return _chunk_handlers;
299}
300
302static void SlNullPointers()
303{
305
306 /* We don't want any savegame conversion code to run
307 * during NULLing; especially those that try to get
308 * pointers from other pools. */
310
311 for (const ChunkHandler &ch : ChunkHandlers()) {
312 Debug(sl, 3, "Nulling pointers for {}", ch.GetName());
313 ch.FixPointers();
314 }
315
316 assert(_sl.action == SLA_NULL);
317}
318
327[[noreturn]] void SlError(StringID string, const std::string &extra_msg)
328{
329 /* Distinguish between loading into _load_check_data vs. normal save/load. */
330 if (_sl.action == SLA_LOAD_CHECK) {
331 _load_check_data.error = string;
332 _load_check_data.error_msg = extra_msg;
333 } else {
334 _sl.error_str = string;
335 _sl.extra_msg = extra_msg;
336 }
337
338 /* We have to nullptr all pointers here; we might be in a state where
339 * the pointers are actually filled with indices, which means that
340 * when we access them during cleaning the pool dereferences of
341 * those indices will be made with segmentation faults as result. */
343
344 /* Logging could be active. */
345 _gamelog.StopAnyAction();
346
347 throw std::exception();
348}
349
357[[noreturn]] void SlErrorCorrupt(const std::string &msg)
358{
359 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
360}
361
362
363typedef void (*AsyncSaveFinishProc)();
364static std::atomic<AsyncSaveFinishProc> _async_save_finish;
365static std::thread _save_thread;
366
372{
373 if (_exit_game) return;
374 while (_async_save_finish.load(std::memory_order_acquire) != nullptr) CSleep(10);
375
376 _async_save_finish.store(proc, std::memory_order_release);
377}
378
383{
384 AsyncSaveFinishProc proc = _async_save_finish.exchange(nullptr, std::memory_order_acq_rel);
385 if (proc == nullptr) return;
386
387 proc();
388
389 if (_save_thread.joinable()) {
390 _save_thread.join();
391 }
392}
393
398uint8_t SlReadByte()
399{
400 return _sl.reader->ReadByte();
401}
402
407void SlWriteByte(uint8_t b)
408{
409 _sl.dumper->WriteByte(b);
410}
411
412static inline int SlReadUint16()
413{
414 int x = SlReadByte() << 8;
415 return x | SlReadByte();
416}
417
418static inline uint32_t SlReadUint32()
419{
420 uint32_t x = SlReadUint16() << 16;
421 return x | SlReadUint16();
422}
423
424static inline uint64_t SlReadUint64()
425{
426 uint32_t x = SlReadUint32();
427 uint32_t y = SlReadUint32();
428 return (uint64_t)x << 32 | y;
429}
430
431static inline void SlWriteUint16(uint16_t v)
432{
433 SlWriteByte(GB(v, 8, 8));
434 SlWriteByte(GB(v, 0, 8));
435}
436
437static inline void SlWriteUint32(uint32_t v)
438{
439 SlWriteUint16(GB(v, 16, 16));
440 SlWriteUint16(GB(v, 0, 16));
441}
442
443static inline void SlWriteUint64(uint64_t x)
444{
445 SlWriteUint32((uint32_t)(x >> 32));
446 SlWriteUint32((uint32_t)x);
447}
448
458static uint SlReadSimpleGamma()
459{
460 uint i = SlReadByte();
461 if (HasBit(i, 7)) {
462 i &= ~0x80;
463 if (HasBit(i, 6)) {
464 i &= ~0x40;
465 if (HasBit(i, 5)) {
466 i &= ~0x20;
467 if (HasBit(i, 4)) {
468 i &= ~0x10;
469 if (HasBit(i, 3)) {
470 SlErrorCorrupt("Unsupported gamma");
471 }
472 i = SlReadByte(); // 32 bits only.
473 }
474 i = (i << 8) | SlReadByte();
475 }
476 i = (i << 8) | SlReadByte();
477 }
478 i = (i << 8) | SlReadByte();
479 }
480 return i;
481}
482
500static void SlWriteSimpleGamma(size_t i)
501{
502 if (i >= (1 << 7)) {
503 if (i >= (1 << 14)) {
504 if (i >= (1 << 21)) {
505 if (i >= (1 << 28)) {
506 assert(i <= UINT32_MAX); // We can only support 32 bits for now.
507 SlWriteByte((uint8_t)(0xF0));
508 SlWriteByte((uint8_t)(i >> 24));
509 } else {
510 SlWriteByte((uint8_t)(0xE0 | (i >> 24)));
511 }
512 SlWriteByte((uint8_t)(i >> 16));
513 } else {
514 SlWriteByte((uint8_t)(0xC0 | (i >> 16)));
515 }
516 SlWriteByte((uint8_t)(i >> 8));
517 } else {
518 SlWriteByte((uint8_t)(0x80 | (i >> 8)));
519 }
520 }
521 SlWriteByte((uint8_t)i);
522}
523
525static inline uint SlGetGammaLength(size_t i)
526{
527 return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
528}
529
530static inline uint SlReadSparseIndex()
531{
532 return SlReadSimpleGamma();
533}
534
535static inline void SlWriteSparseIndex(uint index)
536{
537 SlWriteSimpleGamma(index);
538}
539
540static inline uint SlReadArrayLength()
541{
542 return SlReadSimpleGamma();
543}
544
545static inline void SlWriteArrayLength(size_t length)
546{
547 SlWriteSimpleGamma(length);
548}
549
550static inline uint SlGetArrayLength(size_t length)
551{
552 return SlGetGammaLength(length);
553}
554
558static uint8_t GetSavegameFileType(const SaveLoad &sld)
559{
560 switch (sld.cmd) {
561 case SL_VAR:
562 return GetVarFileType(sld.conv); break;
563
564 case SL_STDSTR:
565 case SL_ARR:
566 case SL_VECTOR:
567 case SL_DEQUE:
568 return GetVarFileType(sld.conv) | SLE_FILE_HAS_LENGTH_FIELD; break;
569
570 case SL_REF:
571 return IsSavegameVersionBefore(SLV_69) ? SLE_FILE_U16 : SLE_FILE_U32;
572
573 case SL_REFLIST:
574 case SL_REFVECTOR:
575 return (IsSavegameVersionBefore(SLV_69) ? SLE_FILE_U16 : SLE_FILE_U32) | SLE_FILE_HAS_LENGTH_FIELD;
576
577 case SL_SAVEBYTE:
578 return SLE_FILE_U8;
579
580 case SL_STRUCT:
581 case SL_STRUCTLIST:
582 return SLE_FILE_STRUCT | SLE_FILE_HAS_LENGTH_FIELD;
583
584 default: NOT_REACHED();
585 }
586}
587
594static inline uint SlCalcConvMemLen(VarType conv)
595{
596 switch (GetVarMemType(conv)) {
597 case SLE_VAR_BL: return sizeof(bool);
598 case SLE_VAR_I8: return sizeof(int8_t);
599 case SLE_VAR_U8: return sizeof(uint8_t);
600 case SLE_VAR_I16: return sizeof(int16_t);
601 case SLE_VAR_U16: return sizeof(uint16_t);
602 case SLE_VAR_I32: return sizeof(int32_t);
603 case SLE_VAR_U32: return sizeof(uint32_t);
604 case SLE_VAR_I64: return sizeof(int64_t);
605 case SLE_VAR_U64: return sizeof(uint64_t);
606 case SLE_VAR_NULL: return 0;
607
608 case SLE_VAR_STR:
609 case SLE_VAR_STRQ:
610 return SlReadArrayLength();
611
612 case SLE_VAR_NAME:
613 default:
614 NOT_REACHED();
615 }
616}
617
624static inline uint8_t SlCalcConvFileLen(VarType conv)
625{
626 switch (GetVarFileType(conv)) {
627 case SLE_FILE_END: return 0;
628 case SLE_FILE_I8: return sizeof(int8_t);
629 case SLE_FILE_U8: return sizeof(uint8_t);
630 case SLE_FILE_I16: return sizeof(int16_t);
631 case SLE_FILE_U16: return sizeof(uint16_t);
632 case SLE_FILE_I32: return sizeof(int32_t);
633 case SLE_FILE_U32: return sizeof(uint32_t);
634 case SLE_FILE_I64: return sizeof(int64_t);
635 case SLE_FILE_U64: return sizeof(uint64_t);
636 case SLE_FILE_STRINGID: return sizeof(uint16_t);
637
638 case SLE_FILE_STRING:
639 return SlReadArrayLength();
640
641 case SLE_FILE_STRUCT:
642 default:
643 NOT_REACHED();
644 }
645}
646
648static inline size_t SlCalcRefLen()
649{
650 return IsSavegameVersionBefore(SLV_69) ? 2 : 4;
651}
652
653void SlSetArrayIndex(uint index)
654{
656 _sl.array_index = index;
657}
658
659static size_t _next_offs;
660
666{
667 /* After reading in the whole array inside the loop
668 * we must have read in all the data, so we must be at end of current block. */
669 if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) {
670 SlErrorCorruptFmt("Invalid chunk size iterating array - expected to be at position {}, actually at {}", _next_offs, _sl.reader->GetSize());
671 }
672
673 for (;;) {
674 uint length = SlReadArrayLength();
675 if (length == 0) {
676 assert(!_sl.expect_table_header);
677 _next_offs = 0;
678 return -1;
679 }
680
681 _sl.obj_len = --length;
682 _next_offs = _sl.reader->GetSize() + length;
683
685 _sl.expect_table_header = false;
686 return INT32_MAX;
687 }
688
689 int index;
690 switch (_sl.block_mode) {
691 case CH_SPARSE_TABLE:
692 case CH_SPARSE_ARRAY: index = (int)SlReadSparseIndex(); break;
693 case CH_TABLE:
694 case CH_ARRAY: index = _sl.array_index++; break;
695 default:
696 Debug(sl, 0, "SlIterateArray error");
697 return -1; // error
698 }
699
700 if (length != 0) return index;
701 }
702}
703
708{
709 while (SlIterateArray() != -1) {
710 SlSkipBytes(_next_offs - _sl.reader->GetSize());
711 }
712}
713
719void SlSetLength(size_t length)
720{
721 assert(_sl.action == SLA_SAVE);
722
723 switch (_sl.need_length) {
724 case NL_WANTLENGTH:
726 if ((_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE) && _sl.expect_table_header) {
727 _sl.expect_table_header = false;
728 SlWriteArrayLength(length + 1);
729 break;
730 }
731
732 switch (_sl.block_mode) {
733 case CH_RIFF:
734 /* Ugly encoding of >16M RIFF chunks
735 * The lower 24 bits are normal
736 * The uppermost 4 bits are bits 24:27 */
737 assert(length < (1 << 28));
738 SlWriteUint32((uint32_t)((length & 0xFFFFFF) | ((length >> 24) << 28)));
739 break;
740 case CH_TABLE:
741 case CH_ARRAY:
742 assert(_sl.last_array_index <= _sl.array_index);
743 while (++_sl.last_array_index <= _sl.array_index) {
744 SlWriteArrayLength(1);
745 }
746 SlWriteArrayLength(length + 1);
747 break;
748 case CH_SPARSE_TABLE:
749 case CH_SPARSE_ARRAY:
750 SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
751 SlWriteSparseIndex(_sl.array_index);
752 break;
753 default: NOT_REACHED();
754 }
755 break;
756
757 case NL_CALCLENGTH:
758 _sl.obj_len += (int)length;
759 break;
760
761 default: NOT_REACHED();
762 }
763}
764
771static void SlCopyBytes(void *ptr, size_t length)
772{
773 uint8_t *p = (uint8_t *)ptr;
774
775 switch (_sl.action) {
776 case SLA_LOAD_CHECK:
777 case SLA_LOAD:
778 for (; length != 0; length--) *p++ = SlReadByte();
779 break;
780 case SLA_SAVE:
781 for (; length != 0; length--) SlWriteByte(*p++);
782 break;
783 default: NOT_REACHED();
784 }
785}
786
789{
790 return _sl.obj_len;
791}
792
800int64_t ReadValue(const void *ptr, VarType conv)
801{
802 switch (GetVarMemType(conv)) {
803 case SLE_VAR_BL: return (*(const bool *)ptr != 0);
804 case SLE_VAR_I8: return *(const int8_t *)ptr;
805 case SLE_VAR_U8: return *(const uint8_t *)ptr;
806 case SLE_VAR_I16: return *(const int16_t *)ptr;
807 case SLE_VAR_U16: return *(const uint16_t*)ptr;
808 case SLE_VAR_I32: return *(const int32_t *)ptr;
809 case SLE_VAR_U32: return *(const uint32_t*)ptr;
810 case SLE_VAR_I64: return *(const int64_t *)ptr;
811 case SLE_VAR_U64: return *(const uint64_t*)ptr;
812 case SLE_VAR_NULL:return 0;
813 default: NOT_REACHED();
814 }
815}
816
824void WriteValue(void *ptr, VarType conv, int64_t val)
825{
826 switch (GetVarMemType(conv)) {
827 case SLE_VAR_BL: *(bool *)ptr = (val != 0); break;
828 case SLE_VAR_I8: *(int8_t *)ptr = val; break;
829 case SLE_VAR_U8: *(uint8_t *)ptr = val; break;
830 case SLE_VAR_I16: *(int16_t *)ptr = val; break;
831 case SLE_VAR_U16: *(uint16_t*)ptr = val; break;
832 case SLE_VAR_I32: *(int32_t *)ptr = val; break;
833 case SLE_VAR_U32: *(uint32_t*)ptr = val; break;
834 case SLE_VAR_I64: *(int64_t *)ptr = val; break;
835 case SLE_VAR_U64: *(uint64_t*)ptr = val; break;
836 case SLE_VAR_NAME: *reinterpret_cast<std::string *>(ptr) = CopyFromOldName(val); break;
837 case SLE_VAR_NULL: break;
838 default: NOT_REACHED();
839 }
840}
841
850static void SlSaveLoadConv(void *ptr, VarType conv)
851{
852 switch (_sl.action) {
853 case SLA_SAVE: {
854 int64_t x = ReadValue(ptr, conv);
855
856 /* Write the value to the file and check if its value is in the desired range */
857 switch (GetVarFileType(conv)) {
858 case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break;
859 case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break;
860 case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break;
862 case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break;
863 case SLE_FILE_I32:
864 case SLE_FILE_U32: SlWriteUint32((uint32_t)x);break;
865 case SLE_FILE_I64:
866 case SLE_FILE_U64: SlWriteUint64(x);break;
867 default: NOT_REACHED();
868 }
869 break;
870 }
871 case SLA_LOAD_CHECK:
872 case SLA_LOAD: {
873 int64_t x;
874 /* Read a value from the file */
875 switch (GetVarFileType(conv)) {
876 case SLE_FILE_I8: x = (int8_t )SlReadByte(); break;
877 case SLE_FILE_U8: x = (uint8_t )SlReadByte(); break;
878 case SLE_FILE_I16: x = (int16_t )SlReadUint16(); break;
879 case SLE_FILE_U16: x = (uint16_t)SlReadUint16(); break;
880 case SLE_FILE_I32: x = (int32_t )SlReadUint32(); break;
881 case SLE_FILE_U32: x = (uint32_t)SlReadUint32(); break;
882 case SLE_FILE_I64: x = (int64_t )SlReadUint64(); break;
883 case SLE_FILE_U64: x = (uint64_t)SlReadUint64(); break;
884 case SLE_FILE_STRINGID: x = RemapOldStringID((uint16_t)SlReadUint16()); break;
885 default: NOT_REACHED();
886 }
887
888 /* Write The value to the struct. These ARE endian safe. */
889 WriteValue(ptr, conv, x);
890 break;
891 }
892 case SLA_PTRS: break;
893 case SLA_NULL: break;
894 default: NOT_REACHED();
895 }
896}
897
905static inline size_t SlCalcStdStringLen(const void *ptr)
906{
907 const std::string *str = reinterpret_cast<const std::string *>(ptr);
908
909 size_t len = str->length();
910 return len + SlGetArrayLength(len); // also include the length of the index
911}
912
913
921void FixSCCEncoded(std::string &str, bool fix_code)
922{
923 if (str.empty()) return;
924
925 /* We need to convert from old escape-style encoding to record separator encoding.
926 * Initial `<SCC_ENCODED><STRINGID>` stays the same.
927 *
928 * `:<SCC_ENCODED><STRINGID>` becomes `<RS><SCC_ENCODED><STRINGID>`
929 * `:<HEX>` becomes `<RS><SCC_ENCODED_NUMERIC><HEX>`
930 * `:"<STRING>"` becomes `<RS><SCC_ENCODED_STRING><STRING>`
931 */
932 std::string result;
933 StringBuilder builder(result);
934
935 bool is_encoded = false; // Set if we determine by the presence of SCC_ENCODED that the string is an encoded string.
936 bool in_string = false; // Set if we in a string, between double-quotes.
937 bool need_type = true; // Set if a parameter type needs to be emitted.
938
939 StringConsumer consumer(str);
940 while (consumer.AnyBytesLeft()) {
941 char32_t c;
942 if (auto r = consumer.TryReadUtf8(); r.has_value()) {
943 c = *r;
944 } else {
945 break;
946 }
947 if (c == SCC_ENCODED || (fix_code && (c == 0xE028 || c == 0xE02A))) {
948 builder.PutUtf8(SCC_ENCODED);
949 need_type = false;
950 is_encoded = true;
951 continue;
952 }
953
954 /* If the first character is not SCC_ENCODED then we don't have to do any conversion. */
955 if (!is_encoded) return;
956
957 if (c == '"') {
958 in_string = !in_string;
959 if (in_string && need_type) {
960 /* Started a new string parameter. */
962 need_type = false;
963 }
964 continue;
965 }
966
967 if (!in_string && c == ':') {
968 builder.PutUtf8(SCC_RECORD_SEPARATOR);
969 need_type = true;
970 continue;
971 }
972 if (need_type) {
973 /* Started a new numeric parameter. */
975 need_type = false;
976 }
977
978 builder.PutUtf8(c);
979 }
980
981 str = std::move(result);
982}
983
988void FixSCCEncodedNegative(std::string &str)
989{
990 if (str.empty()) return;
991
992 StringConsumer consumer(str);
993
994 /* Check whether this is an encoded string */
995 if (!consumer.ReadUtf8If(SCC_ENCODED)) return;
996
997 std::string result;
998 StringBuilder builder(result);
999 builder.PutUtf8(SCC_ENCODED);
1000 while (consumer.AnyBytesLeft()) {
1001 /* Copy until next record */
1002 builder.Put(consumer.ReadUntilUtf8(SCC_RECORD_SEPARATOR, StringConsumer::READ_ONE_SEPARATOR));
1003
1004 /* Check whether this is a numeric parameter */
1005 if (!consumer.ReadUtf8If(SCC_ENCODED_NUMERIC)) continue;
1007
1008 /* First try unsigned */
1009 if (auto u = consumer.TryReadIntegerBase<uint64_t>(16); u.has_value()) {
1010 builder.PutIntegerBase<uint64_t>(*u, 16);
1011 } else {
1012 /* Read as signed, store as unsigned */
1013 auto s = consumer.ReadIntegerBase<int64_t>(16);
1014 builder.PutIntegerBase<uint64_t>(static_cast<uint64_t>(s), 16);
1015 }
1016 }
1017
1018 str = std::move(result);
1019}
1020
1027void SlReadString(std::string &str, size_t length)
1028{
1029 str.resize(length);
1030 SlCopyBytes(str.data(), length);
1031}
1032
1038static void SlStdString(void *ptr, VarType conv)
1039{
1040 std::string *str = reinterpret_cast<std::string *>(ptr);
1041
1042 switch (_sl.action) {
1043 case SLA_SAVE: {
1044 size_t len = str->length();
1045 SlWriteArrayLength(len);
1046 SlCopyBytes(const_cast<void *>(static_cast<const void *>(str->data())), len);
1047 break;
1048 }
1049
1050 case SLA_LOAD_CHECK:
1051 case SLA_LOAD: {
1052 size_t len = SlReadArrayLength();
1053 if (GetVarMemType(conv) == SLE_VAR_NULL) {
1054 SlSkipBytes(len);
1055 return;
1056 }
1057
1058 SlReadString(*str, len);
1059
1061 if ((conv & SLF_ALLOW_CONTROL) != 0) {
1065 }
1066 if ((conv & SLF_ALLOW_NEWLINE) != 0) {
1068 }
1069 if ((conv & SLF_REPLACE_TABCRLF) != 0) {
1071 }
1073 }
1074
1075 case SLA_PTRS: break;
1076 case SLA_NULL: break;
1077 default: NOT_REACHED();
1078 }
1079}
1080
1089static void SlCopyInternal(void *object, size_t length, VarType conv)
1090{
1091 if (GetVarMemType(conv) == SLE_VAR_NULL) {
1092 assert(_sl.action != SLA_SAVE); // Use SL_NULL if you want to write null-bytes
1093 SlSkipBytes(length * SlCalcConvFileLen(conv));
1094 return;
1095 }
1096
1097 /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1098 * as a byte-type. So detect this, and adjust object size accordingly */
1099 if (_sl.action != SLA_SAVE && _sl_version == 0) {
1100 /* all objects except difficulty settings */
1101 if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
1102 conv == SLE_INT32 || conv == SLE_UINT32) {
1103 SlCopyBytes(object, length * SlCalcConvFileLen(conv));
1104 return;
1105 }
1106 /* used for conversion of Money 32bit->64bit */
1107 if (conv == (SLE_FILE_I32 | SLE_VAR_I64)) {
1108 for (uint i = 0; i < length; i++) {
1109 ((int64_t*)object)[i] = (int32_t)std::byteswap(SlReadUint32());
1110 }
1111 return;
1112 }
1113 }
1114
1115 /* If the size of elements is 1 byte both in file and memory, no special
1116 * conversion is needed, use specialized copy-copy function to speed up things */
1117 if (conv == SLE_INT8 || conv == SLE_UINT8) {
1118 SlCopyBytes(object, length);
1119 } else {
1120 uint8_t *a = (uint8_t*)object;
1121 uint8_t mem_size = SlCalcConvMemLen(conv);
1122
1123 for (; length != 0; length --) {
1124 SlSaveLoadConv(a, conv);
1125 a += mem_size; // get size
1126 }
1127 }
1128}
1129
1138void SlCopy(void *object, size_t length, VarType conv)
1139{
1140 if (_sl.action == SLA_PTRS || _sl.action == SLA_NULL) return;
1141
1142 /* Automatically calculate the length? */
1143 if (_sl.need_length != NL_NONE) {
1144 SlSetLength(length * SlCalcConvFileLen(conv));
1145 /* Determine length only? */
1146 if (_sl.need_length == NL_CALCLENGTH) return;
1147 }
1148
1149 SlCopyInternal(object, length, conv);
1150}
1151
1157static inline size_t SlCalcArrayLen(size_t length, VarType conv)
1158{
1159 return SlCalcConvFileLen(conv) * length + SlGetArrayLength(length);
1160}
1161
1168static void SlArray(void *array, size_t length, VarType conv)
1169{
1170 switch (_sl.action) {
1171 case SLA_SAVE:
1172 SlWriteArrayLength(length);
1173 SlCopyInternal(array, length, conv);
1174 return;
1175
1176 case SLA_LOAD_CHECK:
1177 case SLA_LOAD: {
1179 size_t sv_length = SlReadArrayLength();
1180 if (GetVarMemType(conv) == SLE_VAR_NULL) {
1181 /* We don't know this field, so we assume the length in the savegame is correct. */
1182 length = sv_length;
1183 } else if (sv_length != length) {
1184 /* If the SLE_ARR changes size, a savegame bump is required
1185 * and the developer should have written conversion lines.
1186 * Error out to make this more visible. */
1187 SlErrorCorrupt("Fixed-length array is of wrong length");
1188 }
1189 }
1190
1191 SlCopyInternal(array, length, conv);
1192 return;
1193 }
1194
1195 case SLA_PTRS:
1196 case SLA_NULL:
1197 return;
1198
1199 default:
1200 NOT_REACHED();
1201 }
1202}
1203
1214static size_t ReferenceToInt(const void *obj, SLRefType rt)
1215{
1216 assert(_sl.action == SLA_SAVE);
1217
1218 if (obj == nullptr) return 0;
1219
1220 switch (rt) {
1221 case REF_VEHICLE_OLD: // Old vehicles we save as new ones
1222 case REF_VEHICLE: return ((const Vehicle*)obj)->index + 1;
1223 case REF_STATION: return ((const Station*)obj)->index + 1;
1224 case REF_TOWN: return ((const Town*)obj)->index + 1;
1225 case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
1226 case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
1227 case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
1228 case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
1229 case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
1230 case REF_LINK_GRAPH: return ((const LinkGraph*)obj)->index + 1;
1231 case REF_LINK_GRAPH_JOB: return ((const LinkGraphJob*)obj)->index + 1;
1232 default: NOT_REACHED();
1233 }
1234}
1235
1246static void *IntToReference(size_t index, SLRefType rt)
1247{
1248 static_assert(sizeof(size_t) <= sizeof(void *));
1249
1250 assert(_sl.action == SLA_PTRS);
1251
1252 /* After version 4.3 REF_VEHICLE_OLD is saved as REF_VEHICLE,
1253 * and should be loaded like that */
1254 if (rt == REF_VEHICLE_OLD && !IsSavegameVersionBefore(SLV_4, 4)) {
1255 rt = REF_VEHICLE;
1256 }
1257
1258 /* No need to look up nullptr pointers, just return immediately */
1259 if (index == (rt == REF_VEHICLE_OLD ? 0xFFFF : 0)) return nullptr;
1260
1261 /* Correct index. Old vehicles were saved differently:
1262 * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1263 if (rt != REF_VEHICLE_OLD) index--;
1264
1265 switch (rt) {
1266 case REF_ORDERLIST:
1267 if (OrderList::IsValidID(index)) return OrderList::Get(index);
1268 SlErrorCorrupt("Referencing invalid OrderList");
1269
1270 case REF_VEHICLE_OLD:
1271 case REF_VEHICLE:
1272 if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1273 SlErrorCorrupt("Referencing invalid Vehicle");
1274
1275 case REF_STATION:
1276 if (Station::IsValidID(index)) return Station::Get(index);
1277 SlErrorCorrupt("Referencing invalid Station");
1278
1279 case REF_TOWN:
1280 if (Town::IsValidID(index)) return Town::Get(index);
1281 SlErrorCorrupt("Referencing invalid Town");
1282
1283 case REF_ROADSTOPS:
1284 if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1285 SlErrorCorrupt("Referencing invalid RoadStop");
1286
1287 case REF_ENGINE_RENEWS:
1288 if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1289 SlErrorCorrupt("Referencing invalid EngineRenew");
1290
1291 case REF_CARGO_PACKET:
1292 if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1293 SlErrorCorrupt("Referencing invalid CargoPacket");
1294
1295 case REF_STORAGE:
1296 if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1297 SlErrorCorrupt("Referencing invalid PersistentStorage");
1298
1299 case REF_LINK_GRAPH:
1300 if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1301 SlErrorCorrupt("Referencing invalid LinkGraph");
1302
1303 case REF_LINK_GRAPH_JOB:
1304 if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1305 SlErrorCorrupt("Referencing invalid LinkGraphJob");
1306
1307 default: NOT_REACHED();
1308 }
1309}
1310
1316void SlSaveLoadRef(void *ptr, VarType conv)
1317{
1318 switch (_sl.action) {
1319 case SLA_SAVE:
1320 SlWriteUint32((uint32_t)ReferenceToInt(*(void **)ptr, (SLRefType)conv));
1321 break;
1322 case SLA_LOAD_CHECK:
1323 case SLA_LOAD:
1324 *(size_t *)ptr = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
1325 break;
1326 case SLA_PTRS:
1327 *(void **)ptr = IntToReference(*(size_t *)ptr, (SLRefType)conv);
1328 break;
1329 case SLA_NULL:
1330 *(void **)ptr = nullptr;
1331 break;
1332 default: NOT_REACHED();
1333 }
1334}
1335
1339template <template <typename, typename> typename Tstorage, typename Tvar, typename Tallocator = std::allocator<Tvar>>
1341 typedef Tstorage<Tvar, Tallocator> SlStorageT;
1342public:
1349 static size_t SlCalcLen(const void *storage, VarType conv, SaveLoadType cmd = SL_VAR)
1350 {
1351 assert(cmd == SL_VAR || cmd == SL_REF);
1352
1353 const SlStorageT *list = static_cast<const SlStorageT *>(storage);
1354
1355 int type_size = SlGetArrayLength(list->size());
1356 int item_size = SlCalcConvFileLen(cmd == SL_VAR ? conv : (VarType)SLE_FILE_U32);
1357 return list->size() * item_size + type_size;
1358 }
1359
1360 static void SlSaveLoadMember(SaveLoadType cmd, Tvar *item, VarType conv)
1361 {
1362 switch (cmd) {
1363 case SL_VAR: SlSaveLoadConv(item, conv); break;
1364 case SL_REF: SlSaveLoadRef(item, conv); break;
1365 case SL_STDSTR: SlStdString(item, conv); break;
1366 default:
1367 NOT_REACHED();
1368 }
1369 }
1370
1377 static void SlSaveLoad(void *storage, VarType conv, SaveLoadType cmd = SL_VAR)
1378 {
1379 assert(cmd == SL_VAR || cmd == SL_REF || cmd == SL_STDSTR);
1380
1381 SlStorageT *list = static_cast<SlStorageT *>(storage);
1382
1383 switch (_sl.action) {
1384 case SLA_SAVE:
1385 SlWriteArrayLength(list->size());
1386
1387 for (auto &item : *list) {
1388 SlSaveLoadMember(cmd, &item, conv);
1389 }
1390 break;
1391
1392 case SLA_LOAD_CHECK:
1393 case SLA_LOAD: {
1394 size_t length;
1395 switch (cmd) {
1396 case SL_VAR: length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? SlReadUint32() : SlReadArrayLength(); break;
1397 case SL_REF: length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? SlReadUint32() : SlReadArrayLength(); break;
1398 case SL_STDSTR: length = SlReadArrayLength(); break;
1399 default: NOT_REACHED();
1400 }
1401
1402 list->clear();
1403 if constexpr (std::is_same_v<SlStorageT, std::vector<Tvar, Tallocator>>) {
1404 list->reserve(length);
1405 }
1406
1407 /* Load each value and push to the end of the storage. */
1408 for (size_t i = 0; i < length; i++) {
1409 Tvar &data = list->emplace_back();
1410 SlSaveLoadMember(cmd, &data, conv);
1411 }
1412 break;
1413 }
1414
1415 case SLA_PTRS:
1416 for (auto &item : *list) {
1417 SlSaveLoadMember(cmd, &item, conv);
1418 }
1419 break;
1420
1421 case SLA_NULL:
1422 list->clear();
1423 break;
1424
1425 default: NOT_REACHED();
1426 }
1427 }
1428};
1429
1435static inline size_t SlCalcRefListLen(const void *list, VarType conv)
1436{
1438}
1439
1445static void SlRefList(void *list, VarType conv)
1446{
1447 /* Automatically calculate the length? */
1448 if (_sl.need_length != NL_NONE) {
1449 SlSetLength(SlCalcRefListLen(list, conv));
1450 /* Determine length only? */
1451 if (_sl.need_length == NL_CALCLENGTH) return;
1452 }
1453
1455}
1456
1462static size_t SlCalcRefVectorLen(const void *vector, VarType conv)
1463{
1465}
1466
1472static void SlRefVector(void *vector, VarType conv)
1473{
1474 /* Automatically calculate the length? */
1475 if (_sl.need_length != NL_NONE) {
1476 SlSetLength(SlCalcRefVectorLen(vector, conv));
1477 /* Determine length only? */
1478 if (_sl.need_length == NL_CALCLENGTH) return;
1479 }
1480
1482}
1483
1489static inline size_t SlCalcDequeLen(const void *deque, VarType conv)
1490{
1491 switch (GetVarMemType(conv)) {
1492 case SLE_VAR_BL: return SlStorageHelper<std::deque, bool>::SlCalcLen(deque, conv);
1493 case SLE_VAR_I8: return SlStorageHelper<std::deque, int8_t>::SlCalcLen(deque, conv);
1494 case SLE_VAR_U8: return SlStorageHelper<std::deque, uint8_t>::SlCalcLen(deque, conv);
1495 case SLE_VAR_I16: return SlStorageHelper<std::deque, int16_t>::SlCalcLen(deque, conv);
1496 case SLE_VAR_U16: return SlStorageHelper<std::deque, uint16_t>::SlCalcLen(deque, conv);
1497 case SLE_VAR_I32: return SlStorageHelper<std::deque, int32_t>::SlCalcLen(deque, conv);
1498 case SLE_VAR_U32: return SlStorageHelper<std::deque, uint32_t>::SlCalcLen(deque, conv);
1499 case SLE_VAR_I64: return SlStorageHelper<std::deque, int64_t>::SlCalcLen(deque, conv);
1500 case SLE_VAR_U64: return SlStorageHelper<std::deque, uint64_t>::SlCalcLen(deque, conv);
1501
1502 case SLE_VAR_STR:
1503 /* Strings are a length-prefixed field type in the savegame table format,
1504 * these may not be directly stored in another length-prefixed container type. */
1505 NOT_REACHED();
1506
1507 default: NOT_REACHED();
1508 }
1509}
1510
1516static void SlDeque(void *deque, VarType conv)
1517{
1518 switch (GetVarMemType(conv)) {
1519 case SLE_VAR_BL: SlStorageHelper<std::deque, bool>::SlSaveLoad(deque, conv); break;
1520 case SLE_VAR_I8: SlStorageHelper<std::deque, int8_t>::SlSaveLoad(deque, conv); break;
1521 case SLE_VAR_U8: SlStorageHelper<std::deque, uint8_t>::SlSaveLoad(deque, conv); break;
1522 case SLE_VAR_I16: SlStorageHelper<std::deque, int16_t>::SlSaveLoad(deque, conv); break;
1523 case SLE_VAR_U16: SlStorageHelper<std::deque, uint16_t>::SlSaveLoad(deque, conv); break;
1524 case SLE_VAR_I32: SlStorageHelper<std::deque, int32_t>::SlSaveLoad(deque, conv); break;
1525 case SLE_VAR_U32: SlStorageHelper<std::deque, uint32_t>::SlSaveLoad(deque, conv); break;
1526 case SLE_VAR_I64: SlStorageHelper<std::deque, int64_t>::SlSaveLoad(deque, conv); break;
1527 case SLE_VAR_U64: SlStorageHelper<std::deque, uint64_t>::SlSaveLoad(deque, conv); break;
1528
1529 case SLE_VAR_STR:
1530 /* Strings are a length-prefixed field type in the savegame table format,
1531 * these may not be directly stored in another length-prefixed container type.
1532 * This is permitted for load-related actions, because invalid fields of this type are present
1533 * from SLV_COMPANY_ALLOW_LIST up to SLV_COMPANY_ALLOW_LIST_V2. */
1534 assert(_sl.action != SLA_SAVE);
1536 break;
1537
1538 default: NOT_REACHED();
1539 }
1540}
1541
1547static inline size_t SlCalcVectorLen(const void *vector, VarType conv)
1548{
1549 switch (GetVarMemType(conv)) {
1550 case SLE_VAR_BL: NOT_REACHED(); // Not supported
1551 case SLE_VAR_I8: return SlStorageHelper<std::vector, int8_t>::SlCalcLen(vector, conv);
1552 case SLE_VAR_U8: return SlStorageHelper<std::vector, uint8_t>::SlCalcLen(vector, conv);
1553 case SLE_VAR_I16: return SlStorageHelper<std::vector, int16_t>::SlCalcLen(vector, conv);
1554 case SLE_VAR_U16: return SlStorageHelper<std::vector, uint16_t>::SlCalcLen(vector, conv);
1555 case SLE_VAR_I32: return SlStorageHelper<std::vector, int32_t>::SlCalcLen(vector, conv);
1556 case SLE_VAR_U32: return SlStorageHelper<std::vector, uint32_t>::SlCalcLen(vector, conv);
1557 case SLE_VAR_I64: return SlStorageHelper<std::vector, int64_t>::SlCalcLen(vector, conv);
1558 case SLE_VAR_U64: return SlStorageHelper<std::vector, uint64_t>::SlCalcLen(vector, conv);
1559
1560 case SLE_VAR_STR:
1561 /* Strings are a length-prefixed field type in the savegame table format,
1562 * these may not be directly stored in another length-prefixed container type. */
1563 NOT_REACHED();
1564
1565 default: NOT_REACHED();
1566 }
1567}
1568
1574static void SlVector(void *vector, VarType conv)
1575{
1576 switch (GetVarMemType(conv)) {
1577 case SLE_VAR_BL: NOT_REACHED(); // Not supported
1578 case SLE_VAR_I8: SlStorageHelper<std::vector, int8_t>::SlSaveLoad(vector, conv); break;
1579 case SLE_VAR_U8: SlStorageHelper<std::vector, uint8_t>::SlSaveLoad(vector, conv); break;
1580 case SLE_VAR_I16: SlStorageHelper<std::vector, int16_t>::SlSaveLoad(vector, conv); break;
1581 case SLE_VAR_U16: SlStorageHelper<std::vector, uint16_t>::SlSaveLoad(vector, conv); break;
1582 case SLE_VAR_I32: SlStorageHelper<std::vector, int32_t>::SlSaveLoad(vector, conv); break;
1583 case SLE_VAR_U32: SlStorageHelper<std::vector, uint32_t>::SlSaveLoad(vector, conv); break;
1584 case SLE_VAR_I64: SlStorageHelper<std::vector, int64_t>::SlSaveLoad(vector, conv); break;
1585 case SLE_VAR_U64: SlStorageHelper<std::vector, uint64_t>::SlSaveLoad(vector, conv); break;
1586
1587 case SLE_VAR_STR:
1588 /* Strings are a length-prefixed field type in the savegame table format,
1589 * these may not be directly stored in another length-prefixed container type.
1590 * This is permitted for load-related actions, because invalid fields of this type are present
1591 * from SLV_COMPANY_ALLOW_LIST up to SLV_COMPANY_ALLOW_LIST_V2. */
1592 assert(_sl.action != SLA_SAVE);
1594 break;
1595
1596 default: NOT_REACHED();
1597 }
1598}
1599
1601static inline bool SlIsObjectValidInSavegame(const SaveLoad &sld)
1602{
1603 return (_sl_version >= sld.version_from && _sl_version < sld.version_to);
1604}
1605
1611static size_t SlCalcTableHeader(const SaveLoadTable &slt)
1612{
1613 size_t length = 0;
1614
1615 for (auto &sld : slt) {
1616 if (!SlIsObjectValidInSavegame(sld)) continue;
1617
1618 length += SlCalcConvFileLen(SLE_UINT8);
1619 length += SlCalcStdStringLen(&sld.name);
1620 }
1621
1622 length += SlCalcConvFileLen(SLE_UINT8); // End-of-list entry.
1623
1624 for (auto &sld : slt) {
1625 if (!SlIsObjectValidInSavegame(sld)) continue;
1626 if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1627 length += SlCalcTableHeader(sld.handler->GetDescription());
1628 }
1629 }
1630
1631 return length;
1632}
1633
1640size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
1641{
1642 size_t length = 0;
1643
1644 /* Need to determine the length and write a length tag. */
1645 for (auto &sld : slt) {
1646 length += SlCalcObjMemberLength(object, sld);
1647 }
1648 return length;
1649}
1650
1651size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld)
1652{
1653 assert(_sl.action == SLA_SAVE);
1654
1655 if (!SlIsObjectValidInSavegame(sld)) return 0;
1656
1657 switch (sld.cmd) {
1658 case SL_VAR: return SlCalcConvFileLen(sld.conv);
1659 case SL_REF: return SlCalcRefLen();
1660 case SL_ARR: return SlCalcArrayLen(sld.length, sld.conv);
1661 case SL_REFLIST: return SlCalcRefListLen(GetVariableAddress(object, sld), sld.conv);
1662 case SL_REFVECTOR: return SlCalcRefVectorLen(GetVariableAddress(object, sld), sld.conv);
1663 case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld.conv);
1664 case SL_VECTOR: return SlCalcVectorLen(GetVariableAddress(object, sld), sld.conv);
1665 case SL_STDSTR: return SlCalcStdStringLen(GetVariableAddress(object, sld));
1666 case SL_SAVEBYTE: return 1; // a byte is logically of size 1
1667 case SL_NULL: return SlCalcConvFileLen(sld.conv) * sld.length;
1668
1669 case SL_STRUCT:
1670 case SL_STRUCTLIST: {
1671 NeedLength old_need_length = _sl.need_length;
1672 size_t old_obj_len = _sl.obj_len;
1673
1675 _sl.obj_len = 0;
1676
1677 /* Pretend that we are saving to collect the object size. Other
1678 * means are difficult, as we don't know the length of the list we
1679 * are about to store. */
1680 sld.handler->Save(const_cast<void *>(object));
1681 size_t length = _sl.obj_len;
1682
1683 _sl.obj_len = old_obj_len;
1684 _sl.need_length = old_need_length;
1685
1686 if (sld.cmd == SL_STRUCT) {
1687 length += SlGetArrayLength(1);
1688 }
1689
1690 return length;
1691 }
1692
1693 default: NOT_REACHED();
1694 }
1695 return 0;
1696}
1697
1698static bool SlObjectMember(void *object, const SaveLoad &sld)
1699{
1700 if (!SlIsObjectValidInSavegame(sld)) return false;
1701
1702 VarType conv = GB(sld.conv, 0, 8);
1703 switch (sld.cmd) {
1704 case SL_VAR:
1705 case SL_REF:
1706 case SL_ARR:
1707 case SL_REFLIST:
1708 case SL_REFVECTOR:
1709 case SL_DEQUE:
1710 case SL_VECTOR:
1711 case SL_STDSTR: {
1712 void *ptr = GetVariableAddress(object, sld);
1713
1714 switch (sld.cmd) {
1715 case SL_VAR: SlSaveLoadConv(ptr, conv); break;
1716 case SL_REF: SlSaveLoadRef(ptr, conv); break;
1717 case SL_ARR: SlArray(ptr, sld.length, conv); break;
1718 case SL_REFLIST: SlRefList(ptr, conv); break;
1719 case SL_REFVECTOR: SlRefVector(ptr, conv); break;
1720 case SL_DEQUE: SlDeque(ptr, conv); break;
1721 case SL_VECTOR: SlVector(ptr, conv); break;
1722 case SL_STDSTR: SlStdString(ptr, sld.conv); break;
1723 default: NOT_REACHED();
1724 }
1725 break;
1726 }
1727
1728 /* SL_SAVEBYTE writes a value to the savegame to identify the type of an object.
1729 * When loading, the value is read explicitly with SlReadByte() to determine which
1730 * object description to use. */
1731 case SL_SAVEBYTE: {
1732 void *ptr = GetVariableAddress(object, sld);
1733
1734 switch (_sl.action) {
1735 case SLA_SAVE: SlWriteByte(*(uint8_t *)ptr); break;
1736 case SLA_LOAD_CHECK:
1737 case SLA_LOAD:
1738 case SLA_PTRS:
1739 case SLA_NULL: break;
1740 default: NOT_REACHED();
1741 }
1742 break;
1743 }
1744
1745 case SL_NULL: {
1746 assert(GetVarMemType(sld.conv) == SLE_VAR_NULL);
1747
1748 switch (_sl.action) {
1749 case SLA_LOAD_CHECK:
1750 case SLA_LOAD: SlSkipBytes(SlCalcConvFileLen(sld.conv) * sld.length); break;
1751 case SLA_SAVE: for (int i = 0; i < SlCalcConvFileLen(sld.conv) * sld.length; i++) SlWriteByte(0); break;
1752 case SLA_PTRS:
1753 case SLA_NULL: break;
1754 default: NOT_REACHED();
1755 }
1756 break;
1757 }
1758
1759 case SL_STRUCT:
1760 case SL_STRUCTLIST:
1761 switch (_sl.action) {
1762 case SLA_SAVE: {
1763 if (sld.cmd == SL_STRUCT) {
1764 /* Store in the savegame if this struct was written or not. */
1765 SlSetStructListLength(SlCalcObjMemberLength(object, sld) > SlGetArrayLength(1) ? 1 : 0);
1766 }
1767 sld.handler->Save(object);
1768 break;
1769 }
1770
1771 case SLA_LOAD_CHECK: {
1774 }
1775 sld.handler->LoadCheck(object);
1776 break;
1777 }
1778
1779 case SLA_LOAD: {
1782 }
1783 sld.handler->Load(object);
1784 break;
1785 }
1786
1787 case SLA_PTRS:
1788 sld.handler->FixPointers(object);
1789 break;
1790
1791 case SLA_NULL: break;
1792 default: NOT_REACHED();
1793 }
1794 break;
1795
1796 default: NOT_REACHED();
1797 }
1798 return true;
1799}
1800
1805void SlSetStructListLength(size_t length)
1806{
1807 /* Automatically calculate the length? */
1808 if (_sl.need_length != NL_NONE) {
1809 SlSetLength(SlGetArrayLength(length));
1810 if (_sl.need_length == NL_CALCLENGTH) return;
1811 }
1812
1813 SlWriteArrayLength(length);
1814}
1815
1821size_t SlGetStructListLength(size_t limit)
1822{
1823 size_t length = SlReadArrayLength();
1824 if (length > limit) SlErrorCorrupt("List exceeds storage size");
1825
1826 return length;
1827}
1828
1834void SlObject(void *object, const SaveLoadTable &slt)
1835{
1836 /* Automatically calculate the length? */
1837 if (_sl.need_length != NL_NONE) {
1838 SlSetLength(SlCalcObjLength(object, slt));
1839 if (_sl.need_length == NL_CALCLENGTH) return;
1840 }
1841
1842 for (auto &sld : slt) {
1843 SlObjectMember(object, sld);
1844 }
1845}
1846
1852 void Save(void *) const override
1853 {
1854 NOT_REACHED();
1855 }
1856
1857 void Load(void *object) const override
1858 {
1859 size_t length = SlGetStructListLength(UINT32_MAX);
1860 for (; length > 0; length--) {
1861 SlObject(object, this->GetLoadDescription());
1862 }
1863 }
1864
1865 void LoadCheck(void *object) const override
1866 {
1867 this->Load(object);
1868 }
1869
1870 virtual SaveLoadTable GetDescription() const override
1871 {
1872 return {};
1873 }
1874
1876 {
1877 NOT_REACHED();
1878 }
1879};
1880
1887std::vector<SaveLoad> SlTableHeader(const SaveLoadTable &slt)
1888{
1889 /* You can only use SlTableHeader if you are a CH_TABLE. */
1890 assert(_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
1891
1892 switch (_sl.action) {
1893 case SLA_LOAD_CHECK:
1894 case SLA_LOAD: {
1895 std::vector<SaveLoad> saveloads;
1896
1897 /* Build a key lookup mapping based on the available fields. */
1898 std::map<std::string, const SaveLoad *> key_lookup;
1899 for (auto &sld : slt) {
1900 if (!SlIsObjectValidInSavegame(sld)) continue;
1901
1902 /* Check that there is only one active SaveLoad for a given name. */
1903 assert(key_lookup.find(sld.name) == key_lookup.end());
1904 key_lookup[sld.name] = &sld;
1905 }
1906
1907 while (true) {
1908 uint8_t type = 0;
1909 SlSaveLoadConv(&type, SLE_UINT8);
1910 if (type == SLE_FILE_END) break;
1911
1912 std::string key;
1913 SlStdString(&key, SLE_STR);
1914
1915 auto sld_it = key_lookup.find(key);
1916 if (sld_it == key_lookup.end()) {
1917 /* SLA_LOADCHECK triggers this debug statement a lot and is perfectly normal. */
1918 Debug(sl, _sl.action == SLA_LOAD ? 2 : 6, "Field '{}' of type 0x{:02x} not found, skipping", key, type);
1919
1920 std::shared_ptr<SaveLoadHandler> handler = nullptr;
1921 SaveLoadType saveload_type;
1922 switch (type & SLE_FILE_TYPE_MASK) {
1923 case SLE_FILE_STRING:
1924 /* Strings are always marked with SLE_FILE_HAS_LENGTH_FIELD, as they are a list of chars. */
1925 saveload_type = SL_STDSTR;
1926 break;
1927
1928 case SLE_FILE_STRUCT:
1929 /* Structs are always marked with SLE_FILE_HAS_LENGTH_FIELD as SL_STRUCT is seen as a list of 0/1 in length. */
1930 saveload_type = SL_STRUCTLIST;
1931 handler = std::make_shared<SlSkipHandler>();
1932 break;
1933
1934 default:
1935 saveload_type = (type & SLE_FILE_HAS_LENGTH_FIELD) ? SL_ARR : SL_VAR;
1936 break;
1937 }
1938
1939 /* We don't know this field, so read to nothing. */
1940 saveloads.emplace_back(std::move(key), saveload_type, ((VarType)type & SLE_FILE_TYPE_MASK) | SLE_VAR_NULL, 1, SL_MIN_VERSION, SL_MAX_VERSION, nullptr, 0, std::move(handler));
1941 continue;
1942 }
1943
1944 /* Validate the type of the field. If it is changed, the
1945 * savegame should have been bumped so we know how to do the
1946 * conversion. If this error triggers, that clearly didn't
1947 * happen and this is a friendly poke to the developer to bump
1948 * the savegame version and add conversion code. */
1949 uint8_t correct_type = GetSavegameFileType(*sld_it->second);
1950 if (correct_type != type) {
1951 Debug(sl, 1, "Field type for '{}' was expected to be 0x{:02x} but 0x{:02x} was found", key, correct_type, type);
1952 SlErrorCorrupt("Field type is different than expected");
1953 }
1954 saveloads.emplace_back(*sld_it->second);
1955 }
1956
1957 for (auto &sld : saveloads) {
1958 if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1959 sld.handler->load_description = SlTableHeader(sld.handler->GetDescription());
1960 }
1961 }
1962
1963 return saveloads;
1964 }
1965
1966 case SLA_SAVE: {
1967 /* Automatically calculate the length? */
1968 if (_sl.need_length != NL_NONE) {
1970 if (_sl.need_length == NL_CALCLENGTH) break;
1971 }
1972
1973 for (auto &sld : slt) {
1974 if (!SlIsObjectValidInSavegame(sld)) continue;
1975 /* Make sure we are not storing empty keys. */
1976 assert(!sld.name.empty());
1977
1978 uint8_t type = GetSavegameFileType(sld);
1979 assert(type != SLE_FILE_END);
1980
1981 SlSaveLoadConv(&type, SLE_UINT8);
1982 SlStdString(const_cast<std::string *>(&sld.name), SLE_STR);
1983 }
1984
1985 /* Add an end-of-header marker. */
1986 uint8_t type = SLE_FILE_END;
1987 SlSaveLoadConv(&type, SLE_UINT8);
1988
1989 /* After the table, write down any sub-tables we might have. */
1990 for (auto &sld : slt) {
1991 if (!SlIsObjectValidInSavegame(sld)) continue;
1992 if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
1993 /* SlCalcTableHeader already looks in sub-lists, so avoid the length being added twice. */
1994 NeedLength old_need_length = _sl.need_length;
1996
1997 SlTableHeader(sld.handler->GetDescription());
1998
1999 _sl.need_length = old_need_length;
2000 }
2001 }
2002
2003 break;
2004 }
2005
2006 default: NOT_REACHED();
2007 }
2008
2009 return std::vector<SaveLoad>();
2010}
2011
2025std::vector<SaveLoad> SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
2026{
2027 assert(_sl.action == SLA_LOAD || _sl.action == SLA_LOAD_CHECK);
2028 /* CH_TABLE / CH_SPARSE_TABLE always have a header. */
2029 if (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE) return SlTableHeader(slt);
2030
2031 std::vector<SaveLoad> saveloads;
2032
2033 /* Build a key lookup mapping based on the available fields. */
2034 std::map<std::string, std::vector<const SaveLoad *>> key_lookup;
2035 for (auto &sld : slt) {
2036 /* All entries should have a name; otherwise the entry should just be removed. */
2037 assert(!sld.name.empty());
2038
2039 key_lookup[sld.name].push_back(&sld);
2040 }
2041
2042 for (auto &slc : slct) {
2043 if (slc.name.empty()) {
2044 /* In old savegames there can be data we no longer care for. We
2045 * skip this by simply reading the amount of bytes indicated and
2046 * send those to /dev/null. */
2047 saveloads.emplace_back("", SL_NULL, GetVarFileType(slc.null_type) | SLE_VAR_NULL, slc.null_length, slc.version_from, slc.version_to, nullptr, 0, nullptr);
2048 } else {
2049 auto sld_it = key_lookup.find(slc.name);
2050 /* If this branch triggers, it means that an entry in the
2051 * SaveLoadCompat list is not mentioned in the SaveLoad list. Did
2052 * you rename a field in one and not in the other? */
2053 if (sld_it == key_lookup.end()) {
2054 /* This isn't an assert, as that leaves no information what
2055 * field was to blame. This way at least we have breadcrumbs. */
2056 Debug(sl, 0, "internal error: saveload compatibility field '{}' not found", slc.name);
2057 SlErrorCorrupt("Internal error with savegame compatibility");
2058 }
2059 for (auto &sld : sld_it->second) {
2060 saveloads.push_back(*sld);
2061 }
2062 }
2063 }
2064
2065 for (auto &sld : saveloads) {
2066 if (!SlIsObjectValidInSavegame(sld)) continue;
2067 if (sld.cmd == SL_STRUCTLIST || sld.cmd == SL_STRUCT) {
2068 sld.handler->load_description = SlCompatTableHeader(sld.handler->GetDescription(), sld.handler->GetCompatDescription());
2069 }
2070 }
2071
2072 return saveloads;
2073}
2074
2080{
2081 SlObject(nullptr, slt);
2082}
2083
2089void SlAutolength(AutolengthProc *proc, int arg)
2090{
2091 assert(_sl.action == SLA_SAVE);
2092
2093 /* Tell it to calculate the length */
2095 _sl.obj_len = 0;
2096 proc(arg);
2097
2098 /* Setup length */
2101
2102 size_t start_pos = _sl.dumper->GetSize();
2103 size_t expected_offs = start_pos + _sl.obj_len;
2104
2105 /* And write the stuff */
2106 proc(arg);
2107
2108 if (expected_offs != _sl.dumper->GetSize()) {
2109 SlErrorCorruptFmt("Invalid chunk size when writing autolength block, expected {}, got {}", _sl.obj_len, _sl.dumper->GetSize() - start_pos);
2110 }
2111}
2112
2113void ChunkHandler::LoadCheck(size_t len) const
2114{
2115 switch (_sl.block_mode) {
2116 case CH_TABLE:
2117 case CH_SPARSE_TABLE:
2118 SlTableHeader({});
2119 [[fallthrough]];
2120 case CH_ARRAY:
2121 case CH_SPARSE_ARRAY:
2122 SlSkipArray();
2123 break;
2124 case CH_RIFF:
2125 SlSkipBytes(len);
2126 break;
2127 default:
2128 NOT_REACHED();
2129 }
2130}
2131
2136static void SlLoadChunk(const ChunkHandler &ch)
2137{
2138 uint8_t m = SlReadByte();
2139
2141 _sl.obj_len = 0;
2142 _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2143
2144 /* The header should always be at the start. Read the length; the
2145 * Load() should as first action process the header. */
2147 if (SlIterateArray() != INT32_MAX) SlErrorCorrupt("Table chunk without header");
2148 }
2149
2150 switch (_sl.block_mode) {
2151 case CH_TABLE:
2152 case CH_ARRAY:
2153 _sl.array_index = 0;
2154 ch.Load();
2155 if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2156 break;
2157 case CH_SPARSE_TABLE:
2158 case CH_SPARSE_ARRAY:
2159 ch.Load();
2160 if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2161 break;
2162 case CH_RIFF: {
2163 /* Read length */
2164 size_t len = (SlReadByte() << 16) | ((m >> 4) << 24);
2165 len += SlReadUint16();
2166 _sl.obj_len = len;
2167 size_t start_pos = _sl.reader->GetSize();
2168 size_t endoffs = start_pos + len;
2169 ch.Load();
2170
2171 if (_sl.reader->GetSize() != endoffs) {
2172 SlErrorCorruptFmt("Invalid chunk size in RIFF in {} - expected {}, got {}", ch.GetName(), len, _sl.reader->GetSize() - start_pos);
2173 }
2174 break;
2175 }
2176 default:
2177 SlErrorCorrupt("Invalid chunk type");
2178 break;
2179 }
2180
2181 if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2182}
2183
2189static void SlLoadCheckChunk(const ChunkHandler &ch)
2190{
2191 uint8_t m = SlReadByte();
2192
2194 _sl.obj_len = 0;
2195 _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2196
2197 /* The header should always be at the start. Read the length; the
2198 * LoadCheck() should as first action process the header. */
2200 if (SlIterateArray() != INT32_MAX) SlErrorCorrupt("Table chunk without header");
2201 }
2202
2203 switch (_sl.block_mode) {
2204 case CH_TABLE:
2205 case CH_ARRAY:
2206 _sl.array_index = 0;
2207 ch.LoadCheck();
2208 break;
2209 case CH_SPARSE_TABLE:
2210 case CH_SPARSE_ARRAY:
2211 ch.LoadCheck();
2212 break;
2213 case CH_RIFF: {
2214 /* Read length */
2215 size_t len = (SlReadByte() << 16) | ((m >> 4) << 24);
2216 len += SlReadUint16();
2217 _sl.obj_len = len;
2218 size_t start_pos = _sl.reader->GetSize();
2219 size_t endoffs = start_pos + len;
2220 ch.LoadCheck(len);
2221
2222 if (_sl.reader->GetSize() != endoffs) {
2223 SlErrorCorruptFmt("Invalid chunk size in RIFF in {} - expected {}, got {}", ch.GetName(), len, _sl.reader->GetSize() - start_pos);
2224 }
2225 break;
2226 }
2227 default:
2228 SlErrorCorrupt("Invalid chunk type");
2229 break;
2230 }
2231
2232 if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2233}
2234
2240static void SlSaveChunk(const ChunkHandler &ch)
2241{
2242 if (ch.type == CH_READONLY) return;
2243
2244 SlWriteUint32(ch.id);
2245 Debug(sl, 2, "Saving chunk {}", ch.GetName());
2246
2247 _sl.block_mode = ch.type;
2248 _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE);
2249
2251
2252 switch (_sl.block_mode) {
2253 case CH_RIFF:
2254 ch.Save();
2255 break;
2256 case CH_TABLE:
2257 case CH_ARRAY:
2260 ch.Save();
2261 SlWriteArrayLength(0); // Terminate arrays
2262 break;
2263 case CH_SPARSE_TABLE:
2264 case CH_SPARSE_ARRAY:
2266 ch.Save();
2267 SlWriteArrayLength(0); // Terminate arrays
2268 break;
2269 default: NOT_REACHED();
2270 }
2271
2272 if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2273}
2274
2276static void SlSaveChunks()
2277{
2278 for (auto &ch : ChunkHandlers()) {
2279 SlSaveChunk(ch);
2280 }
2281
2282 /* Terminator */
2283 SlWriteUint32(0);
2284}
2285
2292static const ChunkHandler *SlFindChunkHandler(uint32_t id)
2293{
2294 for (const ChunkHandler &ch : ChunkHandlers()) if (ch.id == id) return &ch;
2295 return nullptr;
2296}
2297
2299static void SlLoadChunks()
2300{
2301 uint32_t id;
2302 const ChunkHandler *ch;
2303
2304 for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
2305 Debug(sl, 2, "Loading chunk {:c}{:c}{:c}{:c}", id >> 24, id >> 16, id >> 8, id);
2306
2307 ch = SlFindChunkHandler(id);
2308 if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2309 SlLoadChunk(*ch);
2310 }
2311}
2312
2315{
2316 uint32_t id;
2317 const ChunkHandler *ch;
2318
2319 for (id = SlReadUint32(); id != 0; id = SlReadUint32()) {
2320 Debug(sl, 2, "Loading chunk {:c}{:c}{:c}{:c}", id >> 24, id >> 16, id >> 8, id);
2321
2322 ch = SlFindChunkHandler(id);
2323 if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2324 SlLoadCheckChunk(*ch);
2325 }
2326}
2327
2329static void SlFixPointers()
2330{
2332
2333 for (const ChunkHandler &ch : ChunkHandlers()) {
2334 Debug(sl, 3, "Fixing pointers for {}", ch.GetName());
2335 ch.FixPointers();
2336 }
2337
2338 assert(_sl.action == SLA_PTRS);
2339}
2340
2341
2344 std::optional<FileHandle> file;
2345 long begin;
2346
2351 FileReader(FileHandle &&file) : LoadFilter(nullptr), file(std::move(file)), begin(ftell(*this->file))
2352 {
2353 }
2354
2357 {
2358 if (this->file.has_value()) {
2359 _game_session_stats.savegame_size = ftell(*this->file) - this->begin;
2360 }
2361 }
2362
2363 size_t Read(uint8_t *buf, size_t size) override
2364 {
2365 /* We're in the process of shutting down, i.e. in "failure" mode. */
2366 if (!this->file.has_value()) return 0;
2367
2368 return fread(buf, 1, size, *this->file);
2369 }
2370
2371 void Reset() override
2372 {
2373 clearerr(*this->file);
2374 if (fseek(*this->file, this->begin, SEEK_SET)) {
2375 Debug(sl, 1, "Could not reset the file reading");
2376 }
2377 }
2378};
2379
2382 std::optional<FileHandle> file;
2383
2388 FileWriter(FileHandle &&file) : SaveFilter(nullptr), file(std::move(file))
2389 {
2390 }
2391
2394 {
2395 this->Finish();
2396 }
2397
2398 void Write(uint8_t *buf, size_t size) override
2399 {
2400 /* We're in the process of shutting down, i.e. in "failure" mode. */
2401 if (!this->file.has_value()) return;
2402
2403 if (fwrite(buf, 1, size, *this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
2404 }
2405
2406 void Finish() override
2407 {
2408 if (this->file.has_value()) {
2409 _game_session_stats.savegame_size = ftell(*this->file);
2410 this->file.reset();
2411 }
2412 }
2413};
2414
2415/*******************************************
2416 ********** START OF LZO CODE **************
2417 *******************************************/
2418
2419#ifdef WITH_LZO
2420#include <lzo/lzo1x.h>
2421
2423static const uint LZO_BUFFER_SIZE = 8192;
2424
2431 LZOLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
2432 {
2433 if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2434 }
2435
2436 size_t Read(uint8_t *buf, size_t ssize) override
2437 {
2438 assert(ssize >= LZO_BUFFER_SIZE);
2439
2440 /* Buffer size is from the LZO docs plus the chunk header size. */
2441 uint8_t out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32_t) * 2];
2442 uint32_t tmp[2];
2443 uint32_t size;
2444 lzo_uint len = ssize;
2445
2446 /* Read header*/
2447 if (this->chain->Read((uint8_t*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
2448
2449 /* Check if size is bad */
2450 ((uint32_t*)out)[0] = size = tmp[1];
2451
2452 if (_sl_version != SL_MIN_VERSION) {
2453 tmp[0] = TO_BE32(tmp[0]);
2454 size = TO_BE32(size);
2455 }
2456
2457 if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
2458
2459 /* Read block */
2460 if (this->chain->Read(out + sizeof(uint32_t), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2461
2462 /* Verify checksum */
2463 if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32_t))) SlErrorCorrupt("Bad checksum");
2464
2465 /* Decompress */
2466 int ret = lzo1x_decompress_safe(out + sizeof(uint32_t) * 1, size, buf, &len, nullptr);
2467 if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2468 return len;
2469 }
2470};
2471
2478 LZOSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t) : SaveFilter(std::move(chain))
2479 {
2480 if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2481 }
2482
2483 void Write(uint8_t *buf, size_t size) override
2484 {
2485 const lzo_bytep in = buf;
2486 /* Buffer size is from the LZO docs plus the chunk header size. */
2487 uint8_t out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32_t) * 2];
2488 uint8_t wrkmem[LZO1X_1_MEM_COMPRESS];
2489 lzo_uint outlen;
2490
2491 do {
2492 /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2493 lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : (lzo_uint)size;
2494 lzo1x_1_compress(in, len, out + sizeof(uint32_t) * 2, &outlen, wrkmem);
2495 ((uint32_t*)out)[1] = TO_BE32((uint32_t)outlen);
2496 ((uint32_t*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32_t), outlen + sizeof(uint32_t)));
2497 this->chain->Write(out, outlen + sizeof(uint32_t) * 2);
2498
2499 /* Move to next data chunk. */
2500 size -= len;
2501 in += len;
2502 } while (size > 0);
2503 }
2504};
2505
2506#endif /* WITH_LZO */
2507
2508/*********************************************
2509 ******** START OF NOCOMP CODE (uncompressed)*
2510 *********************************************/
2511
2518 NoCompLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
2519 {
2520 }
2521
2522 size_t Read(uint8_t *buf, size_t size) override
2523 {
2524 return this->chain->Read(buf, size);
2525 }
2526};
2527
2534 NoCompSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t) : SaveFilter(std::move(chain))
2535 {
2536 }
2537
2538 void Write(uint8_t *buf, size_t size) override
2539 {
2540 this->chain->Write(buf, size);
2541 }
2542};
2543
2544/********************************************
2545 ********** START OF ZLIB CODE **************
2546 ********************************************/
2547
2548#if defined(WITH_ZLIB)
2549#include <zlib.h>
2550
2553 z_stream z{};
2555
2560 ZlibLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
2561 {
2562 if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2563 }
2564
2567 {
2568 inflateEnd(&this->z);
2569 }
2570
2571 size_t Read(uint8_t *buf, size_t size) override
2572 {
2573 this->z.next_out = buf;
2574 this->z.avail_out = (uint)size;
2575
2576 do {
2577 /* read more bytes from the file? */
2578 if (this->z.avail_in == 0) {
2579 this->z.next_in = this->fread_buf;
2580 this->z.avail_in = (uint)this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2581 }
2582
2583 /* inflate the data */
2584 int r = inflate(&this->z, 0);
2585 if (r == Z_STREAM_END) break;
2586
2587 if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2588 } while (this->z.avail_out != 0);
2589
2590 return size - this->z.avail_out;
2591 }
2592};
2593
2596 z_stream z{};
2598
2604 ZlibSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level) : SaveFilter(std::move(chain))
2605 {
2606 if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2607 }
2608
2611 {
2612 deflateEnd(&this->z);
2613 }
2614
2621 void WriteLoop(uint8_t *p, size_t len, int mode)
2622 {
2623 uint n;
2624 this->z.next_in = p;
2625 this->z.avail_in = (uInt)len;
2626 do {
2627 this->z.next_out = this->fwrite_buf;
2628 this->z.avail_out = sizeof(this->fwrite_buf);
2629
2637 int r = deflate(&this->z, mode);
2638
2639 /* bytes were emitted? */
2640 if ((n = sizeof(this->fwrite_buf) - this->z.avail_out) != 0) {
2641 this->chain->Write(this->fwrite_buf, n);
2642 }
2643 if (r == Z_STREAM_END) break;
2644
2645 if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2646 } while (this->z.avail_in || !this->z.avail_out);
2647 }
2648
2649 void Write(uint8_t *buf, size_t size) override
2650 {
2651 this->WriteLoop(buf, size, 0);
2652 }
2653
2654 void Finish() override
2655 {
2656 this->WriteLoop(nullptr, 0, Z_FINISH);
2657 this->chain->Finish();
2658 }
2659};
2660
2661#endif /* WITH_ZLIB */
2662
2663/********************************************
2664 ********** START OF LZMA CODE **************
2665 ********************************************/
2666
2667#if defined(WITH_LIBLZMA)
2668#include <lzma.h>
2669
2676static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2677
2680 lzma_stream lzma;
2682
2687 LZMALoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain)), lzma(_lzma_init)
2688 {
2689 /* Allow saves up to 256 MB uncompressed */
2690 if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2691 }
2692
2695 {
2696 lzma_end(&this->lzma);
2697 }
2698
2699 size_t Read(uint8_t *buf, size_t size) override
2700 {
2701 this->lzma.next_out = buf;
2702 this->lzma.avail_out = size;
2703
2704 do {
2705 /* read more bytes from the file? */
2706 if (this->lzma.avail_in == 0) {
2707 this->lzma.next_in = this->fread_buf;
2708 this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2709 }
2710
2711 /* inflate the data */
2712 lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2713 if (r == LZMA_STREAM_END) break;
2714 if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2715 } while (this->lzma.avail_out != 0);
2716
2717 return size - this->lzma.avail_out;
2718 }
2719};
2720
2723 lzma_stream lzma;
2725
2731 LZMASaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level) : SaveFilter(std::move(chain)), lzma(_lzma_init)
2732 {
2733 if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2734 }
2735
2738 {
2739 lzma_end(&this->lzma);
2740 }
2741
2748 void WriteLoop(uint8_t *p, size_t len, lzma_action action)
2749 {
2750 size_t n;
2751 this->lzma.next_in = p;
2752 this->lzma.avail_in = len;
2753 do {
2754 this->lzma.next_out = this->fwrite_buf;
2755 this->lzma.avail_out = sizeof(this->fwrite_buf);
2756
2757 lzma_ret r = lzma_code(&this->lzma, action);
2758
2759 /* bytes were emitted? */
2760 if ((n = sizeof(this->fwrite_buf) - this->lzma.avail_out) != 0) {
2761 this->chain->Write(this->fwrite_buf, n);
2762 }
2763 if (r == LZMA_STREAM_END) break;
2764 if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2765 } while (this->lzma.avail_in || !this->lzma.avail_out);
2766 }
2767
2768 void Write(uint8_t *buf, size_t size) override
2769 {
2770 this->WriteLoop(buf, size, LZMA_RUN);
2771 }
2772
2773 void Finish() override
2774 {
2775 this->WriteLoop(nullptr, 0, LZMA_FINISH);
2776 this->chain->Finish();
2777 }
2778};
2779
2780#endif /* WITH_LIBLZMA */
2781
2782/*******************************************
2783 ************* END OF CODE *****************
2784 *******************************************/
2785
2788 std::string_view name;
2789 uint32_t tag;
2790
2791 std::shared_ptr<LoadFilter> (*init_load)(std::shared_ptr<LoadFilter> chain);
2792 std::shared_ptr<SaveFilter> (*init_write)(std::shared_ptr<SaveFilter> chain, uint8_t compression);
2793
2797};
2798
2799static const uint32_t SAVEGAME_TAG_LZO = TO_BE32('OTTD');
2800static const uint32_t SAVEGAME_TAG_NONE = TO_BE32('OTTN');
2801static const uint32_t SAVEGAME_TAG_ZLIB = TO_BE32('OTTZ');
2802static const uint32_t SAVEGAME_TAG_LZMA = TO_BE32('OTTX');
2803
2806#if defined(WITH_LZO)
2807 /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2808 {"lzo", SAVEGAME_TAG_LZO, CreateLoadFilter<LZOLoadFilter>, CreateSaveFilter<LZOSaveFilter>, 0, 0, 0},
2809#else
2810 {"lzo", SAVEGAME_TAG_LZO, nullptr, nullptr, 0, 0, 0},
2811#endif
2812 /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2813 {"none", SAVEGAME_TAG_NONE, CreateLoadFilter<NoCompLoadFilter>, CreateSaveFilter<NoCompSaveFilter>, 0, 0, 0},
2814#if defined(WITH_ZLIB)
2815 /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2816 * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2817 * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2818 {"zlib", SAVEGAME_TAG_ZLIB, CreateLoadFilter<ZlibLoadFilter>, CreateSaveFilter<ZlibSaveFilter>, 0, 6, 9},
2819#else
2820 {"zlib", SAVEGAME_TAG_ZLIB, nullptr, nullptr, 0, 0, 0},
2821#endif
2822#if defined(WITH_LIBLZMA)
2823 /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2824 * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2825 * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2826 * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2827 * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2828 {"lzma", SAVEGAME_TAG_LZMA, CreateLoadFilter<LZMALoadFilter>, CreateSaveFilter<LZMASaveFilter>, 0, 2, 9},
2829#else
2830 {"lzma", SAVEGAME_TAG_LZMA, nullptr, nullptr, 0, 0, 0},
2831#endif
2832};
2833
2840static std::pair<const SaveLoadFormat &, uint8_t> GetSavegameFormat(std::string_view full_name)
2841{
2842 /* Find default savegame format, the highest one with which files can be written. */
2843 auto it = std::find_if(std::rbegin(_saveload_formats), std::rend(_saveload_formats), [](const auto &slf) { return slf.init_write != nullptr; });
2844 if (it == std::rend(_saveload_formats)) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "no writeable savegame formats");
2845
2846 const SaveLoadFormat &def = *it;
2847
2848 if (!full_name.empty()) {
2849 /* Get the ":..." of the compression level out of the way */
2850 size_t separator = full_name.find(':');
2851 bool has_comp_level = separator != std::string::npos;
2852 std::string_view name = has_comp_level ? full_name.substr(0, separator) : full_name;
2853
2854 for (const auto &slf : _saveload_formats) {
2855 if (slf.init_write != nullptr && name.compare(slf.name) == 0) {
2856 if (has_comp_level) {
2857 auto complevel = full_name.substr(separator + 1);
2858
2859 /* Get the level and determine whether all went fine. */
2860 auto level = ParseInteger<uint8_t>(complevel);
2861 if (!level.has_value() || *level != Clamp(*level, slf.min_compression, slf.max_compression)) {
2863 GetEncodedString(STR_CONFIG_ERROR),
2864 GetEncodedString(STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, complevel),
2865 WL_CRITICAL);
2866 } else {
2867 return {slf, *level};
2868 }
2869 }
2870 return {slf, slf.default_compression};
2871 }
2872 }
2873
2875 GetEncodedString(STR_CONFIG_ERROR),
2876 GetEncodedString(STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, name, def.name),
2877 WL_CRITICAL);
2878 }
2879 return {def, def.default_compression};
2880}
2881
2882/* actual loader/saver function */
2883void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2884extern bool AfterLoadGame();
2885extern bool LoadOldSaveGame(std::string_view file);
2886
2892static void ResetSettings()
2893{
2894 for (auto &desc : GetSaveLoadSettingTable()) {
2895 const SettingDesc *sd = GetSettingDesc(desc);
2896 if (sd->flags.Test(SettingFlag::NotInSave)) continue;
2898
2900 }
2901}
2902
2903extern void ClearOldOrders();
2904
2909{
2911 ResetTempEngineData();
2912 ClearRailTypeLabelList();
2913 ClearRoadTypeLabelList();
2914 ResetOldWaypoints();
2915 ResetSettings();
2916}
2917
2921static inline void ClearSaveLoadState()
2922{
2923 _sl.dumper = nullptr;
2924 _sl.sf = nullptr;
2925 _sl.reader = nullptr;
2926 _sl.lf = nullptr;
2927}
2928
2930static void SaveFileStart()
2931{
2932 SetMouseCursorBusy(true);
2933
2935 _sl.saveinprogress = true;
2936}
2937
2939static void SaveFileDone()
2940{
2941 SetMouseCursorBusy(false);
2942
2944 _sl.saveinprogress = false;
2945
2946#ifdef __EMSCRIPTEN__
2947 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
2948#endif
2949}
2950
2953{
2954 _sl.error_str = str;
2955}
2956
2959{
2960 return GetEncodedString(_sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED);
2961}
2962
2968
2975
2981{
2982 try {
2983 auto [fmt, compression] = GetSavegameFormat(_savegame_format);
2984
2985 /* We have written our stuff to memory, now write it to file! */
2986 uint32_t hdr[2] = { fmt.tag, TO_BE32(SAVEGAME_VERSION << 16) };
2987 _sl.sf->Write((uint8_t*)hdr, sizeof(hdr));
2988
2989 _sl.sf = fmt.init_write(_sl.sf, compression);
2990 _sl.dumper->Flush(_sl.sf);
2991
2993
2994 if (threaded) SetAsyncSaveFinish(SaveFileDone);
2995
2996 return SL_OK;
2997 } catch (...) {
2999
3001
3002 /* We don't want to shout when saving is just
3003 * cancelled due to a client disconnecting. */
3004 if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
3005 /* Skip the "colour" character */
3006 Debug(sl, 0, "{}", GetSaveLoadErrorType().GetDecodedString().substr(3) + GetSaveLoadErrorMessage().GetDecodedString());
3007 asfp = SaveFileError;
3008 }
3009
3010 if (threaded) {
3011 SetAsyncSaveFinish(asfp);
3012 } else {
3013 asfp();
3014 }
3015 return SL_ERROR;
3016 }
3017}
3018
3019void WaitTillSaved()
3020{
3021 if (!_save_thread.joinable()) return;
3022
3023 _save_thread.join();
3024
3025 /* Make sure every other state is handled properly as well. */
3027}
3028
3037static SaveOrLoadResult DoSave(std::shared_ptr<SaveFilter> writer, bool threaded)
3038{
3039 assert(!_sl.saveinprogress);
3040
3041 _sl.dumper = std::make_unique<MemoryDumper>();
3042 _sl.sf = std::move(writer);
3043
3045
3046 SaveViewportBeforeSaveGame();
3047 SlSaveChunks();
3048
3049 SaveFileStart();
3050
3051 if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
3052 if (threaded) Debug(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
3053
3054 SaveOrLoadResult result = SaveFileToDisk(false);
3055 SaveFileDone();
3056
3057 return result;
3058 }
3059
3060 return SL_OK;
3061}
3062
3069SaveOrLoadResult SaveWithFilter(std::shared_ptr<SaveFilter> writer, bool threaded)
3070{
3071 try {
3073 return DoSave(std::move(writer), threaded);
3074 } catch (...) {
3076 return SL_ERROR;
3077 }
3078}
3079
3088static const SaveLoadFormat *DetermineSaveLoadFormat(uint32_t tag, uint32_t raw_version)
3089{
3090 auto fmt = std::ranges::find(_saveload_formats, tag, &SaveLoadFormat::tag);
3091 if (fmt != std::end(_saveload_formats)) {
3092 /* Check version number */
3093 _sl_version = (SaveLoadVersion)(TO_BE32(raw_version) >> 16);
3094 /* Minor is not used anymore from version 18.0, but it is still needed
3095 * in versions before that (4 cases) which can't be removed easy.
3096 * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
3097 _sl_minor_version = (TO_BE32(raw_version) >> 8) & 0xFF;
3098
3099 Debug(sl, 1, "Loading savegame version {}", _sl_version);
3100
3101 /* Is the version higher than the current? */
3102 if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
3103 if (_sl_version >= SLV_START_PATCHPACKS && _sl_version <= SLV_END_PATCHPACKS) SlError(STR_GAME_SAVELOAD_ERROR_PATCHPACK);
3104 return fmt;
3105 }
3106
3107 Debug(sl, 0, "Unknown savegame type, trying to load it as the buggy format");
3108 _sl.lf->Reset();
3111
3112 /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
3113 fmt = std::ranges::find(_saveload_formats, SAVEGAME_TAG_LZO, &SaveLoadFormat::tag);
3114 if (fmt == std::end(_saveload_formats)) {
3115 /* Who removed the LZO savegame format definition? When built without LZO support,
3116 * the formats must still list it just without a method to read the file.
3117 * The caller of this function has to check for the existence of load function. */
3118 NOT_REACHED();
3119 }
3120 return fmt;
3121}
3122
3129static SaveOrLoadResult DoLoad(std::shared_ptr<LoadFilter> reader, bool load_check)
3130{
3131 _sl.lf = std::move(reader);
3132
3133 if (load_check) {
3134 /* Clear previous check data */
3136 /* Mark SL_LOAD_CHECK as supported for this savegame. */
3138 }
3139
3140 uint32_t hdr[2];
3141 if (_sl.lf->Read((uint8_t*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3142
3143 /* see if we have any loader for this type. */
3144 const SaveLoadFormat *fmt = DetermineSaveLoadFormat(hdr[0], hdr[1]);
3145
3146 /* loader for this savegame type is not implemented? */
3147 if (fmt->init_load == nullptr) {
3148 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, fmt::format("Loader for '{}' is not available.", fmt->name));
3149 }
3150
3151 _sl.lf = fmt->init_load(_sl.lf);
3152 _sl.reader = std::make_unique<ReadBuffer>(_sl.lf);
3153 _next_offs = 0;
3154
3155 if (!load_check) {
3157
3158 /* Old maps were hardcoded to 256x256 and thus did not contain
3159 * any mapsize information. Pre-initialize to 256x256 to not to
3160 * confuse old games */
3161 InitializeGame(256, 256, true, true);
3162
3163 _gamelog.Reset();
3164
3166 /*
3167 * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
3168 * shared savegame version 4. Anything before that 'obviously'
3169 * does not have any NewGRFs. Between the introduction and
3170 * savegame version 41 (just before 0.5) the NewGRF settings
3171 * were not stored in the savegame and they were loaded by
3172 * using the settings from the main menu.
3173 * So, to recap:
3174 * - savegame version < 4: do not load any NewGRFs.
3175 * - savegame version >= 41: load NewGRFs from savegame, which is
3176 * already done at this stage by
3177 * overwriting the main menu settings.
3178 * - other savegame versions: use main menu settings.
3179 *
3180 * This means that users *can* crash savegame version 4..40
3181 * savegames if they set incompatible NewGRFs in the main menu,
3182 * but can't crash anymore for savegame version < 4 savegames.
3183 *
3184 * Note: this is done here because AfterLoadGame is also called
3185 * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
3186 */
3188 }
3189 }
3190
3191 if (load_check) {
3192 /* Load chunks into _load_check_data.
3193 * No pools are loaded. References are not possible, and thus do not need resolving. */
3195 } else {
3196 /* Load chunks and resolve references */
3197 SlLoadChunks();
3198 SlFixPointers();
3199 }
3200
3202
3204
3205 if (load_check) {
3206 /* The only part from AfterLoadGame() we need */
3208 } else {
3210
3211 /* After loading fix up savegame for any internal changes that
3212 * might have occurred since then. If it fails, load back the old game. */
3213 if (!AfterLoadGame()) {
3215 return SL_REINIT;
3216 }
3217
3219 }
3220
3221 return SL_OK;
3222}
3223
3229SaveOrLoadResult LoadWithFilter(std::shared_ptr<LoadFilter> reader)
3230{
3231 try {
3233 return DoLoad(std::move(reader), false);
3234 } catch (...) {
3236 return SL_REINIT;
3237 }
3238}
3239
3249SaveOrLoadResult SaveOrLoad(std::string_view filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
3250{
3251 /* An instance of saving is already active, so don't go saving again */
3252 if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) {
3253 /* if not an autosave, but a user action, show error message */
3254 if (!_do_autosave) ShowErrorMessage(GetEncodedString(STR_ERROR_SAVE_STILL_IN_PROGRESS), {}, WL_ERROR);
3255 return SL_OK;
3256 }
3257 WaitTillSaved();
3258
3259 try {
3260 /* Load a TTDLX or TTDPatch game */
3261 if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) {
3263
3264 InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
3265
3266 /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
3267 * and if so a new NewGRF list will be made in LoadOldSaveGame.
3268 * Note: this is done here because AfterLoadGame is also called
3269 * for OTTD savegames which have their own NewGRF logic. */
3271 _gamelog.Reset();
3272 if (!LoadOldSaveGame(filename)) return SL_REINIT;
3276 if (!AfterLoadGame()) {
3278 return SL_REINIT;
3279 }
3281 return SL_OK;
3282 }
3283
3284 assert(dft == DFT_GAME_FILE);
3285 switch (fop) {
3286 case SLO_CHECK:
3288 break;
3289
3290 case SLO_LOAD:
3292 break;
3293
3294 case SLO_SAVE:
3296 break;
3297
3298 default: NOT_REACHED();
3299 }
3300
3301 auto fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
3302
3303 /* Make it a little easier to load savegames from the console */
3304 if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
3305 if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
3306 if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
3307
3308 if (!fh.has_value()) {
3309 SlError(fop == SLO_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3310 }
3311
3312 if (fop == SLO_SAVE) { // SAVE game
3313 Debug(desync, 1, "save: {:08x}; {:02x}; {}", TimerGameEconomy::date, TimerGameEconomy::date_fract, filename);
3314 if (!_settings_client.gui.threaded_saves) threaded = false;
3315
3316 return DoSave(std::make_shared<FileWriter>(std::move(*fh)), threaded);
3317 }
3318
3319 /* LOAD game */
3320 assert(fop == SLO_LOAD || fop == SLO_CHECK);
3321 Debug(desync, 1, "load: {}", filename);
3322 return DoLoad(std::make_shared<FileReader>(std::move(*fh)), fop == SLO_CHECK);
3323 } catch (...) {
3324 /* This code may be executed both for old and new save games. */
3326
3327 /* Skip the "colour" character */
3328 if (fop != SLO_CHECK) Debug(sl, 0, "{}", GetSaveLoadErrorType().GetDecodedString().substr(3) + GetSaveLoadErrorMessage().GetDecodedString());
3329
3330 /* A saver/loader exception!! reinitialize all variables to prevent crash! */
3331 return (fop == SLO_LOAD) ? SL_REINIT : SL_ERROR;
3332 }
3333}
3334
3341{
3342 std::string filename;
3343
3345 filename = GenerateDefaultSaveName() + counter.Extension();
3346 } else {
3347 filename = counter.Filename();
3348 }
3349
3350 Debug(sl, 2, "Autosaving to '{}'", filename);
3351 if (SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) {
3352 ShowErrorMessage(GetEncodedString(STR_ERROR_AUTOSAVE_FAILED), {}, WL_ERROR);
3353 }
3354}
3355
3356
3359{
3361}
3362
3367{
3368 /* Check if we have a name for this map, which is the name of the first
3369 * available company. When there's no company available we'll use
3370 * 'Spectator' as "company" name. */
3372 if (!Company::IsValidID(cid)) {
3373 for (const Company *c : Company::Iterate()) {
3374 cid = c->index;
3375 break;
3376 }
3377 }
3378
3379 std::array<StringParameter, 4> params{};
3380 auto it = params.begin();
3381 *it++ = cid;
3382
3383 /* We show the current game time differently depending on the timekeeping units used by this game. */
3385 /* Insert time played. */
3386 const auto play_time = TimerGameTick::counter / Ticks::TICKS_PER_SECOND;
3387 *it++ = STR_SAVEGAME_DURATION_REALTIME;
3388 *it++ = play_time / 60 / 60;
3389 *it++ = (play_time / 60) % 60;
3390 } else {
3391 /* Insert current date */
3393 case 0: *it++ = STR_JUST_DATE_LONG; break;
3394 case 1: *it++ = STR_JUST_DATE_TINY; break;
3395 case 2: *it++ = STR_JUST_DATE_ISO; break;
3396 default: NOT_REACHED();
3397 }
3398 *it++ = TimerGameEconomy::date;
3399 }
3400
3401 /* Get the correct string (special string for when there's not company) */
3402 std::string filename = GetStringWithArgs(!Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, params);
3403 SanitizeFilename(filename);
3404 return filename;
3405}
3406
3413{
3414 if (ft.abstract == FT_INVALID || ft.abstract == FT_NONE) {
3415 this->file_op = SLO_INVALID;
3416 this->ftype = FIOS_TYPE_INVALID;
3417 return;
3418 }
3419
3420 this->file_op = fop;
3421 this->ftype = ft;
3422}
3423
3429{
3430 this->SetMode(item.type);
3431 this->name = item.name;
3432 this->title = item.title;
3433}
3434
3436{
3437 assert(this->load_description.has_value());
3438 return *this->load_description;
3439}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
void PutUtf8(char32_t c)
Append UTF.8 char.
void Put(std::string_view str)
Append string.
void PutIntegerBase(T value, int base)
Append integer 'value' in given number 'base'.
Container for an encoded string, created by GetEncodedString.
Enum-as-bit-set wrapper.
void StartAction(GamelogActionType at)
Stores information about new action, but doesn't allocate it Action is allocated only when there is a...
Definition gamelog.cpp:65
void Reset()
Resets and frees all memory allocated - used before loading or starting a new game.
Definition gamelog.cpp:94
void StopAction()
Stops logging of any changes.
Definition gamelog.cpp:74
Class for calculation jobs to be run on link graphs.
A connected component of a link graph.
Definition linkgraph.h:37
Handler for saving/loading an object to/from disk.
Definition saveload.h:526
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Handler that is assigned when there is a struct read in the savegame which is not known to the code.
virtual SaveLoadTable GetDescription() const override
Get the description of the fields in the savegame.
virtual SaveLoadCompatTable GetCompatDescription() const override
Get the pre-header description of the fields in the savegame.
void LoadCheck(void *object) const override
Similar to load, but used only to validate savegames.
void Load(void *object) const override
Load the object from disk.
void Save(void *) const override
Save the object to disk.
Template class to help with list-like types.
static void SlSaveLoad(void *storage, VarType conv, SaveLoadType cmd=SL_VAR)
Internal templated helper to save/load a list-like type.
static size_t SlCalcLen(const void *storage, VarType conv, SaveLoadType cmd=SL_VAR)
Internal templated helper to return the size in bytes of a list-like type.
Compose data into a growing std::string.
Parse data from a string / buffer.
std::optional< T > TryReadIntegerBase(int base, bool clamp=false)
Try to read and parse an integer in number 'base', and then advance the reader.
@ READ_ONE_SEPARATOR
Read one separator, and include it in the result.
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
std::optional< char32_t > TryReadUtf8()
Try to read a UTF-8 character, and then advance reader.
T ReadIntegerBase(int base, T def=0, bool clamp=false)
Read and parse an integer in number 'base', and advance the reader.
bool ReadUtf8If(char32_t c)
Check whether the next UTF-8 char matches 'c', and skip it.
std::string_view ReadUntilUtf8(char32_t c, SeparatorUsage sep)
Read data until the first occurrence of UTF-8 char 'c', and advance reader.
static constexpr TimerGameTick::Ticks TICKS_PER_SECOND
Estimation of how many ticks fit in a single second.
static Date date
Current date in days (day counter).
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
static DateFract date_fract
Fractional part of the day.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
@ SCC_ENCODED
Encoded string marker and sub-string parameter.
@ SCC_ENCODED_NUMERIC
Encoded numeric parameter.
@ SCC_ENCODED_STRING
Encoded string parameter.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition error.h:26
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition error.h:27
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
void SanitizeFilename(std::string &filename)
Sanitizes a filename, i.e.
Definition fileio.cpp:1001
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:242
SaveLoadOperation
Operation performed on the file.
Definition fileio_type.h:51
@ SLO_CHECK
Load file for checking and/or preview.
Definition fileio_type.h:52
@ SLO_SAVE
File is being saved.
Definition fileio_type.h:54
@ SLO_LOAD
File is being loaded.
Definition fileio_type.h:53
@ SLO_INVALID
Unknown file operation.
Definition fileio_type.h:56
DetailedFileType
Kinds of files in each AbstractFileType.
Definition fileio_type.h:27
@ DFT_GAME_FILE
Save game or scenario file.
Definition fileio_type.h:30
@ DFT_OLD_GAME_FILE
Old save game or scenario file.
Definition fileio_type.h:29
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
@ SCENARIO_DIR
Base directory for all scenarios.
Definition fileio_type.h:91
@ BASE_DIR
Base directory for all subdirectories.
Definition fileio_type.h:88
@ SAVE_DIR
Base directory for all savegames.
Definition fileio_type.h:89
@ AUTOSAVE_DIR
Subdirectory of save for autosaves.
Definition fileio_type.h:90
@ FT_NONE
nothing to do
Definition fileio_type.h:17
@ FT_INVALID
Invalid or unknown file type.
Definition fileio_type.h:23
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition fios_gui.cpp:41
fluid_settings_t * settings
FluidSynth settings handle.
Gamelog _gamelog
Gamelog instance.
Definition gamelog.cpp:31
@ GLAT_LOAD
Game loaded.
Definition gamelog.h:18
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition gfx.cpp:1676
GameSessionStats _game_session_stats
Statistics about the current session.
Definition gfx.cpp:51
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
bool _networking
are we in networking mode?
Definition network.cpp:67
bool _network_server
network-server is active
Definition network.cpp:68
GRFConfigList _grfconfig
First item in list of current GRF set up.
GRFListCompatibility IsGoodGRFConfigList(GRFConfigList &grfconfig)
Check if all GRFs in the GRF config from a savegame can be loaded.
void ClearGRFConfigList(GRFConfigList &config)
Clear a GRF Config list, freeing all nodes.
static void SlRefVector(void *vector, VarType conv)
Save/Load a vector.
static const uint LZO_BUFFER_SIZE
Buffer size for the LZO compressor.
void SlError(StringID string, const std::string &extra_msg)
Error handler.
Definition saveload.cpp:327
static const ChunkHandler * SlFindChunkHandler(uint32_t id)
Find the ChunkHandler that will be used for processing the found chunk in the savegame or in memory.
static uint8_t GetSavegameFileType(const SaveLoad &sld)
Return the type as saved/loaded inside the savegame.
Definition saveload.cpp:558
static SaveOrLoadResult DoSave(std::shared_ptr< SaveFilter > writer, bool threaded)
Actually perform the saving of the savegame.
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition saveload.cpp:382
void FixSCCEncodedNegative(std::string &str)
Scan the string for SCC_ENCODED_NUMERIC with negative values, and reencode them as uint64_t.
Definition saveload.cpp:988
static const lzma_stream _lzma_init
Have a copy of an initialised LZMA stream.
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...
uint32_t _ttdp_version
version of TTDP savegame (if applicable)
Definition saveload.cpp:68
static const SaveLoadFormat _saveload_formats[]
The different saveload formats known/understood by OpenTTD.
std::string _savegame_format
how to compress savegames
Definition saveload.cpp:71
static void SaveFileDone()
Update the gui accordingly when saving is done and release locks on saveload.
SaveLoadVersion _sl_version
the major savegame version identifier
Definition saveload.cpp:69
static const std::vector< ChunkHandlerRef > & ChunkHandlers()
Definition saveload.cpp:208
uint8_t _sl_minor_version
the minor savegame version, DO NOT USE!
Definition saveload.cpp:70
static size_t SlCalcRefVectorLen(const void *vector, VarType conv)
Return the size in bytes of a vector.
SaveOrLoadResult LoadWithFilter(std::shared_ptr< LoadFilter > reader)
Load the game using a (reader) filter.
static SaveOrLoadResult SaveFileToDisk(bool threaded)
We have written the whole game into memory, _memory_savegame, now find and appropriate compressor and...
static void ResetSaveloadData()
Clear temporary data that is passed between various saveload phases.
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,...
SaveOrLoadResult SaveWithFilter(std::shared_ptr< SaveFilter > writer, bool threaded)
Save the game using a (writer) filter.
static void SlWriteSimpleGamma(size_t i)
Write the header descriptor of an object or an array.
Definition saveload.cpp:500
static size_t SlCalcTableHeader(const SaveLoadTable &slt)
Calculate the size of the table header.
static void ClearSaveLoadState()
Clear/free saveload state.
bool _do_autosave
are we doing an autosave at the moment?
Definition saveload.cpp:72
static std::atomic< AsyncSaveFinishProc > _async_save_finish
Callback to call when the savegame loading is finished.
Definition saveload.cpp:364
static std::thread _save_thread
The thread we're using to compress and write a savegame.
Definition saveload.cpp:365
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:594
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
static void ResetSettings()
Reset all settings to their default, so any settings missing in the savegame are their default,...
void SlWriteByte(uint8_t b)
Wrapper for writing a byte to the dumper.
Definition saveload.cpp:407
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
static size_t SlCalcArrayLen(size_t length, VarType conv)
Return the size in bytes of a certain type of atomic array.
void WriteValue(void *ptr, VarType conv, int64_t val)
Write the value of a setting.
Definition saveload.cpp:824
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition saveload.cpp:363
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:665
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
Called by save thread to tell we finished saving.
Definition saveload.cpp:371
void SetSaveLoadError(StringID str)
Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friend...
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:788
void DoAutoOrNetsave(FiosNumberedSaveName &counter)
Create an autosave or netsave.
static size_t SlCalcRefLen()
Return the size in bytes of a reference (pointer)
Definition saveload.cpp:648
NeedLength
Definition saveload.cpp:83
@ NL_WANTLENGTH
writing length and data
Definition saveload.cpp:85
@ NL_NONE
not working in NeedLength mode
Definition saveload.cpp:84
@ NL_CALCLENGTH
need to calculate the length
Definition saveload.cpp:86
static void SaveFileStart()
Update the gui accordingly when starting saving and set locks on saveload.
static void SlNullPointers()
Null all pointers (convert index -> nullptr)
Definition saveload.cpp:302
static void SlStdString(void *ptr, VarType conv)
Save/Load a std::string.
static SaveOrLoadResult DoLoad(std::shared_ptr< LoadFilter > reader, bool load_check)
Actually perform the loading of a "non-old" savegame.
static size_t SlCalcRefListLen(const void *list, VarType conv)
Return the size in bytes of a list.
static bool SlIsObjectValidInSavegame(const SaveLoad &sld)
Are we going to save this object or not?
EncodedString GetSaveLoadErrorType()
Return the appropriate initial string for an error depending on whether we are saving or loading.
void SlSaveLoadRef(void *ptr, VarType conv)
Handle conversion for references.
static void SlFixPointers()
Fix all pointers (convert index -> pointer)
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:357
void SlSkipArray()
Skip an array or sparse array.
Definition saveload.cpp:707
static void SlLoadChunk(const ChunkHandler &ch)
Load a chunk of data (eg vehicles, stations, etc.)
static void SlLoadCheckChunks()
Load all chunks for savegame checking.
static size_t SlCalcStdStringLen(const void *ptr)
Calculate the gross length of the string that it will occupy in the savegame.
Definition saveload.cpp:905
static uint SlReadSimpleGamma()
Read in the header descriptor of an object or an array.
Definition saveload.cpp:458
SaveLoadAction
What are we currently doing?
Definition saveload.cpp:75
@ SLA_LOAD
loading
Definition saveload.cpp:76
@ SLA_NULL
null all pointers (on loading error)
Definition saveload.cpp:79
@ SLA_SAVE
saving
Definition saveload.cpp:77
@ SLA_LOAD_CHECK
partial loading into _load_check_data
Definition saveload.cpp:80
@ SLA_PTRS
fixing pointers
Definition saveload.cpp:78
static void SlCopyBytes(void *ptr, size_t length)
Save/Load bytes.
Definition saveload.cpp:771
static void SlCopyInternal(void *object, size_t length, VarType conv)
Internal function to save/Load a list of SL_VARs.
SaveOrLoadResult SaveOrLoad(std::string_view filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
Main Save or Load function where the high-level saveload functions are handled.
static void SlArray(void *array, size_t length, VarType conv)
Save/Load the length of the array followed by the array of SL_VAR elements.
SavegameType _savegame_type
type of savegame we are loading
Definition saveload.cpp:65
static void SlSaveLoadConv(void *ptr, VarType conv)
Handle all conversion and typechecking of variables here.
Definition saveload.cpp:850
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition saveload.cpp:66
static void SlLoadCheckChunk(const ChunkHandler &ch)
Load a chunk of data for checking savegames.
void SlSetLength(size_t length)
Sets the length of either a RIFF object or the number of items in an array.
Definition saveload.cpp:719
uint8_t SlReadByte()
Wrapper for reading a byte from the buffer.
Definition saveload.cpp:398
void ClearOldOrders()
Clear all old orders.
Definition order_sl.cpp:111
static SaveLoadParams _sl
Parameters used for/at saveload.
Definition saveload.cpp:206
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
static void SlLoadChunks()
Load all chunks.
static void SlDeque(void *deque, VarType conv)
Save/load a std::deque.
static uint8_t SlCalcConvFileLen(VarType conv)
Return the size in bytes of a certain type of normal/atomic variable as it appears in a saved game.
Definition saveload.cpp:624
static void SlSaveChunk(const ChunkHandler &ch)
Save a chunk of data (eg.
const SaveLoadVersion SAVEGAME_VERSION
Current savegame version of OpenTTD.
size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
Calculate the size of an object.
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
EncodedString GetSaveLoadErrorMessage()
Return the description of the error.
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
bool AfterLoadGame()
Perform a (large) amount of savegame conversion magic in order to load older savegames and to fill th...
int64_t ReadValue(const void *ptr, VarType conv)
Return a signed-long version of the value of a setting.
Definition saveload.cpp:800
static void SlVector(void *vector, VarType conv)
Save/load a std::vector.
static void SaveFileError()
Show a gui message when saving has failed.
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
static std::pair< const SaveLoadFormat &, uint8_t > GetSavegameFormat(std::string_view full_name)
Return the savegameformat of the game.
void FixSCCEncoded(std::string &str, bool fix_code)
Scan the string for old values of SCC_ENCODED and fix it to it's new, value.
Definition saveload.cpp:921
static void SlSaveChunks()
Save all chunks.
std::string GenerateDefaultSaveName()
Get the default name for a savegame or screenshot.
static const size_t MEMORY_CHUNK_SIZE
Save in chunks of 128 KiB.
Definition saveload.cpp:90
static const SaveLoadFormat * DetermineSaveLoadFormat(uint32_t tag, uint32_t raw_version)
Determines the SaveLoadFormat that is connected to the given tag.
void SlAutolength(AutolengthProc *proc, int arg)
Do something of which I have no idea what it is :P.
void SlReadString(std::string &str, size_t length)
Read the given amount of bytes from the buffer into the string.
void SlSetStructListLength(size_t length)
Set the length of this list.
static uint SlGetGammaLength(size_t i)
Return how many bytes used to encode a gamma value.
Definition saveload.cpp:525
static size_t SlCalcVectorLen(const void *vector, VarType conv)
Return the size in bytes of a std::vector.
static void SlRefList(void *list, VarType conv)
Save/Load a list.
static size_t SlCalcDequeLen(const void *deque, VarType conv)
Return the size in bytes of a std::deque.
SavegameType
Types of save games.
Definition saveload.h:428
@ SGT_OTTD
OTTD savegame.
Definition saveload.h:432
SaveOrLoadResult
Save or load result codes.
Definition saveload.h:410
@ SL_OK
completed successfully
Definition saveload.h:411
@ SL_REINIT
error that was caught in the middle of updating game state, need to clear it. (can only happen during...
Definition saveload.h:413
@ SLE_VAR_NULL
useful to write zeros in savegame.
Definition saveload.h:657
@ SLE_FILE_END
Used to mark end-of-header in tables.
Definition saveload.h:630
@ SLE_FILE_TYPE_MASK
Mask to get the file-type (and not any flags).
Definition saveload.h:644
@ SLE_FILE_HAS_LENGTH_FIELD
Bit stored in savegame to indicate field has a length field for each entry.
Definition saveload.h:645
@ SLF_REPLACE_TABCRLF
Replace tabs, cr and lf in the string with spaces.
Definition saveload.h:694
@ SLF_ALLOW_NEWLINE
Allow new lines in the strings.
Definition saveload.h:693
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition saveload.h:692
@ SLE_VAR_STR
string pointer
Definition saveload.h:658
@ SLE_VAR_NAME
old custom name to be converted to a string pointer
Definition saveload.h:660
@ SLE_VAR_STRQ
string pointer enclosed in quotes
Definition saveload.h:659
@ SLE_FILE_STRINGID
StringID offset into strings-array.
Definition saveload.h:639
void SlSkipBytes(size_t length)
Read in bytes from the file/data structure but don't do anything with them, discarding them in effect...
Definition saveload.h:1352
constexpr VarType GetVarFileType(VarType type)
Get the FileType of a setting.
Definition saveload.h:767
SLRefType
Type of reference (SLE_REF, SLE_CONDREF).
Definition saveload.h:605
@ REF_VEHICLE_OLD
Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
Definition saveload.h:609
@ REF_LINK_GRAPH_JOB
Load/save a reference to a link graph job.
Definition saveload.h:616
@ REF_TOWN
Load/save a reference to a town.
Definition saveload.h:608
@ REF_LINK_GRAPH
Load/save a reference to a link graph.
Definition saveload.h:615
@ REF_CARGO_PACKET
Load/save a reference to a cargo packet.
Definition saveload.h:612
@ REF_ENGINE_RENEWS
Load/save a reference to an engine renewal (autoreplace).
Definition saveload.h:611
@ REF_STATION
Load/save a reference to a station.
Definition saveload.h:607
@ REF_ORDERLIST
Load/save a reference to an orderlist.
Definition saveload.h:613
@ REF_STORAGE
Load/save a reference to a persistent storage.
Definition saveload.h:614
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition saveload.h:606
@ REF_ROADSTOPS
Load/save a reference to a bus/truck stop.
Definition saveload.h:610
void * GetVariableAddress(const void *object, const SaveLoad &sld)
Get the address of the variable.
Definition saveload.h:1306
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:517
SaveLoadType
Type of data saved.
Definition saveload.h:700
@ SL_NULL
Save null-bytes and load to nowhere.
Definition saveload.h:714
@ SL_STRUCTLIST
Save/load a list of structs.
Definition saveload.h:711
@ SL_STDSTR
Save/load a std::string.
Definition saveload.h:705
@ SL_REF
Save/load a reference.
Definition saveload.h:702
@ SL_SAVEBYTE
Save (but not load) a byte.
Definition saveload.h:713
@ SL_DEQUE
Save/load a deque of SL_VAR elements.
Definition saveload.h:708
@ SL_STRUCT
Save/load a struct.
Definition saveload.h:703
@ SL_VECTOR
Save/load a vector of SL_VAR elements.
Definition saveload.h:709
@ SL_REFVECTOR
Save/load a vector of SL_REF elements.
Definition saveload.h:716
@ SL_REFLIST
Save/load a list of SL_REF elements.
Definition saveload.h:710
@ SL_ARR
Save/load a fixed-size array of SL_VAR elements.
Definition saveload.h:707
@ SL_VAR
Save/load a variable.
Definition saveload.h:701
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:523
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1268
constexpr VarType GetVarMemType(VarType type)
Get the NumberType of a setting.
Definition saveload.h:756
SaveLoadVersion
SaveLoad versions Previous savegame versions, the trunk revision where they were introduced and the r...
Definition saveload.h:30
@ SLV_69
69 10319
Definition saveload.h:125
@ SLV_FIX_SCC_ENCODED_NEGATIVE
353 PR#14049 Fix encoding of negative parameters.
Definition saveload.h:403
@ SLV_4
4.0 1 4.1 122 0.3.3, 0.3.4 4.2 1222 0.3.5 4.3 1417 4.4 1426
Definition saveload.h:37
@ SLV_SAVELOAD_LIST_LENGTH
293 PR#9374 Consistency in list length with SL_STRUCT / SL_STRUCTLIST / SL_DEQUE / SL_REFLIST.
Definition saveload.h:331
@ SLV_START_PATCHPACKS
220 First known patchpack to use a version just above ours.
Definition saveload.h:321
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:406
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
@ SLV_END_PATCHPACKS
286 Last known patchpack to use a version just above ours.
Definition saveload.h:322
@ SLV_ENCODED_STRING_FORMAT
350 PR#13499 Encoded String format changed.
Definition saveload.h:400
@ SLV_169
169 23816
Definition saveload.h:245
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Definition saveload.h:520
@ CH_TYPE_MASK
All ChunkType values have to be within this mask.
Definition saveload.h:463
@ CH_READONLY
Chunk is never saved.
Definition saveload.h:464
Declaration of filters used for saving and loading savegames.
Declaration of functions used in more save/load files.
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
SettingTable GetSaveLoadSettingTable()
Create a single table with all settings that should be stored/loaded in the savegame.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
@ NotInSave
Do not save with savegame, basically client-based.
@ NoNetworkSync
Do not synchronize over network (but it is saved if SettingFlag::NotInSave is not set).
static constexpr const SettingDesc * GetSettingDesc(const SettingVariant &desc)
Helper to convert the type of the iterated settings description to a pointer to it.
@ SBI_SAVELOAD_FINISH
finished saving
@ SBI_SAVELOAD_START
started saving
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
void StrMakeValidInPlace(char *str, StringValidationSettings settings)
Scans the string for invalid characters and replaces them with a question mark '?' (if not ignored).
Definition string.cpp:155
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
@ AllowControlCode
Allow the special control codes.
@ AllowNewline
Allow newlines; replaces '\r ' with ' ' during processing.
@ ReplaceTabCrNlWithSpace
Replace tabs ('\t'), carriage returns ('\r') and newlines (' ') with spaces.
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition strings.cpp:327
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:91
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Container for cargo from the same location and time.
Definition cargopacket.h:41
Handlers and description of chunk.
Definition saveload.h:468
ChunkType type
Type of the chunk.
Definition saveload.h:470
virtual void LoadCheck(size_t len=0) const
Load the chunk for game preview.
virtual void Load() const =0
Load the chunk.
uint32_t id
Unique ID (4 letters).
Definition saveload.h:469
virtual void Save() const
Save the chunk.
Definition saveload.h:480
GUISettings gui
settings related to the GUI
Struct to store engine replacements.
Yes, simply reading from a file.
~FileReader()
Make sure everything is cleaned up.
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
void Reset() override
Reset this filter to read from the beginning of the file.
FileReader(FileHandle &&file)
Create the file reader, so it reads from a specific file.
long begin
The begin of the file.
std::optional< FileHandle > file
The file to read from.
Deals with the type of the savegame, independent of extension.
Definition saveload.h:417
void SetMode(const FiosType &ft, SaveLoadOperation fop=SLO_LOAD)
Set the mode and file type of the file to save or load.
std::string title
Internal name of the game.
Definition saveload.h:421
FiosType ftype
File type.
Definition saveload.h:419
SaveLoadOperation file_op
File operation to perform.
Definition saveload.h:418
std::string name
Name of the file.
Definition saveload.h:420
void Set(const FiosItem &item)
Set the title of the file.
Yes, simply writing to a file.
std::optional< FileHandle > file
The file to write to.
FileWriter(FileHandle &&file)
Create the file writer, so it writes to a specific file.
void Finish() override
Prepare everything to finish writing the savegame.
~FileWriter()
Make sure everything is cleaned up.
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Deals with finding savegames.
Definition fios.h:78
A savegame name automatically numbered.
Definition fios.h:129
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition fios.cpp:748
std::string Extension()
Generate an extension for a savegame name.
Definition fios.cpp:758
Elements of a file system that are recognized.
Definition fileio_type.h:62
AbstractFileType abstract
Abstract file type.
Definition fileio_type.h:63
bool keep_all_autosave
name the autosave in a different way
uint8_t date_format_in_default_names
should the default savegame/screenshot name use long dates (31th Dec 2008), short dates (31-12-2008) ...
bool threaded_saves
should we do threaded saves?
std::optional< size_t > savegame_size
Size of the last saved savegame in bytes, or std::nullopt if not saved yet.
Definition openttd.h:58
Filter without any compression.
~LZMALoadFilter()
Clean everything up.
lzma_stream lzma
Stream state that we are reading from.
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
uint8_t fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
LZMALoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Filter using LZMA compression.
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
void Finish() override
Prepare everything to finish writing the savegame.
void WriteLoop(uint8_t *p, size_t len, lzma_action action)
Helper loop for writing the data.
LZMASaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Initialise this filter.
lzma_stream lzma
Stream state that we are writing to.
uint8_t fwrite_buf[MEMORY_CHUNK_SIZE]
Buffer for writing to the file.
~LZMASaveFilter()
Clean up what we allocated.
Filter using LZO compression.
LZOLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
size_t Read(uint8_t *buf, size_t ssize) override
Read a given number of bytes from the savegame.
Filter using LZO compression.
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
LZOSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t)
Initialise this filter.
bool checkable
True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable....
Definition fios.h:34
std::string error_msg
Data to pass to string parameters when displaying error.
Definition fios.h:36
StringID error
Error message from loading. INVALID_STRING_ID if no error.
Definition fios.h:35
void Clear()
Reset read data.
Definition fios_gui.cpp:49
GRFListCompatibility grf_compatibility
Summary state of NewGrfs, whether missing files or only compatible found.
Definition fios.h:48
GRFConfigList grfconfig
NewGrf configuration from save.
Definition fios.h:47
Interface for filtering a savegame till it is loaded.
std::shared_ptr< LoadFilter > chain
Chained to the (savegame) filters.
Container for dumping the savegame (quickly) to memory.
Definition saveload.cpp:134
uint8_t * buf
Buffer we're going to write to.
Definition saveload.cpp:136
void WriteByte(uint8_t b)
Write a single byte into the dumper.
Definition saveload.cpp:143
std::vector< std::unique_ptr< uint8_t[]> > blocks
Buffer with blocks of allocated memory.
Definition saveload.cpp:135
uint8_t * bufe
End of the buffer we write to.
Definition saveload.cpp:137
size_t GetSize() const
Get the size of the memory dump made so far.
Definition saveload.cpp:177
void Flush(std::shared_ptr< SaveFilter > writer)
Flush this dumper into a writer.
Definition saveload.cpp:158
Filter without any compression.
NoCompLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
Filter without any compression.
NoCompSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t)
Initialise this filter.
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:264
Class for pooled persistent storage of data.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * Get(auto index)
Returns Titem with given index.
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
A buffer for reading (and buffering) savegame data.
Definition saveload.cpp:93
uint8_t * bufp
Location we're at reading the buffer.
Definition saveload.cpp:95
ReadBuffer(std::shared_ptr< LoadFilter > reader)
Initialise our variables.
Definition saveload.cpp:104
size_t read
The amount of read bytes so far from the filter.
Definition saveload.cpp:98
size_t GetSize() const
Get the size of the memory dump made so far.
Definition saveload.cpp:126
std::shared_ptr< LoadFilter > reader
The filter used to actually read.
Definition saveload.cpp:97
uint8_t buf[MEMORY_CHUNK_SIZE]
Buffer we're going to read from.
Definition saveload.cpp:94
uint8_t * bufe
End of the buffer we can read from.
Definition saveload.cpp:96
A Stop for a Road Vehicle.
Interface for filtering a savegame till it is written.
std::shared_ptr< SaveFilter > chain
Chained to the (savegame) filters.
The format for a reader/writer type of a savegame.
uint32_t tag
the 4-letter tag by which it is identified in the savegame
uint8_t min_compression
the minimum compression level of this format
std::shared_ptr< SaveFilter >(* init_write)(std::shared_ptr< SaveFilter > chain, uint8_t compression)
Constructor for the save filter.
uint8_t default_compression
the default compression level of this format
std::shared_ptr< LoadFilter >(* init_load)(std::shared_ptr< LoadFilter > chain)
Constructor for the load filter.
std::string_view name
name of the compressor/decompressor (debug-only)
uint8_t max_compression
the maximum compression level of this format
The saveload struct, containing reader-writer functions, buffer, version, etc.
Definition saveload.cpp:184
std::unique_ptr< ReadBuffer > reader
Savegame reading buffer.
Definition saveload.cpp:197
std::shared_ptr< SaveFilter > sf
Filter to write the savegame to.
Definition saveload.cpp:195
std::unique_ptr< MemoryDumper > dumper
Memory dumper to write the savegame to.
Definition saveload.cpp:194
StringID error_str
the translatable error message to show
Definition saveload.cpp:200
SaveLoadAction action
are we doing a save or a load atm.
Definition saveload.cpp:185
std::string extra_msg
the error message
Definition saveload.cpp:201
NeedLength need_length
working in NeedLength (Autolength) mode?
Definition saveload.cpp:186
uint8_t block_mode
???
Definition saveload.cpp:187
bool saveinprogress
Whether there is currently a save in progress.
Definition saveload.cpp:203
std::shared_ptr< LoadFilter > lf
Filter to read the savegame from.
Definition saveload.cpp:198
bool expect_table_header
In the case of a table, if the header is saved/loaded.
Definition saveload.cpp:192
size_t obj_len
the length of the current object we are busy with
Definition saveload.cpp:190
bool error
did an error occur or not
Definition saveload.cpp:188
int last_array_index
in the case of an array, the current and last positions
Definition saveload.cpp:191
SaveLoad type struct.
Definition saveload.h:722
uint16_t length
(Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
Definition saveload.h:726
std::shared_ptr< SaveLoadHandler > handler
Custom handler for Save/Load procs.
Definition saveload.h:731
SaveLoadVersion version_to
Save/load the variable before this savegame version.
Definition saveload.h:728
SaveLoadType cmd
The action to take with the saved/loaded type, All types need different action.
Definition saveload.h:724
std::string name
Name of this field (optional, used for tables).
Definition saveload.h:723
VarType conv
Type of the variable to be saved; this field combines both FileVarType and MemVarType.
Definition saveload.h:725
SaveLoadVersion version_from
Save/load the variable starting from this savegame version.
Definition saveload.h:727
Properties of config file settings.
SettingFlags flags
Handles how a setting would show up in the GUI (text/currency, etc.).
virtual void ResetToDefault(void *object) const =0
Reset the setting to its default value.
static bool IsValidID(auto index)
Tests whether given index is a valid index for station of this type.
static Station * Get(auto index)
Gets station with given index.
Station data structure.
Town data structure.
Definition town.h:52
Vehicle data structure.
Filter using Zlib compression.
size_t Read(uint8_t *buf, size_t size) override
Read a given number of bytes from the savegame.
ZlibLoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
uint8_t fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
~ZlibLoadFilter()
Clean everything up.
z_stream z
Stream state we are reading from.
Filter using Zlib compression.
void WriteLoop(uint8_t *p, size_t len, int mode)
Helper loop for writing the data.
z_stream z
Stream state we are writing to.
uint8_t fwrite_buf[MEMORY_CHUNK_SIZE]
Buffer for writing to the file.
void Finish() override
Prepare everything to finish writing the savegame.
~ZlibSaveFilter()
Clean up what we allocated.
ZlibSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Initialise this filter.
void Write(uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition thread.h:24
bool StartNewThread(std::thread *thr, std::string_view name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition thread.h:47
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3265
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition window_type.h:66