incorporate read changes

This commit is contained in:
Snarling
2022-08-24 18:42:42 -04:00
parent 1135508683
commit bba9317ef6
2 changed files with 28 additions and 34 deletions
+21 -27
View File
@@ -1465,8 +1465,8 @@ const base: InternalAPI<NS> = {
},
write:
(ctx: NetscriptContext) =>
(_handle: unknown, _data: unknown = "", _mode: unknown = "a"): void => {
let fn = helpers.string(ctx, "handle", _handle);
(_filename: unknown, _data: unknown = "", _mode: unknown = "a"): void => {
let fn = helpers.string(ctx, "handle", _filename);
const data = helpers.string(ctx, "data", _data);
const mode = helpers.string(ctx, "mode", _mode);
if (!isValidFilePath(fn)) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid filepath: ${fn}`);
@@ -1540,33 +1540,27 @@ const base: InternalAPI<NS> = {
},
read:
(ctx: NetscriptContext) =>
(_port: unknown): string => {
const port = helpers.string(ctx, "port", _port);
if (isString(port)) {
// Read from script or text file
const fn = port;
const server = GetServer(ctx.workerScript.hostname);
if (server == null) {
throw helpers.makeRuntimeErrorMsg(ctx, "Error getting Server. This is a bug. Report to dev.");
}
if (isScriptFilename(fn)) {
// Read from script
const script = ctx.workerScript.getScriptOnServer(fn, server);
if (script == null) {
return "";
}
return script.code;
} else {
// Read from text file
const txtFile = getTextFile(fn, server);
if (txtFile !== null) {
return txtFile.text;
} else {
return "";
}
(_filename: unknown): string => {
const fn = helpers.string(ctx, "filename", _filename);
const server = GetServer(ctx.workerScript.hostname);
if (server == null) {
throw helpers.makeRuntimeErrorMsg(ctx, "Error getting Server. This is a bug. Report to dev.");
}
if (isScriptFilename(fn)) {
// Read from script
const script = ctx.workerScript.getScriptOnServer(fn, server);
if (script == null) {
return "";
}
return script.code;
} else {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${port}`);
// Read from text file
const txtFile = getTextFile(fn, server);
if (txtFile !== null) {
return txtFile.text;
} else {
return "";
}
}
},
peek:
+7 -7
View File
@@ -5980,11 +5980,11 @@ export interface NS {
* then the data will be written in “append” mode which means that the data will be added at the
* end of the file.
*
* @param handle - Filename to be written to.
* @param filename - Name of the file to be written to.
* @param data - Data to write.
* @param mode - Defines the write mode.
*/
write(handle: string, data?: string[] | number | string, mode?: "w" | "a"): void;
write(filename: string, data?: string[] | number | string, mode?: "w" | "a"): void;
/**
* Attempt to write to a port.
@@ -6006,15 +6006,15 @@ export interface NS {
* @remarks
* RAM cost: 0 GB
*
* This function is used to read data from a text file (.txt).
* This function is used to read data from a text file (.txt) or script (.script, .js).
*
* This function will return the data in the specified text
* file. If the text file does not exist, an empty string will be returned.
* This function will return the data in the specified file.
* If the file does not exist, an empty string will be returned.
*
* @param handle - Filename to read from.
* @param filename - Name of the file to be read.
* @returns Data in the specified text file.
*/
read(handle: string);
read(filename: string): string;
/**
* Get a copy of the data from a port without popping it.