fix: cordova loading error.

This commit is contained in:
Rintim 2024-02-08 17:57:24 +08:00
parent 23153ec00a
commit 0200aaaa2e
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
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')
//但这种方式只允许修改game的文件读写函数。 script.src = 'cordova.js'
if (typeof window.initReadWriteFunction == 'function') { document.body.appendChild(script)
const g = {}; await new Promise((resolve) => {
const ReadWriteFunctionName = ['download', 'readFile', 'readFileAsText', 'writeFile', 'removeFile', 'getFileList', 'ensureDirectory', 'createDir']; document.addEventListener('deviceready', async () => {
ReadWriteFunctionName.forEach(prop => { const { cordovaReady } = await import('./cordova.js')
Object.defineProperty(g, prop, { await cordovaReady()
configurable: true, resolve(void 0)
get() { return undefined; }, })
set(newValue) { })
if (typeof newValue == 'function') {
delete g[prop];
g[prop] = game[prop] = newValue;
}
}
});
});
// @ts-ignore
await window.initReadWriteFunction(g).catch(e => {
console.error('文件读写函数初始化失败:', e);
});
} }
window.onbeforeunload = function () { else {
if (config.get('confirm_exit') && !_status.reloading) { //为其他自定义平台提供文件读写函数赋值的一种方式。
return '是否离开游戏?'; //但这种方式只允许修改game的文件读写函数。
if (typeof window.initReadWriteFunction == 'function') {
const g = {}
const ReadWriteFunctionName = ['download', 'readFile', 'readFileAsText', 'writeFile', 'removeFile', 'getFileList', 'ensureDirectory', 'createDir']
ReadWriteFunctionName.forEach(prop => {
Object.defineProperty(g, prop, {
configurable: true,
get() { return undefined },
set(newValue) {
if (typeof newValue == 'function') {
delete g[prop]
g[prop] = game[prop] = newValue
}
}
})
})
// @ts-ignore
await window.initReadWriteFunction(g).catch(e => {
console.error('文件读写函数初始化失败:', e)
})
} }
else { window.onbeforeunload = function () {
return null; if (config.get('confirm_exit') && !_status.reloading) {
return '是否离开游戏?'
}
else {
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() {
}); });
} }
/**
* `window.onload`触发时执行的函数
*
* 目前无任何内容预防以后出现需要的情况
*
* @deprecated
* @return {Promise<void>}
*/
async function onWindowReady() { async function onWindowReady() {
if (Reflect.has(lib, 'device')) {
var 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);
});
});
}
/*
if (_status.packLoaded) {
delete _status.packLoaded;
lib.init.onload();
}
else {
_status.windowLoaded = true;
}
*/
} }
function setBackground() { function setBackground() {