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

OpenTTD Source 14.1

oldloader.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../debug.h"
12 #include "../strings_type.h"
13 #include "../string_func.h"
14 #include "../settings_type.h"
15 #include "../fileio_func.h"
16
17 #include "table/strings.h"
18
19 #include "saveload_internal.h"
20 #include "oldloader.h"
21
22
23 #include "../safeguards.h"
24
25 static const int TTO_HEADER_SIZE = 41;
26 static const int TTD_HEADER_SIZE = 49;
28 static const int HEADER_CHECKSUM_SIZE = 2;
29
30 uint32_t _bump_assert_value;
31
32 static inline OldChunkType GetOldChunkType(OldChunkType type) {return (OldChunkType)GB(type, 0, 4);}
33 static inline OldChunkType GetOldChunkVarType(OldChunkType type) {return (OldChunkType)(GB(type, 8, 8) << 8);}
34 static inline OldChunkType GetOldChunkFileType(OldChunkType type) {return (OldChunkType)(GB(type, 16, 8) << 16);}
35
36 static inline byte CalcOldVarLen(OldChunkType type)
37 {
38 static const byte type_mem_size[] = {0, 1, 1, 2, 2, 4, 4, 8};
39 byte length = GB(type, 8, 8);
40 assert(length != 0 && length < lengthof(type_mem_size));
41 return type_mem_size[length];
42 }
43
49 static byte ReadByteFromFile(LoadgameState *ls)
50 {
51 /* To avoid slow reads, we read BUFFER_SIZE of bytes per time
52 and just return a byte per time */
53 if (ls->buffer_cur >= ls->buffer_count) {
54
55 /* Read some new bytes from the file */
56 int count = (int)fread(ls->buffer, 1, BUFFER_SIZE, ls->file);
57
58 /* We tried to read, but there is nothing in the file anymore.. */
59 if (count == 0) {
60 Debug(oldloader, 0, "Read past end of file, loading failed");
61 throw std::exception();
62 }
63
64 ls->buffer_count = count;
65 ls->buffer_cur = 0;
66 }
67
68 return ls->buffer[ls->buffer_cur++];
69 }
70
76 byte ReadByte(LoadgameState *ls)
77 {
78 /* Old savegames have a nice compression algorithm (RLE)
79 which means that we have a chunk, which starts with a length
80 byte. If that byte is negative, we have to repeat the next byte
81 that many times ( + 1). Else, we need to read that amount of bytes.
82 Works pretty well if you have many zeros behind each other */
83
84 if (ls->chunk_size == 0) {
85 /* Read new chunk */
86 int8_t new_byte = ReadByteFromFile(ls);
87
88 if (new_byte < 0) {
89 /* Repeat next char for new_byte times */
90 ls->decoding = true;
91 ls->decode_char = ReadByteFromFile(ls);
92 ls->chunk_size = -new_byte + 1;
93 } else {
94 ls->decoding = false;
95 ls->chunk_size = new_byte + 1;
96 }
97 }
98
99 ls->total_read++;
100 ls->chunk_size--;
101
102 return ls->decoding ? ls->decode_char : ReadByteFromFile(ls);
103 }
104
110 bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
111 {
112 for (const OldChunks *chunk = chunks; chunk->type != OC_END; chunk++) {
113 if (((chunk->type & OC_TTD) && _savegame_type == SGT_TTO) ||
114 ((chunk->type & OC_TTO) && _savegame_type != SGT_TTO)) {
115 /* TTD(P)-only chunk, but TTO savegame || TTO-only chunk, but TTD/TTDP savegame */
116 continue;
117 }
118
119 byte *ptr = (byte*)chunk->ptr;
120 if (chunk->type & OC_DEREFERENCE_POINTER) ptr = *(byte**)ptr;
121
122 for (uint i = 0; i < chunk->amount; i++) {
123 /* Handle simple types */
124 if (GetOldChunkType(chunk->type) != 0) {
125 switch (GetOldChunkType(chunk->type)) {
126 /* Just read the byte and forget about it */
127 case OC_NULL: ReadByte(ls); break;
128
129 case OC_CHUNK:
130 /* Call function, with 'i' as parameter to tell which item we
131 * are going to read */
132 if (!chunk->proc(ls, i)) return false;
133 break;
134
135 case OC_ASSERT:
136 Debug(oldloader, 4, "Assert point: 0x{:X} / 0x{:X}", ls->total_read, (uint)(size_t)chunk->ptr + _bump_assert_value);
137 if (ls->total_read != (size_t)chunk->ptr + _bump_assert_value) throw std::exception();
138 default: break;
139 }
140 } else {
141 uint64_t res = 0;
142
143 /* Reading from the file: bits 16 to 23 have the FILE type */
144 switch (GetOldChunkFileType(chunk->type)) {
145 case OC_FILE_I8: res = (int8_t)ReadByte(ls); break;
146 case OC_FILE_U8: res = ReadByte(ls); break;
147 case OC_FILE_I16: res = (int16_t)ReadUint16(ls); break;
148 case OC_FILE_U16: res = ReadUint16(ls); break;
149 case OC_FILE_I32: res = (int32_t)ReadUint32(ls); break;
150 case OC_FILE_U32: res = ReadUint32(ls); break;
151 default: NOT_REACHED();
152 }
153
154 /* When both pointers are nullptr, we are just skipping data */
155 if (base == nullptr && chunk->ptr == nullptr) continue;
156
157 /* Chunk refers to a struct member, get address in base. */
158 if (chunk->ptr == nullptr) ptr = (byte *)chunk->offset(base);
159
160 /* Write the data */
161 switch (GetOldChunkVarType(chunk->type)) {
162 case OC_VAR_I8: *(int8_t *)ptr = GB(res, 0, 8); break;
163 case OC_VAR_U8: *(uint8_t *)ptr = GB(res, 0, 8); break;
164 case OC_VAR_I16:*(int16_t *)ptr = GB(res, 0, 16); break;
165 case OC_VAR_U16:*(uint16_t*)ptr = GB(res, 0, 16); break;
166 case OC_VAR_I32:*(int32_t *)ptr = res; break;
167 case OC_VAR_U32:*(uint32_t*)ptr = res; break;
168 case OC_VAR_I64:*(int64_t *)ptr = res; break;
169 case OC_VAR_U64:*(uint64_t*)ptr = res; break;
170 default: NOT_REACHED();
171 }
172
173 /* Increase pointer base for arrays when looping */
174 if (chunk->amount > 1 && chunk->ptr != nullptr) ptr += CalcOldVarLen(chunk->type);
175 }
176 }
177 }
178
179 return true;
180 }
181
187 static void InitLoading(LoadgameState *ls)
188 {
189 ls->chunk_size = 0;
190 ls->total_read = 0;
191
192 ls->decoding = false;
193 ls->decode_char = 0;
194
195 ls->buffer_cur = 0;
196 ls->buffer_count = 0;
197 memset(ls->buffer, 0, BUFFER_SIZE);
198
199 _bump_assert_value = 0;
200
201 _settings_game.construction.freeform_edges = false; // disable so we can convert map array (SetTileType is still used)
202 }
203
210 static bool VerifyOldNameChecksum(char *title, uint len)
211 {
212 uint16_t sum = 0;
213 for (uint i = 0; i < len - HEADER_CHECKSUM_SIZE; i++) {
214 sum += title[i];
215 sum = std::rotl(sum, 1);
216 }
217
218 sum ^= 0xAAAA; // computed checksum
219
220 uint16_t sum2 = title[len - HEADER_CHECKSUM_SIZE]; // checksum in file
221 SB(sum2, 8, 8, title[len - HEADER_CHECKSUM_SIZE + 1]);
222
223 return sum == sum2;
224 }
225
226 static std::tuple<SavegameType, std::string> DetermineOldSavegameTypeAndName(FILE *f)
227 {
228 long pos = ftell(f);
229 char buffer[std::max(TTO_HEADER_SIZE, TTD_HEADER_SIZE)];
230 if (pos < 0 || fread(buffer, 1, lengthof(buffer), f) != lengthof(buffer)) {
231 return { SGT_INVALID, "(broken) Unable to read file" };
232 }
233
234 if (VerifyOldNameChecksum(buffer, TTO_HEADER_SIZE) && fseek(f, pos + TTO_HEADER_SIZE, SEEK_SET) == 0) {
235 return { SGT_TTO, "(TTO)" + StrMakeValid({buffer, TTO_HEADER_SIZE - HEADER_CHECKSUM_SIZE}) };
236 }
237
238 if (VerifyOldNameChecksum(buffer, TTD_HEADER_SIZE) && fseek(f, pos + TTD_HEADER_SIZE, SEEK_SET) == 0) {
239 return { SGT_TTD, "(TTD)" + StrMakeValid({buffer, TTD_HEADER_SIZE - HEADER_CHECKSUM_SIZE}) };
240 }
241
242 return { SGT_INVALID, "(broken) Unknown" };
243 }
244
245 typedef bool LoadOldMainProc(LoadgameState *ls);
246
247 bool LoadOldSaveGame(const std::string &file)
248 {
249 LoadgameState ls;
250
251 Debug(oldloader, 3, "Trying to load a TTD(Patch) savegame");
252
253 InitLoading(&ls);
254
255 /* Open file */
256 ls.file = FioFOpenFile(file, "rb", NO_DIRECTORY);
257
258 if (ls.file == nullptr) {
259 Debug(oldloader, 0, "Cannot open file '{}'", file);
260 return false;
261 }
262
263 SavegameType type;
264 std::tie(type, std::ignore) = DetermineOldSavegameTypeAndName(ls.file);
265
266 LoadOldMainProc *proc = nullptr;
267
268 switch (type) {
269 case SGT_TTO: proc = &LoadTTOMain; break;
270 case SGT_TTD: proc = &LoadTTDMain; break;
271 default:
272 Debug(oldloader, 0, "Unknown savegame type; cannot be loaded");
273 break;
274 }
275
276 _savegame_type = type;
277
278 bool game_loaded;
279 try {
280 game_loaded = proc != nullptr && proc(&ls);
281 } catch (...) {
282 game_loaded = false;
283 }
284
285 if (!game_loaded) {
286 SetSaveLoadError(STR_GAME_SAVELOAD_ERROR_DATA_INTEGRITY_CHECK_FAILED);
287 fclose(ls.file);
288 return false;
289 }
290
291 _pause_mode = PM_PAUSED_SAVELOAD;
292
293 return true;
294 }
295
296 std::string GetOldSaveGameName(const std::string &file)
297 {
298 FILE *f = FioFOpenFile(file, "rb", NO_DIRECTORY);
299 if (f == nullptr) return {};
300
301 std::string name;
302 std::tie(std::ignore, name) = DetermineOldSavegameTypeAndName(f);
303 fclose(f);
304 return name;
305 }
SGT_INVALID
@ SGT_INVALID
broken savegame (used internally)
Definition: saveload.h:412
OC_DEREFERENCE_POINTER
@ OC_DEREFERENCE_POINTER
Dereference the pointer once before writing to it, so we do not have to use big static arrays.
Definition: oldloader.h:77
LoadgameState
Definition: oldloader.h:19
oldloader.h
SetSaveLoadError
void SetSaveLoadError(StringID str)
Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friend...
Definition: saveload.cpp:2758
ReadByte
byte ReadByte(LoadgameState *ls)
Reads a byte from the buffer and decompress if needed.
Definition: oldloader.cpp:76
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
StrMakeValid
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition: string.cpp:114
_savegame_type
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:59
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SavegameType
SavegameType
Types of save games.
Definition: saveload.h:406
PM_PAUSED_SAVELOAD
@ PM_PAUSED_SAVELOAD
A game paused for saving/loading.
Definition: openttd.h:71
SGT_TTO
@ SGT_TTO
TTO savegame.
Definition: saveload.h:411
LoadChunk
bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
Loads a chunk from the old savegame.
Definition: oldloader.cpp:110
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:264
VerifyOldNameChecksum
static bool VerifyOldNameChecksum(char *title, uint len)
Verifies the title has a valid checksum.
Definition: oldloader.cpp:210
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:49
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
OC_TTO
@ OC_TTO
-//- TTO (default is neither of these)
Definition: oldloader.h:43
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:303
ConstructionSettings::freeform_edges
bool freeform_edges
allow terraforming the tiles at the map edges
Definition: settings_type.h:382
HEADER_CHECKSUM_SIZE
static const int HEADER_CHECKSUM_SIZE
The size of the checksum in the name/header of the TTD/TTO savegames.
Definition: oldloader.cpp:28
OldChunkType
OldChunkType
Definition: oldloader.h:35
SGT_TTD
@ SGT_TTD
TTD savegame (can be detected incorrectly)
Definition: saveload.h:407
ReadByteFromFile
static byte ReadByteFromFile(LoadgameState *ls)
Reads a byte from a file (do not call yourself, use ReadByte())
Definition: oldloader.cpp:49
OldChunks
Definition: oldloader.h:87
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:126
InitLoading
static void InitLoading(LoadgameState *ls)
Initialize some data before reading.
Definition: oldloader.cpp:187
OC_END
@ OC_END
End of the whole chunk, all 32 bits set to zero.
Definition: oldloader.h:79
saveload_internal.h
OC_TTD
@ OC_TTD
chunk is valid ONLY for TTD savegames
Definition: oldloader.h:42
SB
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:619
OldChunks::type
OldChunkType type
Type of field.
Definition: oldloader.h:88