oldloader.h

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

Generated on Tue Jul 21 18:48:26 2009 for OpenTTD by  doxygen 1.5.6