unity-find-fault
Version:
A tool to find fault in unity project.
107 lines • 4.53 kB
JavaScript
import fg from "fast-glob";
import fs from "fs-extra";
import path from "path";
import { UnityHelper } from "./UnityHelper.js";
import { toolchain } from "../toolchain.js";
import { HardCodeCollector } from "./HardCodeCollector.js";
export class ImageStriper {
async removeUseless(r) {
const keepMap = {};
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;
}
}
}
}
// 收集ts代码/cs代码引用到的ui prefab
//#region
await HardCodeCollector.ensureData();
//#endregion
// 收集用到的guid
//#region
const usedGUIDMap = await UnityHelper.collectUsedGUID(toolchain.opts.projectRoot);
//#endregion
// 收集图片
//#region
let cnt = 0;
const imgRoots = ['Assets/AssetSources', 'Assets/Arts'];
for (const ir of imgRoots) {
const imgs = await fg(['**/*.png', '**/*.jpg', '**/*.tga'], { cwd: path.join(toolchain.opts.projectRoot, ir), ignore: ['font/**/@*/*.*', 'channel/**/*.*', 'icon/**/*.*', 'images/**/*.*', 'map/**/*.*', 'platIcon/**/*.*'] });
for (const img of imgs) {
if (img.includes('@'))
continue;
if (img.endsWith('connect.png')) {
console.log('kkkk');
}
const f = path.join(ir, img);
if (!keepMap[f] && !HardCodeCollector.isUsed(img)) {
// 未被代码引用
const file = path.join(toolchain.opts.projectRoot, f);
const metaFile = file + '.meta';
const guid = await UnityHelper.readGUID(metaFile);
if (!usedGUIDMap[guid]) {
// 未被直接引用
if (!r.isUsed || r.isRelated(guid)) {
await r.collect(file);
await fs.unlink(file);
await fs.unlink(metaFile);
cnt++;
}
}
}
else {
// console.log('used by code:', img);
}
}
}
const atlasImgs = await this.getMaybeUselessAssetAtlas();
for (const img of atlasImgs) {
const f = path.join('Assets/AssetSources', img);
if (!keepMap[f] && !HardCodeCollector.isUsed(img)) {
// 未被代码引用
const file = path.join(toolchain.opts.projectRoot, f);
const metaFile = file + '.meta';
const guid = await UnityHelper.readGUID(metaFile);
if (!usedGUIDMap[guid]) {
// 未被直接引用
if (!r.isUsed || r.isRelated(guid)) {
await r.collect(file);
await fs.unlink(file);
await fs.unlink(metaFile);
cnt++;
}
}
}
else {
// console.log('used by code:', img);
}
}
//#endregion
console.log('deleted images cnt:', cnt);
}
async getMaybeUselessAssetAtlas() {
const out = [];
const aaa = path.join(toolchain.opts.projectRoot, 'TsScripts/System/atlas/AllAssetAtlas.ts');
if (!fs.existsSync(aaa)) {
console.log('AllAssetAtlas.ts not found');
return out;
}
const aaaContent = await fs.readFile(aaa, 'utf-8');
const assetSourcesRoot = path.join(toolchain.opts.projectRoot, 'Assets/AssetSources');
const sprites = await fg(['images/atlas/*', 'images/sprite/*'], { cwd: assetSourcesRoot, onlyDirectories: true });
for (const sp of sprites) {
if (!aaaContent.includes(sp)) {
const spd = path.join(assetSourcesRoot, sp);
const imgs = await fg(['**/*.png', '**/*.jpg', '**/*.tga'], { cwd: spd });
out.push(...imgs.map((v) => sp + '/' + v));
}
}
return out;
}
}
//# sourceMappingURL=ImageStriper.js.map