OpenTTD
oldloader.h
Go to the documentation of this file.
1 /* $Id: oldloader.h 23595 2011-12-19 17:48:04Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef OLDLOADER_H
13 #define OLDLOADER_H
14 
15 #include "saveload.h"
16 #include "../tile_type.h"
17 
18 static const uint BUFFER_SIZE = 4096;
19 static const uint OLD_MAP_SIZE = 256 * 256;
20 
21 struct LoadgameState {
22  FILE *file;
23 
24  uint chunk_size;
25 
26  bool decoding;
27  byte decode_char;
28 
29  uint buffer_count;
30  uint buffer_cur;
31  byte buffer[BUFFER_SIZE];
32 
33  uint total_read;
34 };
35 
36 /* OldChunk-Type */
38  OC_SIMPLE = 0,
39  OC_NULL = 1,
40  OC_CHUNK = 2,
41  OC_ASSERT = 3,
42  /* 4 bits allocated (16 max) */
43 
44  OC_TTD = 1 << 4,
45  OC_TTO = 1 << 5,
46  /* 4 bits allocated */
47 
48  OC_VAR_I8 = 1 << 8,
49  OC_VAR_U8 = 2 << 8,
50  OC_VAR_I16 = 3 << 8,
51  OC_VAR_U16 = 4 << 8,
52  OC_VAR_I32 = 5 << 8,
53  OC_VAR_U32 = 6 << 8,
54  OC_VAR_I64 = 7 << 8,
55  OC_VAR_U64 = 8 << 8,
56  /* 8 bits allocated (256 max) */
57 
58  OC_FILE_I8 = 1 << 16,
59  OC_FILE_U8 = 2 << 16,
60  OC_FILE_I16 = 3 << 16,
61  OC_FILE_U16 = 4 << 16,
62  OC_FILE_I32 = 5 << 16,
63  OC_FILE_U32 = 6 << 16,
64  /* 8 bits allocated (256 max) */
65 
66  OC_INT8 = OC_VAR_I8 | OC_FILE_I8,
67  OC_UINT8 = OC_VAR_U8 | OC_FILE_U8,
68  OC_INT16 = OC_VAR_I16 | OC_FILE_I16,
69  OC_UINT16 = OC_VAR_U16 | OC_FILE_U16,
70  OC_INT32 = OC_VAR_I32 | OC_FILE_I32,
71  OC_UINT32 = OC_VAR_U32 | OC_FILE_U32,
72 
73  OC_TILE = OC_VAR_U32 | OC_FILE_U16,
74 
80 
81  OC_END = 0,
82 };
83 
85 
86 typedef bool OldChunkProc(LoadgameState *ls, int num);
87 
88 struct OldChunks {
90  uint32 amount;
91 
92  void *ptr;
93  uint offset;
94  OldChunkProc *proc;
95 };
96 
97 /* If it fails, check lines above.. */
98 assert_compile(sizeof(TileIndex) == 4);
99 
100 extern uint _bump_assert_value;
101 byte ReadByte(LoadgameState *ls);
102 bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks);
103 
104 bool LoadTTDMain(LoadgameState *ls);
105 bool LoadTTOMain(LoadgameState *ls);
106 
107 static inline uint16 ReadUint16(LoadgameState *ls)
108 {
109  byte x = ReadByte(ls);
110  return x | ReadByte(ls) << 8;
111 }
112 
113 static inline uint32 ReadUint32(LoadgameState *ls)
114 {
115  uint16 x = ReadUint16(ls);
116  return x | ReadUint16(ls) << 16;
117 }
118 
119 /* Help:
120  * - OCL_SVAR: load 'type' to offset 'offset' in a struct of type 'base', which must also
121  * be given via base in LoadChunk() as real pointer
122  * - OCL_VAR: load 'type' to a global var
123  * - OCL_END: every struct must end with this
124  * - OCL_NULL: read 'amount' of bytes and send them to /dev/null or something
125  * - OCL_CHUNK: load another proc to load a part of the savegame, 'amount' times
126  * - OCL_ASSERT: to check if we are really at the place we expect to be.. because old savegames are too binary to be sure ;)
127  */
128 #define OCL_SVAR(type, base, offset) { type, 1, NULL, (uint)cpp_offsetof(base, offset), NULL }
129 #define OCL_VAR(type, amount, pointer) { type, amount, pointer, 0, NULL }
130 #define OCL_END() { OC_END, 0, NULL, 0, NULL }
131 #define OCL_CNULL(type, amount) { OC_NULL | type, amount, NULL, 0, NULL }
132 #define OCL_CCHUNK(type, amount, proc) { OC_CHUNK | type, amount, NULL, 0, proc }
133 #define OCL_ASSERT(type, size) { OC_ASSERT | type, 1, NULL, size, NULL }
134 #define OCL_NULL(amount) OCL_CNULL((OldChunkType)0, amount)
135 #define OCL_CHUNK(amount, proc) OCL_CCHUNK((OldChunkType)0, amount, proc)
136 
137 #endif /* OLDLOADER_H */