saveload.h
changeset 3046 baa216f9911a
parent 3045 0364fedfd355
child 3048 adf46229d390
equal deleted inserted replaced
3045:0364fedfd355 3046:baa216f9911a
    20 void WaitTillSaved(void);
    20 void WaitTillSaved(void);
    21 
    21 
    22 
    22 
    23 typedef void ChunkSaveLoadProc(void);
    23 typedef void ChunkSaveLoadProc(void);
    24 typedef void AutolengthProc(void *arg);
    24 typedef void AutolengthProc(void *arg);
    25 
       
    26 typedef struct SaveLoadGlobVarList {
       
    27 	void *address;
       
    28 	byte conv;
       
    29 	uint16 from_version;
       
    30 	uint16 to_version;
       
    31 } SaveLoadGlobVarList;
       
    32 
    25 
    33 typedef struct {
    26 typedef struct {
    34 	uint32 id;
    27 	uint32 id;
    35 	ChunkSaveLoadProc *save_proc;
    28 	ChunkSaveLoadProc *save_proc;
    36 	ChunkSaveLoadProc *load_proc;
    29 	ChunkSaveLoadProc *load_proc;
    71 	CH_PRI_3          = 3 << 4,
    64 	CH_PRI_3          = 3 << 4,
    72 	CH_PRI_SHL        = 4,
    65 	CH_PRI_SHL        = 4,
    73 	CH_NUM_PRI_LEVELS = 4,
    66 	CH_NUM_PRI_LEVELS = 4,
    74 };
    67 };
    75 
    68 
    76 typedef enum VarTypes {
    69 enum VarTypes {
    77 	SLE_FILE_I8  = 0,
    70 	/* 4 bytes allocated a maximum of 16 types for NumberType */
    78 	SLE_FILE_U8  = 1,
    71 	SLE_FILE_I8       = 0,
    79 	SLE_FILE_I16 = 2,
    72 	SLE_FILE_U8       = 1,
    80 	SLE_FILE_U16 = 3,
    73 	SLE_FILE_I16      = 2,
    81 	SLE_FILE_I32 = 4,
    74 	SLE_FILE_U16      = 3,
    82 	SLE_FILE_U32 = 5,
    75 	SLE_FILE_I32      = 4,
    83 	SLE_FILE_I64 = 6,
    76 	SLE_FILE_U32      = 5,
    84 	SLE_FILE_U64 = 7,
    77 	SLE_FILE_I64      = 6,
    85 
    78 	SLE_FILE_U64      = 7,
    86 	SLE_FILE_STRINGID = 8,
    79 	SLE_FILE_STRINGID = 8, /// StringID offset into strings-array
    87 //	SLE_FILE_IVAR = 8,
    80 	/* 7 more possible primitives */
    88 //	SLE_FILE_UVAR = 9,
    81 
    89 
    82 	/* 4 bytes allocated a maximum of 16 types for NumberType */
    90 	SLE_VAR_I8   = 0 << 4,
    83 	SLE_VAR_I8   =  0 << 4,
    91 	SLE_VAR_U8   = 1 << 4,
    84 	SLE_VAR_U8   =  1 << 4,
    92 	SLE_VAR_I16  = 2 << 4,
    85 	SLE_VAR_I16  =  2 << 4,
    93 	SLE_VAR_U16  = 3 << 4,
    86 	SLE_VAR_U16  =  3 << 4,
    94 	SLE_VAR_I32  = 4 << 4,
    87 	SLE_VAR_I32  =  4 << 4,
    95 	SLE_VAR_U32  = 5 << 4,
    88 	SLE_VAR_U32  =  5 << 4,
    96 	SLE_VAR_I64  = 6 << 4,
    89 	SLE_VAR_I64  =  6 << 4,
    97 	SLE_VAR_U64  = 7 << 4,
    90 	SLE_VAR_U64  =  7 << 4,
    98 
    91 	SLE_VAR_NULL =  8 << 4, /// useful to write zeros in savegame.
    99 	SLE_VAR_NULL = 8 << 4, // useful to write zeros in savegame.
    92 	/* 7 more possible primitives */
   100 
    93 
   101 	SLE_VAR_INT  = SLE_VAR_I32,
    94 	/* Shortcut values */
   102 	SLE_VAR_UINT = SLE_VAR_U32,
    95 	SLE_VAR_CHAR = SLE_VAR_I8,
   103 
    96 
   104 	SLE_INT8   = SLE_FILE_I8  | SLE_VAR_I8,
    97 	/* Default combinations of variables. As savegames change, so can variables
   105 	SLE_UINT8  = SLE_FILE_U8  | SLE_VAR_U8,
    98 	 * and thus it is possible that the saved value and internal size do not
   106 	SLE_INT16  = SLE_FILE_I16 | SLE_VAR_I16,
    99 	 * match and you need to specify custom combo. The defaults are listed here */
   107 	SLE_UINT16 = SLE_FILE_U16 | SLE_VAR_U16,
   100 	SLE_INT8        = SLE_FILE_I8  | SLE_VAR_I8,
   108 	SLE_INT32  = SLE_FILE_I32 | SLE_VAR_I32,
   101 	SLE_UINT8       = SLE_FILE_U8  | SLE_VAR_U8,
   109 	SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32,
   102 	SLE_INT16       = SLE_FILE_I16 | SLE_VAR_I16,
   110 	SLE_INT64  = SLE_FILE_I64 | SLE_VAR_I64,
   103 	SLE_UINT16      = SLE_FILE_U16 | SLE_VAR_U16,
   111 	SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64,
   104 	SLE_INT32       = SLE_FILE_I32 | SLE_VAR_I32,
   112 
   105 	SLE_UINT32      = SLE_FILE_U32 | SLE_VAR_U32,
   113 	SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U16,
   106 	SLE_INT64       = SLE_FILE_I64 | SLE_VAR_I64,
   114 } VarType;
   107 	SLE_UINT64      = SLE_FILE_U64 | SLE_VAR_U64,
       
   108 	SLE_STRINGID    = SLE_FILE_STRINGID | SLE_VAR_U16,
       
   109 
       
   110 	/* Shortcut values */
       
   111 	SLE_UINT = SLE_UINT32,
       
   112 	SLE_INT  = SLE_INT32,
       
   113 };
       
   114 
       
   115 typedef uint32 VarType;
   115 
   116 
   116 enum SaveLoadTypes {
   117 enum SaveLoadTypes {
   117 	SL_VAR       = 0,
   118 	SL_VAR       = 0,
   118 	SL_REF       = 1,
   119 	SL_REF       = 1,
   119 	SL_ARR       = 2,
   120 	SL_ARR       = 2,
   121 	SL_WRITEBYTE = 8,
   122 	SL_WRITEBYTE = 8,
   122 	SL_INCLUDE   = 9,
   123 	SL_INCLUDE   = 9,
   123 	SL_END       = 15
   124 	SL_END       = 15
   124 };
   125 };
   125 
   126 
       
   127 typedef byte SaveLoadType;
       
   128 typedef uint16 OffSetType;
       
   129 
   126 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
   130 /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */
   127 typedef struct SaveLoad {
   131 typedef struct SaveLoad {
   128 	byte cmd;             /// the action to take with the saved/loaded type, All types need different action
   132 	SaveLoadType cmd;     /// the action to take with the saved/loaded type, All types need different action
   129 	VarType type;         /// type of the variable to be saved, int
   133 	VarType conv;         /// type of the variable to be saved, int
   130 	uint16 offset;        /// offset of this variable in the struct (max offset is 65536)
       
   131 	uint16 length;        /// (conditional) length of the variable (eg. arrays) (max array size is 65536 elements)
   134 	uint16 length;        /// (conditional) length of the variable (eg. arrays) (max array size is 65536 elements)
   132 	uint16 version_from;  /// save/load the variable starting from this savegame version
   135 	uint16 version_from;  /// save/load the variable starting from this savegame version
   133 	uint16 version_to;    /// save/load the variable until this savegame version
   136 	uint16 version_to;    /// save/load the variable until this savegame version
       
   137 	/* NOTE: This element either denotes the address of the variable for a global
       
   138 	 * variable, or the offset within a struct which is then bound to a variable
       
   139 	 * during runtime. Decision on which one to use is controlled by the function
       
   140 	 * that is called to save it. address: SlGlobList, offset: SlObject */
       
   141 	union {
       
   142 		void *address;      /// address of variable
       
   143 		OffSetType offset;  /// offset of variable in the struct (max offset is 65536)
       
   144 	} s;
   134 } SaveLoad;
   145 } SaveLoad;
   135 
   146 
       
   147 /* Same as SaveLoad but global variables are used (for better readability); */
       
   148 typedef SaveLoad SaveLoadGlobVarList;
       
   149 
   136 /* Simple variables, references (pointers) and arrays */
   150 /* Simple variables, references (pointers) and arrays */
   137 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {cmd, type, offsetof(base, variable), length, from, to}
   151 #define SLE_GENERAL(cmd, base, variable, type, length, from, to) {cmd, type, length, from, to, (void*)offsetof(base, variable)}
   138 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
   152 #define SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
   139 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
   153 #define SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
   140 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
   154 #define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
   141 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
   155 #define SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
   142 
   156 
   149 #define SLE_WRITEBYTE(base, variable, game_value, file_value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, game_value, file_value)
   163 #define SLE_WRITEBYTE(base, variable, game_value, file_value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, game_value, file_value)
   150 /* Load common code and put it into each struct (currently only for vehicles */
   164 /* Load common code and put it into each struct (currently only for vehicles */
   151 #define SLE_INCLUDE(base, variable, include_index) SLE_GENERAL(SL_INCLUDE, base, variable, 0, 0, include_index, 0)
   165 #define SLE_INCLUDE(base, variable, include_index) SLE_GENERAL(SL_INCLUDE, base, variable, 0, 0, include_index, 0)
   152 
   166 
   153 /* The same as the ones at the top, only the offset is given directly; used for unions */
   167 /* The same as the ones at the top, only the offset is given directly; used for unions */
   154 #define SLE_GENERALX(cmd, offset, type, param1, param2) {cmd, type, (offset), 0, param1, param2}
   168 #define SLE_GENERALX(cmd, offset, type, param1, param2) {cmd, type, 0, param1, param2, (void*)(offset)}
   155 #define SLE_CONDVARX(offset, type, from, to) SLE_GENERALX(SL_VAR, offset, type, from, to)
   169 #define SLE_CONDVARX(offset, type, from, to) SLE_GENERALX(SL_VAR, offset, type, from, to)
   156 #define SLE_CONDREFX(offset, type, from, to) SLE_GENERALX(SL_REF, offset, type, from, to)
   170 #define SLE_CONDREFX(offset, type, from, to) SLE_GENERALX(SL_REF, offset, type, from, to)
   157 
   171 
   158 #define SLE_VARX(offset, type) SLE_CONDVARX(offset, type, 0, SL_MAX_VERSION)
   172 #define SLE_VARX(offset, type) SLE_CONDVARX(offset, type, 0, SL_MAX_VERSION)
   159 #define SLE_REFX(offset, type) SLE_CONDREFX(offset, type, 0, SL_MAX_VERSION)
   173 #define SLE_REFX(offset, type) SLE_CONDREFX(offset, type, 0, SL_MAX_VERSION)
   160 
   174 
   161 #define SLE_WRITEBYTEX(offset, something) SLE_GENERALX(SL_WRITEBYTE, offset, 0, something, 0)
   175 #define SLE_WRITEBYTEX(offset, something) SLE_GENERALX(SL_WRITEBYTE, offset, 0, something, 0)
   162 #define SLE_INCLUDEX(offset, type) SLE_GENERALX(SL_INCLUDE, offset, type, 0, SL_MAX_VERSION)
   176 #define SLE_INCLUDEX(offset, type) SLE_GENERALX(SL_INCLUDE, offset, type, 0, SL_MAX_VERSION)
   163 
   177 
   164 /* End marker */
   178 /* End marker */
   165 #define SLE_END() {SL_END, 0, 0, 0, 0, 0}
   179 #define SLE_END() {SL_END, 0, 0, 0, 0, (void*)0}
       
   180 
       
   181 /* Simple variables, references (pointers) and arrays, but for global variables */
       
   182 #define SLEG_GENERAL(cmd, variable, type, length, from, to) {cmd, type, length, from, to, &variable}
       
   183 
       
   184 #define SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
       
   185 #define SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
       
   186 #define SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
       
   187 #define SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
       
   188 
       
   189 #define SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
       
   190 #define SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
       
   191 #define SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), SL_MAX_VERSION)
       
   192 #define SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), SL_MAX_VERSION)
       
   193 
       
   194 #define SLEG_END() {SL_END, 0, 0, 0, 0, NULL}
   166 
   195 
   167 /** Checks if the savegame is below major.minor.
   196 /** Checks if the savegame is below major.minor.
   168  */
   197  */
   169 static inline bool CheckSavegameVersionOldStyle(uint16 major, byte minor)
   198 static inline bool CheckSavegameVersionOldStyle(uint16 major, byte minor)
   170 {
   199 {
   181 	return _sl_version < version;
   210 	return _sl_version < version;
   182 }
   211 }
   183 
   212 
   184 void SlSetArrayIndex(uint index);
   213 void SlSetArrayIndex(uint index);
   185 int SlIterateArray(void);
   214 int SlIterateArray(void);
   186 void SlArray(void *array, uint length, VarType conv);
   215 
   187 void SlObject(void *object, const SaveLoad *desc);
       
   188 void SlAutolength(AutolengthProc *proc, void *arg);
   216 void SlAutolength(AutolengthProc *proc, void *arg);
   189 uint SlGetFieldLength(void);
   217 uint SlGetFieldLength(void);
       
   218 void SlSetLength(size_t length);
       
   219 size_t SlCalcObjMemberLength(const SaveLoad *sld);
       
   220 
   190 byte SlReadByte(void);
   221 byte SlReadByte(void);
   191 void SlSetLength(size_t length);
       
   192 void SlWriteByte(byte b);
   222 void SlWriteByte(byte b);
   193 void SlGlobList(const SaveLoadGlobVarList *desc);
   223 
       
   224 void SlGlobList(const SaveLoadGlobVarList *sldg);
       
   225 void SlArray(void *array, uint length, VarType conv);
       
   226 void SlObject(void *object, const SaveLoad *sld);
       
   227 bool SlObjectMember(void *object, const SaveLoad *sld);
   194 
   228 
   195 void SaveFileStart(void);
   229 void SaveFileStart(void);
   196 void SaveFileDone(void);
   230 void SaveFileDone(void);
   197 void SaveFileError(void);
   231 void SaveFileError(void);
   198 #endif /* SAVELOAD_H */
   232 #endif /* SAVELOAD_H */