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 };
00092 
00096 enum SaveLoadVersions {
00097   BEFORE_ADDING_PATCHES_SL            = 185,
00098   BACK_ONE_WAY_SIGNAL_SAFE_WAITING_SL = 186, // Back of one way signals is evil-deadlock.
00099   TREE_GROWTH_RATE_SL                 = 188, // 
00100   PROG_SIGS_SL                        = 189, // If Then Else signals ...
00101   DAYLENGHT_SL                        = 190, // Daylenght
00102   TOWN_CARGO_FACTOR_SL                = 191, // Generated town cargo amount
00103   SL_RESERVATION                      = 192, // C
00104   SL_CAPACITIES                       = 193, // A
00105   SL_COMPONENTS                       = 194, // R
00106   SL_DEMANDS                          = 195, // G
00107   SL_MCF                              = 196, // O
00108   SL_FLOWMAP                          = 197, // D
00109   SL_CARGOMAP                         = 198, // I
00110   SL_EXT_RATING                       = 199, // S (for T @see Keep compatible 8 :P )
00111   TIMESEP_SV                          = 200, // Vehicles seperation
00112   MORE_HEIGHTLEVEL_SAVEGAME_VERSION   = 201, // High high higher ...
00113   KEEP_COMPATIBLE_1_SV                = 202, // 147 (with 147 no new chunks are introduced so no need to do the redeclaration trick)
00114   KEEP_COMPATIBLE_2_SV                = 203, // 148
00115   KEEP_COMPATIBLE_3_SV                = 204, // 148
00116   KEEP_COMPATIBLE_4_SV                = 205, // 149
00117   KEEP_COMPATIBLE_5_SV                = 206, // 150
00118   KEEP_COMPATIBLE_6_SV                = 206, // MORE_HEIGHTLEVEL_SAVEGAME_VERSION(disaster-vehicle_sl)
00119   KEEP_COMPATIBLE_7_SV                = 206, // 151
00120   KEEP_COMPATIBLE_8_SV                = 207, // CargoDist SL_CAPACITIES(last_loading_station)
00121   INGAME_TREELINE_SV                  = 208, // Fix treeline for old games (and have it configurable ingame at the same time ;) )
00122   KEEP_COMPATIBLE_9_SV                = 209, // 151 (vehicle_sl-> CONDNULL)
00123   KEEP_COMPATIBLE_10_SV               = 210, // 152
00124   KEEP_COMPATIBLE_11_SV               = 210, // 153
00125   IS_SL                               = 211, // Infrastructure sharing
00126   EXCLUSIVE_DL_SL                     = 212, // Adjust exclusive counter to daylenght in town_sl.cpp
00127   KEEP_COMPATIBLE_12_SV               = 213, // 154
00128   KEEP_COMPATIBLE_13_SV               = 214, // 155
00129   KEEP_COMPATIBLE_14_SV               = 215, // 156
00130   KEEP_COMPATIBLE_15_SV               = 216, // 157
00131   KEEP_COMPATIBLE_16_SV               = 217, // 158
00132   KEEP_COMPATIBLE_17_SV               = 218, // 159
00133   KEEP_COMPATIBLE_18_SV               = 219, // 160
00134   KEEP_COMPATIBLE_19_SV               = 220, // 160
00135   KEEP_COMPATIBLE_20_SV               = 221, // Save ticks per minute (departure boards)
00136   SL_IB                               = 222, // Improved breakdowns
00137   TL_SV                               = 223, // Trafficlights
00138   TOWN_GROWTH_SV                      = 224, // Town growth needs goods
00139   DESERT_SV                           = 225, // Amount of desert selectable (not sure if this needs to be saved but it does not hurt ...)
00140   KEEP_COMPATIBLE_21_SV               = 226, // TOWN_GROWTH_SV -> saving larger towns values
00141   SPEED_SIGNALS_SV                    = 227, // Speed signals (based on progsigs patch)
00142   KEEP_COMPATIBLE_22_SV               = 228, // No linkgraph overloading (extension to CargoDist).
00143   KEEP_COMPATIBLE_23_SV               = 229, // Advanced setting for industrie placement (More Height Levels)
00144   KEEP_COMPATIBLE_24_SV               = 230, // TL_SV -> Advanced setting for selecting distance between trafficlights in a syncronised consist.
00145   TREEDRAG_SV                         = 231, // Configurable drag limit for trees.
00146 
00148   SL_MAX_VERSION                      = 255
00149 };
00150 
00152 enum ChunkType {
00153   CH_RIFF         =  0,
00154   CH_ARRAY        =  1,
00155   CH_SPARSE_ARRAY =  2,
00156   CH_TYPE_MASK    =  3,
00157   CH_LAST         =  8, 
00158   CH_AUTO_LENGTH  = 16,
00159 };
00160 
00169 enum VarTypes {
00170   /* 4 bits allocated a maximum of 16 types for NumberType */
00171   SLE_FILE_I8       = 0,
00172   SLE_FILE_U8       = 1,
00173   SLE_FILE_I16      = 2,
00174   SLE_FILE_U16      = 3,
00175   SLE_FILE_I32      = 4,
00176   SLE_FILE_U32      = 5,
00177   SLE_FILE_I64      = 6,
00178   SLE_FILE_U64      = 7,
00179   SLE_FILE_STRINGID = 8, 
00180   SLE_FILE_STRING   = 9,
00181   /* 6 more possible file-primitives */
00182 
00183   /* 4 bits allocated a maximum of 16 types for NumberType */
00184   SLE_VAR_BL    =  0 << 4,
00185   SLE_VAR_I8    =  1 << 4,
00186   SLE_VAR_U8    =  2 << 4,
00187   SLE_VAR_I16   =  3 << 4,
00188   SLE_VAR_U16   =  4 << 4,
00189   SLE_VAR_I32   =  5 << 4,
00190   SLE_VAR_U32   =  6 << 4,
00191   SLE_VAR_I64   =  7 << 4,
00192   SLE_VAR_U64   =  8 << 4,
00193   SLE_VAR_NULL  =  9 << 4, 
00194   SLE_VAR_STRB  = 10 << 4, 
00195   SLE_VAR_STRBQ = 11 << 4, 
00196   SLE_VAR_STR   = 12 << 4, 
00197   SLE_VAR_STRQ  = 13 << 4, 
00198   SLE_VAR_NAME  = 14 << 4, 
00199   /* 1 more possible memory-primitives */
00200 
00201   /* Shortcut values */
00202   SLE_VAR_CHAR = SLE_VAR_I8,
00203 
00204   /* Default combinations of variables. As savegames change, so can variables
00205    * and thus it is possible that the saved value and internal size do not
00206    * match and you need to specify custom combo. The defaults are listed here */
00207   SLE_BOOL         = SLE_FILE_I8  | SLE_VAR_BL,
00208   SLE_INT8         = SLE_FILE_I8  | SLE_VAR_I8,
00209   SLE_UINT8        = SLE_FILE_U8  | SLE_VAR_U8,
00210   SLE_INT16        = SLE_FILE_I16 | SLE_VAR_I16,
00211   SLE_UINT16       = SLE_FILE_U16 | SLE_VAR_U16,
00212   SLE_INT32        = SLE_FILE_I32 | SLE_VAR_I32,
00213   SLE_UINT32       = SLE_FILE_U32 | SLE_VAR_U32,
00214   SLE_INT64        = SLE_FILE_I64 | SLE_VAR_I64,
00215   SLE_UINT64       = SLE_FILE_U64 | SLE_VAR_U64,
00216   SLE_CHAR         = SLE_FILE_I8  | SLE_VAR_CHAR,
00217   SLE_STRINGID     = SLE_FILE_STRINGID | SLE_VAR_U16,
00218   SLE_STRINGBUF    = SLE_FILE_STRING   | SLE_VAR_STRB,
00219   SLE_STRINGBQUOTE = SLE_FILE_STRING   | SLE_VAR_STRBQ,
00220   SLE_STRING       = SLE_FILE_STRING   | SLE_VAR_STR,
00221   SLE_STRINGQUOTE  = SLE_FILE_STRING   | SLE_VAR_STRQ,
00222   SLE_NAME         = SLE_FILE_STRINGID | SLE_VAR_NAME,
00223 
00224   /* Shortcut values */
00225   SLE_UINT  = SLE_UINT32,
00226   SLE_INT   = SLE_INT32,
00227   SLE_STRB  = SLE_STRINGBUF,
00228   SLE_STRBQ = SLE_STRINGBQUOTE,
00229   SLE_STR   = SLE_STRING,
00230   SLE_STRQ  = SLE_STRINGQUOTE,
00231 
00232   /* 8 bits allocated for a maximum of 8 flags
00233    * Flags directing saving/loading of a variable */
00234   SLF_NOT_IN_SAVE     = 1 <<  8, 
00235   SLF_NOT_IN_CONFIG   = 1 <<  9, 
00236   SLF_NO_NETWORK_SYNC = 1 << 10, 
00237   /* 5 more possible flags */
00238 };
00239 
00240 typedef uint32 VarType;
00241 
00243 enum SaveLoadTypes {
00244   SL_VAR         =  0, 
00245   SL_REF         =  1, 
00246   SL_ARR         =  2, 
00247   SL_STR         =  3, 
00248   SL_LST         =  4, 
00249   /* non-normal save-load types */
00250   SL_WRITEBYTE   =  8,
00251   SL_VEH_INCLUDE =  9,
00252   SL_ST_INCLUDE  = 10,
00253   SL_END         = 15
00254 };
00255 
00256 typedef byte SaveLoadType; 
00257 
00259 struct SaveLoad {
00260   bool global;         
00261   SaveLoadType cmd;    
00262   VarType conv;        
00263   uint16 length;       
00264   uint16 version_from; 
00265   uint16 version_to;   
00266   /* NOTE: This element either denotes the address of the variable for a global
00267    * variable, or the offset within a struct which is then bound to a variable
00268    * during runtime. Decision on which one to use is controlled by the function
00269    * that is called to save it. address: global=true, offset: global=false */
00270   void *address;       
00271 };
00272 
00274 typedef SaveLoad SaveLoadGlobVarList;
00275 
00286 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}
00287 
00296 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
00297 
00306 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
00307 
00317 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
00318 
00328 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
00329 
00338 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
00339 
00346 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
00347 
00354 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
00355 
00363 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
00364 
00372 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
00373 
00380 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
00381 
00386 #define SLE_NULL(length) SLE_CONDNULL(length, 0, SL_MAX_VERSION)
00387 
00394 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)
00395 
00397 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
00398 
00399 #define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
00400 #define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
00401 
00403 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL}
00404 
00414 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable}
00415 
00423 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
00424 
00432 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
00433 
00442 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
00443 
00452 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
00453 
00461 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
00462 
00468 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
00469 
00475 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
00476 
00482 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
00483 
00489 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
00490 
00496 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
00497 
00504 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}
00505 
00507 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL}
00508 
00515 static inline bool IsSavegameVersionBefore(uint16 major, byte minor = 0)
00516 {
00517   extern uint16 _sl_version;
00518   extern byte   _sl_minor_version;
00519   return _sl_version < major || (minor > 0 && _sl_version == major && _sl_minor_version < minor);
00520 }
00521 
00529 static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to)
00530 {
00531   extern const uint16 SAVEGAME_VERSION;
00532   if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false;
00533 
00534   return true;
00535 }
00536 
00543 static inline VarType GetVarMemType(VarType type)
00544 {
00545   return type & 0xF0; // GB(type, 4, 4) << 4;
00546 }
00547 
00554 static inline VarType GetVarFileType(VarType type)
00555 {
00556   return type & 0xF; // GB(type, 0, 4);
00557 }
00558 
00564 static inline bool IsNumericType(VarType conv)
00565 {
00566   return GetVarMemType(conv) <= SLE_VAR_U64;
00567 }
00568 
00575 static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
00576 {
00577   return (byte*)(sld->global ? NULL : object) + (ptrdiff_t)sld->address;
00578 }
00579 
00580 int64 ReadValue(const void *ptr, VarType conv);
00581 void WriteValue(void *ptr, VarType conv, int64 val);
00582 
00583 void SlSetArrayIndex(uint index);
00584 int SlIterateArray();
00585 
00586 void SlAutolength(AutolengthProc *proc, void *arg);
00587 size_t SlGetFieldLength();
00588 void SlSetLength(size_t length);
00589 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld);
00590 size_t SlCalcObjLength(const void *object, const SaveLoad *sld);
00591 
00592 byte SlReadByte();
00593 void SlWriteByte(byte b);
00594 
00595 void SlGlobList(const SaveLoadGlobVarList *sldg);
00596 void SlArray(void *array, size_t length, VarType conv);
00597 void SlObject(void *object, const SaveLoad *sld);
00598 bool SlObjectMember(void *object, const SaveLoad *sld);
00599 void NORETURN SlError(StringID string, const char *extra_msg = NULL);
00600 void NORETURN SlErrorCorrupt(const char *msg);
00601 
00602 bool SaveloadCrashWithMissingNewGRFs();
00603 
00604 extern char _savegame_format[8];
00605 extern bool _do_autosave;
00606 
00607 #endif /* SAVELOAD_H */