mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-27 01:53:48 +01:00
add wrapped helpers to ctx
add ramcheck into wrapper
This commit is contained in:
parent
ea2b444b87
commit
40f74e4a98
@ -19,11 +19,11 @@ export type WrappedNetscriptAPI = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type NetscriptContext = {
|
export type NetscriptContext = {
|
||||||
workerScript: WorkerScript;
|
|
||||||
function: string;
|
|
||||||
makeRuntimeErrorMsg: (message: string) => string;
|
makeRuntimeErrorMsg: (message: string) => string;
|
||||||
log: (message: () => string) => void;
|
log: (message: () => string) => void;
|
||||||
updateDynamicRam: () => void;
|
workerScript: WorkerScript;
|
||||||
|
function: string;
|
||||||
|
helper: WrappedNetscriptHelpers;
|
||||||
};
|
};
|
||||||
|
|
||||||
type NetscriptHelpers = {
|
type NetscriptHelpers = {
|
||||||
@ -38,21 +38,42 @@ type NetscriptHelpers = {
|
|||||||
getValidPort: (funcName: string, port: any) => IPort;
|
getValidPort: (funcName: string, port: any) => IPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WrappedNetscriptHelpers = {
|
||||||
|
updateDynamicRam: (ramCost: number) => void;
|
||||||
|
makeRuntimeErrorMsg: (msg: string) => string;
|
||||||
|
string: (argName: string, v: unknown) => string;
|
||||||
|
number: (argName: string, v: unknown) => number;
|
||||||
|
boolean: (v: unknown) => boolean;
|
||||||
|
getServer: (hostname: string)=> BaseServer;
|
||||||
|
checkSingularityAccess: () => void;
|
||||||
|
hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise<number>;
|
||||||
|
getValidPort: (port: any) => IPort;
|
||||||
|
}
|
||||||
|
|
||||||
function wrapFunction<T>(helpers: NetscriptHelpers, wrappedAPI: any, workerScript: WorkerScript, func: (ctx: NetscriptContext, ...args: unknown[]) => T, ...tree: string[]): void {
|
function wrapFunction<T>(helpers: NetscriptHelpers, wrappedAPI: any, workerScript: WorkerScript, func: (ctx: NetscriptContext, ...args: unknown[]) => T, ...tree: string[]): void {
|
||||||
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 = {
|
||||||
workerScript,
|
|
||||||
function: functionName,
|
|
||||||
makeRuntimeErrorMsg: (message: string) => { return helpers.makeRuntimeErrorMsg(functionName, message); },
|
makeRuntimeErrorMsg: (message: string) => { return helpers.makeRuntimeErrorMsg(functionName, message); },
|
||||||
log: (message: () => string) => { workerScript.log(functionName, message); },
|
log: (message: () => string) => { workerScript.log(functionName, message); },
|
||||||
updateDynamicRam: () => {
|
workerScript,
|
||||||
helpers.updateDynamicRam(functionName, getRamCost(Player, ...tree, functionName));
|
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),
|
||||||
|
boolean: helpers.boolean,
|
||||||
|
getServer: (hostname: string) => helpers.getServer(hostname, functionName),
|
||||||
|
checkSingularityAccess: () => helpers.checkSingularityAccess(functionName),
|
||||||
|
hack: helpers.hack,
|
||||||
|
getValidPort: (port: any) => helpers.getValidPort(functionName, port)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
function wrappedFunction(...args: unknown[]): T {
|
function wrappedFunction(...args: unknown[]): T {
|
||||||
|
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);
|
||||||
|
@ -21,20 +21,17 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
giftWidth: function (ctx: NetscriptContext): number {
|
giftWidth: function (): number {
|
||||||
ctx.updateDynamicRam();
|
|
||||||
checkStanekAPIAccess("giftWidth");
|
checkStanekAPIAccess("giftWidth");
|
||||||
return staneksGift.width();
|
return staneksGift.width();
|
||||||
},
|
},
|
||||||
giftHeight: function (ctx: NetscriptContext): number {
|
giftHeight: function (): number {
|
||||||
ctx.updateDynamicRam();
|
|
||||||
checkStanekAPIAccess("giftHeight");
|
checkStanekAPIAccess("giftHeight");
|
||||||
return staneksGift.height();
|
return staneksGift.height();
|
||||||
},
|
},
|
||||||
chargeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): Promise<void> {
|
chargeFragment: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): Promise<void> {
|
||||||
ctx.updateDynamicRam();
|
const rootX = ctx.helper.number("rootX", _rootX);
|
||||||
const rootX = helper.number("stanek.chargeFragment", "rootX", _rootX);
|
const rootY = ctx.helper.number("rootY", _rootY);
|
||||||
const rootY = helper.number("stanek.chargeFragment", "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}).`);
|
||||||
@ -46,13 +43,11 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
fragmentDefinitions: function (ctx: NetscriptContext): IFragment[] {
|
fragmentDefinitions: function (ctx: NetscriptContext): IFragment[] {
|
||||||
ctx.updateDynamicRam();
|
|
||||||
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: function (ctx: NetscriptContext): IActiveFragment[] {
|
||||||
ctx.updateDynamicRam();
|
|
||||||
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) => {
|
||||||
@ -60,17 +55,15 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
clearGift: function (ctx: NetscriptContext): void {
|
clearGift: function (ctx: NetscriptContext): void {
|
||||||
ctx.updateDynamicRam();
|
|
||||||
checkStanekAPIAccess("clearGift");
|
checkStanekAPIAccess("clearGift");
|
||||||
ctx.log(() => `Cleared Stanek's Gift.`);
|
ctx.log(() => `Cleared Stanek's Gift.`);
|
||||||
staneksGift.clear();
|
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 {
|
||||||
ctx.updateDynamicRam();
|
const rootX = ctx.helper.number("rootX", _rootX);
|
||||||
const rootX = helper.number("stanek.canPlaceFragment", "rootX", _rootX);
|
const rootY = ctx.helper.number("rootY", _rootY);
|
||||||
const rootY = helper.number("stanek.canPlaceFragment", "rootY", _rootY);
|
const rotation = ctx.helper.number("rotation", _rotation);
|
||||||
const rotation = helper.number("stanek.canPlaceFragment", "rotation", _rotation);
|
const fragmentId = ctx.helper.number("fragmentId", _fragmentId);
|
||||||
const fragmentId = helper.number("stanek.canPlaceFragment", "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}`);
|
||||||
@ -78,29 +71,26 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
|
|||||||
return can;
|
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 {
|
||||||
ctx.updateDynamicRam();
|
const rootX = ctx.helper.number("rootX", _rootX);
|
||||||
const rootX = helper.number("stanek.placeFragment", "rootX", _rootX);
|
const rootY = ctx.helper.number("rootY", _rootY);
|
||||||
const rootY = helper.number("stanek.placeFragment", "rootY", _rootY);
|
const rotation = ctx.helper.number("rotation", _rotation);
|
||||||
const rotation = helper.number("stanek.placeFragment", "rotation", _rotation);
|
const fragmentId = ctx.helper.number("fragmentId", _fragmentId);
|
||||||
const fragmentId = helper.number("stanek.placeFragment", "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: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): IActiveFragment | undefined {
|
||||||
ctx.updateDynamicRam();
|
const rootX = ctx.helper.number("rootX", _rootX);
|
||||||
const rootX = helper.number("stanek.getFragment", "rootX", _rootX);
|
const rootY = ctx.helper.number("rootY", _rootY);
|
||||||
const rootY = helper.number("stanek.getFragment", "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: function (ctx: NetscriptContext, _rootX: unknown, _rootY: unknown): boolean {
|
||||||
ctx.updateDynamicRam();
|
const rootX = ctx.helper.number("rootX", _rootX);
|
||||||
const rootX = helper.number("stanek.removeFragment", "rootX", _rootX);
|
const rootY = ctx.helper.number("rootY", _rootY);
|
||||||
const rootY = helper.number("stanek.removeFragment", "rootY", _rootY);
|
|
||||||
checkStanekAPIAccess("removeFragment");
|
checkStanekAPIAccess("removeFragment");
|
||||||
return staneksGift.delete(rootX, rootY);
|
return staneksGift.delete(rootX, rootY);
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user