run formater

This commit is contained in:
TheMas3212
2022-04-07 10:00:54 +10:00
parent 8c58f0676d
commit f0cfc8700a
3 changed files with 68 additions and 29 deletions
src

@ -5,21 +5,27 @@ import type { WorkerScript } from "./WorkerScript";
import { makeRuntimeRejectMsg } from "../NetscriptEvaluator"; import { makeRuntimeRejectMsg } from "../NetscriptEvaluator";
import { Player } from "../Player"; import { Player } from "../Player";
type ExternalFunction = (...args: any[]) => any; type ExternalFunction = (...args: any[]) => any;
type ExternalAPI = { type ExternalAPI = {
[string: string]: ExternalAPI | ExternalFunction; [string: string]: ExternalAPI | ExternalFunction;
} };
type InternalFunction<F extends (...args: unknown[]) => unknown> = (ctx: NetscriptContext, ...args: unknown[]) => ReturnType<F>; type InternalFunction<F extends (...args: unknown[]) => unknown> = (
ctx: NetscriptContext,
...args: unknown[]
) => ReturnType<F>;
export type InternalAPI<API> = { export type InternalAPI<API> = {
[Property in keyof API]: API[Property] extends ExternalFunction ? InternalFunction<API[Property]> : API[Property] extends ExternalAPI ? InternalAPI<API[Property]> : never; [Property in keyof API]: API[Property] extends ExternalFunction
} ? InternalFunction<API[Property]>
: API[Property] extends ExternalAPI
? InternalAPI<API[Property]>
: never;
};
type WrappedNetscriptFunction = (...args: unknown[]) => unknown; type WrappedNetscriptFunction = (...args: unknown[]) => unknown;
type WrappedNetscriptAPI = { type WrappedNetscriptAPI = {
[string: string]: WrappedNetscriptAPI | WrappedNetscriptFunction; [string: string]: WrappedNetscriptAPI | WrappedNetscriptFunction;
} };
export type NetscriptContext = { export type NetscriptContext = {
makeRuntimeErrorMsg: (message: string) => string; makeRuntimeErrorMsg: (message: string) => string;
@ -39,7 +45,7 @@ type NetscriptHelpers = {
checkSingularityAccess: (func: string) => void; checkSingularityAccess: (func: string) => void;
hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise<number>; hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise<number>;
getValidPort: (funcName: string, port: any) => IPort; getValidPort: (funcName: string, port: any) => IPort;
} };
type WrappedNetscriptHelpers = { type WrappedNetscriptHelpers = {
updateDynamicRam: (ramCost: number) => void; updateDynamicRam: (ramCost: number) => void;
@ -47,20 +53,30 @@ type WrappedNetscriptHelpers = {
string: (argName: string, v: unknown) => string; string: (argName: string, v: unknown) => string;
number: (argName: string, v: unknown) => number; number: (argName: string, v: unknown) => number;
boolean: (v: unknown) => boolean; boolean: (v: unknown) => boolean;
getServer: (hostname: string)=> BaseServer; getServer: (hostname: string) => BaseServer;
checkSingularityAccess: () => void; checkSingularityAccess: () => void;
hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise<number>; hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise<number>;
getValidPort: (port: any) => IPort; 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 = {
makeRuntimeErrorMsg: (message: string) => { return helpers.makeRuntimeErrorMsg(functionName, message); }, makeRuntimeErrorMsg: (message: string) => {
log: (message: () => string) => { workerScript.log(functionName, message); }, return helpers.makeRuntimeErrorMsg(functionName, message);
},
log: (message: () => string) => {
workerScript.log(functionName, message);
},
workerScript, workerScript,
function: functionName, function: functionName,
helper: { helper: {
@ -72,8 +88,8 @@ function wrapFunction<T>(helpers: NetscriptHelpers, wrappedAPI: any, workerScrip
getServer: (hostname: string) => helpers.getServer(hostname, functionName), getServer: (hostname: string) => helpers.getServer(hostname, functionName),
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(functionName, port),
} },
}; };
function wrappedFunction(...args: unknown[]): T { function wrappedFunction(...args: unknown[]): T {
helpers.updateDynamicRam(ctx.function, getRamCost(Player, ...tree, ctx.function)); helpers.updateDynamicRam(ctx.function, getRamCost(Player, ...tree, ctx.function));
@ -82,20 +98,26 @@ function wrapFunction<T>(helpers: NetscriptHelpers, wrappedAPI: any, workerScrip
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,
}); });
} }
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export function wrapAPI(
export function wrapAPI(helpers: NetscriptHelpers, wrappedAPI: ExternalAPI, workerScript: WorkerScript, namespace: any, ...tree: string[]): WrappedNetscriptAPI { helpers: NetscriptHelpers,
if (typeof namespace !== 'object') throw new Error('Invalid namespace?'); 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?");
for (const property of Object.getOwnPropertyNames(namespace)) { for (const property of Object.getOwnPropertyNames(namespace)) {
switch (typeof namespace[property]) { switch (typeof namespace[property]) {
case 'function': { case "function": {
wrapFunction(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property); wrapFunction(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property);
break; break;
} }
case 'object': { case "object": {
wrapAPI(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property); wrapAPI(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property);
break; break;
} }
@ -110,8 +132,8 @@ export function wrapAPI(helpers: NetscriptHelpers, wrappedAPI: ExternalAPI, work
function setNestedProperty(root: any, value: any, ...tree: string[]): any { function setNestedProperty(root: any, value: any, ...tree: string[]): any {
let target = root; let target = root;
const key = tree.pop(); const key = tree.pop();
if (typeof key !== 'string') { if (typeof key !== "string") {
throw new Error('Failure occured while wrapping netscript api (setNestedProperty)') throw new Error("Failure occured while wrapping netscript api (setNestedProperty)");
} }
for (const branch of tree) { for (const branch of tree) {
if (target[branch] === undefined) { if (target[branch] === undefined) {

@ -492,7 +492,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
const sleeve = NetscriptSleeve(Player, workerScript, helper); const sleeve = NetscriptSleeve(Player, workerScript, helper);
const extra = NetscriptExtra(Player, workerScript, helper); const extra = NetscriptExtra(Player, workerScript, helper);
const hacknet = NetscriptHacknet(Player, workerScript, helper); const hacknet = NetscriptHacknet(Player, workerScript, helper);
const stanek = wrapAPI(helper, {}, workerScript, NetscriptStanek(Player, workerScript, helper), 'stanek').stanek as unknown as IStanek; const stanek = wrapAPI(helper, {}, workerScript, NetscriptStanek(Player, workerScript, helper), "stanek")
.stanek as unknown as IStanek;
const bladeburner = NetscriptBladeburner(Player, workerScript, helper); const bladeburner = NetscriptBladeburner(Player, workerScript, helper);
const codingcontract = NetscriptCodingContract(Player, workerScript, helper); const codingcontract = NetscriptCodingContract(Player, workerScript, helper);
const corporation = NetscriptCorporation(Player, workerScript, helper); const corporation = NetscriptCorporation(Player, workerScript, helper);

@ -9,12 +9,16 @@ import { Fragments, FragmentById } from "../CotMG/Fragment";
import { import {
Fragment as IFragment, Fragment as IFragment,
ActiveFragment as IActiveFragment, ActiveFragment as IActiveFragment,
Stanek as IStanek Stanek as IStanek,
} from "../ScriptEditor/NetscriptDefinitions"; } from "../ScriptEditor/NetscriptDefinitions";
import { AugmentationNames } from "../Augmentation/data/AugmentationNames"; import { AugmentationNames } from "../Augmentation/data/AugmentationNames";
import { NetscriptContext, InternalAPI } from "src/Netscript/APIWrapper"; import { NetscriptContext, InternalAPI } from "src/Netscript/APIWrapper";
export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, helper: INetscriptHelper): InternalAPI<IStanek> { export function NetscriptStanek(
player: IPlayer,
workerScript: WorkerScript,
helper: INetscriptHelper,
): InternalAPI<IStanek> {
function checkStanekAPIAccess(func: string): void { function checkStanekAPIAccess(func: string): void {
if (!player.hasAugmentation(AugmentationNames.StaneksGift1, true)) { if (!player.hasAugmentation(AugmentationNames.StaneksGift1, true)) {
helper.makeRuntimeErrorMsg(func, "Requires Stanek's Gift installed."); helper.makeRuntimeErrorMsg(func, "Requires Stanek's Gift installed.");
@ -60,7 +64,13 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
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 {
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);
@ -71,7 +81,13 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel
const can = staneksGift.canPlace(rootX, rootY, rotation, fragment); const can = staneksGift.canPlace(rootX, rootY, rotation, fragment);
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 {
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);