[engine] [adapters] [common] 增加 WebSocket 和子包的开关

This commit is contained in:
SmallMain 2024-11-22 16:54:43 +08:00
parent d6fb7f841b
commit 5a03a9dc86
No known key found for this signature in database
11 changed files with 66 additions and 6 deletions

View File

@ -147,6 +147,7 @@ function _initFromWorker(id, meta) {
CC_WORKER_AUDIO_SYSTEM,
CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL,
CC_WORKER_HTTP_REQUEST,
CC_WORKER_WEBSOCKET,
] = meta;
for (const wrapper of wrappers) {
@ -164,6 +165,7 @@ function _initFromWorker(id, meta) {
globalThis.CC_WORKER_AUDIO_SYSTEM = CC_WORKER_AUDIO_SYSTEM;
globalThis.CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL = CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL;
globalThis.CC_WORKER_HTTP_REQUEST = CC_WORKER_HTTP_REQUEST;
globalThis.CC_WORKER_WEBSOCKET = CC_WORKER_WEBSOCKET;
_inited = true;
if (_initCallback) _initCallback();

View File

@ -21,5 +21,8 @@ globalThis.CC_WORKER_AUDIO_SYSTEM = null;
// 是否启用 Worker 驱动 HTTP 请求
globalThis.CC_WORKER_HTTP_REQUEST = null;
// 是否启用 Worker 驱动 WebSocket
globalThis.CC_WORKER_WEBSOCKET = null;
// Worker 音频系统同步音频属性的间隔时间(单位:毫秒)
globalThis.CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL = null;

View File

@ -16,6 +16,7 @@ module.exports = {
CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL,
CC_CUSTOM_WORKER,
CC_WORKER_HTTP_REQUEST,
CC_WORKER_WEBSOCKET,
CC_WORKER_SCHEDULER,
CC_WORKER_FS_SYNC,
CC_WORKER_SUB_PACKAGE,

View File

@ -112,6 +112,7 @@ const ipcMain = {
CC_WORKER_AUDIO_SYSTEM,
CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL,
CC_WORKER_HTTP_REQUEST,
CC_WORKER_WEBSOCKET,
]);
},

View File

@ -35,6 +35,13 @@ if (!("CC_WORKER_HTTP_REQUEST" in globalThis)) {
globalThis.CC_WORKER_HTTP_REQUEST = (isAndroid || isDevtools) && globalThis.CC_WORKER_HTTP_REQUEST;
}
// 是否启用 Worker 驱动 WebSocket
if (!("CC_WORKER_WEBSOCKET" in globalThis)) {
globalThis.CC_WORKER_WEBSOCKET = false;
// NOTE 截止 2024.10.22,微信未修复 iOS、Windows、Mac 上仅文件系统 API 可以正常使用的问题
globalThis.CC_WORKER_WEBSOCKET = (isAndroid || isDevtools) && globalThis.CC_WORKER_WEBSOCKET;
}
// 是否启用自定义 Worker
if (!("CC_CUSTOM_WORKER" in globalThis)) {
globalThis.CC_CUSTOM_WORKER = false;
@ -42,7 +49,7 @@ if (!("CC_CUSTOM_WORKER" in globalThis)) {
// 是否启用 Worker
if (!("CC_USE_WORKER" in globalThis)) {
globalThis.CC_USE_WORKER = (CC_WORKER_ASSET_PIPELINE || CC_WORKER_AUDIO_SYSTEM || CC_CUSTOM_WORKER || CC_WORKER_HTTP_REQUEST) && hasWorker && !isSubContext;
globalThis.CC_USE_WORKER = (CC_WORKER_ASSET_PIPELINE || CC_WORKER_AUDIO_SYSTEM || CC_CUSTOM_WORKER || CC_WORKER_HTTP_REQUEST || CC_WORKER_WEBSOCKET) && hasWorker && !isSubContext;
}
// 是否启用 Worker 调试模式

5
creator-sp.d.ts vendored
View File

@ -967,6 +967,11 @@ declare var CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL: number;
*/
declare var CC_WORKER_HTTP_REQUEST: boolean;
/**
* Worker WebSocket
*/
declare var CC_WORKER_WEBSOCKET: boolean;
/**
* Worker
*/

View File

@ -64,6 +64,10 @@ module.exports = {
'thread_audio_system_desc': 'When enabled, time-consuming audio operations will be executed in a separate thread, reducing stuttering caused by audio API calls.',
'thread_audio_sync': 'Property Sync Interval (milliseconds)',
'thread_audio_sync_desc': 'How often the properties of audio instances (playback progress, total duration, etc.) are synchronized from the worker thread to the main thread. Too frequent updates may impact performance.',
'thread_ws': 'Multi-threaded WebSocket',
'thread_ws_desc': 'After enabling, the WebSocket will be executed in a separate thread, and custom data encoding/decoding is allowed. Due to the time-consuming data round-trip, please test in practice to see if there is an improvement in performance.',
'thread_subpackage': 'Set Mini-Game subpackage',
'thread_subpackage_desc': 'After enabling, the workers directory will be set as a mini-game subpackage, which will reduce the size of the main package but may affect startup performance. Please enable according to the situation.',
'thread_scheduler': 'Thread Communication Scheduler',
'thread_scheduler_desc': 'When enabled, multiple data communications will be bundled and sent together, which may reduce performance overhead caused by frequent communications.',
'thread_compile_custom_thread_menu': 'Recompile the multithreading extension',

View File

@ -64,6 +64,10 @@ module.exports = {
'thread_audio_system_desc': '启用后将音频耗时操作移至线程中执行,减少由音频 API 调用导致的卡顿。',
'thread_audio_sync': '属性同步间隔(毫秒)',
'thread_audio_sync_desc': '间隔多久从 Worker 线程将音频实例的属性(播放进度、总时长等)同步到主线程,太频繁可能会影响性能。',
'thread_ws': '多线程驱动 WebSocket',
'thread_ws_desc': '启用后将 WebSocket 移至线程中执行,并且允许自定义数据编解码,由于存在数据往返的耗时,请实际测试对性能是否有提升。',
'thread_subpackage': '设为小游戏子包',
'thread_subpackage_desc': '启用后将 workers 目录设为小游戏子包,这会减少主包的大小,但可能影响启动性能,请视情况启用。',
'thread_scheduler': '线程通信调度器',
'thread_scheduler_desc': '启用后将多次数据通信打包发送,这可能会减少因通信次数带来的性能消耗。',
'thread_compile_custom_thread_menu': '重新编译多线程扩展',

View File

@ -111,6 +111,8 @@ function getSettings() {
CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL: getMacroIntegerValue(content, "CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL"),
CC_CUSTOM_WORKER: getMacroBooleanValue(content, "CC_CUSTOM_WORKER"),
CC_WORKER_HTTP_REQUEST: getMacroBooleanValue(content, "CC_WORKER_HTTP_REQUEST"),
CC_WORKER_WEBSOCKET: getMacroBooleanValue(content, "CC_WORKER_WEBSOCKET"),
CC_WORKER_SUB_PACKAGE: getMacroBooleanValue(content, "CC_WORKER_SUB_PACKAGE"),
};
}
}
@ -175,7 +177,7 @@ function checkAndModifyWorkerFiles() {
const gameJson = JSON.parse(fs.readFileSync(gameJsonPath, { encoding: "utf-8" }));
// 是否启用 Worker
if (result.CC_WORKER_ASSET_PIPELINE || result.CC_WORKER_AUDIO_SYSTEM || result.CC_CUSTOM_WORKER || result.CC_WORKER_HTTP_REQUEST) {
if (result.CC_WORKER_ASSET_PIPELINE || result.CC_WORKER_AUDIO_SYSTEM || result.CC_CUSTOM_WORKER || result.CC_WORKER_HTTP_REQUEST || result.CC_WORKER_WEBSOCKET) {
// 没有 Worker 目录与配置的话提醒用户重新安装
if (!(gameJson.workers && fs.existsSync(workerDir))) {
Editor.error(t('thread_not_right_workers_dir'));
@ -183,6 +185,12 @@ function checkAndModifyWorkerFiles() {
} else {
Editor.warn(t('thread_need_delete_files'));
}
// 是否使用子包
if (gameJson.workers) {
gameJson.workers.isSubpackage = result.CC_WORKER_SUB_PACKAGE;
fs.writeFileSync(gameJsonPath, JSON.stringify(gameJson, null, 2));
}
}
}

View File

@ -28,9 +28,6 @@ Editor.Panel.extend({
<ui-prop id="tap" tabindex="-1" name="${t('thread_asset_pipeline')}" tooltip="${t('thread_asset_pipeline_desc')}">
<ui-checkbox id="tapc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="tc" tabindex="-1" name="${t('thread_custom')}" tooltip="${t('thread_custom_desc')}">
<ui-checkbox id="tcc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="fs" tabindex="-1" name="${t('thread_audio_system')}" tooltip="${t('thread_audio_system_desc')}" foldable>
<ui-checkbox id="fsc" tabindex="-1"></ui-checkbox>
<div slot="child">
@ -42,8 +39,17 @@ Editor.Panel.extend({
<ui-prop id="th" tabindex="-1" name="${t('thread_http')}" tooltip="${t('thread_http_desc')}">
<ui-checkbox id="thc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="tw" tabindex="-1" name="${t('thread_ws')}" tooltip="${t('thread_ws_desc')}">
<ui-checkbox id="twc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="tc" tabindex="-1" name="${t('thread_custom')}" tooltip="${t('thread_custom_desc')}">
<ui-checkbox id="tcc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="ts" tabindex="-1" name="${t('thread_scheduler')}" tooltip="${t('thread_scheduler_desc')}">
<ui-checkbox id="tsc" tabindex="-1"></ui-checkbox>
<ui-checkbox id="tsc" tabindex="-1"></ui-checkbox>
</ui-prop>
<ui-prop id="tsp" tabindex="-1" name="${t('thread_subpackage')}" tooltip="${t('thread_subpackage_desc')}">
<ui-checkbox id="tspc" tabindex="-1"></ui-checkbox>
</ui-prop>
</div>
</div>
@ -61,12 +67,16 @@ Editor.Panel.extend({
thread_custom_checkbox: '#tcc',
thread_http: '#th',
thread_http_checkbox: '#thc',
thread_ws: '#tw',
thread_ws_checkbox: '#twc',
thread_audio_system: '#fs',
thread_audio_system_checkbox: '#fsc',
thread_audio_system_interval: '#fsi',
thread_audio_system_interval_input: '#fsii',
thread_scheduler: '#ts',
thread_scheduler_checkbox: '#tsc',
thread_subpackage: '#tsp',
thread_subpackage_checkbox: '#tspc',
},
ready() {
@ -87,6 +97,8 @@ Editor.Panel.extend({
this.$thread_audio_system_checkbox.checked = data.CC_WORKER_AUDIO_SYSTEM;
this.$thread_audio_system_interval_input.value = data.CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL;
this.$thread_http_checkbox.checked = data.CC_WORKER_HTTP_REQUEST;
this.$thread_ws_checkbox.checked = data.CC_WORKER_WEBSOCKET;
this.$thread_subpackage_checkbox.checked = data.CC_WORKER_SUB_PACKAGE;
this.$thread_scheduler_checkbox.checked = data.CC_WORKER_SCHEDULER;
this.$thread_debug_checkbox.addEventListener('change', () => {
@ -105,6 +117,14 @@ Editor.Panel.extend({
this.setSettings("CC_WORKER_HTTP_REQUEST", this.$thread_http_checkbox.checked);
});
this.$thread_ws_checkbox.addEventListener('change', () => {
this.setSettings("CC_WORKER_WEBSOCKET", this.$thread_ws_checkbox.checked);
});
this.$thread_subpackage_checkbox.addEventListener('change', () => {
this.setSettings("CC_WORKER_SUB_PACKAGE", this.$thread_subpackage_checkbox.checked);
});
const onAudioSystemEnableChange = (enabled) => {
this.$thread_audio_system_interval_input.disabled = !enabled;
};

View File

@ -74,3 +74,8 @@ declare var CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL: number;
* Worker HTTP
*/
declare var CC_WORKER_HTTP_REQUEST: boolean;
/**
* Worker WebSocket
*/
declare var CC_WORKER_WEBSOCKET: boolean;