From 9b53896732c77944d9b9053bbac242a580ddd3ea Mon Sep 17 00:00:00 2001 From: TheMas3212 Date: Fri, 8 Apr 2022 09:57:16 +1000 Subject: [PATCH] slight change on how wrapping work based on discussion in #development --- src/Netscript/APIWrapper.ts | 29 ++++++++------- src/NetscriptFunctions/Stanek.ts | 64 ++++++++++++++++---------------- 2 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/Netscript/APIWrapper.ts b/src/Netscript/APIWrapper.ts index 03528a520..f5ed8c860 100644 --- a/src/Netscript/APIWrapper.ts +++ b/src/Netscript/APIWrapper.ts @@ -11,9 +11,8 @@ type ExternalAPI = { }; type InternalFunction unknown> = ( - ctx: NetscriptContext, - ...args: unknown[] -) => ReturnType; + ctx: NetscriptContext +) => (...args: unknown[]) => ReturnType; export type InternalAPI = { [Property in keyof API]: API[Property] extends ExternalFunction ? InternalFunction @@ -59,46 +58,48 @@ type WrappedNetscriptHelpers = { getValidPort: (port: any) => IPort; }; -function wrapFunction( +function wrapFunction( helpers: NetscriptHelpers, wrappedAPI: any, workerScript: WorkerScript, - func: (ctx: NetscriptContext, ...args: unknown[]) => T, + func: (_ctx: NetscriptContext) => (...args: unknown[]) => unknown, ...tree: string[] ): void { + const functionPath = tree.join('.'); const functionName = tree.pop(); if (typeof functionName !== "string") { throw makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api"); } const ctx = { makeRuntimeErrorMsg: (message: string) => { - return helpers.makeRuntimeErrorMsg(functionName, message); + return helpers.makeRuntimeErrorMsg(functionPath, message); }, log: (message: () => string) => { - workerScript.log(functionName, message); + workerScript.log(functionPath, message); }, workerScript, function: functionName, helper: { updateDynamicRam: (ramCost: number) => helpers.updateDynamicRam(functionName, ramCost), - makeRuntimeErrorMsg: (msg: string) => helpers.makeRuntimeErrorMsg(functionName, msg), - string: (argName: string, v: unknown) => helpers.string(functionName, argName, v), - number: (argName: string, v: unknown) => helpers.number(functionName, argName, v), + makeRuntimeErrorMsg: (msg: string) => helpers.makeRuntimeErrorMsg(functionPath, msg), + string: (argName: string, v: unknown) => helpers.string(functionPath, argName, v), + number: (argName: string, v: unknown) => helpers.number(functionPath, argName, v), boolean: helpers.boolean, - getServer: (hostname: string) => helpers.getServer(hostname, functionName), + getServer: (hostname: string) => helpers.getServer(hostname, functionPath), checkSingularityAccess: () => helpers.checkSingularityAccess(functionName), hack: helpers.hack, - getValidPort: (port: any) => helpers.getValidPort(functionName, port), + getValidPort: (port: any) => helpers.getValidPort(functionPath, port), }, }; - function wrappedFunction(...args: unknown[]): T { + function wrappedFunction(...args: unknown[]): unknown { helpers.updateDynamicRam(ctx.function, getRamCost(Player, ...tree, ctx.function)); - return func(ctx, ...args); + return func(ctx)(...args); } const parent = getNestedProperty(wrappedAPI, ...tree); Object.defineProperty(parent, functionName, { value: wrappedFunction, writable: true, + enumerable: true }); } diff --git a/src/NetscriptFunctions/Stanek.ts b/src/NetscriptFunctions/Stanek.ts index 5dd2d53b4..644bd9115 100644 --- a/src/NetscriptFunctions/Stanek.ts +++ b/src/NetscriptFunctions/Stanek.ts @@ -26,88 +26,86 @@ export function NetscriptStanek( } return { - giftWidth: function (): number { + giftWidth: (_ctx: NetscriptContext) => function (): number { checkStanekAPIAccess("giftWidth"); return staneksGift.width(); }, - giftHeight: function (): number { + giftHeight: (_ctx: NetscriptContext) => function (): number { checkStanekAPIAccess("giftHeight"); return staneksGift.height(); }, - chargeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): Promise { - const rootX = ctx.helper.number("rootX", _rootX); - const rootY = ctx.helper.number("rootY", _rootY); + chargeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): Promise { + const rootX = _ctx.helper.number("rootX", _rootX); + const rootY = _ctx.helper.number("rootY", _rootY); checkStanekAPIAccess("chargeFragment"); const fragment = staneksGift.findFragment(rootX, rootY); - if (!fragment) throw ctx.makeRuntimeErrorMsg(`No fragment with root (${rootX}, ${rootY}).`); + if (!fragment) throw _ctx.makeRuntimeErrorMsg(`No fragment with root (${rootX}, ${rootY}).`); const time = staneksGift.inBonus() ? 200 : 1000; return netscriptDelay(time, workerScript).then(function () { const charge = staneksGift.charge(player, fragment, workerScript.scriptRef.threads); - ctx.log(() => `Charged fragment for ${charge} charge.`); + _ctx.log(() => `Charged fragment for ${charge} charge.`); return Promise.resolve(); }); }, - fragmentDefinitions: function (ctx: NetscriptContext): IFragment[] { + fragmentDefinitions: (_ctx: NetscriptContext) => function (): IFragment[] { checkStanekAPIAccess("fragmentDefinitions"); - ctx.log(() => `Returned ${Fragments.length} fragments`); + _ctx.log(() => `Returned ${Fragments.length} fragments`); return Fragments.map((f) => f.copy()); }, - activeFragments: function (ctx: NetscriptContext): IActiveFragment[] { + activeFragments: (_ctx: NetscriptContext) => function (): IActiveFragment[] { checkStanekAPIAccess("activeFragments"); - ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`); + _ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`); return staneksGift.fragments.map((af) => { return { ...af.copy(), ...af.fragment().copy() }; }); }, - clearGift: function (ctx: NetscriptContext): void { + clearGift: (_ctx: NetscriptContext) => function (): void { checkStanekAPIAccess("clearGift"); - ctx.log(() => `Cleared Stanek's Gift.`); + _ctx.log(() => `Cleared Stanek's Gift.`); staneksGift.clear(); }, - canPlaceFragment: function ( - ctx: NetscriptContext, + canPlaceFragment: (_ctx: NetscriptContext) => function ( _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); - const fragmentId = ctx.helper.number("fragmentId", _fragmentId); + const rootX = _ctx.helper.number("rootX", _rootX); + const rootY = _ctx.helper.number("rootY", _rootY); + const rotation = _ctx.helper.number("rotation", _rotation); + const fragmentId = _ctx.helper.number("fragmentId", _fragmentId); checkStanekAPIAccess("canPlaceFragment"); const fragment = FragmentById(fragmentId); - if (!fragment) throw ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`); + if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`); const can = staneksGift.canPlace(rootX, rootY, rotation, fragment); return can; }, - placeFragment: function ( - ctx: NetscriptContext, + placeFragment: (_ctx: NetscriptContext) => function ( _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); - const fragmentId = ctx.helper.number("fragmentId", _fragmentId); + const rootX = _ctx.helper.number("rootX", _rootX); + const rootY = _ctx.helper.number("rootY", _rootY); + const rotation = _ctx.helper.number("rotation", _rotation); + const fragmentId = _ctx.helper.number("fragmentId", _fragmentId); checkStanekAPIAccess("placeFragment"); const fragment = FragmentById(fragmentId); - if (!fragment) throw ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`); + if (!fragment) throw _ctx.makeRuntimeErrorMsg(`Invalid fragment id: ${fragmentId}`); return staneksGift.place(rootX, rootY, rotation, fragment); }, - getFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): IActiveFragment | undefined { - const rootX = ctx.helper.number("rootX", _rootX); - const rootY = ctx.helper.number("rootY", _rootY); + getFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): IActiveFragment | undefined { + const rootX = _ctx.helper.number("rootX", _rootX); + const rootY = _ctx.helper.number("rootY", _rootY); checkStanekAPIAccess("getFragment"); const fragment = staneksGift.findFragment(rootX, rootY); if (fragment !== undefined) return fragment.copy(); return undefined; }, - removeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): boolean { - const rootX = ctx.helper.number("rootX", _rootX); - const rootY = ctx.helper.number("rootY", _rootY); + removeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): boolean { + const rootX = _ctx.helper.number("rootX", _rootX); + const rootY = _ctx.helper.number("rootY", _rootY); checkStanekAPIAccess("removeFragment"); return staneksGift.delete(rootX, rootY); },