mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-17 23:08:36 +02:00
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:
@@ -656,7 +656,7 @@ function getRunningScript(ctx: NetscriptContext, ident: ScriptIdentifier): Runni
|
||||
} else {
|
||||
const scripts = getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
|
||||
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,
|
||||
onlineRunningTime: runningScript.onlineRunningTime,
|
||||
pid: runningScript.pid,
|
||||
parent: runningScript.parent,
|
||||
ramUsage: runningScript.ramUsage,
|
||||
server: runningScript.server,
|
||||
tailProperties:
|
||||
|
||||
@@ -519,6 +519,7 @@ export const RamCosts: RamCostTree<NSFull> = {
|
||||
run: RamCostConstants.Run,
|
||||
exec: RamCostConstants.Exec,
|
||||
spawn: RamCostConstants.Spawn,
|
||||
self: 0,
|
||||
kill: RamCostConstants.Kill,
|
||||
killall: RamCostConstants.Kill,
|
||||
exit: 0,
|
||||
|
||||
@@ -776,6 +776,11 @@ export const ns: InternalAPI<NSFull> = {
|
||||
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:
|
||||
(ctx) =>
|
||||
(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)}.`,
|
||||
);
|
||||
const runningScriptObj = new RunningScript(script, singleRamUsage, args);
|
||||
runningScriptObj.parent = workerScript.pid;
|
||||
runningScriptObj.threads = runOpts.threads;
|
||||
runningScriptObj.temporary = runOpts.temporary;
|
||||
|
||||
|
||||
@@ -58,6 +58,9 @@ export class RunningScript {
|
||||
// Process ID. Must be an integer and equals the PID of corresponding WorkerScript
|
||||
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
|
||||
ramUsage: number = RamCostConstants.Base;
|
||||
|
||||
@@ -168,7 +171,7 @@ export class 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");
|
||||
|
||||
|
||||
26
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
26
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@@ -245,6 +245,13 @@ interface RunningScript {
|
||||
onlineRunningTime: number;
|
||||
/** Process ID. Must be an integer */
|
||||
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.
|
||||
* 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.
|
||||
*/
|
||||
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.
|
||||
* @remarks
|
||||
@@ -9502,11 +9517,22 @@ interface GameInfo {
|
||||
* @public
|
||||
*/
|
||||
interface AutocompleteData {
|
||||
/** All server hostnames */
|
||||
servers: string[];
|
||||
/** All scripts on the current server */
|
||||
scripts: string[];
|
||||
/** All text files on the current server */
|
||||
txts: string[];
|
||||
/** Netscript Enums */
|
||||
enums: NSEnums;
|
||||
/** Parses the flags schema on the already inputted flags */
|
||||
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 {};
|
||||
}
|
||||
},
|
||||
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 pos2: string[] = [];
|
||||
|
||||
Reference in New Issue
Block a user