modelstage 0.5.28 → 0.10.1

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 (98) hide show
  1. package/CHANGELOG.md +92 -0
  2. package/README.md +2 -63
  3. package/config/models/live2d.assetbundls.json +1 -1
  4. package/config/models/live2d.azurlane.json +1 -1
  5. package/config/models/live2d.azurlane_bundle.json +1 -1
  6. package/config/models/live2d.azurlane_bundle.jsonl +2 -0
  7. package/config/models/live2d.azurlane_spine.json +1 -1
  8. package/config/models/live2d.bd2.json +1 -1
  9. package/config/models/live2d.bd2.jsonl +2 -0
  10. package/config/models/live2d.bd2_mod.json +1 -0
  11. package/config/models/live2d.bluearchive.json +1 -1
  12. package/config/models/live2d.nikke.json +1 -1
  13. package/config/models/live2d.nikke.jsonl +2 -0
  14. package/config/models/live2d.nikke_2.json +1 -1
  15. package/config/models/live2d.npc.json +1 -1
  16. package/config/models/live2d.sample_haru.json +1 -0
  17. package/config/models/spine.assetbundls.json +1 -1
  18. package/config/models/spine.azurlane.json +1 -1
  19. package/config/models/spine.azurlane_bundle.json +1 -1
  20. package/config/models/spine.azurlane_bundle.jsonl +2 -0
  21. package/config/models/spine.azurlane_spine.json +1 -1
  22. package/config/models/spine.bd2.json +1 -1
  23. package/config/models/spine.bd2.jsonl +433 -0
  24. package/config/models/spine.bd2_mod.json +1 -0
  25. package/config/models/spine.bluearchive.json +1 -1
  26. package/config/models/spine.nikke.json +1 -1
  27. package/config/models/spine.nikke_2.json +1 -1
  28. package/config/models/spine.npc.json +1 -1
  29. package/config/models/spine.sample_haru.json +1 -0
  30. package/package.json +3 -2
  31. package/plugins/unity-live2d-bundle/index.js +43 -1
  32. package/plugins/unity-live2d-bundle/lib/bundle-probe.js +79 -0
  33. package/plugins/unity-live2d-bundle/plugin.json +2 -1
  34. package/plugins/unity-spine-bundle/index.js +38 -1
  35. package/plugins/unity-spine-bundle/lib/bundle-probe.js +323 -0
  36. package/plugins/unity-spine-bundle/plugin.json +3 -1
  37. package/src/server/analyze/index.mjs +470 -0
  38. package/src/server/assets/probe.mjs +1 -1
  39. package/src/server/core/config.mjs +2 -2
  40. package/src/server/core/log.mjs +1 -1
  41. package/src/server/data/catalog.mjs +41 -16
  42. package/src/server/data/display-name-cache.mjs +142 -0
  43. package/src/server/data/entry-states.mjs +21 -0
  44. package/src/server/data/live2d.mjs +139 -40
  45. package/src/server/data/manifest-cache.mjs +53 -10
  46. package/src/server/data/manifest-jsonl.mjs +264 -0
  47. package/src/server/data/manifest-stamp.mjs +52 -2
  48. package/src/server/data/scan-progress.mjs +305 -0
  49. package/src/server/data/scan-queue.mjs +99 -0
  50. package/src/server/data/spine.mjs +324 -106
  51. package/src/server/http/write.mjs +7 -6
  52. package/src/server/index.mjs +1 -1
  53. package/src/server/plugins/registry.mjs +3 -3
  54. package/src/server/routes/data.mjs +76 -17
  55. package/src/server/routes/display-name.mjs +157 -0
  56. package/src/server/serve.mjs +17 -4
  57. package/src/server/unpack/plugin-store.mjs +1 -1
  58. package/src/server/unpack/runtime/exec.mjs +37 -8
  59. package/src/server/unpack/worker-client.mjs +23 -8
  60. package/src/shared/scan-limits.mjs +23 -0
  61. package/src/web/app/anim-list.js +1 -1
  62. package/src/web/app/backgrounds.js +4 -8
  63. package/src/web/app/dir-picker.js +3 -5
  64. package/src/web/app/memory-budget.js +0 -1
  65. package/src/web/app/memory-guard.js +0 -1
  66. package/src/web/app/model-actions.js +18 -15
  67. package/src/web/app/root-manager.js +2 -4
  68. package/src/web/app/transport-actions.js +1 -1
  69. package/src/web/app/ui-state.js +1 -1
  70. package/src/web/components.css +90 -0
  71. package/src/web/debug/race-probe.js +36 -13
  72. package/src/web/engine/textures.js +1 -2
  73. package/src/web/infra/api.js +10 -19
  74. package/src/web/infra/document.js +3 -3
  75. package/src/web/infra/plugin-host-unpack.mjs +94 -12
  76. package/src/web/infra/plugin-host.js +3 -0
  77. package/src/web/infra/ready.js +177 -39
  78. package/src/web/live2d/bridge.js +1 -1
  79. package/src/web/live2d/runtime-adapter.js +2 -4
  80. package/src/web/panel/panel-settings.js +88 -3
  81. package/src/web/render/surface.js +49 -2
  82. package/src/web/roster/display-name.js +79 -0
  83. package/src/web/roster/events.js +198 -0
  84. package/src/web/roster/filter.js +4 -4
  85. package/src/web/roster/list.js +11 -5
  86. package/src/web/roster/load.js +440 -0
  87. package/src/web/roster/persist.js +46 -37
  88. package/src/web/roster/render.js +284 -0
  89. package/src/web/roster/reveal.js +4 -9
  90. package/src/web/roster/rows.js +35 -13
  91. package/src/web/roster/scan-stream.js +346 -0
  92. package/src/web/roster/snapshot.js +14 -8
  93. package/src/web/roster/store.js +340 -0
  94. package/src/web/roster/type-filter.js +5 -5
  95. package/src/web/roster/view.js +147 -634
  96. package/src/web/spine/canvas-runtime.js +4 -5
  97. package/src/web/spine/runtimes.js +1 -1
  98. package/tool/lib/bundle-probe-node.mjs +146 -32
@@ -0,0 +1,340 @@
1
+ // roster/store.js —— 名册**唯一状态持有者**(状态收口)
2
+ //
3
+ // 为什么要有它:名册原先有 7 份状态(loaded / scan / groupChars / expandedGroups /
4
+ // expandedChars / selected / scanBaseline)分散在 mount、ensureRootLoaded、trackScanProgress、
5
+ // loadGroup、ensureGroupLoaded、rebuildGroupChars、restoreLast、render 里各自直接改写。
6
+ // 写入点一多,任何新功能(例如"增量加条目")都会变成第 8、9 个写入点,行为难以推理。
7
+ //
8
+ // 约定:
9
+ // · **只有本文件可以写 state 的字段**;外部(view.js / snapshot.js)只读。
10
+ // · 所有变更走下面的动作方法;动作保持"最小原子":只改状态,不夹带 UI 逻辑。
11
+ // · 渲染时机分两种(别假设"改完就一定会重画"):
12
+ // ① 改结构/活跃分组的动作(setRoots / setActiveGroup)内部调 notify → 视图的 onChange;
13
+ // ② 其余动作**不** notify —— 由调用方在合适的时机自己补一次渲染(名册走 rAF 合批的 scheduleRender)。
14
+ // 为什么②不统一 notify:加载期一条路径会连改十几次状态,每次 notify 就是十几次整树重绘;
15
+ // 合批是名册首屏性能的关键(见 infra/schedule.js 文件头)。
16
+
17
+ /**
18
+ * @param {{buildChars: Function, nameIndex?: Map, onChange?: () => void}} deps
19
+ * buildChars(models, nameIndex, groupName) ⇒ { chars, stats }(由 view.js 注入,避免本文件依赖构建细节)
20
+ * nameIndex 名表索引(同样由 view.js 注入):本文件要"写完 models 就重算角色树",
21
+ * 而归并需要它 —— 让 store 自己持有,调用方就不必每次记得传
22
+ */
23
+ export function createRosterStore({ buildChars, nameIndex, onChange } = {}) {
24
+ const state = {
25
+ /** 按 name 合并后的游戏列表(结构:{ key, name, roots[] }) */
26
+ groups: [],
27
+ /** rootId → { status, models, source, error, promise, pendingCatalogRetry } */
28
+ loaded: new Map(),
29
+ /** rootId → { monitor, controller, group }(扫描进行中的根) */
30
+ scan: new Map(),
31
+ /** groupKey → { chars, stats }(跨根归并后的角色树) */
32
+ groupChars: new Map(),
33
+ expandedGroups: new Set(),
34
+ expandedChars: new Set(),
35
+ selected: null,
36
+ keyword: '',
37
+ typeFilter: new Set(),
38
+ /** groupKey → { scanned, dirs }(多根逐根扫描时的计数基线) */
39
+ scanBaseline: new Map(),
40
+ /** 当前活跃的**分组**名(注意:语义是"分组",不假设它一定是游戏) */
41
+ activeGroup: null,
42
+ /** 本页生命周期内已经跑过进度流的根(防止重复订阅) */
43
+ scannedRoots: new Set(),
44
+ /** 后台形态探测队列快照:{ remaining, at, rootId }(UI 未实现,数据先就绪) */
45
+ probe: { remaining: null, at: null, rootId: null },
46
+ /** 最近一次"定位到当前项"的结果(滚动/已可见/用户正在滚动…) */
47
+ lastReveal: null,
48
+ };
49
+
50
+ const notify = () => {
51
+ if (typeof onChange === 'function') {
52
+ try { onChange(); } catch { /* 视图回调异常不影响状态 */ }
53
+ }
54
+ };
55
+
56
+ /** 读:某根的加载条目(不存在返回 null,不隐式创建) */
57
+ const rootEntry = (rootId) => state.loaded.get(String(rootId)) || null;
58
+
59
+ /**
60
+ * 跨根归并该组的角色树(内部实现,`setRootModels` 与公开动作 `rebuildChars` 共用一份)。
61
+ *
62
+ * 只收 `status === 'ready'` 的根:还在 loading / 已 error 的根不参与归并,
63
+ * 否则半截数据会先被画出来(用户看到角色数忽多忽少)。
64
+ * @param {string} groupKey
65
+ */
66
+ function rebuildCharsFor(groupKey) {
67
+ const group = state.groups.find((g) => g.key === String(groupKey));
68
+ if (!group) return null;
69
+ const models = [];
70
+ for (const root of group.roots) {
71
+ const entry = state.loaded.get(root.id);
72
+ if (entry && entry.status === 'ready') models.push(...(entry.models || []));
73
+ }
74
+ const built = buildChars(models, nameIndex, group.name);
75
+ state.groupChars.set(group.key, built);
76
+ return built;
77
+ }
78
+
79
+ const actions = {
80
+ state,
81
+
82
+ // ---------------------------------------------------------------- 根与游戏
83
+ /**
84
+ * 重建游戏列表(按 name 合并同名根)。
85
+ * @param {Array} roots /api/roots 的 roots
86
+ * @returns {Array} 合并后的 groups
87
+ */
88
+ setRoots(roots) {
89
+ const byName = new Map();
90
+ for (const root of Array.isArray(roots) ? roots : []) {
91
+ const name = root.name || root.id;
92
+ if (!byName.has(name)) byName.set(name, { key: `group:${name}`, name, roots: [] });
93
+ byName.get(name).roots.push(root);
94
+ }
95
+ state.groups = [...byName.values()];
96
+ notify();
97
+ return state.groups;
98
+ },
99
+
100
+ /** 某根所属的游戏 */
101
+ groupOfRoot(rootId) {
102
+ return state.groups.find((g) => g.roots.some((r) => r.id === rootId)) || null;
103
+ },
104
+
105
+ // ---------------------------------------------------------------- 根的加载状态
106
+ /**
107
+ * 写某根的加载状态(浅合并;不传的字段保持不变)。
108
+ * @param {string} rootId
109
+ * @param {object} patch 例:{ status:'loading' } / { status:'error', error:'…' }
110
+ */
111
+ setRootStatus(rootId, patch) {
112
+ const id = String(rootId);
113
+ const cur = state.loaded.get(id) || { status: 'idle', models: [], source: '', error: null, promise: null };
114
+ state.loaded.set(id, { ...cur, ...(patch || {}) });
115
+ },
116
+
117
+ /** 读某根条目 */
118
+ getRoot(rootId) {
119
+ return rootEntry(rootId);
120
+ },
121
+
122
+ /** 清掉若干根的加载状态(重试用:让它们回到"未加载") */
123
+ resetRoots(rootIds) {
124
+ for (const id of rootIds || []) state.loaded.delete(String(id));
125
+ },
126
+
127
+ /**
128
+ * 写某根的模型清单(权威全量)。
129
+ *
130
+ * **写完自动重算该根所属组的角色树**(派生状态跟着源状态走)。
131
+ * 为什么在这里而不是让调用方各自记得调 rebuildChars:调用方一多必然漏 ——
132
+ * 实测过一次:restoreLast 那条路径漏了归并 ⇒ 加载完成后行上 0 角色、
133
+ * 收起再展开才出现(第二次走的是另一条会归并的路径)。把重算钉在唯一的
134
+ * 写入入口上,新增调用方就不可能再漏。
135
+ * @param {string} rootId
136
+ * @param {Array} models
137
+ * @param {{authoritative?: boolean, source?: string}} [opts]
138
+ */
139
+ setRootModels(rootId, models, opts = {}) {
140
+ const id = String(rootId);
141
+ const entry = state.loaded.get(id);
142
+ if (!entry) return;
143
+ entry.models = Array.isArray(models) ? models : [];
144
+ if (opts.source) entry.source = String(opts.source);
145
+ if (opts.authoritative !== false) entry.status = 'ready';
146
+ // 就绪状态已落地,此刻归并才拿得到这份 models(rebuildChars 只收 status==='ready' 的根)
147
+ const group = actions.groupOfRoot(id);
148
+ if (group) rebuildCharsFor(group.key);
149
+ },
150
+
151
+ // ---------------------------------------------------------------- 扫描
152
+ updateScan(rootId, payload) {
153
+ state.scan.set(String(rootId), payload);
154
+ },
155
+
156
+ clearScan(rootId) {
157
+ state.scan.delete(String(rootId));
158
+ },
159
+
160
+ getScan(rootId) {
161
+ return state.scan.get(String(rootId)) || null;
162
+ },
163
+
164
+ setScanBaseline(groupKey, base) {
165
+ state.scanBaseline.set(String(groupKey), base);
166
+ },
167
+
168
+ getScanBaseline(groupKey) {
169
+ return state.scanBaseline.get(String(groupKey)) || null;
170
+ },
171
+
172
+ clearScanBaseline(groupKey) {
173
+ state.scanBaseline.delete(String(groupKey));
174
+ },
175
+
176
+ // ---------------------------------------------------------------- 角色树
177
+ /**
178
+ * 用该组各根**已就绪**的模型重建角色树(跨根归并)。
179
+ *
180
+ * 正常流程里不需要手动调:`setRootModels` 写完就会自动重算(见那里的注释)。
181
+ * 留成公开动作是为了"幂等兜底":某些路径上模型早已就绪、本次没有新的写入
182
+ * (例如重复展开一个已加载的组),调用方仍可显式要求"确保已归并"。
183
+ * @param {string} groupKey
184
+ * @returns {object|null} { chars, stats }
185
+ */
186
+ rebuildChars(groupKey) {
187
+ return rebuildCharsFor(groupKey);
188
+ },
189
+
190
+ /**
191
+ * 重建分组并**迁移展开态与选中项**。
192
+ *
193
+ * 为什么需要:分组键由显示名派生(`buildChars` 里的 `groupName` 优先用 displayName)。
194
+ * 显示名是**选中后**才取到的,届时分组结构会变 —— 旧 charKey 全部失效。
195
+ * 只改 DOM 文本不重建 ⇒ 分组、角色标题、别名(baseName)都还是旧值,
196
+ * 表现为"刷新浏览器才正确";只重建不迁移 ⇒ 用户展开的角色全被收起。
197
+ * 展开态与选中项都按 **model id** 迁移(charKey 会变,model id 不变)。
198
+ * @param {string} groupKey
199
+ * @returns {object|null} 重建后的 { chars, stats }
200
+ */
201
+ rebuildCharsPreservingExpansion(groupKey) {
202
+ const key = String(groupKey);
203
+ const group = state.groups.find((g) => g.key === key);
204
+ if (!group) return null;
205
+ const before = state.groupChars.get(key);
206
+ const expandedModelIds = new Set();
207
+ for (const c of (before && before.chars) || []) {
208
+ if (!state.expandedChars.has(c.key)) continue; // 角色展开态在 expandedChars(组展开态是 expandedGroups)
209
+ for (const v of c.variants || []) if (v.model && v.model.id) expandedModelIds.add(v.model.id);
210
+ }
211
+ // 旧 charKey 已不属于任何现存角色,先摘掉,避免展开集合里留垃圾
212
+ for (const c of (before && before.chars) || []) state.expandedChars.delete(c.key);
213
+ const built = rebuildCharsFor(key);
214
+ if (!built) return null;
215
+ if (expandedModelIds.size) {
216
+ for (const c of built.chars || []) {
217
+ if ((c.variants || []).some((v) => v.model && expandedModelIds.has(v.model.id))) {
218
+ state.expandedChars.add(c.key);
219
+ }
220
+ }
221
+ }
222
+ const selectedId = state.selected && state.selected.modelId;
223
+ if (selectedId) {
224
+ for (const c of built.chars || []) {
225
+ if ((c.variants || []).some((v) => v.model && v.model.id === selectedId)) {
226
+ state.selected = { ...state.selected, charKey: c.key };
227
+ // 选中项的所属角色必须保持展开:分组键变了,若不补这一下,
228
+ // 用户刚点的行会在重建后"消失"(新 char 默认收起),看起来像点击没生效。
229
+ state.expandedChars.add(c.key);
230
+ break;
231
+ }
232
+ }
233
+ }
234
+ notify();
235
+ return built;
236
+ },
237
+
238
+ // ---------------------------------------------------------------- 展开态
239
+ toggleGroup(groupKey) {
240
+ const key = String(groupKey);
241
+ const next = !state.expandedGroups.has(key);
242
+ if (next) state.expandedGroups.add(key); else state.expandedGroups.delete(key);
243
+ return next;
244
+ },
245
+
246
+ expandGroup(groupKey) {
247
+ state.expandedGroups.add(String(groupKey));
248
+ },
249
+
250
+ collapseAll() {
251
+ state.expandedGroups.clear();
252
+ state.expandedChars.clear();
253
+ },
254
+
255
+ toggleChar(charKey) {
256
+ const key = String(charKey);
257
+ const next = !state.expandedChars.has(key);
258
+ if (next) state.expandedChars.add(key); else state.expandedChars.delete(key);
259
+ return next;
260
+ },
261
+
262
+ expandChar(charKey) {
263
+ state.expandedChars.add(String(charKey));
264
+ },
265
+
266
+ /** 恢复持久化里的展开态(只保留仍存在的游戏) */
267
+ restoreExpanded({ groups, chars }) {
268
+ const keys = new Set(state.groups.map((g) => g.key));
269
+ for (const key of groups || []) if (keys.has(key)) state.expandedGroups.add(key);
270
+ for (const key of chars || []) state.expandedChars.add(key);
271
+ },
272
+
273
+ // ---------------------------------------------------------------- 选中 / 关键词 / 筛选
274
+ /**
275
+ * 写选中项。
276
+ * @param {{modelId:string, groupKey:string, groupName:string|null, charKey:string}|null} payload
277
+ */
278
+ selectModel(payload) {
279
+ state.selected = payload || null;
280
+ },
281
+
282
+ setKeyword(keyword) {
283
+ state.keyword = String(keyword || '').trim().toLowerCase();
284
+ },
285
+
286
+ toggleTypeFilter(typeKey) {
287
+ const key = String(typeKey);
288
+ const next = !state.typeFilter.has(key);
289
+ if (next) state.typeFilter.add(key); else state.typeFilter.delete(key);
290
+ return next;
291
+ },
292
+
293
+ clearTypeFilter() {
294
+ state.typeFilter.clear();
295
+ },
296
+
297
+ /** 一次性把筛选集合替换掉(测试/外部恢复用;正常交互请用 toggleTypeFilter) */
298
+ setTypeFilter(keys) {
299
+ state.typeFilter = new Set(keys || []);
300
+ },
301
+
302
+ // ---------------------------------------------------------------- 分组 / 订阅标记 / 探测 / 定位
303
+ /**
304
+ * 写"当前活跃分组"(分组名,不是 groupKey)。
305
+ * @param {string|null} name
306
+ */
307
+ setActiveGroup(name) {
308
+ state.activeGroup = name ? String(name) : null;
309
+ notify();
310
+ },
311
+
312
+ /** 标记某根已订阅过进度流(防重复订阅) */
313
+ markRootSubscribed(rootId) {
314
+ state.scannedRoots.add(String(rootId));
315
+ },
316
+
317
+ unmarkRootSubscribed(rootId) {
318
+ state.scannedRoots.delete(String(rootId));
319
+ },
320
+
321
+ hasRootSubscribed(rootId) {
322
+ return state.scannedRoots.has(String(rootId));
323
+ },
324
+
325
+ /**
326
+ * 写后台形态探测快照。
327
+ * @param {{remaining:number|null, at:number, rootId:string|null}} snapshot
328
+ */
329
+ setProbe(snapshot) {
330
+ state.probe = { ...(snapshot || {}) };
331
+ },
332
+
333
+ /** 写"最近一次定位结果"(reveal 的返回值,排障与断言读它) */
334
+ setLastReveal(result) {
335
+ state.lastReveal = result || null;
336
+ },
337
+ };
338
+
339
+ return actions;
340
+ }
@@ -34,16 +34,16 @@ export function typeKeyOf(variant) {
34
34
  * scope 参数是为将来扩维度(如按资源根)预留的:当前只消费 keyword,新增维度时只加 scope 字段,
35
35
  * 不动本函数的排序/计数逻辑(设计稿 §5.4)。
36
36
  *
37
- * @param {{games: object[], gameChars: Map, keyword?: string,
37
+ * @param {{groups: object[], groupChars: Map, keyword?: string,
38
38
  * scope?: {keyword?: string}}} input
39
39
  * @returns {Array<{key: string, count: number}>} 按数量降序,并列按 key 字典序(稳定)
40
40
  */
41
- export function deriveTypeCounts({ games, gameChars, keyword = '', scope = null }) {
41
+ export function deriveTypeCounts({ groups, groupChars, keyword = '', scope = null }) {
42
42
  const effectiveKeyword = String((scope && scope.keyword) != null ? scope.keyword : keyword || '').toLowerCase();
43
43
  const counts = new Map();
44
- for (const game of games || []) {
44
+ for (const group of groups || []) {
45
45
  // 复用关键词过滤的同一口径(角色整组判定),保证"数字"与"列表可见性"不会两套算法
46
- const chars = visibleChars(game, { gameChars, keyword: effectiveKeyword });
46
+ const chars = visibleChars(group, { groupChars, keyword: effectiveKeyword });
47
47
  for (const char of chars) {
48
48
  for (const variant of char.variants || []) {
49
49
  const typeKey = typeKeyOf(variant);
@@ -60,7 +60,7 @@ export function deriveTypeCounts({ games, gameChars, keyword = '', scope = null
60
60
  * 按选中类型过滤角色(并把每个角色的变体裁到命中集合)。
61
61
  *
62
62
  * 语义:selectedTypes 为空 ⇒ 不过滤(等于"全部");否则 = 并集(命中任一选中类型即保留)。
63
- * 注意返回的是**新数组/新角色对象**,不改动 state.gameChars 里的原对象(渲染层只读)。
63
+ * 注意返回的是**新数组/新角色对象**,不改动 state.groupChars 里的原对象(渲染层只读)。
64
64
  *
65
65
  * @param {object[]} chars 角色数组(每个带 variants)
66
66
  * @param {Set<string>|string[]} selectedTypes