REFACTOR: Handle migration of ActionIdentifier in Bladeburner code instead of GenericReviver (#1799)

This commit is contained in:
catloversg 2024-11-27 16:22:23 +07:00 committed by GitHub
parent 3846c69204
commit 22b6458c22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

@ -53,6 +53,7 @@ import { resolveTeamCasualties, type OperationTeam } from "./Actions/TeamCasualt
import { shuffleArray } from "../Infiltration/ui/BribeGame";
import { objectAssert } from "../utils/helpers/typeAssertion";
import { throwIfReachable } from "../utils/helpers/throwIfReachable";
import { loadActionIdentifier } from "./utils/loadActionIdentifier";
export const BladeburnerPromise: PromisePair<number> = { promise: null, resolve: null };
@ -1427,6 +1428,25 @@ export class Bladeburner implements OperationTeam {
const contractsData = value.data.contracts;
const operationsData = value.data.operations;
const bladeburner = Generic_fromJSON(Bladeburner, value.data, Bladeburner.keysToLoad);
/**
* Handle migration from pre-v2.6.1 versions:
* - pre-v2.6.1:
* - action is an instance of the ActionIdentifier class. It cannot be null.
* - action.type is a number.
* - 2.6.1:
* - action is a nullable plain object. ActionIdentifier is a "type".
* - action.type is a string.
*/
if (bladeburner.action && typeof bladeburner.action.type === "number") {
bladeburner.action = loadActionIdentifier(bladeburner.action);
if (bladeburner.automateActionHigh) {
bladeburner.automateActionHigh = loadActionIdentifier(bladeburner.automateActionHigh);
}
if (bladeburner.automateActionLow) {
bladeburner.automateActionLow = loadActionIdentifier(bladeburner.automateActionLow);
}
}
// Loading this way allows better typesafety and also allows faithfully reconstructing contracts/operations
// even from save data that is missing a lot of static info about the objects.
loadContractsData(contractsData, bladeburner.contracts);

@ -1,4 +1,3 @@
import { loadActionIdentifier } from "../Bladeburner/utils/loadActionIdentifier";
import { constructorsForReviver, isReviverValue } from "./JSONReviver";
import { validateObject } from "./Validator";
@ -21,10 +20,9 @@ export function Reviver(_key: string, value: unknown): any {
case "Employee": // Entire object removed from game in v2.2.0 (employees abstracted)
case "Company": // Reviver removed in v2.6.1
case "Faction": // Reviver removed in v2.6.1
case "ActionIdentifier": // No longer a class as of v2.6.1
console.warn(`Legacy load type ${value.ctor} converted to expected format while loading.`);
return value.data;
case "ActionIdentifier": // No longer a class as of v2.6.1
return loadActionIdentifier(value.data);
}
// Missing constructor with no special handling. Throw error.
throw new Error(`Could not locate constructor named ${value.ctor}. If the save data is valid, this is a bug.`);