-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhh_Android_save.js
207 lines (191 loc) · 5.88 KB
/
hh_Android_save.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//=============================================================================
// hh_Android_save.js
//=============================================================================
/*:
* @plugindesc v1.0 Add Android local save support
v1.0 添加Android本地存档支持
* @author Hao
*
* @help
*
* The plugin add RMMV saves in internal stroage, but you have to my deployment project to connect the plugin
* Deployment project:https://github.com/huhao1987/RMMV-android-deployment
* 本插件添加了对Android 本地存档的支持,但必须与我的部署方案配合才能使用
*部署方案:https://github.com/huhao1987/RMMV-android-deployment
*/
//The mode is 0(isNwjs()) or 1(isMobileDevice)
var mode=0
StorageManager.isLocalMode = function() {
if(Utils.isNwjs()||Utils.isMobileDevice()){
if(Utils.isMobileDevice())
mode=1;
return true;
}
};
StorageManager.saveToLocalFile = function(savefileId, json) {
if(mode==1){
if(window.androidinterface){
androidinterface.savedata(savefileId, json);
}
}
else{
var data = LZString.compressToBase64(json);
var fs = require('fs');
var dirPath = this.localFileDirectoryPath();
var filePath = this.localFilePath(savefileId);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
fs.writeFileSync(filePath, data);
}
};
StorageManager.loadFromLocalFile = function(savefileId) {
if(mode==1){
if(window.androidinterface){
return androidinterface.loaddata(savefileId);
}
else return null;
}
else{
var data = null;
var fs = require('fs');
var filePath = this.localFilePath(savefileId);
if (fs.existsSync(filePath)) {
data = fs.readFileSync(filePath, { encoding: 'utf8' });
}
return LZString.decompressFromBase64(data);
}
};
StorageManager.loadFromLocalBackupFile = function(savefileId) {
if(mode==1){
if(window.androidinterface){
return androidinterface.restorebackup(savefileId);
}
else return null;
}
else{
var data = null;
var fs = require('fs');
var filePath = this.localFilePath(savefileId) + ".bak";
if (fs.existsSync(filePath)) {
data = fs.readFileSync(filePath, { encoding: 'utf8' });
}
return LZString.decompressFromBase64(data);
}
};
StorageManager.localFileBackupExists = function(savefileId) {
if(mode==1){
if(window.androidinterface){
return androidinterface.backupexists(savefileId);
}
else return false;
}
else{
var fs = require('fs');
return fs.existsSync(this.localFilePath(savefileId) + ".bak");
}
};
StorageManager.localFileExists = function(savefileId) {
if(mode==1){
if(window.androidinterface){
return androidinterface.savefileexists(savefileId);
}
else return false;
}
else{
var fs = require('fs');
return fs.existsSync(this.localFilePath(savefileId));}
};
StorageManager.removeLocalFile = function(savefileId) {
if (this.isLocalMode()) {
if(mode==1){
if(window.androidinterface){
androidinterface.removefile(savefileId);
}
}
else{
var fs = require('fs');
var filePath = this.localFilePath(savefileId);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
}
}
};
StorageManager.cleanBackup = function(savefileId) {
if (this.isLocalMode()) {
if(mode==1){
if(window.androidinterface){
androidinterface.cleanbackup(savefileId);
}
}
else{
var fs = require('fs');
var dirPath = this.localFileDirectoryPath();
var filePath = this.localFilePath(savefileId);
fs.unlinkSync(filePath + ".bak");
}
}
else {
if (this.backupExists(savefileId)) {
var key = this.webStorageKey(savefileId);
localStorage.removeItem(key + "bak");
}
}
};
StorageManager.restoreBackup = function(savefileId) {
if (this.isLocalMode()) {
if(mode==1){
if(window.androidinterface){
androidinterface.restorebackup(savefileId);
}
}
else{
var data = this.loadFromLocalBackupFile(savefileId);
var compressed = LZString.compressToBase64(data);
var fs = require('fs');
var dirPath = this.localFileDirectoryPath();
var filePath = this.localFilePath(savefileId);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
fs.writeFileSync(filePath, compressed);
fs.unlinkSync(filePath + ".bak");}
}
else {
if (this.backupExists(savefileId)) {
var data = this.loadFromWebStorageBackup(savefileId);
var compressed = LZString.compressToBase64(data);
var key = this.webStorageKey(savefileId);
localStorage.setItem(key, compressed);
localStorage.removeItem(key + "bak");
}
}
};
StorageManager.backup = function(savefileId) {
if (this.isLocalMode()) {
if(mode==1){
if(window.androidinterface){
androidinterface.backupdata(savefileId);
}
}
else{
var data = this.loadFromLocalFile(savefileId);
var compressed = LZString.compressToBase64(data);
var fs = require('fs');
var dirPath = this.localFileDirectoryPath();
var filePath = this.localFilePath(savefileId) + ".bak";
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
fs.writeFileSync(filePath, compressed);}
}
else {
if (this.exists(savefileId)) {
var data = this.loadFromWebStorage(savefileId);
var compressed = LZString.compressToBase64(data);
var key = this.webStorageKey(savefileId) + "bak";
localStorage.setItem(key, compressed);
}
}
};