mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-10-23 10:13:13 +02:00
e0272ad4af
* 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).
67 lines
2.5 KiB
TypeScript
67 lines
2.5 KiB
TypeScript
import { Terminal } from "../../Terminal";
|
|
import { BaseServer } from "../../Server/BaseServer";
|
|
import { LogBoxEvents } from "../../ui/React/LogBoxManager";
|
|
import { startWorkerScript } from "../../NetscriptWorker";
|
|
import { RunningScript } from "../../Script/RunningScript";
|
|
import * as libarg from "arg";
|
|
import { formatRam } from "../../ui/formatNumber";
|
|
import { ScriptArg } from "@nsdefs";
|
|
import { isPositiveInteger } from "../../types";
|
|
import { ScriptFilePath } from "../../Paths/ScriptFilePath";
|
|
|
|
export function runScript(path: ScriptFilePath, commandArgs: (string | number | boolean)[], server: BaseServer): void {
|
|
// This takes in the absolute filepath, see "run.ts"
|
|
const script = server.scripts.get(path);
|
|
if (!script) return Terminal.error(`Script ${path} does not exist on this server.`);
|
|
|
|
const runArgs = { "--tail": Boolean, "-t": Number };
|
|
const flags = libarg(runArgs, {
|
|
permissive: true,
|
|
argv: commandArgs,
|
|
});
|
|
const tailFlag = flags["--tail"] === true;
|
|
const numThreads = parseFloat(flags["-t"] ?? 1);
|
|
if (!isPositiveInteger(numThreads)) {
|
|
return Terminal.error("Invalid number of threads specified. Number of threads must be an integer greater than 0");
|
|
}
|
|
|
|
// Todo: Switch out arg for something with typescript support
|
|
const args = flags["_"] as ScriptArg[];
|
|
|
|
// Check if this script is already running
|
|
if (server.getRunningScript(path, args)) {
|
|
return Terminal.error("This script is already running with the same args.");
|
|
}
|
|
|
|
const singleRamUsage = script.getRamUsage(server.scripts);
|
|
if (!singleRamUsage) return Terminal.error("Error while calculating ram usage for this script.");
|
|
|
|
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" +
|
|
(numThreads === 1 ? "" : ` with ${numThreads} threads`) +
|
|
`. Script requires ${formatRam(ramUsage)} of RAM`,
|
|
);
|
|
}
|
|
|
|
// Able to run script
|
|
const runningScript = new RunningScript(script, singleRamUsage, args);
|
|
runningScript.threads = numThreads;
|
|
|
|
const success = startWorkerScript(runningScript, server);
|
|
if (!success) return Terminal.error(`Failed to start script`);
|
|
|
|
Terminal.print(
|
|
`Running script with ${numThreads} thread(s), pid ${runningScript.pid} and args: ${JSON.stringify(args)}.`,
|
|
);
|
|
if (tailFlag) {
|
|
LogBoxEvents.emit(runningScript);
|
|
}
|
|
return;
|
|
}
|