同步插件修改

This commit is contained in:
onvia 2023-07-31 09:26:24 +08:00
parent 40ee90491e
commit 6e294f7a19
5 changed files with 195 additions and 172 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,169 +1,186 @@
"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;
let isPinyin = param.isPinyin;
let options = {
"project-assets": projectAssets,
"cache": cacheFile,
"engine-version": ENGINE_VER,
"pinyin": isPinyin,
};
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);
}
Promise.all(tasks).then(() => {
genUUID2MD5Mapping();
console.log("[ccc-tnt-psd2ui] psd 导出完成,输出位置为:", output ? output : "psd 同级目录");
}).catch((reason) => {
console.log("[ccc-tnt-psd2ui] 导出失败", reason);
}).finally(() => {
});
},
};
function _exec(options, tasks) {
let jsonContent = JSON.stringify(options);
if (!fs_extra_1.default.existsSync(nodejsFile)) {
console.log(`[ccc-tnt-psd2ui] 没有内置 nodejs`, nodejsFile);
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);
}
}
console.log("[ccc-tnt-psd2ui] 命令参数:" + jsonContent);
console.log("[ccc-tnt-psd2ui] 命令执行中");
let base64 = Buffer.from(jsonContent).toString("base64");
tasks.push(new Promise((rs) => {
// console.log(`[ccc-tnt-psd2ui] `, `${nodejsFile} ${psd}` + ' ' + `--json ${base64}`);
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;
"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 commandFile = path_1.default.join(packagePath, "libs", "psd2ui", `command.${os_1.default.platform() == 'darwin' ? "sh" : "bat"}`);
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;
let isPinyin = param.isPinyin;
let options = {
"project-assets": projectAssets,
"cache": cacheFile,
"engine-version": ENGINE_VER,
"pinyin": isPinyin,
};
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);
}
Promise.all(tasks).then(() => {
genUUID2MD5Mapping();
console.log("[ccc-tnt-psd2ui] psd 导出完成,输出位置为:", output ? output : "psd 同级目录");
}).catch((reason) => {
console.log("[ccc-tnt-psd2ui] 导出失败", reason);
}).finally(() => {
});
},
};
function _exec(options, tasks) {
let jsonContent = JSON.stringify(options);
if (!fs_extra_1.default.existsSync(nodejsFile)) {
console.log(`[ccc-tnt-psd2ui] 没有内置 nodejs`, nodejsFile);
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);
}
if (fs_extra_1.default.statSync(commandFile).mode != 33261) {
console.log(`[ccc-tnt-psd2ui] commandFile 设置权限`);
fs_extra_1.default.chmodSync(commandFile, 33261);
}
}
console.log("[ccc-tnt-psd2ui] 命令参数:" + jsonContent);
console.log("[ccc-tnt-psd2ui] 命令执行中");
let base64 = Buffer.from(jsonContent).toString("base64");
tasks.push(new Promise((rs) => {
// console.log(`[ccc-tnt-psd2ui] `, `${nodejsFile} ${psd}` + ' ' + `--json ${base64}`);
// exec(`${nodejsFile} ${psd}` + ' ' + `--json ${base64}`, { windowsHide: false }, (err, stdout, stderr) => {
// console.log("[ccc-tnt-psd2ui]:\n", stdout);
// if (stderr) {
// console.log(stderr);
// }
// rs();
// })
let shellScript = commandFile; // 你的脚本路径
let scriptArgs = `--json ${base64}`; // 你的脚本参数
let command = os_1.default.platform() == 'darwin' ? `osascript -e 'tell app "Terminal" to do script "cd ${process.cwd()}; ${shellScript} ${scriptArgs}"'`
: `start ${commandFile} ${scriptArgs}`;
exec(command, (error, stdout, stderr) => {
console.log("[ccc-tnt-psd2ui]:\n", stdout);
console.log("[ccc-tnt-psd2ui]: 程序执行完后请手动关闭终端窗口");
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;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -245,6 +245,12 @@ export abstract class PsdLayer {
if (!text || !PsdLayer.isPinyin) {
return text;
}
let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
if (!reg.test(text)) {
return text;
}
let names = pinyin(text, {
toneType: "none",
type: "array"