2022-03-20 09:32:29 +01:00
|
|
|
import { getRamCost } from "./RamCostGenerator";
|
|
|
|
import type { IPort } from "../NetscriptPort";
|
|
|
|
import type { BaseServer } from "../Server/BaseServer";
|
|
|
|
import type { WorkerScript } from "./WorkerScript";
|
|
|
|
import { makeRuntimeRejectMsg } from "../NetscriptEvaluator";
|
|
|
|
import { Player } from "../Player";
|
2022-07-15 07:51:30 +02:00
|
|
|
import { CityName } from "../Locations/data/CityNames";
|
|
|
|
import { BasicHGWOptions } from "../ScriptEditor/NetscriptDefinitions";
|
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 = {
|
|
|
|
makeRuntimeErrorMsg: (message: string) => string;
|
|
|
|
log: (message: () => string) => void;
|
2022-03-29 23:57:17 +02:00
|
|
|
workerScript: WorkerScript;
|
|
|
|
function: string;
|
|
|
|
helper: WrappedNetscriptHelpers;
|
2022-03-20 09:32:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
type NetscriptHelpers = {
|
|
|
|
updateDynamicRam: (fnName: string, ramCost: number) => void;
|
|
|
|
makeRuntimeErrorMsg: (caller: string, msg: string) => string;
|
|
|
|
string: (funcName: string, argName: string, v: unknown) => string;
|
|
|
|
number: (funcName: string, argName: string, v: unknown) => number;
|
2022-04-12 08:51:10 +02:00
|
|
|
city: (funcName: string, argName: string, v: unknown) => CityName;
|
2022-03-20 09:32:29 +01:00
|
|
|
boolean: (v: unknown) => boolean;
|
2022-05-25 02:16:39 +02:00
|
|
|
getServer: (hostname: string, ctx: NetscriptContext) => BaseServer;
|
2022-03-20 09:32:29 +01:00
|
|
|
checkSingularityAccess: (func: string) => void;
|
2022-05-25 02:16:39 +02:00
|
|
|
hack: (
|
|
|
|
ctx: NetscriptContext,
|
2022-07-15 07:51:30 +02:00
|
|
|
hostname: string,
|
|
|
|
manual: boolean,
|
|
|
|
{ threads: requestedThreads, stock }?: BasicHGWOptions,
|
2022-05-25 02:16:39 +02:00
|
|
|
) => Promise<number>;
|
2022-07-15 07:51:30 +02:00
|
|
|
getValidPort: (funcName: string, port: number) => IPort;
|
2022-04-07 02:00:54 +02:00
|
|
|
};
|
2022-03-20 09:32:29 +01:00
|
|
|
|
2022-03-29 23:57:17 +02:00
|
|
|
type WrappedNetscriptHelpers = {
|
|
|
|
makeRuntimeErrorMsg: (msg: string) => string;
|
|
|
|
string: (argName: string, v: unknown) => string;
|
|
|
|
number: (argName: string, v: unknown) => number;
|
2022-04-12 08:51:10 +02:00
|
|
|
city: (argName: string, v: unknown) => CityName;
|
2022-03-29 23:57:17 +02:00
|
|
|
boolean: (v: unknown) => boolean;
|
2022-04-07 02:00:54 +02:00
|
|
|
getServer: (hostname: string) => BaseServer;
|
2022-03-29 23:57:17 +02:00
|
|
|
checkSingularityAccess: () => void;
|
2022-07-15 07:51:30 +02:00
|
|
|
hack: (hostname: string, manual: boolean, { threads: requestedThreads, stock }?: BasicHGWOptions) => Promise<number>;
|
|
|
|
getValidPort: (port: number) => IPort;
|
2022-04-07 02:00:54 +02:00
|
|
|
};
|
2022-03-29 23:57:17 +02:00
|
|
|
|
2022-04-08 01:57:16 +02:00
|
|
|
function wrapFunction(
|
2022-04-07 02:00:54 +02:00
|
|
|
helpers: NetscriptHelpers,
|
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") {
|
|
|
|
throw makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api");
|
2022-03-20 09:32:29 +01:00
|
|
|
}
|
|
|
|
const ctx = {
|
2022-04-07 02:00:54 +02:00
|
|
|
makeRuntimeErrorMsg: (message: string) => {
|
2022-04-08 01:57:16 +02:00
|
|
|
return helpers.makeRuntimeErrorMsg(functionPath, message);
|
2022-04-07 02:00:54 +02:00
|
|
|
},
|
|
|
|
log: (message: () => string) => {
|
2022-04-08 01:57:16 +02:00
|
|
|
workerScript.log(functionPath, message);
|
2022-04-07 02:00:54 +02:00
|
|
|
},
|
2022-03-29 23:57:17 +02:00
|
|
|
workerScript,
|
|
|
|
function: functionName,
|
|
|
|
helper: {
|
2022-04-08 01:57:16 +02:00
|
|
|
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),
|
2022-04-12 08:51:10 +02:00
|
|
|
city: (argName: string, v: unknown) => helpers.city(functionPath, argName, v),
|
2022-03-29 23:57:17 +02:00
|
|
|
boolean: helpers.boolean,
|
2022-05-25 02:16:39 +02:00
|
|
|
getServer: (hostname: string) => helpers.getServer(hostname, ctx),
|
2022-03-29 23:57:17 +02:00
|
|
|
checkSingularityAccess: () => helpers.checkSingularityAccess(functionName),
|
2022-07-15 07:51:30 +02:00
|
|
|
hack: (hostname: string, manual: boolean, extra?: BasicHGWOptions) => helpers.hack(ctx, hostname, manual, extra),
|
|
|
|
getValidPort: (port: number) => helpers.getValidPort(functionPath, port),
|
2022-04-07 02:00:54 +02:00
|
|
|
},
|
2022-03-20 09:32:29 +01:00
|
|
|
};
|
2022-04-08 01:57:16 +02:00
|
|
|
function wrappedFunction(...args: unknown[]): unknown {
|
2022-03-29 23:57:17 +02:00
|
|
|
helpers.updateDynamicRam(ctx.function, 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
|
|
|
}
|
|
|
|
const parent = getNestedProperty(wrappedAPI, ...tree);
|
|
|
|
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(
|
|
|
|
helpers: NetscriptHelpers,
|
|
|
|
wrappedAPI: ExternalAPI,
|
|
|
|
workerScript: WorkerScript,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
namespace: any,
|
|
|
|
...tree: string[]
|
|
|
|
): WrappedNetscriptAPI {
|
|
|
|
if (typeof namespace !== "object") throw new Error("Invalid namespace?");
|
2022-03-20 09:32:29 +01:00
|
|
|
for (const property of Object.getOwnPropertyNames(namespace)) {
|
|
|
|
switch (typeof namespace[property]) {
|
2022-04-07 02:00:54 +02:00
|
|
|
case "function": {
|
2022-03-20 09:32:29 +01:00
|
|
|
wrapFunction(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property);
|
|
|
|
break;
|
|
|
|
}
|
2022-04-07 02:00:54 +02:00
|
|
|
case "object": {
|
2022-03-20 09:32:29 +01:00
|
|
|
wrapAPI(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
setNestedProperty(wrappedAPI, namespace[property], ...tree, property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wrappedAPI;
|
|
|
|
}
|
|
|
|
|
2022-07-15 07:51:30 +02:00
|
|
|
function setNestedProperty(root: any, value: any, ...tree: string[]): void {
|
2022-03-20 09:32:29 +01:00
|
|
|
let target = root;
|
|
|
|
const key = tree.pop();
|
2022-04-07 02:00:54 +02:00
|
|
|
if (typeof key !== "string") {
|
|
|
|
throw new Error("Failure occured while wrapping netscript api (setNestedProperty)");
|
2022-03-20 09:32:29 +01:00
|
|
|
}
|
|
|
|
for (const branch of tree) {
|
|
|
|
if (target[branch] === undefined) {
|
|
|
|
target[branch] = {};
|
|
|
|
}
|
|
|
|
target = target[branch];
|
|
|
|
}
|
|
|
|
target[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNestedProperty(root: any, ...tree: string[]): any {
|
|
|
|
let target = root;
|
|
|
|
for (const branch of tree) {
|
|
|
|
if (target[branch] === undefined) {
|
|
|
|
target[branch] = {};
|
|
|
|
}
|
|
|
|
target = target[branch];
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|