三国卡牌客户端基础资源仓库
hch
2025-09-11 9e1075c83ce5dace7adce242083788bdffdf5d0c
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using dnlib.DotNet;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
 
namespace HybridCLR.Editor.AOT
{
 
    public class Analyzer
    {
        public class Options
        {
            public AssemblyReferenceDeepCollector Collector { get; set; }
 
            public int MaxIterationCount { get; set; }
 
            public bool ComputeAotAssembly { get; set; }
        }
 
        private readonly int _maxInterationCount;
 
        private readonly AssemblyReferenceDeepCollector _assemblyCollector;
 
        private readonly bool _computeAotAssembly;
 
        private readonly HashSet<GenericClass> _genericTypes = new HashSet<GenericClass>();
        private readonly HashSet<GenericMethod> _genericMethods = new HashSet<GenericMethod>();
 
        private List<GenericMethod> _processingMethods = new List<GenericMethod>();
        private List<GenericMethod> _newMethods = new List<GenericMethod>();
 
        public IReadOnlyCollection<GenericClass> GenericTypes => _genericTypes;
 
        public IReadOnlyCollection<GenericMethod> GenericMethods => _genericMethods;
 
        private readonly MethodReferenceAnalyzer _methodReferenceAnalyzer;
 
        private readonly HashSet<string> _hotUpdateAssemblyFiles;
 
        public ConstraintContext ConstraintContext { get; } = new ConstraintContext();
 
        public List<GenericClass> AotGenericTypes { get; } = new List<GenericClass>();
 
        public List<GenericMethod> AotGenericMethods { get; } = new List<GenericMethod>();
 
        public Analyzer(Options options)
        {
            _assemblyCollector = options.Collector;
            _maxInterationCount = options.MaxIterationCount;
            _computeAotAssembly = options.ComputeAotAssembly;
            _methodReferenceAnalyzer = new MethodReferenceAnalyzer(this.OnNewMethod);
            _hotUpdateAssemblyFiles = new HashSet<string>(options.Collector.GetRootAssemblyNames().Select(assName => assName + ".dll"));
        }
 
        private void TryAddAndWalkGenericType(GenericClass gc)
        {
            if (gc == null)
            {
                return;
            }
            gc = gc.ToGenericShare();
            if (_genericTypes.Add(gc) && NeedWalk(null, gc.Type))
            {
                WalkType(gc);
            }
        }
 
        private bool NeedWalk(MethodDef callFrom, TypeDef type)
        {
            return _hotUpdateAssemblyFiles.Contains(type.Module.Name) || callFrom == null || callFrom.HasGenericParameters;
        }
 
        private bool IsAotType(TypeDef type)
        {
            return _computeAotAssembly || !_hotUpdateAssemblyFiles.Contains(type.Module.Name);
        }
 
        private bool IsAotGenericMethod(MethodDef method)
        {
            return IsAotType(method.DeclaringType) && method.HasGenericParameters;
        }
 
        private void OnNewMethod(MethodDef methodDef, List<TypeSig> klassGenericInst, List<TypeSig> methodGenericInst, GenericMethod method)
        {
            if(method == null)
            {
                return;
            }
            if (NeedWalk(methodDef, method.Method.DeclaringType) && _genericMethods.Add(method))
            {
                _newMethods.Add(method);
            }
            if (method.KlassInst != null)
            {
                TryAddAndWalkGenericType(new GenericClass(method.Method.DeclaringType, method.KlassInst));
            }
        }
 
        private void TryAddMethodNotWalkType(GenericMethod method)
        {
            if (method == null)
            {
                return;
            }
            if (NeedWalk(null, method.Method.DeclaringType) && _genericMethods.Add(method))
            {
                _newMethods.Add(method);
            }
        }
 
        private void WalkType(GenericClass gc)
        {
            //Debug.Log($"typespec:{sig} {sig.GenericType} {sig.GenericType.TypeDefOrRef.ResolveTypeDef()}");
            //Debug.Log($"== walk generic type:{new GenericInstSig(gc.Type.ToTypeSig().ToClassOrValueTypeSig(), gc.KlassInst)}");
            ITypeDefOrRef baseType = gc.Type.BaseType;
            if (baseType != null && baseType.TryGetGenericInstSig() != null)
            {
                GenericClass parentType = GenericClass.ResolveClass((TypeSpec)baseType, new GenericArgumentContext(gc.KlassInst, null));
                TryAddAndWalkGenericType(parentType);
            }
            foreach (var method in gc.Type.Methods)
            {
                if (method.HasGenericParameters || !method.HasBody || method.Body.Instructions == null)
                {
                    continue;
                }
                var gm = new GenericMethod(method, gc.KlassInst, null).ToGenericShare();
                //Debug.Log($"add method:{gm.Method} {gm.KlassInst}");
                TryAddMethodNotWalkType(gm);
            }
        }
 
        private void WalkType(TypeDef typeDef)
        {
            if (typeDef.HasGenericParameters)
            {
                return;
            }
            ITypeDefOrRef baseType = typeDef.BaseType;
            if (baseType != null && baseType.TryGetGenericInstSig() != null)
            {
                GenericClass gc = GenericClass.ResolveClass((TypeSpec)baseType, null);
                TryAddAndWalkGenericType(gc);
            }
        }
 
        private void Prepare()
        {
            // 将所有非泛型函数全部加入函数列表,同时立马walk这些method。
            // 后续迭代中将只遍历MethodSpec
            foreach (var ass in _assemblyCollector.GetLoadedModulesOfRootAssemblies())
            {
                foreach (TypeDef typeDef in ass.GetTypes())
                {
                    WalkType(typeDef);
                }
 
                for (uint rid = 1, n = ass.Metadata.TablesStream.TypeSpecTable.Rows; rid <= n; rid++)
                {
                    var ts = ass.ResolveTypeSpec(rid);
                    var cs = GenericClass.ResolveClass(ts, null)?.ToGenericShare();
                    if (cs != null)
                    {
                        TryAddAndWalkGenericType(cs);
                    }
                }
 
                for (uint rid = 1, n = ass.Metadata.TablesStream.MethodSpecTable.Rows; rid <= n; rid++)
                {
                    var ms = ass.ResolveMethodSpec(rid);
                    var gm = GenericMethod.ResolveMethod(ms, null)?.ToGenericShare();
                    TryAddMethodNotWalkType(gm);
                }
            }
            Debug.Log($"PostPrepare genericTypes:{_genericTypes.Count} genericMethods:{_genericMethods.Count} newMethods:{_newMethods.Count}");
        }
 
        private void RecursiveCollect()
        {
            for (int i = 0; i < _maxInterationCount && _newMethods.Count > 0; i++)
            {
                var temp = _processingMethods;
                _processingMethods = _newMethods;
                _newMethods = temp;
                _newMethods.Clear();
 
                foreach (var method in _processingMethods)
                {
                    _methodReferenceAnalyzer.WalkMethod(method.Method, method.KlassInst, method.MethodInst);
                }
                Debug.Log($"iteration:[{i}] genericClass:{_genericTypes.Count} genericMethods:{_genericMethods.Count} newMethods:{_newMethods.Count}");
            }
        }
 
        private bool IsNotShareableAOTGenericType(TypeDef typeDef)
        {
            if (!IsAotType(typeDef))
            {
                return false;
            }
            return typeDef.GenericParameters.Any(c => !c.HasReferenceTypeConstraint);
        }
 
        private bool IsNotShareableAOTGenericMethod(MethodDef method)
        {
            if (!IsAotGenericMethod(method))
            {
                return false;
            }
            return method.GenericParameters.Concat(method.DeclaringType.GenericParameters).Any(c => !c.HasReferenceTypeConstraint);
        }
 
        private void FilterAOTGenericTypeAndMethods()
        {
            ConstraintContext cc = this.ConstraintContext;
            AotGenericTypes.AddRange(_genericTypes.Where(type => IsNotShareableAOTGenericType(type.Type)).Select(gc => cc.ApplyConstraints(gc)));
            AotGenericMethods.AddRange(_genericMethods.Where(method => IsNotShareableAOTGenericMethod(method.Method)).Select(gm => cc.ApplyConstraints(gm)));
        }
 
        public void Run()
        {
            Prepare();
            RecursiveCollect();
            FilterAOTGenericTypeAndMethods();
        }
    }
}