OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
22
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"
34#include "../vehicle_base.h"
35#include "../company_func.h"
37#include "../autoreplace_base.h"
38#include "../roadstop_base.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"
51#include "saveload_error.hpp"
52#include "saveload_filter.h"
53#include "saveload_func.h"
54#include "saveload_internal.h"
55
56#include <atomic>
57#ifdef __EMSCRIPTEN__
58# include <emscripten.h>
59#endif
60
61#ifdef WITH_LZO
62#include <lzo/lzo1x.h>
63#endif
64
65#if defined(WITH_ZLIB)
66#include <zlib.h>
67#endif /* WITH_ZLIB */
68
69#if defined(WITH_LIBLZMA)
70#include <lzma.h>
71#endif /* WITH_LIBLZMA */
72
74#include "table/strings.h"
75
76#include "../safeguards.h"
77
79
82
83uint32_t _ttdp_version;
86std::string _savegame_format;
88
90enum class SaveLoadAction : uint8_t {
96};
97
98enum class NeedLength : uint8_t {
102};
103
105static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
106
110 uint8_t *bufp = nullptr;
111 uint8_t *bufe = nullptr;
112 std::shared_ptr<LoadFilter> reader{};
113 size_t read = 0;
114
119 ReadBuffer(std::shared_ptr<LoadFilter> reader) : reader(std::move(reader))
120 {
121 }
122
123 inline uint8_t ReadByte()
124 {
125 if (this->bufp == this->bufe) {
126 size_t len = this->reader->Read(this->buf, lengthof(this->buf));
127 if (len == 0) SlErrorCorrupt("Unexpected end of chunk");
128
129 this->read += len;
130 this->bufp = this->buf;
131 this->bufe = this->buf + len;
132 }
133
134 return *this->bufp++;
135 }
136
141 size_t GetSize() const
142 {
143 return this->read - (this->bufe - this->bufp);
144 }
145};
146
147
150 std::vector<std::unique_ptr<uint8_t[]>> blocks{};
151 uint8_t *buf = nullptr;
152 uint8_t *bufe = nullptr;
153
158 inline void WriteByte(uint8_t b)
159 {
160 /* Are we at the end of this chunk? */
161 if (this->buf == this->bufe) {
162 this->buf = this->blocks.emplace_back(std::make_unique<uint8_t[]>(MEMORY_CHUNK_SIZE)).get();
163 this->bufe = this->buf + MEMORY_CHUNK_SIZE;
164 }
165
166 *this->buf++ = b;
167 }
168
173 void Flush(std::shared_ptr<SaveFilter> writer)
174 {
175 uint i = 0;
176 size_t t = this->GetSize();
177
178 while (t > 0) {
179 size_t to_write = std::min(MEMORY_CHUNK_SIZE, t);
180
181 writer->Write(this->blocks[i++].get(), to_write);
182 t -= to_write;
183 }
184
185 writer->Finish();
186 }
187
192 size_t GetSize() const
193 {
194 return this->blocks.size() * MEMORY_CHUNK_SIZE - (this->bufe - this->buf);
195 }
196};
197
203 bool error;
204
205 size_t obj_len;
206 int array_index, last_array_index;
208
209 std::unique_ptr<MemoryDumper> dumper;
210 std::shared_ptr<SaveFilter> sf;
211
212 std::unique_ptr<ReadBuffer> reader;
213 std::shared_ptr<LoadFilter> lf;
214
216 std::string extra_msg;
217
219};
220
222
223static const std::vector<ChunkHandlerRef> &ChunkHandlers()
224{
225 /* These define the chunks */
226 extern const ChunkHandlerTable _gamelog_chunk_handlers;
227 extern const ChunkHandlerTable _map_chunk_handlers;
228 extern const ChunkHandlerTable _misc_chunk_handlers;
229 extern const ChunkHandlerTable _name_chunk_handlers;
230 extern const ChunkHandlerTable _cheat_chunk_handlers;
231 extern const ChunkHandlerTable _setting_chunk_handlers;
232 extern const ChunkHandlerTable _company_chunk_handlers;
233 extern const ChunkHandlerTable _engine_chunk_handlers;
234 extern const ChunkHandlerTable _veh_chunk_handlers;
235 extern const ChunkHandlerTable _waypoint_chunk_handlers;
236 extern const ChunkHandlerTable _depot_chunk_handlers;
237 extern const ChunkHandlerTable _order_chunk_handlers;
238 extern const ChunkHandlerTable _town_chunk_handlers;
239 extern const ChunkHandlerTable _sign_chunk_handlers;
240 extern const ChunkHandlerTable _station_chunk_handlers;
241 extern const ChunkHandlerTable _industry_chunk_handlers;
242 extern const ChunkHandlerTable _economy_chunk_handlers;
243 extern const ChunkHandlerTable _subsidy_chunk_handlers;
244 extern const ChunkHandlerTable _cargomonitor_chunk_handlers;
245 extern const ChunkHandlerTable _goal_chunk_handlers;
246 extern const ChunkHandlerTable _story_page_chunk_handlers;
247 extern const ChunkHandlerTable _league_chunk_handlers;
248 extern const ChunkHandlerTable _ai_chunk_handlers;
249 extern const ChunkHandlerTable _game_chunk_handlers;
250 extern const ChunkHandlerTable _animated_tile_chunk_handlers;
251 extern const ChunkHandlerTable _newgrf_chunk_handlers;
252 extern const ChunkHandlerTable _group_chunk_handlers;
253 extern const ChunkHandlerTable _cargopacket_chunk_handlers;
254 extern const ChunkHandlerTable _autoreplace_chunk_handlers;
255 extern const ChunkHandlerTable _labelmaps_chunk_handlers;
256 extern const ChunkHandlerTable _linkgraph_chunk_handlers;
257 extern const ChunkHandlerTable _airport_chunk_handlers;
258 extern const ChunkHandlerTable _object_chunk_handlers;
259 extern const ChunkHandlerTable _persistent_storage_chunk_handlers;
260 extern const ChunkHandlerTable _water_region_chunk_handlers;
261 extern const ChunkHandlerTable _randomizer_chunk_handlers;
262
264 static const ChunkHandlerTable _chunk_handler_tables[] = {
265 _gamelog_chunk_handlers,
266 _map_chunk_handlers,
267 _misc_chunk_handlers,
268 _name_chunk_handlers,
269 _cheat_chunk_handlers,
270 _setting_chunk_handlers,
271 _veh_chunk_handlers,
272 _waypoint_chunk_handlers,
273 _depot_chunk_handlers,
274 _order_chunk_handlers,
275 _industry_chunk_handlers,
276 _economy_chunk_handlers,
277 _subsidy_chunk_handlers,
278 _cargomonitor_chunk_handlers,
279 _goal_chunk_handlers,
280 _story_page_chunk_handlers,
281 _league_chunk_handlers,
282 _engine_chunk_handlers,
283 _town_chunk_handlers,
284 _sign_chunk_handlers,
285 _station_chunk_handlers,
286 _company_chunk_handlers,
287 _ai_chunk_handlers,
288 _game_chunk_handlers,
289 _animated_tile_chunk_handlers,
290 _newgrf_chunk_handlers,
291 _group_chunk_handlers,
292 _cargopacket_chunk_handlers,
293 _autoreplace_chunk_handlers,
294 _labelmaps_chunk_handlers,
295 _linkgraph_chunk_handlers,
296 _airport_chunk_handlers,
297 _object_chunk_handlers,
298 _persistent_storage_chunk_handlers,
299 _water_region_chunk_handlers,
300 _randomizer_chunk_handlers,
301 };
302
303 static std::vector<ChunkHandlerRef> _chunk_handlers;
304
305 if (_chunk_handlers.empty()) {
306 for (auto &chunk_handler_table : _chunk_handler_tables) {
307 for (auto &chunk_handler : chunk_handler_table) {
308 _chunk_handlers.push_back(chunk_handler);
309 }
310 }
311 }
312
313 return _chunk_handlers;
314}
315
317static void SlNullPointers()
318{
319 _sl.action = SaveLoadAction::Null;
320
321 /* We don't want any savegame conversion code to run
322 * during NULLing; especially those that try to get
323 * pointers from other pools. */
325
326 for (const ChunkHandler &ch : ChunkHandlers()) {
327 Debug(Facility::Sl, Severity::Notice, "Nulling pointers for {}", ch.GetName());
328 ch.FixPointers();
329 }
330
331 assert(_sl.action == SaveLoadAction::Null);
332}
333
342[[noreturn]] void SlError(StringID string, const std::string &extra_msg)
343{
344 /* Distinguish between loading into _load_check_data vs. normal save/load. */
345 if (_sl.action == SaveLoadAction::LoadCheck) {
346 _load_check_data.error = string;
347 _load_check_data.error_msg = extra_msg;
348 } else {
349 _sl.error_str = string;
350 _sl.extra_msg = extra_msg;
351 }
352
353 /* We have to nullptr all pointers here; we might be in a state where
354 * the pointers are actually filled with indices, which means that
355 * when we access them during cleaning the pool dereferences of
356 * those indices will be made with segmentation faults as result. */
357 if (_sl.action == SaveLoadAction::Load || _sl.action == SaveLoadAction::Ptrs) SlNullPointers();
358
359 /* Logging could be active. */
360 _gamelog.StopAnyAction();
361
362 throw std::exception();
363}
364
372[[noreturn]] void SlErrorCorrupt(const std::string &msg)
373{
374 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
375}
376
377
378typedef void (*AsyncSaveFinishProc)();
379static std::atomic<AsyncSaveFinishProc> _async_save_finish;
380static std::thread _save_thread;
381
387{
388 if (_exit_game) return;
389 while (_async_save_finish.load(std::memory_order_acquire) != nullptr) CSleep(10);
390
391 _async_save_finish.store(proc, std::memory_order_release);
392}
393
398{
399 AsyncSaveFinishProc proc = _async_save_finish.exchange(nullptr, std::memory_order_acq_rel);
400 if (proc == nullptr) return;
401
402 proc();
403
404 if (_save_thread.joinable()) {
405 _save_thread.join();
406 }
407}
408
413uint8_t SlReadByte()
414{
415 return _sl.reader->ReadByte();
416}
417
422void SlWriteByte(uint8_t b)
423{
424 _sl.dumper->WriteByte(b);
425}
426
427static inline int SlReadUint16()
428{
429 int x = SlReadByte() << 8;
430 return x | SlReadByte();
431}
432
433static inline uint32_t SlReadUint32()
434{
435 uint32_t x = SlReadUint16() << 16;
436 return x | SlReadUint16();
437}
438
439static inline uint64_t SlReadUint64()
440{
441 uint32_t x = SlReadUint32();
442 uint32_t y = SlReadUint32();
443 return static_cast<uint64_t>(x) << 32 | y;
444}
445
446static inline void SlWriteUint16(uint16_t v)
447{
448 SlWriteByte(GB(v, 8, 8));
449 SlWriteByte(GB(v, 0, 8));
450}
451
452static inline void SlWriteUint32(uint32_t v)
453{
454 SlWriteUint16(GB(v, 16, 16));
455 SlWriteUint16(GB(v, 0, 16));
456}
457
458static inline void SlWriteUint64(uint64_t x)
459{
460 SlWriteUint32(static_cast<uint32_t>(x >> 32));
461 SlWriteUint32(static_cast<uint32_t>(x));
462}
463
468static inline ChunkId SlReadChunkId()
469{
470 ChunkId label{};
471 for (uint8_t &b : label) b = SlReadByte();
472 return label;
473}
474
484static uint SlReadSimpleGamma()
485{
486 uint i = SlReadByte();
487 if (HasBit(i, 7)) {
488 i &= ~0x80;
489 if (HasBit(i, 6)) {
490 i &= ~0x40;
491 if (HasBit(i, 5)) {
492 i &= ~0x20;
493 if (HasBit(i, 4)) {
494 i &= ~0x10;
495 if (HasBit(i, 3)) {
496 SlErrorCorrupt("Unsupported gamma");
497 }
498 i = SlReadByte(); // 32 bits only.
499 }
500 i = (i << 8) | SlReadByte();
501 }
502 i = (i << 8) | SlReadByte();
503 }
504 i = (i << 8) | SlReadByte();
505 }
506 return i;
507}
508
525
526static void SlWriteSimpleGamma(size_t i)
527{
528 if (i >= (1 << 7)) {
529 if (i >= (1 << 14)) {
530 if (i >= (1 << 21)) {
531 if (i >= (1 << 28)) {
532 assert(i <= UINT32_MAX); // We can only support 32 bits for now.
533 SlWriteByte(static_cast<uint8_t>(0xF0));
534 SlWriteByte(static_cast<uint8_t>(i >> 24));
535 } else {
536 SlWriteByte(static_cast<uint8_t>(0xE0 | (i >> 24)));
537 }
538 SlWriteByte(static_cast<uint8_t>(i >> 16));
539 } else {
540 SlWriteByte(static_cast<uint8_t>(0xC0 | (i >> 16)));
541 }
542 SlWriteByte(static_cast<uint8_t>(i >> 8));
543 } else {
544 SlWriteByte(static_cast<uint8_t>(0x80 | (i >> 8)));
545 }
546 }
547 SlWriteByte(static_cast<uint8_t>(i));
548}
549
555static inline uint SlGetGammaLength(size_t i)
556{
557 return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21)) + (i >= (1 << 28));
558}
559
560static inline uint SlReadSparseIndex()
561{
562 return SlReadSimpleGamma();
563}
564
565static inline void SlWriteSparseIndex(uint index)
566{
567 SlWriteSimpleGamma(index);
568}
569
570static inline uint SlReadArrayLength()
571{
572 return SlReadSimpleGamma();
573}
574
575static inline void SlWriteArrayLength(size_t length)
576{
577 SlWriteSimpleGamma(length);
578}
579
580static inline uint SlGetArrayLength(size_t length)
581{
582 return SlGetGammaLength(length);
583}
584
590 static constexpr uint8_t HAS_FIELD_LENGTH_BIT = 4;
591 uint8_t storage{};
592
595
601 SavegameFileType(VarFileType file_type, bool has_field_length = false) : storage(to_underlying(file_type))
602 {
603 /* 0 is not allowed as it's the end-of-table marker, larger is not allowed due to the field length bit. */
604 assert(IsInsideMM(to_underlying(file_type), 1, 1 << HAS_FIELD_LENGTH_BIT));
605 AssignBit(this->storage, HAS_FIELD_LENGTH_BIT, has_field_length);
606 }
607
612 constexpr bool IsEnd() const { return storage == 0; }
613
618 constexpr bool HasFieldLength() const
619 {
620 assert(!this->IsEnd());
621 return HasBit(storage, HAS_FIELD_LENGTH_BIT);
622 }
623
628 constexpr VarFileType Type() const
629 {
630 assert(!this->IsEnd());
631 return static_cast<VarFileType>(GB(storage, 0, HAS_FIELD_LENGTH_BIT));
632 }
633};
634
641{
642 switch (sld.cmd) {
644 return sld.conv.file;
645
649 return { sld.conv.file, true };
650
653
657
659 return VarFileType::U8;
660
663 return { VarFileType::Struct, true };
664
665 default: NOT_REACHED();
666 }
667}
668
675static inline uint SlCalcConvMemLen(VarMemType conv)
676{
677 switch (conv) {
678 case VarMemType::Bool: return sizeof(bool);
679 case VarMemType::I8: return sizeof(int8_t);
680 case VarMemType::U8: return sizeof(uint8_t);
681 case VarMemType::I16: return sizeof(int16_t);
682 case VarMemType::U16: return sizeof(uint16_t);
683 case VarMemType::I32: return sizeof(int32_t);
684 case VarMemType::U32: return sizeof(uint32_t);
685 case VarMemType::I64: return sizeof(int64_t);
686 case VarMemType::U64: return sizeof(uint64_t);
687 case VarMemType::Null: return 0;
688 case VarMemType::Label: return sizeof(BaseLabel);
689
690 case VarMemType::Str:
691 return SlReadArrayLength();
692
693 case VarMemType::Name:
694 default:
695 NOT_REACHED();
696 }
697}
698
705static inline uint8_t SlCalcConvFileLen(VarType conv)
706{
707 switch (conv.file) {
708 case VarFileType::I8: return sizeof(int8_t);
709 case VarFileType::U8: return sizeof(uint8_t);
710 case VarFileType::I16: return sizeof(int16_t);
711 case VarFileType::U16: return sizeof(uint16_t);
712 case VarFileType::I32: return sizeof(int32_t);
713 case VarFileType::U32: return sizeof(uint32_t);
714 case VarFileType::I64: return sizeof(int64_t);
715 case VarFileType::U64: return sizeof(uint64_t);
716 case VarFileType::StringID: return sizeof(uint16_t);
717
719 return SlReadArrayLength();
720
722 default:
723 NOT_REACHED();
724 }
725}
726
731static inline size_t SlCalcRefLen()
732{
734}
735
736void SlSetArrayIndex(uint index)
737{
738 _sl.need_length = NeedLength::WantLength;
739 _sl.array_index = index;
740}
741
742static size_t _next_offs;
743
749{
750 /* After reading in the whole array inside the loop
751 * we must have read in all the data, so we must be at end of current block. */
752 if (_next_offs != 0 && _sl.reader->GetSize() != _next_offs) {
753 SlErrorCorruptFmt("Invalid chunk size iterating array - expected to be at position {}, actually at {}", _next_offs, _sl.reader->GetSize());
754 }
755
756 for (;;) {
757 uint length = SlReadArrayLength();
758 if (length == 0) {
759 assert(!_sl.expect_table_header);
760 _next_offs = 0;
761 return -1;
762 }
763
764 _sl.obj_len = --length;
765 _next_offs = _sl.reader->GetSize() + length;
766
767 if (_sl.expect_table_header) {
768 _sl.expect_table_header = false;
769 return INT32_MAX;
770 }
771
772 int index;
773 switch (_sl.chunk_type) {
775 case ChunkType::SparseArray: index = static_cast<int>(SlReadSparseIndex()); break;
776 case ChunkType::Table:
777 case ChunkType::Array: index = _sl.array_index++; break;
778 default:
779 Debug(Facility::Sl, Severity::Critical, "SlIterateArray error");
780 return -1; // error
781 }
782
783 if (length != 0) return index;
784 }
785}
786
791{
792 while (SlIterateArray() != -1) {
793 SlSkipBytes(_next_offs - _sl.reader->GetSize());
794 }
795}
796
802void SlSetLength(size_t length)
803{
804 assert(_sl.action == SaveLoadAction::Save);
805
806 switch (_sl.need_length) {
808 _sl.need_length = NeedLength::None;
809 if ((_sl.chunk_type == ChunkType::Table || _sl.chunk_type == ChunkType::SparseTable) && _sl.expect_table_header) {
810 _sl.expect_table_header = false;
811 SlWriteArrayLength(length + 1);
812 break;
813 }
814
815 switch (_sl.chunk_type) {
816 case ChunkType::Riff:
817 /* Ugly encoding of >16M RIFF chunks
818 * The lower 24 bits are normal
819 * The uppermost 4 bits are bits 24:27 */
820 assert(length < (1 << 28));
821 SlWriteUint32((uint32_t)((length & 0xFFFFFF) | ((length >> 24) << 28)));
822 break;
823 case ChunkType::Table:
824 case ChunkType::Array:
825 assert(_sl.last_array_index <= _sl.array_index);
826 while (++_sl.last_array_index <= _sl.array_index) {
827 SlWriteArrayLength(1);
828 }
829 SlWriteArrayLength(length + 1);
830 break;
833 SlWriteArrayLength(length + 1 + SlGetArrayLength(_sl.array_index)); // Also include length of sparse index.
834 SlWriteSparseIndex(_sl.array_index);
835 break;
836 default: NOT_REACHED();
837 }
838 break;
839
841 _sl.obj_len += static_cast<int>(length);
842 break;
843
844 default: NOT_REACHED();
845 }
846}
847
854static void SlCopyBytes(void *ptr, size_t length)
855{
856 uint8_t *p = static_cast<uint8_t *>(ptr);
857
858 switch (_sl.action) {
861 for (; length != 0; length--) *p++ = SlReadByte();
862 break;
864 for (; length != 0; length--) SlWriteByte(*p++);
865 break;
866 default: NOT_REACHED();
867 }
868}
869
875{
876 return _sl.obj_len;
877}
878
886int64_t ReadValue(const void *ptr, VarMemType conv)
887{
888 switch (conv) {
889 case VarMemType::Bool: return (*static_cast<const bool *>(ptr) != 0);
890 case VarMemType::I8: return *static_cast<const int8_t *>(ptr);
891 case VarMemType::U8: return *static_cast<const uint8_t *>(ptr);
892 case VarMemType::I16: return *static_cast<const int16_t *>(ptr);
893 case VarMemType::U16: return *static_cast<const uint16_t *>(ptr);
894 case VarMemType::I32: return *static_cast<const int32_t *>(ptr);
895 case VarMemType::U32: return *static_cast<const uint32_t *>(ptr);
896 case VarMemType::I64: return *static_cast<const int64_t *>(ptr);
897 case VarMemType::U64: return *static_cast<const uint64_t *>(ptr);
898 case VarMemType::Null: return 0;
899 default: NOT_REACHED();
900 }
901}
902
910void WriteValue(void *ptr, VarMemType conv, int64_t val)
911{
912 switch (conv) {
913 case VarMemType::Bool: *static_cast<bool *>(ptr) = (val != 0); break;
914 case VarMemType::I8: *static_cast<int8_t *>(ptr) = val; break;
915 case VarMemType::U8: *static_cast<uint8_t *>(ptr) = val; break;
916 case VarMemType::I16: *static_cast<int16_t *>(ptr) = val; break;
917 case VarMemType::U16: *static_cast<uint16_t *>(ptr) = val; break;
918 case VarMemType::I32: *static_cast<int32_t *>(ptr) = val; break;
919 case VarMemType::U32: *static_cast<uint32_t *>(ptr) = val; break;
920 case VarMemType::I64: *static_cast<int64_t *>(ptr) = val; break;
921 case VarMemType::U64: *static_cast<uint64_t *>(ptr) = val; break;
922 case VarMemType::Name: *reinterpret_cast<std::string *>(ptr) = CopyFromOldName(static_cast<StringID>(val)); break;
923 case VarMemType::Null: break;
924 default: NOT_REACHED();
925 }
926}
927
936static void SlSaveLoadConv(void *ptr, VarType conv)
937{
938 switch (_sl.action) {
940 if (conv == VarTypes::LABEL) {
941 /* Labels are written in reverse order as that is the way GrfIDs used to be written.
942 * Changing the order means changing external applications that extract this data. */
943 BaseLabel *label = static_cast<BaseLabel *>(ptr);
944 for (auto it = label->rbegin(); it != label->rend(); it++) SlWriteByte(*it);
945 break;
946 }
947
948 int64_t x = ReadValue(ptr, conv.mem);
949
950 /* Write the value to the file and check if its value is in the desired range */
951 switch (conv.file) {
952 case VarFileType::I8:
953 assert(x >= -128 && x <= 127);
954 SlWriteByte(x);
955 break;
956
957 case VarFileType::U8:
958 assert(x >= 0 && x <= 255);
959 SlWriteByte(x);
960 break;
961
962 case VarFileType::I16:
963 assert(x >= -32768 && x <= 32767);
964 SlWriteUint16(x);
965 break;
966
968 case VarFileType::U16:
969 assert(x >= 0 && x <= 65535);
970 SlWriteUint16(x);
971 break;
972
973 case VarFileType::I32:
974 case VarFileType::U32:
975 SlWriteUint32(static_cast<uint32_t>(x));
976 break;
977
978 case VarFileType::I64:
979 case VarFileType::U64:
980 SlWriteUint64(x);
981 break;
982
983 default: NOT_REACHED();
984 }
985 break;
986 }
989 if (conv == VarTypes::LABEL) {
990 /* Labels are written in reverse order as that is the way GrfIDs used to be written.
991 * Changing the order means changing external applications that extract this data.
992 * The road/rail type labels were in forward order. They are fixed when needed in
993 * their respective loaders. */
994 BaseLabel *label = static_cast<BaseLabel *>(ptr);
995 for (auto it = label->rbegin(); it != label->rend(); it++) *it = SlReadByte();
996 break;
997 }
998
999 int64_t x;
1000 /* Read a value from the file */
1001 switch (conv.file) {
1002 case VarFileType::I8: x = static_cast<int8_t>(SlReadByte()); break;
1003 case VarFileType::U8: x = static_cast<uint8_t>(SlReadByte()); break;
1004 case VarFileType::I16: x = static_cast<int16_t>(SlReadUint16()); break;
1005 case VarFileType::U16: x = static_cast<uint16_t>(SlReadUint16()); break;
1006 case VarFileType::I32: x = static_cast<int32_t>(SlReadUint32()); break;
1007 case VarFileType::U32: x = static_cast<uint32_t>(SlReadUint32()); break;
1008 case VarFileType::I64: x = static_cast<int64_t>(SlReadUint64()); break;
1009 case VarFileType::U64: x = static_cast<uint64_t>(SlReadUint64()); break;
1010 case VarFileType::StringID: x = RemapOldStringID(static_cast<StringID>(SlReadUint16())).base(); break;
1011 default: NOT_REACHED();
1012 }
1013
1014 /* Write The value to the struct. These ARE endian safe. */
1015 WriteValue(ptr, conv.mem, x);
1016 break;
1017 }
1018 case SaveLoadAction::Ptrs: break;
1019 case SaveLoadAction::Null: break;
1020 default: NOT_REACHED();
1021 }
1022}
1023
1031static inline size_t SlCalcStdStringLen(const void *ptr)
1032{
1033 const std::string *str = reinterpret_cast<const std::string *>(ptr);
1034
1035 size_t len = str->length();
1036 return len + SlGetArrayLength(len); // also include the length of the index
1037}
1038
1039
1048void FixSCCEncoded(std::string &str, bool fix_code)
1049{
1050 if (str.empty()) return;
1051
1052 /* We need to convert from old escape-style encoding to record separator encoding.
1053 * Initial `<SCC_ENCODED><STRINGID>` stays the same.
1054 *
1055 * `:<SCC_ENCODED><STRINGID>` becomes `<RS><SCC_ENCODED><STRINGID>`
1056 * `:<HEX>` becomes `<RS><SCC_ENCODED_NUMERIC><HEX>`
1057 * `:"<STRING>"` becomes `<RS><SCC_ENCODED_STRING><STRING>`
1058 */
1059 std::string result;
1060 StringBuilder builder(result);
1061
1062 bool is_encoded = false; // Set if we determine by the presence of SCC_ENCODED that the string is an encoded string.
1063 bool in_string = false; // Set if we in a string, between double-quotes.
1064 bool need_type = true; // Set if a parameter type needs to be emitted.
1065
1066 StringConsumer consumer(str);
1067 while (consumer.AnyBytesLeft()) {
1068 char32_t c;
1069 if (auto r = consumer.TryReadUtf8(); r.has_value()) {
1070 c = *r;
1071 } else {
1072 break;
1073 }
1074 if (c == SCC_ENCODED || (fix_code && (c == 0xE028 || c == 0xE02A))) {
1075 builder.PutUtf8(SCC_ENCODED);
1076 need_type = false;
1077 is_encoded = true;
1078 continue;
1079 }
1080
1081 /* If the first character is not SCC_ENCODED then we don't have to do any conversion. */
1082 if (!is_encoded) return;
1083
1084 if (c == '"') {
1085 in_string = !in_string;
1086 if (in_string && need_type) {
1087 /* Started a new string parameter. */
1088 builder.PutUtf8(SCC_ENCODED_STRING);
1089 need_type = false;
1090 }
1091 continue;
1092 }
1093
1094 if (!in_string && c == ':') {
1095 builder.PutUtf8(SCC_RECORD_SEPARATOR);
1096 need_type = true;
1097 continue;
1098 }
1099 if (need_type) {
1100 /* Started a new numeric parameter. */
1102 need_type = false;
1103 }
1104
1105 builder.PutUtf8(c);
1106 }
1107
1108 str = std::move(result);
1109}
1110
1115void FixSCCEncodedNegative(std::string &str)
1116{
1117 if (str.empty()) return;
1118
1119 StringConsumer consumer(str);
1120
1121 /* Check whether this is an encoded string */
1122 if (!consumer.ReadUtf8If(SCC_ENCODED)) return;
1123
1124 std::string result;
1125 StringBuilder builder(result);
1126 builder.PutUtf8(SCC_ENCODED);
1127 while (consumer.AnyBytesLeft()) {
1128 /* Copy until next record */
1129 builder.Put(consumer.ReadUntilUtf8(SCC_RECORD_SEPARATOR, StringConsumer::READ_ONE_SEPARATOR));
1130
1131 /* Check whether this is a numeric parameter */
1132 if (!consumer.ReadUtf8If(SCC_ENCODED_NUMERIC)) continue;
1134
1135 /* First try unsigned */
1136 if (auto u = consumer.TryReadIntegerBase<uint64_t>(16); u.has_value()) {
1137 builder.PutIntegerBase<uint64_t>(*u, 16);
1138 } else {
1139 /* Read as signed, store as unsigned */
1140 auto s = consumer.ReadIntegerBase<int64_t>(16);
1141 builder.PutIntegerBase<uint64_t>(static_cast<uint64_t>(s), 16);
1142 }
1143 }
1144
1145 str = std::move(result);
1146}
1147
1154void SlReadString(std::string &str, size_t length)
1155{
1156 str.resize(length);
1157 SlCopyBytes(str.data(), length);
1158}
1159
1165static void SlStdString(void *ptr, VarType conv)
1166{
1167 std::string *str = reinterpret_cast<std::string *>(ptr);
1168
1169 switch (_sl.action) {
1170 case SaveLoadAction::Save: {
1171 size_t len = str->length();
1172 SlWriteArrayLength(len);
1173 SlCopyBytes(const_cast<void *>(static_cast<const void *>(str->data())), len);
1174 break;
1175 }
1176
1178 case SaveLoadAction::Load: {
1179 size_t len = SlReadArrayLength();
1180 if (conv.mem == VarMemType::Null) {
1181 SlSkipBytes(len);
1182 return;
1183 }
1184
1185 SlReadString(*str, len);
1186
1192 }
1194 }
1195
1196 case SaveLoadAction::Ptrs: break;
1197 case SaveLoadAction::Null: break;
1198 default: NOT_REACHED();
1199 }
1200}
1201
1210static void SlCopyInternal(void *object, size_t length, VarType conv)
1211{
1212 if (conv.mem == VarMemType::Null) {
1213 assert(_sl.action != SaveLoadAction::Save); // Use SaveLoadType::Null if you want to write null-bytes
1214 SlSkipBytes(length * SlCalcConvFileLen(conv));
1215 return;
1216 }
1217
1218 /* NOTICE - handle some buggy stuff, in really old versions everything was saved
1219 * as a byte-type. So detect this, and adjust object size accordingly */
1221 /* all objects except difficulty settings */
1222 if (conv == VarTypes::I16 || conv == VarTypes::U16 || conv == VarTypes::STRINGID ||
1223 conv == VarTypes::I32 || conv == VarTypes::U32) {
1224 SlCopyBytes(object, length * SlCalcConvFileLen(conv));
1225 return;
1226 }
1227 /* used for conversion of Money 32bit->64bit */
1228 if (conv.file == VarFileType::I32 && conv.mem == VarMemType::I64) {
1229 for (uint i = 0; i < length; i++) {
1230 static_cast<int64_t *>(object)[i] = std::byteswap(SlReadUint32());
1231 }
1232 return;
1233 }
1234 }
1235
1236 /* If the size of elements is 1 byte both in file and memory, no special
1237 * conversion is needed, use specialized copy-copy function to speed up things */
1238 if (conv == VarTypes::I8 || conv == VarTypes::U8) {
1239 SlCopyBytes(object, length);
1240 } else {
1241 uint8_t *a = static_cast<uint8_t *>(object);
1242 uint8_t mem_size = SlCalcConvMemLen(conv.mem);
1243
1244 for (; length != 0; length --) {
1245 SlSaveLoadConv(a, conv);
1246 a += mem_size; // get size
1247 }
1248 }
1249}
1250
1259void SlCopy(void *object, size_t length, VarType conv)
1260{
1261 assert(object != nullptr); // Use SlSkipBytes instead
1262 if (_sl.action == SaveLoadAction::Ptrs || _sl.action == SaveLoadAction::Null) return;
1263
1264 /* Automatically calculate the length? */
1265 if (_sl.need_length != NeedLength::None) {
1266 SlSetLength(length * SlCalcConvFileLen(conv));
1267 /* Determine length only? */
1268 if (_sl.need_length == NeedLength::CalcLength) return;
1269 }
1270
1271 SlCopyInternal(object, length, conv);
1272}
1273
1280static inline size_t SlCalcArrayLen(size_t length, VarType conv)
1281{
1282 return SlCalcConvFileLen(conv) * length + SlGetArrayLength(length);
1283}
1284
1291static void SlArray(void *array, size_t length, VarType conv)
1292{
1293 switch (_sl.action) {
1295 SlWriteArrayLength(length);
1296 SlCopyInternal(array, length, conv);
1297 return;
1298
1300 case SaveLoadAction::Load: {
1302 size_t sv_length = SlReadArrayLength();
1303 if (conv.mem == VarMemType::Null) {
1304 /* We don't know this field, so we assume the length in the savegame is correct. */
1305 length = sv_length;
1306 } else if (sv_length != length) {
1307 /* If the SLE_ARR changes size, a savegame bump is required
1308 * and the developer should have written conversion lines.
1309 * Error out to make this more visible. */
1310 SlErrorCorrupt("Fixed-length array is of wrong length");
1311 }
1312 }
1313
1314 SlCopyInternal(array, length, conv);
1315 return;
1316 }
1317
1320 return;
1321
1322 default:
1323 NOT_REACHED();
1324 }
1325}
1326
1337static uint32_t ReferenceToInt(const void *obj, SLRefType rt)
1338{
1339 assert(_sl.action == SaveLoadAction::Save);
1340
1341 if (obj == nullptr) return 0;
1342
1343 switch (rt) {
1344 case SLRefType::OldVehicle: // Old vehicles we save as new ones
1345 case SLRefType::Vehicle: return static_cast<const Vehicle *>(obj)->index + 1;
1346 case SLRefType::Station: return static_cast<const Station *>(obj)->index + 1;
1347 case SLRefType::Town: return static_cast<const Town *>(obj)->index + 1;
1348 case SLRefType::RoadStop: return static_cast<const RoadStop *>(obj)->index + 1;
1349 case SLRefType::EngineRenew: return static_cast<const EngineRenew *>(obj)->index + 1;
1350 case SLRefType::CargoPacket: return static_cast<const CargoPacket *>(obj)->index + 1;
1351 case SLRefType::OrderList: return static_cast<const OrderList *>(obj)->index + 1;
1352 case SLRefType::Storage: return static_cast<const PersistentStorage *>(obj)->index + 1;
1353 case SLRefType::LinkGraph: return static_cast<const LinkGraph *>(obj)->index + 1;
1354 case SLRefType::LinkGraphJob: return static_cast<const LinkGraphJob *>(obj)->index + 1;
1355 default: NOT_REACHED();
1356 }
1357}
1358
1369static void *IntToReference(size_t index, SLRefType rt)
1370{
1371 static_assert(sizeof(size_t) <= sizeof(void *));
1372
1373 assert(_sl.action == SaveLoadAction::Ptrs);
1374
1375 /* After version 4.3 SLRefType::OldVehicle is saved as SLRefType::Vehicle,
1376 * and should be loaded like that */
1378 rt = SLRefType::Vehicle;
1379 }
1380
1381 /* No need to look up nullptr pointers, just return immediately */
1382 if (index == (rt == SLRefType::OldVehicle ? 0xFFFF : 0)) return nullptr;
1383
1384 /* Correct index. Old vehicles were saved differently:
1385 * invalid vehicle was 0xFFFF, now we use 0x0000 for everything invalid. */
1386 if (rt != SLRefType::OldVehicle) index--;
1387
1388 switch (rt) {
1390 if (OrderList::IsValidID(index)) return OrderList::Get(index);
1391 SlErrorCorrupt("Referencing invalid OrderList");
1392
1394 case SLRefType::Vehicle:
1395 if (Vehicle::IsValidID(index)) return Vehicle::Get(index);
1396 SlErrorCorrupt("Referencing invalid Vehicle");
1397
1398 case SLRefType::Station:
1399 if (Station::IsValidID(index)) return Station::Get(index);
1400 SlErrorCorrupt("Referencing invalid Station");
1401
1402 case SLRefType::Town:
1403 if (Town::IsValidID(index)) return Town::Get(index);
1404 SlErrorCorrupt("Referencing invalid Town");
1405
1407 if (RoadStop::IsValidID(index)) return RoadStop::Get(index);
1408 SlErrorCorrupt("Referencing invalid RoadStop");
1409
1411 if (EngineRenew::IsValidID(index)) return EngineRenew::Get(index);
1412 SlErrorCorrupt("Referencing invalid EngineRenew");
1413
1415 if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
1416 SlErrorCorrupt("Referencing invalid CargoPacket");
1417
1418 case SLRefType::Storage:
1419 if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
1420 SlErrorCorrupt("Referencing invalid PersistentStorage");
1421
1423 if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
1424 SlErrorCorrupt("Referencing invalid LinkGraph");
1425
1427 if (LinkGraphJob::IsValidID(index)) return LinkGraphJob::Get(index);
1428 SlErrorCorrupt("Referencing invalid LinkGraphJob");
1429
1430 default: NOT_REACHED();
1431 }
1432}
1433
1439void SlSaveLoadRef(void *ptr, VarType conv)
1440{
1441 switch (_sl.action) {
1443 SlWriteUint32(ReferenceToInt(*static_cast<void **>(ptr), conv.ref));
1444 break;
1447 *static_cast<size_t *>(ptr) = IsSavegameVersionBefore(SaveLoadVersion::MoreCargoPackets) ? SlReadUint16() : SlReadUint32();
1448 break;
1450 *static_cast<void **>(ptr) = IntToReference(*static_cast<size_t *>(ptr), conv.ref);
1451 break;
1453 *static_cast<void **>(ptr) = nullptr;
1454 break;
1455 default: NOT_REACHED();
1456 }
1457}
1458
1462template <template <typename, typename> typename Tstorage, typename Tvar, typename Tallocator = std::allocator<Tvar>>
1464 typedef Tstorage<Tvar, Tallocator> SlStorageT;
1465public:
1473 static size_t SlCalcLen(const void *storage, VarType conv, SaveLoadType cmd = SaveLoadType::Variable)
1474 {
1475 assert(cmd == SaveLoadType::Variable || cmd == SaveLoadType::Reference);
1476
1477 const SlStorageT *list = static_cast<const SlStorageT *>(storage);
1478
1479 int type_size = SlGetArrayLength(list->size());
1480 int item_size = SlCalcConvFileLen(cmd == SaveLoadType::Variable ? conv : VarType{VarFileType::U32, {}});
1481 return list->size() * item_size + type_size;
1482 }
1483
1484 static void SlSaveLoadMember(SaveLoadType cmd, Tvar *item, VarType conv)
1485 {
1486 switch (cmd) {
1487 case SaveLoadType::Variable: SlSaveLoadConv(item, conv); break;
1488 case SaveLoadType::Reference: SlSaveLoadRef(item, conv); break;
1489 case SaveLoadType::String: SlStdString(item, conv); break;
1490 default:
1491 NOT_REACHED();
1492 }
1493 }
1494
1501 static void SlSaveLoad(void *storage, VarType conv, SaveLoadType cmd = SaveLoadType::Variable)
1502 {
1503 assert(cmd == SaveLoadType::Variable || cmd == SaveLoadType::Reference || cmd == SaveLoadType::String);
1504
1505 SlStorageT *list = static_cast<SlStorageT *>(storage);
1506
1507 switch (_sl.action) {
1509 SlWriteArrayLength(list->size());
1510
1511 for (auto &item : *list) {
1512 SlSaveLoadMember(cmd, &item, conv);
1513 }
1514 break;
1515
1517 case SaveLoadAction::Load: {
1518 size_t length;
1519 switch (cmd) {
1520 case SaveLoadType::Variable: length = IsSavegameVersionBefore(SaveLoadVersion::SaveloadListLength) ? SlReadUint32() : SlReadArrayLength(); break;
1521 case SaveLoadType::Reference: length = IsSavegameVersionBefore(SaveLoadVersion::MoreCargoPackets) ? SlReadUint16() : IsSavegameVersionBefore(SaveLoadVersion::SaveloadListLength) ? SlReadUint32() : SlReadArrayLength(); break;
1522 case SaveLoadType::String: length = SlReadArrayLength(); break;
1523 default: NOT_REACHED();
1524 }
1525
1526 list->clear();
1527 if constexpr (std::is_same_v<SlStorageT, std::vector<Tvar, Tallocator>>) {
1528 list->reserve(length);
1529 }
1530
1531 /* Load each value and push to the end of the storage. */
1532 for (size_t i = 0; i < length; i++) {
1533 Tvar &data = list->emplace_back();
1534 SlSaveLoadMember(cmd, &data, conv);
1535 }
1536 break;
1537 }
1538
1540 for (auto &item : *list) {
1541 SlSaveLoadMember(cmd, &item, conv);
1542 }
1543 break;
1544
1546 list->clear();
1547 break;
1548
1549 default: NOT_REACHED();
1550 }
1551 }
1552};
1553
1560static inline size_t SlCalcRefListLen(const void *list, VarType conv)
1561{
1563}
1564
1570static void SlRefList(void *list, VarType conv)
1571{
1572 /* Automatically calculate the length? */
1573 if (_sl.need_length != NeedLength::None) {
1574 SlSetLength(SlCalcRefListLen(list, conv));
1575 /* Determine length only? */
1576 if (_sl.need_length == NeedLength::CalcLength) return;
1577 }
1578
1580}
1581
1588static size_t SlCalcRefVectorLen(const void *vector, VarType conv)
1589{
1591}
1592
1598static void SlRefVector(void *vector, VarType conv)
1599{
1600 /* Automatically calculate the length? */
1601 if (_sl.need_length != NeedLength::None) {
1602 SlSetLength(SlCalcRefVectorLen(vector, conv));
1603 /* Determine length only? */
1604 if (_sl.need_length == NeedLength::CalcLength) return;
1605 }
1606
1608}
1609
1616static inline size_t SlCalcVectorLen(const void *vector, VarType conv)
1617{
1618 switch (conv.mem) {
1619 case VarMemType::Bool: NOT_REACHED(); // Not supported
1628
1629 case VarMemType::Str:
1630 /* Strings are a length-prefixed field type in the savegame table format,
1631 * these may not be directly stored in another length-prefixed container type. */
1632 NOT_REACHED();
1633
1634 default: NOT_REACHED();
1635 }
1636}
1637
1643static void SlVector(void *vector, VarType conv)
1644{
1645 switch (conv.mem) {
1646 case VarMemType::Bool: NOT_REACHED(); // Not supported
1655
1656 case VarMemType::Str:
1657 /* Strings are a length-prefixed field type in the savegame table format,
1658 * these may not be directly stored in another length-prefixed container type.
1659 * This is permitted for load-related actions, because invalid fields of this type are present
1660 * from SaveLoadVersion::CompanyAllowList up to SaveLoadVersion::CompanyAllowListV2. */
1661 assert(_sl.action != SaveLoadAction::Save);
1663 break;
1664
1665 default: NOT_REACHED();
1666 }
1667}
1668
1674static inline bool SlIsObjectValidInSavegame(const SaveLoad &sld)
1675{
1676 return (_sl_version >= sld.version_from && _sl_version < sld.version_to);
1677}
1678
1684static size_t SlCalcTableHeader(const SaveLoadTable &slt)
1685{
1686 size_t length = 0;
1687
1688 for (auto &sld : slt) {
1689 if (!SlIsObjectValidInSavegame(sld)) continue;
1690
1692 length += SlCalcStdStringLen(&sld.name);
1693 }
1694
1695 length += SlCalcConvFileLen(VarTypes::U8); // End-of-list entry.
1696
1697 for (auto &sld : slt) {
1698 if (!SlIsObjectValidInSavegame(sld)) continue;
1699 if (sld.cmd == SaveLoadType::StructList || sld.cmd == SaveLoadType::Struct) {
1700 length += SlCalcTableHeader(sld.handler->GetDescription());
1701 }
1702 }
1703
1704 return length;
1705}
1706
1713size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt)
1714{
1715 size_t length = 0;
1716
1717 /* Need to determine the length and write a length tag. */
1718 for (auto &sld : slt) {
1719 length += SlCalcObjMemberLength(object, sld);
1720 }
1721 return length;
1722}
1723
1724size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld)
1725{
1726 assert(_sl.action == SaveLoadAction::Save);
1727
1728 if (!SlIsObjectValidInSavegame(sld)) return 0;
1729
1730 switch (sld.cmd) {
1733 case SaveLoadType::Array: return SlCalcArrayLen(sld.length, sld.conv);
1736 case SaveLoadType::Vector: return SlCalcVectorLen(GetVariableAddress(object, sld), sld.conv);
1738 case SaveLoadType::SaveByte: return 1; // a byte is logically of size 1
1739 case SaveLoadType::Null: return SlCalcConvFileLen(sld.conv) * sld.length;
1740
1743 NeedLength old_need_length = _sl.need_length;
1744 size_t old_obj_len = _sl.obj_len;
1745
1746 _sl.need_length = NeedLength::CalcLength;
1747 _sl.obj_len = 0;
1748
1749 /* Pretend that we are saving to collect the object size. Other
1750 * means are difficult, as we don't know the length of the list we
1751 * are about to store. */
1752 sld.handler->Save(const_cast<void *>(object));
1753 size_t length = _sl.obj_len;
1754
1755 _sl.obj_len = old_obj_len;
1756 _sl.need_length = old_need_length;
1757
1758 if (sld.cmd == SaveLoadType::Struct) {
1759 length += SlGetArrayLength(1);
1760 }
1761
1762 return length;
1763 }
1764
1765 default: NOT_REACHED();
1766 }
1767 return 0;
1768}
1769
1770static bool SlObjectMember(void *object, const SaveLoad &sld)
1771{
1772 if (!SlIsObjectValidInSavegame(sld)) return false;
1773
1774 switch (sld.cmd) {
1781 case SaveLoadType::String: {
1782 void *ptr = GetVariableAddress(object, sld);
1783
1784 switch (sld.cmd) {
1785 case SaveLoadType::Variable: SlSaveLoadConv(ptr, sld.conv); break;
1786 case SaveLoadType::Reference: SlSaveLoadRef(ptr, sld.conv); break;
1787 case SaveLoadType::Array: SlArray(ptr, sld.length, sld.conv); break;
1788 case SaveLoadType::ReferenceList: SlRefList(ptr, sld.conv); break;
1789 case SaveLoadType::ReferenceVector: SlRefVector(ptr, sld.conv); break;
1790 case SaveLoadType::Vector: SlVector(ptr, sld.conv); break;
1791 case SaveLoadType::String: SlStdString(ptr, sld.conv); break;
1792 default: NOT_REACHED();
1793 }
1794 break;
1795 }
1796
1797 /* SaveLoadType::SaveByte writes a value to the savegame to identify the type of an object.
1798 * When loading, the value is read explicitly with SlReadByte() to determine which
1799 * object description to use. */
1801 void *ptr = GetVariableAddress(object, sld);
1802
1803 switch (_sl.action) {
1804 case SaveLoadAction::Save: SlWriteByte(*static_cast<uint8_t *>(ptr)); break;
1808 case SaveLoadAction::Null: break;
1809 default: NOT_REACHED();
1810 }
1811 break;
1812 }
1813
1814 case SaveLoadType::Null: {
1815 assert(sld.conv.mem == VarMemType::Null);
1816
1817 switch (_sl.action) {
1820 case SaveLoadAction::Save: for (int i = 0; i < SlCalcConvFileLen(sld.conv) * sld.length; i++) SlWriteByte(0); break;
1822 case SaveLoadAction::Null: break;
1823 default: NOT_REACHED();
1824 }
1825 break;
1826 }
1827
1830 switch (_sl.action) {
1831 case SaveLoadAction::Save: {
1832 if (sld.cmd == SaveLoadType::Struct) {
1833 /* Store in the savegame if this struct was written or not. */
1834 SlSetStructListLength(SlCalcObjMemberLength(object, sld) > SlGetArrayLength(1) ? 1 : 0);
1835 }
1836 sld.handler->Save(object);
1837 break;
1838 }
1839
1843 }
1844 sld.handler->LoadCheck(object);
1845 break;
1846 }
1847
1848 case SaveLoadAction::Load: {
1851 }
1852 sld.handler->Load(object);
1853 break;
1854 }
1855
1857 sld.handler->FixPointers(object);
1858 break;
1859
1860 case SaveLoadAction::Null: break;
1861 default: NOT_REACHED();
1862 }
1863 break;
1864
1865 default: NOT_REACHED();
1866 }
1867 return true;
1868}
1869
1874void SlSetStructListLength(size_t length)
1875{
1876 /* Automatically calculate the length? */
1877 if (_sl.need_length != NeedLength::None) {
1878 SlSetLength(SlGetArrayLength(length));
1879 if (_sl.need_length == NeedLength::CalcLength) return;
1880 }
1881
1882 SlWriteArrayLength(length);
1883}
1884
1890size_t SlGetStructListLength(size_t limit)
1891{
1892 size_t length = SlReadArrayLength();
1893 if (length > limit) SlErrorCorrupt("List exceeds storage size");
1894
1895 return length;
1896}
1897
1903void SlObject(void *object, const SaveLoadTable &slt)
1904{
1905 /* Automatically calculate the length? */
1906 if (_sl.need_length != NeedLength::None) {
1907 SlSetLength(SlCalcObjLength(object, slt));
1908 if (_sl.need_length == NeedLength::CalcLength) return;
1909 }
1910
1911 for (auto &sld : slt) {
1912 SlObjectMember(object, sld);
1913 }
1914}
1915
1921 void Save(void *) const override
1922 {
1923 NOT_REACHED();
1924 }
1925
1926 void Load(void *object) const override
1927 {
1928 size_t length = SlGetStructListLength(UINT32_MAX);
1929 for (; length > 0; length--) {
1930 SlObject(object, this->GetLoadDescription());
1931 }
1932 }
1933
1934 void LoadCheck(void *object) const override
1935 {
1936 this->Load(object);
1937 }
1938
1940 {
1941 return {};
1942 }
1943
1945 {
1946 NOT_REACHED();
1947 }
1948};
1949
1956std::vector<SaveLoad> SlTableHeader(const SaveLoadTable &slt)
1957{
1958 /* You can only use SlTableHeader if you are a ChunkType::Table or ChunkType::SparseTable. */
1959 assert(_sl.chunk_type == ChunkType::Table || _sl.chunk_type == ChunkType::SparseTable);
1960
1961 switch (_sl.action) {
1963 case SaveLoadAction::Load: {
1964 std::vector<SaveLoad> saveloads;
1965
1966 /* Build a key lookup mapping based on the available fields. */
1967 std::map<std::string, const SaveLoad *> key_lookup;
1968 for (auto &sld : slt) {
1969 if (!SlIsObjectValidInSavegame(sld)) continue;
1970
1971 /* Check that there is only one active SaveLoad for a given name. */
1972 assert(key_lookup.find(sld.name) == key_lookup.end());
1973 key_lookup[sld.name] = &sld;
1974 }
1975
1976 while (true) {
1977 SavegameFileType type{};
1979 if (type.IsEnd()) break;
1980
1981 std::string key;
1983
1984 auto sld_it = key_lookup.find(key);
1985 if (sld_it == key_lookup.end()) {
1986 /* SLA_LOADCHECK triggers this debug statement a lot and is perfectly normal. */
1987 Debug(Facility::Sl, _sl.action == SaveLoadAction::Load ? Severity::Warning : Severity::Debug2, "Field '{}' of type 0x{:02x} not found, skipping", key, type.storage);
1988
1989 std::shared_ptr<SaveLoadHandler> handler = nullptr;
1990 SaveLoadType saveload_type;
1991 switch (type.Type()) {
1993 saveload_type = SaveLoadType::String;
1994 break;
1995
1997 saveload_type = SaveLoadType::StructList;
1998 handler = std::make_shared<SlSkipHandler>();
1999 break;
2000
2001 default:
2003 break;
2004 }
2005
2006 /* We don't know this field, so read to nothing. */
2007 saveloads.emplace_back(std::move(key), saveload_type, VarType{type.Type(), VarMemType::Null}, 1, SaveLoadVersion::MinVersion, SaveLoadVersion::MaxVersion, nullptr, 0, std::move(handler));
2008 continue;
2009 }
2010
2011 /* Validate the type of the field. If it is changed, the
2012 * savegame should have been bumped so we know how to do the
2013 * conversion. If this error triggers, that clearly didn't
2014 * happen and this is a friendly poke to the developer to bump
2015 * the savegame version and add conversion code. */
2016 SavegameFileType correct_type = GetSavegameFileType(*sld_it->second);
2017 if (correct_type.storage != type.storage) {
2018 Debug(Facility::Sl, Severity::Error, "Field type for '{}' was expected to be 0x{:02x} but 0x{:02x} was found", key, correct_type.storage, type.storage);
2019 SlErrorCorrupt("Field type is different than expected");
2020 }
2021 saveloads.emplace_back(*sld_it->second);
2022 }
2023
2024 for (auto &sld : saveloads) {
2026 sld.handler->load_description = SlTableHeader(sld.handler->GetDescription());
2027 }
2028 }
2029
2030 return saveloads;
2031 }
2032
2033 case SaveLoadAction::Save: {
2034 /* Automatically calculate the length? */
2035 if (_sl.need_length != NeedLength::None) {
2037 if (_sl.need_length == NeedLength::CalcLength) break;
2038 }
2039
2040 for (auto &sld : slt) {
2041 if (!SlIsObjectValidInSavegame(sld)) continue;
2042 /* Make sure we are not storing empty keys. */
2043 assert(!sld.name.empty());
2044
2046 assert(!type.IsEnd());
2047
2049 SlStdString(const_cast<std::string *>(&sld.name), VarTypes::STR);
2050 }
2051
2052 /* Add an end-of-header marker. */
2053 SavegameFileType type{};
2055
2056 /* After the table, write down any sub-tables we might have. */
2057 for (auto &sld : slt) {
2058 if (!SlIsObjectValidInSavegame(sld)) continue;
2060 /* SlCalcTableHeader already looks in sub-lists, so avoid the length being added twice. */
2061 NeedLength old_need_length = _sl.need_length;
2062 _sl.need_length = NeedLength::None;
2063
2064 SlTableHeader(sld.handler->GetDescription());
2065
2066 _sl.need_length = old_need_length;
2067 }
2068 }
2069
2070 break;
2071 }
2072
2073 default: NOT_REACHED();
2074 }
2075
2076 return std::vector<SaveLoad>();
2077}
2078
2092std::vector<SaveLoad> SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
2093{
2094 assert(_sl.action == SaveLoadAction::Load || _sl.action == SaveLoadAction::LoadCheck);
2095 /* ChunkType::Table / ChunkType::SparseTable always have a header. */
2096 if (_sl.chunk_type == ChunkType::Table || _sl.chunk_type == ChunkType::SparseTable) return SlTableHeader(slt);
2097
2098 std::vector<SaveLoad> saveloads;
2099
2100 /* Build a key lookup mapping based on the available fields. */
2101 std::map<std::string_view, std::vector<const SaveLoad *>> key_lookup;
2102 for (auto &sld : slt) {
2103 /* All entries should have a name; otherwise the entry should just be removed. */
2104 assert(!sld.name.empty());
2105
2106 key_lookup[sld.name].push_back(&sld);
2107 }
2108
2109 for (auto &slc : slct) {
2110 if (slc.name.empty()) {
2111 /* In old savegames there can be data we no longer care for. We
2112 * skip this by simply reading the amount of bytes indicated and
2113 * send those to /dev/null. */
2114 saveloads.emplace_back("", SaveLoadType::Null, VarType{VarFileType::U8, VarMemType::Null}, slc.null_length, slc.version_from, slc.version_to, nullptr, 0, nullptr);
2115 } else {
2116 auto sld_it = key_lookup.find(slc.name);
2117 /* If this branch triggers, it means that an entry in the
2118 * SaveLoadCompat list is not mentioned in the SaveLoad list. Did
2119 * you rename a field in one and not in the other? */
2120 if (sld_it == key_lookup.end()) {
2121 /* This isn't an assert, as that leaves no information what
2122 * field was to blame. This way at least we have breadcrumbs. */
2123 Debug(Facility::Sl, Severity::Critical, "internal error: saveload compatibility field '{}' not found", slc.name);
2124 SlErrorCorrupt("Internal error with savegame compatibility");
2125 }
2126 for (auto &sld : sld_it->second) {
2127 saveloads.push_back(*sld);
2128 }
2129 }
2130 }
2131
2132 for (auto &sld : saveloads) {
2133 if (!SlIsObjectValidInSavegame(sld)) continue;
2135 sld.handler->load_description = SlCompatTableHeader(sld.handler->GetDescription(), sld.handler->GetCompatDescription());
2136 }
2137 }
2138
2139 return saveloads;
2140}
2141
2147{
2148 SlObject(nullptr, slt);
2149}
2150
2156void SlAutolength(AutolengthProc *proc, int arg)
2157{
2158 assert(_sl.action == SaveLoadAction::Save);
2159
2160 /* Tell it to calculate the length */
2161 _sl.need_length = NeedLength::CalcLength;
2162 _sl.obj_len = 0;
2163 proc(arg);
2164
2165 /* Setup length */
2166 _sl.need_length = NeedLength::WantLength;
2167 SlSetLength(_sl.obj_len);
2168
2169 size_t start_pos = _sl.dumper->GetSize();
2170 size_t expected_offs = start_pos + _sl.obj_len;
2171
2172 /* And write the stuff */
2173 proc(arg);
2174
2175 if (expected_offs != _sl.dumper->GetSize()) {
2176 SlErrorCorruptFmt("Invalid chunk size when writing autolength block, expected {}, got {}", _sl.obj_len, _sl.dumper->GetSize() - start_pos);
2177 }
2178}
2179
2180void ChunkHandler::LoadCheck(size_t len) const
2181{
2182 switch (_sl.chunk_type) {
2183 case ChunkType::Table:
2185 SlTableHeader({});
2186 [[fallthrough]];
2187 case ChunkType::Array:
2189 SlSkipArray();
2190 break;
2191 case ChunkType::Riff:
2192 SlSkipBytes(len);
2193 break;
2194 default:
2195 NOT_REACHED();
2196 }
2197}
2198
2203static void SlLoadChunk(const ChunkHandler &ch)
2204{
2205 uint8_t m = SlReadByte();
2206
2207 _sl.chunk_type = static_cast<ChunkType>(m & to_underlying(ChunkType::FileTypeMask));
2208 _sl.obj_len = 0;
2209 _sl.expect_table_header = (_sl.chunk_type == ChunkType::Table || _sl.chunk_type == ChunkType::SparseTable);
2210
2211 /* The header should always be at the start. Read the length; the
2212 * Load() should as first action process the header. */
2213 if (_sl.expect_table_header) {
2214 if (SlIterateArray() != INT32_MAX) SlErrorCorrupt("Table chunk without header");
2215 }
2216
2217 switch (_sl.chunk_type) {
2218 case ChunkType::Table:
2219 case ChunkType::Array:
2220 _sl.array_index = 0;
2221 ch.Load();
2222 if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2223 break;
2226 ch.Load();
2227 if (_next_offs != 0) SlErrorCorrupt("Invalid array length");
2228 break;
2229 case ChunkType::Riff: {
2230 /* Read length */
2231 size_t len = (SlReadByte() << 16) | ((m >> 4) << 24);
2232 len += SlReadUint16();
2233 _sl.obj_len = len;
2234 size_t start_pos = _sl.reader->GetSize();
2235 size_t endoffs = start_pos + len;
2236 ch.Load();
2237
2238 if (_sl.reader->GetSize() != endoffs) {
2239 SlErrorCorruptFmt("Invalid chunk size in RIFF in {} - expected {}, got {}", ch.GetName(), len, _sl.reader->GetSize() - start_pos);
2240 }
2241 break;
2242 }
2243 default:
2244 SlErrorCorrupt("Invalid chunk type");
2245 break;
2246 }
2247
2248 if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2249}
2250
2256static void SlLoadCheckChunk(const ChunkHandler &ch)
2257{
2258 uint8_t m = SlReadByte();
2259
2260 _sl.chunk_type = static_cast<ChunkType>(m & to_underlying(ChunkType::FileTypeMask));
2261 _sl.obj_len = 0;
2262 _sl.expect_table_header = (_sl.chunk_type == ChunkType::Table || _sl.chunk_type == ChunkType::SparseTable);
2263
2264 /* The header should always be at the start. Read the length; the
2265 * LoadCheck() should as first action process the header. */
2266 if (_sl.expect_table_header) {
2267 if (SlIterateArray() != INT32_MAX) SlErrorCorrupt("Table chunk without header");
2268 }
2269
2270 switch (_sl.chunk_type) {
2271 case ChunkType::Table:
2272 case ChunkType::Array:
2273 _sl.array_index = 0;
2274 ch.LoadCheck();
2275 break;
2278 ch.LoadCheck();
2279 break;
2280 case ChunkType::Riff: {
2281 /* Read length */
2282 size_t len = (SlReadByte() << 16) | ((m >> 4) << 24);
2283 len += SlReadUint16();
2284 _sl.obj_len = len;
2285 size_t start_pos = _sl.reader->GetSize();
2286 size_t endoffs = start_pos + len;
2287 ch.LoadCheck(len);
2288
2289 if (_sl.reader->GetSize() != endoffs) {
2290 SlErrorCorruptFmt("Invalid chunk size in RIFF in {} - expected {}, got {}", ch.GetName(), len, _sl.reader->GetSize() - start_pos);
2291 }
2292 break;
2293 }
2294 default:
2295 SlErrorCorrupt("Invalid chunk type");
2296 break;
2297 }
2298
2299 if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2300}
2301
2307static void SlSaveChunk(const ChunkHandler &ch)
2308{
2309 if (ch.type == ChunkType::ReadOnly) return;
2310
2311 for (uint8_t b : ch.id) SlWriteByte(b);
2312 Debug(Facility::Sl, Severity::Warning, "Saving chunk {}", ch.GetName());
2313
2314 _sl.chunk_type = ch.type;
2315 _sl.expect_table_header = (_sl.chunk_type == ChunkType::Table || _sl.chunk_type == ChunkType::SparseTable);
2316
2317 _sl.need_length = (_sl.expect_table_header || _sl.chunk_type == ChunkType::Riff) ? NeedLength::WantLength : NeedLength::None;
2318
2319 switch (_sl.chunk_type) {
2320 case ChunkType::Riff:
2321 ch.Save();
2322 break;
2323 case ChunkType::Table:
2324 case ChunkType::Array:
2325 _sl.last_array_index = 0;
2326 SlWriteByte(to_underlying(_sl.chunk_type));
2327 ch.Save();
2328 SlWriteArrayLength(0); // Terminate arrays
2329 break;
2332 SlWriteByte(to_underlying(_sl.chunk_type));
2333 ch.Save();
2334 SlWriteArrayLength(0); // Terminate arrays
2335 break;
2336 default: NOT_REACHED();
2337 }
2338
2339 if (_sl.expect_table_header) SlErrorCorrupt("Table chunk without header");
2340}
2341
2343static void SlSaveChunks()
2344{
2345 for (auto &ch : ChunkHandlers()) {
2346 SlSaveChunk(ch);
2347 }
2348
2349 /* Terminator */
2350 SlWriteUint32(0);
2351}
2352
2360{
2361 for (const ChunkHandler &ch : ChunkHandlers()) if (ch.id == id) return &ch;
2362 return nullptr;
2363}
2364
2366static void SlLoadChunks()
2367{
2368 for (ChunkId id = SlReadChunkId(); !id.Empty(); id = SlReadChunkId()) {
2369 Debug(Facility::Sl, Severity::Warning, "Loading chunk {}", id.AsString());
2370
2371 const ChunkHandler *ch = SlFindChunkHandler(id);
2372 if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2373 SlLoadChunk(*ch);
2374 }
2375}
2376
2379{
2380 for (ChunkId id = SlReadChunkId(); !id.Empty(); id = SlReadChunkId()) {
2381 Debug(Facility::Sl, Severity::Warning, "Loading chunk {}", id.AsString());
2382
2383 const ChunkHandler *ch = SlFindChunkHandler(id);
2384 if (ch == nullptr) SlErrorCorrupt("Unknown chunk type");
2385 SlLoadCheckChunk(*ch);
2386 }
2387}
2388
2390static void SlFixPointers()
2391{
2392 _sl.action = SaveLoadAction::Ptrs;
2393
2394 for (const ChunkHandler &ch : ChunkHandlers()) {
2395 Debug(Facility::Sl, Severity::Notice, "Fixing pointers for {}", ch.GetName());
2396 ch.FixPointers();
2397 }
2398
2399 assert(_sl.action == SaveLoadAction::Ptrs);
2400}
2401
2402
2405 std::optional<FileHandle> file;
2406 long begin;
2407
2412 FileReader(FileHandle &&file) : LoadFilter(nullptr), file(std::move(file)), begin(ftell(*this->file))
2413 {
2414 }
2415
2417 ~FileReader() override
2418 {
2419 if (this->file.has_value()) {
2420 _game_session_stats.savegame_size = ftell(*this->file) - this->begin;
2421 }
2422 }
2423
2424 size_t Read(uint8_t *buf, size_t size) override
2425 {
2426 /* We're in the process of shutting down, i.e. in "failure" mode. */
2427 if (!this->file.has_value()) return 0;
2428
2429 return fread(buf, 1, size, *this->file);
2430 }
2431
2432 void Reset() override
2433 {
2434 clearerr(*this->file);
2435 if (fseek(*this->file, this->begin, SEEK_SET)) {
2436 Debug(Facility::Sl, Severity::Error, "Could not reset the file reading");
2437 }
2438 }
2439};
2440
2443 std::optional<FileHandle> file;
2444
2449 FileWriter(FileHandle &&file) : SaveFilter(nullptr), file(std::move(file))
2450 {
2451 }
2452
2454 ~FileWriter() override
2455 {
2456 this->Finish();
2457 }
2458
2459 void Write(const uint8_t *buf, size_t size) override
2460 {
2461 /* We're in the process of shutting down, i.e. in "failure" mode. */
2462 if (!this->file.has_value()) return;
2463
2464 if (fwrite(buf, 1, size, *this->file) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
2465 }
2466
2467 void Finish() override
2468 {
2469 if (this->file.has_value()) {
2470 _game_session_stats.savegame_size = ftell(*this->file);
2471 this->file.reset();
2472 }
2473 }
2474};
2475
2476/*******************************************
2477 ********** START OF LZO CODE **************
2478 *******************************************/
2479
2480#ifdef WITH_LZO
2481
2483static const uint LZO_BUFFER_SIZE = 8192;
2484
2491 LZOLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
2492 {
2493 if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2494 }
2495
2496 size_t Read(uint8_t *buf, size_t ssize) override
2497 {
2498 assert(ssize >= LZO_BUFFER_SIZE);
2499
2500 /* Buffer size is from the LZO docs plus the chunk header size. */
2501 uint8_t out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32_t) * 2];
2502 uint32_t tmp[2];
2503 uint32_t size;
2504 lzo_uint len = ssize;
2505
2506 /* Read header*/
2507 if (this->chain->Read((uint8_t*)tmp, sizeof(tmp)) != sizeof(tmp)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE, "File read failed");
2508
2509 /* Check if size is bad */
2510 ((uint32_t*)out)[0] = size = tmp[1];
2511
2513 tmp[0] = TO_BE32(tmp[0]);
2514 size = TO_BE32(size);
2515 }
2516
2517 if (size >= sizeof(out)) SlErrorCorrupt("Inconsistent size");
2518
2519 /* Read block */
2520 if (this->chain->Read(out + sizeof(uint32_t), size) != size) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2521
2522 /* Verify checksum */
2523 if (tmp[0] != lzo_adler32(0, out, size + sizeof(uint32_t))) SlErrorCorrupt("Bad checksum");
2524
2525 /* Decompress */
2526 int ret = lzo1x_decompress_safe(out + sizeof(uint32_t) * 1, size, buf, &len, nullptr);
2527 if (ret != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
2528 return len;
2529 }
2530};
2531
2538 LZOSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t) : SaveFilter(std::move(chain))
2539 {
2540 if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2541 }
2542
2543 void Write(const uint8_t *buf, size_t size) override
2544 {
2545 const lzo_bytep in = buf;
2546 /* Buffer size is from the LZO docs plus the chunk header size. */
2547 uint8_t out[LZO_BUFFER_SIZE + LZO_BUFFER_SIZE / 16 + 64 + 3 + sizeof(uint32_t) * 2];
2548 uint8_t wrkmem[LZO1X_1_MEM_COMPRESS];
2549 lzo_uint outlen;
2550
2551 do {
2552 /* Compress up to LZO_BUFFER_SIZE bytes at once. */
2553 lzo_uint len = size > LZO_BUFFER_SIZE ? LZO_BUFFER_SIZE : static_cast<lzo_uint>(size);
2554 lzo1x_1_compress(in, len, out + sizeof(uint32_t) * 2, &outlen, wrkmem);
2555 ((uint32_t*)out)[1] = TO_BE32(static_cast<uint32_t>(outlen));
2556 ((uint32_t*)out)[0] = TO_BE32(lzo_adler32(0, out + sizeof(uint32_t), outlen + sizeof(uint32_t)));
2557 this->chain->Write(out, outlen + sizeof(uint32_t) * 2);
2558
2559 /* Move to next data chunk. */
2560 size -= len;
2561 in += len;
2562 } while (size > 0);
2563 }
2564};
2565
2566#endif /* WITH_LZO */
2567
2568/*********************************************
2569 ******** START OF NOCOMP CODE (uncompressed)*
2570 *********************************************/
2571
2578 NoCompLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
2579 {
2580 }
2581
2582 size_t Read(uint8_t *buf, size_t size) override
2583 {
2584 return this->chain->Read(buf, size);
2585 }
2586};
2587
2594 NoCompSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t) : SaveFilter(std::move(chain))
2595 {
2596 }
2597
2598 void Write(const uint8_t *buf, size_t size) override
2599 {
2600 this->chain->Write(buf, size);
2601 }
2602};
2603
2604/********************************************
2605 ********** START OF ZLIB CODE **************
2606 ********************************************/
2607
2608#if defined(WITH_ZLIB)
2609
2612 z_stream z{};
2614
2619 ZlibLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
2620 {
2621 if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2622 }
2623
2626 {
2627 inflateEnd(&this->z);
2628 }
2629
2630 size_t Read(uint8_t *buf, size_t size) override
2631 {
2632 this->z.next_out = buf;
2633 this->z.avail_out = static_cast<uint>(size);
2634
2635 do {
2636 /* read more bytes from the file? */
2637 if (this->z.avail_in == 0) {
2638 this->z.next_in = this->fread_buf;
2639 this->z.avail_in = static_cast<uint>(this->chain->Read(this->fread_buf, sizeof(this->fread_buf)));
2640 }
2641
2642 /* inflate the data */
2643 int r = inflate(&this->z, 0);
2644 if (r == Z_STREAM_END) break;
2645
2646 if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "inflate() failed");
2647 } while (this->z.avail_out != 0);
2648
2649 return size - this->z.avail_out;
2650 }
2651};
2652
2655 z_stream z{};
2657
2663 ZlibSaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level) : SaveFilter(std::move(chain))
2664 {
2665 if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2666 }
2667
2670 {
2671 deflateEnd(&this->z);
2672 }
2673
2680 void WriteLoop(const uint8_t *p, size_t len, int mode)
2681 {
2682 uint n;
2683 this->z.next_in = const_cast<uint8_t *>(p); // zlib does not modify the data, but is non-const for legacy reasons
2684 this->z.avail_in = static_cast<uInt>(len);
2685 do {
2686 this->z.next_out = this->fwrite_buf;
2687 this->z.avail_out = sizeof(this->fwrite_buf);
2688
2696 int r = deflate(&this->z, mode);
2697
2698 /* bytes were emitted? */
2699 if ((n = sizeof(this->fwrite_buf) - this->z.avail_out) != 0) {
2700 this->chain->Write(this->fwrite_buf, n);
2701 }
2702 if (r == Z_STREAM_END) break;
2703
2704 if (r != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "zlib returned error code");
2705 } while (this->z.avail_in || !this->z.avail_out);
2706 }
2707
2708 void Write(const uint8_t *buf, size_t size) override
2709 {
2710 this->WriteLoop(buf, size, 0);
2711 }
2712
2713 void Finish() override
2714 {
2715 this->WriteLoop(nullptr, 0, Z_FINISH);
2716 this->chain->Finish();
2717 }
2718};
2719
2720#endif /* WITH_ZLIB */
2721
2722/********************************************
2723 ********** START OF LZMA CODE **************
2724 ********************************************/
2725
2726#if defined(WITH_LIBLZMA)
2727
2734static const lzma_stream _lzma_init = LZMA_STREAM_INIT;
2735
2738 lzma_stream lzma;
2740
2745 LZMALoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain)), lzma(_lzma_init)
2746 {
2747 /* Allow saves up to 256 MB uncompressed */
2748 if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
2749 }
2750
2753 {
2754 lzma_end(&this->lzma);
2755 }
2756
2757 size_t Read(uint8_t *buf, size_t size) override
2758 {
2759 this->lzma.next_out = buf;
2760 this->lzma.avail_out = size;
2761
2762 do {
2763 /* read more bytes from the file? */
2764 if (this->lzma.avail_in == 0) {
2765 this->lzma.next_in = this->fread_buf;
2766 this->lzma.avail_in = this->chain->Read(this->fread_buf, sizeof(this->fread_buf));
2767 }
2768
2769 /* inflate the data */
2770 lzma_ret r = lzma_code(&this->lzma, LZMA_RUN);
2771 if (r == LZMA_STREAM_END) break;
2772 if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2773 } while (this->lzma.avail_out != 0);
2774
2775 return size - this->lzma.avail_out;
2776 }
2777};
2778
2781 lzma_stream lzma;
2783
2789 LZMASaveFilter(std::shared_ptr<SaveFilter> chain, uint8_t compression_level) : SaveFilter(std::move(chain)), lzma(_lzma_init)
2790 {
2791 if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
2792 }
2793
2796 {
2797 lzma_end(&this->lzma);
2798 }
2799
2806 void WriteLoop(const uint8_t *p, size_t len, lzma_action action)
2807 {
2808 size_t n;
2809 this->lzma.next_in = p;
2810 this->lzma.avail_in = len;
2811 do {
2812 this->lzma.next_out = this->fwrite_buf;
2813 this->lzma.avail_out = sizeof(this->fwrite_buf);
2814
2815 lzma_ret r = lzma_code(&this->lzma, action);
2816
2817 /* bytes were emitted? */
2818 if ((n = sizeof(this->fwrite_buf) - this->lzma.avail_out) != 0) {
2819 this->chain->Write(this->fwrite_buf, n);
2820 }
2821 if (r == LZMA_STREAM_END) break;
2822 if (r != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "liblzma returned error code");
2823 } while (this->lzma.avail_in || !this->lzma.avail_out);
2824 }
2825
2826 void Write(const uint8_t *buf, size_t size) override
2827 {
2828 this->WriteLoop(buf, size, LZMA_RUN);
2829 }
2830
2831 void Finish() override
2832 {
2833 this->WriteLoop(nullptr, 0, LZMA_FINISH);
2834 this->chain->Finish();
2835 }
2836};
2837
2838#endif /* WITH_LIBLZMA */
2839
2840/*******************************************
2841 ************* END OF CODE *****************
2842 *******************************************/
2843
2846
2849 std::shared_ptr<LoadFilter> (*init_load)(std::shared_ptr<LoadFilter> chain);
2850 std::shared_ptr<SaveFilter> (*init_write)(std::shared_ptr<SaveFilter> chain, uint8_t compression);
2851
2852 std::string_view name;
2854
2858};
2859
2864
2867#if defined(WITH_LZO)
2868 /* Roughly 75% larger than zlib level 6 at only ~7% of the CPU usage. */
2870#else
2871 {nullptr, nullptr, "lzo", SAVEGAME_TAG_LZO, 0, 0, 0},
2872#endif
2873 /* Roughly 5 times larger at only 1% of the CPU usage over zlib level 6. */
2875#if defined(WITH_ZLIB)
2876 /* After level 6 the speed reduction is significant (1.5x to 2.5x slower per level), but the reduction in filesize is
2877 * fairly insignificant (~1% for each step). Lower levels become ~5-10% bigger by each level than level 6 while level
2878 * 1 is "only" 3 times as fast. Level 0 results in uncompressed savegames at about 8 times the cost of "none". */
2880#else
2881 {nullptr, nullptr, "zlib", SAVEGAME_TAG_ZLIB, 0, 0, 0},
2882#endif
2883#if defined(WITH_LIBLZMA)
2884 /* Level 2 compression is speed wise as fast as zlib level 6 compression (old default), but results in ~10% smaller saves.
2885 * Higher compression levels are possible, and might improve savegame size by up to 25%, but are also up to 10 times slower.
2886 * The next significant reduction in file size is at level 4, but that is already 4 times slower. Level 3 is primarily 50%
2887 * slower while not improving the filesize, while level 0 and 1 are faster, but don't reduce savegame size much.
2888 * It's OTTX and not e.g. OTTL because liblzma is part of xz-utils and .tar.xz is preferred over .tar.lzma. */
2890#else
2891 {nullptr, nullptr, "lzma", SAVEGAME_TAG_LZMA, 0, 0, 0},
2892#endif
2893};
2894
2901static std::pair<const SaveLoadFormat &, uint8_t> GetSavegameFormat(std::string_view full_name)
2902{
2903 /* Find default savegame format, the highest one with which files can be written. */
2904 auto it = std::find_if(std::rbegin(_saveload_formats), std::rend(_saveload_formats), [](const auto &slf) { return slf.init_write != nullptr; });
2905 if (it == std::rend(_saveload_formats)) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "no writeable savegame formats");
2906
2907 const SaveLoadFormat &def = *it;
2908
2909 if (!full_name.empty()) {
2910 /* Get the ":..." of the compression level out of the way */
2911 size_t separator = full_name.find(':');
2912 bool has_comp_level = separator != std::string::npos;
2913 std::string_view name = has_comp_level ? full_name.substr(0, separator) : full_name;
2914
2915 for (const auto &slf : _saveload_formats) {
2916 if (slf.init_write != nullptr && name == slf.name) {
2917 if (has_comp_level) {
2918 auto complevel = full_name.substr(separator + 1);
2919
2920 /* Get the level and determine whether all went fine. */
2921 auto level = ParseInteger<uint8_t>(complevel);
2922 if (!level.has_value() || *level != Clamp(*level, slf.min_compression, slf.max_compression)) {
2924 GetEncodedString(STR_CONFIG_ERROR),
2925 GetEncodedString(STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, complevel),
2927 } else {
2928 return {slf, *level};
2929 }
2930 }
2931 return {slf, slf.default_compression};
2932 }
2933 }
2934
2936 GetEncodedString(STR_CONFIG_ERROR),
2937 GetEncodedString(STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, name, def.name),
2939 }
2940 return {def, def.default_compression};
2941}
2942
2943/* actual loader/saver function */
2944void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);
2945extern bool AfterLoadGame();
2946extern bool LoadOldSaveGame(std::string_view file);
2947
2953static void ResetSettings()
2954{
2955 for (auto &desc : GetSaveLoadSettingTable()) {
2956 const SettingDesc *sd = GetSettingDesc(desc);
2957 if (sd->flags.Test(SettingFlag::NotInSave)) continue;
2959
2961 }
2962}
2963
2964extern void ClearOldOrders();
2965
2970{
2972 ResetTempEngineData();
2973 ClearRailTypeLabelList();
2974 ClearRoadTypeLabelList();
2975 ResetOldWaypoints();
2976 ResetSettings();
2977}
2978
2982static inline void ClearSaveLoadState()
2983{
2984 _sl.dumper = nullptr;
2985 _sl.sf = nullptr;
2986 _sl.reader = nullptr;
2987 _sl.lf = nullptr;
2988}
2989
2991static void SaveFileStart()
2992{
2993 SetMouseCursorBusy(true);
2994
2995 InvalidateWindowData(WindowClass::Statusbar, 0, SBI_SAVELOAD_START);
2996 _sl.saveinprogress = true;
2997}
2998
3000static void SaveFileDone()
3001{
3002 SetMouseCursorBusy(false);
3003
3004 InvalidateWindowData(WindowClass::Statusbar, 0, SBI_SAVELOAD_FINISH);
3005 _sl.saveinprogress = false;
3006
3007#ifdef __EMSCRIPTEN__
3008 EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
3009#endif
3010}
3011
3017{
3018 _sl.error_str = str;
3019}
3020
3026{
3027 return GetEncodedString(_sl.action == SaveLoadAction::Save ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED);
3028}
3029
3035{
3036 return GetEncodedString(_sl.error_str, _sl.extra_msg);
3037}
3038
3045
3052static SaveLoadResult SaveFileToDisk(bool threaded)
3053{
3054 try {
3055 auto [fmt, compression] = GetSavegameFormat(_savegame_format);
3056
3057 /* We have written our stuff to memory, now write it to file! */
3058 _sl.sf->Write(fmt.tag.data(), fmt.tag.size());
3059
3060 uint32_t version = TO_BE32(to_underlying(SAVEGAME_VERSION) << 16);
3061 _sl.sf->Write(reinterpret_cast<uint8_t *>(&version), sizeof(version));
3062
3063 _sl.sf = fmt.init_write(_sl.sf, compression);
3064 _sl.dumper->Flush(_sl.sf);
3065
3067
3068 if (threaded) SetAsyncSaveFinish(SaveFileDone);
3069
3070 return SaveLoadResult::Ok;
3071 } catch (...) {
3073
3075
3076 /* We don't want to shout when saving is just
3077 * cancelled due to a client disconnecting. */
3078 if (_sl.error_str != STR_NETWORK_ERROR_LOSTCONNECTION) {
3079 Debug(Facility::Sl, Severity::Critical, "{} {}", GetSaveLoadErrorType().GetDecodedString(), GetSaveLoadErrorMessage().GetDecodedString());
3080 asfp = SaveFileError;
3081 }
3082
3083 if (threaded) {
3084 SetAsyncSaveFinish(asfp);
3085 } else {
3086 asfp();
3087 }
3088 return SaveLoadResult::Error;
3089 }
3090}
3091
3092void WaitTillSaved()
3093{
3094 if (!_save_thread.joinable()) return;
3095
3096 _save_thread.join();
3097
3098 /* Make sure every other state is handled properly as well. */
3100}
3101
3110static SaveLoadResult DoSave(std::shared_ptr<SaveFilter> writer, bool threaded)
3111{
3112 assert(!_sl.saveinprogress);
3113
3114 _sl.dumper = std::make_unique<MemoryDumper>();
3115 _sl.sf = std::move(writer);
3116
3118
3119 SaveViewportBeforeSaveGame();
3120 SlSaveChunks();
3121
3122 SaveFileStart();
3123
3124 if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
3125 if (threaded) Debug(Facility::Sl, Severity::Error, "Cannot create savegame thread, reverting to single-threaded mode...");
3126
3127 SaveLoadResult result = SaveFileToDisk(false);
3128 SaveFileDone();
3129
3130 return result;
3131 }
3132
3133 return SaveLoadResult::Ok;
3134}
3135
3142SaveLoadResult SaveWithFilter(std::shared_ptr<SaveFilter> writer, bool threaded)
3143{
3144 try {
3145 _sl.action = SaveLoadAction::Save;
3146 return DoSave(std::move(writer), threaded);
3147 } catch (...) {
3149 return SaveLoadResult::Error;
3150 }
3151}
3152
3161static const SaveLoadFormat *DetermineSaveLoadFormat(SaveLoadFormatTag tag, uint32_t raw_version)
3162{
3163 auto fmt = std::ranges::find(_saveload_formats, tag, &SaveLoadFormat::tag);
3164 if (fmt != std::end(_saveload_formats)) {
3165 /* Check version number */
3166 _sl_version = (SaveLoadVersion)(TO_BE32(raw_version) >> 16);
3167 /* Minor is not used anymore from version 18.0, but it is still needed
3168 * in versions before that (4 cases) which can't be removed easy.
3169 * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */
3170 _sl_minor_version = (TO_BE32(raw_version) >> 8) & 0xFF;
3171
3172 Debug(Facility::Sl, Severity::Error, "Loading savegame version {}", _sl_version);
3173
3174 /* Is the version higher than the current? */
3175 if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME);
3177 return fmt;
3178 }
3179
3180 Debug(Facility::Sl, Severity::Critical, "Unknown savegame type, trying to load it as the buggy format");
3181 _sl.lf->Reset();
3184
3185 /* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
3186 fmt = std::ranges::find(_saveload_formats, SAVEGAME_TAG_LZO, &SaveLoadFormat::tag);
3187 if (fmt == std::end(_saveload_formats)) {
3188 /* Who removed the LZO savegame format definition? When built without LZO support,
3189 * the formats must still list it just without a method to read the file.
3190 * The caller of this function has to check for the existence of load function. */
3191 NOT_REACHED();
3192 }
3193 return fmt;
3194}
3195
3202static SaveLoadResult DoLoad(std::shared_ptr<LoadFilter> reader, bool load_check)
3203{
3204 _sl.lf = std::move(reader);
3205
3206 if (load_check) {
3207 /* Clear previous check data */
3208 _load_check_data.Clear();
3209 /* Mark SL_LOAD_CHECK as supported for this savegame. */
3210 _load_check_data.checkable = true;
3211 }
3212
3213 SaveLoadFormatTag tag{};
3214 if (_sl.lf->Read(tag.data(), tag.size()) != tag.size()) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3215
3216 uint32_t version;
3217 if (_sl.lf->Read(reinterpret_cast<uint8_t*>(&version), sizeof(version)) != sizeof(version)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3218
3219 /* see if we have any loader for this type. */
3220 const SaveLoadFormat *fmt = DetermineSaveLoadFormat(tag, version);
3221
3222 /* loader for this savegame type is not implemented? */
3223 if (fmt->init_load == nullptr) {
3224 SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, fmt::format("Loader for '{}' is not available.", fmt->name));
3225 }
3226
3227 _sl.lf = fmt->init_load(_sl.lf);
3228 _sl.reader = std::make_unique<ReadBuffer>(_sl.lf);
3229 _next_offs = 0;
3230
3231 if (!load_check) {
3233
3234 /* Old maps were hardcoded to 256x256 and thus did not contain
3235 * any mapsize information. Pre-initialize to 256x256 to not to
3236 * confuse old games */
3237 InitializeGame(256, 256, true, true);
3238
3239 _gamelog.Reset();
3240
3242 /*
3243 * NewGRFs were introduced between 0.3,4 and 0.3.5, which both
3244 * shared savegame version 4. Anything before that 'obviously'
3245 * does not have any NewGRFs. Between the introduction and
3246 * savegame version 41 (just before 0.5) the NewGRF settings
3247 * were not stored in the savegame and they were loaded by
3248 * using the settings from the main menu.
3249 * So, to recap:
3250 * - savegame version < 4: do not load any NewGRFs.
3251 * - savegame version >= 41: load NewGRFs from savegame, which is
3252 * already done at this stage by
3253 * overwriting the main menu settings.
3254 * - other savegame versions: use main menu settings.
3255 *
3256 * This means that users *can* crash savegame version 4..40
3257 * savegames if they set incompatible NewGRFs in the main menu,
3258 * but can't crash anymore for savegame version < 4 savegames.
3259 *
3260 * Note: this is done here because AfterLoadGame is also called
3261 * for TTO/TTD/TTDP savegames which have their own NewGRF logic.
3262 */
3264 }
3265 }
3266
3267 if (load_check) {
3268 /* Load chunks into _load_check_data.
3269 * No pools are loaded. References are not possible, and thus do not need resolving. */
3271 } else {
3272 /* Load chunks and resolve references */
3273 SlLoadChunks();
3274 SlFixPointers();
3275 }
3276
3278
3280
3281 if (load_check) {
3282 /* The only part from AfterLoadGame() we need */
3283 _load_check_data.grf_compatibility = IsGoodGRFConfigList(_load_check_data.grfconfig);
3284 } else {
3285 _gamelog.StartAction(GamelogActionType::Load);
3286
3287 /* After loading fix up savegame for any internal changes that
3288 * might have occurred since then. If it fails, load back the old game. */
3289 if (!AfterLoadGame()) {
3290 _gamelog.StopAction();
3292 }
3293
3294 _gamelog.StopAction();
3295 }
3296
3297 return SaveLoadResult::Ok;
3298}
3299
3305SaveLoadResult LoadWithFilter(std::shared_ptr<LoadFilter> reader)
3306{
3307 try {
3308 _sl.action = SaveLoadAction::Load;
3309 return DoLoad(std::move(reader), false);
3310 } catch (...) {
3313 }
3314}
3315
3326SaveLoadResult SaveOrLoad(std::string_view filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
3327{
3328 /* An instance of saving is already active, so don't go saving again */
3329 if (_sl.saveinprogress && fop == SaveLoadOperation::Save && dft == DetailedFileType::GameFile && threaded) {
3330 /* if not an autosave, but a user action, show error message */
3331 if (!_do_autosave) ShowErrorMessage(GetEncodedString(STR_ERROR_SAVE_STILL_IN_PROGRESS), {}, WarningLevel::Error);
3332 return SaveLoadResult::Ok;
3333 }
3334 WaitTillSaved();
3335
3336 try {
3337 /* Load a TTDLX or TTDPatch game */
3340
3341 InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
3342
3343 /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
3344 * and if so a new NewGRF list will be made in LoadOldSaveGame.
3345 * Note: this is done here because AfterLoadGame is also called
3346 * for OTTD savegames which have their own NewGRF logic. */
3348 _gamelog.Reset();
3349 if (!LoadOldSaveGame(filename)) return SaveLoadResult::ReInit;
3352 _gamelog.StartAction(GamelogActionType::Load);
3353 if (!AfterLoadGame()) {
3354 _gamelog.StopAction();
3356 }
3357 _gamelog.StopAction();
3358 return SaveLoadResult::Ok;
3359 }
3360
3361 assert(dft == DetailedFileType::GameFile);
3362 switch (fop) {
3365 break;
3366
3368 _sl.action = SaveLoadAction::Load;
3369 break;
3370
3372 _sl.action = SaveLoadAction::Save;
3373 break;
3374
3375 default: NOT_REACHED();
3376 }
3377
3378 auto fh = (fop == SaveLoadOperation::Save) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
3379
3380 /* Make it a little easier to load savegames from the console */
3381 if (!fh.has_value() && fop != SaveLoadOperation::Save) fh = FioFOpenFile(filename, "rb", Subdirectory::Save);
3382 if (!fh.has_value() && fop != SaveLoadOperation::Save) fh = FioFOpenFile(filename, "rb", Subdirectory::Base);
3383 if (!fh.has_value() && fop != SaveLoadOperation::Save) fh = FioFOpenFile(filename, "rb", Subdirectory::Scenario);
3384
3385 if (!fh.has_value()) {
3386 SlError(fop == SaveLoadOperation::Save ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
3387 }
3388
3389 if (fop == SaveLoadOperation::Save) { // SAVE game
3391 if (!_settings_client.gui.threaded_saves) threaded = false;
3392
3393 return DoSave(std::make_shared<FileWriter>(std::move(*fh)), threaded);
3394 }
3395
3396 /* LOAD game */
3397 assert(fop == SaveLoadOperation::Load || fop == SaveLoadOperation::Check);
3398 Debug(Facility::Desync, Severity::Error, "load: {}", filename);
3399 return DoLoad(std::make_shared<FileReader>(std::move(*fh)), fop == SaveLoadOperation::Check);
3400 } catch (...) {
3401 /* This code may be executed both for old and new save games. */
3403
3404 if (fop != SaveLoadOperation::Check) Debug(Facility::Sl, Severity::Critical, "{} {}", GetSaveLoadErrorType().GetDecodedString(), GetSaveLoadErrorMessage().GetDecodedString());
3405
3406 /* A saver/loader exception!! reinitialize all variables to prevent crash! */
3408 }
3409}
3410
3416{
3417 std::string filename;
3418
3419 if (_settings_client.gui.keep_all_autosave) {
3420 filename = GenerateDefaultSaveName() + counter.Extension();
3421 } else {
3422 filename = counter.Filename();
3423 }
3424
3425 Debug(Facility::Sl, Severity::Warning, "Autosaving to '{}'", filename);
3427 ShowErrorMessage(GetEncodedString(STR_ERROR_AUTOSAVE_FAILED), {}, WarningLevel::Error);
3428 }
3429}
3430
3431
3437
3443{
3444 /* Check if we have a name for this map, which is the name of the first
3445 * available company. When there's no company available we'll use
3446 * 'Spectator' as "company" name. */
3447 CompanyID cid = _local_company;
3448 if (!Company::IsValidID(cid)) {
3449 for (const Company *c : Company::Iterate()) {
3450 cid = c->index;
3451 break;
3452 }
3453 }
3454
3455 std::array<StringParameter, 4> params{};
3456 auto it = params.begin();
3457 *it++ = cid;
3458
3459 /* We show the current game time differently depending on the timekeeping units used by this game. */
3461 /* Insert time played. */
3462 const auto play_time = TimerGameTick::counter / Ticks::TICKS_PER_SECOND;
3463 *it++ = STR_SAVEGAME_DURATION_REALTIME;
3464 *it++ = play_time / 60 / 60;
3465 *it++ = (play_time / 60) % 60;
3466 } else {
3467 /* Insert current date */
3468 switch (_settings_client.gui.date_format_in_default_names) {
3469 case 0: *it++ = STR_JUST_DATE_LONG; break;
3470 case 1: *it++ = STR_JUST_DATE_TINY; break;
3471 case 2: *it++ = STR_JUST_DATE_ISO; break;
3472 default: NOT_REACHED();
3473 }
3474 *it++ = TimerGameEconomy::date;
3475 }
3476
3477 /* Get the correct string (special string for when there's not company) */
3478 std::string filename = GetStringWithArgs(!Company::IsValidID(cid) ? STR_SAVEGAME_NAME_SPECTATOR : STR_SAVEGAME_NAME_DEFAULT, params);
3479 SanitizeFilename(filename);
3480 return filename;
3481}
3482
3489{
3492 this->ftype = FIOS_TYPE_INVALID;
3493 return;
3494 }
3495
3496 this->file_op = fop;
3497 this->ftype = ft;
3498}
3499
3505{
3506 this->SetMode(item.type);
3507 this->name = item.name;
3508 this->title = item.title;
3509}
3510
3512{
3513 assert(this->load_description.has_value());
3514 return *this->load_description;
3515}
Base class for autoreplaces/autorenews.
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
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 HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr enable_if_t< is_integral_v< T >, T > byteswap(T x) noexcept
Custom implementation of std::byteswap; remove once we build with C++23.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
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.
Class for calculation jobs to be run on link graphs.
A connected component of a link graph.
Definition linkgraph.h:35
Handler for saving/loading an object to/from disk.
Definition saveload.h:521
std::optional< std::vector< SaveLoad > > load_description
Description derived from savegame being loaded.
Definition saveload.h:523
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.
SaveLoadCompatTable GetCompatDescription() const override
Get the pre-header description of the fields in the savegame.
SaveLoadTable GetDescription() const override
Get the 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 size_t SlCalcLen(const void *storage, VarType conv, SaveLoadType cmd=SaveLoadType::Variable)
Internal templated helper to return the size in bytes of a list-like type.
static void SlSaveLoad(void *storage, VarType conv, SaveLoadType cmd=SaveLoadType::Variable)
Internal templated helper to save/load 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.
Functions related to companies.
Control codes that are embedded in the translation strings.
@ SCC_ENCODED
Encoded string marker and sub-string parameter.
@ SCC_ENCODED_NUMERIC
Encoded numeric parameter.
@ SCC_ENCODED_STRING
Encoded string parameter.
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Desync
Desync message facility.
Definition debug_type.h:41
@ Sl
Saveload message facility.
Definition debug_type.h:39
@ Warning
Warning, wrong but okay if you don't know.
Definition debug_type.h:17
@ Notice
Notice.
Definition debug_type.h:18
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
@ Debug2
Debug #2 - Low level debug messages.
Definition debug_type.h:21
@ Error
Error, but we are recovering.
Definition debug_type.h:16
Function to handling different endian machines.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
Functions related to errors.
@ Critical
Critical errors, the MessageBox is shown in all cases.
Definition error.h:25
@ Error
Errors (eg. saving/loading failed).
Definition error.h:24
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:1056
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:248
Functions for standard in/out file operations.
SaveLoadOperation
Operation performed on the file.
Definition fileio_type.h:51
@ Check
Load file for checking and/or preview.
Definition fileio_type.h:52
@ Invalid
Unknown file operation.
Definition fileio_type.h:56
@ Save
File is being saved.
Definition fileio_type.h:54
@ Load
File is being loaded.
Definition fileio_type.h:53
DetailedFileType
Kinds of files in each AbstractFileType.
Definition fileio_type.h:27
@ OldGameFile
Old save game or scenario file.
Definition fileio_type.h:29
@ GameFile
Save game or scenario file.
Definition fileio_type.h:30
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
@ Base
Base directory for all subdirectories.
Definition fileio_type.h:88
@ Autosave
Subdirectory of save for autosaves.
Definition fileio_type.h:90
@ Scenario
Base directory for all scenarios.
Definition fileio_type.h:91
@ Save
Base directory for all savegames.
Definition fileio_type.h:89
@ Invalid
Invalid or unknown file type.
Definition fileio_type.h:23
@ None
nothing to do
Definition fileio_type.h:17
Declarations for savegames operations.
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition fios_gui.cpp:42
fluid_settings_t * settings
FluidSynth settings handle.
uint32_t _ttdp_version
version of TTDP savegame (if applicable)
Definition saveload.cpp:83
SaveLoadVersion _sl_version
the major savegame version identifier
Definition saveload.cpp:84
uint8_t _sl_minor_version
the minor savegame version, DO NOT USE!
Definition saveload.cpp:85
Gamelog _gamelog
Gamelog instance.
Definition gamelog.cpp:30
SavegameType _savegame_type
type of savegame we are loading
Definition saveload.cpp:80
const SaveLoadVersion SAVEGAME_VERSION
current savegame version
Functions to be called to log fundamental changes to the game.
@ Load
Game loaded.
Definition gamelog.h:20
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition gfx.cpp:1689
GameSessionStats _game_session_stats
Statistics about the current session.
Definition gfx.cpp:47
Declaration of link graph classes used for cargo distribution.
Declaration of link graph job classes used for cargo distribution.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
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:63
bool _network_server
network-server is active
Definition network.cpp:64
Basic functions/variables used all over the place.
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.
NewGRF handling of rail types.
NewGRF handling of road types.
uint8_t ReadByte(LoadgameState &ls)
Reads a byte from the buffer and decompress if needed.
Definition oldloader.cpp:86
Base class for roadstops.
A number of safeguards to prevent using unsafe methods.
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:342
void ProcessAsyncSaveFinish()
Handle async save finishes.
Definition saveload.cpp:397
void FixSCCEncodedNegative(std::string &str)
Scan the string for SCC_ENCODED_NUMERIC with negative values, and reencode them as uint64_t.
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...
static SaveLoadResult DoSave(std::shared_ptr< SaveFilter > writer, bool threaded)
Actually perform the saving of the savegame.
static const SaveLoadFormat _saveload_formats[]
The different saveload formats known/understood by OpenTTD.
std::string _savegame_format
how to compress savegames
Definition saveload.cpp:86
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:84
static SavegameFileType GetSavegameFileType(const SaveLoad &sld)
Return the type as saved/loaded inside the savegame.
Definition saveload.cpp:640
SaveLoadResult 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 uint32_t ReferenceToInt(const void *obj, SLRefType rt)
Pointers cannot be saved to a savegame, so this functions gets the index of the item,...
static const std::vector< ChunkHandlerRef > & ChunkHandlers()
Definition saveload.cpp:223
static size_t SlCalcRefVectorLen(const void *vector, VarType conv)
Return the size in bytes of a vector.
static void ResetSaveloadData()
Clear temporary data that is passed between various saveload phases.
static void SlWriteSimpleGamma(size_t i)
Write the header descriptor of an object or an array.
Definition saveload.cpp:526
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:87
static std::atomic< AsyncSaveFinishProc > _async_save_finish
Callback to call when the savegame loading is finished.
Definition saveload.cpp:379
static std::thread _save_thread
The thread we're using to compress and write a savegame.
Definition saveload.cpp:380
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:422
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Label< struct SaveLoadFormatLabelTag > SaveLoadFormatTag
Unique 4-letter tag for the different saveload formats.
static SaveLoadResult DoLoad(std::shared_ptr< LoadFilter > reader, bool load_check)
Actually perform the loading of a "non-old" savegame.
SaveLoadResult SaveWithFilter(std::shared_ptr< SaveFilter > writer, bool threaded)
Save the game using a (writer) filter.
static size_t SlCalcArrayLen(size_t length, VarType conv)
Return the size in bytes of a certain type of atomic array.
void(* AsyncSaveFinishProc)()
Callback for when the savegame loading is finished.
Definition saveload.cpp:378
static const SaveLoadFormatTag SAVEGAME_TAG_LZMA
Tag for a game with lzma compression.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:748
static const SaveLoadFormatTag SAVEGAME_TAG_LZO
Tag for a game compressed with LZO.
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
Called by save thread to tell we finished saving.
Definition saveload.cpp:386
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 SaveLoadType::Variables to/from a savegame.
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:874
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:731
NeedLength
Definition saveload.cpp:98
@ WantLength
writing length and data
Definition saveload.cpp:100
@ None
not working in NeedLength mode
Definition saveload.cpp:99
@ CalcLength
need to calculate the length
Definition saveload.cpp:101
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:317
static void SlStdString(void *ptr, VarType conv)
Save/Load a std::string.
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:372
void SlSkipArray()
Skip an array or sparse array.
Definition saveload.cpp:790
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.
static uint SlReadSimpleGamma()
Read in the header descriptor of an object or an array.
Definition saveload.cpp:484
SaveLoadAction
What are we currently doing?
Definition saveload.cpp:90
@ Ptrs
fixing pointers
Definition saveload.cpp:93
@ Null
null all pointers (on loading error)
Definition saveload.cpp:94
@ LoadCheck
partial loading into _load_check_data
Definition saveload.cpp:95
@ Load
loading
Definition saveload.cpp:91
static void SlCopyBytes(void *ptr, size_t length)
Save/Load bytes.
Definition saveload.cpp:854
static ChunkId SlReadChunkId()
Read the ChunkId.
Definition saveload.cpp:468
static void SlCopyInternal(void *object, size_t length, VarType conv)
Internal function to save/Load a list of SaveLoadType::Variables.
static void SlArray(void *array, size_t length, VarType conv)
Save/Load the length of the array followed by the array of SaveLoadType::Variable elements.
static void SlSaveLoadConv(void *ptr, VarType conv)
Handle all conversion and typechecking of variables here.
Definition saveload.cpp:936
static const SaveLoadFormatTag SAVEGAME_TAG_NONE
Tag for a game without compression.
void WriteValue(void *ptr, VarMemType conv, int64_t val)
Write the value of a setting.
Definition saveload.cpp:910
FileToSaveLoad _file_to_saveload
File to save or load in the openttd loop.
Definition saveload.cpp:81
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:802
uint8_t SlReadByte()
Wrapper for reading a byte from the buffer.
Definition saveload.cpp:413
void ClearOldOrders()
Clear all old orders.
Definition order_sl.cpp:115
static SaveLoadParams _sl
Parameters used for/at saveload.
Definition saveload.cpp:221
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit).
static void SlLoadChunks()
Load all chunks.
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:705
static void SlSaveChunk(const ChunkHandler &ch)
Save a chunk of data (eg.
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...
SaveLoadResult LoadWithFilter(std::shared_ptr< LoadFilter > reader)
Load the game using a (reader) filter.
static void SlVector(void *vector, VarType conv)
Save/load a std::vector.
static void SaveFileError()
Show a gui message when saving has failed.
static SaveLoadResult SaveFileToDisk(bool threaded)
We have written the whole game into memory, _memory_savegame, now find and appropriate compressor and...
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.
static uint SlCalcConvMemLen(VarMemType conv)
Return the size in bytes of a certain type of normal/atomic variable as it appears in memory.
Definition saveload.cpp:675
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.
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:105
int64_t ReadValue(const void *ptr, VarMemType conv)
Return a signed-long version of the value of a setting.
Definition saveload.cpp:886
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.
static const SaveLoadFormatTag SAVEGAME_TAG_ZLIB
Tag for a game with zlib compression.
static const ChunkHandler * SlFindChunkHandler(ChunkId id)
Find the ChunkHandler that will be used for processing the found chunk in the savegame or in memory.
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:555
static const SaveLoadFormat * DetermineSaveLoadFormat(SaveLoadFormatTag tag, uint32_t raw_version)
Determines the SaveLoadFormat that is connected to the given tag.
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.
VarMemType
The types/structures of data we have in memory.
Definition saveload.h:657
@ U64
A 64 bit unsigned int.
Definition saveload.h:666
@ Name
old custom name to be converted to a string pointer
Definition saveload.h:669
@ I8
A 8 bit signed int.
Definition saveload.h:659
@ U8
A 8 bit unsigned int.
Definition saveload.h:660
@ Label
A 4 character Label, stored as-is.
Definition saveload.h:670
@ Null
useful to write zeros in savegame.
Definition saveload.h:667
@ I16
A 16 bit signed int.
Definition saveload.h:661
@ Bool
A boolean value.
Definition saveload.h:658
@ U32
A 32 bit unsigned int.
Definition saveload.h:664
@ I32
A 32 bit signed int.
Definition saveload.h:663
@ I64
A 64 bit signed int.
Definition saveload.h:665
@ Str
string pointer
Definition saveload.h:668
@ U16
A 16 bit unsigned int.
Definition saveload.h:662
VarFileType
The types/structures of data that can be stored in the file.
Definition saveload.h:636
@ String
A string.
Definition saveload.h:651
@ U64
A 64 bit unsigned int.
Definition saveload.h:649
@ I8
A 8 bit signed int.
Definition saveload.h:640
@ U8
A 8 bit unsigned int.
Definition saveload.h:642
@ Struct
An arbitrary structure.
Definition saveload.h:652
@ I16
A 16 bit signed int.
Definition saveload.h:643
@ U32
A 32 bit unsigned int.
Definition saveload.h:646
@ StringID
StringID offset into strings-array.
Definition saveload.h:650
@ I32
A 32 bit signed int.
Definition saveload.h:645
@ I64
A 64 bit signed int.
Definition saveload.h:648
@ U16
A 16 bit unsigned int.
Definition saveload.h:644
SavegameType
Types of save games.
Definition saveload.h:432
@ OTTD
OTTD savegame.
Definition saveload.h:436
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:1323
SLRefType
Type of reference (SaveLoad::Reference, SaveLoad::ReferenceList, SaveLoad::ReferenceVector).
Definition saveload.h:621
@ LinkGraph
Load/save a reference to a link graph.
Definition saveload.h:631
@ CargoPacket
Load/save a reference to a cargo packet.
Definition saveload.h:628
@ OrderList
Load/save a reference to an orderlist.
Definition saveload.h:629
@ Station
Load/save a reference to a station.
Definition saveload.h:623
@ OldVehicle
Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
Definition saveload.h:625
@ Storage
Load/save a reference to a persistent storage.
Definition saveload.h:630
@ EngineRenew
Load/save a reference to an engine renewal (autoreplace).
Definition saveload.h:627
@ Town
Load/save a reference to a town.
Definition saveload.h:624
@ LinkGraphJob
Load/save a reference to a link graph job.
Definition saveload.h:632
@ Vehicle
Load/save a reference to a vehicle.
Definition saveload.h:622
@ RoadStop
Load/save a reference to a bus/truck stop.
Definition saveload.h:626
void * GetVariableAddress(const void *object, const SaveLoad &sld)
Get the address of the variable.
Definition saveload.h:1266
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:515
SaveLoadType
Type of data saved.
Definition saveload.h:729
@ ReferenceList
Save/load a list of SaveLoadType::Reference elements.
Definition saveload.h:738
@ String
Save/load a std::string.
Definition saveload.h:734
@ Array
Save/load a fixed-size array of SaveLoadType::Variable elements.
Definition saveload.h:736
@ Variable
Save/load a variable.
Definition saveload.h:730
@ Vector
Save/load a vector of SaveLoadType::Variable elements.
Definition saveload.h:737
@ Reference
Save/load a reference.
Definition saveload.h:731
@ StructList
Save/load a list of structs.
Definition saveload.h:739
@ Struct
Save/load a struct.
Definition saveload.h:732
@ Null
Save null-bytes and load to nowhere.
Definition saveload.h:742
@ SaveByte
Save (but not load) a byte.
Definition saveload.h:741
@ ReferenceVector
Save/load a vector of SaveLoadType::Reference elements.
Definition saveload.h:744
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:518
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1225
SaveLoadVersion
SaveLoad versions Previous savegame versions, the trunk revision where they were introduced and the r...
Definition saveload.h:35
@ EndPatchpacks
Saveload version: 286 Last known patchpack to use a version just above ours.
Definition saveload.h:327
@ MoveSccEncoded
Saveload version: 169, SVN revision: 23816 Move SCC_ENCODED to the first StringControlCode.
Definition saveload.h:251
@ TownTolerancePauseMode
Saveload version: 4.0, SVN revision: 1 Town council tolerance and pause mode.
Definition saveload.h:43
@ MoreCargoPackets
Saveload version: 69, SVN revision: 10319 Allow more than ~65k cargo packets.
Definition saveload.h:131
@ EncodedStringFormat
Saveload version: 350, GitHub pull request: 13499 Encoded String format changed.
Definition saveload.h:405
@ FixSccEncodedNegative
Saveload version: 353, GitHub pull request: 14049 Fix encoding of negative parameters.
Definition saveload.h:408
@ MinVersion
First savegame version.
Definition saveload.h:36
@ SaveloadListLength
Saveload version: 293, GitHub pull request: 9374 Consistency in list length with SaveLoadType::Struc...
Definition saveload.h:336
@ MaxVersion
Highest possible saveload version.
Definition saveload.h:428
@ StartPatchpacks
Saveload version: 220 First known patchpack to use a version just above ours.
Definition saveload.h:326
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
ChunkType
Type of a chunk.
Definition saveload.h:445
@ SparseTable
A SparseArray with a header describing the elements.
Definition saveload.h:450
@ ReadOnly
Chunk is never saved.
Definition saveload.h:453
@ Array
Contiguous array of elements starting at index 0.
Definition saveload.h:447
@ Table
An Array with a header describing the elements.
Definition saveload.h:449
@ FileTypeMask
All ChunkType values that are saved in the file have to be within this mask.
Definition saveload.h:452
@ Riff
4 bits store the chunk type, 28 bits the number of bytes.
Definition saveload.h:446
@ SparseArray
Array of elements with index for each element.
Definition saveload.h:448
Label< struct ChunkIdTag > ChunkId
Label/unique identifier for each of the chunks in the savegame.
Definition saveload.h:457
Functions/types related to errors from savegames.
void SlErrorCorruptFmt(const fmt::format_string< Args... > format, Args &&... fmt_args)
Issue an SlErrorCorrupt with a format string.
Declaration of filters used for saving and loading savegames.
std::shared_ptr< SaveFilter > CreateSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Instantiator for a save filter.
std::shared_ptr< LoadFilter > CreateLoadFilter(std::shared_ptr< LoadFilter > chain)
Instantiator for a load filter.
Functions related to saving and loading games.
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.
SaveLoadResult
Save or load result codes.
@ Error
error that was caught before internal structures were modified
@ Ok
completed successfully
@ ReInit
error that was caught in the middle of updating game state, need to clear it. (can only happen during...
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
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
Functions and types used internally for the settings configurations.
@ 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.
Base classes/functions for stations.
Functions, definitions and such used only by the GUI.
@ SBI_SAVELOAD_FINISH
finished saving
@ SBI_SAVELOAD_START
started saving
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:261
void StrMakeValidInPlace(char *str, StringValidationSettings settings)
Scans the string for invalid characters and replaces them with a question mark '?
Definition string.cpp:156
Compose strings from textual and binary data.
Parse strings.
static std::optional< T > ParseInteger(std::string_view arg, int base=10, bool clamp=false)
Change a string into its number representation.
Functions related to low-level strings.
@ ReplaceWithQuestionMark
Replace the unknown/bad bits with question marks.
Definition string_type.h:45
@ AllowControlCode
Allow the special control codes.
Definition string_type.h:47
EnumBitSet< StringValidationSetting, uint8_t > StringValidationSettings
Bitset of StringValidationSetting elements.
Definition string_type.h:57
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:335
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:89
Functions related to OTTD's strings.
Types related to strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
Base for a four character label/tag/id.
constexpr bool Empty() const
Check whether the label is empty.
Container for cargo from the same location and time.
Definition cargopacket.h:40
Handlers and description of chunk.
Definition saveload.h:460
ChunkType type
Type of the chunk.
Definition saveload.h:462
virtual void LoadCheck(size_t len=0) const
Load the chunk for game preview.
ChunkId id
Unique ID (4 letters).
Definition saveload.h:461
std::string GetName() const
Get the name of this chunk.
Definition saveload.h:505
virtual void Load() const =0
Load the chunk.
virtual void Save() const
Save the chunk.
Definition saveload.h:478
Struct to store engine replacements.
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() override
Make sure everything is cleaned up.
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.
void SetMode(const FiosType &ft, SaveLoadOperation fop=SaveLoadOperation::Load)
Set the mode and file type of the file to save or load.
FiosType ftype
File type.
SaveLoadOperation file_op
File operation to perform.
std::string name
Name of the file.
EncodedString title
Internal name of the game.
void Set(const FiosItem &item)
Set the mode, title and name of the file.
std::optional< FileHandle > file
The file to write to.
void Write(const uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
~FileWriter() override
Make sure everything is cleaned up.
FileWriter(FileHandle &&file)
Create the file writer, so it writes to a specific file.
void Finish() override
Prepare everything to finish writing the savegame.
Deals with finding savegames.
Definition fios.h:85
A savegame name automatically numbered.
Definition fios.h:127
std::string Filename()
Generate a savegame name and number according to _settings_client.gui.max_num_autosaves.
Definition fios.cpp:755
std::string Extension()
Generate an extension for a savegame name.
Definition fios.cpp:765
Elements of a file system that are recognized.
Definition fileio_type.h:62
AbstractFileType abstract
Abstract file type.
Definition fileio_type.h:63
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.
~LZMALoadFilter() override
Clean everything up.
uint8_t fread_buf[MEMORY_CHUNK_SIZE]
Buffer for reading from the file.
LZMALoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
~LZMASaveFilter() override
Clean up what we allocated.
void WriteLoop(const uint8_t *p, size_t len, lzma_action action)
Helper loop for writing the data.
void Write(const 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.
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.
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.
void Write(const 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.
A four character label/tag/id.
std::shared_ptr< LoadFilter > chain
Chained to the (savegame) filters.
LoadFilter(std::shared_ptr< LoadFilter > chain)
Initialise this filter.
Container for dumping the savegame (quickly) to memory.
Definition saveload.cpp:149
uint8_t * buf
Buffer we're going to write to.
Definition saveload.cpp:151
void WriteByte(uint8_t b)
Write a single byte into the dumper.
Definition saveload.cpp:158
std::vector< std::unique_ptr< uint8_t[]> > blocks
Buffer with blocks of allocated memory.
Definition saveload.cpp:150
uint8_t * bufe
End of the buffer we write to.
Definition saveload.cpp:152
size_t GetSize() const
Get the size of the memory dump made so far.
Definition saveload.cpp:192
void Flush(std::shared_ptr< SaveFilter > writer)
Flush this dumper into a writer.
Definition saveload.cpp:173
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.
void Write(const uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
NoCompSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t)
Initialise this filter.
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:383
Class for pooled persistent storage of data.
static Pool::IterateWrapper< Company > Iterate(size_t from=0)
static OrderList * Get(auto index)
uint8_t * bufp
Location we're at reading the buffer.
Definition saveload.cpp:110
ReadBuffer(std::shared_ptr< LoadFilter > reader)
Initialise our variables.
Definition saveload.cpp:119
size_t read
The amount of read bytes so far from the filter.
Definition saveload.cpp:113
size_t GetSize() const
Get the size of the memory dump made so far.
Definition saveload.cpp:141
std::shared_ptr< LoadFilter > reader
The filter used to actually read.
Definition saveload.cpp:112
uint8_t buf[MEMORY_CHUNK_SIZE]
Buffer we're going to read from.
Definition saveload.cpp:109
uint8_t * bufe
End of the buffer we can read from.
Definition saveload.cpp:111
A Stop for a Road Vehicle.
SaveFilter(std::shared_ptr< SaveFilter > chain)
Initialise this filter.
std::shared_ptr< SaveFilter > chain
Chained to the (savegame) filters.
The format for a reader/writer type of a 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.
SaveLoadFormatTag tag
the 4-letter tag by which it is identified in the savegame
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:199
std::unique_ptr< ReadBuffer > reader
Savegame reading buffer.
Definition saveload.cpp:212
std::shared_ptr< SaveFilter > sf
Filter to write the savegame to.
Definition saveload.cpp:210
ChunkType chunk_type
The type of chunk we are reading or writing.
Definition saveload.cpp:202
std::unique_ptr< MemoryDumper > dumper
Memory dumper to write the savegame to.
Definition saveload.cpp:209
StringID error_str
the translatable error message to show
Definition saveload.cpp:215
SaveLoadAction action
are we doing a save or a load atm.
Definition saveload.cpp:200
std::string extra_msg
the error message
Definition saveload.cpp:216
NeedLength need_length
working in NeedLength (Autolength) mode?
Definition saveload.cpp:201
bool saveinprogress
Whether there is currently a save in progress.
Definition saveload.cpp:218
std::shared_ptr< LoadFilter > lf
Filter to read the savegame from.
Definition saveload.cpp:213
bool expect_table_header
In the case of a table, if the header is saved/loaded.
Definition saveload.cpp:207
size_t obj_len
the length of the current object we are busy with
Definition saveload.cpp:205
bool error
did an error occur or not
Definition saveload.cpp:203
int last_array_index
in the case of an array, the current and last positions
Definition saveload.cpp:206
SaveLoad type struct.
Definition saveload.h:763
uint16_t length
(Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
Definition saveload.h:778
std::shared_ptr< SaveLoadHandler > handler
Custom handler for Save/Load procs.
Definition saveload.h:783
SaveLoadVersion version_to
Save/load the variable before this savegame version.
Definition saveload.h:780
SaveLoadType cmd
The action to take with the saved/loaded type, All types need different action.
Definition saveload.h:776
std::string name
Name of this field (optional, used for tables).
Definition saveload.h:775
VarType conv
Type of the variable to be saved; this field combines both FileVarType and MemVarType.
Definition saveload.h:777
SaveLoadVersion version_from
Save/load the variable starting from this savegame version.
Definition saveload.h:779
Container/wrapper for the file type that is used in tables in the save game.
Definition saveload.cpp:589
uint8_t storage
Actual storage of the file type.
Definition saveload.cpp:591
constexpr VarFileType Type() const
Get the VarType for this field.
Definition saveload.cpp:628
SavegameFileType(VarFileType file_type, bool has_field_length=false)
Create the type.
Definition saveload.cpp:601
constexpr bool HasFieldLength() const
Does this field have a length?
Definition saveload.cpp:618
constexpr bool IsEnd() const
Is this the end-of-table marker?
Definition saveload.cpp:612
static constexpr uint8_t HAS_FIELD_LENGTH_BIT
Set this bit to denote the type has a field length.
Definition saveload.cpp:590
SavegameFileType()
Create an end-of-table marker.
Definition saveload.cpp:594
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 Station * Get(auto index)
Station data structure.
Town data structure.
Definition town.h:64
Container of a variable's characteristics about a variable's storage.
Definition saveload.h:674
SLRefType ref
The reference type.
Definition saveload.h:678
VarMemType mem
The way of storing data in memory.
Definition saveload.h:676
StringValidationSettings string_validation_settings
Any settings related to validation of the strings.
Definition saveload.h:677
VarFileType file
The way of storing data in the file.
Definition saveload.h:675
static constexpr VarType U16
Store a 16 bits unsigned int.
Definition saveload.h:717
static constexpr VarType U8
Store a 8 bits unsigned int.
Definition saveload.h:715
static constexpr VarType STR
Store string.
Definition saveload.h:723
static constexpr VarType I16
Store a 16 bits signed int.
Definition saveload.h:716
static constexpr VarType I8
Store a 8 bits signed int.
Definition saveload.h:714
static constexpr VarType LABEL
Store a Label as-is.
Definition saveload.h:725
static constexpr VarType U32
Store a 32 bits unsigned int.
Definition saveload.h:719
static constexpr VarType STRINGID
Store a StringID.
Definition saveload.h:722
static constexpr VarType I32
Store a 32 bits signed int.
Definition saveload.h:718
Vehicle data structure.
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() override
Clean everything up.
z_stream z
Stream state we are reading from.
z_stream z
Stream state we are writing to.
void Write(const uint8_t *buf, size_t size) override
Write a given number of bytes into the savegame.
uint8_t fwrite_buf[MEMORY_CHUNK_SIZE]
Buffer for writing to the file.
~ZlibSaveFilter() override
Clean up what we allocated.
void Finish() override
Prepare everything to finish writing the savegame.
ZlibSaveFilter(std::shared_ptr< SaveFilter > chain, uint8_t compression_level)
Initialise this filter.
void WriteLoop(const uint8_t *p, size_t len, int mode)
Helper loop for writing the data.
Base of all threads.
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
Definition of the game-economy-timer.
Base of the town class.
Base class for all vehicles.
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:3377
Window functions not directly related to making/drawing windows.