[2023/09/18: ①] Move content handle to `lib.init.parsex`.

This commit is contained in:
Rintim 2023-09-18 12:55:40 +08:00
parent 897f993409
commit c7bf5c03f2
1 changed files with 75 additions and 38 deletions

View File

@ -10234,47 +10234,84 @@
localStorage.removeItem(lib.configprefix+'background');
}
},
//by 诗笺、Tipx-L
parsex:function(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();
//获取第一个 { 后的所有字符
str=str.slice(str.indexOf('{')+1);
//func中要写步骤的话必须要写step 0
if(str.indexOf('step 0')==-1){
str='{if(event.step==1) {event.finish();return;}\n'+str;
}else{
var skip=0;
//每层最多找99个step
for (var k=0;k<99;k++) {
//正则表达式
var reg=new RegExp(`['"]step ${k}['"]`);
var result=str.slice(skip).match(reg);
if(result==null) break;
var insertStr;
if(k==0){
insertStr=`switch(step){case 0:`;
}else{
insertStr=`break;case ${k}:`;
}
var copy=str;
copy=copy.slice(0,skip+result.index)+insertStr+copy.slice(skip+result.index+result[0].length);
//测试是否有错误
try{
new Function(copy);
str=copy;
skip+=result.index+insertStr.length;
}catch(error){
k--;
skip+=result.index+result[0].length;
parsex:function(item){
//by 诗笺、Tipx-L
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();
//获取第一个 { 后的所有字符
str=str.slice(str.indexOf('{')+1);
//func中要写步骤的话必须要写step 0
if(str.indexOf('step 0')==-1){
str='{if(event.step==1) {event.finish();return;}\n'+str;
}else{
var skip=0;
//每层最多找99个step
for (var k=0;k<99;k++) {
//正则表达式
var reg=new RegExp(`['"]step ${k}['"]`);
var result=str.slice(skip).match(reg);
if(result==null) break;
var insertStr;
if(k==0){
insertStr=`switch(step){case 0:`;
}else{
insertStr=`break;case ${k}:`;
}
var copy=str;
copy=copy.slice(0,skip+result.index)+insertStr+copy.slice(skip+result.index+result[0].length);
//测试是否有错误
try{
new Function(copy);
str=copy;
skip+=result.index+insertStr.length;
}catch(error){
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)) {
// TODO: Parse Generator Function
throw new Error("NYI: Pase Generator Function");
}
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){
if(typeof func=='function'){