diff --git a/src/Netscript/APIWrapper.ts b/src/Netscript/APIWrapper.ts index 7cc2933bb..03528a520 100644 --- a/src/Netscript/APIWrapper.ts +++ b/src/Netscript/APIWrapper.ts @@ -5,21 +5,27 @@ import type { WorkerScript } from "./WorkerScript"; import { makeRuntimeRejectMsg } from "../NetscriptEvaluator"; import { Player } from "../Player"; - type ExternalFunction = (...args: any[]) => any; type ExternalAPI = { [string: string]: ExternalAPI | ExternalFunction; -} +}; -type InternalFunction unknown> = (ctx: NetscriptContext, ...args: unknown[]) => ReturnType; +type InternalFunction unknown> = ( + ctx: NetscriptContext, + ...args: unknown[] +) => ReturnType; export type InternalAPI = { - [Property in keyof API]: API[Property] extends ExternalFunction ? InternalFunction : API[Property] extends ExternalAPI ? InternalAPI : never; -} + [Property in keyof API]: API[Property] extends ExternalFunction + ? InternalFunction + : API[Property] extends ExternalAPI + ? InternalAPI + : never; +}; type WrappedNetscriptFunction = (...args: unknown[]) => unknown; type WrappedNetscriptAPI = { [string: string]: WrappedNetscriptAPI | WrappedNetscriptFunction; -} +}; export type NetscriptContext = { makeRuntimeErrorMsg: (message: string) => string; @@ -39,7 +45,7 @@ type NetscriptHelpers = { checkSingularityAccess: (func: string) => void; hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise; getValidPort: (funcName: string, port: any) => IPort; -} +}; type WrappedNetscriptHelpers = { updateDynamicRam: (ramCost: number) => void; @@ -47,20 +53,30 @@ type WrappedNetscriptHelpers = { string: (argName: string, v: unknown) => string; number: (argName: string, v: unknown) => number; boolean: (v: unknown) => boolean; - getServer: (hostname: string)=> BaseServer; + getServer: (hostname: string) => BaseServer; checkSingularityAccess: () => void; hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise; getValidPort: (port: any) => IPort; -} +}; -function wrapFunction(helpers: NetscriptHelpers, wrappedAPI: any, workerScript: WorkerScript, func: (ctx: NetscriptContext, ...args: unknown[]) => T, ...tree: string[]): void { +function wrapFunction( + helpers: NetscriptHelpers, + wrappedAPI: any, + workerScript: WorkerScript, + func: (ctx: NetscriptContext, ...args: unknown[]) => T, + ...tree: string[] +): void { const functionName = tree.pop(); - if (typeof functionName !== 'string') { - throw makeRuntimeRejectMsg(workerScript, 'Failure occured while wrapping netscript api'); + if (typeof functionName !== "string") { + throw makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api"); } const ctx = { - makeRuntimeErrorMsg: (message: string) => { return helpers.makeRuntimeErrorMsg(functionName, message); }, - log: (message: () => string) => { workerScript.log(functionName, message); }, + makeRuntimeErrorMsg: (message: string) => { + return helpers.makeRuntimeErrorMsg(functionName, message); + }, + log: (message: () => string) => { + workerScript.log(functionName, message); + }, workerScript, function: functionName, helper: { @@ -72,8 +88,8 @@ function wrapFunction(helpers: NetscriptHelpers, wrappedAPI: any, workerScrip getServer: (hostname: string) => helpers.getServer(hostname, functionName), checkSingularityAccess: () => helpers.checkSingularityAccess(functionName), hack: helpers.hack, - getValidPort: (port: any) => helpers.getValidPort(functionName, port) - } + getValidPort: (port: any) => helpers.getValidPort(functionName, port), + }, }; function wrappedFunction(...args: unknown[]): T { helpers.updateDynamicRam(ctx.function, getRamCost(Player, ...tree, ctx.function)); @@ -82,20 +98,26 @@ function wrapFunction(helpers: NetscriptHelpers, wrappedAPI: any, workerScrip const parent = getNestedProperty(wrappedAPI, ...tree); Object.defineProperty(parent, functionName, { value: wrappedFunction, - writable: true + writable: true, }); } -// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export function wrapAPI(helpers: NetscriptHelpers, wrappedAPI: ExternalAPI, workerScript: WorkerScript, namespace: any, ...tree: string[]): WrappedNetscriptAPI { - if (typeof namespace !== 'object') throw new Error('Invalid namespace?'); +export function wrapAPI( + helpers: NetscriptHelpers, + wrappedAPI: ExternalAPI, + workerScript: WorkerScript, + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + namespace: any, + ...tree: string[] +): WrappedNetscriptAPI { + if (typeof namespace !== "object") throw new Error("Invalid namespace?"); for (const property of Object.getOwnPropertyNames(namespace)) { switch (typeof namespace[property]) { - case 'function': { + case "function": { wrapFunction(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property); break; } - case 'object': { + case "object": { wrapAPI(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property); break; } @@ -110,8 +132,8 @@ export function wrapAPI(helpers: NetscriptHelpers, wrappedAPI: ExternalAPI, work function setNestedProperty(root: any, value: any, ...tree: string[]): any { let target = root; const key = tree.pop(); - if (typeof key !== 'string') { - throw new Error('Failure occured while wrapping netscript api (setNestedProperty)') + if (typeof key !== "string") { + throw new Error("Failure occured while wrapping netscript api (setNestedProperty)"); } for (const branch of tree) { if (target[branch] === undefined) { diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index a24978bb0..0ced4545f 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -492,7 +492,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { const sleeve = NetscriptSleeve(Player, workerScript, helper); const extra = NetscriptExtra(Player, workerScript, helper); const hacknet = NetscriptHacknet(Player, workerScript, helper); - const stanek = wrapAPI(helper, {}, workerScript, NetscriptStanek(Player, workerScript, helper), 'stanek').stanek as unknown as IStanek; + const stanek = wrapAPI(helper, {}, workerScript, NetscriptStanek(Player, workerScript, helper), "stanek") + .stanek as unknown as IStanek; const bladeburner = NetscriptBladeburner(Player, workerScript, helper); const codingcontract = NetscriptCodingContract(Player, workerScript, helper); const corporation = NetscriptCorporation(Player, workerScript, helper); diff --git a/src/NetscriptFunctions/Stanek.ts b/src/NetscriptFunctions/Stanek.ts index dd65f74a4..5dd2d53b4 100644 --- a/src/NetscriptFunctions/Stanek.ts +++ b/src/NetscriptFunctions/Stanek.ts @@ -9,12 +9,16 @@ import { Fragments, FragmentById } from "../CotMG/Fragment"; import { Fragment as IFragment, ActiveFragment as IActiveFragment, - Stanek as IStanek + Stanek as IStanek, } from "../ScriptEditor/NetscriptDefinitions"; import { AugmentationNames } from "../Augmentation/data/AugmentationNames"; import { NetscriptContext, InternalAPI } from "src/Netscript/APIWrapper"; -export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, helper: INetscriptHelper): InternalAPI { +export function NetscriptStanek( + player: IPlayer, + workerScript: WorkerScript, + helper: INetscriptHelper, +): InternalAPI { function checkStanekAPIAccess(func: string): void { if (!player.hasAugmentation(AugmentationNames.StaneksGift1, true)) { helper.makeRuntimeErrorMsg(func, "Requires Stanek's Gift installed."); @@ -60,7 +64,13 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel ctx.log(() => `Cleared Stanek's Gift.`); staneksGift.clear(); }, - canPlaceFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean { + canPlaceFragment: function ( + ctx: NetscriptContext, + _rootX: unknown, + _rootY: unknown, + _rotation: unknown, + _fragmentId: unknown, + ): boolean { const rootX = ctx.helper.number("rootX", _rootX); const rootY = ctx.helper.number("rootY", _rootY); const rotation = ctx.helper.number("rotation", _rotation); @@ -71,7 +81,13 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel const can = staneksGift.canPlace(rootX, rootY, rotation, fragment); return can; }, - placeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown, _rotation: unknown, _fragmentId: unknown): boolean { + placeFragment: function ( + ctx: NetscriptContext, + _rootX: unknown, + _rootY: unknown, + _rotation: unknown, + _fragmentId: unknown, + ): boolean { const rootX = ctx.helper.number("rootX", _rootX); const rootY = ctx.helper.number("rootY", _rootY); const rotation = ctx.helper.number("rotation", _rotation);