Saveload: Remove test pass from ScriptInstance::Save
Use SlConditionallySave to discard the saved data instead if there was an error
This commit is contained in:
@@ -378,7 +378,7 @@ ScriptLogTypes::LogData &ScriptInstance::GetLogData()
|
|||||||
* - null: No data.
|
* - null: No data.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* static */ bool ScriptInstance::SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test)
|
/* static */ bool ScriptInstance::SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth)
|
||||||
{
|
{
|
||||||
if (max_depth == 0) {
|
if (max_depth == 0) {
|
||||||
ScriptLog::Error("Savedata can only be nested to 25 deep. No data saved."); // SQUIRREL_MAX_DEPTH = 25
|
ScriptLog::Error("Savedata can only be nested to 25 deep. No data saved."); // SQUIRREL_MAX_DEPTH = 25
|
||||||
@@ -387,22 +387,16 @@ ScriptLogTypes::LogData &ScriptInstance::GetLogData()
|
|||||||
|
|
||||||
switch (sq_gettype(vm, index)) {
|
switch (sq_gettype(vm, index)) {
|
||||||
case OT_INTEGER: {
|
case OT_INTEGER: {
|
||||||
if (!test) {
|
SlWriteByte(SQSL_INT);
|
||||||
SlWriteByte(SQSL_INT);
|
|
||||||
}
|
|
||||||
SQInteger res;
|
SQInteger res;
|
||||||
sq_getinteger(vm, index, &res);
|
sq_getinteger(vm, index, &res);
|
||||||
if (!test) {
|
int64 value = (int64)res;
|
||||||
int64 value = (int64)res;
|
SlArray(&value, 1, SLE_INT64);
|
||||||
SlArray(&value, 1, SLE_INT64);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OT_STRING: {
|
case OT_STRING: {
|
||||||
if (!test) {
|
SlWriteByte(SQSL_STRING);
|
||||||
SlWriteByte(SQSL_STRING);
|
|
||||||
}
|
|
||||||
const SQChar *buf;
|
const SQChar *buf;
|
||||||
sq_getstring(vm, index, &buf);
|
sq_getstring(vm, index, &buf);
|
||||||
size_t len = strlen(buf) + 1;
|
size_t len = strlen(buf) + 1;
|
||||||
@@ -410,21 +404,17 @@ ScriptLogTypes::LogData &ScriptInstance::GetLogData()
|
|||||||
ScriptLog::Error("Maximum string length is 254 chars. No data saved.");
|
ScriptLog::Error("Maximum string length is 254 chars. No data saved.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!test) {
|
SlWriteByte((byte)len);
|
||||||
SlWriteByte((byte)len);
|
SlArray(const_cast<char *>(buf), len, SLE_CHAR);
|
||||||
SlArray(const_cast<char *>(buf), len, SLE_CHAR);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OT_ARRAY: {
|
case OT_ARRAY: {
|
||||||
if (!test) {
|
SlWriteByte(SQSL_ARRAY);
|
||||||
SlWriteByte(SQSL_ARRAY);
|
|
||||||
}
|
|
||||||
sq_pushnull(vm);
|
sq_pushnull(vm);
|
||||||
while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
|
while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
|
||||||
/* Store the value */
|
/* Store the value */
|
||||||
bool res = SaveObject(vm, -1, max_depth - 1, test);
|
bool res = SaveObject(vm, -1, max_depth - 1);
|
||||||
sq_pop(vm, 2);
|
sq_pop(vm, 2);
|
||||||
if (!res) {
|
if (!res) {
|
||||||
sq_pop(vm, 1);
|
sq_pop(vm, 1);
|
||||||
@@ -432,20 +422,16 @@ ScriptLogTypes::LogData &ScriptInstance::GetLogData()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sq_pop(vm, 1);
|
sq_pop(vm, 1);
|
||||||
if (!test) {
|
SlWriteByte(SQSL_ARRAY_TABLE_END);
|
||||||
SlWriteByte(SQSL_ARRAY_TABLE_END);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OT_TABLE: {
|
case OT_TABLE: {
|
||||||
if (!test) {
|
SlWriteByte(SQSL_TABLE);
|
||||||
SlWriteByte(SQSL_TABLE);
|
|
||||||
}
|
|
||||||
sq_pushnull(vm);
|
sq_pushnull(vm);
|
||||||
while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
|
while (SQ_SUCCEEDED(sq_next(vm, index - 1))) {
|
||||||
/* Store the key + value */
|
/* Store the key + value */
|
||||||
bool res = SaveObject(vm, -2, max_depth - 1, test) && SaveObject(vm, -1, max_depth - 1, test);
|
bool res = SaveObject(vm, -2, max_depth - 1) && SaveObject(vm, -1, max_depth - 1);
|
||||||
sq_pop(vm, 2);
|
sq_pop(vm, 2);
|
||||||
if (!res) {
|
if (!res) {
|
||||||
sq_pop(vm, 1);
|
sq_pop(vm, 1);
|
||||||
@@ -453,28 +439,20 @@ ScriptLogTypes::LogData &ScriptInstance::GetLogData()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sq_pop(vm, 1);
|
sq_pop(vm, 1);
|
||||||
if (!test) {
|
SlWriteByte(SQSL_ARRAY_TABLE_END);
|
||||||
SlWriteByte(SQSL_ARRAY_TABLE_END);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OT_BOOL: {
|
case OT_BOOL: {
|
||||||
if (!test) {
|
SlWriteByte(SQSL_BOOL);
|
||||||
SlWriteByte(SQSL_BOOL);
|
|
||||||
}
|
|
||||||
SQBool res;
|
SQBool res;
|
||||||
sq_getbool(vm, index, &res);
|
sq_getbool(vm, index, &res);
|
||||||
if (!test) {
|
SlWriteByte(res ? 1 : 0);
|
||||||
SlWriteByte(res ? 1 : 0);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case OT_NULL: {
|
case OT_NULL: {
|
||||||
if (!test) {
|
SlWriteByte(SQSL_NULL);
|
||||||
SlWriteByte(SQSL_NULL);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -503,7 +481,7 @@ void ScriptInstance::Save()
|
|||||||
if (this->is_save_data_on_stack) {
|
if (this->is_save_data_on_stack) {
|
||||||
SlWriteByte(1);
|
SlWriteByte(1);
|
||||||
/* Save the data that was just loaded. */
|
/* Save the data that was just loaded. */
|
||||||
SaveObject(vm, -1, SQUIRREL_MAX_DEPTH, false);
|
SaveObject(vm, -1, SQUIRREL_MAX_DEPTH);
|
||||||
} else if (!this->is_started) {
|
} else if (!this->is_started) {
|
||||||
SaveEmpty();
|
SaveEmpty();
|
||||||
return;
|
return;
|
||||||
@@ -542,9 +520,11 @@ void ScriptInstance::Save()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sq_pushobject(vm, savedata);
|
sq_pushobject(vm, savedata);
|
||||||
if (SaveObject(vm, -1, SQUIRREL_MAX_DEPTH, true)) {
|
bool saved = SlConditionallySave([&]() {
|
||||||
SlWriteByte(1);
|
SlWriteByte(1);
|
||||||
SaveObject(vm, -1, SQUIRREL_MAX_DEPTH, false);
|
return SaveObject(vm, -1, SQUIRREL_MAX_DEPTH);
|
||||||
|
});
|
||||||
|
if (saved) {
|
||||||
this->is_save_data_on_stack = true;
|
this->is_save_data_on_stack = true;
|
||||||
} else {
|
} else {
|
||||||
SaveEmpty();
|
SaveEmpty();
|
||||||
|
@@ -315,11 +315,9 @@ private:
|
|||||||
* @param index The index on the squirrel stack of the element to save.
|
* @param index The index on the squirrel stack of the element to save.
|
||||||
* @param max_depth The maximum depth recursive arrays / tables will be stored
|
* @param max_depth The maximum depth recursive arrays / tables will be stored
|
||||||
* with before an error is returned.
|
* with before an error is returned.
|
||||||
* @param test If true, don't really store the data but only check if it is
|
|
||||||
* valid.
|
|
||||||
* @return True if the saving was successful.
|
* @return True if the saving was successful.
|
||||||
*/
|
*/
|
||||||
static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
|
static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load all objects from a savegame.
|
* Load all objects from a savegame.
|
||||||
|
Reference in New Issue
Block a user