Files
Million/Million/Manager/InteratorScripter.cs
2022-10-18 16:23:43 +09:00

141 lines
3.9 KiB
C#

using Microsoft.CodeAnalysis.CSharp.Scripting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Million.Manager
{
public class InteratorScripter
{
/// <summary>
/// C# 코드기준으로 스크립팅 해줌
/// </summary>
public string regularExpressionString;
/// <summary>
/// 캐쉬 Dictionary
/// </summary>
private Dictionary<Dictionary<string, bool>,bool> rememberDic = new Dictionary<Dictionary<string, bool>, bool>();
/// <summary>
///
/// </summary>
/// <param name="RegularExpression"></param>
/// <returns></returns>
public bool DicExcuteScript(Dictionary<string,bool> RegularExpression)
{
try {
//중복 검사
var data = rememberDic.Keys.FirstOrDefault(x => !x.Except(RegularExpression).Any());
if (data != null)
{
return rememberDic[data];
}
else
{
return rememberDic[RegularExpression] = ScriptExcute(ChangeScript(regularExpressionString, RegularExpression));
}
}catch(Exception e)
{
return ScriptExcute(ChangeScript(regularExpressionString, RegularExpression));
}
}
/// <summary>
/// 스크립트용도로 변환 기능
/// </summary>
/// <param name="script"></param>
/// <param name="conditionDic">Key값은 반듯이 int로 변경가능해야한다</param>
/// <returns></returns>
public static string ChangeScript(string script, Dictionary<string,bool> conditionDic)
{
var conList = conditionDic.ToList();
conList.Sort((x,y)=> y.Key.Length.CompareTo(x.Key.Length));
foreach(var con in conList)
{
script = script.Replace(con.Key, con.Value.ToString().ToLower());
}
return script;
}
/// <summary>
/// 문자열값을 스크립트 돌림, 실패시 false 반환
/// </summary>
/// <param name="regString"></param>
/// <returns></returns>
public static bool ScriptExcute(string regString)
{
try
{
return CSharpScript.EvaluateAsync<bool>(regString).Result;
}
catch(Exception ex)
{
throw ex;
}
return false;
}
public static void ScriptExcute111(string regString)
{
try
{
var result = CSharpScript.EvaluateAsync(regString);
result.ToString();
}
catch (Exception ex)
{
ex.ToString();
}
}
/// <summary>
/// 문자열값을 스크립트 돌림, 실패시 0 반환
/// </summary>
/// <param name="regString"></param>
/// <returns></returns>
public static double ScriptExcuteDouble(string regString)
{
try
{
return CSharpScript.EvaluateAsync<double>(regString).Result;
}
catch (Exception ex)
{
ex.ToString();
}
return 0.0;
}
/// <summary>
///
/// </summary>
/// <param name="dataList"></param>
public static void ScriptExcute(Dictionary<string,bool> dataList)
{
string exKey;
try
{
foreach (var data in dataList)
{
exKey = data.Key;
dataList[data.Key] = CSharpScript.EvaluateAsync<bool>(data.Key).Result;
}
}
catch(Exception ex)
{
ex.ToString();
}
}
}
}