diff --git a/adapters/common/engine/AssetManager.js b/adapters/common/engine/AssetManager.js index 31a05d7d..386255dc 100644 --- a/adapters/common/engine/AssetManager.js +++ b/adapters/common/engine/AssetManager.js @@ -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?:\/\/.*/; diff --git a/adapters/common/cache-manager-proxy.js b/adapters/common/worker/cache-manager-proxy.js similarity index 100% rename from adapters/common/cache-manager-proxy.js rename to adapters/common/worker/cache-manager-proxy.js diff --git a/adapters/modules.json b/adapters/modules.json index 96fb3495..faf21bb6 100644 --- a/adapters/modules.json +++ b/adapters/modules.json @@ -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": [] -} \ No newline at end of file +} diff --git a/adapters/platforms/wechat/index.js b/adapters/platforms/wechat/index.js index c62b012a..cc8eb09b 100644 --- a/adapters/platforms/wechat/index.js +++ b/adapters/platforms/wechat/index.js @@ -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; + } + }, }); diff --git a/adapters/platforms/wechat/res/game.js b/adapters/platforms/wechat/res/game.js index 26114179..98b04d2d 100644 --- a/adapters/platforms/wechat/res/game.js +++ b/adapters/platforms/wechat/res/game.js @@ -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(); }); diff --git a/adapters/platforms/wechat/res/workers/audio-worker.js b/adapters/platforms/wechat/res/workers/audio-worker.js new file mode 100644 index 00000000..9970d62e --- /dev/null +++ b/adapters/platforms/wechat/res/workers/audio-worker.js @@ -0,0 +1,9 @@ +var audio_worker = { + map: {}, + create(callback, sn) { + this.map[sn] = worker.createInnerAudioContext(); + }, + +}; + +module.exports = audio_worker; diff --git a/adapters/platforms/wechat/res/workers/handlers.js b/adapters/platforms/wechat/res/workers/handlers.js index 4d2c7139..b36e7b86 100644 --- a/adapters/platforms/wechat/res/workers/handlers.js +++ b/adapters/platforms/wechat/res/workers/handlers.js @@ -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); +} diff --git a/adapters/platforms/wechat/res/workers/ipc-worker.js b/adapters/platforms/wechat/res/workers/ipc-worker.js index ec36a506..902c1570 100644 --- a/adapters/platforms/wechat/res/workers/ipc-worker.js +++ b/adapters/platforms/wechat/res/workers/ipc-worker.js @@ -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(); diff --git a/adapters/platforms/wechat/res/workers/macro.js b/adapters/platforms/wechat/res/workers/macro.js index 8d3d8785..d2299a7e 100644 --- a/adapters/platforms/wechat/res/workers/macro.js +++ b/adapters/platforms/wechat/res/workers/macro.js @@ -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; diff --git a/adapters/platforms/wechat/res/worker_adapter/asset-manager.js b/adapters/platforms/wechat/worker/asset-manager.js similarity index 100% rename from adapters/platforms/wechat/res/worker_adapter/asset-manager.js rename to adapters/platforms/wechat/worker/asset-manager.js diff --git a/adapters/platforms/wechat/worker/audio.js b/adapters/platforms/wechat/worker/audio.js new file mode 100644 index 00000000..00452915 --- /dev/null +++ b/adapters/platforms/wechat/worker/audio.js @@ -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; diff --git a/adapters/platforms/wechat/res/worker_adapter/handlers.js b/adapters/platforms/wechat/worker/handlers.js similarity index 51% rename from adapters/platforms/wechat/res/worker_adapter/handlers.js rename to adapters/platforms/wechat/worker/handlers.js index 98ae3a59..dffd0f8c 100644 --- a/adapters/platforms/wechat/res/worker_adapter/handlers.js +++ b/adapters/platforms/wechat/worker/handlers.js @@ -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); +} diff --git a/adapters/platforms/wechat/res/worker_adapter/index.js b/adapters/platforms/wechat/worker/index.js similarity index 53% rename from adapters/platforms/wechat/res/worker_adapter/index.js rename to adapters/platforms/wechat/worker/index.js index 9f18eb34..752cf384 100644 --- a/adapters/platforms/wechat/res/worker_adapter/index.js +++ b/adapters/platforms/wechat/worker/index.js @@ -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(); } }, }; diff --git a/adapters/platforms/wechat/res/worker_adapter/ipc-main.js b/adapters/platforms/wechat/worker/ipc-main.js similarity index 80% rename from adapters/platforms/wechat/res/worker_adapter/ipc-main.js rename to adapters/platforms/wechat/worker/ipc-main.js index 2f873b0d..ca3172b6 100644 --- a/adapters/platforms/wechat/res/worker_adapter/ipc-main.js +++ b/adapters/platforms/wechat/worker/ipc-main.js @@ -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, ]); }, diff --git a/adapters/platforms/wechat/res/worker_adapter/macro.js b/adapters/platforms/wechat/worker/macro.js similarity index 62% rename from adapters/platforms/wechat/res/worker_adapter/macro.js rename to adapters/platforms/wechat/worker/macro.js index bda96acf..e1479444 100644 --- a/adapters/platforms/wechat/res/worker_adapter/macro.js +++ b/adapters/platforms/wechat/worker/macro.js @@ -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;