Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
LicenseInteropProxy.cs
Go to the documentation of this file.
1using System;
5
7
8internal sealed class LicenseInteropProxy
9{
10 private static readonly Type s_licenseAttrType = Type.GetType("System.ComponentModel.LicenseProviderAttribute, System.ComponentModel.TypeConverter", throwOnError: false);
11
12 private static readonly Type s_licenseExceptionType = Type.GetType("System.ComponentModel.LicenseException, System.ComponentModel.TypeConverter", throwOnError: false);
13
15
17
19
21
23
25
26 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicMethods)]
27 private readonly Type _licInfoHelper;
28
30
31 private object _licContext;
32
34
35 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2111:ReflectionToDynamicallyAccessedMembers", Justification = "The type parameter to LicenseManager.CreateWithContext method has PublicConstructors annotation. We only invoke this methodfrom AllocateAndValidateLicense which annotates the value passed in with the same annotation.")]
37 {
38 Type type = Type.GetType("System.ComponentModel.LicenseManager, System.ComponentModel.TypeConverter", throwOnError: true);
39 Type type2 = Type.GetType("System.ComponentModel.LicenseContext, System.ComponentModel.TypeConverter", throwOnError: true);
40 _setSavedLicenseKey = type2.GetMethod("SetSavedLicenseKey", BindingFlags.Instance | BindingFlags.Public);
41 _createWithContext = type.GetMethod("CreateWithContext", new Type[2]
42 {
43 typeof(Type),
44 type2
45 });
46 Type nestedType = type.GetNestedType("LicenseInteropHelper", BindingFlags.NonPublic);
47 _validateTypeAndReturnDetails = nestedType.GetMethod("ValidateAndRetrieveLicenseDetails", BindingFlags.Static | BindingFlags.Public);
48 _getCurrentContextInfo = nestedType.GetMethod("GetCurrentContextInfo", BindingFlags.Static | BindingFlags.Public);
49 Type nestedType2 = type.GetNestedType("CLRLicenseContext", BindingFlags.NonPublic);
50 _createDesignContext = nestedType2.GetMethod("CreateDesignContext", BindingFlags.Static | BindingFlags.Public);
51 _createRuntimeContext = nestedType2.GetMethod("CreateRuntimeContext", BindingFlags.Static | BindingFlags.Public);
52 _licInfoHelper = type.GetNestedType("LicInfoHelperLicenseContext", BindingFlags.NonPublic);
54 }
55
56 public static object Create()
57 {
58 return new LicenseInteropProxy();
59 }
60
61 public static bool HasLicense(Type type)
62 {
63 if (s_licenseAttrType == null)
64 {
65 return false;
66 }
67 return type.IsDefined(s_licenseAttrType, inherit: true);
68 }
69
70 public void GetLicInfo(Type type, out bool runtimeKeyAvail, out bool licVerified)
71 {
72 runtimeKeyAvail = false;
73 licVerified = false;
75 object[] array = new object[4] { obj, type, null, null };
76 if ((bool)_validateTypeAndReturnDetails.Invoke(null, BindingFlags.DoNotWrapExceptions, null, array, null))
77 {
78 IDisposable disposable = (IDisposable)array[2];
79 if (disposable != null)
80 {
81 disposable.Dispose();
82 licVerified = true;
83 }
84 array = new object[1] { type.AssemblyQualifiedName };
85 runtimeKeyAvail = (bool)_licInfoHelperContains.Invoke(obj, BindingFlags.DoNotWrapExceptions, null, array, null);
86 }
87 }
88
89 public string RequestLicKey(Type type)
90 {
91 object[] array = new object[4] { null, type, null, null };
92 if (!(bool)_validateTypeAndReturnDetails.Invoke(null, BindingFlags.DoNotWrapExceptions, null, array, null))
93 {
94 throw new COMException();
95 }
96 ((IDisposable)array[2])?.Dispose();
97 string text = (string)array[3];
98 if (text == null)
99 {
100 throw new COMException();
101 }
102 return text;
103 }
104
105 public object AllocateAndValidateLicense([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, string key, bool isDesignTime)
106 {
107 object obj;
108 if (isDesignTime)
109 {
110 object[] parameters = new object[1] { type };
111 obj = _createDesignContext.Invoke(null, BindingFlags.DoNotWrapExceptions, null, parameters, null);
112 }
113 else
114 {
115 object[] parameters = new object[2] { type, key };
116 obj = _createRuntimeContext.Invoke(null, BindingFlags.DoNotWrapExceptions, null, parameters, null);
117 }
118 try
119 {
120 object[] parameters = new object[2] { type, obj };
121 return _createWithContext.Invoke(null, BindingFlags.DoNotWrapExceptions, null, parameters, null);
122 }
123 catch (Exception ex) when (ex.GetType() == s_licenseExceptionType)
124 {
125 throw new COMException(ex.Message, -2147221230);
126 }
127 }
128
129 public void GetCurrentContextInfo(RuntimeTypeHandle rth, out bool isDesignTime, out IntPtr bstrKey)
130 {
131 Type typeFromHandle = Type.GetTypeFromHandle(rth);
132 object[] array = new object[3] { typeFromHandle, null, null };
133 _licContext = _getCurrentContextInfo.Invoke(null, BindingFlags.DoNotWrapExceptions, null, array, null);
134 _targetRcwType = typeFromHandle;
135 isDesignTime = (bool)array[1];
136 bstrKey = Marshal.StringToBSTR((string)array[2]);
137 }
138
139 public void SaveKeyInCurrentContext(IntPtr bstrKey)
140 {
141 if (!(bstrKey == IntPtr.Zero))
142 {
143 string text = Marshal.PtrToStringBSTR(bstrKey);
144 object[] parameters = new object[2] { _targetRcwType, text };
145 _setSavedLicenseKey.Invoke(_licContext, BindingFlags.DoNotWrapExceptions, null, parameters, null);
146 }
147 }
148}
void GetLicInfo(Type type, out bool runtimeKeyAvail, out bool licVerified)
object AllocateAndValidateLicense([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, string key, bool isDesignTime)
void GetCurrentContextInfo(RuntimeTypeHandle rth, out bool isDesignTime, out IntPtr bstrKey)
static ? object CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
Definition Activator.cs:17
virtual string Message
Definition Exception.cs:100
new Type GetType()
Definition Exception.cs:437
object? Invoke(object? obj, object?[]? parameters)
static unsafe IntPtr StringToBSTR(string? s)
Definition Marshal.cs:1531
static string PtrToStringBSTR(IntPtr ptr)
Definition Marshal.cs:1542
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
static Type GetTypeFromHandle(RuntimeTypeHandle handle)
Type? GetNestedType(string name)
Definition Type.cs:797
MethodInfo? GetMethod(string name)
Definition Type.cs:675
static readonly IntPtr Zero
Definition IntPtr.cs:18