Files
Continentis/Assets/Scripts/MainGame/Base/Interpreters/DynamicTextInterpreter.cs
SoulliesOfficial c3c4a17440 测试
2025-10-26 00:23:50 -04:00

176 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 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, "#FFA500")));
interpreter.UnsetFunction("HintKeyword");
interpreter.SetFunction("HintKeyword", new Func<string, string>(kw => SetKeyword(ref hintKeywords, kw, "#FFA500")));
interpreter.SetFunction("HintKeyword", new Func<string, string, string>((kw, colorHex) => SetKeyword(ref hintKeywords, kw, colorHex)));
interpreter.UnsetFunction("DescKeyword");
interpreter.SetFunction("DescKeyword", new Func<string, string>(kw =>DescKeyword(kw, "#FFA500")));
interpreter.SetFunction("DescKeyword", new Func<string, string, string>(DescKeyword));
interpreter.UnsetFunction("ColorText");
interpreter.SetFunction("ColorText", new Func<string, string, string>(ColorText));
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;
//本地函数,用于添加关键词到集合中并返回格式化后的关键词字符串
string SetKeyword(ref List<string> collection, string keyword, string colorHex)
{
if (!string.IsNullOrEmpty(keyword) && !collection.Contains(keyword))
{
collection.Add(keyword);
}
return Keyword(keyword, colorHex);
}
}
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>";
}
}
}