NETSCRIPT: Add "temporary" as a RunOption to run/exec/spawn (#432)

This commit is contained in:
David Walker
2023-03-21 15:54:49 -07:00
committed by GitHub
parent 042a476f78
commit 98f7f473b4
22 changed files with 611 additions and 45 deletions
+26 -1
View File
@@ -6,7 +6,7 @@ import { ScriptDeath } from "./ScriptDeath";
import { formatExp, formatMoney, formatRam, formatThreads } from "../ui/formatNumber";
import { ScriptArg } from "./ScriptArg";
import { CityName } from "../Enums";
import { BasicHGWOptions, RunningScript as IRunningScript, Person as IPerson } from "@nsdefs";
import { BasicHGWOptions, RunOptions, RunningScript as IRunningScript, Person as IPerson } from "@nsdefs";
import { Server } from "../Server/Server";
import {
calculateHackingChance,
@@ -41,6 +41,7 @@ export const helpers = {
number,
positiveInteger,
scriptArgs,
runOptions,
argsToString,
makeBasicErrorMsg,
makeRuntimeErrorMsg,
@@ -67,6 +68,12 @@ export const helpers = {
failOnHacknetServer,
};
// RunOptions with non-optional, type-validated members, for passing between internal functions.
export interface CompleteRunOptions {
threads: PositiveInteger;
temporary: boolean;
}
export function assertMember<T extends string>(
ctx: NetscriptContext,
obj: Record<string, T> | T[],
@@ -174,6 +181,23 @@ function scriptArgs(ctx: NetscriptContext, args: unknown) {
return args;
}
function runOptions(ctx: NetscriptContext, thread_or_opt: unknown): CompleteRunOptions {
let threads: any = 1;
let temporary: any = false;
if (typeof thread_or_opt !== "object" || thread_or_opt === null) {
threads = thread_or_opt ?? 1;
} else {
// Lie and pretend it's a RunOptions. It could be anything, we'll deal with that below.
const options = thread_or_opt as RunOptions;
threads = options.threads ?? 1;
temporary = options.temporary ?? false;
}
return {
threads: positiveInteger(ctx, "thread", threads),
temporary: !!temporary,
};
}
/** Convert multiple arguments for tprint or print into a single string. */
function argsToString(args: unknown[]): string {
let out = "";
@@ -718,6 +742,7 @@ function createPublicRunningScript(runningScript: RunningScript): IRunningScript
ramUsage: runningScript.ramUsage,
server: runningScript.server,
threads: runningScript.threads,
temporary: runningScript.temporary,
};
}