bitburner-src/src/TextFile.ts

136 lines
3.5 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";
2021-09-25 20:42:57 +02:00
import { Generic_fromJSON, Generic_toJSON, Reviver } from "./utils/JSONReviver";
/**
* Represents a plain text file that is typically stored on a server.
*/
export class TextFile {
2021-09-05 01:09:30 +02:00
/**
* The full file name.
*/
fn: string;
/**
* The content of the file.
*/
text: string;
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.
*/
append(txt: string): void {
this.text += txt;
}
/**
* Serves the file to the user as a downloadable resource through the browser.
*/
download(): void {
const filename: string = this.fn;
const file: Blob = new Blob([this.text], { type: "text/plain" });
/* tslint:disable-next-line:strict-boolean-expressions */
2021-10-13 08:27:55 +02:00
const navigator = window.navigator as any;
if (navigator.msSaveOrOpenBlob) {
2021-09-05 01:09:30 +02:00
// IE10+
2021-10-13 08:27:55 +02:00
navigator.msSaveOrOpenBlob(file, filename);
2021-09-05 01:09:30 +02:00
} else {
// Others
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(() => {
2021-09-05 01:09:30 +02:00
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
2021-05-01 09:17:31 +02:00
}
2021-09-05 01:09:30 +02:00
}
/**
* Retrieve the content of the file.
*/
read(): string {
return this.text;
}
/**
* Shows the content to the user via the game's dialog box.
*/
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.
*/
toJSON(): any {
return Generic_toJSON("TextFile", this);
}
/**
* Replaces the current content with the text provided.
*/
write(txt: string): void {
this.text = txt;
}
/**
* Initiatizes a TextFile from a JSON save state.
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static fromJSON(value: any): TextFile {
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-05-01 09:17:31 +02:00
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
2021-09-06 21:06:08 +02:00
export function getTextFile(fn: string, server: BaseServer): TextFile | null {
2021-09-05 01:09:30 +02:00
const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
2021-09-05 01:09:30 +02:00
for (const file of server.textFiles as TextFile[]) {
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-05-01 09:17:31 +02:00
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
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;
}