BLADEBURNER: Typesafety / refactoring (#1154)

This commit is contained in:
Snarling
2024-03-28 21:52:37 -04:00
committed by GitHub
parent 5f1a94a9d3
commit 6669c4da6a
79 changed files with 3876 additions and 5462 deletions
+5 -8
View File
@@ -1,17 +1,16 @@
/* Generic Reviver, toJSON, and fromJSON functions used for saving and loading objects */
import type { Unknownify } from "../types";
import { ObjectValidator, validateObject } from "./Validator";
import { JSONMap, JSONSet } from "../Types/Jsonable";
import { loadActionIdentifier } from "../Bladeburner/utils/loadActionIdentifier";
type JsonableClass = (new () => { toJSON: () => IReviverValue }) & {
fromJSON: (value: IReviverValue) => any;
validationData?: ObjectValidator<any>;
};
export interface IReviverValue {
export interface IReviverValue<T = any> {
ctor: string;
data: any;
data: T;
}
function isReviverValue(value: unknown): value is IReviverValue {
return (
@@ -37,6 +36,8 @@ export function Reviver(_key: string, value: unknown): any {
case "Faction": // Reviver removed in 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.`);
@@ -102,7 +103,3 @@ export function Generic_fromJSON<T extends Record<string, any>>(
for (const [key, val] of Object.entries(data) as [keyof T, T[keyof T]][]) obj[key] = val;
return obj;
}
// This function is empty because Unknownify<T> is a typesafe assertion on any object with no runtime checks needed.
// eslint-disable-next-line @typescript-eslint/no-empty-function
export function assertLoadingType<T extends object>(val: object): asserts val is Unknownify<T> {}