Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ComponentActivator.cs
Go to the documentation of this file.
1using System;
7
9
10public static class ComponentActivator
11{
13
15
17
18 private static bool IsSupported { get; } = InitializeIsSupported();
19
20
21 private static bool InitializeIsSupported()
22 {
23 if (!AppContext.TryGetSwitch("System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting", out var isEnabled))
24 {
25 return true;
26 }
27 return isEnabled;
28 }
29
30 private static string MarshalToString(IntPtr arg, string argName)
31 {
33 if (text == null)
34 {
36 }
37 return text;
38 }
39
40 [RequiresUnreferencedCode("Native hosting is not trim compatible and this warning will be seen if trimming is enabled.", Url = "https://aka.ms/dotnet-illink/nativehost")]
43 {
44 if (!IsSupported)
45 {
46 return -2147450713;
47 }
48 try
49 {
50 string assemblyPath = MarshalToString(assemblyPathNative, "assemblyPathNative");
51 string typeName = MarshalToString(typeNameNative, "typeNameNative");
52 string methodName = MarshalToString(methodNameNative, "methodNameNative");
53 if (reserved != IntPtr.Zero)
54 {
55 throw new ArgumentOutOfRangeException("reserved");
56 }
58 {
59 throw new ArgumentNullException("functionHandle");
60 }
63 }
64 catch (Exception ex)
65 {
66 return ex.HResult;
67 }
68 return 0;
69 }
70
73 {
74 if (!IsSupported)
75 {
76 return -2147450713;
77 }
78 try
79 {
80 string typeName = MarshalToString(typeNameNative, "typeNameNative");
81 string methodName = MarshalToString(methodNameNative, "methodNameNative");
82 if (loadContext != IntPtr.Zero)
83 {
84 throw new ArgumentOutOfRangeException("loadContext");
85 }
86 if (reserved != IntPtr.Zero)
87 {
88 throw new ArgumentOutOfRangeException("reserved");
89 }
91 {
92 throw new ArgumentNullException("functionHandle");
93 }
95 }
96 catch (Exception ex)
97 {
98 return ex.HResult;
99 }
100 return 0;
101 }
102
103 [RequiresUnreferencedCode("Native hosting is not trim compatible and this warning will be seen if trimming is enabled.", Url = "https://aka.ms/dotnet-illink/nativehost")]
117
118 [RequiresUnreferencedCode("Native hosting is not trim compatible and this warning will be seen if trimming is enabled.", Url = "https://aka.ms/dotnet-illink/nativehost")]
120 {
121 Func<AssemblyName, Assembly> assemblyResolver = (AssemblyName name) => alc.LoadFromAssemblyName(name);
122 Type type;
124 {
126 }
127 else if (delegateTypeNative == (IntPtr)(-1))
128 {
129 type = null;
130 }
131 else
132 {
133 string typeName2 = MarshalToString(delegateTypeNative, "delegateTypeNative");
135 }
136 Type type2 = Type.GetType(typeName, assemblyResolver, null, throwOnError: true);
138 if (type == null)
139 {
140 BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
142 if (method == null)
143 {
144 throw new MissingMethodException(typeName, methodName);
145 }
146 if (method.GetCustomAttribute<UnmanagedCallersOnlyAttribute>() == null)
147 {
149 }
150 intPtr = method.MethodHandle.GetFunctionPointer();
151 }
152 else
153 {
157 {
159 }
160 }
161 return intPtr;
162 }
163}
static IsolatedComponentLoadContext GetIsolatedComponentLoadContext(string assemblyPath)
static IntPtr InternalGetFunctionPointer(AssemblyLoadContext alc, string typeName, string methodName, IntPtr delegateTypeNative)
static unsafe int LoadAssemblyAndGetFunctionPointer(IntPtr assemblyPathNative, IntPtr typeNameNative, IntPtr methodNameNative, IntPtr delegateTypeNative, IntPtr reserved, IntPtr functionHandle)
static readonly Dictionary< string, IsolatedComponentLoadContext > s_assemblyLoadContexts
static readonly Dictionary< IntPtr, Delegate > s_delegates
static string MarshalToString(IntPtr arg, string argName)
static unsafe int GetFunctionPointer(IntPtr typeNameNative, IntPtr methodNameNative, IntPtr delegateTypeNative, IntPtr loadContext, IntPtr reserved, IntPtr functionHandle)
delegate int ComponentEntryPoint(IntPtr args, int sizeBytes)
static bool TryGetSwitch(string switchName, out bool isEnabled)
Definition AppContext.cs:74
static ? Delegate CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure)
Definition Delegate.cs:152
static ? string PtrToStringAuto(IntPtr ptr, int len)
Definition Marshal.cs:1572
static IntPtr GetFunctionPointerForDelegate(Delegate d)
Definition Marshal.cs:1457
static string InvalidOperation_FunctionMissingUnmanagedCallersOnly
Definition SR.cs:1690
Definition SR.cs:7
static StringComparer InvariantCulture
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
static readonly IntPtr Zero
Definition IntPtr.cs:18