Fixed the error handling on loading sfallgv.sav

* to improve backward compatibility with older saved games.
This commit is contained in:
NovaRain
2019-07-15 20:49:55 +08:00
parent e127be29e8
commit b33277c35a
6 changed files with 28 additions and 22 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
;sfall configuration settings
;v4.2.0
;v4.2
[Main]
;Change to 1 if you want to use command line args to tell sfall to use another ini file.
+1 -1
View File
@@ -45,7 +45,7 @@ void BugFixes::DrugsSaveFix(HANDLE file) {
bool BugFixes::DrugsLoadFix(HANDLE file) {
DWORD count, sizeRead;
ReadFile(file, &count, 4, &sizeRead, 0);
//if (sizeRead != 4) return true;
if (sizeRead != 4) return false;
for (DWORD i = 0; i < count; i++) {
DWORD pid;
ReadFile(file, &pid, 4, &sizeRead, 0);
+6 -3
View File
@@ -229,8 +229,10 @@ static bool LoadGame_Before() {
if (uID > UniqueID::Start) Objects::uniqueID = uID;
ReadFile(h, &data, 4, &size, 0);
Worldmap::SetAddedYears(data >> 16);
if (size != 4 || !Perks::load(h) || script::LoadArrays(h)) goto errorLoad;
if (BugFixes::DrugsLoadFix(h)) goto errorLoad;
if (size != 4 || !Perks::load(h)) goto errorLoad;
long result = script::LoadArrays(h); // 1 - old save, -1 - broken save
if (result == -1) goto errorLoad;
if (!result && BugFixes::DrugsLoadFix(h)) goto errorLoad;
CloseHandle(h);
} else {
dlogr("Cannot open sfallgv.sav - assuming non-sfall save.", DL_MAIN);
@@ -245,12 +247,13 @@ static bool LoadGame_Before() {
} else {
dlogr("Cannot open sfalldb.sav.", DL_MAIN);
}
return false;
/////////////////////////////////////////////////
errorLoad:
CloseHandle(h);
dlog_f("ERROR reading data: %s\n", DL_MAIN, buf);
fo::func::debug_printf("\n[SFALL] ERROR reading data: %s", buf);
return (true & !isDebug);
}
+18 -15
View File
@@ -183,12 +183,13 @@ bool LoadArrayElement(sArrayElement* el, HANDLE h)
return (el->len) ? (unused != el->len) : (unused != 4);
}
static bool LoadArraysOld(HANDLE h) {
dlogr("Loading arrays (old fmt)", DL_MAIN);
static long LoadArraysOld(HANDLE h) {
DWORD count, unused, id;
ReadFile(h, &count, 4, &unused, 0); // count of saved arrays
if (unused != 4) return true;
if (unused != 4) return -1;
if (!count) return 0;
dlogr("Loading arrays (old fmt)", DL_MAIN);
sArrayVarOld var;
sArrayVar varN;
@@ -196,7 +197,7 @@ static bool LoadArraysOld(HANDLE h) {
for (DWORD i = 0; i < count; i++) {
ReadFile(h, &id, 4, &unused, 0);
ReadFile(h, &var, 8, &unused, 0);
if (unused != 8) return true;
if (unused != 8) return -1;
var.types = new DWORD[var.len];
var.data = new char[var.len * var.datalen];
@@ -226,23 +227,25 @@ static bool LoadArraysOld(HANDLE h) {
arrays.insert(array_pair(id, varN));
savedArrays[varN.key] = id;
}
return false;
return 1;
}
bool LoadArrays(HANDLE h) {
long LoadArrays(HANDLE h) {
nextArrayID = 1;
if (LoadArraysOld(h)) return true;
dlogr("Loading arrays (new fmt)", DL_MAIN);
long result = LoadArraysOld(h);
if (result) return result;
DWORD count, unused, elCount;
ReadFile(h, &count, 4, &unused, 0); // count of saved arrays
if (unused != 4) return true;
if (unused != 4 && !result) return 1;
dlogr("Loading arrays (new fmt)", DL_MAIN);
if (unused != 4) return -1;
sArrayVar arrayVar;
for (DWORD i = 0; i < count; i++) {
if (LoadArrayElement(&arrayVar.key, h)) return true;
if (LoadArrayElement(&arrayVar.key, h)) return -1;
if (arrayVar.key.intVal == 0 || static_cast<long>(arrayVar.key.type) >= 4) { // partial compatibility with 3.4
arrayVar.key.intVal = static_cast<long>(arrayVar.key.type);
arrayVar.key.type = DataType::INT;
@@ -250,12 +253,12 @@ bool LoadArrays(HANDLE h) {
ReadFile(h, &arrayVar.flags, 4, &unused, 0);
ReadFile(h, &elCount, 4, &unused, 0); // actual number of elements: keys+values
if (unused != 4) return true;
if (unused != 4) return -1;
bool isAssoc = arrayVar.isAssoc();
arrayVar.val.resize(elCount);
for (size_t j = 0; j < elCount; j++) { // normal and associative arrays stored and loaded equally
if (LoadArrayElement(&arrayVar.val[j], h)) return true;
if (LoadArrayElement(&arrayVar.val[j], h)) return -1;
if (isAssoc && (j % 2) == 0) { // only difference is that keyHash is filled with appropriate indexes
arrayVar.keyHash[arrayVar.val[j]] = j;
}
@@ -267,7 +270,7 @@ bool LoadArrays(HANDLE h) {
savedArrays[arrayVar.key] = nextArrayID++;
arrayVar.keyHash.clear();
}
return false;
return 0;
}
void SaveArrays(HANDLE h) {
+1 -1
View File
@@ -151,7 +151,7 @@ extern DWORD arraysBehavior;
// saved arrays: arrayKey => arrayId
extern ArrayKeysMap savedArrays;
bool LoadArrays(HANDLE h);
long LoadArrays(HANDLE h);
void SaveArrays(HANDLE h);
int GetNumArrays();
void GetArrays(int* arrays);
+1 -1
View File
@@ -27,7 +27,7 @@
#define VERSION_BUILD 0
#define VERSION_REV 0
#define VERSION_STRING "4.2.0"
#define VERSION_STRING "4.2"
//#define CHECK_VAL (4)