-
Notifications
You must be signed in to change notification settings - Fork 0
/
persist.ts
127 lines (110 loc) · 3.2 KB
/
persist.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const stdlib: typeof import("@grakkit/stdlib-paper") = require("@grakkit/stdlib-paper");
const Paths = stdlib.type("java.nio.file.Paths");
const File = stdlib.type("java.io.File");
function stringToUint8Array(str: string): Uint8Array {
let result = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
result[i] = str.charCodeAt(i);
}
return result;
}
//https://stackoverflow.com/a/22373197
function stringFromUint8Array(array: Uint8Array) {
let out, i, len, c;
let char2, char3;
out = "";
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
import { FileHelper } from "./filehelper.js";
export class Persist {
static SINGLETON: Persist;
static get(): Persist {
if (!Persist.SINGLETON) Persist.SINGLETON = new Persist();
return Persist.SINGLETON;
}
private constructor() {
}
resolve(fpath: string): string {
return Paths.get(
stdlib.plugin.getDataFolder().getAbsolutePath(),
"persist",
fpath + ".json"
).toString();
}
async setData(fpath: string, data: Uint8Array): Promise<boolean> {
return new Promise(async (_resolve, _reject) => {
try {
fpath = this.resolve(fpath);
// console.log("abs fpath", fpath);
let helper = FileHelper.get();
await helper.create(fpath);
// console.log("create abs fpath");
// console.log("Writting buf");
await helper.write(fpath, data);
// console.log("wrote buf");
} catch (ex) {
_reject(ex);
return;
}
_resolve(true);
});
}
async setString(fpath: string, data: string): Promise<boolean> {
return this.setData(fpath, stringToUint8Array(data));
}
async setJson<T>(fpath: string, data: T): Promise<boolean> {
return this.setString(fpath, JSON.stringify(data));
}
async getData(fpath: string): Promise<Uint8Array> {
return new Promise(async (_resolve, _reject) => {
fpath = this.resolve(fpath);
let helper = FileHelper.get();
await helper.create(fpath);
let data = await helper.read(fpath);
_resolve(data);
});
}
async getString(fpath: string): Promise<string> {
return new Promise(async (_resolve, _reject) => {
let data = await this.getData(fpath);
_resolve(stringFromUint8Array(data));
});
}
async getJson<T>(fpath: string): Promise<T> {
return new Promise(async (_resolve, _reject) => {
let str = await this.getString(fpath);
let result: T;
try {
result = JSON.parse(str);
} catch (ex) {
_reject(ex);
return;
}
_resolve(result);
});
}
}