-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorage.unity.ts
45 lines (41 loc) · 1.13 KB
/
storage.unity.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
// @ts-nocheck
import { System, UnityEngine } from "csharp";
import { Storage } from './storage'
class LocalStorage extends Storage {
protected $file: string;
protected $directory: string;
constructor(file: string = `${UnityEngine.Application.persistentDataPath}/webapi/localStorage.json`) {
super();
this.$file = file;
this.$directory = System.IO.Path.GetDirectoryName(this.$file);
if (System.IO.File.Exists(file)) {
try {
const stream = new System.IO.StreamReader(file);
const text = stream.ReadToEnd();
this._items = JSON.parse(text);
} catch (error) {
throw new Error("Cannot open storage file " + file);
}
}
}
protected flush() {
if (!System.IO.File.Exists(this.$directory)) {
System.IO.Directory.CreateDirectory(this.$directory);
}
const stream = new System.IO.StreamWriter(this.$file);
if (stream) {
let text = JSON.stringify(this._items, undefined, '\t');
stream.Write(text);
stream.Flush();
} else {
throw new Error("Cannot open storage file " + this.$file);
}
}
}
export default {
exports: {
Storage,
sessionStorage: new Storage(),
localStorage: new LocalStorage(),
}
};