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 
00019 enum SaveOrLoadResult {
00020   SL_OK     = 0, 
00021   SL_ERROR  = 1, 
00022   SL_REINIT = 2, 
00023 };
00024 
00026 enum SaveOrLoadMode {
00027   SL_INVALID    = -1, 
00028   SL_LOAD       =  0, 
00029   SL_SAVE       =  1, 
00030   SL_OLD_LOAD   =  2, 
00031   SL_PNG        =  3, 
00032   SL_BMP        =  4, 
00033   SL_LOAD_CHECK =  5, 
00034 };
00035 
00037 enum SavegameType {
00038   SGT_TTD,    
00039   SGT_TTDP1,  
00040   SGT_TTDP2,  
00041   SGT_OTTD,   
00042   SGT_TTO,    
00043   SGT_INVALID = 0xFF, 
00044 };
00045 
00046 void GenerateDefaultSaveName(char *buf, const char *last);
00047 void SetSaveLoadError(uint16 str);
00048 const char *GetSaveLoadErrorString();
00049 SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded = true);
00050 void WaitTillSaved();
00051 void ProcessAsyncSaveFinish();
00052 void DoExitSave();
00053 
00054 SaveOrLoadResult SaveWithFilter(struct SaveFilter *writer, bool threaded);
00055 SaveOrLoadResult LoadWithFilter(struct LoadFilter *reader);
00056 
00057 typedef void ChunkSaveLoadProc();
00058 typedef void AutolengthProc(void *arg);
00059 
00061 struct ChunkHandler {
00062   uint32 id;                          
00063   ChunkSaveLoadProc *save_proc;       
00064   ChunkSaveLoadProc *load_proc;       
00065   ChunkSaveLoadProc *ptrs_proc;       
00066   ChunkSaveLoadProc *load_check_proc; 
00067   uint32 flags;                       
00068 };
00069 
00070 struct NullStruct {
00071   byte null;
00072 };
00073 
00075 enum SLRefType {
00076   REF_ORDER         = 0, 
00077   REF_VEHICLE       = 1, 
00078   REF_STATION       = 2, 
00079   REF_TOWN          = 3, 
00080   REF_VEHICLE_OLD   = 4, 
00081   REF_ROADSTOPS     = 5, 
00082   REF_ENGINE_RENEWS = 6, 
00083   REF_CARGO_PACKET  = 7, 
00084   REF_ORDERLIST     = 8, 
00085   REF_STORAGE       = 9, 
00086 };
00087 
00089 enum ChunkType {
00090   CH_RIFF         =  0,
00091   CH_ARRAY        =  1,
00092   CH_SPARSE_ARRAY =  2,
00093   CH_TYPE_MASK    =  3,
00094   CH_LAST         =  8, 
00095   CH_AUTO_LENGTH  = 16,
00096 };
00097 
00106 enum VarTypes {
00107   /* 4 bits allocated a maximum of 16 types for NumberType */
00108   SLE_FILE_I8       = 0,
00109   SLE_FILE_U8       = 1,
00110   SLE_FILE_I16      = 2,
00111   SLE_FILE_U16      = 3,
00112   SLE_FILE_I32      = 4,
00113   SLE_FILE_U32      = 5,
00114   SLE_FILE_I64      = 6,
00115   SLE_FILE_U64      = 7,
00116   SLE_FILE_STRINGID = 8, 
00117   SLE_FILE_STRING   = 9,
00118   /* 6 more possible file-primitives */
00119 
00120   /* 4 bits allocated a maximum of 16 types for NumberType */
00121   SLE_VAR_BL    =  0 << 4,
00122   SLE_VAR_I8    =  1 << 4,
00123   SLE_VAR_U8    =  2 << 4,
00124   SLE_VAR_I16   =  3 << 4,
00125   SLE_VAR_U16   =  4 << 4,
00126   SLE_VAR_I32   =  5 << 4,
00127   SLE_VAR_U32   =  6 << 4,
00128   SLE_VAR_I64   =  7 << 4,
00129   SLE_VAR_U64   =  8 << 4,
00130   SLE_VAR_NULL  =  9 << 4, 
00131   SLE_VAR_STRB  = 10 << 4, 
00132   SLE_VAR_STRBQ = 11 << 4, 
00133   SLE_VAR_STR   = 12 << 4, 
00134   SLE_VAR_STRQ  = 13 << 4, 
00135   SLE_VAR_NAME  = 14 << 4, 
00136   /* 1 more possible memory-primitives */
00137 
00138   /* Shortcut values */
00139   SLE_VAR_CHAR = SLE_VAR_I8,
00140 
00141   /* Default combinations of variables. As savegames change, so can variables
00142    * and thus it is possible that the saved value and internal size do not
00143    * match and you need to specify custom combo. The defaults are listed here */
00144   SLE_BOOL         = SLE_FILE_I8  | SLE_VAR_BL,
00145   SLE_INT8         = SLE_FILE_I8  | SLE_VAR_I8,
00146   SLE_UINT8        = SLE_FILE_U8  | SLE_VAR_U8,
00147   SLE_INT16        = SLE_FILE_I16 | SLE_VAR_I16,
00148   SLE_UINT16       = SLE_FILE_U16 | SLE_VAR_U16,
00149   SLE_INT32        = SLE_FILE_I32 | SLE_VAR_I32,
00150   SLE_UINT32       = SLE_FILE_U32 | SLE_VAR_U32,
00151   SLE_INT64        = SLE_FILE_I64 | SLE_VAR_I64,
00152   SLE_UINT64       = SLE_FILE_U64 | SLE_VAR_U64,
00153   SLE_CHAR         = SLE_FILE_I8  | SLE_VAR_CHAR,
00154   SLE_STRINGID     = SLE_FILE_STRINGID | SLE_VAR_U16,
00155   SLE_STRINGBUF    = SLE_FILE_STRING   | SLE_VAR_STRB,
00156   SLE_STRINGBQUOTE = SLE_FILE_STRING   | SLE_VAR_STRBQ,
00157   SLE_STRING       = SLE_FILE_STRING   | SLE_VAR_STR,
00158   SLE_STRINGQUOTE  = SLE_FILE_STRING   | SLE_VAR_STRQ,
00159   SLE_NAME         = SLE_FILE_STRINGID | SLE_VAR_NAME,
00160 
00161   /* Shortcut values */
00162   SLE_UINT  = SLE_UINT32,
00163   SLE_INT   = SLE_INT32,
00164   SLE_STRB  = SLE_STRINGBUF,
00165   SLE_STRBQ = SLE_STRINGBQUOTE,
00166   SLE_STR   = SLE_STRING,
00167   SLE_STRQ  = SLE_STRINGQUOTE,
00168 
00169   /* 8 bits allocated for a maximum of 8 flags
00170    * Flags directing saving/loading of a variable */
00171   SLF_NOT_IN_SAVE     = 1 <<  8, 
00172   SLF_NOT_IN_CONFIG   = 1 <<  9, 
00173   SLF_NO_NETWORK_SYNC = 1 << 10, 
00174   SLF_ALLOW_CONTROL   = 1 << 11, 
00175   SLF_ALLOW_NEWLINE   = 1 << 12, 
00176   /* 3 more possible flags */
00177 };
00178 
00179 typedef uint32 VarType;
00180 
00182 enum SaveLoadTypes {
00183   SL_VAR         =  0, 
00184   SL_REF         =  1, 
00185   SL_ARR         =  2, 
00186   SL_STR         =  3, 
00187   SL_LST         =  4, 
00188   /* non-normal save-load types */
00189   SL_WRITEBYTE   =  8,
00190   SL_VEH_INCLUDE =  9,
00191   SL_ST_INCLUDE  = 10,
00192   SL_END         = 15
00193 };
00194 
00195 typedef byte SaveLoadType; 
00196 
00198 struct SaveLoad {
00199   bool global;         
00200   SaveLoadType cmd;    
00201   VarType conv;        
00202   uint16 length;       
00203   uint16 version_from; 
00204   uint16 version_to;   
00205   /* NOTE: This element either denotes the address of the variable for a global
00206    * variable, or the offset within a struct which is then bound to a variable
00207    * during runtime. Decision on which one to use is controlled by the function
00208    * that is called to save it. address: global=true, offset: global=false */
00209   void *address;       
00210 };
00211 
00213 typedef SaveLoad SaveLoadGlobVarList;
00214 
00225 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}
00226 
00235 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
00236 
00245 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
00246 
00256 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
00257 
00267 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
00268 
00277 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
00278 
00285 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
00286 
00293 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
00294 
00302 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
00303 
00311 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
00312 
00319 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
00320 
00325 #define SLE_NULL(length) SLE_CONDNULL(length, 0, SL_MAX_VERSION)
00326 
00333 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)
00334 
00336 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
00337 
00338 #define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
00339 #define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
00340 
00342 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL}
00343 
00353 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable}
00354 
00362 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
00363 
00371 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
00372 
00381 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
00382 
00391 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
00392 
00400 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
00401 
00407 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
00408 
00414 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
00415 
00421 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
00422 
00428 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
00429 
00435 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
00436 
00443 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}
00444 
00446 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL}
00447 
00454 static inline bool IsSavegameVersionBefore(uint16 major, byte minor = 0)
00455 {
00456   extern uint16 _sl_version;
00457   extern byte   _sl_minor_version;
00458   return _sl_version < major || (minor > 0 && _sl_version == major && _sl_minor_version < minor);
00459 }
00460 
00468 static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to)
00469 {
00470   extern const uint16 SAVEGAME_VERSION;
00471   if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false;
00472 
00473   return true;
00474 }
00475 
00482 static inline VarType GetVarMemType(VarType type)
00483 {
00484   return type & 0xF0; // GB(type, 4, 4) << 4;
00485 }
00486 
00493 static inline VarType GetVarFileType(VarType type)
00494 {
00495   return type & 0xF; // GB(type, 0, 4);
00496 }
00497 
00503 static inline bool IsNumericType(VarType conv)
00504 {
00505   return GetVarMemType(conv) <= SLE_VAR_U64;
00506 }
00507 
00514 static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
00515 {
00516   return const_cast<byte *>((const byte*)(sld->global ? NULL : object) + (ptrdiff_t)sld->address);
00517 }
00518 
00519 int64 ReadValue(const void *ptr, VarType conv);
00520 void WriteValue(void *ptr, VarType conv, int64 val);
00521 
00522 void SlSetArrayIndex(uint index);
00523 int SlIterateArray();
00524 
00525 void SlAutolength(AutolengthProc *proc, void *arg);
00526 size_t SlGetFieldLength();
00527 void SlSetLength(size_t length);
00528 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld);
00529 size_t SlCalcObjLength(const void *object, const SaveLoad *sld);
00530 
00531 byte SlReadByte();
00532 void SlWriteByte(byte b);
00533 
00534 void SlGlobList(const SaveLoadGlobVarList *sldg);
00535 void SlArray(void *array, size_t length, VarType conv);
00536 void SlObject(void *object, const SaveLoad *sld);
00537 bool SlObjectMember(void *object, const SaveLoad *sld);
00538 void NORETURN SlError(StringID string, const char *extra_msg = NULL);
00539 void NORETURN SlErrorCorrupt(const char *msg);
00540 
00541 bool SaveloadCrashWithMissingNewGRFs();
00542 
00543 extern char _savegame_format[8];
00544 extern bool _do_autosave;
00545 
00550 enum SaveLoadVersions {
00551   SL_TRUNK = 179,
00552   SL_CAPACITIES,
00553   SL_COMPONENTS,
00554   SL_DEMANDS,
00555   SL_MCF,
00556   SL_FLOWMAP,
00557   SL_RESERVATION,
00558   SL_CARGOMAP,
00559   SL_EXT_RATING,
00560 
00562   SL_MAX_VERSION = 255
00563 };
00564 
00565 #endif /* SAVELOAD_H */