mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-23 09:42:53 +02:00
BLADEBURNER: Typesafety / refactoring (#1154)
This commit is contained in:
@@ -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> {}
|
||||
|
||||
Reference in New Issue
Block a user