unity-find-fault
Version:
A tool to find fault in unity project.
97 lines • 4.05 kB
JavaScript
import fg from "fast-glob";
import fs from "fs-extra";
import path from "path";
import { UnityHelper } from "./UnityHelper.js";
import { Record } from "./Record.js";
import { toolchain } from "../toolchain.js";
export class IconStriper {
imgRoots = ['icon'];
async removeUseless() {
const assetsRoot = path.join(toolchain.opts.projectRoot, 'Assets/AssetSources');
const keepMap = {};
const usedByCfgMap = {};
// 收集配置中配了的图标
//#region
if (toolchain.opts.cfg != null) {
// 部分写死保留的prefab
if (toolchain.opts.cfg.keep != null) {
for (const k of toolchain.opts.cfg.keep) {
const keeps = await fg(k, { cwd: toolchain.opts.projectRoot });
for (const f of keeps) {
keepMap[path.normalize(f)] = true;
}
}
}
// 读入表格使用的模型
if (toolchain.opts.cfg.iconCfg != null) {
for (const ic of toolchain.opts.cfg.iconCfg) {
if (ic.disabled)
continue;
for (const rule of ic.rules) {
const cf = path.join(assetsRoot, 'data', rule.cfgFile);
if (!fs.existsSync(cf)) {
console.error('cfg file not eixsts:', cf);
continue;
}
const cfgs = await fs.readJson(cf);
for (const c of cfgs) {
const p = ic.path.replaceAll('*', c[rule.cfgKey]).toLowerCase();
usedByCfgMap[p] = true;
usedByCfgMap[p.replace('.png', '_b.png')] = true;
// 部分图标还加了职业后缀
usedByCfgMap[p.replace('.png', '_1.png')] = true;
usedByCfgMap[p.replace('.png', '_2.png')] = true;
usedByCfgMap[p.replace('.png', '_3.png')] = true;
}
}
}
}
}
else {
console.error('no cfg!');
process.exit(1);
}
//#endregion
// 收集用到的guid
//#region
const usedGUIDMap = await UnityHelper.collectUsedGUID(toolchain.opts.projectRoot);
//#endregion
// 收集图片
//#region
let cnt = 0;
for (const r of this.imgRoots) {
const imgRoot = path.join('Assets/AssetSources', r);
const imgs = await fg(['**/*.png', '**/*.jpg', '**/*.tga'], { cwd: path.join(toolchain.opts.projectRoot, imgRoot) });
for (const img of imgs) {
const iconID = Number(path.basename(img, path.extname(img)));
if (iconID >= 210000 || iconID <= 220070) {
// 货币图标不删
continue;
}
const f = path.join(imgRoot, img);
if (keepMap[f]) {
continue;
}
if (!usedByCfgMap[(r + '/' + img).toLowerCase()]) {
// 未被配置
const file = path.join(toolchain.opts.projectRoot, f);
const metaFile = file + '.meta';
const guid = await UnityHelper.readGUID(metaFile);
if (!usedGUIDMap[guid]) {
// 未被直接引用
await fs.unlink(file);
await fs.unlink(metaFile);
await Record.Instance.recordGUID(guid, file);
cnt++;
}
}
else {
// console.log('used by code:', img);
}
}
}
//#endregion
console.log('deleted images cnt:', cnt);
}
}
//# sourceMappingURL=IconStriper.js.map