refactor: fix-borders 升级改名为 fix-meta,新增 trim 破坏识别

- 改名 fix-borders → fix-meta(CLI 子命令 fix-meta;MCP tool meta_fix),名字更通用
- 新增第三类噪音识别:trimType 从 none 被 Cocos 改成自动裁剪(连带 width/height/
  offset/顶点全变)→ 还原该 frame 到 git(恢复不裁剪)。保守:只还原 git=none 的
  明确破坏,不碰 git 本就 auto 的(分不清破坏 vs 真改)
- 三类统一处理:① 纯顺序/格式噪音 ② 九宫格 border 重置 ③ trim 强改

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
furao
2026-06-07 19:00:47 +08:00
co-authored by Claude Opus 4.8
parent 7ae2f994cf
commit f54f0762ef
5 changed files with 66 additions and 49 deletions
@@ -1,20 +1,26 @@
// ============================================================
// editor/fix-borders.js — 清理 Cocos 重启/重新导入造成的图片 meta 噪音
// editor/fix-meta.js — 清理 Cocos 重启/重新导入造成的图片 meta 噪音
//
// 现象(Cocos 重启/重新导入有时发生)
// 1) 纯 key 顺序/格式变化:如 userData 里 trimType/atlasUuid 等字段被重排位置,
// Cocos 重启/重新导入有时会改坏图片 .meta,常见三类
// 1) 纯 key 顺序/格式变化:如 userData 里 trimType/atlasUuid 等被重排位置,
// 值没变,只产生 git diff 噪音。
// 2) 九宫格 border 被重置:subMetas.<hash>.userData.{borderTop,Bottom,Left,Right}
// 被清成 0(九宫格丢失,数据真丢)。
// 被清成 0(九宫格丢失)。
// 3) trim 裁剪被强改:用户设 trimType="none"(不裁剪)的图,被改成 "auto"(自动裁剪),
// 连带 width/height/offset/trimX-Y/顶点全变。
//
// 修复:扫 git 工作区改动的 .meta,逐个对比 git 版本:
// - 值深度相等(只 key 顺序/格式不同)→ 直接还原成 git 原文,消噪音;
// - 值不等但只是九宫格 border 被重置成 0 → 用 git 的值精准还原 border
// 还原后若整体已等于 git(差异仅 border + 顺序)也直接写 git 原文,否则只改 border
// 字段、保留 meta 其它真实改动。
// 修复(保守,只还原能确认是 Cocos 破坏的,不碰分不清的):扫 git 工作区改动的 .meta,
// 逐个对比 git 版本:
// - 值深度相等(只 key 顺序/格式不同)→ 还原成 git 原文
// - border 被重置成 0(git 有值)→ 用 git 的值精准还原 border
// - trimType 从 none 被改成裁剪 → 还原该 frame 的 userData 到 git(恢复不裁剪);
// - 还原后若整体已等于 git → 直接写 git 原文,否则只改动过的字段、保留 meta 其它真实改动。
//
// 不处理:git 本来就 trimType="auto" 的图(auto→auto 值变分不清破坏 vs 真改)、
// 以及其它无明确破坏特征的值变化。
//
// 靠 git 对比拿正确值;git 调用统一带 core.quotePath=false,正确处理中文路径。
// 纯 Node + git,跨平台。供 CLIfix-borders-cmd)和 MCP tool 共用。
// 纯 Node + git,跨平台。供 CLIfix-meta-cmd)和 MCP tool 共用。
// ============================================================
'use strict';
@@ -39,14 +45,15 @@ function norm(o) {
return o;
}
function valueEqual(a, b) { return JSON.stringify(norm(a)) === JSON.stringify(norm(b)); }
function deepClone(o) { return JSON.parse(JSON.stringify(o)); }
/**
* 清理被 Cocos 重启搞 .meta顺序噪音 + 九宫格 border 重置
* 清理被 Cocos 重启搞 .meta顺序噪音 / 九宫格 border 重置 / trim 被强改
* @param {string} projectOrRoot 项目路径git 仓库或其子目录均可
* @param {{dryRun?:boolean}} [opts]
* @returns {{gitRoot, scanned, reorderFiles, borderFiles, borderFrames, details:string[]}}
* @returns {{gitRoot, scanned, reorderFiles, fixedFiles, borderFrames, trimFrames, details:string[]}}
*/
function fixResetBorders(projectOrRoot, opts) {
function fixMeta(projectOrRoot, opts) {
opts = opts || {};
var dryRun = !!opts.dryRun;
@@ -63,7 +70,7 @@ function fixResetBorders(projectOrRoot, opts) {
var changed = git(gitRoot, ['diff', '--name-only', '--', '*.meta'])
.split('\n').map(function (s) { return s.trim(); }).filter(Boolean);
var reorderFiles = 0, borderFiles = 0, borderFrames = 0;
var reorderFiles = 0, fixedFiles = 0, borderFrames = 0, trimFrames = 0;
var details = [];
for (var i = 0; i < changed.length; i++) {
@@ -83,7 +90,7 @@ function fixResetBorders(projectOrRoot, opts) {
continue;
}
// 2) 值不等 → 检查九宫格 border 是否被重置成 0,精准还原
// 2) 值不等 → 逐 frame 识别 trim 破坏 / border 重置
var wSubs = work && work.subMetas;
var hSubs = head && head.subMetas;
if (!wSubs || !hSubs || typeof wSubs !== 'object' || typeof hSubs !== 'object') continue;
@@ -95,21 +102,28 @@ function fixResetBorders(projectOrRoot, opts) {
var wud = wSubs[key] && wSubs[key].userData;
var hud = hSubs[key] && hSubs[key].userData;
if (!wud || !hud) continue;
if (!BORDER_KEYS.some(function (b) { return b in wud; })) continue;
if (allZero(wud) && anyNonZero(hud)) {
// trim 破坏:git trimType=none、工作区被改成裁剪(none→非none)→ 还原整个 frame userData
if (hud.trimType === 'none' && wud.trimType && wud.trimType !== 'none') {
wSubs[key].userData = deepClone(hud);
metaChanged = true; trimFrames++;
details.push('[trim] ' + rel + ' [' + key + '] ' + wud.trimType + '→none(还原不裁剪)');
continue;
}
// border 重置:工作区 border 全 0、git 有非 0 → 精准还原 border 四字段
if (BORDER_KEYS.some(function (b) { return b in wud; }) && allZero(wud) && anyNonZero(hud)) {
for (var b = 0; b < BORDER_KEYS.length; b++) wud[BORDER_KEYS[b]] = hud[BORDER_KEYS[b]];
metaChanged = true;
borderFrames++;
metaChanged = true; borderFrames++;
details.push('[border] ' + rel + ' [' + key + '] → T' + hud.borderTop + '/B' + hud.borderBottom +
'/L' + hud.borderLeft + '/R' + hud.borderRight);
}
}
if (metaChanged) {
borderFiles++;
fixedFiles++;
if (!dryRun) {
// 还原 border 后若整体已等于 git(差异仅 border + 顺序)→ 直接写 git 原文,最干净;
// 否则只改了 border 字段、保留其它真实改动 → 按 work 写回(Cocos 标准格式)
// 还原后若整体已等于 git → 直接写 git 原文(连带消顺序噪音);否则按 work 写回、保留其它真实改动
if (valueEqual(work, head)) fs.writeFileSync(abs, headStr, 'utf8');
else fs.writeFileSync(abs, JSON.stringify(work, null, 2) + '\n', 'utf8');
}
@@ -118,9 +132,10 @@ function fixResetBorders(projectOrRoot, opts) {
return {
gitRoot: gitRoot, scanned: changed.length,
reorderFiles: reorderFiles, borderFiles: borderFiles, borderFrames: borderFrames,
reorderFiles: reorderFiles, fixedFiles: fixedFiles,
borderFrames: borderFrames, trimFrames: trimFrames,
details: details,
};
}
module.exports = { fixResetBorders: fixResetBorders };
module.exports = { fixMeta: fixMeta };