[adapters] 增加小游戏适配部分源码

This commit is contained in:
SmallMain
2024-10-16 17:12:08 +08:00
parent 887d4a96c9
commit 07bf3b7a96
345 changed files with 38447 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
const downloader = cc.assetManager.downloader;
const originalDownloadTTF = downloader._downloaders['.ttf'];
function downloadTTF (url, options, onComplete) {
// can't use cached ttf on Xiaomi platform
options.cacheEnabled = false;
originalDownloadTTF(url, options, onComplete);
}
downloader.register({
'.ttf': downloadTTF,
});

View File

@@ -0,0 +1 @@
require('./download-ttf');

View File

@@ -0,0 +1,267 @@
/****************************************************************************
Copyright (c) 2017-2019 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var fs = qg.getFileSystemManager ? qg.getFileSystemManager() : null;
var outOfStorageRegExp = /the maximum size of the file storage/; // not exactly right
var fsUtils = {
fs,
isOutOfStorage (errMsg) {
return outOfStorageRegExp.test(errMsg);
},
getUserDataPath () {
return qg.env.USER_DATA_PATH;
},
checkFsValid () {
if (!fs) {
console.warn('can not get the file system!');
return false;
}
return true;
},
deleteFile (filePath, onComplete) {
fs.unlink({
filePath: filePath,
success: function () {
onComplete && onComplete(null);
},
fail: function (res) {
console.warn(`Delete file failed: path: ${filePath} message: ${res.errMsg}`);
onComplete && onComplete(new Error(res.errMsg));
}
});
},
downloadFile (remoteUrl, filePath, header, onProgress, onComplete) {
var options = {
url: remoteUrl,
success: function (res) {
if (res.statusCode === 200) {
onComplete && onComplete(null, res.tempFilePath || res.filePath);
}
else {
if (res.filePath) {
fsUtils.deleteFile(res.filePath);
}
console.warn(`Download file failed: path: ${remoteUrl} message: ${res.statusCode}`);
onComplete && onComplete(new Error(res.statusCode), null);
}
},
fail: function (res) {
console.warn(`Download file failed: path: ${remoteUrl} message: ${res.errMsg}`);
onComplete && onComplete(new Error(res.errMsg), null);
}
}
if (filePath) options.filePath = filePath;
if (header) options.header = header;
var task = qg.downloadFile(options);
onProgress && task.onProgressUpdate(onProgress);
},
saveFile (srcPath, destPath, onComplete) {
qg.saveFile({
tempFilePath: srcPath,
filePath: destPath,
success: function (res) {
onComplete && onComplete(null);
},
fail: function (res) {
console.warn(`Save file failed: path: ${srcPath} message: ${res.errMsg}`);
onComplete && onComplete(new Error(res.errMsg));
}
});
},
copyFile (srcPath, destPath, onComplete) {
fs.copyFile({
srcPath: srcPath,
destPath: destPath,
success: function () {
onComplete && onComplete(null);
},
fail: function (res) {
console.warn(`Copy file failed: path: ${srcPath} message: ${ res.errMsg}`);
onComplete && onComplete(new Error(res.errMsg));
}
});
},
writeFile (path, data, encoding, onComplete) {
fs.writeFile({
filePath: path,
encoding: encoding,
data: data,
success: function () {
onComplete && onComplete(null);
},
fail: function (res) {
console.warn(`Write file failed: path: ${path} message: ${res.errMsg}`);
onComplete && onComplete(new Error(res.errMsg));
}
});
},
writeFileSync (path, data, encoding) {
try {
fs.writeFileSync(path, data, encoding);
return null;
}
catch (e) {
console.warn(`Write file failed: path: ${path} message: ${e.message}`);
return new Error(e.message);
}
},
readFile (filePath, encoding, onComplete) {
fs.readFile({
filePath: filePath,
encoding: encoding,
success: function (res) {
onComplete && onComplete(null, res.data);
},
fail: function (res) {
console.warn(`Read file failed: path: ${filePath} message: ${res.errMsg}`);
onComplete && onComplete (new Error(res.errMsg), null);
}
});
},
readDir (filePath, onComplete) {
fs.readdir({
dirPath: filePath,
success: function (res) {
onComplete && onComplete(null, res.files);
},
fail: function (res) {
console.warn(`Read directory failed: path ${filePath} message: ${res.errMsg}`);
onComplete && onComplete(new Error(res.errMsg), null);
}
});
},
readText (filePath, onComplete) {
fsUtils.readFile(filePath, 'utf8', onComplete);
},
readArrayBuffer (filePath, onComplete) {
fsUtils.readFile(filePath, '', onComplete);
},
readJson (filePath, onComplete) {
fsUtils.readFile(filePath, 'utf8', function (err, text) {
var out = null;
if (!err) {
try {
out = JSON.parse(text);
}
catch (e) {
console.warn(`Read json failed: path: ${filePath} message: ${e.message}`);
err = new Error(e.message);
}
}
onComplete && onComplete(err, out);
});
},
readJsonSync (path) {
try {
var str = fs.readFileSync(path, 'utf8');
return JSON.parse(str);
}
catch (e) {
console.warn(`Read json failed: path: ${path} message: ${e.message}`);
return new Error(e.message);
}
},
makeDirSync (path, recursive) {
try {
fs.mkdirSync(path, recursive);
return null;
}
catch (e) {
console.warn(`Make directory failed: path: ${path} message: ${e.message}`);
return new Error(e.message);
}
},
rmdirSync (dirPath, recursive) {
try {
fs.rmdirSync(dirPath, recursive);
}
catch (e) {
console.warn(`rm directory failed: path: ${dirPath} message: ${e.message}`);
return new Error(e.message);
}
},
exists (filePath, onComplete) {
fs.access({
path: filePath,
success: function () {
onComplete && onComplete(true);
},
fail: function () {
onComplete && onComplete(false);
}
});
},
loadSubpackage (name, onProgress, onComplete) {
name = 'usr_' + name; // prevention of name conflicts with platform
var task = qg.loadSubpackage({
name: name,
success: function () {
onComplete && onComplete();
},
fail: function (res) {
console.warn(`Load Subpackage failed: name: ${name} message: ${res.errMsg}`);
onComplete && onComplete(new Error(`Failed to load subpackage ${name}: ${res.errMsg}`));
}
});
onProgress && task.onProgressUpdate(onProgress);
return task;
},
unzip (zipFilePath, targetPath, onComplete) {
fs.unzip({
zipFilePath,
targetPath,
success () {
onComplete && onComplete(null);
},
fail (errMsg) {
console.warn(`unzip failed: path: ${zipFilePath} message: ${errMsg}`);
onComplete && onComplete(new Error('unzip failed: ' + errMsg));
},
})
},
};
window.fsUtils = module.exports = fsUtils;

View File

@@ -0,0 +1,10 @@
const adapter = window.__globalAdapter;
let adaptSysFunc = adapter.adaptSys;
Object.assign(adapter, {
// Extend adaptSys interface
adaptSys (sys) {
adaptSysFunc.call(this, sys);
sys.platform = sys.XIAOMI_GAME;
},
});

View File

@@ -0,0 +1,119 @@
const utils = require('../../../common/utils');
if (window.__globalAdapter) {
let globalAdapter = window.__globalAdapter;
// SystemInfo
globalAdapter.isSubContext = false; // sub context not supported
globalAdapter.isDevTool = false;
utils.cloneMethod(globalAdapter, qg, 'getSystemInfoSync');
// TouchEvent
globalAdapter.onTouchStart = function (cb) {
window.canvas.ontouchstart = cb;
};
globalAdapter.onTouchMove = function (cb) {
window.canvas.ontouchmove = cb;
};
globalAdapter.onTouchEnd = function (cb) {
window.canvas.ontouchend = cb;
};
globalAdapter.onTouchCancel = function (cb) {
window.canvas.ontouchcancel = cb;
};
// Audio
utils.cloneMethod(globalAdapter, qg, 'createInnerAudioContext');
// FrameRate
utils.cloneMethod(globalAdapter, qg, 'setPreferredFramesPerSecond');
// Keyboard
globalAdapter.showKeyboard = function (res) {
res.confirmHold = true; // HACK: confirmHold not working on Xiaomi platform
qg.showKeyboard(res);
};
utils.cloneMethod(globalAdapter, qg, 'hideKeyboard');
utils.cloneMethod(globalAdapter, qg, 'updateKeyboard');
utils.cloneMethod(globalAdapter, qg, 'onKeyboardInput');
utils.cloneMethod(globalAdapter, qg, 'onKeyboardConfirm');
utils.cloneMethod(globalAdapter, qg, 'onKeyboardComplete');
utils.cloneMethod(globalAdapter, qg, 'offKeyboardInput');
utils.cloneMethod(globalAdapter, qg, 'offKeyboardConfirm');
utils.cloneMethod(globalAdapter, qg, 'offKeyboardComplete');
// Message
utils.cloneMethod(globalAdapter, qg, 'getOpenDataContext');
utils.cloneMethod(globalAdapter, qg, 'onMessage');
// SharedCanvas
utils.cloneMethod(globalAdapter, qg, 'getSharedCanvas');
// Font
utils.cloneMethod(globalAdapter, qg, 'loadFont');
// hide show Event
utils.cloneMethod(globalAdapter, qg, 'onShow');
utils.cloneMethod(globalAdapter, qg, 'onHide');
// Accelerometer
// NOTE: There is no LANDSCAPE_LEFT on Xiaomi platform
// const LANDSCAPE_LEFT = -90;
const LANDSCAPE_RIGHT = 90;
// NOTE: the data in callback registered on onAccelerometerChange is 10 times than the standard data
// Need to be scaled by 0.1
const accelerometerFactor = -0.1;
let isAccelerometerInit = false;
let deviceOrientation = 1;
let isLandscape = window.orientation === LANDSCAPE_RIGHT;
if (qg.onDeviceOrientationChange) {
qg.onDeviceOrientationChange(function (res) {
if (res.value === 'landscape') {
deviceOrientation = 1;
}
else if (res.value === 'landscapeReverse') {
deviceOrientation = -1;
}
});
}
Object.assign(globalAdapter, {
startAccelerometer (cb) {
if (!isAccelerometerInit) {
isAccelerometerInit = true;
qg.onAccelerometerChange && qg.onAccelerometerChange(function (res) {
let resClone = {};
let x = res.x;
let y = res.y;
if (isLandscape) {
let tmp = x;
x = -y;
y = tmp;
}
resClone.x = x * accelerometerFactor;
resClone.y = y * accelerometerFactor;
resClone.z = res.z;
cb && cb(resClone);
});
}
else {
qg.startAccelerometer && qg.startAccelerometer({
fail (err) {
console.error('start accelerometer failed', err);
},
// success () {},
// complete () {},
});
}
},
stopAccelerometer () {
qg.stopAccelerometer && qg.stopAccelerometer({
fail (err) {
console.error('stop accelerometer failed', err);
},
// success () {},
// complete () {},
});
},
});
}