Merge pull request #4073 from Hoekstraa/unlimited-ports

API: More ports (previously max 20, now practically unlimited)
This commit is contained in:
hydroflame 2022-09-23 00:51:50 -03:00 committed by GitHub
commit 0044761fd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 29 deletions

@ -124,7 +124,7 @@ export const CONSTANTS: {
// NeuroFlux Governor Augmentation cost multiplier
NeuroFluxGovernorLevelMult: 1.14,
NumNetscriptPorts: 20,
NumNetscriptPorts: Number.MAX_SAFE_INTEGER,
// Server-related constants
HomeComputerMaxRam: 1073741824, // 2 ^ 30

@ -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;
}

@ -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";
@ -1518,29 +1518,15 @@ const base: InternalAPI<NS> = {
tryWritePort:
(ctx: NetscriptContext) =>
(_port: unknown, data: unknown = ""): Promise<any> => {
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.`,
);
}
const iport = NetscriptPorts[port - 1];
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) =>

@ -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";
@ -37,18 +37,15 @@ import { Terminal } from "./Terminal";
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<number, IPort> = 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();
}