using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; public class ConfigInitTask : LaunchTask { public override float expectTime { get { return LocalSave.GetFloat("ConfigInitTask_ExpectTime", 10f); } protected set { LocalSave.SetFloat("ConfigInitTask_ExpectTime", value); } } float threshold = 1f; public override void Begin() { LaunchInHot.m_CurrentStage = LaunchStage.ConfigInit; duration = Mathf.Max(0.5f, expectTime); threshold = Application.platform == RuntimePlatform.WindowsEditor ? 1f : 0.9f; // LaunchPostProcess.Instance.Begin(); // InitialFunctionConfig.Init(true); //有更新再初始化一次 } public override void End() { expectTime = timer; Debug.LogFormat("{0}执行时长:{1};", this.GetType().Name, timer); // TODO YYL // OperationLogCollect.Instance.RecordLauchEvent(3); // OperationLogCollect.Instance.RecordEvent(3); #if UNITY_EDITOR var config = FuncConfigConfig.Get("HeroAttrFormula"); var propertyFormula = config.Numerical1; var fightPropertyFormula = config.Numerical2; var fightPowerFormula = config.Numerical3; var skillFightPowerFormula = config.Numerical4; //读取文件 FightPowerFormula.cs文件,通过公式字符串生成代码 var filePath = "Assets/Scripts/Main/System/Main/FightPowerFormula.cs"; var fileContent = File.ReadAllText(filePath); // 生成新的代码内容 var newContent = GenerateFightPowerFormulaCode(propertyFormula, fightPropertyFormula, fightPowerFormula, skillFightPowerFormula); // 如果内容有变化,则写入文件 if (newContent != fileContent) { File.WriteAllText(filePath, newContent); Debug.LogError("战斗公式有变更 FightPowerFormula.cs 已根据配置公式重新生成, 请提交代码!"); } else { Debug.Log("FightPowerFormula.cs 内容未变化,无需重新生成"); } #endif } #if UNITY_EDITOR private string GenerateFightPowerFormulaCode(string propertyFormula, string fightPropertyFormula, string fightPowerFormula, string skillFightPowerFormula) { // 提取变量名 var propertyVariables = ExtractVariables(propertyFormula); var fightPropertyVariables = ExtractVariables(fightPropertyFormula); var fightPowerVariables = ExtractVariables(fightPowerFormula); var skillFightPowerVariables = ExtractVariables(skillFightPowerFormula); // 处理公式中的类型转换操作符 var processedPropertyFormula = ProcessTypeConversionOperators(propertyFormula); var processedFightPropertyFormula = ProcessTypeConversionOperators(fightPropertyFormula); var processedFightPowerFormula = ProcessTypeConversionOperators(fightPowerFormula); var processedSkillFightPowerFormula = ProcessTypeConversionOperators(skillFightPowerFormula); // 生成代码 var code = new System.Text.StringBuilder(); code.AppendLine("using System.Collections.Generic;"); code.AppendLine(); code.AppendLine("public class FightPowerFormula"); code.AppendLine("{"); // 添加公式注释 code.AppendLine(" // 基础属性公式"); code.AppendLine($" // {propertyFormula}"); code.AppendLine(" // 战斗属性公式"); code.AppendLine($" // {fightPropertyFormula}"); code.AppendLine(" // 战斗力公式"); code.AppendLine($" // {fightPowerFormula}"); code.AppendLine(" // 技能战斗力公式"); code.AppendLine($" // {skillFightPowerFormula}"); code.AppendLine(); // 生成基础属性计算方法 code.AppendLine(" public static double GetBaseAttr(Dictionary variables)"); code.AppendLine(" {"); foreach (var variable in propertyVariables) { code.AppendLine($" double {variable} = variables[\"{variable}\"];"); } code.AppendLine(); code.AppendLine($" return {processedPropertyFormula};"); code.AppendLine(" }"); code.AppendLine(); // 生成战斗属性计算方法 code.AppendLine(" public static double GetFightAttr(Dictionary variables)"); code.AppendLine(" {"); foreach (var variable in fightPropertyVariables) { code.AppendLine($" double {variable} = variables[\"{variable}\"];"); } code.AppendLine(); code.AppendLine($" return {processedFightPropertyFormula};"); code.AppendLine(" }"); code.AppendLine(); // 生成战斗力计算方法 code.AppendLine(" public static double GetFightPower(Dictionary variables)"); code.AppendLine(" {"); foreach (var variable in fightPowerVariables) { code.AppendLine($" double {variable} = variables[\"{variable}\"];"); } code.AppendLine(); code.AppendLine($" return {processedFightPowerFormula};"); code.AppendLine(" }"); code.AppendLine(); // 生成技能战斗力计算方法 code.AppendLine(" public static double GetSkillsFightPower(Dictionary variables)"); code.AppendLine(" {"); foreach (var variable in skillFightPowerVariables) { code.AppendLine($" double {variable} = variables[\"{variable}\"];"); } code.AppendLine(); code.AppendLine($" return {processedSkillFightPowerFormula};"); code.AppendLine(" }"); code.AppendLine(); code.AppendLine("}"); return code.ToString(); } private string ProcessTypeConversionOperators(string formula) { // 将类型转换操作符转换为加括号的形式 var processedFormula = formula; // 处理 long(expression) 转换为 (long)(expression) processedFormula = System.Text.RegularExpressions.Regex.Replace(processedFormula, @"\b(long|int|float|double)\s*\(", "($1)("); return processedFormula; } private List ExtractVariables(string formula) { var variables = new List(); var words = formula.Split(new char[] { ' ', '+', '-', '*', '/', '(', ')', ',', '=', '<', '>', '!', '&', '|', '%', ':' }, StringSplitOptions.RemoveEmptyEntries); foreach (var word in words) { // 过滤掉数字、运算符和关键字 if (!double.TryParse(word, out _) && !IsOperator(word) && !IsKeyword(word) && !variables.Contains(word)) { variables.Add(word); } } return variables; } private bool IsOperator(string word) { var operators = new string[] { "if", "else", "min", "max", "long", "int", "float", "double" }; return operators.Contains(word); } private bool IsKeyword(string word) { var keywords = new string[] { "return", "var", "class", "public", "private", "static", "void", "using", "namespace" }; return keywords.Contains(word); } #endif public override void Update() { if (done) { return; } timer += Time.deltaTime; // if (!ConfigInitiator.IsLoginConfigInited)\ // TODO YYL // if (!ConfigInitiator.done) // { // done = false; // progress = timer / duration; // } // else { done = true; } ExceptionReport(); } }