| 61 | * 微信小游戏平台适配器 |
| 62 | */ |
| 63 | export class WeChatAdapter implements IPlatformAdapter { |
| 64 | readonly name = 'wechat-minigame'; |
| 65 | readonly version: string; |
| 66 | |
| 67 | // 子系统实例 |
| 68 | private _canvas: WeChatCanvasSubsystem | null = null; |
| 69 | private _audio: WeChatAudioSubsystem | null = null; |
| 70 | private _storage: WeChatStorageSubsystem | null = null; |
| 71 | private _network: WeChatNetworkSubsystem | null = null; |
| 72 | private _input: WeChatInputSubsystem | null = null; |
| 73 | private _file: WeChatFileSubsystem | null = null; |
| 74 | private _wasm: WeChatWASMSubsystem | null = null; |
| 75 | |
| 76 | private _deviceInfo: WechatMinigame.DeviceInfo | null = null; |
| 77 | private _windowInfo: WechatMinigame.WindowInfo | null = null; |
| 78 | private _appBaseInfo: WechatMinigame.AppBaseInfo | null = null; |
| 79 | |
| 80 | constructor() { |
| 81 | if (!isWeChatMiniGame()) { |
| 82 | throw new Error('当前环境不是微信小游戏环境'); |
| 83 | } |
| 84 | |
| 85 | // 使用新的分离 API 获取系统信息 |
| 86 | const wxApi = getWx(); |
| 87 | this._deviceInfo = wxApi.getDeviceInfo(); |
| 88 | this._windowInfo = wxApi.getWindowInfo(); |
| 89 | this._appBaseInfo = wxApi.getAppBaseInfo(); |
| 90 | this.version = this._appBaseInfo.SDKVersion; |
| 91 | } |
| 92 | |
| 93 | // ======================================================================== |
| 94 | // IPlatformAdapter 基础实现 |
| 95 | // ======================================================================== |
| 96 | |
| 97 | isWorkerSupported(): boolean { |
| 98 | // 微信小游戏支持 Worker,但有限制 |
| 99 | return typeof getWx().createWorker === 'function'; |
| 100 | } |
| 101 | |
| 102 | isSharedArrayBufferSupported(): boolean { |
| 103 | // 微信小游戏不支持 SharedArrayBuffer |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | getHardwareConcurrency(): number { |
| 108 | // 微信小游戏无法获取真实核心数,返回保守值 |
| 109 | return 2; |
| 110 | } |
| 111 | |
| 112 | createWorker(script: string, options?: WorkerCreationOptions): PlatformWorker { |
| 113 | // 微信小游戏 Worker 需要指定文件路径,不支持内联脚本 |
| 114 | // script 参数应该是 worker 文件的路径 |
| 115 | const worker = getWx().createWorker(script, { |
| 116 | useExperimentalWorker: true |
| 117 | }); |
| 118 | |
| 119 | return new WeChatWorker(worker); |
| 120 | } |
nothing calls this directly
no outgoing calls
no test coverage detected