mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2024-12-25 19:28:28 +00:00
[adapters] 优化多线程特性文件结构,增加部分多线程音频系统代码,支持 Worker 子包特性(默认不开启),修复 Devtools 下强制不启用 Worker 问题
This commit is contained in:
parent
514e203483
commit
fc9792c562
@ -1,4 +1,4 @@
|
||||
const cacheManager = CC_WORKER_ASSET_PIPELINE ? require('../cache-manager-proxy') : require('../cache-manager');
|
||||
const cacheManager = CC_WORKER_ASSET_PIPELINE ? require('../worker/cache-manager-proxy') : require('../cache-manager');
|
||||
const { fs, downloadFile, readText, readArrayBuffer, readJson, loadSubpackage, getUserDataPath, exists } = window.fsUtils;
|
||||
|
||||
const REGEX = /^https?:\/\/.*/;
|
||||
|
@ -8,7 +8,8 @@
|
||||
"Audio": [
|
||||
"./common/engine/Audio.js",
|
||||
"./common/engine/AudioEngine.js",
|
||||
"./platforms/alipay/wrapper/engine/AudioEngine.js"
|
||||
"./platforms/alipay/wrapper/engine/AudioEngine.js",
|
||||
"./platforms/wechat/worker/audio.js"
|
||||
],
|
||||
"AudioSource": [],
|
||||
"Action": [],
|
||||
@ -56,4 +57,4 @@
|
||||
"WechatSubContext": [],
|
||||
"SwanSubContext": [],
|
||||
"TypeScript Polyfill": []
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,21 @@
|
||||
const _global = GameGlobal;
|
||||
const adapter = _global.__globalAdapter = {};
|
||||
|
||||
let inited = false;
|
||||
let _callback = null;
|
||||
let _wait_worker_t = 0;
|
||||
|
||||
Object.assign(adapter, {
|
||||
init () {
|
||||
init() {
|
||||
const { init: initWorker } = require('./worker');
|
||||
initWorker(() => {
|
||||
inited = true;
|
||||
if (CC_USE_WORKER && _callback) {
|
||||
console.log("worker waiting time:", Date.now() - _wait_worker_t);
|
||||
}
|
||||
_callback && _callback();
|
||||
_callback = null;
|
||||
});
|
||||
require('./wrapper/builtin');
|
||||
_global.DOMParser = require('../../common/xmldom/dom-parser').DOMParser;
|
||||
require('./wrapper/unify');
|
||||
@ -11,10 +24,22 @@ Object.assign(adapter, {
|
||||
require('./wrapper/systemInfo');
|
||||
},
|
||||
|
||||
adaptEngine () {
|
||||
adaptEngine() {
|
||||
require('./wrapper/error-reporter');
|
||||
require('../../common/engine');
|
||||
require('./wrapper/engine');
|
||||
require('./wrapper/sub-context-adapter');
|
||||
},
|
||||
|
||||
onInited(callback) {
|
||||
_wait_worker_t = Date.now();
|
||||
if (inited) {
|
||||
if (CC_USE_WORKER) {
|
||||
console.log("worker waiting time:", Date.now() - _wait_worker_t);
|
||||
}
|
||||
callback();
|
||||
} else {
|
||||
_callback = callback;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
@ -1,6 +1,4 @@
|
||||
const { init: initWorker, onInited: onWorkerInited } = require('./worker_adapter/index.js');
|
||||
require('adapter-js-path');
|
||||
initWorker();
|
||||
__globalAdapter.init();
|
||||
require('cocos2d-js-path');
|
||||
require('physics-js-path');
|
||||
@ -19,8 +17,6 @@ if (cc.sys.platform !== cc.sys.WECHAT_GAME_SUB) {
|
||||
cc.macro.CLEANUP_IMAGE_CACHE = true;
|
||||
}
|
||||
|
||||
const t = Date.now();
|
||||
onWorkerInited(() => {
|
||||
console.log("worker waiting time:", Date.now() - t);
|
||||
__globalAdapter.onInited(() => {
|
||||
window.boot();
|
||||
});
|
||||
|
9
adapters/platforms/wechat/res/workers/audio-worker.js
Normal file
9
adapters/platforms/wechat/res/workers/audio-worker.js
Normal file
@ -0,0 +1,9 @@
|
||||
var audio_worker = {
|
||||
map: {},
|
||||
create(callback, sn) {
|
||||
this.map[sn] = worker.createInnerAudioContext();
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
module.exports = audio_worker;
|
@ -4,3 +4,8 @@ if (globalThis.CC_WORKER_ASSET_PIPELINE) {
|
||||
const cacheManager = require("./cache-manager-worker.js");
|
||||
registerHandler("cacheManager", cacheManager);
|
||||
}
|
||||
|
||||
if (globalThis.CC_WORKER_AUDIO_SYSTEM) {
|
||||
const audio = require("./audio-worker.js");
|
||||
registerHandler("audio", audio);
|
||||
}
|
||||
|
@ -144,7 +144,7 @@ function _initFromWorker(id, meta) {
|
||||
wrappers,
|
||||
CC_WORKER_FS_SYNC,
|
||||
CC_WORKER_ASSET_PIPELINE,
|
||||
CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD,
|
||||
CC_WORKER_AUDIO_SYSTEM,
|
||||
] = meta;
|
||||
|
||||
for (const wrapper of wrappers) {
|
||||
@ -159,7 +159,7 @@ function _initFromWorker(id, meta) {
|
||||
|
||||
globalThis.CC_WORKER_FS_SYNC = CC_WORKER_FS_SYNC;
|
||||
globalThis.CC_WORKER_ASSET_PIPELINE = CC_WORKER_ASSET_PIPELINE;
|
||||
globalThis.CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD = CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD;
|
||||
globalThis.CC_WORKER_AUDIO_SYSTEM = CC_WORKER_AUDIO_SYSTEM;
|
||||
|
||||
_inited = true;
|
||||
if (_initCallback) _initCallback();
|
||||
|
@ -9,8 +9,8 @@ globalThis.CC_WORKER_DEBUG = false;
|
||||
// 是否启用 Worker 使用同步版本的文件系统 API
|
||||
globalThis.CC_WORKER_FS_SYNC = null;
|
||||
|
||||
// 是否启用 Worker 驱动资源管线(下载、缓存)
|
||||
// 是否启用 Worker 驱动资源管线
|
||||
globalThis.CC_WORKER_ASSET_PIPELINE = null;
|
||||
|
||||
// 是否启用 Worker 驱动资源管线(加载)
|
||||
globalThis.CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD = null;
|
||||
// 是否启用 Worker 驱动音频系统
|
||||
globalThis.CC_WORKER_AUDIO_SYSTEM = null;
|
||||
|
77
adapters/platforms/wechat/worker/audio.js
Normal file
77
adapters/platforms/wechat/worker/audio.js
Normal file
@ -0,0 +1,77 @@
|
||||
let _id = 0;
|
||||
|
||||
class WorkerAudio {
|
||||
id = ++_id;
|
||||
|
||||
get src() {
|
||||
|
||||
}
|
||||
set src(str) {
|
||||
|
||||
}
|
||||
|
||||
get loop() {
|
||||
|
||||
}
|
||||
set loop(v) {
|
||||
|
||||
}
|
||||
_loop = false;
|
||||
|
||||
get volume() {
|
||||
|
||||
}
|
||||
set volume(v) {
|
||||
|
||||
}
|
||||
_volume = 1;
|
||||
|
||||
// 只读,从 Worker 单向同步值
|
||||
duration = 0;
|
||||
currentTime = 0;
|
||||
paused = true;
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
get src() {
|
||||
|
||||
}
|
||||
set src(clip) {
|
||||
|
||||
}
|
||||
|
||||
play() {
|
||||
|
||||
}
|
||||
|
||||
pause() {
|
||||
|
||||
}
|
||||
|
||||
seek() {
|
||||
|
||||
}
|
||||
|
||||
stop() {
|
||||
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var audioWorkerAdapter = {
|
||||
on(id, callback) {
|
||||
|
||||
},
|
||||
off(id, callback) {
|
||||
|
||||
},
|
||||
};
|
||||
|
||||
globalThis.WorkerAudio = WorkerAudio;
|
||||
module.exports = audioWorkerAdapter;
|
@ -2,3 +2,8 @@ if (CC_WORKER_ASSET_PIPELINE) {
|
||||
const assetManagerWorkerAdapter = require("./asset-manager.js");
|
||||
ipcMain.registerHandler("assetManager", assetManagerWorkerAdapter);
|
||||
}
|
||||
|
||||
if (CC_WORKER_AUDIO_SYSTEM && cc._Audio) {
|
||||
const audioWorkerAdapter = require("./audio.js");
|
||||
ipcMain.registerHandler("audioAdapter", audioWorkerAdapter);
|
||||
}
|
@ -1,12 +1,9 @@
|
||||
require("./macro.js");
|
||||
require("./macro");
|
||||
require("./ipc-main.js");
|
||||
require("./handlers.js");
|
||||
|
||||
let inited = false;
|
||||
let _callback = null;
|
||||
|
||||
module.exports = {
|
||||
init() {
|
||||
init(callback) {
|
||||
if (CC_USE_WORKER) {
|
||||
var t = Date.now();
|
||||
ipcMain.init(() => {
|
||||
@ -15,25 +12,15 @@ module.exports = {
|
||||
CC_USE_WORKER,
|
||||
CC_WORKER_DEBUG,
|
||||
CC_WORKER_ASSET_PIPELINE,
|
||||
CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD,
|
||||
CC_WORKER_AUDIO_SYSTEM,
|
||||
CC_WORKER_SCHEDULER,
|
||||
CC_WORKER_FS_SYNC,
|
||||
CC_WORKER_SUB_PACKAGE,
|
||||
});
|
||||
inited = true;
|
||||
_callback && _callback();
|
||||
_callback = null;
|
||||
callback && callback();
|
||||
});
|
||||
} else {
|
||||
inited = true;
|
||||
_callback && _callback();
|
||||
_callback = null;
|
||||
}
|
||||
},
|
||||
onInited(callback) {
|
||||
if (inited) {
|
||||
callback();
|
||||
} else {
|
||||
_callback = callback;
|
||||
callback && callback();
|
||||
}
|
||||
},
|
||||
};
|
@ -16,25 +16,44 @@ const ipcMain = {
|
||||
init(callback) {
|
||||
this._initCallback = callback;
|
||||
|
||||
// NOTE { useExperimentalWorker: true } 会让有状态的 Worker 处理很复杂,暂时不使用
|
||||
this.worker = wx.createWorker("workers/index.js");
|
||||
|
||||
this.worker.onMessage(
|
||||
CC_WORKER_SCHEDULER
|
||||
? msgs => {
|
||||
for (let index = 0; index < msgs.length; index++) {
|
||||
const msg = msgs[index];
|
||||
this._handleWorkerMessage(msg);
|
||||
const loadSrc = (cb) => {
|
||||
if (CC_WORKER_SUB_PACKAGE) {
|
||||
wx.preDownloadSubpackage({
|
||||
packageType: "workers",
|
||||
success() {
|
||||
cb();
|
||||
},
|
||||
fail(res) {
|
||||
console.error("load worker fail:", res);
|
||||
loadSrc(cb);
|
||||
}
|
||||
}
|
||||
: this._handleWorkerMessage.bind(this)
|
||||
);
|
||||
});
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
||||
if (CC_WORKER_SCHEDULER) {
|
||||
sendScheduler.init(this);
|
||||
}
|
||||
loadSrc(() => {
|
||||
// NOTE { useExperimentalWorker: true } 会让有状态的 Worker 处理很复杂,暂时不使用
|
||||
this.worker = wx.createWorker("workers/index.js");
|
||||
|
||||
this._init();
|
||||
this.worker.onMessage(
|
||||
CC_WORKER_SCHEDULER
|
||||
? msgs => {
|
||||
for (let index = 0; index < msgs.length; index++) {
|
||||
const msg = msgs[index];
|
||||
this._handleWorkerMessage(msg);
|
||||
}
|
||||
}
|
||||
: this._handleWorkerMessage.bind(this)
|
||||
);
|
||||
|
||||
if (CC_WORKER_SCHEDULER) {
|
||||
sendScheduler.init(this);
|
||||
}
|
||||
|
||||
this._init();
|
||||
});
|
||||
},
|
||||
|
||||
_handleWorkerMessage(msg) {
|
||||
@ -90,7 +109,7 @@ const ipcMain = {
|
||||
_handlers,
|
||||
CC_WORKER_FS_SYNC,
|
||||
CC_WORKER_ASSET_PIPELINE,
|
||||
CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD,
|
||||
CC_WORKER_AUDIO_SYSTEM,
|
||||
]);
|
||||
},
|
||||
|
@ -2,18 +2,21 @@ const isSubContext = wx.getOpenDataContext === undefined;
|
||||
const sysinfo = wx.getSystemInfoSync();
|
||||
const platform = sysinfo.platform.toLowerCase();
|
||||
const isAndroid = platform === "android";
|
||||
const isDevtools = platform === "devtools";
|
||||
const sdkVersion = sysinfo.SDKVersion.split('.').map(Number);
|
||||
// >= 2.20.2
|
||||
const hasWorker = sdkVersion[0] > 2 || (sdkVersion[0] === 2 && (sdkVersion[1] > 20 || (sdkVersion[1] === 20 && sdkVersion[2] >= 2)));
|
||||
// >= 2.27.3
|
||||
const useSubpackage = sdkVersion[0] > 2 || (sdkVersion[0] === 2 && (sdkVersion[1] > 27 || (sdkVersion[1] === 27 && sdkVersion[2] >= 3)));
|
||||
|
||||
// 是否启用 Worker 驱动资源管线(下载、缓存)
|
||||
// 是否启用 Worker 驱动资源管线
|
||||
globalThis.CC_WORKER_ASSET_PIPELINE = false;
|
||||
|
||||
// 是否启用 Worker 驱动资源管线(加载)
|
||||
globalThis.CC_WORKER_ASSET_PIPELINE_INCLUDE_LOAD = false;
|
||||
// 是否启用 Worker 驱动音频系统
|
||||
globalThis.CC_WORKER_AUDIO_SYSTEM = false;
|
||||
|
||||
// NOTE 截止 2024.10.22,微信未修复 iOS、Windows、Mac 上仅文件系统 API 可以正常使用的问题
|
||||
globalThis.CC_WORKER_ASSET_PIPELINE = isAndroid && globalThis.CC_WORKER_ASSET_PIPELINE;
|
||||
globalThis.CC_WORKER_ASSET_PIPELINE = (isAndroid || isDevtools) && globalThis.CC_WORKER_ASSET_PIPELINE;
|
||||
|
||||
// 是否启用 Worker
|
||||
globalThis.CC_USE_WORKER = (CC_WORKER_ASSET_PIPELINE) && hasWorker && !isSubContext;
|
||||
@ -26,4 +29,8 @@ globalThis.CC_WORKER_SCHEDULER = true;
|
||||
|
||||
// 是否启用 Worker 使用同步版本的文件系统 API
|
||||
// NOTE: IOS 不支持 async 文件系统 API,Android 不支持部分 sync 文件系统 API,其余系统暂不确定
|
||||
globalThis.CC_WORKER_FS_SYNC = !isAndroid;
|
||||
globalThis.CC_WORKER_FS_SYNC = !isAndroid && !isDevtools;
|
||||
|
||||
// 是否启用 Worker 子包
|
||||
// NOTE 截止 2024.10.22,部分安卓机型声明使用子包 Worker 会报 java.string 错误
|
||||
globalThis.CC_WORKER_SUB_PACKAGE = false;
|
Loading…
Reference in New Issue
Block a user