From f836b1ecaf1c05965486b35ec7eb9e37949e1f5f Mon Sep 17 00:00:00 2001 From: JianMiau Date: Thu, 18 Aug 2022 12:09:30 +0800 Subject: [PATCH] =?UTF-8?q?[add]=20=E6=9B=BF=E6=8F=9B=E8=B3=87=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 34 ++++++++-- main.js | 151 ++++++++++++++++++++++++----------------- panel/index.js | 88 +++++++++++++++--------- scene-obtain.js | 86 +++++++++++++++-------- src/main/editor-api.js | 16 +++++ 5 files changed, 248 insertions(+), 127 deletions(-) diff --git a/index.html b/index.html index e6f483f..0f4ff4b 100644 --- a/index.html +++ b/index.html @@ -3,21 +3,43 @@

Uuid Convert


-

1. 填入要轉換的UUID

+

1. 填入要轉換的資源


-

要轉換的UUID

-
- -
-
+
+

要轉換的UUID

+
+ +
+
+
+

+
+ 是否替換資源 + +
+
執行
diff --git a/main.js b/main.js index 06ea96c..68eefbb 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,5 @@ "use strict"; - +var fs = require('fs'); // 引用檔案物件 const EditorAPI = require('./src/main/editor-api'); // ------------decode-uuid @@ -21,69 +21,98 @@ const Indices = UuidTemplate.map((x, i) => x === '-' ? NaN : i).filter(isFinite) let HexMap = {} { - for (let i = 0; i < HexChars.length; i++) { - let char = HexChars[i] - HexMap[char] = i - } + for (let i = 0; i < HexChars.length; i++) { + let char = HexChars[i] + HexMap[char] = i + } } module.exports = { - load() { - this.init(); - }, - unload() { - // Editor.log("卸載執行"); - }, - /** 初始化 */ - init() { - // this.createDirectory(); - }, - /** 創建Spine節點 */ - uuidconvert(...args) { - let self = this; - try { - let needconvertuuid = args[0]; - let uuid; - let long_uuid; - let short_uuid; - if (needconvertuuid.indexOf("-") === -1) { - // let convertuuid = this.decodeUuid(needconvertuuid); - let convertuuid = EditorAPI.decompressUuid(needconvertuuid); - uuid = convertuuid; - short_uuid = needconvertuuid; - long_uuid = convertuuid; - } else { - // let convertuuid = this.compressUuid(needconvertuuid); - let convertuuid = EditorAPI.compressUuid(needconvertuuid); - uuid = needconvertuuid; - short_uuid = convertuuid; - long_uuid = needconvertuuid; - } - Editor.log('short uuid', short_uuid); - Editor.log('long uuid', long_uuid); - Editor.Ipc.sendToAll('assets:hint', uuid); - Editor.log('path: ', Editor.assetdb.uuidToUrl(uuid)); - } catch (error) { - Editor.log(`uuidconvert error: ${error}`); - } - }, + load() { + this.init(); + }, + unload() { + // Editor.log("卸載執行"); + }, + /** 初始化 */ + init() { + // this.createDirectory(); + }, + /** 讀取資源資訊 */ + uuidconvert(...args) { + let self = this; + try { + let needconvertuuid = args[0]; + let long_uuid; + let short_uuid; + let path; + if (needconvertuuid.indexOf("-") === -1) { + // let convertuuid = this.decodeUuid(needconvertuuid); + let convertuuid = EditorAPI.decompressUuid(needconvertuuid); + short_uuid = needconvertuuid; + long_uuid = convertuuid; + } else { + // let convertuuid = this.compressUuid(needconvertuuid); + let convertuuid = EditorAPI.compressUuid(needconvertuuid); + short_uuid = convertuuid; + long_uuid = needconvertuuid; + } + path = Editor.assetdb.uuidToUrl(long_uuid); + return [short_uuid, long_uuid, path]; + } catch (error) { + Editor.error(`uuidconvert error: ${error}`); + } + }, + /** 替換資源 */ + replaceResource(...args) { + try { + let uuid1 = args[0]; + let uuid2 = args[1]; + let source = args[2]; + let [short_uuid1, long_uuid1, path1] = this.uuidconvert(uuid1); + let [short_uuid2, long_uuid2, path2] = this.uuidconvert(uuid2); + let [short_uuid3, long_uuid3, path3] = this.uuidconvert(source); + // Editor.log(`path1: ${uuid1}`); + // Editor.log(`path2: ${uuid2}`); + // Editor.log(`path3: ${source}`); - messages: { - /** 打開面板 */ - "open-panel"() { - Editor.Panel.open("uuidconvert"); - }, - /** 保存按鈕點擊 */ - "run-click"(event, ...args) { - this.uuidconvert(...args); - }, - // /** 面板加載完成 */ - // "panel-load-finish"(evnet, ...args) { - // Editor.Scene.callSceneScript("uuidconvert", "get-default-info", { args: args }, function (err, response) { - // // Editor.log("callSceneScript: " + response); - // Editor.Ipc.sendToPanel("uuidconvert", "setDefault", response); - // }); - // }, - }, + let sourceInfo = EditorAPI.assetInfoByUuid(source); + let source_path = sourceInfo.path; + let sourceData = fs.readFileSync(source_path, "utf8"); // 讀取檔案 + sourceData = sourceData.replace(new RegExp(short_uuid1, 'g'), short_uuid2); + fs.writeFileSync(source_path, sourceData); + Editor.assetdb.refresh(path3, function (err, results) { }); + } catch (error) { + Editor.error(`replaceResource error: ${error}`); + } + }, + + messages: { + /** 打開面板 */ + "open-panel"() { + Editor.Panel.open("uuidconvert"); + Editor.log("Uuid Convert Ver 20220818"); + }, + /** 保存按鈕點擊 */ + "run-click"(event, ...args) { + // this.uuidconvert(...args); + let [short_uuid, long_uuid, path] = this.uuidconvert(...args); + Editor.log('short uuid', short_uuid); + Editor.log('long uuid', long_uuid); + Editor.Ipc.sendToAll('assets:hint', long_uuid); + Editor.log('path: ', path); + }, + /** 保存按鈕點擊 */ + "replaceResource"(event, ...args) { + this.replaceResource(...args); + }, + // /** 面板加載完成 */ + // "panel-load-finish"(evnet, ...args) { + // Editor.Scene.callSceneScript("uuidconvert", "get-default-info", { args: args }, function (err, response) { + // // Editor.log("callSceneScript: " + response); + // Editor.Ipc.sendToPanel("uuidconvert", "setDefault", response); + // }); + // }, + }, } \ No newline at end of file diff --git a/panel/index.js b/panel/index.js index 34ec30b..a03d9fa 100644 --- a/panel/index.js +++ b/panel/index.js @@ -2,40 +2,66 @@ const PACKAGE_NAME = "uuidconvert"; const fs = require("fs"); Editor.Panel.extend({ - style: fs.readFileSync(Editor.url(`packages://${PACKAGE_NAME}/static/style/default/index.css`), 'utf-8') - .concat(fs.readFileSync(Editor.url(`packages://${PACKAGE_NAME}/static/style/default/bootstrap.min.css`), 'utf-8')), + style: fs.readFileSync(Editor.url(`packages://${PACKAGE_NAME}/static/style/default/index.css`), 'utf-8') + .concat(fs.readFileSync(Editor.url(`packages://${PACKAGE_NAME}/static/style/default/bootstrap.min.css`), 'utf-8')), - template: fs.readFileSync( - Editor.url(`packages://${PACKAGE_NAME}/index.html`), 'utf-8'), + template: fs.readFileSync( + Editor.url(`packages://${PACKAGE_NAME}/index.html`), 'utf-8'), - $: { - run: '#run', - uuid: '#uuid', - asset: '#asset', - }, + $: { + run: '#run', + uuid: '#uuid', + asset: '#asset', + asset2: '#asset2', + source: '#source', + isReplaceResource: '#isReplaceResource', + ReplaceResource: '#ReplaceResource', + ReplaceResource1: '#ReplaceResource1', + ReplaceResource2: '#ReplaceResource2', + }, - ready() { - this.$run.addEventListener("confirm", this.methods.run.bind(this)); - }, + ready() { + this.$run.addEventListener("confirm", this.methods.run.bind(this)); + this.$isReplaceResource.addEventListener("change", this.methods.setReplaceResource.bind(this)); + }, - methods: { - run() { - let uuid = ""; - // Editor.log(`$asset: ${this.$asset.value}`); - if (!this.$uuid.value) { - if (!this.$asset.value) { - Editor.error("請輸入要轉換的UUID"); - return; - } else { - uuid = this.$asset.value; - } - } else { - uuid = this.$uuid.value; - } - Editor.Ipc.sendToMain("uuidconvert:run-click", uuid); - }, - }, + methods: { + run() { + let uuid = ""; + // Editor.log(`$asset: ${this.$asset.value}`); + if (!this.$uuid.value) { + if (!this.$asset.value) { + Editor.error("請輸入要轉換的UUID"); + return; + } else { + uuid = this.$asset.value; + } + } else { + uuid = this.$uuid.value; + } + let ReplaceResource = this.$isReplaceResource.value; + if (ReplaceResource) { + // if (!this.$asset2.value) { + // Editor.error("請輸入要取代的資源"); + // return; + // } else if (!this.$source.value) { + // Editor.error("請輸入要取代的檔案"); + // return; + // } + Editor.Ipc.sendToMain("uuidconvert:replaceResource", uuid, this.$asset2.value, this.$source.value); + } else { + Editor.Ipc.sendToMain("uuidconvert:run-click", uuid); + } + }, + setReplaceResource() { + // Editor.log(`$isReplaceResource: ${this.$isReplaceResource.value}`); + let ReplaceResource = this.$isReplaceResource.value; + this.$ReplaceResource.style.display = ReplaceResource ? "" : "none"; + this.$ReplaceResource1.style.display = ReplaceResource ? "none" : ""; + this.$ReplaceResource2.style.display = ReplaceResource ? "" : "none"; + }, + }, - messages: { - } + messages: { + } }); \ No newline at end of file diff --git a/scene-obtain.js b/scene-obtain.js index 76b0b4c..102c748 100644 --- a/scene-obtain.js +++ b/scene-obtain.js @@ -1,37 +1,65 @@ "use strict"; let fs = require("fs"); let path = require("path"); +const EditorAPI = require('./src/main/editor-api'); module.exports = { - /** - * 獲取場景節點下的配置信息 - * @param event event - * @param data 這邊把panel的Node帶進來 - */ - "getFsPath": function (event, request) { - let self = this; - var response = null; - var args = request.args; - try { - let url = `db://assets/${args[0]._value}`; - url = url.replace(/\\/g, "/"); - // let url = `db://assets/=SceneLobby`; - // Editor.log(`url: ${url}`); - let fsPath = Editor.remote.assetdb.urlToFspath(url); - // Editor.log(`fsPath: ${fsPath}`); + /** + * 獲取場景節點下的配置信息 + * @param event event + * @param data 這邊把panel的Node帶進來 + */ + "replaceResource": function (event, request) { + let self = this; + var response = null; + var args = request.args; + try { + let uuid1 = args[0]; + let uuid2 = args[1]; + let uuid3 = args[2]; + let path = Editor.assetdb.uuidToUrl(uuid3); + // let assetInfo = EditorAPI.assetInfoByUuid(uuid1); + Editor.log(`path: ${path}`); + // Editor.log(`node: ${JSON.stringify(assetInfo)}`); + // let Version_path = Version_assetInfo.path; + // Editor.log("Version_path: " + Version_path); + // let Version = fs.readFileSync(Version_path, "utf8"); // 讀取檔案 + // Editor.log(`uuid3: ${uuid3}`); + // Editor.assetdb.queryInfoByUuid(uuid1, function (err, info) { + // Editor.log(`path: ${info.path}`); + // Editor.log(`url: ${info.url}`); + // Editor.log(`type: ${info.type}`); + // // Editor.assetdb.queryAssets(info.url, info.type, function (err, info) { + // // Editor.log(`info: ${JSON.stringify(info)}`); + // // }); + // }); - // this.fileSearch(fsPath); + // Editor.Selection.select('asset', uuid1); + // Editor.Selection.clear('node') + // let nodeUuids = Editor.Selection.curSelection("node"); + // Editor.Ipc.sendToPanel("scene", "scene:create-nodes-by-uuids", [uuid1], nodeUuids, { unlinkPrefab: null }) + // Editor.Selection.select('node', uuid1) + // nodeUuids = Editor.Selection.curSelection("node"); - let res = { - url: url, - fsPath, fsPath - } - response = JSON.stringify(res); - } catch (error) { - Editor.log(`getFsPath error: ${error}`); - } - if (event.reply) { - event.reply(null, response); - } - }, + // let source = cc.find("SlotPaytable_66"); + // // 修改运行中的节点属性: + // Editor.Ipc.sendToPanel('scene', 'scene:set-property', { + // id: uuid1, + // path: "name",//要修改的属性 + // type: "String", + // value: "8787", + // isSubProp: false, + // }); + // Editor.log(`node: ${JSON.stringify(source)}`); + // Editor.log(`node: ${source}`); + + // response = JSON.stringify(res); + } catch (error) { + Editor.error(`replaceResource error: ${error}`); + } + response = "666"; + if (event.reply) { + event.reply(null, response); + } + }, }; \ No newline at end of file diff --git a/src/main/editor-api.js b/src/main/editor-api.js index d2ec8e2..50ff622 100644 --- a/src/main/editor-api.js +++ b/src/main/editor-api.js @@ -37,6 +37,22 @@ const EditorAPI = { return Editor.assetdb.assetInfoByUuid(uuid); }, + /** + * 通过 uuid 获取资源信息 + * @param {string} uuid + */ + queryInfoByUuid(uuid, Function) { + Editor.assetdb.queryInfoByUuid(uuid3, Function(err, info)); + }, + + /** + * 通过 uuid 获取资源信息 + * @param {string} uuid + */ + queryAssets(url, type, Function) { + Editor.assetdb.queryAssets(url, type, Function(err, info)); + }, + /** * 通过 uuid 获取子资源信息 * @param {string} uuid