saveload.h

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

Generated on Wed Dec 23 23:27:54 2009 for OpenTTD by  doxygen 1.5.6