180 lines
6.5 KiB
C#
180 lines
6.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Globalization;
|
||
using DynamicExpresso;
|
||
using SLSFramework.General;
|
||
using UnityEngine;
|
||
|
||
namespace Continentis.MainGame
|
||
{
|
||
public partial class DynamicTextInterpreter
|
||
{
|
||
public static void InitializeInterpreter(ref Interpreter interpreter)
|
||
{
|
||
interpreter.SetFunction("Keyword", new Func<string, string>(kw => SetKeyword(kw, "#FFA500")));
|
||
interpreter.SetFunction("HintKeyword", new Func<string, string>(kw => SetKeyword(kw, "#FFA500")));
|
||
interpreter.SetFunction("HintKeyword", new Func<string, string, string>(SetKeyword));
|
||
interpreter.SetFunction("DescKeyword", new Func<string, string>(kw =>DescKeyword(kw, "#FFA500")));
|
||
interpreter.SetFunction("DescKeyword", new Func<string, string, string>(DescKeyword));
|
||
interpreter.SetFunction("ColorText", new Func<string, string, string>(ColorText));
|
||
|
||
interpreter.SetFunction("Value", new Func<float, string>((cv) => GetValue(cv, false)));
|
||
interpreter.SetFunction("Value", new Func<float, float, string>((cv, bv) => GetValue(cv, bv, true, false)));
|
||
interpreter.SetFunction("Value", new Func<float, float, bool, string>((cv, bv, high) => GetValue(cv, bv, high, false)));
|
||
interpreter.SetFunction("Value", new Func<float, float, bool, bool, string>((cv, bv, high, percent) => GetValue(cv, bv, high, percent)));
|
||
|
||
//本地函数,用于添加关键词到集合中并返回格式化后的关键词字符串
|
||
string SetKeyword(string keyword, string colorHex)
|
||
{
|
||
if (!string.IsNullOrEmpty(keyword))
|
||
{
|
||
return Keyword(keyword, colorHex);
|
||
}
|
||
|
||
return string.Empty;
|
||
}
|
||
}
|
||
|
||
public static string Parse(Interpreter interpreter, string template)
|
||
{
|
||
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);
|
||
Debug.Log($"Evaluating expression: {cleanExpression}");
|
||
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;
|
||
}
|
||
|
||
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 colorHex)
|
||
{
|
||
string color = colorHex;
|
||
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 colorHex)
|
||
{
|
||
string color = colorHex;
|
||
string result = key;
|
||
if (MainGameManager.Instance.keywordData.TryGetKeyword(key, out InterpretedKeyword keyword))
|
||
{
|
||
result = keyword.name.Localize();
|
||
}
|
||
|
||
return $"<color={color}>{result}</color>";
|
||
}
|
||
|
||
public static string ColorText(string text, string colorHex)
|
||
{
|
||
string color = colorHex;
|
||
return $"<color={color}>{text}</color>";
|
||
}
|
||
}
|
||
} |