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

99 lines
3.2 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";
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";
2021-10-12 00:14:10 +02:00
import { numeralWrapper } from "../../ui/numeralFormat";
2021-09-16 01:50:44 +02:00
export function runScript(
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
commandArgs: (string | number | boolean)[],
2021-09-16 01:50:44 +02:00
): void {
if (commandArgs.length < 1) {
terminal.error(
`Bug encountered with Terminal.runScript(). Command array has a length of less than 1: ${commandArgs}`,
);
return;
}
const scriptName = terminal.getFilepath(commandArgs[0] + "");
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))) {
terminal.error("Invalid number of threads specified. Number of threads must be greater than 0");
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) {
terminal.error("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;
}
// Check for admin rights and that there is enough RAM availble to run
const script = server.scripts[i];
2021-12-14 21:56:11 +01:00
script.server = player.getCurrentServer().hostname;
2021-09-16 01:50:44 +02:00
const ramUsage = script.ramUsage * numThreads;
const ramAvailable = server.maxRam - server.ramUsed;
if (!server.hasAdminRights) {
2021-10-27 02:26:05 +02:00
terminal.error("Need root access to run script");
2021-09-16 01:50:44 +02:00
return;
}
if (ramUsage > ramAvailable) {
2021-10-27 02:26:05 +02:00
terminal.error(
2021-09-16 01:50:44 +02:00
"This machine does not have enough RAM to run this script with " +
numThreads +
" threads. Script requires " +
2021-10-12 00:14:10 +02:00
numeralWrapper.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-01-05 01:09:34 +01:00
const success = startWorkerScript(player, runningScript, server);
2021-09-16 01:50:44 +02:00
if (!success) {
terminal.error(`Failed to start script`);
return;
}
terminal.print(
`Running script with ${numThreads} thread(s), pid ${runningScript.pid} and args: ${JSON.stringify(args)}.`,
);
2022-01-09 21:22:23 +01:00
if (runningScript.filename.endsWith(".ns")) {
terminal.warn(".ns files are deprecated, please rename everything to .js");
}
2021-09-16 01:50:44 +02:00
if (tailFlag) {
2021-10-01 07:00:50 +02:00
LogBoxEvents.emit(runningScript);
2021-09-16 01:50:44 +02:00
}
return;
}
2021-10-27 02:26:05 +02:00
terminal.error("No such script");
2021-09-16 01:50:44 +02:00
}