truelight@0: #ifndef SAVELOAD_H truelight@0: #define SAVELOAD_H truelight@0: truelight@817: #include truelight@817: tron@1093: typedef void ChunkSaveLoadProc(void); truelight@0: typedef void AutolengthProc(void *arg); truelight@0: truelight@0: typedef struct SaveLoadGlobVarList { truelight@0: void *address; truelight@0: byte conv; truelight@0: byte from_version; truelight@0: byte to_version; truelight@0: } SaveLoadGlobVarList; truelight@0: truelight@0: typedef struct { truelight@0: uint32 id; truelight@0: ChunkSaveLoadProc *save_proc; truelight@0: ChunkSaveLoadProc *load_proc; truelight@0: uint32 flags; truelight@0: } ChunkHandler; truelight@0: truelight@0: typedef struct { truelight@0: byte null; truelight@0: } NullStruct; truelight@0: truelight@817: typedef void WriterProc(uint len); tron@1093: typedef uint ReaderProc(void); truelight@817: Darkvater@1881: typedef enum SLRefType { truelight@1024: REF_ORDER = 0, truelight@1024: REF_VEHICLE = 1, truelight@1024: REF_STATION = 2, truelight@1024: REF_TOWN = 3, celestar@1217: REF_VEHICLE_OLD = 4, celestar@1217: REF_ROADSTOPS = 5 Darkvater@1881: } SLRefType; truelight@0: Darkvater@1881: typedef uint ReferenceToIntProc(const void *obj, SLRefType rt); Darkvater@1881: typedef void *IntToReferenceProc(uint index, SLRefType rt); Darkvater@1881: Darkvater@1881: typedef struct SaveLoad SaveLoad; Darkvater@1881: Darkvater@1881: /** The saveload struct, containing reader-writer functions, bufffer, version, etc. */ Darkvater@1881: typedef struct { Darkvater@1881: bool save; /// are we doing a save or a load atm. True when saving Darkvater@1881: byte need_length; /// ??? Darkvater@1881: byte block_mode; /// ??? Darkvater@1881: bool error; /// did an error occur or not Darkvater@1881: byte version; /// the major savegame version identifier Darkvater@1881: uint16 full_version; /// the full version of the savegame Darkvater@1881: Darkvater@1881: int obj_len; /// the length of the current object we are busy with Darkvater@1881: int array_index, last_array_index; /// in the case of an array, the current and last positions Darkvater@1881: Darkvater@1881: uint32 offs_base; /// the offset in number of bytes since we started writing data (eg uncompressed savegame size) Darkvater@1881: Darkvater@1881: WriterProc *write_bytes; /// savegame writer function Darkvater@1881: ReaderProc *read_bytes; /// savegame loader function Darkvater@1881: Darkvater@1881: ReferenceToIntProc *ref_to_int_proc; /// function to convert pointers to numbers when saving a game Darkvater@1881: IntToReferenceProc *int_to_ref_proc; /// function to convert numbers to pointers when loading a game Darkvater@1881: Darkvater@1881: const ChunkHandler* const *chs; /// the chunk of data that is being processed atm (vehicles, signs, etc.) Darkvater@1881: const SaveLoad* const *includes; /// the internal layouf of the given chunk Darkvater@1881: Darkvater@1881: /** When saving/loading savegames, they are always saved to a temporary memory-place Darkvater@1881: * to be flushed to file (save) or to final place (load) when full. */ Darkvater@1881: byte *bufp, *bufe; /// bufp(ointer) gives the current position in the buffer bufe(nd) gives the end of the buffer Darkvater@1881: Darkvater@1881: // these 3 may be used by compressor/decompressors. Darkvater@1881: byte *buf; /// pointer to temporary memory to read/write, initialized by SaveLoadFormat->initread/write Darkvater@1881: uint bufsize; /// the size of the temporary memory *buf Darkvater@1881: FILE *fh; /// the file from which is read or written to Darkvater@1881: Darkvater@1881: void (*excpt_uninit)(void); /// the function to execute on any encountered error Darkvater@1881: const char *excpt_msg; /// the error message Darkvater@1881: jmp_buf excpt; /// @todo used to jump to "exception handler"; really ugly Darkvater@1881: } SaverLoader; Darkvater@1881: Darkvater@1881: extern SaverLoader _sl; truelight@0: truelight@0: enum { truelight@0: INC_VEHICLE_COMMON = 0, truelight@0: }; truelight@0: truelight@0: enum { truelight@0: CH_RIFF = 0, truelight@0: CH_ARRAY = 1, truelight@0: CH_SPARSE_ARRAY = 2, truelight@0: CH_TYPE_MASK = 3, truelight@0: CH_LAST = 8, truelight@0: CH_AUTO_LENGTH = 16, truelight@0: CH_PRI_0 = 0 << 4, truelight@0: CH_PRI_1 = 1 << 4, truelight@0: CH_PRI_2 = 2 << 4, truelight@0: CH_PRI_3 = 3 << 4, truelight@0: CH_PRI_SHL = 4, truelight@0: CH_NUM_PRI_LEVELS = 4, truelight@0: }; truelight@0: Darkvater@1881: typedef enum VarTypes { Darkvater@1881: SLE_FILE_I8 = 0, Darkvater@1881: SLE_FILE_U8 = 1, truelight@0: SLE_FILE_I16 = 2, truelight@0: SLE_FILE_U16 = 3, truelight@0: SLE_FILE_I32 = 4, truelight@0: SLE_FILE_U32 = 5, truelight@0: SLE_FILE_I64 = 6, truelight@0: SLE_FILE_U64 = 7, truelight@0: truelight@0: SLE_FILE_STRINGID = 8, truelight@0: // SLE_FILE_IVAR = 8, truelight@0: // SLE_FILE_UVAR = 9, truelight@0: Darkvater@1881: SLE_VAR_I8 = 0 << 4, Darkvater@1881: SLE_VAR_U8 = 1 << 4, Darkvater@1881: SLE_VAR_I16 = 2 << 4, Darkvater@1881: SLE_VAR_U16 = 3 << 4, Darkvater@1881: SLE_VAR_I32 = 4 << 4, Darkvater@1881: SLE_VAR_U32 = 5 << 4, Darkvater@1881: SLE_VAR_I64 = 6 << 4, Darkvater@1881: SLE_VAR_U64 = 7 << 4, truelight@0: truelight@0: SLE_VAR_NULL = 8 << 4, // useful to write zeros in savegame. truelight@0: Darkvater@1881: SLE_VAR_INT = SLE_VAR_I32, truelight@0: SLE_VAR_UINT = SLE_VAR_U32, truelight@0: Darkvater@1881: SLE_INT8 = SLE_FILE_I8 | SLE_VAR_I8, Darkvater@1881: SLE_UINT8 = SLE_FILE_U8 | SLE_VAR_U8, Darkvater@1881: SLE_INT16 = SLE_FILE_I16 | SLE_VAR_I16, truelight@0: SLE_UINT16 = SLE_FILE_U16 | SLE_VAR_U16, Darkvater@1881: SLE_INT32 = SLE_FILE_I32 | SLE_VAR_I32, truelight@0: SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32, Darkvater@1881: SLE_INT64 = SLE_FILE_I64 | SLE_VAR_I64, truelight@0: SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64, truelight@0: truelight@0: SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U16, Darkvater@1881: } VarType; Darkvater@1881: Darkvater@1881: enum SaveLoadTypes { Darkvater@1881: SL_VAR = 0, Darkvater@1881: SL_REF = 1, Darkvater@1881: SL_ARR = 2, Darkvater@1881: SL_CONDVAR = 0 | (1 << 2), // 4 Darkvater@1881: SL_CONDREF = 1 | (1 << 2), // 5 Darkvater@1881: SL_CONDARR = 2 | (1 << 2), // 6 Darkvater@1881: // non-normal save-load types Darkvater@1881: SL_WRITEBYTE = 8, Darkvater@1881: SL_INCLUDE = 9, Darkvater@1881: SL_END = 15 truelight@0: }; truelight@0: Darkvater@1881: /** SaveLoad type struct. Do NOT use this directly but use the SLE_ macros defined just below! */ Darkvater@1881: struct SaveLoad { Darkvater@1881: byte cmd; /// the action to take with the saved/loaded type, All types need different action Darkvater@1881: VarType type; /// type of the variable to be saved, int Darkvater@1881: uint16 offset; /// offset of this variable in the struct (max offset is 65536) Darkvater@1881: uint16 length; /// (conditional) length of the variable (eg. arrays) (max array size is 65536 elements) Darkvater@1881: uint16 version_from; /// save/load the variable starting from this savegame version Darkvater@1881: uint16 version_to; /// save/load the variable until this savegame version Darkvater@1881: }; truelight@0: Darkvater@1881: /* Simple variables, references (pointers) and arrays */ Darkvater@1881: #define SLE_VAR(base, variable, type) {SL_VAR, type, offsetof(base, variable), 0, 0, 0} Darkvater@1881: #define SLE_REF(base, variable, type) {SL_REF, type, offsetof(base, variable), 0, 0, 0} Darkvater@1881: #define SLE_ARR(base, variable, type, length) {SL_ARR, type, offsetof(base, variable), length, 0, 0} Darkvater@1881: /* Conditional variables, references (pointers) and arrays that are only valid for certain savegame versions */ Darkvater@1881: #define SLE_CONDVAR(base, variable, type, from, to) {SL_CONDVAR, type, offsetof(base, variable), 0, from, to} Darkvater@1881: #define SLE_CONDREF(base, variable, type, from, to) {SL_CONDREF, type, offsetof(base, variable), 0, from, to} Darkvater@1881: #define SLE_CONDARR(base, variable, type, length, from, to) {SL_CONDARR, type, offsetof(base, variable), length, from, to} Darkvater@1881: /* Translate values ingame to different values in the savegame and vv */ Darkvater@1881: #define SLE_WRITEBYTE(base, variable, game_value, file_value) {SL_WRITEBYTE, 0, offsetof(base, variable), 0, game_value, file_value} Darkvater@1881: /* Load common code and put it into each struct (currently only for vehicles */ Darkvater@1881: #define SLE_INCLUDE(base, variable, include_index) {SL_INCLUDE, 0, offsetof(base, variable), 0, include_index, 0} truelight@0: Darkvater@1881: /* The same as the ones at the top, only the offset is given directly; used for unions */ Darkvater@1881: #define SLE_VARX(offset, type) {SL_VAR, type, offset, 0, 0, 0} Darkvater@1881: #define SLE_REFX(offset, type) {SL_REF, type, offset, 0, 0, 0} Darkvater@1881: #define SLE_CONDVARX(offset, type, from, to) {SL_CONDVAR, type, offset, 0, from, to} Darkvater@1881: #define SLE_CONDREFX(offset, type, from, to) {SL_CONDREF, type, offset, 0, from, to} Darkvater@1881: #define SLE_WRITEBYTEX(offset, something) {SL_WRITEBYTE, 0, offset, 0, something, 0} Darkvater@1881: #define SLE_INCLUDEX(offset, type) {SL_INCLUDE, type, offset, 0, 0, 0} truelight@0: Darkvater@1881: /* End marker */ Darkvater@1881: #define SLE_END() {SL_END, 0, 0, 0, 0, 0} truelight@0: truelight@0: void SlSetArrayIndex(uint index); tron@1093: int SlIterateArray(void); orudge@1887: void SlArray(void *array, uint length, VarType conv); Darkvater@1881: void SlObject(void *object, const SaveLoad *desc); truelight@0: void SlAutolength(AutolengthProc *proc, void *arg); tron@1093: uint SlGetFieldLength(void); tron@1093: int SlReadByte(void); Darkvater@1881: void SlSetLength(size_t length); Darkvater@1881: void SlWriteByte(byte b); truelight@0: void SlGlobList(const SaveLoadGlobVarList *desc); truelight@0: truelight@0: #endif /* SAVELOAD_H */