From d77d704d27e2d86be77c39d687bd1af17dbe05f2 Mon Sep 17 00:00:00 2001 From: Darkvater Date: Mon, 20 Feb 2006 19:58:46 +0000 Subject: (svn r3628) - Add BOOL and STRING types to saveload capabilities. String is unused up till now and it saves the full-buffer, regardless of how many characters the string actually has. So give a warning for that and figure it out later. --- saveload.c | 29 ++++++++++++++++++++++++++++- saveload.h | 33 ++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/saveload.c b/saveload.c index 5401e0ace..0eb33e1fd 100644 --- a/saveload.c +++ b/saveload.c @@ -102,7 +102,7 @@ static inline uint32 SlGetOffs(void) {return _sl.offs_base - (_sl.bufe - _sl.buf * @return Return the size of this type in bytes */ static inline byte SlCalcConvMemLen(VarType conv) { - static const byte conv_mem_size[] = {1, 1, 2, 2, 4, 4, 8, 8, 0}; + static const byte conv_mem_size[] = {1, 1, 1, 2, 2, 4, 4, 8, 8, 0}; byte length = (conv >> 4) & 0xF; assert(length < lengthof(conv_mem_size)); return conv_mem_size[length]; @@ -413,6 +413,7 @@ static void SlSaveLoadConv(void *ptr, VarType conv) if (_sl.save) { /* SAVE values */ /* Read a value from the struct. These ARE endian safe. */ switch ((conv >> 4) & 0xF) { + case SLE_VAR_BL >> 4: case SLE_VAR_I8 >> 4: x = *(int8*)ptr; break; case SLE_VAR_U8 >> 4: x = *(byte*)ptr; break; case SLE_VAR_I16 >> 4: x = *(int16*)ptr; break; @@ -454,6 +455,7 @@ static void SlSaveLoadConv(void *ptr, VarType conv) /* Write The value to the struct. These ARE endian safe. */ switch ((conv >> 4) & 0xF) { + case SLE_VAR_BL >> 4: case SLE_VAR_I8 >> 4: *(int8*)ptr = x; break; case SLE_VAR_U8 >> 4: *(byte*)ptr = x; break; case SLE_VAR_I16 >> 4: *(int16*)ptr = x; break; @@ -468,6 +470,27 @@ static void SlSaveLoadConv(void *ptr, VarType conv) } } +static inline size_t SlCalcStringLen(const char *ptr, uint length) +{ + DEBUG(misc, 1) ("[Sl] TODO: save/load real length"); + return length;//(_sl.save) ? min(strlen(ptr), length - 1) : SlReadArrayLength(); +} + +/** + * Save/Load a string. + * @param ptr the string being manipulated + * @param the length of the string (full length) + * @param conv must be SLE_FILE_STRING + * @todo the full length of the string is saved, even when the buffer + * is 512 bytes and only 10 of those are used */ +static void SlString(void *ptr, uint length, VarType conv) +{ + uint len = SlCalcStringLen(ptr, length); + assert((conv & 0xF) == SLE_FILE_STRING); + + SlCopyBytes(ptr, len); +} + /** * Return the size in bytes of a certain type of atomic array * @param length The length of the array counted in elements @@ -549,6 +572,7 @@ size_t SlCalcObjMemberLength(const SaveLoad *sld) case SL_VAR: case SL_REF: case SL_ARR: + case SL_STR: /* CONDITIONAL saveload types depend on the savegame version */ if (!SlIsObjectValidInSavegame(sld)) break; @@ -556,6 +580,7 @@ size_t SlCalcObjMemberLength(const SaveLoad *sld) case SL_VAR: return SlCalcConvFileLen(sld->conv); case SL_REF: return SlCalcRefLen(); case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv); + case SL_STR: return SlCalcStringLen(sld->s.address, sld->length); default: NOT_REACHED(); } break; @@ -573,6 +598,7 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld) case SL_VAR: case SL_REF: case SL_ARR: + case SL_STR: /* CONDITIONAL saveload types depend on the savegame version */ if (!SlIsObjectValidInSavegame(sld)) return false; @@ -586,6 +612,7 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld) *(void**)ptr = _sl.int_to_ref_proc(SlReadUint16(), conv); break; case SL_ARR: SlArray(ptr, sld->length, conv); break; + case SL_STR: SlString(ptr, sld->length, conv); break; default: NOT_REACHED(); } break; diff --git a/saveload.h b/saveload.h index f2f04248d..2f9c28b5c 100644 --- a/saveload.h +++ b/saveload.h @@ -77,19 +77,23 @@ enum VarTypes { SLE_FILE_I64 = 6, SLE_FILE_U64 = 7, SLE_FILE_STRINGID = 8, /// StringID offset into strings-array - /* 7 more possible primitives */ + SLE_FILE_STRING = 9, + /* 6 more possible primitives */ /* 4 bytes allocated a maximum of 16 types for NumberType */ - SLE_VAR_I8 = 0 << 4, - SLE_VAR_U8 = 1 << 4, - SLE_VAR_I16 = 2 << 4, - SLE_VAR_U16 = 3 << 4, - SLE_VAR_I32 = 4 << 4, - SLE_VAR_U32 = 5 << 4, - SLE_VAR_I64 = 6 << 4, - SLE_VAR_U64 = 7 << 4, - SLE_VAR_NULL = 8 << 4, /// useful to write zeros in savegame. - /* 7 more possible primitives */ + SLE_VAR_BL = 0 << 4, + SLE_VAR_I8 = 1 << 4, + SLE_VAR_U8 = 2 << 4, + SLE_VAR_I16 = 3 << 4, + SLE_VAR_U16 = 4 << 4, + SLE_VAR_I32 = 5 << 4, + SLE_VAR_U32 = 6 << 4, + SLE_VAR_I64 = 7 << 4, + SLE_VAR_U64 = 8 << 4, + SLE_VAR_NULL = 9 << 4, /// useful to write zeros in savegame. + SLE_VAR_STRB = 10 << 4, /// normal string (with pre-allocated buffer) + SLE_VAR_STRQ = 11 << 4, /// string enclosed in parentheses + /* 4 more possible primitives */ /* Shortcut values */ SLE_VAR_CHAR = SLE_VAR_I8, @@ -97,6 +101,7 @@ enum VarTypes { /* Default combinations of variables. As savegames change, so can variables * and thus it is possible that the saved value and internal size do not * match and you need to specify custom combo. The defaults are listed here */ + SLE_BOOL = SLE_FILE_I8 | SLE_VAR_BL, SLE_INT8 = SLE_FILE_I8 | SLE_VAR_I8, SLE_UINT8 = SLE_FILE_U8 | SLE_VAR_U8, SLE_INT16 = SLE_FILE_I16 | SLE_VAR_I16, @@ -105,11 +110,16 @@ enum VarTypes { SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32, SLE_INT64 = SLE_FILE_I64 | SLE_VAR_I64, SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64, + SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR, SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U16, + SLE_STRINGBUF = SLE_FILE_STRING | SLE_VAR_STRB, + SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ, /* Shortcut values */ SLE_UINT = SLE_UINT32, SLE_INT = SLE_INT32, + SLE_STRB = SLE_STRINGBUF, + SLE_STRQ = SLE_STRINGQUOTE, }; typedef uint32 VarType; @@ -118,6 +128,7 @@ enum SaveLoadTypes { SL_VAR = 0, SL_REF = 1, SL_ARR = 2, + SL_STR = 3, // non-normal save-load types SL_WRITEBYTE = 8, SL_INCLUDE = 9, -- cgit v1.2.3-70-g09d2