167 lines
5.6 KiB
C#
167 lines
5.6 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Globalization;
|
|||
|
|
using Continentis.MainGame.Card;
|
|||
|
|
using DynamicExpresso;
|
|||
|
|
using SLSFramework.General;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace Continentis.MainGame
|
|||
|
|
{
|
|||
|
|
public partial class DynamicTextInterpreter
|
|||
|
|
{
|
|||
|
|
public static string Parse(Interpreter interpreter, string template, List<string> keywords, List<string> hintKeywords)
|
|||
|
|
{
|
|||
|
|
interpreter.UnsetFunction("Keyword");
|
|||
|
|
interpreter.SetFunction("Keyword", new Func<string, string>(kw => SetKeyword(ref keywords, kw)));
|
|||
|
|
interpreter.UnsetFunction("HintKeyword");
|
|||
|
|
interpreter.SetFunction("HintKeyword", new Func<string, string>(kw => SetKeyword(ref hintKeywords, kw)));
|
|||
|
|
interpreter.UnsetFunction("DescKeyword");
|
|||
|
|
interpreter.SetFunction("DescKeyword", new Func<string, string>(DescKeyword));
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
while (template.Contains("$"))
|
|||
|
|
{
|
|||
|
|
int startIndex = template.LastIndexOf('$');
|
|||
|
|
int endIndex = FindMatchingClosingParenthesis(template, startIndex);
|
|||
|
|
|
|||
|
|
if (endIndex == -1)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"解析错误: 在 '{template}' 中找不到与 '{template.Substring(startIndex)}' 匹配的闭合括号。");
|
|||
|
|
return template; // 中断以防止死循环
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string expressionToEvaluate = template.Substring(startIndex, endIndex - startIndex + 1);
|
|||
|
|
string cleanExpression = expressionToEvaluate.Substring(1);
|
|||
|
|
|
|||
|
|
object result = interpreter.Eval(cleanExpression);
|
|||
|
|
string resultAsLiteral = result.ToString();
|
|||
|
|
template = template.Substring(0, startIndex) + resultAsLiteral + template.Substring(endIndex + 1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
throw new Exception($"解析模板时发生严重错误: {ex.Message}\nStackTrace: {ex.StackTrace}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return template;
|
|||
|
|
|
|||
|
|
//本地函数,用于添加关键词到集合中并返回格式化后的关键词字符串
|
|||
|
|
string SetKeyword(ref List<string> collection, string keyword)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(keyword) && !collection.Contains(keyword))
|
|||
|
|
{
|
|||
|
|
collection.Add(keyword);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return Keyword(keyword);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static int FindMatchingClosingParenthesis(string text, int startIndex)
|
|||
|
|
{
|
|||
|
|
int openParenIndex = text.IndexOf('(', startIndex);
|
|||
|
|
if (openParenIndex == -1) return -1;
|
|||
|
|
|
|||
|
|
int parenthesisCounter = 1;
|
|||
|
|
bool isInString = false;
|
|||
|
|
|
|||
|
|
for (int i = openParenIndex + 1; i < text.Length; i++)
|
|||
|
|
{
|
|||
|
|
char c = text[i];
|
|||
|
|
char prevC = i > 0 ? text[i - 1] : '\0';
|
|||
|
|
|
|||
|
|
// 检查是否进入或退出字符串(忽略转义的引号 \")
|
|||
|
|
if (c == '"' && prevC != '\\')
|
|||
|
|
{
|
|||
|
|
isInString = !isInString;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果在字符串中,则跳过对括号的计数
|
|||
|
|
if (isInString)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (c == '(')
|
|||
|
|
{
|
|||
|
|
parenthesisCounter++;
|
|||
|
|
}
|
|||
|
|
else if (c == ')')
|
|||
|
|
{
|
|||
|
|
parenthesisCounter--;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (parenthesisCounter == 0)
|
|||
|
|
{
|
|||
|
|
return i;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return -1; // 没有找到匹配的闭合括号
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public partial class DynamicTextInterpreter
|
|||
|
|
{
|
|||
|
|
public static string GetValue(float currentValue, float baseValue, bool higherIsBetter, bool inPercent)
|
|||
|
|
{
|
|||
|
|
string color = "white";
|
|||
|
|
|
|||
|
|
if (currentValue > baseValue)
|
|||
|
|
{
|
|||
|
|
color = higherIsBetter ? "green" : "red";
|
|||
|
|
}
|
|||
|
|
else if (currentValue < baseValue)
|
|||
|
|
{
|
|||
|
|
color = higherIsBetter ? "red" : "green";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string valueStr = currentValue.ToString(CultureInfo.InvariantCulture);
|
|||
|
|
|
|||
|
|
if (inPercent)
|
|||
|
|
{
|
|||
|
|
valueStr = Mathf.RoundToInt(currentValue * 100) + "%";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return $"<color={color}>{valueStr}</color>";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string GetValue(float currentValue, bool inPercent)
|
|||
|
|
{
|
|||
|
|
string valueStr = currentValue.ToString(CultureInfo.InvariantCulture);
|
|||
|
|
|
|||
|
|
if (inPercent)
|
|||
|
|
{
|
|||
|
|
valueStr = Mathf.RoundToInt(currentValue * 100) + "%";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return valueStr;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string Keyword(string key)
|
|||
|
|
{
|
|||
|
|
string color = "orange";
|
|||
|
|
string result = key;
|
|||
|
|
if (MainGameManager.Instance.keywordData.TryGetKeyword(key, out InterpretedKeyword keyword))
|
|||
|
|
{
|
|||
|
|
result = keyword.name.Localize();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return $"<color={color}>{result}</color>";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string DescKeyword(string key)
|
|||
|
|
{
|
|||
|
|
string color = "yellow";
|
|||
|
|
string result = key;
|
|||
|
|
if (MainGameManager.Instance.keywordData.TryGetKeyword(key, out InterpretedKeyword keyword))
|
|||
|
|
{
|
|||
|
|
result = keyword.name.Localize();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return $"<color={color}>{result}</color>";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|