Skip to content

Commit 4a6f83b

Browse files
authored
Revert "refactor platforms (#126)" (#128)
1 parent 04f063e commit 4a6f83b

27 files changed

Lines changed: 2100 additions & 16 deletions

File tree

cc.config.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,13 @@
577577
"value": false,
578578
"internal": false
579579
},
580+
"XIAOMI": {
581+
"comment": "Running in the xiaomi's quick game.",
582+
"type": "boolean",
583+
"value": false,
584+
"ccGlobal": true,
585+
"internal": false
586+
},
580587
"ALIPAY": {
581588
"comment": "Running in the alipay's mini game.",
582589
"type": "boolean",
@@ -624,6 +631,13 @@
624631
"ccGlobal": true,
625632
"internal": false
626633
},
634+
"MIGU": {
635+
"comment": "Running in the migu's quick game.",
636+
"type": "boolean",
637+
"value": false,
638+
"ccGlobal": true,
639+
"internal": false
640+
},
627641
"HONOR": {
628642
"comment": "Running in the honor's quick game.",
629643
"type": "boolean",
@@ -724,14 +738,14 @@
724738
"MINIGAME": {
725739
"comment": "Running in mini game.",
726740
"type": "boolean",
727-
"value": "$WECHAT || $WECHAT_MINI_PROGRAM || $ALIPAY || $TAOBAO || $TAOBAO_MINIGAME || $BYTEDANCE",
741+
"value": "$WECHAT || $WECHAT_MINI_PROGRAM || $XIAOMI || $ALIPAY || $TAOBAO || $TAOBAO_MINIGAME || $BYTEDANCE",
728742
"ccGlobal": true,
729743
"internal": false
730744
},
731745
"RUNTIME_BASED": {
732746
"comment": "Running in runtime based environment.",
733747
"type": "boolean",
734-
"value": "$OPPO || $VIVO || $HUAWEI || $HONOR || $COCOS_RUNTIME || $SUD || $SUDV2",
748+
"value": "$OPPO || $VIVO || $HUAWEI || $MIGU || $HONOR || $COCOS_RUNTIME || $SUD || $SUDV2",
735749
"ccGlobal": true,
736750
"internal": false
737751
},

cocos/asset/asset-manager/download-dom-image.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
THE SOFTWARE.
2424
*/
2525

26+
import { XIAOMI } from 'internal:constants';
2627
import { getError } from '../../core';
2728
import { ccwindow } from '../../core/global-exports';
2829

@@ -33,7 +34,8 @@ export default function downloadDomImage (
3334
): HTMLImageElement {
3435
const img = new ccwindow.Image();
3536

36-
if (ccwindow.location.protocol !== 'file:') {
37+
// NOTE: on xiaomi platform, we need to force setting img.crossOrigin as 'anonymous'
38+
if (ccwindow.location.protocol !== 'file:' || XIAOMI) {
3739
img.crossOrigin = 'anonymous';
3840
}
3941

cocos/asset/assets/image-asset.jsb.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
THE SOFTWARE.
2323
*/
2424

25-
import { ALIPAY, JSB, TEST, EDITOR, NODEJS } from 'internal:constants';
25+
import { ALIPAY, XIAOMI, JSB, TEST, EDITOR, NODEJS } from 'internal:constants';
2626
import { Format, FormatFeatureBit, deviceManager } from '../../gfx';
2727
import { PixelFormat } from './asset-enum';
2828
import { sys, macro, warnID, cclegacy } from '../../core';
@@ -54,7 +54,7 @@ function isImageBitmap (imageSource: any): boolean {
5454
}
5555

5656
function isNativeImage (imageSource: ImageSource): imageSource is (HTMLImageElement | HTMLCanvasElement | ImageBitmap) {
57-
if (ALIPAY) {
57+
if (ALIPAY || XIAOMI) {
5858
// We're unable to grab the constructors of Alipay native image or canvas object.
5959
return !('_data' in imageSource);
6060
}

cocos/asset/assets/image-asset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
// @ts-check
2626
import { ccclass, override } from 'cc.decorator';
27-
import { EDITOR, NODEJS, ALIPAY, JSB, TEST, TAOBAO, TAOBAO_MINIGAME, WECHAT_MINI_PROGRAM, BYTEDANCE } from 'internal:constants';
27+
import { EDITOR, NODEJS, ALIPAY, XIAOMI, JSB, TEST, TAOBAO, TAOBAO_MINIGAME, WECHAT_MINI_PROGRAM, BYTEDANCE } from 'internal:constants';
2828
import { Device, Format, FormatFeatureBit, deviceManager } from '../../gfx';
2929
import { Asset } from './asset';
3030
import { PixelFormat } from './asset-enum';
@@ -163,7 +163,7 @@ function fetchImageSource (imageSource: ImageSource): HTMLCanvasElement | HTMLIm
163163

164164
// 返回该图像源是否是平台提供的图像对象。
165165
function isNativeImage (imageSource: ImageSource): imageSource is (HTMLImageElement | HTMLCanvasElement | ImageBitmap) {
166-
if (ALIPAY || TAOBAO || TAOBAO_MINIGAME || WECHAT_MINI_PROGRAM || BYTEDANCE) {
166+
if (ALIPAY || TAOBAO || TAOBAO_MINIGAME || XIAOMI || WECHAT_MINI_PROGRAM || BYTEDANCE) {
167167
// We're unable to grab the constructors of Alipay native image or canvas object.
168168
return !('_data' in imageSource);
169169
}

cocos/core/platform/deprecated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ replaceProperty(
9797
sys,
9898
'sys',
9999
['UNKNOWN', 'EDITOR_PAGE', 'EDITOR_CORE', 'MOBILE_BROWSER', 'DESKTOP_BROWSER', 'WIN32', 'MACOS', 'IOS', 'ANDROID', 'OHOS',
100-
'WECHAT_GAME', 'ALIPAY_MINI_GAME', 'BYTEDANCE_MINI_GAME',
100+
'WECHAT_GAME', 'XIAOMI_QUICK_GAME', 'ALIPAY_MINI_GAME', 'BYTEDANCE_MINI_GAME',
101101
'OPPO_MINI_GAME', 'VIVO_MINI_GAME', 'HUAWEI_QUICK_GAME'].map((item) => ({
102102
name: item,
103103
target: sys.Platform,

pal/env/minigame/env.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/* eslint-disable import/no-dynamic-require */
26-
import { TAOBAO, TAOBAO_MINIGAME, WECHAT, WECHAT_MINI_PROGRAM } from 'internal:constants';
26+
import { TAOBAO, TAOBAO_MINIGAME, WECHAT, WECHAT_MINI_PROGRAM, XIAOMI } from 'internal:constants';
2727
import { checkPalIntegrity, withImpl } from '../../integrity-check';
2828

2929
declare const require: (path: string) => any;
@@ -37,6 +37,9 @@ export function findCanvas (): { frame: HTMLDivElement, container: HTMLDivElemen
3737
}
3838

3939
export function loadJsFile (path: string): any {
40+
if (XIAOMI) {
41+
return require(`../../${path}`);
42+
}
4043
if (WECHAT || WECHAT_MINI_PROGRAM) {
4144
return __wxRequire(path);
4245
}

pal/minigame/xiaomi.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.
3+
4+
https://www.cocos.com/
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10+
of the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
*/
24+
25+
import { IMiniGame } from 'pal/minigame';
26+
import { checkPalIntegrity, withImpl } from '../integrity-check';
27+
import { Orientation } from '../screen-adapter/enum-type';
28+
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';
29+
import { warn } from '../../cocos/core/platform/debug';
30+
31+
declare let qg: any;
32+
33+
const minigame: IMiniGame = {} as IMiniGame;
34+
cloneObject(minigame, qg);
35+
36+
// #region SystemInfo
37+
const systemInfo = minigame.getSystemInfoSync();
38+
minigame.isDevTool = false;
39+
40+
minigame.isLandscape = systemInfo.screenWidth > systemInfo.screenHeight;
41+
// init landscapeOrientation as LANDSCAPE_RIGHT
42+
const landscapeOrientation = Orientation.LANDSCAPE_RIGHT;
43+
// NOTE: onDeviceOrientationChange is not supported on this platform
44+
// qg.onDeviceOrientationChange((res) => {
45+
// if (res.value === 'landscape') {
46+
// landscapeOrientation = Orientation.LANDSCAPE_RIGHT;
47+
// } else if (res.value === 'landscapeReverse') {
48+
// landscapeOrientation = Orientation.LANDSCAPE_LEFT;
49+
// }
50+
// });
51+
Object.defineProperty(minigame, 'orientation', {
52+
get () {
53+
return minigame.isLandscape ? landscapeOrientation : Orientation.PORTRAIT;
54+
},
55+
});
56+
// #endregion SystemInfo
57+
58+
// #region TouchEvent
59+
minigame.onTouchStart = (cb): void => {
60+
window.canvas.ontouchstart = cb;
61+
};
62+
minigame.onTouchMove = (cb): void => {
63+
window.canvas.ontouchmove = cb;
64+
};
65+
minigame.onTouchEnd = (cb): void => {
66+
window.canvas.ontouchend = cb;
67+
};
68+
minigame.onTouchCancel = (cb): void => {
69+
window.canvas.ontouchcancel = cb;
70+
};
71+
// #endregion TouchEvent
72+
73+
// // Keyboard
74+
// globalAdapter.showKeyboard = function (res) {
75+
// res.confirmHold = true; // HACK: confirmHold not working on Xiaomi platform
76+
// qg.showKeyboard(res);
77+
// };
78+
79+
// #region Accelerometer
80+
let _customAccelerometerCb: AccelerometerChangeCallback | undefined;
81+
let _innerAccelerometerCb: AccelerometerChangeCallback | undefined;
82+
minigame.onAccelerometerChange = (cb: AccelerometerChangeCallback): void => {
83+
// qg.offAccelerometerChange() is not supported.
84+
// so we can only register AccelerometerChange callback, but can't unregister.
85+
if (!_innerAccelerometerCb) {
86+
_innerAccelerometerCb = (res: any): void => {
87+
let x = res.x;
88+
let y = res.y;
89+
if (minigame.isLandscape) {
90+
const orientationFactor = (landscapeOrientation === Orientation.LANDSCAPE_RIGHT ? 1 : -1);
91+
const tmp = x;
92+
x = -y * orientationFactor;
93+
y = tmp * orientationFactor;
94+
}
95+
96+
const standardFactor = -0.1;
97+
x *= standardFactor;
98+
y *= standardFactor;
99+
const resClone = {
100+
x,
101+
y,
102+
z: res.z,
103+
};
104+
_customAccelerometerCb?.(resClone);
105+
};
106+
qg.onAccelerometerChange(_innerAccelerometerCb);
107+
}
108+
_customAccelerometerCb = cb;
109+
};
110+
minigame.offAccelerometerChange = (cb?: AccelerometerChangeCallback): void => {
111+
// qg.offAccelerometerChange() is not supported.
112+
_customAccelerometerCb = undefined;
113+
};
114+
// #endregion Accelerometer
115+
116+
// #region InnerAudioContext
117+
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(qg, {
118+
onPlay: true,
119+
onPause: true,
120+
onStop: true,
121+
onSeek: false,
122+
});
123+
const originalCreateInnerAudioContext = minigame.createInnerAudioContext;
124+
minigame.createInnerAudioContext = (): InnerAudioContext => {
125+
const audioContext = originalCreateInnerAudioContext.call(minigame);
126+
const originalStop = audioContext.stop;
127+
Object.defineProperty(audioContext, 'stop', {
128+
configurable: true,
129+
value (): void {
130+
// NOTE: stop won't seek to 0 when audio is paused on Xiaomi platform.
131+
audioContext.seek(0);
132+
originalStop.call(audioContext);
133+
},
134+
});
135+
return audioContext;
136+
};
137+
// #endregion InnerAudioContext
138+
139+
// #region SafeArea
140+
minigame.getSafeArea = (): SafeArea => {
141+
warn('getSafeArea is not supported on this platform');
142+
const systemInfo = minigame.getSystemInfoSync();
143+
return {
144+
top: 0,
145+
left: 0,
146+
bottom: systemInfo.screenHeight,
147+
right: systemInfo.screenWidth,
148+
width: systemInfo.screenWidth,
149+
height: systemInfo.screenHeight,
150+
};
151+
};
152+
// #endregion SafeArea
153+
154+
export { minigame };
155+
156+
checkPalIntegrity<typeof import('pal/minigame')>(withImpl<typeof import('./xiaomi')>());

pal/system-info/enum-type/platform.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export enum Platform {
4040
// Minigame
4141
WECHAT_GAME = 'WECHAT_GAME',
4242
WECHAT_MINI_PROGRAM = 'WECHAT_MINI_PROGRAM',
43+
XIAOMI_QUICK_GAME = 'XIAOMI_QUICK_GAME',
4344
ALIPAY_MINI_GAME = 'ALIPAY_MINI_GAME',
4445
TAOBAO_CREATIVE_APP = 'TAOBAO_CREATIVE_APP',
4546
TAOBAO_MINI_GAME = 'TAOBAO_MINI_GAME',
@@ -48,6 +49,7 @@ export enum Platform {
4849
OPPO_MINI_GAME = 'OPPO_MINI_GAME',
4950
VIVO_MINI_GAME = 'VIVO_MINI_GAME',
5051
HUAWEI_QUICK_GAME = 'HUAWEI_QUICK_GAME',
52+
MIGU_MINI_GAME = 'MIGU_MINI_GAME',
5153
HONOR_MINI_GAME = 'HONOR_MINI_GAME',
5254
SUD_MINI_GAME = 'SUD_MINI_GAME',
5355
SUDV2_MINI_GAME = 'SUDV2_MINI_GAME',

pal/system-info/minigame/system-info.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
THE SOFTWARE.
2424
*/
2525

26-
import { ALIPAY, BYTEDANCE, HUAWEI, OPPO, RUNTIME_BASED, SUD, SUDV2, VIVO, HONOR, WECHAT, DEBUG, TEST, TAOBAO, TAOBAO_MINIGAME, WECHAT_MINI_PROGRAM } from 'internal:constants';
26+
import { ALIPAY, BYTEDANCE, HUAWEI, OPPO, RUNTIME_BASED, SUD, SUDV2, VIVO, MIGU, HONOR, WECHAT, XIAOMI, DEBUG, TEST, TAOBAO, TAOBAO_MINIGAME, WECHAT_MINI_PROGRAM } from 'internal:constants';
2727
import { minigame, SystemInfo as MinigameSystemInfo } from 'pal/minigame';
2828
import { IFeatureMap } from 'pal/system-info';
2929
import { EventTarget } from '../../../cocos/core/event';
@@ -38,6 +38,8 @@ if (WECHAT) {
3838
currentPlatform = Platform.WECHAT_GAME;
3939
} else if (WECHAT_MINI_PROGRAM) {
4040
currentPlatform = Platform.WECHAT_MINI_PROGRAM;
41+
} else if (XIAOMI) {
42+
currentPlatform = Platform.XIAOMI_QUICK_GAME;
4143
} else if (ALIPAY) {
4244
currentPlatform = Platform.ALIPAY_MINI_GAME;
4345
} else if (TAOBAO) {
@@ -52,6 +54,8 @@ if (WECHAT) {
5254
currentPlatform = Platform.VIVO_MINI_GAME;
5355
} else if (HUAWEI) {
5456
currentPlatform = Platform.HUAWEI_QUICK_GAME;
57+
} else if (MIGU) {
58+
currentPlatform = Platform.MIGU_MINI_GAME;
5559
} else if (HONOR) {
5660
currentPlatform = Platform.HONOR_MINI_GAME;
5761
} else if (SUD) {
@@ -197,7 +201,7 @@ class SystemInfo extends EventTarget {
197201

198202
// init isMobile and platform
199203
this.platform = currentPlatform;
200-
// Some minigame platforms don't support getting the platform, such as runtime, so this.os returns UNKNOWN.
204+
// Some minigame platforms don't support getting the platform, such as runtime and Xiaomi, so this.os returns UNKNOWN.
201205
// Most platforms are mobile, so set UNKNOWN to mobile.
202206
this.isMobile = this.os === OS.ANDROID || this.os === OS.IOS || this.os === OS.UNKNOWN;
203207

pal/wasm/wasm-minigame.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
THE SOFTWARE.
2323
*/
2424

25-
import { HUAWEI, TAOBAO_MINIGAME, WASM_SUBPACKAGE } from 'internal:constants';
25+
import { HUAWEI, TAOBAO_MINIGAME, WASM_SUBPACKAGE, XIAOMI } from 'internal:constants';
2626
import { minigame } from 'pal/minigame';
2727
import { basename } from '../../cocos/core/utils/path';
2828
import { checkPalIntegrity, withImpl } from '../integrity-check';
@@ -116,7 +116,9 @@ export function ensureWasmModuleReady (): Promise<void> {
116116
*/
117117
function getPlatformBinaryUrl (binaryUrl: string): Promise<string> {
118118
return new Promise((resolve) => {
119-
if (TAOBAO_MINIGAME && WASM_SUBPACKAGE) {
119+
if (XIAOMI) {
120+
resolve(`src/cocos-js/${binaryUrl}`);
121+
} if (TAOBAO_MINIGAME && WASM_SUBPACKAGE) {
120122
resolve(`__ccWasmAssetSubpkg__/${basename(binaryUrl)}`);
121123
} else {
122124
resolve(`cocos-js/${binaryUrl}`);

0 commit comments

Comments
 (0)