-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeFunctionLoader.cs
More file actions
126 lines (110 loc) · 4.51 KB
/
NativeFunctionLoader.cs
File metadata and controls
126 lines (110 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace NativeFunctionLoader;
/// <summary>
/// This exception is thrown when a function fails to inject
/// <remarks>
/// <b>Authors:</b>Marlon Klaus<br></br>
/// <b>Created by:</b>Marlon Klaus<br></br>
/// <b>Created on:</b>10/27/2021<br></br>
/// <b>Since:</b> 1.0.0.0
/// </remarks>
/// </summary>
public sealed class NativeFunctionLoadingException : Exception {
private static string BuildExceptionMessage(string[] libraryNames, string functionName) {
var builder = new StringBuilder().Append(
$"Failed to load function {functionName} from one of this following libraries: ");
foreach (var libraryName in libraryNames) {
builder.Append($"{libraryName} ");
}
return builder.ToString();
}
internal NativeFunctionLoadingException(string[] libraryNames, string functionName) : base(
BuildExceptionMessage(libraryNames, functionName)) {
}
}
/// <summary>
/// This function loader can inject native functions into unmanaged delegate pointers
/// <remarks>
/// <b>Authors:</b>Marlon Klaus<br></br>
/// <b>Created by:</b>Marlon Klaus<br></br>
/// <b>Created on:</b>10/27/2021<br></br>
/// <b>Since:</b> 1.0.0.0
/// </remarks>
/// </summary>
public static class NativeFunctionLoader {
private static readonly Platforms CurrentPlatform = DetectPlatform();
private static readonly Dictionary<string, IntPtr> Libraries = new();
private static Platforms DetectPlatform() {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return Platforms.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return Platforms.Mac;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return Platforms.Linux;
if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD)) return Platforms.BSD;
throw new Exception("Unsupported platform");
}
private static IntPtr GetFunction(string[] libraryNames, string functionName) {
var library = IntPtr.Zero;
foreach (var libraryName in libraryNames) {
if (Libraries.TryGetValue(libraryName, out library)) break;
}
if (library == IntPtr.Zero) {
foreach (var libraryName in libraryNames) {
if (NativeLibrary.TryLoad(libraryName, out library)) {
Libraries.Add(libraryName, library);
break;
}
}
}
if (library == IntPtr.Zero) return IntPtr.Zero;
NativeLibrary.TryGetExport(library, functionName, out IntPtr function);
return function;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsValidField(FieldInfo field) {
return field.IsPublic && field.IsStatic && field.IsInitOnly;
}
private static void LoadFunction(FieldInfo field, NativeFunction attribute) {
if (!attribute.Platforms.HasFlag(CurrentPlatform)) return;
var functionName = attribute.CustomFunctionName ?? field.Name;
var function = GetFunction(attribute.LibraryNames, functionName);
if (function == IntPtr.Zero && attribute.HasToExist) {
throw new NativeFunctionLoadingException(attribute.LibraryNames, functionName);
}
field.SetValue(null, function);
}
/// <summary>
/// Injects native functions into all fields of the given type containing an NativeFunction attribute
/// This method has to be called from the static constructor of the given type
/// </summary>
/// <remarks>
/// <b>Authors:</b>Marlon Klaus<br></br>
/// <b>Created by:</b>Marlon Klaus<br></br>
/// <b>Created on:</b>10/27/2021<br></br>
/// <b>Since:</b> 1.0.0.0
/// </remarks>
/// <param name="type">The type</param>
public static void Load(Type type) {
foreach (var field in type.GetFields()) {
var attribute = field.GetCustomAttribute<NativeFunction>();
if (attribute == null) continue;
if (!IsValidField(field)) continue;
LoadFunction(field, attribute);
}
}
/// <summary>
/// Frees all native libraries
/// <remarks>
/// <b>Authors:</b>Marlon Klaus<br></br>
/// <b>Created by:</b>Marlon Klaus<br></br>
/// <b>Created on:</b>10/27/2021<br></br>
/// <b>Since:</b> 1.0.0.0
/// </remarks>
/// </summary>
public static void Free() {
foreach (var library in Libraries.Values) {
NativeLibrary.Free(library);
}
}
}