mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-26 17:43:48 +01:00
slight change on how wrapping work based on discussion in #development
This commit is contained in:
parent
f0cfc8700a
commit
9b53896732
@ -11,9 +11,8 @@ type ExternalAPI = {
|
||||
};
|
||||
|
||||
type InternalFunction<F extends (...args: unknown[]) => unknown> = (
|
||||
ctx: NetscriptContext,
|
||||
...args: unknown[]
|
||||
) => ReturnType<F>;
|
||||
ctx: NetscriptContext
|
||||
) => (...args: unknown[]) => ReturnType<F>;
|
||||
export type InternalAPI<API> = {
|
||||
[Property in keyof API]: API[Property] extends ExternalFunction
|
||||
? InternalFunction<API[Property]>
|
||||
@ -59,46 +58,48 @@ type WrappedNetscriptHelpers = {
|
||||
getValidPort: (port: any) => IPort;
|
||||
};
|
||||
|
||||
function wrapFunction<T>(
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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<void> {
|
||||
const rootX = ctx.helper.number("rootX", _rootX);
|
||||
const rootY = ctx.helper.number("rootY", _rootY);
|
||||
chargeFragment: (_ctx: NetscriptContext) => function (_rootX: unknown, _rootY: unknown): Promise<void> {
|
||||
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);
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user