uuidconvert/main.js

118 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-03-31 00:57:26 +00:00
"use strict";
2022-08-18 04:09:30 +00:00
var fs = require('fs'); // 引用檔案物件
2022-03-31 00:57:26 +00:00
const EditorAPI = require('./src/main/editor-api');
// ------------decode-uuid
const BASE64_KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const values = new Array(123); // max char code in base64Keys
for (let i = 0; i < 123; ++i) { values[i] = 64; } // fill with placeholder('=') index
for (let i = 0; i < 64; ++i) { values[BASE64_KEYS.charCodeAt(i)] = i; }
// decoded value indexed by base64 char code
const BASE64_VALUES = values;
const HexChars = '0123456789abcdef'.split('');
const _t = ['', '', '', ''];
const UuidTemplate = _t.concat(_t, '-', _t, '-', _t, '-', _t, '-', _t, _t, _t);
const Indices = UuidTemplate.map((x, i) => x === '-' ? NaN : i).filter(isFinite);
let HexMap = {}
{
2022-08-18 04:09:30 +00:00
for (let i = 0; i < HexChars.length; i++) {
let char = HexChars[i]
HexMap[char] = i
}
2022-03-31 00:57:26 +00:00
}
module.exports = {
2022-08-18 04:09:30 +00:00
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}`);
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}`);
}
},
2022-03-31 00:57:26 +00:00
2022-08-18 04:09:30 +00:00
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);
// });
// },
},
2022-03-31 00:57:26 +00:00
}