Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DelegateHelpers.cs
Go to the documentation of this file.
8
10
11internal static class DelegateHelpers
12{
13 internal sealed class TypeInfo
14 {
16
18 }
19
20 private static readonly Type[] s_delegateCtorSignature = new Type[2]
21 {
22 typeof(object),
24 };
25
26 private static TypeInfo _DelegateCache = new TypeInfo();
27
29 {
31 {
34 for (int i = 0; i < types.Count; i++)
35 {
37 }
39 if (delegateCache.DelegateType == null)
40 {
41 delegateCache.MakeDelegateType(returnType, types);
42 }
43 return delegateCache.DelegateType;
44 }
45 }
46
48 {
50 {
54 {
55 Type type = dynamicMetaObject.Expression.Type;
57 {
58 type = type.MakeByRefType();
59 }
61 }
63 if (delegateCache.DelegateType == null)
64 {
65 Type[] array = new Type[args.Length + 2];
66 array[0] = typeof(CallSite);
67 array[^1] = returnType;
68 for (int j = 0; j < args.Length; j++)
69 {
71 Type type2 = dynamicMetaObject2.Expression.Type;
73 {
74 type2 = type2.MakeByRefType();
75 }
76 array[j + 1] = type2;
77 }
78 delegateCache.DelegateType = MakeNewDelegate(array);
79 }
80 return delegateCache.DelegateType;
81 }
82 }
83
84 private static bool IsByRef(DynamicMetaObject mo)
85 {
87 {
88 return parameterExpression.IsByRef;
89 }
90 return false;
91 }
92
94 {
95 Type returnType = types[^1];
96 Type[] parameterTypes = types.RemoveLast();
97 TypeBuilder typeBuilder = AssemblyGen.DefineDelegateType("Delegate" + types.Length);
99 typeBuilder.DefineMethod("Invoke", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.VtableLayoutMask, returnType, parameterTypes).SetImplementationFlags(MethodImplAttributes.CodeTypeMask);
100 return typeBuilder.CreateTypeInfo();
101 }
102
103 internal static Type MakeDelegateType(Type[] types)
104 {
106 {
107 TypeInfo typeInfo = _DelegateCache;
108 for (int i = 0; i < types.Length; i++)
109 {
110 typeInfo = NextTypeInfo(types[i], typeInfo);
111 }
112 if (typeInfo.DelegateType == null)
113 {
114 typeInfo.DelegateType = MakeNewDelegate((Type[])types.Clone());
115 }
116 return typeInfo.DelegateType;
117 }
118 }
119
121 {
123 {
125 }
126 }
127
129 {
131 {
133 }
134 }
135
137 {
138 if (curTypeInfo.TypeChain == null)
139 {
140 curTypeInfo.TypeChain = new Dictionary<Type, TypeInfo>();
141 }
143 {
144 value = new TypeInfo();
145 if (!initialArg.IsCollectible)
146 {
147 curTypeInfo.TypeChain[initialArg] = value;
148 }
149 }
150 return value;
151 }
152
153 internal static Type MakeNewDelegate(Type[] types)
154 {
155 bool flag;
156 if (types.Length > 17)
157 {
158 flag = true;
159 }
160 else
161 {
162 flag = false;
163 foreach (Type type in types)
164 {
165 if (type.IsByRef || type.IsByRefLike || type.IsPointer)
166 {
167 flag = true;
168 break;
169 }
170 }
171 }
172 if (flag)
173 {
175 }
176 if (types[^1] == typeof(void))
177 {
178 return GetActionType(types.RemoveLast());
179 }
180 return GetFuncType(types);
181 }
182
183 internal static Type GetFuncType(Type[] types)
184 {
185 return types.Length switch
186 {
187 1 => typeof(Func<>).MakeGenericType(types),
188 2 => typeof(Func<, >).MakeGenericType(types),
189 3 => typeof(Func<, , >).MakeGenericType(types),
190 4 => typeof(Func<, , , >).MakeGenericType(types),
191 5 => typeof(Func<, , , , >).MakeGenericType(types),
192 6 => typeof(Func<, , , , , >).MakeGenericType(types),
193 7 => typeof(Func<, , , , , , >).MakeGenericType(types),
194 8 => typeof(Func<, , , , , , , >).MakeGenericType(types),
195 9 => typeof(Func<, , , , , , , , >).MakeGenericType(types),
196 10 => typeof(Func<, , , , , , , , , >).MakeGenericType(types),
197 11 => typeof(Func<, , , , , , , , , , >).MakeGenericType(types),
198 12 => typeof(Func<, , , , , , , , , , , >).MakeGenericType(types),
199 13 => typeof(Func<, , , , , , , , , , , , >).MakeGenericType(types),
200 14 => typeof(Func<, , , , , , , , , , , , , >).MakeGenericType(types),
201 15 => typeof(Func<, , , , , , , , , , , , , , >).MakeGenericType(types),
202 16 => typeof(Func<, , , , , , , , , , , , , , , >).MakeGenericType(types),
203 17 => typeof(Func<, , , , , , , , , , , , , , , , >).MakeGenericType(types),
204 _ => null,
205 };
206 }
207
208 internal static Type GetActionType(Type[] types)
209 {
210 return types.Length switch
211 {
212 0 => typeof(Action),
213 1 => typeof(Action<>).MakeGenericType(types),
214 2 => typeof(Action<, >).MakeGenericType(types),
215 3 => typeof(Action<, , >).MakeGenericType(types),
216 4 => typeof(Action<, , , >).MakeGenericType(types),
217 5 => typeof(Action<, , , , >).MakeGenericType(types),
218 6 => typeof(Action<, , , , , >).MakeGenericType(types),
219 7 => typeof(Action<, , , , , , >).MakeGenericType(types),
220 8 => typeof(Action<, , , , , , , >).MakeGenericType(types),
221 9 => typeof(Action<, , , , , , , , >).MakeGenericType(types),
222 10 => typeof(Action<, , , , , , , , , >).MakeGenericType(types),
223 11 => typeof(Action<, , , , , , , , , , >).MakeGenericType(types),
224 12 => typeof(Action<, , , , , , , , , , , >).MakeGenericType(types),
225 13 => typeof(Action<, , , , , , , , , , , , >).MakeGenericType(types),
226 14 => typeof(Action<, , , , , , , , , , , , , >).MakeGenericType(types),
227 15 => typeof(Action<, , , , , , , , , , , , , , >).MakeGenericType(types),
228 16 => typeof(Action<, , , , , , , , , , , , , , , >).MakeGenericType(types),
229 _ => null,
230 };
231 }
232}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
static TypeBuilder DefineDelegateType(string name)
static TypeInfo NextTypeInfo(Type initialArg)
static Type MakeCallSiteDelegate(ReadOnlyCollection< Expression > types, Type returnType)
static TypeInfo GetNextTypeInfo(Type initialArg, TypeInfo curTypeInfo)
static TypeInfo NextTypeInfo(Type initialArg, TypeInfo curTypeInfo)
static bool IsByRef(DynamicMetaObject mo)
static Type MakeDeferredSiteDelegate(DynamicMetaObject[] args, Type returnType)
void SetImplementationFlags(MethodImplAttributes attributes)
static int DefineMethod(QCallModule module, int tkParent, string name, byte[] signature, int sigLength, MethodAttributes attributes)
ConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, Type[]? parameterTypes)