mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-05-02 13:57:05 +02:00
Unify error handling
This commit is contained in:
@@ -13,7 +13,6 @@ import { RamCalculationErrorCode } from "./RamCalculationErrorCodes";
|
||||
import { RamCosts, RamCostConstants } from "../Netscript/RamCostGenerator";
|
||||
import { Script } from "./Script";
|
||||
import { areImportsEquals } from "../Terminal/DirectoryHelpers";
|
||||
import { IPlayer } from "../PersonObjects/IPlayer";
|
||||
import { Node } from "../NetscriptJSEvaluator";
|
||||
|
||||
export interface RamUsageEntry {
|
||||
@@ -40,11 +39,8 @@ const memCheckGlobalKey = ".__GLOBAL__";
|
||||
* Parses code into an AST and walks through it recursively to calculate
|
||||
* RAM usage. Also accounts for imported modules.
|
||||
* @param {Script[]} otherScripts - All other scripts on the server. Used to account for imported scripts
|
||||
* @param {string} codeCopy - The code being parsed
|
||||
* @param {WorkerScript} workerScript - Object containing RAM costs of Netscript functions. Also used to
|
||||
* keep track of what functions have/havent been accounted for
|
||||
*/
|
||||
function parseOnlyRamCalculate(player: IPlayer, otherScripts: Script[], code: string): RamCalculation {
|
||||
* @param {string} code - The code being parsed */
|
||||
function parseOnlyRamCalculate(otherScripts: Script[], code: string): RamCalculation {
|
||||
try {
|
||||
/**
|
||||
* Maps dependent identifiers to their dependencies.
|
||||
@@ -157,11 +153,11 @@ function parseOnlyRamCalculate(player: IPlayer, otherScripts: Script[], code: st
|
||||
// Check if this identifier is a function in the workerScript environment.
|
||||
// If it is, then we need to get its RAM cost.
|
||||
try {
|
||||
function applyFuncRam(cost: number | ((p: IPlayer) => number)): number {
|
||||
function applyFuncRam(cost: number | (() => number)): number {
|
||||
if (typeof cost === "number") {
|
||||
return cost;
|
||||
} else if (typeof cost === "function") {
|
||||
return cost(player);
|
||||
return cost();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
@@ -178,7 +174,7 @@ function parseOnlyRamCalculate(player: IPlayer, otherScripts: Script[], code: st
|
||||
prefix: string,
|
||||
obj: object,
|
||||
ref: string,
|
||||
): { func: (p: IPlayer) => number | number; refDetail: string } | undefined => {
|
||||
): { func: () => number | number; refDetail: string } | undefined => {
|
||||
if (!obj) return;
|
||||
const elem = Object.entries(obj).find(([key]) => key === ref);
|
||||
if (elem !== undefined && (typeof elem[1] === "function" || typeof elem[1] === "number")) {
|
||||
@@ -381,9 +377,9 @@ function parseOnlyCalculateDeps(code: string, currentModule: string): ParseDepsR
|
||||
* @param {Script[]} otherScripts - All other scripts on the server.
|
||||
* Used to account for imported scripts
|
||||
*/
|
||||
export function calculateRamUsage(player: IPlayer, codeCopy: string, otherScripts: Script[]): RamCalculation {
|
||||
export function calculateRamUsage(codeCopy: string, otherScripts: Script[]): RamCalculation {
|
||||
try {
|
||||
return parseOnlyRamCalculate(player, otherScripts, codeCopy);
|
||||
return parseOnlyRamCalculate(otherScripts, codeCopy);
|
||||
} catch (e) {
|
||||
console.error(`Failed to parse script for RAM calculations:`);
|
||||
console.error(e);
|
||||
|
||||
@@ -9,7 +9,6 @@ import { ScriptUrl } from "./ScriptUrl";
|
||||
|
||||
import { Generic_fromJSON, Generic_toJSON, IReviverValue, Reviver } from "../utils/JSONReviver";
|
||||
import { roundToTwo } from "../utils/helpers/roundToTwo";
|
||||
import { IPlayer } from "../PersonObjects/IPlayer";
|
||||
import { ScriptModule } from "./ScriptModule";
|
||||
|
||||
let globalModuleSequenceNumber = 0;
|
||||
@@ -52,13 +51,13 @@ export class Script {
|
||||
// hostname of server that this script is on.
|
||||
server = "";
|
||||
|
||||
constructor(player: IPlayer | null = null, fn = "", code = "", server = "", otherScripts: Script[] = []) {
|
||||
constructor(fn = "", code = "", server = "", otherScripts: Script[] = []) {
|
||||
this.filename = fn;
|
||||
this.code = code;
|
||||
this.server = server; // hostname of server this script is on
|
||||
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
|
||||
if (this.code !== "" && player !== null) {
|
||||
this.updateRamUsage(player, otherScripts);
|
||||
if (this.code !== "") {
|
||||
this.updateRamUsage(otherScripts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,13 +93,13 @@ export class Script {
|
||||
* @param {string} code - The new contents of the script
|
||||
* @param {Script[]} otherScripts - Other scripts on the server. Used to process imports
|
||||
*/
|
||||
saveScript(player: IPlayer, filename: string, code: string, hostname: string, otherScripts: Script[]): void {
|
||||
saveScript(filename: string, code: string, hostname: string, otherScripts: Script[]): void {
|
||||
// Update code and filename
|
||||
this.code = Script.formatCode(code);
|
||||
|
||||
this.filename = filename;
|
||||
this.server = hostname;
|
||||
this.updateRamUsage(player, otherScripts);
|
||||
this.updateRamUsage(otherScripts);
|
||||
this.markUpdated();
|
||||
for (const dependent of this.dependents) {
|
||||
const [dependentScript] = otherScripts.filter(
|
||||
@@ -114,8 +113,8 @@ export class Script {
|
||||
* Calculates and updates the script's RAM usage based on its code
|
||||
* @param {Script[]} otherScripts - Other scripts on the server. Used to process imports
|
||||
*/
|
||||
updateRamUsage(player: IPlayer, otherScripts: Script[]): void {
|
||||
const res = calculateRamUsage(player, this.code, otherScripts);
|
||||
updateRamUsage(otherScripts: Script[]): void {
|
||||
const res = calculateRamUsage(this.code, otherScripts);
|
||||
if (res.cost > 0) {
|
||||
this.ramUsage = roundToTwo(res.cost);
|
||||
this.ramUsageEntries = res.entries;
|
||||
|
||||
Reference in New Issue
Block a user