From 119f28cade4e9596d49be496c0bf6e6fcdb29c49 Mon Sep 17 00:00:00 2001 From: shijian <2954700422@qq.com> Date: Mon, 4 Dec 2023 09:04:13 +0800 Subject: [PATCH 1/4] =?UTF-8?q?game#prompt=E5=92=8Cgame#alert=E8=BF=9B?= =?UTF-8?q?=E8=A1=8Cpromise=E5=8C=96=E3=80=82=E6=96=87=E4=BB=B6=E8=AF=BB?= =?UTF-8?q?=E5=86=99=E5=87=BD=E6=95=B0=E4=B8=8D=E9=9C=80=E8=A6=81=E8=80=83?= =?UTF-8?q?=E8=99=91=E5=B9=B3=E5=8F=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 167 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 98 insertions(+), 69 deletions(-) diff --git a/game/game.js b/game/game.js index 65f37e630..e87d54ecb 100644 --- a/game/game.js +++ b/game/game.js @@ -7838,6 +7838,7 @@ new Promise(resolve=>{ /** * @type {import('path')} */ + // @ts-ignore path:{}, getErrorTip:msg=>{ if(typeof msg!='string'){ @@ -9915,62 +9916,6 @@ new Promise(resolve=>{ ui.css.default=lib.init.css(lib.assetURL+'layout/default','layout'); proceed2(); })}; - - const initGamePromises=function(){ - game.promises.download=function(url,folder,dev,onprogress){ - return new Promise((resolve,reject)=>{ - game.download(url,folder,resolve,reject,dev,onprogress); - }); - }; - - game.promises.readFile=function(filename){ - return new Promise((resolve,reject)=>{ - game.readFile(filename,resolve,reject); - }); - }; - - game.promises.readFileAsText=function(filename){ - return new Promise((resolve,reject)=>{ - game.readFileAsText(filename,resolve,reject); - }); - }; - - game.promises.writeFile=function(data,path,name){ - return (new Promise((resolve,reject)=>{ - game.writeFile(data,path,name,resolve); - })).then(result=>{ - return new Promise((resolve,reject)=>{ - if(result instanceof Error){ - reject(result); - }else{ - resolve(result); - } - }); - }); - }; - - game.promises.removeFile=function(dir){ - return (new Promise((resolve,reject)=>{ - game.removeFile(dir,resolve); - })).then(result=>{ - return new Promise((resolve,reject)=>{ - if(result instanceof Error){ - reject(result); - }else{ - resolve(result); - } - }); - }); - }; - - game.promises.getFileList=function(dir){ - return new Promise((resolve,reject)=>{ - game.getFileList(dir,resolve,reject); - }); - }; - - game.promises.ensureDirectory=game.ensureDirectory; - }; if(lib.device){ lib.init.cordovaReady=function(){ @@ -10015,8 +9960,8 @@ new Promise(resolve=>{ if ("cordova" in window && "plugins" in window.cordova && "permissions" in window.cordova.plugins) { const permissions = cordova.plugins.permissions; const requests = ["WRITE_EXTERNAL_STORAGE", "READ_EXTERNAL_STORAGE"] - requests.forEach((request) => { - permissions.checkPermission(permissions[request], (status) => { + requests.forEach(request => { + permissions.checkPermission(permissions[request], status => { if (!status.hasPermission) { permissions.requestPermission(permissions[request], lib.other.ignore, lib.other.ignore); } @@ -10150,7 +10095,6 @@ new Promise(resolve=>{ createDirectory(); },reject)); }; - initGamePromises(); if(ui.updateUpdate){ ui.updateUpdate(); } @@ -10335,7 +10279,6 @@ new Promise(resolve=>{ createDirectory(); }); }; - initGamePromises(); if(ui.updateUpdate){ ui.updateUpdate(); } @@ -36105,10 +36048,95 @@ new Promise(resolve=>{ } }; const game={ - /** - * @type { { [key: string]: (...args:[])=>Promise } } - */ - promises:{}, + promises:{ + /** + * 模仿h5的prompt,用于显示可提示用户进行输入的对话框 + * + * 注: 由于参数列表是随意的,在这里我准备限制一下这个函数的参数顺序 + * + * @type {{ + * (title: string, forced?: boolean): Promise; + * (alertOption: 'alert', title: string, forced?: boolean): Promise; + * }} + * + * @param { string } title 设置prompt标题与input内容 + * @param { boolean } [forced] 为true的话将没有"取消按钮" + * @param { string } alertOption 设置prompt是否模拟alert + * @example + * ```js + * // 只设置标题(但是input的初始值就变成了undefined) + * game.promises.prompt('###prompt标题').then(value => console.log(value)); + * // 设置标题和input初始内容 + * game.promises.prompt('###prompt标题###input初始内容').then(value => console.log(value)); + * ``` + * @returns { Promise } + */ + prompt(alertOption,title,forced){ + return new Promise((resolve,reject)=>{ + if(alertOption!='alert'){ + forced=title||false; + title=option; + game.prompt(title,forced,resolve); + }else{ + game.prompt(alertOption,title,forced,resolve); + } + }); + }, + /** + * 模仿h5的alert,用于显示信息的对话框 + * + * @param { string } title + * @example + * ```js + * await game.promises.alert('弹窗内容'); + * ``` + * @returns { Promise } + */ + alert(title){ + return new Promise((resolve,reject)=>{ + game.prompt('alert',title,resolve); + }); + }, + // 读写函数promises化(不用考虑其对应函数是否存在) + download(url,folder,dev,onprogress){ + return new Promise((resolve,reject)=>{ + game.download(url,folder,resolve,reject,dev,onprogress); + }); + }, + readFile(filename){ + return new Promise((resolve,reject)=>{ + game.readFile(filename,resolve,reject); + }); + }, + readFileAsText(filename){ + return new Promise((resolve,reject)=>{ + game.readFileAsText(filename,resolve,reject); + }); + }, + writeFile(data,path,name){ + return (new Promise((resolve,reject)=>{ + game.writeFile(data,path,name,resolve); + })).then(result=>{ + return new Promise((resolve,reject)=>{ + if(result instanceof Error){ + reject(result); + }else{ + resolve(result); + } + }); + }); + }, + ensureDirectory(list,callback,file){ + return new Promise((resolve,reject)=>{ + game.ensureDirectory(list,callback,file).then(resolve).catch(reject); + }); + }, + createDir(directory) { + return new Promise((resolve,reject)=>{ + game.createDir(directory,resolve,reject); + }); + }, + }, globalEventHandlers: new class { constructor() { this._handlers = {}; @@ -39660,7 +39688,6 @@ new Promise(resolve=>{ for(var i=0;i{ } } if(!callback){ - return; + callback=function(){}; } //try{ // if(noinput){ @@ -39710,16 +39737,18 @@ new Promise(resolve=>{ var controls=ui.create.div(dialog); var clickConfirm=function(){ if(noinput){ + //给一个返回值使promise化正常使用 + callback(true); promptContainer.remove(); } - else if(input.value){ + else{ callback(input.value); promptContainer.remove(); } } var clickCancel=function(){ + callback(false); if(!forced){ - callback(false); promptContainer.remove(); } } @@ -39727,7 +39756,7 @@ new Promise(resolve=>{ if(!forced){ ui.create.div('.menubutton.large','取消',controls,clickCancel); } - if(noinput){ + if(noinput||(str2&&str2.length>0)){ confirmNode.classList.remove('disabled'); } else{ From 1404d4666b539bdf470a6dd5429de72f6f9fb074 Mon Sep 17 00:00:00 2001 From: shijian <2954700422@qq.com> Date: Mon, 4 Dec 2023 09:22:18 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=B8=BA=E5=85=B6=E4=BB=96=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=B9=B3=E5=8F=B0=E6=8F=90=E4=BE=9B=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=AF=BB=E5=86=99=E5=87=BD=E6=95=B0=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E7=A7=8D=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/game/game.js b/game/game.js index e87d54ecb..6ef70f010 100644 --- a/game/game.js +++ b/game/game.js @@ -10284,14 +10284,33 @@ new Promise(resolve=>{ } } else{ - window.onbeforeunload=function(){ - if(lib.config.confirm_exit&&!_status.reloading){ + //为其他自定义平台提供文件读写函数赋值的一种方式。 + //但这种方式只能修改game的文件读写函数。 + if(window.initRWFunction){ + const g={}; + const RWFunctionName=['download','readFile','readFileAsText','writeFile','removeFile','getFileList','ensureDirectory','createDir']; + RWFunctionName.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; + } + } + }) + }); + window.initRWFunction(g); + } + window.onbeforeunload = function () { + if (lib.config.confirm_exit && !_status.reloading) { return '是否离开游戏?' } - else{ + else { return null; } - } + }; } lib.config=window.config; From 761ac53e66f217d4b88df9abd93f35d0e5b0aa78 Mon Sep 17 00:00:00 2001 From: shijian <2954700422@qq.com> Date: Mon, 4 Dec 2023 09:23:22 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9end=5Fof=5Fline=E4=B8=BAc?= =?UTF-8?q?rlf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 61c31431d..d8a6ed938 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,7 +3,7 @@ root = true [*] charset = utf-8 -end_of_line = lf +end_of_line = crlf insert_final_newline = true [*.js] From 7f89db7fb32f6600979723a0a3ea903dd8cd5a43 Mon Sep 17 00:00:00 2001 From: shijian <2954700422@qq.com> Date: Mon, 4 Dec 2023 09:29:19 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/game/game.js b/game/game.js index 6ef70f010..75099564c 100644 --- a/game/game.js +++ b/game/game.js @@ -10286,10 +10286,10 @@ new Promise(resolve=>{ else{ //为其他自定义平台提供文件读写函数赋值的一种方式。 //但这种方式只能修改game的文件读写函数。 - if(window.initRWFunction){ + if(window.initReadWriteFunction){ const g={}; - const RWFunctionName=['download','readFile','readFileAsText','writeFile','removeFile','getFileList','ensureDirectory','createDir']; - RWFunctionName.forEach(prop=>{ + const ReadWriteFunctionName=['download','readFile','readFileAsText','writeFile','removeFile','getFileList','ensureDirectory','createDir']; + ReadWriteFunctionName.forEach(prop=>{ Object.defineProperty(g,prop,{ configurable:true, get(){ return undefined; }, @@ -10301,7 +10301,7 @@ new Promise(resolve=>{ } }) }); - window.initRWFunction(g); + window.initReadWriteFunction(g); } window.onbeforeunload = function () { if (lib.config.confirm_exit && !_status.reloading) {