# saveload.h

Go to the documentation of this file.

    00001 /* $Id: saveload.h 20872 2010-10-02 14:51:33Z 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
    00103 enum VarTypes {
    00104   /* 4 bits allocated a maximum of 16 types for NumberType */
    00105   SLE_FILE_I8       = 0,
    00106   SLE_FILE_U8       = 1,
    00107   SLE_FILE_I16      = 2,
    00108   SLE_FILE_U16      = 3,
    00109   SLE_FILE_I32      = 4,
    00110   SLE_FILE_U32      = 5,
    00111   SLE_FILE_I64      = 6,
    00112   SLE_FILE_U64      = 7,
    00113   SLE_FILE_STRINGID = 8,
    00114   SLE_FILE_STRING   = 9,
    00115   /* 6 more possible file-primitives */
    00116
    00117   /* 4 bits allocated a maximum of 16 types for NumberType */
    00118   SLE_VAR_BL    =  0 << 4,
    00119   SLE_VAR_I8    =  1 << 4,
    00120   SLE_VAR_U8    =  2 << 4,
    00121   SLE_VAR_I16   =  3 << 4,
    00122   SLE_VAR_U16   =  4 << 4,
    00123   SLE_VAR_I32   =  5 << 4,
    00124   SLE_VAR_U32   =  6 << 4,
    00125   SLE_VAR_I64   =  7 << 4,
    00126   SLE_VAR_U64   =  8 << 4,
    00127   SLE_VAR_NULL  =  9 << 4,
    00128   SLE_VAR_STRB  = 10 << 4,
    00129   SLE_VAR_STRBQ = 11 << 4,
    00130   SLE_VAR_STR   = 12 << 4,
    00131   SLE_VAR_STRQ  = 13 << 4,
    00132   SLE_VAR_NAME  = 14 << 4,
    00133   /* 1 more possible memory-primitives */
    00134
    00135   /* Shortcut values */
    00136   SLE_VAR_CHAR = SLE_VAR_I8,
    00137
    00138   /* Default combinations of variables. As savegames change, so can variables
    00139    * and thus it is possible that the saved value and internal size do not
    00140    * match and you need to specify custom combo. The defaults are listed here */
    00141   SLE_BOOL         = SLE_FILE_I8  | SLE_VAR_BL,
    00142   SLE_INT8         = SLE_FILE_I8  | SLE_VAR_I8,
    00143   SLE_UINT8        = SLE_FILE_U8  | SLE_VAR_U8,
    00144   SLE_INT16        = SLE_FILE_I16 | SLE_VAR_I16,
    00145   SLE_UINT16       = SLE_FILE_U16 | SLE_VAR_U16,
    00146   SLE_INT32        = SLE_FILE_I32 | SLE_VAR_I32,
    00147   SLE_UINT32       = SLE_FILE_U32 | SLE_VAR_U32,
    00148   SLE_INT64        = SLE_FILE_I64 | SLE_VAR_I64,
    00149   SLE_UINT64       = SLE_FILE_U64 | SLE_VAR_U64,
    00150   SLE_CHAR         = SLE_FILE_I8  | SLE_VAR_CHAR,
    00151   SLE_STRINGID     = SLE_FILE_STRINGID | SLE_VAR_U16,
    00152   SLE_STRINGBUF    = SLE_FILE_STRING   | SLE_VAR_STRB,
    00153   SLE_STRINGBQUOTE = SLE_FILE_STRING   | SLE_VAR_STRBQ,
    00154   SLE_STRING       = SLE_FILE_STRING   | SLE_VAR_STR,
    00155   SLE_STRINGQUOTE  = SLE_FILE_STRING   | SLE_VAR_STRQ,
    00156   SLE_NAME         = SLE_FILE_STRINGID | SLE_VAR_NAME,
    00157
    00158   /* Shortcut values */
    00159   SLE_UINT  = SLE_UINT32,
    00160   SLE_INT   = SLE_INT32,
    00161   SLE_STRB  = SLE_STRINGBUF,
    00162   SLE_STRBQ = SLE_STRINGBQUOTE,
    00163   SLE_STR   = SLE_STRING,
    00164   SLE_STRQ  = SLE_STRINGQUOTE,
    00165
    00166   /* 8 bits allocated for a maximum of 8 flags
    00167    * Flags directing saving/loading of a variable */
    00168   SLF_SAVE_NO      = 1 <<  8,
    00169   SLF_CONFIG_NO    = 1 <<  9,
    00170   SLF_NETWORK_NO   = 1 << 10,
    00171   /* 5 more possible flags */
    00172 };
    00173
    00174 typedef uint32 VarType;
    00175
    00176 enum SaveLoadTypes {
    00177   SL_VAR         =  0,
    00178   SL_REF         =  1,
    00179   SL_ARR         =  2,
    00180   SL_STR         =  3,
    00181   SL_LST         =  4,
    00182   /* non-normal save-load types */
    00183   SL_WRITEBYTE   =  8,
    00184   SL_VEH_INCLUDE =  9,
    00185   SL_ST_INCLUDE  = 10,
    00186   SL_END         = 15
    00187 };
    00188
    00189 typedef byte SaveLoadType;
    00190
    00192 struct SaveLoad {
    00193   bool global;
    00194   SaveLoadType cmd;
    00195   VarType conv;
    00196   uint16 length;
    00197   uint16 version_from;
    00198   uint16 version_to;
    00199   /* NOTE: This element either denotes the address of the variable for a global
    00200    * variable, or the offset within a struct which is then bound to a variable
    00201    * during runtime. Decision on which one to use is controlled by the function
    00202    * that is called to save it. address: global=true, offset: global=false */
    00203   void *address;
    00204 };
    00205
    00206 /* Same as SaveLoad but global variables are used (for better readability); */
    00207 typedef SaveLoad SaveLoadGlobVarList;
    00208
    00209 /* Simple variables, references (pointers) and arrays */
    00210 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}
    00211 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
    00212 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
    00213 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
    00214 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
    00215 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
    00216
    00217 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
    00218 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
    00219 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
    00220 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
    00221 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
    00222
    00223 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to)
    00224
    00225 /* Translate values ingame to different values in the savegame and vv */
    00226 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
    00227
    00228 #define SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
    00229 #define SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
    00230
    00231 /* End marker */
    00232 #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL}
    00233
    00234 /* Simple variables, references (pointers) and arrays, but for global variables */
    00235 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable}
    00236
    00237 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
    00238 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
    00239 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
    00240 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
    00241 #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
    00242
    00243 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
    00244 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
    00245 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
    00246 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
    00247 #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
    00248
    00249 #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to, (void*)NULL}
    00250
    00251 #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL}
    00252
    00255 static inline bool CheckSavegameVersionOldStyle(uint16 major, byte minor)
    00256 {
    00257   extern uint16 _sl_version;
    00258   extern byte   _sl_minor_version;
    00259   return (_sl_version < major) || (_sl_version == major && _sl_minor_version < minor);
    00260 }
    00261
    00264 static inline bool CheckSavegameVersion(uint16 version)
    00265 {
    00266   extern uint16 _sl_version;
    00267   return _sl_version < version;
    00268 }
    00269
    00272 static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to)
    00273 {
    00274   extern const uint16 SAVEGAME_VERSION;
    00275   if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false;
    00276
    00277   return true;
    00278 }
    00279
    00280 /* Get the NumberType of a setting. This describes the integer type
    00281  * as it is represented in memory
    00282  * @param type VarType holding information about the variable-type
    00283  * @return return the SLE_VAR_* part of a variable-type description */
    00284 static inline VarType GetVarMemType(VarType type)
    00285 {
    00286   return type & 0xF0; // GB(type, 4, 4) << 4;
    00287 }
    00288
    00289 /* Get the FileType of a setting. This describes the integer type
    00290  * as it is represented in a savegame/file
    00291  * @param type VarType holding information about the variable-type
    00292  * @param return the SLE_FILE_* part of a variable-type description */
    00293 static inline VarType GetVarFileType(VarType type)
    00294 {
    00295   return type & 0xF; // GB(type, 0, 4);
    00296 }
    00297
    00303 static inline bool IsNumericType(VarType conv)
    00304 {
    00305   return GetVarMemType(conv) <= SLE_VAR_U64;
    00306 }
    00307
    00312 static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
    00313 {
    00314   return (byte*)(sld->global ? NULL : object) + (ptrdiff_t)sld->address;
    00315 }
    00316
    00317 int64 ReadValue(const void *ptr, VarType conv);
    00318 void WriteValue(void *ptr, VarType conv, int64 val);
    00319
    00320 void SlSetArrayIndex(uint index);
    00321 int SlIterateArray();
    00322
    00323 void SlAutolength(AutolengthProc *proc, void *arg);
    00324 size_t SlGetFieldLength();
    00325 void SlSetLength(size_t length);
    00326 size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld);
    00327 size_t SlCalcObjLength(const void *object, const SaveLoad *sld);
    00328
    00329 byte SlReadByte();
    00330 void SlWriteByte(byte b);
    00331
    00332 void SlGlobList(const SaveLoadGlobVarList *sldg);
    00333 void SlArray(void *array, size_t length, VarType conv);
    00334 void SlObject(void *object, const SaveLoad *sld);
    00335 bool SlObjectMember(void *object, const SaveLoad *sld);
    00336 void NORETURN SlErrorCorrupt(const char *msg);
    00337
    00338 bool SaveloadCrashWithMissingNewGRFs();
    00339
    00340 extern char _savegame_format[8];
    00341
    00342 #endif /* SAVELOAD_H */