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

88 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-09-06 15:07:12 +02:00
import { Terminal } from "../../Terminal";
2021-09-16 01:50:44 +02:00
import { BaseServer } from "../../Server/BaseServer";
2021-10-01 07:00:50 +02:00
import { LogBoxEvents } from "../../ui/React/LogBoxManager";
2021-09-16 01:50:44 +02:00
import { startWorkerScript } from "../../NetscriptWorker";
import { RunningScript } from "../../Script/RunningScript";
import { findRunningScript } from "../../Script/ScriptHelpers";
import * as libarg from "arg";
import { formatRam } from "../../ui/formatNumber";
2021-09-16 01:50:44 +02:00
2022-09-13 18:37:24 +02:00
export function runScript(commandArgs: (string | number | boolean)[], server: BaseServer): void {
2021-09-16 01:50:44 +02:00
if (commandArgs.length < 1) {
2022-09-06 15:07:12 +02:00
Terminal.error(
2021-09-16 01:50:44 +02:00
`Bug encountered with Terminal.runScript(). Command array has a length of less than 1: ${commandArgs}`,
);
return;
}
2022-09-06 15:07:12 +02:00
const scriptName = Terminal.getFilepath(commandArgs[0] + "");
2021-09-16 01:50:44 +02:00
const runArgs = { "--tail": Boolean, "-t": Number };
const flags = libarg(runArgs, {
permissive: true,
argv: commandArgs.slice(1),
});
const threadFlag = Math.round(parseFloat(flags["-t"]));
const tailFlag = flags["--tail"] === true;
if (flags["-t"] !== undefined && (threadFlag < 0 || isNaN(threadFlag))) {
2022-09-06 15:07:12 +02:00
Terminal.error("Invalid number of threads specified. Number of threads must be greater than 0");
2021-09-16 01:50:44 +02:00
return;
}
const numThreads = !isNaN(threadFlag) && threadFlag > 0 ? threadFlag : 1;
const args = flags["_"];
// Check if this script is already running
if (findRunningScript(scriptName, args, server) != null) {
2022-09-06 15:07:12 +02:00
Terminal.error(
2022-04-07 01:30:08 +02:00
"This script is already running with the same args. Cannot run multiple instances with the same args",
);
2021-09-16 01:50:44 +02:00
return;
}
// Check if the script exists and if it does run it
for (let i = 0; i < server.scripts.length; i++) {
if (server.scripts[i].filename !== scriptName) {
continue;
}
2022-10-09 07:25:31 +02:00
// Check for admin rights and that there is enough RAM available to run
2021-09-16 01:50:44 +02:00
const script = server.scripts[i];
2022-09-06 15:07:12 +02:00
script.server = server.hostname;
2021-09-16 01:50:44 +02:00
const ramUsage = script.ramUsage * numThreads;
const ramAvailable = server.maxRam - server.ramUsed;
if (!server.hasAdminRights) {
2022-09-06 15:07:12 +02:00
Terminal.error("Need root access to run script");
2021-09-16 01:50:44 +02:00
return;
}
if (ramUsage > ramAvailable + 0.001) {
2022-09-06 15:07:12 +02:00
Terminal.error(
"This machine does not have enough RAM to run this script" +
(numThreads === 1 ? "" : ` with ${numThreads} threads`) +
`. Script requires ${formatRam(ramUsage)} of RAM`,
2021-09-16 01:50:44 +02:00
);
return;
}
// Able to run script
const runningScript = new RunningScript(script, args);
runningScript.threads = numThreads;
2022-08-28 02:56:12 +02:00
const success = startWorkerScript(runningScript, server);
2021-09-16 01:50:44 +02:00
if (!success) {
2022-09-06 15:07:12 +02:00
Terminal.error(`Failed to start script`);
2021-09-16 01:50:44 +02:00
return;
}
2022-09-06 15:07:12 +02:00
Terminal.print(
2021-09-16 01:50:44 +02:00
`Running script with ${numThreads} thread(s), pid ${runningScript.pid} and args: ${JSON.stringify(args)}.`,
);
if (tailFlag) {
2021-10-01 07:00:50 +02:00
LogBoxEvents.emit(runningScript);
2021-09-16 01:50:44 +02:00
}
return;
}
2022-09-06 15:07:12 +02:00
Terminal.error("No such script");
2021-09-16 01:50:44 +02:00
}