From ddb10f833c46962783169078d5ceb39019ce2cd7 Mon Sep 17 00:00:00 2001 From: LJ <23249107+LJNeon@users.noreply.github.com> Date: Thu, 15 Feb 2024 06:51:37 -0700 Subject: [PATCH] TERMINAL: Add --ram-override flag to the run command (#1055) * Add --ram-override flag to run command * Update help command * Fix whitespace * Update run usage message * Update autocomplete for run command * Format --- src/Terminal/HelpText.ts | 105 +++++++++--------- src/Terminal/commands/run.ts | 5 +- src/Terminal/commands/runScript.ts | 11 +- src/Terminal/getTabCompletionPossibilities.ts | 2 +- 4 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/Terminal/HelpText.ts b/src/Terminal/HelpText.ts index 9776ea173..5c49c4d08 100644 --- a/src/Terminal/HelpText.ts +++ b/src/Terminal/HelpText.ts @@ -1,53 +1,54 @@ export const TerminalHelpText: string[] = [ "Type 'help name' to learn more about the command ", " ", - ' alias [-g] [name="value"] Create or display Terminal aliases', - " analyze Get information about the current machine ", - " backdoor Install a backdoor on the current machine ", - " buy [-l/-a/program] Purchase a program through the Dark Web", - " cat [file] Display a .msg, .lit, or .txt file", - " cd [dir] Change to a new directory", - " check [script] [args...] Print a script's logs to Terminal", - " clear Clear all text on the terminal ", - " cls See 'clear' command ", - " connect [hostname] Connects to a remote server", - " cp [src] [dst] Copy a file", - " download [script/text file] Downloads scripts or text files to your computer", - " expr [math expression] Evaluate a mathematical expression", - " free Check the machine's memory (RAM) usage", - " grow Spoof money in a servers bank account, increasing the amount available.", - " hack Hack the current machine", - " help [command] Display this help text, or the help text for a command", - " history [-c] Display the terminal history", - " home Connect to home computer", - " hostname Displays the hostname of the machine", - " kill [script/pid] [args...] Stops the specified script on the current server ", - " killall Stops all running scripts on the current machine", - " ls [dir] [--grep pattern] Displays all files on the machine", - " lscpu Displays the number of CPU cores on the machine", - " mem [script] [-t n] Displays the amount of RAM required to run the script", - " mv [src] [dest] Move/rename a text or script file", - " nano [file ...] Text editor - Open up and edit one or more scripts or text files", - " ps Display all scripts that are currently running", - " rm [file] Delete a file from the server", - " run [name] [-t n] [--tail] [args...] Execute a program or script", - " scan Prints all immediately-available network connections", - " scan-analyze [d] [-a] Prints info for all servers up to d nodes away", - " scp [file ...] [server] Copies a file to a destination server", - " sudov Shows whether you have root access on this computer", - " tail [script] [args...] Displays dynamic logs for the specified script", - " top Displays all running scripts and their RAM usage", - " unalias [alias name] Deletes the specified alias", - " vim [file ...] Text editor - Open up and edit one or more scripts or text files in vim mode", - " weaken Reduce the security of the current machine", - " wget [url] [target file] Retrieves code/text from a web server", + ' alias [-g] [name="value"] Create or display Terminal aliases', + " analyze Get information about the current machine ", + " backdoor Install a backdoor on the current machine ", + " buy [-l/-a/program] Purchase a program through the Dark Web", + " cat [file] Display a .msg, .lit, or .txt file", + " cd [dir] Change to a new directory", + " check [script] [args...] Print a script's logs to Terminal", + " clear Clear all text on the terminal ", + " cls See 'clear' command ", + " connect [hostname] Connects to a remote server", + " cp [src] [dest] Copy a file", + " download [script/text file] Downloads scripts or text files to your computer", + " expr [math expression] Evaluate a mathematical expression", + " free Check the machine's memory (RAM) usage", + " grow Spoof money in a servers bank account, increasing the amount available.", + " hack Hack the current machine", + " help [command] Display this help text, or the help text for a command", + " history [-c] Display the terminal history", + " home Connect to home computer", + " hostname Displays the hostname of the machine", + " kill [script/pid] [args...] Stops the specified script on the current server ", + " killall Stops all running scripts on the current machine", + " ls [dir] [--grep pattern] Displays all files on the machine", + " lscpu Displays the number of CPU cores on the machine", + " mem [script] [-t n] Displays the amount of RAM required to run the script", + " mv [src] [dest] Move/rename a text or script file", + " nano [files...] Text editor - Open up and edit one or more scripts or text files", + " ps Display all scripts that are currently running", + " rm [file] Delete a file from the server", + " run [script] [-t n] [--tail] Execute a program or script", + " [--ram-override n] [args...]", + " scan Prints all immediately-available network connections", + " scan-analyze [d] [-a] Prints info for all servers up to d nodes away", + " scp [files...] [server] Copies a file to a destination server", + " sudov Shows whether you have root access on this computer", + " tail [script] [args...] Displays dynamic logs for the specified script", + " top Displays all running scripts and their RAM usage", + " unalias [alias name] Deletes the specified alias", + " vim [files...] Text editor - Open up and edit one or more scripts or text files in vim mode", + " weaken Reduce the security of the current machine", + " wget [url] [target file] Retrieves code/text from a web server", " ", ]; const TemplatedHelpTexts: Record string[]> = { scriptEditor: (command) => { return [ - `Usage: ${command} [file ...] | [glob]`, + `Usage: ${command} [file names...] | [glob]`, ` `, `Opens up the specified file(s) in the Script Editor. Only scripts (.js, or .script) or text files (.txt) `, `can be edited using the Script Editor. If a file does not exist a new one will be created`, @@ -133,7 +134,7 @@ export const HelpTexts: Record = { " ", ], cat: [ - "Usage: cat [file]", + "Usage: cat [file name]", " ", "Display message (.msg), literature (.lit), or text (.txt) files. Examples:", " ", @@ -193,7 +194,7 @@ export const HelpTexts: Record = { "a backdoor installed can be connected to. To see which servers can be connected to, use the 'scan' command.", " ", ], - cp: ["Usage: cp [src] [dst]", " ", "Copy a file on this server. To copy a file to another server use scp.", " "], + cp: ["Usage: cp [src] [dest]", " ", "Copy a file on this server. To copy a file to another server use scp.", " "], download: [ "Usage: download [script/text file]", " ", @@ -358,7 +359,7 @@ export const HelpTexts: Record = { nano: TemplatedHelpTexts.scriptEditor("nano"), ps: ["Usage: ps", " ", "Prints all scripts that are running on the current server", " "], rm: [ - "Usage: rm [file]", + "Usage: rm [file name]", " ", "Removes the specified file from the current server. A file can be a script, a program, or a message file. ", " ", @@ -366,15 +367,17 @@ export const HelpTexts: Record = { " ", ], run: [ - "Usage: run [file name] [-t] [num threads] [args...]", + "Usage: run [file name] [-t num_threads] [--tail] [--ram-override ram_in_GBs] [args...]", " ", "Execute a program, script or coding contract.", " ", - "The '[-t]', '[num threads]', and '[args...]' arguments are only valid when running a script. The '-t' flag is used ", - "to indicate that the script should be run with the specified number of threads. If the flag is omitted, ", - "then the script will be run with a single thread by default. ", - "If the '-t' flag is used, then it MUST come immediately ", - "after the script name, and the [num threads] argument MUST come immediately afterwards. ", + "The '[-t num_threads]', '[--tail]', `[--ram-override ram_in_GBs]`, and '[args...]' arguments are only valid", + "when running a script. The '-t' flag is used to indicate that the script should be run with the specified", + "number of threads. If the flag is omitted, then the script will be run with a single thread by default. The", + "'--tail' flag is used to immediately open a tail window for the script being ran. And the '--ram-override'", + "flag is used to override the amount of ram (per thread) the script is ran with. If the script ends up using", + "more than that amount of ram it will crash. If any of the flags are used, then they MUST come immediately", + "after the script name.", " ", "[args...] represents a variable number of arguments that will be passed into the script. See the documentation ", "about script arguments. Each specified argument must be separated by a space. ", @@ -407,7 +410,7 @@ export const HelpTexts: Record = { " ", ], scp: [ - "Usage: scp [filename ...] [target server]", + "Usage: scp [file names...] [target server]", " ", "Copies the specified file(s) from the current server to the target server. ", "This command only works for script files (.script or .js extension), literature files (.lit extension), ", diff --git a/src/Terminal/commands/run.ts b/src/Terminal/commands/run.ts index 9dd4f6845..6fb981e9a 100644 --- a/src/Terminal/commands/run.ts +++ b/src/Terminal/commands/run.ts @@ -9,7 +9,10 @@ import { hasProgramExtension } from "../../Paths/ProgramFilePath"; export function run(args: (string | number | boolean)[], server: BaseServer): void { // Run a program or a script const arg = args.shift(); - if (!arg) return Terminal.error("Usage: run [program/script] [-t] [num threads] [arg1] [arg2]..."); + if (!arg) + return Terminal.error( + "Usage: run [program/script] [-t num_threads] [--tail] [--ram-override ram_in_GBs] [args...]", + ); const path = Terminal.getFilepath(String(arg)); if (!path) return Terminal.error(`${arg} is not a valid filepath.`); diff --git a/src/Terminal/commands/runScript.ts b/src/Terminal/commands/runScript.ts index 14ea2c48e..f2a7be22c 100644 --- a/src/Terminal/commands/runScript.ts +++ b/src/Terminal/commands/runScript.ts @@ -9,28 +9,35 @@ import { ScriptArg } from "@nsdefs"; import { isPositiveInteger } from "../../types"; import { ScriptFilePath } from "../../Paths/ScriptFilePath"; import { sendDeprecationNotice } from "./common/deprecation"; +import { RamCostConstants } from "../../Netscript/RamCostGenerator"; 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 runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number }; const flags = libarg(runArgs, { permissive: true, argv: commandArgs, }); const tailFlag = flags["--tail"] === true; const numThreads = parseFloat(flags["-t"] ?? 1); + const ramOverride = flags["--ram-override"] != null ? parseFloat(flags["--ram-override"]) : null; if (!isPositiveInteger(numThreads)) { return Terminal.error("Invalid number of threads specified. Number of threads must be an integer greater than 0"); } + if (ramOverride != null && (isNaN(ramOverride) || ramOverride < RamCostConstants.Base)) { + return Terminal.error( + `Invalid ram override specified. Ram override must be a number greater than ${RamCostConstants.Base}`, + ); + } if (!server.hasAdminRights) return Terminal.error("Need root access to run script"); // Todo: Switch out arg for something with typescript support const args = flags._ as ScriptArg[]; - const singleRamUsage = script.getRamUsage(server.scripts); + const singleRamUsage = ramOverride ?? script.getRamUsage(server.scripts); if (!singleRamUsage) { return Terminal.error(`Error while calculating ram usage for this script. ${script.ramCalculationError}`); } diff --git a/src/Terminal/getTabCompletionPossibilities.ts b/src/Terminal/getTabCompletionPossibilities.ts index 1fd9a8a55..f02b5780c 100644 --- a/src/Terminal/getTabCompletionPossibilities.ts +++ b/src/Terminal/getTabCompletionPossibilities.ts @@ -312,7 +312,7 @@ export async function getTabCompletionPossibilities(terminalText: string, baseDi } if (!loadedModule || !loadedModule.autocomplete) return; // Doesn't have an autocomplete function. - const runArgs = { "--tail": Boolean, "-t": Number }; + const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number }; const flags = libarg(runArgs, { permissive: true, argv: command.slice(2),