2022-03-20 09:32:29 +01:00
|
|
|
import { getRamCost } from "./RamCostGenerator";
|
|
|
|
import type { WorkerScript } from "./WorkerScript";
|
|
|
|
import { Player } from "../Player";
|
2022-08-08 19:43:41 +02:00
|
|
|
import { helpers } from "./NetscriptHelpers";
|
2022-03-20 09:32:29 +01:00
|
|
|
|
2022-07-15 07:51:30 +02:00
|
|
|
type ExternalFunction = (...args: unknown[]) => unknown;
|
2022-05-24 23:51:48 +02:00
|
|
|
export type ExternalAPI = {
|
2022-03-30 01:11:13 +02:00
|
|
|
[string: string]: ExternalAPI | ExternalFunction;
|
2022-04-07 02:00:54 +02:00
|
|
|
};
|
2022-03-30 01:11:13 +02:00
|
|
|
|
2022-04-09 07:15:45 +02:00
|
|
|
type InternalFunction<F extends (...args: unknown[]) => unknown> = (ctx: NetscriptContext) => F;
|
2022-05-24 23:51:48 +02:00
|
|
|
|
2022-03-30 01:11:13 +02:00
|
|
|
export type InternalAPI<API> = {
|
2022-04-07 02:00:54 +02:00
|
|
|
[Property in keyof API]: API[Property] extends ExternalFunction
|
|
|
|
? InternalFunction<API[Property]>
|
2022-05-25 00:34:00 +02:00
|
|
|
: API[Property] extends object
|
2022-04-07 02:00:54 +02:00
|
|
|
? InternalAPI<API[Property]>
|
|
|
|
: never;
|
|
|
|
};
|
2022-03-30 01:11:13 +02:00
|
|
|
|
|
|
|
type WrappedNetscriptFunction = (...args: unknown[]) => unknown;
|
|
|
|
type WrappedNetscriptAPI = {
|
2022-04-09 07:15:45 +02:00
|
|
|
readonly [string: string]: WrappedNetscriptAPI | WrappedNetscriptFunction;
|
2022-04-07 02:00:54 +02:00
|
|
|
};
|
2022-03-20 09:32:29 +01:00
|
|
|
|
|
|
|
export type NetscriptContext = {
|
2022-03-29 23:57:17 +02:00
|
|
|
workerScript: WorkerScript;
|
|
|
|
function: string;
|
2022-08-08 21:51:50 +02:00
|
|
|
functionPath: string;
|
2022-03-20 09:32:29 +01:00
|
|
|
};
|
|
|
|
|
2022-04-08 01:57:16 +02:00
|
|
|
function wrapFunction(
|
2022-07-15 07:51:30 +02:00
|
|
|
wrappedAPI: ExternalAPI,
|
2022-04-07 02:00:54 +02:00
|
|
|
workerScript: WorkerScript,
|
2022-04-08 01:57:16 +02:00
|
|
|
func: (_ctx: NetscriptContext) => (...args: unknown[]) => unknown,
|
2022-04-07 02:00:54 +02:00
|
|
|
...tree: string[]
|
|
|
|
): void {
|
2022-04-08 02:02:59 +02:00
|
|
|
const functionPath = tree.join(".");
|
2022-03-20 09:32:29 +01:00
|
|
|
const functionName = tree.pop();
|
2022-04-07 02:00:54 +02:00
|
|
|
if (typeof functionName !== "string") {
|
2022-08-08 19:43:41 +02:00
|
|
|
throw helpers.makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api");
|
2022-03-20 09:32:29 +01:00
|
|
|
}
|
|
|
|
const ctx = {
|
2022-03-29 23:57:17 +02:00
|
|
|
workerScript,
|
|
|
|
function: functionName,
|
2022-08-08 21:51:50 +02:00
|
|
|
functionPath,
|
2022-03-20 09:32:29 +01:00
|
|
|
};
|
2022-04-08 01:57:16 +02:00
|
|
|
function wrappedFunction(...args: unknown[]): unknown {
|
2022-08-08 19:43:41 +02:00
|
|
|
helpers.checkEnvFlags(ctx);
|
|
|
|
helpers.updateDynamicRam(ctx, getRamCost(Player, ...tree, ctx.function));
|
2022-04-08 01:57:16 +02:00
|
|
|
return func(ctx)(...args);
|
2022-03-20 09:32:29 +01:00
|
|
|
}
|
2022-08-06 23:53:09 +02:00
|
|
|
const parent = getNestedProperty(wrappedAPI, tree);
|
2022-03-20 09:32:29 +01:00
|
|
|
Object.defineProperty(parent, functionName, {
|
|
|
|
value: wrappedFunction,
|
2022-04-07 02:00:54 +02:00
|
|
|
writable: true,
|
2022-04-08 02:02:59 +02:00
|
|
|
enumerable: true,
|
2022-03-20 09:32:29 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-07 02:00:54 +02:00
|
|
|
export function wrapAPI(
|
|
|
|
wrappedAPI: ExternalAPI,
|
|
|
|
workerScript: WorkerScript,
|
2022-07-20 07:37:41 +02:00
|
|
|
namespace: object,
|
2022-04-07 02:00:54 +02:00
|
|
|
...tree: string[]
|
|
|
|
): WrappedNetscriptAPI {
|
2022-07-20 07:37:41 +02:00
|
|
|
for (const [key, value] of Object.entries(namespace)) {
|
2022-07-20 17:34:49 +02:00
|
|
|
if (typeof value === "function") {
|
2022-08-08 19:43:41 +02:00
|
|
|
wrapFunction(wrappedAPI, workerScript, value, ...tree, key);
|
2022-07-20 17:34:49 +02:00
|
|
|
} else if (Array.isArray(value)) {
|
2022-08-06 23:53:09 +02:00
|
|
|
setNestedProperty(wrappedAPI, value.slice(), key);
|
2022-07-20 17:34:49 +02:00
|
|
|
} else if (typeof value === "object") {
|
2022-08-08 19:43:41 +02:00
|
|
|
wrapAPI(wrappedAPI, workerScript, value, ...tree, key);
|
2022-07-20 17:34:49 +02:00
|
|
|
} else {
|
|
|
|
setNestedProperty(wrappedAPI, value, ...tree, key);
|
2022-03-20 09:32:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wrappedAPI;
|
|
|
|
}
|
|
|
|
|
2022-08-06 23:53:09 +02:00
|
|
|
function setNestedProperty(root: any, value: unknown, ...tree: string[]): void {
|
2022-03-20 09:32:29 +01:00
|
|
|
let target = root;
|
|
|
|
const key = tree.pop();
|
2022-08-06 23:53:09 +02:00
|
|
|
if (!key) throw new Error("Failure occured while wrapping netscript api (setNestedProperty)");
|
|
|
|
for (const branch of tree) {
|
|
|
|
target[branch] ??= {};
|
|
|
|
target = target[branch];
|
2022-03-20 09:32:29 +01:00
|
|
|
}
|
2022-08-06 23:53:09 +02:00
|
|
|
target[key] = value;
|
2022-03-20 09:32:29 +01:00
|
|
|
}
|
|
|
|
|
2022-08-06 23:53:09 +02:00
|
|
|
function getNestedProperty(root: any, tree: string[]): unknown {
|
2022-03-20 09:32:29 +01:00
|
|
|
let target = root;
|
|
|
|
for (const branch of tree) {
|
2022-08-06 23:53:09 +02:00
|
|
|
target[branch] ??= {};
|
2022-03-20 09:32:29 +01:00
|
|
|
target = target[branch];
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|