cortico-world-extra-actions 0.3.0 → 0.3.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/dist/webui.js CHANGED
@@ -221,6 +221,7 @@ const panel = {
221
221
  <select class="ea-sel" data-role="pack"></select>
222
222
  <input class="ea-inp" data-role="q" placeholder="搜名字 / id…">
223
223
  <button class="ea-btn" data-act="library">选择动作库…</button>
224
+ <button class="ea-btn" data-act="restore-pack" title="创建一个空的深深形象包(只有身体状态)">恢复空的形象包</button>
224
225
  <span class="ea-lib" data-role="lib"></span>
225
226
  </div>
226
227
  <div class="ea-note" data-role="note" hidden></div>
@@ -262,7 +263,7 @@ const panel = {
262
263
  const REQUIRED = [
263
264
  '[data-role=pack]', '[data-role=q]', '[data-role=lib]', '[data-role=note]',
264
265
  '[data-role=installed]', '[data-role=chips]', '[data-role=list]', '[data-role=sum]',
265
- '.ea-foot', '[data-act=save]', '[data-act=clear]', '[data-act=add]', '[data-act=prune]', '[data-act=library]',
266
+ '.ea-foot', '[data-act=save]', '[data-act=clear]', '[data-act=add]', '[data-act=prune]', '[data-act=library]', '[data-act=restore-pack]',
266
267
  ];
267
268
  const missing = REQUIRED.filter((sel) => !wrap.querySelector(sel));
268
269
  if (missing.length) throw new Error('面板模板缺少节点:' + missing.join(', '));
@@ -289,7 +290,7 @@ const panel = {
289
290
  const list = (state?.figures ?? []).filter((f) => f.supported && !f.builtin);
290
291
  packSel.innerHTML = '';
291
292
  if (!list.length) {
292
- packSel.appendChild(el('option', null, '没有可写的形象包'));
293
+ packSel.appendChild(el('option', null, '没有可写的形象包(点「恢复空的形象包」)'));
293
294
  packSel.disabled = true;
294
295
  return;
295
296
  }
@@ -567,6 +568,18 @@ const panel = {
567
568
  ? `已删除「${a?.zh ?? id}」,包里没有额外动作了,词表已还原`
568
569
  : `已删除「${a?.zh ?? id}」,还剩 ${r.remaining} 段`);
569
570
  renderPack(); packSel.value = packId; renderList(); renderInstalled(); renderSum(); renderNote();
571
+ } else if (act === 'restore-pack') {
572
+ if (!confirm('在形象包目录里创建一个「空的深深形象包」?\n\n只有 4 段身体状态,没有额外动作、词表空。\n如果已经有同 id 的包,只检查并补齐缺失文件,不会再建第二个。')) return;
573
+ busy(true); say('正在准备形象包…');
574
+ const r = await ctx.invoke('restore-pack', []);
575
+ if (r.created) say('已创建空的深深形象包');
576
+ else if (r.repaired) say('形象包缺文件,已补齐');
577
+ else say('形象包已存在,没有重复创建');
578
+ if (r.duplicates && r.duplicates.length) {
579
+ say(`另外发现 ${r.duplicates.length} 个重复的同 id 包,请手动删掉:` + r.duplicates.map((p) => String(p).split(/[\\/]/).pop()).join('、'), true);
580
+ }
581
+ await loadAll();
582
+ if (r.dir) { packSel.value = r.dir; renderNote(); renderList(); renderInstalled(); renderSum(); }
570
583
  } else if (act === 'add') {
571
584
  let dir = null;
572
585
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-extra-actions",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "勾选动作库里的动作,写进形象包的「额外动作拓展」文件夹,让桌宠照着做。自带入门形象包与示例动作,装完即用。",
package/src/starter.js CHANGED
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * 这样装完插件就有东西可勾;105 段全量动作库仍然可以在面板里「选择动作库…」指过去。
8
8
  */
9
- import { cpSync, existsSync, mkdirSync } from 'node:fs';
9
+ import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
10
10
  import { join } from 'node:path';
11
11
 
12
12
  const PACK_ID = 'shenshen-actions';
@@ -14,6 +14,94 @@ const PACK_ID = 'shenshen-actions';
14
14
  /** 内置素材的根目录:<包>/assets/starter */
15
15
  const starterRoot = (pkgDir) => join(pkgDir, 'assets', 'starter');
16
16
 
17
+ const readJson = (file, fallback = null) => {
18
+ try { return JSON.parse(readFileSync(file, 'utf8').replace(/^\uFEFF/, '')); } catch { return fallback; }
19
+ };
20
+
21
+ /** 这个包里该有的东西齐不齐。 */
22
+ function packHealth(dir) {
23
+ const fig = readJson(join(dir, 'figure.json'), null);
24
+ const states = ['state-idle', 'state-walk', 'state-drag', 'look-around']
25
+ .filter((s) => existsSync(join(dir, 'actions', 'core', s, 'action.json')));
26
+ return {
27
+ figureJson: !!fig && fig.id === PACK_ID,
28
+ player: existsSync(join(dir, 'figure.js')),
29
+ states,
30
+ ok: !!fig && fig.id === PACK_ID && existsSync(join(dir, 'figure.js')) && states.length === 4,
31
+ };
32
+ }
33
+
34
+ /** figures 目录下所有声明自己是 shenshen-actions 的文件夹(用于查重)。 */
35
+ function packsClaimingId(figures) {
36
+ const out = [];
37
+ if (!existsSync(figures)) return out;
38
+ for (const e of readdirSync(figures, { withFileTypes: true })) {
39
+ if (!e.isDirectory() || e.name.startsWith('.')) continue;
40
+ const fig = readJson(join(figures, e.name, 'figure.json'), null);
41
+ if (fig && fig.id === PACK_ID) out.push({ dir: e.name, path: join(figures, e.name), bot: packHealth(join(figures, e.name)) });
42
+ }
43
+ return out;
44
+ }
45
+
46
+ /**
47
+ * 一键得到「空的深深形象包」:只有 4 段身体状态,没有额外动作、词表也是空的。
48
+ *
49
+ * 保证同时只有一个:
50
+ * - 已经有(哪怕文件夹被改了名)→ 绝不再造一个,只把缺的东西补上;
51
+ * - 找到多个 → 不动任何文件,把重复的报出来让人自己删。
52
+ */
53
+ export function restorePack({ pkgDir, figuresDir, log }) {
54
+ const figures = figuresDir();
55
+ const src = join(starterRoot(pkgDir), 'figure');
56
+ if (!existsSync(join(src, 'figure.js'))) throw new Error('插件里没有自带形象包(assets/starter/figure 缺失)');
57
+
58
+ const found = packsClaimingId(figures);
59
+ const canonical = found.find((p) => p.dir === PACK_ID) ?? null;
60
+ const others = found.filter((p) => p.dir !== PACK_ID);
61
+
62
+ /* 已经有:修一修(缺了才补),绝不重复造 */
63
+ if (found.length) {
64
+ const target = canonical ?? found[0];
65
+ const before = target.bot;
66
+ if (!before.ok) {
67
+ // 只补缺的那部分:figure.js / figure.json / 状态段
68
+ if (!before.player) cpSync(join(src, 'figure.js'), join(target.path, 'figure.js'));
69
+ if (!before.figureJson) {
70
+ const fig = readJson(join(src, 'figure.json'), { id: PACK_ID });
71
+ writeFileSync(join(target.path, 'figure.json'), JSON.stringify(fig, null, 2) + '\n', 'utf8');
72
+ }
73
+ for (const s of ['state-idle', 'state-walk', 'state-drag', 'look-around']) {
74
+ if (!existsSync(join(target.path, 'actions', 'core', s, 'action.json'))) {
75
+ cpSync(join(src, 'actions', 'core', s), join(target.path, 'actions', 'core', s), { recursive: true, force: false });
76
+ }
77
+ }
78
+ }
79
+ const after = packHealth(target.path);
80
+ log?.(`形象包已存在,未重复创建:${target.path}`);
81
+ return {
82
+ ok: true,
83
+ created: false,
84
+ repaired: !before.ok && after.ok,
85
+ path: target.path,
86
+ dir: target.dir,
87
+ duplicates: others.map((p) => p.path),
88
+ health: after,
89
+ };
90
+ }
91
+
92
+ /* 没有:造一个空的 */
93
+ mkdirSync(figures, { recursive: true });
94
+ const dst = join(figures, PACK_ID);
95
+ cpSync(src, dst, { recursive: true, force: false, errorOnExist: false });
96
+ // 清空额外动作与词表 —— "空的"形象包
97
+ rmSync(join(dst, '额外动作拓展'), { recursive: true, force: true });
98
+ const fig = readJson(join(dst, 'figure.json'), { id: PACK_ID });
99
+ fig.vocab = [];
100
+ writeFileSync(join(dst, 'figure.json'), JSON.stringify(fig, null, 2) + '\n', 'utf8');
101
+ log?.(`空的形象包已创建:${dst}`);
102
+ return { ok: true, created: true, repaired: false, path: dst, dir: PACK_ID, duplicates: [], health: packHealth(dst) };
103
+ }
104
+
17
105
  /**
18
106
  * @param {{ pkgDir: string, figuresDir: () => string, dataDir: string, libraryDir: () => string, saveSettings: (patch: object) => void, log?: (msg: string, extra?: object) => void }} o
19
107
  */
package/src/world.js CHANGED
@@ -13,7 +13,7 @@ import { dirname, isAbsolute, join, normalize, resolve } from 'node:path';
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 } from './starter.js';
16
+ import { ensureStarter, restorePack } from './starter.js';
17
17
  import { petFigures, petState } from './pet.js';
18
18
 
19
19
  const ID = 'extra-actions';
@@ -282,6 +282,8 @@ export class ExtraActionsWorld {
282
282
  return this.prune(this.cleanPackId(a[0]));
283
283
  case 'reindex':
284
284
  return this.reindex();
285
+ case 'restore-pack':
286
+ return this.restoreShenshenPack();
285
287
  case 'save': {
286
288
  const packId = this.cleanPackId(a[0]);
287
289
  const ids = Array.isArray(a[1]) ? a[1].slice(0, MAX_SELECTION).map((v) => String(v).slice(0, 40)) : [];
@@ -371,6 +373,17 @@ export class ExtraActionsWorld {
371
373
  return { ok: true, libraryDir: this.libraryDir() };
372
374
  }
373
375
 
376
+ /** 一键(重新)得到一个空的深深形象包;同时只允许存在一个。 */
377
+ async restoreShenshenPack() {
378
+ const r = restorePack({
379
+ pkgDir: join(HERE, '..'),
380
+ figuresDir: () => this.figuresDir(),
381
+ log: (msg) => this.host?.log?.info?.(msg),
382
+ });
383
+ this.bustCatalog();
384
+ return r;
385
+ }
386
+
374
387
  /** 由 action.json 重生成 actions/index.js —— 清单和磁盘对不上时的万能修复。 */
375
388
  async reindex() {
376
389
  const lib = this.libraryDir();