From 4d47785f28f0155751e1430d0edca8bfdcfb13a1 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Tue, 23 Aug 2022 22:06:05 -0400 Subject: [PATCH 1/5] Fix ns documentation for scp and write --- src/NetscriptFunctions.ts | 59 ++++++++++------------ src/ScriptEditor/NetscriptDefinitions.d.ts | 28 +++++----- 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index b1c102245..686526e89 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1465,47 +1465,42 @@ const base: InternalAPI = { }, write: (ctx: NetscriptContext) => - (_port: unknown, _data: unknown = "", _mode: unknown = "a"): void => { - const port = helpers.string(ctx, "port", _port); + (_handle: unknown, _data: unknown = "", _mode: unknown = "a"): void => { + let fn = helpers.string(ctx, "handle", _handle); const data = helpers.string(ctx, "data", _data); const mode = helpers.string(ctx, "mode", _mode); - if (isString(port)) { - // Write to script or text file - let fn = port; - if (!isValidFilePath(fn)) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid filepath: ${fn}`); + if (!isValidFilePath(fn)) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid filepath: ${fn}`); - if (fn.lastIndexOf("/") === 0) fn = removeLeadingSlash(fn); + if (fn.lastIndexOf("/") === 0) fn = removeLeadingSlash(fn); - const server = helpers.getServer(ctx, ctx.workerScript.hostname); + const server = helpers.getServer(ctx, ctx.workerScript.hostname); - if (isScriptFilename(fn)) { - // Write to script - let script = ctx.workerScript.getScriptOnServer(fn, server); - if (script == null) { - // Create a new script - script = new Script(Player, fn, String(data), server.hostname, server.scripts); - server.scripts.push(script); - return script.updateRamUsage(Player, server.scripts); - } - mode === "w" ? (script.code = String(data)) : (script.code += data); + if (isScriptFilename(fn)) { + // Write to script + let script = ctx.workerScript.getScriptOnServer(fn, server); + if (script == null) { + // Create a new script + script = new Script(Player, fn, String(data), server.hostname, server.scripts); + server.scripts.push(script); return script.updateRamUsage(Player, server.scripts); - } else { - // Write to text file - const txtFile = getTextFile(fn, server); - if (txtFile == null) { - createTextFile(fn, String(data), server); - return; - } - if (mode === "w") { - txtFile.write(String(data)); - } else { - txtFile.append(String(data)); - } } - return; + mode === "w" ? (script.code = String(data)) : (script.code += data); + return script.updateRamUsage(Player, server.scripts); } else { - throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${port}`); + // Write to text file + if (!fn.endsWith(".txt")) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid filename: ${fn}`); + const txtFile = getTextFile(fn, server); + if (txtFile == null) { + createTextFile(fn, String(data), server); + return; + } + if (mode === "w") { + txtFile.write(String(data)); + } else { + txtFile.append(String(data)); + } } + return; }, tryWritePort: (ctx: NetscriptContext) => diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index bc5be7694..5d9f0082d 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -5413,25 +5413,25 @@ export interface NS { * ```ts * // NS2: * //Copies foo.lit from the helios server to the home computer: - * await ns.scp("foo.lit", "home", "helios" ); + * ns.scp("foo.lit", "home", "helios" ); * * //Tries to copy three files from rothman-uni to home computer: * files = ["foo1.lit", "foo2.script", "foo3.script"]; - * await ns.scp(files, "home", "rothman-uni"); + * ns.scp(files, "home", "rothman-uni"); * ``` * @example * ```ts * //ns2, copies files from home to a target server * const server = ns.args[0]; * const files = ["hack.js","weaken.js","grow.js"]; - * await ns.scp(files, server, "home"); + * ns.scp(files, server, "home"); * ``` * @param files - Filename or an array of filenames of script/literature files to copy. - * @param source - Host of the source server, which is the server from which the file will be copied. This argument is optional and if it’s omitted the source will be the current server. * @param destination - Host of the destination server, which is the server to which the file will be copied. - * @returns True if the script/literature file is successfully copied over and false otherwise. If the files argument is an array then this function will return true if at least one of the files in the array is successfully copied. + * @param source - Host of the source server, which is the server from which the file will be copied. This argument is optional and if it’s omitted the source will be the current server. + * @returns True if the file is successfully copied over and false otherwise. If the files argument is an array then this function will return false if any of the operations failed. */ - scp(files: string | string[], destination: string, source?: string): Promise; + scp(files: string | string[], destination: string, source?: string): boolean; /** * List files on a server. @@ -5971,20 +5971,20 @@ export interface NS { * @remarks * RAM cost: 0 GB * - * This function can be used to write data to a text file (.txt). + * This function can be used to write data to a text file (.txt) or a script (.js or .script). * - * This function will write data to that text file. If the specified text file does not exist, + * This function will write data to that file. If the specified file does not exist, * then it will be created. The third argument mode, defines how the data will be written to - * the text file. If *mode is set to “w”, then the data is written in “write” mode which means - * that it will overwrite all existing data on the text file. If mode is set to any other value + * the file. If *mode is set to “w”, then the data is written in “write” mode which means + * that it will overwrite all existing data on the file. If mode is set to any other value * then the data will be written in “append” mode which means that the data will be added at the - * end of the text file. + * end of the file. * - * @param handle - Filename of the text file that will be written to. + * @param handle - Filename to be written to. * @param data - Data to write. - * @param mode - Defines the write mode. Only valid when writing to text files. + * @param mode - Defines the write mode. */ - write(handle: string, data?: string[] | number | string, mode?: "w" | "a"): Promise; + write(handle: string, data?: string[] | number | string, mode?: "w" | "a"): void; /** * Attempt to write to a port. From 11355086839e33bab554b76995ef47b6ae9b0634 Mon Sep 17 00:00:00 2001 From: MycroftJr <33704388+mycroftjr@users.noreply.github.com> Date: Wed, 24 Aug 2022 01:56:05 -0700 Subject: [PATCH 2/5] fix ns.read return type --- src/ScriptEditor/NetscriptDefinitions.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 5d9f0082d..fa92dceff 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -6014,7 +6014,7 @@ export interface NS { * @param handle - Filename to read from. * @returns Data in the specified text file. */ - read(handle: string): PortData; + read(handle: string); /** * Get a copy of the data from a port without popping it. From bba9317ef634f4e72051814b5982ff09ccd1c21b Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Wed, 24 Aug 2022 18:42:42 -0400 Subject: [PATCH 3/5] incorporate read changes --- src/NetscriptFunctions.ts | 48 ++++++++++------------ src/ScriptEditor/NetscriptDefinitions.d.ts | 14 +++---- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 686526e89..e1a528e0b 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1465,8 +1465,8 @@ const base: InternalAPI = { }, 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 = { }, 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: diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index fa92dceff..1e7f4318a 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -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. From 650db2dee773735171f035819065e0602dd02270 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Wed, 24 Aug 2022 19:09:18 -0400 Subject: [PATCH 4/5] Add type fix for flags --- src/NetscriptFunctions/Flags.ts | 2 +- src/ScriptEditor/NetscriptDefinitions.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NetscriptFunctions/Flags.ts b/src/NetscriptFunctions/Flags.ts index 1d08dc67b..2b9682c84 100644 --- a/src/NetscriptFunctions/Flags.ts +++ b/src/NetscriptFunctions/Flags.ts @@ -4,7 +4,7 @@ import { ScriptArg } from "../Netscript/ScriptArg"; import { NetscriptContext } from "../Netscript/APIWrapper"; type FlagType = StringConstructor | NumberConstructor | BooleanConstructor | StringConstructor[]; -type FlagsRet = { [key: string]: ScriptArg }; +type FlagsRet = { [key: string]: ScriptArg | string[] }; export function Flags(ctx: NetscriptContext | string[]): (data: unknown) => FlagsRet { const vargs = Array.isArray(ctx) ? ctx : ctx.workerScript.args; return (schema: unknown): FlagsRet => { diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 1e7f4318a..06dc84e4d 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -6531,7 +6531,7 @@ export interface NS { * // {"_":[],"delay":0,"server":"foodnstuff","exclude":[],"help":true} * ``` */ - flags(schema: [string, string | number | boolean | string[]][]): { [key: string]: ScriptArg }; + flags(schema: [string, string | number | boolean | string[]][]): { [key: string]: ScriptArg | string[] }; /** * Share your computer with your factions. From d1aa114e29b9722cf7eb778a7797b7b3a59d3880 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Wed, 24 Aug 2022 20:28:38 -0400 Subject: [PATCH 5/5] Fix build error / additional type for flags --- src/ScriptEditor/NetscriptDefinitions.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 06dc84e4d..8f83a5aa3 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -7358,5 +7358,5 @@ interface AutocompleteData { servers: string[]; scripts: string[]; txts: string[]; - flags(schema: [string, string | number | boolean | string[]][]): { [key: string]: ScriptArg }; + flags(schema: [string, string | number | boolean | string[]][]): { [key: string]: ScriptArg | string[] }; }