mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
接入obfuz->2.0
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
using dnlib.DotNet;
|
||||
using HybridCLR.Editor.ABI;
|
||||
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.MethodBridge
|
||||
{
|
||||
|
||||
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)
|
||||
{
|
||||
if (gc == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lock(_lock)
|
||||
{
|
||||
gc = StandardizeClass(gc);
|
||||
if (_genericTypes.Add(gc))
|
||||
{
|
||||
WalkType(gc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private void OnNewMethod(MethodDef methodDef, List<TypeSig> klassGenericInst, List<TypeSig> methodGenericInst, GenericMethod method)
|
||||
{
|
||||
lock(_lock)
|
||||
{
|
||||
method = StandardizeMethod(method);
|
||||
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)
|
||||
{
|
||||
var gm = StandardizeMethod(new GenericMethod(method, gc.KlassInst, null));
|
||||
//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)
|
||||
{
|
||||
// 对于带泛型的参数,统一泛型共享为object
|
||||
var gm = StandardizeMethod(new GenericMethod(method, null, null));
|
||||
_genericMethods.Add(gm);
|
||||
}
|
||||
}
|
||||
|
||||
private void Prepare()
|
||||
{
|
||||
// 将所有非泛型函数全部加入函数列表,同时立马walk这些method。
|
||||
// 后续迭代中将只遍历MethodSpec
|
||||
foreach (var ass in _assemblyCollector.GetLoadedModules())
|
||||
{
|
||||
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);
|
||||
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();
|
||||
if (gm == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
gm = StandardizeMethod(gm);
|
||||
if (_genericMethods.Add(gm))
|
||||
{
|
||||
_newMethods.Add(gm);
|
||||
}
|
||||
}
|
||||
}
|
||||
Debug.Log($"PostPrepare allMethods:{_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();
|
||||
|
||||
Task.WaitAll(_processingMethods.Select(method => Task.Run(() =>
|
||||
{
|
||||
_methodReferenceAnalyzer.WalkMethod(method.Method, method.KlassInst, method.MethodInst);
|
||||
})).ToArray());
|
||||
Debug.Log($"iteration:[{i}] genericClass:{_genericTypes.Count} genericMethods:{_genericMethods.Count} newMethods:{_newMethods.Count}");
|
||||
}
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
Prepare();
|
||||
RecursiveCollect();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ee1ec106190e514489c7ba32bc7bc2e7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,9 @@
|
||||
using dnlib.DotNet;
|
||||
|
||||
namespace HybridCLR.Editor.MethodBridge
|
||||
{
|
||||
public class CallNativeMethodSignatureInfo
|
||||
{
|
||||
public MethodSig MethodSig { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2e4ca0a49975a84a8a72dbc70ec7795
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,65 @@
|
||||
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.MethodBridge
|
||||
{
|
||||
|
||||
public class CalliAnalyzer
|
||||
{
|
||||
private readonly List<ModuleDefMD> _rootModules = new List<ModuleDefMD>();
|
||||
|
||||
private readonly List<CallNativeMethodSignatureInfo> _calliMethodSignatures = new List<CallNativeMethodSignatureInfo>();
|
||||
|
||||
public List<CallNativeMethodSignatureInfo> CalliMethodSignatures => _calliMethodSignatures;
|
||||
|
||||
public CalliAnalyzer(AssemblyCache cache, List<string> assemblyNames)
|
||||
{
|
||||
foreach (var assemblyName in assemblyNames)
|
||||
{
|
||||
_rootModules.Add(cache.LoadModule(assemblyName));
|
||||
}
|
||||
}
|
||||
|
||||
private void CollectCalli()
|
||||
{
|
||||
foreach (var mod in _rootModules)
|
||||
{
|
||||
Debug.Log($"ass:{mod.FullName} methodcount:{mod.Metadata.TablesStream.MethodTable.Rows}");
|
||||
for (uint rid = 1, n = mod.Metadata.TablesStream.MethodTable.Rows; rid <= n; rid++)
|
||||
{
|
||||
var method = mod.ResolveMethod(rid);
|
||||
//Debug.Log($"method:{method}");
|
||||
if (!method.HasBody)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var il in method.Body.Instructions)
|
||||
{
|
||||
if (il.OpCode.Code == dnlib.DotNet.Emit.Code.Calli)
|
||||
{
|
||||
MethodSig methodSig = (MethodSig)il.Operand;
|
||||
|
||||
_calliMethodSignatures.Add(new CallNativeMethodSignatureInfo()
|
||||
{
|
||||
MethodSig = methodSig,
|
||||
});
|
||||
Debug.Log($"method:{method} calli method signature:{methodSig}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
CollectCalli();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ce33c8e48da5a649b261ba3a60fd3b9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e42a0f3bcbc5ddf438a85ae16c1b3116
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,78 @@
|
||||
using dnlib.DotNet;
|
||||
using HybridCLR.Editor.ABI;
|
||||
using HybridCLR.Editor.Meta;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CallingConvention = System.Runtime.InteropServices.CallingConvention;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HybridCLR.Editor.MethodBridge
|
||||
{
|
||||
public class RawMonoPInvokeCallbackMethodInfo
|
||||
{
|
||||
public MethodDef Method { get; set; }
|
||||
|
||||
public CustomAttribute GenerationAttribute { get; set; }
|
||||
}
|
||||
|
||||
public class MonoPInvokeCallbackAnalyzer
|
||||
{
|
||||
|
||||
private readonly List<ModuleDefMD> _rootModules = new List<ModuleDefMD>();
|
||||
|
||||
private readonly List<RawMonoPInvokeCallbackMethodInfo> _reversePInvokeMethods = new List<RawMonoPInvokeCallbackMethodInfo>();
|
||||
|
||||
public List<RawMonoPInvokeCallbackMethodInfo> ReversePInvokeMethods => _reversePInvokeMethods;
|
||||
|
||||
public MonoPInvokeCallbackAnalyzer(AssemblyCache cache, List<string> assemblyNames)
|
||||
{
|
||||
foreach (var assemblyName in assemblyNames)
|
||||
{
|
||||
_rootModules.Add(cache.LoadModule(assemblyName));
|
||||
}
|
||||
}
|
||||
|
||||
private void CollectReversePInvokeMethods()
|
||||
{
|
||||
foreach (var mod in _rootModules)
|
||||
{
|
||||
Debug.Log($"ass:{mod.FullName} method count:{mod.Metadata.TablesStream.MethodTable.Rows}");
|
||||
for (uint rid = 1, n = mod.Metadata.TablesStream.MethodTable.Rows; rid <= n; rid++)
|
||||
{
|
||||
var method = mod.ResolveMethod(rid);
|
||||
//Debug.Log($"method:{method}");
|
||||
if (!method.IsStatic || !method.HasCustomAttributes)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
CustomAttribute wa = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.Name == "MonoPInvokeCallbackAttribute");
|
||||
if (wa == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!MetaUtil.IsSupportedPInvokeMethodSignature(method.MethodSig))
|
||||
{
|
||||
Debug.LogError($"MonoPInvokeCallback method {method.FullName} has unsupported parameter or return type. Please check the method signature.");
|
||||
}
|
||||
//foreach (var ca in method.CustomAttributes)
|
||||
//{
|
||||
// Debug.Log($"{ca.AttributeType.FullName} {ca.TypeFullName}");
|
||||
//}
|
||||
_reversePInvokeMethods.Add(new RawMonoPInvokeCallbackMethodInfo()
|
||||
{
|
||||
Method = method,
|
||||
GenerationAttribute = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "HybridCLR.ReversePInvokeWrapperGenerationAttribute"),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
CollectReversePInvokeMethods();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c172068b408c0e349b2ceee4c4635085
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,50 @@
|
||||
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.MethodBridge
|
||||
{
|
||||
|
||||
public class PInvokeAnalyzer
|
||||
{
|
||||
private readonly List<ModuleDefMD> _rootModules = new List<ModuleDefMD>();
|
||||
|
||||
private readonly List<CallNativeMethodSignatureInfo> _pinvokeMethodSignatures = new List<CallNativeMethodSignatureInfo>();
|
||||
|
||||
public List<CallNativeMethodSignatureInfo> PInvokeMethodSignatures => _pinvokeMethodSignatures;
|
||||
|
||||
public PInvokeAnalyzer(AssemblyCache cache, List<string> assemblyNames)
|
||||
{
|
||||
foreach (var assemblyName in assemblyNames)
|
||||
{
|
||||
_rootModules.Add(cache.LoadModule(assemblyName));
|
||||
}
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
foreach (var mod in _rootModules)
|
||||
{
|
||||
foreach (TypeDef type in mod.GetTypes())
|
||||
{
|
||||
foreach (MethodDef method in type.Methods)
|
||||
{
|
||||
if (method.IsPinvokeImpl)
|
||||
{
|
||||
if (!MetaUtil.IsSupportedPInvokeMethodSignature(method.MethodSig))
|
||||
{
|
||||
Debug.LogError($"PInvoke method {method.FullName} has unsupported parameter or return type. Please check the method signature.");
|
||||
}
|
||||
_pinvokeMethodSignatures.Add(new CallNativeMethodSignatureInfo { MethodSig = method.MethodSig });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9923175c961b78849aeaf99708e294ce
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user