OpenTTD Source: src/saveload/oldloader.h Source File

OpenTTD Source 15.3

oldloader.h
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
10#ifndef OLDLOADER_H
11#define OLDLOADER_H
12
13#include "saveload.h"
14#include "../tile_type.h"
15
16static const uint BUFFER_SIZE = 4096;
17static const uint OLD_MAP_SIZE = 256;
18
19 struct LoadgameState {
20 std::optional<FileHandle> file;
21
22 uint chunk_size = 0;
23
24 bool decoding = false;
25 uint8_t decode_char = 0;
26
27 uint buffer_count = 0;
28 uint buffer_cur = 0;
29 std::array<uint8_t, BUFFER_SIZE> buffer{};
30
31 uint total_read = 0;
32
33 uint8_t vehicle_multiplier = 1;
34 std::vector<StringID> vehicle_names;
35};
36
37/* OldChunk-Type */
38 enum OldChunkType : uint32_t {
39 OC_SIMPLE = 0,
40 OC_NULL = 1,
41 OC_CHUNK = 2,
42 OC_ASSERT = 3,
43 /* 4 bits allocated (16 max) */
44
45 OC_TTD = 1 << 4,
46 OC_TTO = 1 << 5,
47 /* 4 bits allocated */
48
49 OC_VAR_I8 = 1 << 8,
50 OC_VAR_U8 = 2 << 8,
51 OC_VAR_I16 = 3 << 8,
52 OC_VAR_U16 = 4 << 8,
53 OC_VAR_I32 = 5 << 8,
54 OC_VAR_U32 = 6 << 8,
55 OC_VAR_I64 = 7 << 8,
56 OC_VAR_U64 = 8 << 8,
57 /* 8 bits allocated (256 max) */
58
59 OC_FILE_I8 = 1 << 16,
60 OC_FILE_U8 = 2 << 16,
61 OC_FILE_I16 = 3 << 16,
62 OC_FILE_U16 = 4 << 16,
63 OC_FILE_I32 = 5 << 16,
64 OC_FILE_U32 = 6 << 16,
65 /* 8 bits allocated (256 max) */
66
67 OC_INT8 = OC_VAR_I8 | OC_FILE_I8,
68 OC_UINT8 = OC_VAR_U8 | OC_FILE_U8,
69 OC_INT16 = OC_VAR_I16 | OC_FILE_I16,
70 OC_UINT16 = OC_VAR_U16 | OC_FILE_U16,
71 OC_INT32 = OC_VAR_I32 | OC_FILE_I32,
72 OC_UINT32 = OC_VAR_U32 | OC_FILE_U32,
73
74 OC_TILE = OC_VAR_U32 | OC_FILE_U16,
75
76 OC_END = 0,
77};
78
79 DECLARE_ENUM_AS_BIT_SET(OldChunkType)
80
81typedef bool OldChunkProc(LoadgameState &ls, int num);
82typedef void *OffsetProc(void *base);
83
84 struct OldChunks {
85 OldChunkType type;
86 uint32_t amount;
87
88 void *ptr;
89 OffsetProc *offset;
90 OldChunkProc *proc;
91};
92
93extern uint _bump_assert_value;
94uint8_t ReadByte(LoadgameState &ls);
95bool LoadChunk(LoadgameState &ls, void *base, const OldChunks *chunks);
96
97bool LoadTTDMain(LoadgameState &ls);
98bool LoadTTOMain(LoadgameState &ls);
99
100inline uint16_t ReadUint16(LoadgameState &ls)
101{
102 uint8_t x = ReadByte(ls);
103 return x | ReadByte(ls) << 8;
104}
105
106inline uint32_t ReadUint32(LoadgameState &ls)
107{
108 uint16_t x = ReadUint16(ls);
109 return x | ReadUint16(ls) << 16;
110}
111
112/* Help:
113 * - OCL_SVAR: load 'type' to offset 'offset' in a struct of type 'base', which must also
114 * be given via base in LoadChunk() as real pointer
115 * - OCL_VAR: load 'type' to a global var
116 * - OCL_END: every struct must end with this
117 * - OCL_NULL: read 'amount' of bytes and send them to /dev/null or something
118 * - OCL_CHUNK: load another proc to load a part of the savegame, 'amount' times
119 * - OCL_ASSERT: to check if we are really at the place we expect to be.. because old savegames are too binary to be sure ;)
120 */
121#define OCL_SVAR(type, base, offset) { type, 1, nullptr, [] (void *b) -> void * { return std::addressof(static_cast<base *>(b)->offset); }, nullptr }
122#define OCL_VAR(type, amount, pointer) { type, amount, pointer, nullptr, nullptr }
123#define OCL_END() { OC_END, 0, nullptr, nullptr, nullptr }
124#define OCL_CNULL(type, amount) { OC_NULL | type, amount, nullptr, nullptr, nullptr }
125#define OCL_CCHUNK(type, amount, proc) { OC_CHUNK | type, amount, nullptr, nullptr, proc }
126#define OCL_ASSERT(type, size) { OC_ASSERT | type, 1, (void *)(size_t)size, nullptr, nullptr }
127#define OCL_NULL(amount) OCL_CNULL((OldChunkType)0, amount)
128#define OCL_CHUNK(amount, proc) OCL_CCHUNK((OldChunkType)0, amount, proc)
129
130#endif /* OLDLOADER_H */
DECLARE_ENUM_AS_BIT_SET
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
Operators to allow to work with enum as with type safe bit set in C++.
Definition enum_type.hpp:119
OldChunkType
OldChunkType
Definition oldloader.h:38
OC_END
@ OC_END
End of the whole chunk, all 32 bits set to zero.
Definition oldloader.h:76
OC_TTO
@ OC_TTO
-//- TTO (default is neither of these)
Definition oldloader.h:46
OC_TTD
@ OC_TTD
chunk is valid ONLY for TTD savegames
Definition oldloader.h:45
LoadChunk
bool LoadChunk(LoadgameState &ls, void *base, const OldChunks *chunks)
Loads a chunk from the old savegame.
Definition oldloader.cpp:120
ReadByte
uint8_t ReadByte(LoadgameState &ls)
Reads a byte from the buffer and decompress if needed.
Definition oldloader.cpp:86
saveload.h
Functions/types related to saving and loading games.
LoadgameState
Definition oldloader.h:19
LoadgameState::vehicle_multiplier
uint8_t vehicle_multiplier
TTDPatch vehicle multiplier.
Definition oldloader.h:33
OldChunks
Definition oldloader.h:84
OldChunks::amount
uint32_t amount
Amount of fields.
Definition oldloader.h:86
OldChunks::type
OldChunkType type
Type of field.
Definition oldloader.h:85
OldChunks::offset
OffsetProc * offset
Pointer to function that returns the actual memory address of a member (ignored if ptr is not nullptr...
Definition oldloader.h:89
OldChunks::ptr
void * ptr
Pointer where to save the data (takes precedence over offset)
Definition oldloader.h:88
OldChunks::proc
OldChunkProc * proc
Pointer to function that is called with OC_CHUNK.
Definition oldloader.h:90