bitburner-src/src/Terminal/commands/mem.ts

55 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-09-16 01:50:44 +02:00
import { ITerminal } from "../ITerminal";
2021-09-17 08:58:02 +02:00
import { IRouter } from "../../ui/Router";
2021-09-16 01:50:44 +02:00
import { IPlayer } from "../../PersonObjects/IPlayer";
import { BaseServer } from "../../Server/BaseServer";
import { numeralWrapper } from "../../ui/numeralFormat";
export function mem(
terminal: ITerminal,
2021-09-17 08:58:02 +02:00
router: IRouter,
2021-09-16 01:50:44 +02:00
player: IPlayer,
server: BaseServer,
2021-12-03 20:44:32 +01:00
args: (string | number | boolean)[],
2021-09-16 01:50:44 +02:00
): void {
try {
if (args.length !== 1 && args.length !== 3) {
terminal.error("Incorrect usage of mem command. usage: mem [scriptname] [-t] [number threads]");
return;
}
const scriptName = args[0] + "";
let numThreads = 1;
if (args.length === 3 && args[1] === "-t") {
numThreads = Math.round(parseInt(args[2] + ""));
if (isNaN(numThreads) || numThreads < 1) {
terminal.error("Invalid number of threads specified. Number of threads must be greater than 1");
return;
}
}
const script = terminal.getScript(player, scriptName);
if (script == null) {
terminal.error("mem failed. No such script exists!");
2021-09-16 01:50:44 +02:00
return;
}
const ramUsage = script.ramUsage * numThreads;
terminal.print(
`This script requires ${numeralWrapper.formatRAM(ramUsage)} of RAM to run for ${numThreads} thread(s)`,
);
2022-01-05 22:41:48 +01:00
const verboseEntries = script.ramUsageEntries?.sort((a, b) => b.cost - a.cost) ?? [];
for (const entry of verboseEntries) {
2022-01-06 00:32:29 +01:00
terminal.print(`${numeralWrapper.formatRAM(entry.cost * numThreads).padStart(8)} | ${entry.name} (${entry.type})`);
2022-01-05 22:41:48 +01:00
}
if (ramUsage > 0 && verboseEntries.length === 0) {
// Let's warn the user that he might need to save his script again to generate the detailed entries
terminal.warn('You might have to open & save this script to see the detailed RAM usage information.');
}
2021-09-16 01:50:44 +02:00
} catch (e) {
terminal.error(e + "");
}
}