cortico-world-extra-actions 0.3.19 → 0.3.21

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.19",
3
+ "version": "0.3.21",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "勾选动作库里的动作,写进形象包的「额外动作拓展」文件夹,让桌宠照着做。自带入门形象包与示例动作;完整 105 段可一键下载安装(不用解压、不用选文件夹)。",
package/src/online.js CHANGED
@@ -11,14 +11,37 @@
11
11
  */
12
12
  import { existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs';
13
13
  import { isAbsolute, join, normalize, sep } from 'node:path';
14
+ import { createHash } from 'node:crypto';
14
15
  import { extractZip, listZip } from './zip.js';
16
+ import { extractTarGz } from './tar.js';
15
17
 
16
18
  /** 维护者把 Gitee 仓库的 raw/下载前缀填这里(末尾不要带斜杠)。 */
17
19
  export const DEFAULT_REMOTE = '';
18
20
 
21
+ /** 动作库的分片 npm 包(npmmirror 会自动镜像,国内直连)。sha256 用于完整性校验。 */
22
+ export const LIB_PARTS = [
23
+ { name: 'cortico-world-shenshen-lib-1', version: '1.0.0', modules: 15, bytes: 51758812, sha256: 'db10bf40ae3078d087966b048fd1ab9704b16ffd99179e80799e75ddf2efc1ca' },
24
+ { name: 'cortico-world-shenshen-lib-2', version: '1.0.0', modules: 15, bytes: 53693012, sha256: '541bb2ce6ee0ccc53aa9437213ec15e7b981ee0edb04b33037d941c235b6fcff' },
25
+ { name: 'cortico-world-shenshen-lib-3', version: '1.0.0', modules: 15, bytes: 53643090, sha256: 'b4a908bf0c4fb778829e13ca230801d3110935104d6b5b2e692131372e946182' },
26
+ { name: 'cortico-world-shenshen-lib-4', version: '1.0.0', modules: 13, bytes: 53889958, sha256: 'c528c70954cb20afb57568c85ada3b6af8ca274a059bde9c2cba8774e37f65fe' },
27
+ { name: 'cortico-world-shenshen-lib-5', version: '1.0.0', modules: 13, bytes: 52249938, sha256: '575b60e97fe87198dc9794f3848b6cfd422238df081810ac7169c804cfa80212' },
28
+ { name: 'cortico-world-shenshen-lib-6', version: '1.0.0', modules: 14, bytes: 51269171, sha256: '19b06067b724555e36633ba63f0696631287278447e78d4a0606509de4b03f64' },
29
+ { name: 'cortico-world-shenshen-lib-7', version: '1.0.0', modules: 14, bytes: 51556774, sha256: 'a533d38e5504156539bbbdcf4bf231d6162f9033a83ab82317cd5c72be934a6e' },
30
+ { name: 'cortico-world-shenshen-lib-8', version: '1.0.0', modules: 6, bytes: 20030747, sha256: 'ac8fdf8c0927369226ca09f646f2a8a40c167a954260670463c64727713d0a81' },
31
+ ];
32
+
33
+ /** 分片 tarball 地址:国内优先 npmmirror,失败再退回官方 registry。 */
34
+ export const partTarballs = (p) => [
35
+ `https://registry.npmmirror.com/${p.name}/-/${p.name}-${p.version}.tgz`,
36
+ `https://registry.npmjs.org/${p.name}/-/${p.name}-${p.version}.tgz`,
37
+ ];
38
+
19
39
  const MAX_LIBRARY_BYTES = 800 * 1024 * 1024; // 完整库上限
20
40
  const MAX_ACTION_BYTES = 32 * 1024 * 1024; // 单个动作上限
21
41
  /** 没配 remoteBase 时的兜底:GitHub Release 上的完整库(国内可能慢,但不至于完全没有)。 */
42
+ /** 单段动作的兜底地址:GitHub Release 上的扁平资产 `<id>.zip`(Gitee 建好后用 remoteActionBase 覆盖)。 */
43
+ export const FALLBACK_ACTION_BASE = 'https://github.com/MOYU-AI-Lab/cortico-world-extra-actions/releases/download/per-action';
44
+
22
45
  export const FALLBACK_LIBRARY = 'https://github.com/MOYU-AI-Lab/cortico-world-extra-actions/releases/download/library/shenshen-action-library.zip';
23
46
  /** 只收这些条目:动作库的两种格式(帧序列 / 单文件动画)。 */
24
47
  const WANT = /(^|\/)actions\/[a-z][a-z0-9-]*\/[a-z0-9][a-z0-9-]*\/(action\.json|anim\.webp|preview\.webp|f_\d{4}\.webp)$/;
@@ -116,3 +139,39 @@ export const libraryUrl = (base) => `${String(base).replace(/\/+$/, '')}/library
116
139
  export const actionUrl = (base, category, id) => `${String(base).replace(/\/+$/, '')}/actions/${category}/${id}.zip`;
117
140
  /** 扁平布局(GitHub Release 的资产做不到子目录):<base>/<id>.zip */
118
141
  export const flatActionUrl = (base, id) => `${String(base).replace(/\/+$/, '')}/${id}.zip`;
142
+
143
+
144
+ /**
145
+ * 分片安装:逐个下载 npm 包 tarball → 校验 sha256 → 解压进动作库。
146
+ * 一片失败不影响已装好的;返回装了哪些模块。
147
+ */
148
+ export async function installLibraryParts({ parts = LIB_PARTS, dest, log, onProgress }) {
149
+ mkdirSync(dest, { recursive: true });
150
+ const before = new Set(summarize(dest));
151
+ let bytes = 0;
152
+ let failed = 0;
153
+ for (let i = 0; i < parts.length; i++) {
154
+ const p = parts[i];
155
+ let buf = null;
156
+ let lastErr = null;
157
+ for (const url of partTarballs(p)) {
158
+ try { buf = await fetchBytes(url, MAX_LIBRARY_BYTES); break; } catch (e) { lastErr = e; }
159
+ }
160
+ if (!buf) { failed++; log?.(`第 ${i + 1} 片下载失败:${lastErr?.message ?? '未知错误'}`); continue; }
161
+ bytes += buf.length;
162
+ if (p.sha256) {
163
+ const got = createHash('sha256').update(buf).digest('hex');
164
+ if (got !== p.sha256) { failed++; log?.(`第 ${i + 1} 片校验不通过,已跳过`); continue; }
165
+ }
166
+ const r = extractTarGz(buf, {
167
+ dest,
168
+ mapPath: (rel) => (rel.startsWith('package/') ? rel.slice(8) : rel),
169
+ onFile: (rel) => WANT.test(rel),
170
+ });
171
+ log?.(`第 ${i + 1}/${parts.length} 片:${r.files.length} 个文件`);
172
+ onProgress?.(i + 1, parts.length);
173
+ }
174
+ const mods = summarize(dest);
175
+ const added = mods.filter((m) => !before.has(m));
176
+ return { ok: failed === 0, path: dest, modules: mods.length, added: added.length, failed, bytes };
177
+ }
package/src/tar.js ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * 最小 tar.gz 读取器(只用 node:zlib)。
3
+ *
4
+ * npm 包的 tarball 就是 tar.gz —— 动作库按 npm 包分发时得能解它。
5
+ * 安全约束与 zip 那边一致:不安全路径整条丢掉,只允许 onFile 认的文件落盘。
6
+ */
7
+ import { mkdirSync, writeFileSync } from 'node:fs';
8
+ import { dirname, join, normalize, sep } from 'node:path';
9
+ import { gunzipSync } from 'node:zlib';
10
+ import { safeZipPath } from './zip.js';
11
+
12
+ const MAX_ENTRY_BYTES = 128 * 1024 * 1024;
13
+
14
+ const str = (buf, from, to) => buf.toString('utf8', from, to).replace(/\0[\s\S]*$/, '');
15
+
16
+ /**
17
+ * @param {Buffer} buf .tgz
18
+ * @param {{ dest: string, onFile?: (rel: string) => boolean, mapPath?: (rel: string) => string|null }} o
19
+ */
20
+ export function extractTarGz(buf, { dest, onFile = () => true, mapPath = null }) {
21
+ const tar = gunzipSync(buf);
22
+ const files = [];
23
+ let skipped = 0;
24
+ let off = 0;
25
+ const root = normalize(dest + sep);
26
+
27
+ while (off + 512 <= tar.length) {
28
+ const h = tar.subarray(off, off + 512);
29
+ if (h.every((b) => b === 0)) break; // 结束块
30
+ const name = str(h, 0, 100);
31
+ const prefix = str(h, 345, 500);
32
+ const sizeStr = str(h, 124, 136).trim();
33
+ const size = sizeStr ? parseInt(sizeStr, 8) : 0;
34
+ const type = h[156] === 0 ? 0x30 : h[156];
35
+ off += 512;
36
+
37
+ if (type === 0x30 && Number.isFinite(size) && size >= 0) {
38
+ let rel = safeZipPath(prefix ? `${prefix}/${name}` : name);
39
+ if (rel) {
40
+ if (mapPath) rel = mapPath(rel);
41
+ if (rel && onFile(rel)) {
42
+ if (size > MAX_ENTRY_BYTES) throw new Error(`条目太大:${rel}`);
43
+ const full = join(dest, ...rel.split('/'));
44
+ if (!normalize(full).startsWith(root)) throw new Error(`越界:${rel}`);
45
+ mkdirSync(dirname(full), { recursive: true });
46
+ writeFileSync(full, tar.subarray(off, off + size));
47
+ files.push(rel);
48
+ } else skipped++;
49
+ } else skipped++;
50
+ }
51
+ off += Math.ceil(size / 512) * 512;
52
+ }
53
+ return { files, skipped };
54
+ }
package/src/world.js CHANGED
@@ -14,7 +14,7 @@ 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
16
  import { ensureStarter, installStarterLibrary, libraryHealth, restorePack, safePackName } from './starter.js';
17
- import { FALLBACK_LIBRARY, actionUrl, flatActionUrl, installAction, installLibrary, libraryUrl } from './online.js';
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
 
20
20
  const ID = 'extra-actions';
@@ -515,7 +515,7 @@ export class ExtraActionsWorld {
515
515
  remoteActionBase() {
516
516
  const c = this.ctx.cfg && typeof this.ctx.cfg === 'object' ? this.ctx.cfg : {};
517
517
  const pick = (v) => (typeof v === 'string' && v.trim() ? v.trim().replace(/\/+$/, '') : '');
518
- return pick(c.remoteActionBase) || '';
518
+ return pick(c.remoteActionBase) || FALLBACK_ACTION_BASE;
519
519
  }
520
520
 
521
521
  /** 面板需要知道:完整库从哪下、能不能单个下。 */
@@ -535,10 +535,35 @@ export class ExtraActionsWorld {
535
535
  /** 一键:下载完整动作库 → 解压进动作库 → 设为当前库。 */
536
536
  async downloadLibrary() {
537
537
  const info = this.downloadInfo();
538
+ const existing = this.libraryDir();
539
+ // 没配自家源时:走 npm 分片(国内走 npmmirror,比 GitHub 稳)
540
+ if (!info.base && LIB_PARTS.length) {
541
+ const dest = existing && existsSync(existing) ? existing : join(this.dataDir, 'shenshen-library');
542
+ const r = await installLibraryParts({
543
+ dest,
544
+ log: (m) => this.host?.log?.info?.(m),
545
+ onProgress: (i, n) => this.host?.log?.info?.(`动作库 ${i}/${n} 片`),
546
+ });
547
+ // 分片有失败(镜像没同步等)时就再用整包兜一次,别让用户停在半截
548
+ if (!r.ok || r.modules < 20) {
549
+ this.host?.log?.warn?.(`分片装了 ${r.modules} 段(失败 ${r.failed} 片),改用整包兜底`);
550
+ try {
551
+ const z = await installLibrary({ url: FALLBACK_LIBRARY, libraryDir: r.path, dataDir: this.dataDir, log: (m) => this.host?.log?.info?.(m) });
552
+ this.saveSettings({ libraryDir: z.path });
553
+ this.bustCatalog();
554
+ return { ok: true, ...z, libraryDir: z.path, viaZip: true };
555
+ } catch (e) {
556
+ this.host?.log?.warn?.('整包兜底也失败了:' + String(e?.message ?? e));
557
+ }
558
+ }
559
+ this.saveSettings({ libraryDir: r.path });
560
+ this.bustCatalog();
561
+ return { ok: r.ok, ...r, libraryDir: r.path };
562
+ }
538
563
  if (!info.libraryUrl) throw new Error('还没配置下载源');
539
564
  const r = await installLibrary({
540
565
  url: info.libraryUrl,
541
- libraryDir: this.libraryDir(),
566
+ libraryDir: existing,
542
567
  dataDir: this.dataDir,
543
568
  log: (m) => this.host?.log?.info?.(m),
544
569
  });
@@ -53,7 +53,7 @@ const oneZip = buildZip([
53
53
  ['action.json', JSON.stringify({ id: 'fireworks', category: 'festival', word: 'fireworks', zh: '放烟花', say: '放烟花', clip: 'actions/festival/fireworks', frames: 1, fps: 24, seconds: 1, anim: true })],
54
54
  ['anim.webp', 'RIFFxxxxWEBPVP8 '],
55
55
  ]);
56
- const routes = new Map([['/lib/library.zip', libZip], ['/lib/actions/festival/fireworks.zip', oneZip]]);
56
+ const routes = new Map([['/lib/library.zip', libZip], ['/lib/actions/festival/fireworks.zip', oneZip], ['/lib/fireworks.zip', oneZip]]);
57
57
  const server = createServer((req, res) => {
58
58
  const b = routes.get(req.url);
59
59
  if (!b) { res.writeHead(404).end(); return; }
@@ -67,7 +67,7 @@ rmSync(root, { recursive: true, force: true });
67
67
  const dataDir = join(root, 'data');
68
68
  mkdirSync(dataDir, { recursive: true });
69
69
 
70
- const cfg = { remoteBase: base };
70
+ const cfg = { remoteBase: base, remoteActionBase: base }; // 单段走扁平布局 <base>/<id>.zip
71
71
  // 宿主的 persist 会写回同一个 cfg,这里照做,否则 libraryDir() 读不到
72
72
  const settings = cfg;
73
73
  const ctx = {
@@ -87,6 +87,7 @@ const check = (fn, msg) => { try { fn(); console.log('ok ' + msg); } catch (e)
87
87
  let st = await w.invoke('picker', 'state');
88
88
  check(() => assert.equal(st.download.perAction, true), 'remoteBase makes per-action downloads available');
89
89
  check(() => assert.equal(st.download.libraryUrl, base + '/library.zip'), 'library url = <base>/library.zip');
90
+ check(() => assert.equal(st.download.flat, true), 'per-action uses the flat layout when remoteActionBase is set');
90
91
 
91
92
  /* 2. 一键下载完整库 */
92
93
  const r = await w.invoke('picker', 'download-library');