"use strict"; let fs = require("fs"); let path = require("path"); const FileUtil = require('./core/FileUtil'); const { print, translate } = require('./src/eazax/editor-main-util'); const EditorAPI = require('./src/main/editor-api'); const Parser = require('./src/main/parser'); const Finder = require('./src/main/finder'); const Printer = require('./src/main//printer'); module.exports = { load() { this.init(); }, unload() { // Editor.log("卸載執行"); }, /** 初始化 */ init() { // this.createDirectory(); }, /** 創建Spine節點 */ sourcecheck(...args) { let self = this; try { // Editor.log("文件1: " + JSON.stringify(args[0]._value)); Editor.Scene.callSceneScript("sourcecheck", "getFsPath", { args: args }, async function (err, response) { // Editor.log("getFsPath response: " + response); let res = JSON.parse(response); if (!FileUtil.isFileExit(res.fsPath)) { Editor.error(`路徑錯誤: ${res.fsPath}`); Editor.log(`檢查停止`); return; } Editor.log(`檢查路徑: ${res.url}`); let files = []; files = self.fileSearch(res.fsPath, res.url, files); // Editor.log(`總共有${files.length}個物件`); for (let i = 0; i < files.length; i++) { // for (let i = 0; i < 1; i++) { await self.checkreq(files[i][0], files[i][1]); } Editor.log(`檢查完畢`); }); } catch (error) { Editor.log(`sourcecheck error: ${error}`); } }, /** * * @param fsPath fsPath */ fileSearch(fsPath, url, files) { try { // Editor.log(`fsPath: ${fsPath}`); if (FileUtil.isFileExit(fsPath)) { fs.readdirSync(fsPath).forEach(file => { // Editor.log(`file: ${file}`); let path = `${fsPath}\\${file}` if (this.checkfile(path)) { files = this.fileSearch(path, url, files); } else if (this.checkusefile(path)) { // this.checkreq(path, url); files.push([path, url]) } }); return files; } else { // Editor.error(`fsPath error: ${fsPath}`); return files; } } catch (error) { Editor.error(`fileSearch error: ${error}`); } }, /** * 檢查引用 * @param fsPath fsPath */ async checkreq(fsPath, myurl) { // Editor.log(`file: ${fsPath}`); try { let uuid = EditorAPI.fspathToUuid(fsPath); // Editor.log(`uuid: ${uuid}, file: ${fsPath}`); const assetInfo = EditorAPI.assetInfoByUuid(uuid), shortUrl = assetInfo.url.replace('db://', ''); // 查找引用 // print('log', '🔍', `${translate('find-asset-refs')} ${shortUrl}`); let refs = null; try { refs = await Finder.findByUuid(uuid); } catch (error) { Editor.error(`findByUuid error: ${error}, fsPath: ${fsPath}`); return; } // Editor.log(`refs: ${JSON.stringify(refs)}`); let newrefs = []; for (let i = 0; i < refs.length; i++) { let ref = refs[i]; let url = ref.url; if (url.indexOf(myurl) === -1) { // Editor.log(`url: ${url}, myurl: ${myurl}, fsPath: ${EditorAPI.fspathToUrl(fsPath)}`); newrefs.push(ref); } } if (newrefs.length > 0) { // 打印结果 Printer.printResult({ type: assetInfo.type, uuid: uuid, url: assetInfo.url, path: assetInfo.path, refs: newrefs, }); } } catch (error) { Editor.error(`checkreq error: ${error}, fsPath: ${fsPath}`); } }, /** * 檢查副檔名 * @param fsPath fsPath */ checkusefile(fsPath) { try { if ((fsPath.indexOf(".PNG") !== -1 || fsPath.indexOf(".png") !== -1 || fsPath.indexOf(".jpg") !== -1 || fsPath.indexOf(".anim") !== -1 || fsPath.indexOf(".fnt") !== -1) && fsPath.indexOf(".meta") === -1) { return true; } return false; } catch (error) { Editor.error(`checkfile error: ${error}`); return false; } }, /** * 檢查副檔名 * @param fsPath fsPath */ checkfile(fsPath) { try { if (fsPath.indexOf(".fire") === -1 && fsPath.indexOf(".fnt") === -1 && fsPath.indexOf(".anim") === -1 && fsPath.indexOf(".prefab") === -1 && fsPath.indexOf(".meta") === -1 && fsPath.indexOf(".pac") === -1 && fsPath.indexOf(".PNG") === -1 && fsPath.indexOf(".png") === -1 && fsPath.indexOf(".jpg") === -1) { return true; } return false; } catch (error) { Editor.error(`checkfile error: ${error}`); return false; } }, messages: { /** 打開面板 */ "open-panel"() { Editor.Panel.open("sourcecheck"); }, /** 保存按鈕點擊 */ "run-click"(event, ...args) { this.sourcecheck(...args); }, // /** 面板加載完成 */ // "panel-load-finish"(evnet, ...args) { // Editor.Scene.callSceneScript("sourcecheck", "get-default-info", { args: args }, function (err, response) { // // Editor.log("callSceneScript: " + response); // Editor.Ipc.sendToPanel("sourcecheck", "setDefault", response); // }); // }, }, }