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";
|
2019-04-11 10:37:40 +02:00
|
|
|
|
2022-10-04 12:40:10 +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 {
|
2022-10-04 12:40:10 +02:00
|
|
|
/** The full file name. */
|
2021-09-05 01:09:30 +02:00
|
|
|
fn: string;
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** The content of the file. */
|
2021-09-05 01:09:30 +02:00
|
|
|
text: string;
|
|
|
|
|
2022-12-30 02:28:53 +01:00
|
|
|
//TODO 2.3: Why are we using getter/setter for fn as filename? Rename parameter as more-readable "filename"
|
2022-10-04 12:40:10 +02:00
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** 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
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** Retrieve the content of the file. */
|
2021-09-05 01:09:30 +02:00
|
|
|
read(): string {
|
|
|
|
return this.text;
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** Shows the content to the user via the game's dialog box. */
|
2021-09-05 01:09:30 +02:00
|
|
|
show(): void {
|
2022-11-04 12:23:33 +01:00
|
|
|
dialogBoxCreate(`${this.fn}\n\n${this.text}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +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);
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** Replaces the current content with the text provided. */
|
2021-09-05 01:09:30 +02:00
|
|
|
write(txt: string): void {
|
|
|
|
this.text = txt;
|
|
|
|
}
|
|
|
|
|
2022-10-09 07:25:31 +02:00
|
|
|
/** Initializes 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);
|
|
|
|
}
|
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-09-06 21:06:08 +02:00
|
|
|
export function getTextFile(fn: string, server: BaseServer): TextFile | null {
|
2021-12-18 04:59:41 +01:00
|
|
|
let filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn;
|
|
|
|
|
|
|
|
if (isInRootDirectory(filename)) {
|
|
|
|
filename = removeLeadingSlash(filename);
|
|
|
|
}
|
2018-06-14 15:11:28 +02:00
|
|
|
|
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;
|
2018-06-14 15:11:28 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2018-06-14 15:11:28 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return null;
|
2018-06-14 15:11:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-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;
|
2018-06-14 15:11:28 +02:00
|
|
|
}
|