mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-19 15:54:09 +02:00
Refactored Code to make Save/Load work. This included re-structuring classes so that they contained only native objects/arrays of native objects and using a Reviver function in the JSON.parse. Cleaned up some code that was no longer needed
This commit is contained in:
+15
-13
@@ -6,18 +6,19 @@
|
||||
// constructor that has a `fromJSON` property on it, it hands
|
||||
// off to that `fromJSON` fuunction, passing in the value.
|
||||
function Reviver(key, value) {
|
||||
var ctor;
|
||||
|
||||
if (typeof value === "object" &&
|
||||
typeof value.ctor === "string" &&
|
||||
typeof value.data !== "undefined") {
|
||||
ctor = Reviver.constructors[value.ctor] || window[value.ctor];
|
||||
if (typeof ctor === "function" &&
|
||||
typeof ctor.fromJSON === "function") {
|
||||
return ctor.fromJSON(value);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
var ctor;
|
||||
//console.log("Reviver called with key: " + key + ", and value: " + value);
|
||||
if (typeof value === "object" &&
|
||||
typeof value.ctor === "string" &&
|
||||
typeof value.data !== "undefined") {
|
||||
ctor = Reviver.constructors[value.ctor] || window[value.ctor];
|
||||
if (typeof ctor === "function" &&
|
||||
typeof ctor.fromJSON === "function") {
|
||||
|
||||
return ctor.fromJSON(value);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Reviver.constructors = {}; // A list of constructors the smart reviver should know about
|
||||
|
||||
@@ -35,6 +36,7 @@ Reviver.constructors = {}; // A list of constructors the smart reviver should kn
|
||||
function Generic_toJSON(ctorName, obj, keys) {
|
||||
var data, index, key;
|
||||
|
||||
console.log("Generic_toJSON() called");
|
||||
if (!keys) {
|
||||
keys = Object.keys(obj); // Only "own" properties are included
|
||||
}
|
||||
@@ -62,4 +64,4 @@ function Generic_fromJSON(ctor, data) {
|
||||
obj[name] = data[name];
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user