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

51 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-09-06 15:07:12 +02:00
import { Terminal } from "../../Terminal";
import { findRunningScripts } from "../../Script/ScriptHelpers";
import { killWorkerScriptByPid } from "../../Netscript/killWorkerScript";
FILES: Path rework & typesafety (#479) * Added new types for various file paths, all in the Paths folder. * TypeSafety and other helper functions related to these types * Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands * Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way * Server.textFiles is now a map * TextFile no longer uses a fn property, now it is filename * Added a shared ContentFile interface for shared functionality between TextFile and Script. * related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa. * File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root. * Singularized the MessageFilename and LiteratureName enums * Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath). * Fix several issues with tab completion, which included pretty much a complete rewrite * Changed the autocomplete display options so there's less chance it clips outside the display area. * Turned CompletedProgramName into an enum. * Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine. * For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
import { hasScriptExtension } from "../../Paths/ScriptFilePath";
2021-09-16 01:50:44 +02:00
import type { BaseServer } from "../../Server/BaseServer";
2022-09-06 15:07:12 +02:00
export function kill(args: (string | number | boolean)[], server: BaseServer): void {
try {
if (args.length < 1 || typeof args[0] === "boolean") {
Terminal.error("Incorrect usage of kill command. Usage: kill [pid] or kill [scriptname] [arg1] [arg2]...");
return;
}
2021-09-16 01:50:44 +02:00
// Kill by PID
if (typeof args[0] === "number") {
const pid = args[0];
const res = killWorkerScriptByPid(pid);
if (res) {
Terminal.print(`Killing script with PID ${pid}`);
} else {
Terminal.error(`Failed to kill script with PID ${pid}. No such script is running`);
}
2021-09-16 01:50:44 +02:00
return;
}
const path = Terminal.getFilepath(args[0]);
if (!path) return Terminal.error(`Invalid filename: ${args[0]}`);
if (!hasScriptExtension(path)) return Terminal.error(`Invalid file extension. Kill can only be used on scripts.`);
const runningScripts = findRunningScripts(path, args.slice(1), server);
if (runningScripts === null) {
Terminal.error("No such script is running. Nothing to kill");
return;
}
let killed = 0;
for (const pid of runningScripts.keys()) {
killed++;
if (killed < 5) {
Terminal.print(`Killing ${path} with pid ${pid}`);
}
killWorkerScriptByPid(pid);
}
if (killed >= 5) {
Terminal.print(`... killed ${killed} instances total`);
}
} catch (e) {
Terminal.error(e + "");
}
2021-09-16 01:50:44 +02:00
}