Files
Continentis/Assets/Scripts/MainGame/Base/Interpreters/DynamicTextInterpreter.cs

180 lines
6.5 KiB
C#
Raw Normal View History

2025-10-23 00:49:44 -04:00
using System;
using System.Collections.Generic;
using System.Globalization;
using DynamicExpresso;
using SLSFramework.General;
using UnityEngine;
namespace Continentis.MainGame
{
public partial class DynamicTextInterpreter
{
2025-10-27 07:04:34 -04:00
public static void InitializeInterpreter(ref Interpreter interpreter)
2025-10-23 00:49:44 -04:00
{
2025-10-27 07:04:34 -04:00
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));
2025-10-24 09:11:22 -04:00
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));
2025-10-27 07:04:34 -04:00
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);
}
2025-10-23 00:49:44 -04:00
2025-10-27 07:04:34 -04:00
return string.Empty;
}
}
public static string Parse(Interpreter interpreter, string template)
{
2025-12-11 17:25:49 -05:00
//try
2025-10-23 00:49:44 -04:00
{
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);
2025-10-24 09:11:22 -04:00
Debug.Log($"Evaluating expression: {cleanExpression}");
2025-10-23 00:49:44 -04:00
object result = interpreter.Eval(cleanExpression);
string resultAsLiteral = result.ToString();
template = template.Substring(0, startIndex) + resultAsLiteral + template.Substring(endIndex + 1);
}
}
2025-12-11 17:25:49 -05:00
/*catch (Exception ex)
2025-10-23 00:49:44 -04:00
{
throw new Exception($"解析模板时发生严重错误: {ex.Message}\nStackTrace: {ex.StackTrace}");
2025-12-11 17:25:49 -05:00
}*/
2025-10-23 00:49:44 -04:00
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;
}
2025-10-24 09:11:22 -04:00
public static string Keyword(string key, string colorHex)
2025-10-23 00:49:44 -04:00
{
2025-10-24 09:11:22 -04:00
string color = colorHex;
2025-10-23 00:49:44 -04:00
string result = key;
if (MainGameManager.Instance.keywordData.TryGetKeyword(key, out InterpretedKeyword keyword))
{
result = keyword.name.Localize();
}
return $"<color={color}>{result}</color>";
}
2025-10-24 09:11:22 -04:00
public static string DescKeyword(string key, string colorHex)
2025-10-23 00:49:44 -04:00
{
2025-10-24 09:11:22 -04:00
string color = colorHex;
2025-10-23 00:49:44 -04:00
string result = key;
if (MainGameManager.Instance.keywordData.TryGetKeyword(key, out InterpretedKeyword keyword))
{
result = keyword.name.Localize();
}
return $"<color={color}>{result}</color>";
}
2025-10-24 09:11:22 -04:00
public static string ColorText(string text, string colorHex)
{
string color = colorHex;
return $"<color={color}>{text}</color>";
}
2025-10-23 00:49:44 -04:00
}
}