OpenTTD
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef OLDLOADER_H
11 #define OLDLOADER_H
12 
13 #include "saveload.h"
14 #include "../tile_type.h"
15 
16 static const uint BUFFER_SIZE = 4096;
17 static const uint OLD_MAP_SIZE = 256 * 256;
18 
19 struct LoadgameState {
20  FILE *file;
21 
22  uint chunk_size;
23 
24  bool decoding;
25  byte decode_char;
26 
27  uint buffer_count;
28  uint buffer_cur;
29  byte buffer[BUFFER_SIZE];
30 
31  uint total_read;
32 };
33 
34 /* OldChunk-Type */
36  OC_SIMPLE = 0,
37  OC_NULL = 1,
38  OC_CHUNK = 2,
39  OC_ASSERT = 3,
40  /* 4 bits allocated (16 max) */
41 
42  OC_TTD = 1 << 4,
43  OC_TTO = 1 << 5,
44  /* 4 bits allocated */
45 
46  OC_VAR_I8 = 1 << 8,
47  OC_VAR_U8 = 2 << 8,
48  OC_VAR_I16 = 3 << 8,
49  OC_VAR_U16 = 4 << 8,
50  OC_VAR_I32 = 5 << 8,
51  OC_VAR_U32 = 6 << 8,
52  OC_VAR_I64 = 7 << 8,
53  OC_VAR_U64 = 8 << 8,
54  /* 8 bits allocated (256 max) */
55 
56  OC_FILE_I8 = 1 << 16,
57  OC_FILE_U8 = 2 << 16,
58  OC_FILE_I16 = 3 << 16,
59  OC_FILE_U16 = 4 << 16,
60  OC_FILE_I32 = 5 << 16,
61  OC_FILE_U32 = 6 << 16,
62  /* 8 bits allocated (256 max) */
63 
64  OC_INT8 = OC_VAR_I8 | OC_FILE_I8,
65  OC_UINT8 = OC_VAR_U8 | OC_FILE_U8,
66  OC_INT16 = OC_VAR_I16 | OC_FILE_I16,
67  OC_UINT16 = OC_VAR_U16 | OC_FILE_U16,
68  OC_INT32 = OC_VAR_I32 | OC_FILE_I32,
69  OC_UINT32 = OC_VAR_U32 | OC_FILE_U32,
70 
71  OC_TILE = OC_VAR_U32 | OC_FILE_U16,
72 
78 
79  OC_END = 0,
80 };
81 
83 
84 typedef bool OldChunkProc(LoadgameState *ls, int num);
85 
86 struct OldChunks {
88  uint32 amount;
89 
90  void *ptr;
91  uint offset;
92  OldChunkProc *proc;
93 };
94 
95 /* If it fails, check lines above.. */
96 assert_compile(sizeof(TileIndex) == 4);
97 
98 extern uint _bump_assert_value;
99 byte ReadByte(LoadgameState *ls);
100 bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks);
101 
102 bool LoadTTDMain(LoadgameState *ls);
103 bool LoadTTOMain(LoadgameState *ls);
104 
105 static inline uint16 ReadUint16(LoadgameState *ls)
106 {
107  byte x = ReadByte(ls);
108  return x | ReadByte(ls) << 8;
109 }
110 
111 static inline uint32 ReadUint32(LoadgameState *ls)
112 {
113  uint16 x = ReadUint16(ls);
114  return x | ReadUint16(ls) << 16;
115 }
116 
117 /* Help:
118  * - OCL_SVAR: load 'type' to offset 'offset' in a struct of type 'base', which must also
119  * be given via base in LoadChunk() as real pointer
120  * - OCL_VAR: load 'type' to a global var
121  * - OCL_END: every struct must end with this
122  * - OCL_NULL: read 'amount' of bytes and send them to /dev/null or something
123  * - OCL_CHUNK: load another proc to load a part of the savegame, 'amount' times
124  * - OCL_ASSERT: to check if we are really at the place we expect to be.. because old savegames are too binary to be sure ;)
125  */
126 #define OCL_SVAR(type, base, offset) { type, 1, nullptr, (uint)cpp_offsetof(base, offset), nullptr }
127 #define OCL_VAR(type, amount, pointer) { type, amount, pointer, 0, nullptr }
128 #define OCL_END() { OC_END, 0, nullptr, 0, nullptr }
129 #define OCL_CNULL(type, amount) { OC_NULL | type, amount, nullptr, 0, nullptr }
130 #define OCL_CCHUNK(type, amount, proc) { OC_CHUNK | type, amount, nullptr, 0, proc }
131 #define OCL_ASSERT(type, size) { OC_ASSERT | type, 1, nullptr, size, nullptr }
132 #define OCL_NULL(amount) OCL_CNULL((OldChunkType)0, amount)
133 #define OCL_CHUNK(amount, proc) OCL_CCHUNK((OldChunkType)0, amount, proc)
134 
135 #endif /* OLDLOADER_H */
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
void * ptr
Pointer where to save the data (may only be set if offset is 0)
Definition: oldloader.h:90
-//- TTO (default is neither of these)
Definition: oldloader.h:43
OldChunkType
Definition: oldloader.h:35
chunk is valid ONLY for TTD savegames
Definition: oldloader.h:42
OldChunkType type
Type of field.
Definition: oldloader.h:87
Functions/types related to saving and loading games.
bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
Loads a chunk from the old savegame.
Definition: oldloader.cpp:109
uint offset
Offset from basepointer (may only be set if ptr is nullptr)
Definition: oldloader.h:91
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
uint32 amount
Amount of fields.
Definition: oldloader.h:88
End of the whole chunk, all 32 bits set to zero.
Definition: oldloader.h:79
byte ReadByte(LoadgameState *ls)
Reads a byte from the buffer and decompress if needed.
Definition: oldloader.cpp:75
Dereference the pointer once before writing to it, so we do not have to use big static arrays...
Definition: oldloader.h:77