mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-18 07:18:38 +02: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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user