yyl
12 小时以前 71365e5c15d81759c04d7aab953fa757fb183f9b
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
using Jace;
using System;
using System.Collections.Generic;
 
public static class JaceCalculator
{
    private static readonly CalculationEngine Engine = new CalculationEngine();
    private static readonly Dictionary<string, double> _reusableVariables = new Dictionary<string, double>();
    private static readonly object _lockObject = new object();
    
    // 公式缓存,避免重复解析
    private static readonly Dictionary<string, Func<Dictionary<string, double>, double>> _formulaCache = 
        new Dictionary<string, Func<Dictionary<string, double>, double>>();
 
    /// <summary>
    /// 解析并计算数学表达式(优化版本,减少GC)
    /// </summary>
    /// <param name="formula">数学表达式字符串</param>
    /// <param name="variables">变量字典</param>
    /// <returns>计算结果</returns>
    public static double Calculate(string formula, Dictionary<string, double> variables)
    {
        if (string.IsNullOrEmpty(formula))
        {
            throw new ArgumentException("Formula cannot be null or empty.");
        }
 
        if (variables == null || variables.Count == 0)
        {
            throw new ArgumentException("Variables dictionary cannot be null or empty.");
        }
 
        try
        {
            // 使用缓存优化
            if (!_formulaCache.TryGetValue(formula, out var compiledFormula))
            {
                compiledFormula = Engine.Build(formula);
                _formulaCache[formula] = compiledFormula;
            }
            
            return compiledFormula(variables);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException($"Failed to calculate formula: {ex.Message}", ex);
        }
    }
 
    /// <summary>
    /// 预编译常用公式,减少运行时解析开销
    /// </summary>
    /// <param name="formulas">需要预编译的公式数组</param>
    public static void PrecompileFormulas(params string[] formulas)
    {
        if (formulas == null || formulas.Length == 0) return;
        
        lock (_lockObject)
        {
            foreach (var formula in formulas)
            {
                if (!string.IsNullOrEmpty(formula) && !_formulaCache.ContainsKey(formula))
                {
                    try
                    {
                        var compiledFormula = Engine.Build(formula);
                        _formulaCache[formula] = compiledFormula;
                    }
                    catch (Exception ex)
                    {
                        UnityEngine.Debug.LogWarning($"Failed to precompile formula '{formula}': {ex.Message}");
                    }
                }
            }
        }
    }
 
    /// <summary>
    /// 清空公式缓存(用于内存管理)
    /// </summary>
    public static void ClearCache()
    {
        lock (_lockObject)
        {
            _formulaCache.Clear();
        }
    }
 
    public static void Init()
    {
        Engine.AddFunction("int", (Func<double, double>)(x => (int)x));
        Engine.AddFunction("long", (Func<double, double>)(x => (long)x));
        
    }
}