Discuz!

 找回密码
 立即注册
查看: 1|回复: 0

WOF 精灵/背景 采集器(CPS1 Lab)

[复制链接]

14

主题

23

回帖

92

积分

管理员

积分
92
发表于 1 小时前 | 显示全部楼层 |阅读模式
用途:v在 cps1lab 页面自动采集《天地を喰らうII》wof.zip 的单角色精灵图集与纯实景背景帧,并一键导出 ZIP

使用说明

## 一、安装(一次性)

1. Chrome 打开扩展商店,安装 **Tampermonkey**(篡改猴)。
2. 点击浏览器右上角的 Tampermonkey 图标 → **管理面板** → **添加新脚本**。
3. 把 `wof采集器.user.js` 的内容整段粘贴进去(覆盖模板里的示例代码),**Ctrl+S** 保存。
   - 也可以在管理面板里开启「高级」→ 直接把 .user.js 文件拖进浏览器,扩展会接管安装。
4. 确认脚本处于「启用」状态,并且 `@match` 里是 `https://www.cps1lab.com/*`。

## 二、每次使用

1. 打开 <https://www.cps1lab.com/play/>。
2. 点页面上的 **LOAD**(或直接把 `wof.zip` 拖进页面),等模拟器把游戏跑起来。
3. 右上角出现深绿色 **「WOF 采集器」** 面板,第一行会显示 `精灵 N 组 | 图集 … | 背景 N 帧 | 帧号 …`。

## 三、面板按钮

| 按钮 | 作用 |
| --- | --- |
| ▶ 自动采集 | 每 0.6 秒抓一次当前画面的 OAM,自动分组去重,累加到图集 |
| ▶ 自动背景 | 每 1.5 秒抓一帧纯实景(不含 HUD)背景,最多缓存 400 帧 |
| 采集一次 | 手动抓一帧 |
| ▶ 自动推进 | 内置机器人:P1 右行连打、P2 若已加入则后置横扫;遇到死亡/标题/选人等暗底文字画面会自动按 START(含投币) |
| 导出精灵 ZIP | 把图集按索引切成单张透明 PNG,打包成 zip(含 `索引.csv`) |
| 导出背景 ZIP | 把缓存里的背景帧打包成 zip |
| 导出图集 PNG | 导出整张 2048×8192 拼图 + 需要配合 `索引.csv` |
| 清空重来 | 清空精灵与背景缓存 |

## 四、建议的采集流程

1. 开「自动推进」+「自动采集」+「自动背景」,让它自己打;
2. 想手动玩也可以:自己操作键盘,同时开着「自动采集」「自动背景」;
3. 打到没新东西(面板上「精灵 N 组」不再增长)就点「导出精灵 ZIP」;
4. 想继续换关卡,就手动续关(死亡画面按 START → 选人 → 继续),或者开「自动推进」。

## 五、注意

- **必须联网**:脚本依赖 cps1lab 这个网页模拟器提供的 `window.__emu`(OAM、调色板、gfx ROM、帧缓冲)。
- 采集是**逐帧按 OAM 分组**:场上角色越多、动作越全,抓到的组越多;同一组会被指纹自动去重。
- 背景帧来自帧缓冲 `fb32`,**不含 HUD**(HUD 在文字层),所以导出就是纯场景图。
- 若某次导出时浏览器提示"多个文件下载",同意即可(精灵是打包成一个 zip,不会散落)。
- 控制台可用 `__WOF.counts()` 查看数量、`__WOF.collect()` 手动抓一帧、`__WOF.dumpIdx()` 导出索引 JSON。

> (注:部分内容由豆包 AI 生成)

  1. // ==UserScript==
  2. // @name         WOF 精灵/背景 采集器(CPS1 Lab)
  3. // @namespace    wof-extractor
  4. // @version      1.1
  5. // @description  在 cps1lab 页面自动采集《天地を喰らうII》wof.zip 的单角色精灵图集与纯实景背景帧,并一键导出 ZIP
  6. // @match        https://www.cps1lab.com/*
  7. // @match        https://cps1lab.com/*
  8. // @run-at       document-idle
  9. // @grant        none
  10. // ==/UserScript==

  11. (function () {
  12.   'use strict';

  13.   const AWC = 2048, AHC = 8192;
  14.   const COLLECT_EVERY_MS = 600;   // 自动采集间隔
  15.   const BG_EVERY_MS = 1500;       // 自动抓背景间隔

  16.   let emu = null, atlasCv = null, atlasCtx = null;
  17.   let cx = 0, cy = 0, lh = 0;
  18.   const map = {};          // 指纹去重
  19.   const idx = [];          // 索引
  20.   const bgFrames = [];     // {t,d}
  21.   let lastFc = -1, lastBgT = -1;
  22.   let autoCollect = false, autoPlay = false, autoBg = false;
  23.   let timer = null;
  24.   const stats = { collect: 0, bg: 0 };

  25.   // ---------------- 图集 ----------------
  26.   function initAtlas() {
  27.     if (atlasCv) return;
  28.     atlasCv = document.createElement('canvas');
  29.     atlasCv.width = AWC; atlasCv.height = AHC;
  30.     atlasCtx = atlasCv.getContext('2d');
  31.     atlasCtx.clearRect(0, 0, AWC, AHC);
  32.   }

  33.   // ---------------- 单个 OAM 条目绘制 ----------------
  34.   function drawEntry(fb, e, r) {
  35.     const v = emu.video, pal = v.paletteCache;
  36.     const t = e * 8;
  37.     const x = (r[t] << 8 | r[t + 1]), y = (r[t + 2] << 8 | r[t + 3]);
  38.     const c = (r[t + 4] << 8 | r[t + 5]), d = (r[t + 6] << 8 | r[t + 7]);
  39.     if ((d & 0xFF00) === 0xFF00) return -1;         // 结束标记
  40.     const f = d & 31, p = (d >> 5) & 1, m = (d >> 6) & 1;
  41.     const nx = ((d >> 8) & 15) + 1, ny = ((d >> 12) & 15) + 1;
  42.     if (d & 0xFF00) {
  43.       for (let ry = 0; ry < ny; ry++) for (let rx = 0; rx < nx; rx++) {
  44.         const sx = ((x + rx * 16) & 511) - 64, sy = ((y + ry * 16) & 511) - 16;
  45.         if (sx >= 384 || sx + 15 < 0 || sy >= 224 || sy + 15 < 0) continue;
  46.         let ti;
  47.         if (m) ti = p ? (c & -16) + ((c + (nx - 1) - rx) & 15) + 16 * (ny - 1 - ry)
  48.                       : (c & -16) + ((c + rx) & 15) + 16 * (ny - 1 - ry);
  49.         else   ti = p ? (c & -16) + ((c + (nx - 1) - rx) & 15) + 16 * ry
  50.                       : (c & -16) + ((c + rx) & 15) + 16 * ry;
  51.         v.drawSpriteTileFast(fb, pal, v.graphicsRom, v.graphicsRom.length, v.tileRowBuf, ti, f, p, m, sx, sy);
  52.       }
  53.     } else {
  54.       const sx2 = (x & 511) - 64, sy2 = (y & 511) - 16;
  55.       if (sx2 >= 384 || sx2 + 15 < 0 || sy2 >= 224 || sy2 + 15 < 0) return -1;
  56.       v.drawSpriteTileFast(fb, pal, v.graphicsRom, v.graphicsRom.length, v.tileRowBuf, c, f, p, m, sx2, sy2);
  57.     }
  58.     return f;
  59.   }

  60.   // ---------------- 采集一帧 ----------------
  61.   function collectOnce() {
  62.     if (!emu) return null;
  63.     initAtlas();
  64.     const r = emu.video.objBuffer, n = r.length / 8;
  65.     let end = n - 1;
  66.     for (let e = 0; e < n; e++) {
  67.       if (((r[e * 8 + 6] << 8 | r[e * 8 + 7]) & 0xFF00) === 0xFF00) { end = e - 1; break; }
  68.     }
  69.     // 按 调色板号 + y 回跳 分组
  70.     const groups = []; let cur = null, prevY = 0, prevPal = -1;
  71.     for (let e = 0; e <= end; e++) {
  72.       const t = e * 8;
  73.       const y = (r[t + 2] << 8 | r[t + 3]), d = (r[t + 6] << 8 | r[t + 7]), f = d & 31;
  74.       if (cur === null || f !== prevPal || y < prevY - 2) { if (cur) groups.push(cur); cur = { pal: f, list: [] }; }
  75.       cur.list.push(e); prevY = y; prevPal = f;
  76.     }
  77.     if (cur) groups.push(cur);

  78.     let added = 0;
  79.     for (const g of groups) {
  80.       const fb = new Uint32Array(384 * 224);
  81.       for (const k of g.list) drawEntry(fb, k, r);
  82.       let minx = 999, miny = 999, maxx = -1, maxy = -1, cnt = 0;
  83.       for (let yy = 0; yy < 224; yy++) for (let xx = 0; xx < 384; xx++) {
  84.         if (fb[yy * 384 + xx] !== 0) { cnt++; if (xx < minx) minx = xx; if (xx > maxx) maxx = xx; if (yy < miny) miny = yy; if (yy > maxy) maxy = yy; }
  85.       }
  86.       if (cnt < 60 || maxx < 0) continue;
  87.       const codes = g.list.map(e => (r[e * 8 + 4] << 8 | r[e * 8 + 5]));
  88.       const fp = g.pal + '#' + codes.join(',');
  89.       if (map[fp]) continue;
  90.       const w = maxx - minx + 1, h = maxy - miny + 1;
  91.       const tmp = document.createElement('canvas'); tmp.width = w; tmp.height = h;
  92.       const tid = tmp.getContext('2d').createImageData(w, h);
  93.       for (let yy2 = 0; yy2 < h; yy2++) for (let xx2 = 0; xx2 < w; xx2++) {
  94.         const pp = fb[(yy2 + miny) * 384 + (xx2 + minx)];
  95.         const o = (yy2 * w + xx2) * 4;
  96.         tid.data[o] = pp & 255; tid.data[o + 1] = (pp >> 8) & 255; tid.data[o + 2] = (pp >> 16) & 255;
  97.         tid.data[o + 3] = pp === 0 ? 0 : 255;
  98.       }
  99.       tmp.getContext('2d').putImageData(tid, 0, 0);
  100.       if (cx + w > AWC) { cx = 0; cy += lh + 3; lh = 0; }
  101.       if (cy + h >= AHC) continue;
  102.       atlasCtx.drawImage(tmp, cx, cy);
  103.       map[fp] = 1;
  104.       idx.push({ id: idx.length, x: cx, y: cy, w: w, h: h, pal: g.pal, n: g.list.length, codes: codes, codesJoin: codes.join(','), fc: emu.frameCount });
  105.       if (h > lh) lh = h;
  106.       cx += w + 3; added++;
  107.     }
  108.     stats.collect++;
  109.     refresh();
  110.     return added;
  111.   }

  112.   // ---------------- 背景帧 ----------------
  113.   let bgCv = null;
  114.   function grabFrame() {
  115.     if (!emu) return null;
  116.     if (!bgCv) { bgCv = document.createElement('canvas'); bgCv.width = emu.renderer.nativeWidth; bgCv.height = emu.renderer.nativeHeight; }
  117.     const w = bgCv.width, h = bgCv.height, ctx2 = bgCv.getContext('2d');
  118.     const img = ctx2.createImageData(w, h);
  119.     const fb = emu.video.fb32;
  120.     const src = new Uint32Array(fb.buffer ? fb.buffer : fb);
  121.     const d8 = new Uint8ClampedArray(img.data.buffer);
  122.     for (let i = 0, n = w * h; i < n; i++) {
  123.       const v = src[i];
  124.       d8[i * 4] = v & 255; d8[i * 4 + 1] = (v >> 8) & 255; d8[i * 4 + 2] = (v >> 16) & 255; d8[i * 4 + 3] = 255;
  125.     }
  126.     ctx2.putImageData(img, 0, 0);
  127.     return bgCv.toDataURL('image/png');
  128.   }
  129.   function saveBG() {
  130.     const t = emu.frameCount;
  131.     if (t === lastBgT) return;
  132.     lastBgT = t;
  133.     bgFrames.push({ t, d: grabFrame() });
  134.     while (bgFrames.length > 400) bgFrames.shift();
  135.     stats.bg = bgFrames.length;
  136.     refresh();
  137.   }

  138.   // ---------------- 自动驾驶(可选)---------------
  139.   const KEYS = ['up', 'down', 'left', 'right', 'button1', 'button2', 'button3'];
  140.   function isMenu() {
  141.     const f = emu.video.fb32;
  142.     let dark = 0, t = 0, white = 0;
  143.     for (let y = 8; y < 216; y += 4) for (let x = 8; x < 376; x += 4) {
  144.       const v = f[y * 384 + x], r = v & 255, g = (v >> 8) & 255, b = (v >> 16) & 255; t++;
  145.       if (r < 40 && g < 40 && b < 40) dark++;
  146.     }
  147.     for (let y = 172; y < 200; y++) for (let x = 96; x < 220; x++) {
  148.       const v = f[y * 384 + x], r = v & 255, g = (v >> 8) & 255, b = (v >> 16) & 255;
  149.       if (r > 200 && g > 200 && b > 200) white++;
  150.     }
  151.     return (dark / t > 0.45) && white > 60;
  152.   }
  153.   let botFrame = 0;
  154.   function botStep() {
  155.     if (!emu || emu.paused) return;
  156.     if (isMenu()) {
  157.       const v = emu.input.virtualState[0];
  158.       if (emu.bus.coinCtrl[0] < 2) { v.add('coin'); for (let i = 0; i < 30; i++) emu.stepFrame(); v.delete('coin'); }
  159.       v.add('start'); for (let i = 0; i < 30; i++) emu.stepFrame(); v.delete('start'); for (let j = 0; j < 200; j++) emu.stepFrame();
  160.       return;
  161.     }
  162.     // P1 前压连打,P2 后置横扫(有 2P 才生效)
  163.     const vs = emu.input.virtualState;
  164.     for (let p = 0; p < vs.length; p++) KEYS.forEach(k => vs[p] && vs[p].delete(k));
  165.     const n = 60;
  166.     for (let i = 0; i < n; i++) {
  167.       const f = botFrame + i;
  168.       vs[0].add('right'); if (f % 16 < 10) vs[0].add('button1');
  169.       if (vs[1]) { if (f % 70 < 40) vs[1].add('right'); else vs[1].add('left'); if (f % 16 < 10) vs[1].add('button1'); }
  170.       emu.stepFrame();
  171.     }
  172.     botFrame += n;
  173.   }

  174.   // ---------------- 主循环 ----------------
  175.   function loopTick() {
  176.     if (!emu) { tryAttach(); return; }
  177.     if (autoPlay) { try { botStep(); } catch (e) { } }
  178.     if (autoBg) { try { saveBG(); } catch (e) { } }
  179.     if (autoCollect && emu.frameCount !== lastFc) { lastFc = emu.frameCount; try { collectOnce(); } catch (e) { } }
  180.   }
  181.   function startLoop() { if (!timer) timer = setInterval(loopTick, COLLECT_EVERY_MS); }

  182.   // ---------------- ZIP(store,不压缩)----------------
  183.   const CRC_TABLE = (function () {
  184.     const t = new Uint32Array(256);
  185.     for (let i = 0; i < 256; i++) { let c = i; for (let k = 0; k < 8; k++) c = c & 1 ? 0xEDB88320 ^ (c >>> 1) : c >>> 1; t[i] = c >>> 0; }
  186.     return t;
  187.   })();
  188.   function crc32(bytes) {
  189.     let c = 0xFFFFFFFF;
  190.     for (let i = 0; i < bytes.length; i++) c = CRC_TABLE[(c ^ bytes[i]) & 0xFF] ^ (c >>> 8);
  191.     return (c ^ 0xFFFFFFFF) >>> 0;
  192.   }
  193.   function dataUrlToBytes(d) {
  194.     const b64 = d.split(',')[1];
  195.     const bin = atob(b64);
  196.     const out = new Uint8Array(bin.length);
  197.     for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
  198.     return out;
  199.   }
  200.   function textToBytes(s) {
  201.     const out = new Uint8Array(s.length);
  202.     for (let i = 0; i < s.length; i++) out[i] = s.charCodeAt(i);
  203.     return out;
  204.   }
  205.   function makeZip(files) { // files: [{name, bytes}]
  206.     const parts = [];
  207.     const central = [];
  208.     let offset = 0;
  209.     const enc = new TextEncoder();
  210.     for (const f of files) {
  211.       const nameBytes = enc.encode(f.name);
  212.       const crc = crc32(f.bytes);
  213.       const lh = new Uint8Array(30 + nameBytes.length);
  214.       const dv = new DataView(lh.buffer);
  215.       dv.setUint32(0, 0x04034b50, true);
  216.       dv.setUint16(4, 20, true); dv.setUint16(6, 0x0800, true); dv.setUint16(8, 0, true);
  217.       dv.setUint16(10, 0, true); dv.setUint16(12, 0x21, true); // 1980-01-01
  218.       dv.setUint32(14, crc, true);
  219.       dv.setUint32(18, f.bytes.length, true); dv.setUint32(22, f.bytes.length, true);
  220.       dv.setUint16(26, nameBytes.length, true); dv.setUint16(28, 0, true);
  221.       lh.set(nameBytes, 30);
  222.       parts.push(lh, f.bytes);
  223.       const ch = new Uint8Array(46 + nameBytes.length);
  224.       const dv2 = new DataView(ch.buffer);
  225.       dv2.setUint32(0, 0x02014b50, true);
  226.       dv2.setUint16(4, 20, true); dv2.setUint16(6, 20, true); dv2.setUint16(8, 0x0800, true);
  227.       dv2.setUint16(10, 0, true); dv2.setUint16(12, 0, true); dv2.setUint16(14, 0x21, true);
  228.       dv2.setUint32(16, crc, true);
  229.       dv2.setUint32(20, f.bytes.length, true); dv2.setUint32(24, f.bytes.length, true);
  230.       dv2.setUint16(28, nameBytes.length, true);
  231.       dv2.setUint32(42, offset, true);
  232.       ch.set(nameBytes, 46);
  233.       central.push(ch);
  234.       offset += lh.length + f.bytes.length;
  235.     }
  236.     let cdSize = 0;
  237.     for (const c of central) cdSize += c.length;
  238.     const end = new Uint8Array(22);
  239.     const dv3 = new DataView(end.buffer);
  240.     dv3.setUint32(0, 0x06054b50, true);
  241.     dv3.setUint16(8, files.length, true); dv3.setUint16(10, files.length, true);
  242.     dv3.setUint32(12, cdSize, true); dv3.setUint32(16, offset, true);
  243.     const all = new Blob([...parts, ...central, end], { type: 'application/zip' });
  244.     return all;
  245.   }
  246.   function download(blob, name) {
  247.     const a = document.createElement('a');
  248.     a.href = URL.createObjectURL(blob);
  249.     a.download = name; a.click();
  250.     setTimeout(() => URL.revokeObjectURL(a.href), 3000);
  251.   }

  252.   // 从图集按索引切图
  253.   function sliceSprites() {
  254.     const out = [];
  255.     const tmp = document.createElement('canvas');
  256.     const tctx = tmp.getContext('2d');
  257.     for (const e of idx) {
  258.       tmp.width = e.w; tmp.height = e.h;
  259.       tctx.clearRect(0, 0, e.w, e.h);
  260.       tctx.drawImage(atlasCv, e.x, e.y, e.w, e.h, 0, 0, e.w, e.h);
  261.       const url = tmp.toDataURL('image/png');
  262.       out.push({ name: `sprites/s${String(e.id).padStart(4, '0')}_p${e.pal}_${e.w}x${e.h}.png`, bytes: dataUrlToBytes(url) });
  263.     }
  264.     let csv = 'id,palette,w,h,n,codes\n';
  265.     for (const e of idx) csv += `${e.id},${e.pal},${e.w},${e.h},${e.n},"${e.codesJoin}"\n`;
  266.     out.push({ name: 'sprites/索引.csv', bytes: textToBytes(csv) });
  267.     return out;
  268.   }

  269.   // ---------------- UI ----------------
  270.   let panel, logEl;
  271.   function log(msg) {
  272.     if (!logEl) return;
  273.     logEl.textContent = msg;
  274.   }
  275.   function refresh() {
  276.     if (!panel) return;
  277.     const s = panel.querySelector('#wofStat');
  278.     if (s) s.textContent = `精灵 ${idx.length} 组 | 图集 ${cx}/${cy + lh} | 背景 ${bgFrames.length} 帧 | 帧号 ${emu ? emu.frameCount : '-'}`;
  279.     const b1 = panel.querySelector('#wofAuto');
  280.     if (b1) b1.textContent = autoPlay ? '■ 停止自动推进' : '▶ 自动推进';
  281.     const b2 = panel.querySelector('#wofAutoC');
  282.     if (b2) b2.textContent = autoCollect ? '■ 停止自动采集' : '▶ 自动采集';
  283.     const b3 = panel.querySelector('#wofAutoB');
  284.     if (b3) b3.textContent = autoBg ? '■ 停止自动背景' : '▶ 自动背景';
  285.   }
  286.   function buildUI() {
  287.     if (panel) return;
  288.     panel = document.createElement('div');
  289.     panel.style.cssText = `position:fixed;right:12px;top:64px;z-index:2147483000;width:266px;
  290.       background:#111a14;color:#b7ffc9;border:1px solid #2e7d4f;border-radius:8px;padding:10px 12px;
  291.       font:12px/1.5 ui-monospace,Menlo,monospace;box-shadow:0 6px 20px rgba(0,0,0,.5)`;
  292.     panel.innerHTML = `
  293.       <div style="font-weight:700;color:#6ff;letter-spacing:.5px">WOF 采集器</div>
  294.       <div id="wofStat" style="margin:6px 0 8px;color:#8ee6a8">等待模拟器载入…</div>
  295.       <div style="display:grid;grid-template-columns:1fr 1fr;gap:6px">
  296.         <button id="wofAutoC" style="${btn()}">▶ 自动采集</button>
  297.         <button id="wofAuto" style="${btn()}">▶ 自动推进</button>
  298.         <button id="wofOnce" style="${btn()}">采集一次</button>
  299.         <button id="wofAutoB" style="${btn()}">▶ 自动背景</button>
  300.         <button id="wofZip" style="${btn()}">导出精灵 ZIP</button>
  301.         <button id="wofZipBg" style="${btn()}">导出背景 ZIP</button>
  302.         <button id="wofAtlas" style="${btn()}">导出图集 PNG</button>
  303.         <button id="wofReset" style="${btn()}">清空重来</button>
  304.       </div>
  305.       <div id="wofLog" style="margin-top:8px;color:#7f9f8a;min-height:16px"></div>`;
  306.     document.body.appendChild(panel);
  307.     logEl = panel.querySelector('#wofLog');
  308.     panel.querySelector('#wofAutoC').onclick = () => { autoCollect = !autoCollect; refresh(); };
  309.     panel.querySelector('#wofAuto').onclick = () => { autoPlay = !autoPlay; refresh(); };
  310.     panel.querySelector('#wofOnce').onclick = () => { const a = collectOnce(); log(`本次新增 ${a} 组,累计 ${idx.length}`); };
  311.     panel.querySelector('#wofAutoB').onclick = () => { autoBg = !autoBg; refresh(); log(autoBg ? '自动背景:开' : '自动背景:关'); };
  312.     panel.querySelector('#wofZip').onclick = () => {
  313.       if (!idx.length) { log('还没有精灵,先采集'); return; }
  314.       log('正在打包…');
  315.       setTimeout(() => {
  316.         const files = sliceSprites();
  317.         download(makeZip(files), `wof_sprites_${idx.length}.zip`);
  318.         log(`已导出 ${idx.length} 张精灵`);
  319.       }, 10);
  320.     };
  321.     panel.querySelector('#wofZipBg').onclick = () => {
  322.       if (!bgFrames.length) { log('还没有背景帧'); return; }
  323.       log('正在打包背景…');
  324.       setTimeout(() => {
  325.         const files = bgFrames.map(f => ({ name: `bg/f${String(f.t).padStart(6, '0')}.png`, bytes: dataUrlToBytes(f.d) }));
  326.         download(makeZip(files), `wof_bg_${bgFrames.length}.zip`);
  327.         log(`已导出 ${bgFrames.length} 帧背景`);
  328.       }, 10);
  329.     };
  330.     panel.querySelector('#wofAtlas').onclick = () => {
  331.       if (!idx.length) { log('还没有精灵'); return; }
  332.       const h = Math.min(AHC, cy + lh + 4);
  333.       const c2 = document.createElement('canvas'); c2.width = AWC; c2.height = h;
  334.       c2.getContext('2d').drawImage(atlasCv, 0, 0);
  335.       c2.toBlob(b => download(b, `wof_atlas_${idx.length}.png`), 'image/png');
  336.       log('已导出图集 PNG');
  337.     };
  338.     panel.querySelector('#wofReset').onclick = () => {
  339.       if (!confirm('清空已采集的精灵与背景帧?')) return;
  340.       idx.length = 0; for (const k in map) delete map[k];
  341.       cx = 0; cy = 0; lh = 0;
  342.       atlasCtx.clearRect(0, 0, AWC, AHC);
  343.       bgFrames.length = 0;
  344.       refresh(); log('已清空');
  345.     };
  346.     refresh();
  347.   }
  348.   function btn() {
  349.     return 'background:#1d3326;color:#a9ffc6;border:1px solid #2e7d4f;border-radius:5px;padding:5px 6px;cursor:pointer;font:inherit';
  350.   }

  351.   // ---------------- 挂载 ----------------
  352.   function tryAttach() {
  353.     if (window.__emu && window.__emu.video && window.__emu.video.objBuffer) {
  354.       emu = window.__emu;
  355.       initAtlas();
  356.       buildUI();
  357.       log('已连接模拟器,可以开始了');
  358.       startLoop();
  359.     }
  360.   }

  361.   // 暴露给控制台
  362.   window.__WOF = {
  363.     collect: () => collectOnce(),
  364.     bg: () => saveBG(),
  365.     dumpIdx: () => JSON.stringify(idx),
  366.     counts: () => ({ sprites: idx.length, bg: bgFrames.length, frame: emu && emu.frameCount }),
  367.     zipTest: () => {
  368.       const b = makeZip([{ name: 'a.txt', bytes: textToBytes('hello') }, { name: 'b/x.png', bytes: textToBytes('y') }]);
  369.       return b.size;   // 期望 204
  370.     },
  371.     zipSprites: (limit) => {
  372.       const all = sliceSprites();
  373.       return makeZip(limit ? all.slice(0, limit) : all).size;
  374.     },
  375.     runZip: (limit) => {
  376.       const all = sliceSprites();
  377.       const files = limit ? all.slice(0, limit) : all;
  378.       download(makeZip(files), `wof_test_${files.length}.zip`);
  379.       return files.length;
  380.     }
  381.   };

  382.   const t = setInterval(() => {
  383.     tryAttach();
  384.     if (emu && panel) { clearInterval(t); }
  385.   }, 800);
  386.   setTimeout(buildUI, 3000);
  387. })();
  388. //(注:内容由AI生成)
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Discuz! X

GMT+8, 2026-11-22 21:45 , Processed in 0.161274 second(s), 20 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表