-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorage.godot.ts
38 lines (35 loc) · 910 Bytes
/
storage.godot.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
import { Storage } from "./storage";
class LocalStorage extends Storage {
protected file: string;
constructor(file: string = "user://localStorage.json") {
super();
this.file = file;
const fs = new godot.File();
if (fs.file_exists(file)) {
if (godot.Error.OK == fs.open(file, godot.File.READ)) {
let text = fs.get_as_text() || "[]";
fs.close();
this._items = JSON.parse(text);
} else {
throw new Error("Cannot open storage file " + file);
}
}
}
protected flush() {
const file = new godot.File();
if (godot.Error.OK == file.open(this.file, godot.File.WRITE_READ)) {
let text = JSON.stringify(this._items, undefined, '\t');
file.store_string(text);
file.close();
} else {
throw new Error("Cannot open storage file " + this.file);
}
}
}
export default {
exports: {
Storage,
sessionStorage: new Storage(),
localStorage: new LocalStorage(),
}
};