mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 14:28:36 +02:00
PORTS: Support all serializable data. (#1089)
A significant portion of players who use ports are passing objects through them. Currently they are required to handle that themselves via JSON serialization. This PR adds better support for passing objects; which is more convenient, extensive, and optimized (probably, more on this one later). This adds zero overhead to existing (or when passing any primitive types) port usage, and also isn't a breaking change. The questions to debate here are: Should objects be supported in the first place? If so, how exactly do we want to serialize objects? Based on an extensive discussion in Discord, the overwhelming majority answered "yes" to question one. As for question two, that has been much more hotly contested. Ultimately, `structuredClone` was used, despite less-than-stellar performance, because other options were worse either in safety, speed, error-handling, or multiple of the above.
This commit is contained in:
30
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
30
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@@ -24,9 +24,6 @@ interface Skills {
|
||||
*/
|
||||
type CodingContractData = any;
|
||||
|
||||
/** @public */
|
||||
type PortData = string | number;
|
||||
|
||||
/** @public */
|
||||
type ScriptArg = string | number | boolean;
|
||||
|
||||
@@ -1054,20 +1051,23 @@ export interface NetscriptPort {
|
||||
* @remarks
|
||||
* RAM cost: 0 GB
|
||||
*
|
||||
* @returns The data popped off the queue if it was full. */
|
||||
write(value: string | number): PortData | null;
|
||||
* @param value - Data to write, it's cloned with structuredClone().
|
||||
* @returns The data popped off the queue if it was full.
|
||||
*/
|
||||
write(value: any): any;
|
||||
|
||||
/**
|
||||
* Attempt to write data to the port.
|
||||
* @remarks
|
||||
* RAM cost: 0 GB
|
||||
*
|
||||
* @param value - Data to write, it's cloned with structuredClone().
|
||||
* @returns True if the data was added to the port, false if the port was full
|
||||
*/
|
||||
tryWrite(value: string | number): boolean;
|
||||
tryWrite(value: any): boolean;
|
||||
|
||||
/**
|
||||
* Sleeps until the port is written to.
|
||||
* Waits until the port is written to.
|
||||
* @remarks
|
||||
* RAM cost: 0 GB
|
||||
*/
|
||||
@@ -1082,7 +1082,7 @@ export interface NetscriptPort {
|
||||
* If the port is empty, then the string “NULL PORT DATA” will be returned.
|
||||
* @returns the data read.
|
||||
*/
|
||||
read(): PortData;
|
||||
read(): any;
|
||||
|
||||
/**
|
||||
* Retrieve the first element from the port without removing it.
|
||||
@@ -1094,7 +1094,7 @@ export interface NetscriptPort {
|
||||
* the port is empty, the string “NULL PORT DATA” will be returned.
|
||||
* @returns the data read
|
||||
*/
|
||||
peek(): PortData;
|
||||
peek(): any;
|
||||
|
||||
/**
|
||||
* Check if the port is full.
|
||||
@@ -6642,10 +6642,10 @@ export interface NS {
|
||||
* Otherwise, the data will be written normally.
|
||||
*
|
||||
* @param portNumber - Port to attempt to write to. Must be a positive integer.
|
||||
* @param data - Data to write.
|
||||
* @param data - Data to write, it's cloned with structuredClone().
|
||||
* @returns True if the data is successfully written to the port, and false otherwise.
|
||||
*/
|
||||
tryWritePort(portNumber: number, data: string | number): boolean;
|
||||
tryWritePort(portNumber: number, data: any): boolean;
|
||||
|
||||
/**
|
||||
* Listen for a port write.
|
||||
@@ -6685,7 +6685,7 @@ export interface NS {
|
||||
* @param portNumber - Port to peek. Must be a positive integer.
|
||||
* @returns Data in the specified port.
|
||||
*/
|
||||
peek(portNumber: number): PortData;
|
||||
peek(portNumber: number): any;
|
||||
|
||||
/**
|
||||
* Clear data from a file.
|
||||
@@ -6716,10 +6716,10 @@ export interface NS {
|
||||
*
|
||||
* Write data to the given Netscript port.
|
||||
* @param portNumber - Port to write to. Must be a positive integer.
|
||||
* @param data - Data to write.
|
||||
* @param data - Data to write, it's cloned with structuredClone().
|
||||
* @returns The data popped off the queue if it was full, or null if it was not full.
|
||||
*/
|
||||
writePort(portNumber: number, data: string | number): PortData | null;
|
||||
writePort(portNumber: number, data: any): any;
|
||||
/**
|
||||
* Read data from a port.
|
||||
* @remarks
|
||||
@@ -6731,7 +6731,7 @@ export interface NS {
|
||||
* @param portNumber - Port to read from. Must be a positive integer.
|
||||
* @returns The data read.
|
||||
*/
|
||||
readPort(portNumber: number): PortData;
|
||||
readPort(portNumber: number): any;
|
||||
|
||||
/**
|
||||
* Get all data on a port.
|
||||
|
||||
Reference in New Issue
Block a user