@craft_eco/craft-mcp 0.1.34 → 0.1.35-beta.2

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 (113) hide show
  1. package/README.md +2 -2
  2. package/bin/craft-mcp.mjs +11 -2
  3. package/dist-server/core/http-utils.mjs +11 -0
  4. package/dist-server/game-workflow/asset-formats.mjs +1 -1
  5. package/dist-server/game-workflow/bundle-files.mjs +19 -0
  6. package/dist-server/game-workflow/bundle-template.mjs +5 -0
  7. package/dist-server/game-workflow/context-state.mjs +1 -1
  8. package/dist-server/game-workflow/draft-archive.mjs +61 -0
  9. package/dist-server/game-workflow/draft-binding-migration.mjs +73 -0
  10. package/dist-server/game-workflow/draft-uploads.mjs +258 -75
  11. package/dist-server/game-workflow/native-context.mjs +4 -1
  12. package/dist-server/game-workflow/project-bundles.mjs +17 -6
  13. package/dist-server/game-workflow/projects.mjs +4 -0
  14. package/dist-server/game-workflow/template-refresh.mjs +430 -0
  15. package/dist-server/integrations/distribution-targets.mjs +2 -0
  16. package/dist-server/integrations/files.mjs +30 -2
  17. package/dist-server/integrations/fixed-launcher.mjs +4 -4
  18. package/dist-server/integrations/install.mjs +5 -5
  19. package/dist-server/integrations/installation-state.mjs +6 -7
  20. package/dist-server/integrations/launcher-start.mjs +12 -8
  21. package/dist-server/integrations/opencode-config.mjs +1 -1
  22. package/dist-server/integrations/project-setup.mjs +0 -1
  23. package/dist-server/integrations/runtime-candidate.mjs +8 -12
  24. package/dist-server/integrations/runtime-gc.mjs +20 -21
  25. package/dist-server/integrations/runtime.mjs +7 -3
  26. package/dist-server/integrations/updates.mjs +64 -27
  27. package/dist-server/launcher.mjs +39 -14
  28. package/dist-server/mcp/login-page.mjs +2 -2
  29. package/dist-server/mcp/server.mjs +10 -12
  30. package/dist-server/mcp/update-session.mjs +3 -3
  31. package/dist-server/upstream/{craft-aigc.mjs → craft-platform.mjs} +40 -115
  32. package/dist-server/upstream/draft-audit.mjs +51 -0
  33. package/dist-server/upstream/draft-transport.mjs +184 -0
  34. package/package.json +3 -3
  35. package/resources/craft-update/SKILL.md +2 -0
  36. package/resources/targets/claude/js/js/bundle.json +15 -9
  37. package/resources/targets/claude/js/target.json +8 -6
  38. package/resources/targets/claude/js3d/js3d/bundle.json +21 -10
  39. package/resources/targets/claude/js3d/target.json +14 -7
  40. package/resources/targets/codebuddy/js/js/bundle.json +15 -9
  41. package/resources/targets/codebuddy/js/target.json +8 -6
  42. package/resources/targets/codebuddy/js3d/js3d/bundle.json +21 -10
  43. package/resources/targets/codebuddy/js3d/target.json +14 -7
  44. package/resources/targets/codex/js/js/bundle.json +15 -9
  45. package/resources/targets/codex/js/target.json +8 -6
  46. package/resources/targets/codex/js3d/js3d/bundle.json +21 -10
  47. package/resources/targets/codex/js3d/target.json +14 -7
  48. package/resources/targets/comate/js/js/bundle.json +15 -9
  49. package/resources/targets/comate/js/target.json +8 -6
  50. package/resources/targets/comate/js3d/js3d/bundle.json +21 -10
  51. package/resources/targets/comate/js3d/target.json +14 -7
  52. package/resources/targets/cursor/js/js/bundle.json +15 -9
  53. package/resources/targets/cursor/js/target.json +8 -6
  54. package/resources/targets/cursor/js3d/js3d/bundle.json +21 -10
  55. package/resources/targets/cursor/js3d/target.json +14 -7
  56. package/resources/targets/dsh/js/js/bundle.json +15 -9
  57. package/resources/targets/dsh/js/target.json +8 -6
  58. package/resources/targets/dsh/js3d/js3d/bundle.json +21 -10
  59. package/resources/targets/dsh/js3d/target.json +14 -7
  60. package/resources/targets/index.json +56 -56
  61. package/resources/targets/kimi/js/js/bundle.json +15 -9
  62. package/resources/targets/kimi/js/target.json +8 -6
  63. package/resources/targets/kimi/js3d/js3d/bundle.json +21 -10
  64. package/resources/targets/kimi/js3d/target.json +14 -7
  65. package/resources/targets/opencode/js/js/bundle.json +15 -9
  66. package/resources/targets/opencode/js/target.json +8 -6
  67. package/resources/targets/opencode/js3d/js3d/bundle.json +21 -10
  68. package/resources/targets/opencode/js3d/target.json +14 -7
  69. package/resources/targets/qoder/js/js/bundle.json +15 -9
  70. package/resources/targets/qoder/js/target.json +8 -6
  71. package/resources/targets/qoder/js3d/js3d/bundle.json +21 -10
  72. package/resources/targets/qoder/js3d/target.json +14 -7
  73. package/resources/targets/qoder-cn/js/js/bundle.json +15 -9
  74. package/resources/targets/qoder-cn/js/target.json +8 -6
  75. package/resources/targets/qoder-cn/js3d/js3d/bundle.json +21 -10
  76. package/resources/targets/qoder-cn/js3d/target.json +14 -7
  77. package/resources/targets/qwen/js/js/bundle.json +15 -9
  78. package/resources/targets/qwen/js/target.json +8 -6
  79. package/resources/targets/qwen/js3d/js3d/bundle.json +21 -10
  80. package/resources/targets/qwen/js3d/target.json +14 -7
  81. package/resources/targets/trae/js/js/bundle.json +15 -9
  82. package/resources/targets/trae/js/target.json +8 -6
  83. package/resources/targets/trae/js3d/js3d/bundle.json +21 -10
  84. package/resources/targets/trae/js3d/target.json +14 -7
  85. package/resources/targets/workbuddy/js/js/bundle.json +15 -9
  86. package/resources/targets/workbuddy/js/target.json +8 -6
  87. package/resources/targets/workbuddy/js3d/js3d/bundle.json +21 -10
  88. package/resources/targets/workbuddy/js3d/target.json +14 -7
  89. package/resources/targets/zcode/js/js/bundle.json +15 -9
  90. package/resources/targets/zcode/js/target.json +8 -6
  91. package/resources/targets/zcode/js3d/js3d/bundle.json +21 -10
  92. package/resources/targets/zcode/js3d/target.json +14 -7
  93. package/resources/templates/js/materialize_paths.json +9 -0
  94. package/resources/templates/js/project/package.json +1 -0
  95. package/resources/templates/js/project/runtime/lightgame/1.1.0/platform/platform-capture-dom.mjs +176 -0
  96. package/resources/templates/js/project/runtime/lightgame/1.1.0/platform/platform.d.mts +5 -1
  97. package/resources/templates/js/project/runtime/lightgame/1.1.0/platform/platform.mjs +87 -3
  98. package/resources/templates/js/project/runtime/lightgame/1.1.0/release.json +3 -2
  99. package/resources/templates/js/project/scripts/refresh-runtime.mjs +50 -0
  100. package/resources/templates/js3d/materialize_paths.json +9 -0
  101. package/resources/templates/js3d/project/package.json +4 -2
  102. package/resources/templates/js3d/project/runtime/lightgame/1.1.0/platform/platform-capture-dom.mjs +176 -0
  103. package/resources/templates/js3d/project/runtime/lightgame/1.1.0/platform/platform.d.mts +5 -1
  104. package/resources/templates/js3d/project/runtime/lightgame/1.1.0/platform/platform.mjs +87 -3
  105. package/resources/templates/js3d/project/runtime/lightgame/1.1.0/release.json +3 -2
  106. package/resources/templates/js3d/project/scripts/refresh-runtime.mjs +50 -0
  107. package/resources/templates/js3d/project/scripts/skill-modules/gameplay-check/check.mjs +331 -0
  108. package/resources/templates/js3d/project/src/game/capability/core.ts +3 -0
  109. package/resources/templates/js3d/project/src/main.ts +23 -6
  110. package/resources/templates/js3d/project/src/skill-modules/gameplay/core/index.d.mts +92 -0
  111. package/resources/templates/js3d/project/src/skill-modules/gameplay/core/index.mjs +375 -0
  112. package/resources/templates/js3d/project/src/skill-modules/gameplay/installed.json +6 -0
  113. package/dist-server/game-workflow/local-context.mjs +0 -64
@@ -0,0 +1,375 @@
1
+ /**
2
+ * Gameplay 内核:唯一主循环、实体注册、定时调度、事件通道、帧内事件去重。
3
+ *
4
+ * 约束(见 references/core.md):
5
+ * - 全工程只允许一个运行中的 Kernel,第二个 start() 直接抛错。业务另写 requestAnimationFrame 不会抛错,由 check.mjs 判失败。
6
+ * - 模拟走固定步长;渲染只读 alpha 做插值,不写状态。
7
+ * - 输入层的采样与边沿清理挂在 onBeforeStep / onAfterStep 上,按固定步而不是按渲染帧。
8
+ * - 实体只在 World 里登记,切场景时 clear() 逐个 dispose,计数归零。
9
+ * - 定时器按模拟时间推进,暂停时冻结,clear() 时清空。业务代码不用 setTimeout / setInterval。
10
+ */
11
+
12
+ const DEFAULT_STEP_SECONDS = 1 / 60;
13
+ const DEFAULT_MAX_SUB_STEPS = 5;
14
+ export const FRAME_EVENT = 'frame-event';
15
+
16
+ function positive(value, name) {
17
+ if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
18
+ throw new TypeError(`Kernel: ${name} 必须是正数,收到 ${value}`);
19
+ }
20
+ return value;
21
+ }
22
+
23
+ export class Kernel {
24
+ static #active = null;
25
+
26
+ #stepSeconds;
27
+ #maxSubSteps;
28
+ #maxFrameSeconds;
29
+ #entities = new Set();
30
+ #pendingAdd = new Set();
31
+ #pendingRemove = new Set();
32
+ #timers = new Map();
33
+ #nextTimerId = 1;
34
+ #listeners = new Map();
35
+ #frameEvents = new Map();
36
+ #renderListeners = [];
37
+ #beforeStep = [];
38
+ #afterStep = [];
39
+ #accumulator = 0;
40
+ #alpha = 0;
41
+ #frameSeconds = 0;
42
+ #simulated = 0;
43
+ #steps = 0;
44
+ #timeScale = 1;
45
+ #paused = false;
46
+ #loop = null;
47
+ #rafId = null;
48
+ #lastTime = 0;
49
+ #disposed = false;
50
+
51
+ constructor(options = {}) {
52
+ const step = options.stepSeconds ?? DEFAULT_STEP_SECONDS;
53
+ const maxSubSteps = options.maxSubSteps ?? DEFAULT_MAX_SUB_STEPS;
54
+ if (!Number.isSafeInteger(maxSubSteps) || maxSubSteps <= 0) {
55
+ throw new TypeError(`Kernel: maxSubSteps 必须是正整数,收到 ${maxSubSteps}`);
56
+ }
57
+ this.#stepSeconds = positive(step, 'stepSeconds');
58
+ this.#maxSubSteps = maxSubSteps;
59
+ this.#maxFrameSeconds = options.maxFrameSeconds ?? this.#stepSeconds * maxSubSteps;
60
+ positive(this.#maxFrameSeconds, 'maxFrameSeconds');
61
+ }
62
+
63
+ get stepSeconds() { return this.#stepSeconds; }
64
+ /** 世界里的实体数量。clear() 之后必须为 0。 */
65
+ get size() { return this.#entities.size; }
66
+ /** 待触发的定时器数量。clear() 之后必须为 0。 */
67
+ get pending() { return this.#timers.size; }
68
+ get paused() { return this.#paused; }
69
+ get timeScale() { return this.#timeScale; }
70
+ get time() {
71
+ return { frameSeconds: this.#frameSeconds, simulated: this.#simulated, steps: this.#steps, alpha: this.#alpha };
72
+ }
73
+
74
+ /**
75
+ * 运行状态快照,只含可序列化的数字与布尔,供自动验收读取(挂到页面探针里即可)。
76
+ * 重开、换关卡之后 entities / timers / listeners 不应比上一次更多。
77
+ */
78
+ probe() {
79
+ let listeners = 0;
80
+ for (const list of this.#listeners.values()) listeners += list.length;
81
+ return {
82
+ running: this.#loop !== null,
83
+ paused: this.#paused,
84
+ timeScale: this.#timeScale,
85
+ entities: this.#entities.size,
86
+ timers: this.#timers.size,
87
+ listeners,
88
+ renderHooks: this.#renderListeners.length,
89
+ stepHooks: this.#beforeStep.length + this.#afterStep.length,
90
+ steps: this.#steps,
91
+ simulatedMs: Math.round(this.#simulated * 1000),
92
+ };
93
+ }
94
+
95
+ // ---- 世界 ----
96
+
97
+ /** 登记实体。实体只需按需实现 update(dt) / dispose()。重复登记同一个对象会被忽略。 */
98
+ add(entity) {
99
+ if (!entity || typeof entity !== 'object') throw new TypeError('Kernel.add: 实体必须是对象');
100
+ if (this.#entities.has(entity) || this.#pendingAdd.has(entity)) return entity;
101
+ if (this.#loop) this.#pendingAdd.add(entity);
102
+ else this.#entities.add(entity);
103
+ return entity;
104
+ }
105
+
106
+ /** 取消尚未生效的登记并立即 dispose;已在世界里的实体在安全的步进边界移除。 */
107
+ remove(entity) {
108
+ if (this.#pendingAdd.delete(entity)) {
109
+ this.#disposeEntity(entity);
110
+ return true;
111
+ }
112
+ if (!this.#entities.has(entity)) return false;
113
+ if (this.#loop) this.#pendingRemove.add(entity);
114
+ else this.#dropEntity(entity);
115
+ return true;
116
+ }
117
+
118
+ /** 清空世界:逐个 dispose,清掉所有定时器与未派发的帧内事件。切场景时调用。 */
119
+ clear() {
120
+ const entities = [...this.#entities, ...this.#pendingAdd];
121
+ this.#entities.clear();
122
+ this.#pendingAdd.clear();
123
+ this.#pendingRemove.clear();
124
+ this.#timers.clear();
125
+ this.#frameEvents.clear();
126
+ this.#accumulator = 0;
127
+ for (const entity of entities) this.#disposeEntity(entity);
128
+ }
129
+
130
+ // ---- 定时 ----
131
+
132
+ /** 模拟时间过 seconds 后执行一次。返回句柄,cancel() 取消。 */
133
+ after(seconds, callback) {
134
+ return this.#createTimer(seconds, null, callback, 'after');
135
+ }
136
+
137
+ /** 每隔 seconds 执行一次,按模拟时间推进。 */
138
+ every(seconds, callback) {
139
+ return this.#createTimer(seconds, seconds, callback, 'every');
140
+ }
141
+
142
+ // ---- 事件 ----
143
+
144
+ /** 订阅事件,返回取消订阅函数。 */
145
+ on(name, listener) {
146
+ if (typeof listener !== 'function') throw new TypeError('Kernel.on: listener 必须是函数');
147
+ const list = this.#listeners.get(name) ?? [];
148
+ list.push(listener);
149
+ this.#listeners.set(name, list);
150
+ return () => this.off(name, listener);
151
+ }
152
+
153
+ off(name, listener) {
154
+ const list = this.#listeners.get(name);
155
+ if (!list) return;
156
+ const index = list.indexOf(listener);
157
+ if (index >= 0) list.splice(index, 1);
158
+ if (!list.length) this.#listeners.delete(name);
159
+ }
160
+
161
+ emit(name, payload) {
162
+ const list = this.#listeners.get(name);
163
+ if (!list) return;
164
+ for (const listener of [...list]) listener(payload);
165
+ }
166
+
167
+ /**
168
+ * 推迟到本次渲染的固定步全部跑完后派发一次。
169
+ * 同一个 key 在一帧的多个子步里被触发多次,订阅方只收到最后一次。
170
+ * 动画的脚步、命中这类按帧表现的事件用它,避免低帧率时重复触发。
171
+ */
172
+ emitOnce(name, payload, key = name) {
173
+ this.#frameEvents.set(key, { name, payload });
174
+ }
175
+
176
+ // ---- 循环 ----
177
+
178
+ /** 渲染回调:cb(alpha, frameSeconds, simulatedSeconds),第三个参数是本帧实际推进的模拟秒数。 */
179
+ onRender(callback) {
180
+ if (typeof callback !== 'function') throw new TypeError('Kernel.onRender: callback 必须是函数');
181
+ this.#renderListeners.push(callback);
182
+ return () => {
183
+ const index = this.#renderListeners.indexOf(callback);
184
+ if (index >= 0) this.#renderListeners.splice(index, 1);
185
+ };
186
+ }
187
+
188
+ /**
189
+ * 每个固定步开始前调用 cb(dt),早于所有实体的 update。
190
+ * 输入层的 sample() 挂这里:一帧跑 0 步时边沿不丢,跑多步时只在第一步看到按下。
191
+ * 不随 clear() 清掉,属于接线而不是世界内容。返回取消函数。
192
+ */
193
+ onBeforeStep(callback) {
194
+ return this.#hook(this.#beforeStep, callback, 'onBeforeStep');
195
+ }
196
+
197
+ /** 每个固定步结束后调用 cb(dt),晚于所有实体与定时器。输入层的 endFrame() 挂这里。 */
198
+ onAfterStep(callback) {
199
+ return this.#hook(this.#afterStep, callback, 'onAfterStep');
200
+ }
201
+
202
+ start() {
203
+ if (this.#disposed) throw new Error('Kernel: 已销毁的实例不能重新启动');
204
+ if (this.#loop) return;
205
+ if (Kernel.#active && Kernel.#active !== this) {
206
+ throw new Error('Kernel: 已有运行中的实例。全工程只允许一个主循环,不要在项目里再起第二个');
207
+ }
208
+ this.#lastTime = now();
209
+ const tick = () => {
210
+ if (this.#loop !== tick) return;
211
+ this.#rafId = null;
212
+ try {
213
+ const current = now();
214
+ const elapsed = (current - this.#lastTime) / 1000;
215
+ this.#lastTime = current;
216
+ this.advance(elapsed);
217
+ if (this.#loop === tick) this.#rafId = requestAnimationFrame(tick);
218
+ } catch (error) {
219
+ if (this.#loop === tick) this.stop();
220
+ throw error;
221
+ }
222
+ };
223
+ this.#rafId = requestAnimationFrame(tick);
224
+ this.#loop = tick;
225
+ Kernel.#active = this;
226
+ return this;
227
+ }
228
+
229
+ stop() {
230
+ this.#loop = null;
231
+ if (this.#rafId !== null) cancelAnimationFrame(this.#rafId);
232
+ this.#rafId = null;
233
+ if (Kernel.#active === this) Kernel.#active = null;
234
+ }
235
+
236
+ pause() {
237
+ this.#paused = true;
238
+ this.#accumulator = 0;
239
+ }
240
+
241
+ /** 恢复后不会补算暂停期间的时间:暂停 5 秒只丢掉 5 秒,不产生位移跳变。 */
242
+ resume() {
243
+ if (!this.#paused) return;
244
+ this.#paused = false;
245
+ this.#accumulator = 0;
246
+ this.#lastTime = now();
247
+ }
248
+
249
+ /** 时间缩放(打击停顿、慢动作)。属于内核,不下放给各能力。 */
250
+ setTimeScale(value) {
251
+ this.#timeScale = positive(value, 'timeScale');
252
+ }
253
+
254
+ /**
255
+ * 推进一帧。start() 内部由 requestAnimationFrame 调用;
256
+ * 单元测试和自定义驱动器可以手动调用,业务代码不要调用。
257
+ */
258
+ advance(frameSeconds) {
259
+ if (this.#disposed) return;
260
+ const raw = Math.min(Math.max(Number(frameSeconds) || 0, 0), this.#maxFrameSeconds);
261
+ this.#frameSeconds = raw;
262
+ let simulatedSeconds = 0;
263
+ if (!this.#paused) {
264
+ this.#accumulator += raw;
265
+ let stepped = 0;
266
+ while (this.#accumulator >= this.#stepSeconds && stepped < this.#maxSubSteps) {
267
+ const dt = this.#stepSeconds * this.#timeScale;
268
+ this.#simulated += dt;
269
+ simulatedSeconds += dt;
270
+ this.#steps += 1;
271
+ this.#accumulator -= this.#stepSeconds;
272
+ stepped += 1;
273
+ for (const callback of [...this.#beforeStep]) callback(dt);
274
+ this.#updateEntities(dt);
275
+ this.#runTimers(dt);
276
+ for (const callback of [...this.#afterStep]) callback(dt);
277
+ }
278
+ // 触到子步上限说明这一帧已经跑不动了,丢掉剩余债务,避免越积越多导致后续卡死
279
+ if (stepped === this.#maxSubSteps) this.#accumulator = 0;
280
+ this.#alpha = this.#accumulator / this.#stepSeconds;
281
+ }
282
+ this.#flushFrameEvents();
283
+ for (const callback of [...this.#renderListeners]) callback(this.#alpha, raw, simulatedSeconds);
284
+ }
285
+
286
+ dispose() {
287
+ if (this.#disposed) return;
288
+ this.stop();
289
+ this.clear();
290
+ this.#listeners.clear();
291
+ this.#renderListeners.length = 0;
292
+ this.#beforeStep.length = 0;
293
+ this.#afterStep.length = 0;
294
+ this.#disposed = true;
295
+ }
296
+
297
+ // ---- 内部 ----
298
+
299
+ #hook(list, callback, label) {
300
+ if (typeof callback !== 'function') throw new TypeError(`Kernel.${label}: callback 必须是函数`);
301
+ list.push(callback);
302
+ return () => {
303
+ const index = list.indexOf(callback);
304
+ if (index >= 0) list.splice(index, 1);
305
+ };
306
+ }
307
+
308
+ #updateEntities(dt) {
309
+ this.#flushWorldChanges();
310
+ const snapshot = [...this.#entities];
311
+ for (const entity of snapshot) {
312
+ if (!this.#entities.has(entity)) continue;
313
+ if (typeof entity.update === 'function') entity.update(dt);
314
+ }
315
+ this.#flushWorldChanges();
316
+ }
317
+
318
+ #flushWorldChanges() {
319
+ if (this.#pendingAdd.size) {
320
+ for (const entity of this.#pendingAdd) this.#entities.add(entity);
321
+ this.#pendingAdd.clear();
322
+ }
323
+ if (this.#pendingRemove.size) {
324
+ for (const entity of this.#pendingRemove) this.#dropEntity(entity);
325
+ this.#pendingRemove.clear();
326
+ }
327
+ }
328
+
329
+ #dropEntity(entity) {
330
+ if (this.#entities.delete(entity)) this.#disposeEntity(entity);
331
+ }
332
+
333
+ #disposeEntity(entity) {
334
+ if (typeof entity.dispose === 'function') entity.dispose();
335
+ }
336
+
337
+ #createTimer(seconds, interval, callback, label) {
338
+ positive(seconds, `${label} 的间隔`);
339
+ if (typeof callback !== 'function') throw new TypeError(`Kernel.${label}: callback 必须是函数`);
340
+ const id = this.#nextTimerId++;
341
+ const timer = { id, remaining: seconds, interval, callback };
342
+ this.#timers.set(id, timer);
343
+ return { get id() { return id; }, cancel: () => { this.#timers.delete(id); } };
344
+ }
345
+
346
+ #runTimers(dt) {
347
+ if (!this.#timers.size) return;
348
+ for (const timer of [...this.#timers.values()]) {
349
+ if (!this.#timers.has(timer.id)) continue;
350
+ timer.remaining -= dt;
351
+ let guard = 0;
352
+ while (timer.remaining <= 0 && this.#timers.has(timer.id)) {
353
+ timer.callback();
354
+ if (timer.interval === null) {
355
+ this.#timers.delete(timer.id);
356
+ break;
357
+ }
358
+ timer.remaining += timer.interval;
359
+ // interval 远小于步长时一次子步内会连续触发多次,限制在同一步内最多 8 次,防止卡死
360
+ if (++guard >= 8) { timer.remaining = timer.interval; break; }
361
+ }
362
+ }
363
+ }
364
+
365
+ #flushFrameEvents() {
366
+ if (!this.#frameEvents.size) return;
367
+ const events = [...this.#frameEvents.values()];
368
+ this.#frameEvents.clear();
369
+ for (const event of events) this.emit(event.name, event.payload);
370
+ }
371
+ }
372
+
373
+ function now() {
374
+ return typeof performance !== 'undefined' && typeof performance.now === 'function' ? performance.now() : Date.now();
375
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": "0.1.0",
3
+ "modules": [
4
+ "core"
5
+ ]
6
+ }
@@ -1,64 +0,0 @@
1
- import path from 'node:path';
2
- import { mkdir, mkdtemp, readFile, readdir, writeFile, lstat, rename, rm } from 'node:fs/promises';
3
- import { randomUUID } from 'node:crypto';
4
- import { safeProjectFile } from './asset-import.mjs';
5
- import { verifyBundle } from './bundle-files.mjs';
6
- import { loadEngineResource } from './engines.mjs';
7
- import { copyBundleArtPreviews } from './art-previews.mjs';
8
-
9
- export const localContextPath = (host,engine,id)=>`.craft/contexts/${host}/${engine}/${id}`;
10
-
11
- // All parent components are checked using the same project boundary as game files.
12
- async function directory(project, relative) {
13
- const marker = await safeProjectFile(project,relative+'/.directory-check',true);
14
- return path.dirname(marker);
15
- }
16
- async function noLinks(root) {
17
- for (const entry of await readdir(root,{withFileTypes:true})) {
18
- if (entry.isSymbolicLink() || (!entry.isDirectory() && !entry.isFile())) throw new Error('Project context contains a link or special file');
19
- if (entry.isDirectory()) await noLinks(path.join(root,entry.name));
20
- }
21
- }
22
-
23
- // Immutable version directories keep an in-flight operation on the exact files it selected.
24
- // Only the sync record switches after the new snapshot is complete and verified.
25
- export async function materializeContext(project, current, previous) {
26
- const {host,engine,bundle,resources} = current;
27
- const relative = localContextPath(host,engine,bundle.id);
28
- const parent = await directory(project,`.craft/contexts/${host}/${engine}`);
29
- const target = path.join(parent,bundle.id);
30
- let present = false, backup;
31
- try {
32
- const stat = await lstat(target);
33
- if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error('Project context target must be a regular directory');
34
- await noLinks(target);
35
- present = true;
36
- } catch (error) { if (error.code !== 'ENOENT') throw error; }
37
- if (present) {
38
- try {
39
- const selected = await loadEngineResource(target,engine,bundle.id,host,{contextOnly:true});
40
- return {selected,relative,materialized:false};
41
- } catch (error) {
42
- // Do not claim an arbitrary pre-existing directory as an owned context.
43
- if (previous?.path !== relative) throw new Error('Unmanaged or incomplete project context at '+relative+'; preserve it before retrying', {cause:error});
44
- }
45
- }
46
- const staging = await mkdtemp(path.join(parent,'.staging-'));
47
- try {
48
- const files = [...Object.keys(bundle.files).filter(name=>!name.startsWith(engine+'/project/')),engine+'/bundle.json'];
49
- for (const name of files) {
50
- const output = await safeProjectFile(staging,name,true);
51
- await writeFile(output,await readFile(path.join(resources,name)),{flag:'wx'});
52
- }
53
- await copyBundleArtPreviews(resources,staging,bundle);
54
- await verifyBundle(staging,engine,bundle.id,{contextOnly:true});
55
- if (present) {
56
- const backups = await directory(project,`.craft/context-backups/${host}/${engine}`);
57
- backup = path.join(backups,bundle.id+'-'+randomUUID());
58
- await rename(target,backup);
59
- }
60
- await rename(staging,target);
61
- const selected = await loadEngineResource(target,engine,bundle.id,host,{contextOnly:true});
62
- return {selected,relative,materialized:true,...(backup ? {backup} : {})};
63
- } finally { await rm(staging,{recursive:true,force:true}); }
64
- }