From 5526355a4374ebd8ef7c76e967571058bd962379 Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Mon, 16 May 2022 19:15:38 -0500 Subject: [PATCH 1/8] Set up the skeleton of an ns.closeTail function --- src/NetscriptFunctions.ts | 4 ++++ src/ScriptEditor/NetscriptDefinitions.d.ts | 18 ++++++++++++++++++ src/ui/React/LogBoxManager.tsx | 2 ++ 3 files changed, 24 insertions(+) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 14d953f1f..ed7e2cb8e 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -988,6 +988,10 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { LogBoxEvents.emit(runningScriptObj); }, + closeTail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any[]): void { + updateDynamicRam("closeTail", getRamCost(Player, "closeTail")); + // TODO + }, nuke: function (_hostname: unknown): boolean { updateDynamicRam("nuke", getRamCost(Player, "nuke")); const hostname = helper.string("tail", "hostname", _hostname); diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 63f6d2250..bdc1a34a8 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4981,6 +4981,24 @@ export interface NS { */ tail(fn?: FilenameOrPID, host?: string, ...args: any[]): void; + /** + * Close the tail window of a script. + * @remarks + * RAM cost: 0 GB + * + * Closes a script’s logs. This is functionally the same pressing the "Close" button on the tail window. + * + * If the function is called with no arguments, it will close the current script’s logs. + * + * Otherwise, the fn, hostname/ip, and args… arguments can be used to close the logs from another script. + * Remember that scripts are uniquely identified by both their names and arguments. + * + * @param fn - Optional. Filename or PID of the script having its tail closed. If omitted, the current script is used. + * @param host - Optional. Hostname of the script having its tail closed. Defaults to the server this script is running on. If args are specified, this is not optional. + * @param args - Arguments for the script having its tail closed. + */ + closeTail(fn?: FilenameOrPID, host?: string, ...args: any[]): void; + /** * Get the list of servers connected to a server. * @remarks diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index 21839a23b..023f38a19 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -131,6 +131,8 @@ function LogWindow(props: IProps): React.ReactElement { return () => clearInterval(id); }, []); + //TODO Not actually a todo I just want this file to show up as changed + //so I can find it easier later function kill(): void { killWorkerScript(script, script.server, true); } From 2b9d408b55a5a166be43be1c0658d392353b1517 Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Tue, 17 May 2022 13:45:30 -0500 Subject: [PATCH 2/8] First attempt at an implementation; requires script be running --- src/NetscriptFunctions.ts | 23 ++++++++++++++++++----- src/ui/React/LogBoxManager.tsx | 11 +++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index ed7e2cb8e..6c4b36637 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -55,7 +55,7 @@ import { makeRuntimeRejectMsg, netscriptDelay, resolveNetscriptRequestedThreads import { numeralWrapper } from "./ui/numeralFormat"; import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions"; -import { LogBoxEvents } from "./ui/React/LogBoxManager"; +import { LogBoxEvents, LogBoxCloserEvents } from "./ui/React/LogBoxManager"; import { arrayToString } from "./utils/helpers/arrayToString"; import { isString } from "./utils/helpers/isString"; @@ -193,7 +193,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { throw makeRuntimeRejectMsg( workerScript, `Invalid scriptArgs argument passed into getRunningScript() from ${callingFnName}(). ` + - `This is probably a bug. Please report to game developer`, + `This is probably a bug. Please report to game developer`, ); } @@ -826,8 +826,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { workerScript.log( "weaken", () => - `'${server.hostname}' security level weakened to ${ - server.hackDifficulty + `'${server.hostname}' security level weakened to ${server.hackDifficulty }. Gained ${numeralWrapper.formatExp(expGain)} hacking exp (t=${numeralWrapper.formatThreads(threads)})`, ); workerScript.scriptRef.onlineExpGained += expGain; @@ -990,7 +989,21 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { }, closeTail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any[]): void { updateDynamicRam("closeTail", getRamCost(Player, "closeTail")); - // TODO + //Get the script object + let runningScriptObj; + if (arguments.length === 0) { + runningScriptObj = workerScript.scriptRef; + } else if (typeof fn === "number") { + runningScriptObj = getRunningScriptByPid(fn, "tail"); + } else { + runningScriptObj = getRunningScript(fn, hostname, "tail", scriptArgs); + } + if (runningScriptObj == null) { + workerScript.log("closeTail", () => getCannotFindRunningScriptErrorMessage(fn, hostname, scriptArgs)); + return; + } + //Emit an event to tell the game to close the tail window + LogBoxCloserEvents.emit(runningScriptObj); }, nuke: function (_hostname: unknown): boolean { updateDynamicRam("nuke", getRamCost(Player, "nuke")); diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index 023f38a19..1555df75d 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -23,6 +23,7 @@ import { Settings } from "../../Settings/Settings"; let layerCounter = 0; export const LogBoxEvents = new EventEmitter<[RunningScript]>(); +export const LogBoxCloserEvents = new EventEmitter<[RunningScript]>(); export const LogBoxClearEvents = new EventEmitter<[]>(); interface Log { @@ -51,6 +52,16 @@ export function LogBoxManager(): React.ReactElement { [], ); + //Event used by ns.closeTail to close tail windows + useEffect( + () => + LogBoxCloserEvents.subscribe((script: RunningScript) => { + const id = script.server + "-" + script.filename + script.args.map((x: any): string => `${x}`).join("-"); + close(id); + }), + [], + ); + useEffect(() => LogBoxClearEvents.subscribe(() => { logs = []; From e17fe6f61854192bf081375969a604d005e99853 Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Tue, 17 May 2022 14:46:30 -0500 Subject: [PATCH 3/8] closeTail(fn, host, ...args) can now close dead script tails closeTail(pid) still cannot --- src/NetscriptFunctions.ts | 36 +++++++++++++++++++++++----------- src/ui/React/LogBoxManager.tsx | 6 +++--- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 6c4b36637..df6016d45 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -989,21 +989,35 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { }, closeTail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any[]): void { updateDynamicRam("closeTail", getRamCost(Player, "closeTail")); - //Get the script object - let runningScriptObj; + //Get the script details + let server; + let filename; + let args; if (arguments.length === 0) { - runningScriptObj = workerScript.scriptRef; + //Get the details from the script calling the function + const runningScriptObj = workerScript.scriptRef; + server = runningScriptObj.server; + filename = runningScriptObj.filename; + args = runningScriptObj.args; } else if (typeof fn === "number") { - runningScriptObj = getRunningScriptByPid(fn, "tail"); + //Get the details via the pid of a running script + const runningScriptObj = getRunningScriptByPid(fn, "tail"); + if (runningScriptObj == null) { + workerScript.log("closeTail", () => getCannotFindRunningScriptErrorMessage(`${fn}`, hostname, scriptArgs)); + return; + } + server = runningScriptObj.server; + filename = runningScriptObj.filename; + args = runningScriptObj.args; } else { - runningScriptObj = getRunningScript(fn, hostname, "tail", scriptArgs); + //Get the details from the input directly + server = hostname; + filename = fn; + args = scriptArgs; } - if (runningScriptObj == null) { - workerScript.log("closeTail", () => getCannotFindRunningScriptErrorMessage(fn, hostname, scriptArgs)); - return; - } - //Emit an event to tell the game to close the tail window - LogBoxCloserEvents.emit(runningScriptObj); + + //Emit an event to tell the game to close the tail window if it exists + LogBoxCloserEvents.emit(server, filename, args.map((x: any): string => `${x}`)); }, nuke: function (_hostname: unknown): boolean { updateDynamicRam("nuke", getRamCost(Player, "nuke")); diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index 1555df75d..514d06e38 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -23,7 +23,7 @@ import { Settings } from "../../Settings/Settings"; let layerCounter = 0; export const LogBoxEvents = new EventEmitter<[RunningScript]>(); -export const LogBoxCloserEvents = new EventEmitter<[RunningScript]>(); +export const LogBoxCloserEvents = new EventEmitter<[any, any, any[]]>(); export const LogBoxClearEvents = new EventEmitter<[]>(); interface Log { @@ -55,8 +55,8 @@ export function LogBoxManager(): React.ReactElement { //Event used by ns.closeTail to close tail windows useEffect( () => - LogBoxCloserEvents.subscribe((script: RunningScript) => { - const id = script.server + "-" + script.filename + script.args.map((x: any): string => `${x}`).join("-"); + LogBoxCloserEvents.subscribe((hostname: any, filename: any, stringArgs: any[]) => { + const id = hostname + "-" + filename + stringArgs.map((x: any): string => `${x}`).join("-"); close(id); }), [], From 8903cd2a8b20ec43533b0cbbe72dfa985d730154 Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Tue, 17 May 2022 15:19:35 -0500 Subject: [PATCH 4/8] Removed closePid(fn, host, ...args) and fixed closePid(pid) --- src/NetscriptFunctions.ts | 33 +++++---------------------------- src/ui/React/LogBoxManager.tsx | 28 +++++++++++++++++----------- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index df6016d45..367eb7bcf 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -987,37 +987,14 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { LogBoxEvents.emit(runningScriptObj); }, - closeTail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any[]): void { + closeTail: function (pid?: number): void { updateDynamicRam("closeTail", getRamCost(Player, "closeTail")); - //Get the script details - let server; - let filename; - let args; - if (arguments.length === 0) { - //Get the details from the script calling the function - const runningScriptObj = workerScript.scriptRef; - server = runningScriptObj.server; - filename = runningScriptObj.filename; - args = runningScriptObj.args; - } else if (typeof fn === "number") { - //Get the details via the pid of a running script - const runningScriptObj = getRunningScriptByPid(fn, "tail"); - if (runningScriptObj == null) { - workerScript.log("closeTail", () => getCannotFindRunningScriptErrorMessage(`${fn}`, hostname, scriptArgs)); - return; - } - server = runningScriptObj.server; - filename = runningScriptObj.filename; - args = runningScriptObj.args; - } else { - //Get the details from the input directly - server = hostname; - filename = fn; - args = scriptArgs; + //Get the pid of the calling script if no pid is given + if (pid === undefined) { + pid = workerScript.scriptRef.pid; } - //Emit an event to tell the game to close the tail window if it exists - LogBoxCloserEvents.emit(server, filename, args.map((x: any): string => `${x}`)); + LogBoxCloserEvents.emit(pid); }, nuke: function (_hostname: unknown): boolean { updateDynamicRam("nuke", getRamCost(Player, "nuke")); diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index 514d06e38..eeec501fe 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -23,7 +23,7 @@ import { Settings } from "../../Settings/Settings"; let layerCounter = 0; export const LogBoxEvents = new EventEmitter<[RunningScript]>(); -export const LogBoxCloserEvents = new EventEmitter<[any, any, any[]]>(); +export const LogBoxCloserEvents = new EventEmitter<[number]>(); export const LogBoxClearEvents = new EventEmitter<[]>(); interface Log { @@ -55,9 +55,8 @@ export function LogBoxManager(): React.ReactElement { //Event used by ns.closeTail to close tail windows useEffect( () => - LogBoxCloserEvents.subscribe((hostname: any, filename: any, stringArgs: any[]) => { - const id = hostname + "-" + filename + stringArgs.map((x: any): string => `${x}`).join("-"); - close(id); + LogBoxCloserEvents.subscribe((pid: number) => { + closePid(pid); }), [], ); @@ -69,11 +68,18 @@ export function LogBoxManager(): React.ReactElement { }), ); + //Close tail windows by their id function close(id: string): void { logs = logs.filter((l) => l.id !== id); rerender(); } + //Close tail windows by their pid + function closePid(pid: number): void { + logs = logs.filter((log) => log.script.pid != pid); + rerender(); + } + return ( <> {logs.map((log) => ( @@ -249,14 +255,14 @@ function LogWindow(props: IProps): React.ReactElement { minHeight: `${minConstraints[1]}px`, ...(minimized ? { - border: "none", - margin: 0, - maxHeight: 0, - padding: 0, - } + border: "none", + margin: 0, + maxHeight: 0, + padding: 0, + } : { - border: `1px solid ${Settings.theme.welllight}`, - }), + border: `1px solid ${Settings.theme.welllight}`, + }), }} ref={container} > From b18c18e19a1ef065d9c286d1ffa34e5663f38a95 Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Tue, 17 May 2022 15:22:37 -0500 Subject: [PATCH 5/8] Updated documentation of closeTail to reflect last commit --- src/ScriptEditor/NetscriptDefinitions.d.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index bdc1a34a8..2ed344175 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4990,14 +4990,11 @@ export interface NS { * * If the function is called with no arguments, it will close the current script’s logs. * - * Otherwise, the fn, hostname/ip, and args… arguments can be used to close the logs from another script. - * Remember that scripts are uniquely identified by both their names and arguments. + * Otherwise, the pid argument can be used to close the logs from another script. * - * @param fn - Optional. Filename or PID of the script having its tail closed. If omitted, the current script is used. - * @param host - Optional. Hostname of the script having its tail closed. Defaults to the server this script is running on. If args are specified, this is not optional. - * @param args - Arguments for the script having its tail closed. + * @param pid - Optional. PID of the script having its tail closed. If omitted, the current script is used. */ - closeTail(fn?: FilenameOrPID, host?: string, ...args: any[]): void; + closeTail(pid?: number): void; /** * Get the list of servers connected to a server. From c01cf85d27faf84abb04c52da035a0a1e2555dbd Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Tue, 17 May 2022 15:25:27 -0500 Subject: [PATCH 6/8] Formatted and linted --- src/NetscriptFunctions.ts | 5 +++-- src/ScriptEditor/NetscriptDefinitions.d.ts | 2 +- src/ui/React/LogBoxManager.tsx | 14 +++++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 367eb7bcf..a555a6665 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -193,7 +193,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { throw makeRuntimeRejectMsg( workerScript, `Invalid scriptArgs argument passed into getRunningScript() from ${callingFnName}(). ` + - `This is probably a bug. Please report to game developer`, + `This is probably a bug. Please report to game developer`, ); } @@ -826,7 +826,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { workerScript.log( "weaken", () => - `'${server.hostname}' security level weakened to ${server.hackDifficulty + `'${server.hostname}' security level weakened to ${ + server.hackDifficulty }. Gained ${numeralWrapper.formatExp(expGain)} hacking exp (t=${numeralWrapper.formatThreads(threads)})`, ); workerScript.scriptRef.onlineExpGained += expGain; diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 2ed344175..dfc63eee6 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4994,7 +4994,7 @@ export interface NS { * * @param pid - Optional. PID of the script having its tail closed. If omitted, the current script is used. */ - closeTail(pid?: number): void; + closeTail(pid?: number): void; /** * Get the list of servers connected to a server. diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index eeec501fe..4286b59e3 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -255,14 +255,14 @@ function LogWindow(props: IProps): React.ReactElement { minHeight: `${minConstraints[1]}px`, ...(minimized ? { - border: "none", - margin: 0, - maxHeight: 0, - padding: 0, - } + border: "none", + margin: 0, + maxHeight: 0, + padding: 0, + } : { - border: `1px solid ${Settings.theme.welllight}`, - }), + border: `1px solid ${Settings.theme.welllight}`, + }), }} ref={container} > From d52e1f44b4328ee9f51de09af1f1dbcc6a1914b9 Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Tue, 17 May 2022 15:26:26 -0500 Subject: [PATCH 7/8] Removed my stupid comment lol (sorry!) --- src/ui/React/LogBoxManager.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index 4286b59e3..8da57a924 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -148,8 +148,6 @@ function LogWindow(props: IProps): React.ReactElement { return () => clearInterval(id); }, []); - //TODO Not actually a todo I just want this file to show up as changed - //so I can find it easier later function kill(): void { killWorkerScript(script, script.server, true); } From b3aa0578fb10821dae3af2a11d3d0d2f20c80670 Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Tue, 17 May 2022 15:54:05 -0500 Subject: [PATCH 8/8] Minor refactor --- src/NetscriptFunctions.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index a555a6665..6674d6944 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -988,12 +988,9 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { LogBoxEvents.emit(runningScriptObj); }, - closeTail: function (pid?: number): void { + closeTail: function (_pid: unknown = workerScript.scriptRef.pid): void { updateDynamicRam("closeTail", getRamCost(Player, "closeTail")); - //Get the pid of the calling script if no pid is given - if (pid === undefined) { - pid = workerScript.scriptRef.pid; - } + const pid = helper.number("closeTail", "pid", _pid); //Emit an event to tell the game to close the tail window if it exists LogBoxCloserEvents.emit(pid); },