modelstage 0.6.1 → 0.11.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 (43) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/THIRD_PARTY_NOTICES.md +20 -0
  3. package/config/models/live2d.assetbundls.json +1 -1
  4. package/config/models/live2d.azurlane_bundle.json +1 -1
  5. package/config/models/live2d.bd2_mod.json +1 -0
  6. package/config/models/live2d.bluearchive.json +1 -1
  7. package/config/models/live2d.root.json +1 -0
  8. package/config/models/live2d.sample_haru.json +1 -0
  9. package/config/models/spine.assetbundls.json +1 -1
  10. package/config/models/spine.azurlane_bundle.json +1 -1
  11. package/config/models/spine.bd2_mod.json +1 -0
  12. package/config/models/spine.bluearchive.json +1 -1
  13. package/config/models/spine.root.json +1 -0
  14. package/config/models/spine.sample_haru.json +1 -0
  15. package/package.json +2 -2
  16. package/plugins/unity-live2d-bundle/index.js +105 -5
  17. package/plugins/unity-live2d-bundle/lib/astc.js +1307 -0
  18. package/plugins/unity-live2d-bundle/lib/bundle-probe.js +79 -0
  19. package/plugins/unity-live2d-bundle/lib/texdecode.js +60 -15
  20. package/plugins/unity-live2d-bundle/plugin.json +3 -1
  21. package/plugins/unity-spine-bundle/index.js +187 -15
  22. package/plugins/unity-spine-bundle/lib/astc.js +1307 -0
  23. package/plugins/unity-spine-bundle/lib/bundle-probe.js +323 -0
  24. package/plugins/unity-spine-bundle/lib/texdecode.js +60 -15
  25. package/plugins/unity-spine-bundle/plugin.json +4 -1
  26. package/src/server/analyze/index.mjs +470 -0
  27. package/src/server/data/catalog.mjs +30 -2
  28. package/src/server/data/display-name-cache.mjs +142 -0
  29. package/src/server/data/entry-states.mjs +21 -0
  30. package/src/server/http/write.mjs +7 -6
  31. package/src/server/index.mjs +1 -1
  32. package/src/server/plugins/registry.mjs +2 -2
  33. package/src/server/routes/display-name.mjs +157 -0
  34. package/src/server/serve.mjs +8 -0
  35. package/src/server/unpack/runtime/exec.mjs +19 -1
  36. package/src/web/infra/plugin-host-unpack.mjs +13 -3
  37. package/src/web/infra/plugin-host.js +3 -0
  38. package/src/web/render/surface.js +49 -2
  39. package/src/web/roster/display-name.js +79 -0
  40. package/src/web/roster/events.js +40 -1
  41. package/src/web/roster/list.js +8 -2
  42. package/src/web/roster/store.js +48 -0
  43. package/tool/lib/bundle-probe-node.mjs +146 -32
@@ -10,6 +10,10 @@
10
10
  // 两端各自加载同一份文件即可。
11
11
  //
12
12
  // 挂载:globalThis.__ULB__.bundleProbe = { run, formatSupport }
13
+ //
14
+ // 同源副本:本文件在 unity-live2d-bundle 与 unity-spine-bundle 两个插件里各存一份,
15
+ // 内容逐字节相同(两个插件都声明 bundle-analyzer 角色,各自要能独立提供 analyze)。
16
+ // 改一处必须同步改另一处;test/bundle-probe-parity.test.mjs 比对两份 sha256。
13
17
  // ============================================================================
14
18
 
15
19
  'use strict';
@@ -146,6 +150,50 @@
146
150
  }
147
151
  }
148
152
 
153
+ /**
154
+ * 从对象表取 TextAsset:名字 / 类型 / 骨架版本 / 字节数。
155
+ *
156
+ * 为什么走 typetree 的 rawStrings 而不是 extract 的文本分支:
157
+ * m_Script 是二进制(.skel),按 UTF-8 字符串读再编码回去会毁字节
158
+ * (实测 651994 字节 → 641818 字符含 165995 个 U+FFFD,长度都对不上)。
159
+ *
160
+ * 骨架版本的正则与 src/server/assets/probe.mjs 的 detectSkelVersion 完全一致:
161
+ * 两处必须同口径,否则"分析说 4.1.11、扫描器说默认 4.0"会各说一套。
162
+ *
163
+ * @param {object} sf serialized.parse() 结果
164
+ * @returns {Array<{name: string, kind: string, spineVersion: string|null, size: number}>}
165
+ */
166
+ function collectTextAssets(sf) {
167
+ const CLASS_ID_TEXT_ASSET = 49;
168
+ const out = [];
169
+ for (const obj of NS.serialized.byClass(sf, CLASS_ID_TEXT_ASSET)) {
170
+ let fields;
171
+ try {
172
+ fields = NS.typetree.readObject(sf, obj, { rawStrings: true });
173
+ } catch {
174
+ continue; // 单个对象读不出来不影响其它 TextAsset
175
+ }
176
+ const name = (fields.m_Name && typeof fields.m_Name.text === 'string')
177
+ ? fields.m_Name.text : String(fields.m_Name || '');
178
+ const raw = fields.m_Script && fields.m_Script.bytes;
179
+ if (!raw || !raw.length) continue;
180
+ const lower = name.toLowerCase();
181
+ let kind = 'bytes';
182
+ if (lower.endsWith('.skel')) kind = 'skel';
183
+ else if (lower.endsWith('.atlas')) kind = 'atlas';
184
+ else if (lower.endsWith('.json')) kind = 'json';
185
+ let spineVersion = null;
186
+ if (kind === 'skel') {
187
+ // 只看前 512 字节:二进制骨架的版本串在文件头附近,整份读进来没有意义
188
+ const head = new TextDecoder('latin1').decode(raw.subarray(0, 512));
189
+ const m = head.match(/(2\.\d+|3\.[5-8]|4\.[0-3])\.\d+[\w-]*/);
190
+ if (m) spineVersion = m[0];
191
+ }
192
+ out.push({ name, kind, spineVersion, size: raw.length });
193
+ }
194
+ return out;
195
+ }
196
+
149
197
  /**
150
198
  * 预检一个资源包。
151
199
  * @param {Uint8Array} bytes 资源包字节
@@ -226,12 +274,43 @@
226
274
  nodes: parsed.nodes.length,
227
275
  bytes: bytes.length,
228
276
  },
277
+ // 节点列表(path + size):只读分析接口要逐条列出来回答"包里有什么"。
278
+ // 预检自己只用 nodes 的**个数**,多给一层列表不增加解析成本。
279
+ nodes: parsed.nodes.map((n) => ({ path: n.path, size: n.size })),
229
280
  objects: (sf.objects || []).length,
230
281
  moc3: got.moc3 ? { bytes: got.moc3.length } : null,
231
282
  cubism2Moc: cubism2.count > 0 ? { count: cubism2.count, names: cubism2.names, version: cubism2.sample ? cubism2.sample.version : null } : null,
232
283
  reject,
233
284
  physics3: got.physics3 ? { bytes: got.physics3.length } : null,
234
285
  nativeFiles: (got.nativeFiles || []).map((f) => f.name),
286
+ // TextAsset 明细(名字 / 类型 / 骨架版本 / 字节数):只读分析接口要用它回答
287
+ // "这个包里有哪些骨架、各是什么版本",而预检本身只要"有没有"就够 —— 多给一层不增加解析成本。
288
+ textAssets: collectTextAssets(sf),
289
+ /**
290
+ * 列表显示名(契约 §1.3)。
291
+ *
292
+ * 取值顺序(各条都是"容器里的入口文件名去后缀",不认识业务语义、不编名字):
293
+ * ① `.skel` —— Spine 的入口
294
+ * ② `.model3.json`(TextAsset 形式)—— Cubism 4 的入口
295
+ * ③ `.model3.json`(原生文件形式,见 nativeFiles)
296
+ * ④ `.moc3`(原生文件形式)
297
+ * 多骨架时取**对象表顺序第一个**(本步不拼多个);都没有 ⇒ null(不是空串)。
298
+ */
299
+ displayName: (() => {
300
+ const assets = collectTextAssets(sf);
301
+ const skel = assets.find((t) => t && t.kind === 'skel');
302
+ if (skel && skel.name) return String(skel.name).replace(/\.skel$/i, '');
303
+ const model3Text = assets.find((t) => t && typeof t.name === 'string' && /\.model3\.json$/i.test(t.name));
304
+ if (model3Text) return String(model3Text.name).replace(/\.model3\.json$/i, '');
305
+ const natives = Array.isArray(got.nativeFiles)
306
+ ? got.nativeFiles.map((f) => f && f.name).filter((n) => typeof n === 'string' && n)
307
+ : [];
308
+ const model3Native = natives.find((n) => /\.model3\.json$/i.test(n));
309
+ if (model3Native) return String(model3Native).replace(/\.model3\.json$/i, '');
310
+ const moc3Native = natives.find((n) => /\.moc3$/i.test(n));
311
+ if (moc3Native) return String(moc3Native).replace(/\.moc3$/i, '');
312
+ return null;
313
+ })(),
235
314
  textures,
236
315
  // 动作数 = AnimationClip 对象数(与装载时的 motion 数同源,但不跑转换)
237
316
  motions: countByClass(sf, 'AnimationClip'),
@@ -2,8 +2,9 @@
2
2
  // plugins/unity-live2d-bundle/lib/texdecode.js —— 纹理格式分发
3
3
  //
4
4
  // 职责:读 Texture2D 的 m_TextureFormat,分发给对应解码器,返回 RGBA 字节。
5
- // · 已实现:ETC2_RGBA8(47) / ETC2_RGB(45) / ETC1_RGB4(34) / 未压缩 RGBA32/RGB24/Alpha8 等直通格式
6
- // · 未实现:ASTC / PVRTC / BCn 等 —— **抛可读错误**并把格式名与格式号带上,
5
+ // · 已实现:ASTC 全系列(48~59,解码器在 lib/astc.js) / ETC2_RGBA8(47) / ETC2_RGBA1(46)
6
+ // / ETC2_RGB(45) / ETC1_RGB4(34) / DXT1/3/4/5(10/12/26/27) / 未压缩 RGBA32/RGB24/Alpha8 等直通格式
7
+ // · 未实现:PVRTC / BC6H / BC7 等 —— **抛可读错误**并把格式名与格式号带上,
7
8
  // 绝不返回空白图或猜出来的图(否则上层会把"解码失败"当成"资源损坏")。
8
9
  //
9
10
  // 纹理尺寸与数据量的自检:按格式的块大小算出期望数据量,与给的字节数比对;
@@ -27,8 +28,24 @@
27
28
  7: { name: 'RGB565', block: 2, size: 1 },
28
29
  34: { name: 'ETC_RGB4', compress: 'etc1', block: 8, size: 4 },
29
30
  45: { name: 'ETC2_RGB', compress: 'etc2', block: 8, size: 4 },
31
+ 46: { name: 'ETC2_RGBA1', compress: 'etc2a1', block: 16, size: 4 },
30
32
  47: { name: 'ETC2_RGBA8', compress: 'etc2a8', block: 16, size: 4 },
31
- 48: { name: 'ETC2_RGBA1', compress: 'etc2a1', block: 16, size: 4 },
33
+ // 48~59 是 ASTC 系列(UnityPy/enums/TextureFormat.py;并被真实包的 m_CompleteImageSize
34
+ // 反推验证:440x440/87616 只有 6x6 唯一解、1024x1024/672400 只有 5x5)。
35
+ // **48 不是 ETC2_RGBA1**(那个是 46)。块边长各不相同,必须逐条写 bw/bh,
36
+ // 否则 expectedSize 会按 4x4 算错长度,把"格式未支持"错报成"数据不足"。
37
+ 48: { name: 'ASTC_RGB_4x4', block: 16, size: 4, bw: 4, bh: 4 },
38
+ 49: { name: 'ASTC_RGB_5x5', block: 16, size: 4, bw: 5, bh: 5 },
39
+ 50: { name: 'ASTC_RGB_6x6', block: 16, size: 4, bw: 6, bh: 6 },
40
+ 51: { name: 'ASTC_RGB_8x8', block: 16, size: 4, bw: 8, bh: 8 },
41
+ 52: { name: 'ASTC_RGB_10x10', block: 16, size: 4, bw: 10, bh: 10 },
42
+ 53: { name: 'ASTC_RGB_12x12', block: 16, size: 4, bw: 12, bh: 12 },
43
+ 54: { name: 'ASTC_RGBA_4x4', block: 16, size: 4, bw: 4, bh: 4 },
44
+ 55: { name: 'ASTC_RGBA_5x5', block: 16, size: 4, bw: 5, bh: 5 },
45
+ 56: { name: 'ASTC_RGBA_6x6', block: 16, size: 4, bw: 6, bh: 6 },
46
+ 57: { name: 'ASTC_RGBA_8x8', block: 16, size: 4, bw: 8, bh: 8 },
47
+ 58: { name: 'ASTC_RGBA_10x10', block: 16, size: 4, bw: 10, bh: 10 },
48
+ 59: { name: 'ASTC_RGBA_12x12', block: 16, size: 4, bw: 12, bh: 12 },
32
49
  // BC/DXT 家族(PC/主机资源包常用;块式压缩,解码实现见 lib/dxt.js)
33
50
  10: { name: 'DXT1', compress: 'bc1', block: 8, size: 4 },
34
51
  12: { name: 'DXT5', compress: 'bc3', block: 16, size: 4 },
@@ -46,8 +63,10 @@
46
63
  const f = FORMATS[fmt];
47
64
  if (!f) return null;
48
65
  if (f.size === 1) return width * height * f.block;
49
- const bw = Math.ceil(width / 4);
50
- const bh = Math.ceil(height / 4);
66
+ // 块边长按格式取(缺省 4):ETC/BC 全是 4x4 走缺省,ASTC 是 4~12 各不相同。
67
+ // 写死 4 会让 ASTC 算出偏大的期望长度 ⇒ 抛 TEX_DATA_SHORT,把"格式未支持"藏成"数据不足"。
68
+ const bw = Math.ceil(width / (f.bw || 4));
69
+ const bh = Math.ceil(height / (f.bh || 4));
51
70
  return bw * bh * f.block;
52
71
  }
53
72
 
@@ -128,19 +147,42 @@
128
147
  * 调用方**不得依赖**返回的 rgba 与入参共享底层 buffer(有的格式共享、有的不共享,
129
148
  * 原地修改它可能改到源数据、也可能改不到)。
130
149
  */
150
+ /**
151
+ * 生成"某个 ASTC 格式号"的解码器入口。
152
+ *
153
+ * 为什么要工厂而不是十二条手写箭头函数:块边长(4x4 ~ 12x12)只有 FORMATS 一处真相,
154
+ * 手抄一份数字到 DECODERS 就多了一处会漂移的副本 —— FORMATS 改了而这里没改时,
155
+ * 解码器会按错的块尺寸切块,表现为"图能出但整片错位",比直接报错难查得多。
156
+ */
157
+ const astcDecoder = (fmt) => (d, w, h) => {
158
+ const entry = FORMATS[fmt];
159
+ // astc.js 由文件清单在 texdecode.js 之前注入;缺了要说清是"没加载"而不是"格式不支持"
160
+ if (!NS.astc || typeof NS.astc.decodeAstc !== 'function') {
161
+ throw NS.errors.fail('ULB_ERR_TEX_DECODE',
162
+ `ASTC 解码器未加载(lib/astc.js 未按序注入?),无法解码 ${entry.name}(${fmt})`);
163
+ }
164
+ return NS.astc.decodeAstc(d, w, h, entry.bw, entry.bh);
165
+ };
166
+
131
167
  const DECODERS = {
132
168
  // 压缩格式:解码器自身产出 RGBA
133
169
  34: (d, w, h) => NS.etc2.decodeEtc1Rgb(d, w, h),
134
170
  45: (d, w, h) => NS.etc2.decodeEtc2Rgb(d, w, h),
135
171
  47: (d, w, h) => NS.etc2.decodeEtc2Rgba8(d, w, h),
136
- // 48 ETC2_RGBA1:ETC2 RGB 块(8 字节)+ 1bit alpha 平面(8 字节)。块布局与位序见
137
- // lib/etc2.js 的 decodeEtc2Rgba1 注释。此格式长期"格式表里有、解码器没登记",
138
- // 导致该格式的包整包报 ULB_ERR_TEX_DECODE(现场:蔚蓝档案部分 spinebackground 包)。
139
- 48: (d, w, h) => NS.etc2.decodeEtc2Rgba1(d, w, h),
172
+ // 46 ETC2_RGBA1:ETC2 RGB 块(8 字节)+ 1bit alpha 平面(8 字节)。块布局与位序见
173
+ // lib/etc2.js 的 decodeEtc2Rgba1 注释。
174
+ // 注意编号:ETC2_RGBA1 是 **46**,不是 48(48 是 ASTC_RGB_4x4,见 FORMATS 注释)。
175
+ 46: (d, w, h) => NS.etc2.decodeEtc2Rgba1(d, w, h),
140
176
  10: (d, w, h) => NS.dxt.decodeBc1(d, w, h),
141
177
  12: (d, w, h) => NS.dxt.decodeBc3(d, w, h),
142
178
  26: (d, w, h) => NS.dxt.decodeBc4(d, w, h),
143
179
  27: (d, w, h) => NS.dxt.decodeBc5(d, w, h),
180
+ // ASTC 全系列(48~53 无 alpha 变体 / 54~59 带 alpha 变体):同一个解码器,只有块边长不同。
181
+ // 块边长从 FORMATS 的 bw/bh 取(见 astcDecoder 的注释),这里只登记"哪个格式号归 ASTC"。
182
+ 48: astcDecoder(48), 49: astcDecoder(49), 50: astcDecoder(50),
183
+ 51: astcDecoder(51), 52: astcDecoder(52), 53: astcDecoder(53),
184
+ 54: astcDecoder(54), 55: astcDecoder(55), 56: astcDecoder(56),
185
+ 57: astcDecoder(57), 58: astcDecoder(58), 59: astcDecoder(59),
144
186
  // 未压缩格式:切片后按通道展开
145
187
  4: (d, w, h, need) => d.slice(0, need),
146
188
  5: (d, w, h, need) => argbToRgba(d.slice(0, need)),
@@ -159,12 +201,15 @@
159
201
  function decode(meta, data, opts) {
160
202
  const fmt = meta.format;
161
203
  const f = FORMATS[fmt];
162
- if (!f) throw NS.errors.fail('ULB_ERR_TEX_FORMAT_UNSUPPORTED', `暂不支持纹理格式 ${fmt}(${meta.name || ''})——已支持:ETC2_RGBA8(47)、ETC2_RGBA1(48)、ETC2_RGB(45)、ETC_RGB4(34)、DXT1(10)、DXT5(12)、BC4(26)、BC5(27)、RGBA32(4)、ARGB32(5)、RGB24(3)、Alpha8(1)、RGB565(7);暂未实现:ASTC(50 系列)、PVRTC(30/31)、BC6H(24)、BC7(25)`);
204
+ if (!f) throw NS.errors.fail('ULB_ERR_TEX_FORMAT_UNSUPPORTED', `暂不支持纹理格式 ${fmt}(${meta.name || ''})——已支持:ETC2_RGBA8(47)、ETC2_RGBA1(46)、ETC2_RGB(45)、ETC_RGB4(34)、DXT1(10)、DXT5(12)、BC4(26)、BC5(27)、ASTC 全系列(48~59)、RGBA32(4)、ARGB32(5)、RGB24(3)、Alpha8(1)、RGB565(7);未实现:PVRTC(30~33)、BC6H(24)、BC7(25)`);
205
+ // 解码器检查放在长度检查**之前**:表里有、但没登记解码器(PVRTC / BC6H / BC7 这类)
206
+ // 属于"格式未支持",不是"数据不足"。顺序反了会用尺寸错误掩盖真实原因。
207
+ const decoder = DECODERS[fmt];
208
+ if (!decoder) {
209
+ throw NS.errors.fail('ULB_ERR_TEX_FORMAT_UNSUPPORTED', `纹理格式 ${f.name}(${fmt})尚未实现解码器——当前未接入的格式只有 PVRTC / BC6H / BC7`);
210
+ }
163
211
  const need = expectedSize(fmt, meta.width, meta.height);
164
212
  if (data.length < need) throw NS.errors.fail('ULB_ERR_TEX_DATA_SHORT', `纹理 ${meta.name || ''}(${f.name} ${meta.width}x${meta.height})数据不足:需要 ${need} 字节,实际 ${data.length}`);
165
- // 表里有格式、但没有登记解码器(如 ASTC 50 系列)⇒ 继续走这条兜底
166
- const decoder = DECODERS[fmt];
167
- if (!decoder) throw NS.errors.fail('ULB_ERR_TEX_DECODE', `纹理格式 ${f.name} 的分发分支缺失`);
168
213
  const rgba = decoder(data, meta.width, meta.height, need);
169
214
  // 统一翻转:原实现用 finish() 包住每个 return,等价于在这里一次做完
170
215
  return !opts || opts.flipY !== false ? flipY(rgba, meta.width, meta.height) : rgba;
@@ -174,8 +219,8 @@
174
219
  * 某个 Unity TextureFormat 能否**真正解码**。
175
220
  *
176
221
  * 为什么需要它:FORMATS 是"格式字典"(名称 / 尺寸 / block 布局),**不代表能解码** ——
177
- * 例如 ASTC(50 系列) 在 FORMATS 里可能有条目、但 DECODERS 没登记,decode() 会抛
178
- * ULB_ERR_TEX_DECODE。若外部(预检 / 面板提示)拿 `!!FORMATS[fmt]` 当"支持",
222
+ * 例如 PVRTC 可能在 FORMATS 里有条目、但 DECODERS 没登记,decode() 会抛
223
+ * ULB_ERR_TEX_FORMAT_UNSUPPORTED。若外部(预检 / 面板提示)拿 `!!FORMATS[fmt]` 当"支持",
179
224
  * 就会先报"支持"、真正解包时才炸 —— 同一件事两套判据。
180
225
  * ⇒ 能力判定只认这张表:**这是"能不能解码"的唯一真相**。
181
226
  *
@@ -2,7 +2,8 @@
2
2
  "id": "unity-live2d-bundle",
3
3
  "name": "Unity Live2D 资源包",
4
4
  "provides": [
5
- "asset-provider"
5
+ "asset-provider",
6
+ "bundle-analyzer"
6
7
  ],
7
8
  "label": "Live2D包",
8
9
  "accent": "#6d28d9",
@@ -68,6 +69,7 @@
68
69
  "lib/extract.js",
69
70
  "lib/etc2.js",
70
71
  "lib/dxt.js",
72
+ "lib/astc.js",
71
73
  "lib/texdecode.js",
72
74
  "lib/bundle-probe.js",
73
75
  "lib/png.js",
@@ -125,8 +125,9 @@ function install() {
125
125
  ? fields.m_Name.text : String(fields.m_Name || '');
126
126
  const raw = fields.m_Script && fields.m_Script.bytes;
127
127
  if (!raw) continue;
128
- if (/\.skel$/i.test(name)) skel.push({ name, bytes: raw });
129
- else if (/\.atlas$/i.test(name)) atlas.push({ name, bytes: raw });
128
+ // 带上 pathID:同名的资源靠它区分(m_Name 不保证唯一,实测单包有 2 份同名 .skel / .atlas)
129
+ if (/\.skel$/i.test(name)) skel.push({ name, bytes: raw, pathId: String(obj.pathID) });
130
+ else if (/\.atlas$/i.test(name)) atlas.push({ name, bytes: raw, pathId: String(obj.pathID) });
130
131
  }
131
132
  return { skel, atlas };
132
133
  }
@@ -150,6 +151,118 @@ function install() {
150
151
  return pages;
151
152
  }
152
153
 
154
+ /**
155
+ * 与 parseAtlasPages 同源,但**连每页声明的尺寸一起返回**:`[{page, w, h}]`。
156
+ *
157
+ * 为什么不改 parseAtlasPages 本身:它被单测与 `__test` 直接调用(返回值是纯页名数组)。
158
+ * 新增函数而不是改签名 ⇒ 现有调用方零影响。
159
+ *
160
+ * 为什么需要尺寸:同角色多套外观会有**同名不同尺寸**的贴图(实测两份 char_010_chen:
161
+ * 256x256 与 512x512),而两份 atlas 也都声明页名 char_010_chen.png。只按名字对号
162
+ * ⇒ 把 256² 的图贴到按 512² 算的 region 坐标上 ⇒ 错位/缺图。
163
+ */
164
+ function parseAtlasPagesDetailed(atlasBytes) {
165
+ const text = new TextDecoder('utf-8').decode(atlasBytes).replace(/^\uFEFF/, '');
166
+ const out = [];
167
+ let cur = null;
168
+ for (const raw of text.split('\n')) {
169
+ const line = raw.replace(/\r$/, '');
170
+ const trimmed = line.trim();
171
+ if (!trimmed) continue;
172
+ if (/^size\s*:/i.test(trimmed)) {
173
+ if (cur && cur.w == null) {
174
+ const parts = trimmed.split(':')[1].split(',');
175
+ const w = parseInt(parts[0], 10);
176
+ const h = parseInt(parts[1], 10);
177
+ if (Number.isFinite(w) && Number.isFinite(h)) { cur.w = w; cur.h = h; }
178
+ }
179
+ continue;
180
+ }
181
+ // 行首无空白 + 以图片扩展名结尾 ⇒ 页名行(与 parseAtlasPages 同判据)
182
+ if (/^[^\s].*\.(png|jpg|jpeg|webp)\s*$/i.test(line)) {
183
+ cur = { page: trimmed, w: null, h: null };
184
+ out.push(cur);
185
+ }
186
+ }
187
+ return out;
188
+ }
189
+
190
+ /**
191
+ * 同名资源的键名:`<stem>__<pathId><ext>`(与 Python worker 同口径)。
192
+ *
193
+ * 为什么需要:`m_Name` 不保证唯一。用名字当 Map 键时后写的会覆盖先写的,
194
+ * 覆盖后 entry 里写的名字可能指向**另一套**的内容 ⇒ 前端报 Region not found。
195
+ * 为什么用 pathID 而不是序号:pathID 在同一 serialized file 内天然唯一,
196
+ * 与对象遍历顺序无关 ⇒ 同一包两次导出结果一致(可复现)。
197
+ */
198
+ function disambiguateKey(name, pathId) {
199
+ const dot = String(name).lastIndexOf('.');
200
+ const stem = dot > 0 ? String(name).slice(0, dot) : String(name);
201
+ const ext = dot > 0 ? String(name).slice(dot) : '';
202
+ return `${stem}__${pathId}${ext}`;
203
+ }
204
+
205
+ /**
206
+ * 按 **region 命中率** 选与 skel 匹配的 atlas(与 worker 的 _pick_atlas_for_skel 同判据)。
207
+ *
208
+ * 为什么不能按名字配对:同名覆盖下 `char_010_chen.skel` 与 `char_010_chen.atlas`
209
+ * 名字永远对得上,哪怕它们来自不同的一套。只有"atlas 的 region 名能否在 skel 的
210
+ * string table 里找到"才是内容层面的真判据。
211
+ *
212
+ * 为什么不能要求唯一命中:同一角色的多套外观会**共享一部分 region 名**(实测 1~2 个噪声),
213
+ * 所以取命中数最高者;并列时按 atlas 在包内的先后取第一个(稳定,不依赖遍历细节)。
214
+ */
215
+ function pickAtlasForSkel(skelBytes, atlases) {
216
+ // ⚠️ 必须先把 skel 的字节**当成字符串**再查子串:
217
+ // `Uint8Array.prototype.includes()` 是"数组里有没有这个**字节值**",
218
+ // 传一个 Uint8Array 进去永远 false —— 那会让命中率恒为 0、配对悄悄退化成"取第一个"。
219
+ // latin1 解码是 1:1 字节映射,所以 latin1 字符串的 includes 等价于字节子串查找。
220
+ const haystack = new TextDecoder('latin1').decode(skelBytes);
221
+ let best = null;
222
+ let bestHits = -1;
223
+ for (const a of atlases) {
224
+ let hits = 0;
225
+ // region 名(行首无缩进、非属性、非页名)才是 skel string table 里的东西;
226
+ // 页名带 `.png` 后缀,skel 里不会有,不参与统计。
227
+ for (const region of atlasRegions(a.bytes)) {
228
+ if (region && haystack.includes(region)) hits += 1;
229
+ }
230
+ if (hits > bestHits) { best = a; bestHits = hits; }
231
+ }
232
+ return best || (atlases[0] || null);
233
+ }
234
+
235
+ /** atlas 里的 region 名:行首无缩进、非属性行、非页名行(与 worker 的 _atlas_regions 同判据)。 */
236
+ function atlasRegions(atlasBytes) {
237
+ const text = new TextDecoder('utf-8').decode(atlasBytes).replace(/^\uFEFF/, '');
238
+ const out = [];
239
+ for (const raw of text.split('\n')) {
240
+ const line = raw.replace(/\r$/, '');
241
+ const trimmed = line.trim();
242
+ if (!trimmed) continue;
243
+ if (line[0] === ' ' || line[0] === '\t') continue; // 属性行
244
+ if (/^(size|format|filter|repeat|pma|scale)\s*:/i.test(trimmed)) continue;
245
+ if (/\.(png|jpg|jpeg|webp)\s*$/i.test(trimmed)) continue; // 页名行
246
+ out.push(trimmed);
247
+ }
248
+ return out;
249
+ }
250
+
251
+ /**
252
+ * 按名字 + **尺寸**找纹理(与 worker 的 _find_texture 同判据):
253
+ * 尺寸匹配优先,对不上才回落同名第一份。
254
+ */
255
+ function findTextureForPage(textures, pageStem, w, h) {
256
+ const cands = textures.filter((t) => String(t.name) === pageStem
257
+ || String(t.name).toLowerCase() === pageStem.toLowerCase());
258
+ if (!cands.length) return null;
259
+ if (w && h) {
260
+ const exact = cands.find((t) => t.width === w && t.height === h);
261
+ if (exact) return exact;
262
+ }
263
+ return cands[0];
264
+ }
265
+
153
266
  /**
154
267
  * 读 spine 二进制头部的版本串(形如 "4.1.20"),用于自检与错误信息。
155
268
  * .skel 头 8 字节是 hash,随后是版本串(本项目样本里是明文 ASCII)。
@@ -297,9 +410,12 @@ function install() {
297
410
 
298
411
  // 按「去扩展名的同名」配对(c200_01.skel ↔ c200_01.atlas);配不上就退第一个,不猜内容
299
412
  const stemOf = (n) => String(n).replace(/\.[^.\\/]+$/, '');
300
- const skelPick = skel[0];
301
- const stem = stemOf(skelPick.name);
302
- const atlasPick = atlas.find((a) => stemOf(a.name) === stem) || atlas[0];
413
+ // 选**数据量最大**的骨架作主模型(与 worker 的 max(skels, key=字节数) 同判据)。
414
+ // 为什么不能取第一个:一个包里可能有多套外观(实测 char_010_chen 与 build_char_010_chen),
415
+ // 取第一个取决于对象序,换版本就可能变。主角色的数据量通常远大于附属,这条判据不依赖命名。
416
+ const skelPick = skel.reduce((a, b) => ((b.bytes && b.bytes.length) > (a.bytes && a.bytes.length) ? b : a), skel[0]);
417
+ // 配对按 **region 命中率**,不按 stem 同名(同名永远对得上,却可能跨套)。
418
+ const atlasPick = pickAtlasForSkel(skelPick.bytes, atlas);
303
419
  if (!atlasPick) throw NS.errors.fail(NS.errors.CODES.NO_ATLAS, `有 ${skelPick.name} 但包里没有 .atlas`);
304
420
 
305
421
  // 版本串读不到只记录不失败:不同 Spine 版本的头部布局可能不同,硬失败会误拒可用的包
@@ -307,16 +423,17 @@ function install() {
307
423
  if (!spineVersion) ulbLog('warn', `.skel 头部未识别出 Spine 版本串(${skelPick.name}),继续解包`);
308
424
 
309
425
  // —— 纹理:按 atlas 页名找同名 Texture2D,解码后编 PNG ——
310
- const pages = parseAtlasPages(atlasPick.bytes);
426
+ const pages = parseAtlasPagesDetailed(atlasPick.bytes);
311
427
  if (!pages.length) throw NS.errors.fail(NS.errors.CODES.NO_ATLAS, 'atlas 里没有解析到贴图页名');
312
428
  const allTextures = NS.extract.findTextures(sf);
313
429
 
314
430
  const files = new Map();
315
431
  const textureFiles = [];
316
- for (const page of pages) {
432
+ for (const pg of pages) {
433
+ const page = pg.page;
317
434
  const pageStem = stemOf(page);
318
- const tex = allTextures.find((t) => t.name === pageStem)
319
- || allTextures.find((t) => String(t.name).toLowerCase() === pageStem.toLowerCase());
435
+ // 按 **atlas 声明的尺寸** 找同名纹理(尺寸是这几者唯一能互相印证的东西)
436
+ const tex = findTextureForPage(allTextures, pageStem, pg.w, pg.h);
320
437
  if (!tex) {
321
438
  throw NS.errors.fail(NS.errors.CODES.EXTRACT,
322
439
  `atlas 声明了贴图页「${page}」,但包里找不到同名 Texture2D(${pageStem})`);
@@ -340,14 +457,32 @@ function install() {
340
457
  textureFiles.push(page);
341
458
  }
342
459
 
343
- // —— 骨架 / 图集(原始字节,绝不走字符串回编码)——
344
- files.set(skelPick.name, new Blob([skelPick.bytes], { type: 'application/octet-stream' }));
345
- files.set(atlasPick.name, new Blob([atlasPick.bytes], { type: 'application/octet-stream' }));
460
+ // —— 骨架 / 图集:**全部**同名资源都进表,不再互相覆盖 ——
461
+ //
462
+ // `Map.set` 用 name 当键时,同名的后一份会把前一份覆盖掉:实测本包有 2 份
463
+ // `char_010_chen.skel`(328066 / 114901)与 2 份 `char_010_chen.atlas`(3969 / 6703)。
464
+ // 一旦覆盖,entry 里写的名字可能指向**另一套**的内容 ⇒ atlas 的 region 在 skel
465
+ // 里一个都找不到 ⇒ 前端报 "Region not found in atlas"。
466
+ // 同名时按 `<stem>__<pathId><ext>` 区分(与 Python worker 完全同口径:pathID 天然唯一、
467
+ // 与对象遍历顺序无关,所以可复现)。
468
+ const allTextAssets = [...skel, ...atlas];
469
+ const nameCount = new Map();
470
+ for (const t of allTextAssets) nameCount.set(t.name, (nameCount.get(t.name) || 0) + 1);
471
+ const fileKeyOf = (t) => {
472
+ if (!t || !t.name) return null;
473
+ // 只出现一次 ⇒ 保持原名(零回归:单套资源的包产物与以前完全一样)
474
+ if ((nameCount.get(t.name) || 1) <= 1) return t.name;
475
+ return disambiguateKey(t.name, t.pathId);
476
+ };
477
+ for (const t of allTextAssets) {
478
+ const key = fileKeyOf(t);
479
+ if (key && !files.has(key)) files.set(key, new Blob([t.bytes], { type: 'application/octet-stream' }));
480
+ }
346
481
 
347
482
  // —— 入口清单:字段名与前端约定一致(skeletonFile / atlasFile / textureFiles)——
348
483
  const entryJson = {
349
- skeletonFile: skelPick.name,
350
- atlasFile: atlasPick.name,
484
+ skeletonFile: fileKeyOf(skelPick),
485
+ atlasFile: fileKeyOf(atlasPick),
351
486
  textureFiles,
352
487
  };
353
488
  const entryBytes = new TextEncoder().encode(JSON.stringify(entryJson, null, 2));
@@ -498,6 +633,41 @@ function install() {
498
633
  }
499
634
  }
500
635
 
636
+ // 契约 §1.1 bundle-analyzer:只读分析容器字节流。
637
+ // 实现直接复用 lib/bundle-probe.js(与本插件同目录的那份,与 live2d 插件的逐字节相同),
638
+ // 这里只做"事实 → 契约字段"的映射。verdict 不在这里算:那是核心的判据(src/shared/bundle-shape.mjs)。
639
+ function analyze(bytes) {
640
+ const bp = NS && NS.bundleProbe;
641
+ if (!bp || typeof bp.run !== 'function') {
642
+ return {
643
+ container: null,
644
+ objects: null,
645
+ textures: null,
646
+ warnings: ['插件内缺少 bundle-probe 实现(lib/bundle-probe.js 未加载)'],
647
+ };
648
+ }
649
+ const res = bp.run(bytes) || {};
650
+ return {
651
+ // 契约字段
652
+ container: res.container || null,
653
+ objects: typeof res.objects === 'number' ? res.objects : null,
654
+ textures: Array.isArray(res.textures) ? res.textures : null,
655
+ // 契约 §1.1 的 textAssets:名字 / 类型 / 骨架版本 / 字节数,由 lib/bundle-probe.js 从对象表取出
656
+ textAssets: Array.isArray(res.textAssets) ? res.textAssets : null,
657
+ warnings: Array.isArray(res.warnings) ? res.warnings : [],
658
+ // 透传既有字段(与 live2d 插件同一套键,便于核心与诊断两端同口径)
659
+ ok: res.ok,
660
+ code: res.code,
661
+ error: res.error,
662
+ moc3: res.moc3 || null,
663
+ cubism2Moc: res.cubism2Moc || null,
664
+ physics3: res.physics3 || null,
665
+ motions: res.motions,
666
+ reject: res.reject || null,
667
+ nativeFiles: res.nativeFiles,
668
+ };
669
+ }
670
+
501
671
  if (host) {
502
672
  host.register({
503
673
  id: PLUGIN_ID,
@@ -507,6 +677,8 @@ function install() {
507
677
  resolve,
508
678
  // 契约 §2.2:让宿主与检查器都能发现"本插件能自己算内存代价"
509
679
  estimateMemory,
680
+ // 契约 §1.1 bundle-analyzer:只读分析容器字节流(不产模型、不写文件)
681
+ analyze,
510
682
  // kind 的唯一权威来源是 plugin.json 的 produces.kind(契约 §3.2),这里带上只为调试可读。
511
683
  kind: 'spine',
512
684
  fileKinds: ['bundle'],
@@ -516,7 +688,7 @@ function install() {
516
688
  // node 侧返回解析入口:服务端解包直接从返回值取 canHandle/resolve,不经过宿主注册。
517
689
  // __test 只给单测用(纯函数、无副作用、宿主不读它):parseAtlasPages 是闭包内私有函数,
518
690
  // 没有它就无法为"带 BOM 的 atlas 首行"补单测(这正是 NIKKE 根 34 份带 BOM 的 atlas 丢第一页贴图的根因)。
519
- return { canHandle, resolve, estimateMemory, probeContainer, PLUGIN_ID, __test: { parseAtlasPages } };
691
+ return { canHandle, resolve, estimateMemory, probeContainer, analyze, PLUGIN_ID, __test: { parseAtlasPages, parseAtlasPagesDetailed, pickAtlasForSkel, atlasRegions, findTextureForPage, disambiguateKey } };
520
692
  }
521
693
 
522
694
  // 执行安装。为什么不用 `export { canHandle }`:那会把文件变成 ESM,