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

80 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-09-06 15:07:12 +02:00
import { Terminal } from "../../Terminal";
2021-09-16 01:50:44 +02:00
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 FileSaver from "file-saver";
import JSZip from "jszip";
export function exportScripts(pattern: string, server: BaseServer): void {
const matchEnding = pattern.length == 1 || pattern === "*.*" ? null : pattern.slice(1); // Treat *.* the same as *
const zip = new JSZip();
// Helper function to zip any file contents whose name matches the pattern
const zipFiles = (fileNames: string[], fileContents: string[]): void => {
for (let i = 0; i < fileContents.length; ++i) {
let name = fileNames[i];
if (name.startsWith("/")) name = name.slice(1);
if (!matchEnding || name.endsWith(matchEnding))
zip.file(name, new Blob([fileContents[i]], { type: "text/plain" }));
}
};
// In the case of script files, we pull from the server.scripts array
if (!matchEnding || isScriptFilename(matchEnding))
zipFiles(
server.scripts.map((s) => s.filename),
server.scripts.map((s) => s.code),
);
// In the case of text files, we pull from the server.scripts array
if (!matchEnding || matchEnding.endsWith(".txt"))
zipFiles(
server.textFiles.map((s) => s.fn),
server.textFiles.map((s) => s.text),
);
// Return an error if no files matched, rather than an empty zip folder
if (Object.keys(zip.files).length == 0) throw new Error(`No files match the pattern ${pattern}`);
const zipFn = `bitburner${isScriptFilename(pattern) ? "Scripts" : pattern === "*.txt" ? "Texts" : "Files"}.zip`;
2022-07-20 03:53:43 +02:00
zip.generateAsync({ type: "blob" }).then((content: Blob) => FileSaver.saveAs(content, zipFn));
}
2022-09-06 15:07:12 +02:00
export function download(args: (string | number | boolean)[], server: BaseServer): void {
2021-09-16 01:50:44 +02:00
try {
if (args.length !== 1) {
2022-09-06 15:07:12 +02:00
Terminal.error("Incorrect usage of download command. Usage: download [script/text file]");
2021-09-16 01:50:44 +02:00
return;
}
const fn = args[0] + "";
// If the parameter starts with *, download all files that match the wildcard pattern
if (fn.startsWith("*")) {
try {
exportScripts(fn, server);
return;
2022-07-15 07:51:30 +02:00
} catch (e: unknown) {
let msg = String(e);
if (e !== null && typeof e == "object" && e.hasOwnProperty("message")) {
msg = String((e as { message: unknown }).message);
}
2022-09-06 15:07:12 +02:00
return Terminal.error(msg);
}
2021-09-16 01:50:44 +02:00
} else if (isScriptFilename(fn)) {
// Download a single script
2022-09-06 15:07:12 +02:00
const script = Terminal.getScript(fn);
2021-09-16 01:50:44 +02:00
if (script != null) {
return script.download();
}
} else if (fn.endsWith(".txt")) {
// Download a single text file
2022-09-06 15:07:12 +02:00
const txt = Terminal.getTextFile(fn);
2021-09-16 01:50:44 +02:00
if (txt != null) {
return txt.download();
}
} else {
2022-09-06 15:07:12 +02:00
Terminal.error(`Cannot download this filetype`);
2021-09-16 01:50:44 +02:00
return;
}
2022-09-06 15:07:12 +02:00
Terminal.error(`${fn} does not exist`);
2021-09-16 01:50:44 +02:00
return;
} catch (e) {
2022-09-06 15:07:12 +02:00
Terminal.error(e + "");
2021-09-16 01:50:44 +02:00
return;
}
}