Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ModuleHandle.cs
Go to the documentation of this file.
7
8namespace System;
9
10public struct ModuleHandle
11{
12 public static readonly ModuleHandle EmptyHandle;
13
14 private readonly RuntimeModule m_ptr;
15
17
18 internal ModuleHandle(RuntimeModule module)
19 {
20 m_ptr = module;
21 }
22
24 {
25 return m_ptr;
26 }
27
28 public override int GetHashCode()
29 {
30 if (!(m_ptr != null))
31 {
32 return 0;
33 }
34 return m_ptr.GetHashCode();
35 }
36
37 public override bool Equals([NotNullWhen(true)] object? obj)
38 {
39 if (!(obj is ModuleHandle moduleHandle))
40 {
41 return false;
42 }
43 return moduleHandle.m_ptr == m_ptr;
44 }
45
47 {
48 return handle.m_ptr == m_ptr;
49 }
50
51 public static bool operator ==(ModuleHandle left, ModuleHandle right)
52 {
53 return left.Equals(right);
54 }
55
56 public static bool operator !=(ModuleHandle left, ModuleHandle right)
57 {
58 return !left.Equals(right);
59 }
60
61 [MethodImpl(MethodImplOptions.InternalCall)]
62 internal static extern IRuntimeMethodInfo GetDynamicMethod(DynamicMethod method, RuntimeModule module, string name, byte[] sig, Resolver resolver);
63
64 [MethodImpl(MethodImplOptions.InternalCall)]
65 internal static extern int GetToken(RuntimeModule module);
66
67 private static void ValidateModulePointer(RuntimeModule module)
68 {
69 if ((object)module == null)
70 {
71 ThrowInvalidOperationException();
72 }
73 [StackTraceHidden]
74 [DoesNotReturn]
75 static void ThrowInvalidOperationException()
76 {
78 }
79 }
80
81 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
83 {
84 return ResolveTypeHandle(typeToken);
85 }
86
87 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
89 {
90 return ResolveTypeHandle(typeToken, null, null);
91 }
92
93 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
94 public unsafe RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
95 {
98 IntPtr[] array = null;
99 int length = 0;
100 IntPtr[] array2 = null;
101 int length2 = 0;
102 if (typeInstantiationContext != null && typeInstantiationContext.Length != 0)
103 {
104 typeInstantiationContext = (RuntimeTypeHandle[])typeInstantiationContext.Clone();
105 array = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out length);
106 }
107 if (methodInstantiationContext != null && methodInstantiationContext.Length != 0)
108 {
109 methodInstantiationContext = (RuntimeTypeHandle[])methodInstantiationContext.Clone();
110 array2 = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out length2);
111 }
112 fixed (IntPtr* typeInstArgs = array)
113 {
114 fixed (IntPtr* methodInstArgs = array2)
115 {
116 try
117 {
118 RuntimeType o = null;
119 ResolveType(new QCallModule(ref module), typeToken, typeInstArgs, length, methodInstArgs, length2, ObjectHandleOnStack.Create(ref o));
120 GC.KeepAlive(typeInstantiationContext);
121 GC.KeepAlive(methodInstantiationContext);
122 return new RuntimeTypeHandle(o);
123 }
124 catch (Exception)
125 {
126 if (!GetMetadataImport(module).IsValidToken(typeToken))
127 {
128 throw new ArgumentOutOfRangeException("typeToken", SR.Format(SR.Argument_InvalidToken, typeToken, new ModuleHandle(module)));
129 }
130 throw;
131 }
132 }
133 }
134 }
135
136 [DllImport("QCall", CharSet = CharSet.Unicode)]
137 private unsafe static extern void ResolveType(QCallModule module, int typeToken, IntPtr* typeInstArgs, int typeInstCount, IntPtr* methodInstArgs, int methodInstCount, ObjectHandleOnStack type);
138
139 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
141 {
142 return ResolveMethodHandle(methodToken);
143 }
144
145 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
147 {
148 return ResolveMethodHandle(methodToken, null, null);
149 }
150
151 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
152 public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
153 {
154 RuntimeModule runtimeModule = GetRuntimeModule();
155 typeInstantiationContext = (RuntimeTypeHandle[])typeInstantiationContext?.Clone();
156 methodInstantiationContext = (RuntimeTypeHandle[])methodInstantiationContext?.Clone();
157 int length;
158 IntPtr[] typeInstantiationContext2 = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out length);
159 int length2;
160 IntPtr[] methodInstantiationContext2 = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out length2);
161 RuntimeMethodHandleInternal runtimeMethodHandleInternal = ResolveMethodHandleInternal(runtimeModule, methodToken, typeInstantiationContext2, length, methodInstantiationContext2, length2);
162 IRuntimeMethodInfo method = new RuntimeMethodInfoStub(runtimeMethodHandleInternal, RuntimeMethodHandle.GetLoaderAllocator(runtimeMethodHandleInternal));
163 GC.KeepAlive(typeInstantiationContext);
164 GC.KeepAlive(methodInstantiationContext);
165 return new RuntimeMethodHandle(method);
166 }
167
168 internal unsafe static RuntimeMethodHandleInternal ResolveMethodHandleInternal(RuntimeModule module, int methodToken, IntPtr[] typeInstantiationContext, int typeInstCount, IntPtr[] methodInstantiationContext, int methodInstCount)
169 {
170 ValidateModulePointer(module);
171 try
172 {
173 fixed (IntPtr* typeInstArgs = typeInstantiationContext)
174 {
175 fixed (IntPtr* methodInstArgs = methodInstantiationContext)
176 {
177 return ResolveMethod(new QCallModule(ref module), methodToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount);
178 }
179 }
180 }
181 catch (Exception)
182 {
183 if (!GetMetadataImport(module).IsValidToken(methodToken))
184 {
185 throw new ArgumentOutOfRangeException("methodToken", SR.Format(SR.Argument_InvalidToken, methodToken, new ModuleHandle(module)));
186 }
187 throw;
188 }
189 }
190
191 [DllImport("QCall", CharSet = CharSet.Unicode)]
192 private unsafe static extern RuntimeMethodHandleInternal ResolveMethod(QCallModule module, int methodToken, IntPtr* typeInstArgs, int typeInstCount, IntPtr* methodInstArgs, int methodInstCount);
193
194 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
196 {
197 return ResolveFieldHandle(fieldToken);
198 }
199
200 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
202 {
203 return ResolveFieldHandle(fieldToken, null, null);
204 }
205
206 [RequiresUnreferencedCode("Trimming changes metadata tokens")]
207 public unsafe RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
208 {
210 ValidateModulePointer(module);
211 IntPtr[] array = null;
212 int length = 0;
213 IntPtr[] array2 = null;
214 int length2 = 0;
215 if (typeInstantiationContext != null && typeInstantiationContext.Length != 0)
216 {
217 typeInstantiationContext = (RuntimeTypeHandle[])typeInstantiationContext.Clone();
218 array = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out length);
219 }
220 if (methodInstantiationContext != null && methodInstantiationContext.Length != 0)
221 {
222 methodInstantiationContext = (RuntimeTypeHandle[])methodInstantiationContext.Clone();
223 array2 = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out length2);
224 }
225 fixed (IntPtr* typeInstArgs = array)
226 {
227 fixed (IntPtr* methodInstArgs = array2)
228 {
229 try
230 {
231 IRuntimeFieldInfo o = null;
232 ResolveField(new QCallModule(ref module), fieldToken, typeInstArgs, length, methodInstArgs, length2, ObjectHandleOnStack.Create(ref o));
233 GC.KeepAlive(typeInstantiationContext);
234 GC.KeepAlive(methodInstantiationContext);
235 return new RuntimeFieldHandle(o);
236 }
237 catch (Exception)
238 {
239 if (!GetMetadataImport(module).IsValidToken(fieldToken))
240 {
241 throw new ArgumentOutOfRangeException("fieldToken", SR.Format(SR.Argument_InvalidToken, fieldToken, new ModuleHandle(module)));
242 }
243 throw;
244 }
245 }
246 }
247 }
248
249 [DllImport("QCall", CharSet = CharSet.Unicode)]
250 private unsafe static extern void ResolveField(QCallModule module, int fieldToken, IntPtr* typeInstArgs, int typeInstCount, IntPtr* methodInstArgs, int methodInstCount, ObjectHandleOnStack retField);
251
252 [DllImport("QCall", CharSet = CharSet.Unicode)]
253 private static extern Interop.BOOL _ContainsPropertyMatchingHash(QCallModule module, int propertyToken, uint hash);
254
255 internal static bool ContainsPropertyMatchingHash(RuntimeModule module, int propertyToken, uint hash)
256 {
257 return _ContainsPropertyMatchingHash(new QCallModule(ref module), propertyToken, hash) != Interop.BOOL.FALSE;
258 }
259
260 [DllImport("QCall", CharSet = CharSet.Unicode)]
262
264 {
265 RuntimeType o = null;
266 GetModuleType(new QCallModule(ref module), ObjectHandleOnStack.Create(ref o));
267 return o;
268 }
269
270 [DllImport("QCall", CharSet = CharSet.Unicode)]
271 private unsafe static extern void GetPEKind(QCallModule handle, int* peKind, int* machine);
272
273 internal unsafe static void GetPEKind(RuntimeModule module, out PortableExecutableKinds peKind, out ImageFileMachine machine)
274 {
275 System.Runtime.CompilerServices.Unsafe.SkipInit(out int num);
276 System.Runtime.CompilerServices.Unsafe.SkipInit(out int num2);
277 GetPEKind(new QCallModule(ref module), &num, &num2);
278 peKind = (PortableExecutableKinds)num;
279 machine = (ImageFileMachine)num2;
280 }
281
282 [MethodImpl(MethodImplOptions.InternalCall)]
283 internal static extern int GetMDStreamVersion(RuntimeModule module);
284
285 [MethodImpl(MethodImplOptions.InternalCall)]
286 private static extern IntPtr _GetMetadataImport(RuntimeModule module);
287
289 {
290 return new MetadataImport(_GetMetadataImport(module), module);
291 }
292}
static void KeepAlive(object? obj)
Definition GC.cs:180
Definition GC.cs:8
override int GetHashCode()
Definition Module.cs:316
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_InvalidToken
Definition SR.cs:744
static string InvalidOperation_NullModuleHandle
Definition SR.cs:1496
Definition SR.cs:7
static void ValidateModulePointer(RuntimeModule module)
static unsafe void GetPEKind(RuntimeModule module, out PortableExecutableKinds peKind, out ImageFileMachine machine)
static RuntimeType GetModuleType(RuntimeModule module)
RuntimeTypeHandle ResolveTypeHandle(int typeToken)
RuntimeModule GetRuntimeModule()
ModuleHandle(RuntimeModule module)
RuntimeFieldHandle ResolveFieldHandle(int fieldToken)
static void GetModuleType(QCallModule handle, ObjectHandleOnStack type)
static unsafe RuntimeMethodHandleInternal ResolveMethodHandleInternal(RuntimeModule module, int methodToken, IntPtr[] typeInstantiationContext, int typeInstCount, IntPtr[] methodInstantiationContext, int methodInstCount)
static bool operator!=(ModuleHandle left, ModuleHandle right)
override int GetHashCode()
override bool Equals([NotNullWhen(true)] object? obj)
static bool operator==(ModuleHandle left, ModuleHandle right)
RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken(int fieldToken)
static unsafe void ResolveType(QCallModule module, int typeToken, IntPtr *typeInstArgs, int typeInstCount, IntPtr *methodInstArgs, int methodInstCount, ObjectHandleOnStack type)
RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
unsafe RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
readonly RuntimeModule m_ptr
static unsafe RuntimeMethodHandleInternal ResolveMethod(QCallModule module, int methodToken, IntPtr *typeInstArgs, int typeInstCount, IntPtr *methodInstArgs, int methodInstCount)
static unsafe void GetPEKind(QCallModule handle, int *peKind, int *machine)
static readonly ModuleHandle EmptyHandle
static int GetToken(RuntimeModule module)
unsafe RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
static IRuntimeMethodInfo GetDynamicMethod(DynamicMethod method, RuntimeModule module, string name, byte[] sig, Resolver resolver)
static unsafe void ResolveField(QCallModule module, int fieldToken, IntPtr *typeInstArgs, int typeInstCount, IntPtr *methodInstArgs, int methodInstCount, ObjectHandleOnStack retField)
RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken(int typeToken)
bool Equals(ModuleHandle handle)
static bool ContainsPropertyMatchingHash(RuntimeModule module, int propertyToken, uint hash)
static MetadataImport GetMetadataImport(RuntimeModule module)
RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken(int methodToken)
static int GetMDStreamVersion(RuntimeModule module)
static IntPtr _GetMetadataImport(RuntimeModule module)
RuntimeMethodHandle ResolveMethodHandle(int methodToken)
static Interop.BOOL _ContainsPropertyMatchingHash(QCallModule module, int propertyToken, uint hash)
static LoaderAllocator GetLoaderAllocator(RuntimeMethodHandleInternal method)
static IntPtr[] CopyRuntimeTypeHandles(RuntimeTypeHandle[] inHandles, out int length)