From 22b6458c22c145130e60f066719757812e1051cf Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:22:23 +0700 Subject: [PATCH] REFACTOR: Handle migration of ActionIdentifier in Bladeburner code instead of GenericReviver (#1799) --- src/Bladeburner/Bladeburner.ts | 20 ++++++++++++++++++++ src/utils/GenericReviver.ts | 4 +--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Bladeburner/Bladeburner.ts b/src/Bladeburner/Bladeburner.ts index 35e5267fa..d3ee85820 100644 --- a/src/Bladeburner/Bladeburner.ts +++ b/src/Bladeburner/Bladeburner.ts @@ -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 = { 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); diff --git a/src/utils/GenericReviver.ts b/src/utils/GenericReviver.ts index 5f4cbd6aa..a69e792a6 100644 --- a/src/utils/GenericReviver.ts +++ b/src/utils/GenericReviver.ts @@ -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.`);