BUGFIX: Prevent crash that could occur with server named runningScripts (#502)

This commit is contained in:
Snarling 2023-05-03 20:19:56 -04:00 committed by GitHub
parent 003be33886
commit d25254caf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 18 deletions

@ -5,7 +5,6 @@ import { SnackbarEvents, ToastVariant } from "./ui/React/Snackbar";
import { IReturnStatus } from "./types"; import { IReturnStatus } from "./types";
import { GetServer } from "./Server/AllServers"; import { GetServer } from "./Server/AllServers";
import { ImportPlayerData, SaveData, saveObject } from "./SaveObject"; import { ImportPlayerData, SaveData, saveObject } from "./SaveObject";
import { Settings } from "./Settings/Settings";
import { exportScripts } from "./Terminal/commands/download"; import { exportScripts } from "./Terminal/commands/download";
import { CONSTANTS } from "./Constants"; import { CONSTANTS } from "./Constants";
import { hash } from "./hash/hash"; import { hash } from "./hash/hash";
@ -133,7 +132,7 @@ function initSaveFunctions(): void {
triggerScriptsExport: (): void => exportScripts("*", Player.getHomeComputer()), triggerScriptsExport: (): void => exportScripts("*", Player.getHomeComputer()),
getSaveData: (): { save: string; fileName: string } => { getSaveData: (): { save: string; fileName: string } => {
return { return {
save: saveObject.getSaveString(Settings.ExcludeRunningScriptsFromSave), save: saveObject.getSaveString(),
fileName: saveObject.getSaveFileName(), fileName: saveObject.getSaveFileName(),
}; };
}, },

@ -88,10 +88,15 @@ class BitburnerSaveObject {
LastExportBonus = ""; LastExportBonus = "";
StaneksGiftSave = ""; StaneksGiftSave = "";
getSaveString(excludeRunningScripts = false): string { getSaveString(forceExcludeRunningScripts = false): string {
this.PlayerSave = JSON.stringify(Player); this.PlayerSave = JSON.stringify(Player);
this.AllServersSave = saveAllServers(excludeRunningScripts); // For the servers save, overwrite the ExcludeRunningScripts setting if forced
const originalExcludeSetting = Settings.ExcludeRunningScriptsFromSave;
if (forceExcludeRunningScripts) Settings.ExcludeRunningScriptsFromSave = true;
this.AllServersSave = saveAllServers();
Settings.ExcludeRunningScriptsFromSave = originalExcludeSetting;
this.CompaniesSave = JSON.stringify(Companies); this.CompaniesSave = JSON.stringify(Companies);
this.FactionsSave = JSON.stringify(Factions); this.FactionsSave = JSON.stringify(Factions);
this.AliasesSave = JSON.stringify(Aliases); this.AliasesSave = JSON.stringify(Aliases);
@ -105,14 +110,13 @@ class BitburnerSaveObject {
if (Player.gang) this.AllGangsSave = JSON.stringify(AllGangs); if (Player.gang) this.AllGangsSave = JSON.stringify(AllGangs);
const saveString = btoa(unescape(encodeURIComponent(JSON.stringify(this)))); const saveString = btoa(unescape(encodeURIComponent(JSON.stringify(this))));
return saveString; return saveString;
} }
saveGame(emitToastEvent = true): Promise<void> { saveGame(emitToastEvent = true): Promise<void> {
const savedOn = new Date().getTime(); const savedOn = new Date().getTime();
Player.lastSave = savedOn; Player.lastSave = savedOn;
const saveString = this.getSaveString(Settings.ExcludeRunningScriptsFromSave); const saveString = this.getSaveString();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
save(saveString) save(saveString)
.then(() => { .then(() => {
@ -146,7 +150,7 @@ class BitburnerSaveObject {
} }
exportGame(): void { exportGame(): void {
const saveString = this.getSaveString(Settings.ExcludeRunningScriptsFromSave); const saveString = this.getSaveString();
const filename = this.getSaveFileName(); const filename = this.getSaveFileName();
download(filename, saveString); download(filename, saveString);
} }

@ -199,13 +199,6 @@ export function loadAllServers(saveString: string): void {
AllServers = JSON.parse(saveString, Reviver); AllServers = JSON.parse(saveString, Reviver);
} }
function excludeReplacer(key: string, value: any): any { export function saveAllServers(): string {
if (key === "runningScripts") { return JSON.stringify(AllServers);
return [];
}
return value;
}
export function saveAllServers(excludeRunningScripts = false): string {
return JSON.stringify(AllServers, excludeRunningScripts ? excludeReplacer : undefined);
} }

@ -21,6 +21,7 @@ import { LiteratureName } from "src/Literature/data/LiteratureNames";
import { CompletedProgramName } from "src/Programs/Programs"; import { CompletedProgramName } from "src/Programs/Programs";
import { getKeyList } from "../utils/helpers/getKeyList"; import { getKeyList } from "../utils/helpers/getKeyList";
import lodash from "lodash"; import lodash from "lodash";
import { Settings } from "../Settings/Settings";
import type { ScriptKey } from "../utils/helpers/scriptKey"; import type { ScriptKey } from "../utils/helpers/scriptKey";
@ -292,6 +293,10 @@ export abstract class BaseServer implements IServer {
// RunningScripts are stored as a simple array, both for backward compatibility, // RunningScripts are stored as a simple array, both for backward compatibility,
// compactness, and ease of filtering them here. // compactness, and ease of filtering them here.
const result = Generic_toJSON(ctorName, this, keys); const result = Generic_toJSON(ctorName, this, keys);
if (Settings.ExcludeRunningScriptsFromSave) {
result.data.runningScripts = [];
return result;
}
const rsArray: RunningScript[] = []; const rsArray: RunningScript[] = [];
for (const byPid of this.runningScriptMap.values()) { for (const byPid of this.runningScriptMap.values()) {

@ -2,6 +2,7 @@ import "../../src/Player";
import { loadAllServers, saveAllServers } from "../../src/Server/AllServers"; import { loadAllServers, saveAllServers } from "../../src/Server/AllServers";
import { loadAllRunningScripts } from "../../src/NetscriptWorker"; import { loadAllRunningScripts } from "../../src/NetscriptWorker";
import { Settings } from "../../src/Settings/Settings";
jest.useFakeTimers(); jest.useFakeTimers();
@ -152,7 +153,7 @@ test("load/saveAllServers", () => {
loadStandardServers(); loadStandardServers();
// Re-stringify with indenting for nicer diffs // Re-stringify with indenting for nicer diffs
const result = saveAllServers(/*excludeRunningScripts=*/ false); const result = saveAllServers();
expect(JSON.stringify(JSON.parse(result), null, 2)).toMatchSnapshot(); expect(JSON.stringify(JSON.parse(result), null, 2)).toMatchSnapshot();
}); });
@ -163,6 +164,7 @@ test("load/saveAllServers pruning RunningScripts", () => {
loadStandardServers(); loadStandardServers();
// Re-stringify with indenting for nicer diffs // Re-stringify with indenting for nicer diffs
const result = saveAllServers(/*excludeRunningScripts=*/ true); Settings.ExcludeRunningScriptsFromSave = true;
const result = saveAllServers();
expect(JSON.stringify(JSON.parse(result), null, 2)).toMatchSnapshot(); expect(JSON.stringify(JSON.parse(result), null, 2)).toMatchSnapshot();
}); });