diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index f3929568c..c56ed1018 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -140,6 +140,7 @@ export const RamCosts: IMap = { scp: RamCostConstants.ScriptScpRamCost, ls: RamCostConstants.ScriptScanRamCost, ps: RamCostConstants.ScriptScanRamCost, + getRecentScripts: RamCostConstants.ScriptScanRamCost, hasRootAccess: RamCostConstants.ScriptHasRootAccessRamCost, getIp: RamCostConstants.ScriptGetHostnameRamCost, getHostname: RamCostConstants.ScriptGetHostnameRamCost, diff --git a/src/Netscript/RecentScripts.ts b/src/Netscript/RecentScripts.ts index 02e584b0d..d2f4ef04a 100644 --- a/src/Netscript/RecentScripts.ts +++ b/src/Netscript/RecentScripts.ts @@ -1,19 +1,23 @@ import { RunningScript } from "src/Script/RunningScript"; +import { Settings } from "../Settings/Settings"; import { WorkerScript } from "./WorkerScript"; export const recentScripts: RecentScript[] = []; export function AddRecentScript(workerScript: WorkerScript): void { if (recentScripts.find((r) => r.pid === workerScript.pid)) return; + + const killedTime = new Date(); recentScripts.unshift({ filename: workerScript.name, args: workerScript.args, pid: workerScript.pid, - timestamp: new Date(), - + timestamp: killedTime, + timestampEpoch: killedTime.getTime(), runningScript: workerScript.scriptRef, }); - while (recentScripts.length > 50) { + + while (recentScripts.length > Settings.MaxRecentScriptsCapacity) { recentScripts.pop(); } } @@ -23,5 +27,6 @@ export interface RecentScript { args: string[]; pid: number; timestamp: Date; + timestampEpoch: number; runningScript: RunningScript; } diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index b645b75d6..c110368e4 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -79,6 +79,8 @@ import { Gang as IGang, Bladeburner as IBladeburner, Stanek as IStanek, + RunningScript as IRunningScript, + RecentScript as IRecentScript, SourceFileLvl, BasicHGWOptions, ProcessInfo, @@ -98,6 +100,7 @@ import { SnackbarEvents } from "./ui/React/Snackbar"; import { Flags } from "./NetscriptFunctions/Flags"; import { calculateIntelligenceBonus } from "./PersonObjects/formulas/intelligence"; import { CalculateShareMult, StartSharing } from "./NetworkShare/Share"; +import { recentScripts } from "./Netscript/RecentScripts"; import { CityName } from "./Locations/data/CityNames"; import { wrapAPI } from "./Netscript/APIWrapper"; @@ -213,6 +216,32 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { return null; }; + /** + * Sanitizes a `RunningScript` to remove sensitive information, making it suitable for + * return through an NS function. + * @see NS.getRecentScripts + * @see NS.getRunningScript + * @param runningScript Existing, internal RunningScript + * @returns A sanitized, NS-facing copy of the RunningScript + */ + const createPublicRunningScript = function (runningScript: RunningScript): IRunningScript { + return { + args: runningScript.args.slice(), + filename: runningScript.filename, + logs: runningScript.logs.slice(), + offlineExpGained: runningScript.offlineExpGained, + offlineMoneyMade: runningScript.offlineMoneyMade, + offlineRunningTime: runningScript.offlineRunningTime, + onlineExpGained: runningScript.onlineExpGained, + onlineMoneyMade: runningScript.onlineMoneyMade, + onlineRunningTime: runningScript.onlineRunningTime, + pid: runningScript.pid, + ramUsage: runningScript.ramUsage, + server: runningScript.server, + threads: runningScript.threads, + }; + }; + /** * Helper function for getting the error log message when the user specifies * a nonexistent running script @@ -1431,6 +1460,10 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { allFiles.sort(); return allFiles; }, + getRecentScripts: function (): IRecentScript[] { + updateDynamicRam("getRecentScripts", getRamCost(Player, "getRecentScripts")); + return recentScripts.map((rs) => ({ ...rs, runningScript: createPublicRunningScript(rs.runningScript) })); + }, ps: function (_hostname: unknown = workerScript.hostname): ProcessInfo[] { updateDynamicRam("ps", getRamCost(Player, "ps")); const hostname = helper.string("ps", "hostname", _hostname); @@ -2146,21 +2179,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { runningScript = getRunningScript(fn, hostname, "getRunningScript", args); } if (runningScript === null) return null; - return { - args: runningScript.args.slice(), - filename: runningScript.filename, - logs: runningScript.logs.slice(), - offlineExpGained: runningScript.offlineExpGained, - offlineMoneyMade: runningScript.offlineMoneyMade, - offlineRunningTime: runningScript.offlineRunningTime, - onlineExpGained: runningScript.onlineExpGained, - onlineMoneyMade: runningScript.onlineMoneyMade, - onlineRunningTime: runningScript.onlineRunningTime, - pid: runningScript.pid, - ramUsage: runningScript.ramUsage, - server: runningScript.server, - threads: runningScript.threads, - }; + return createPublicRunningScript(runningScript); }, getHackTime: function (_hostname: unknown = workerScript.hostname): number { updateDynamicRam("getHackTime", getRamCost(Player, "getHackTime")); diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index e6e654e2e..64e5d8ddf 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -101,24 +101,56 @@ interface Player { /** * @public */ -interface RunningScript { +export interface RunningScript { + /** Arguments the script was called with */ args: string[]; + /** Filename of the script */ filename: string; + /** + * Script logs as an array. The newest log entries are at the bottom. + * Timestamps, if enabled, are placed inside `[brackets]` at the start of each line. + **/ logs: string[]; + /** Total amount of hacking experience earned from this script when offline */ offlineExpGained: number; + /** Total amount of money made by this script when offline */ offlineMoneyMade: number; - /** Offline running time of the script, in seconds **/ + /** Number of seconds that the script has been running offline */ offlineRunningTime: number; + /** Total amount of hacking experience earned from this script when online */ onlineExpGained: number; + /** Total amount of money made by this script when online */ onlineMoneyMade: number; - /** Online running time of the script, in seconds **/ + /** Number of seconds that this script has been running online */ onlineRunningTime: number; + /** Process ID. Must be an integer */ pid: number; + /** How much RAM this script uses for ONE thread */ ramUsage: number; + /** Hostname of the server on which this script runs */ server: string; + /** Number of threads that this script runs with */ threads: number; } +/** + * @public + */ +interface RecentScript { + /** Arguments the script was called with */ + args: string[]; + /** Filename of the script */ + filename: string; + /** Process ID. Must be an integer */ + pid: number; + /** Timestamp of when the script was killed */ + timestamp: Date; + /** Numeric epoch of timestamp */ + timestampEpoch: number; + /** An inactive copy of the last `RunningScript` associated to the script */ + runningScript: RunningScript; +} + /** * Data representing the internal values of a crime. * @public @@ -4782,6 +4814,27 @@ export interface NS extends Singularity { */ getScriptLogs(fn?: string, host?: string, ...args: any[]): string[]; + /** + * Get an array of recently killed scripts across all servers. + * @remarks + * RAM cost: 0.2 GB + * + * The most recently killed script is the first element in the array. + * Note that there is a maximum number of recently killed scripts which are tracked. + * This is configurable in the game's options as `Recently killed scripts size`. + * + * @usage below: + * ```ts + * let recentScripts = ns.getRecentScripts(); + * let mostRecent = recentScripts.shift() + * if (mostRecent) + * ns.tprint(mostRecent.logs.join('\n')) + * ``` + * + * @returns Array with information about previously killed scripts. + */ + getRecentScripts(): RecentScript[]; + /** * Open the tail window of a script. * @remarks diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index 5bd4268bb..1e3d529d9 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -981,11 +981,11 @@ export function Root(props: IProps): React.ReactElement { {" "} - Documentation:{" "} + Documentation:{" "} Basic - {" "} - | + + {" | "} Full diff --git a/src/Settings/Settings.ts b/src/Settings/Settings.ts index 978365639..906c9ca2d 100644 --- a/src/Settings/Settings.ts +++ b/src/Settings/Settings.ts @@ -63,6 +63,11 @@ interface IDefaultSettings { */ Locale: string; + /** + * Limit the number of recently killed script entries being tracked. + */ + MaxRecentScriptsCapacity: number; + /** * Limit the number of log entries for each script being executed on each server. */ @@ -191,6 +196,7 @@ export const defaultSettings: IDefaultSettings = { EnableBashHotkeys: false, TimestampsFormat: "", Locale: "en", + MaxRecentScriptsCapacity: 50, MaxLogCapacity: 50, MaxPortCapacity: 50, MaxTerminalCapacity: 500, @@ -228,6 +234,7 @@ export const Settings: ISettings & ISelfInitializer & ISelfLoading = { EnableBashHotkeys: defaultSettings.EnableBashHotkeys, TimestampsFormat: defaultSettings.TimestampsFormat, Locale: "en", + MaxRecentScriptsCapacity: defaultSettings.MaxRecentScriptsCapacity, MaxLogCapacity: defaultSettings.MaxLogCapacity, MaxPortCapacity: defaultSettings.MaxPortCapacity, MaxTerminalCapacity: defaultSettings.MaxTerminalCapacity, diff --git a/src/ui/React/GameOptionsRoot.tsx b/src/ui/React/GameOptionsRoot.tsx index 795116598..d717aeeb2 100644 --- a/src/ui/React/GameOptionsRoot.tsx +++ b/src/ui/React/GameOptionsRoot.tsx @@ -64,6 +64,7 @@ export function GameOptionsRoot(props: IProps): React.ReactElement { const importInput = useRef(null); const [execTime, setExecTime] = useState(Settings.CodeInstructionRunTime); + const [recentScriptsSize, setRecentScriptsSize] = useState(Settings.MaxRecentScriptsCapacity); const [logSize, setLogSize] = useState(Settings.MaxLogCapacity); const [portSize, setPortSize] = useState(Settings.MaxPortCapacity); const [terminalSize, setTerminalSize] = useState(Settings.MaxTerminalCapacity); @@ -79,6 +80,11 @@ export function GameOptionsRoot(props: IProps): React.ReactElement { Settings.CodeInstructionRunTime = newValue as number; } + function handleRecentScriptsSizeChange(event: any, newValue: number | number[]): void { + setRecentScriptsSize(newValue as number); + Settings.MaxRecentScriptsCapacity = newValue as number; + } + function handleLogSizeChange(event: any, newValue: number | number[]): void { setLogSize(newValue as number); Settings.MaxLogCapacity = newValue as number; @@ -176,6 +182,24 @@ export function GameOptionsRoot(props: IProps): React.ReactElement { max={100} valueLabelDisplay="auto" /> + + The maximum number of recently killed script entries being tracked. Setting this too high can + cause the game to use a lot of memory. + + } + > + Recently killed scripts size + +