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
@@ -0,0 +1,323 @@
1
+ // ============================================================================
2
+ // lib/bundle-probe.js —— 资源包「装载前预检」(只做便宜的事)
3
+ //
4
+ // 目的:不装载、不解码纹理像素,就能回答"这个资源包能不能装 / 装起来会缺什么"。
5
+ // 预检内容:容器头 → 对象表 → moc3/physics3 存在性 → 纹理格式是否可解码 → 动画片段数。
6
+ //
7
+ // 为什么放在 lib 而不是插件/服务端各写一份:
8
+ // 浏览器侧(插件的 probe)与 Node 侧(/api/bundle/probe)必须给出**同一口径**的结论,
9
+ // 否则"预检说能装、装载却失败"这类漂移会反复出现。这里只依赖 NS.* 里已有的库,
10
+ // 两端各自加载同一份文件即可。
11
+ //
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。
17
+ // ============================================================================
18
+
19
+ 'use strict';
20
+
21
+ {
22
+ const NS = (globalThis.__ULB__ = globalThis.__ULB__ || {});
23
+
24
+ // 已知但暂未实现的格式:预检要让调用方看到"ASTC"而不是"未知格式 50",
25
+ // 这样才能据此判断"是这个资源用了新压缩格式"而不是"文件坏了"。
26
+ const KNOWN_UNSUPPORTED = {
27
+ 24: 'BC6H', 25: 'BC7',
28
+ 30: 'PVRTC_RGB2', 31: 'PVRTC_RGBA2', 32: 'PVRTC_RGB4', 33: 'PVRTC_RGBA4',
29
+ 50: 'ASTC_RGBA', 51: 'ASTC_RGBA_HDR', 52: 'ASTC_RGB', 53: 'ASTC_RGB_HDR',
30
+ };
31
+
32
+ /**
33
+ * 查某个 Unity TextureFormat 是否**可解码**。
34
+ *
35
+ * 判据只有一个:`texdecode.canDecode(fmt)`(内部只认 DECODERS 表)。
36
+ * 为什么不再用 `!!FORMATS[fmt]`:FORMATS 是格式字典(名称/尺寸/block),含 48 ETC2_RGBA1
37
+ * 这种"有条目但从未实现解码"的格式 ⇒ 预检会报"支持"、真正解包才抛 ULB_ERR_TEX_DECODE,
38
+ * 同一件事两套判据。改名/尺寸仍走 FORMATS,但"能不能解"不再由它决定。
39
+ */
40
+ function formatSupport(fmt) {
41
+ const table = NS.texdecode && NS.texdecode.FORMATS ? NS.texdecode.FORMATS : null;
42
+ const entry = table ? table[fmt] : null;
43
+ const raw = NS.texdecode && NS.texdecode.formatName ? NS.texdecode.formatName(fmt) : `未知格式 ${fmt}`;
44
+ const name = entry ? raw : (KNOWN_UNSUPPORTED[fmt] || raw);
45
+ const canDecode = NS.texdecode && typeof NS.texdecode.canDecode === 'function'
46
+ ? NS.texdecode.canDecode(fmt)
47
+ : false; // 缺 canDecode 时按"不可解码"处理:宁可早拒绝,也不要报"支持"却在解包时炸
48
+ return { supported: canDecode, formatName: name };
49
+ }
50
+
51
+ /** 从序列化文件里按类名统计对象数(避免为了数动作而跑一遍动画转换) */
52
+ function countByClass(sf, className) {
53
+ try {
54
+ const stats = NS.serialized.typeStats(sf);
55
+ if (!stats) return null;
56
+ for (const k of Object.keys(stats)) {
57
+ if (k.toLowerCase() === String(className).toLowerCase()) return stats[k];
58
+ }
59
+ return 0;
60
+ } catch {
61
+ return null;
62
+ }
63
+ }
64
+
65
+ // Cubism 2.1 运行时数据 .moc 的魔数:**小写三字节** 'm' 'o' 'c',紧随其后的第 4 字节是
66
+ // **文件版本号**(不是第四位魔数)。取证来源:Cubism 2 官方运行时 live2d.min.js 的
67
+ // loadModel() 里逐字节校验 `109 && 111 && 99`,随后把下一字节当 version 与 SDK 版本比较。
68
+ // ⇒ 判据**不能**写成 "moc\0"(第 4 字节是版本号,通常非 0)。
69
+ const CUBISM2_MOC_MAGIC = [0x6d, 0x6f, 0x63];
70
+
71
+ /**
72
+ * 在这个包里找 Cubism 2.1 的 .moc 运行时数据(**只判定存在性,不解析**)。
73
+ *
74
+ * 为什么要它:.moc 是 Cubism 2.1 的模型二进制,Cubism 3+ 的 SDK/运行时**完全不支持**它
75
+ * (官方帮助文档明说不支持 .moc/.mtn)。这类包既没有 moc3、也没有 .skel,两个 asset-provider
76
+ * 都不会认领 ⇒ 用户在界面上只看到一句"插件判定不处理该文件",无法判断是自己资源有问题、
77
+ * 还是格式超出支持范围。本函数就是为了让上层能说清"这是什么、为什么不行、你能做什么"。
78
+ *
79
+ * 判据(**宁缺勿错**,三条全满足才算命中):
80
+ * ① MonoBehaviour(class 114)的字节里出现 'moc' 魔数;
81
+ * ② 魔数前 4 字节(u32)是有意义的长度 L,且 `魔数起点 + L === 对象末尾`
82
+ * —— 与 findMoc3 同款自洽校验,把"随机字节里凑巧出现 moc"挡掉;
83
+ * ③ 魔数后第 4 字节是本文件的版本号,落在合理区间 1..16。
84
+ * 扩展名不参与判定(只用来在文案里报出文件名,供用户核对)。
85
+ *
86
+ * @param {object} sf SerializedFile(NS.serialized.parse 的结果)
87
+ * @returns {{count:number, names:string[], sample:{object:object, magicOffset:number, version:number, bytes:number}|null}}
88
+ */
89
+ function findCubism2Moc(sf) {
90
+ const out = { count: 0, names: [], sample: null };
91
+ if (!sf || !NS.serialized || !NS.extract) return out;
92
+ let monos;
93
+ try {
94
+ monos = NS.serialized.byClass(sf, 114);
95
+ } catch {
96
+ return out; // 对象表读不出来就当没发现(宁可漏报,也不把别的包误判成 Cubism 2)
97
+ }
98
+ // 第一路(主判据):MonoBehaviour 内嵌字节里的魔数 + 前置长度自洽 + 版本合理
99
+ for (const obj of monos) {
100
+ const bytes = sf.bytes.subarray(obj.dataOffset, obj.dataOffset + obj.byteSize);
101
+ let from = 0;
102
+ for (;;) {
103
+ const at = NS.extract.indexOfMagic(bytes, CUBISM2_MOC_MAGIC, from);
104
+ if (at < 0) break;
105
+ from = at + 1;
106
+ // ② 前置长度自洽:魔数前 4 字节是小端长度,且「魔数起点 + L」恰好到对象末尾
107
+ // (与 findMoc3 同款;这一条把"随机字节里凑巧出现 moc"挡掉)
108
+ if (at < 4) continue;
109
+ const len = bytes[at - 4] | (bytes[at - 3] << 8) | (bytes[at - 2] << 16) | (bytes[at - 1] << 24);
110
+ if (!(len > 16) || at + len !== bytes.length) continue;
111
+ // ③ 版本号合理(Cubism 2.1 的 .moc 版本是个小整数,见魔数注释里的取证)
112
+ const end = at + 3;
113
+ if (end >= bytes.length) continue;
114
+ const version = bytes[end];
115
+ if (version < 1 || version > 16) continue;
116
+ out.count += 1;
117
+ const name = readObjectName(sf, obj);
118
+ if (name) out.names.push(name);
119
+ if (!out.sample) out.sample = { object: obj, magicOffset: at, version, bytes: len, by: 'magic' };
120
+ break; // 一个对象里只认第一处(同一个 .moc 不会被切成两段)
121
+ }
122
+ }
123
+ // 第二路(兜底,只作辅助):TextAsset 的名字以 .moc 结尾。
124
+ // 为什么要这一路:真 .moc 在包里的形态**没有样本可验**(当前资源 0 个)。
125
+ // 若它是以 TextAsset/独立文件形式存在(而非仿真 moc3 的内嵌 TypelessData),
126
+ // 就不存在"前置 u32 长度",第一路会漏报。此时扩展名是唯一线索。
127
+ // 安全性:本函数只在 run() 里"已确认没有 moc3"之后才被调用,所以不会把 moc3 包误判成 Cubism 2。
128
+ if (out.count === 0) {
129
+ try {
130
+ for (const ta of NS.extract.findTextAssets(sf)) {
131
+ const nm = ta && ta.name ? String(ta.name) : '';
132
+ if (!/\.moc$/i.test(nm)) continue;
133
+ out.count += 1;
134
+ out.names.push(nm);
135
+ if (!out.sample) out.sample = { object: ta.object, magicOffset: -1, version: null, bytes: ta.bytes ? ta.bytes.length : null, by: 'name' };
136
+ break;
137
+ }
138
+ } catch { /* 类型树读不出来就不兜底,不影响主判据 */ }
139
+ }
140
+ return out;
141
+ }
142
+
143
+ /** 读对象名(读不出来返回空串;名字只用于文案,不参与判定) */
144
+ function readObjectName(sf, obj) {
145
+ try {
146
+ const fields = NS.typetree ? NS.typetree.readObject(sf, obj) : null;
147
+ return (fields && (fields.m_Name || fields.m_PathName)) || '';
148
+ } catch {
149
+ return '';
150
+ }
151
+ }
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
+
197
+ /**
198
+ * 预检一个资源包。
199
+ * @param {Uint8Array} bytes 资源包字节
200
+ * @returns {object} { ok, code?, error?, container?, objects?, moc3?, physics3?, textures?, motions? }
201
+ */
202
+ function run(bytes) {
203
+ const E = NS.errors;
204
+ if (!bytes || !bytes.length) {
205
+ return { ok: false, code: E.CODES.NO_FILE, error: '资源包为空或不存在' };
206
+ }
207
+ // 容器头:只看签名,不合格直接定性(不必进解析)
208
+ let sig = '';
209
+ for (let i = 0; i < Math.min(7, bytes.length); i++) sig += String.fromCharCode(bytes[i]);
210
+ if (sig !== 'UnityFS') {
211
+ const shown = sig.replace(/[^\x20-\x7e]/g, '?');
212
+ return { ok: false, code: E.CODES.NOT_UNITYFS, error: `不是 UnityFS 资源包(signature="${shown}")` };
213
+ }
214
+
215
+ let parsed;
216
+ let sf;
217
+ let got;
218
+ try {
219
+ parsed = NS.unityfs.parse(bytes);
220
+ // 结构判据(不是名字前缀):CAB- + 32 位十六进制、无后缀。
221
+ // .resS / .resource 也以 CAB- 开头,但都不是 serialized file(后者是 FSB5 音频银行)。
222
+ const cab = parsed.nodes.find((n) => /^CAB-[0-9a-f]{32}$/i.test(n.path));
223
+ if (!cab) {
224
+ return { ok: false, code: E.CODES.NO_CAB, error: '资源包里没有 CAB-* 节点(不是资产包)' };
225
+ }
226
+ const resSNode = parsed.nodes.find((n) => /\.resS$/.test(n.path));
227
+ const cabBytes = NS.unityfs.nodeBytes(parsed, cab.path);
228
+ const resSBytes = resSNode ? NS.unityfs.nodeBytes(parsed, resSNode.path) : null;
229
+ sf = NS.serialized.parse(cabBytes);
230
+ got = NS.extract.extractAll({ cabBytes, resSBytes, sf });
231
+ } catch (e) {
232
+ return { ok: false, code: E.CODES.CONTAINER_PARSE, error: e.message };
233
+ }
234
+
235
+ // 纹理:只判定格式是否可解码,**不解码像素**(解码是最贵的一步,预检故意跳过)
236
+ const textures = (got.textures || []).map((t) => {
237
+ const s = formatSupport(t.format);
238
+ return {
239
+ name: t.name,
240
+ width: t.width,
241
+ height: t.height,
242
+ format: t.format,
243
+ formatName: s.formatName,
244
+ supported: s.supported,
245
+ };
246
+ });
247
+ const unsupported = textures.filter((t) => !t.supported).map((t) => t.formatName);
248
+
249
+ // Cubism 2.1 的 .moc 存在性:**只有"没有 moc3"时才有意义**(有 moc3 的包一律按 moc3 走,
250
+ // 哪怕字节里凑巧有 'moc' 片段也不该被说成 Cubism 2 —— 这是判据可靠性的关键)
251
+ const moc3Hit = !!got.moc3;
252
+ const cubism2 = moc3Hit ? { count: 0, names: [], sample: null } : findCubism2Moc(sf);
253
+ // 结构化"为什么没人认领",交给上层(宿主/调试日志)原样透出给用户
254
+ let reject = null;
255
+ if (!moc3Hit && cubism2.count > 0) {
256
+ const ver = cubism2.sample ? cubism2.sample.version : null;
257
+ const fileHint = cubism2.names.length ? `(${cubism2.names.join('、')})` : '';
258
+ reject = {
259
+ code: 'LIVE2D_CUBISM2_MOC_UNSUPPORTED',
260
+ // 三句话缺一不可:这是什么 / 为什么不行 / 用户能做什么
261
+ message: `这是 Cubism 2.1 的 .moc 模型数据${fileHint}${ver ? `,版本 ${ver}` : ''}。`
262
+ + 'Cubism 3 及以后的 SDK/运行时已不支持 .moc 与 .mtn 这两种旧格式,本页面的 Live2D 渲染只支持 .moc3。'
263
+ + '若你手上有该模型的 .cmox 工程文件,可用官方 Cubism Editor 打开并转换为 .moc3 后再放进来;'
264
+ + '如果是从游戏包里直接拆出的 .moc,则没有可用的转换途径(需要原作者提供工程文件)。',
265
+ };
266
+ }
267
+
268
+ return {
269
+ ok: true,
270
+ container: {
271
+ version: parsed.version,
272
+ engine: parsed.engineVersion,
273
+ player: parsed.playerVersion,
274
+ nodes: parsed.nodes.length,
275
+ bytes: bytes.length,
276
+ },
277
+ // 节点列表(path + size):只读分析接口要逐条列出来回答"包里有什么"。
278
+ // 预检自己只用 nodes 的**个数**,多给一层列表不增加解析成本。
279
+ nodes: parsed.nodes.map((n) => ({ path: n.path, size: n.size })),
280
+ objects: (sf.objects || []).length,
281
+ moc3: got.moc3 ? { bytes: got.moc3.length } : null,
282
+ cubism2Moc: cubism2.count > 0 ? { count: cubism2.count, names: cubism2.names, version: cubism2.sample ? cubism2.sample.version : null } : null,
283
+ reject,
284
+ physics3: got.physics3 ? { bytes: got.physics3.length } : null,
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
+ })(),
314
+ textures,
315
+ // 动作数 = AnimationClip 对象数(与装载时的 motion 数同源,但不跑转换)
316
+ motions: countByClass(sf, 'AnimationClip'),
317
+ // 有纹理格式解不了时不算整体失败,只把结论带出来,交给调用方决定
318
+ warnings: unsupported.length ? [`有纹理格式暂不支持:${[...new Set(unsupported)].join('、')}`] : [],
319
+ };
320
+ }
321
+
322
+ NS.bundleProbe = { run, formatSupport, findCubism2Moc };
323
+ }
@@ -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-spine-bundle",
3
3
  "name": "Unity Spine 资源包",
4
4
  "provides": [
5
- "asset-provider"
5
+ "asset-provider",
6
+ "bundle-analyzer"
6
7
  ],
7
8
  "label": "Spine包",
8
9
  "accent": "#2b6cb0",
@@ -68,7 +69,9 @@
68
69
  "lib/extract.js",
69
70
  "lib/etc2.js",
70
71
  "lib/dxt.js",
72
+ "lib/astc.js",
71
73
  "lib/texdecode.js",
74
+ "lib/bundle-probe.js",
72
75
  "lib/png.js",
73
76
  "index.js"
74
77
  ]