saveload.h

Go to the documentation of this file.
00001 /* $Id$ */
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 #include "../strings_type.h"
00017 
00018 #ifdef SIZE_MAX
00019 #undef SIZE_MAX
00020 #endif
00021 
00022 #define SIZE_MAX ((size_t)-1)
00023 
00025 enum SaveOrLoadResult {
00026   SL_OK     = 0, 
00027   SL_ERROR  = 1, 
00028   SL_REINIT = 2, 
00029 };
00030 
00032 enum SaveOrLoadMode {
00033   SL_INVALID    = -1, 
00034   SL_LOAD       =  0, 
00035   SL_SAVE       =  1, 
00036   SL_OLD_LOAD   =  2, 
00037   SL_PNG        =  3, 
00038   SL_BMP        =  4, 
00039   SL_LOAD_CHECK =  5, 
00040 };
00041 
00043 enum SavegameType {
00044   SGT_TTD,    
00045   SGT_TTDP1,  
00046   SGT_TTDP2,  
00047   SGT_OTTD,   
00048   SGT_TTO,    
00049   SGT_INVALID = 0xFF 
00050 };
00051 
00052 void GenerateDefaultSaveName(char *buf, const char *last);
00053 void SetSaveLoadError(uint16 str);
00054 const char *GetSaveLoadErrorString();
00055 SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded = true);
00056 void WaitTillSaved();
00057 void ProcessAsyncSaveFinish();
00058 void DoExitSave();
00059 
00060 SaveOrLoadResult SaveWithFilter(struct SaveFilter *writer, bool threaded);
00061 SaveOrLoadResult LoadWithFilter(struct LoadFilter *reader);
00062 
00063 typedef void ChunkSaveLoadProc();
00064 typedef void AutolengthProc(void *arg);
00065 
00067 struct ChunkHandler {
00068   uint32 id;                          
00069   ChunkSaveLoadProc *save_proc;       
00070   ChunkSaveLoadProc *load_proc;       
00071   ChunkSaveLoadProc *ptrs_proc;       
00072   ChunkSaveLoadProc *load_check_proc; 
00073   uint32 flags;                       
00074 };
00075 
00076 struct NullStruct {
00077   byte null;
00078 };
00079 
00081 enum SLRefType {
00082   REF_ORDER         = 0, 
00083   REF_VEHICLE       = 1, 
00084   REF_STATION       = 2, 
00085   REF_TOWN          = 3, 
00086   REF_VEHICLE_OLD   = 4, 
00087   REF_ROADSTOPS     = 5, 
00088   REF_ENGINE_RENEWS = 6, 
00089   REF_CARGO_PACKET  = 7, 
00090   REF_ORDERLIST     = 8, 
00091   REF_ROUTE_LINK    = 9, 
00092 };
00093 
00095 #define SL_MAX_VERSION 255
00096 
00098 enum ChunkType {
00099   CH_RIFF         =  0,
00100   CH_ARRAY        =  1,
00101   CH_SPARSE_ARRAY =  2,
00102   CH_TYPE_MASK    =  3,
00103   CH_LAST         =  8, 
00104   CH_AUTO_LENGTH  = 16,
00105 };
00106 
00115 enum VarTypes {
00116   /* 4 bits allocated a maximum of 16 types for NumberType */
00117   SLE_FILE_I8       = 0,
00118   SLE_FILE_U8       = 1,
00119   SLE_FILE_I16      = 2,
00120   SLE_FILE_U16      = 3,
00121   SLE_FILE_I32      = 4,
00122   SLE_FILE_U32      = 5,
00123   SLE_FILE_I64      = 6,
00124   SLE_FILE_U64      = 7,
00125   SLE_FILE_STRINGID = 8, 
00126   SLE_FILE_STRING   = 9,
00127   /* 6 more possible file-primitives */
00128 
00129   /* 4 bits allocated a maximum of 16 types for NumberType */
00130   SLE_VAR_BL    =  0 << 4,
00131   SLE_VAR_I8    =  1 << 4,
00132   SLE_VAR_U8    =  2 << 4,
00133   SLE_VAR_I16   =  3 << 4,
00134   SLE_VAR_U16   =  4 << 4,
00135   SLE_VAR_I32   =  5 << 4,
00136   SLE_VAR_U32   =  6 << 4,
00137   SLE_VAR_I64   =  7 << 4,
00138   SLE_VAR_U64   =  8 << 4,
00139   SLE_VAR_NULL  =  9 << 4, 
00140   SLE_VAR_STRB  = 10 << 4, 
00141   SLE_VAR_STRBQ = 11 << 4, 
00142   SLE_VAR_STR   = 12 << 4, 
00143   SLE_VAR_STRQ  = 13 << 4, 
00144   SLE_VAR_NAME  = 14 << 4, 
00145   /* 1 more possible memory-primitives */
00146 
00147   /* Shortcut values */
00148   SLE_VAR_CHAR = SLE_VAR_I8,
00149 
00150   /* Default combinations of variables. As savegames change, so can variables
00151    * and thus it is possible that the saved value and internal size do not
00152    * match and you need to specify custom combo. The defaults are listed here */
00153   SLE_BOOL         = SLE_FILE_I8  | SLE_VAR_BL,
00154   SLE_INT8         = SLE_FILE_I8  | SLE_VAR_I8,
00155   SLE_UINT8        = SLE_FILE_U8  | SLE_VAR_U8,
00156   SLE_INT16        = SLE_FILE_I16 | SLE_VAR_I16,
00157   SLE_UINT16       = SLE_FILE_U16 | SLE_VAR_U16,
00158   SLE_INT32        = SLE_FILE_I32 | SLE_VAR_I32,
00159   SLE_UINT32       = SLE_FILE_U32 | SLE_VAR_U32,
00160   SLE_INT64        = SLE_FILE_I64 | SLE_VAR_I64,
00161   SLE_UINT64       = SLE_FILE_U64 | SLE_VAR_U64,
00162   SLE_CHAR         = SLE_FILE_I8  | SLE_VAR_CHAR,
00163   SLE_STRINGID     = SLE_FILE_STRINGID | SLE_VAR_U16,
00164   SLE_STRINGBUF    = SLE_FILE_STRING   | SLE_VAR_STRB,
00165   SLE_STRINGBQUOTE = SLE_FILE_STRING   | SLE_VAR_STRBQ,
00166   SLE_STRING       = SLE_FILE_STRING   | SLE_VAR_STR,
00167   SLE_STRINGQUOTE  = SLE_FILE_STRING   | SLE_VAR_STRQ,
00168   SLE_NAME         = SLE_FILE_STRINGID | SLE_VAR_NAME,
00169 
00170   /* Shortcut values */
00171   SLE_UINT  = SLE_UINT32,
00172   SLE_INT   = SLE_INT32,
00173   SLE_STRB  = SLE_STRINGBUF,
00174   SLE_STRBQ = SLE_STRINGBQUOTE,
00175   SLE_STR   = SLE_STRING,
00176   SLE_STRQ  = SLE_STRINGQUOTE,
00177 
00178   /* 8 bits allocated for a maximum of 8 flags
00179    * Flags directing saving/loading of a variable */
00180   SLF_NOT_IN_SAVE     = 1 <<  8, 
00181   SLF_NOT_IN_CONFIG   = 1 <<  9, 
00182   SLF_NO_NETWORK_SYNC = 1 << 10, 
00183   /* 5 more possible flags */
00184 };
00185 
00186 typedef uint32 VarType;
00187 
00189 enum SaveLoadTypes {
00190   SL_VAR         =  0, 
00191   SL_REF         =  1, 
00192   SL_ARR         =  2, 
00193   SL_STR         =  3, 
00194   SL_LST         =  4, 
00195   /* non-normal save-load types */
00196   SL_WRITEBYTE   =  8,
00197   SL_VEH_INCLUDE =  9,
00198   SL_ST_INCLUDE  = 10,
00199   SL_END         = 15
00200 };
00201 
00202 typedef byte SaveLoadType; 
00203 
00205 struct SaveLoad {
00206   bool global;         
00207   SaveLoadType cmd;    
00208   VarType conv;        
00209   uint16 length;       
00210   uint16 version_from; 
00211   uint16 version_to;   
00212   /* NOTE: This element either denotes the address of the variable for a global
00213    * variable, or the offset within a struct which is then bound to a variable
00214    * during runtime. Decision on which one to use is controlled by the function
00215    * that is called to save it. address: global=true, offset: global=false */
00216   void *address;       
00217 };
00218 
00220 typedef SaveLoad SaveLoadGlobVarList;
00221 
00232 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}
00233 
00242 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
00243 
00252 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
00253 
00263 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
00264 
00274 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
00275 
00284 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
00285 
00292 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
00293 
00300 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
00301 
00309 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
00310 
00318 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
00319 
00326 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
00327 
00332 #define SLE_NULL(length) SLE_CONDNULL(length, 0, SL_MAX_VERSION)
00333 
00340 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)
00341 
00343 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
00344 
00345 #define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
00346 #define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
00347 
00349 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL}
00350 
00360 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable}
00361 
00369 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
00370 
00378 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
00379 
00388 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
00389 
00398 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
00399 
00407 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
00408 
00414 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
00415 
00421 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
00422 
00428 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
00429 
00435 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
00436 
00442 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
00443 
00450 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}
00451 
00453 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL}
00454 
00461 static inline bool IsSavegameVersionBefore(uint16 major, byte minor = 0)
00462 {
00463   extern uint16 _sl_version;
00464   extern byte   _sl_minor_version;
00465   return _sl_version < major || (minor > 0 && _sl_version == major && _sl_minor_version < minor);
00466 }
00467 
00475 static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to)
00476 {
00477   extern const uint16 SAVEGAME_VERSION;
00478   if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false;
00479 
00480   return true;
00481 }
00482 
00489 static inline VarType GetVarMemType(VarType type)
00490 {
00491   return type & 0xF0; // GB(type, 4, 4) << 4;
00492 }
00493 
00500 static inline VarType GetVarFileType(VarType type)
00501 {
00502   return type & 0xF; // GB(type, 0, 4);
00503 }
00504 
00510 static inline bool IsNumericType(VarType conv)
00511 {
00512   return GetVarMemType(conv) <= SLE_VAR_U64;
00513 }
00514 
00521 static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
00522 {
00523   return (byte*)(sld->global ? NULL : object) + (ptrdiff_t)sld->address;
00524 }
00525 
00526 int64 ReadValue(const void *ptr, VarType conv);
00527 void WriteValue(void *ptr, VarType conv, int64 val);
00528 
00529 void SlSetArrayIndex(uint index);
00530 int SlIterateArray();
00531 
00532 void SlAutolength(AutolengthProc *proc, void *arg);
00533 size_t SlGetFieldLength();
00534 void SlSetLength(size_t length);
00535 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld);
00536 size_t SlCalcObjLength(const void *object, const SaveLoad *sld);
00537 
00538 byte SlReadByte();
00539 void SlWriteByte(byte b);
00540 
00541 void SlGlobList(const SaveLoadGlobVarList *sldg);
00542 void SlArray(void *array, size_t length, VarType conv);
00543 void SlObject(void *object, const SaveLoad *sld);
00544 bool SlObjectMember(void *object, const SaveLoad *sld);
00545 void NORETURN SlError(StringID string, const char *extra_msg = NULL);
00546 void NORETURN SlErrorCorrupt(const char *msg);
00547 
00548 bool SaveloadCrashWithMissingNewGRFs();
00549 
00550 extern char _savegame_format[8];
00551 extern bool _do_autosave;
00552 
00553 #endif /* SAVELOAD_H */

Generated on Mon May 9 05:18:59 2011 for OpenTTD by  doxygen 1.6.1