UNPKG

unity-find-fault

Version:

A tool to find fault in unity project.

93 lines 4.59 kB
import fg from "fast-glob"; import fs from "fs-extra"; import path from "path"; import { toolchain } from "../toolchain.js"; import normalizePath from "normalize-path"; export class HardCodeCollector { static usedBySrcMap = null; /** * * @param file Relative to Assets/AssetSources or Absolute path. * @returns */ static isUsed(file) { if (HardCodeCollector.usedBySrcMap == null) { throw 'Call ensureData first'; } if (file.includes('Draw.png')) { console.log('ddd'); } if (path.isAbsolute(file)) { const assetSourcesRoot = path.join(toolchain.opts.projectRoot, 'Assets/AssetSources'); file = path.relative(assetSourcesRoot, file); } file = normalizePath(file); if (HardCodeCollector.usedBySrcMap[(file).toLowerCase()]) return true; const ext = path.extname(file); const bn = path.basename(file, ext); return HardCodeCollector.usedBySrcMap[bn.toLowerCase()]; } static async ensureData() { if (HardCodeCollector.usedBySrcMap != null) { return; } HardCodeCollector.usedBySrcMap = {}; const assetSourcesRoot = path.join(toolchain.opts.projectRoot, 'Assets/AssetSources'); const srcs = await fg(['**/*.ts', '**/*.cs'], { cwd: toolchain.opts.projectRoot, ignore: ['**/*.d.ts', '**/Editor/**/*.cs', '**/Plugins/**/*.cs'] }); for (const src of srcs) { const file = path.join(toolchain.opts.projectRoot, src); const content = await fs.readFile(file, 'utf-8'); const lines = content.replaceAll(/\/\/[^\r\n]+/g, '').split(/\r?\n/); for (const line of lines) { if (line.includes('this.elems.get') || line.includes('ElemFinder.find')) { continue; } const mchs = line.matchAll(/('|"|`)\S+?\1/g); for (const mch of mchs) { const str = mch[0]; const quoted = str.substring(1, str.length - 1).toLowerCase(); if (quoted == 'Draw') { console.log('aaaaa'); } if (quoted.endsWith('.png') || quoted.endsWith('.jpg') || quoted.endsWith('.prefab') || quoted.endsWith('.asset') || quoted.endsWith('.bytes')) { // 目前仅处理简单的情况,其它复杂情况暂不考虑 let pattern; if (quoted.includes('{0}')) { // 处理类似'ui/texture/mini{0}.png' pattern = quoted.replace(/\{\d+\}/g, '*'); } else { // 处理类似'ui/texture/' + platform[fysdk.ins.platform].tolowercase() + '_hall_bg_' + this.bgoutfitname() + '.png' pattern = quoted.replace(/['"]\s*\+.+?\+\s*['"]/g, '*'); } if (pattern != null && pattern.includes('/')) { if (pattern.includes('/*.png') || pattern.includes('/*.jpg') || pattern.includes('/*.prefab') || pattern.includes('/*.asset') || pattern.includes('/*.bytes')) { console.warn('本模式将导致所有文件被认定为有效:', pattern); console.warn(file); // process.exit(1); } if (!pattern.match(/^\*\*\/\*\.\w+$/)) { const pics = await fg(pattern, { cwd: assetSourcesRoot, caseSensitiveMatch: false }); for (const p of pics) { // console.log('mark used by code: ', pic, ', hit: ', p); if (p.toLowerCase() == 'connect') { console.log('aaaaa'); } HardCodeCollector.usedBySrcMap[p.toLowerCase()] = true; } } } else { HardCodeCollector.usedBySrcMap[quoted] = true; } } else { HardCodeCollector.usedBySrcMap[quoted] = true; } } } } } } //# sourceMappingURL=HardCodeCollector.js.map