tron@2186: /* $Id$ */ tron@2186: belugas@6916: /** @file saveload.h */ belugas@6916: truelight@0: #ifndef SAVELOAD_H truelight@0: #define SAVELOAD_H truelight@0: rubidium@7425: #include "fileio.h" rubidium@7425: rubidium@5838: #ifdef SIZE_MAX rubidium@5838: #undef SIZE_MAX rubidium@5838: #endif rubidium@5838: rubidium@5838: #define SIZE_MAX ((size_t)-1) rubidium@5838: rubidium@6574: enum SaveOrLoadResult { belugas@6916: SL_OK = 0, ///< completed successfully belugas@6916: SL_ERROR = 1, ///< error that was caught before internal structures were modified belugas@6916: SL_REINIT = 2, ///< error that was caught in the middle of updating game state, need to clear it. (can only happen during load) rubidium@6574: }; tron@2162: rubidium@6574: enum SaveOrLoadMode { Darkvater@3044: SL_INVALID = -1, Darkvater@3044: SL_LOAD = 0, Darkvater@3044: SL_SAVE = 1, Darkvater@3044: SL_OLD_LOAD = 2, truelight@4300: SL_PNG = 3, truelight@4300: SL_BMP = 4, rubidium@6574: }; tron@2162: rubidium@7532: void SetSaveLoadError(uint16 str); rubidium@7532: const char *GetSaveLoadErrorString(); rubidium@7425: SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb); rubidium@6573: void WaitTillSaved(); rubidium@6573: void DoExitSave(); tron@2162: tron@2162: rubidium@6573: typedef void ChunkSaveLoadProc(); truelight@0: typedef void AutolengthProc(void *arg); truelight@0: rubidium@6574: struct ChunkHandler { truelight@0: uint32 id; truelight@0: ChunkSaveLoadProc *save_proc; truelight@0: ChunkSaveLoadProc *load_proc; truelight@0: uint32 flags; rubidium@6574: }; truelight@0: rubidium@6574: struct NullStruct { truelight@0: byte null; rubidium@6574: }; truelight@0: rubidium@6574: enum SLRefType { rubidium@4344: REF_ORDER = 0, rubidium@4344: REF_VEHICLE = 1, rubidium@4344: REF_STATION = 2, rubidium@4344: REF_TOWN = 3, rubidium@4344: REF_VEHICLE_OLD = 4, rubidium@4344: REF_ROADSTOPS = 5, peter1138@2848: REF_ENGINE_RENEWS = 6, rubidium@7506: REF_CARGO_PACKET = 7, rubidium@6574: }; truelight@0: Darkvater@3044: #define SL_MAX_VERSION 255 Darkvater@1881: truelight@0: enum { truelight@0: INC_VEHICLE_COMMON = 0, truelight@0: }; truelight@0: truelight@0: enum { rubidium@4344: CH_RIFF = 0, rubidium@4344: CH_ARRAY = 1, rubidium@4344: CH_SPARSE_ARRAY = 2, rubidium@4344: CH_TYPE_MASK = 3, rubidium@4344: CH_LAST = 8, Darkvater@3044: CH_AUTO_LENGTH = 16, Darkvater@3044: Darkvater@3044: CH_PRI_0 = 0 << 4, Darkvater@3044: CH_PRI_1 = 1 << 4, Darkvater@3044: CH_PRI_2 = 2 << 4, Darkvater@3044: CH_PRI_3 = 3 << 4, Darkvater@3044: CH_PRI_SHL = 4, truelight@0: CH_NUM_PRI_LEVELS = 4, truelight@0: }; truelight@0: Darkvater@3114: /** VarTypes is the general bitmasked magic type that tells us Darkvater@3114: * certain characteristics about the variable it refers to. For example Darkvater@3114: * SLE_FILE_* gives the size(type) as it would be in the savegame and Darkvater@3114: * SLE_VAR_* the size(type) as it is in memory during runtime. These are Darkvater@3114: * the first 8 bytes (0-3 SLE_FILE, 4-7 SLE_VAR). Darkvater@3114: * Bytes 8-15 are reserved for various flags as explained below */ Darkvater@3046: enum VarTypes { Darkvater@3046: /* 4 bytes allocated a maximum of 16 types for NumberType */ Darkvater@3046: SLE_FILE_I8 = 0, Darkvater@3046: SLE_FILE_U8 = 1, Darkvater@3046: SLE_FILE_I16 = 2, Darkvater@3046: SLE_FILE_U16 = 3, Darkvater@3046: SLE_FILE_I32 = 4, Darkvater@3046: SLE_FILE_U32 = 5, Darkvater@3046: SLE_FILE_I64 = 6, Darkvater@3046: SLE_FILE_U64 = 7, Darkvater@3114: SLE_FILE_STRINGID = 8, ///< StringID offset into strings-array Darkvater@3048: SLE_FILE_STRING = 9, Darkvater@3114: /* 6 more possible file-primitives */ truelight@0: Darkvater@3046: /* 4 bytes allocated a maximum of 16 types for NumberType */ rubidium@4344: SLE_VAR_BL = 0 << 4, rubidium@4344: SLE_VAR_I8 = 1 << 4, rubidium@4344: SLE_VAR_U8 = 2 << 4, rubidium@4344: SLE_VAR_I16 = 3 << 4, rubidium@4344: SLE_VAR_U16 = 4 << 4, rubidium@4344: SLE_VAR_I32 = 5 << 4, rubidium@4344: SLE_VAR_U32 = 6 << 4, rubidium@4344: SLE_VAR_I64 = 7 << 4, rubidium@4344: SLE_VAR_U64 = 8 << 4, rubidium@4344: SLE_VAR_NULL = 9 << 4, ///< useful to write zeros in savegame. Darkvater@5140: SLE_VAR_STRB = 10 << 4, ///< string (with pre-allocated buffer) rubidium@4344: SLE_VAR_STRBQ = 11 << 4, ///< string enclosed in quotes (with pre-allocated buffer) rubidium@4344: SLE_VAR_STR = 12 << 4, ///< string pointer Darkvater@5140: SLE_VAR_STRQ = 13 << 4, ///< string pointer enclosed in quotes peter1138@8754: SLE_VAR_NAME = 14 << 4, ///< old custom name to be converted to a char pointer Darkvater@4255: /* 2 more possible memory-primitives */ truelight@0: Darkvater@3046: /* Shortcut values */ Darkvater@3046: SLE_VAR_CHAR = SLE_VAR_I8, truelight@0: Darkvater@3046: /* Default combinations of variables. As savegames change, so can variables Darkvater@3046: * and thus it is possible that the saved value and internal size do not Darkvater@3046: * match and you need to specify custom combo. The defaults are listed here */ rubidium@4344: SLE_BOOL = SLE_FILE_I8 | SLE_VAR_BL, rubidium@4344: SLE_INT8 = SLE_FILE_I8 | SLE_VAR_I8, rubidium@4344: SLE_UINT8 = SLE_FILE_U8 | SLE_VAR_U8, rubidium@4344: SLE_INT16 = SLE_FILE_I16 | SLE_VAR_I16, rubidium@4344: SLE_UINT16 = SLE_FILE_U16 | SLE_VAR_U16, rubidium@4344: SLE_INT32 = SLE_FILE_I32 | SLE_VAR_I32, rubidium@4344: SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32, rubidium@4344: SLE_INT64 = SLE_FILE_I64 | SLE_VAR_I64, rubidium@4344: SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64, rubidium@4344: SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR, rubidium@4344: SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U16, rubidium@4344: SLE_STRINGBUF = SLE_FILE_STRING | SLE_VAR_STRB, rubidium@4344: SLE_STRINGBQUOTE = SLE_FILE_STRING | SLE_VAR_STRBQ, rubidium@4344: SLE_STRING = SLE_FILE_STRING | SLE_VAR_STR, rubidium@4344: SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ, peter1138@8754: SLE_NAME = SLE_FILE_STRINGID | SLE_VAR_NAME, truelight@0: Darkvater@3046: /* Shortcut values */ rubidium@4344: SLE_UINT = SLE_UINT32, rubidium@4344: SLE_INT = SLE_INT32, rubidium@4344: SLE_STRB = SLE_STRINGBUF, rubidium@4344: SLE_STRBQ = SLE_STRINGBQUOTE, rubidium@4344: SLE_STR = SLE_STRING, rubidium@4344: SLE_STRQ = SLE_STRINGQUOTE, Darkvater@3115: Darkvater@3115: /* 8 bytes allocated for a maximum of 8 flags Darkvater@3115: * Flags directing saving/loading of a variable */ Darkvater@3115: SLF_SAVE_NO = 1 << 8, ///< do not save with savegame, basically player-based Darkvater@3115: SLF_CONFIG_NO = 1 << 9, ///< do not save to config file Darkvater@3115: SLF_NETWORK_NO = 1 << 10, ///< do not synchronize over network (but it is saved if SSF_SAVE_NO is not set) Darkvater@3115: /* 5 more possible flags */ Darkvater@3046: }; Darkvater@3046: Darkvater@3046: typedef uint32 VarType; Darkvater@1881: Darkvater@1881: enum SaveLoadTypes { rubidium@7989: SL_VAR = 0, rubidium@7989: SL_REF = 1, rubidium@7989: SL_ARR = 2, rubidium@7989: SL_STR = 3, rubidium@7989: SL_LST = 4, Darkvater@1881: // non-normal save-load types rubidium@7989: SL_WRITEBYTE = 8, rubidium@7989: SL_VEH_INCLUDE = 9, rubidium@7989: SL_END = 15 truelight@0: }; truelight@0: Darkvater@3046: typedef byte SaveLoadType; Darkvater@3046: Darkvater@1881: /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */ rubidium@6574: struct SaveLoad { rubidium@7506: bool global; ///< should we load a global variable or a non-global one Darkvater@3114: SaveLoadType cmd; ///< the action to take with the saved/loaded type, All types need different action Darkvater@3114: VarType conv; ///< type of the variable to be saved, int Darkvater@3114: uint16 length; ///< (conditional) length of the variable (eg. arrays) (max array size is 65536 elements) Darkvater@3114: uint16 version_from; ///< save/load the variable starting from this savegame version Darkvater@3114: uint16 version_to; ///< save/load the variable until this savegame version Darkvater@3046: /* NOTE: This element either denotes the address of the variable for a global Darkvater@3046: * variable, or the offset within a struct which is then bound to a variable Darkvater@3046: * during runtime. Decision on which one to use is controlled by the function rubidium@7506: * that is called to save it. address: global=true, offset: global=false */ Darkvater@3114: void *address; ///< address of variable OR offset of variable in the struct (max offset is 65536) rubidium@6574: }; truelight@0: Darkvater@3046: /* Same as SaveLoad but global variables are used (for better readability); */ Darkvater@3046: typedef SaveLoad SaveLoadGlobVarList; Darkvater@3046: Darkvater@1881: /* Simple variables, references (pointers) and arrays */ rubidium@7506: #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)} Darkvater@3044: #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to) Darkvater@3044: #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to) Darkvater@3044: #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to) Darkvater@3044: #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to) peter1138@6995: #define SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to) Darkvater@3044: Darkvater@3044: #define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION) Darkvater@3044: #define SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION) Darkvater@3044: #define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION) Darkvater@3044: #define SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION) peter1138@6995: #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION) Darkvater@3044: Darkvater@3240: #define SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to) Darkvater@3222: rubidium@7313: /* Translate values ingame to different values in the savegame and vv */ rubidium@7313: #define SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value) truelight@0: Darkvater@1881: /* The same as the ones at the top, only the offset is given directly; used for unions */ rubidium@8106: #define SLE_GENERALX(cmd, offset, type, length, param1, param2) {false, cmd, type, length, param1, param2, (void*)(offset)} rubidium@8106: #define SLE_CONDVARX(offset, type, from, to) SLE_GENERALX(SL_VAR, offset, type, 0, from, to) rubidium@8106: #define SLE_CONDARRX(offset, type, length, from, to) SLE_GENERALX(SL_ARR, offset, type, length, from, to) rubidium@8106: #define SLE_CONDREFX(offset, type, from, to) SLE_GENERALX(SL_REF, offset, type, 0, from, to) Darkvater@3044: Darkvater@3044: #define SLE_VARX(offset, type) SLE_CONDVARX(offset, type, 0, SL_MAX_VERSION) Darkvater@3044: #define SLE_REFX(offset, type) SLE_CONDREFX(offset, type, 0, SL_MAX_VERSION) Darkvater@3044: rubidium@8106: #define SLE_WRITEBYTEX(offset, something) SLE_GENERALX(SL_WRITEBYTE, offset, 0, 0, something, 0) rubidium@8106: #define SLE_VEH_INCLUDEX() SLE_GENERALX(SL_VEH_INCLUDE, 0, 0, 0, 0, SL_MAX_VERSION) truelight@0: Darkvater@1881: /* End marker */ rubidium@7506: #define SLE_END() {false, SL_END, 0, 0, 0, 0, NULL} Darkvater@3046: Darkvater@3046: /* Simple variables, references (pointers) and arrays, but for global variables */ rubidium@7506: #define SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable} Darkvater@3046: Darkvater@3046: #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to) Darkvater@3046: #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to) Darkvater@3046: #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to) Darkvater@3046: #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to) peter1138@6995: #define SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to) Darkvater@3046: Darkvater@3046: #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION) Darkvater@3046: #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION) Darkvater@3222: #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION) Darkvater@3222: #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION) peter1138@6995: #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION) Darkvater@3222: rubidium@7506: #define SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to, (void*)NULL} Darkvater@3046: rubidium@7506: #define SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL} truelight@0: truelight@2685: /** Checks if the savegame is below major.minor. truelight@2685: */ truelight@2685: static inline bool CheckSavegameVersionOldStyle(uint16 major, byte minor) truelight@2685: { Darkvater@3045: extern uint16 _sl_version; Darkvater@3045: extern byte _sl_minor_version; truelight@2685: return (_sl_version < major) || (_sl_version == major && _sl_minor_version < minor); truelight@2685: } truelight@2685: truelight@2685: /** Checks if the savegame is below version. truelight@2685: */ truelight@2685: static inline bool CheckSavegameVersion(uint16 version) truelight@2685: { Darkvater@3045: extern uint16 _sl_version; truelight@2685: return _sl_version < version; truelight@2685: } truelight@2685: Darkvater@3117: /** Checks if some version from/to combination falls within the range of the Darkvater@3117: * active savegame version */ Darkvater@3117: static inline bool SlIsObjectCurrentlyValid(uint16 version_from, uint16 version_to) Darkvater@3117: { Darkvater@3117: extern const uint16 SAVEGAME_VERSION; Darkvater@3117: if (SAVEGAME_VERSION < version_from || SAVEGAME_VERSION > version_to) return false; Darkvater@3117: Darkvater@3117: return true; Darkvater@3117: } Darkvater@3117: Darkvater@3108: /* Get the NumberType of a setting. This describes the integer type Darkvater@3108: * as it is represented in memory Darkvater@3108: * @param type VarType holding information about the variable-type Darkvater@3108: * @return return the SLE_VAR_* part of a variable-type description */ Darkvater@3108: static inline VarType GetVarMemType(VarType type) Darkvater@3108: { Darkvater@3625: return type & 0xF0; // GB(type, 4, 4) << 4; Darkvater@3108: } Darkvater@3108: Darkvater@3108: /* Get the FileType of a setting. This describes the integer type Darkvater@3108: * as it is represented in a savegame/file Darkvater@3108: * @param type VarType holding information about the variable-type Darkvater@3108: * @param return the SLE_FILE_* part of a variable-type description */ Darkvater@3108: static inline VarType GetVarFileType(VarType type) Darkvater@3108: { Darkvater@3108: return type & 0xF; // GB(type, 0, 4); Darkvater@3108: } Darkvater@3108: Darkvater@5141: /** Get the address of the variable. Which one to pick depends on the object Darkvater@5141: * pointer. If it is NULL we are dealing with global variables so the address Darkvater@5141: * is taken. If non-null only the offset is stored in the union and we need Darkvater@5141: * to add this to the address of the object */ Darkvater@5141: static inline void *GetVariableAddress(const void *object, const SaveLoad *sld) Darkvater@5141: { Darkvater@5141: return (byte*)object + (ptrdiff_t)sld->address; Darkvater@5141: } Darkvater@5141: Darkvater@3108: int64 ReadValue(const void *ptr, VarType conv); Darkvater@3108: void WriteValue(void *ptr, VarType conv, int64 val); Darkvater@3108: truelight@0: void SlSetArrayIndex(uint index); rubidium@6573: int SlIterateArray(); Darkvater@3046: truelight@0: void SlAutolength(AutolengthProc *proc, void *arg); rubidium@6573: uint SlGetFieldLength(); Darkvater@1881: void SlSetLength(size_t length); Darkvater@5142: size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld); Darkvater@3046: rubidium@6573: byte SlReadByte(); Darkvater@1881: void SlWriteByte(byte b); Darkvater@3046: Darkvater@3046: void SlGlobList(const SaveLoadGlobVarList *sldg); Darkvater@3046: void SlArray(void *array, uint length, VarType conv); Darkvater@3046: void SlObject(void *object, const SaveLoad *sld); Darkvater@3046: bool SlObjectMember(void *object, const SaveLoad *sld); truelight@0: truelight@0: #endif /* SAVELOAD_H */