Merge pull request #933 from nofficalfs/Dev-Fix-CordovaImport

W.I.P | Fix a group of bugs of onload before cordova.
This commit is contained in:
Spmario233 2024-02-09 14:16:31 +08:00 committed by GitHub
commit c91206a97e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 53 deletions

View File

@ -7,7 +7,6 @@ import { UI as ui } from '../ui/index.js';
import { nonameInitialized } from '../util/index.js'; import { nonameInitialized } from '../util/index.js';
export async function cordovaReady() { export async function cordovaReady() {
lib.path = (await import('../library/path.js')).default;
if (lib.device == 'android') { if (lib.device == 'android') {
document.addEventListener("pause", function () { document.addEventListener("pause", function () {
if (!_status.paused2 && (typeof _status.event.isMine == 'function' && !_status.event.isMine())) { if (!_status.paused2 && (typeof _status.event.isMine == 'function' && !_status.event.isMine())) {

View File

@ -174,39 +174,52 @@ export async function boot() {
const { nodeReady } = await import('./node.js'); const { nodeReady } = await import('./node.js');
nodeReady(); nodeReady();
} }
// 手机平台已在别处判断 else {
else if (!Reflect.has(lib, 'device')) { Reflect.set(lib, 'path', (await import('../library/path.js')).default)
Reflect.set(lib, 'path', (await import('../library/path.js')).default); if (Reflect.has(lib, 'device')) {
const script = document.createElement('script')
script.src = 'cordova.js'
document.body.appendChild(script)
await new Promise((resolve) => {
document.addEventListener('deviceready', async () => {
const { cordovaReady } = await import('./cordova.js')
await cordovaReady()
resolve(void 0)
})
})
}
else {
//为其他自定义平台提供文件读写函数赋值的一种方式。 //为其他自定义平台提供文件读写函数赋值的一种方式。
//但这种方式只允许修改game的文件读写函数。 //但这种方式只允许修改game的文件读写函数。
if (typeof window.initReadWriteFunction == 'function') { if (typeof window.initReadWriteFunction == 'function') {
const g = {}; const g = {}
const ReadWriteFunctionName = ['download', 'readFile', 'readFileAsText', 'writeFile', 'removeFile', 'getFileList', 'ensureDirectory', 'createDir']; const ReadWriteFunctionName = ['download', 'readFile', 'readFileAsText', 'writeFile', 'removeFile', 'getFileList', 'ensureDirectory', 'createDir']
ReadWriteFunctionName.forEach(prop => { ReadWriteFunctionName.forEach(prop => {
Object.defineProperty(g, prop, { Object.defineProperty(g, prop, {
configurable: true, configurable: true,
get() { return undefined; }, get() { return undefined },
set(newValue) { set(newValue) {
if (typeof newValue == 'function') { if (typeof newValue == 'function') {
delete g[prop]; delete g[prop]
g[prop] = game[prop] = newValue; g[prop] = game[prop] = newValue
} }
} }
}); })
}); })
// @ts-ignore // @ts-ignore
await window.initReadWriteFunction(g).catch(e => { await window.initReadWriteFunction(g).catch(e => {
console.error('文件读写函数初始化失败:', e); console.error('文件读写函数初始化失败:', e)
}); })
} }
window.onbeforeunload = function () { window.onbeforeunload = function () {
if (config.get('confirm_exit') && !_status.reloading) { if (config.get('confirm_exit') && !_status.reloading) {
return '是否离开游戏?'; return '是否离开游戏?'
} }
else { else {
return null; return null
}
}
} }
};
} }
const loadCssPromise = loadCss(); const loadCssPromise = loadCss();
@ -534,6 +547,8 @@ export async function boot() {
document.addEventListener('touchmove', ui.click.windowtouchmove); document.addEventListener('touchmove', ui.click.windowtouchmove);
} }
await waitDomLoad;
const stylesLoaded = await Promise.all(stylesLoading); const stylesLoaded = await Promise.all(stylesLoading);
const stylesLength = Math.min(stylesName.length, stylesLoaded.length); const stylesLength = Math.min(stylesName.length, stylesLoaded.length);
for (let i = 0; i < stylesLength; ++i) { for (let i = 0; i < stylesLength; ++i) {
@ -653,7 +668,6 @@ export async function boot() {
delete _status.importing; delete _status.importing;
} }
await waitDomLoad;
await onload(resetGameTimeout); await onload(resetGameTimeout);
} }
@ -782,28 +796,15 @@ async function loadCss() {
}); });
} }
async function onWindowReady() { /**
if (Reflect.has(lib, 'device')) { * `window.onload`触发时执行的函数
var script = document.createElement('script'); *
script.src = 'cordova.js'; * 目前无任何内容预防以后出现需要的情况
document.body.appendChild(script); *
await new Promise((resolve) => { * @deprecated
document.addEventListener('deviceready', async () => { * @return {Promise<void>}
const { cordovaReady } = await import('./cordova.js');
await cordovaReady();
resolve(void 0);
});
});
}
/*
if (_status.packLoaded) {
delete _status.packLoaded;
lib.init.onload();
}
else {
_status.windowLoaded = true;
}
*/ */
async function onWindowReady() {
} }
function setBackground() { function setBackground() {