saveload.h

Go to the documentation of this file.
00001 /* $Id: saveload.h 11822 2008-01-12 19:58:06Z peter1138 $ */
00002 
00005 #ifndef SAVELOAD_H
00006 #define SAVELOAD_H
00007 
00008 #include "fileio.h"
00009 
00010 #ifdef SIZE_MAX
00011 #undef SIZE_MAX
00012 #endif
00013 
00014 #define SIZE_MAX ((size_t)-1)
00015 
00016 enum SaveOrLoadResult {
00017   SL_OK     = 0, 
00018   SL_ERROR  = 1, 
00019   SL_REINIT = 2, 
00020 };
00021 
00022 enum SaveOrLoadMode {
00023   SL_INVALID  = -1,
00024   SL_LOAD     =  0,
00025   SL_SAVE     =  1,
00026   SL_OLD_LOAD =  2,
00027   SL_PNG      =  3,
00028   SL_BMP      =  4,
00029 };
00030 
00031 void SetSaveLoadError(uint16 str);
00032 const char *GetSaveLoadErrorString();
00033 SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb);
00034 void WaitTillSaved();
00035 void DoExitSave();
00036 
00037 
00038 typedef void ChunkSaveLoadProc();
00039 typedef void AutolengthProc(void *arg);
00040 
00041 struct ChunkHandler {
00042   uint32 id;
00043   ChunkSaveLoadProc *save_proc;
00044   ChunkSaveLoadProc *load_proc;
00045   uint32 flags;
00046 };
00047 
00048 struct NullStruct {
00049   byte null;
00050 };
00051 
00052 enum SLRefType {
00053   REF_ORDER         = 0,
00054   REF_VEHICLE       = 1,
00055   REF_STATION       = 2,
00056   REF_TOWN          = 3,
00057   REF_VEHICLE_OLD   = 4,
00058   REF_ROADSTOPS     = 5,
00059   REF_ENGINE_RENEWS = 6,
00060   REF_CARGO_PACKET  = 7,
00061 };
00062 
00063 #define SL_MAX_VERSION 255
00064 
00065 enum {
00066   INC_VEHICLE_COMMON = 0,
00067 };
00068 
00069 enum {
00070   CH_RIFF         =  0,
00071   CH_ARRAY        =  1,
00072   CH_SPARSE_ARRAY =  2,
00073   CH_TYPE_MASK    =  3,
00074   CH_LAST         =  8,
00075   CH_AUTO_LENGTH  = 16,
00076 
00077   CH_PRI_0          = 0 << 4,
00078   CH_PRI_1          = 1 << 4,
00079   CH_PRI_2          = 2 << 4,
00080   CH_PRI_3          = 3 << 4,
00081   CH_PRI_SHL        = 4,
00082   CH_NUM_PRI_LEVELS = 4,
00083 };
00084 
00091 enum VarTypes {
00092   /* 4 bytes allocated a maximum of 16 types for NumberType */
00093   SLE_FILE_I8       = 0,
00094   SLE_FILE_U8       = 1,
00095   SLE_FILE_I16      = 2,
00096   SLE_FILE_U16      = 3,
00097   SLE_FILE_I32      = 4,
00098   SLE_FILE_U32      = 5,
00099   SLE_FILE_I64      = 6,
00100   SLE_FILE_U64      = 7,
00101   SLE_FILE_STRINGID = 8, 
00102   SLE_FILE_STRING   = 9,
00103   /* 6 more possible file-primitives */
00104 
00105   /* 4 bytes allocated a maximum of 16 types for NumberType */
00106   SLE_VAR_BL    =  0 << 4,
00107   SLE_VAR_I8    =  1 << 4,
00108   SLE_VAR_U8    =  2 << 4,
00109   SLE_VAR_I16   =  3 << 4,
00110   SLE_VAR_U16   =  4 << 4,
00111   SLE_VAR_I32   =  5 << 4,
00112   SLE_VAR_U32   =  6 << 4,
00113   SLE_VAR_I64   =  7 << 4,
00114   SLE_VAR_U64   =  8 << 4,
00115   SLE_VAR_NULL  =  9 << 4, 
00116   SLE_VAR_STRB  = 10 << 4, 
00117   SLE_VAR_STRBQ = 11 << 4, 
00118   SLE_VAR_STR   = 12 << 4, 
00119   SLE_VAR_STRQ  = 13 << 4, 
00120   SLE_VAR_NAME  = 14 << 4, 
00121   /* 2 more possible memory-primitives */
00122 
00123   /* Shortcut values */
00124   SLE_VAR_CHAR = SLE_VAR_I8,
00125 
00126   /* Default combinations of variables. As savegames change, so can variables
00127    * and thus it is possible that the saved value and internal size do not
00128    * match and you need to specify custom combo. The defaults are listed here */
00129   SLE_BOOL         = SLE_FILE_I8  | SLE_VAR_BL,
00130   SLE_INT8         = SLE_FILE_I8  | SLE_VAR_I8,
00131   SLE_UINT8        = SLE_FILE_U8  | SLE_VAR_U8,
00132   SLE_INT16        = SLE_FILE_I16 | SLE_VAR_I16,
00133   SLE_UINT16       = SLE_FILE_U16 | SLE_VAR_U16,
00134   SLE_INT32        = SLE_FILE_I32 | SLE_VAR_I32,
00135   SLE_UINT32       = SLE_FILE_U32 | SLE_VAR_U32,
00136   SLE_INT64        = SLE_FILE_I64 | SLE_VAR_I64,
00137   SLE_UINT64       = SLE_FILE_U64 | SLE_VAR_U64,
00138   SLE_CHAR         = SLE_FILE_I8  | SLE_VAR_CHAR,
00139   SLE_STRINGID     = SLE_FILE_STRINGID | SLE_VAR_U16,
00140   SLE_STRINGBUF    = SLE_FILE_STRING   | SLE_VAR_STRB,
00141   SLE_STRINGBQUOTE = SLE_FILE_STRING   | SLE_VAR_STRBQ,
00142   SLE_STRING       = SLE_FILE_STRING   | SLE_VAR_STR,
00143   SLE_STRINGQUOTE  = SLE_FILE_STRING   | SLE_VAR_STRQ,
00144   SLE_NAME         = SLE_FILE_STRINGID | SLE_VAR_NAME,
00145 
00146   /* Shortcut values */
00147   SLE_UINT  = SLE_UINT32,
00148   SLE_INT   = SLE_INT32,
00149   SLE_STRB  = SLE_STRINGBUF,
00150   SLE_STRBQ = SLE_STRINGBQUOTE,
00151   SLE_STR   = SLE_STRING,
00152   SLE_STRQ  = SLE_STRINGQUOTE,
00153 
00154   /* 8 bytes allocated for a maximum of 8 flags
00155    * Flags directing saving/loading of a variable */
00156   SLF_SAVE_NO      = 1 <<  8, 
00157   SLF_CONFIG_NO    = 1 <<  9, 
00158   SLF_NETWORK_NO   = 1 << 10, 
00159   /* 5 more possible flags */
00160 };
00161 
00162 typedef uint32 VarType;
00163 
00164 enum SaveLoadTypes {
00165   SL_VAR         =  0,
00166   SL_REF         =  1,
00167   SL_ARR         =  2,
00168   SL_STR         =  3,
00169   SL_LST         =  4,
00170   // non-normal save-load types
00171   SL_WRITEBYTE   =  8,
00172   SL_VEH_INCLUDE =  9,
00173   SL_END         = 15
00174 };
00175 
00176 typedef byte SaveLoadType;
00177 
00179 struct SaveLoad {
00180   bool global;         
00181   SaveLoadType cmd;    
00182   VarType conv;        
00183   uint16 length;       
00184   uint16 version_from; 
00185   uint16 version_to;   
00186   /* NOTE: This element either denotes the address of the variable for a global
00187    * variable, or the offset within a struct which is then bound to a variable
00188    * during runtime. Decision on which one to use is controlled by the function
00189    * that is called to save it. address: global=true, offset: global=false */
00190   void *address;       
00191 };
00192 
00193 /* Same as SaveLoad but global variables are used (for better readability); */
00194 typedef SaveLoad SaveLoadGlobVarList;
00195 
00196 /* Simple variables, references (pointers) and arrays */
00197 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}
00198 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
00199 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
00200 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
00201 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
00202 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
00203 
00204 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
00205 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
00206 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
00207 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
00208 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
00209 
00210 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to)
00211 
00212 /* Translate values ingame to different values in the savegame and vv */
00213 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
00214 
00215 /* The same as the ones at the top, only the offset is given directly; used for unions */
00216 #define SLE_GENERALX(cmd, offset, type, length, param1, param2) {false, cmd, type, length, param1, param2, (void*)(offset)}
00217 #define SLE_CONDVARX(offset, type, from, to) SLE_GENERALX(SL_VAR, offset, type, 0, from, to)
00218 #define SLE_CONDARRX(offset, type, length, from, to) SLE_GENERALX(SL_ARR, offset, type, length, from, to)
00219 #define SLE_CONDREFX(offset, type, from, to) SLE_GENERALX(SL_REF, offset, type, 0, from, to)
00220 
00221 #define SLE_VARX(offset, type) SLE_CONDVARX(offset, type, 0, SL_MAX_VERSION)
00222 #define SLE_REFX(offset, type) SLE_CONDREFX(offset, type, 0, SL_MAX_VERSION)
00223 
00224 #define SLE_WRITEBYTEX(offset, something) SLE_GENERALX(SL_WRITEBYTE, offset, 0, 0, something, 0)
00225 #define SLE_VEH_INCLUDEX() SLE_GENERALX(SL_VEH_INCLUDE, 0, 0, 0, 0, SL_MAX_VERSION)
00226 
00227 /* End marker */
00228 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL}
00229 
00230 /* Simple variables, references (pointers) and arrays, but for global variables */
00231 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable}
00232 
00233 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
00234 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
00235 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
00236 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
00237 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
00238 
00239 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
00240 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
00241 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
00242 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
00243 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
00244 
00245 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to, (void*)NULL}
00246 
00247 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL}
00248 
00251 static inline bool CheckSavegameVersionOldStyle(uint16 major, byte minor)
00252 {
00253   extern uint16 _sl_version;
00254   extern byte   _sl_minor_version;
00255   return (_sl_version < major) || (_sl_version == major && _sl_minor_version < minor);
00256 }
00257 
00260 static inline bool CheckSavegameVersion(uint16 version)
00261 {
00262   extern uint16 _sl_version;
00263   return _sl_version < version;
00264 }
00265 
00268 static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to)
00269 {
00270   extern const uint16 SAVEGAME_VERSION;
00271   if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false;
00272 
00273   return true;
00274 }
00275 
00276 /* Get the NumberType of a setting. This describes the integer type
00277  * as it is represented in memory
00278  * @param type VarType holding information about the variable-type
00279  * @return return the SLE_VAR_* part of a variable-type description */
00280 static inline VarType GetVarMemType(VarType type)
00281 {
00282   return type & 0xF0; // GB(type, 4, 4) << 4;
00283 }
00284 
00285 /* Get the FileType of a setting. This describes the integer type
00286  * as it is represented in a savegame/file
00287  * @param type VarType holding information about the variable-type
00288  * @param return the SLE_FILE_* part of a variable-type description */
00289 static inline VarType GetVarFileType(VarType type)
00290 {
00291   return type & 0xF; // GB(type, 0, 4);
00292 }
00293 
00298 static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
00299 {
00300   return (byte*)object + (ptrdiff_t)sld->address;
00301 }
00302 
00303 int64 ReadValue(const void *ptr, VarType conv);
00304 void WriteValue(void *ptr, VarType conv, int64 val);
00305 
00306 void SlSetArrayIndex(uint index);
00307 int SlIterateArray();
00308 
00309 void SlAutolength(AutolengthProc *proc, void *arg);
00310 uint SlGetFieldLength();
00311 void SlSetLength(size_t length);
00312 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld);
00313 
00314 byte SlReadByte();
00315 void SlWriteByte(byte b);
00316 
00317 void SlGlobList(const SaveLoadGlobVarList *sldg);
00318 void SlArray(void *array, uint length, VarType conv);
00319 void SlObject(void *object, const SaveLoad *sld);
00320 bool SlObjectMember(void *object, const SaveLoad *sld);
00321 
00322 void SaveFileStart();
00323 void SaveFileDone();
00324 void SaveFileError();
00325 #endif /* SAVELOAD_H */

Generated on Wed Oct 1 17:03:23 2008 for openttd by  doxygen 1.5.6