Fix Static Ram Circumventing for all NS functions

This commit is contained in:
Nikolai Korolev 2022-03-07 16:18:58 +03:00 committed by GitHub
parent e80190e687
commit 25e28df42b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -550,6 +550,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return calculatePercentMoneyHacked(server, Player); return calculatePercentMoneyHacked(server, Player);
}, },
hackAnalyzeSecurity: function (threads: any): number { hackAnalyzeSecurity: function (threads: any): number {
updateDynamicRam("hackAnalyzeSecurity", getRamCost(Player, "hackAnalyzeSecurity"));
return CONSTANTS.ServerFortifyAmount * threads; return CONSTANTS.ServerFortifyAmount * threads;
}, },
hackAnalyzeChance: function (hostname: any): any { hackAnalyzeChance: function (hostname: any): any {
@ -564,6 +565,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return calculateHackingChance(server, Player); return calculateHackingChance(server, Player);
}, },
sleep: function (time: any): any { sleep: function (time: any): any {
updateDynamicRam("sleep", getRamCost(Player, "sleep"));
if (time === undefined) { if (time === undefined) {
throw makeRuntimeErrorMsg("sleep", "Takes 1 argument."); throw makeRuntimeErrorMsg("sleep", "Takes 1 argument.");
} }
@ -573,6 +575,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}); });
}, },
asleep: function (time: any): any { asleep: function (time: any): any {
updateDynamicRam("asleep", getRamCost(Player, "asleep"));
if (time === undefined) { if (time === undefined) {
throw makeRuntimeErrorMsg("asleep", "Takes 1 argument."); throw makeRuntimeErrorMsg("asleep", "Takes 1 argument.");
} }
@ -650,6 +653,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return numCycleForGrowth(server, Number(growth), Player, cores); return numCycleForGrowth(server, Number(growth), Player, cores);
}, },
growthAnalyzeSecurity: function (threads: any): number { growthAnalyzeSecurity: function (threads: any): number {
updateDynamicRam("growthAnalyzeSecurity", getRamCost(Player, "growthAnalyzeSecurity"));
return 2 * CONSTANTS.ServerFortifyAmount * threads; return 2 * CONSTANTS.ServerFortifyAmount * threads;
}, },
weaken: function (hostname: any, { threads: requestedThreads }: any = {}): any { weaken: function (hostname: any, { threads: requestedThreads }: any = {}): any {
@ -702,10 +706,12 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}); });
}, },
weakenAnalyze: function (threads: any, cores: any = 1): number { weakenAnalyze: function (threads: any, cores: any = 1): number {
updateDynamicRam("weakenAnalyze", getRamCost(Player, "weakenAnalyze"));
const coreBonus = 1 + (cores - 1) / 16; const coreBonus = 1 + (cores - 1) / 16;
return CONSTANTS.ServerWeakenAmount * threads * coreBonus * BitNodeMultipliers.ServerWeakenRate; return CONSTANTS.ServerWeakenAmount * threads * coreBonus * BitNodeMultipliers.ServerWeakenRate;
}, },
share: function (): Promise<void> { share: function (): Promise<void> {
updateDynamicRam("share", getRamCost(Player, "share"));
workerScript.log("share", () => "Sharing this computer."); workerScript.log("share", () => "Sharing this computer.");
const end = StartSharing(workerScript.scriptRef.threads * calculateIntelligenceBonus(Player.intelligence, 2)); const end = StartSharing(workerScript.scriptRef.threads * calculateIntelligenceBonus(Player.intelligence, 2));
return netscriptDelay(10000, workerScript).finally(function () { return netscriptDelay(10000, workerScript).finally(function () {
@ -714,21 +720,25 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}); });
}, },
getSharePower: function (): number { getSharePower: function (): number {
updateDynamicRam("getSharePower", getRamCost(Player, "getSharePower"));
return CalculateShareMult(); return CalculateShareMult();
}, },
print: function (...args: any[]): void { print: function (...args: any[]): void {
updateDynamicRam("print", getRamCost(Player, "print"));
if (args.length === 0) { if (args.length === 0) {
throw makeRuntimeErrorMsg("print", "Takes at least 1 argument."); throw makeRuntimeErrorMsg("print", "Takes at least 1 argument.");
} }
workerScript.print(argsToString(args)); workerScript.print(argsToString(args));
}, },
printf: function (format: string, ...args: any[]): void { printf: function (format: string, ...args: any[]): void {
updateDynamicRam("printf", getRamCost(Player, "printf"));
if (typeof format !== "string") { if (typeof format !== "string") {
throw makeRuntimeErrorMsg("printf", "First argument must be string for the format."); throw makeRuntimeErrorMsg("printf", "First argument must be string for the format.");
} }
workerScript.print(vsprintf(format, args)); workerScript.print(vsprintf(format, args));
}, },
tprint: function (...args: any[]): void { tprint: function (...args: any[]): void {
updateDynamicRam("tprint", getRamCost(Player, "tprint"));
if (args.length === 0) { if (args.length === 0) {
throw makeRuntimeErrorMsg("tprint", "Takes at least 1 argument."); throw makeRuntimeErrorMsg("tprint", "Takes at least 1 argument.");
} }
@ -752,6 +762,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
Terminal.print(`${workerScript.scriptRef.filename}: ${str}`); Terminal.print(`${workerScript.scriptRef.filename}: ${str}`);
}, },
tprintf: function (format: any, ...args: any): any { tprintf: function (format: any, ...args: any): any {
updateDynamicRam("tprintf", getRamCost(Player, "tprintf"));
if (typeof format !== "string") { if (typeof format !== "string") {
throw makeRuntimeErrorMsg("tprintf", "First argument must be string for the format."); throw makeRuntimeErrorMsg("tprintf", "First argument must be string for the format.");
} }
@ -776,9 +787,11 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
Terminal.print(`${str}`); Terminal.print(`${str}`);
}, },
clearLog: function (): any { clearLog: function (): any {
updateDynamicRam("clearLog", getRamCost(Player, "clearLog"));
workerScript.scriptRef.clearLog(); workerScript.scriptRef.clearLog();
}, },
disableLog: function (fn: any): any { disableLog: function (fn: any): any {
updateDynamicRam("disableLog", getRamCost(Player, "disableLog"));
if (fn === "ALL") { if (fn === "ALL") {
for (fn of Object.keys(possibleLogs)) { for (fn of Object.keys(possibleLogs)) {
workerScript.disableLogs[fn] = true; workerScript.disableLogs[fn] = true;
@ -792,6 +805,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
} }
}, },
enableLog: function (fn: any): any { enableLog: function (fn: any): any {
updateDynamicRam("enableLog", getRamCost(Player, "enableLog"));
if (fn === "ALL") { if (fn === "ALL") {
for (fn of Object.keys(possibleLogs)) { for (fn of Object.keys(possibleLogs)) {
delete workerScript.disableLogs[fn]; delete workerScript.disableLogs[fn];
@ -804,12 +818,14 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
workerScript.log("enableLog", () => `Enabled logging for ${fn}`); workerScript.log("enableLog", () => `Enabled logging for ${fn}`);
}, },
isLogEnabled: function (fn: any): any { isLogEnabled: function (fn: any): any {
updateDynamicRam("isLogEnabled", getRamCost(Player, "isLogEnabled"));
if (possibleLogs[fn] === undefined) { if (possibleLogs[fn] === undefined) {
throw makeRuntimeErrorMsg("isLogEnabled", `Invalid argument: ${fn}.`); throw makeRuntimeErrorMsg("isLogEnabled", `Invalid argument: ${fn}.`);
} }
return workerScript.disableLogs[fn] ? false : true; return workerScript.disableLogs[fn] ? false : true;
}, },
getScriptLogs: function (fn: any, hostname: any, ...scriptArgs: any): any { getScriptLogs: function (fn: any, hostname: any, ...scriptArgs: any): any {
updateDynamicRam("getScriptLogs", getRamCost(Player, "getScriptLogs"));
const runningScriptObj = getRunningScript(fn, hostname, "getScriptLogs", scriptArgs); const runningScriptObj = getRunningScript(fn, hostname, "getScriptLogs", scriptArgs);
if (runningScriptObj == null) { if (runningScriptObj == null) {
workerScript.log("getScriptLogs", () => getCannotFindRunningScriptErrorMessage(fn, hostname, scriptArgs)); workerScript.log("getScriptLogs", () => getCannotFindRunningScriptErrorMessage(fn, hostname, scriptArgs));
@ -819,6 +835,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return runningScriptObj.logs.slice(); return runningScriptObj.logs.slice();
}, },
tail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any): any { tail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any): any {
updateDynamicRam("tail", getRamCost(Player, "tail"));
let runningScriptObj; let runningScriptObj;
if (arguments.length === 0) { if (arguments.length === 0) {
runningScriptObj = workerScript.scriptRef; runningScriptObj = workerScript.scriptRef;
@ -1085,6 +1102,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return scriptsRunning; return scriptsRunning;
}, },
exit: function (): any { exit: function (): any {
updateDynamicRam("exit", getRamCost(Player, "exit"));
workerScript.running = false; // Prevent workerScript from "finishing execution naturally" workerScript.running = false; // Prevent workerScript from "finishing execution naturally"
if (killWorkerScript(workerScript)) { if (killWorkerScript(workerScript)) {
workerScript.log("exit", () => "Exiting..."); workerScript.log("exit", () => "Exiting...");
@ -1628,6 +1646,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return cost; return cost;
}, },
purchaseServer: function (aname: any, aram: any): any { purchaseServer: function (aname: any, aram: any): any {
updateDynamicRam("purchaseServer", getRamCost(Player, "purchaseServer"));
const name = helper.string("purchaseServer", "name", aname); const name = helper.string("purchaseServer", "name", aname);
const ram = helper.number("purchaseServer", "ram", aram); const ram = helper.number("purchaseServer", "ram", aram);
updateDynamicRam("purchaseServer", getRamCost(Player, "purchaseServer")); updateDynamicRam("purchaseServer", getRamCost(Player, "purchaseServer"));
@ -1772,6 +1791,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return res; return res;
}, },
writePort: function (port: any, data: any = ""): any { writePort: function (port: any, data: any = ""): any {
updateDynamicRam("writePort", getRamCost(Player, "writePort"));
if (typeof data !== "string" && typeof data !== "number") { if (typeof data !== "string" && typeof data !== "number") {
throw makeRuntimeErrorMsg( throw makeRuntimeErrorMsg(
"writePort", "writePort",
@ -1854,6 +1874,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
} }
}, },
readPort: function (port: any): any { readPort: function (port: any): any {
updateDynamicRam("readPort", getRamCost(Player, "readPort"));
// Read from port // Read from port
const iport = helper.getValidPort("readPort", port); const iport = helper.getValidPort("readPort", port);
const x = iport.read(); const x = iport.read();
@ -1913,6 +1934,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return 0; return 0;
}, },
clearPort: function (port: any): any { clearPort: function (port: any): any {
updateDynamicRam("clearPort", getRamCost(Player, "clearPort"));
// Clear port // Clear port
const iport = helper.getValidPort("clearPort", port); const iport = helper.getValidPort("clearPort", port);
return iport.clear(); return iport.clear();
@ -1961,6 +1983,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return suc; return suc;
}, },
getScriptName: function (): any { getScriptName: function (): any {
updateDynamicRam("getScriptName", getRamCost(Player, "getScriptName"));
return workerScript.name; return workerScript.name;
}, },
getScriptRam: function (scriptname: any, hostname: any = workerScript.hostname): any { getScriptRam: function (scriptname: any, hostname: any = workerScript.hostname): any {
@ -2092,6 +2115,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
} }
}, },
nFormat: function (n: any, format: any): any { nFormat: function (n: any, format: any): any {
updateDynamicRam("nFormat", getRamCost(Player, "nFormat"));
if (isNaN(n) || isNaN(parseFloat(n)) || typeof format !== "string") { if (isNaN(n) || isNaN(parseFloat(n)) || typeof format !== "string") {
return ""; return "";
} }
@ -2099,6 +2123,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return numeralWrapper.format(parseFloat(n), format); return numeralWrapper.format(parseFloat(n), format);
}, },
tFormat: function (milliseconds: any, milliPrecision: any = false): any { tFormat: function (milliseconds: any, milliPrecision: any = false): any {
updateDynamicRam("tFormat", getRamCost(Player, "tFormat"));
return convertTimeMsToTimeElapsedString(milliseconds, milliPrecision); return convertTimeMsToTimeElapsedString(milliseconds, milliPrecision);
}, },
getTimeSinceLastAug: function (): any { getTimeSinceLastAug: function (): any {
@ -2106,10 +2131,12 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return Player.playtimeSinceLastAug; return Player.playtimeSinceLastAug;
}, },
alert: function (message: any): void { alert: function (message: any): void {
updateDynamicRam("alert", getRamCost(Player, "alert"));
message = argsToString([message]); message = argsToString([message]);
dialogBoxCreate(message); dialogBoxCreate(message);
}, },
toast: function (message: any, variant: any = "success", duration: any = 2000): void { toast: function (message: any, variant: any = "success", duration: any = 2000): void {
updateDynamicRam("toast", getRamCost(Player, "toast"));
if (!["success", "info", "warning", "error"].includes(variant)) if (!["success", "info", "warning", "error"].includes(variant))
throw new Error(`variant must be one of "success", "info", "warning", or "error"`); throw new Error(`variant must be one of "success", "info", "warning", or "error"`);
@ -2117,6 +2144,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
SnackbarEvents.emit(message, variant, duration); SnackbarEvents.emit(message, variant, duration);
}, },
prompt: function (txt: any): any { prompt: function (txt: any): any {
updateDynamicRam("prompt", getRamCost(Player, "prompt"));
if (!isString(txt)) { if (!isString(txt)) {
txt = JSON.stringify(txt); txt = JSON.stringify(txt);
} }
@ -2129,6 +2157,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}); });
}, },
wget: async function (url: any, target: any, hostname: any = workerScript.hostname): Promise<boolean> { wget: async function (url: any, target: any, hostname: any = workerScript.hostname): Promise<boolean> {
updateDynamicRam("wget", getRamCost(Player, "wget"));
if (!isScriptFilename(target) && !target.endsWith(".txt")) { if (!isScriptFilename(target) && !target.endsWith(".txt")) {
workerScript.log("wget", () => `Invalid target file: '${target}'. Must be a script or text file.`); workerScript.log("wget", () => `Invalid target file: '${target}'. Must be a script or text file.`);
return Promise.resolve(false); return Promise.resolve(false);
@ -2170,7 +2199,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return Math.floor(CONSTANTS.BaseFavorToDonate * BitNodeMultipliers.RepToDonateToFaction); return Math.floor(CONSTANTS.BaseFavorToDonate * BitNodeMultipliers.RepToDonateToFaction);
}, },
getOwnedSourceFiles: function (): SourceFileLvl[] { getOwnedSourceFiles: function (): SourceFileLvl[] {
helper.updateDynamicRam("getOwnedSourceFiles", getRamCost(Player, "getOwnedSourceFiles")); updateDynamicRam("getOwnedSourceFiles", getRamCost(Player, "getOwnedSourceFiles"));
const res: SourceFileLvl[] = []; const res: SourceFileLvl[] = [];
for (let i = 0; i < Player.sourceFiles.length; ++i) { for (let i = 0; i < Player.sourceFiles.length; ++i) {
res.push({ res.push({
@ -2181,7 +2210,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return res; return res;
}, },
getPlayer: function (): INetscriptPlayer { getPlayer: function (): INetscriptPlayer {
helper.updateDynamicRam("getPlayer", getRamCost(Player, "getPlayer")); updateDynamicRam("getPlayer", getRamCost(Player, "getPlayer"));
const data = { const data = {
hacking: Player.hacking, hacking: Player.hacking,
@ -2276,6 +2305,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return data; return data;
}, },
atExit: function (f: any): void { atExit: function (f: any): void {
updateDynamicRam("atExit", getRamCost(Player, "atExit"));
if (typeof f !== "function") { if (typeof f !== "function") {
throw makeRuntimeErrorMsg("atExit", "argument should be function"); throw makeRuntimeErrorMsg("atExit", "argument should be function");
} }