[add] 替換資源

This commit is contained in:
建喵 2022-08-18 12:09:30 +08:00
parent c5f2494657
commit f836b1ecaf
5 changed files with 248 additions and 127 deletions

View File

@ -3,21 +3,43 @@
<h1 style="text-align:center">Uuid Convert</h1> <h1 style="text-align:center">Uuid Convert</h1>
<hr /> <hr />
<div> <div>
<h2 style="text-align:left">1. 填入要轉換的UUID</h2> <h2 style="text-align:left">1. 填入要轉換的資源</h2>
</div> </div>
<hr /> <hr />
<div> <div>
<div id="ReplaceResource1" style="display:''">
<h1 style="text-align:left">要轉換的UUID</h1> <h1 style="text-align:left">要轉換的UUID</h1>
<div class="input-group input-group-lg"> <div class="input-group input-group-lg">
<input id="uuid" type="text" class="form-control" placeholder="UUID"> <input id="uuid" type="text" class="form-control" placeholder="UUID">
</div> </div>
<br> <br>
</div>
<div id="ReplaceResource2" style="display:none">
<h3 style="text-align:left">原本的資源</h3>
</div>
<div class="input-group input-group-lg"> <div class="input-group input-group-lg">
<ui-asset id="asset" class="flex-1" type="native-asset" droppable="asset"> <ui-asset id="asset" class="flex-1" type="native-asset" droppable="asset">
</ui-asset> </ui-asset>
</div> </div>
</div> </div>
<hr /> <hr />
<div>
<ui-checkbox id="isReplaceResource">是否替換資源</ui-checkbox>
<div id="ReplaceResource" style="display:none">
<h3 style="text-align:left">2. 要覆蓋上去的資源(TS)</h3>
<div class="input-group input-group-lg">
<ui-asset id="asset2" class="flex-1" type="native-asset" droppable="asset">
</ui-asset>
</div>
<hr />
<h3 style="text-align:left">3. 需要替換的檔案(Scene、Prefab)</h3>
<div class="input-group input-group-lg">
<ui-asset id="source" class="flex-1" type="native-asset" droppable="asset">
</ui-asset>
</div>
</div>
</div>
<hr />
<div style="text-align:right"> <div style="text-align:right">
<ui-button id="run" class="blue btn btn-primary btn-lg btn-block">執行</ui-button> <ui-button id="run" class="blue btn btn-primary btn-lg btn-block">執行</ui-button>
</div> </div>

51
main.js
View File

@ -1,5 +1,5 @@
"use strict"; "use strict";
var fs = require('fs'); // 引用檔案物件
const EditorAPI = require('./src/main/editor-api'); const EditorAPI = require('./src/main/editor-api');
// ------------decode-uuid // ------------decode-uuid
@ -38,33 +38,52 @@ module.exports = {
init() { init() {
// this.createDirectory(); // this.createDirectory();
}, },
/** 創建Spine節點 */ /** 讀取資源資訊 */
uuidconvert(...args) { uuidconvert(...args) {
let self = this; let self = this;
try { try {
let needconvertuuid = args[0]; let needconvertuuid = args[0];
let uuid;
let long_uuid; let long_uuid;
let short_uuid; let short_uuid;
let path;
if (needconvertuuid.indexOf("-") === -1) { if (needconvertuuid.indexOf("-") === -1) {
// let convertuuid = this.decodeUuid(needconvertuuid); // let convertuuid = this.decodeUuid(needconvertuuid);
let convertuuid = EditorAPI.decompressUuid(needconvertuuid); let convertuuid = EditorAPI.decompressUuid(needconvertuuid);
uuid = convertuuid;
short_uuid = needconvertuuid; short_uuid = needconvertuuid;
long_uuid = convertuuid; long_uuid = convertuuid;
} else { } else {
// let convertuuid = this.compressUuid(needconvertuuid); // let convertuuid = this.compressUuid(needconvertuuid);
let convertuuid = EditorAPI.compressUuid(needconvertuuid); let convertuuid = EditorAPI.compressUuid(needconvertuuid);
uuid = needconvertuuid;
short_uuid = convertuuid; short_uuid = convertuuid;
long_uuid = needconvertuuid; long_uuid = needconvertuuid;
} }
Editor.log('short uuid', short_uuid); path = Editor.assetdb.uuidToUrl(long_uuid);
Editor.log('long uuid', long_uuid); return [short_uuid, long_uuid, path];
Editor.Ipc.sendToAll('assets:hint', uuid);
Editor.log('path: ', Editor.assetdb.uuidToUrl(uuid));
} catch (error) { } catch (error) {
Editor.log(`uuidconvert error: ${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}`);
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}`);
} }
}, },
@ -72,10 +91,20 @@ module.exports = {
/** 打開面板 */ /** 打開面板 */
"open-panel"() { "open-panel"() {
Editor.Panel.open("uuidconvert"); Editor.Panel.open("uuidconvert");
Editor.log("Uuid Convert Ver 20220818");
}, },
/** 保存按鈕點擊 */ /** 保存按鈕點擊 */
"run-click"(event, ...args) { "run-click"(event, ...args) {
this.uuidconvert(...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) { // "panel-load-finish"(evnet, ...args) {

View File

@ -12,10 +12,17 @@ Editor.Panel.extend({
run: '#run', run: '#run',
uuid: '#uuid', uuid: '#uuid',
asset: '#asset', asset: '#asset',
asset2: '#asset2',
source: '#source',
isReplaceResource: '#isReplaceResource',
ReplaceResource: '#ReplaceResource',
ReplaceResource1: '#ReplaceResource1',
ReplaceResource2: '#ReplaceResource2',
}, },
ready() { ready() {
this.$run.addEventListener("confirm", this.methods.run.bind(this)); this.$run.addEventListener("confirm", this.methods.run.bind(this));
this.$isReplaceResource.addEventListener("change", this.methods.setReplaceResource.bind(this));
}, },
methods: { methods: {
@ -32,7 +39,26 @@ Editor.Panel.extend({
} else { } else {
uuid = this.$uuid.value; 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); 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";
}, },
}, },

View File

@ -1,6 +1,7 @@
"use strict"; "use strict";
let fs = require("fs"); let fs = require("fs");
let path = require("path"); let path = require("path");
const EditorAPI = require('./src/main/editor-api');
module.exports = { module.exports = {
/** /**
@ -8,28 +9,55 @@ module.exports = {
* @param event event * @param event event
* @param data 這邊把panel的Node帶進來 * @param data 這邊把panel的Node帶進來
*/ */
"getFsPath": function (event, request) { "replaceResource": function (event, request) {
let self = this; let self = this;
var response = null; var response = null;
var args = request.args; var args = request.args;
try { try {
let url = `db://assets/${args[0]._value}`; let uuid1 = args[0];
url = url.replace(/\\/g, "/"); let uuid2 = args[1];
// let url = `db://assets/=SceneLobby`; let uuid3 = args[2];
// Editor.log(`url: ${url}`); let path = Editor.assetdb.uuidToUrl(uuid3);
let fsPath = Editor.remote.assetdb.urlToFspath(url); // let assetInfo = EditorAPI.assetInfoByUuid(uuid1);
// Editor.log(`fsPath: ${fsPath}`); 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 = { // let source = cc.find("SlotPaytable_66");
url: url, // // 修改运行中的节点属性:
fsPath, fsPath // Editor.Ipc.sendToPanel('scene', 'scene:set-property', {
} // id: uuid1,
response = JSON.stringify(res); // path: "name",//要修改的属性
// type: "String",
// value: "8787",
// isSubProp: false,
// });
// Editor.log(`node: ${JSON.stringify(source)}`);
// Editor.log(`node: ${source}`);
// response = JSON.stringify(res);
} catch (error) { } catch (error) {
Editor.log(`getFsPath error: ${error}`); Editor.error(`replaceResource error: ${error}`);
} }
response = "666";
if (event.reply) { if (event.reply) {
event.reply(null, response); event.reply(null, response);
} }

View File

@ -37,6 +37,22 @@ const EditorAPI = {
return Editor.assetdb.assetInfoByUuid(uuid); 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 获取子资源信息 * 通过 uuid 获取子资源信息
* @param {string} uuid * @param {string} uuid