ccc-tnt-psd2ui/ccc-tnt-psd2ui-v3.4.+/dist/main.js

170 lines
6.0 KiB
JavaScript
Raw Normal View History

2023-07-22 07:43:18 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.unload = exports.load = exports.methods = void 0;
//@ts-ignore
const package_json_1 = __importDefault(require("../package.json"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const child_process_1 = __importDefault(require("child_process"));
let exec = child_process_1.default.exec;
const ENGINE_VER = "v342"; //
const packagePath = path_1.default.join(Editor.Project.path, "extensions", package_json_1.default.name);
const projectAssets = path_1.default.join(Editor.Project.path, "assets");
const cacheFile = path_1.default.join(Editor.Project.path, "local", "psd-to-prefab-cache.json");
const configFile = path_1.default.join(`${packagePath}/config/psd.config.json`);
const nodejsFile = path_1.default.join(packagePath, "bin", `node${os_1.default.platform() == 'darwin' ? "" : ".exe"}`);
const psd = path_1.default.join(packagePath, "libs", "psd2ui", "index.js");
let uuid2md5 = new Map();
let cacheFileJson = {};
/**
* @en
* @zh 为扩展的主进程的注册方法
*/
exports.methods = {
openPanel() {
Editor.Panel.open(package_json_1.default.name);
},
onClickPsd2UICache() {
console.log(`main-> onClickPsd2UICache111 `);
return new Promise((resolve, reject) => {
console.log(`main-> onClickPsd2UICache`);
let options = {
"project-assets": projectAssets,
"cache": cacheFile,
"init": true,
"engine-version": ENGINE_VER
};
Promise.all(_exec(options, [])).then(() => {
console.log("[psd2prefab] 执行缓存结束");
resolve();
});
});
},
async onPsd2UIDropFiles(param) {
let files = param.files;
let isForceImg = param.isForceImg;
let isImgOnly = param.isImgOnly;
let output = param.output;
2023-07-27 06:30:29 +00:00
let isPinyin = param.isPinyin;
2023-07-22 07:43:18 +00:00
let options = {
"project-assets": projectAssets,
"cache": cacheFile,
"engine-version": ENGINE_VER,
2023-07-27 06:30:29 +00:00
"pinyin": isPinyin,
2023-07-22 07:43:18 +00:00
};
let tasks = [];
for (let i = 0; i < files.length; i++) {
const file = files[i];
let stat = fs_extra_1.default.statSync(file);
if (stat.isFile()) {
let ext = path_1.default.extname(file);
if (ext != '.psd') {
continue;
}
}
let args = JSON.parse(JSON.stringify(options));
args["input"] = file;
if (output) {
args["output"] = output;
}
if (isImgOnly) {
// 只导出图片
args["img-only"] = true;
}
else {
// 强制导出图片
if (isForceImg) {
args["force-img"] = true;
}
args["config"] = configFile;
}
_exec(args, tasks);
}
2023-07-27 06:30:29 +00:00
Promise.all(tasks).then(() => {
genUUID2MD5Mapping();
console.log("[ccc-tnt-psd2ui] psd 导出完成,输出位置为:", output ? output : "psd 同级目录");
}).catch((reason) => {
console.log("[ccc-tnt-psd2ui] 导出失败", reason);
}).finally(() => {
});
2023-07-22 07:43:18 +00:00
},
};
function _exec(options, tasks) {
let jsonContent = JSON.stringify(options);
if (!fs_extra_1.default.existsSync(nodejsFile)) {
2023-07-27 06:30:29 +00:00
console.log(`[ccc-tnt-psd2ui] 没有内置 nodejs`, nodejsFile);
2023-07-22 07:43:18 +00:00
return tasks;
}
// 处理权限问题
if (os_1.default.platform() === 'darwin') {
if (fs_extra_1.default.statSync(nodejsFile).mode != 33261) {
console.log(`[ccc-tnt-psd2ui] 设置权限`);
fs_extra_1.default.chmodSync(nodejsFile, 33261);
}
}
2023-07-27 06:30:29 +00:00
console.log("[ccc-tnt-psd2ui] 命令参数:" + jsonContent);
console.log("[ccc-tnt-psd2ui] 命令执行中");
2023-07-22 07:43:18 +00:00
let base64 = Buffer.from(jsonContent).toString("base64");
tasks.push(new Promise((rs) => {
2023-07-27 06:30:29 +00:00
// console.log(`[ccc-tnt-psd2ui] `, `${nodejsFile} ${psd}` + ' ' + `--json ${base64}`);
2023-07-22 07:43:18 +00:00
exec(`${nodejsFile} ${psd}` + ' ' + `--json ${base64}`, { windowsHide: false }, (err, stdout, stderr) => {
console.log("[ccc-tnt-psd2ui]:\n", stdout);
if (stderr) {
console.log(stderr);
}
rs();
});
}));
return tasks;
}
/**
* 资源删除的监听
*
* @param {*} event
*/
function onAssetDeletedListener(event) {
if (uuid2md5.has(event)) {
let md5 = uuid2md5.get(event);
console.log(`[ccc-tnt-psd2ui] 删除资源 md5: ${md5}, uuid: ${event}`);
delete cacheFileJson[`${md5}`];
fs_extra_1.default.writeFileSync(cacheFile, JSON.stringify(cacheFileJson, null, 2));
}
}
/**
* 生成 uuid MD5 的映射
*
*/
function genUUID2MD5Mapping() {
if (!fs_extra_1.default.existsSync(cacheFile)) {
return;
}
let content = fs_extra_1.default.readFileSync(cacheFile, 'utf-8');
let obj = JSON.parse(content);
cacheFileJson = obj;
for (const key in obj) {
const element = obj[key];
uuid2md5.set(element.textureUuid, key);
}
}
/**
* @en Hooks triggered after extension loading is complete
* @zh 扩展加载完成后触发的钩子
*/
const load = function () {
genUUID2MD5Mapping();
Editor.Message.addBroadcastListener("asset-db:asset-delete", onAssetDeletedListener);
};
exports.load = load;
/**
* @en Hooks triggered after extension uninstallation is complete
* @zh 扩展卸载完成后触发的钩子
*/
const unload = function () {
Editor.Message.removeBroadcastListener("asset-db:asset-delete", onAssetDeletedListener);
};
exports.unload = unload;