mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-21 00:32:51 +02:00
v2.2.1 (#293)
* handle enums differently * Enums are frozen and fed directly to the proxy * Enums are not included in the NSFull definition, allowing samekeys for RamCostTree<API>, InternalAPI<API>, and ExternalAPI<API> * Rewrote a lot of the ramcalc test, with better typing thanks to the samekeys above * Fix ns1 for proxy (args, pid, and enums after above changes were not being added to ns1 scripts.) * Fixed an overview issue where the bars could display inaccurately. Update changelog and bump version to 2.2.1
This commit is contained in:
+17
-30
@@ -1,11 +1,7 @@
|
||||
import { getRamCost } from "./RamCostGenerator";
|
||||
import type { WorkerScript } from "./WorkerScript";
|
||||
import { helpers } from "./NetscriptHelpers";
|
||||
import { ScriptArg } from "./ScriptArg";
|
||||
import { cloneDeep } from "lodash";
|
||||
|
||||
/** Generic type for an enums object */
|
||||
type Enums = Record<string, Record<string, string>>;
|
||||
/** Permissive type for the documented API functions */
|
||||
type APIFn = (...args: any[]) => void;
|
||||
/** Type for the actual wrapped function given to the player */
|
||||
@@ -13,24 +9,14 @@ type WrappedFn = (...args: unknown[]) => unknown;
|
||||
/** Type for internal, unwrapped ctx function that produces an APIFunction */
|
||||
type InternalFn<F extends APIFn> = (ctx: NetscriptContext) => ((...args: unknown[]) => ReturnType<F>) & F;
|
||||
|
||||
// args, enums, and pid are excluded from the API for typing purposes via the definition of NSFull.
|
||||
// They do in fact exist on the external API (but are absent on the internal API and ramcost tree)
|
||||
export type ExternalAPI<API> = {
|
||||
[key in keyof API]: API[key] extends Enums
|
||||
? Enums
|
||||
: key extends "args"
|
||||
? ScriptArg[] // "args" required to be ScriptArg[]
|
||||
: API[key] extends APIFn
|
||||
? WrappedFn
|
||||
: ExternalAPI<API[key]>;
|
||||
[key in keyof API]: API[key] extends APIFn ? WrappedFn : ExternalAPI<API[key]>;
|
||||
};
|
||||
|
||||
export type InternalAPI<API> = {
|
||||
[key in keyof API]: API[key] extends Enums
|
||||
? API[key] & Enums
|
||||
: key extends "args"
|
||||
? ScriptArg[]
|
||||
: API[key] extends APIFn
|
||||
? InternalFn<API[key]>
|
||||
: InternalAPI<API[key]>;
|
||||
[key in keyof API]: API[key] extends APIFn ? InternalFn<API[key]> : InternalAPI<API[key]>;
|
||||
};
|
||||
|
||||
export type NetscriptContext = {
|
||||
@@ -54,40 +40,41 @@ export function NSProxy<API>(
|
||||
ownKeys(__target: unknown) {
|
||||
return Reflect.ownKeys(ns);
|
||||
},
|
||||
getOwnPropertyDescriptor(__target: unknown, key: string) {
|
||||
getOwnPropertyDescriptor(__target: unknown, key: keyof API & string) {
|
||||
if (!Reflect.has(ns, key)) return undefined;
|
||||
return { value: this.get(__target, key, this), configurable: true, enumerable: true, writable: false };
|
||||
},
|
||||
defineProperty(__target: unknown, __key: unknown, __attrs: unknown) {
|
||||
throw new TypeError("ns instances are not modifiable!");
|
||||
},
|
||||
get(__target: unknown, key: string, __receiver: any) {
|
||||
const ours = memoed[key as keyof API];
|
||||
set(__target: unknown, __key: unknown, __attrs: unknown) {
|
||||
throw new TypeError("ns instances are not modifiable!");
|
||||
},
|
||||
get(__target: unknown, key: keyof API & string, __receiver: any) {
|
||||
const ours = memoed[key];
|
||||
if (ours) return ours;
|
||||
|
||||
const field = ns[key as keyof API];
|
||||
const field = ns[key];
|
||||
if (!field) return field;
|
||||
|
||||
if (key === "enums") {
|
||||
const enumObj = Object.freeze(cloneDeep(field as Enums));
|
||||
for (const member of Object.values(enumObj)) Object.freeze(member);
|
||||
return ((memoed[key as keyof API] as Enums) = enumObj);
|
||||
}
|
||||
if (typeof field === "function") {
|
||||
const arrayPath = [...tree, key];
|
||||
const functionPath = arrayPath.join(".");
|
||||
const wrappedFunction = function (...args: unknown[]): unknown {
|
||||
const ctx = { workerScript: ws, function: key, functionPath };
|
||||
const func = field(ctx); //Allows throwing before ram chack
|
||||
const func = field(ctx); //Allows throwing before ram check, for removedFunction
|
||||
helpers.checkEnvFlags(ctx);
|
||||
helpers.updateDynamicRam(ctx, getRamCost(...tree, key));
|
||||
return func(...args);
|
||||
};
|
||||
return ((memoed[key as keyof API] as WrappedFn) = wrappedFunction);
|
||||
return ((memoed[key] as WrappedFn) = wrappedFunction);
|
||||
}
|
||||
if (typeof field === "object") {
|
||||
// TODO unplanned: Make this work generically
|
||||
return ((memoed[key as keyof API] as unknown) = NSProxy(ws, field as InternalAPI<unknown>, [...tree, key]));
|
||||
return ((memoed[key] as ExternalAPI<API[keyof API]>) = NSProxy(ws, field as InternalAPI<API[keyof API]>, [
|
||||
...tree,
|
||||
key,
|
||||
]));
|
||||
}
|
||||
console.warn(`Unexpected data while wrapping API.`, "tree:", tree, "key:", key, "field:", field);
|
||||
throw new Error("Error while wrapping netscript API. See console.");
|
||||
|
||||
Reference in New Issue
Block a user