fixed #16562: Wasm support for ByteDance minigame platform (#16628)

This commit is contained in:
James Chen
2023-12-20 09:54:54 +08:00
committed by GitHub
parent 6c8d39e735
commit 4ce3e6e64e
13 changed files with 106 additions and 45 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ declare module 'pal/wasm' {
* @param wasmUrl the url of wasm, this should be a url relative from build output chunk.
* @param importObject the standard `WebAssembly.Imports` instance
*/
export function instantiateWasm (wasmUrl: string, importObject: WebAssembly.Imports): Promise<any>;
export function instantiateWasm (wasmUrl: string, importObject: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
/**
* Fetch binary data from wasm url or js mem url.
+1 -1
View File
@@ -653,7 +653,7 @@
"WASM_SUPPORT_MODE": {
"comment": "Whether support wasm, here we provide 3 options:\n0: The platform doesn't support WASM\n1: The platform supports WASM\n2: The platform may support WASM, especially on Web platform",
"type": "number",
"value": "$HTML5 ? 2 : ($NATIVE ? (($OPEN_HARMONY||$IOS)?0:1): ($MINIGAME? ($WECHAT?1:0) :0))",
"value": "($HTML5 || $BYTEDANCE) ? 2 : ($NATIVE ? (($OPEN_HARMONY||$IOS)?0:1): ($MINIGAME? ($WECHAT?1:0) :0))",
"internal": true
},
"FORCE_BANNING_BULLET_WASM": {
+2 -2
View File
@@ -143,8 +143,8 @@ function initWasm (wasmFactory, wasmUrl: string): Promise<void> {
receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void,
) {
// NOTE: the Promise return by instantiateWasm hook can't be caught.
instantiateWasm(wasmUrl, importObject).then((result: any) => {
receiveInstance(result.instance as WebAssembly.Instance, result.module as WebAssembly.Module);
instantiateWasm(wasmUrl, importObject).then((result) => {
receiveInstance(result.instance, result.module);
}).catch((err) => reject(errorMessage(err)));
},
}).then((Instance: any) => {
+2 -2
View File
@@ -100,8 +100,8 @@ function initWASM (wasmFactory, wasmUrl: string): Promise<void> {
receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void,
) {
// NOTE: the Promise return by instantiateWasm hook can't be caught.
instantiateWasm(wasmUrl, importObject).then((result: any) => {
receiveInstance(result.instance as WebAssembly.Instance, result.module as WebAssembly.Module);
instantiateWasm(wasmUrl, importObject).then((result) => {
receiveInstance(result.instance, result.module);
}).catch((err) => reject(errorMessage(err)));
},
}).then((instance: any) => {
+3 -3
View File
@@ -79,7 +79,7 @@ export function InitPhysXLibs (): Promise<void> {
}
}
function InitPhysXLibsInternal (physxWasmFactory, physxWasmUrl, physxAsmFactory): any {
function InitPhysXLibsInternal (physxWasmFactory, physxWasmUrl: string, physxAsmFactory): any {
if (shouldUseWasmModule()) {
return initWASM(physxWasmFactory, physxWasmUrl);
} else {
@@ -104,7 +104,7 @@ function initASM (physxAsmFactory): any {
}
}
function initWASM (physxWasmFactory, physxWasmUrl): any {
function initWASM (physxWasmFactory, physxWasmUrl: string): any {
globalThis.PhysX = globalThis.PHYSX ? globalThis.PHYSX : physxWasmFactory;
if (globalThis.PhysX != null) {
return globalThis.PhysX({
@@ -112,7 +112,7 @@ function initWASM (physxWasmFactory, physxWasmUrl): any {
importObject: WebAssembly.Imports,
receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void,
): any {
return instantiateWasm(physxWasmUrl, importObject).then((result: any): void => {
return instantiateWasm(physxWasmUrl, importObject).then((result): void => {
receiveInstance(result.instance, result.module);
});
},
+7 -5
View File
@@ -42,14 +42,16 @@ let wasmInstance: SpineWasm.instance = null!;
const registerList: any[] = [];
///////////////////////////////////////////////////////////////////////////////////////////////////
function initWasm (wasmFactory, wasmUrl): Promise<void> {
function initWasm (wasmFactory, wasmUrl: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
const errorMessage = (err: any): string => `[Spine]: Spine wasm load failed: ${err}`;
wasmFactory({
instantiateWasm (importObject: WebAssembly.Imports,
receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void) {
instantiateWasm (
importObject: WebAssembly.Imports,
receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void,
) {
// NOTE: the Promise return by instantiateWasm hook can't be caught.
instantiateWasm(wasmUrl, importObject).then((result: any) => {
instantiateWasm(wasmUrl, importObject).then((result) => {
receiveInstance(result.instance, result.module);
}).catch((err) => reject(errorMessage(err)));
},
@@ -62,7 +64,7 @@ function initWasm (wasmFactory, wasmUrl): Promise<void> {
});
}
function initAsmJS (asmFactory, asmJsMemUrl): Promise<void> {
function initAsmJS (asmFactory, asmJsMemUrl: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (CULL_ASM_JS_MODULE) {
reject(getError(4601));
+49 -1
View File
@@ -61,6 +61,37 @@ if (WECHAT) {
currentPlatform = Platform.QTT_MINI_GAME;
}
let isVersionGreaterOrEqualTo;
if (BYTEDANCE) {
isVersionGreaterOrEqualTo = function isVersionGreaterOrEqualTo (versionA: string, versionB: string): boolean {
// Split the version number string into an array of integers
function parseVersion (version: string): number[] {
return version.split('.').map((part: string) => parseInt(part, 10));
}
// Parse the version number strings into arrays
const versionArrayA = parseVersion(versionA);
const versionArrayB = parseVersion(versionB);
if (versionArrayA.length !== versionArrayB.length) {
return false;
}
// Compare versions level by level
for (let i = 0; i < versionArrayA.length; i++) {
if (versionArrayA[i] > versionArrayB[i]) {
// versionA > versionB
return true;
} else if (versionArrayA[i] < versionArrayB[i]) {
// versionA < versionB
return false;
}
}
// versionA === versionB
return true;
};
}
class SystemInfo extends EventTarget {
public readonly networkType: NetworkType;
public readonly isNative: boolean;
@@ -129,6 +160,23 @@ class SystemInfo extends EventTarget {
this.isXR = false;
const isPCWechat = WECHAT && this.os === OS.WINDOWS && !minigame.isDevTool;
const supportWasm = ((): boolean => {
if (WECHAT) {
return true;
}
if (BYTEDANCE) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error TTWebAssembly is defined if bytedance client supports wasm.
if (isVersionGreaterOrEqualTo(minigameSysInfo.SDKVersion, '3.7.0') && typeof TTWebAssembly === 'object') {
return true;
}
}
return false;
})();
this._featureMap = {
[Feature.WEBP]: false, // Initialize in Promise,
[Feature.IMAGE_BITMAP]: false,
@@ -146,7 +194,7 @@ class SystemInfo extends EventTarget {
[Feature.EVENT_HANDLE]: this.isXR,
[Feature.EVENT_HMD]: this.isXR,
[Feature.EVENT_HANDHELD]: false,
[Feature.WASM]: WECHAT,
[Feature.WASM]: supportWasm,
};
this._initPromise = [];
+9 -2
View File
@@ -28,21 +28,28 @@ import { basename } from '../../cocos/core/utils/path';
import { checkPalIntegrity, withImpl } from '../integrity-check';
import { log } from '../../cocos/core/platform/debug';
// NOTE: The global variable `CCWebAssembly` is assigned in platforms/(bytedance|wechat)/wrapper/builtin/index.js
declare namespace CCWebAssembly {
// The first argument of `instantiate` function in mini-game platforms is always a wasm url.
function instantiate(url: string, importObject?: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
}
export function instantiateWasm (wasmUrl: string, importObject: WebAssembly.Imports): Promise<any> {
return getPlatformBinaryUrl(wasmUrl).then((url) => WebAssembly.instantiate(url, importObject));
return getPlatformBinaryUrl(wasmUrl).then((url: string) => CCWebAssembly.instantiate(url, importObject));
}
export function fetchBuffer (binaryUrl: string): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
getPlatformBinaryUrl(binaryUrl).then((url) => {
// NOTE: fsUtils is defined in engine-adapter, we need to access globalThis explicitly for Taobao platform
globalThis.fsUtils.readArrayBuffer(url, (err, arrayBuffer) => {
globalThis.fsUtils.readArrayBuffer(url, (err, arrayBuffer: ArrayBuffer) => {
if (err) {
reject(err);
return;
}
resolve(arrayBuffer);
});
// eslint-disable-next-line @typescript-eslint/no-empty-function
}).catch((e) => {});
});
}
+1 -1
View File
@@ -42,7 +42,7 @@ export function fetchBuffer (binaryUrl: string): Promise<ArrayBuffer> {
// IDEA: it's better we implement another PAL for nodejs platform.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');
const arrayBuffer = fs.readFileSync(binaryUrl);
const arrayBuffer = fs.readFileSync(binaryUrl) as ArrayBuffer;
resolve(arrayBuffer);
});
return;
@@ -1,62 +1,65 @@
import * as _window from './window'
import document from './document'
import HTMLElement from './HTMLElement'
import * as _window from './window';
import document from './document';
import HTMLElement from './HTMLElement';
const global = GameGlobal
const global = GameGlobal;
function inject () {
_window.document = document;
_window.addEventListener = (type, listener) => {
_window.document.addEventListener(type, listener)
}
_window.document.addEventListener(type, listener);
};
_window.removeEventListener = (type, listener) => {
_window.document.removeEventListener(type, listener)
}
_window.document.removeEventListener(type, listener);
};
_window.dispatchEvent = function (event = {}) {
console.log('window.dispatchEvent' , event.type, event);
console.log('window.dispatchEvent', event.type, event);
// nothing to do
}
};
const { platform } = tt.getSystemInfoSync()
const { platform } = tt.getSystemInfoSync();
// 开发者工具无法重定义 window
if (typeof __devtoolssubcontext === 'undefined' && platform === 'devtools') {
for (const key in _window) {
const descriptor = Object.getOwnPropertyDescriptor(global, key)
const descriptor = Object.getOwnPropertyDescriptor(global, key);
if (!descriptor || descriptor.configurable === true) {
Object.defineProperty(window, key, {
value: _window[key]
})
value: _window[key],
});
}
}
for (const key in _window.document) {
const descriptor = Object.getOwnPropertyDescriptor(global.document, key)
const descriptor = Object.getOwnPropertyDescriptor(global.document, key);
if (!descriptor || descriptor.configurable === true) {
Object.defineProperty(global.document, key, {
value: _window.document[key]
})
value: _window.document[key],
});
}
}
window.parent = window
window.parent = window;
} else {
for (const key in _window) {
global[key] = _window[key]
global[key] = _window[key];
}
global.window = _window
window = global
window.top = window.parent = window
global.window = _window;
window = global;
window.top = window.parent = window;
GameGlobal.global = GameGlobal.global || GameGlobal;
}
}
if (!GameGlobal.__isAdapterInjected) {
GameGlobal.__isAdapterInjected = true
inject()
GameGlobal.__isAdapterInjected = true;
inject();
}
//NOTE: Don't override global `WebAssembly` variable since bytedance platform has a bug that will cause TTWebAssembly.instantiate fails.
// So we add our own `CCWebAssembly` variable here.
global.CCWebAssembly = global.TTWebAssembly;
require('../../../../common/xmldom/dom-parser');
require('../unify');
require('../unify');
@@ -58,7 +58,7 @@ if (!GameGlobal.__isAdapterInjected) {
}
// overwrite global WebAssembly
global.WebAssembly = global.WXWebAssembly;
global.CCWebAssembly = global.WebAssembly = global.WXWebAssembly;
require('../../../../common/xmldom/dom-parser');
require('../unify');
+1
View File
@@ -19,6 +19,7 @@ App({
DOMParser: undefined,
WebAssembly: undefined,
WXWebAssembly: undefined,
CCWebAssembly: undefined,
_touchStart: undefined,
_touchMove: undefined,
_touchEnd: undefined,
+1 -1
View File
@@ -1,7 +1,7 @@
var GameGlobal = getApp().GameGlobal;
exports.adapt = function (canvas, width, height) {
GameGlobal.canvas = canvas;
GameGlobal.WXWebAssembly = WXWebAssembly;
GameGlobal.CCWebAssembly = GameGlobal.WXWebAssembly = WXWebAssembly;
GameGlobal.requestAnimationFrame = GameGlobal.canvas.requestAnimationFrame || globalThis.setTimeout;
GameGlobal.cancelAnimationFrame = GameGlobal.canvas.cancelAnimationFrame || globalThis.setTimeout;
GameGlobal.setTimeout = globalThis.setTimeout;