bitburner-src/src/TextFile.ts

120 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-09-25 20:42:57 +02:00
import { dialogBoxCreate } from "./ui/React/DialogBox";
2021-09-06 21:06:08 +02:00
import { BaseServer } from "./Server/BaseServer";
2022-07-15 01:00:10 +02:00
import { Generic_fromJSON, Generic_toJSON, IReviverValue, Reviver } from "./utils/JSONReviver";
2022-04-07 01:30:08 +02:00
import { removeLeadingSlash, isInRootDirectory } from "./Terminal/DirectoryHelpers";
/** Represents a plain text file that is typically stored on a server. */
export class TextFile {
/** The full file name. */
2021-09-05 01:09:30 +02:00
fn: string;
/** The content of the file. */
2021-09-05 01:09:30 +02:00
text: string;
//TODO: Why are we using getter/setter for fn as filename?
/** The full file name. */
2022-04-07 01:30:08 +02:00
get filename(): string {
2022-01-12 18:53:54 +01:00
return this.fn;
}
/** The full file name. */
2022-04-07 01:30:08 +02:00
set filename(value: string) {
2022-01-12 18:53:54 +01:00
this.fn = value;
}
2021-09-05 01:09:30 +02:00
constructor(fn = "", txt = "") {
this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, "");
this.text = txt;
}
/** Concatenates the raw values to the end of current content. */
2021-09-05 01:09:30 +02:00
append(txt: string): void {
this.text += txt;
}
/** Serves the file to the user as a downloadable resource through the browser. */
2021-09-05 01:09:30 +02:00
download(): void {
const file: Blob = new Blob([this.text], { type: "text/plain" });
2022-07-20 08:36:21 +02:00
const a: HTMLAnchorElement = document.createElement("a");
const url: string = URL.createObjectURL(file);
a.href = url;
a.download = this.fn;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
2021-09-05 01:09:30 +02:00
}
/** Retrieve the content of the file. */
2021-09-05 01:09:30 +02:00
read(): string {
return this.text;
}
/** Shows the content to the user via the game's dialog box. */
2021-09-05 01:09:30 +02:00
show(): void {
dialogBoxCreate(`${this.fn}<br /><br />${this.text}`);
2021-09-05 01:09:30 +02:00
}
/** Serialize the current file to a JSON save state. */
2022-07-15 01:00:10 +02:00
toJSON(): IReviverValue {
2021-09-05 01:09:30 +02:00
return Generic_toJSON("TextFile", this);
}
/** Replaces the current content with the text provided. */
2021-09-05 01:09:30 +02:00
write(txt: string): void {
this.text = txt;
}
/** Initiatizes a TextFile from a JSON save state. */
2022-07-15 01:00:10 +02:00
static fromJSON(value: IReviverValue): TextFile {
2021-09-05 01:09:30 +02:00
return Generic_fromJSON(TextFile, value.data);
}
}
Reviver.constructors.TextFile = TextFile;
/**
* Retrieve the file object for the filename on the specified server.
* @param fn The file name to look for
* @param server The server object to look in
* @returns The file object, or null if it couldn't find it.
*/
2021-09-06 21:06:08 +02:00
export function getTextFile(fn: string, server: BaseServer): TextFile | null {
let filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
if (isInRootDirectory(filename)) {
filename = removeLeadingSlash(filename);
}
2022-07-15 01:00:10 +02:00
for (const file of server.textFiles) {
2021-09-05 01:09:30 +02:00
if (file.fn === filename) {
return file;
}
2021-09-05 01:09:30 +02:00
}
2021-09-05 01:09:30 +02:00
return null;
}
/**
* Creates a TextFile on the target server.
* @param fn The file name to create.
* @param txt The contents of the file.
* @param server The server that the file should be created on.
* @returns The instance of the file.
*/
2021-09-09 05:47:34 +02:00
export function createTextFile(fn: string, txt: string, server: BaseServer): TextFile | undefined {
2021-09-05 01:09:30 +02:00
if (getTextFile(fn, server) !== null) {
// This should probably be a `throw`...
/* tslint:disable-next-line:no-console */
2021-09-09 05:47:34 +02:00
console.error(`A file named "${fn}" already exists on server ${server.hostname}.`);
2021-09-05 01:09:30 +02:00
return undefined;
}
const file: TextFile = new TextFile(fn, txt);
server.textFiles.push(file);
return file;
}