hch
1 天以前 a19acb609721b89419fe55785643a0d4f1959368
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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<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)
        {
            return;
        }
 
        timer += Time.deltaTime;
        // if (!ConfigInitiator.IsLoginConfigInited)\
        // TODO YYL
        // if (!ConfigInitiator.done)
        // {
        //     done = false;
        //     progress = timer / duration;
        // }
        // else
        {
            done = true;
        }
 
        ExceptionReport();
    }
 
}