mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-11 02:03:58 +01:00
PoC for save validation on load via hooking the Reviver function and
static property validationData on classes PoC/example implemented with HacknetNode: validates cores is a number in the range between 1 and HacknetNodeConstants.MaxCores validates level is a number in range between 1 and HacknetNodeConstants.MaxLevel validates ram is a number in range between 1 and HacknetNodeConstants.MaxRam validates onlineTimeSeconds in non negative number validates totalMoneyGenerated is a non negative number
This commit is contained in:
parent
d8a5f5b03c
commit
d9064b608f
@ -18,8 +18,36 @@ import { HacknetNodeConstants } from "./data/Constants";
|
||||
|
||||
import { dialogBoxCreate } from "../ui/React/DialogBox";
|
||||
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../utils/JSONReviver";
|
||||
import { ObjectValidator } from "src/utils/Validator";
|
||||
|
||||
export class HacknetNode implements IHacknetNode {
|
||||
|
||||
static validationData: ObjectValidator<HacknetNode> = {
|
||||
cores: {
|
||||
default: 1,
|
||||
min: 1,
|
||||
max: HacknetNodeConstants.MaxCores
|
||||
},
|
||||
level: {
|
||||
default: 1,
|
||||
min: 1,
|
||||
max: HacknetNodeConstants.MaxLevel
|
||||
},
|
||||
ram: {
|
||||
default: 1,
|
||||
min: 1,
|
||||
max: HacknetNodeConstants.MaxRam
|
||||
},
|
||||
onlineTimeSeconds: {
|
||||
default: 0,
|
||||
min: 0
|
||||
},
|
||||
totalMoneyGenerated: {
|
||||
default: 0,
|
||||
min: 0
|
||||
}
|
||||
}
|
||||
|
||||
// Node's number of cores
|
||||
cores = 1;
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
/* Generic Reviver, toJSON, and fromJSON functions used for saving and loading objects */
|
||||
|
||||
import { validateObject } from "./Validator";
|
||||
|
||||
interface IReviverValue {
|
||||
ctor: string;
|
||||
data: any;
|
||||
@ -26,7 +28,11 @@ export function Reviver(key: string, value: IReviverValue | null): any {
|
||||
const ctor = Reviver.constructors[value.ctor];
|
||||
|
||||
if (typeof ctor === "function" && typeof ctor.fromJSON === "function") {
|
||||
return ctor.fromJSON(value);
|
||||
const obj = ctor.fromJSON(value);
|
||||
if (ctor.validationData !== undefined) {
|
||||
validateObject(obj, ctor.validationData);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
|
31
src/utils/Validator.ts
Normal file
31
src/utils/Validator.ts
Normal file
@ -0,0 +1,31 @@
|
||||
export type ObjectValidator<T> = {
|
||||
[key in keyof T]?: ParameterValidator<T, keyof T>;
|
||||
}
|
||||
|
||||
interface ParameterValidator<T, U extends keyof T> {
|
||||
default?: any;
|
||||
min?: number;
|
||||
max?: number;
|
||||
func?: (obj: T, validator: ObjectValidator<T>, key: U) => void;
|
||||
}
|
||||
|
||||
export function validateObject<T extends Record<string, unknown>, U extends keyof T>(obj: T, validator: ObjectValidator<T>): void {
|
||||
for (const key of Object.keys(validator) as U[]) {
|
||||
const paramValidator = validator[key];
|
||||
if (paramValidator !== undefined) {
|
||||
if (paramValidator.func !== undefined) {
|
||||
paramValidator.func(obj, validator, key);
|
||||
} else {
|
||||
if ((typeof obj[key]) !== (typeof paramValidator.default)) {
|
||||
obj[key] = paramValidator.default
|
||||
}
|
||||
if (typeof obj[key] === 'number' && paramValidator.min !== undefined) {
|
||||
if (obj[key] < paramValidator.min) obj[key] = paramValidator.min as T[U];
|
||||
}
|
||||
if (typeof obj[key] === 'number' && paramValidator.max !== undefined) {
|
||||
if (obj[key] > paramValidator.max) obj[key] = paramValidator.max as T[U];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user