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.
Files changed (103) hide show
  1. package/dist/async/index.js +162 -0
  2. package/dist/async/zeppos_timer.js +58 -0
  3. package/dist/audio/index.js +260 -0
  4. package/dist/config/index.js +57 -0
  5. package/dist/debug/index.js +8 -0
  6. package/dist/index.js +103 -0
  7. package/dist/platform/index.js +1 -0
  8. package/dist/plugins/basic_command/$.js +8 -0
  9. package/dist/plugins/basic_command/audio.js +40 -0
  10. package/dist/plugins/basic_command/basic.js +124 -0
  11. package/dist/plugins/basic_command/character.js +112 -0
  12. package/dist/plugins/basic_command/conditional.js +260 -0
  13. package/dist/plugins/basic_command/config.js +22 -0
  14. package/dist/plugins/basic_command/decorator.js +24 -0
  15. package/dist/plugins/basic_command/eval.js +67 -0
  16. package/dist/plugins/basic_command/img.js +249 -0
  17. package/dist/plugins/basic_command/index.js +22 -0
  18. package/dist/plugins/basic_command/menu.js +140 -0
  19. package/dist/plugins/global_gesture/index.js +25 -0
  20. package/dist/plugins/transform/animation.js +440 -0
  21. package/dist/plugins/transform/commands.js +38 -0
  22. package/dist/plugins/transform/example_profiles.js +32 -0
  23. package/dist/plugins/transform/hz_anime.js +211 -0
  24. package/dist/plugins/transform/index.js +93 -0
  25. package/dist/script/index.js +537 -0
  26. package/dist/script/readscript.js +15 -0
  27. package/dist/script/strtools.js +157 -0
  28. package/dist/storage/decorator.js +260 -0
  29. package/dist/storage/fs.js +96 -0
  30. package/dist/storage/index.js +442 -0
  31. package/dist/system/index.js +144 -0
  32. package/dist/ui/index.js +535 -0
  33. package/dist/utils/path.js +289 -0
  34. package/license.txt +202 -0
  35. package/package.json +26 -0
  36. package/src/async/index.ts +124 -0
  37. package/src/async/zeppos_timer.js +65 -0
  38. package/src/audio/index.ts +224 -0
  39. package/src/config/index.ts +80 -0
  40. package/src/debug/index.ts +11 -0
  41. package/src/index.ts +122 -0
  42. package/src/platform/index.ts +158 -0
  43. package/src/plugins/basic_command/$.ts +11 -0
  44. package/src/plugins/basic_command/audio.ts +53 -0
  45. package/src/plugins/basic_command/basic.ts +145 -0
  46. package/src/plugins/basic_command/character.ts +144 -0
  47. package/src/plugins/basic_command/conditional.ts +349 -0
  48. package/src/plugins/basic_command/config.ts +29 -0
  49. package/src/plugins/basic_command/decorator.ts +29 -0
  50. package/src/plugins/basic_command/eval.ts +88 -0
  51. package/src/plugins/basic_command/img.ts +317 -0
  52. package/src/plugins/basic_command/index.ts +24 -0
  53. package/src/plugins/basic_command/menu.ts +178 -0
  54. package/src/plugins/global_gesture/index.ts +29 -0
  55. package/src/plugins/transform/animation.ts +542 -0
  56. package/src/plugins/transform/commands.ts +53 -0
  57. package/src/plugins/transform/example_profiles.ts +36 -0
  58. package/src/plugins/transform/hz_anime.ts +214 -0
  59. package/src/plugins/transform/index.ts +141 -0
  60. package/src/plugins/transform/readme.md +1 -0
  61. package/src/script/index.ts +623 -0
  62. package/src/script/readscript.ts +17 -0
  63. package/src/script/strtools.ts +159 -0
  64. package/src/storage/decorator.ts +473 -0
  65. package/src/storage/fs.ts +104 -0
  66. package/src/storage/index.ts +541 -0
  67. package/src/system/index.ts +95 -0
  68. package/src/ui/index.ts +699 -0
  69. package/src/utils/path.js +338 -0
  70. package/tsconfig.json +111 -0
  71. package/types/async/index.d.ts +24 -0
  72. package/types/async/zeppos_timer.d.ts +14 -0
  73. package/types/audio/index.d.ts +64 -0
  74. package/types/config/index.d.ts +9 -0
  75. package/types/debug/index.d.ts +6 -0
  76. package/types/index.d.ts +41 -0
  77. package/types/platform/index.d.ts +134 -0
  78. package/types/plugins/basic_command/$.d.ts +2 -0
  79. package/types/plugins/basic_command/audio.d.ts +2 -0
  80. package/types/plugins/basic_command/basic.d.ts +3 -0
  81. package/types/plugins/basic_command/character.d.ts +2 -0
  82. package/types/plugins/basic_command/conditional.d.ts +2 -0
  83. package/types/plugins/basic_command/config.d.ts +2 -0
  84. package/types/plugins/basic_command/decorator.d.ts +2 -0
  85. package/types/plugins/basic_command/eval.d.ts +2 -0
  86. package/types/plugins/basic_command/img.d.ts +2 -0
  87. package/types/plugins/basic_command/index.d.ts +2 -0
  88. package/types/plugins/basic_command/menu.d.ts +2 -0
  89. package/types/plugins/global_gesture/index.d.ts +2 -0
  90. package/types/plugins/transform/animation.d.ts +131 -0
  91. package/types/plugins/transform/commands.d.ts +7 -0
  92. package/types/plugins/transform/example_profiles.d.ts +2 -0
  93. package/types/plugins/transform/hz_anime.d.ts +51 -0
  94. package/types/plugins/transform/index.d.ts +13 -0
  95. package/types/script/index.d.ts +123 -0
  96. package/types/script/readscript.d.ts +2 -0
  97. package/types/script/strtools.d.ts +31 -0
  98. package/types/storage/decorator.d.ts +41 -0
  99. package/types/storage/fs.d.ts +1 -0
  100. package/types/storage/index.d.ts +86 -0
  101. package/types/system/index.d.ts +35 -0
  102. package/types/ui/index.d.ts +167 -0
  103. package/types/utils/path.d.ts +84 -0
@@ -0,0 +1,289 @@
1
+ // path utils for ZeppOS
2
+ // @CuberQAQ
3
+ // Generated by GPT-4o mini
4
+ "use strict";
5
+ var util = {
6
+ isString: (x) => typeof x === "string", // 检查是否为字符串
7
+ isObject: (x) => typeof x === "object", // 检查是否为对象
8
+ };
9
+ /**
10
+ * 检查路径是否包含协议
11
+ * @param {string} path - 路径字符串
12
+ * @returns {boolean} - 如果路径包含协议,则返回 true
13
+ */
14
+ function isProtocolPath(path) {
15
+ return /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(path);
16
+ }
17
+ /**
18
+ * 提取协议和路径部分
19
+ * @param {string} path - 路径字符串
20
+ * @returns {{protocol: string, filepath: string}} - 包含协议和文件路径的对象
21
+ */
22
+ function extractProtocol(path) {
23
+ const match = path.match(/^([a-zA-Z][a-zA-Z\d+\-.]*):\/\/(.*)$/);
24
+ if (match) {
25
+ return {
26
+ protocol: match[1], // 获取协议
27
+ filepath: match[2], // 获取文件路径
28
+ };
29
+ }
30
+ return { protocol: null, filepath: path }; // 如果没有协议,返回原路径
31
+ }
32
+ /**
33
+ * 解析路径数组并规范化
34
+ * @param {string[]} parts - 路径组成部分
35
+ * @param {boolean} allowAboveRoot - 是否允许上级目录
36
+ * @returns {string[]} - 规范化后的路径部分
37
+ */
38
+ function normalizeArray(parts, allowAboveRoot) {
39
+ var res = [];
40
+ for (var i = 0; i < parts.length; i++) {
41
+ var p = parts[i];
42
+ // 忽略空部分和当前目录(.)
43
+ if (!p || p === ".")
44
+ continue;
45
+ // 处理上级目录(..)
46
+ if (p === "..") {
47
+ if (res.length && res[res.length - 1] !== "..") {
48
+ res.pop(); // 移除最后一个部分
49
+ }
50
+ else if (allowAboveRoot) {
51
+ res.push(".."); // 允许上级目录
52
+ }
53
+ }
54
+ else {
55
+ res.push(p); // 添加有效部分
56
+ }
57
+ }
58
+ return res;
59
+ }
60
+ /**
61
+ * 移除数组两端的空元素
62
+ * @param {string[]} arr - 输入数组
63
+ * @returns {string[]} - 去除空元素后的数组
64
+ */
65
+ function trimArray(arr) {
66
+ var lastIndex = arr.length - 1;
67
+ var start = 0;
68
+ for (; start <= lastIndex; start++) {
69
+ if (arr[start])
70
+ break;
71
+ }
72
+ var end = lastIndex;
73
+ for (; end >= 0; end--) {
74
+ if (arr[end])
75
+ break;
76
+ }
77
+ if (start === 0 && end === lastIndex)
78
+ return arr;
79
+ if (start > end)
80
+ return [];
81
+ return arr.slice(start, end + 1);
82
+ }
83
+ // 使用正则表达式分割路径
84
+ var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
85
+ var posix = {};
86
+ /**
87
+ * 分割路径
88
+ * @param {string} filename - 输入的路径字符串
89
+ * @returns {string[]} - 分割后的路径组成部分
90
+ */
91
+ function posixSplitPath(filename) {
92
+ return splitPathRe.exec(filename).slice(1);
93
+ }
94
+ /**
95
+ * 解析路径,将相对路径转换为绝对路径
96
+ * @param {...string} paths - 要解析的路径
97
+ * @returns {string} - 解析后的绝对路径
98
+ */
99
+ posix.resolve = function () {
100
+ var resolvedPath = "", resolvedAbsolute = false;
101
+ for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
102
+ var path = i >= 0 ? arguments[i] : process.cwd();
103
+ // 跳过空和无效的条目
104
+ if (!util.isString(path)) {
105
+ throw new TypeError("Arguments to path.resolve must be strings");
106
+ }
107
+ else if (!path) {
108
+ continue;
109
+ }
110
+ // 检查路径是否包含协议
111
+ if (isProtocolPath(path)) {
112
+ const { protocol, filepath } = extractProtocol(path);
113
+ resolvedPath = filepath + "/" + resolvedPath;
114
+ resolvedAbsolute = true; // 将协议路径视为绝对路径
115
+ }
116
+ else {
117
+ resolvedPath = path + "/" + resolvedPath;
118
+ resolvedAbsolute = path[0] === "/";
119
+ }
120
+ }
121
+ // 规范化路径
122
+ resolvedPath = normalizeArray(resolvedPath.split("/"), !resolvedAbsolute).join("/");
123
+ return (resolvedAbsolute ? "/" : "") + resolvedPath || ".";
124
+ };
125
+ /**
126
+ * 规范化路径,去除多余的斜杠和当前目录
127
+ * @param {string} path - 输入的路径字符串
128
+ * @returns {string} - 规范化后的路径
129
+ */
130
+ posix.normalize = function (path) {
131
+ // 检查路径是否包含协议
132
+ if (isProtocolPath(path)) {
133
+ const { protocol, filepath } = extractProtocol(path);
134
+ return protocol + "://" + normalizeArray(filepath.split("/")).join("/");
135
+ }
136
+ var isAbsolute = posix.isAbsolute(path), trailingSlash = path && path[path.length - 1] === "/";
137
+ // 规范化路径
138
+ path = normalizeArray(path.split("/"), !isAbsolute).join("/");
139
+ if (!path && !isAbsolute) {
140
+ path = ".";
141
+ }
142
+ if (path && trailingSlash) {
143
+ path += "/";
144
+ }
145
+ return (isAbsolute ? "/" : "") + path;
146
+ };
147
+ /**
148
+ * 检查路径是否为绝对路径
149
+ * @param {string} path - 输入的路径字符串
150
+ * @returns {boolean} - 如果路径为绝对路径,则返回 true
151
+ */
152
+ posix.isAbsolute = function (path) {
153
+ return path.charAt(0) === "/";
154
+ };
155
+ /**
156
+ * 连接多个路径段为一个路径
157
+ * @param {...string} segments - 要连接的路径段
158
+ * @returns {string} - 连接后的路径
159
+ */
160
+ posix.join = function () {
161
+ var path = "";
162
+ for (var i = 0; i < arguments.length; i++) {
163
+ var segment = arguments[i];
164
+ if (!util.isString(segment)) {
165
+ throw new TypeError("Arguments to path.join must be strings");
166
+ }
167
+ if (segment) {
168
+ if (!path) {
169
+ path += segment;
170
+ }
171
+ else {
172
+ path += "/" + segment;
173
+ }
174
+ }
175
+ }
176
+ return posix.normalize(path);
177
+ };
178
+ /**
179
+ * 计算相对路径
180
+ * @param {string} from - 起始路径
181
+ * @param {string} to - 目标路径
182
+ * @returns {string} - 相对路径
183
+ */
184
+ posix.relative = function (from, to) {
185
+ from = posix.resolve(from).substr(1);
186
+ to = posix.resolve(to).substr(1);
187
+ var fromParts = trimArray(from.split("/"));
188
+ var toParts = trimArray(to.split("/"));
189
+ var length = Math.min(fromParts.length, toParts.length);
190
+ var samePartsLength = length;
191
+ for (var i = 0; i < length; i++) {
192
+ if (fromParts[i] !== toParts[i]) {
193
+ samePartsLength = i;
194
+ break;
195
+ }
196
+ }
197
+ var outputParts = [];
198
+ for (var i = samePartsLength; i < fromParts.length; i++) {
199
+ outputParts.push("..");
200
+ }
201
+ outputParts = outputParts.concat(toParts.slice(samePartsLength));
202
+ return outputParts.join("/");
203
+ };
204
+ /**
205
+ * 获取路径的目录名
206
+ * @param {string} path - 输入的路径字符串
207
+ * @returns {string} - 目录名
208
+ */
209
+ posix.dirname = function (path) {
210
+ var result = posixSplitPath(path), root = result[0], dir = result[1];
211
+ if (!root && !dir) {
212
+ return ".";
213
+ }
214
+ if (dir) {
215
+ dir = dir.substr(0, dir.length - 1); // 去掉尾部斜杠
216
+ }
217
+ return root + dir;
218
+ };
219
+ /**
220
+ * 获取路径的基本名
221
+ * @param {string} path - 输入的路径字符串
222
+ * @param {string} [ext] - 可选的扩展名,如果提供则将其从基本名中去除
223
+ * @returns {string} - 基本名
224
+ */
225
+ posix.basename = function (path, ext) {
226
+ var f = posixSplitPath(path)[2];
227
+ if (ext && f.substr(-1 * ext.length) === ext) {
228
+ f = f.substr(0, f.length - ext.length);
229
+ }
230
+ return f;
231
+ };
232
+ /**
233
+ * 获取路径的扩展名
234
+ * @param {string} path - 输入的路径字符串
235
+ * @returns {string} - 扩展名
236
+ */
237
+ posix.extname = function (path) {
238
+ return posixSplitPath(path)[3];
239
+ };
240
+ /**
241
+ * 格式化路径对象为路径字符串
242
+ * @param {Object} pathObject - 路径对象
243
+ * @param {string} pathObject.root - 根路径
244
+ * @param {string} pathObject.dir - 目录路径
245
+ * @param {string} pathObject.base - 基本文件名
246
+ * @param {string} pathObject.ext - 扩展名
247
+ * @param {string} pathObject.name - 文件名(不包含扩展名)
248
+ * @returns {string} - 格式化后的路径字符串
249
+ */
250
+ posix.format = function (pathObject) {
251
+ if (!util.isObject(pathObject)) {
252
+ throw new TypeError("Parameter 'pathObject' must be an object, not " + typeof pathObject);
253
+ }
254
+ var root = pathObject.root || "";
255
+ if (!util.isString(root)) {
256
+ throw new TypeError("'pathObject.root' must be a string or undefined, not " +
257
+ typeof pathObject.root);
258
+ }
259
+ var dir = pathObject.dir ? pathObject.dir + posix.sep : "";
260
+ var base = pathObject.base || "";
261
+ return dir + base;
262
+ };
263
+ /**
264
+ * 解析路径字符串为路径对象
265
+ * @param {string} pathString - 输入的路径字符串
266
+ * @returns {{root: string, dir: string, base: string, ext: string, name: string}} - 路径对象
267
+ */
268
+ posix.parse = function (pathString) {
269
+ if (!util.isString(pathString)) {
270
+ throw new TypeError("Parameter 'pathString' must be a string, not " + typeof pathString);
271
+ }
272
+ var allParts = posixSplitPath(pathString);
273
+ if (!allParts || allParts.length !== 4) {
274
+ throw new TypeError("Invalid path '" + pathString + "'");
275
+ }
276
+ allParts[1] = allParts[1] || "";
277
+ allParts[2] = allParts[2] || "";
278
+ allParts[3] = allParts[3] || "";
279
+ return {
280
+ root: allParts[0],
281
+ dir: allParts[0] + allParts[1].slice(0, -1),
282
+ base: allParts[2],
283
+ ext: allParts[3],
284
+ name: allParts[2].slice(0, allParts[2].length - allParts[3].length),
285
+ };
286
+ };
287
+ posix.sep = "/"; // POSIX 路径分隔符
288
+ posix.delimiter = ":"; // POSIX 路径分隔符(用于环境变量)
289
+ export default posix; // 导出 posix 模块
package/license.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2024 CuberQAQ <a224503353@163.com>
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "hzengine-core",
3
+ "version": "0.1.2-dev",
4
+ "description": "An Visual Novel Engine for Zepp OS",
5
+ "main": "./dist/index.js",
6
+ "types": "./src/index.ts",
7
+ "type": "module",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "watch": "tsc --watch"
11
+ },
12
+ "keywords": [
13
+ "Galgame",
14
+ "HZEngine",
15
+ "NVL",
16
+ "ZeppOS"
17
+ ],
18
+ "author": "CuberQAQ",
19
+ "license": "Apache-2.0",
20
+ "devDependencies": {
21
+ "@zeppos/device-types": "^3.0.0"
22
+ },
23
+ "dependencies": {
24
+ "@cuberqaq/path-polyfill": "^1.0.2"
25
+ }
26
+ }
@@ -0,0 +1,124 @@
1
+ import { HZEngineCore, Storage } from "../index.js";
2
+ import { Save } from "../storage/decorator.js";
3
+ import { ZeppTimer } from "./zeppos_timer.js";
4
+ // import { Time } from "@zos/sensor";
5
+
6
+ export class Async {
7
+ static _nextTickCallbacks: (() => void)[] = [];
8
+ static _nextTickTimerId: number | null = null;
9
+ static nextTick(callback: () => void) {
10
+ this._nextTickCallbacks.push(callback);
11
+ if (this._nextTickTimerId == null) {
12
+ this._nextTickTimerId = setTimeout(() => {
13
+ this._nextTickTimerCallback();
14
+ }, 30) as unknown as number;
15
+ }
16
+ }
17
+
18
+ static _nextTickTimerCallback() {
19
+ this._nextTickTimerId = null;
20
+ const callbacks = this._nextTickCallbacks;
21
+ this._nextTickCallbacks = [];
22
+ callbacks.forEach((cb) => {
23
+ cb();
24
+ });
25
+ }
26
+
27
+ // private _fps = 45;
28
+ // private _hmTime = new Time();
29
+ private _lastTime: number;
30
+ constructor(public _core: HZEngineCore) {
31
+ // console.log("async init");
32
+ // let timer = new ZeppTimer(() => {
33
+ // // console.log(`async timer cb`);
34
+ // this._scheduleTask();
35
+ // }, ~~(1000 / this._fps));
36
+ // timer.start();
37
+ this._lastTime = this._core.platform.getTime();
38
+ // setInterval(() => {
39
+ // this._scheduleTask();
40
+ // }, ~~(1000 / this._fps));
41
+ _core.platform.setFrameInterval(() => {
42
+ this._scheduleTask();
43
+ });
44
+ }
45
+
46
+ _scheduleTask() {
47
+ // TODO 沒有充分考慮存檔時產生的問題(this._delayTasks引用變化),及其它問題
48
+ let now = this._core.platform.getTime();
49
+ let deltaTime = now - this._lastTime;
50
+ this._lastTime = now;
51
+
52
+ // console.log(`async timer cb d=${deltaTime}`);
53
+
54
+ for (let id in this._delayTasks) {
55
+ let task = this._delayTasks[id];
56
+ task.next -= deltaTime;
57
+ if (task.next <= 0) {
58
+ this._core.emit(task.event, ...task.args);
59
+ if (task.repeat >= 0) {
60
+ // interval
61
+ task.next += task.repeat;
62
+ } else {
63
+ delete this._delayTasks[id];
64
+ }
65
+ }
66
+ }
67
+ }
68
+
69
+ @Save("async.nextTimerId")
70
+ private accessor _nextTimerId: number = 50;
71
+
72
+ // tasks
73
+ @Save("async.delayTasks")
74
+ private accessor _delayTasks: Record<number, Async.Task> = {};
75
+
76
+ // 注意在task執行中立即創建一個新的task,會在同一個js task中執行
77
+ // 注意周期實際為1個frame的時間的整數倍
78
+ addDelayTask(
79
+ event: string,
80
+ args: Storage.Saveable<unknown>[],
81
+ delayMs: number
82
+ ): number {
83
+ let task: Async.Task = {
84
+ event,
85
+ args,
86
+ next: delayMs,
87
+ repeat: -1,
88
+ };
89
+ let id = this._nextTimerId++;
90
+ this._delayTasks[id] = task;
91
+ return id;
92
+ }
93
+
94
+ addRepeatTask(
95
+ event: string,
96
+ args: Storage.Saveable<unknown>[],
97
+ intervalMs: number,
98
+ delayMs?: number
99
+ ): number {
100
+ intervalMs = intervalMs >= 0 ? intervalMs : 0;
101
+ let task: Async.Task = {
102
+ event,
103
+ args,
104
+ next: delayMs ?? intervalMs,
105
+ repeat: intervalMs,
106
+ };
107
+ let id = this._nextTimerId++;
108
+ this._delayTasks[id] = task;
109
+ return id;
110
+ }
111
+
112
+ removeTask(id: number) {
113
+ delete this._delayTasks[id];
114
+ }
115
+ }
116
+
117
+ export namespace Async {
118
+ export interface Task {
119
+ event: string;
120
+ args: Storage.Saveable<unknown>[];
121
+ next: number; // remaining time in ms
122
+ repeat: number; // -1 for once ; >=0 for repeat circle
123
+ }
124
+ }