Merge branch 'dev' into add-ns-getRecentScripts

This commit is contained in:
hydroflame
2022-04-12 14:21:18 -04:00
committed by GitHub
518 changed files with 15410 additions and 160938 deletions
+153
View File
@@ -0,0 +1,153 @@
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";
type ExternalFunction = (...args: any[]) => any;
type ExternalAPI = {
[string: string]: ExternalAPI | ExternalFunction;
};
type InternalFunction<F extends (...args: unknown[]) => unknown> = (ctx: NetscriptContext) => F;
export type InternalAPI<API> = {
[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 WrappedNetscriptAPI = {
readonly [string: string]: WrappedNetscriptAPI | WrappedNetscriptFunction;
};
export type NetscriptContext = {
makeRuntimeErrorMsg: (message: string) => string;
log: (message: () => string) => void;
workerScript: WorkerScript;
function: string;
helper: WrappedNetscriptHelpers;
};
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;
boolean: (v: unknown) => boolean;
getServer: (hostname: string, callingFnName: string) => BaseServer;
checkSingularityAccess: (func: string) => void;
hack: (hostname: any, manual: any, { threads: requestedThreads, stock }?: any) => Promise<number>;
getValidPort: (funcName: string, port: any) => IPort;
};
type WrappedNetscriptHelpers = {
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(
helpers: NetscriptHelpers,
wrappedAPI: any,
workerScript: WorkerScript,
func: (_ctx: NetscriptContext) => (...args: unknown[]) => unknown,
...tree: string[]
): void {
const functionPath = tree.join(".");
const functionName = tree.pop();
if (typeof functionName !== "string") {
throw makeRuntimeRejectMsg(workerScript, "Failure occured while wrapping netscript api");
}
const ctx = {
makeRuntimeErrorMsg: (message: string) => {
return helpers.makeRuntimeErrorMsg(functionPath, message);
},
log: (message: () => string) => {
workerScript.log(functionPath, message);
},
workerScript,
function: functionName,
helper: {
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),
boolean: helpers.boolean,
getServer: (hostname: string) => helpers.getServer(hostname, functionPath),
checkSingularityAccess: () => helpers.checkSingularityAccess(functionName),
hack: helpers.hack,
getValidPort: (port: any) => helpers.getValidPort(functionPath, port),
},
};
function wrappedFunction(...args: unknown[]): unknown {
helpers.updateDynamicRam(ctx.function, getRamCost(Player, ...tree, ctx.function));
return func(ctx)(...args);
}
const parent = getNestedProperty(wrappedAPI, ...tree);
Object.defineProperty(parent, functionName, {
value: wrappedFunction,
writable: true,
enumerable: true,
});
}
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?");
for (const property of Object.getOwnPropertyNames(namespace)) {
switch (typeof namespace[property]) {
case "function": {
wrapFunction(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property);
break;
}
case "object": {
wrapAPI(helpers, wrappedAPI, workerScript, namespace[property], ...tree, property);
break;
}
default: {
setNestedProperty(wrappedAPI, namespace[property], ...tree, property);
}
}
}
return wrappedAPI;
}
function setNestedProperty(root: any, value: any, ...tree: string[]): any {
let target = root;
const key = tree.pop();
if (typeof key !== "string") {
throw new Error("Failure occured while wrapping netscript api (setNestedProperty)");
}
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;
}
+26 -7
View File
@@ -59,6 +59,8 @@ export const RamCostConstants: IMap<number> = {
ScriptBladeburnerApiBaseRamCost: 4,
ScriptStanekWidth: 0.4,
ScriptStanekHeight: 0.4,
ScriptStanekCharge: 0.4,
ScriptStanekFragmentDefinitions: 0,
ScriptStanekPlacedFragments: 5,
@@ -106,6 +108,7 @@ export const RamCosts: IMap<any> = {
hackAnalyzeSecurity: RamCostConstants.ScriptHackAnalyzeRamCost,
hackAnalyzeChance: RamCostConstants.ScriptHackAnalyzeRamCost,
sleep: 0,
asleep: 0,
share: 2.4,
getSharePower: 0.2,
grow: RamCostConstants.ScriptGrowRamCost,
@@ -133,6 +136,7 @@ export const RamCosts: IMap<any> = {
kill: RamCostConstants.ScriptKillRamCost,
killall: RamCostConstants.ScriptKillRamCost,
exit: 0,
atExit: 0,
scp: RamCostConstants.ScriptScpRamCost,
ls: RamCostConstants.ScriptScanRamCost,
ps: RamCostConstants.ScriptScanRamCost,
@@ -179,6 +183,8 @@ export const RamCosts: IMap<any> = {
getForecast: RamCostConstants.ScriptBuySellStockRamCost,
purchase4SMarketData: RamCostConstants.ScriptBuySellStockRamCost,
purchase4SMarketDataTixApi: RamCostConstants.ScriptBuySellStockRamCost,
purchaseWseAccount: RamCostConstants.ScriptBuySellStockRamCost,
purchaseTixApi: RamCostConstants.ScriptBuySellStockRamCost,
},
getPurchasedServerLimit: RamCostConstants.ScriptGetPurchasedServerLimit,
getPurchasedServerMaxRam: RamCostConstants.ScriptGetPurchasedServerMaxRam,
@@ -206,6 +212,7 @@ export const RamCosts: IMap<any> = {
getScriptExpGain: RamCostConstants.ScriptGetScriptRamCost,
getRunningScript: RamCostConstants.ScriptGetRunningScriptRamCost,
nFormat: 0,
tFormat: 0,
getTimeSinceLastAug: RamCostConstants.ScriptGetHackTimeRamCost,
prompt: 0,
wget: 0,
@@ -213,18 +220,22 @@ export const RamCosts: IMap<any> = {
getPlayer: RamCostConstants.ScriptSingularityFn1RamCost / 4,
mv: 0,
getOwnedSourceFiles: RamCostConstants.ScriptGetOwnedSourceFiles,
tail: 0,
toast: 0,
// Singularity Functions
universityCourse: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
gymWorkout: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
travelToCity: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
goToLocation: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
goToLocation: SF4Cost(RamCostConstants.ScriptSingularityFn3RamCost),
purchaseTor: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
purchaseProgram: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
getCurrentServer: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
connect: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
manualHack: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
installBackdoor: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost),
getDarkwebProgramCost: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost / 4),
getDarkwebPrograms: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost / 4),
getStats: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost / 4),
getCharacterInformation: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost / 4),
hospitalize: SF4Cost(RamCostConstants.ScriptSingularityFn1RamCost / 4),
@@ -355,14 +366,16 @@ export const RamCosts: IMap<any> = {
},
stanek: {
charge: RamCostConstants.ScriptStanekCharge,
giftWidth: RamCostConstants.ScriptStanekWidth,
giftHeight: RamCostConstants.ScriptStanekHeight,
chargeFragment: RamCostConstants.ScriptStanekCharge,
fragmentDefinitions: RamCostConstants.ScriptStanekFragmentDefinitions,
activeFragments: RamCostConstants.ScriptStanekPlacedFragments,
clear: RamCostConstants.ScriptStanekClear,
canPlace: RamCostConstants.ScriptStanekCanPlace,
place: RamCostConstants.ScriptStanekPlace,
get: RamCostConstants.ScriptStanekFragmentAt,
remove: RamCostConstants.ScriptStanekDeleteAt,
clearGift: RamCostConstants.ScriptStanekClear,
canPlaceFragment: RamCostConstants.ScriptStanekCanPlace,
placeFragment: RamCostConstants.ScriptStanekPlace,
getFragment: RamCostConstants.ScriptStanekFragmentAt,
removeFragment: RamCostConstants.ScriptStanekDeleteAt,
},
ui: {
@@ -375,6 +388,12 @@ export const RamCosts: IMap<any> = {
getGameInfo: 0,
},
grafting: {
getAugmentationGraftPrice: 3.75,
getAugmentationGraftTime: 3.75,
graftAugmentation: 7.5,
},
heart: {
// Easter egg function
break: 0,
+37
View File
@@ -0,0 +1,37 @@
import { WorkerScript } from "./WorkerScript";
/**
* Script death marker.
*
* IMPORTANT: the game engine should not base any of it's decisions on the data
* carried in a ScriptDeath instance.
*
* This is because ScriptDeath instances are thrown through player code when a
* script is killed. Which grants the player access to the class and the ability
* to construct new instances with arbitrary data.
*/
export class ScriptDeath {
/** Process ID number. */
pid: number;
/** Filename of the script. */
name: string;
/** IP Address on which the script was running */
hostname: string;
/** Status message in case of script error. */
errorMessage = "";
constructor(ws: WorkerScript) {
this.pid = ws.pid;
this.name = ws.name;
this.hostname = ws.hostname;
this.errorMessage = ws.errorMessage;
Object.freeze(this);
}
}
Object.freeze(ScriptDeath);
Object.freeze(ScriptDeath.prototype);
+2 -2
View File
@@ -60,7 +60,7 @@ export class WorkerScript {
env: Environment;
/**
* Status message in case of script error. Currently unused I think
* Status message in case of script error.
*/
errorMessage = "";
@@ -102,7 +102,7 @@ export class WorkerScript {
scriptRef: RunningScript;
/**
* IP Address on which this script is running
* hostname on which this script is running
*/
hostname: string;
+2 -1
View File
@@ -2,6 +2,7 @@
* Stops an actively-running script (represented by a WorkerScript object)
* and removes it from the global pool of active scripts.
*/
import { ScriptDeath } from "./ScriptDeath";
import { WorkerScript } from "./WorkerScript";
import { workerScripts } from "./WorkerScripts";
import { WorkerScriptStartStopEventEmitter } from "./WorkerScriptStartStopEventEmitter";
@@ -139,7 +140,7 @@ function killNetscriptDelay(workerScript: WorkerScript): void {
if (workerScript.delay) {
clearTimeout(workerScript.delay);
if (workerScript.delayReject) {
workerScript.delayReject(workerScript);
workerScript.delayReject(new ScriptDeath(workerScript));
}
}
}