Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NativeLibrary.cs
Go to the documentation of this file.
4
6
7public static class NativeLibrary
8{
10
11 internal static IntPtr LoadLibraryByName(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, bool throwOnError)
12 {
13 RuntimeAssembly assembly2 = (RuntimeAssembly)assembly;
14 return LoadByName(libraryName, new QCallAssembly(ref assembly2), searchPath.HasValue, (uint)searchPath.GetValueOrDefault(), throwOnError);
15 }
16
17 [DllImport("QCall", CharSet = CharSet.Unicode)]
18 internal static extern IntPtr LoadFromPath(string libraryName, bool throwOnError);
19
20 [DllImport("QCall", CharSet = CharSet.Unicode)]
21 internal static extern IntPtr LoadByName(string libraryName, QCallAssembly callingAssembly, bool hasDllImportSearchPathFlag, uint dllImportSearchPathFlag, bool throwOnError);
22
23 [DllImport("QCall", CharSet = CharSet.Unicode)]
24 internal static extern void FreeLib(IntPtr handle);
25
26 [DllImport("QCall", CharSet = CharSet.Unicode)]
27 internal static extern IntPtr GetSymbol(IntPtr handle, string symbolName, bool throwOnError);
28
29 public static IntPtr Load(string libraryPath)
30 {
31 if (libraryPath == null)
32 {
33 throw new ArgumentNullException("libraryPath");
34 }
35 return LoadFromPath(libraryPath, throwOnError: true);
36 }
37
38 public static bool TryLoad(string libraryPath, out IntPtr handle)
39 {
40 if (libraryPath == null)
41 {
42 throw new ArgumentNullException("libraryPath");
43 }
44 handle = LoadFromPath(libraryPath, throwOnError: false);
45 return handle != IntPtr.Zero;
46 }
47
48 public static IntPtr Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
49 {
50 if (libraryName == null)
51 {
52 throw new ArgumentNullException("libraryName");
53 }
54 if (assembly == null)
55 {
56 throw new ArgumentNullException("assembly");
57 }
58 if (!assembly.IsRuntimeImplemented())
59 {
61 }
62 return LoadLibraryByName(libraryName, assembly, searchPath, throwOnError: true);
63 }
64
65 public static bool TryLoad(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle)
66 {
67 if (libraryName == null)
68 {
69 throw new ArgumentNullException("libraryName");
70 }
71 if (assembly == null)
72 {
73 throw new ArgumentNullException("assembly");
74 }
75 if (!assembly.IsRuntimeImplemented())
76 {
78 }
79 handle = LoadLibraryByName(libraryName, assembly, searchPath, throwOnError: false);
80 return handle != IntPtr.Zero;
81 }
82
83 public static void Free(IntPtr handle)
84 {
85 if (!(handle == IntPtr.Zero))
86 {
88 }
89 }
90
91 public static IntPtr GetExport(IntPtr handle, string name)
92 {
93 if (handle == IntPtr.Zero)
94 {
95 throw new ArgumentNullException("handle");
96 }
97 if (name == null)
98 {
99 throw new ArgumentNullException("name");
100 }
101 return GetSymbol(handle, name, throwOnError: true);
102 }
103
104 public static bool TryGetExport(IntPtr handle, string name, out IntPtr address)
105 {
106 if (handle == IntPtr.Zero)
107 {
108 throw new ArgumentNullException("handle");
109 }
110 if (name == null)
111 {
112 throw new ArgumentNullException("name");
113 }
114 address = GetSymbol(handle, name, throwOnError: false);
115 return address != IntPtr.Zero;
116 }
117
118 public static void SetDllImportResolver(Assembly assembly, DllImportResolver resolver)
119 {
120 if (assembly == null)
121 {
122 throw new ArgumentNullException("assembly");
123 }
124 if (resolver == null)
125 {
126 throw new ArgumentNullException("resolver");
127 }
128 if (!assembly.IsRuntimeImplemented())
129 {
131 }
132 if (s_nativeDllResolveMap == null)
133 {
135 }
136 try
137 {
138 s_nativeDllResolveMap.Add(assembly, resolver);
139 }
140 catch (ArgumentException)
141 {
143 }
144 }
145
146 internal static IntPtr LoadLibraryCallbackStub(string libraryName, Assembly assembly, bool hasDllImportSearchPathFlags, uint dllImportSearchPathFlags)
147 {
148 if (s_nativeDllResolveMap == null)
149 {
150 return IntPtr.Zero;
151 }
152 if (!s_nativeDllResolveMap.TryGetValue(assembly, out var value))
153 {
154 return IntPtr.Zero;
155 }
156 return value(libraryName, assembly, hasDllImportSearchPathFlags ? new DllImportSearchPath?((DllImportSearchPath)dllImportSearchPathFlags) : null);
157 }
158}
static void SetDllImportResolver(Assembly assembly, DllImportResolver resolver)
static IntPtr LoadLibraryCallbackStub(string libraryName, Assembly assembly, bool hasDllImportSearchPathFlags, uint dllImportSearchPathFlags)
static IntPtr GetExport(IntPtr handle, string name)
static IntPtr Load(string libraryPath)
static IntPtr LoadFromPath(string libraryName, bool throwOnError)
static ConditionalWeakTable< Assembly, DllImportResolver > s_nativeDllResolveMap
static bool TryLoad(string libraryPath, out IntPtr handle)
static IntPtr GetSymbol(IntPtr handle, string symbolName, bool throwOnError)
static bool TryGetExport(IntPtr handle, string name, out IntPtr address)
static bool TryLoad(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, out IntPtr handle)
static IntPtr LoadLibraryByName(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, bool throwOnError)
static IntPtr Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
static IntPtr LoadByName(string libraryName, QCallAssembly callingAssembly, bool hasDllImportSearchPathFlag, uint dllImportSearchPathFlag, bool throwOnError)
static string Argument_MustBeRuntimeAssembly
Definition SR.cs:774
static string InvalidOperation_CannotRegisterSecondResolver
Definition SR.cs:1416
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
delegate IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
static readonly IntPtr Zero
Definition IntPtr.cs:18