mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-10-09 15:45:24 +00:00
[adapters] [extension] 增加对多线程 HTTP 的支持,修复创建多线程扩展错误的问题
This commit is contained in:
@@ -56,6 +56,8 @@ module.exports = {
|
||||
'thread_debug_desc': 'When enabled, detailed logs will be output for debugging, which may significantly reduce performance.',
|
||||
'thread_custom': 'Project multithreading extension',
|
||||
'thread_custom_desc': 'This will activate the multithreading extension in the project\'s worker directory.',
|
||||
'thread_http': 'Multi-threaded XMLHttpRequest',
|
||||
'thread_http_desc': 'When enabled, XMLHttpRequest will be moved to a thread for execution, because there is a data roundtrip time, please actually test whether there is an improvement in performance.',
|
||||
'thread_asset_pipeline': 'Multi-threaded Asset Pipeline',
|
||||
'thread_asset_pipeline_desc': 'When enabled, the asset pipeline will be executed in a separate thread, reducing stuttering caused by resource downloading, caching, and loading.',
|
||||
'thread_audio_system': 'Multi-threaded Audio System',
|
||||
|
@@ -56,6 +56,8 @@ module.exports = {
|
||||
'thread_debug_desc': '启用后将会输出详细日志以便进行调试,这可能会大幅降低性能。',
|
||||
'thread_custom': '项目多线程扩展',
|
||||
'thread_custom_desc': '启用后将会激活项目 worker 目录下的自定义扩展。',
|
||||
'thread_http': '多线程驱动 XMLHttpRequest',
|
||||
'thread_http_desc': '启用后 XMLHttpRequest 将会移至线程中执行,由于存在数据往返的耗时,请实际测试对性能是否有提升。',
|
||||
'thread_asset_pipeline': '多线程驱动资源管线',
|
||||
'thread_asset_pipeline_desc': '启用后将资源管线移至线程中执行,减少由资源下载、缓存与加载导致的卡顿。',
|
||||
'thread_audio_system': '多线程驱动音频系统',
|
||||
|
@@ -110,6 +110,7 @@ function getSettings() {
|
||||
CC_WORKER_SCHEDULER: getMacroBooleanValue(content, "CC_WORKER_SCHEDULER"),
|
||||
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"),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -174,7 +175,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) {
|
||||
if (result.CC_WORKER_ASSET_PIPELINE || result.CC_WORKER_AUDIO_SYSTEM || result.CC_CUSTOM_WORKER || result.CC_WORKER_HTTP_REQUEST) {
|
||||
// 没有 Worker 目录与配置的话提醒用户重新安装
|
||||
if (!(gameJson.workers && fs.existsSync(workerDir))) {
|
||||
Editor.error(t('thread_not_right_workers_dir'));
|
||||
|
@@ -39,6 +39,9 @@ Editor.Panel.extend({
|
||||
</ui-prop>
|
||||
</div>
|
||||
</ui-prop>
|
||||
<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="ts" tabindex="-1" name="${t('thread_scheduler')}" tooltip="${t('thread_scheduler_desc')}">
|
||||
<ui-checkbox id="tsc" tabindex="-1"></ui-checkbox>
|
||||
</ui-prop>
|
||||
@@ -56,6 +59,8 @@ Editor.Panel.extend({
|
||||
thread_asset_pipeline_checkbox: '#tapc',
|
||||
thread_custom: '#tc',
|
||||
thread_custom_checkbox: '#tcc',
|
||||
thread_http: '#th',
|
||||
thread_http_checkbox: '#thc',
|
||||
thread_audio_system: '#fs',
|
||||
thread_audio_system_checkbox: '#fsc',
|
||||
thread_audio_system_interval: '#fsi',
|
||||
@@ -81,6 +86,7 @@ Editor.Panel.extend({
|
||||
this.$thread_asset_pipeline_checkbox.checked = data.CC_WORKER_ASSET_PIPELINE;
|
||||
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_scheduler_checkbox.checked = data.CC_WORKER_SCHEDULER;
|
||||
|
||||
this.$thread_debug_checkbox.addEventListener('change', () => {
|
||||
@@ -95,6 +101,10 @@ Editor.Panel.extend({
|
||||
this.setSettings("CC_CUSTOM_WORKER", this.$thread_custom_checkbox.checked);
|
||||
});
|
||||
|
||||
this.$thread_http_checkbox.addEventListener('change', () => {
|
||||
this.setSettings("CC_WORKER_HTTP_REQUEST", this.$thread_http_checkbox.checked);
|
||||
});
|
||||
|
||||
const onAudioSystemEnableChange = (enabled) => {
|
||||
this.$thread_audio_system_interval_input.disabled = !enabled;
|
||||
};
|
||||
|
72
extension/templates/2.4.13/2.2.0/worker/creator-worker.d.ts
vendored
Normal file
72
extension/templates/2.4.13/2.2.0/worker/creator-worker.d.ts
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
declare namespace worker {
|
||||
export const createInnerAudioContext: any;
|
||||
export const connectSocket: any;
|
||||
export function createSharedArrayBuffer(size: number): WXSharedArrayBuffer;
|
||||
export const downloadFile: any;
|
||||
export const env: { USER_DATA_PATH: string };
|
||||
export const getFileSystemManager: any;
|
||||
export const onMessage: any;
|
||||
export const postMessage: any;
|
||||
export const request: any;
|
||||
export const uploadFile: any;
|
||||
export interface WXSharedArrayBuffer {
|
||||
buffer: SharedArrayBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "ipc-worker.js" {
|
||||
/**
|
||||
* 是否初始化完成
|
||||
*
|
||||
* - 初始化完成后,宏才被设为有效值
|
||||
*/
|
||||
export const inited: boolean;
|
||||
|
||||
/**
|
||||
* 访问主线程的入口
|
||||
*/
|
||||
export const main: any;
|
||||
|
||||
/**
|
||||
* 注册主线程可以访问的入口
|
||||
*
|
||||
* 请务必在脚本执行时调用才有效。
|
||||
*/
|
||||
export function registerHandler(name: string, handler: object): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否启用自定义 Worker
|
||||
*/
|
||||
declare var CC_CUSTOM_WORKER: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 调度模式,这会减少通信次数
|
||||
*/
|
||||
declare var CC_WORKER_SCHEDULER: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 调试模式
|
||||
*/
|
||||
declare var CC_WORKER_DEBUG: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 使用同步版本的文件系统 API
|
||||
*/
|
||||
declare var CC_WORKER_FS_SYNC: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 驱动资源管线
|
||||
*/
|
||||
declare var CC_WORKER_ASSET_PIPELINE: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 驱动音频系统
|
||||
*/
|
||||
declare var CC_WORKER_AUDIO_SYSTEM: boolean;
|
||||
|
||||
/**
|
||||
* Worker 音频系统同步音频属性的间隔时间(单位:毫秒)
|
||||
*/
|
||||
declare var CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL: number;
|
||||
|
13
extension/templates/2.4.13/2.2.0/worker/jsconfig.json
Normal file
13
extension/templates/2.4.13/2.2.0/worker/jsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "ES5",
|
||||
"skipLibCheck": true,
|
||||
"downlevelIteration": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
},
|
||||
"include": [
|
||||
"src",
|
||||
"creator-worker.d.ts"
|
||||
],
|
||||
}
|
3
extension/templates/2.4.13/2.2.0/worker/src/index.js
Normal file
3
extension/templates/2.4.13/2.2.0/worker/src/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
// 该文件会在 Worker 初始化时执行,可在这里初始化或者引用其它脚本
|
||||
// This file will be executed when the Worker is initialized, either initialize it here or require another script.
|
||||
require("math.js");
|
9
extension/templates/2.4.13/2.2.0/worker/src/math.js
Normal file
9
extension/templates/2.4.13/2.2.0/worker/src/math.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const { registerHandler } = require("ipc-worker.js");
|
||||
|
||||
export function add(x, y, callback) {
|
||||
callback(x + y);
|
||||
}
|
||||
|
||||
registerHandler("math", {
|
||||
add,
|
||||
});
|
76
extension/templates/2.4.13/2.3.0/worker/creator-worker.d.ts
vendored
Normal file
76
extension/templates/2.4.13/2.3.0/worker/creator-worker.d.ts
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
declare namespace worker {
|
||||
export const createInnerAudioContext: any;
|
||||
export const connectSocket: any;
|
||||
export function createSharedArrayBuffer(size: number): WXSharedArrayBuffer;
|
||||
export const downloadFile: any;
|
||||
export const env: { USER_DATA_PATH: string };
|
||||
export const getFileSystemManager: any;
|
||||
export const onMessage: any;
|
||||
export const postMessage: any;
|
||||
export const request: any;
|
||||
export const uploadFile: any;
|
||||
export interface WXSharedArrayBuffer {
|
||||
buffer: SharedArrayBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "ipc-worker.js" {
|
||||
/**
|
||||
* 是否初始化完成
|
||||
*
|
||||
* - 初始化完成后,宏才被设为有效值
|
||||
*/
|
||||
export const inited: boolean;
|
||||
|
||||
/**
|
||||
* 访问主线程的入口
|
||||
*/
|
||||
export const main: any;
|
||||
|
||||
/**
|
||||
* 注册主线程可以访问的入口
|
||||
*
|
||||
* 请务必在脚本执行时调用才有效。
|
||||
*/
|
||||
export function registerHandler(name: string, handler: object): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否启用自定义 Worker
|
||||
*/
|
||||
declare var CC_CUSTOM_WORKER: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 调度模式,这会减少通信次数
|
||||
*/
|
||||
declare var CC_WORKER_SCHEDULER: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 调试模式
|
||||
*/
|
||||
declare var CC_WORKER_DEBUG: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 使用同步版本的文件系统 API
|
||||
*/
|
||||
declare var CC_WORKER_FS_SYNC: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 驱动资源管线
|
||||
*/
|
||||
declare var CC_WORKER_ASSET_PIPELINE: boolean;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 驱动音频系统
|
||||
*/
|
||||
declare var CC_WORKER_AUDIO_SYSTEM: boolean;
|
||||
|
||||
/**
|
||||
* Worker 音频系统同步音频属性的间隔时间(单位:毫秒)
|
||||
*/
|
||||
declare var CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL: number;
|
||||
|
||||
/**
|
||||
* 是否启用 Worker 驱动 HTTP 请求
|
||||
*/
|
||||
declare var CC_WORKER_HTTP_REQUEST: boolean;
|
13
extension/templates/2.4.13/2.3.0/worker/jsconfig.json
Normal file
13
extension/templates/2.4.13/2.3.0/worker/jsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "ES5",
|
||||
"skipLibCheck": true,
|
||||
"downlevelIteration": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
},
|
||||
"include": [
|
||||
"src",
|
||||
"creator-worker.d.ts"
|
||||
],
|
||||
}
|
3
extension/templates/2.4.13/2.3.0/worker/src/index.js
Normal file
3
extension/templates/2.4.13/2.3.0/worker/src/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
// 该文件会在 Worker 初始化时执行,可在这里初始化或者引用其它脚本
|
||||
// This file will be executed when the Worker is initialized, either initialize it here or require another script.
|
||||
require("math.js");
|
9
extension/templates/2.4.13/2.3.0/worker/src/math.js
Normal file
9
extension/templates/2.4.13/2.3.0/worker/src/math.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const { registerHandler } = require("ipc-worker.js");
|
||||
|
||||
export function add(x, y, callback) {
|
||||
callback(x + y);
|
||||
}
|
||||
|
||||
registerHandler("math", {
|
||||
add,
|
||||
});
|
Reference in New Issue
Block a user