mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2025-01-05 12:57:35 +01:00
FIX: show ram calculation error reason to player (#627)
This commit is contained in:
parent
9e75621cd2
commit
08e3afd125
@ -22,10 +22,21 @@ export interface RamUsageEntry {
|
||||
cost: number;
|
||||
}
|
||||
|
||||
export interface RamCalculation {
|
||||
export type RamCalculationSuccess = {
|
||||
cost: number;
|
||||
entries?: RamUsageEntry[];
|
||||
}
|
||||
entries: RamUsageEntry[];
|
||||
errorCode?: never;
|
||||
errorMessage?: never;
|
||||
};
|
||||
|
||||
export type RamCalculationFailure = {
|
||||
cost?: never;
|
||||
entries?: never;
|
||||
errorCode: RamCalculationErrorCode;
|
||||
errorMessage?: string;
|
||||
};
|
||||
|
||||
export type RamCalculation = RamCalculationSuccess | RamCalculationFailure;
|
||||
|
||||
// These special strings are used to reference the presence of a given logical
|
||||
// construct within a user script.
|
||||
@ -80,7 +91,6 @@ function parseOnlyRamCalculate(otherScripts: Map<ScriptFilePath, Script>, code:
|
||||
dependencyMap = Object.assign(dependencyMap, result.dependencyMap);
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse the initial module, which is the "main" script that is being run
|
||||
const initialModule = "__SPECIAL_INITIAL_MODULE__";
|
||||
parseCode(code, initialModule);
|
||||
@ -93,9 +103,13 @@ function parseOnlyRamCalculate(otherScripts: Map<ScriptFilePath, Script>, code:
|
||||
|
||||
// Using root as the path base right now. Difficult to implement
|
||||
const filename = resolveScriptFilePath(nextModule, root, ns1 ? ".script" : ".js");
|
||||
if (!filename) return { cost: RamCalculationErrorCode.ImportError }; // Invalid import path
|
||||
if (!filename) {
|
||||
return { errorCode: RamCalculationErrorCode.ImportError, errorMessage: `Invalid import path: "${nextModule}"` };
|
||||
}
|
||||
const script = otherScripts.get(filename);
|
||||
if (!script) return { cost: RamCalculationErrorCode.ImportError }; // No such file on server
|
||||
if (!script) {
|
||||
return { errorCode: RamCalculationErrorCode.ImportError, errorMessage: `No such file on server: "${filename}"` };
|
||||
}
|
||||
|
||||
parseCode(script.code, nextModule);
|
||||
}
|
||||
@ -183,12 +197,6 @@ function parseOnlyRamCalculate(otherScripts: Map<ScriptFilePath, Script>, code:
|
||||
detailedCosts.push({ type: "misc", name: "Max Ram Cap", cost: RamCostConstants.Max });
|
||||
}
|
||||
return { cost: ram, entries: detailedCosts.filter((e) => e.cost > 0) };
|
||||
} catch (error) {
|
||||
// console.info("parse or eval error: ", error);
|
||||
// This is not unexpected. The user may be editing a script, and it may be in
|
||||
// a transitory invalid state.
|
||||
return { cost: RamCalculationErrorCode.SyntaxError };
|
||||
}
|
||||
}
|
||||
|
||||
export function checkInfiniteLoop(code: string): number {
|
||||
@ -362,20 +370,21 @@ function parseOnlyCalculateDeps(code: string, currentModule: string): ParseDepsR
|
||||
|
||||
/**
|
||||
* Calculate's a scripts RAM Usage
|
||||
* @param {string} codeCopy - The script's code
|
||||
* @param {string} code - The script's code
|
||||
* @param {Script[]} otherScripts - All other scripts on the server.
|
||||
* Used to account for imported scripts
|
||||
*/
|
||||
export function calculateRamUsage(
|
||||
codeCopy: string,
|
||||
code: string,
|
||||
otherScripts: Map<ScriptFilePath, Script>,
|
||||
ns1?: boolean,
|
||||
): RamCalculation {
|
||||
try {
|
||||
return parseOnlyRamCalculate(otherScripts, codeCopy, ns1);
|
||||
return parseOnlyRamCalculate(otherScripts, code, ns1);
|
||||
} catch (e) {
|
||||
console.error(`Failed to parse script for RAM calculations:`);
|
||||
console.error(e);
|
||||
return { cost: RamCalculationErrorCode.SyntaxError };
|
||||
return {
|
||||
errorCode: RamCalculationErrorCode.SyntaxError,
|
||||
errorMessage: e instanceof Error ? e.message : undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ export class Script implements ContentFile {
|
||||
// Ram calculation, only exists after first poll of ram cost after updating
|
||||
ramUsage: number | null = null;
|
||||
ramUsageEntries: RamUsageEntry[] = [];
|
||||
ramCalculationError: string | null = null;
|
||||
|
||||
// Runtime data that only exists when the script has been initiated. Cleared when script or a dependency script is updated.
|
||||
mod: LoadedModule | null = null;
|
||||
@ -65,6 +66,7 @@ export class Script implements ContentFile {
|
||||
// Always clear ram usage
|
||||
this.ramUsage = null;
|
||||
this.ramUsageEntries.length = 0;
|
||||
this.ramCalculationError = null;
|
||||
// Early return if there's already no URL
|
||||
if (!this.mod) return;
|
||||
this.mod = null;
|
||||
@ -88,12 +90,15 @@ export class Script implements ContentFile {
|
||||
*/
|
||||
updateRamUsage(otherScripts: Map<ScriptFilePath, Script>): void {
|
||||
const ramCalc = calculateRamUsage(this.code, otherScripts, this.filename.endsWith(".script"));
|
||||
if (ramCalc.cost >= RamCostConstants.Base) {
|
||||
if (ramCalc.cost && ramCalc.cost >= RamCostConstants.Base) {
|
||||
this.ramUsage = roundToTwo(ramCalc.cost);
|
||||
this.ramUsageEntries = ramCalc.entries as RamUsageEntry[];
|
||||
} else {
|
||||
this.ramUsage = null;
|
||||
this.ramCalculationError = null;
|
||||
return;
|
||||
}
|
||||
|
||||
this.ramUsage = null;
|
||||
this.ramCalculationError = ramCalc.errorMessage ?? null;
|
||||
}
|
||||
|
||||
/** Remove script from server. Fails if the provided server isn't the server for this script. */
|
||||
|
@ -34,9 +34,9 @@ export function ScriptEditorContextProvider({ children, vim }: { children: React
|
||||
setRamEntries([["N/A", ""]]);
|
||||
return;
|
||||
}
|
||||
const codeCopy = newCode + "";
|
||||
const ramUsage = calculateRamUsage(codeCopy, server.scripts);
|
||||
if (ramUsage.cost > 0) {
|
||||
|
||||
const ramUsage = calculateRamUsage(newCode, server.scripts);
|
||||
if (ramUsage.cost && ramUsage.cost > 0) {
|
||||
const entries = ramUsage.entries?.sort((a, b) => b.cost - a.cost) ?? [];
|
||||
const entriesDisp = [];
|
||||
for (const entry of entries) {
|
||||
@ -48,23 +48,20 @@ export function ScriptEditorContextProvider({ children, vim }: { children: React
|
||||
return;
|
||||
}
|
||||
|
||||
let RAM = "";
|
||||
const entriesDisp = [];
|
||||
switch (ramUsage.cost) {
|
||||
case RamCalculationErrorCode.ImportError: {
|
||||
RAM = "RAM: Import Error";
|
||||
entriesDisp.push(["Import Error", ""]);
|
||||
if (ramUsage.errorCode !== undefined) {
|
||||
setRamEntries([["Syntax Error", ramUsage.errorMessage ?? ""]]);
|
||||
switch (ramUsage.errorCode) {
|
||||
case RamCalculationErrorCode.ImportError:
|
||||
setRAM("RAM: Import Error");
|
||||
break;
|
||||
}
|
||||
case RamCalculationErrorCode.SyntaxError:
|
||||
default: {
|
||||
RAM = "RAM: Syntax Error";
|
||||
entriesDisp.push(["Syntax Error", ""]);
|
||||
setRAM("RAM: Syntax Error");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
setRAM("RAM: Syntax Error");
|
||||
setRamEntries([["Syntax Error", ""]]);
|
||||
}
|
||||
setRAM(RAM);
|
||||
setRamEntries(entriesDisp);
|
||||
};
|
||||
|
||||
const [isUpdatingRAM, { on: startUpdatingRAM, off: finishUpdatingRAM }] = useBoolean(false);
|
||||
|
@ -24,18 +24,19 @@ export function runScript(path: ScriptFilePath, commandArgs: (string | number |
|
||||
if (!isPositiveInteger(numThreads)) {
|
||||
return Terminal.error("Invalid number of threads specified. Number of threads must be an integer greater than 0");
|
||||
}
|
||||
if (!server.hasAdminRights) return Terminal.error("Need root access to run script");
|
||||
|
||||
// Todo: Switch out arg for something with typescript support
|
||||
const args = flags._ as ScriptArg[];
|
||||
|
||||
const singleRamUsage = script.getRamUsage(server.scripts);
|
||||
if (!singleRamUsage) return Terminal.error("Error while calculating ram usage for this script.");
|
||||
if (!singleRamUsage) {
|
||||
return Terminal.error(`Error while calculating ram usage for this script. ${script.ramCalculationError}`);
|
||||
}
|
||||
|
||||
const ramUsage = singleRamUsage * numThreads;
|
||||
const ramAvailable = server.maxRam - server.ramUsed;
|
||||
|
||||
if (!server.hasAdminRights) return Terminal.error("Need root access to run script");
|
||||
|
||||
if (ramUsage > ramAvailable + 0.001) {
|
||||
return Terminal.error(
|
||||
"This machine does not have enough RAM to run this script" +
|
||||
|
Loading…
Reference in New Issue
Block a user