mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-21 08:42:53 +02:00
prettify, sorry for the big ass commit
This commit is contained in:
+99
-94
@@ -1,98 +1,97 @@
|
||||
import { setTimeoutRef } from "./utils/SetTimeoutRef";
|
||||
import { dialogBoxCreate } from "../utils/DialogBox";
|
||||
import {
|
||||
Generic_fromJSON,
|
||||
Generic_toJSON,
|
||||
Reviver,
|
||||
Generic_fromJSON,
|
||||
Generic_toJSON,
|
||||
Reviver,
|
||||
} from "../utils/JSONReviver";
|
||||
|
||||
|
||||
/**
|
||||
* Represents a plain text file that is typically stored on a server.
|
||||
*/
|
||||
export class TextFile {
|
||||
/**
|
||||
* The full file name.
|
||||
*/
|
||||
fn: string;
|
||||
/**
|
||||
* The full file name.
|
||||
*/
|
||||
fn: string;
|
||||
|
||||
/**
|
||||
* The content of the file.
|
||||
*/
|
||||
text: 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;
|
||||
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 */
|
||||
if (window.navigator.msSaveOrOpenBlob) {
|
||||
// IE10+
|
||||
window.navigator.msSaveOrOpenBlob(file, filename);
|
||||
} 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();
|
||||
setTimeoutRef(() => {
|
||||
document.body.removeChild(a);
|
||||
window.URL.revokeObjectURL(url);
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenates the raw values to the end of current content.
|
||||
*/
|
||||
append(txt: string): void {
|
||||
this.text += txt;
|
||||
}
|
||||
/**
|
||||
* Retrieve the content of the file.
|
||||
*/
|
||||
read(): string {
|
||||
return this.text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 */
|
||||
if (window.navigator.msSaveOrOpenBlob) {
|
||||
// IE10+
|
||||
window.navigator.msSaveOrOpenBlob(file, filename);
|
||||
} 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();
|
||||
setTimeoutRef(() => {
|
||||
document.body.removeChild(a);
|
||||
window.URL.revokeObjectURL(url);
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Shows the content to the user via the game's dialog box.
|
||||
*/
|
||||
show(): void {
|
||||
dialogBoxCreate(`${this.fn}<br /><br />${this.text}`, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the content of the file.
|
||||
*/
|
||||
read(): string {
|
||||
return this.text;
|
||||
}
|
||||
/**
|
||||
* Serialize the current file to a JSON save state.
|
||||
*/
|
||||
toJSON(): any {
|
||||
return Generic_toJSON("TextFile", this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the content to the user via the game's dialog box.
|
||||
*/
|
||||
show(): void {
|
||||
dialogBoxCreate(`${this.fn}<br /><br />${this.text}`, true);
|
||||
}
|
||||
/**
|
||||
* Replaces the current content with the text provided.
|
||||
*/
|
||||
write(txt: string): void {
|
||||
this.text = txt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
@@ -105,15 +104,15 @@ Reviver.constructors.TextFile = TextFile;
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export function getTextFile(fn: string, server: any): TextFile | null {
|
||||
const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
|
||||
const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
|
||||
|
||||
for (const file of (server.textFiles as TextFile[])) {
|
||||
if (file.fn === filename) {
|
||||
return file;
|
||||
}
|
||||
for (const file of server.textFiles as TextFile[]) {
|
||||
if (file.fn === filename) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,16 +123,22 @@ export function getTextFile(fn: string, server: any): TextFile | null {
|
||||
* @returns The instance of the file.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export function createTextFile(fn: string, txt: string, server: any): TextFile | undefined {
|
||||
if (getTextFile(fn, server) !== null) {
|
||||
// This should probably be a `throw`...
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.error(`A file named "${fn}" already exists on server ${server.hostname}.`);
|
||||
export function createTextFile(
|
||||
fn: string,
|
||||
txt: string,
|
||||
server: any,
|
||||
): TextFile | undefined {
|
||||
if (getTextFile(fn, server) !== null) {
|
||||
// This should probably be a `throw`...
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.error(
|
||||
`A file named "${fn}" already exists on server ${server.hostname}.`,
|
||||
);
|
||||
|
||||
return undefined;
|
||||
}
|
||||
const file: TextFile = new TextFile(fn, txt);
|
||||
server.textFiles.push(file);
|
||||
return undefined;
|
||||
}
|
||||
const file: TextFile = new TextFile(fn, txt);
|
||||
server.textFiles.push(file);
|
||||
|
||||
return file;
|
||||
return file;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user