From ec81fb60c34c4e0b44f061d60d09fc9e4b3272c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Hoekstra?= Date: Thu, 1 Sep 2022 16:13:10 +0200 Subject: [PATCH 1/6] Create ports on demand. Increase max to infinity. --- src/Constants.ts | 2 +- src/Netscript/NetscriptHelpers.ts | 14 ++++++++++---- src/NetscriptFunctions.ts | 9 +++++++-- src/NetscriptWorker.ts | 10 ++++------ 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Constants.ts b/src/Constants.ts index 353eaf8e6..9fcb0a9d1 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -124,7 +124,7 @@ export const CONSTANTS: { // NeuroFlux Governor Augmentation cost multiplier NeuroFluxGovernorLevelMult: 1.14, - NumNetscriptPorts: 20, + NumNetscriptPorts: Infinity, // Server-related constants HomeComputerMaxRam: 1073741824, // 2 ^ 30 diff --git a/src/Netscript/NetscriptHelpers.ts b/src/Netscript/NetscriptHelpers.ts index e420c226d..0cfa2db1b 100644 --- a/src/Netscript/NetscriptHelpers.ts +++ b/src/Netscript/NetscriptHelpers.ts @@ -20,7 +20,7 @@ import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers"; import { CONSTANTS } from "../Constants"; import { influenceStockThroughServerHack } from "../StockMarket/PlayerInfluencing"; -import { IPort } from "../NetscriptPort"; +import { IPort, NetscriptPort } from "../NetscriptPort"; import { NetscriptPorts } from "../NetscriptWorker"; import { IPlayer } from "../PersonObjects/IPlayer"; import { FormulaGang } from "../Gang/formulas/formulas"; @@ -492,10 +492,16 @@ function getValidPort(ctx: NetscriptContext, port: number): IPort { `Trying to use an invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`, ); } - const iport = NetscriptPorts[port - 1]; + let iport = NetscriptPorts.get(port); if (iport == null || !(iport instanceof Object)) { - throw makeRuntimeErrorMsg(ctx, `Could not find port: ${port}. This is a bug. Report to dev.`); + NetscriptPorts.set(port, NetscriptPort()); } + // Try again. + iport = NetscriptPorts.get(port); + if (iport == null || !(iport instanceof Object)) { + throw helpers.makeRuntimeErrorMsg(ctx, `Could not find port: ${port}. This is a bug. Report to dev.`); + } + return iport; } @@ -607,7 +613,7 @@ function getRunningScriptByArgs( throw helpers.makeRuntimeRejectMsg( ctx.workerScript, `Invalid scriptArgs argument passed into getRunningScript() from ${ctx.function}(). ` + - `This is probably a bug. Please report to game developer`, + `This is probably a bug. Please report to game developer`, ); } diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 96952422f..91c43a637 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -54,7 +54,7 @@ import { NetscriptCorporation } from "./NetscriptFunctions/Corporation"; import { NetscriptFormulas } from "./NetscriptFunctions/Formulas"; import { NetscriptStockMarket } from "./NetscriptFunctions/StockMarket"; import { NetscriptGrafting } from "./NetscriptFunctions/Grafting"; -import { IPort } from "./NetscriptPort"; +import { NetscriptPort, IPort } from "./NetscriptPort"; import { NS, Player as INetscriptPlayer, @@ -1533,7 +1533,12 @@ const base: InternalAPI = { `Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`, ); } - const iport = NetscriptPorts[port - 1]; + let iport = NetscriptPorts.get(port); + if (iport == null || !(iport instanceof Object)) { + NetscriptPorts.set(port, NetscriptPort()); + } + // Try again. + iport = NetscriptPorts.get(port); if (iport == null || !(iport instanceof Object)) { throw helpers.makeRuntimeErrorMsg(ctx, `Could not find port: ${port}. This is a bug. Report to dev.`); } diff --git a/src/NetscriptWorker.ts b/src/NetscriptWorker.ts index 6dc79dd0c..c59e64ff9 100644 --- a/src/NetscriptWorker.ts +++ b/src/NetscriptWorker.ts @@ -13,7 +13,7 @@ import { CONSTANTS } from "./Constants"; import { Interpreter } from "./ThirdParty/JSInterpreter"; import { NetscriptFunctions } from "./NetscriptFunctions"; import { executeJSScript, Node } from "./NetscriptJSEvaluator"; -import { NetscriptPort, IPort } from "./NetscriptPort"; +import { IPort } from "./NetscriptPort"; import { RunningScript } from "./Script/RunningScript"; import { getRamUsageFromRunningScript } from "./Script/RunningScriptHelpers"; import { scriptCalculateOfflineProduction } from "./Script/ScriptHelpers"; @@ -38,17 +38,15 @@ import { ScriptArg } from "./Netscript/ScriptArg"; import { helpers } from "./Netscript/NetscriptHelpers"; // Netscript Ports are instantiated here -export const NetscriptPorts: IPort[] = []; -for (let i = 0; i < CONSTANTS.NumNetscriptPorts; ++i) { - NetscriptPorts.push(NetscriptPort()); -} +export const NetscriptPorts: Map = new Map(); export function prestigeWorkerScripts(): void { for (const ws of workerScripts.values()) { ws.env.stopFlag = true; killWorkerScript(ws); } - for (const port of NetscriptPorts) { + + for (const [__, port] of NetscriptPorts.entries()) { port.clear(); } From 55df97d3d5ad0aa696ffa7d869b1fc9c23bfd6e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Hoekstra?= Date: Thu, 1 Sep 2022 16:27:31 +0200 Subject: [PATCH 2/6] Format --- src/Netscript/NetscriptHelpers.ts | 2 +- src/NetscriptWorker.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Netscript/NetscriptHelpers.ts b/src/Netscript/NetscriptHelpers.ts index 0cfa2db1b..2e4f85da3 100644 --- a/src/Netscript/NetscriptHelpers.ts +++ b/src/Netscript/NetscriptHelpers.ts @@ -613,7 +613,7 @@ function getRunningScriptByArgs( throw helpers.makeRuntimeRejectMsg( ctx.workerScript, `Invalid scriptArgs argument passed into getRunningScript() from ${ctx.function}(). ` + - `This is probably a bug. Please report to game developer`, + `This is probably a bug. Please report to game developer`, ); } diff --git a/src/NetscriptWorker.ts b/src/NetscriptWorker.ts index c59e64ff9..6338bdf62 100644 --- a/src/NetscriptWorker.ts +++ b/src/NetscriptWorker.ts @@ -45,7 +45,7 @@ export function prestigeWorkerScripts(): void { ws.env.stopFlag = true; killWorkerScript(ws); } - + for (const [__, port] of NetscriptPorts.entries()) { port.clear(); } From 10a1c2f00de964f5dfc5a1fe46f9998c95da562f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Hoekstra?= Date: Thu, 1 Sep 2022 16:33:31 +0200 Subject: [PATCH 3/6] Lower limit to MAX_SAFE_INTEGER --- src/Constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Constants.ts b/src/Constants.ts index 9fcb0a9d1..c44cc10bc 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -124,7 +124,7 @@ export const CONSTANTS: { // NeuroFlux Governor Augmentation cost multiplier NeuroFluxGovernorLevelMult: 1.14, - NumNetscriptPorts: Infinity, + NumNetscriptPorts: Number.MAX_SAFE_INTEGER, // Server-related constants HomeComputerMaxRam: 1073741824, // 2 ^ 30 From d6e5b45db1265bc129c4feb9992cb4721b80f9e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Hoekstra?= Date: Thu, 1 Sep 2022 16:53:58 +0200 Subject: [PATCH 4/6] Delete now outdated comment --- src/NetscriptWorker.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NetscriptWorker.ts b/src/NetscriptWorker.ts index 6338bdf62..4b3b38c44 100644 --- a/src/NetscriptWorker.ts +++ b/src/NetscriptWorker.ts @@ -37,7 +37,6 @@ import { Terminal } from "./Terminal"; import { ScriptArg } from "./Netscript/ScriptArg"; import { helpers } from "./Netscript/NetscriptHelpers"; -// Netscript Ports are instantiated here export const NetscriptPorts: Map = new Map(); export function prestigeWorkerScripts(): void { From 335eab0f1423d3bfba4f4eff0a6440030c80775b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Hoekstra?= Date: Fri, 2 Sep 2022 19:14:56 +0200 Subject: [PATCH 5/6] tryWritePort now also uses helper function --- src/NetscriptFunctions.ts | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 91c43a637..d941efd01 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1518,34 +1518,15 @@ const base: InternalAPI = { tryWritePort: (ctx: NetscriptContext) => (_port: unknown, data: unknown = ""): Promise => { - let port = helpers.number(ctx, "port", _port); + const port = helpers.number(ctx, "port", _port); if (typeof data !== "string" && typeof data !== "number") { throw helpers.makeRuntimeErrorMsg( ctx, `Trying to write invalid data to a port: only strings and numbers are valid.`, ); } - if (!isNaN(port)) { - port = Math.round(port); - if (port < 1 || port > CONSTANTS.NumNetscriptPorts) { - throw helpers.makeRuntimeErrorMsg( - ctx, - `Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`, - ); - } - let iport = NetscriptPorts.get(port); - if (iport == null || !(iport instanceof Object)) { - NetscriptPorts.set(port, NetscriptPort()); - } - // Try again. - iport = NetscriptPorts.get(port); - if (iport == null || !(iport instanceof Object)) { - throw helpers.makeRuntimeErrorMsg(ctx, `Could not find port: ${port}. This is a bug. Report to dev.`); - } - return Promise.resolve(iport.tryWrite(data)); - } else { - throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${port}`); - } + const iport = helpers.getValidPort(ctx, port); + return Promise.resolve(iport.tryWrite(data)); }, readPort: (ctx: NetscriptContext) => From c7573cd97b6b605277d20c030a7ef34d814b2f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Hoekstra?= Date: Fri, 2 Sep 2022 19:19:00 +0200 Subject: [PATCH 6/6] Remove unused imports. --- src/NetscriptFunctions.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index d941efd01..d20985482 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -31,7 +31,7 @@ import { Server } from "./Server/Server"; import { influenceStockThroughServerGrow } from "./StockMarket/PlayerInfluencing"; import { isValidFilePath, removeLeadingSlash } from "./Terminal/DirectoryHelpers"; import { TextFile, getTextFile, createTextFile } from "./TextFile"; -import { NetscriptPorts, runScriptFromScript } from "./NetscriptWorker"; +import { runScriptFromScript } from "./NetscriptWorker"; import { killWorkerScript } from "./Netscript/killWorkerScript"; import { workerScripts } from "./Netscript/WorkerScripts"; import { WorkerScript } from "./Netscript/WorkerScript"; @@ -54,7 +54,7 @@ import { NetscriptCorporation } from "./NetscriptFunctions/Corporation"; import { NetscriptFormulas } from "./NetscriptFunctions/Formulas"; import { NetscriptStockMarket } from "./NetscriptFunctions/StockMarket"; import { NetscriptGrafting } from "./NetscriptFunctions/Grafting"; -import { NetscriptPort, IPort } from "./NetscriptPort"; +import { IPort } from "./NetscriptPort"; import { NS, Player as INetscriptPlayer,