Merge pull request #669 from nonameShijian/PR-Branch

修复getAttackRange的错误,游戏内控制台作用域优化,游戏读写函数promise化:game.promises,优化游戏内控制台的显示
This commit is contained in:
Spmario233 2023-11-24 00:32:04 +08:00 committed by GitHub
commit a6ebeb6de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 253 additions and 96 deletions

View File

@ -9885,6 +9885,62 @@ new Promise(resolve=>{
ui.css.default=lib.init.css(lib.assetURL+'layout/default','layout'); ui.css.default=lib.init.css(lib.assetURL+'layout/default','layout');
proceed2(); 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){ if(lib.device){
lib.init.cordovaReady=function(){ lib.init.cordovaReady=function(){
@ -9931,7 +9987,7 @@ new Promise(resolve=>{
if(!url.startsWith('http')){ if(!url.startsWith('http')){
url=get.url(dev)+url; url=get.url(dev)+url;
} }
var fileTransfer = new FileTransfer(); var fileTransfer=new FileTransfer();
folder=lib.assetURL+folder; folder=lib.assetURL+folder;
if(onprogress){ if(onprogress){
fileTransfer.onprogress=function(progressEvent){ fileTransfer.onprogress=function(progressEvent){
@ -9952,8 +10008,8 @@ new Promise(resolve=>{
window.resolveLocalFileSystemURL(lib.assetURL,function(entry){ window.resolveLocalFileSystemURL(lib.assetURL,function(entry){
entry.getFile(filename,{},function(fileEntry){ entry.getFile(filename,{},function(fileEntry){
fileEntry.file(function(fileToLoad){ fileEntry.file(function(fileToLoad){
var fileReader = new FileReader(); var fileReader=new FileReader();
fileReader.onload = function(e){ fileReader.onload=function(e){
callback(e.target.result); callback(e.target.result);
}; };
fileReader.readAsArrayBuffer(fileToLoad, "UTF-8"); fileReader.readAsArrayBuffer(fileToLoad, "UTF-8");
@ -9965,8 +10021,8 @@ new Promise(resolve=>{
window.resolveLocalFileSystemURL(lib.assetURL,function(entry){ window.resolveLocalFileSystemURL(lib.assetURL,function(entry){
entry.getFile(filename,{},function(fileEntry){ entry.getFile(filename,{},function(fileEntry){
fileEntry.file(function(fileToLoad){ fileEntry.file(function(fileToLoad){
var fileReader = new FileReader(); var fileReader=new FileReader();
fileReader.onload = function(e){ fileReader.onload=function(e){
callback(e.target.result); callback(e.target.result);
}; };
fileReader.readAsText(fileToLoad, "UTF-8"); fileReader.readAsText(fileToLoad, "UTF-8");
@ -9977,33 +10033,31 @@ new Promise(resolve=>{
game.writeFile=function(data,path,name,callback){ game.writeFile=function(data,path,name,callback){
game.ensureDirectory(path,function(){ game.ensureDirectory(path,function(){
if(Object.prototype.toString.call(data)=='[object File]'){ if(Object.prototype.toString.call(data)=='[object File]'){
var fileReader = new FileReader(); var fileReader=new FileReader();
fileReader.onload = function(e){ fileReader.onload=function(e){
game.writeFile(e.target.result,path,name,callback); game.writeFile(e.target.result,path,name,callback);
}; };
fileReader.readAsArrayBuffer(data, "UTF-8"); fileReader.readAsArrayBuffer(data,"UTF-8");
} }
else{ else{
window.resolveLocalFileSystemURL(lib.assetURL+path,function(entry){ window.resolveLocalFileSystemURL(lib.assetURL+path,function(entry){
entry.getFile(name,{create:true},function(fileEntry){ entry.getFile(name,{create:true},function(fileEntry){
fileEntry.createWriter(function(fileWriter){ fileEntry.createWriter(function(fileWriter){
fileWriter.onwriteend=callback; fileWriter.onwriteend=callback;
fileWriter.write(data); fileWriter.write(data);
}); },callback);
}); },callback);
}); },callback);
} }
}); });
}; };
game.removeFile=function(dir,callback){ game.removeFile=function(dir,callback){
window.resolveLocalFileSystemURL(lib.assetURL,function(entry){ window.resolveLocalFileSystemURL(lib.assetURL,function(entry){
entry.getFile(dir,{},function(fileEntry){ entry.getFile(dir,{},function(fileEntry){
fileEntry.remove(); fileEntry.remove();
if(callback){ if(callback) callback();
callback(); },callback||function(){});
} },callback||function(){});
});
});
}; };
game.getFileList=(dir,success,failure)=>{ game.getFileList=(dir,success,failure)=>{
var files=[],folders=[]; var files=[],folders=[];
@ -10055,6 +10109,7 @@ new Promise(resolve=>{
createDirectory(); createDirectory();
},reject)); },reject));
}; };
initGamePromises();
if(ui.updateUpdate){ if(ui.updateUpdate){
ui.updateUpdate(); ui.updateUpdate();
} }
@ -10239,6 +10294,7 @@ new Promise(resolve=>{
createDirectory(); createDirectory();
}); });
}; };
initGamePromises();
if(ui.updateUpdate){ if(ui.updateUpdate){
ui.updateUpdate(); ui.updateUpdate();
} }
@ -28219,7 +28275,7 @@ new Promise(resolve=>{
}); });
equips.forEach(card=>{ equips.forEach(card=>{
const info=get.info(card,false).distance; const info=get.info(card,false).distance;
if(ininfo&&info.globalFrom){ if(info&&info.globalFrom){
range+=info.globalFrom; range+=info.globalFrom;
} }
}) })
@ -36248,6 +36304,10 @@ new Promise(resolve=>{
} }
}; };
const game={ const game={
/**
* @type { { [key: string]: (...args:[])=>Promise } }
*/
promises:{},
globalEventHandlers: new class { globalEventHandlers: new class {
constructor() { constructor() {
this._handlers = {}; this._handlers = {};
@ -51330,53 +51390,127 @@ new Promise(resolve=>{
node._initLink=function(){ node._initLink=function(){
node.link=page; node.link=page;
page.classList.add('menu-sym'); page.classList.add('menu-sym');
var text=document.createElement('div');
text.style.width='194px'; const text=document.createElement('div');
text.style.height='124px'; text.css({
text.style.padding='3px'; 'width':'194px',
text.style.borderRadius='2px'; 'height':'124px',
text.style.boxShadow='rgba(0, 0, 0, 0.2) 0 0 0 1px'; 'padding':'3px',
text.style.textAlign='left'; 'borderRadius':'2px',
text.style.webkitUserSelect='initial'; 'boxShadow':'rgba(0, 0, 0, 0.2) 0 0 0 1px',
text.style.overflow='scroll'; 'textAlign':'left',
text.style.position='absolute'; 'webkitUserSelect':'initial',
text.style.left='30px'; 'overflow':'scroll',
text.style.top='50px'; 'position':'absolute',
text.style.wordBreak='break-all'; 'left':'30px',
var pre=ui.create.node('pre.fullsize',text); 'top':'50px',
pre.style.margin=0; 'wordBreak':'break-all'
pre.style.padding=0; });
pre.style.position='relative';
pre.style.webkitUserSelect = pre.style.userSelect = 'text'; const pre=ui.create.node('pre.fullsize',text);
text.css.call(pre,{
'margin':'0',
'padding':'0',
'position':'relative',
'webkitUserSelect': 'text',
'userSelect':'text'
});
lib.setScroll(pre); lib.setScroll(pre);
page.appendChild(text); page.appendChild(text);
const text2=document.createElement('input');
text.css.call(text2,{
'width':'200px',
'height':'20px',
'padding':'0',
'position': 'absolute',
'top':'15px',
'left':'30px',
'resize':'none',
'border':'none',
'borderRadius':'2px',
'boxShadow':'rgba(0, 0, 0, 0.2) 0 0 0 1px'
});
// var caption=ui.create.div('','输入命令',page); const g={};
// caption.style.margin='6px'; const logs=[];
// caption.style.position='absolute'; let logindex=-1;
// caption.style.width='120px'; let proxyWindow=Object.assign({},window,{
// caption.style.top='129px'; _status:_status,
// caption.style.left='64px'; lib:lib,
var text2=document.createElement('input'); game:game,
text2.style.width='200px'; ui:ui,
text2.style.height='20px'; get:get,
text2.style.padding='0'; ai:ai,
text2.style.position='absolute'; cheat:lib.cheat
text2.style.top='15px'; });
text2.style.left='30px'; Object.defineProperties(proxyWindow, {
text2.style.resize='none'; '_status':{
text2.style.border='none'; configurable:false,
text2.style.borderRadius='2px'; enumerable:true,
text2.style.boxShadow='rgba(0, 0, 0, 0.2) 0 0 0 1px'; writable:false
var g={}; },
var logs=[]; 'lib':{
var logindex=-1; configurable:false,
var cheat=lib.cheat; enumerable:true,
//使用正则匹配绝大多数的普通obj对象避免解析成代码块。 writable:false
var reg=/^\{([^{}]+:\s*([^\s,]*|'[^']*'|"[^"]*"|\{[^}]*\}|\[[^\]]*\]|null|undefined|([a-zA-Z$_][a-zA-Z0-9$_]*\s*:\s*)?[a-zA-Z$_][a-zA-Z0-9$_]*\(\)))(?:,\s*([^{}]+:\s*(?:[^\s,]*|'[^']*'|"[^"]*"|\{[^}]*\}|\[[^\]]*\]|null|undefined|([a-zA-Z$_][a-zA-Z0-9$_]*\s*:\s*)?[a-zA-Z$_][a-zA-Z0-9$_]*\(\))))*\}$/; },
'game':{
configurable:false,
enumerable:true,
writable:false
},
'ui':{
configurable:false,
enumerable:true,
writable:false
},
'get':{
configurable:false,
enumerable:true,
writable:false
},
'ai':{
configurable:false,
enumerable:true,
writable:false
},
'cheat':{
configurable:false,
enumerable:true,
writable:false
}
});
if(typeof window.Proxy=='function'){
proxyWindow=new Proxy(proxyWindow,{
set(target,prop,newValue) {
if (!['_status','lib','game','ui','get','ai','cheat'].includes(prop)){
Reflect.set(window, prop, newValue);
}
return Reflect.set(target,prop,newValue);
}
});
}
//使用new Function隔绝作用域避免在控制台可以直接访问到runCommand等变量 //使用new Function隔绝作用域避免在控制台可以直接访问到runCommand等变量
var fun=(new Function('reg','value','_status','lib','game','ui','get','ai',`"use strict";\nreturn eval(reg.test(value)?('('+value+')'):value)`)); /**
var runCommand=function(e){ * @type { (value:string)=>any }
*/
const fun=(new Function('window',`
const _status=window._status;
const lib=window.lib;
const game=window.game;
const ui=window.ui;
const get=window.get;
const ai=window.ai;
const cheat=window.lib.cheat;
//使用正则匹配绝大多数的普通obj对象避免解析成代码块。
const reg=/^\{([^{}]+:\s*([^\s,]*|'[^']*'|"[^"]*"|\{[^}]*\}|\[[^\]]*\]|null|undefined|([a-zA-Z$_][a-zA-Z0-9$_]*\s*:\s*)?[a-zA-Z$_][a-zA-Z0-9$_]*\(\)))(?:,\s*([^{}]+:\s*(?:[^\s,]*|'[^']*'|"[^"]*"|\{[^}]*\}|\[[^\]]*\]|null|undefined|([a-zA-Z$_][a-zA-Z0-9$_]*\s*:\s*)?[a-zA-Z$_][a-zA-Z0-9$_]*\(\))))*\}$/;
return function(value){
"use strict";
return eval(reg.test(value)?('('+value+')'):value);
}
`))(proxyWindow);
const runCommand=()=>{
if(text2.value&&!['up','down'].contains(text2.value)){ if(text2.value&&!['up','down'].contains(text2.value)){
logindex=-1; logindex=-1;
logs.unshift(text2.value); logs.unshift(text2.value);
@ -51415,10 +51549,9 @@ new Promise(resolve=>{
else{ else{
if(!game.observe&&!game.online){ if(!game.observe&&!game.online){
try{ try{
var value=text2.value.trim(); let value=text2.value.trim();
if(value.endsWith(";")) value=value.slice(0,-1).trim(); if(value.endsWith(";")) value=value.slice(0,-1).trim();
var result=fun(reg,value,_status,lib,game,ui,get,ai); game.print(fun(value));
game.print(result);
} }
catch(e){ catch(e){
game.print(e); game.print(e);
@ -51427,7 +51560,7 @@ new Promise(resolve=>{
text2.value=''; text2.value='';
} }
} }
text2.addEventListener('keydown',function(e){ text2.addEventListener('keydown',e=>{
if(e.keyCode==13){ if(e.keyCode==13){
runCommand(); runCommand();
} }
@ -51450,26 +51583,51 @@ new Promise(resolve=>{
}); });
page.appendChild(text2); page.appendChild(text2);
game.print=function(){ game.print=function(){
var args=[].slice.call(arguments); const args=[...arguments];
var printResult=args.map(arg=>{ const printResult=args.map(arg=>{
if(get.is.object(arg)||typeof arg=='function'){ if(typeof arg!='string'){
var argi=get.stringify(arg); const parse=(obj)=>{
if(argi/*&&argi.length<5000*/){ if(Array.isArray(obj)){
return argi.replace(/&/g, '&amp;') return `[${obj.map(v=>parse(v))}]`;
.replace(/</g, '&lt;') }else if(typeof obj=='function'){
.replace(/>/g, '&gt;') return `Function`;
.replace(/"/g, '&quot;') }else if(typeof obj!='string'){
.replace(/'/g, '&#39;'); return String(obj);
}else{
return `'${String(obj)}'`;
}
};
if(typeof arg=='function'){
let argi;
try{
argi=get.stringify(arg);
if(argi==='') argi=arg.toString();
}catch(_){
argi=arg.toString();
}
return argi.replace(/&/g,'&amp;')
.replace(/</g,'&lt;')
.replace(/>/g,'&gt;')
.replace(/"/g,'&quot;')
.replace(/'/g,'&#39;');
}
else if(typeof arg=='object'){
let msg='';
for(const name of Object.getOwnPropertyNames(arg)){
msg+=`${name}: ${parse(arg[name])}<br>`;
}
return `<details><summary>${parse(arg)}</summary>${msg}</details>`;
}else{
return parse(arg);
} }
else return arg.toString();
}else{ }else{
var str=String(arg); const str=String(arg);
if (!/<[a-zA-Z]+[^>]*?\/?>.*?(?=<\/[a-zA-Z]+[^>]*?>|$)/.exec(str)) return String(arg) if (!/<[a-zA-Z]+[^>]*?\/?>.*?(?=<\/[a-zA-Z]+[^>]*?>|$)/.exec(str)) return str
.replace(/&/g, '&amp;') .replace(/&/g,'&amp;')
.replace(/</g, '&lt;') .replace(/</g,'&lt;')
.replace(/>/g, '&gt;') .replace(/>/g,'&gt;')
.replace(/"/g, '&quot;') .replace(/"/g,'&quot;')
.replace(/'/g, '&#39;'); .replace(/'/g,'&#39;');
else return str; else return str;
} }
}).join(' '); }).join(' ');
@ -51477,13 +51635,11 @@ new Promise(resolve=>{
text.scrollTop=text.scrollHeight; text.scrollTop=text.scrollHeight;
} }
if(_status.toprint){ if(_status.toprint){
for(var i=0;i<_status.toprint.length;i++){ game.print(...status.toprint);
game.print.apply(this,_status.toprint[i]);
}
delete _status.toprint; delete _status.toprint;
} }
runButton.listen(runCommand); runButton.listen(runCommand);
clearButton.listen(function(){ clearButton.listen(()=>{
pre.innerHTML=''; pre.innerHTML='';
}); });
}; };
@ -63540,6 +63696,7 @@ new Promise(resolve=>{
} }
} }
}, },
//我愚蠢的弟弟呦这是最后一次兼容46内核兼容版了
get:get get:get
}; };
/** /**