slight change on how wrapping work based on discussion in #development

This commit is contained in:
TheMas3212
2022-04-08 09:57:16 +10:00
parent f0cfc8700a
commit 9b53896732
2 changed files with 46 additions and 47 deletions

View File

@ -11,9 +11,8 @@ type ExternalAPI = {
}; };
type InternalFunction<F extends (...args: unknown[]) => unknown> = ( type InternalFunction<F extends (...args: unknown[]) => unknown> = (
ctx: NetscriptContext, ctx: NetscriptContext
...args: unknown[] ) => (...args: unknown[]) => ReturnType<F>;
) => ReturnType<F>;
export type InternalAPI<API> = { export type InternalAPI<API> = {
[Property in keyof API]: API[Property] extends ExternalFunction [Property in keyof API]: API[Property] extends ExternalFunction
? InternalFunction<API[Property]> ? InternalFunction<API[Property]>
@ -59,46 +58,48 @@ type WrappedNetscriptHelpers = {
getValidPort: (port: any) => IPort; getValidPort: (port: any) => IPort;
}; };
function wrapFunction<T>( function wrapFunction(
helpers: NetscriptHelpers, helpers: NetscriptHelpers,
wrappedAPI: any, wrappedAPI: any,
workerScript: WorkerScript, workerScript: WorkerScript,
func: (ctx: NetscriptContext, ...args: unknown[]) => T, func: (_ctx: NetscriptContext) => (...args: unknown[]) => unknown,
...tree: string[] ...tree: string[]
): void { ): void {
const functionPath = tree.join('.');
const functionName = tree.pop(); const functionName = tree.pop();
if (typeof functionName !== "string") { if (typeof functionName !== "string") {
throw makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api"); throw makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api");
} }
const ctx = { const ctx = {
makeRuntimeErrorMsg: (message: string) => { makeRuntimeErrorMsg: (message: string) => {
return helpers.makeRuntimeErrorMsg(functionName, message); return helpers.makeRuntimeErrorMsg(functionPath, message);
}, },
log: (message: () => string) => { log: (message: () => string) => {
workerScript.log(functionName, message); workerScript.log(functionPath, message);
}, },
workerScript, workerScript,
function: functionName, function: functionName,
helper: { helper: {
updateDynamicRam: (ramCost: number) => helpers.updateDynamicRam(functionName, ramCost), updateDynamicRam: (ramCost: number) => helpers.updateDynamicRam(functionName, ramCost),
makeRuntimeErrorMsg: (msg: string) => helpers.makeRuntimeErrorMsg(functionName, msg), makeRuntimeErrorMsg: (msg: string) => helpers.makeRuntimeErrorMsg(functionPath, msg),
string: (argName: string, v: unknown) => helpers.string(functionName, argName, v), string: (argName: string, v: unknown) => helpers.string(functionPath, argName, v),
number: (argName: string, v: unknown) => helpers.number(functionName, argName, v), number: (argName: string, v: unknown) => helpers.number(functionPath, argName, v),
boolean: helpers.boolean, boolean: helpers.boolean,
getServer: (hostname: string) => helpers.getServer(hostname, functionName), getServer: (hostname: string) => helpers.getServer(hostname, functionPath),
checkSingularityAccess: () => helpers.checkSingularityAccess(functionName), checkSingularityAccess: () => helpers.checkSingularityAccess(functionName),
hack: helpers.hack, 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)); helpers.updateDynamicRam(ctx.function, getRamCost(Player, ...tree, ctx.function));
return func(ctx, ...args); return func(ctx)(...args);
} }
const parent = getNestedProperty(wrappedAPI, ...tree); const parent = getNestedProperty(wrappedAPI, ...tree);
Object.defineProperty(parent, functionName, { Object.defineProperty(parent, functionName, {
value: wrappedFunction, value: wrappedFunction,
writable: true, writable: true,
enumerable: true
}); });
} }

View File

@ -26,88 +26,86 @@ export function NetscriptStanek(
} }
return { return {
giftWidth: function (): number { giftWidth: (_ctx: NetscriptContext) => function (): number {
checkStanekAPIAccess("giftWidth"); checkStanekAPIAccess("giftWidth");
return staneksGift.width(); return staneksGift.width();
}, },
giftHeight: function (): number { giftHeight: (_ctx: NetscriptContext) => function (): number {
checkStanekAPIAccess("giftHeight"); checkStanekAPIAccess("giftHeight");
return staneksGift.height(); return staneksGift.height();
}, },
chargeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): Promise<void> { chargeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): Promise<void> {
const rootX = ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("chargeFragment"); checkStanekAPIAccess("chargeFragment");
const fragment = staneksGift.findFragment(rootX, rootY); 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; const time = staneksGift.inBonus() ? 200 : 1000;
return netscriptDelay(time, workerScript).then(function () { return netscriptDelay(time, workerScript).then(function () {
const charge = staneksGift.charge(player, fragment, workerScript.scriptRef.threads); 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(); return Promise.resolve();
}); });
}, },
fragmentDefinitions: function (ctx: NetscriptContext): IFragment[] { fragmentDefinitions: (_ctx: NetscriptContext) => function (): IFragment[] {
checkStanekAPIAccess("fragmentDefinitions"); checkStanekAPIAccess("fragmentDefinitions");
ctx.log(() => `Returned ${Fragments.length} fragments`); _ctx.log(() => `Returned ${Fragments.length} fragments`);
return Fragments.map((f) => f.copy()); return Fragments.map((f) => f.copy());
}, },
activeFragments: function (ctx: NetscriptContext): IActiveFragment[] { activeFragments: (_ctx: NetscriptContext) => function (): IActiveFragment[] {
checkStanekAPIAccess("activeFragments"); checkStanekAPIAccess("activeFragments");
ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`); _ctx.log(() => `Returned ${staneksGift.fragments.length} fragments`);
return staneksGift.fragments.map((af) => { return staneksGift.fragments.map((af) => {
return { ...af.copy(), ...af.fragment().copy() }; return { ...af.copy(), ...af.fragment().copy() };
}); });
}, },
clearGift: function (ctx: NetscriptContext): void { clearGift: (_ctx: NetscriptContext) => function (): void {
checkStanekAPIAccess("clearGift"); checkStanekAPIAccess("clearGift");
ctx.log(() => `Cleared Stanek's Gift.`); _ctx.log(() => `Cleared Stanek's Gift.`);
staneksGift.clear(); staneksGift.clear();
}, },
canPlaceFragment: function ( canPlaceFragment: (_ctx: NetscriptContext) => function (
ctx: NetscriptContext,
_rootX: unknown, _rootX: unknown,
_rootY: unknown, _rootY: unknown,
_rotation: unknown, _rotation: unknown,
_fragmentId: unknown, _fragmentId: unknown,
): boolean { ): boolean {
const rootX = ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
const rotation = ctx.helper.number("rotation", _rotation); const rotation = _ctx.helper.number("rotation", _rotation);
const fragmentId = ctx.helper.number("fragmentId", _fragmentId); const fragmentId = _ctx.helper.number("fragmentId", _fragmentId);
checkStanekAPIAccess("canPlaceFragment"); checkStanekAPIAccess("canPlaceFragment");
const fragment = FragmentById(fragmentId); 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); const can = staneksGift.canPlace(rootX, rootY, rotation, fragment);
return can; return can;
}, },
placeFragment: function ( placeFragment: (_ctx: NetscriptContext) => function (
ctx: NetscriptContext,
_rootX: unknown, _rootX: unknown,
_rootY: unknown, _rootY: unknown,
_rotation: unknown, _rotation: unknown,
_fragmentId: unknown, _fragmentId: unknown,
): boolean { ): boolean {
const rootX = ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
const rotation = ctx.helper.number("rotation", _rotation); const rotation = _ctx.helper.number("rotation", _rotation);
const fragmentId = ctx.helper.number("fragmentId", _fragmentId); const fragmentId = _ctx.helper.number("fragmentId", _fragmentId);
checkStanekAPIAccess("placeFragment"); checkStanekAPIAccess("placeFragment");
const fragment = FragmentById(fragmentId); 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); return staneksGift.place(rootX, rootY, rotation, fragment);
}, },
getFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): IActiveFragment | undefined { getFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): IActiveFragment | undefined {
const rootX = ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("getFragment"); checkStanekAPIAccess("getFragment");
const fragment = staneksGift.findFragment(rootX, rootY); const fragment = staneksGift.findFragment(rootX, rootY);
if (fragment !== undefined) return fragment.copy(); if (fragment !== undefined) return fragment.copy();
return undefined; return undefined;
}, },
removeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): boolean { removeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): boolean {
const rootX = ctx.helper.number("rootX", _rootX); const rootX = _ctx.helper.number("rootX", _rootX);
const rootY = ctx.helper.number("rootY", _rootY); const rootY = _ctx.helper.number("rootY", _rootY);
checkStanekAPIAccess("removeFragment"); checkStanekAPIAccess("removeFragment");
return staneksGift.delete(rootX, rootY); return staneksGift.delete(rootX, rootY);
}, },