Some cleanup

This commit is contained in:
Snarling 2022-08-09 18:08:04 -04:00
parent 931ea730a5
commit 648b7e84bf
7 changed files with 162 additions and 234 deletions

@ -1,6 +1,6 @@
import { NetscriptContext } from "./APIWrapper";
import { WorkerScript } from "./WorkerScript";
import { GetServer } from "../Server/AllServers";
import { GetAllServers, GetServer } from "../Server/AllServers";
import { Player } from "../Player";
import { ScriptDeath } from "./ScriptDeath";
import { isString } from "../utils/helpers/isString";
@ -28,24 +28,24 @@ import { GangMember } from "../Gang/GangMember";
import { GangMemberTask } from "../Gang/GangMemberTask";
import { RunningScript } from "../Script/RunningScript";
import { toNative } from "../NetscriptFunctions/toNative";
//Helpers that are not specific to use in WorkerScripts/NetscriptContexts should be in src/utils/helpers instead
import { ScriptIdentifier } from "./ScriptIdentifier";
import { findRunningScript, findRunningScriptByPid } from "../Script/ScriptHelpers";
import { RunningScript as IRunningScript } from "../ScriptEditor/NetscriptDefinitions";
import { arrayToString } from "../utils/helpers/arrayToString";
import { HacknetServer } from "../Hacknet/HacknetServer";
import { BaseServer } from "../Server/BaseServer";
export const helpers = {
//Type checking, conversion, validation
string,
number,
scriptArgs,
argsToString,
isScriptErrorMessage,
//Error checking and generation
makeRuntimeRejectMsg,
makeRuntimeErrorMsg,
resolveNetscriptRequestedThreads,
//Checking for API access or flags that would prevent a function from running
checkEnvFlags,
checkSingularityAccess,
//Functionality
netscriptDelay,
updateDynamicRam,
city,
@ -59,25 +59,22 @@ export const helpers = {
gangMember,
gangTask,
log,
getFunctionNames,
getRunningScript,
getRunningScriptByArgs,
getCannotFindRunningScriptErrorMessage,
createPublicRunningScript,
failOnHacknetServer,
};
export type ScriptIdentifier = //This was previously in INetscriptHelper.ts, may move to its own file or a generic types file.
| number
| {
scriptname: string;
hostname: string;
args: ScriptArg[];
};
/** If v is a number or string, returns the string representation. Error for other non-strings. */
/** Convert a provided value v for argument argName to string. If it wasn't originally a string or number, throw. */
function string(ctx: NetscriptContext, argName: string, v: unknown): string {
if (typeof v === "string") return v;
if (typeof v === "number") return v + ""; // cast to string;
throw makeRuntimeErrorMsg(ctx, `'${argName}' should be a string.`);
}
/** Validates v as non-NaN number, or as a string representation of a number, and returns that number. Error on NaN or non-number. */
/** Convert provided value v for argument argName to number. Throw if could not convert to a non-NaN number. */
function number(ctx: NetscriptContext, argName: string, v: unknown): number {
if (typeof v === "string") {
const x = parseFloat(v);
@ -89,7 +86,7 @@ function number(ctx: NetscriptContext, argName: string, v: unknown): number {
throw makeRuntimeErrorMsg(ctx, `'${argName}' should be a number.`);
}
/** Validates args as a ScriptArg[]. Throws an error if it is not. */
/** Returns args back if it is a ScriptArg[]. Throws an error if it is not. */
function scriptArgs(ctx: NetscriptContext, args: unknown) {
if (!isScriptArgs(args)) throw makeRuntimeErrorMsg(ctx, "'args' is not an array of script args");
return args;
@ -104,7 +101,7 @@ function isScriptErrorMessage(msg: string): boolean {
return splitMsg.length == 4;
}
/** Used to convert multiple arguments for tprint or print into a single string. */
/** Convert multiple arguments for tprint or print into a single string. */
function argsToString(args: unknown[]): string {
let out = "";
for (let arg of args) {
@ -253,6 +250,7 @@ function checkEnvFlags(ctx: NetscriptContext): void {
}
}
/** Set a timeout for performing a task, mark the script as busy in the meantime. */
function netscriptDelay(ctx: NetscriptContext, time: number): Promise<void> {
const ws = ctx.workerScript;
return new Promise(function (resolve, reject) {
@ -268,6 +266,7 @@ function netscriptDelay(ctx: NetscriptContext, time: number): Promise<void> {
});
}
/** Adds to dynamic ram cost when calling new ns functions from a script */
function updateDynamicRam(ctx: NetscriptContext, ramCost: number): void {
const ws = ctx.workerScript;
const fnName = ctx.function;
@ -302,6 +301,7 @@ function updateDynamicRam(ctx: NetscriptContext, ramCost: number): void {
}
}
/** Validates the input v as being a CityName. Throws an error if it is not. */
function city(ctx: NetscriptContext, argName: string, v: unknown): CityName {
if (typeof v !== "string") throw makeRuntimeErrorMsg(ctx, `${argName} should be a city name.`);
const s = v as CityName;
@ -574,3 +574,92 @@ function getRunningScriptByArgs(
// If no arguments are specified, return the current RunningScript
return ctx.workerScript.scriptRef;
}
/** Provides an array of all function names on a nested object */
function getFunctionNames(obj: object, prefix: string): string[] {
const functionNames: string[] = [];
for (const [key, value] of Object.entries(obj)) {
if (key === "args") {
continue;
} else if (typeof value == "function") {
functionNames.push(prefix + key);
} else if (typeof value == "object") {
functionNames.push(...getFunctionNames(value, key + "."));
}
}
return functionNames;
}
function getRunningScriptByPid(pid: number): RunningScript | null {
for (const server of GetAllServers()) {
const runningScript = findRunningScriptByPid(pid, server);
if (runningScript) return runningScript;
}
return null;
}
function getRunningScript(ctx: NetscriptContext, ident: ScriptIdentifier): RunningScript | null {
if (typeof ident === "number") {
return getRunningScriptByPid(ident);
} else {
return getRunningScriptByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
}
}
/**
* Helper function for getting the error log message when the user specifies
* a nonexistent running script
* @param {string} fn - Filename of script
* @param {string} hostname - Hostname/ip of the server on which the script resides
* @param {any[]} scriptArgs - Running script's arguments
* @returns {string} Error message to print to logs
*/
function getCannotFindRunningScriptErrorMessage(ident: ScriptIdentifier): string {
if (typeof ident === "number") return `Cannot find running script with pid: ${ident}`;
return `Cannot find running script ${ident.scriptname} on server ${ident.hostname} with args: ${arrayToString(
ident.args,
)}`;
}
/**
* 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
*/
function createPublicRunningScript(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,
};
}
/**
* Used to fail a function if the function's target is a Hacknet Server.
* This is used for functions that should run on normal Servers, but not Hacknet Servers
* @param {Server} server - Target server
* @param {string} callingFn - Name of calling function. For logging purposes
* @returns {boolean} True if the server is a Hacknet Server, false otherwise
*/
function failOnHacknetServer(ctx: NetscriptContext, server: BaseServer, callingFn = ""): boolean {
if (server instanceof HacknetServer) {
ctx.workerScript.log(callingFn, () => `Does not work on Hacknet Servers`);
return true;
} else {
return false;
}
}

@ -0,0 +1,9 @@
import { ScriptArg } from "./ScriptArg";
export type ScriptIdentifier = //This was previously in INetscriptHelper.ts, may move to its own file or a generic types file.
| number
| {
scriptname: string;
hostname: string;
args: ScriptArg[];
};

@ -1,12 +1,8 @@
import $ from "jquery";
import { vsprintf, sprintf } from "sprintf-js";
import { WorkerScriptStartStopEventEmitter } from "./Netscript/WorkerScriptStartStopEventEmitter";
import { BitNodeMultipliers } from "./BitNode/BitNodeMultipliers";
import { CONSTANTS } from "./Constants";
import {
calculateHackingChance,
calculateHackingExpGain,
@ -15,22 +11,14 @@ import {
calculateGrowTime,
calculateWeakenTime,
} from "./Hacking";
import { netscriptCanGrow, netscriptCanWeaken } from "./Hacking/netscriptCanHack";
import { HacknetServer } from "./Hacknet/HacknetServer";
import { Terminal } from "./Terminal";
import { Player } from "./Player";
import { Programs } from "./Programs/Programs";
import { Script } from "./Script/Script";
import { findRunningScript, findRunningScriptByPid } from "./Script/ScriptHelpers";
import { isScriptFilename } from "./Script/isScriptFilename";
import { PromptEvent } from "./ui/React/PromptManager";
import { GetServer, GetAllServers, DeleteServer, AddToAllServers, createUniqueRandomIp } from "./Server/AllServers";
import { RunningScript } from "./Script/RunningScript";
import { GetServer, DeleteServer, AddToAllServers, createUniqueRandomIp } from "./Server/AllServers";
import {
getServerOnNetwork,
numCycleForGrowth,
@ -41,24 +29,18 @@ import {
import { getPurchaseServerCost, getPurchaseServerLimit, getPurchaseServerMaxRam } from "./Server/ServerPurchases";
import { Server } from "./Server/Server";
import { influenceStockThroughServerGrow } from "./StockMarket/PlayerInfluencing";
import { isValidFilePath, removeLeadingSlash } from "./Terminal/DirectoryHelpers";
import { TextFile, getTextFile, createTextFile } from "./TextFile";
import { NetscriptPorts, runScriptFromScript } from "./NetscriptWorker";
import { killWorkerScript } from "./Netscript/killWorkerScript";
import { workerScripts } from "./Netscript/WorkerScripts";
import { WorkerScript } from "./Netscript/WorkerScript";
import { helpers, ScriptIdentifier } from "./Netscript/NetscriptHelpers";
import { helpers } from "./Netscript/NetscriptHelpers";
import { numeralWrapper } from "./ui/numeralFormat";
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions";
import { LogBoxEvents, LogBoxCloserEvents } from "./ui/React/LogBoxManager";
import { arrayToString } from "./utils/helpers/arrayToString";
import { isString } from "./utils/helpers/isString";
import { BaseServer } from "./Server/BaseServer";
import { NetscriptGang } from "./NetscriptFunctions/Gang";
import { NetscriptSleeve } from "./NetscriptFunctions/Sleeve";
import { NetscriptExtra } from "./NetscriptFunctions/Extra";
@ -73,15 +55,9 @@ import { NetscriptFormulas } from "./NetscriptFunctions/Formulas";
import { NetscriptStockMarket } from "./NetscriptFunctions/StockMarket";
import { NetscriptGrafting } from "./NetscriptFunctions/Grafting";
import { IPort } from "./NetscriptPort";
import {
NS as INS,
NS,
Player as INetscriptPlayer,
Gang as IGang,
Bladeburner as IBladeburner,
Stanek as IStanek,
Infiltration as IInfiltration,
RunningScript as IRunningScript,
RecentScript as IRecentScript,
BasicHGWOptions,
ProcessInfo,
@ -90,12 +66,9 @@ import {
BitNodeMultipliers as IBNMults,
Server as IServerDef,
RunningScript as IRunningScriptDef,
// ToastVariant,
} from "./ScriptEditor/NetscriptDefinitions";
import { NetscriptSingularity } from "./NetscriptFunctions/Singularity";
import { toNative } from "./NetscriptFunctions/toNative";
import { dialogBoxCreate } from "./ui/React/DialogBox";
import { SnackbarEvents, ToastVariant } from "./ui/React/Snackbar";
import { checkEnum } from "./utils/helpers/checkEnum";
@ -107,163 +80,13 @@ import { recentScripts } from "./Netscript/RecentScripts";
import { InternalAPI, NetscriptContext, wrapAPI } from "./Netscript/APIWrapper";
import { ScriptArg } from "./Netscript/ScriptArg";
interface NS extends INS {
[key: string]: any;
gang: IGang;
bladeburner: IBladeburner;
stanek: IStanek;
infiltration: IInfiltration;
}
const argsToString = function (args: unknown[]): string {
let out = "";
for (let arg of args) {
if (arg === null) {
out += "null";
continue;
}
if (arg === undefined) {
out += "undefined";
continue;
}
arg = toNative(arg);
out += typeof arg === "object" ? JSON.stringify(arg) : `${arg}`;
}
return out;
};
function getFunctionNames(obj: object, prefix: string): string[] {
const functionNames: string[] = [];
for (const [key, value] of Object.entries(obj)) {
if (key === "args") {
continue;
} else if (typeof value == "function") {
functionNames.push(prefix + key);
} else if (typeof value == "object") {
functionNames.push(...getFunctionNames(value, key + "."));
}
}
return functionNames;
}
const getRunningScriptByPid = function (pid: number): RunningScript | null {
for (const server of GetAllServers()) {
const runningScript = findRunningScriptByPid(pid, server);
if (runningScript) return runningScript;
}
return null;
};
const getRunningScript = (ctx: NetscriptContext, ident: ScriptIdentifier): RunningScript | null => {
if (typeof ident === "number") {
return getRunningScriptByPid(ident);
} else {
return getRunningScriptByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
}
};
/**
* Searches for and returns the RunningScript object for the specified script.
* If the 'fn' argument is not specified, this returns the current RunningScript.
* @param {string} fn - Filename of script
* @param {string} hostname - Hostname/ip of the server on which the script resides
* @param {any[]} scriptArgs - Running script's arguments
* @returns {RunningScript}
* Running script identified by the parameters, or null if no such script
* exists, or the current running script if the first argument 'fn'
* is not specified.
*/
const getRunningScriptByArgs = function (
ctx: NetscriptContext,
fn: string,
hostname: string,
scriptArgs: ScriptArg[],
): RunningScript | null {
if (!Array.isArray(scriptArgs)) {
throw helpers.makeRuntimeRejectMsg(
ctx.workerScript,
`Invalid scriptArgs argument passed into getRunningScript() from ${ctx.function}(). ` +
`This is probably a bug. Please report to game developer`,
);
}
if (fn != null && typeof fn === "string") {
// Get Logs of another script
if (hostname == null) {
hostname = ctx.workerScript.hostname;
}
const server = helpers.getServer(ctx, hostname);
return findRunningScript(fn, scriptArgs, server);
}
// If no arguments are specified, return the current RunningScript
return ctx.workerScript.scriptRef;
};
/**
* Helper function for getting the error log message when the user specifies
* a nonexistent running script
* @param {string} fn - Filename of script
* @param {string} hostname - Hostname/ip of the server on which the script resides
* @param {any[]} scriptArgs - Running script's arguments
* @returns {string} Error message to print to logs
*/
const getCannotFindRunningScriptErrorMessage = function (ident: ScriptIdentifier): string {
if (typeof ident === "number") return `Cannot find running script with pid: ${ident}`;
return `Cannot find running script ${ident.scriptname} on server ${ident.hostname} with args: ${arrayToString(
ident.args,
)}`;
};
/**
* 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,
};
};
/**
* Used to fail a function if the function's target is a Hacknet Server.
* This is used for functions that should run on normal Servers, but not Hacknet Servers
* @param {Server} server - Target server
* @param {string} callingFn - Name of calling function. For logging purposes
* @returns {boolean} True if the server is a Hacknet Server, false otherwise
*/
const failOnHacknetServer = function (ctx:NetscriptContext, server: BaseServer, callingFn = ""): boolean {
if (server instanceof HacknetServer) {
ctx.workerScript.log(callingFn, () => `Does not work on Hacknet Servers`);
return true;
} else {
return false;
}
};
export function NetscriptFunctions(workerScript: WorkerScript): NS {
const wrappedNS = wrapAPI({}, workerScript, ns) as unknown as INS;
const wrappedNS = wrapAPI({}, workerScript, ns) as unknown as NS;
(wrappedNS.args as ScriptArg[]) = workerScript.args;
return wrappedNS;
}
const base: InternalAPI<INS> = {
const base: InternalAPI<NS> = {
args: [],
enums: {
toast: ToastVariant,
@ -586,7 +409,7 @@ const base: InternalAPI<INS> = {
if (args.length === 0) {
throw helpers.makeRuntimeErrorMsg(ctx, "Takes at least 1 argument.");
}
ctx.workerScript.print(argsToString(args));
ctx.workerScript.print(helpers.argsToString(args));
},
printf:
(ctx: NetscriptContext) =>
@ -603,7 +426,7 @@ const base: InternalAPI<INS> = {
if (args.length === 0) {
throw helpers.makeRuntimeErrorMsg(ctx, "Takes at least 1 argument.");
}
const str = argsToString(args);
const str = helpers.argsToString(args);
if (str.startsWith("ERROR") || str.startsWith("FAIL")) {
Terminal.error(`${ctx.workerScript.scriptRef.filename}: ${str}`);
return;
@ -693,9 +516,9 @@ const base: InternalAPI<INS> = {
(ctx: NetscriptContext) =>
(scriptID: unknown, hostname: unknown, ...scriptArgs: unknown[]): string[] => {
const ident = helpers.scriptIdentifier(ctx, scriptID, hostname, scriptArgs);
const runningScriptObj = getRunningScript(ctx, ident);
const runningScriptObj = helpers.getRunningScript(ctx, ident);
if (runningScriptObj == null) {
helpers.log(ctx, () => getCannotFindRunningScriptErrorMessage(ident));
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
return [];
}
@ -705,9 +528,9 @@ const base: InternalAPI<INS> = {
(ctx: NetscriptContext) =>
(scriptID: unknown, hostname: unknown, ...scriptArgs: unknown[]): void => {
const ident = helpers.scriptIdentifier(ctx, scriptID, hostname, scriptArgs);
const runningScriptObj = getRunningScript(ctx, ident);
const runningScriptObj = helpers.getRunningScript(ctx, ident);
if (runningScriptObj == null) {
helpers.log(ctx, () => getCannotFindRunningScriptErrorMessage(ident));
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
return;
}
@ -943,9 +766,9 @@ const base: InternalAPI<INS> = {
}
const server = helpers.getServer(ctx, ident.hostname);
const runningScriptObj = getRunningScriptByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
const runningScriptObj = helpers.getRunningScriptByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
if (runningScriptObj == null) {
helpers.log(ctx, () => getCannotFindRunningScriptErrorMessage(ident));
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
return false;
}
@ -1214,7 +1037,7 @@ const base: InternalAPI<INS> = {
getRecentScripts: () => (): IRecentScript[] => {
return recentScripts.map((rs) => ({
timeOfDeath: rs.timeOfDeath,
...createPublicRunningScript(rs.runningScript),
...helpers.createPublicRunningScript(rs.runningScript),
}));
},
ps:
@ -1311,7 +1134,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "Cannot be executed on this server.");
return 0;
}
if (failOnHacknetServer(ctx, server, "getServerMoneyAvailable")) {
if (helpers.failOnHacknetServer(ctx, server, "getServerMoneyAvailable")) {
return 0;
}
if (server.hostname == "home") {
@ -1331,7 +1154,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "Cannot be executed on this server.");
return 1;
}
if (failOnHacknetServer(ctx, server, "getServerSecurityLevel")) {
if (helpers.failOnHacknetServer(ctx, server, "getServerSecurityLevel")) {
return 1;
}
helpers.log(
@ -1350,7 +1173,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "Cannot be executed on this server.");
return 1;
}
if (failOnHacknetServer(ctx, server, "getServerBaseSecurityLevel")) {
if (helpers.failOnHacknetServer(ctx, server, "getServerBaseSecurityLevel")) {
return 1;
}
helpers.log(
@ -1368,7 +1191,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "Cannot be executed on this server.");
return 1;
}
if (failOnHacknetServer(ctx, server, "getServerMinSecurityLevel")) {
if (helpers.failOnHacknetServer(ctx, server, "getServerMinSecurityLevel")) {
return 1;
}
helpers.log(
@ -1386,7 +1209,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "Cannot be executed on this server.");
return 1;
}
if (failOnHacknetServer(ctx, server, "getServerRequiredHackingLevel")) {
if (helpers.failOnHacknetServer(ctx, server, "getServerRequiredHackingLevel")) {
return 1;
}
helpers.log(
@ -1404,7 +1227,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "Cannot be executed on this server.");
return 0;
}
if (failOnHacknetServer(ctx, server, "getServerMaxMoney")) {
if (helpers.failOnHacknetServer(ctx, server, "getServerMaxMoney")) {
return 0;
}
helpers.log(ctx, () => `returned ${numeralWrapper.formatMoney(server.moneyMax)} for '${server.hostname}'`);
@ -1419,7 +1242,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "Cannot be executed on this server.");
return 1;
}
if (failOnHacknetServer(ctx, server, "getServerGrowth")) {
if (helpers.failOnHacknetServer(ctx, server, "getServerGrowth")) {
return 1;
}
helpers.log(ctx, () => `returned ${server.serverGrowth} for '${server.hostname}'`);
@ -1434,7 +1257,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "Cannot be executed on this server.");
return 5;
}
if (failOnHacknetServer(ctx, server, "getServerNumPortsRequired")) {
if (helpers.failOnHacknetServer(ctx, server, "getServerNumPortsRequired")) {
return 5;
}
helpers.log(ctx, () => `returned ${server.numOpenPortsRequired} for '${server.hostname}'`);
@ -1505,7 +1328,7 @@ const base: InternalAPI<INS> = {
(ctx: NetscriptContext) =>
(fn: unknown, hostname: unknown, ...scriptArgs: unknown[]): boolean => {
const ident = helpers.scriptIdentifier(ctx, fn, hostname, scriptArgs);
return getRunningScript(ctx, ident) !== null;
return helpers.getRunningScript(ctx, ident) !== null;
},
getPurchasedServerLimit: () => (): number => {
return getPurchaseServerLimit();
@ -1907,9 +1730,9 @@ const base: InternalAPI<INS> = {
(ctx: NetscriptContext) =>
(fn: unknown, hostname: unknown, ...args: unknown[]): IRunningScriptDef | null => {
const ident = helpers.scriptIdentifier(ctx, fn, hostname, args);
const runningScript = getRunningScript(ctx, ident);
const runningScript = helpers.getRunningScript(ctx, ident);
if (runningScript === null) return null;
return createPublicRunningScript(runningScript);
return helpers.createPublicRunningScript(runningScript);
},
getHackTime:
(ctx: NetscriptContext) =>
@ -1920,7 +1743,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "invalid for this kind of server");
return Infinity;
}
if (failOnHacknetServer(ctx, server, "getHackTime")) {
if (helpers.failOnHacknetServer(ctx, server, "getHackTime")) {
return Infinity;
}
@ -1935,7 +1758,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "invalid for this kind of server");
return Infinity;
}
if (failOnHacknetServer(ctx, server, "getGrowTime")) {
if (helpers.failOnHacknetServer(ctx, server, "getGrowTime")) {
return Infinity;
}
@ -1950,7 +1773,7 @@ const base: InternalAPI<INS> = {
helpers.log(ctx, () => "invalid for this kind of server");
return Infinity;
}
if (failOnHacknetServer(ctx, server, "getWeakenTime")) {
if (helpers.failOnHacknetServer(ctx, server, "getWeakenTime")) {
return Infinity;
}
@ -1969,9 +1792,9 @@ const base: InternalAPI<INS> = {
(ctx: NetscriptContext) =>
(fn: unknown, hostname: unknown, ...args: unknown[]): number => {
const ident = helpers.scriptIdentifier(ctx, fn, hostname, args);
const runningScript = getRunningScript(ctx, ident);
const runningScript = helpers.getRunningScript(ctx, ident);
if (runningScript == null) {
helpers.log(ctx, () => getCannotFindRunningScriptErrorMessage(ident));
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
return -1;
}
return runningScript.onlineMoneyMade / runningScript.onlineRunningTime;
@ -1987,9 +1810,9 @@ const base: InternalAPI<INS> = {
(ctx: NetscriptContext) =>
(fn: unknown, hostname: unknown, ...args: unknown[]): number => {
const ident = helpers.scriptIdentifier(ctx, fn, hostname, args);
const runningScript = getRunningScript(ctx, ident);
const runningScript = helpers.getRunningScript(ctx, ident);
if (runningScript == null) {
helpers.log(ctx, () => getCannotFindRunningScriptErrorMessage(ident));
helpers.log(ctx, () => helpers.getCannotFindRunningScriptErrorMessage(ident));
return -1;
}
return runningScript.onlineExpGained / runningScript.onlineRunningTime;
@ -2192,4 +2015,4 @@ const ns = {
...NetscriptExtra(),
};
const possibleLogs = Object.fromEntries([...getFunctionNames(ns, "")].map((a) => [a, true]));
const possibleLogs = Object.fromEntries([...helpers.getFunctionNames(ns, "")].map((a) => [a, true]));

@ -276,7 +276,10 @@ export function NetscriptGang(): InternalAPI<IGang> {
if (!equipment) return false;
const res = member.buyUpgrade(equipment, player, gang);
if (res) {
ctx.workerScript.log("gang.purchaseEquipment", () => `Purchased '${equipName}' for Gang member '${memberName}'`);
ctx.workerScript.log(
"gang.purchaseEquipment",
() => `Purchased '${equipName}' for Gang member '${memberName}'`,
);
} else {
ctx.workerScript.log(
"gang.purchaseEquipment",

@ -37,6 +37,7 @@ import { Terminal } from "./Terminal";
import { IPlayer } from "./PersonObjects/IPlayer";
import { ScriptArg } from "./Netscript/ScriptArg";
import { helpers } from "./Netscript/NetscriptHelpers";
import { NS } from "./ScriptEditor/NetscriptDefinitions";
// Netscript Ports are instantiated here
export const NetscriptPorts: IPort[] = [];
@ -114,8 +115,11 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<void> {
}
const interpreterInitialization = function (int: Interpreter, scope: unknown): void {
interface NS1 extends NS {
[key: string]: any;
}
//Add the Netscript environment
const ns = NetscriptFunctions(workerScript);
const ns = NetscriptFunctions(workerScript) as NS1;
for (const name of Object.keys(ns)) {
const entry = ns[name];
if (typeof entry === "function") {
@ -167,7 +171,7 @@ function startNetscript1Script(workerScript: WorkerScript): Promise<void> {
name === "vsprintf" ||
name === "scp" ||
name == "write" ||
name === "tryWrite" ||
name === "tryWritePort" ||
name === "run" ||
name === "exec"
) {