Merge pull request #346 from nofficalfs/PR-Experimentalize-Content

[Experimentalize] `parsex`支持更多参数
This commit is contained in:
Spmario233 2023-09-18 16:34:19 +08:00 committed by GitHub
commit 76e058159f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 107 additions and 48 deletions

View File

@ -10235,47 +10235,103 @@
localStorage.removeItem(lib.configprefix+'background'); localStorage.removeItem(lib.configprefix+'background');
} }
}, },
//by 诗笺、Tipx-L parsex:function(item){
parsex:function(func){ //by 诗笺、Tipx-L
//Remove all comments function Legacy(func){
//移除所有注释 //Remove all comments
var str=func.toString().replace(/((?:(?:^[ \t]*)?(?:\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/(?:[ \t]*\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/)))?|\/\/(?:[^\\]|\\(?:\r?\n)?)*?(?:\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/))|(?=\r?\n))))+)|("(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|(?:\r?\n|[\s\S])[^\/"'\\\s]*)/mg,'$2').trim(); //移除所有注释
//获取第一个 { 后的所有字符 var str=func.toString().replace(/((?:(?:^[ \t]*)?(?:\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\/(?:[ \t]*\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/)))?|\/\/(?:[^\\]|\\(?:\r?\n)?)*?(?:\r?\n(?=[ \t]*(?:\r?\n|\/\*|\/\/))|(?=\r?\n))))+)|("(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|(?:\r?\n|[\s\S])[^\/"'\\\s]*)/mg,'$2').trim();
str=str.slice(str.indexOf('{')+1); //获取第一个 { 后的所有字符
//func中要写步骤的话必须要写step 0 str=str.slice(str.indexOf('{')+1);
if(str.indexOf('step 0')==-1){ //func中要写步骤的话必须要写step 0
str='{if(event.step==1) {event.finish();return;}\n'+str; if(str.indexOf('step 0')==-1){
}else{ str='{if(event.step==1) {event.finish();return;}\n'+str;
var skip=0; }else{
//每层最多找99个step var skip=0;
for (var k=0;k<99;k++) { //每层最多找99个step
//正则表达式 for (var k=0;k<99;k++) {
var reg=new RegExp(`['"]step ${k}['"]`); //正则表达式
var result=str.slice(skip).match(reg); var reg=new RegExp(`['"]step ${k}['"]`);
if(result==null) break; var result=str.slice(skip).match(reg);
var insertStr; if(result==null) break;
if(k==0){ var insertStr;
insertStr=`switch(step){case 0:`; if(k==0){
}else{ insertStr=`switch(step){case 0:`;
insertStr=`break;case ${k}:`; }else{
} insertStr=`break;case ${k}:`;
var copy=str; }
copy=copy.slice(0,skip+result.index)+insertStr+copy.slice(skip+result.index+result[0].length); var copy=str;
//测试是否有错误 copy=copy.slice(0,skip+result.index)+insertStr+copy.slice(skip+result.index+result[0].length);
try{ //测试是否有错误
new Function(copy); try{
str=copy; new Function(copy);
skip+=result.index+insertStr.length; str=copy;
}catch(error){ skip+=result.index+insertStr.length;
k--; }catch(error){
skip+=result.index+result[0].length; k--;
skip+=result.index+result[0].length;
}
} }
str=`if(event.step==${k}){event.finish();return;}`+str;
} }
str=`if(event.step==${k}){event.finish();return;}`+str; return (new Function('event','step','source','player','target','targets',
'card','cards','skill','forced','num','trigger','result',
'_status','lib','game','ui','get','ai',str));
}
switch(typeof item){
case "object":
if(Array.isArray(item)){
let lastEvent=null;
return (event,step,source,player,target,targets,card,cards,skill,forced,num,trigger,result,_status,lib,game,ui,get,ai)=>{
if(step>=item.length) return event.finish();
var current=item[step];
lastEvent=current(event,{
event:event,
step:step,
source:source,
player:player,
target:target,
targets:targets,
card:card,
cards:cards,
skill:skill,
forced:forced,
num:num,
trigger:trigger,
result:result
},(lastEvent&&("result" in lastEvent))?lastEvent.result:null);
}
}
else{
// TODO: Parse Common Object
throw new Error("NYI: Parse Common Object");
}
case "function":
if (gnc.is.generatorFunc(item)) {
let gen,lastEvent;
return (event,step,source,player,target,targets,card,cards,skill,forced,num,trigger,result,_status,lib,game,ui,get,ai)=>{
if(!gen)gen=item(event,{
event:event,
step:step,
source:source,
player:player,
target:target,
targets:targets,
card:card,
cards:cards,
skill:skill,
forced:forced,
num:num,
trigger:trigger,
result:result
});
var res=gen.next((lastEvent&&("result" in lastEvent))?lastEvent.result:null);
if(res.done) event.finish();
else lastEvent=res.value;
}
}
else return Legacy(item);
} }
return (new Function('event','step','source','player','target','targets',
'card','cards','skill','forced','num','trigger','result',
'_status','lib','game','ui','get','ai',str));
}, },
eval:function(func){ eval:function(func){
if(typeof func=='function'){ if(typeof func=='function'){
@ -28603,16 +28659,19 @@
} }
return this; return this;
}, },
setContent:function(name){ setContent:function(item){
if(typeof name=='function'){ switch(typeof item){
this.content=lib.init.parsex(name); case "object":
} case "function":
else{ this.content=lib.init.parsex(item);
if(!lib.element.content[name]._parsed){ break;
lib.element.content[name]=lib.init.parsex(lib.element.content[name]); default:
lib.element.content[name]._parsed=true; if(!lib.element.content[item]._parsed){
} lib.element.content[item]=lib.init.parsex(lib.element.content[item]);
this.content=lib.element.content[name]; lib.element.content[item]._parsed=true;
}
this.content=lib.element.content[item];
break;
} }
return this; return this;
}, },