2022-09-22 08:56:07 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2022-09-23 09:40:06 +08:00
|
|
|
|
namespace HybridCLR.Editor.MethodBridge
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class Analyzer
|
|
|
|
|
{
|
|
|
|
|
public class Options
|
|
|
|
|
{
|
|
|
|
|
public AssemblyReferenceDeepCollector Collector { get; set; }
|
|
|
|
|
|
|
|
|
|
public int MaxIterationCount { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly int _maxInterationCount;
|
|
|
|
|
private readonly AssemblyReferenceDeepCollector _assemblyCollector;
|
|
|
|
|
|
|
|
|
|
private readonly object _lock = new object();
|
|
|
|
|
|
|
|
|
|
private readonly List<TypeDef> _typeDefs = new List<TypeDef>();
|
|
|
|
|
|
|
|
|
|
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 IReadOnlyList<TypeDef> TypeDefs => _typeDefs;
|
|
|
|
|
|
|
|
|
|
public IReadOnlyCollection<GenericClass> GenericTypes => _genericTypes;
|
|
|
|
|
|
|
|
|
|
public IReadOnlyCollection<GenericMethod> GenericMethods => _genericMethods;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly MethodReferenceAnalyzer _methodReferenceAnalyzer;
|
|
|
|
|
|
|
|
|
|
public Analyzer(Options options)
|
|
|
|
|
{
|
|
|
|
|
_maxInterationCount = options.MaxIterationCount;
|
|
|
|
|
_assemblyCollector = options.Collector;
|
|
|
|
|
_methodReferenceAnalyzer = new MethodReferenceAnalyzer(this.OnNewMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TryAddAndWalkGenericType(GenericClass gc)
|
|
|
|
|
{
|
2022-10-19 11:43:56 +08:00
|
|
|
|
if (gc == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-09-22 08:56:07 +08:00
|
|
|
|
lock(_lock)
|
|
|
|
|
{
|
2023-08-22 11:16:31 +08:00
|
|
|
|
gc = StandardizeClass(gc);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
if (_genericTypes.Add(gc))
|
|
|
|
|
{
|
|
|
|
|
WalkType(gc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 11:16:31 +08:00
|
|
|
|
private GenericClass StandardizeClass(GenericClass gc)
|
|
|
|
|
{
|
|
|
|
|
TypeDef typeDef = gc.Type;
|
|
|
|
|
ICorLibTypes corLibTypes = typeDef.Module.CorLibTypes;
|
|
|
|
|
List<TypeSig> klassGenericParams = gc.KlassInst != null ? MetaUtil.ToShareTypeSigs(corLibTypes, gc.KlassInst) : (typeDef.GenericParameters.Count > 0 ? MetaUtil.CreateDefaultGenericParams(typeDef.Module, typeDef.GenericParameters.Count) : null);
|
|
|
|
|
return new GenericClass(typeDef, klassGenericParams);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GenericMethod StandardizeMethod(GenericMethod gm)
|
|
|
|
|
{
|
|
|
|
|
TypeDef typeDef = gm.Method.DeclaringType;
|
|
|
|
|
ICorLibTypes corLibTypes = typeDef.Module.CorLibTypes;
|
|
|
|
|
List<TypeSig> klassGenericParams = gm.KlassInst != null ? MetaUtil.ToShareTypeSigs(corLibTypes, gm.KlassInst) : (typeDef.GenericParameters.Count > 0 ? MetaUtil.CreateDefaultGenericParams(typeDef.Module, typeDef.GenericParameters.Count) : null);
|
|
|
|
|
List<TypeSig> methodGenericParams = gm.MethodInst != null ? MetaUtil.ToShareTypeSigs(corLibTypes, gm.MethodInst) : (gm.Method.GenericParameters.Count > 0 ? MetaUtil.CreateDefaultGenericParams(typeDef.Module, gm.Method.GenericParameters.Count) : null);
|
|
|
|
|
return new GenericMethod(gm.Method, klassGenericParams, methodGenericParams);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 11:47:17 +08:00
|
|
|
|
private void OnNewMethod(MethodDef methodDef, List<TypeSig> klassGenericInst, List<TypeSig> methodGenericInst, GenericMethod method)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
lock(_lock)
|
|
|
|
|
{
|
2023-08-22 11:16:31 +08:00
|
|
|
|
method = StandardizeMethod(method);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
if (_genericMethods.Add(method))
|
|
|
|
|
{
|
|
|
|
|
_newMethods.Add(method);
|
|
|
|
|
}
|
|
|
|
|
if (method.KlassInst != null)
|
|
|
|
|
{
|
|
|
|
|
TryAddAndWalkGenericType(new GenericClass(method.Method.DeclaringType, method.KlassInst));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-08-22 11:16:31 +08:00
|
|
|
|
var gm = StandardizeMethod(new GenericMethod(method, gc.KlassInst, null));
|
2022-09-22 08:56:07 +08:00
|
|
|
|
//Debug.Log($"add method:{gm.Method} {gm.KlassInst}");
|
|
|
|
|
|
|
|
|
|
if (_genericMethods.Add(gm))
|
|
|
|
|
{
|
|
|
|
|
if (method.HasBody && method.Body.Instructions != null)
|
|
|
|
|
{
|
|
|
|
|
_newMethods.Add(gm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WalkType(TypeDef typeDef)
|
|
|
|
|
{
|
|
|
|
|
_typeDefs.Add(typeDef);
|
|
|
|
|
ITypeDefOrRef baseType = typeDef.BaseType;
|
|
|
|
|
if (baseType != null && baseType.TryGetGenericInstSig() != null)
|
|
|
|
|
{
|
|
|
|
|
GenericClass gc = GenericClass.ResolveClass((TypeSpec)baseType, null);
|
|
|
|
|
TryAddAndWalkGenericType(gc);
|
|
|
|
|
}
|
|
|
|
|
foreach (var method in typeDef.Methods)
|
|
|
|
|
{
|
2022-10-14 14:47:27 +08:00
|
|
|
|
// 对于带泛型的参数,统一泛型共享为object
|
2023-08-22 11:16:31 +08:00
|
|
|
|
var gm = StandardizeMethod(new GenericMethod(method, null, null));
|
|
|
|
|
_genericMethods.Add(gm);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Prepare()
|
|
|
|
|
{
|
|
|
|
|
// 将所有非泛型函数全部加入函数列表,同时立马walk这些method。
|
|
|
|
|
// 后续迭代中将只遍历MethodSpec
|
2023-10-07 21:59:56 +08:00
|
|
|
|
foreach (var ass in _assemblyCollector.GetLoadedModules())
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
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);
|
2023-08-22 11:16:31 +08:00
|
|
|
|
var cs = GenericClass.ResolveClass(ts, null);
|
2023-07-12 23:09:58 +08:00
|
|
|
|
if (cs != null)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2023-07-12 23:09:58 +08:00
|
|
|
|
TryAddAndWalkGenericType(cs);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
if (gm == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-08-22 11:16:31 +08:00
|
|
|
|
gm = StandardizeMethod(gm);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
if (_genericMethods.Add(gm))
|
|
|
|
|
{
|
|
|
|
|
_newMethods.Add(gm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-22 11:16:31 +08:00
|
|
|
|
Debug.Log($"PostPrepare allMethods:{_genericMethods.Count} newMethods:{_newMethods.Count}");
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RecursiveCollect()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _maxInterationCount && _newMethods.Count > 0; i++)
|
|
|
|
|
{
|
|
|
|
|
var temp = _processingMethods;
|
|
|
|
|
_processingMethods = _newMethods;
|
|
|
|
|
_newMethods = temp;
|
|
|
|
|
_newMethods.Clear();
|
|
|
|
|
|
2022-10-14 14:47:27 +08:00
|
|
|
|
Task.WaitAll(_processingMethods.Select(method => Task.Run(() =>
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
_methodReferenceAnalyzer.WalkMethod(method.Method, method.KlassInst, method.MethodInst);
|
2022-10-14 14:47:27 +08:00
|
|
|
|
})).ToArray());
|
2023-08-22 11:16:31 +08:00
|
|
|
|
Debug.Log($"iteration:[{i}] genericClass:{_genericTypes.Count} genericMethods:{_genericMethods.Count} newMethods:{_newMethods.Count}");
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
Prepare();
|
|
|
|
|
RecursiveCollect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|