bitburner-src/src/SaveObject.js

107 lines
3.7 KiB
JavaScript
Raw Normal View History

/* SaveObject.js
* Defines the object used to save/load games
*/
var saveObject = new BitburnerSaveObject();
function BitburnerSaveObject() {
this.PlayerSave = "";
this.AllServersSave = "";
this.CompaniesSave = "";
this.FactionsSave = "";
this.SpecialServerIpsSave = "";
this.AugmentationsSave = "";
2017-05-23 17:12:09 +02:00
this.AliasesSave = "";
this.MessagesSave = "";
this.VersionSave = "";
}
BitburnerSaveObject.prototype.saveGame = function() {
this.PlayerSave = JSON.stringify(Player);
this.AllServersSave = JSON.stringify(AllServers);
this.CompaniesSave = JSON.stringify(Companies);
this.FactionsSave = JSON.stringify(Factions);
this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps);
this.AugmentationsSave = JSON.stringify(Augmentations);
2017-05-23 17:12:09 +02:00
this.AliasesSave = JSON.stringify(Aliases);
this.MessagesSave = JSON.stringify(Messages);
this.VersionSave = JSON.stringify(CONSTANTS.Version);
var saveString = btoa(unescape(encodeURIComponent(JSON.stringify(this))));
window.localStorage.setItem("bitburnerSave", saveString);
2017-05-15 19:23:20 +02:00
console.log("Game saved!");
Engine.createStatusText("Game saved!");
}
loadGame = function(saveObj) {
if (!window.localStorage.getItem("bitburnerSave")) {
console.log("No save file to load");
return false;
}
var saveString = decodeURIComponent(escape(atob(window.localStorage.getItem("bitburnerSave"))));
saveObj = JSON.parse(saveString, Reviver);
Player = JSON.parse(saveObj.PlayerSave, Reviver);
AllServers = JSON.parse(saveObj.AllServersSave, Reviver);
Companies = JSON.parse(saveObj.CompaniesSave, Reviver);
Factions = JSON.parse(saveObj.FactionsSave, Reviver);
SpecialServerIps = JSON.parse(saveObj.SpecialServerIpsSave, Reviver);
Augmentations = JSON.parse(saveObj.AugmentationsSave, Reviver);
if (saveObj.hasOwnProperty("AliasesSave")) {
try {
Aliases = JSON.parse(saveObj.AliasesSave, Reviver);
} catch(e) {
Aliases = {};
}
} else {
Aliases = {};
}
if (saveObj.hasOwnProperty("MessagesSave")) {
try {
Messages = JSON.parse(saveObj.MessagesSave, Reviver);
} catch(e) {
initMessages();
}
} else {
initMessages();
}
if (saveObj.hasOwnProperty("VersionSave")) {
try {
var ver = JSON.parse(saveObj.VersionSave, Reviver);
if (ver != CONSTANTS.Version) {
createNewUpdateText();
}
} catch(e) {
createNewUpdateText();
}
} else {
createNewUpdateText();
}
return true;
}
BitburnerSaveObject.prototype.deleteGame = function() {
if (window.localStorage.getItem("bitburnerSave")) {
window.localStorage.removeItem("bitburnerSave");
}
Engine.createStatusText("Game deleted!");
}
createNewUpdateText = function() {
dialogBoxCreate("New update!<br>" +
"Please report any bugs/issues through the github repository " +
"or the Bitburner subreddit (reddit.com/r/bitburner).<br><br>" +
CONSTANTS.LatestUpdate);
}
BitburnerSaveObject.prototype.toJSON = function() {
return Generic_toJSON("BitburnerSaveObject", this);
}
BitburnerSaveObject.fromJSON = function(value) {
return Generic_fromJSON(BitburnerSaveObject, value.data);
}
Reviver.constructors.BitburnerSaveObject = BitburnerSaveObject;