# oldloader.h

Go to the documentation of this file.

    00001 /* $Id: oldloader.h 18809 2010-01-15 16:41:15Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef OLDLOADER_H
    00013 #define OLDLOADER_H
    00014
    00015 #include "saveload.h"
    00016 #include "../tile_type.h"
    00017
    00018 enum {
    00019   BUFFER_SIZE = 4096,
    00020   OLD_MAP_SIZE = 256 * 256,
    00021 };
    00022
    00023 struct LoadgameState {
    00024   FILE *file;
    00025
    00026   uint chunk_size;
    00027
    00028   bool decoding;
    00029   byte decode_char;
    00030
    00031   uint buffer_count;
    00032   uint buffer_cur;
    00033   byte buffer[BUFFER_SIZE];
    00034
    00035   uint total_read;
    00036   bool failed;
    00037 };
    00038
    00039 /* OldChunk-Type */
    00040 enum OldChunkType {
    00041   OC_SIMPLE    = 0,
    00042   OC_NULL      = 1,
    00043   OC_CHUNK     = 2,
    00044   OC_ASSERT    = 3,
    00045   /* 4 bits allocated (16 max) */
    00046
    00047   OC_TTD       = 1 << 4,
    00048   OC_TTO       = 1 << 5,
    00049   /* 4 bits allocated */
    00050
    00051   OC_VAR_I8    = 1 << 8,
    00052   OC_VAR_U8    = 2 << 8,
    00053   OC_VAR_I16   = 3 << 8,
    00054   OC_VAR_U16   = 4 << 8,
    00055   OC_VAR_I32   = 5 << 8,
    00056   OC_VAR_U32   = 6 << 8,
    00057   OC_VAR_I64   = 7 << 8,
    00058   OC_VAR_U64   = 8 << 8,
    00059   /* 8 bits allocated (256 max) */
    00060
    00061   OC_FILE_I8   = 1 << 16,
    00062   OC_FILE_U8   = 2 << 16,
    00063   OC_FILE_I16  = 3 << 16,
    00064   OC_FILE_U16  = 4 << 16,
    00065   OC_FILE_I32  = 5 << 16,
    00066   OC_FILE_U32  = 6 << 16,
    00067   /* 8 bits allocated (256 max) */
    00068
    00069   OC_INT8      = OC_VAR_I8   | OC_FILE_I8,
    00070   OC_UINT8     = OC_VAR_U8   | OC_FILE_U8,
    00071   OC_INT16     = OC_VAR_I16  | OC_FILE_I16,
    00072   OC_UINT16    = OC_VAR_U16  | OC_FILE_U16,
    00073   OC_INT32     = OC_VAR_I32  | OC_FILE_I32,
    00074   OC_UINT32    = OC_VAR_U32  | OC_FILE_U32,
    00075
    00076   OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
    00077
    00082   OC_DEREFERENCE_POINTER = 1 << 31,
    00083
    00084   OC_END       = 0
    00085 };
    00086
    00087 DECLARE_ENUM_AS_BIT_SET(OldChunkType);
    00088
    00089 typedef bool OldChunkProc(LoadgameState *ls, int num);
    00090
    00091 struct OldChunks {
    00092   OldChunkType type;
    00093   uint32 amount;
    00094
    00095   void *ptr;
    00096   uint offset;
    00097   OldChunkProc *proc;
    00098 };
    00099
    00100 /* If it fails, check lines above.. */
    00101 assert_compile(sizeof(TileIndex) == 4);
    00102
    00103 extern uint _bump_assert_value;
    00104 byte ReadByte(LoadgameState *ls);
    00105 bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks);
    00106
    00107 bool LoadTTDMain(LoadgameState *ls);
    00108 bool LoadTTOMain(LoadgameState *ls);
    00109
    00110 static inline uint16 ReadUint16(LoadgameState *ls)
    00111 {
    00112   byte x = ReadByte(ls);
    00113   return x | ReadByte(ls) << 8;
    00114 }
    00115
    00116 static inline uint32 ReadUint32(LoadgameState *ls)
    00117 {
    00118   uint16 x = ReadUint16(ls);
    00119   return x | ReadUint16(ls) << 16;
    00120 }
    00121
    00122 /* Help:
    00123  *  - OCL_SVAR: load 'type' to offset 'offset' in a struct of type 'base', which must also
    00124  *       be given via base in LoadChunk() as real pointer
    00125  *  - OCL_VAR: load 'type' to a global var
    00126  *  - OCL_END: every struct must end with this
    00127  *  - OCL_NULL: read 'amount' of bytes and send them to /dev/null or something
    00128  *  - OCL_CHUNK: load another proc to load a part of the savegame, 'amount' times
    00129  *  - OCL_ASSERT: to check if we are really at the place we expect to be.. because old savegames are too binary to be sure ;)
    00130  */
    00131 #define OCL_SVAR(type, base, offset)         { type,                 1,    NULL, (uint)cpp_offsetof(base, offset), NULL }
    00132 #define OCL_VAR(type, amount, pointer)       { type,            amount, pointer,    0,                             NULL }
    00133 #define OCL_END()                            { OC_END,               0,    NULL,    0,                             NULL }
    00134 #define OCL_CNULL(type, amount)              { OC_NULL | type,  amount,    NULL,    0,                             NULL }
    00135 #define OCL_CCHUNK(type, amount, proc)       { OC_CHUNK | type, amount,    NULL,    0,                             proc }
    00136 #define OCL_ASSERT(type, size)               { OC_ASSERT | type,     1,    NULL, size,                             NULL }
    00137 #define OCL_NULL(amount)        OCL_CNULL((OldChunkType)0, amount)
    00138 #define OCL_CHUNK(amount, proc) OCL_CCHUNK((OldChunkType)0, amount, proc)
    00139
    00140 #endif /* OLDLOADER_H */