Skip to content

Commit b301b44

Browse files
committed
[refactor] move ReversePInvokeWrap/Analyzer.cs to MethodBridge/MonoPInvokeCallbackAnalyzer.cs
[change] validate unsupported parameter type(.e.g string) in MonoPInvokeCallback signature when generate MethodBridge file
1 parent ddc3332 commit b301b44

6 files changed

Lines changed: 42 additions & 40 deletions

File tree

Editor/Commands/MethodBridgeGeneratorCommand.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using HybridCLR.Editor.ABI;
33
using HybridCLR.Editor.Meta;
44
using HybridCLR.Editor.MethodBridge;
5-
using HybridCLR.Editor.ReversePInvokeWrap;
65
using System;
76
using System.Collections.Generic;
87
using System.IO;
@@ -31,7 +30,7 @@ public static void CleanIl2CppBuildCache()
3130
Directory.Delete(il2cppBuildCachePath, true);
3231
}
3332

34-
private static void GenerateMethodBridgeCppFile(IReadOnlyCollection<GenericMethod> genericMethods, List<RawReversePInvokeMethodInfo> reversePInvokeMethods, IReadOnlyCollection<CallNativeMethodSignatureInfo> calliMethodSignatures, string tempFile, string outputFile)
33+
private static void GenerateMethodBridgeCppFile(IReadOnlyCollection<GenericMethod> genericMethods, List<RawMonoPInvokeCallbackMethodInfo> reversePInvokeMethods, IReadOnlyCollection<CallNativeMethodSignatureInfo> calliMethodSignatures, string tempFile, string outputFile)
3534
{
3635
string templateCode = File.ReadAllText(tempFile, Encoding.UTF8);
3736
var g = new Generator(new Generator.Options()
@@ -78,7 +77,7 @@ public static void GenerateMethodBridgeAndReversePInvokeWrapper(BuildTarget targ
7877
List<string> hotUpdateDlls = SettingsUtil.HotUpdateAssemblyNamesExcludePreserved;
7978
var cache = new AssemblyCache(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotUpdateDlls));
8079

81-
var reversePInvokeAnalyzer = new ReversePInvokeWrap.Analyzer(cache, hotUpdateDlls);
80+
var reversePInvokeAnalyzer = new MonoPInvokeCallbackAnalyzer(cache, hotUpdateDlls);
8281
reversePInvokeAnalyzer.Run();
8382

8483
var calliAnalyzer = new CalliAnalyzer(cache, hotUpdateDlls);

Editor/Meta/MetaUtil.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,29 @@ public static List<TypeSig> CreateDefaultGenericParams(ModuleDef module, int ge
189189
}
190190
return methodGenericParams;
191191
}
192+
193+
public static bool IsSupportedPInvokeTypeSig(TypeSig typeSig)
194+
{
195+
typeSig = typeSig.RemovePinnedAndModifiers();
196+
if (typeSig.IsByRef)
197+
{
198+
return true;
199+
}
200+
switch (typeSig.ElementType)
201+
{
202+
case ElementType.SZArray:
203+
case ElementType.Array:
204+
//case ElementType.Class:
205+
case ElementType.String:
206+
//case ElementType.Object:
207+
return false;
208+
default: return true;
209+
}
210+
}
211+
212+
public static bool IsSupportedPInvokeMethodSignature(MethodSig methodSig)
213+
{
214+
return IsSupportedPInvokeTypeSig(methodSig.RetType) && methodSig.Params.All(p => IsSupportedPInvokeTypeSig(p));
215+
}
192216
}
193217
}

Editor/MethodBridge/Generator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using dnlib.DotNet;
22
using HybridCLR.Editor.ABI;
33
using HybridCLR.Editor.Meta;
4-
using HybridCLR.Editor.ReversePInvokeWrap;
54
using HybridCLR.Editor.Template;
65
using System;
76
using System.Collections.Generic;
@@ -30,7 +29,7 @@ public class Options
3029

3130
public IReadOnlyCollection<GenericMethod> GenericMethods { get; set; }
3231

33-
public List<RawReversePInvokeMethodInfo> ReversePInvokeMethods { get; set; }
32+
public List<RawMonoPInvokeCallbackMethodInfo> ReversePInvokeMethods { get; set; }
3433

3534
public IReadOnlyCollection<CallNativeMethodSignatureInfo> CalliMethodSignatures { get; set; }
3635

@@ -59,7 +58,7 @@ private class CalliMethodInfo
5958

6059
private readonly List<GenericMethod> _genericMethods;
6160

62-
private readonly List<RawReversePInvokeMethodInfo> _originalReversePInvokeMethods;
61+
private readonly List<RawMonoPInvokeCallbackMethodInfo> _originalReversePInvokeMethods;
6362

6463
private readonly List<CallNativeMethodSignatureInfo> _originalCalliMethodSignatures;
6564

@@ -588,7 +587,7 @@ private static CallingConvention GetCallingConvention(MethodDef method)
588587
return (CallingConvention)conv;
589588
}
590589

591-
private List<ABIReversePInvokeMethodInfo> BuildABIMethods(List<RawReversePInvokeMethodInfo> rawMethods)
590+
private List<ABIReversePInvokeMethodInfo> BuildABIMethods(List<RawMonoPInvokeCallbackMethodInfo> rawMethods)
592591
{
593592
var methodsBySig = new Dictionary<string, ABIReversePInvokeMethodInfo>();
594593
foreach (var method in rawMethods)

Editor/ReversePInvokeWrap/Analyzer.cs renamed to Editor/MethodBridge/MonoPInvokeCallbackAnalyzer.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99
using CallingConvention = System.Runtime.InteropServices.CallingConvention;
1010
using UnityEngine;
1111

12-
namespace HybridCLR.Editor.ReversePInvokeWrap
12+
namespace HybridCLR.Editor.MethodBridge
1313
{
14-
public class RawReversePInvokeMethodInfo
14+
public class RawMonoPInvokeCallbackMethodInfo
1515
{
1616
public MethodDef Method { get; set; }
1717

1818
public CustomAttribute GenerationAttribute { get; set; }
1919
}
2020

21-
public class Analyzer
21+
public class MonoPInvokeCallbackAnalyzer
2222
{
2323

2424
private readonly List<ModuleDefMD> _rootModules = new List<ModuleDefMD>();
2525

26-
private readonly List<RawReversePInvokeMethodInfo> _reversePInvokeMethods = new List<RawReversePInvokeMethodInfo>();
26+
private readonly List<RawMonoPInvokeCallbackMethodInfo> _reversePInvokeMethods = new List<RawMonoPInvokeCallbackMethodInfo>();
2727

28-
public List<RawReversePInvokeMethodInfo> ReversePInvokeMethods => _reversePInvokeMethods;
28+
public List<RawMonoPInvokeCallbackMethodInfo> ReversePInvokeMethods => _reversePInvokeMethods;
2929

30-
public Analyzer(AssemblyCache cache, List<string> assemblyNames)
30+
public MonoPInvokeCallbackAnalyzer(AssemblyCache cache, List<string> assemblyNames)
3131
{
3232
foreach (var assemblyName in assemblyNames)
3333
{
@@ -39,7 +39,7 @@ private void CollectReversePInvokeMethods()
3939
{
4040
foreach (var mod in _rootModules)
4141
{
42-
Debug.Log($"ass:{mod.FullName} methodcount:{mod.Metadata.TablesStream.MethodTable.Rows}");
42+
Debug.Log($"ass:{mod.FullName} method count:{mod.Metadata.TablesStream.MethodTable.Rows}");
4343
for (uint rid = 1, n = mod.Metadata.TablesStream.MethodTable.Rows; rid <= n; rid++)
4444
{
4545
var method = mod.ResolveMethod(rid);
@@ -53,11 +53,15 @@ private void CollectReversePInvokeMethods()
5353
{
5454
continue;
5555
}
56+
if (!MetaUtil.IsSupportedPInvokeMethodSignature(method.MethodSig))
57+
{
58+
throw new Exception($"MonoPInvokeCallback method {method.FullName} has unsupported parameter or return type. Please check the method signature.");
59+
}
5660
//foreach (var ca in method.CustomAttributes)
5761
//{
5862
// Debug.Log($"{ca.AttributeType.FullName} {ca.TypeFullName}");
5963
//}
60-
_reversePInvokeMethods.Add(new RawReversePInvokeMethodInfo()
64+
_reversePInvokeMethods.Add(new RawMonoPInvokeCallbackMethodInfo()
6165
{
6266
Method = method,
6367
GenerationAttribute = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "HybridCLR.ReversePInvokeWrapperGenerationAttribute"),
File renamed without changes.

Editor/MethodBridge/PInvokeAnalyzer.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,6 @@ public PInvokeAnalyzer(AssemblyCache cache, List<string> assemblyNames)
2626
}
2727
}
2828

29-
private static bool IsSupportedPInvokeTypeSig(TypeSig typeSig)
30-
{
31-
typeSig = typeSig.RemovePinnedAndModifiers();
32-
if (typeSig.IsByRef)
33-
{
34-
return true;
35-
}
36-
switch (typeSig.ElementType)
37-
{
38-
case ElementType.SZArray:
39-
case ElementType.Array:
40-
//case ElementType.Class:
41-
case ElementType.String:
42-
//case ElementType.Object:
43-
return false;
44-
default: return true;
45-
}
46-
}
47-
48-
private static bool IsSupportedPInvokeMethodSignature(MethodSig methodSig)
49-
{
50-
return IsSupportedPInvokeTypeSig(methodSig.RetType) && methodSig.Params.All(p => IsSupportedPInvokeTypeSig(p));
51-
}
52-
5329
public void Run()
5430
{
5531
foreach (var mod in _rootModules)
@@ -60,7 +36,7 @@ public void Run()
6036
{
6137
if (method.IsPinvokeImpl)
6238
{
63-
if (!IsSupportedPInvokeMethodSignature(method.MethodSig))
39+
if (!MetaUtil.IsSupportedPInvokeMethodSignature(method.MethodSig))
6440
{
6541
throw new Exception($"PInvoke method {method.FullName} has unsupported parameter or return type. Please check the method signature.");
6642
}

0 commit comments

Comments
 (0)