From a19acb609721b89419fe55785643a0d4f1959368 Mon Sep 17 00:00:00 2001
From: hch <305670599@qq.com>
Date: 星期五, 14 十一月 2025 20:03:47 +0800
Subject: [PATCH] 0312 动态检测战力公式

---
 Main/Core/GameEngine/Launch/ConfigInitTask.cs |  155 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 155 insertions(+), 0 deletions(-)

diff --git a/Main/Core/GameEngine/Launch/ConfigInitTask.cs b/Main/Core/GameEngine/Launch/ConfigInitTask.cs
index 350a21b..1fe1122 100644
--- a/Main/Core/GameEngine/Launch/ConfigInitTask.cs
+++ b/Main/Core/GameEngine/Launch/ConfigInitTask.cs
@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
+using System.Linq;
 using UnityEngine;
 public class ConfigInitTask : LaunchTask
 {
@@ -30,8 +31,162 @@
         // 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);
+        
+        // 鐢熸垚鏂扮殑浠g爜鍐呭
+        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);
+        
+        // 鐢熸垚浠g爜
+        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<string, double> 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<string, double> 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<string, double> 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<string, double> 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<string> ExtractVariables(string formula)
+    {
+        var variables = new List<string>();
+        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)

--
Gitblit v1.8.0