mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-19 06:03:50 +01:00
Merge pull request #4028 from Snarling/scpAndWriteDocFix
NETSCRIPT: Update netscript definition file for scp, write, read, and flags
This commit is contained in:
commit
2463fc2012
@ -1465,47 +1465,42 @@ const base: InternalAPI<NS> = {
|
||||
},
|
||||
write:
|
||||
(ctx: NetscriptContext) =>
|
||||
(_port: unknown, _data: unknown = "", _mode: unknown = "a"): void => {
|
||||
const port = helpers.string(ctx, "port", _port);
|
||||
(_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 (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) =>
|
||||
@ -1545,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:
|
||||
|
@ -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 => {
|
||||
|
42
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
42
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@ -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<boolean>;
|
||||
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 filename - Name of the file 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<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): PortData;
|
||||
read(filename: string): string;
|
||||
|
||||
/**
|
||||
* Get a copy of the data from a port without popping it.
|
||||
@ -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.
|
||||
@ -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[] };
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user