2019-02-20 09:42:27 +01:00
|
|
|
import { setTimeoutRef } from "./utils/SetTimeoutRef";
|
2018-06-14 20:57:44 +02:00
|
|
|
import { dialogBoxCreate } from "../utils/DialogBox";
|
2019-04-11 10:37:40 +02:00
|
|
|
import {
|
|
|
|
Generic_fromJSON,
|
|
|
|
Generic_toJSON,
|
2021-04-30 05:52:56 +02:00
|
|
|
Reviver,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "../utils/JSONReviver";
|
|
|
|
|
2018-06-14 15:11:28 +02:00
|
|
|
|
2018-06-14 20:57:44 +02:00
|
|
|
/**
|
|
|
|
* Represents a plain text file that is typically stored on a server.
|
|
|
|
*/
|
2018-06-14 15:11:28 +02:00
|
|
|
export class TextFile {
|
2018-06-14 20:57:44 +02:00
|
|
|
/**
|
|
|
|
* The full file name.
|
|
|
|
*/
|
2018-06-14 15:11:28 +02:00
|
|
|
fn: string;
|
2018-06-14 20:57:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The content of the file.
|
|
|
|
*/
|
2018-06-14 15:11:28 +02:00
|
|
|
text: string;
|
|
|
|
|
2021-04-30 05:52:56 +02:00
|
|
|
constructor(fn = "", txt = "") {
|
2018-06-14 20:57:44 +02:00
|
|
|
this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, "");
|
2018-06-14 15:11:28 +02:00
|
|
|
this.text = txt;
|
|
|
|
}
|
|
|
|
|
2018-06-14 20:57:44 +02:00
|
|
|
/**
|
|
|
|
* Concatenates the raw values to the end of current content.
|
|
|
|
*/
|
|
|
|
append(txt: string): void {
|
2018-06-14 15:11:28 +02:00
|
|
|
this.text += txt;
|
|
|
|
}
|
|
|
|
|
2018-06-14 20:57:44 +02:00
|
|
|
/**
|
|
|
|
* 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 */
|
2018-06-14 15:11:28 +02:00
|
|
|
if (window.navigator.msSaveOrOpenBlob) {
|
|
|
|
// IE10+
|
|
|
|
window.navigator.msSaveOrOpenBlob(file, filename);
|
|
|
|
} else {
|
|
|
|
// Others
|
2018-06-14 20:57:44 +02:00
|
|
|
const a: HTMLAnchorElement = document.createElement("a");
|
|
|
|
const url: string = URL.createObjectURL(file);
|
2018-06-14 15:11:28 +02:00
|
|
|
a.href = url;
|
|
|
|
a.download = this.fn;
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
2019-02-20 09:42:27 +01:00
|
|
|
setTimeoutRef(() => {
|
|
|
|
document.body.removeChild(a);
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
}, 0);
|
2018-06-14 15:11:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 20:57:44 +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}`, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the current file to a JSON save state.
|
|
|
|
*/
|
|
|
|
toJSON(): any {
|
2018-06-14 15:11:28 +02:00
|
|
|
return Generic_toJSON("TextFile", this);
|
|
|
|
}
|
|
|
|
|
2018-06-14 20:57:44 +02:00
|
|
|
/**
|
|
|
|
* Replaces the current content with the text provided.
|
|
|
|
*/
|
|
|
|
write(txt: string): void {
|
|
|
|
this.text = txt;
|
2018-06-14 15:11:28 +02:00
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2018-06-14 15:11:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.TextFile = TextFile;
|
|
|
|
|
2018-06-14 20:57:44 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2018-06-14 20:57:44 +02:00
|
|
|
export function getTextFile(fn: string, server: any): TextFile | null {
|
|
|
|
const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
|
2018-06-14 15:11:28 +02:00
|
|
|
|
2018-06-14 20:57:44 +02:00
|
|
|
for (const file of (server.textFiles as TextFile[])) {
|
|
|
|
if (file.fn === filename) {
|
|
|
|
return file;
|
2018-06-14 15:11:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a TextFile on the target server.
|
2018-06-14 20:57:44 +02:00
|
|
|
* @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.
|
2018-06-14 15:11:28 +02:00
|
|
|
*/
|
2021-05-01 09:17:31 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2018-06-14 20:57:44 +02:00
|
|
|
export function createTextFile(fn: string, txt: string, server: any): TextFile | undefined {
|
2018-06-14 15:11:28 +02:00
|
|
|
if (getTextFile(fn, server) !== null) {
|
2018-06-14 20:57:44 +02:00
|
|
|
// This should probably be a `throw`...
|
|
|
|
/* tslint:disable-next-line:no-console */
|
2018-06-14 15:11:28 +02:00
|
|
|
console.error(`A file named "${fn}" already exists on server ${server.hostname}.`);
|
2018-06-14 20:57:44 +02:00
|
|
|
|
|
|
|
return undefined;
|
2018-06-14 15:11:28 +02:00
|
|
|
}
|
2018-06-14 20:57:44 +02:00
|
|
|
const file: TextFile = new TextFile(fn, txt);
|
2018-06-14 15:11:28 +02:00
|
|
|
server.textFiles.push(file);
|
2018-06-14 20:57:44 +02:00
|
|
|
|
2018-06-14 15:11:28 +02:00
|
|
|
return file;
|
|
|
|
}
|