bitburner-src/src/Terminal/commands/mv.ts

103 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-09-16 01:50:44 +02:00
import { ITerminal } from "../ITerminal";
2021-09-17 08:58:02 +02:00
import { IRouter } from "../../ui/Router";
2021-09-16 01:50:44 +02:00
import { IPlayer } from "../../PersonObjects/IPlayer";
import { BaseServer } from "../../Server/BaseServer";
2021-09-24 22:34:21 +02:00
import { isScriptFilename } from "../../Script/isScriptFilename";
2021-09-16 01:50:44 +02:00
import { TextFile } from "../../TextFile";
import { Script } from "../../Script/Script";
import { getDestinationFilepath, areFilesEqual } from "../DirectoryHelpers";
2021-09-16 01:50:44 +02:00
export function mv(
terminal: ITerminal,
2021-09-17 08:58:02 +02:00
router: IRouter,
2021-09-16 01:50:44 +02:00
player: IPlayer,
server: BaseServer,
2021-12-03 20:44:32 +01:00
args: (string | number | boolean)[],
2021-09-16 01:50:44 +02:00
): void {
if (args.length !== 2) {
terminal.error(`Incorrect number of arguments. Usage: mv [src] [dest]`);
return;
}
try {
const source = args[0] + "";
const t_dest = args[1] + "";
2021-09-16 01:50:44 +02:00
if (!isScriptFilename(source) && !source.endsWith(".txt")) {
terminal.error(`'mv' can only be used on scripts and text files (.txt)`);
return;
}
const srcFile = terminal.getFile(player, source);
if (srcFile == null) {
terminal.error(`Source file ${source} does not exist`);
return;
}
const sourcePath = terminal.getFilepath(source);
// Get the destination based on the source file and the current directory
const dest = getDestinationFilepath(t_dest, source, terminal.cwd());
if (dest === null) {
terminal.error("error parsing dst file");
return;
}
2021-09-16 01:50:44 +02:00
const destFile = terminal.getFile(player, dest);
const destPath = terminal.getFilepath(dest);
if (areFilesEqual(sourcePath, destPath)) {
terminal.error(`Source and destination files are the same file`);
return;
}
2021-09-16 01:50:44 +02:00
// 'mv' command only works on scripts and txt files.
// Also, you can't convert between different file types
if (isScriptFilename(source)) {
const script = srcFile as Script;
if (!isScriptFilename(dest)) {
terminal.error(`Source and destination files must have the same type`);
return;
}
// Command doesnt work if script is running
if (server.isRunning(sourcePath)) {
terminal.error(`Cannot use 'mv' on a script that is running`);
return;
}
if (destFile != null) {
// Already exists, will be overwritten, so we'll delete it
const status = server.removeFile(dest);
2021-09-16 01:50:44 +02:00
if (!status.res) {
terminal.error(`Something went wrong...please contact game dev (probably a bug)`);
return;
} else {
terminal.print("Warning: The destination file was overwritten");
}
}
script.filename = destPath;
} else if (srcFile instanceof TextFile) {
const textFile = srcFile as TextFile;
if (!dest.endsWith(".txt")) {
terminal.error(`Source and destination files must have the same type`);
return;
}
if (destFile != null) {
// Already exists, will be overwritten, so we'll delete it
const status = server.removeFile(destPath);
if (!status.res) {
terminal.error(`Something went wrong...please contact game dev (probably a bug)`);
return;
} else {
terminal.print("Warning: The destination file was overwritten");
}
}
textFile.fn = destPath;
}
} catch (e) {
terminal.error(e + "");
}
}