Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / saveload / oldloader.h
blobf92aa31f3287c7f11d7324d5c13f6d1703d6c59a
1 /* $Id: oldloader.h 23595 2011-12-19 17:48:04Z rubidium $ */
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 */
10 /** @file oldloader.h Declarations of strctures and function used in loader of old savegames */
12 #ifndef OLDLOADER_H
13 #define OLDLOADER_H
15 #include "saveload.h"
16 #include "../tile_type.h"
18 static const uint BUFFER_SIZE = 4096;
19 static const uint OLD_MAP_SIZE = 256 * 256;
21 struct LoadgameState {
22 FILE *file;
24 uint chunk_size;
26 bool decoding;
27 byte decode_char;
29 uint buffer_count;
30 uint buffer_cur;
31 byte buffer[BUFFER_SIZE];
33 uint total_read;
36 /* OldChunk-Type */
37 enum OldChunkType {
38 OC_SIMPLE = 0,
39 OC_NULL = 1,
40 OC_CHUNK = 2,
41 OC_ASSERT = 3,
42 /* 4 bits allocated (16 max) */
44 OC_TTD = 1 << 4, ///< chunk is valid ONLY for TTD savegames
45 OC_TTO = 1 << 5, ///< -//- TTO (default is neither of these)
46 /* 4 bits allocated */
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) */
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) */
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,
73 OC_TILE = OC_VAR_U32 | OC_FILE_U16,
75 /**
76 * Dereference the pointer once before writing to it,
77 * so we do not have to use big static arrays.
79 OC_DEREFERENCE_POINTER = 1 << 31,
81 OC_END = 0, ///< End of the whole chunk, all 32 bits set to zero
84 DECLARE_ENUM_AS_BIT_SET(OldChunkType)
86 typedef bool OldChunkProc(LoadgameState *ls, int num);
88 struct OldChunks {
89 OldChunkType type; ///< Type of field
90 uint32 amount; ///< Amount of fields
92 void *ptr; ///< Pointer where to save the data (may only be set if offset is 0)
93 uint offset; ///< Offset from basepointer (may only be set if ptr is NULL)
94 OldChunkProc *proc; ///< Pointer to function that is called with OC_CHUNK
97 /* If it fails, check lines above.. */
98 assert_compile(sizeof(TileIndex) == 4);
100 extern uint _bump_assert_value;
101 byte ReadByte(LoadgameState *ls);
102 bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks);
104 bool LoadTTDMain(LoadgameState *ls);
105 bool LoadTTOMain(LoadgameState *ls);
107 static inline uint16 ReadUint16(LoadgameState *ls)
109 byte x = ReadByte(ls);
110 return x | ReadByte(ls) << 8;
113 static inline uint32 ReadUint32(LoadgameState *ls)
115 uint16 x = ReadUint16(ls);
116 return x | ReadUint16(ls) << 16;
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 ;)
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)
137 #endif /* OLDLOADER_H */