-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCraftopiaWorldSaveDataHandler.ts
More file actions
39 lines (34 loc) · 1.08 KB
/
Copy pathCraftopiaWorldSaveDataHandler.ts
File metadata and controls
39 lines (34 loc) · 1.08 KB
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
import { gzipDecode } from "./deps.ts";
import { CraftopiaWorldSaveData } from "./CraftopiaWorldSaveData.ts";
export class CraftopiaWorldSaveDataHandler {
static unpackWorldSaveDataFromPath(path: string) {
const data = Deno.readFileSync(path);
return CraftopiaWorldSaveDataHandler.unpackWorldSaveData(data);
}
static unpackWorldSaveData(data: Uint8Array): CraftopiaWorldSaveData {
const unpacked = gzipDecode(data);
const json = JSON.parse(new TextDecoder().decode(unpacked));
return json;
}
path: string;
data?: Uint8Array;
saveData?: CraftopiaWorldSaveData;
constructor(path: string, data?: Uint8Array) {
this.path = path;
this.data = data;
}
fetchData() {
if (!this.data) {
this.data = Deno.readFileSync(this.path);
}
return this.data;
}
fetchSaveData() {
if (!this.saveData) {
this.saveData = CraftopiaWorldSaveDataHandler.unpackWorldSaveData(
this.fetchData(),
);
}
return this.saveData;
}
}