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.
- package/CHANGELOG.md +12 -0
- package/THIRD_PARTY_NOTICES.md +20 -0
- package/config/models/live2d.assetbundls.json +1 -1
- package/config/models/live2d.azurlane_bundle.json +1 -1
- package/config/models/live2d.bd2_mod.json +1 -0
- package/config/models/live2d.bluearchive.json +1 -1
- package/config/models/live2d.root.json +1 -0
- package/config/models/live2d.sample_haru.json +1 -0
- package/config/models/spine.assetbundls.json +1 -1
- package/config/models/spine.azurlane_bundle.json +1 -1
- package/config/models/spine.bd2_mod.json +1 -0
- package/config/models/spine.bluearchive.json +1 -1
- package/config/models/spine.root.json +1 -0
- package/config/models/spine.sample_haru.json +1 -0
- package/package.json +2 -2
- package/plugins/unity-live2d-bundle/index.js +105 -5
- package/plugins/unity-live2d-bundle/lib/astc.js +1307 -0
- package/plugins/unity-live2d-bundle/lib/bundle-probe.js +79 -0
- package/plugins/unity-live2d-bundle/lib/texdecode.js +60 -15
- package/plugins/unity-live2d-bundle/plugin.json +3 -1
- package/plugins/unity-spine-bundle/index.js +187 -15
- package/plugins/unity-spine-bundle/lib/astc.js +1307 -0
- package/plugins/unity-spine-bundle/lib/bundle-probe.js +323 -0
- package/plugins/unity-spine-bundle/lib/texdecode.js +60 -15
- package/plugins/unity-spine-bundle/plugin.json +4 -1
- package/src/server/analyze/index.mjs +470 -0
- package/src/server/data/catalog.mjs +30 -2
- package/src/server/data/display-name-cache.mjs +142 -0
- package/src/server/data/entry-states.mjs +21 -0
- package/src/server/http/write.mjs +7 -6
- package/src/server/index.mjs +1 -1
- package/src/server/plugins/registry.mjs +2 -2
- package/src/server/routes/display-name.mjs +157 -0
- package/src/server/serve.mjs +8 -0
- package/src/server/unpack/runtime/exec.mjs +19 -1
- package/src/web/infra/plugin-host-unpack.mjs +13 -3
- package/src/web/infra/plugin-host.js +3 -0
- package/src/web/render/surface.js +49 -2
- package/src/web/roster/display-name.js +79 -0
- package/src/web/roster/events.js +40 -1
- package/src/web/roster/list.js +8 -2
- package/src/web/roster/store.js +48 -0
- package/tool/lib/bundle-probe-node.mjs +146 -32
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
// ============================================================================
|
|
2
2
|
// tool/lib/bundle-probe-node.mjs —— 在 Node 里跑插件的「装载前预检」
|
|
3
3
|
//
|
|
4
|
-
//
|
|
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
|
-
/**
|
|
22
|
-
const
|
|
26
|
+
/** 分析能力在契约里的角色名(插件契约 §一) */
|
|
27
|
+
const ANALYZER_ROLE = 'bundle-analyzer';
|
|
23
28
|
|
|
24
29
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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(
|
|
30
|
-
return
|
|
89
|
+
export function libDirFor(provider) {
|
|
90
|
+
return provider.libDir;
|
|
31
91
|
}
|
|
32
92
|
|
|
33
93
|
/**
|
|
34
|
-
*
|
|
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}
|
|
103
|
+
* @param {{libFiles: string[]}} provider
|
|
44
104
|
* @returns {string[]} 形如 ['errors.js', 'bytes.js', ...]
|
|
45
105
|
*/
|
|
46
|
-
export function libOrderFor(
|
|
47
|
-
|
|
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
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
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))
|
|
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:
|
|
144
|
+
vm.runInContext(fs.readFileSync(abs, 'utf8'), ctx, { filename: `${provider.pluginId}-lib/${rel}` });
|
|
80
145
|
}
|
|
81
|
-
|
|
82
|
-
return
|
|
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
|
|
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
|
-
|
|
227
|
+
// 带上提供者 id:调用方(/api/analyze)要用它回答"这份结论是哪个插件给的",
|
|
228
|
+
// 缺了就只能给 null,等于把"解耦可见"这条验收做成摆设。
|
|
229
|
+
return Object.assign({ pluginId: provider.pluginId }, res || {}, shape || {});
|
|
117
230
|
}
|
|
118
231
|
|
|
119
232
|
// 路径与清单都**不再导出成常量**(常量会诱使别人继续手写第二份);
|
|
120
|
-
// 需要自查清单时调 libOrderFor(
|
|
121
|
-
export
|
|
233
|
+
// 需要自查清单时调 libOrderFor(provider) 现取。
|
|
234
|
+
export { ROOT, ANALYZER_ROLE };
|
|
235
|
+
export const PROBE_PATHS = { ROOT, findAnalyzerProvider, libDirFor, libOrderFor };
|