added basic type check to tryWritePort

This commit is contained in:
phyzical 2022-02-07 22:14:48 +08:00
parent 6ed5a445a8
commit e8954bc1ee
3 changed files with 8 additions and 2 deletions

2
dist/bitburner.d.ts vendored

@ -3905,7 +3905,7 @@ export declare interface NS extends Singularity {
* @param data - Data to write.
* @returns True if the data is successfully written to the port, and false otherwise.
*/
tryWritePort(port: number, data: string[] | number): Promise<boolean>;
tryWritePort(port: number, data: string | number): Promise<boolean>;
/**
* Read content of a file.

@ -1841,6 +1841,12 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
},
tryWritePort: function (port: any, data: any = ""): any {
updateDynamicRam("tryWritePort", getRamCost(Player, "tryWritePort"));
if ((typeof data !== "string" && typeof data !== "number") || isNS(data)) {
throw makeRuntimeErrorMsg(
"writePort",
`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) {

@ -5555,7 +5555,7 @@ export interface NS extends Singularity {
* @param data - Data to write.
* @returns True if the data is successfully written to the port, and false otherwise.
*/
tryWritePort(port: number, data: string[] | number): Promise<boolean>;
tryWritePort(port: number, data: string | number): Promise<boolean>;
/**
* Read content of a file.