diff --git a/dist/bitburner.d.ts b/dist/bitburner.d.ts index 7b2f4df66..08176beca 100644 --- a/dist/bitburner.d.ts +++ b/dist/bitburner.d.ts @@ -4099,6 +4099,26 @@ export declare interface NS extends Singularity { */ atExit(f: () => void): void; + /** + * Move a file on the target server. + * @remarks + * RAM cost: 0 GB + * + * NS2 exclusive + * + * Move the source file to the specified destination on the target server. + * + * This command only works for scripts and text files (.txt). It cannot, however, be used + * to convert from script to text file, or vice versa. + * + * This function can also be used to rename files. + * + * @param host - Host of target server. + * @param source - Filename of the source file. + * @param destination - Filename of the destination file. + */ + mv(host: string, source: string, destination: string): void; + /** * Parse command line flags. * @remarks @@ -6190,12 +6210,12 @@ export declare interface WarehouseAPI { * @param all - Sell in all city */ sellProduct( - divisionName: string, - cityName: string, - productName: string, - amt: string, - price: string, - all: boolean, + divisionName: string, + cityName: string, + productName: string, + amt: string, + price: string, + all: boolean, ): void; /** * Discontinue a product. @@ -6279,12 +6299,12 @@ export declare interface WarehouseAPI { * @param amt - Amount of material to export. */ exportMaterial( - sourceDivision: string, - sourceCity: string, - targetDivision: string, - targetCity: string, - materialName: string, - amt: number, + sourceDivision: string, + sourceCity: string, + targetDivision: string, + targetCity: string, + materialName: string, + amt: number, ): void; /** * Cancel material export @@ -6296,12 +6316,12 @@ export declare interface WarehouseAPI { * @param amt - Amount of material to export. */ cancelExportMaterial( - sourceDivision: string, - sourceCity: string, - targetDivision: string, - targetCity: string, - materialName: string, - amt: number, + sourceDivision: string, + sourceCity: string, + targetDivision: string, + targetCity: string, + materialName: string, + amt: number, ): void; /** * Purchase warehouse for a new city @@ -6324,11 +6344,11 @@ export declare interface WarehouseAPI { * @param marketingInvest - Amount to invest for the marketing of the product. */ makeProduct( - divisionName: string, - cityName: string, - productName: string, - designInvest: number, - marketingInvest: number, + divisionName: string, + cityName: string, + productName: string, + designInvest: number, + marketingInvest: number, ): void; } diff --git a/markdown/bitburner.ns.mv.md b/markdown/bitburner.ns.mv.md new file mode 100644 index 000000000..9439eca16 --- /dev/null +++ b/markdown/bitburner.ns.mv.md @@ -0,0 +1,38 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [NS](./bitburner.ns.md) > [atExit](./bitburner.ns.atexit.md) + +## NS.mv() method + +Move a file on the target server. + +Signature: + +```typescript +mv(host: string, source: string, destination: string): void; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| host | string | Host of target server. | +| source | string | Filename of the source file. | +| destination | string | Filename of the destination file. | + +Returns: + +void + +## Remarks + +RAM cost: 0 GB + +NS2 exclusive + +Move the source file to the specified destination on the target server. + +This command only works for scripts and text files (.txt). It cannot, however, be used +to convert from script to text file, or vice versa. + +This function can also be used to rename files. diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index c026bd642..8092b775e 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -207,6 +207,7 @@ export const RamCosts: IMap = { wget: 0, getFavorToDonate: RamCostConstants.ScriptGetFavorToDonate, getPlayer: RamCostConstants.ScriptSingularityFn1RamCost / 4, + mv: 0, getOwnedSourceFiles: RamCostConstants.ScriptGetOwnedSourceFiles, // Singularity Functions diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 6a41e4a30..fc56a9b28 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -2258,6 +2258,64 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { } workerScript.atExit = f; }, + mv: function (host: string, source: string, destination: string): void { + updateDynamicRam("mv", getRamCost(Player, "mv")); + + if (arguments.length != 3) throw makeRuntimeErrorMsg("mv", "Takes 3 argument."); + + if (!isValidFilePath(source)) throw makeRuntimeErrorMsg("mv", `Invalid filename: '${source}'`); + if (!isValidFilePath(destination)) throw makeRuntimeErrorMsg("mv", `Invalid filename: '${destination}'`); + + const source_is_txt = source.endsWith(".txt"); + const dest_is_txt = destination.endsWith(".txt"); + + if (!isScriptFilename(source) && !source_is_txt) throw makeRuntimeErrorMsg("mv", `'mv' can only be used on scripts and text files (.txt)`); + if (source_is_txt != dest_is_txt) throw makeRuntimeErrorMsg("mv", `Source and destination files must have the same type`); + + if (source === destination) { + return; + } + + // This will throw if the server is not found, we do not need to validate result. + const destServer: BaseServer | null = safeGetServer(host, "mv"); + + if (!source_is_txt && destServer.isRunning(source)) throw makeRuntimeErrorMsg("mv", `Cannot use 'mv' on a script that is running`) + + interface File { + filename: string; + } + + const files = source_is_txt ? destServer.textFiles : destServer.scripts; + let source_file: File | null = null; + let dest_file: File | null = null; + + for (let i = 0; i < files.length; ++i) { + const file = files[i]; + if (file.filename === source) { + source_file = file; + } else if (file.filename === destination) { + dest_file = file; + } + } + + if (source_file == null) throw makeRuntimeErrorMsg("mv", `Source file ${source} does not exist`) + + if (dest_file != null) { + if (dest_file instanceof TextFile && source_file instanceof TextFile) { + dest_file.text = source_file.text; + } else if (dest_file instanceof Script && source_file instanceof Script) { + dest_file.code = source_file.code; + dest_file.markUpdated(); + } + + destServer.removeFile(source); + } else { + source_file.filename = destination; + if (source_file instanceof Script) { + source_file.markUpdated(); + } + } + }, flags: Flags(workerScript.args), }; diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 67c8bf1b7..6c149a95d 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -5930,6 +5930,26 @@ export interface NS extends Singularity { */ atExit(f: () => void): void; + /** + * Move a file on the target server. + * @remarks + * RAM cost: 0 GB + * + * NS2 exclusive + * + * Move the source file to the specified destination on the target server. + * + * This command only works for scripts and text files (.txt). It cannot, however, be used + * to convert from script to text file, or vice versa. + * + * This function can also be used to rename files. + * + * @param host - Host of target server. + * @param source - Filename of the source file. + * @param destination - Filename of the destination file. + */ + mv(host: string, source: string, destination: string): void; + /** * Parse command line flags. * @remarks diff --git a/src/TextFile.ts b/src/TextFile.ts index cf77058c3..db4051f6f 100644 --- a/src/TextFile.ts +++ b/src/TextFile.ts @@ -17,6 +17,20 @@ export class TextFile { */ text: string; + /** + * The full file name. + */ + get filename(): string { + return this.fn; + } + + /** + * The full file name. + */ + set filename(value: string) { + this.fn = value; + } + constructor(fn = "", txt = "") { this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, ""); this.text = txt;