2022-10-17 12:16:18 +08:00
|
|
|
|
using dnlib.DotNet;
|
2022-10-17 21:38:39 +08:00
|
|
|
|
using HybridCLR.Editor.ABI;
|
2022-10-17 12:16:18 +08:00
|
|
|
|
using HybridCLR.Editor.Meta;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-04-30 12:50:48 +08:00
|
|
|
|
using CallingConvention = System.Runtime.InteropServices.CallingConvention;
|
2022-10-17 12:16:18 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace HybridCLR.Editor.ReversePInvokeWrap
|
|
|
|
|
{
|
2022-10-17 21:38:39 +08:00
|
|
|
|
public class RawReversePInvokeMethodInfo
|
2022-10-17 12:16:18 +08:00
|
|
|
|
{
|
|
|
|
|
public MethodDef Method { get; set; }
|
|
|
|
|
|
|
|
|
|
public CustomAttribute GenerationAttribute { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 21:38:39 +08:00
|
|
|
|
public class ABIReversePInvokeMethodInfo
|
|
|
|
|
{
|
|
|
|
|
public MethodDesc Method { get; set; }
|
|
|
|
|
|
2024-04-30 12:50:48 +08:00
|
|
|
|
public CallingConvention Callvention { get; set; }
|
|
|
|
|
|
2022-10-17 21:38:39 +08:00
|
|
|
|
public int Count { get; set; }
|
2024-04-30 12:50:48 +08:00
|
|
|
|
|
|
|
|
|
public string Signature { get; set; }
|
2022-10-17 21:38:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 12:16:18 +08:00
|
|
|
|
public class Analyzer
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private readonly List<ModuleDefMD> _rootModules = new List<ModuleDefMD>();
|
|
|
|
|
|
2022-10-17 21:38:39 +08:00
|
|
|
|
private readonly List<RawReversePInvokeMethodInfo> _reversePInvokeMethods = new List<RawReversePInvokeMethodInfo>();
|
|
|
|
|
|
2024-05-26 11:59:54 +08:00
|
|
|
|
public List<RawReversePInvokeMethodInfo> ReversePInvokeMethods => _reversePInvokeMethods;
|
|
|
|
|
|
2022-10-17 12:16:18 +08:00
|
|
|
|
public Analyzer(AssemblyCache cache, List<string> assemblyNames)
|
|
|
|
|
{
|
2023-11-01 20:33:42 +08:00
|
|
|
|
foreach (var assemblyName in assemblyNames)
|
2022-10-17 12:16:18 +08:00
|
|
|
|
{
|
2023-11-01 20:33:42 +08:00
|
|
|
|
_rootModules.Add(cache.LoadModule(assemblyName));
|
2022-10-17 12:16:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 21:38:39 +08:00
|
|
|
|
private void CollectReversePInvokeMethods()
|
2022-10-17 12:16:18 +08:00
|
|
|
|
{
|
2022-10-17 21:38:39 +08:00
|
|
|
|
foreach (var mod in _rootModules)
|
2022-10-17 12:16:18 +08:00
|
|
|
|
{
|
|
|
|
|
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.IsStatic || !method.HasCustomAttributes)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-10-18 13:30:42 +08:00
|
|
|
|
CustomAttribute wa = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.Name == "MonoPInvokeCallbackAttribute");
|
2022-10-17 12:16:18 +08:00
|
|
|
|
if (wa == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//foreach (var ca in method.CustomAttributes)
|
|
|
|
|
//{
|
|
|
|
|
// Debug.Log($"{ca.AttributeType.FullName} {ca.TypeFullName}");
|
|
|
|
|
//}
|
2022-10-17 21:38:39 +08:00
|
|
|
|
_reversePInvokeMethods.Add(new RawReversePInvokeMethodInfo()
|
2022-10-17 12:16:18 +08:00
|
|
|
|
{
|
|
|
|
|
Method = method,
|
|
|
|
|
GenerationAttribute = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "HybridCLR.ReversePInvokeWrapperGenerationAttribute"),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-17 21:38:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
CollectReversePInvokeMethods();
|
2022-10-17 12:16:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|