Skip to content

Commit 45749ff

Browse files
committed
[new] add Managed2NativeFunctionPointer MethodBridge functions
1 parent b176d63 commit 45749ff

5 files changed

Lines changed: 230 additions & 28 deletions

File tree

Editor/Commands/MethodBridgeGeneratorCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void CleanIl2CppBuildCache()
3131
Directory.Delete(il2cppBuildCachePath, true);
3232
}
3333

34-
private static void GenerateMethodBridgeCppFile(IReadOnlyCollection<GenericMethod> genericMethods, List<RawReversePInvokeMethodInfo> reversePInvokeMethods, string outputFile)
34+
private static void GenerateMethodBridgeCppFile(IReadOnlyCollection<GenericMethod> genericMethods, List<RawReversePInvokeMethodInfo> reversePInvokeMethods, IReadOnlyCollection<RawCalliMethodSignatureInfo> calliMethodSignatures, string outputFile)
3535
{
3636
string templateCode = File.ReadAllText(outputFile, Encoding.UTF8);
3737
var g = new Generator(new Generator.Options()
@@ -40,6 +40,7 @@ private static void GenerateMethodBridgeCppFile(IReadOnlyCollection<GenericMetho
4040
OutputFile = outputFile,
4141
GenericMethods = genericMethods,
4242
ReversePInvokeMethods = reversePInvokeMethods,
43+
CalliMethodSignatures = calliMethodSignatures,
4344
Development = EditorUserBuildSettings.development,
4445
});
4546

@@ -80,9 +81,12 @@ public static void GenerateMethodBridgeAndReversePInvokeWrapper(BuildTarget targ
8081
var reversePInvokeAnalyzer = new ReversePInvokeWrap.Analyzer(cache, hotUpdateDlls);
8182
reversePInvokeAnalyzer.Run();
8283

84+
var calliAnalyzer = new CalliAnalyzer(cache, hotUpdateDlls);
85+
calliAnalyzer.Run();
86+
8387
string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.cpp";
8488

85-
GenerateMethodBridgeCppFile(methodBridgeAnalyzer.GenericMethods, reversePInvokeAnalyzer.ReversePInvokeMethods, outputFile);
89+
GenerateMethodBridgeCppFile(methodBridgeAnalyzer.GenericMethods, reversePInvokeAnalyzer.ReversePInvokeMethods, calliAnalyzer.CalliMethodSignatures, outputFile);
8690

8791
CleanIl2CppBuildCache();
8892
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using dnlib.DotNet;
2+
using HybridCLR.Editor.Meta;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using UnityEngine;
9+
10+
namespace HybridCLR.Editor.MethodBridge
11+
{
12+
13+
public class RawCalliMethodSignatureInfo
14+
{
15+
public MethodSig MethodSig { get; set; }
16+
}
17+
18+
public class CalliAnalyzer
19+
{
20+
private readonly List<ModuleDefMD> _rootModules = new List<ModuleDefMD>();
21+
22+
private readonly List<RawCalliMethodSignatureInfo> _calliMethodSignatures = new List<RawCalliMethodSignatureInfo>();
23+
24+
public List<RawCalliMethodSignatureInfo> CalliMethodSignatures => _calliMethodSignatures;
25+
26+
public CalliAnalyzer(AssemblyCache cache, List<string> assemblyNames)
27+
{
28+
foreach (var assemblyName in assemblyNames)
29+
{
30+
_rootModules.Add(cache.LoadModule(assemblyName));
31+
}
32+
}
33+
34+
private void CollectCalli()
35+
{
36+
foreach (var mod in _rootModules)
37+
{
38+
Debug.Log($"ass:{mod.FullName} methodcount:{mod.Metadata.TablesStream.MethodTable.Rows}");
39+
for (uint rid = 1, n = mod.Metadata.TablesStream.MethodTable.Rows; rid <= n; rid++)
40+
{
41+
var method = mod.ResolveMethod(rid);
42+
//Debug.Log($"method:{method}");
43+
if (!method.HasBody)
44+
{
45+
continue;
46+
}
47+
48+
foreach (var il in method.Body.Instructions)
49+
{
50+
if (il.OpCode.Code == dnlib.DotNet.Emit.Code.Calli)
51+
{
52+
MethodSig methodSig = (MethodSig)il.Operand;
53+
54+
_calliMethodSignatures.Add(new RawCalliMethodSignatureInfo()
55+
{
56+
MethodSig = methodSig,
57+
});
58+
Debug.Log($"method:{method} calli method signature:{methodSig}");
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
public void Run()
66+
{
67+
CollectCalli();
68+
}
69+
}
70+
}

Editor/MethodBridge/CalliAnalyzer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)