hzengine-core 0.1.2-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/async/index.js +162 -0
- package/dist/async/zeppos_timer.js +58 -0
- package/dist/audio/index.js +260 -0
- package/dist/config/index.js +57 -0
- package/dist/debug/index.js +8 -0
- package/dist/index.js +103 -0
- package/dist/platform/index.js +1 -0
- package/dist/plugins/basic_command/$.js +8 -0
- package/dist/plugins/basic_command/audio.js +40 -0
- package/dist/plugins/basic_command/basic.js +124 -0
- package/dist/plugins/basic_command/character.js +112 -0
- package/dist/plugins/basic_command/conditional.js +260 -0
- package/dist/plugins/basic_command/config.js +22 -0
- package/dist/plugins/basic_command/decorator.js +24 -0
- package/dist/plugins/basic_command/eval.js +67 -0
- package/dist/plugins/basic_command/img.js +249 -0
- package/dist/plugins/basic_command/index.js +22 -0
- package/dist/plugins/basic_command/menu.js +140 -0
- package/dist/plugins/global_gesture/index.js +25 -0
- package/dist/plugins/transform/animation.js +440 -0
- package/dist/plugins/transform/commands.js +38 -0
- package/dist/plugins/transform/example_profiles.js +32 -0
- package/dist/plugins/transform/hz_anime.js +211 -0
- package/dist/plugins/transform/index.js +93 -0
- package/dist/script/index.js +537 -0
- package/dist/script/readscript.js +15 -0
- package/dist/script/strtools.js +157 -0
- package/dist/storage/decorator.js +260 -0
- package/dist/storage/fs.js +96 -0
- package/dist/storage/index.js +442 -0
- package/dist/system/index.js +144 -0
- package/dist/ui/index.js +535 -0
- package/dist/utils/path.js +289 -0
- package/license.txt +202 -0
- package/package.json +26 -0
- package/src/async/index.ts +124 -0
- package/src/async/zeppos_timer.js +65 -0
- package/src/audio/index.ts +224 -0
- package/src/config/index.ts +80 -0
- package/src/debug/index.ts +11 -0
- package/src/index.ts +122 -0
- package/src/platform/index.ts +158 -0
- package/src/plugins/basic_command/$.ts +11 -0
- package/src/plugins/basic_command/audio.ts +53 -0
- package/src/plugins/basic_command/basic.ts +145 -0
- package/src/plugins/basic_command/character.ts +144 -0
- package/src/plugins/basic_command/conditional.ts +349 -0
- package/src/plugins/basic_command/config.ts +29 -0
- package/src/plugins/basic_command/decorator.ts +29 -0
- package/src/plugins/basic_command/eval.ts +88 -0
- package/src/plugins/basic_command/img.ts +317 -0
- package/src/plugins/basic_command/index.ts +24 -0
- package/src/plugins/basic_command/menu.ts +178 -0
- package/src/plugins/global_gesture/index.ts +29 -0
- package/src/plugins/transform/animation.ts +542 -0
- package/src/plugins/transform/commands.ts +53 -0
- package/src/plugins/transform/example_profiles.ts +36 -0
- package/src/plugins/transform/hz_anime.ts +214 -0
- package/src/plugins/transform/index.ts +141 -0
- package/src/plugins/transform/readme.md +1 -0
- package/src/script/index.ts +623 -0
- package/src/script/readscript.ts +17 -0
- package/src/script/strtools.ts +159 -0
- package/src/storage/decorator.ts +473 -0
- package/src/storage/fs.ts +104 -0
- package/src/storage/index.ts +541 -0
- package/src/system/index.ts +95 -0
- package/src/ui/index.ts +699 -0
- package/src/utils/path.js +338 -0
- package/tsconfig.json +111 -0
- package/types/async/index.d.ts +24 -0
- package/types/async/zeppos_timer.d.ts +14 -0
- package/types/audio/index.d.ts +64 -0
- package/types/config/index.d.ts +9 -0
- package/types/debug/index.d.ts +6 -0
- package/types/index.d.ts +41 -0
- package/types/platform/index.d.ts +134 -0
- package/types/plugins/basic_command/$.d.ts +2 -0
- package/types/plugins/basic_command/audio.d.ts +2 -0
- package/types/plugins/basic_command/basic.d.ts +3 -0
- package/types/plugins/basic_command/character.d.ts +2 -0
- package/types/plugins/basic_command/conditional.d.ts +2 -0
- package/types/plugins/basic_command/config.d.ts +2 -0
- package/types/plugins/basic_command/decorator.d.ts +2 -0
- package/types/plugins/basic_command/eval.d.ts +2 -0
- package/types/plugins/basic_command/img.d.ts +2 -0
- package/types/plugins/basic_command/index.d.ts +2 -0
- package/types/plugins/basic_command/menu.d.ts +2 -0
- package/types/plugins/global_gesture/index.d.ts +2 -0
- package/types/plugins/transform/animation.d.ts +131 -0
- package/types/plugins/transform/commands.d.ts +7 -0
- package/types/plugins/transform/example_profiles.d.ts +2 -0
- package/types/plugins/transform/hz_anime.d.ts +51 -0
- package/types/plugins/transform/index.d.ts +13 -0
- package/types/script/index.d.ts +123 -0
- package/types/script/readscript.d.ts +2 -0
- package/types/script/strtools.d.ts +31 -0
- package/types/storage/decorator.d.ts +41 -0
- package/types/storage/fs.d.ts +1 -0
- package/types/storage/index.d.ts +86 -0
- package/types/system/index.d.ts +35 -0
- package/types/ui/index.d.ts +167 -0
- package/types/utils/path.d.ts +84 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// import * as this._core.platform from "@zos/fs";
|
|
2
|
+
import Path from "../utils/path.js";
|
|
3
|
+
// import { isFileSync } from "./fs.js";
|
|
4
|
+
export class Storage {
|
|
5
|
+
constructor(_core) {
|
|
6
|
+
this._core = _core;
|
|
7
|
+
this.projectRoot = null;
|
|
8
|
+
this.cacheRoot = null;
|
|
9
|
+
this.saveRoot = null;
|
|
10
|
+
this.preloadedData = null;
|
|
11
|
+
this.packageData = null;
|
|
12
|
+
// Storage Data
|
|
13
|
+
/**
|
|
14
|
+
* 全局数据
|
|
15
|
+
* 其中的数据不会跟随存档保存,而是直接存储在全局数据文件中
|
|
16
|
+
* 如:设置、CG解锁情况等
|
|
17
|
+
*/
|
|
18
|
+
this._globalData = null;
|
|
19
|
+
/**
|
|
20
|
+
* 存档数据
|
|
21
|
+
* 其中的数据会跟随存档保存
|
|
22
|
+
* 如:脚本执行位置即调用栈,攻略度等
|
|
23
|
+
*/
|
|
24
|
+
this._archiveData = null;
|
|
25
|
+
/**
|
|
26
|
+
* 保存全局数据
|
|
27
|
+
* 可以多次調用,實際上會異步儲存,也就是在一個宏任務中即使調用多次,也只會在宏任務結束後儲存一次
|
|
28
|
+
*/
|
|
29
|
+
this._saveGlobalDataTimerId = null;
|
|
30
|
+
this._saveArchiveDataTimerId = null;
|
|
31
|
+
}
|
|
32
|
+
loadProject(options) {
|
|
33
|
+
this._core.emit("beforeLoadProject");
|
|
34
|
+
if (!this._core.platform.readdirSync({ path: options.projectPath })) {
|
|
35
|
+
throw "Dir not exist";
|
|
36
|
+
}
|
|
37
|
+
this.projectRoot = options.projectPath;
|
|
38
|
+
this.cacheRoot = options.cachePath;
|
|
39
|
+
this.saveRoot = options.savePath;
|
|
40
|
+
this.loadPackageData();
|
|
41
|
+
this.preload();
|
|
42
|
+
this._core.emit("afterLoadProject");
|
|
43
|
+
}
|
|
44
|
+
loadPackageData() {
|
|
45
|
+
if (!this.projectRoot)
|
|
46
|
+
throw "projectDir is null";
|
|
47
|
+
this._core.debug.log(`loadPackageData ${this.projectRoot} ${Path.join(this.projectRoot, "hz_package.json")}`);
|
|
48
|
+
if (!this._core.platform.statSync({
|
|
49
|
+
path: Path.join(this.projectRoot, "hz_package.json"),
|
|
50
|
+
})) {
|
|
51
|
+
throw "HZEngine Package File (hz_package.json) not exist";
|
|
52
|
+
}
|
|
53
|
+
this.packageData = JSON.parse(this._core.platform.readFileSync({
|
|
54
|
+
path: Path.join(this.projectRoot, "hz_package.json"),
|
|
55
|
+
options: { encoding: "utf8" },
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
get globalData() {
|
|
59
|
+
if (!this._globalData) {
|
|
60
|
+
this.loadGlobalData();
|
|
61
|
+
}
|
|
62
|
+
if (this._globalData == null)
|
|
63
|
+
throw `[HZEngine] GlobalData is null`;
|
|
64
|
+
return this._globalData;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* alias globalData
|
|
68
|
+
*/
|
|
69
|
+
get gd() {
|
|
70
|
+
return this.globalData;
|
|
71
|
+
}
|
|
72
|
+
get archiveData() {
|
|
73
|
+
if (!this._archiveData) {
|
|
74
|
+
this.loadArchiveData();
|
|
75
|
+
}
|
|
76
|
+
if (this._archiveData == null)
|
|
77
|
+
throw `[HZEngine] ArchiveData is null`;
|
|
78
|
+
return this._archiveData;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* alias archiveData
|
|
82
|
+
*/
|
|
83
|
+
get sd() {
|
|
84
|
+
return this.archiveData;
|
|
85
|
+
}
|
|
86
|
+
loadGlobalData() {
|
|
87
|
+
if (!this.saveRoot) {
|
|
88
|
+
throw "saveDir is null, please loadProject first";
|
|
89
|
+
}
|
|
90
|
+
this._core.emit("beforeLoadGlobalData");
|
|
91
|
+
if (this._core.platform.statSync({
|
|
92
|
+
path: Path.join(this.saveRoot, "globalData.json"),
|
|
93
|
+
})) {
|
|
94
|
+
this._globalData = JSON.parse(this._core.platform.readFileSync({
|
|
95
|
+
path: Path.join(this.saveRoot, "globalData.json"),
|
|
96
|
+
options: {
|
|
97
|
+
encoding: "utf8",
|
|
98
|
+
},
|
|
99
|
+
}));
|
|
100
|
+
if (this._globalData == null) {
|
|
101
|
+
this._globalData = {}; // TODO initial GlobalData value
|
|
102
|
+
this._core.emit("initGlobalData");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
this._core.debug.log(`globalData.json not exist, create it.`);
|
|
107
|
+
this._globalData = {}; // Initial GlobalData value
|
|
108
|
+
this._core.emit("initGlobalData");
|
|
109
|
+
this._core.platform.writeFileSync({
|
|
110
|
+
path: Path.join(this.saveRoot, "globalData.json"),
|
|
111
|
+
data: JSON.stringify(this._globalData),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
this._core.emit("afterLoadGlobalData");
|
|
115
|
+
}
|
|
116
|
+
saveGlobalData() {
|
|
117
|
+
if (!this.projectRoot) {
|
|
118
|
+
throw "projectDir is null, please loadProject first";
|
|
119
|
+
}
|
|
120
|
+
if (this._saveGlobalDataTimerId)
|
|
121
|
+
return;
|
|
122
|
+
this._saveGlobalDataTimerId = setTimeout(() => {
|
|
123
|
+
this._saveGlobalDataTimerId = null;
|
|
124
|
+
if (!this.projectRoot) {
|
|
125
|
+
throw "projectDir is null, please loadProject first";
|
|
126
|
+
}
|
|
127
|
+
this._core.emit("beforeSaveGlobalData");
|
|
128
|
+
let res = this._core.platform.writeFileSync({
|
|
129
|
+
path: Path.join(this.saveRoot, "globalData.json"),
|
|
130
|
+
data: JSON.stringify(this._globalData),
|
|
131
|
+
}); // TODO
|
|
132
|
+
if (res < 0)
|
|
133
|
+
throw `[HZEngine] save globalData to globalData.json failed, code = ${res}`;
|
|
134
|
+
this._core.debug.log(`save globalData to globalData.json`);
|
|
135
|
+
this._core.emit("afterSaveGlobalData");
|
|
136
|
+
}, 0);
|
|
137
|
+
}
|
|
138
|
+
loadArchiveData(archiveFile) {
|
|
139
|
+
this._core.emit("beforeLoadArchive");
|
|
140
|
+
if (archiveFile) {
|
|
141
|
+
if (!this.saveRoot)
|
|
142
|
+
throw `saveRoot is null, please loadProject first`;
|
|
143
|
+
if (!this._core.platform.statSync({
|
|
144
|
+
path: Path.join(this.saveRoot, archiveFile),
|
|
145
|
+
})) {
|
|
146
|
+
throw `Archive [${archiveFile}] not exist`;
|
|
147
|
+
}
|
|
148
|
+
let archiveData = JSON.parse(this._core.platform.readFileSync({
|
|
149
|
+
path: Path.join(this.saveRoot, archiveFile),
|
|
150
|
+
options: {
|
|
151
|
+
encoding: "utf8",
|
|
152
|
+
},
|
|
153
|
+
}));
|
|
154
|
+
if (archiveData == null)
|
|
155
|
+
throw `[HZEngine] ArchiveData is null`;
|
|
156
|
+
this._archiveData = archiveData;
|
|
157
|
+
this._core.debug.log(`load archiveData from ${archiveFile}`);
|
|
158
|
+
this._core.emit("afterLoadArchive");
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
this._core.debug.log(`load archiveData from empty template`);
|
|
162
|
+
this._archiveData = {};
|
|
163
|
+
this._core.emit("initArchiveData");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* 保存存档数据
|
|
168
|
+
* 可以多次調用,實際上會異步儲存,也就是在一個宏任務中即使調用多次,也只會在宏任務結束後儲存一次
|
|
169
|
+
* @param archiveFile 存档文件目錄及名字
|
|
170
|
+
*/
|
|
171
|
+
saveArchiveData(archiveFile, immediate = false) {
|
|
172
|
+
if (!this.saveRoot)
|
|
173
|
+
throw `saveRoot is null, please loadProject first`;
|
|
174
|
+
this._core.emit("beforeSaveArchive");
|
|
175
|
+
this._core.debug.log("Will save archiveData to " + archiveFile);
|
|
176
|
+
let saveFunc = () => {
|
|
177
|
+
this._core.debug.log("Saving archiveData to " + archiveFile);
|
|
178
|
+
if (!this.saveRoot)
|
|
179
|
+
throw `projectDir is null, please loadProject first`;
|
|
180
|
+
let res = this._core.platform.writeFileSync({
|
|
181
|
+
path: Path.join(this.saveRoot, archiveFile),
|
|
182
|
+
data: JSON.stringify(this._archiveData),
|
|
183
|
+
}); //TODO
|
|
184
|
+
if (res < 0)
|
|
185
|
+
throw `[HZEngine] save archiveData to ${archiveFile} failed, code = ${res}`;
|
|
186
|
+
this._core.debug.log(`Save archiveData to ${archiveFile}`);
|
|
187
|
+
this._core.emit("afterSaveArchive");
|
|
188
|
+
};
|
|
189
|
+
if (immediate) {
|
|
190
|
+
saveFunc();
|
|
191
|
+
if (this._saveArchiveDataTimerId) {
|
|
192
|
+
clearTimeout(this._saveArchiveDataTimerId);
|
|
193
|
+
this._saveArchiveDataTimerId = null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else
|
|
197
|
+
this._saveArchiveDataTimerId = setTimeout(() => {
|
|
198
|
+
this._saveArchiveDataTimerId = null;
|
|
199
|
+
saveFunc();
|
|
200
|
+
}, 0);
|
|
201
|
+
}
|
|
202
|
+
getSaveableData(data, auto_correct, ...key_chain) {
|
|
203
|
+
let obj = data;
|
|
204
|
+
// if(obj == null) throw `[HZEngine] saveable data is null`
|
|
205
|
+
for (let key of key_chain) {
|
|
206
|
+
if (obj == null)
|
|
207
|
+
throw Error(`[HZEngine] saveable data is null`);
|
|
208
|
+
if (typeof obj !== "object")
|
|
209
|
+
throw Error(`[HZEngine] saveable data is not object`);
|
|
210
|
+
if (Array.isArray(obj))
|
|
211
|
+
throw Error(`[HZEngine] saveable data is array`);
|
|
212
|
+
if (!obj[key]) {
|
|
213
|
+
if (auto_correct) {
|
|
214
|
+
obj[key] = {};
|
|
215
|
+
}
|
|
216
|
+
else
|
|
217
|
+
throw Error(`[HZEngine] saveable data key ${key} not exist`);
|
|
218
|
+
}
|
|
219
|
+
obj = obj[key];
|
|
220
|
+
}
|
|
221
|
+
if (obj == null)
|
|
222
|
+
throw Error(`[HZEngine] saveable data result obj is null`);
|
|
223
|
+
// if (typeof obj !== "object")
|
|
224
|
+
// throw Error(`[HZEngine] saveable data result obj is not array or object`);
|
|
225
|
+
// if (Array.isArray(obj)) throw Error(`[HZEngine] saveable data result is an array, key_chain = ${key_chain.join(".")}, res = ${JSON.stringify(obj)}`);
|
|
226
|
+
return obj;
|
|
227
|
+
}
|
|
228
|
+
setSaveableData(data, auto_correct, value, ...key_chain) {
|
|
229
|
+
this._core.debug.log(`setSaveableData ${key_chain} => ${JSON.stringify(value)}`);
|
|
230
|
+
if (key_chain.length == 0)
|
|
231
|
+
throw `key_chain is empty`;
|
|
232
|
+
let parentObj = this.getSaveableData(data, auto_correct, ...key_chain.slice(0, -1));
|
|
233
|
+
if (parentObj == null)
|
|
234
|
+
throw `[HZEngine] saveable data is null`;
|
|
235
|
+
if (typeof parentObj !== "object")
|
|
236
|
+
throw `[HZEngine] saveable data is not object`;
|
|
237
|
+
if (Array.isArray(parentObj))
|
|
238
|
+
throw `[HZEngine] saveable data is array`;
|
|
239
|
+
parentObj[key_chain[key_chain.length - 1]] = value;
|
|
240
|
+
}
|
|
241
|
+
checkSaveableData(data, ...key_chain) {
|
|
242
|
+
return this.getSaveableData(data, false, ...key_chain);
|
|
243
|
+
}
|
|
244
|
+
// Preload
|
|
245
|
+
preload() {
|
|
246
|
+
if (!this.cacheRoot) {
|
|
247
|
+
throw "cacheRoot is null, please loadProject first";
|
|
248
|
+
}
|
|
249
|
+
// writeFileSync({path: "data://test.json", data: "awa", options: {encoding: "utf8"}});
|
|
250
|
+
if (this._core.platform.statSync({
|
|
251
|
+
path: Path.join(this.cacheRoot, "preloaded.json"),
|
|
252
|
+
})) {
|
|
253
|
+
// 已经预加载过了,退出
|
|
254
|
+
this.preloadedData = JSON.parse(this._core.platform.readFileSync({
|
|
255
|
+
path: Path.join(this.cacheRoot, "preloaded.json"),
|
|
256
|
+
options: {
|
|
257
|
+
encoding: "utf8",
|
|
258
|
+
},
|
|
259
|
+
}));
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
this.preloadedData = {
|
|
263
|
+
script: {
|
|
264
|
+
labelMap: {},
|
|
265
|
+
hzsInfoMap: {},
|
|
266
|
+
},
|
|
267
|
+
image: {
|
|
268
|
+
nameMap: {},
|
|
269
|
+
},
|
|
270
|
+
animation: {
|
|
271
|
+
profileMap: {},
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
this.preloadScript();
|
|
275
|
+
this.preloadImage();
|
|
276
|
+
this.preloadAnimation();
|
|
277
|
+
// console.log(JSON.stringify(this.preloadedData));
|
|
278
|
+
this._core.platform.writeFileSync({
|
|
279
|
+
path: Path.join(this.cacheRoot, "preloaded.json"),
|
|
280
|
+
data: JSON.stringify(this.preloadedData),
|
|
281
|
+
});
|
|
282
|
+
// console.log(
|
|
283
|
+
// `${Path.join(this.projectDir, "preloaded.json")} = ${readFileSync({
|
|
284
|
+
// path: Path.join(this.projectDir, "preloaded.json"),
|
|
285
|
+
// options:{encoding:"utf8"}
|
|
286
|
+
// })}`
|
|
287
|
+
// );
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* 预加载脚本
|
|
291
|
+
* 遍历出所有hzs文件和所有label,建立map
|
|
292
|
+
*/
|
|
293
|
+
preloadScript() {
|
|
294
|
+
// TODO
|
|
295
|
+
// 记录脚本label的位置
|
|
296
|
+
// 这里的index是以0开始计数的行数
|
|
297
|
+
let labelMap = this.preloadedData.script.labelMap;
|
|
298
|
+
let scriptDir = Path.join(this.projectRoot, "script");
|
|
299
|
+
let hzsInfoMap = this.preloadedData.script.hzsInfoMap;
|
|
300
|
+
if (!this._core.platform.readdirSync({ path: scriptDir }))
|
|
301
|
+
throw "项目文件夹中script文件夹不存在";
|
|
302
|
+
/**
|
|
303
|
+
* 1. 预加载所有的label并检查冲突
|
|
304
|
+
* 2. 记录所有脚本文件的行数
|
|
305
|
+
*/
|
|
306
|
+
const preloadHzs = (path) => {
|
|
307
|
+
// let fd = this._core.platform.openSync({ path });
|
|
308
|
+
// if (fd < 0) throw "Fd<0";
|
|
309
|
+
// let size = this._core.platform.statSync({ path })!.size;
|
|
310
|
+
// let arrbuf = new ArrayBuffer(size);
|
|
311
|
+
// this._core.platform.readSync({ fd, buffer: arrbuf });
|
|
312
|
+
// let buffer = Buffer.from(arrbuf);
|
|
313
|
+
// let contentStr = buffer.toString();
|
|
314
|
+
let contentStr = this._core.platform.readFileSync({ path, options: { encoding: "utf8" } });
|
|
315
|
+
let contentLines = contentStr.split("\n");
|
|
316
|
+
let totalLines = contentLines.length;
|
|
317
|
+
hzsInfoMap[path] = { totalLines };
|
|
318
|
+
for (let i = 0; i < totalLines; ++i) {
|
|
319
|
+
let line = contentLines[i].trim();
|
|
320
|
+
if (line.startsWith("*")) {
|
|
321
|
+
let len = line.length, p = 1, q;
|
|
322
|
+
while (p < len && line.charAt(p) === " ")
|
|
323
|
+
++p;
|
|
324
|
+
if (p === len)
|
|
325
|
+
throw `Lost Label Name at file(${path}) line(${i + 1})`;
|
|
326
|
+
q = p;
|
|
327
|
+
while (q < len && line.charAt(q) !== " ")
|
|
328
|
+
++q;
|
|
329
|
+
// [p, q)
|
|
330
|
+
let label = line.slice(p, q);
|
|
331
|
+
if (labelMap[label]) {
|
|
332
|
+
throw `Label name "${label}" conflict : \
|
|
333
|
+
at [${labelMap[label][0]}(line ${labelMap[label][1]})] \
|
|
334
|
+
[${path}(line ${i + 1})]`;
|
|
335
|
+
}
|
|
336
|
+
labelMap[label] = [path, i];
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
// 遍历所有hzs文件
|
|
341
|
+
const traverseScript = (path) => {
|
|
342
|
+
let dirs = this._core.platform.readdirSync({ path });
|
|
343
|
+
// console.log(dirs);
|
|
344
|
+
for (let dir of dirs) {
|
|
345
|
+
let subpath = Path.join(path, dir);
|
|
346
|
+
if (this._core.platform.isFileSync({ path: subpath })) {
|
|
347
|
+
// 是文件
|
|
348
|
+
if (dir.endsWith(".hzs")) {
|
|
349
|
+
preloadHzs(subpath);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
// 是目录
|
|
354
|
+
traverseScript(subpath);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
traverseScript(scriptDir);
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* 预加载资源
|
|
362
|
+
* 遍历所有png文件,计算对应的name key,建立map
|
|
363
|
+
*/
|
|
364
|
+
preloadImage() {
|
|
365
|
+
let nameMap = this.preloadedData.image.nameMap;
|
|
366
|
+
let imageDir = Path.join(this.projectRoot, "image");
|
|
367
|
+
if (!this._core.platform.readdirSync({ path: imageDir }))
|
|
368
|
+
throw "项目文件夹中image文件夹不存在";
|
|
369
|
+
/**
|
|
370
|
+
* 1. 预加载所有的image并检查冲突
|
|
371
|
+
* 2. 记录所有image的name key和路径
|
|
372
|
+
*/
|
|
373
|
+
function preloadImage(path) {
|
|
374
|
+
let raw_name = Path.parse(path).name;
|
|
375
|
+
let name_key = raw_name
|
|
376
|
+
.trim()
|
|
377
|
+
.replace("_", " ")
|
|
378
|
+
.replace(/ +/, " ")
|
|
379
|
+
.toLowerCase();
|
|
380
|
+
if (nameMap[name_key])
|
|
381
|
+
throw `Image name key conflict [${name_key}], at file [${path}] and [${nameMap[name_key]}]`;
|
|
382
|
+
nameMap[name_key] = [path];
|
|
383
|
+
}
|
|
384
|
+
// 遍历所有hzs文件
|
|
385
|
+
const traverseImage = (path) => {
|
|
386
|
+
let dirs = this._core.platform.readdirSync({ path });
|
|
387
|
+
// console.log(dirs);
|
|
388
|
+
for (let dir of dirs) {
|
|
389
|
+
let subpath = Path.join(path, dir);
|
|
390
|
+
if (this._core.platform.isFileSync({ path: subpath })) {
|
|
391
|
+
// 是文件
|
|
392
|
+
if (dir.endsWith(".png")) {
|
|
393
|
+
preloadImage(subpath);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
// 是目录
|
|
398
|
+
traverseImage(subpath);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
traverseImage(imageDir);
|
|
403
|
+
// console.log(
|
|
404
|
+
// `Preloaded image: ${JSON.stringify(this.preloadedData.image.nameMap)}`
|
|
405
|
+
// );
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* 預加載動畫profile
|
|
409
|
+
* 遍歷animation文件夾下的所有json文件,以文件名為key,json内容為value
|
|
410
|
+
*/
|
|
411
|
+
preloadAnimation() {
|
|
412
|
+
let profileMap = this.preloadedData.animation.profileMap;
|
|
413
|
+
let animationDir = Path.join(this.projectRoot, "animation");
|
|
414
|
+
if (!this._core.platform.readdirSync({ path: animationDir }))
|
|
415
|
+
throw "项目文件夹中animation文件夹不存在";
|
|
416
|
+
function preloadAnimation(path) {
|
|
417
|
+
let raw_name = Path.parse(path).name;
|
|
418
|
+
let name_key = raw_name.trim().replace(/ +/, "_");
|
|
419
|
+
if (profileMap[name_key])
|
|
420
|
+
throw `Animation profile name key conflict [${name_key}], at file [${path}] and [${profileMap[name_key]}]`;
|
|
421
|
+
profileMap[name_key] = [path];
|
|
422
|
+
}
|
|
423
|
+
const traverseAnimation = (path) => {
|
|
424
|
+
let dirs = this._core.platform.readdirSync({ path });
|
|
425
|
+
// console.log(dirs);
|
|
426
|
+
for (let dir of dirs) {
|
|
427
|
+
let subpath = Path.join(path, dir);
|
|
428
|
+
if (this._core.platform.isFileSync({ path: subpath })) {
|
|
429
|
+
// 是文件
|
|
430
|
+
if (dir.endsWith(".json")) {
|
|
431
|
+
preloadAnimation(subpath);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
// 是目录
|
|
436
|
+
traverseAnimation(subpath);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
traverseAnimation(animationDir);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
36
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
37
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
38
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
39
|
+
};
|
|
40
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
41
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
42
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
43
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
44
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
45
|
+
};
|
|
46
|
+
import { Save } from "../storage/decorator.js";
|
|
47
|
+
let System = (() => {
|
|
48
|
+
var _a, _System_condition_accessor_storage;
|
|
49
|
+
let _condition_decorators;
|
|
50
|
+
let _condition_initializers = [];
|
|
51
|
+
let _condition_extraInitializers = [];
|
|
52
|
+
return _a = class System {
|
|
53
|
+
constructor(_core) {
|
|
54
|
+
this._core = _core;
|
|
55
|
+
_System_condition_accessor_storage.set(this, __runInitializers(this, _condition_initializers, _a.Condition.Free));
|
|
56
|
+
this._pauseTimer = (__runInitializers(this, _condition_extraInitializers), null);
|
|
57
|
+
_core.on("system.continue", () => {
|
|
58
|
+
this._pauseTimer = null;
|
|
59
|
+
this.continue();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
get condition() { return __classPrivateFieldGet(this, _System_condition_accessor_storage, "f"); }
|
|
63
|
+
set condition(value) { __classPrivateFieldSet(this, _System_condition_accessor_storage, value, "f"); }
|
|
64
|
+
/**
|
|
65
|
+
* 暂停(可指定一段时间)
|
|
66
|
+
* 后调用的会覆盖之前pause的设定时间
|
|
67
|
+
* @param delayMs
|
|
68
|
+
*/
|
|
69
|
+
pause(delayMs) {
|
|
70
|
+
this._core.debug.log(`Pause`);
|
|
71
|
+
if (this.condition === _a.Condition.Gaming) {
|
|
72
|
+
this.condition = _a.Condition.Pause;
|
|
73
|
+
}
|
|
74
|
+
else
|
|
75
|
+
throw `pause but condition error (todo)`; // TODO
|
|
76
|
+
if (this._pauseTimer) {
|
|
77
|
+
this._core.async.removeTask(this._pauseTimer);
|
|
78
|
+
this._pauseTimer = null;
|
|
79
|
+
}
|
|
80
|
+
if (delayMs !== undefined) {
|
|
81
|
+
this._pauseTimer = this._core.async.addDelayTask("system.continue", [], delayMs);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 继续由于pause中断的游戏
|
|
86
|
+
*/
|
|
87
|
+
continue() {
|
|
88
|
+
if (this.condition !== _a.Condition.Pause)
|
|
89
|
+
return;
|
|
90
|
+
this.condition = _a.Condition.Gaming;
|
|
91
|
+
this.run();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 阻塞
|
|
95
|
+
*/
|
|
96
|
+
block() {
|
|
97
|
+
if (this.condition !== _a.Condition.Gaming)
|
|
98
|
+
throw `block but condition error (todo)`;
|
|
99
|
+
this.condition = _a.Condition.Blocked;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 取消阻塞
|
|
103
|
+
*/
|
|
104
|
+
unBlock() {
|
|
105
|
+
if (this.condition !== _a.Condition.Blocked)
|
|
106
|
+
return;
|
|
107
|
+
this.condition = _a.Condition.Gaming;
|
|
108
|
+
this.run();
|
|
109
|
+
}
|
|
110
|
+
run() {
|
|
111
|
+
this.condition = _a.Condition.Gaming;
|
|
112
|
+
while (this.condition === _a.Condition.Gaming) {
|
|
113
|
+
// console.log("Run Single Line");
|
|
114
|
+
this._core.script.runSingleLine();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
start(initLabel = "start") {
|
|
118
|
+
// 初始化存档
|
|
119
|
+
this._core.storage.archiveData;
|
|
120
|
+
this._core.debug.log("Game Start");
|
|
121
|
+
this._core.script.clear();
|
|
122
|
+
this._core.script.jumpLabel(initLabel);
|
|
123
|
+
this.run();
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
_System_condition_accessor_storage = new WeakMap(),
|
|
127
|
+
(() => {
|
|
128
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
129
|
+
_condition_decorators = [Save("system.condition")];
|
|
130
|
+
__esDecorate(_a, null, _condition_decorators, { kind: "accessor", name: "condition", static: false, private: false, access: { has: obj => "condition" in obj, get: obj => obj.condition, set: (obj, value) => { obj.condition = value; } }, metadata: _metadata }, _condition_initializers, _condition_extraInitializers);
|
|
131
|
+
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
132
|
+
})(),
|
|
133
|
+
_a;
|
|
134
|
+
})();
|
|
135
|
+
export { System };
|
|
136
|
+
(function (System) {
|
|
137
|
+
let Condition;
|
|
138
|
+
(function (Condition) {
|
|
139
|
+
Condition[Condition["Free"] = 0] = "Free";
|
|
140
|
+
Condition[Condition["Pause"] = 1] = "Pause";
|
|
141
|
+
Condition[Condition["Blocked"] = 2] = "Blocked";
|
|
142
|
+
Condition[Condition["Gaming"] = 3] = "Gaming";
|
|
143
|
+
})(Condition = System.Condition || (System.Condition = {}));
|
|
144
|
+
})(System || (System = {}));
|