forked from bitburner-official/bitburner-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTOR: Mitigate cyclic dependency between Jsonable classes (bitbur…
- Loading branch information
1 parent
45a6ca6
commit 05da0ef
Showing
23 changed files
with
121 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { basicErrorMessage } from "../Netscript/ErrorMessages"; | ||
import { ScriptDeath } from "../Netscript/ScriptDeath"; | ||
import type { WorkerScript } from "../Netscript/WorkerScript"; | ||
import { dialogBoxCreate } from "../ui/React/DialogBox"; | ||
|
||
/** Generate an error dialog when workerscript is known */ | ||
export function handleUnknownError(e: unknown, ws: WorkerScript | null = null, initialText = "") { | ||
if (e instanceof ScriptDeath) { | ||
// No dialog for ScriptDeath | ||
return; | ||
} | ||
if (ws && typeof e === "string") { | ||
const headerText = basicErrorMessage(ws, "", ""); | ||
if (!e.includes(headerText)) e = basicErrorMessage(ws, e); | ||
} else if (e instanceof SyntaxError) { | ||
const msg = `${e.message} (sorry we can't be more helpful)`; | ||
e = ws ? basicErrorMessage(ws, msg, "SYNTAX") : `SYNTAX ERROR:\n\n${msg}`; | ||
} else if (e instanceof Error) { | ||
// Ignore any cancellation errors from Monaco that get here | ||
if (e.name === "Canceled" && e.message === "Canceled") return; | ||
const msg = `${e.message}${e.stack ? `\nstack:\n${e.stack.toString()}` : ""}`; | ||
e = ws ? basicErrorMessage(ws, msg) : `RUNTIME ERROR:\n\n${msg}`; | ||
} | ||
if (typeof e !== "string") { | ||
console.error("Unexpected error:", e); | ||
const msg = `Unexpected type of error thrown. This error was likely thrown manually within a script. | ||
Error has been logged to the console.\n\nType of error: ${typeof e}\nValue of error: ${e}`; | ||
e = ws ? basicErrorMessage(ws, msg, "UNKNOWN") : msg; | ||
} | ||
dialogBoxCreate(initialText + String(e)); | ||
} | ||
|
||
/** Use this handler to handle the error when we call getSaveData function or getSaveInfo function */ | ||
export function handleGetSaveDataInfoError(error: unknown, fromGetSaveInfo = false) { | ||
console.error(error); | ||
let errorMessage = `Cannot get save ${fromGetSaveInfo ? "info" : "data"}. Error: ${error}.`; | ||
if (error instanceof RangeError) { | ||
errorMessage += " This may be because the save data is too large."; | ||
} | ||
if (error instanceof Error && error.stack) { | ||
errorMessage += `\nStack:\n${error.stack}`; | ||
} | ||
dialogBoxCreate(errorMessage); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { loadActionIdentifier } from "../Bladeburner/utils/loadActionIdentifier"; | ||
import { constructorsForReviver, isReviverValue } from "./JSONReviver"; | ||
import { validateObject } from "./Validator"; | ||
|
||
/** | ||
* A generic "smart reviver" function. | ||
* Looks for object values with a `ctor` property and a `data` property. | ||
* If it finds them, and finds a matching constructor, it hands | ||
* off to that `fromJSON` function, passing in the value. */ | ||
export function Reviver(_key: string, value: unknown): any { | ||
if (!isReviverValue(value)) { | ||
return value; | ||
} | ||
const ctor = constructorsForReviver[value.ctor]; | ||
if (!ctor) { | ||
// Known missing constructors with special handling. | ||
switch (value.ctor) { | ||
case "AllServersMap": // Reviver removed in v0.43.1 | ||
case "Industry": // No longer part of save data since v2.3.0 | ||
case "Employee": // Entire object removed from game in v2.2.0 (employees abstracted) | ||
case "Company": // Reviver removed in v2.6.1 | ||
case "Faction": // Reviver removed in v2.6.1 | ||
console.warn(`Legacy load type ${value.ctor} converted to expected format while loading.`); | ||
return value.data; | ||
case "ActionIdentifier": // No longer a class as of v2.6.1 | ||
return loadActionIdentifier(value.data); | ||
} | ||
// Missing constructor with no special handling. Throw error. | ||
throw new Error(`Could not locate constructor named ${value.ctor}. If the save data is valid, this is a bug.`); | ||
} | ||
|
||
const obj = ctor.fromJSON(value); | ||
if (ctor.validationData !== undefined) { | ||
validateObject(obj, ctor.validationData); | ||
} | ||
return obj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Hashes the input string. This is a fast hash, so NOT good for cryptography. | ||
* This has been ripped off here: https://stackoverflow.com/a/52171480 | ||
* @param str The string that is to be hashed | ||
* @param seed A seed to randomize the result | ||
* @returns An hexadecimal string representation of the hashed input | ||
*/ | ||
export function cyrb53(str: string, seed = 0): string { | ||
let h1 = 0xdeadbeef ^ seed; | ||
let h2 = 0x41c6ce57 ^ seed; | ||
for (let i = 0, ch; i < str.length; i++) { | ||
ch = str.charCodeAt(i); | ||
h1 = Math.imul(h1 ^ ch, 2654435761); | ||
h2 = Math.imul(h2 ^ ch, 1597334677); | ||
} | ||
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909); | ||
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909); | ||
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(16); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters