cortico-world-extra-actions 0.3.23 → 0.3.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-extra-actions",
3
- "version": "0.3.23",
3
+ "version": "0.3.25",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "勾选动作库里的动作,写进形象包的「额外动作拓展」文件夹,让桌宠照着做。自带入门形象包与示例动作;完整 105 段可一键下载安装(不用解压、不用选文件夹)。",
package/src/writer.js CHANGED
@@ -131,9 +131,10 @@ export function packLooksSupported(packDir) {
131
131
  const st = lstatSync(entry, { throwIfNoEntry: false });
132
132
  if (!st || !st.isFile() || st.size > 2 * 1024 * 1024) return false;
133
133
  const js = readText(entry) ?? '';
134
- // 只认真正会去读这个文件夹的播放器:动作库那个(actions/index.js + 额外动作拓展/index.js)。
135
- // 早先那个只认 ext/clips.js 的补丁不认,别的角色(horo、clawd…)也不认。
136
- return /额外动作拓展|actions\/index\.js|actions\/index['"]/.test(js);
134
+ // 只认入口里真的写着「额外动作拓展」的播放器 —— 那是唯一会去读我们写的文件夹的约定。
135
+ // 不能按 `actions/index.js` 放行:别的作者按帧序列做的形象包也有这个入口,
136
+ // 写进去会连带改它的 figure.json 词表,把它自己的动作词洗掉(扩展市场 Issue #3)。
137
+ return js.includes(EXTRA_DIR);
137
138
  }
138
139
 
139
140
  /** 当前这个包里已经装了哪些额外动作。 */
@@ -202,7 +203,22 @@ function applyVocab(packDir, actions, managed) {
202
203
  const base = (clean && Array.isArray(managed.baseVocab)) ? managed.baseVocab : current;
203
204
 
204
205
  const wanted = new Set(actions.map((a) => a.word).filter(Boolean));
205
- const kept = base.filter((w) => w?.kind !== 'motion' || wanted.has(w?.id));
206
+ // 判据不是「谁加的」,而是「这段动作还在不在包里」:
207
+ // - 勾了的:留;
208
+ // - 没勾但包里还有这段模块(身体状态、别人自带动作):也留 —— 不能替人删词;
209
+ // - 没勾且模块已经删掉:这才回收,免得 bot 点名一个不存在的动作。
210
+ const hasModule = (id) => {
211
+ if (!id) return false;
212
+ for (const base of [join(packDir, EXTRA_DIR), join(packDir, 'actions')]) {
213
+ try {
214
+ for (const cat of readdirSync(base, { withFileTypes: true })) {
215
+ if (cat.isDirectory() && existsSync(join(base, cat.name, String(id), 'action.json'))) return true;
216
+ }
217
+ } catch { /* 目录不存在就算了 */ }
218
+ }
219
+ return false;
220
+ };
221
+ const kept = base.filter((w) => w?.kind !== 'motion' || wanted.has(w?.id) || hasModule(w?.id));
206
222
  const have = new Set(kept.map((w) => w?.id));
207
223
  const added = [];
208
224
  for (const a of actions) {
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Cortico 扩展市场 Issue #3 的回归测试。
3
+ *
4
+ * 1) 只认入口里写着「额外动作拓展」的形象包;别的作者按帧序列做的包(入口只提
5
+ * actions/index.js)必须判为「不可写」,否则我们保存时会改它的 figure.json。
6
+ * 2) 保存只添加/回收「我们自己加过的」动作词,包里原有的 motion 词一条都不许少。
7
+ */
8
+ import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
9
+ import { dirname, join } from 'node:path';
10
+ import { fileURLToPath, pathToFileURL } from 'node:url';
11
+ import assert from 'node:assert/strict';
12
+
13
+ const here = dirname(fileURLToPath(import.meta.url));
14
+ const W = await import(pathToFileURL(join(here, '..', 'src', 'writer.js')).href);
15
+
16
+ const fails = [];
17
+ const check = (fn, msg) => { try { fn(); console.log('ok ' + msg); } catch (e) { fails.push(`${msg} → ${e.message}`); console.log('FAIL ' + msg); } };
18
+
19
+ const root = join(here, '_tmp-issue3');
20
+ rmSync(root, { recursive: true, force: true });
21
+
22
+ /** 造一个形象包:入口脚本写什么由参数决定。 */
23
+ function makePack(name, { entryJs, vocab }) {
24
+ const dir = join(root, name);
25
+ mkdirSync(dir, { recursive: true });
26
+ writeFileSync(join(dir, 'figure.json'), JSON.stringify({
27
+ manifest: 2, api: 2, id: name, version: '1.0.0',
28
+ name: { zh: name }, entry: 'figure.js', vocab,
29
+ }, null, 2), 'utf8');
30
+ writeFileSync(join(dir, 'figure.js'), entryJs, 'utf8');
31
+ return dir;
32
+ }
33
+
34
+ /* ① 别人的帧序列包:入口只提 actions/index.js */
35
+ const foreign = makePack('foreign-pack', {
36
+ entryJs: `export const manifest = 2;\nimport './actions/index.js';\n// 只读 actions/index.js 的播放器\n`,
37
+ vocab: [
38
+ { id: 'hello', kind: 'motion', names: { zh: ['打招呼'] }, seconds: 2 },
39
+ { id: 'bye', kind: 'motion', names: { zh: ['再见'] }, seconds: 2 },
40
+ { id: 'smile', kind: 'expression', names: { zh: ['微笑'] } },
41
+ ],
42
+ });
43
+ check(() => assert.equal(W.packLooksSupported(foreign), false, '别人的帧序列包不能被判为可写'), '只提 actions/index.js 的包判为不可写');
44
+
45
+ /* ② 我们的包:入口里写着 额外动作拓展 */
46
+ const ours = makePack('our-pack', {
47
+ entryJs: `const EXTRA_DIR = '额外动作拓展';\nimport './actions/index.js';\nimport './额外动作拓展/index.js';\n`,
48
+ vocab: [
49
+ { id: 'own-word', kind: 'motion', names: { zh: ['包里原有的动作'] }, seconds: 3 },
50
+ { id: 'body', kind: 'expression', names: { zh: ['待机'] } },
51
+ ],
52
+ });
53
+ check(() => assert.equal(W.packLooksSupported(ours), true), '写了 额外动作拓展 的包判为可写');
54
+
55
+ /* ③ 保存:包里原有的 motion 词必须留着,只加我们勾的 */
56
+ // pack 原有的动作补模块
57
+ // 包里原有的动作,真实情况下一定有自己的模块目录(词能对上片段)
58
+ {
59
+ const d = join(ours, 'actions', 'core', 'own-word');
60
+ mkdirSync(d, { recursive: true });
61
+ writeFileSync(join(d, 'action.json'), JSON.stringify({ id: 'own-word', category: 'core', clip: 'actions/core/own-word', frames: 2, seconds: 3 }), 'utf8');
62
+ }
63
+
64
+ const lib = join(root, 'lib');
65
+ const modDir = join(lib, 'actions', 'core', 'cheer-hop');
66
+ mkdirSync(modDir, { recursive: true });
67
+ writeFileSync(join(modDir, 'action.json'), JSON.stringify({
68
+ id: 'cheer-hop', category: 'core', word: 'cheer-hop', zh: '开心跃动', say: '开心跃动',
69
+ clip: 'actions/core/cheer-hop', frames: 2, fps: 24, seconds: 1, anim: true,
70
+ }), 'utf8');
71
+ writeFileSync(join(modDir, 'anim.webp'), 'RIFFxxxxWEBPVP8 ', 'utf8');
72
+
73
+ const action = {
74
+ id: 'cheer-hop', category: 'core', word: 'cheer-hop', zh: '开心跃动', say: '开心跃动',
75
+ clip: 'actions/core/cheer-hop', frames: 2, fps: 24, seconds: 1, anim: true,
76
+ };
77
+ W.save({ packDir: ours, libraryDir: lib, ids: ['cheer-hop'], catalog: [action] });
78
+ const fig = JSON.parse(readFileSync(join(ours, 'figure.json'), 'utf8'));
79
+ const ids = fig.vocab.map((w) => w.id);
80
+ check(() => assert.ok(ids.includes('own-word'), '包里原有的 motion 词没被删'), '保存后 own-word 还在');
81
+ check(() => assert.ok(ids.includes('body'), '包里原有的 expression 词没被删'), '保存后 body 还在');
82
+ check(() => assert.ok(ids.includes('cheer-hop'), '勾选的动作被加进词表'), '保存后 cheer-hop 已加入');
83
+
84
+ /* ④ 再保存成空:只回收我们加的,原有的一条不少 */
85
+ W.clear(ours, [action]);
86
+ const fig2 = JSON.parse(readFileSync(join(ours, 'figure.json'), 'utf8'));
87
+ const ids2 = fig2.vocab.map((w) => w.id);
88
+ check(() => assert.equal(ids2.includes('cheer-hop'), false, '取消勾选后我们加的词被回收'), '清空后 cheer-hop 已回收');
89
+ check(() => assert.deepEqual(ids2.sort(), ['body', 'own-word']), '清空后包原有的词完整保留');
90
+
91
+ /* ⑤ 别人的包:即使强行调用 save 也不该被改(写之前会判 supported) */
92
+ const before = readFileSync(join(foreign, 'figure.json'), 'utf8');
93
+ check(() => assert.equal(W.packLooksSupported(foreign), false), '外来包始终不可写(保存入口会因此拒绝)');
94
+ check(() => assert.equal(readFileSync(join(foreign, 'figure.json'), 'utf8'), before), '外来包的 figure.json 一个字节都没变');
95
+
96
+ rmSync(root, { recursive: true, force: true });
97
+ console.log(fails.length ? `\nFAIL (${fails.length})\n- ${fails.join('\n- ')}` : '\nall checks passed');
98
+ process.exit(fails.length ? 1 : 0);