cortico-world-extra-actions 0.3.28 → 0.3.30
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/assets/catalog.json +1957 -1
- package/assets/starter/figure/figure.js +13 -3
- package/assets/starter/figure/figure.json +29 -1
- package/package.json +2 -2
- package/src/starter.js +133 -0
- package/src/world.js +17 -1
- package/src/writer.js +31 -5
- package/tools/doctor.mjs +125 -0
- package/tools/test_click.mjs +167 -0
- package/tools/test_theme.mjs +101 -0
- package/tools/test_vocab_names.mjs +128 -0
|
@@ -42,6 +42,8 @@ const VARIETY_GAP_MS = 6000;
|
|
|
42
42
|
const CLICK_GAP_MS = 400;
|
|
43
43
|
/** 没勾点击回应时退回去用的那几个动作词。 */
|
|
44
44
|
const CLICK_FALLBACK = new Set(['cheer-wave', 'cheer-hop', 'tickle-giggle', 'shy-startle', 'tsun-pout']);
|
|
45
|
+
/** 点击回应最多播这么久就回待机:反应本来就该短促,10 秒的整段留着轮播用。 */
|
|
46
|
+
const REACTION_MAX = 2.6;
|
|
45
47
|
/** 关掉 blob 预取(退回逐帧换真地址),只在排查问题时用。 */
|
|
46
48
|
const NO_BLOB_CACHE = false;
|
|
47
49
|
/** 每帧之后往前预取几张。 */
|
|
@@ -214,10 +216,11 @@ function createFigure(base, index, allow = null) {
|
|
|
214
216
|
for (let k = 0; k < n; k++) load(c, (from + k) % total);
|
|
215
217
|
}
|
|
216
218
|
|
|
217
|
-
|
|
219
|
+
/** cap:最多播几秒(点击回应用 REACTION_MAX,别的调用不传 = 播完整段)。 */
|
|
220
|
+
const play = (word, cap = Infinity) => {
|
|
218
221
|
const c = byWord.get(word);
|
|
219
222
|
if (!c) return false;
|
|
220
|
-
override = { clip: c.clip, seconds: c.seconds, from: -1, kind: 'word' };
|
|
223
|
+
override = { clip: c.clip, seconds: Math.min(c.seconds, cap), from: -1, kind: 'word' };
|
|
221
224
|
return true;
|
|
222
225
|
};
|
|
223
226
|
|
|
@@ -244,12 +247,19 @@ function createFigure(base, index, allow = null) {
|
|
|
244
247
|
/** 点击:在可点击的回应里随机放一段;正在走 / 被拎着 / 蹲着时不打断。 */
|
|
245
248
|
react() {
|
|
246
249
|
if (/walk|run|drag|air|crouch/.test(modeNow)) return false;
|
|
250
|
+
// ① 标了"点击"的段(清单里 reaction: true)
|
|
247
251
|
let pool = reactions.filter((w) => byWord.has(w) && (!allow || allow.has(w)));
|
|
252
|
+
// ② 退到约定的几段常见回应
|
|
248
253
|
if (!pool.length && allow) {
|
|
249
254
|
pool = [...allow].filter((w) => CLICK_FALLBACK.has(w) && byWord.has(w));
|
|
250
255
|
}
|
|
256
|
+
// ③ 还没有?就从已勾选的段里随便挑一段 —— 戳一下总得动一下,
|
|
257
|
+
// 否则用户勾的全是"吃饭/伸懒腰"时,点她完全没反应。
|
|
258
|
+
if (!pool.length && allow) {
|
|
259
|
+
pool = [...allow].filter((w) => byWord.has(w));
|
|
260
|
+
}
|
|
251
261
|
if (!pool.length) return false;
|
|
252
|
-
return play(pool[Math.floor(Math.random() * pool.length)]);
|
|
262
|
+
return play(pool[Math.floor(Math.random() * pool.length)], REACTION_MAX);
|
|
253
263
|
},
|
|
254
264
|
|
|
255
265
|
draw(petG, face, o) {
|
|
@@ -18,7 +18,35 @@
|
|
|
18
18
|
"export": "createActionBody",
|
|
19
19
|
"thumb": "thumb.png",
|
|
20
20
|
"axes": [],
|
|
21
|
-
"presets": [
|
|
21
|
+
"presets": [
|
|
22
|
+
{
|
|
23
|
+
"id": "teal",
|
|
24
|
+
"name": {
|
|
25
|
+
"zh": "深深 · 青绿",
|
|
26
|
+
"zh-Hant": "深深 · 青綠",
|
|
27
|
+
"en": "Shenshen · Teal"
|
|
28
|
+
},
|
|
29
|
+
"accent": "#0b7a5a",
|
|
30
|
+
"console": {
|
|
31
|
+
"light": {
|
|
32
|
+
"a": "#0b7a5a",
|
|
33
|
+
"a2": "#0f9d76",
|
|
34
|
+
"on": "#ffffff",
|
|
35
|
+
"t": "#0b6b4f",
|
|
36
|
+
"c3": "#3f8f78",
|
|
37
|
+
"c4": "#8a8a90"
|
|
38
|
+
},
|
|
39
|
+
"dark": {
|
|
40
|
+
"a": "#2fd3a0",
|
|
41
|
+
"a2": "#7fe6c2",
|
|
42
|
+
"on": "#062a20",
|
|
43
|
+
"t": "#8ae8c6",
|
|
44
|
+
"c3": "#5fbfa0",
|
|
45
|
+
"c4": "#9a9aa0"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
],
|
|
22
50
|
"vocab": [],
|
|
23
51
|
"sounds": {},
|
|
24
52
|
"can": {
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cortico-world-extra-actions",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.30",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
|
-
"description": "
|
|
6
|
+
"description": "勾选动作库里的动作,写进形象包的「额外动作拓展」文件夹,让桌宠照着做。自带入门形象包(含控制台配色)与示例动作;完整 105 段可一键下载安装。",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"cortico-world",
|
|
9
9
|
"coopanion",
|
package/src/starter.js
CHANGED
|
@@ -275,3 +275,136 @@ export function installStarterLibrary({ pkgDir, parentDir, log }) {
|
|
|
275
275
|
log?.(`自带示例动作库已建在 ${dst}`);
|
|
276
276
|
return { created: true, path: dst };
|
|
277
277
|
}
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* 给「深深」这个形象包补一套控制台配色预设(宿主 console-theme.ts 的 figureSchemes)。
|
|
282
|
+
*
|
|
283
|
+
* 为什么需要:宿主对**没有 console 预设**的包,会把配色钉回 mint(配色方案里的第一个),
|
|
284
|
+
* 用户在控制台换配色之后一切到深深就被改回去。补一套预设之后,切到深深 = 切到「深深 · 青绿」,
|
|
285
|
+
* 而不是回落默认。
|
|
286
|
+
*
|
|
287
|
+
* 只对自己这个包(id = shenshen-actions)动手:只新增、不删除、不覆盖已有预设,
|
|
288
|
+
* 改之前留 .bak,重复调用不会再写。
|
|
289
|
+
*/
|
|
290
|
+
export function ensureConsolePreset(packDir, log) {
|
|
291
|
+
const id = safePackName(packDir);
|
|
292
|
+
const figFile = join(packDir, 'figure.json');
|
|
293
|
+
const fig = readJson(figFile, null);
|
|
294
|
+
if (!fig || fig.id !== PACK_ID) return { changed: false, reason: 'not-our-pack' };
|
|
295
|
+
const presets = Array.isArray(fig.presets) ? fig.presets : [];
|
|
296
|
+
if (presets.some((p) => p && p.console)) return { changed: false, reason: 'already-has-console' };
|
|
297
|
+
if (presets.some((p) => p && p.id === CONSOLE_PRESET.id)) return { changed: false, reason: 'id-taken' };
|
|
298
|
+
|
|
299
|
+
const backup = join(packDir, 'figure.json.bak-配色');
|
|
300
|
+
if (!existsSync(backup)) writeFileSync(backup, readFileSync(figFile, 'utf8'), 'utf8');
|
|
301
|
+
fig.presets = [CONSOLE_PRESET, ...presets];
|
|
302
|
+
writeFileSync(figFile, JSON.stringify(fig, null, 2) + '\n', 'utf8');
|
|
303
|
+
log?.('已给形象包补上控制台配色预设:深深 · 青绿');
|
|
304
|
+
return { changed: true, preset: CONSOLE_PRESET.id };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** 深深包的配色预设(对比度:浅色 on-accent 5.3:1、t/纸面 6.3:1;深色 8.1:1 / 11.4:1)。 */
|
|
308
|
+
export const CONSOLE_PRESET = {
|
|
309
|
+
"id": "teal",
|
|
310
|
+
"name": {
|
|
311
|
+
"zh": "深深 · 青绿",
|
|
312
|
+
"zh-Hant": "深深 · 青綠",
|
|
313
|
+
"en": "Shenshen · Teal"
|
|
314
|
+
},
|
|
315
|
+
"accent": "#0b7a5a",
|
|
316
|
+
"console": {
|
|
317
|
+
"light": {
|
|
318
|
+
"a": "#0b7a5a",
|
|
319
|
+
"a2": "#0f9d76",
|
|
320
|
+
"on": "#ffffff",
|
|
321
|
+
"t": "#0b6b4f",
|
|
322
|
+
"c3": "#3f8f78",
|
|
323
|
+
"c4": "#8a8a90"
|
|
324
|
+
},
|
|
325
|
+
"dark": {
|
|
326
|
+
"a": "#2fd3a0",
|
|
327
|
+
"a2": "#7fe6c2",
|
|
328
|
+
"on": "#062a20",
|
|
329
|
+
"t": "#8ae8c6",
|
|
330
|
+
"c3": "#5fbfa0",
|
|
331
|
+
"c4": "#9a9aa0"
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* 把「深深」这个形象包里的播放器升级到当前版本。
|
|
338
|
+
*
|
|
339
|
+
* 为什么需要:点击回应(戳一戳)的逻辑在 figure.js 里。老包里的播放器有三个问题 ——
|
|
340
|
+
* 没有"没有点击段就随便挑一段"的兜底、回应会播满 10 秒、以及没有上限。
|
|
341
|
+
* 只对自己这个包动手:内容没变(已有标记)就不写,改之前留 .bak-播放器。
|
|
342
|
+
*/
|
|
343
|
+
export function ensurePlayer(packDir, pkgDir, log) {
|
|
344
|
+
const figFile = join(packDir, 'figure.json');
|
|
345
|
+
const fig = readJson(figFile, null);
|
|
346
|
+
if (!fig || fig.id !== PACK_ID) return { changed: false, reason: 'not-our-pack' };
|
|
347
|
+
const entry = typeof fig.entry === 'string' && fig.entry ? fig.entry : 'figure.js';
|
|
348
|
+
const target = join(packDir, entry);
|
|
349
|
+
const src = join(starterRoot(pkgDir), 'figure', 'figure.js');
|
|
350
|
+
if (!existsSync(src) || !existsSync(target)) return { changed: false, reason: 'no-player' };
|
|
351
|
+
const now = readFileSync(target, 'utf8');
|
|
352
|
+
const next = readFileSync(src, 'utf8');
|
|
353
|
+
if (now === next || now.includes('REACTION_MAX')) return { changed: false, reason: 'up-to-date' };
|
|
354
|
+
writeFileSync(join(packDir, entry + '.bak-播放器'), now, 'utf8');
|
|
355
|
+
writeFileSync(target, next, 'utf8');
|
|
356
|
+
log?.('形象包的播放器已升级(点击回应更灵敏)');
|
|
357
|
+
return { changed: true };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* 修掉形象包里「宿主不认」的词表名字。
|
|
362
|
+
*
|
|
363
|
+
* 宿主 packs.ts 的 readVocab 有一条硬规则:名字里不能有 , , 、 空白 【】 <> <>,
|
|
364
|
+
* 也不能超过 32 字、不能和别的词(含 id)重名。**只要有一条不合格,整份 vocab 被拒** ——
|
|
365
|
+
* 表现是 bot 一个动作都点不出来(用户会说"某个动作为什么调用不出来")。
|
|
366
|
+
*
|
|
367
|
+
* 我们早先的清单里有一个名字带逗号(是啊,吃什么),勾上它就会把整份词表拖坏。
|
|
368
|
+
* 这里把已经是这种状态的老包修回来:只改 names,不动 id/kind/about/seconds,先备份,幂等。
|
|
369
|
+
*/
|
|
370
|
+
export function ensureVocabNames(packDir, log) {
|
|
371
|
+
const figFile = join(packDir, 'figure.json');
|
|
372
|
+
const fig = readJson(figFile, null);
|
|
373
|
+
if (!fig || fig.id !== PACK_ID) return { changed: false, reason: 'not-our-pack' };
|
|
374
|
+
const vocab = Array.isArray(fig.vocab) ? fig.vocab : [];
|
|
375
|
+
if (!vocab.length) return { changed: false, reason: 'empty' };
|
|
376
|
+
|
|
377
|
+
const BREAK = /[,,、\s【】<><>]/g;
|
|
378
|
+
const taken = new Set();
|
|
379
|
+
const fixed = [];
|
|
380
|
+
let changed = false;
|
|
381
|
+
for (const w of vocab) {
|
|
382
|
+
const next = JSON.parse(JSON.stringify(w));
|
|
383
|
+
const id = String(next.id ?? '');
|
|
384
|
+
const names = next.names && typeof next.names === 'object' ? next.names : {};
|
|
385
|
+
for (const [lang, list] of Object.entries(names)) {
|
|
386
|
+
if (!Array.isArray(list)) continue;
|
|
387
|
+
names[lang] = list.map((raw) => {
|
|
388
|
+
let name = String(raw ?? '').replace(BREAK, '').trim().slice(0, 32) || id;
|
|
389
|
+
if (taken.has(name)) name = id; // 撞名就退回 id(id 一定唯一且合规)
|
|
390
|
+
return name;
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
next.names = names;
|
|
394
|
+
const flat = [id, ...Object.values(names).flat()];
|
|
395
|
+
for (const n of flat) {
|
|
396
|
+
if (taken.has(n)) { changed = true; next.names = { zh: [id] }; taken.add(id); break; }
|
|
397
|
+
taken.add(n);
|
|
398
|
+
}
|
|
399
|
+
const before = JSON.stringify(w);
|
|
400
|
+
if (JSON.stringify(next) !== before) changed = true;
|
|
401
|
+
fixed.push(next);
|
|
402
|
+
}
|
|
403
|
+
if (!changed) return { changed: false, reason: 'already-clean' };
|
|
404
|
+
|
|
405
|
+
writeFileSync(join(packDir, 'figure.json.bak-词表名'), readFileSync(figFile, 'utf8'), 'utf8');
|
|
406
|
+
fig.vocab = fixed;
|
|
407
|
+
writeFileSync(figFile, JSON.stringify(fig, null, 2) + '\n', 'utf8');
|
|
408
|
+
log?.('已修好形象包里宿主不认的词表名字(否则 bot 一个动作都点不出来)');
|
|
409
|
+
return { changed: true };
|
|
410
|
+
}
|
package/src/world.js
CHANGED
|
@@ -13,7 +13,7 @@ import { basename, delimiter, dirname, isAbsolute, join, normalize, resolve } fr
|
|
|
13
13
|
import { fileURLToPath } from 'node:url';
|
|
14
14
|
import { EXTRA_DIR, clear, inspect, isInside, packLooksSupported, prunePack, readExtra, removeOne, save } from './writer.js';
|
|
15
15
|
import { addModules, rebuildIndex, scanLibrary } from './importer.js';
|
|
16
|
-
import { ensureStarter, installStarterLibrary, libraryHealth, restorePack, safePackName } from './starter.js';
|
|
16
|
+
import { ensureConsolePreset, ensurePlayer, ensureStarter, ensureVocabNames, installStarterLibrary, libraryHealth, restorePack, safePackName } from './starter.js';
|
|
17
17
|
import { FALLBACK_ACTION_BASE, FALLBACK_LIBRARY, LIB_PARTS, actionUrl, flatActionUrl, installAction, installLibrary, installLibraryParts, libraryUrl } from './online.js';
|
|
18
18
|
import { petFigures, petState } from './pet.js';
|
|
19
19
|
|
|
@@ -91,6 +91,22 @@ export class ExtraActionsWorld {
|
|
|
91
91
|
log: (msg, extra) => host?.log?.info?.(msg, extra),
|
|
92
92
|
});
|
|
93
93
|
if (seeded.pack || seeded.library) this.bustCatalog();
|
|
94
|
+
// 老用户的深深包没有控制台配色预设 → 宿主会把配色钉回 mint(见 console-theme.ts)。
|
|
95
|
+
// 只给自己这个包补,只加不改,幂等。
|
|
96
|
+
try {
|
|
97
|
+
for (const fig of await this.figures()) {
|
|
98
|
+
if (fig?.id === 'shenshen-actions' && fig.dir) {
|
|
99
|
+
const r = ensureConsolePreset(fig.dir, (m) => host?.log?.info?.(m));
|
|
100
|
+
if (r.changed) this.bustCatalog();
|
|
101
|
+
// 播放器也升级:老包的点击回应会播满 10 秒,且没勾"点击段"时完全没反应
|
|
102
|
+
ensurePlayer(fig.dir, join(HERE, '..'), (m) => host?.log?.info?.(m));
|
|
103
|
+
// 词表名字里有宿主不认的字符时,整份 vocab 会被拒 → bot 一个动作都点不出来
|
|
104
|
+
if (ensureVocabNames(fig.dir, (m) => host?.log?.info?.(m)).changed) this.bustCatalog();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
} catch (e) {
|
|
108
|
+
host?.log?.warn?.('补配色预设失败(不影响使用):' + String(e?.message ?? e));
|
|
109
|
+
}
|
|
94
110
|
} catch (e) {
|
|
95
111
|
host?.log?.warn?.('自带素材没能铺开(不影响已有配置)', { err: String(e) });
|
|
96
112
|
}
|
package/src/writer.js
CHANGED
|
@@ -170,13 +170,30 @@ export function readExtra(packDir) {
|
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
/** 按动作库条目造一条 vocab。 */
|
|
173
|
+
/**
|
|
174
|
+
* 写成 figure.json 的 vocab 时,名字必须过宿主的校验(packs.ts 的 readVocab):
|
|
175
|
+
* - NAME_BREAK:不能含 , , 、 空白 【】 <> <>
|
|
176
|
+
* - INLINE_TAG_MAX = 32 字
|
|
177
|
+
* - 名字(含 id)不能重复
|
|
178
|
+
* 注意:一条不合格是**整份 vocab 被拒**(readVocab 直接返回错误),
|
|
179
|
+
* 于是 bot 一个动作都点不出来 —— 不是"那一个动作坏了"。
|
|
180
|
+
*/
|
|
181
|
+
const NAME_BREAK = /[,,、\s【】<><>]/g;
|
|
182
|
+
const NAME_MAX = 32;
|
|
183
|
+
|
|
184
|
+
/** 把名字洗成宿主认的样子;洗完是空的就退回 id。 */
|
|
185
|
+
export function safeWordName(raw, id) {
|
|
186
|
+
const cleaned = String(raw ?? '').replace(NAME_BREAK, '').trim().slice(0, NAME_MAX);
|
|
187
|
+
return cleaned || String(id ?? '');
|
|
188
|
+
}
|
|
189
|
+
|
|
173
190
|
export function vocabEntry(action) {
|
|
174
|
-
const name = action.say || action.zh;
|
|
191
|
+
const name = safeWordName(action.say || action.zh, action.word);
|
|
175
192
|
return {
|
|
176
193
|
id: action.word,
|
|
177
194
|
kind: 'motion',
|
|
178
195
|
names: { zh: [name] },
|
|
179
|
-
about: { zh: `${action.catZh || action.category}:${action.zh}` },
|
|
196
|
+
about: { zh: `${action.catZh || action.category}:${safeWordName(action.zh, action.word)}` },
|
|
180
197
|
seconds: action.seconds,
|
|
181
198
|
};
|
|
182
199
|
}
|
|
@@ -228,9 +245,13 @@ function applyVocab(packDir, actions, managed) {
|
|
|
228
245
|
added.push(a.word);
|
|
229
246
|
}
|
|
230
247
|
const removed = current.filter((w) => !have.has(w?.id)).map((w) => w?.id);
|
|
248
|
+
// 没有实质变化就不落盘:figure.json 也是宿主在读的文件,多余的重写会让
|
|
249
|
+
// 「外观跟着形象走」这类逻辑重新求值,用户改的配色看起来就"掉回默认"。
|
|
250
|
+
const unchanged = sameIds(current.map((w) => w?.id), kept.map((w) => w?.id));
|
|
251
|
+
if (unchanged) return { added, removed, baseVocab: base, written: kept.map((w) => w?.id), wrote: false };
|
|
231
252
|
fig.vocab = kept;
|
|
232
253
|
writeJson(file, fig);
|
|
233
|
-
return { added, removed, baseVocab: base, written: kept.map((w) => w?.id) };
|
|
254
|
+
return { added, removed, baseVocab: base, written: kept.map((w) => w?.id), wrote: true };
|
|
234
255
|
}
|
|
235
256
|
|
|
236
257
|
/** 清空时把词表还回去:先回到 base,实在对不上就只摘掉自己加的那些。 */
|
|
@@ -241,13 +262,18 @@ function restoreVocab(packDir, managed) {
|
|
|
241
262
|
const current = Array.isArray(fig.vocab) ? fig.vocab : [];
|
|
242
263
|
const clean = managed?.written && sameIds(current.map((w) => w?.id), managed.written);
|
|
243
264
|
let restored = false;
|
|
265
|
+
let next;
|
|
244
266
|
if (clean && Array.isArray(managed.baseVocab)) {
|
|
245
|
-
|
|
267
|
+
next = managed.baseVocab;
|
|
246
268
|
restored = true;
|
|
247
269
|
} else {
|
|
248
270
|
const drop = new Set(Array.isArray(managed?.added) ? managed.added : []);
|
|
249
|
-
|
|
271
|
+
next = current.filter((w) => !drop.has(w?.id));
|
|
272
|
+
}
|
|
273
|
+
if (sameIds((next ?? []).map((w) => w?.id), current.map((w) => w?.id))) {
|
|
274
|
+
return { added: [], removed: [], restored, wrote: false }; // 已经是这个状态,不碰文件
|
|
250
275
|
}
|
|
276
|
+
fig.vocab = next;
|
|
251
277
|
writeJson(file, fig);
|
|
252
278
|
return { added: [], removed: [], restored };
|
|
253
279
|
}
|
package/tools/doctor.mjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 形象包体检:查「某个动作为什么点不出名字」这类问题。
|
|
3
|
+
* node doctor.mjs <形象包目录>
|
|
4
|
+
*
|
|
5
|
+
* 检查四件事,任何一条对不上,bot 就调用不出那个词:
|
|
6
|
+
* 1. 额外动作拓展/ 下的模块是否存在、action.json 是否可读
|
|
7
|
+
* 2. 模块里的 word 与 额外动作拓展/index.js 里登记的是否一致
|
|
8
|
+
* 3. figure.json 的 vocab 里是否有这个词(bot 只认 vocab)
|
|
9
|
+
* 4. 模块的素材是否齐(anim.webp 或 f_0001.webp…)
|
|
10
|
+
* 另外把「库里没有、只是清单里有」的词单独列出来(那种必然调用不出来)。
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
13
|
+
import { dirname, join, resolve } from 'node:path';
|
|
14
|
+
|
|
15
|
+
const pack = resolve(process.argv[2] ?? '');
|
|
16
|
+
if (!pack || !existsSync(join(pack, 'figure.json'))) {
|
|
17
|
+
console.error('用法: node doctor.mjs <形象包目录>');
|
|
18
|
+
process.exit(2);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const readJson = (f, d = null) => { try { return JSON.parse(readFileSync(f, 'utf8').replace(/^\uFEFF/, '')); } catch { return d; } };
|
|
22
|
+
const fig = readJson(join(pack, 'figure.json')) ?? {};
|
|
23
|
+
const extrasRoot = join(pack, '额外动作拓展');
|
|
24
|
+
|
|
25
|
+
/* 1. 模块清单 */
|
|
26
|
+
const modules = new Map(); // id -> { cat, dir, json }
|
|
27
|
+
if (existsSync(extrasRoot)) {
|
|
28
|
+
for (const cat of readdirSync(extrasRoot, { withFileTypes: true })) {
|
|
29
|
+
if (!cat.isDirectory()) continue;
|
|
30
|
+
for (const m of readdirSync(join(extrasRoot, cat.name), { withFileTypes: true })) {
|
|
31
|
+
if (!m.isDirectory()) continue;
|
|
32
|
+
const dir = join(extrasRoot, cat.name, m.name);
|
|
33
|
+
const json = readJson(join(dir, 'action.json'));
|
|
34
|
+
modules.set(m.name, { cat: cat.name, dir, json });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/* 2. index.js 里登记的词 */
|
|
40
|
+
// 直接 import 这个 ES module(它是真 JS,可能带尾逗号,JSON.parse 会失败)
|
|
41
|
+
let indexEntries = [];
|
|
42
|
+
const indexFile = join(extrasRoot, 'index.js');
|
|
43
|
+
if (existsSync(indexFile)) {
|
|
44
|
+
try {
|
|
45
|
+
const mod = await import('file:///' + indexFile.replace(/\\/g, '/') + '?v=' + Math.random());
|
|
46
|
+
if (Array.isArray(mod?.default)) indexEntries = mod.default;
|
|
47
|
+
} catch (e) {
|
|
48
|
+
console.log('⚠️ 额外动作拓展/index.js 语法有问题,桌宠会读不了:' + String(e?.message ?? e));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const indexByClip = new Map(indexEntries.map((e) => [e.clip, e]));
|
|
52
|
+
|
|
53
|
+
/* 3. vocab(bot 真正认识的词) */
|
|
54
|
+
const vocab = Array.isArray(fig.vocab) ? fig.vocab : [];
|
|
55
|
+
const vocabIds = new Set(vocab.map((w) => w?.id).filter(Boolean));
|
|
56
|
+
|
|
57
|
+
const problems = [];
|
|
58
|
+
const notes = [];
|
|
59
|
+
|
|
60
|
+
for (const [id, { cat, dir, json }] of modules) {
|
|
61
|
+
const clip = `额外动作拓展/${cat}/${id}`;
|
|
62
|
+
if (!json) { problems.push(`${id}:action.json 读不出来`); continue; }
|
|
63
|
+
const word = json.word ?? null;
|
|
64
|
+
if (!word) { notes.push(`${id}:没有 word(当成状态段,不参与点名)`); continue; }
|
|
65
|
+
|
|
66
|
+
const entry = indexByClip.get(clip);
|
|
67
|
+
if (!entry) problems.push(`${id}:模块在,但 额外动作拓展/index.js 里没有它 → 桌宠看不见`);
|
|
68
|
+
else if (entry.word !== word) problems.push(`${id}:index 登记的词是 ${entry.word},模块里写的是 ${word} → 对不上`);
|
|
69
|
+
|
|
70
|
+
if (!vocabIds.has(word)) problems.push(`${id}(${json.zh ?? ''}):figure.json 的 vocab 里没有 "${word}" → bot 认不出这个词`);
|
|
71
|
+
|
|
72
|
+
const hasAnim = existsSync(join(dir, 'anim.webp'));
|
|
73
|
+
const frames = readdirSync(dir).filter((n) => /^f_\d{4}\.webp$/.test(n)).length;
|
|
74
|
+
if (!hasAnim && frames === 0) problems.push(`${id}:素材一个都没有(既没 anim.webp 也没 f_*.webp)`);
|
|
75
|
+
else if (!hasAnim && json.frames && frames !== json.frames) problems.push(`${id}:帧数 ${frames}/${json.frames}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/* 3.5 vocab 本身会不会被宿主拒(一票否决:一条坏名字 → 整份词表作废) */
|
|
79
|
+
{
|
|
80
|
+
const NAME_BREAK = /[,,、\s【】<><>]/;
|
|
81
|
+
const taken = new Set();
|
|
82
|
+
for (const w of vocab) {
|
|
83
|
+
const id = String(w?.id ?? '');
|
|
84
|
+
const bad = (why) => problems.push(`vocab "${id}":${why} → 宿主会拒掉**整份词表**,bot 一个动作都点不出来`);
|
|
85
|
+
if (!/^[a-z0-9][a-z0-9-]{0,31}$/.test(id)) { bad('id 不合法'); continue; }
|
|
86
|
+
for (const list of Object.values(w?.names ?? {})) {
|
|
87
|
+
for (const n of Array.isArray(list) ? list : []) {
|
|
88
|
+
if (typeof n !== 'string' || !n.trim() || n.length > 32 || NAME_BREAK.test(n)) bad(`名字 ${JSON.stringify(n)} 含禁字符或超 32 字`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (const n of [id, ...Object.values(w?.names ?? {}).flat()]) {
|
|
92
|
+
if (taken.has(n)) bad(`名字重复:${n}`);
|
|
93
|
+
taken.add(n);
|
|
94
|
+
}
|
|
95
|
+
const about = w?.about;
|
|
96
|
+
if (!about || typeof about !== 'object' || !Object.values(about).some((v) => typeof v === 'string' && v.trim())) bad('缺 about');
|
|
97
|
+
if (typeof w?.seconds !== 'number' || !(w.seconds > 0)) bad('seconds 不合法');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* 4. vocab 里有、但包里没有的词(通常是删了模块没重存) */
|
|
102
|
+
for (const w of vocabIds) {
|
|
103
|
+
if (w.startsWith?.('state-')) continue;
|
|
104
|
+
const hit = [...modules.values()].some((m) => (m.json?.word ?? null) === w);
|
|
105
|
+
if (!hit) problems.push(`"${w}":vocab 里有,但包里没有这个模块 → bot 会点名一段不存在的动作`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* 输出 */
|
|
109
|
+
console.log(`形象包:${pack}`);
|
|
110
|
+
console.log(`额外动作模块:${modules.size} 个 | index 条目:${indexEntries.length} 条 | vocab:${vocab.length} 条`);
|
|
111
|
+
console.log('');
|
|
112
|
+
if (notes.length) {
|
|
113
|
+
console.log('— 说明(不算错)');
|
|
114
|
+
for (const n of notes) console.log(' · ' + n);
|
|
115
|
+
console.log('');
|
|
116
|
+
}
|
|
117
|
+
if (!problems.length) {
|
|
118
|
+
console.log('✅ 没发现问题:每个模块都在 index 和 vocab 里,素材也齐。');
|
|
119
|
+
} else {
|
|
120
|
+
console.log(`❌ 发现 ${problems.length} 个问题:`);
|
|
121
|
+
for (const p of problems) console.log(' · ' + p);
|
|
122
|
+
console.log('');
|
|
123
|
+
console.log('修法:在扩展面板里把这段重新勾上 → 点「保存随机列表」→ 完全退出 Coopanion 再打开 → 装扮页重新点一下形象。');
|
|
124
|
+
}
|
|
125
|
+
process.exit(problems.length ? 1 : 0);
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「戳一戳」的回归测试(跑真正的 figure.js,用一层假 DOM)。
|
|
3
|
+
* node test_click.mjs
|
|
4
|
+
*
|
|
5
|
+
* 覆盖:
|
|
6
|
+
* - 清单里有 reaction:true 的段 → 点一下播那一段
|
|
7
|
+
* - 一段 reaction 都没有(只勾了吃饭/伸懒腰这类)→ 也要动(最新兜底)
|
|
8
|
+
* - 回应时长被 REACTION_MAX(2.6s)截断,不是播满 10 秒
|
|
9
|
+
* - 被拎起(drag 模式)时不抢动作
|
|
10
|
+
*/
|
|
11
|
+
import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { dirname, join } from 'node:path';
|
|
13
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
14
|
+
|
|
15
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const PLAYER = join(here, '..', 'assets', 'starter', 'figure', 'figure.js');
|
|
17
|
+
const fails = [];
|
|
18
|
+
const check = (fn, msg) => { try { fn(); console.log('ok ' + msg); } catch (e) { fails.push(`${msg} → ${e.message}`); console.log('FAIL ' + msg); } };
|
|
19
|
+
|
|
20
|
+
/* ---------- 假 DOM ---------- */
|
|
21
|
+
class El {
|
|
22
|
+
constructor(tag) { this.tagName = tag; this.attrs = {}; this.children = []; this.isConnected = true; this.style = {}; }
|
|
23
|
+
setAttribute(k, v) { this.attrs[k] = String(v); }
|
|
24
|
+
getAttribute(k) { return this.attrs[k]; }
|
|
25
|
+
appendChild(c) { this.children.push(c); return c; }
|
|
26
|
+
removeChild(c) { this.children = this.children.filter((x) => x !== c); }
|
|
27
|
+
append(...cs) { for (const c of cs) this.appendChild(c); }
|
|
28
|
+
querySelector() { return null; }
|
|
29
|
+
remove() { this.isConnected = false; }
|
|
30
|
+
}
|
|
31
|
+
globalThis.document = { createElement: (t) => new El(t), createElementNS: (_n, t) => new El(t), querySelectorAll: () => [] };
|
|
32
|
+
// 播放器会把每帧 fetch 成 blob 再换成 objectURL;这里把源地址塞进 blob,
|
|
33
|
+
// 这样 href 里仍能看出是哪一段(否则全是 blob:fake 看不出)。
|
|
34
|
+
URL.createObjectURL = (b) => 'blob:' + String(b?.__url ?? 'x').split('/').slice(-3).join('/');
|
|
35
|
+
URL.revokeObjectURL = () => {};
|
|
36
|
+
globalThis.fetch = async (u) => ({ ok: true, blob: async () => ({ __url: String(u) }) });
|
|
37
|
+
/** 播放器用 new Image() 探测 anim.webp;这里一律失败 → 走逐帧路径,href 里能直接看出是哪一段。 */
|
|
38
|
+
globalThis.Image = class { set src(v) { this._src = v; setTimeout(() => this.onerror?.(), 0); } get src() { return this._src; } };
|
|
39
|
+
let NOW = 0;
|
|
40
|
+
globalThis.performance = { now: () => NOW };
|
|
41
|
+
|
|
42
|
+
/* ---------- 造包 ---------- */
|
|
43
|
+
const root = join(here, '_tmp-click');
|
|
44
|
+
rmSync(root, { recursive: true, force: true });
|
|
45
|
+
|
|
46
|
+
const CLIPS = {
|
|
47
|
+
idle: { id: 'state-idle', cat: 'core', word: null, seconds: 10, roles: ['idle'] },
|
|
48
|
+
eat: { id: 'eat-rice', cat: 'eat', word: 'eat-rice', seconds: 10 },
|
|
49
|
+
shy: { id: 'shy-startle', cat: 'core', word: 'shy-startle', seconds: 10, reaction: true },
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const entry = (f) => ({
|
|
53
|
+
id: f.id, word: f.word, clip: `actions/${f.cat}/${f.id}`, frames: 2, fps: 24, seconds: f.seconds,
|
|
54
|
+
name: f.id, category: f.cat, random: false, ...(f.roles ? { roles: f.roles } : {}), ...(f.reaction ? { reaction: true } : {}),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
function makePack(name, indexClips) {
|
|
58
|
+
const pack = join(root, name);
|
|
59
|
+
for (const f of Object.values(CLIPS)) {
|
|
60
|
+
const d = join(pack, 'actions', f.cat, f.id);
|
|
61
|
+
mkdirSync(d, { recursive: true });
|
|
62
|
+
writeFileSync(join(d, 'action.json'), JSON.stringify({
|
|
63
|
+
id: f.id, category: f.cat, clip: `actions/${f.cat}/${f.id}`, word: f.word, zh: f.id, say: f.id,
|
|
64
|
+
frames: 2, fps: 24, seconds: f.seconds, random: false, ...(f.roles ? { roles: f.roles } : {}), ...(f.reaction ? { reaction: true } : {}),
|
|
65
|
+
}), 'utf8');
|
|
66
|
+
writeFileSync(join(d, 'anim.webp'), 'RIFFxxxxWEBPVP8 ', 'utf8');
|
|
67
|
+
}
|
|
68
|
+
writeFileSync(join(pack, 'actions', 'index.js'), 'export default ' + JSON.stringify(Object.values(CLIPS).map(entry)) + ';\n', 'utf8');
|
|
69
|
+
mkdirSync(join(pack, '额外动作拓展'), { recursive: true });
|
|
70
|
+
writeFileSync(join(pack, '额外动作拓展', 'index.js'), 'export default ' + JSON.stringify(indexClips.map(entry)) + ';\n', 'utf8');
|
|
71
|
+
writeFileSync(join(pack, 'figure.json'), JSON.stringify({ manifest: 2, api: 2, id: 'shenshen-actions', entry: 'figure.js' }), 'utf8');
|
|
72
|
+
return pack;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function load(pack) {
|
|
76
|
+
const mod = await import(pathToFileURL(PLAYER).href + '?v=' + Math.random());
|
|
77
|
+
const petG = new El('g');
|
|
78
|
+
const kit = {
|
|
79
|
+
createBody(_h, o) {
|
|
80
|
+
const fig = o.figure;
|
|
81
|
+
return {
|
|
82
|
+
words: [], step() {}, layout: () => ({ x: 128, facing: 1, mode: 'idle' }), do: () => true,
|
|
83
|
+
pointer: () => true, draw: (...a) => fig.draw(...a), react: () => fig.react(),
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
const body = await mod.createActionBody(pathToFileURL(pack + '/').href, {
|
|
88
|
+
kit,
|
|
89
|
+
host: { root: new El('div'), bounds: () => ({ W: 640, H: 360, floorY: 300, S: 1 }) },
|
|
90
|
+
scheme: null, model: undefined,
|
|
91
|
+
loadImage: async () => new El('img'),
|
|
92
|
+
asset: (p) => new URL(p, pathToFileURL(pack + '/').href),
|
|
93
|
+
});
|
|
94
|
+
return { body, petG };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const href = (petG) => {
|
|
98
|
+
const img = petG.children.find((c) => c.tagName === 'image');
|
|
99
|
+
return img ? decodeURIComponent(String(img.attrs.href ?? '')) : '';
|
|
100
|
+
};
|
|
101
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
102
|
+
/**
|
|
103
|
+
* 画一帧。anim.webp 的探测是异步的(真播放器要点一下才知道能不能动),
|
|
104
|
+
* 探测没回来之前播放器会「保持上一帧」,所以这里等一下让探针落地。
|
|
105
|
+
*/
|
|
106
|
+
const drawAt = async (body, petG, t, mode = 'idle') => {
|
|
107
|
+
// 画三次、中间各让一次事件循环:切换片段 → 探测 anim.webp(异步)→ 下一帧才真正换图
|
|
108
|
+
for (let i = 0; i < 3; i++) {
|
|
109
|
+
NOW = t * 1000;
|
|
110
|
+
body.draw(petG, {}, { t, mode, facing: 1 });
|
|
111
|
+
await sleep(10);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const click = (body, at) => { NOW = at; body.pointer('down', { x: 100, y: 100 }); NOW = at + 60; body.pointer('up', { x: 100, y: 100 }); };
|
|
115
|
+
|
|
116
|
+
/* ① 有"点击段":播它,2.6s 后回待机 */
|
|
117
|
+
{
|
|
118
|
+
const pack = makePack('with-reaction', [CLIPS.shy, CLIPS.eat]);
|
|
119
|
+
const { body, petG } = await load(pack);
|
|
120
|
+
await drawAt(body, petG, 0); drawAt(body, petG, 0.1);
|
|
121
|
+
click(body, 100);
|
|
122
|
+
await drawAt(body, petG, 0.2);
|
|
123
|
+
check(() => { const h = href(petG); if (!/shy-startle/.test(h)) throw new Error('点完播的是:' + h); }, '有 reaction 段时:点一下播那一段');
|
|
124
|
+
await drawAt(body, petG, 2.4);
|
|
125
|
+
check(() => { const h = href(petG); if (!/shy-startle/.test(h)) throw new Error('2.4 秒就回待机了:' + h); }, '2.4 秒时仍在播(没被过早截断)');
|
|
126
|
+
await drawAt(body, petG, 3.2);
|
|
127
|
+
check(() => { const h = href(petG); if (!/state-idle/.test(h)) throw new Error('3.2 秒还在播:' + h); }, '过了 2.6 秒回待机');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* ② 没有"点击段":也要动 */
|
|
131
|
+
{
|
|
132
|
+
const pack = makePack('no-reaction', [CLIPS.eat]);
|
|
133
|
+
const { body, petG } = await load(pack);
|
|
134
|
+
await drawAt(body, petG, 0); drawAt(body, petG, 0.1);
|
|
135
|
+
click(body, 100);
|
|
136
|
+
await drawAt(body, petG, 0.2);
|
|
137
|
+
check(() => { const h = href(petG); if (!/eat-rice/.test(h)) throw new Error('完全没有反应:' + h); }, '没有"点击段"时也会挑一段响应(兜底)');
|
|
138
|
+
await drawAt(body, petG, 3.1);
|
|
139
|
+
check(() => { const h = href(petG); if (!/state-idle/.test(h)) throw new Error('没回待机:' + h); }, '兜底回应同样按时回待机');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/* ③ 被拎起时不抢动作 */
|
|
143
|
+
{
|
|
144
|
+
const pack = makePack('drag', [CLIPS.shy]);
|
|
145
|
+
const { body, petG } = await load(pack);
|
|
146
|
+
await drawAt(body, petG, 0, 'drag');
|
|
147
|
+
check(() => { if (body.react() !== false) throw new Error('drag 时还抢了动作'); }, '被拎起时不响应点击');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/* ④ 空包不抛错 */
|
|
151
|
+
{
|
|
152
|
+
const pack = makePack('empty', []);
|
|
153
|
+
const { body, petG } = await load(pack);
|
|
154
|
+
await drawAt(body, petG, 0);
|
|
155
|
+
check(() => { body.react(); }, '什么都没有时点一下不抛错');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/* ⑤ 播放器文件本身带标记 */
|
|
159
|
+
check(() => {
|
|
160
|
+
const s = readFileSync(PLAYER, 'utf8');
|
|
161
|
+
if (!s.includes('REACTION_MAX')) throw new Error('没有 REACTION_MAX');
|
|
162
|
+
if (!s.includes('pool = [...allow].filter((w) => byWord.has(w));')) throw new Error('没有"随便挑一段"的兜底');
|
|
163
|
+
}, '播放器里有 REACTION_MAX 与点击兜底');
|
|
164
|
+
|
|
165
|
+
rmSync(root, { recursive: true, force: true });
|
|
166
|
+
console.log(fails.length ? `\nFAIL (${fails.length})\n- ${fails.join('\n- ')}` : '\nall checks passed');
|
|
167
|
+
process.exit(fails.length ? 1 : 0);
|