Closed catloversg closed 3 days ago
In the latest commit, I refactored loadStaneksGift
. There is a behavior change:
saveString
is an empty string, we load default data; otherwise, we parse saveString
and pretend that everything is okay. If there is an error, the caller (loadGame
) will handle it with a try-catch.saveString
to JSON.parse in a try-catch. Validate the result. If it's invalid, we load default data, then show a dialog.Rationale:
saveString
can only be invalid (empty string or invalid data) if the save file is older than v0.54.0 or it's a badly edited one.From now on, I'll use this code for other "loader" functions if I need to change them (I'm testing another PR that changes loadAllGangs
):
let somethingData: unknown;
try {
somethingData = JSON.parse(saveString, Reviver);
} catch (error) {
console.error(error);
}
if (!validate(somethingData)) {
console.error("Invalid somethingData:", saveString);
somethingData = DefaultData;
setTimeout(() => {
dialogBoxCreate("Cannot load data of something. Something is reset.");
}, 1000);
return;
}
something = somethingData;
After #1774 is merged, with proper "loader" functions, we can gradually deal with legacy code in loadGame
in a safe way.
Unrelated question:
We have src\utils\helpers\typeAssertion.ts
and src\utils\TypeAssertion.ts
. Can I merge them later in another PR (src\utils\helpers\typeAssertion.ts
-> src\utils\TypeAssertion.ts
)?
Unrelated question: We have
src\utils\helpers\typeAssertion.ts
andsrc\utils\TypeAssertion.ts
. Can I merge them later in another PR (src\utils\helpers\typeAssertion.ts
->src\utils\TypeAssertion.ts
)?
Please do, that's just silly. Unless there are circular dependency issues, but those should be solved by breaking individual functions out, not having two of the same file.
In testing this change with the old saves I have, I ran into this issue. I don't think this is a problem with this change directly, but rather an earlier undetected regression:
Error: WorldDaemon is not a normal server. This is a bug. Please contact developers. (at "Loading Screen")
I confirmed that it has the same issue on dev
. So, an uncaught regression. This PR seems fine across all the test saves I have.
While we search and fix lint errors, we (kind of) agree that we should not typecast the result of
JSON.parse
(e.g.,JSON.parse(saveString, Reviver) as Something
. This PR searches all references ofJSON.parse
and checks if they are typecasting in a questionable way. My guideline:unknown
.