UNPKG

unity-find-fault

Version:

A tool to find fault in unity project.

90 lines 3.69 kB
import fg from 'fast-glob'; import fs from 'fs-extra'; import yaml from 'yaml'; import _ from 'lodash'; import path from 'path'; import { toolchain } from '../toolchain.js'; export class UnityHelper { static CompositeFileTypes = ['.anim', '.controller', '.fontsettings', '.mat', '.overrideController', '.physicsMaterial2D', '.prefab', '.spriteatlas', '.terrainlayer', '.unity', '.asset', '.bytes']; static async collectUsedGUID(dir, exclude) { // 收集用到的guid const out = {}; const pattern = UnityHelper.CompositeFileTypes.filter((v) => exclude == null || !exclude.includes(v)).map((v) => `**/*${v}`); const prefabs = await fg(pattern, { cwd: dir }); for (const prefab of prefabs) { if (prefab.endsWith('ziyan_SkeletonData.asset')) { console.log('aaa'); } const file = path.join(dir, prefab); const content = await fs.readFile(file, 'utf-8'); const guids = content.matchAll(/guid: (\w{32})/g); for (const g of guids) { const guid = g[1]; let arr = out[guid]; if (arr == null) { out[guid] = arr = []; } if (!arr.includes(file)) arr.push(file); } } return out; } static async collectExistingGUID(dir) { // 收集所有存在的guid const out = []; const metas = await fg(['**/*.meta'], { cwd: dir }); for (const meta of metas) { const file = path.join(dir, meta); const guid = await this.readGUID(file); if (out.includes(guid)) { console.warn('guid conflicts:', guid); } else { out.push(guid); } } return _.uniq(out); } static async getScriptGUID(scriptName) { const r = path.join(toolchain.opts.projectRoot, 'Assets'); const scripts = await fg(`**/${scriptName}`, { cwd: r }); if (scripts.length > 0) { return await UnityHelper.readGUID(path.join(r, scripts[0]) + '.meta'); } return null; } static async readGUID(metaFile) { const content = await fs.readFile(metaFile, 'utf-8'); const result = content.match(/guid: (\w{32})/); if (result == null) { console.error('Cannot parse guid from:', metaFile); return ''; } return result[1]; } static async readTextureMeta(metaFile) { const content = await fs.readFile(metaFile, 'utf-8'); const guidRst = content.match(/guid: (\w{32})/); const guid = guidRst != null ? guidRst[1] : ''; const textureTypeRst = content.match(/textureType: (\d+)/); const textureType = textureTypeRst != null ? Number(textureTypeRst[1]) : 0; const spriteBorderRst = content.match(/spriteBorder: \{x: (\d*\.?\d+), y: (\d*\.?\d+), z: (\d*\.?\d+), w: (\d*\.?\d+)\}/); const spriteBorder = { x: Number(spriteBorderRst[1]), y: Number(spriteBorderRst[2]), z: Number(spriteBorderRst[3]), w: Number(spriteBorderRst[4]) }; return { guid, textureType, spriteBorder }; } static async loadMeta(metaFile) { const content = await fs.readFile(metaFile, 'utf-8'); return yaml.parse(content); } static async unlinkFile(file) { await fs.unlink(file); await fs.unlink(file + '.meta'); } static async removeDir(dir) { await fs.remove(dir); if (fs.existsSync(dir + '.meta')) await fs.unlink(dir + '.meta'); } } //# sourceMappingURL=UnityHelper.js.map