Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
LicenseManager.cs
Go to the documentation of this file.
7
9
10public sealed class LicenseManager
11{
12 private sealed class CLRLicenseContext : LicenseContext
13 {
14 private readonly Type _type;
15
16 private string _key;
17
18 public override LicenseUsageMode UsageMode { get; }
19
21 {
22 UsageMode = mode;
23 _type = type;
24 }
25
27 {
28 return new CLRLicenseContext(type, LicenseUsageMode.Designtime);
29 }
30
32 {
33 CLRLicenseContext cLRLicenseContext = new CLRLicenseContext(type, LicenseUsageMode.Runtime);
34 if (key != null)
35 {
36 cLRLicenseContext.SetSavedLicenseKey(type, key);
37 }
38 return cLRLicenseContext;
39 }
40
41 public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly)
42 {
43 if (type == _type)
44 {
45 return _key;
46 }
47 return null;
48 }
49
50 public override void SetSavedLicenseKey(Type type, string key)
51 {
52 if (type == _type)
53 {
54 _key = key;
55 }
56 }
57 }
58
60 {
61 private readonly Hashtable _savedLicenseKeys = new Hashtable();
62
63 public override LicenseUsageMode UsageMode => LicenseUsageMode.Designtime;
64
65 public bool Contains(string assemblyName)
66 {
67 return _savedLicenseKeys.Contains(assemblyName);
68 }
69
70 public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly)
71 {
72 return null;
73 }
74
75 public override void SetSavedLicenseKey(Type type, string key)
76 {
77 _savedLicenseKeys[type.AssemblyQualifiedName] = key;
78 }
79 }
80
81 private static class LicenseInteropHelper
82 {
83 public static bool ValidateAndRetrieveLicenseDetails(LicenseContext context, Type type, out License license, out string licenseKey)
84 {
85 if (context == null)
86 {
87 context = CurrentContext;
88 }
89 return ValidateInternalRecursive(context, type, null, allowExceptions: false, out license, out licenseKey);
90 }
91
92 public static LicenseContext GetCurrentContextInfo(Type type, out bool isDesignTime, out string key)
93 {
94 LicenseContext currentContext = CurrentContext;
95 isDesignTime = currentContext.UsageMode == LicenseUsageMode.Designtime;
96 key = null;
97 if (!isDesignTime)
98 {
99 key = currentContext.GetSavedLicenseKey(type, null);
100 }
101 return currentContext;
102 }
103 }
104
105 private static readonly object s_selfLock = new object();
106
107 private static volatile LicenseContext s_context;
108
109 private static object s_contextLockHolder;
110
111 private static volatile Hashtable s_providers;
112
113 private static volatile Hashtable s_providerInstances;
114
115 private static readonly object s_internalSyncObject = new object();
116
118 {
119 get
120 {
121 if (s_context == null)
122 {
124 {
125 if (s_context == null)
126 {
128 }
129 }
130 }
131 return s_context;
132 }
133 set
134 {
136 {
137 if (s_contextLockHolder != null)
138 {
140 }
142 }
143 }
144 }
145
147 {
148 get
149 {
150 if (s_context != null)
151 {
152 return s_context.UsageMode;
153 }
154 return LicenseUsageMode.Runtime;
155 }
156 }
157
158 private static void CacheProvider(Type type, LicenseProvider provider)
159 {
160 if (s_providers == null)
161 {
163 }
164 lock (s_providers)
165 {
166 s_providers[type] = provider;
167 }
168 if (provider != null)
169 {
170 if (s_providerInstances == null)
171 {
173 }
174 Type type2 = provider.GetType();
176 {
177 s_providerInstances[type2] = provider;
178 }
179 }
180 }
181
182 [UnsupportedOSPlatform("browser")]
183 public static object? CreateWithContext([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, LicenseContext creationContext)
184 {
185 return CreateWithContext(type, creationContext, Array.Empty<object>());
186 }
187
188 [UnsupportedOSPlatform("browser")]
189 public static object? CreateWithContext([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, LicenseContext creationContext, object[] args)
190 {
191 object obj = null;
193 {
194 LicenseContext currentContext = CurrentContext;
195 try
196 {
197 CurrentContext = creationContext;
199 try
200 {
201 return Activator.CreateInstance(type, args);
202 }
204 {
205 throw ex.InnerException;
206 }
207 }
208 finally
209 {
211 CurrentContext = currentContext;
212 }
213 }
214 }
215
217 {
218 if (s_providers != null)
219 {
220 return s_providers.ContainsKey(type);
221 }
222 return false;
223 }
224
226 {
227 return (LicenseProvider)(s_providers?[type]);
228 }
229
230 private static LicenseProvider GetCachedProviderInstance(Type providerType)
231 {
232 return (LicenseProvider)(s_providerInstances?[providerType]);
233 }
234
235 public static bool IsLicensed(Type type)
236 {
237 License license;
238 bool result = ValidateInternal(type, null, allowExceptions: false, out license);
239 if (license != null)
240 {
241 license.Dispose();
242 license = null;
243 }
244 return result;
245 }
246
247 public static bool IsValid(Type type)
248 {
249 License license;
250 bool result = ValidateInternal(type, null, allowExceptions: false, out license);
251 if (license != null)
252 {
253 license.Dispose();
254 license = null;
255 }
256 return result;
257 }
258
259 public static bool IsValid(Type type, object? instance, out License? license)
260 {
261 return ValidateInternal(type, instance, allowExceptions: false, out license);
262 }
263
264 public static void LockContext(object contextUser)
265 {
267 {
268 if (s_contextLockHolder != null)
269 {
271 }
272 s_contextLockHolder = contextUser;
273 }
274 }
275
276 public static void UnlockContext(object contextUser)
277 {
279 {
280 if (s_contextLockHolder != contextUser)
281 {
283 }
284 s_contextLockHolder = null;
285 }
286 }
287
288 private static bool ValidateInternal(Type type, object instance, bool allowExceptions, out License license)
289 {
290 string licenseKey;
291 return ValidateInternalRecursive(CurrentContext, type, instance, allowExceptions, out license, out licenseKey);
292 }
293
294 private static bool ValidateInternalRecursive(LicenseContext context, Type type, object instance, bool allowExceptions, out License license, out string licenseKey)
295 {
296 LicenseProvider licenseProvider = GetCachedProvider(type);
297 if (licenseProvider == null && !GetCachedNoLicenseProvider(type))
298 {
300 if (licenseProviderAttribute != null)
301 {
302 Type licenseProvider2 = licenseProviderAttribute.LicenseProvider;
303 licenseProvider = GetCachedProviderInstance(licenseProvider2) ?? ((LicenseProvider)Activator.CreateInstance(licenseProvider2));
304 }
305 CacheProvider(type, licenseProvider);
306 }
307 license = null;
308 bool flag = true;
309 licenseKey = null;
310 if (licenseProvider != null)
311 {
312 license = licenseProvider.GetLicense(context, type, instance, allowExceptions);
313 if (license == null)
314 {
315 flag = false;
316 }
317 else
318 {
319 licenseKey = license.LicenseKey;
320 }
321 }
322 if (flag && instance == null)
323 {
324 Type baseType = type.BaseType;
325 if (baseType != typeof(object) && baseType != null)
326 {
327 if (license != null)
328 {
329 license.Dispose();
330 license = null;
331 }
332 flag = ValidateInternalRecursive(context, baseType, null, allowExceptions, out license, out var _);
333 if (license != null)
334 {
335 license.Dispose();
336 license = null;
337 }
338 }
339 }
340 return flag;
341 }
342
343 public static void Validate(Type type)
344 {
345 if (!ValidateInternal(type, null, allowExceptions: true, out var license))
346 {
347 throw new LicenseException(type);
348 }
349 if (license != null)
350 {
351 license.Dispose();
352 license = null;
353 }
354 }
355
356 public static License? Validate(Type type, object? instance)
357 {
358 if (!ValidateInternal(type, instance, allowExceptions: true, out var license))
359 {
360 throw new LicenseException(type, instance);
361 }
362 return license;
363 }
364}
static ? object CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
Definition Activator.cs:17
static ? Attribute GetCustomAttribute(MemberInfo element, Type attributeType)
Definition Attribute.cs:411
virtual bool Contains(object key)
Definition Hashtable.cs:719
virtual ? string GetSavedLicenseKey(Type type, Assembly? resourceAssembly)
CLRLicenseContext(Type type, LicenseUsageMode mode)
static CLRLicenseContext CreateRuntimeContext(Type type, string key)
override void SetSavedLicenseKey(Type type, string key)
override string GetSavedLicenseKey(Type type, Assembly resourceAssembly)
static CLRLicenseContext CreateDesignContext(Type type)
override string GetSavedLicenseKey(Type type, Assembly resourceAssembly)
static bool ValidateAndRetrieveLicenseDetails(LicenseContext context, Type type, out License license, out string licenseKey)
static LicenseContext GetCurrentContextInfo(Type type, out bool isDesignTime, out string key)
static volatile Hashtable s_providerInstances
static volatile LicenseContext s_context
static ? object CreateWithContext([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, LicenseContext creationContext)
static bool ValidateInternal(Type type, object instance, bool allowExceptions, out License license)
static LicenseProvider GetCachedProviderInstance(Type providerType)
static ? License Validate(Type type, object? instance)
static void UnlockContext(object contextUser)
static void LockContext(object contextUser)
static bool ValidateInternalRecursive(LicenseContext context, Type type, object instance, bool allowExceptions, out License license, out string licenseKey)
static readonly object s_internalSyncObject
static void CacheProvider(Type type, LicenseProvider provider)
static bool GetCachedNoLicenseProvider(Type type)
static volatile Hashtable s_providers
static LicenseProvider GetCachedProvider(Type type)
static bool IsValid(Type type, object? instance, out License? license)
static ? object CreateWithContext([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, LicenseContext creationContext, object[] args)
License? GetLicense(LicenseContext context, Type type, object? instance, bool allowExceptions)
Exception? InnerException
Definition Exception.cs:104
static string LicMgrContextCannotBeChanged
Definition SR.cs:114
static string LicMgrDifferentUser
Definition SR.cs:118
static string LicMgrAlreadyLocked
Definition SR.cs:116
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
Type? BaseType
Definition Type.cs:295