# saveload.h

Go to the documentation of this file.

    00001 /* $Id: saveload.h 15718 2009-03-15 00:32:18Z rubidium $ */
    00002
    00005 #ifndef SAVELOAD_H
    00006 #define SAVELOAD_H
    00007
    00008 #include "../fileio_type.h"
    00009
    00010 #ifdef SIZE_MAX
    00011 #undef SIZE_MAX
    00012 #endif
    00013
    00014 #define SIZE_MAX ((size_t)-1)
    00015
    00016 enum SaveOrLoadResult {
    00017   SL_OK     = 0,
    00018   SL_ERROR  = 1,
    00019   SL_REINIT = 2,
    00020 };
    00021
    00022 enum SaveOrLoadMode {
    00023   SL_INVALID  = -1,
    00024   SL_LOAD     =  0,
    00025   SL_SAVE     =  1,
    00026   SL_OLD_LOAD =  2,
    00027   SL_PNG      =  3,
    00028   SL_BMP      =  4,
    00029 };
    00030
    00031 enum SavegameType {
    00032   SGT_TTD,
    00033   SGT_TTDP1,
    00034   SGT_TTDP2,
    00035   SGT_OTTD,
    00036   SGT_TTO,
    00037   SGT_INVALID = 0xFF
    00038 };
    00039
    00040 void GenerateDefaultSaveName(char *buf, const char *last);
    00041 void SetSaveLoadError(uint16 str);
    00042 const char *GetSaveLoadErrorString();
    00043 SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb);
    00044 void WaitTillSaved();
    00045 void DoExitSave();
    00046
    00047
    00048 typedef void ChunkSaveLoadProc();
    00049 typedef void AutolengthProc(void *arg);
    00050
    00051 struct ChunkHandler {
    00052   uint32 id;
    00053   ChunkSaveLoadProc *save_proc;
    00054   ChunkSaveLoadProc *load_proc;
    00055   uint32 flags;
    00056 };
    00057
    00058 struct NullStruct {
    00059   byte null;
    00060 };
    00061
    00062 enum SLRefType {
    00063   REF_ORDER         = 0,
    00064   REF_VEHICLE       = 1,
    00065   REF_STATION       = 2,
    00066   REF_TOWN          = 3,
    00067   REF_VEHICLE_OLD   = 4,
    00068   REF_ROADSTOPS     = 5,
    00069   REF_ENGINE_RENEWS = 6,
    00070   REF_CARGO_PACKET  = 7,
    00071   REF_ORDERLIST     = 8,
    00072 };
    00073
    00074 #define SL_MAX_VERSION 255
    00075
    00076 enum {
    00077   INC_VEHICLE_COMMON = 0,
    00078 };
    00079
    00080 enum {
    00081   CH_RIFF         =  0,
    00082   CH_ARRAY        =  1,
    00083   CH_SPARSE_ARRAY =  2,
    00084   CH_TYPE_MASK    =  3,
    00085   CH_LAST         =  8,
    00086   CH_AUTO_LENGTH  = 16,
    00087
    00088   CH_PRI_0          = 0 << 4,
    00089   CH_PRI_1          = 1 << 4,
    00090   CH_PRI_2          = 2 << 4,
    00091   CH_PRI_3          = 3 << 4,
    00092   CH_PRI_SHL        = 4,
    00093   CH_NUM_PRI_LEVELS = 4,
    00094 };
    00095
    00102 enum VarTypes {
    00103   /* 4 bits allocated a maximum of 16 types for NumberType */
    00104   SLE_FILE_I8       = 0,
    00105   SLE_FILE_U8       = 1,
    00106   SLE_FILE_I16      = 2,
    00107   SLE_FILE_U16      = 3,
    00108   SLE_FILE_I32      = 4,
    00109   SLE_FILE_U32      = 5,
    00110   SLE_FILE_I64      = 6,
    00111   SLE_FILE_U64      = 7,
    00112   SLE_FILE_STRINGID = 8,
    00113   SLE_FILE_STRING   = 9,
    00114   /* 6 more possible file-primitives */
    00115
    00116   /* 4 bits allocated a maximum of 16 types for NumberType */
    00117   SLE_VAR_BL    =  0 << 4,
    00118   SLE_VAR_I8    =  1 << 4,
    00119   SLE_VAR_U8    =  2 << 4,
    00120   SLE_VAR_I16   =  3 << 4,
    00121   SLE_VAR_U16   =  4 << 4,
    00122   SLE_VAR_I32   =  5 << 4,
    00123   SLE_VAR_U32   =  6 << 4,
    00124   SLE_VAR_I64   =  7 << 4,
    00125   SLE_VAR_U64   =  8 << 4,
    00126   SLE_VAR_NULL  =  9 << 4,
    00127   SLE_VAR_STRB  = 10 << 4,
    00128   SLE_VAR_STRBQ = 11 << 4,
    00129   SLE_VAR_STR   = 12 << 4,
    00130   SLE_VAR_STRQ  = 13 << 4,
    00131   SLE_VAR_NAME  = 14 << 4,
    00132   /* 1 more possible memory-primitives */
    00133
    00134   /* Shortcut values */
    00135   SLE_VAR_CHAR = SLE_VAR_I8,
    00136
    00137   /* Default combinations of variables. As savegames change, so can variables
    00138    * and thus it is possible that the saved value and internal size do not
    00139    * match and you need to specify custom combo. The defaults are listed here */
    00140   SLE_BOOL         = SLE_FILE_I8  | SLE_VAR_BL,
    00141   SLE_INT8         = SLE_FILE_I8  | SLE_VAR_I8,
    00142   SLE_UINT8        = SLE_FILE_U8  | SLE_VAR_U8,
    00143   SLE_INT16        = SLE_FILE_I16 | SLE_VAR_I16,
    00144   SLE_UINT16       = SLE_FILE_U16 | SLE_VAR_U16,
    00145   SLE_INT32        = SLE_FILE_I32 | SLE_VAR_I32,
    00146   SLE_UINT32       = SLE_FILE_U32 | SLE_VAR_U32,
    00147   SLE_INT64        = SLE_FILE_I64 | SLE_VAR_I64,
    00148   SLE_UINT64       = SLE_FILE_U64 | SLE_VAR_U64,
    00149   SLE_CHAR         = SLE_FILE_I8  | SLE_VAR_CHAR,
    00150   SLE_STRINGID     = SLE_FILE_STRINGID | SLE_VAR_U16,
    00151   SLE_STRINGBUF    = SLE_FILE_STRING   | SLE_VAR_STRB,
    00152   SLE_STRINGBQUOTE = SLE_FILE_STRING   | SLE_VAR_STRBQ,
    00153   SLE_STRING       = SLE_FILE_STRING   | SLE_VAR_STR,
    00154   SLE_STRINGQUOTE  = SLE_FILE_STRING   | SLE_VAR_STRQ,
    00155   SLE_NAME         = SLE_FILE_STRINGID | SLE_VAR_NAME,
    00156
    00157   /* Shortcut values */
    00158   SLE_UINT  = SLE_UINT32,
    00159   SLE_INT   = SLE_INT32,
    00160   SLE_STRB  = SLE_STRINGBUF,
    00161   SLE_STRBQ = SLE_STRINGBQUOTE,
    00162   SLE_STR   = SLE_STRING,
    00163   SLE_STRQ  = SLE_STRINGQUOTE,
    00164
    00165   /* 8 bits allocated for a maximum of 8 flags
    00166    * Flags directing saving/loading of a variable */
    00167   SLF_SAVE_NO      = 1 <<  8,
    00168   SLF_CONFIG_NO    = 1 <<  9,
    00169   SLF_NETWORK_NO   = 1 << 10,
    00170   /* 5 more possible flags */
    00171 };
    00172
    00173 typedef uint32 VarType;
    00174
    00175 enum SaveLoadTypes {
    00176   SL_VAR         =  0,
    00177   SL_REF         =  1,
    00178   SL_ARR         =  2,
    00179   SL_STR         =  3,
    00180   SL_LST         =  4,
    00181   /* non-normal save-load types */
    00182   SL_WRITEBYTE   =  8,
    00183   SL_VEH_INCLUDE =  9,
    00184   SL_END         = 15
    00185 };
    00186
    00187 typedef byte SaveLoadType;
    00188
    00190 struct SaveLoad {
    00191   bool global;
    00192   SaveLoadType cmd;
    00193   VarType conv;
    00194   uint16 length;
    00195   uint16 version_from;
    00196   uint16 version_to;
    00197   /* NOTE: This element either denotes the address of the variable for a global
    00198    * variable, or the offset within a struct which is then bound to a variable
    00199    * during runtime. Decision on which one to use is controlled by the function
    00200    * that is called to save it. address: global=true, offset: global=false */
    00201   void *address;
    00202 };
    00203
    00204 /* Same as SaveLoad but global variables are used (for better readability); */
    00205 typedef SaveLoad SaveLoadGlobVarList;
    00206
    00207 /* Simple variables, references (pointers) and arrays */
    00208 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}
    00209 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
    00210 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
    00211 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
    00212 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
    00213 #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
    00214
    00215 #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
    00216 #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
    00217 #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
    00218 #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
    00219 #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
    00220
    00221 #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to)
    00222
    00223 /* Translate values ingame to different values in the savegame and vv */
    00224 #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
    00225
    00226 /* The same as the ones at the top, only the offset is given directly; used for unions */
    00227 #define SLE_GENERALX(cmd, offset, type, length, param1, param2) {false, cmd, type, length, param1, param2, (void*)(offset)}
    00228 #define SLE_CONDVARX(offset, type, from, to) SLE_GENERALX(SL_VAR, offset, type, 0, from, to)
    00229 #define SLE_CONDARRX(offset, type, length, from, to) SLE_GENERALX(SL_ARR, offset, type, length, from, to)
    00230 #define SLE_CONDREFX(offset, type, from, to) SLE_GENERALX(SL_REF, offset, type, 0, from, to)
    00231
    00232 #define SLE_VARX(offset, type) SLE_CONDVARX(offset, type, 0, SL_MAX_VERSION)
    00233 #define SLE_REFX(offset, type) SLE_CONDREFX(offset, type, 0, SL_MAX_VERSION)
    00234
    00235 #define SLE_WRITEBYTEX(offset, something) SLE_GENERALX(SL_WRITEBYTE, offset, 0, 0, something, 0)
    00236 #define SLE_VEH_INCLUDEX() SLE_GENERALX(SL_VEH_INCLUDE, 0, 0, 0, 0, SL_MAX_VERSION)
    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 */