NETSCRIPT: Add ns.self() as a free info function (#1636)

* added utility info

* moved info to running script

* fix for RAM cost

* description changes

Co-authored-by: David Walker <d0sboots@gmail.com>

* fixed wrong formatting

* Added parent to ignored fields

---------

Co-authored-by: David Walker <d0sboots@gmail.com>
This commit is contained in:
G4mingJon4s 2024-09-08 02:34:24 +02:00 committed by GitHub
parent 0ce5cd35dc
commit 651b17739c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 145 additions and 7 deletions

@ -4,6 +4,8 @@
## AutocompleteData.enums property ## AutocompleteData.enums property
Netscript Enums
**Signature:** **Signature:**
```typescript ```typescript

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [AutocompleteData](./bitburner.autocompletedata.md) &gt; [filename](./bitburner.autocompletedata.filename.md)
## AutocompleteData.filename property
The filename of the script about to be run
**Signature:**
```typescript
filename: string;
```

@ -4,6 +4,8 @@
## AutocompleteData.flags() method ## AutocompleteData.flags() method
Parses the flags schema on the already inputted flags
**Signature:** **Signature:**
```typescript ```typescript

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [AutocompleteData](./bitburner.autocompletedata.md) &gt; [hostname](./bitburner.autocompletedata.hostname.md)
## AutocompleteData.hostname property
The hostname of the server the script would be running on
**Signature:**
```typescript
hostname: string;
```

@ -16,14 +16,17 @@ interface AutocompleteData
| Property | Modifiers | Type | Description | | Property | Modifiers | Type | Description |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| [enums](./bitburner.autocompletedata.enums.md) | | [NSEnums](./bitburner.nsenums.md) | | | [enums](./bitburner.autocompletedata.enums.md) | | [NSEnums](./bitburner.nsenums.md) | Netscript Enums |
| [scripts](./bitburner.autocompletedata.scripts.md) | | string\[\] | | | [filename](./bitburner.autocompletedata.filename.md) | | string | The filename of the script about to be run |
| [servers](./bitburner.autocompletedata.servers.md) | | string\[\] | | | [hostname](./bitburner.autocompletedata.hostname.md) | | string | The hostname of the server the script would be running on |
| [txts](./bitburner.autocompletedata.txts.md) | | string\[\] | | | [processes](./bitburner.autocompletedata.processes.md) | | [ProcessInfo](./bitburner.processinfo.md)<!-- -->\[\] | The processes running on the host |
| [scripts](./bitburner.autocompletedata.scripts.md) | | string\[\] | All scripts on the current server |
| [servers](./bitburner.autocompletedata.servers.md) | | string\[\] | All server hostnames |
| [txts](./bitburner.autocompletedata.txts.md) | | string\[\] | All text files on the current server |
## Methods ## Methods
| Method | Description | | Method | Description |
| --- | --- | | --- | --- |
| [flags(schema)](./bitburner.autocompletedata.flags.md) | | | [flags(schema)](./bitburner.autocompletedata.flags.md) | Parses the flags schema on the already inputted flags |

@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [AutocompleteData](./bitburner.autocompletedata.md) &gt; [processes](./bitburner.autocompletedata.processes.md)
## AutocompleteData.processes property
The processes running on the host
**Signature:**
```typescript
processes: ProcessInfo[];
```

@ -4,6 +4,8 @@
## AutocompleteData.scripts property ## AutocompleteData.scripts property
All scripts on the current server
**Signature:** **Signature:**
```typescript ```typescript

@ -4,6 +4,8 @@
## AutocompleteData.servers property ## AutocompleteData.servers property
All server hostnames
**Signature:** **Signature:**
```typescript ```typescript

@ -4,6 +4,8 @@
## AutocompleteData.txts property ## AutocompleteData.txts property
All text files on the current server
**Signature:** **Signature:**
```typescript ```typescript

@ -155,6 +155,7 @@ export async function main(ns) {
| [scp(files, destination, source)](./bitburner.ns.scp.md) | Copy file between servers. | | [scp(files, destination, source)](./bitburner.ns.scp.md) | Copy file between servers. |
| [scriptKill(script, host)](./bitburner.ns.scriptkill.md) | Kill all scripts with a filename. | | [scriptKill(script, host)](./bitburner.ns.scriptkill.md) | Kill all scripts with a filename. |
| [scriptRunning(script, host)](./bitburner.ns.scriptrunning.md) | Check if any script with a filename is running. | | [scriptRunning(script, host)](./bitburner.ns.scriptrunning.md) | Check if any script with a filename is running. |
| [self()](./bitburner.ns.self.md) | Returns the currently running script. |
| [serverExists(host)](./bitburner.ns.serverexists.md) | Returns a boolean denoting whether or not the specified server exists. | | [serverExists(host)](./bitburner.ns.serverexists.md) | Returns a boolean denoting whether or not the specified server exists. |
| [setTitle(title, pid)](./bitburner.ns.settitle.md) | Set the title of the tail window of a script. | | [setTitle(title, pid)](./bitburner.ns.settitle.md) | Set the title of the tail window of a script. |
| [share()](./bitburner.ns.share.md) | Share the server's ram with your factions. | | [share()](./bitburner.ns.share.md) | Share the server's ram with your factions. |

@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [NS](./bitburner.ns.md) &gt; [self](./bitburner.ns.self.md)
## NS.self() method
Returns the currently running script.
**Signature:**
```typescript
self(): RunningScript;
```
**Returns:**
[RunningScript](./bitburner.runningscript.md)
## Remarks
RAM cost: 0 GB

@ -25,6 +25,7 @@ interface RunningScript
| [onlineExpGained](./bitburner.runningscript.onlineexpgained.md) | | number | Total amount of hacking experience earned from this script when online | | [onlineExpGained](./bitburner.runningscript.onlineexpgained.md) | | number | Total amount of hacking experience earned from this script when online |
| [onlineMoneyMade](./bitburner.runningscript.onlinemoneymade.md) | | number | Total amount of money made by this script when online | | [onlineMoneyMade](./bitburner.runningscript.onlinemoneymade.md) | | number | Total amount of money made by this script when online |
| [onlineRunningTime](./bitburner.runningscript.onlinerunningtime.md) | | number | Number of seconds that this script has been running online | | [onlineRunningTime](./bitburner.runningscript.onlinerunningtime.md) | | number | Number of seconds that this script has been running online |
| [parent](./bitburner.runningscript.parent.md) | | number | <p>Process ID of the parent process.</p><p>If this script was started by another script, this will be the PID of that script. If this script was started directly through the terminal, the value will be 0.</p> |
| [pid](./bitburner.runningscript.pid.md) | | number | Process ID. Must be an integer | | [pid](./bitburner.runningscript.pid.md) | | number | Process ID. Must be an integer |
| [ramUsage](./bitburner.runningscript.ramusage.md) | | number | How much RAM this script uses for ONE thread. Also known as "static RAM usage," this value does not change once the script is started, unless you call ns.ramOverride(). | | [ramUsage](./bitburner.runningscript.ramusage.md) | | number | How much RAM this script uses for ONE thread. Also known as "static RAM usage," this value does not change once the script is started, unless you call ns.ramOverride(). |
| [server](./bitburner.runningscript.server.md) | | string | Hostname of the server on which this script runs | | [server](./bitburner.runningscript.server.md) | | string | Hostname of the server on which this script runs |

@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [bitburner](./bitburner.md) &gt; [RunningScript](./bitburner.runningscript.md) &gt; [parent](./bitburner.runningscript.parent.md)
## RunningScript.parent property
Process ID of the parent process.
If this script was started by another script, this will be the PID of that script. If this script was started directly through the terminal, the value will be 0.
**Signature:**
```typescript
parent: number;
```

@ -656,7 +656,7 @@ function getRunningScript(ctx: NetscriptContext, ident: ScriptIdentifier): Runni
} else { } else {
const scripts = getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args); const scripts = getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
if (scripts === null) return null; if (scripts === null) return null;
return scripts.values().next().value; return scripts.values().next().value ?? null;
} }
} }
@ -696,6 +696,7 @@ function createPublicRunningScript(runningScript: RunningScript, workerScript?:
onlineMoneyMade: runningScript.onlineMoneyMade, onlineMoneyMade: runningScript.onlineMoneyMade,
onlineRunningTime: runningScript.onlineRunningTime, onlineRunningTime: runningScript.onlineRunningTime,
pid: runningScript.pid, pid: runningScript.pid,
parent: runningScript.parent,
ramUsage: runningScript.ramUsage, ramUsage: runningScript.ramUsage,
server: runningScript.server, server: runningScript.server,
tailProperties: tailProperties:

@ -519,6 +519,7 @@ export const RamCosts: RamCostTree<NSFull> = {
run: RamCostConstants.Run, run: RamCostConstants.Run,
exec: RamCostConstants.Exec, exec: RamCostConstants.Exec,
spawn: RamCostConstants.Spawn, spawn: RamCostConstants.Spawn,
self: 0,
kill: RamCostConstants.Kill, kill: RamCostConstants.Kill,
killall: RamCostConstants.Kill, killall: RamCostConstants.Kill,
exit: 0, exit: 0,

@ -776,6 +776,11 @@ export const ns: InternalAPI<NSFull> = {
throw new ScriptDeath(ctx.workerScript); throw new ScriptDeath(ctx.workerScript);
} }
}, },
self: (ctx) => () => {
const runningScript = helpers.getRunningScript(ctx, ctx.workerScript.pid);
if (runningScript == null) throw helpers.errorMessage(ctx, "Cannot find running script. This is a bug.");
return helpers.createPublicRunningScript(runningScript, ctx.workerScript);
},
kill: kill:
(ctx) => (ctx) =>
(scriptID, hostname = ctx.workerScript.hostname, ...scriptArgs) => { (scriptID, hostname = ctx.workerScript.hostname, ...scriptArgs) => {

@ -484,6 +484,7 @@ export function runScriptFromScript(
() => `'${scriptname}' on '${host.hostname}' with ${runOpts.threads} threads and args: ${arrayToString(args)}.`, () => `'${scriptname}' on '${host.hostname}' with ${runOpts.threads} threads and args: ${arrayToString(args)}.`,
); );
const runningScriptObj = new RunningScript(script, singleRamUsage, args); const runningScriptObj = new RunningScript(script, singleRamUsage, args);
runningScriptObj.parent = workerScript.pid;
runningScriptObj.threads = runOpts.threads; runningScriptObj.threads = runOpts.threads;
runningScriptObj.temporary = runOpts.temporary; runningScriptObj.temporary = runOpts.temporary;

@ -58,6 +58,9 @@ export class RunningScript {
// Process ID. Must be an integer and equals the PID of corresponding WorkerScript // Process ID. Must be an integer and equals the PID of corresponding WorkerScript
pid = -1; pid = -1;
// Process ID of the parent process. 0 indicates no parent (such as run from terminal).
parent = 0;
// How much RAM this script uses for ONE thread // How much RAM this script uses for ONE thread
ramUsage: number = RamCostConstants.Base; ramUsage: number = RamCostConstants.Base;
@ -168,7 +171,7 @@ export class RunningScript {
} }
} }
const includedProperties = getKeyList(RunningScript, { const includedProperties = getKeyList(RunningScript, {
removedKeys: ["logs", "dependencies", "logUpd", "pid", "tailProps"], removedKeys: ["logs", "dependencies", "logUpd", "pid", "parent", "tailProps"],
}); });
const includedPropsNoTitle = includedProperties.filter((x) => x !== "title"); const includedPropsNoTitle = includedProperties.filter((x) => x !== "title");

@ -245,6 +245,13 @@ interface RunningScript {
onlineRunningTime: number; onlineRunningTime: number;
/** Process ID. Must be an integer */ /** Process ID. Must be an integer */
pid: number; pid: number;
/**
* Process ID of the parent process.
*
* If this script was started by another script, this will be the PID of that script.
* If this script was started directly through the terminal, the value will be 0.
*/
parent: number;
/** /**
* How much RAM this script uses for ONE thread. * How much RAM this script uses for ONE thread.
* Also known as "static RAM usage," this value does not change once the * Also known as "static RAM usage," this value does not change once the
@ -6592,6 +6599,14 @@ export interface NS {
* @param args - Additional arguments to pass into the new script that is being run. * @param args - Additional arguments to pass into the new script that is being run.
*/ */
spawn(script: string, threadOrOptions?: number | SpawnOptions, ...args: ScriptArg[]): void; spawn(script: string, threadOrOptions?: number | SpawnOptions, ...args: ScriptArg[]): void;
/**
* Returns the currently running script.
* @remarks
* RAM cost: 0 GB
*/
self(): RunningScript;
/** /**
* Terminate the script with the provided PID. * Terminate the script with the provided PID.
* @remarks * @remarks
@ -9502,11 +9517,22 @@ interface GameInfo {
* @public * @public
*/ */
interface AutocompleteData { interface AutocompleteData {
/** All server hostnames */
servers: string[]; servers: string[];
/** All scripts on the current server */
scripts: string[]; scripts: string[];
/** All text files on the current server */
txts: string[]; txts: string[];
/** Netscript Enums */
enums: NSEnums; enums: NSEnums;
/** Parses the flags schema on the already inputted flags */
flags(schema: [string, string | number | boolean | string[]][]): { [key: string]: ScriptArg | string[] }; flags(schema: [string, string | number | boolean | string[]][]): { [key: string]: ScriptArg | string[] };
/** The hostname of the server the script would be running on */
hostname: string;
/** The filename of the script about to be run */
filename: string;
/** The processes running on the host */
processes: ProcessInfo[];
} }
/** /**

@ -312,6 +312,17 @@ export async function getTabCompletionPossibilities(terminalText: string, baseDi
return {}; return {};
} }
}, },
hostname: currServ.hostname,
filename: script.filename,
processes: Array.from(currServ.runningScriptMap.values(), (m) =>
Array.from(m.values(), (r) => ({
pid: r.pid,
filename: r.filename,
threads: r.threads,
args: r.args.slice(),
temporary: r.temporary,
})),
).flat(),
}; };
let pos: string[] = []; let pos: string[] = [];
let pos2: string[] = []; let pos2: string[] = [];