1
0
Fork 0

(svn r16331) -Codechange: replace _sl.save by an enum

release/1.0
smatz 2009-05-17 11:15:11 +00:00
parent b23c967230
commit 3322cc978f
1 changed files with 180 additions and 149 deletions

View File

@ -52,9 +52,15 @@ char _savegame_format[8]; ///< how to compress savegames
typedef void WriterProc(size_t len); typedef void WriterProc(size_t len);
typedef size_t ReaderProc(); typedef size_t ReaderProc();
/** What are we currently doing? */
enum SaveLoadAction {
SLA_LOAD, ///< loading
SLA_SAVE, ///< saving
};
/** The saveload struct, containing reader-writer functions, bufffer, version, etc. */ /** The saveload struct, containing reader-writer functions, bufffer, version, etc. */
static struct { struct SaveLoadParams {
bool save; ///< are we doing a save or a load atm. True when saving SaveLoadAction action; ///< are we doing a save or a load atm.
byte need_length; ///< ??? byte need_length; ///< ???
byte block_mode; ///< ??? byte block_mode; ///< ???
bool error; ///< did an error occur or not bool error; ///< did an error occur or not
@ -82,7 +88,9 @@ static struct {
void (*excpt_uninit)(); ///< the function to execute on any encountered error void (*excpt_uninit)(); ///< the function to execute on any encountered error
StringID error_str; ///< the translateable error message to show StringID error_str; ///< the translateable error message to show
char *extra_msg; ///< the error message char *extra_msg; ///< the error message
} _sl; };
static SaveLoadParams _sl;
enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2}; enum NeedLengthValues {NL_NONE = 0, NL_WANTLENGTH = 1, NL_CALCLENGTH = 2};
@ -388,7 +396,7 @@ int SlIterateArray()
*/ */
void SlSetLength(size_t length) void SlSetLength(size_t length)
{ {
assert(_sl.save); assert(_sl.action == SLA_SAVE);
switch (_sl.need_length) { switch (_sl.need_length) {
case NL_WANTLENGTH: case NL_WANTLENGTH:
@ -429,10 +437,14 @@ static void SlCopyBytes(void *ptr, size_t length)
{ {
byte *p = (byte *)ptr; byte *p = (byte *)ptr;
if (_sl.save) { switch (_sl.action) {
for (; length != 0; length--) {SlWriteByteInternal(*p++);} case SLA_LOAD:
} else {
for (; length != 0; length--) { *p++ = SlReadByteInternal(); } for (; length != 0; length--) { *p++ = SlReadByteInternal(); }
break;
case SLA_SAVE:
for (; length != 0; length--) { SlWriteByteInternal(*p++); }
break;
default: NOT_REACHED();
} }
} }
@ -506,11 +518,9 @@ void WriteValue(void *ptr, VarType conv, int64 val)
*/ */
static void SlSaveLoadConv(void *ptr, VarType conv) static void SlSaveLoadConv(void *ptr, VarType conv)
{ {
int64 x = 0; switch (_sl.action) {
case SLA_SAVE: {
if (_sl.save) { // SAVE values int64 x = ReadValue(ptr, conv);
/* Read a value from the struct. These ARE endian safe. */
x = ReadValue(ptr, conv);
/* Write the value to the file and check if its value is in the desired range */ /* Write the value to the file and check if its value is in the desired range */
switch (GetVarFileType(conv)) { switch (GetVarFileType(conv)) {
@ -525,8 +535,10 @@ static void SlSaveLoadConv(void *ptr, VarType conv)
case SLE_FILE_U64: SlWriteUint64(x);break; case SLE_FILE_U64: SlWriteUint64(x);break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
} else { // LOAD values break;
}
case SLA_LOAD: {
int64 x;
/* Read a value from the file */ /* Read a value from the file */
switch (GetVarFileType(conv)) { switch (GetVarFileType(conv)) {
case SLE_FILE_I8: x = (int8 )SlReadByte(); break; case SLE_FILE_I8: x = (int8 )SlReadByte(); break;
@ -543,6 +555,9 @@ static void SlSaveLoadConv(void *ptr, VarType conv)
/* Write The value to the struct. These ARE endian safe. */ /* Write The value to the struct. These ARE endian safe. */
WriteValue(ptr, conv, x); WriteValue(ptr, conv, x);
break;
}
default: NOT_REACHED();
} }
} }
@ -596,9 +611,9 @@ static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType con
* @param conv must be SLE_FILE_STRING */ * @param conv must be SLE_FILE_STRING */
static void SlString(void *ptr, size_t length, VarType conv) static void SlString(void *ptr, size_t length, VarType conv)
{ {
switch (_sl.action) {
case SLA_SAVE: {
size_t len; size_t len;
if (_sl.save) { // SAVE string
switch (GetVarMemType(conv)) { switch (GetVarMemType(conv)) {
default: NOT_REACHED(); default: NOT_REACHED();
case SLE_VAR_STRB: case SLE_VAR_STRB:
@ -614,8 +629,10 @@ static void SlString(void *ptr, size_t length, VarType conv)
SlWriteArrayLength(len); SlWriteArrayLength(len);
SlCopyBytes(ptr, len); SlCopyBytes(ptr, len);
} else { // LOAD string break;
len = SlReadArrayLength(); }
case SLA_LOAD: {
size_t len = SlReadArrayLength();
switch (GetVarMemType(conv)) { switch (GetVarMemType(conv)) {
default: NOT_REACHED(); default: NOT_REACHED();
@ -645,6 +662,9 @@ static void SlString(void *ptr, size_t length, VarType conv)
((char *)ptr)[len] = '\0'; // properly terminate the string ((char *)ptr)[len] = '\0'; // properly terminate the string
str_validate((char *)ptr, (char *)ptr + len); str_validate((char *)ptr, (char *)ptr + len);
break;
}
default: NOT_REACHED();
} }
} }
@ -675,7 +695,7 @@ void SlArray(void *array, size_t length, VarType conv)
/* NOTICE - handle some buggy stuff, in really old versions everything was saved /* NOTICE - handle some buggy stuff, in really old versions everything was saved
* as a byte-type. So detect this, and adjust array size accordingly */ * as a byte-type. So detect this, and adjust array size accordingly */
if (!_sl.save && _sl_version == 0) { if (_sl.action != SLA_SAVE && _sl_version == 0) {
/* all arrays except difficulty settings */ /* all arrays except difficulty settings */
if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID || if (conv == SLE_INT16 || conv == SLE_UINT16 || conv == SLE_STRINGID ||
conv == SLE_INT32 || conv == SLE_UINT32) { conv == SLE_INT32 || conv == SLE_UINT32) {
@ -742,7 +762,8 @@ void SlList(void *list, SLRefType conv)
std::list<void *> *l = (std::list<void *> *) list; std::list<void *> *l = (std::list<void *> *) list;
if (_sl.save) { switch (_sl.action) {
case SLA_SAVE: {
SlWriteUint32((uint32)l->size()); SlWriteUint32((uint32)l->size());
std::list<void *>::iterator iter; std::list<void *>::iterator iter;
@ -750,7 +771,9 @@ void SlList(void *list, SLRefType conv)
void *ptr = *iter; void *ptr = *iter;
SlWriteUint32(ReferenceToInt(ptr, conv)); SlWriteUint32(ReferenceToInt(ptr, conv));
} }
} else { break;
}
case SLA_LOAD: {
uint length = CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(); uint length = CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32();
/* Load each reference and push to the end of the list */ /* Load each reference and push to the end of the list */
@ -758,6 +781,9 @@ void SlList(void *list, SLRefType conv)
void *ptr = IntToReference(CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(), conv); void *ptr = IntToReference(CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(), conv);
l->push_back(ptr); l->push_back(ptr);
} }
break;
}
default: NOT_REACHED();
} }
} }
@ -776,7 +802,7 @@ static inline bool SlIsObjectValidInSavegame(const SaveLoad *sld)
* bytestream itself as well, so there is no need to skip it somewhere else */ * bytestream itself as well, so there is no need to skip it somewhere else */
static inline bool SlSkipVariableOnLoad(const SaveLoad *sld) static inline bool SlSkipVariableOnLoad(const SaveLoad *sld)
{ {
if ((sld->conv & SLF_NETWORK_NO) && !_sl.save && _networking && !_network_server) { if ((sld->conv & SLF_NETWORK_NO) && _sl.action != SLA_SAVE && _networking && !_network_server) {
SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length); SlSkipBytes(SlCalcConvMemLen(sld->conv) * sld->length);
return true; return true;
} }
@ -803,7 +829,7 @@ size_t SlCalcObjLength(const void *object, const SaveLoad *sld)
size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld) size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
{ {
assert(_sl.save); assert(_sl.action == SLA_SAVE);
switch (sld->cmd) { switch (sld->cmd) {
case SL_VAR: case SL_VAR:
@ -847,10 +873,14 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld)
switch (sld->cmd) { switch (sld->cmd) {
case SL_VAR: SlSaveLoadConv(ptr, conv); break; case SL_VAR: SlSaveLoadConv(ptr, conv); break;
case SL_REF: // Reference variable, translate case SL_REF: // Reference variable, translate
if (_sl.save) { switch (_sl.action) {
case SLA_SAVE:
SlWriteUint32(ReferenceToInt(*(void **)ptr, (SLRefType)conv)); SlWriteUint32(ReferenceToInt(*(void **)ptr, (SLRefType)conv));
} else { break;
case SLA_LOAD:
*(void **)ptr = IntToReference(CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(), (SLRefType)conv); *(void **)ptr = IntToReference(CheckSavegameVersion(69) ? SlReadUint16() : SlReadUint32(), (SLRefType)conv);
break;
default: NOT_REACHED();
} }
break; break;
case SL_ARR: SlArray(ptr, sld->length, conv); break; case SL_ARR: SlArray(ptr, sld->length, conv); break;
@ -866,10 +896,10 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld)
* game_value: the value of the variable ingame is abused by sld->version_from * game_value: the value of the variable ingame is abused by sld->version_from
* file_value: the value of the variable in the savegame is abused by sld->version_to */ * file_value: the value of the variable in the savegame is abused by sld->version_to */
case SL_WRITEBYTE: case SL_WRITEBYTE:
if (_sl.save) { switch (_sl.action) {
SlWriteByte(sld->version_to); case SLA_SAVE: SlWriteByte(sld->version_to); break;
} else { case SLA_LOAD: *(byte *)ptr = sld->version_from; break;
*(byte*)ptr = sld->version_from; default: NOT_REACHED();
} }
break; break;
@ -877,6 +907,7 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld)
case SL_VEH_INCLUDE: case SL_VEH_INCLUDE:
SlObject(ptr, GetVehicleDescription(VEH_END)); SlObject(ptr, GetVehicleDescription(VEH_END));
break; break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
return true; return true;
@ -919,7 +950,7 @@ void SlAutolength(AutolengthProc *proc, void *arg)
{ {
size_t offs; size_t offs;
assert(_sl.save); assert(_sl.action == SL_SAVE);
/* Tell it to calculate the length */ /* Tell it to calculate the length */
_sl.need_length = NL_CALCLENGTH; _sl.need_length = NL_CALCLENGTH;
@ -1579,7 +1610,7 @@ const char *GetSaveLoadErrorString()
SetDParamStr(1, _sl.extra_msg); SetDParamStr(1, _sl.extra_msg);
static char err_str[512]; static char err_str[512];
GetString(err_str, _sl.save ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str)); GetString(err_str, _sl.action == SLA_SAVE ? STR_ERROR_GAME_SAVE_FAILED : STR_ERROR_GAME_LOAD_FAILED, lastof(err_str));
return err_str; return err_str;
} }
@ -1718,7 +1749,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb)
_sl.bufe = _sl.bufp = NULL; _sl.bufe = _sl.bufp = NULL;
_sl.offs_base = 0; _sl.offs_base = 0;
_sl.save = (mode != 0); _sl.action = (mode != 0) ? SLA_SAVE : SLA_LOAD;
_sl.chs = _chunk_handlers; _sl.chs = _chunk_handlers;
/* General tactic is to first save the game to memory, then use an available writer /* General tactic is to first save the game to memory, then use an available writer