Skip to content

Commit 55358d4

Browse files
committed
Add TryGetExportUnsafe extension
1 parent 0b46a37 commit 55358d4

2 files changed

Lines changed: 129 additions & 7 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace Hi3Helper.Plugin.Core.Utility;
4+
5+
public static partial class PluginPInvokeExtension
6+
{
7+
[SkipLocalsInit]
8+
public static bool TryGetExportUnsafe(this nint handle, string exportName, out nint callbackP)
9+
=> TryGetExportAddressCore(handle, exportName, out callbackP);
10+
11+
[SkipLocalsInit]
12+
public static unsafe bool TryGetExportUnsafe(this nint handle, string exportName, out delegate* unmanaged[Cdecl]<void> delegateC)
13+
{
14+
delegateC = null;
15+
if (!TryGetExportAddressCore(handle, exportName, out nint callbackP))
16+
{
17+
return false;
18+
}
19+
20+
delegateC = (delegate* unmanaged[Cdecl]<void>)callbackP;
21+
return true;
22+
}
23+
24+
[SkipLocalsInit]
25+
public static unsafe bool TryGetExportUnsafe<TReturn>(this nint handle, string exportName, out delegate* unmanaged[Cdecl]<TReturn> delegateC)
26+
{
27+
delegateC = null;
28+
if (!TryGetExportAddressCore(handle, exportName, out nint callbackP))
29+
{
30+
return false;
31+
}
32+
33+
delegateC = (delegate* unmanaged[Cdecl]<TReturn>)callbackP;
34+
return true;
35+
}
36+
37+
[SkipLocalsInit]
38+
public static unsafe bool TryGetExportUnsafe<T1>(this nint handle, string exportName, out delegate* unmanaged[Cdecl]<T1, void> delegateC)
39+
where T1 : unmanaged, allows ref struct
40+
{
41+
delegateC = null;
42+
if (!TryGetExportAddressCore(handle, exportName, out nint callbackP))
43+
{
44+
return false;
45+
}
46+
47+
delegateC = (delegate* unmanaged[Cdecl]<T1, void>)callbackP;
48+
return true;
49+
}
50+
51+
[SkipLocalsInit]
52+
public static unsafe bool TryGetExportUnsafe<T1, TReturn>(this nint handle, string exportName, out delegate* unmanaged[Cdecl]<T1, TReturn> delegateC)
53+
where T1 : unmanaged, allows ref struct
54+
{
55+
delegateC = null;
56+
if (!TryGetExportAddressCore(handle, exportName, out nint callbackP))
57+
{
58+
return false;
59+
}
60+
61+
delegateC = (delegate* unmanaged[Cdecl]<T1, TReturn>)callbackP;
62+
return true;
63+
}
64+
65+
[SkipLocalsInit]
66+
public static unsafe bool TryGetExportUnsafe<T1, T2>(this nint handle, string exportName, out delegate* unmanaged[Cdecl]<T1, T2, void> delegateC)
67+
where T1 : unmanaged, allows ref struct
68+
where T2 : unmanaged, allows ref struct
69+
{
70+
delegateC = null;
71+
if (!TryGetExportAddressCore(handle, exportName, out nint callbackP))
72+
{
73+
return false;
74+
}
75+
76+
delegateC = (delegate* unmanaged[Cdecl]<T1, T2, void>)callbackP;
77+
return true;
78+
}
79+
80+
[SkipLocalsInit]
81+
public static unsafe bool TryGetExportUnsafe<T1, T2, TReturn>(this nint handle, string exportName, out delegate* unmanaged[Cdecl]<T1, T2, TReturn> delegateC)
82+
where T1 : unmanaged, allows ref struct
83+
where T2 : unmanaged, allows ref struct
84+
{
85+
delegateC = null;
86+
if (!TryGetExportAddressCore(handle, exportName, out nint callbackP))
87+
{
88+
return false;
89+
}
90+
91+
delegateC = (delegate* unmanaged[Cdecl]<T1, T2, TReturn>)callbackP;
92+
return true;
93+
}
94+
95+
[SkipLocalsInit]
96+
public static unsafe bool TryGetExportUnsafe<T1, T2, T3>(this nint handle, string exportName, out delegate* unmanaged[Cdecl]<T1, T2, T3, void> delegateC)
97+
where T1 : unmanaged, allows ref struct
98+
where T2 : unmanaged, allows ref struct
99+
where T3 : unmanaged, allows ref struct
100+
{
101+
delegateC = null;
102+
if (!TryGetExportAddressCore(handle, exportName, out nint callbackP))
103+
{
104+
return false;
105+
}
106+
107+
delegateC = (delegate* unmanaged[Cdecl]<T1, T2, T3, void>)callbackP;
108+
return true;
109+
}
110+
111+
[SkipLocalsInit]
112+
public static unsafe bool TryGetExportUnsafe<T1, T2, T3, TReturn>(this nint handle, string exportName, out delegate* unmanaged[Cdecl]<T1, T2, T3, TReturn> delegateC)
113+
where T1 : unmanaged, allows ref struct
114+
where T2 : unmanaged, allows ref struct
115+
where T3 : unmanaged, allows ref struct
116+
{
117+
delegateC = null;
118+
if (!TryGetExportAddressCore(handle, exportName, out nint callbackP))
119+
{
120+
return false;
121+
}
122+
123+
delegateC = (delegate* unmanaged[Cdecl]<T1, T2, T3, TReturn>)callbackP;
124+
return true;
125+
}
126+
}

Utility/PluginPInvokeExtension.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
namespace Hi3Helper.Plugin.Core.Utility;
66

7-
public static class PluginPInvokeExtension
7+
public static partial class PluginPInvokeExtension
88
{
9+
[SkipLocalsInit]
910
public static bool TryGetExport<T>(this nint handle, string exportName, out T callback)
1011
where T : Delegate
1112
{
@@ -16,12 +17,7 @@ public static bool TryGetExport<T>(this nint handle, string exportName, out T ca
1617
return true;
1718
}
1819

19-
public static bool TryGetExportUnsafe(this nint handle, string exportName, out nint callbackP)
20-
{
21-
Unsafe.SkipInit(out callbackP);
22-
return TryGetExportAddressCore(handle, exportName, out callbackP);
23-
}
24-
20+
[SkipLocalsInit]
2521
private static unsafe bool TryGetExportAddressCore(
2622
nint handle,
2723
string exportName,

0 commit comments

Comments
 (0)