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.
- package/CHANGELOG.md +92 -0
- package/README.md +2 -63
- package/config/models/live2d.assetbundls.json +1 -1
- package/config/models/live2d.azurlane.json +1 -1
- package/config/models/live2d.azurlane_bundle.json +1 -1
- package/config/models/live2d.azurlane_bundle.jsonl +2 -0
- package/config/models/live2d.azurlane_spine.json +1 -1
- package/config/models/live2d.bd2.json +1 -1
- package/config/models/live2d.bd2.jsonl +2 -0
- package/config/models/live2d.bd2_mod.json +1 -0
- package/config/models/live2d.bluearchive.json +1 -1
- package/config/models/live2d.nikke.json +1 -1
- package/config/models/live2d.nikke.jsonl +2 -0
- package/config/models/live2d.nikke_2.json +1 -1
- package/config/models/live2d.npc.json +1 -1
- package/config/models/live2d.sample_haru.json +1 -0
- package/config/models/spine.assetbundls.json +1 -1
- package/config/models/spine.azurlane.json +1 -1
- package/config/models/spine.azurlane_bundle.json +1 -1
- package/config/models/spine.azurlane_bundle.jsonl +2 -0
- package/config/models/spine.azurlane_spine.json +1 -1
- package/config/models/spine.bd2.json +1 -1
- package/config/models/spine.bd2.jsonl +433 -0
- package/config/models/spine.bd2_mod.json +1 -0
- package/config/models/spine.bluearchive.json +1 -1
- package/config/models/spine.nikke.json +1 -1
- package/config/models/spine.nikke_2.json +1 -1
- package/config/models/spine.npc.json +1 -1
- package/config/models/spine.sample_haru.json +1 -0
- package/package.json +3 -2
- package/plugins/unity-live2d-bundle/index.js +43 -1
- package/plugins/unity-live2d-bundle/lib/bundle-probe.js +79 -0
- package/plugins/unity-live2d-bundle/plugin.json +2 -1
- package/plugins/unity-spine-bundle/index.js +38 -1
- package/plugins/unity-spine-bundle/lib/bundle-probe.js +323 -0
- package/plugins/unity-spine-bundle/plugin.json +3 -1
- package/src/server/analyze/index.mjs +470 -0
- package/src/server/assets/probe.mjs +1 -1
- package/src/server/core/config.mjs +2 -2
- package/src/server/core/log.mjs +1 -1
- package/src/server/data/catalog.mjs +41 -16
- package/src/server/data/display-name-cache.mjs +142 -0
- package/src/server/data/entry-states.mjs +21 -0
- package/src/server/data/live2d.mjs +139 -40
- package/src/server/data/manifest-cache.mjs +53 -10
- package/src/server/data/manifest-jsonl.mjs +264 -0
- package/src/server/data/manifest-stamp.mjs +52 -2
- package/src/server/data/scan-progress.mjs +305 -0
- package/src/server/data/scan-queue.mjs +99 -0
- package/src/server/data/spine.mjs +324 -106
- package/src/server/http/write.mjs +7 -6
- package/src/server/index.mjs +1 -1
- package/src/server/plugins/registry.mjs +3 -3
- package/src/server/routes/data.mjs +76 -17
- package/src/server/routes/display-name.mjs +157 -0
- package/src/server/serve.mjs +17 -4
- package/src/server/unpack/plugin-store.mjs +1 -1
- package/src/server/unpack/runtime/exec.mjs +37 -8
- package/src/server/unpack/worker-client.mjs +23 -8
- package/src/shared/scan-limits.mjs +23 -0
- package/src/web/app/anim-list.js +1 -1
- package/src/web/app/backgrounds.js +4 -8
- package/src/web/app/dir-picker.js +3 -5
- package/src/web/app/memory-budget.js +0 -1
- package/src/web/app/memory-guard.js +0 -1
- package/src/web/app/model-actions.js +18 -15
- package/src/web/app/root-manager.js +2 -4
- package/src/web/app/transport-actions.js +1 -1
- package/src/web/app/ui-state.js +1 -1
- package/src/web/components.css +90 -0
- package/src/web/debug/race-probe.js +36 -13
- package/src/web/engine/textures.js +1 -2
- package/src/web/infra/api.js +10 -19
- package/src/web/infra/document.js +3 -3
- package/src/web/infra/plugin-host-unpack.mjs +94 -12
- package/src/web/infra/plugin-host.js +3 -0
- package/src/web/infra/ready.js +177 -39
- package/src/web/live2d/bridge.js +1 -1
- package/src/web/live2d/runtime-adapter.js +2 -4
- package/src/web/panel/panel-settings.js +88 -3
- package/src/web/render/surface.js +49 -2
- package/src/web/roster/display-name.js +79 -0
- package/src/web/roster/events.js +198 -0
- package/src/web/roster/filter.js +4 -4
- package/src/web/roster/list.js +11 -5
- package/src/web/roster/load.js +440 -0
- package/src/web/roster/persist.js +46 -37
- package/src/web/roster/render.js +284 -0
- package/src/web/roster/reveal.js +4 -9
- package/src/web/roster/rows.js +35 -13
- package/src/web/roster/scan-stream.js +346 -0
- package/src/web/roster/snapshot.js +14 -8
- package/src/web/roster/store.js +340 -0
- package/src/web/roster/type-filter.js +5 -5
- package/src/web/roster/view.js +147 -634
- package/src/web/spine/canvas-runtime.js +4 -5
- package/src/web/spine/runtimes.js +1 -1
- package/tool/lib/bundle-probe-node.mjs +146 -32
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// server/data/display-name-cache.mjs —— 列表显示名缓存(config/bundle-display-names.json)
|
|
2
|
+
//
|
|
3
|
+
// 只做存取,不解其意:名字是内容语义,由插件经 analyze() 给出;本模块不认识
|
|
4
|
+
// `.skel` / `.atlas` / moc3,也不对名字做任何截断、拼接、大小写处理。
|
|
5
|
+
//
|
|
6
|
+
// 为什么缓存归核心而不是插件:插件可能被禁用或替换,但"上次算出来的名字"仍然可读;
|
|
7
|
+
// 而且跨请求存取本来就是核心的职责(与 entry-states 的结论缓存同性质)。
|
|
8
|
+
//
|
|
9
|
+
// 失效:mtimeMs 或 size 任一变化即视为失效(与 manifests 的 dirStamps 同一思路:
|
|
10
|
+
// 内容没变就别重算,变了就重算,不依赖时间)。
|
|
11
|
+
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
|
|
15
|
+
/** 缓存文件名(与 models/ 清单目录并列,落在 config/config 同级) */
|
|
16
|
+
const CACHE_FILENAME = 'bundle-display-names.json';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 缓存文件路径。
|
|
20
|
+
*
|
|
21
|
+
* 数据根形如 `<dataRoot>/config/resources.json`,本文件放 `<dataRoot>/config/bundle-display-names.json`。
|
|
22
|
+
* @param {string} configFile 资源根配置文件路径(与清单缓存用同一个入口)
|
|
23
|
+
* @returns {string}
|
|
24
|
+
*/
|
|
25
|
+
export function displayNameCacheFile(configFile) {
|
|
26
|
+
return path.join(path.dirname(configFile), CACHE_FILENAME);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 读整个缓存表。
|
|
31
|
+
*
|
|
32
|
+
* 文件不存在 / 解析失败一律返回空表(**不抛**):缓存坏了回落到旧行为(显示文件名),
|
|
33
|
+
* 不能因为一个展示态缓存让列表打不开。
|
|
34
|
+
* @param {string} configFile
|
|
35
|
+
* @returns {Record<string, {displayName: string|null, mtimeMs: number, size: number}>}
|
|
36
|
+
*/
|
|
37
|
+
export function readDisplayNameCache(configFile) {
|
|
38
|
+
try {
|
|
39
|
+
const raw = fs.readFileSync(displayNameCacheFile(configFile), 'utf8');
|
|
40
|
+
const obj = JSON.parse(raw);
|
|
41
|
+
return obj && typeof obj === 'object' && !Array.isArray(obj) ? obj : {};
|
|
42
|
+
} catch {
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 写整个缓存表(调用方负责合并,本函数只落盘)。
|
|
49
|
+
*
|
|
50
|
+
* 原子写:先写 `.tmp` 再 rename —— 与清单写入同一做法,避免读到半份 JSON。
|
|
51
|
+
* @param {string} configFile
|
|
52
|
+
* @param {Record<string, object>} table
|
|
53
|
+
*/
|
|
54
|
+
export function writeDisplayNameCache(configFile, table) {
|
|
55
|
+
const file = displayNameCacheFile(configFile);
|
|
56
|
+
const tmp = `${file}.tmp`;
|
|
57
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
58
|
+
fs.writeFileSync(tmp, JSON.stringify(table, null, 2), 'utf8');
|
|
59
|
+
fs.renameSync(tmp, file);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 该条目是否**已经尝试过**(无论拿到名字还是拿不到)。
|
|
64
|
+
*
|
|
65
|
+
* 为什么要区分"没记录"与"记录为 null":
|
|
66
|
+
* 插件不支持 / 解析失败时我们会写一条 `displayName: null` 的记录(表示"这个包取不到名字")。
|
|
67
|
+
* 若只看 `lookupDisplayName` 是否为 null,那种条目会被当成"没查过"而**每次都重跑一遍分析**,
|
|
68
|
+
* 在 181 个包的规模下就是白烧 2~5 分钟。
|
|
69
|
+
*
|
|
70
|
+
* @param {Record<string, object>} table
|
|
71
|
+
* @param {string} rootId
|
|
72
|
+
* @param {string} rel
|
|
73
|
+
* @param {{mtimeMs?: number, size: number}} [stat]
|
|
74
|
+
* @returns {boolean}
|
|
75
|
+
*/
|
|
76
|
+
export function hasDisplayNameRecord(table, rootId, rel, stat, analyzerFingerprint = null) {
|
|
77
|
+
const key = `${rootId}/${rel}`;
|
|
78
|
+
const hit = table && table[key];
|
|
79
|
+
if (!hit) return false;
|
|
80
|
+
if (analyzerFingerprint !== null) {
|
|
81
|
+
// 分析器变了 ⇒ 旧结论(尤其"取不到名字"这种)不能继续挡住新能力:
|
|
82
|
+
// 插件从"没有 displayName"升级到"有"时,旧的 null 记录若继续命中,
|
|
83
|
+
// 列表会永远显示文件名,且没有任何迹象说明原因。
|
|
84
|
+
if (String(hit.analyzerFingerprint || '') !== String(analyzerFingerprint)) return false;
|
|
85
|
+
}
|
|
86
|
+
if (stat) {
|
|
87
|
+
if (Number(hit.size) !== Number(stat.size)) return false;
|
|
88
|
+
if (Number.isFinite(stat.mtimeMs) && Number(hit.mtimeMs) !== Number(stat.mtimeMs)) return false;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 取一个条目的显示名(按 mtimeMs/size 判失效)。
|
|
95
|
+
*
|
|
96
|
+
* `mtimeMs` 是**可选**的:catalog 构造条目时只顺手带了 `size`,为了一个展示态去多做一次
|
|
97
|
+
* stat 不值得。给了 mtimeMs 就两个都比,没给就只比 size(文件内容变了 size 几乎必变)。
|
|
98
|
+
*
|
|
99
|
+
* @param {Record<string, object>} table 缓存表
|
|
100
|
+
* @param {string} rootId
|
|
101
|
+
* @param {string} rel
|
|
102
|
+
* @param {{mtimeMs?: number, size: number}} stat 当前文件状态;缺了就按"无法判失效"处理
|
|
103
|
+
* @returns {string|null}
|
|
104
|
+
*/
|
|
105
|
+
export function lookupDisplayName(table, rootId, rel, stat) {
|
|
106
|
+
const key = `${rootId}/${rel}`;
|
|
107
|
+
const hit = table && table[key];
|
|
108
|
+
if (!hit || typeof hit.displayName !== 'string') return null;
|
|
109
|
+
// 没有当前 stat 时无法判失效 ⇒ 采信缓存(采信旧名比丢名更稳)
|
|
110
|
+
if (stat) {
|
|
111
|
+
if (Number(hit.size) !== Number(stat.size)) return null;
|
|
112
|
+
if (Number.isFinite(stat.mtimeMs) && Number(hit.mtimeMs) !== Number(stat.mtimeMs)) return null;
|
|
113
|
+
}
|
|
114
|
+
return hit.displayName;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 写入一条缓存(就地改表,不落盘;由调用方决定何时 write)。
|
|
119
|
+
*
|
|
120
|
+
* `displayName` 为空串或全空白时**写成 null** —— 契约要求"空串表达未给"不是合法状态,
|
|
121
|
+
* 存成 null 后下次读就会回落旧行为,避免列表出现空标题。
|
|
122
|
+
* @param {Record<string, object>} table
|
|
123
|
+
* @param {string} rootId
|
|
124
|
+
* @param {string} rel
|
|
125
|
+
* @param {{displayName: string|null, mtimeMs: number, size: number}} entry
|
|
126
|
+
*/
|
|
127
|
+
export function setDisplayNameEntry(table, rootId, rel, entry) {
|
|
128
|
+
const name = typeof entry.displayName === 'string' ? entry.displayName.trim() : '';
|
|
129
|
+
table[`${rootId}/${rel}`] = {
|
|
130
|
+
displayName: name || null,
|
|
131
|
+
mtimeMs: Number(entry.mtimeMs),
|
|
132
|
+
size: Number(entry.size),
|
|
133
|
+
// 分析器指纹:插件升级(lib 变化)后旧结论自动失效,见 hasDisplayNameRecord
|
|
134
|
+
analyzerFingerprint: entry.analyzerFingerprint === undefined || entry.analyzerFingerprint === null
|
|
135
|
+
? null
|
|
136
|
+
: String(entry.analyzerFingerprint),
|
|
137
|
+
at: Date.now(),
|
|
138
|
+
};
|
|
139
|
+
return table;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export { CACHE_FILENAME };
|
|
@@ -279,10 +279,31 @@ export function createEntryStates(opts = {}) {
|
|
|
279
279
|
}
|
|
280
280
|
}
|
|
281
281
|
|
|
282
|
+
/**
|
|
283
|
+
* 列表显示名(工厂第四字段)。
|
|
284
|
+
*
|
|
285
|
+
* 只从调用方注入的 `ctx.displayNameFor` 取,本函数**不解析、不猜语义**:
|
|
286
|
+
* 名字是内容语义,归插件;这里是"把缓存里的值原样带出"。
|
|
287
|
+
* 拿不到 / 抛错 / 空串 / 全空白 一律 null ⇒ 前端回落既有规则(costume > 目录名 > 文件名),
|
|
288
|
+
* 即"插件未声明"与"缓存没命中"对上层是同一个表现,旧行为不变。
|
|
289
|
+
*/
|
|
290
|
+
let displayName = null;
|
|
291
|
+
if (typeof ctx.displayNameFor === 'function') {
|
|
292
|
+
try {
|
|
293
|
+
const got = ctx.displayNameFor(state);
|
|
294
|
+
displayName = typeof got === 'string' && got.trim() ? got.trim() : null;
|
|
295
|
+
} catch {
|
|
296
|
+
displayName = null; // 显示名是展示态,不能因为取它失败而让整条状态算不出来
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
282
300
|
return {
|
|
283
301
|
display: { accent, provider, candidates },
|
|
284
302
|
support,
|
|
285
303
|
loadResult,
|
|
304
|
+
// 第四字段:列表显示名。由调用方经 ctx.displayNameFor 注入(缓存里查出来的),
|
|
305
|
+
// 本函数不认识任何格式知识,只把值原样带出;null 表示"没取到",前端据此回落旧规则。
|
|
306
|
+
displayName,
|
|
286
307
|
// 结论已作废 / 从没结论 ⇒ 需要 §八 后台探测(调用方据此排队,不在同步路径解析包体)
|
|
287
308
|
pendingProbe,
|
|
288
309
|
};
|
|
@@ -10,10 +10,16 @@
|
|
|
10
10
|
import fs from 'node:fs';
|
|
11
11
|
import { createLogger } from '../core/log.mjs';
|
|
12
12
|
import { dirSignature, signatureMatches, generatorFingerprint } from './manifest-stamp.mjs';
|
|
13
|
-
import { live2dManifestFile, ensureManifestDir } from './manifest-cache.mjs';
|
|
13
|
+
import { live2dManifestFile, ensureManifestDir, live2dManifestBase } from './manifest-cache.mjs';
|
|
14
14
|
import path from 'node:path';
|
|
15
15
|
import { encodePath } from '../assets/probe.mjs';
|
|
16
16
|
import { resolveLive2DRuntimes } from '../../../tool/resolve-live2d-runtimes.mjs';
|
|
17
|
+
// 进度节流:与 Spine 侧共用同一份实现
|
|
18
|
+
import { createProgressThrottle } from './scan-progress.mjs';
|
|
19
|
+
// P1:清单改 JSONL(边扫边写)+ 双读兼容
|
|
20
|
+
import { openManifestWriter, readManifest } from './manifest-jsonl.mjs';
|
|
21
|
+
// 并发上限 + 让路提示(同 spine,理由见 scan-queue.mjs 顶部)
|
|
22
|
+
import { acquireScanSlot, releaseScanSlot, isInteractiveBusy } from './scan-queue.mjs';
|
|
17
23
|
|
|
18
24
|
/** 本模块日志(统一形状 {at,level,tag,msg,detail}) */
|
|
19
25
|
const log = createLogger('live2d');
|
|
@@ -21,7 +27,6 @@ const log = createLogger('live2d');
|
|
|
21
27
|
const rtLog = createLogger('live2d-runtimes');
|
|
22
28
|
|
|
23
29
|
/** Live2D 清单缓存的格式版本(结构变更时 +1,旧缓存自动失效重扫) */
|
|
24
|
-
const LIVE2D_MANIFEST_VERSION = 1;
|
|
25
30
|
/** 运行时库文件清单缓存(探测一次即复用) */
|
|
26
31
|
let live2dRuntimeFilesCache = null;
|
|
27
32
|
|
|
@@ -125,7 +130,6 @@ export function buildLive2DModel(root, relPath, defName, cubism) {
|
|
|
125
130
|
|
|
126
131
|
return {
|
|
127
132
|
id: `live2d-${root.id}-${(relPath || '.').replace(/\//g, '_')}`,
|
|
128
|
-
kind: 'live2d',
|
|
129
133
|
cubism,
|
|
130
134
|
name,
|
|
131
135
|
srcDir: relPath,
|
|
@@ -140,30 +144,26 @@ export function buildLive2DModel(root, relPath, defName, cubism) {
|
|
|
140
144
|
motions,
|
|
141
145
|
// 列表缩略图:模型目录里的 icon.png(有则用)
|
|
142
146
|
iconUrl: fileObj('icon.png') ? fileUrl('icon.png') : null,
|
|
143
|
-
fileSize: [defName, mocName, ...textures, ...expressions.map((e) => e.file),
|
|
144
|
-
...Object.values(motions).flat().map((mo) => mo.file)]
|
|
145
|
-
.map(fileObj).filter(Boolean).reduce((sum, f) => sum + f.size, 0),
|
|
146
147
|
};
|
|
147
148
|
}
|
|
148
149
|
|
|
149
150
|
/** 扫一个目录:识别 Cubism 4(*.model3.json)优先于 Cubism 2(model.json)。 */
|
|
150
151
|
function modelsInDir(root, absDir, relPath, entries) {
|
|
152
|
+
// 同步(回退理由同 spine.collectDirEntries:异步化在 CIFS 上更慢)
|
|
151
153
|
const dirs = [];
|
|
152
154
|
const files = new Map(); // 文件名 -> { name, size }
|
|
153
155
|
for (const e of entries) {
|
|
154
156
|
if (e.name.startsWith('.')) continue;
|
|
155
157
|
const full = path.join(absDir, e.name);
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
continue;
|
|
161
|
-
}
|
|
158
|
+
// **不再逐文件 stat**:目录判定用 Dirent(`e.isDirectory()`);
|
|
159
|
+
// 这里原来的 size 唯一用途是 `model.fileSize`(前端从未读过,死数据)。
|
|
160
|
+
// 注意:`fileObj()` 里的 stat **保留** —— 它要 `st.isFile()` 判「文件真的在不在」,
|
|
161
|
+
// 去掉会让「引用不存在的贴图」从告警变成沉默(缺贴图页数会变多)。
|
|
162
162
|
if (e.isDirectory()) {
|
|
163
163
|
dirs.push({ name: e.name, full });
|
|
164
164
|
continue;
|
|
165
165
|
}
|
|
166
|
-
files.set(e.name, { name: e.name
|
|
166
|
+
files.set(e.name, { name: e.name });
|
|
167
167
|
}
|
|
168
168
|
const out = [];
|
|
169
169
|
const def4 = [...files.keys()].find((n) => /\.model3\.json$/i.test(n)) || null;
|
|
@@ -176,10 +176,33 @@ function modelsInDir(root, absDir, relPath, entries) {
|
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
/** 扫描一个资源根里的全部 Live2D 模型。 */
|
|
179
|
-
export function scanLive2DRootServer(root) {
|
|
179
|
+
export async function scanLive2DRootServer(root, opts = {}) {
|
|
180
180
|
const models = [];
|
|
181
|
+
// 进度回调可选:不传时与改动前逐字一致(现有调用方不传)
|
|
182
|
+
const throttle = typeof opts.onProgress === 'function'
|
|
183
|
+
? createProgressThrottle({ onProgress: opts.onProgress, kind: 'live2d', rootId: root.id })
|
|
184
|
+
: null;
|
|
185
|
+
let dirsScanned = 0;
|
|
186
|
+
|
|
187
|
+
// 分片让出:与 spine 侧同一套策略(每 20 个目录或每 50ms),理由见 spine.mjs 里的注释
|
|
188
|
+
// setImmediate 而不是 setTimeout(0):后者在负载下实际延迟可达几十 ms,扫描让出上千次会累积成数倍劣化(实测 24s→169s)
|
|
189
|
+
const yieldToEventLoop = () => new Promise((resolve) => setImmediate(resolve));
|
|
190
|
+
const YIELD_DIRS = 20;
|
|
191
|
+
const YIELD_MS = 50;
|
|
192
|
+
let sinceYieldDirs = 0;
|
|
193
|
+
let lastYieldAt = Date.now();
|
|
194
|
+
async function maybeYield() {
|
|
195
|
+
sinceYieldDirs += 1;
|
|
196
|
+
// 让路:有交互请求时再多让一点,但幅度有限(理由见 spine.mjs 同处注释)
|
|
197
|
+
const busy = isInteractiveBusy();
|
|
198
|
+
if (sinceYieldDirs >= (busy ? 12 : YIELD_DIRS) || Date.now() - lastYieldAt >= (busy ? 25 : YIELD_MS)) {
|
|
199
|
+
sinceYieldDirs = 0;
|
|
200
|
+
lastYieldAt = Date.now();
|
|
201
|
+
await yieldToEventLoop();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
181
204
|
|
|
182
|
-
function walk(absDir, relPath) {
|
|
205
|
+
async function walk(absDir, relPath) {
|
|
183
206
|
let entries;
|
|
184
207
|
try {
|
|
185
208
|
entries = fs.readdirSync(absDir, { withFileTypes: true });
|
|
@@ -187,11 +210,25 @@ export function scanLive2DRootServer(root) {
|
|
|
187
210
|
return;
|
|
188
211
|
}
|
|
189
212
|
const found = modelsInDir(root, absDir, relPath, entries);
|
|
190
|
-
|
|
191
|
-
|
|
213
|
+
// 边扫边写挂点(P1):不传则完全无副作用
|
|
214
|
+
if (found.models.length) {
|
|
215
|
+
models.push(...found.models);
|
|
216
|
+
if (typeof opts.onModel === 'function') {
|
|
217
|
+
for (const m of found.models) opts.onModel(m);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
dirsScanned += 1;
|
|
221
|
+
if (throttle) {
|
|
222
|
+
throttle.bump({ scanned: models.length, dirs: dirsScanned, currentPath: relPath, dirCompleted: true });
|
|
223
|
+
}
|
|
224
|
+
await maybeYield();
|
|
225
|
+
for (const d of found.dirs) {
|
|
226
|
+
await walk(d.full, relPath ? `${relPath}/${d.name}` : d.name);
|
|
227
|
+
}
|
|
192
228
|
}
|
|
193
229
|
|
|
194
|
-
walk(root.path, '');
|
|
230
|
+
await walk(root.path, '');
|
|
231
|
+
if (throttle) throttle.flush();
|
|
195
232
|
return models;
|
|
196
233
|
}
|
|
197
234
|
|
|
@@ -199,41 +236,103 @@ export function scanLive2DRootServer(root) {
|
|
|
199
236
|
* 取 Live2D 模型列表(带落盘缓存,逻辑与 Spine 的 getModels 一致)。
|
|
200
237
|
* 为什么缓存:天命之子有 2000+ 角色目录,每次刷新全量扫描会明显变慢。
|
|
201
238
|
*/
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
239
|
+
/**
|
|
240
|
+
* 在跑的扫描:key = `live2d:<rootId>` → promise(与 spine 同口径,理由见 spine.mjs 里的注释)。
|
|
241
|
+
*/
|
|
242
|
+
const live2dInflight = new Map();
|
|
243
|
+
|
|
244
|
+
export async function getLive2DModels(configFile, cfg, force, onProgress, onStart, onModel) {
|
|
245
|
+
const key = `live2d:${cfg.id}`;
|
|
246
|
+
const existing = live2dInflight.get(key);
|
|
247
|
+
if (existing) return existing;
|
|
248
|
+
// 先占位再启动(同 spine,理由见其注释)
|
|
249
|
+
let settleResolve;
|
|
250
|
+
let settleReject;
|
|
251
|
+
const gate = new Promise((resolve, reject) => { settleResolve = resolve; settleReject = reject; });
|
|
252
|
+
live2dInflight.set(key, gate);
|
|
253
|
+
doGetLive2DModels(configFile, cfg, force, onProgress, onStart, onModel)
|
|
254
|
+
.then(settleResolve, settleReject)
|
|
255
|
+
.finally(() => { if (live2dInflight.get(key) === gate) live2dInflight.delete(key); });
|
|
256
|
+
return gate;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** 真正干活的实现(单飞包装见上面的 getLive2DModels) */
|
|
260
|
+
async function doGetLive2DModels(configFile, cfg, force, onProgress, onStart, onModel) {
|
|
261
|
+
const base = live2dManifestBase(configFile, cfg.id);
|
|
262
|
+
// 与 spine 同口径:默认 .json,env=jsonl 才写 JSONL(两种都能读)
|
|
263
|
+
const wantJsonl = String(process.env.MODELSTAGE_MANIFEST_FORMAT || 'json').toLowerCase() === 'jsonl';
|
|
264
|
+
|
|
265
|
+
if (!force) {
|
|
266
|
+
// 双读:.jsonl 优先,缺则回落 .json(视为 complete=true)
|
|
267
|
+
const m = readManifest(base);
|
|
268
|
+
if (m.format !== 'none') {
|
|
269
|
+
if (!m.complete) {
|
|
270
|
+
log.info('清单不完整(complete 非 true),重建', { id: cfg.id, badLines: m.badLines });
|
|
271
|
+
} else {
|
|
209
272
|
const gen = generatorFingerprint();
|
|
210
|
-
if (
|
|
211
|
-
log.info('解析器已变化,清单失效重建', { id: cfg.id, from:
|
|
212
|
-
} else if (signatureMatches(
|
|
213
|
-
return { models:
|
|
273
|
+
if (m.meta.generator !== gen) {
|
|
274
|
+
log.info('解析器已变化,清单失效重建', { id: cfg.id, from: m.meta.generator || null, to: gen });
|
|
275
|
+
} else if (signatureMatches(m.meta.dirStamps, cfg.path)) {
|
|
276
|
+
return { models: m.models, cached: true, ts: m.meta.ts };
|
|
214
277
|
} else {
|
|
215
278
|
log.info('目录已变化,清单失效重建', { id: cfg.id });
|
|
216
279
|
}
|
|
217
280
|
}
|
|
218
|
-
} catch {
|
|
219
|
-
/* 缓存损坏则重扫 */
|
|
220
281
|
}
|
|
221
282
|
}
|
|
222
|
-
|
|
283
|
+
|
|
223
284
|
const ts = Date.now();
|
|
285
|
+
const generator = generatorFingerprint();
|
|
286
|
+
const dirStamps = dirSignature(cfg.path);
|
|
287
|
+
ensureManifestDir(configFile);
|
|
288
|
+
|
|
289
|
+
const writer = wantJsonl
|
|
290
|
+
? openManifestWriter(base, { rootId: cfg.id, ts, generator, dirStamps })
|
|
291
|
+
: null;
|
|
292
|
+
|
|
293
|
+
// 并发上限(同 spine):全局只允许 1 个根在扫
|
|
294
|
+
await acquireScanSlot(`live2d:${cfg.id}`);
|
|
295
|
+
|
|
296
|
+
let models;
|
|
297
|
+
try {
|
|
298
|
+
models = await scanLive2DRootServer(cfg, {
|
|
299
|
+
...(onProgress ? { onProgress } : {}),
|
|
300
|
+
...(typeof onStart === 'function' ? { onStart } : {}),
|
|
301
|
+
...((writer || typeof onModel === 'function')
|
|
302
|
+
? {
|
|
303
|
+
onModel: (m) => {
|
|
304
|
+
if (writer) writer.append(m);
|
|
305
|
+
if (typeof onModel === 'function') onModel(m);
|
|
306
|
+
},
|
|
307
|
+
}
|
|
308
|
+
: {}),
|
|
309
|
+
});
|
|
310
|
+
if (writer) writer.finish(models.length);
|
|
311
|
+
} catch (e) {
|
|
312
|
+
if (writer) writer.abort();
|
|
313
|
+
throw e;
|
|
314
|
+
} finally {
|
|
315
|
+
releaseScanSlot();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (wantJsonl) {
|
|
319
|
+
log.info('已生成清单(jsonl)', { id: cfg.id, count: models.length, bytes: writer.bytesWritten });
|
|
320
|
+
return { models, cached: false, ts };
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// 回退:只写 .json(旧格式)
|
|
224
324
|
try {
|
|
225
|
-
|
|
226
|
-
//
|
|
227
|
-
|
|
325
|
+
const file = `${base}.json`;
|
|
326
|
+
// 清单头与 spine 完全一致(见 docs:v 是唯一的格式版本字段;kind 一律不写进清单,
|
|
327
|
+
// 由 catalog 读取时盖章;rootName/rootPath 无消费方已移除)
|
|
228
328
|
const body = JSON.stringify({
|
|
229
|
-
version: LIVE2D_MANIFEST_VERSION,
|
|
230
329
|
rootId: cfg.id,
|
|
231
330
|
ts,
|
|
232
|
-
|
|
233
|
-
generator
|
|
331
|
+
count: models.length,
|
|
332
|
+
generator,
|
|
333
|
+
dirStamps,
|
|
234
334
|
models,
|
|
235
335
|
});
|
|
236
|
-
// 与 spine 清单同口径:先写临时文件再 rename,避免并发读拿到半份 JSON
|
|
237
336
|
fs.writeFileSync(`${file}.tmp`, body, 'utf8');
|
|
238
337
|
fs.renameSync(`${file}.tmp`, file);
|
|
239
338
|
} catch (e) {
|
|
@@ -266,7 +365,7 @@ export function createLive2d(p) {
|
|
|
266
365
|
live2dModelsFile: (rootId) => live2dModelsFile(p.configFile, rootId),
|
|
267
366
|
buildLive2DModel,
|
|
268
367
|
scanLive2DRootServer,
|
|
269
|
-
getLive2DModels: (cfg, force) => getLive2DModels(p.configFile, cfg, force),
|
|
368
|
+
getLive2DModels: (cfg, force, onProgress, onStart, onModel) => getLive2DModels(p.configFile, cfg, force, onProgress, onStart, onModel),
|
|
270
369
|
getLive2DRuntimeFiles,
|
|
271
370
|
};
|
|
272
371
|
}
|
|
@@ -17,14 +17,28 @@ export function manifestDir(configFile) {
|
|
|
17
17
|
return path.join(path.dirname(configFile), 'models');
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* 清单的**基名**(不带扩展名):config/models/spine.<rootId>
|
|
22
|
+
* 为什么引入 base:同一份清单现在有两种落盘格式(.jsonl 当前 / .json 历史),
|
|
23
|
+
* 读写层需要"同一个名字 + 不同后缀",而不是各自拼一遍字符串(拼两处早晚拼歪)。
|
|
24
|
+
*/
|
|
25
|
+
export function spineManifestBase(configFile, rootId) {
|
|
26
|
+
return path.join(manifestDir(configFile), `spine.${rootId}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Live2D 清单基名:config/models/live2d.<rootId> */
|
|
30
|
+
export function live2dManifestBase(configFile, rootId) {
|
|
31
|
+
return path.join(manifestDir(configFile), `live2d.${rootId}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Spine 清单路径(历史 JSON 格式):config/models/spine.<rootId>.json */
|
|
21
35
|
export function spineManifestFile(configFile, rootId) {
|
|
22
|
-
return
|
|
36
|
+
return `${spineManifestBase(configFile, rootId)}.json`;
|
|
23
37
|
}
|
|
24
38
|
|
|
25
|
-
/** Live2D
|
|
39
|
+
/** Live2D 清单路径(历史 JSON 格式):config/models/live2d.<rootId>.json */
|
|
26
40
|
export function live2dManifestFile(configFile, rootId) {
|
|
27
|
-
return
|
|
41
|
+
return `${live2dManifestBase(configFile, rootId)}.json`;
|
|
28
42
|
}
|
|
29
43
|
|
|
30
44
|
/** 写清单前确保目录存在(首次运行或目录被清空时,不能因为缺目录写失败) */
|
|
@@ -37,21 +51,50 @@ export function ensureManifestDir(configFile) {
|
|
|
37
51
|
* 文件不存在、目录不存在都不算错(本来就没有清单可删)。
|
|
38
52
|
*/
|
|
39
53
|
export function invalidateManifests(configFile, rootId) {
|
|
54
|
+
// **只删成品**(.jsonl / .json),**不删 tmp**。
|
|
55
|
+
//
|
|
56
|
+
// 为什么 tmp 不能删:tmp 是"扫描正在写的那份"。invalidate 可能被**并发触发**
|
|
57
|
+
// (另一个根 refresh、或任何一次失效),删掉正在写的 tmp ⇒ 那个扫描 finish 时 rename ENOENT
|
|
58
|
+
// (实测日志:`rename 'spine.<rootId>.jsonl.tmp' -> 'spine.<rootId>.jsonl'`)。
|
|
59
|
+
// tmp 现在带 pid+seq 唯一名,靠"年龄"清理:只删老到不可能是"正在写"的。
|
|
60
|
+
const FINAL_SUFFIXES = ['.jsonl', '.json'];
|
|
61
|
+
const TMP_MAX_AGE_MS = 60 * 60 * 1000; // 1 小时:正常扫描远用不到,超过即视为残留
|
|
62
|
+
const TMP_RE = /\.\d+\.\d+\.jsonl\.tmp$/; // <base>.<pid>.<seq>.jsonl.tmp
|
|
63
|
+
|
|
64
|
+
function cleanupStaleTmp(dir) {
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
try {
|
|
67
|
+
for (const n of fs.readdirSync(dir)) {
|
|
68
|
+
if (!TMP_RE.test(n)) continue;
|
|
69
|
+
const full = path.join(dir, n);
|
|
70
|
+
try {
|
|
71
|
+
// 只删老的:新 tmp 可能正是另一个扫描正在写的
|
|
72
|
+
if (now - fs.statSync(full).mtimeMs > TMP_MAX_AGE_MS) fs.unlinkSync(full);
|
|
73
|
+
} catch { /* 单个文件处理失败不影响整体 */ }
|
|
74
|
+
}
|
|
75
|
+
} catch { /* 目录读不了就算了 */ }
|
|
76
|
+
}
|
|
77
|
+
|
|
40
78
|
if (rootId) {
|
|
41
|
-
for (const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
79
|
+
for (const base of [spineManifestBase(configFile, rootId), live2dManifestBase(configFile, rootId)]) {
|
|
80
|
+
for (const suffix of FINAL_SUFFIXES) {
|
|
81
|
+
const f = `${base}${suffix}`;
|
|
82
|
+
try {
|
|
83
|
+
if (fs.existsSync(f)) fs.unlinkSync(f);
|
|
84
|
+
} catch {
|
|
85
|
+
/* 删除失败不影响调用方:清单不可用会被后续扫描覆盖 */
|
|
86
|
+
}
|
|
46
87
|
}
|
|
47
88
|
}
|
|
89
|
+
cleanupStaleTmp(manifestDir(configFile));
|
|
48
90
|
return;
|
|
49
91
|
}
|
|
50
92
|
try {
|
|
51
93
|
const dir = manifestDir(configFile);
|
|
52
94
|
for (const n of fs.readdirSync(dir)) {
|
|
53
|
-
if (/^(?:spine|live2d)\..+\.json$/.test(n)) fs.unlinkSync(path.join(dir, n));
|
|
95
|
+
if (/^(?:spine|live2d)\..+\.(?:jsonl|json)$/.test(n)) fs.unlinkSync(path.join(dir, n));
|
|
54
96
|
}
|
|
97
|
+
cleanupStaleTmp(dir);
|
|
55
98
|
} catch {
|
|
56
99
|
/* 目录不存在 = 没有清单要删 */
|
|
57
100
|
}
|