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
@@ -99,15 +99,14 @@ export async function ensureRuntimeSource(ver) {
99
99
  if (window.__SPINE_RUNTIME_SOURCES__[ver]) return;
100
100
  let text = runtimeSourceCache.get(ver);
101
101
  if (!text) {
102
- // 20s:运行时脚本实测 439KB(spine-webgl 4.1.0),CIFS/NAS 上读大文件可能数秒。
103
- // 不给超时会让"装载模型"永久卡在"加载运行时"(用户看不出是卡住还是慢)。
102
+ // 不设时限:运行时脚本几百 KB,慢就等;失败要能分清"没这个版本"和"磁盘慢"。
104
103
  let r;
105
104
  try {
106
- r = await fetch(`${RUNTIME_BASE}${ver}.js`, { cache: 'no-store', signal: AbortSignal.timeout(20000) });
105
+ r = await fetch(`${RUNTIME_BASE}${ver}.js`, { cache: 'no-store' });
107
106
  } catch (e) {
108
- // 超时/中断必须翻译成人话:裸的 "The operation was aborted." 分不清是
107
+ // 请求被中断要翻译成人话:裸的 "The operation was aborted." 分不清是
109
108
  // "这台机器没装这个版本"(要补文件)还是"服务端/磁盘慢"(等或换机器)—— 两者的处置完全不同。
110
- throw new Error(`运行时 ${ver} 加载超时或中断(上限 20s):${(e && e.message) || e}`);
109
+ throw new Error(`运行时 ${ver} 加载被中断:${(e && e.message) || e}`);
111
110
  }
112
111
  if (!r.ok) {
113
112
  // 404 最常见的原因是"这台机器上没放这个版本" ⇒ 附上服务端给的可复制补齐方案,
@@ -130,7 +130,7 @@ export class SpineRuntimeManager {
130
130
  // 20s:与 canvas-runtime 同口径(运行时脚本实测 439KB),别让"加载运行时"无限挂着
131
131
  let r;
132
132
  try {
133
- r = await fetch(`${RUNTIME_BASE}${version}.js`, { cache: 'no-store', signal: AbortSignal.timeout(20000) }); // dsh-skip-quality 本地同源运行时脚本读取,既有代码
133
+ r = await fetch(`${RUNTIME_BASE}${version}.js`, { cache: 'no-store' }); // dsh-skip-quality 本地同源运行时脚本读取,既有代码
134
134
  } catch (e) {
135
135
  // 同 canvas-runtime:把裸的 abort 翻成"超时/中断",否则与"没装这个版本"混为一谈
136
136
  throw new Error(`运行时 ${version} 加载超时或中断(上限 20s):${(e && e.message) || e}`);
@@ -1,10 +1,15 @@
1
1
  // ============================================================================
2
2
  // tool/lib/bundle-probe-node.mjs —— 在 Node 里跑插件的「装载前预检」
3
3
  //
4
- // 为什么要这一层:预检实现只有一份(plugins/unity-live2d-bundle/lib/bundle-probe.js),
4
+ // 为什么要这一层:预检实现只有插件里的 lib/bundle-probe.js 一份。
5
5
  // 浏览器由插件 index.js 用,服务端由本文件把同一份 js 读进 vm 沙箱执行。
6
6
  // 这样 /api/bundle/probe 与页面里的 probe 结论必然一致,不存在两份解析逻辑。
7
7
  //
8
+ // **按能力找提供者,不认插件名**:
9
+ // 谁声明了 plugin.json 的 provides 含 `bundle-analyzer`,就用谁的 lib。
10
+ // 一个都没有时**降级**为按文件头判定种类(UnityFS / CAB- / MOC3 / ZIP / JSON),
11
+ // 而不是报错 —— "没装插件"应当表现为信息更少,不是功能坏掉。
12
+ //
8
13
  // 只加载**数据层**需要的库(不加载 index.js / 渲染相关),首次调用时建沙箱并缓存。
9
14
  // ============================================================================
10
15
 
@@ -18,20 +23,75 @@ import { histogramFromObjects, isAssetDependencyHistogram, dependencyReason } fr
18
23
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
19
24
  const ROOT = path.resolve(__dirname, '..', '..');
20
25
 
21
- /** 预检用哪个插件的 lib(预检实现只有 unity-live2d-bundle 这一份,spine 复用它) */
22
- const PROBE_PLUGIN_ID = 'unity-live2d-bundle';
26
+ /** 分析能力在契约里的角色名(插件契约 §一) */
27
+ const ANALYZER_ROLE = 'bundle-analyzer';
23
28
 
24
29
  /**
25
- * 取某插件的 lib 目录。
26
- * @param {string} [pluginId]
30
+ * 按 `provides` 找能分析容器字节流的插件。
31
+ *
32
+ * 排序规则:`matchPriority` 小的优先(与资产提供者的选择顺序同口径),
33
+ * 同优先级时按 id 字典序 —— 保证**同一台机器上每次选到同一个**,
34
+ * 不依赖 readdir 的返回顺序(那个顺序在换文件系统后会变)。
35
+ *
36
+ * @param {string} [pluginsDir] 插件根目录;缺省为项目的 plugins/。测试可传一个空目录验降级路径。
37
+ * @returns {{pluginId: string, libDir: string, libFiles: string[]}|null}
38
+ */
39
+ export function findAnalyzerProvider(pluginsDir = path.join(ROOT, 'plugins')) {
40
+ let entries;
41
+ try {
42
+ entries = fs.readdirSync(pluginsDir, { withFileTypes: true });
43
+ } catch {
44
+ return null; // 没有 plugins 目录 = 一个提供者都没有,走降级
45
+ }
46
+
47
+ const candidates = [];
48
+ for (const entry of entries) {
49
+ if (!entry.isDirectory()) continue;
50
+ // 与 src/server/plugins/registry.mjs 同一判据:`.` 开头的目录不是插件。
51
+ // `.trash`(回收站)与 `.bak-*` 里常有完整的历史插件副本,若参与分派,
52
+ // 分析结论可能来自已被删除的旧版本。
53
+ if (entry.name.startsWith('.')) continue;
54
+ const manifestPath = path.join(pluginsDir, entry.name, 'plugin.json');
55
+ let manifest;
56
+ try {
57
+ manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
58
+ } catch {
59
+ continue; // 没有清单或清单坏了:跳过(不是本角色的提供者)
60
+ }
61
+ const provides = Array.isArray(manifest.provides) ? manifest.provides : [];
62
+ if (!provides.includes(ANALYZER_ROLE)) continue;
63
+
64
+ const files = Array.isArray(manifest.files) ? manifest.files : [];
65
+ const libFiles = files
66
+ .filter((f) => typeof f === 'string' && f.startsWith('lib/'))
67
+ .map((f) => f.slice('lib/'.length));
68
+ if (!libFiles.length) continue; // 声明了角色却没有 lib:无法执行,跳过
69
+
70
+ candidates.push({
71
+ pluginId: manifest.id || entry.name,
72
+ libDir: path.join(pluginsDir, entry.name, 'lib'),
73
+ libFiles,
74
+ priority: Number.isFinite(manifest.matchPriority) ? manifest.matchPriority : Number.MAX_SAFE_INTEGER,
75
+ });
76
+ }
77
+
78
+ if (!candidates.length) return null;
79
+ candidates.sort((a, b) => (a.priority - b.priority) || a.pluginId.localeCompare(b.pluginId));
80
+ const { pluginId, libDir, libFiles } = candidates[0];
81
+ return { pluginId, libDir, libFiles };
82
+ }
83
+
84
+ /**
85
+ * 取某提供者的 lib 目录。
86
+ * @param {{libDir: string}} provider
27
87
  * @returns {string}
28
88
  */
29
- function libDirFor(pluginId = PROBE_PLUGIN_ID) {
30
- return path.join(ROOT, 'plugins', pluginId, 'lib');
89
+ export function libDirFor(provider) {
90
+ return provider.libDir;
31
91
  }
32
92
 
33
93
  /**
34
- * 从插件的 `plugin.json` 派生「预检要加载的 lib 文件清单」——**唯一真相是 manifest**。
94
+ * 取某提供者「预检要加载的 lib 文件清单」——**唯一真相是 manifest**。
35
95
  *
36
96
  * 为什么必须派生:这份清单曾经是手写的,与 plugin.json 的 files[] 构成"同一事实的两处表达"。
37
97
  * 漏一边的表现极难定位:**浏览器能跑(按 files[] 加载)、服务端预检报缺依赖(按手写清单加载)**。
@@ -40,23 +100,27 @@ function libDirFor(pluginId = PROBE_PLUGIN_ID) {
40
100
  * 顺序也来自 manifest:files[] 本身就是依赖顺序(errors 最先、bundle-probe 在后),
41
101
  * 派生时**原样保留**,不重排(重排就等于又引入一处人工判断)。
42
102
  *
43
- * @param {string} [pluginId]
103
+ * @param {{libFiles: string[]}} provider
44
104
  * @returns {string[]} 形如 ['errors.js', 'bytes.js', ...]
45
105
  */
46
- export function libOrderFor(pluginId = PROBE_PLUGIN_ID) {
47
- const manifestPath = path.join(ROOT, 'plugins', pluginId, 'plugin.json');
48
- const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
49
- const files = Array.isArray(manifest.files) ? manifest.files : [];
50
- return files
51
- .filter((f) => typeof f === 'string' && f.startsWith('lib/'))
52
- .map((f) => f.slice('lib/'.length));
106
+ export function libOrderFor(provider) {
107
+ return provider.libFiles;
53
108
  }
54
109
 
55
- let sandbox = null;
110
+ /**
111
+ * 一个提供者的沙箱(按 pluginId 缓存)。
112
+ *
113
+ * 为什么按提供者缓存而不是全局一个:两个插件各自有一份 lib/bundle-probe.js,
114
+ * 缓存键必须能区分它们,否则"先扫到谁就用谁的实现"会变成隐式约定。
115
+ * @type {Map<string, object>}
116
+ */
117
+ const sandboxes = new Map();
118
+
119
+ /** 建(或取缓存的)库沙箱。首次用到某提供者时才读文件 + 执行。 */
120
+ function getSandbox(provider) {
121
+ const cached = sandboxes.get(provider.pluginId);
122
+ if (cached) return cached;
56
123
 
57
- /** 建(或取缓存的)库沙箱。首次调用才读文件 + 执行。 */
58
- function getSandbox() {
59
- if (sandbox) return sandbox;
60
124
  const ctx = {
61
125
  console: { log() {}, info() {}, warn() {}, error() {}, debug() {} },
62
126
  TextDecoder,
@@ -68,27 +132,74 @@ function getSandbox() {
68
132
  ctx.window = ctx;
69
133
  ctx.self = ctx;
70
134
  vm.createContext(ctx);
71
- // 清单在**运行时**从 plugin.json 派生(不是模块加载时算一次):
72
- // 这样新增 lib 文件后无需改这里,也不会因进程长期驻留而用上过期清单。
73
- const libDir = libDirFor(PROBE_PLUGIN_ID);
74
- for (const rel of libOrderFor(PROBE_PLUGIN_ID)) {
135
+
136
+ const libDir = libDirFor(provider);
137
+ for (const rel of libOrderFor(provider)) {
75
138
  const abs = path.join(libDir, rel);
76
139
  // 报错要能自己说清"该看哪两个地方":manifest 声明了但磁盘上没有
77
- if (!fs.existsSync(abs)) throw new Error(`预检依赖缺失:plugins/${PROBE_PLUGIN_ID}/lib/${rel}(plugin.json 的 files[] 里有它,但磁盘上没有)`);
140
+ if (!fs.existsSync(abs)) {
141
+ throw new Error(`预检依赖缺失:plugins/${provider.pluginId}/lib/${rel}(plugin.json 的 files[] 里有它,但磁盘上没有)`);
142
+ }
78
143
  // 用 vm 执行而非 import:这些库是浏览器风格的脚本(挂 globalThis.__ULB__),不是 ESM
79
- vm.runInContext(fs.readFileSync(abs, 'utf8'), ctx, { filename: `ulb-lib/${rel}` });
144
+ vm.runInContext(fs.readFileSync(abs, 'utf8'), ctx, { filename: `${provider.pluginId}-lib/${rel}` });
80
145
  }
81
- sandbox = ctx;
82
- return sandbox;
146
+ sandboxes.set(provider.pluginId, ctx);
147
+ return ctx;
148
+ }
149
+
150
+ /**
151
+ * 一个都认不出来时的按文件头判定。
152
+ *
153
+ * 只看前几个字节,不下任何"能不能用"的结论 —— 那需要真解析。
154
+ * 这段存在的意义是:没装插件时,调用方仍然知道**手里这个文件大概是什么**。
155
+ * @param {Uint8Array} bytes
156
+ * @returns {{kind: string, note: string}}
157
+ */
158
+ function detectByMagic(bytes) {
159
+ const len = bytes.length;
160
+ const ascii = (n) => {
161
+ let s = '';
162
+ for (let i = 0; i < Math.min(n, len); i += 1) s += String.fromCharCode(bytes[i]);
163
+ return s;
164
+ };
165
+ if (ascii(7) === 'UnityFS') return { kind: 'UnityFS', note: 'Unity 资源包(装 unity-*-bundle 插件可细看内容)' };
166
+ if (ascii(4) === 'CAB-') return { kind: 'CAB-', note: 'Unity serialized file(裸 CAB,通常是资源包解出来的节点)' };
167
+ if (ascii(4) === 'MOC3') return { kind: 'MOC3', note: 'Cubism 4 模型二进制' };
168
+ if (len >= 2 && bytes[0] === 0x50 && bytes[1] === 0x4b) return { kind: 'ZIP', note: 'ZIP 容器' };
169
+ if (len >= 1 && (bytes[0] === 0x7b || bytes[0] === 0x5b)) return { kind: 'JSON', note: 'JSON 文本' };
170
+ return { kind: 'unknown', note: '无法从文件头识别' };
171
+ }
172
+
173
+ /** 没有提供者时的降级结论:形状与正常结论兼容,只是字段更少。 */
174
+ export function analyzeByMagicOnly(bytes) {
175
+ const magic = detectByMagic(bytes);
176
+ return {
177
+ ok: false,
178
+ code: 'ULB_ERR_NO_ANALYZER',
179
+ error: `未安装能分析资源包的插件;仅按文件头判定为 ${magic.kind}(${magic.note})`,
180
+ container: null,
181
+ objects: null,
182
+ textures: null,
183
+ magic: magic.kind,
184
+ warnings: [
185
+ `按文件头判定为 ${magic.kind}:${magic.note}`,
186
+ `安装声明 ${ANALYZER_ROLE} 角色的插件后,可得到容器头 / 对象表 / 纹理格式等完整分析`,
187
+ ],
188
+ };
83
189
  }
84
190
 
85
191
  /**
86
192
  * 预检一个资源包字节流。
193
+ *
194
+ * 提供者按 plugin.json 的 provides 找(不认插件名);一个都没有时降级为魔数判定。
87
195
  * @param {Uint8Array} bytes
88
196
  * @returns {object} 与插件内 probe 同源的结论 + 包内类型构成(依赖包判定用)
89
197
  */
90
198
  export function probeBundleBytes(bytes) {
91
- const ctx = getSandbox();
199
+ const provider = findAnalyzerProvider();
200
+ if (!provider) return analyzeByMagicOnly(bytes);
201
+
202
+ const ctx = getSandbox(provider);
92
203
  const NS = ctx.__ULB__;
93
204
  const bp = NS && NS.bundleProbe;
94
205
  const res = (bp && typeof bp.run === 'function') ? bp.run(bytes) : null;
@@ -113,9 +224,12 @@ export function probeBundleBytes(bytes) {
113
224
  }
114
225
  } catch { /* 形态探测失败不影响主结论(探不到就不下依赖包结论) */ }
115
226
 
116
- return Object.assign({}, res || {}, shape || {});
227
+ // 带上提供者 id:调用方(/api/analyze)要用它回答"这份结论是哪个插件给的",
228
+ // 缺了就只能给 null,等于把"解耦可见"这条验收做成摆设。
229
+ return Object.assign({ pluginId: provider.pluginId }, res || {}, shape || {});
117
230
  }
118
231
 
119
232
  // 路径与清单都**不再导出成常量**(常量会诱使别人继续手写第二份);
120
- // 需要自查清单时调 libOrderFor(pluginId) 现取。
121
- export const PROBE_PATHS = { ROOT, libDirFor, libOrderFor };
233
+ // 需要自查清单时调 libOrderFor(provider) 现取。
234
+ export { ROOT, ANALYZER_ROLE };
235
+ export const PROBE_PATHS = { ROOT, findAnalyzerProvider, libDirFor, libOrderFor };