增加设置面板

This commit is contained in:
SmallMain 2024-10-24 18:02:51 +08:00
parent 5c90f8fc31
commit f22c29d78c
No known key found for this signature in database
5 changed files with 257 additions and 1 deletions

View File

@ -0,0 +1,4 @@
module.exports = {
'settingsmenuname': 'Enhance Kit Settings...',
'settings_title': 'Enhance Kit Settings',
};

View File

@ -0,0 +1,4 @@
module.exports = {
'settingsmenuname': '增强包设置...',
'settings_title': '增强包设置',
};

View File

@ -1,3 +1,136 @@
'use strict'; 'use strict';
const fs = require('fs');
const path = require('path');
module.exports = {}; /**
* 环境信息
*/
let engineEditorPath = path.dirname(path.dirname(Editor.frameworkPath));
const engineMinigameAdapterPath = path.join(engineEditorPath, "builtin", "adapters");
const engineWechatMinigameWorkerMainMacroPath = path.join(engineEditorPath, "builtin", "adapters", "platforms/wechat/worker/macro.js");
const engineWechatMinigameWorkerSubMacroPath = path.join(engineEditorPath, "builtin", "adapters", "platforms/wechat/res/workers/macro.js");
const WECHAT_MINIGAME_WORKER_SUB_PATH = "platforms/wechat/res/workers";
const WECHAT_MINIGAME_CONFIG_PATH = "platforms/wechat/res/game.json";
function t(str) {
return Editor.T('enhance-kit.' + str);
}
function getMinigameAdapterVersion() {
try {
return fs.readFileSync(path.join(engineMinigameAdapterPath, "VERSION.md"), { encoding: "utf-8" }).trim();
} catch (error) {
// Editor.error(error);
return "";
}
}
function getSettings() {
const minigameVersion = getMinigameAdapterVersion();
const isUninstalled = minigameVersion === "";
const isSupported = !isUninstalled && Number(minigameVersion.split(".")[0]) >= 2;
if (isUninstalled) {
return { code: -1, errMsg: "请先安装增强包。" };
} else if (!isSupported) {
return { code: -2, errMsg: "需安装版本 >= 2.0.0 的增强包,以支持设置面板功能。" };
} else {
const content = fs.readFileSync(engineWechatMinigameWorkerMainMacroPath, { encoding: "utf-8" });
return {
code: 0,
CC_WORKER_DEBUG: getMacroBooleanValue(content, "CC_WORKER_DEBUG"),
CC_WORKER_ASSET_PIPELINE: getMacroBooleanValue(content, "CC_WORKER_ASSET_PIPELINE"),
CC_WORKER_AUDIO_SYSTEM: getMacroBooleanValue(content, "CC_WORKER_AUDIO_SYSTEM"),
CC_WORKER_SCHEDULER: getMacroBooleanValue(content, "CC_WORKER_SCHEDULER"),
};
}
}
function syncSettingsToSubWorker() {
const result = getSettings();
if (result.code === 0) {
let content = fs.readFileSync(engineWechatMinigameWorkerSubMacroPath, { encoding: "utf-8" });
for (const key in result) {
if (key !== "code") {
content = setMacroBooleanValue(content, key, result[key]);
}
}
fs.writeFileSync(engineWechatMinigameWorkerSubMacroPath, content);
}
}
function setSettings(macro, value) {
{
const content = fs.readFileSync(engineWechatMinigameWorkerMainMacroPath, { encoding: "utf-8" });
fs.writeFileSync(engineWechatMinigameWorkerMainMacroPath, setMacroBooleanValue(content, macro, value));
}
checkAndModifyWorkerFiles();
syncSettingsToSubWorker();
}
function getMacroBooleanValue(text, macro) {
const regex = new RegExp(`globalThis\\.${macro}\\s*=\\s*(true|false);`);
const match = text.match(regex);
return match ? match[1] === 'true' : null;
}
function setMacroBooleanValue(text, macro, value) {
const regex = new RegExp(`globalThis\\.${macro}\\s*=\\s*(true|false);`);
const replacement = `globalThis.${macro} = ${value};`;
return text.replace(regex, replacement);
}
function checkAndModifyWorkerFiles() {
const result = getSettings();
if (result.code === 0) {
const workerDir = path.join(engineMinigameAdapterPath, WECHAT_MINIGAME_WORKER_SUB_PATH);
const gameJsonPath = path.join(engineMinigameAdapterPath, WECHAT_MINIGAME_CONFIG_PATH);
const gameJson = JSON.parse(fs.readFileSync(gameJsonPath, { encoding: "utf-8" }));
// 是否启用 Worker
if (result.CC_WORKER_ASSET_PIPELINE || result.CC_WORKER_AUDIO_SYSTEM) {
// 没有 Worker 目录与配置的话提醒用户重新安装
if (!(gameJson.workers && fs.existsSync(workerDir))) {
Editor.error("你启用了增强包的多线程特性,但未检测到正确的 workers 目录与 game.json 字段请重新安装增强包详情请查看文档TODO");
}
} else {
Editor.warn("你禁用了增强包的多线程特性可以手动删除相关文件以减少包体大小详情请查看文档TODO");
}
}
}
module.exports = {
async load() {
// 这里场景未准备就绪,无法获取 engineVersion
},
messages: {
openSettings() {
Editor.Panel.open('enhance-kit');
},
getSettings(event) {
const result = getSettings();
event.reply(null, result);
},
setSettings(event, macro, value) {
setSettings(macro, value);
event.reply(null);
},
"scene:ready"(event) {
checkAndModifyWorkerFiles();
},
},
};

View File

@ -4,6 +4,20 @@
"description": "This extension provides support for cocos enhance kit.", "description": "This extension provides support for cocos enhance kit.",
"author": "SmallMain", "author": "SmallMain",
"main": "main.js", "main": "main.js",
"panel": {
"main": "panel/index.js",
"type": "dockable",
"title": "i18n:enhance-kit.settings_title",
"width": 500,
"height": 600,
"min-width": 500,
"min-height": 600
},
"main-menu": {
"i18n:MAIN_MENU.project.title/i18n:enhance-kit.settingsmenuname": {
"message": "enhance-kit:openSettings"
}
},
"runtime-resource": { "runtime-resource": {
"path": "resources", "path": "resources",
"name": "resources" "name": "resources"

View File

@ -0,0 +1,101 @@
Editor.Panel.extend({
style: `
:host { margin: 5px; }
h1 { margin-left: 15px; }
.desc { color: gray; margin-left: 15px; margin-right: 15px; }
.subdesc { color: gray; }
.sub { margin-left: 30px; margin-right: 30px; }
.hidden { display: none; }
`,
template: `
<div id="unripe">
<p id="unripe_tip" class="desc">加载中...</p>
</div>
<div id="ready" class="hidden">
<h1>多线程支持</h1>
<p class="desc">该特性仅在微信小游戏平台下有效</p>
<hr />
<div class="sub">
<p class="subdesc">请注意以下为全局设置会影响所有项目并在重新安装升级或卸载后丢失所有设置</p>
<ui-prop id="td" tabindex="-1" name="调试模式" tooltip="启用后将会输出详细日志以便进行调试,这可能会大幅降低性能。">
<ui-checkbox id="tdc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="tap" tabindex="-1" name="多线程驱动资源管线" tooltip="启用后将资源管线移至线程中执行,减少由资源下载、缓存与加载导致的卡顿。">
<ui-checkbox id="tapc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="fs" tabindex="-1" name="多线程驱动音频系统" tooltip="启用后将音频耗时操作移至线程中执行,减少由音频 API 调用导致的卡顿。">
<ui-checkbox id="fsc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="ts" tabindex="-1" name="线程通信调度器" tooltip="启用后将会对多次数据通信打包发送,这可能会减少因通信次数带来的性能消耗。">
<ui-checkbox id="tsc" tabindex="-1"></ui-checkbox>
</ui-prop>
</div>
</div>
`,
$: {
unripe_area: '#unripe',
unripe_tip: '#unripe_tip',
ready_area: '#ready',
thread_debug: '#td',
thread_debug_checkbox: '#tdc',
thread_asset_pipeline: '#tap',
thread_asset_pipeline_checkbox: '#tapc',
thread_audio_system: '#fs',
thread_audio_system_checkbox: '#fsc',
thread_scheduler: '#ts',
thread_scheduler_checkbox: '#tsc',
},
ready() {
Editor.Ipc.sendToMain('enhance-kit:getSettings', (error, data) => {
if (error) {
this.$unripe_tip.textContent = '发生错误:' + String(error);
this.$unripe_tip.style.color = 'red';
return;
}
if (data.code === 0) {
this.$unripe_area.classList.add('hidden');
this.$ready_area.classList.remove('hidden');
this.$thread_debug_checkbox.checked = data.CC_WORKER_DEBUG;
this.$thread_asset_pipeline_checkbox.checked = data.CC_WORKER_ASSET_PIPELINE;
this.$thread_audio_system_checkbox.checked = data.CC_WORKER_AUDIO_SYSTEM;
this.$thread_scheduler_checkbox.checked = data.CC_WORKER_SCHEDULER;
this.$thread_debug_checkbox.addEventListener('change', () => {
this.setSettings("CC_WORKER_DEBUG", this.$thread_debug_checkbox.checked);
});
this.$thread_asset_pipeline_checkbox.addEventListener('change', () => {
this.setSettings("CC_WORKER_ASSET_PIPELINE", this.$thread_asset_pipeline_checkbox.checked);
});
this.$thread_audio_system_checkbox.addEventListener('change', () => {
this.setSettings("CC_WORKER_AUDIO_SYSTEM", this.$thread_audio_system_checkbox.checked);
});
this.$thread_scheduler_checkbox.addEventListener('change', () => {
this.setSettings("CC_WORKER_SCHEDULER", this.$thread_scheduler_checkbox.checked);
});
} else {
this.$unripe_tip.textContent = data.errMsg;
this.$unripe_tip.style.color = 'red';
}
}, 5000);
},
async setSettings(macro, value) {
return new Promise((resolve, reject) => {
Editor.Ipc.sendToMain('enhance-kit:setSettings', macro, value, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
}, 3000);
});
},
});