rename some stuff and close some exploits

This commit is contained in:
Olivier Gagnon
2021-11-12 15:42:57 -05:00
parent 7059bdf08a
commit 6bd6b3061e
19 changed files with 128 additions and 80 deletions

View File

@@ -161,7 +161,7 @@ export const RamCosts: IMap<any> = {
deleteServer: RamCostConstants.ScriptPurchaseServerRamCost,
getPurchasedServers: RamCostConstants.ScriptPurchaseServerRamCost,
write: 0,
tryWrite: 0,
tryWritePort: 0,
read: 0,
peek: 0,
clear: 0,

View File

@@ -63,7 +63,7 @@ import { NetscriptGang } from "./NetscriptFunctions/Gang";
import { NetscriptSleeve } from "./NetscriptFunctions/Sleeve";
import { NetscriptExtra } from "./NetscriptFunctions/Extra";
import { NetscriptHacknet } from "./NetscriptFunctions/Hacknet";
import { NS as INS } from "./ScriptEditor/NetscriptDefinitions";
import { NS as INS, Player as INetscriptPlayer } from "./ScriptEditor/NetscriptDefinitions";
import { NetscriptBladeburner } from "./NetscriptFunctions/Bladeburner";
import { NetscriptCodingContract } from "./NetscriptFunctions/CodingContract";
import { NetscriptCorporation } from "./NetscriptFunctions/Corporation";
@@ -1635,6 +1635,12 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
writePort: function (port: any, data: any = ""): any {
// Write to port
// Port 1-10
if (typeof data !== "string" && typeof data !== "number") {
throw makeRuntimeErrorMsg(
"writePort",
`Trying to write invalid data to a port: only strings and numbers are valid.`,
);
}
port = Math.round(port);
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
throw makeRuntimeErrorMsg(
@@ -1701,23 +1707,23 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
throw makeRuntimeErrorMsg("write", `Invalid argument: ${port}`);
}
},
tryWrite: function (port: any, data: any = ""): any {
updateDynamicRam("tryWrite", getRamCost("tryWrite"));
tryWritePort: function (port: any, data: any = ""): any {
updateDynamicRam("tryWritePort", getRamCost("tryWritePort"));
if (!isNaN(port)) {
port = Math.round(port);
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
throw makeRuntimeErrorMsg(
"tryWrite",
"tryWritePort",
`Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
);
}
const iport = NetscriptPorts[port - 1];
if (iport == null || !(iport instanceof Object)) {
throw makeRuntimeErrorMsg("tryWrite", `Could not find port: ${port}. This is a bug. Report to dev.`);
throw makeRuntimeErrorMsg("tryWritePort", `Could not find port: ${port}. This is a bug. Report to dev.`);
}
return Promise.resolve(iport.tryWrite(data));
} else {
throw makeRuntimeErrorMsg("tryWrite", `Invalid argument: ${port}`);
throw makeRuntimeErrorMsg("tryWritePort", `Invalid argument: ${port}`);
}
},
readPort: function (port: any): any {
@@ -2088,7 +2094,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return Math.floor(CONSTANTS.BaseFavorToDonate * BitNodeMultipliers.RepToDonateToFaction);
},
getPlayer: function (): any {
getPlayer: function (): INetscriptPlayer {
helper.updateDynamicRam("getPlayer", getRamCost("getPlayer"));
const data = {

View File

@@ -4522,7 +4522,7 @@ export interface NS extends Singularity {
/**
* Write data to a file.
* @remarks
* RAM cost: 1 GB
* RAM cost: 0 GB
*
* This function can be used to either write data to a text file (.txt).
*
@@ -4542,7 +4542,7 @@ export interface NS extends Singularity {
/**
* Attempt to write to a port.
* @remarks
* RAM cost: 1 GB
* RAM cost: 0 GB
*
* Attempts to write data to the specified Netscript Port.
* If the port is full, the data will not be written.
@@ -4552,12 +4552,12 @@ export interface NS extends Singularity {
* @param data - Data to write.
* @returns True if the data is successfully written to the port, and false otherwise.
*/
tryWrite(port: number, data: string[] | number): Promise<boolean>;
tryWritePort(port: number, data: string[] | number): Promise<boolean>;
/**
* Read content of a file.
* @remarks
* RAM cost: 1 GB
* RAM cost: 0 GB
*
* This function is used to read data from a port or from a text file (.txt).
*
@@ -4572,7 +4572,7 @@ export interface NS extends Singularity {
/**
* Get a copy of the data from a port without popping it.
* @remarks
* RAM cost: 1 GB
* RAM cost: 0 GB
*
* This function is used to peek at the data from a port. It returns the
* first element in the specified port without removing that element. If
@@ -4613,7 +4613,7 @@ export interface NS extends Singularity {
* Write data to that netscript port.
* @returns The data popped off the queue if it was full.
*/
writePort(port: number, data: any): Promise<any>;
writePort(port: number, data: string | number): Promise<any>;
/**
* Read data from a port.
* @remarks