Elin Decompiled Documentation EA 23.326 Nightly
Loading...
Searching...
No Matches
ClassCache.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq.Expressions;
4using System.Reflection;
5
6public class ClassCache<T>
7{
8 public Dictionary<string, Func<T>> dict = new Dictionary<string, Func<T>>();
9
10 public Func<T> CreateDelegate(Type type)
11 {
12 Func<T> result = () => default(T);
13 if (type == null)
14 {
15 return result;
16 }
17 ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
18 if (constructor != null)
19 {
20 result = Expression.Lambda<Func<T>>(Expression.New(constructor), Array.Empty<ParameterExpression>()).Compile();
21 }
22 return result;
23 }
24
25 public T Create<T2>(string id, string assembly)
26 {
27 id = id.Trim();
28 Func<T> func = dict.TryGetValue(id);
29 if (func != null)
30 {
31 return func();
32 }
33 Type type = Type.GetType(id + ", " + assembly);
34 if (type == null)
35 {
36 for (int num = ClassCache.typeLoaders.Count - 1; num >= 0; num--)
37 {
38 type = ClassCache.typeLoaders[num](id);
39 if (type != null)
40 {
41 break;
42 }
43 }
44 }
45 func = CreateDelegate(type);
46 dict[id] = func;
47 return func();
48 }
49}
50public class ClassCache
51{
53
54 public static HashSet<string> assemblies = new HashSet<string>();
55
56 public static List<Func<string, Type>> typeLoaders = new List<Func<string, Type>> { LoadTypeFromGlobalNamespace };
57
58 public static HashSet<Type> modTypes = new HashSet<Type>();
59
60 public static T Create<T>(string id, string assembly = "Assembly-CSharp")
61 {
62 return (T)caches.Create<T>(id, assembly);
63 }
64
65 public static Type LoadTypeFromGlobalNamespace(string id)
66 {
67 foreach (string assembly in assemblies)
68 {
69 Type type = Type.GetType(id + ", " + assembly);
70 if (type != null)
71 {
72 return type;
73 }
74 }
75 return null;
76 }
77}
Func< T > CreateDelegate(Type type)
Definition: ClassCache.cs:10
static ClassCache< object > caches
Definition: ClassCache.cs:52
static HashSet< string > assemblies
Definition: ClassCache.cs:54
static T Create< T >(string id, string assembly="Assembly-CSharp")
Definition: ClassCache.cs:60
T Create< T2 >(string id, string assembly)
Definition: ClassCache.cs:25
static HashSet< Type > modTypes
Definition: ClassCache.cs:58
static List< Func< string, Type > > typeLoaders
Definition: ClassCache.cs:56
static Type LoadTypeFromGlobalNamespace(string id)
Definition: ClassCache.cs:65
Dictionary< string, Func< T > > dict
Definition: ClassCache.cs:8