Files
Continentis/Assets/Scripts/MainGame/Base/Interpreters/BuffTextInterpreter.cs
SoulliesOfficial 2906206f0c keyword + animation
2025-10-27 07:04:34 -04:00

92 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using DynamicExpresso;
using UnityEngine;
namespace Continentis.MainGame
{
public static partial class BuffTextInterpreter
{
private static readonly Interpreter TextInterpreter;
static BuffTextInterpreter()
{
TextInterpreter = new Interpreter();
DynamicTextInterpreter.InitializeInterpreter(ref TextInterpreter);
foreach (KeyValuePair<string, InterpretedKeyword> keyword in MainGameManager.Instance.keywordData.interpretedKeywords)
{
TextInterpreter.SetVariable(keyword.Key, keyword.Key);
}
}
public static void InterpretText<T>(BuffBase<T> buff)
{
TextInterpreter.SetFunction("ParameterInt", new Func<string, string>((paramName) => GetParameterInt(buff, paramName, false)));
TextInterpreter.SetFunction("ParameterAbsInt", new Func<string, string>((paramName) => GetParameterInt(buff, paramName, true)));
TextInterpreter.SetFunction("ParameterPercent", new Func<string, string>((paramName) => GetParameterPercent(buff, paramName)));
TextInterpreter.SetFunction("ParameterFloat", new Func<string, string>((paramName) => GetParameterFloat(buff, paramName)));
TextInterpreter.SetFunction("ParameterString", new Func<string, string>((paramName) => GetParameterString(buff, paramName)));
string descriptionToParse = buff.contentSubmodule.originalFunctionText;
string result = DynamicTextInterpreter.Parse(TextInterpreter, descriptionToParse);
buff.contentSubmodule.interpretedFunctionText = result;
}
}
public static partial class BuffTextInterpreter
{
private static string GetParameterInt<T>(BuffBase<T> buff, string parameterName, bool isAbsolute)
{
if (buff.contentSubmodule.parameterGetters.TryGetValue(parameterName, out Func<string> getter))
{
int intValue = Convert.ToInt32(getter());
if (isAbsolute)
{
return Math.Abs(intValue).ToString();
}
else
{
return intValue.ToString();
}
}
Debug.LogError($"无法找到参数获取器: {parameterName} 在 Buff: {buff.contentSubmodule.displayName}");
return "ERROR";
}
private static string GetParameterFloat<T>(BuffBase<T> buff, string parameterName)
{
if (buff.contentSubmodule.parameterGetters.TryGetValue(parameterName, out Func<string> getter))
{
return Convert.ToSingle(getter()).ToString(CultureInfo.InvariantCulture);
}
Debug.LogError($"无法找到参数获取器: {parameterName} 在 Buff: {buff.contentSubmodule.displayName}");
return "ERROR";
}
private static string GetParameterPercent<T>(BuffBase<T> buff, string parameterName)
{
if (buff.contentSubmodule.parameterGetters.TryGetValue(parameterName, out Func<string> getter))
{
return Mathf.RoundToInt(Convert.ToSingle(getter()) * 100).ToString() + "%";
}
Debug.LogError($"无法找到参数获取器: {parameterName} 在 Buff: {buff.contentSubmodule.displayName}");
return "ERROR";
}
private static string GetParameterString<T>(BuffBase<T> buff, string parameterName)
{
if (buff.contentSubmodule.parameterGetters.TryGetValue(parameterName, out Func<string> getter))
{
return getter();
}
Debug.LogError($"无法找到参数获取器: {parameterName} 在 Buff: {buff.contentSubmodule.displayName}");
return "ERROR";
}
}
}