mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-27 19:37:07 +02:00
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).
This commit is contained in:
@@ -3,27 +3,21 @@ import { BaseServer } from "../../Server/BaseServer";
|
||||
import { LogBoxEvents } from "../../ui/React/LogBoxManager";
|
||||
import { startWorkerScript } from "../../NetscriptWorker";
|
||||
import { RunningScript } from "../../Script/RunningScript";
|
||||
import { findRunningScript } from "../../Script/ScriptHelpers";
|
||||
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(commandArgs: (string | number | boolean)[], server: BaseServer): 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] + "");
|
||||
if (!scriptName) return Terminal.error(`Invalid filename: ${commandArgs[0]}`);
|
||||
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.slice(1),
|
||||
argv: commandArgs,
|
||||
});
|
||||
const tailFlag = flags["--tail"] === true;
|
||||
const numThreads = parseFloat(flags["-t"] ?? 1);
|
||||
@@ -35,34 +29,24 @@ export function runScript(commandArgs: (string | number | boolean)[], server: Ba
|
||||
const args = flags["_"] as ScriptArg[];
|
||||
|
||||
// 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",
|
||||
);
|
||||
return;
|
||||
if (server.getRunningScript(path, args)) {
|
||||
return Terminal.error("This script is already running with the same args.");
|
||||
}
|
||||
|
||||
// Check if the script exists and if it does run it
|
||||
const script = server.scripts.get(scriptName);
|
||||
if (!script) return Terminal.error("No such script");
|
||||
|
||||
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) {
|
||||
Terminal.error("Need root access to run script");
|
||||
return;
|
||||
}
|
||||
if (!server.hasAdminRights) return Terminal.error("Need root access to run script");
|
||||
|
||||
if (ramUsage > ramAvailable + 0.001) {
|
||||
Terminal.error(
|
||||
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`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Able to run script
|
||||
@@ -70,10 +54,7 @@ export function runScript(commandArgs: (string | number | boolean)[], server: Ba
|
||||
runningScript.threads = numThreads;
|
||||
|
||||
const success = startWorkerScript(runningScript, server);
|
||||
if (!success) {
|
||||
Terminal.error(`Failed to start script`);
|
||||
return;
|
||||
}
|
||||
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)}.`,
|
||||
|
||||
Reference in New Issue
Block a user