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,162 @@
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
+ // import { Time } from "@zos/sensor";
48
+ let Async = (() => {
49
+ var _a, _Async__nextTimerId_accessor_storage, _Async__delayTasks_accessor_storage;
50
+ let __nextTimerId_decorators;
51
+ let __nextTimerId_initializers = [];
52
+ let __nextTimerId_extraInitializers = [];
53
+ let __delayTasks_decorators;
54
+ let __delayTasks_initializers = [];
55
+ let __delayTasks_extraInitializers = [];
56
+ return _a = class Async {
57
+ static nextTick(callback) {
58
+ this._nextTickCallbacks.push(callback);
59
+ if (this._nextTickTimerId == null) {
60
+ this._nextTickTimerId = setTimeout(() => {
61
+ this._nextTickTimerCallback();
62
+ }, 30);
63
+ }
64
+ }
65
+ static _nextTickTimerCallback() {
66
+ this._nextTickTimerId = null;
67
+ const callbacks = this._nextTickCallbacks;
68
+ this._nextTickCallbacks = [];
69
+ callbacks.forEach((cb) => {
70
+ cb();
71
+ });
72
+ }
73
+ constructor(_core) {
74
+ this._core = _core;
75
+ _Async__nextTimerId_accessor_storage.set(this, __runInitializers(this, __nextTimerId_initializers, 50));
76
+ _Async__delayTasks_accessor_storage.set(this, (__runInitializers(this, __nextTimerId_extraInitializers), __runInitializers(this, __delayTasks_initializers, {})));
77
+ __runInitializers(this, __delayTasks_extraInitializers);
78
+ this._core = _core;
79
+ // console.log("async init");
80
+ // let timer = new ZeppTimer(() => {
81
+ // // console.log(`async timer cb`);
82
+ // this._scheduleTask();
83
+ // }, ~~(1000 / this._fps));
84
+ // timer.start();
85
+ this._lastTime = this._core.platform.getTime();
86
+ // setInterval(() => {
87
+ // this._scheduleTask();
88
+ // }, ~~(1000 / this._fps));
89
+ _core.platform.setFrameInterval(() => {
90
+ this._scheduleTask();
91
+ });
92
+ }
93
+ _scheduleTask() {
94
+ // TODO 沒有充分考慮存檔時產生的問題(this._delayTasks引用變化),及其它問題
95
+ let now = this._core.platform.getTime();
96
+ let deltaTime = now - this._lastTime;
97
+ this._lastTime = now;
98
+ // console.log(`async timer cb d=${deltaTime}`);
99
+ for (let id in this._delayTasks) {
100
+ let task = this._delayTasks[id];
101
+ task.next -= deltaTime;
102
+ if (task.next <= 0) {
103
+ this._core.emit(task.event, ...task.args);
104
+ if (task.repeat >= 0) {
105
+ // interval
106
+ task.next += task.repeat;
107
+ }
108
+ else {
109
+ delete this._delayTasks[id];
110
+ }
111
+ }
112
+ }
113
+ }
114
+ get _nextTimerId() { return __classPrivateFieldGet(this, _Async__nextTimerId_accessor_storage, "f"); }
115
+ set _nextTimerId(value) { __classPrivateFieldSet(this, _Async__nextTimerId_accessor_storage, value, "f"); }
116
+ // tasks
117
+ get _delayTasks() { return __classPrivateFieldGet(this, _Async__delayTasks_accessor_storage, "f"); }
118
+ set _delayTasks(value) { __classPrivateFieldSet(this, _Async__delayTasks_accessor_storage, value, "f"); }
119
+ // 注意在task執行中立即創建一個新的task,會在同一個js task中執行
120
+ // 注意周期實際為1個frame的時間的整數倍
121
+ addDelayTask(event, args, delayMs) {
122
+ let task = {
123
+ event,
124
+ args,
125
+ next: delayMs,
126
+ repeat: -1,
127
+ };
128
+ let id = this._nextTimerId++;
129
+ this._delayTasks[id] = task;
130
+ return id;
131
+ }
132
+ addRepeatTask(event, args, intervalMs, delayMs) {
133
+ intervalMs = intervalMs >= 0 ? intervalMs : 0;
134
+ let task = {
135
+ event,
136
+ args,
137
+ next: delayMs !== null && delayMs !== void 0 ? delayMs : intervalMs,
138
+ repeat: intervalMs,
139
+ };
140
+ let id = this._nextTimerId++;
141
+ this._delayTasks[id] = task;
142
+ return id;
143
+ }
144
+ removeTask(id) {
145
+ delete this._delayTasks[id];
146
+ }
147
+ },
148
+ _Async__nextTimerId_accessor_storage = new WeakMap(),
149
+ _Async__delayTasks_accessor_storage = new WeakMap(),
150
+ (() => {
151
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
152
+ __nextTimerId_decorators = [Save("async.nextTimerId")];
153
+ __delayTasks_decorators = [Save("async.delayTasks")];
154
+ __esDecorate(_a, null, __nextTimerId_decorators, { kind: "accessor", name: "_nextTimerId", static: false, private: false, access: { has: obj => "_nextTimerId" in obj, get: obj => obj._nextTimerId, set: (obj, value) => { obj._nextTimerId = value; } }, metadata: _metadata }, __nextTimerId_initializers, __nextTimerId_extraInitializers);
155
+ __esDecorate(_a, null, __delayTasks_decorators, { kind: "accessor", name: "_delayTasks", static: false, private: false, access: { has: obj => "_delayTasks" in obj, get: obj => obj._delayTasks, set: (obj, value) => { obj._delayTasks = value; } }, metadata: _metadata }, __delayTasks_initializers, __delayTasks_extraInitializers);
156
+ if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
157
+ })(),
158
+ _a._nextTickCallbacks = [],
159
+ _a._nextTickTimerId = null,
160
+ _a;
161
+ })();
162
+ export { Async };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * zeppos-timer.js
3
+ * @description An accurate timer for ZeppOS. 一个适用于ZeppOS的准确的计时器
4
+ * @version 1.0.0
5
+ * @date 2023/04/07
6
+ * @author XiaomaiTX
7
+ * @license MIT
8
+ * https://github.com/XiaomaiTX/zeppos-timer
9
+ *
10
+ * */
11
+ import { Time } from "@zos/sensor";
12
+ export class ZeppTimer {
13
+ constructor(callback, interval) {
14
+ this.callback = callback;
15
+ this.interval = interval;
16
+ this.timerId = null;
17
+ this.startTime = null;
18
+ this.nextTick = null;
19
+ this.time = new Time();
20
+ this.stopped = false;
21
+ }
22
+ start(delay = 0) {
23
+ this.startTime = this.time.getTime() + delay;
24
+ this.nextTick = this.startTime + this.interval;
25
+ this.scheduleTick();
26
+ }
27
+ stop() {
28
+ this.timerId && clearTimeout(this.timerId);
29
+ this.stopped = true;
30
+ }
31
+ scheduleTick() {
32
+ if (this.stopped)
33
+ return;
34
+ const currentTime = this.time.getTime();
35
+ const delay = Math.max(0, this.nextTick - currentTime);
36
+ this.timerId = setTimeout(() => {
37
+ this.timerId = null;
38
+ this.tick();
39
+ }, delay);
40
+ }
41
+ tick() {
42
+ const currentTime = this.time.getTime();
43
+ // 计算误差,确保计时器的准确性
44
+ const error = currentTime - this.nextTick;
45
+ if (error > this.interval) {
46
+ // 如果误差大于一个间隔时间,则将 nextTick 更新为当前时间
47
+ this.nextTick = currentTime;
48
+ }
49
+ else {
50
+ // 否则将 nextTick 加上一个间隔时间
51
+ this.nextTick += this.interval;
52
+ }
53
+ // 调用回调函数
54
+ this.callback();
55
+ // 继续调度下一个 tick
56
+ this.scheduleTick();
57
+ }
58
+ }
@@ -0,0 +1,260 @@
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
+ // / <reference path="./zos_media.d.ts" />
47
+ // import { create, id } from "@zos/media";
48
+ import { CustomSave } from "../storage/decorator.js";
49
+ let Audio = (() => {
50
+ var _a, _Audio__channels_accessor_storage;
51
+ let __channels_decorators;
52
+ let __channels_initializers = [];
53
+ let __channels_extraInitializers = [];
54
+ return _a = class Audio {
55
+ // static _hmPlayer = create(id.PLAYER);
56
+ constructor(_core) {
57
+ this._core = _core;
58
+ _Audio__channels_accessor_storage.set(this, __runInitializers(this, __channels_initializers, {}));
59
+ __runInitializers(this, __channels_extraInitializers);
60
+ this._core = _core;
61
+ this._channels["audio"] = new _a.Channel(this);
62
+ }
63
+ get _channels() { return __classPrivateFieldGet(this, _Audio__channels_accessor_storage, "f"); }
64
+ set _channels(value) { __classPrivateFieldSet(this, _Audio__channels_accessor_storage, value, "f"); }
65
+ get channels() {
66
+ return this._channels;
67
+ }
68
+ createChannel(name) {
69
+ // TODO support more audio channels
70
+ if (Object.getOwnPropertyNames(this._channels).length > 0)
71
+ throw "Can't create more than one audio channel on ZeppOS";
72
+ if (this._channels[name])
73
+ throw "Channel Already Exist";
74
+ return (this._channels[name] = new _a.Channel(this));
75
+ }
76
+ },
77
+ _Audio__channels_accessor_storage = new WeakMap(),
78
+ (() => {
79
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
80
+ __channels_decorators = [CustomSave("audio.channels", function (channels) {
81
+ let res = {};
82
+ for (let key in channels) {
83
+ res[key] = channels[key].serialize();
84
+ }
85
+ return res;
86
+ }, function (serialized) {
87
+ // clear exist channels
88
+ for (let key in this._channels) {
89
+ this._channels[key].stop();
90
+ this._channels[key].onRelease();
91
+ }
92
+ let res = {};
93
+ for (let key in serialized) {
94
+ res[key] = Audio.Channel.deserialize(this, serialized[key]);
95
+ }
96
+ return res;
97
+ })];
98
+ __esDecorate(_a, null, __channels_decorators, { kind: "accessor", name: "_channels", static: false, private: false, access: { has: obj => "_channels" in obj, get: obj => obj._channels, set: (obj, value) => { obj._channels = value; } }, metadata: _metadata }, __channels_initializers, __channels_extraInitializers);
99
+ if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
100
+ })(),
101
+ _a;
102
+ })();
103
+ export { Audio };
104
+ (function (Audio) {
105
+ class Channel {
106
+ constructor(_audio) {
107
+ this._audio = _audio;
108
+ this.mode = Channel.Mode.PlayInOrder;
109
+ this.status = Channel.Status.Stopped;
110
+ this._playbackList = [];
111
+ this._nowIndex = null;
112
+ this.currentInfo = null;
113
+ this._audioPlayer = _audio._core.platform.createAudioPlayer();
114
+ this._audioPlayer.onPrepared = (success) => {
115
+ this._onPrepared(success);
116
+ };
117
+ this._audioPlayer.onCompleted = () => {
118
+ this._onCompleted();
119
+ };
120
+ // Audio._hmPlayer.addEventListener(Audio._hmPlayer.event.PREPARE, (res) =>
121
+ // this._onPrepared(res)
122
+ // );
123
+ // Audio._hmPlayer.addEventListener(Audio._hmPlayer.event.COMPLETE, () => {
124
+ // this._onCompleted();
125
+ // });
126
+ }
127
+ onRelease() {
128
+ this._audioPlayer.release(); // TODO
129
+ this._audio._core.platform.releaseAudioPlayer(this._audioPlayer);
130
+ }
131
+ push(item) {
132
+ this._playbackList.push(item);
133
+ }
134
+ play() {
135
+ if (this.status !== Channel.Status.Stopped)
136
+ return;
137
+ this._playNext();
138
+ }
139
+ pause() {
140
+ if (this.status !== Channel.Status.Playing)
141
+ return;
142
+ this._audioPlayer.stop();
143
+ this.status = Channel.Status.Stopped;
144
+ }
145
+ /**
146
+ * 停止并清空队列
147
+ */
148
+ stop() {
149
+ if (this.status !== Channel.Status.Playing)
150
+ return;
151
+ this._audio._core.debug.log("audio channel stop");
152
+ this._audioPlayer.stop();
153
+ this._playbackList = [];
154
+ this.currentInfo = null;
155
+ this._nowIndex = null;
156
+ this.status = Channel.Status.Stopped;
157
+ }
158
+ serialize() {
159
+ return {
160
+ mode: this.mode,
161
+ status: this.status,
162
+ playbackList: this._playbackList,
163
+ nowIndex: this._nowIndex,
164
+ };
165
+ }
166
+ static deserialize(audio, data) {
167
+ let channel = new Channel(audio);
168
+ channel.mode = data.mode;
169
+ channel.status = data.status;
170
+ channel._playbackList = data.playbackList;
171
+ channel._nowIndex = data.nowIndex;
172
+ if (channel.status === Channel.Status.Playing)
173
+ channel._playNext();
174
+ return channel;
175
+ }
176
+ _onPrepared(result) {
177
+ var _a, _b;
178
+ if (result) {
179
+ let mediaInfo = this._audioPlayer.getMediaInfo();
180
+ this.currentInfo = {
181
+ // placeholder
182
+ artist: (_a = mediaInfo.artist) !== null && _a !== void 0 ? _a : "未知",
183
+ title: (_b = mediaInfo.title) !== null && _b !== void 0 ? _b : "未知",
184
+ duration: mediaInfo.duration,
185
+ };
186
+ console.log("=== prepare succeed ===");
187
+ this._audioPlayer.start();
188
+ }
189
+ else {
190
+ console.log("=== prepare fail ===");
191
+ this.status = Channel.Status.Error;
192
+ this._audioPlayer.release();
193
+ }
194
+ }
195
+ _onCompleted() {
196
+ // update playback list
197
+ if (this.mode === Channel.Mode.PlayInOrder) {
198
+ this._playbackList.shift();
199
+ if (this._playbackList.length > 0)
200
+ this._nowIndex = 0;
201
+ else
202
+ this._nowIndex = null;
203
+ }
204
+ else if (this.mode === Channel.Mode.PlayInOrderRepeat) {
205
+ if (this._playbackList.length > 0) {
206
+ if (this._nowIndex === null)
207
+ this._nowIndex = 0;
208
+ this._nowIndex = (this._nowIndex + 1) % this._playbackList.length;
209
+ }
210
+ else
211
+ this._nowIndex = null;
212
+ }
213
+ this._playNext();
214
+ }
215
+ /**
216
+ * Play the next item in the playback list. If there are no items in the
217
+ * playback list, set the channel status to Stopped and return.
218
+ * @private
219
+ */
220
+ _playNext() {
221
+ if (this._playbackList.length === 0) {
222
+ this._nowIndex = null;
223
+ this.status = Channel.Status.Stopped;
224
+ return;
225
+ }
226
+ if (this.mode === Channel.Mode.PlayInOrder) {
227
+ if (this._nowIndex === null)
228
+ this._nowIndex = 0;
229
+ // otherwise onCompleted will set _nowIndex to next index
230
+ }
231
+ else if (this.mode === Channel.Mode.PlayInOrderRepeat) {
232
+ if (this._nowIndex === null)
233
+ this._nowIndex = 0;
234
+ // otherwise onCompleted will set _nowIndex to next index
235
+ }
236
+ // Audio._hmPlayer.setSource(Audio._hmPlayer.source.FILE, {
237
+ // file: this._playbackList[this._nowIndex!].path,
238
+ // });
239
+ this._audioPlayer.setSource({
240
+ path: this._playbackList[this._nowIndex].path,
241
+ });
242
+ this._audioPlayer.prepare();
243
+ this.status = Channel.Status.Playing;
244
+ }
245
+ }
246
+ Audio.Channel = Channel;
247
+ (function (Channel) {
248
+ let Mode;
249
+ (function (Mode) {
250
+ Mode[Mode["PlayInOrder"] = 0] = "PlayInOrder";
251
+ Mode[Mode["PlayInOrderRepeat"] = 1] = "PlayInOrderRepeat";
252
+ })(Mode = Channel.Mode || (Channel.Mode = {}));
253
+ let Status;
254
+ (function (Status) {
255
+ Status[Status["Stopped"] = 0] = "Stopped";
256
+ Status[Status["Playing"] = 1] = "Playing";
257
+ Status[Status["Error"] = 2] = "Error";
258
+ })(Status = Channel.Status || (Channel.Status = {}));
259
+ })(Channel = Audio.Channel || (Audio.Channel = {}));
260
+ })(Audio || (Audio = {}));
@@ -0,0 +1,57 @@
1
+ export class Config {
2
+ constructor(_core) {
3
+ this._core = _core;
4
+ _core.on("initGlobalData", () => {
5
+ _core.storage.setSaveableData(_core.storage.globalData, true, Object.assign({}, defaultConfig), "config");
6
+ });
7
+ _core.on("afterLoadGlobalData", () => {
8
+ var _a;
9
+ if (!((_a = _core.storage.globalData) === null || _a === void 0 ? void 0 : _a.config)) {
10
+ _core.storage.setSaveableData(_core.storage.globalData, true, Object.assign({}, defaultConfig), "config");
11
+ }
12
+ _core.storage.saveGlobalData();
13
+ });
14
+ }
15
+ getConfig(key) {
16
+ let keys = this.normalizeKey(key);
17
+ // this._core.debug.log(`getConfig keys = ${JSON.stringify(keys)}`);
18
+ if (keys == null)
19
+ return;
20
+ try {
21
+ let res = this._core.storage.getSaveableData(this._core.storage.globalData, false, "config", ...keys);
22
+ // this._core.debug.log(`getConfig res = ${JSON.stringify(res)}`);
23
+ return res;
24
+ }
25
+ catch (e) {
26
+ // this._core.debug.log(`getConfig error`,e);
27
+ return;
28
+ }
29
+ }
30
+ setConfig(key, value) {
31
+ let keys = this.normalizeKey(key);
32
+ if (keys == null)
33
+ return;
34
+ this._core.storage.setSaveableData(this._core.storage.globalData, true, value, "config", ...keys);
35
+ this._core.storage.saveGlobalData();
36
+ }
37
+ normalizeKey(key) {
38
+ let keys = [];
39
+ key.split(".").forEach((key) => {
40
+ if (key.trim().length > 0)
41
+ keys.push(key);
42
+ else
43
+ return null;
44
+ });
45
+ return keys;
46
+ }
47
+ resetConfig() { }
48
+ }
49
+ const defaultConfig = {
50
+ game: {
51
+ autoplay: {
52
+ enable: false,
53
+ ms_per_char: 150,
54
+ extra_delay: 1300,
55
+ },
56
+ },
57
+ };
@@ -0,0 +1,8 @@
1
+ export class Debug {
2
+ constructor(_core) {
3
+ this._core = _core;
4
+ }
5
+ log(...args) {
6
+ console.log("[HZEngine]", ...args);
7
+ }
8
+ }
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ /**
2
+ * HZEngineCore
3
+ * @copyright Copyright (c) 2024 CuberQAQ. All rights reserved.
4
+ */
5
+ import { Async } from "./async/index.js";
6
+ import { Audio } from "./audio/index.js";
7
+ import { Config } from "./config/index.js";
8
+ import { Debug } from "./debug/index.js";
9
+ import { basic_command } from "./plugins/basic_command/index.js";
10
+ import { global_gesture } from "./plugins/global_gesture/index.js";
11
+ import { registerPlugin } from "./plugins/transform/index.js";
12
+ import { Script } from "./script/index.js";
13
+ import { Storage } from "./storage/index.js";
14
+ import { System } from "./system/index.js";
15
+ import { UI } from "./ui/index.js";
16
+ class HZEngineCore {
17
+ constructor(platform) {
18
+ this.platform = platform;
19
+ this._eventCallbacks = new Map();
20
+ this.plugins = new Map();
21
+ // 請不要調整這裡的初始化順序,不然會有問題(裝飾器裡有時候要用到前面初始化的東西)
22
+ this.storage = new Storage(this);
23
+ this.async = new Async(this);
24
+ this.ui = new UI(this);
25
+ this.script = new Script(this);
26
+ this.system = new System(this);
27
+ this.config = new Config(this);
28
+ this.audio = new Audio(this);
29
+ this.debug = new Debug(this);
30
+ // internal plugin
31
+ this.loadPlugin("global_gesture", global_gesture);
32
+ this.loadPlugin("transform", registerPlugin);
33
+ this.loadPlugin("basic_command", basic_command);
34
+ }
35
+ loadProject(options) {
36
+ this.storage.loadProject(options);
37
+ }
38
+ start(callback) {
39
+ // this.system.start()
40
+ Async.nextTick(() => {
41
+ var _a;
42
+ this.debug.log("[HZEngine] Game Start");
43
+ let title = (_a = this.storage.packageData) === null || _a === void 0 ? void 0 : _a.name;
44
+ if (title == null) {
45
+ throw `[HZEngine] project name is null, please loadProject first or check your project.json format`;
46
+ }
47
+ this.ui.getRouter("page").push("title", {
48
+ title,
49
+ });
50
+ // this.on("gameEnd", () => {
51
+ // let router = this.ui.getRouter("page")!;
52
+ // if (router.length > 0) return;
53
+ // router.push("title", {
54
+ // title,
55
+ // });
56
+ // });
57
+ callback === null || callback === void 0 ? void 0 : callback();
58
+ });
59
+ }
60
+ end() {
61
+ var _a;
62
+ this.debug.log("[HZEngine] Game End, return to title");
63
+ let title = (_a = this.storage.packageData) === null || _a === void 0 ? void 0 : _a.name;
64
+ if (title == null) {
65
+ throw `[HZEngine] project name is null, please loadProject first or check your project.json format`;
66
+ }
67
+ this.system.condition = System.Condition.Free;
68
+ this.ui.resetUI();
69
+ if (this.ui.getRouter("page").length > 0)
70
+ return;
71
+ this.ui.getRouter("page").push("title", {
72
+ title,
73
+ });
74
+ }
75
+ // Load Plugin
76
+ loadPlugin(name, plugin) {
77
+ this.debug.log(`[HZEngine] load plugin [${name}]`);
78
+ let slot = plugin(this);
79
+ if (slot != undefined)
80
+ this.plugins.set(name, slot);
81
+ }
82
+ // Event Bus
83
+ on(event, cb) {
84
+ if (this._eventCallbacks.has(event)) {
85
+ this._eventCallbacks.get(event).add(cb);
86
+ }
87
+ else {
88
+ this._eventCallbacks.set(event, new Set().add(cb));
89
+ }
90
+ }
91
+ off(event, cb) {
92
+ var _a;
93
+ return !!((_a = this._eventCallbacks.get(event)) === null || _a === void 0 ? void 0 : _a.delete(cb));
94
+ }
95
+ emit(event, ...args) {
96
+ var _a;
97
+ (_a = this._eventCallbacks.get(event)) === null || _a === void 0 ? void 0 : _a.forEach((cb) => {
98
+ cb(...args);
99
+ });
100
+ }
101
+ }
102
+ export { HZEngineCore, UI, Storage, Script, System, Async };
103
+ export * as TransformPlugin from "./plugins/transform/index.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ export function $_command(core) {
2
+ core.script.use((ctx, next) => {
3
+ if (ctx.rawtext.trim().startsWith("$")) {
4
+ ctx.rawtext = "eval " + ctx.rawtext.trim().slice(1).trim();
5
+ }
6
+ return next();
7
+ });
8
+ }