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(); TextInterpreter.SetFunction("Value", new Func((cv) => DynamicTextInterpreter.GetValue(cv, false))); TextInterpreter.SetFunction("Value", new Func((cv, bv) => DynamicTextInterpreter.GetValue(cv, bv, true, false))); TextInterpreter.SetFunction("Value", new Func((cv, bv, high) => DynamicTextInterpreter.GetValue(cv, bv, high, false))); TextInterpreter.SetFunction("Value", new Func((cv, bv, high, percent) => DynamicTextInterpreter.GetValue(cv, bv, high, percent))); foreach (KeyValuePair keyword in MainGameManager.Instance.keywordData.interpretedKeywords) { TextInterpreter.SetVariable(keyword.Key, keyword.Key); } } public static void InterpretText(BuffBase buff) { TextInterpreter.SetFunction("ParameterInt", new Func((paramName) => GetParameterInt(buff, paramName, false))); TextInterpreter.SetFunction("ParameterAbsInt", new Func((paramName) => GetParameterInt(buff, paramName, true))); TextInterpreter.SetFunction("ParameterPercent", new Func((paramName) => GetParameterPercent(buff, paramName))); TextInterpreter.SetFunction("ParameterFloat", new Func((paramName) => GetParameterFloat(buff, paramName))); TextInterpreter.SetFunction("ParameterString", new Func((paramName) => GetParameterString(buff, paramName))); string descriptionToParse = buff.contentSubmodule.originalFunctionText; string result = DynamicTextInterpreter.Parse(TextInterpreter, descriptionToParse, new List(), new List()); buff.contentSubmodule.interpretedFunctionText = result; } } public static partial class BuffTextInterpreter { private static string GetParameterInt(BuffBase buff, string parameterName, bool isAbsolute) { if (buff.contentSubmodule.parameterGetters.TryGetValue(parameterName, out Func 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(BuffBase buff, string parameterName) { if (buff.contentSubmodule.parameterGetters.TryGetValue(parameterName, out Func getter)) { return Convert.ToSingle(getter()).ToString(CultureInfo.InvariantCulture); } Debug.LogError($"无法找到参数获取器: {parameterName} 在 Buff: {buff.contentSubmodule.displayName}"); return "ERROR"; } private static string GetParameterPercent(BuffBase buff, string parameterName) { if (buff.contentSubmodule.parameterGetters.TryGetValue(parameterName, out Func getter)) { return Mathf.RoundToInt(Convert.ToSingle(getter()) * 100).ToString() + "%"; } Debug.LogError($"无法找到参数获取器: {parameterName} 在 Buff: {buff.contentSubmodule.displayName}"); return "ERROR"; } private static string GetParameterString(BuffBase buff, string parameterName) { if (buff.contentSubmodule.parameterGetters.TryGetValue(parameterName, out Func getter)) { return getter(); } Debug.LogError($"无法找到参数获取器: {parameterName} 在 Buff: {buff.contentSubmodule.displayName}"); return "ERROR"; } } }