forked from bitburner-official/bitburner-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextFile.ts
59 lines (50 loc) · 1.76 KB
/
TextFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { BaseServer } from "./Server/BaseServer";
import { Generic_fromJSON, Generic_toJSON, IReviverValue, constructorsForReviver } from "./utils/JSONReviver";
import { TextFilePath } from "./Paths/TextFilePath";
import { ContentFile } from "./Paths/ContentFile";
/** Represents a plain text file that is typically stored on a server. */
export class TextFile implements ContentFile {
/** The full file name. */
filename: TextFilePath;
/** The content of the file. */
text: string;
// Shared interface on Script and TextFile for accessing content
get content() {
return this.text;
}
set content(text: string) {
this.text = text;
}
constructor(filename = "default.txt" as TextFilePath, txt = "") {
this.filename = filename;
this.text = txt;
}
/** Serves the file to the user as a downloadable resource through the browser. */
download(): void {
const file: Blob = new Blob([this.text], { type: "text/plain" });
const a: HTMLAnchorElement = document.createElement("a");
const url: string = URL.createObjectURL(file);
a.href = url;
a.download = this.filename;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
}
/** Serialize the current file to a JSON save state. */
toJSON(): IReviverValue {
return Generic_toJSON("TextFile", this);
}
deleteFromServer(server: BaseServer): boolean {
if (!server.textFiles.has(this.filename)) return false;
server.textFiles.delete(this.filename);
return true;
}
/** Initializes a TextFile from a JSON save state. */
static fromJSON(value: IReviverValue): TextFile {
return Generic_fromJSON(TextFile, value.data);
}
}
constructorsForReviver.TextFile = TextFile;