cortico-world-extra-actions 0.3.23 → 0.3.24

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.24",
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,10 @@ 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
+ // (旧逻辑是「删掉所有没被勾选的 motion 词」,对外来形象包等于摧毁它的词表。)
208
+ const mine = new Set(Array.isArray(managed?.added) ? managed.added : []);
209
+ const kept = base.filter((w) => !mine.has(w?.id) || wanted.has(w?.id));
206
210
  const have = new Set(kept.map((w) => w?.id));
207
211
  const added = [];
208
212
  for (const a of actions) {
@@ -0,0 +1,90 @@
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
+ const lib = join(root, 'lib');
57
+ const modDir = join(lib, 'actions', 'core', 'cheer-hop');
58
+ mkdirSync(modDir, { recursive: true });
59
+ writeFileSync(join(modDir, 'action.json'), JSON.stringify({
60
+ id: 'cheer-hop', category: 'core', word: 'cheer-hop', zh: '开心跃动', say: '开心跃动',
61
+ clip: 'actions/core/cheer-hop', frames: 2, fps: 24, seconds: 1, anim: true,
62
+ }), 'utf8');
63
+ writeFileSync(join(modDir, 'anim.webp'), 'RIFFxxxxWEBPVP8 ', 'utf8');
64
+
65
+ const action = {
66
+ id: 'cheer-hop', category: 'core', word: 'cheer-hop', zh: '开心跃动', say: '开心跃动',
67
+ clip: 'actions/core/cheer-hop', frames: 2, fps: 24, seconds: 1, anim: true,
68
+ };
69
+ W.save({ packDir: ours, libraryDir: lib, ids: ['cheer-hop'], catalog: [action] });
70
+ const fig = JSON.parse(readFileSync(join(ours, 'figure.json'), 'utf8'));
71
+ const ids = fig.vocab.map((w) => w.id);
72
+ check(() => assert.ok(ids.includes('own-word'), '包里原有的 motion 词没被删'), '保存后 own-word 还在');
73
+ check(() => assert.ok(ids.includes('body'), '包里原有的 expression 词没被删'), '保存后 body 还在');
74
+ check(() => assert.ok(ids.includes('cheer-hop'), '勾选的动作被加进词表'), '保存后 cheer-hop 已加入');
75
+
76
+ /* ④ 再保存成空:只回收我们加的,原有的一条不少 */
77
+ W.clear(ours, [action]);
78
+ const fig2 = JSON.parse(readFileSync(join(ours, 'figure.json'), 'utf8'));
79
+ const ids2 = fig2.vocab.map((w) => w.id);
80
+ check(() => assert.equal(ids2.includes('cheer-hop'), false, '取消勾选后我们加的词被回收'), '清空后 cheer-hop 已回收');
81
+ check(() => assert.deepEqual(ids2.sort(), ['body', 'own-word']), '清空后包原有的词完整保留');
82
+
83
+ /* ⑤ 别人的包:即使强行调用 save 也不该被改(写之前会判 supported) */
84
+ const before = readFileSync(join(foreign, 'figure.json'), 'utf8');
85
+ check(() => assert.equal(W.packLooksSupported(foreign), false), '外来包始终不可写(保存入口会因此拒绝)');
86
+ check(() => assert.equal(readFileSync(join(foreign, 'figure.json'), 'utf8'), before), '外来包的 figure.json 一个字节都没变');
87
+
88
+ rmSync(root, { recursive: true, force: true });
89
+ console.log(fails.length ? `\nFAIL (${fails.length})\n- ${fails.join('\n- ')}` : '\nall checks passed');
90
+ process.exit(fails.length ? 1 : 0);