From 0200aaaa2ee0f7440b5b000c9e6c26858b879a6d Mon Sep 17 00:00:00 2001 From: Rintim Date: Thu, 8 Feb 2024 17:57:24 +0800 Subject: [PATCH] fix: cordova loading error. --- noname/init/cordova.js | 1 - noname/init/index.js | 105 +++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/noname/init/cordova.js b/noname/init/cordova.js index 291d70906..184aeea67 100644 --- a/noname/init/cordova.js +++ b/noname/init/cordova.js @@ -7,7 +7,6 @@ import { UI as ui } from '../ui/index.js'; import { nonameInitialized } from '../util/index.js'; export async function cordovaReady() { - lib.path = (await import('../library/path.js')).default; if (lib.device == 'android') { document.addEventListener("pause", function () { if (!_status.paused2 && (typeof _status.event.isMine == 'function' && !_status.event.isMine())) { diff --git a/noname/init/index.js b/noname/init/index.js index 2bbdbd8af..9ce7b1f8b 100644 --- a/noname/init/index.js +++ b/noname/init/index.js @@ -174,39 +174,52 @@ export async function boot() { const { nodeReady } = await import('./node.js'); nodeReady(); } - // 手机平台已在别处判断 - else if (!Reflect.has(lib, 'device')) { - Reflect.set(lib, 'path', (await import('../library/path.js')).default); - //为其他自定义平台提供文件读写函数赋值的一种方式。 - //但这种方式只允许修改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 { + 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) + }) + }) } - window.onbeforeunload = function () { - if (config.get('confirm_exit') && !_status.reloading) { - return '是否离开游戏?'; + else { + //为其他自定义平台提供文件读写函数赋值的一种方式。 + //但这种方式只允许修改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 { - return null; + window.onbeforeunload = function () { + if (config.get('confirm_exit') && !_status.reloading) { + return '是否离开游戏?' + } + else { + return null + } } - }; + } } const loadCssPromise = loadCss(); @@ -534,6 +547,8 @@ export async function boot() { document.addEventListener('touchmove', ui.click.windowtouchmove); } + await waitDomLoad; + const stylesLoaded = await Promise.all(stylesLoading); const stylesLength = Math.min(stylesName.length, stylesLoaded.length); for (let i = 0; i < stylesLength; ++i) { @@ -653,7 +668,6 @@ export async function boot() { delete _status.importing; } - await waitDomLoad; await onload(resetGameTimeout); } @@ -782,28 +796,15 @@ async function loadCss() { }); } +/** + * `window.onload`触发时执行的函数 + * + * 目前无任何内容,预防以后出现需要的情况 + * + * @deprecated + * @return {Promise} + */ 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() {