Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CustomAttributeBuilder.cs
Go to the documentation of this file.
2using System.IO;
3using System.Text;
4
6
8{
10
11 private object[] m_constructorArgs;
12
13 private byte[] m_blob;
14
15 public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs)
16 : this(con, constructorArgs, Array.Empty<PropertyInfo>(), Array.Empty<object>(), Array.Empty<FieldInfo>(), Array.Empty<object>())
17 {
18 }
19
20 public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, PropertyInfo[] namedProperties, object?[] propertyValues)
21 : this(con, constructorArgs, namedProperties, propertyValues, Array.Empty<FieldInfo>(), Array.Empty<object>())
22 {
23 }
24
25 public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, FieldInfo[] namedFields, object?[] fieldValues)
26 : this(con, constructorArgs, Array.Empty<PropertyInfo>(), Array.Empty<object>(), namedFields, fieldValues)
27 {
28 }
29
30 public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, PropertyInfo[] namedProperties, object?[] propertyValues, FieldInfo[] namedFields, object?[] fieldValues)
31 {
32 if (con == null)
33 {
34 throw new ArgumentNullException("con");
35 }
36 if (constructorArgs == null)
37 {
38 throw new ArgumentNullException("constructorArgs");
39 }
40 if (namedProperties == null)
41 {
42 throw new ArgumentNullException("namedProperties");
43 }
44 if (propertyValues == null)
45 {
46 throw new ArgumentNullException("propertyValues");
47 }
48 if (namedFields == null)
49 {
50 throw new ArgumentNullException("namedFields");
51 }
52 if (fieldValues == null)
53 {
54 throw new ArgumentNullException("fieldValues");
55 }
56 if (namedProperties.Length != propertyValues.Length)
57 {
58 throw new ArgumentException(SR.Arg_ArrayLengthsDiffer, "namedProperties, propertyValues");
59 }
60 if (namedFields.Length != fieldValues.Length)
61 {
62 throw new ArgumentException(SR.Arg_ArrayLengthsDiffer, "namedFields, fieldValues");
63 }
64 if ((con.Attributes & MethodAttributes.Static) == MethodAttributes.Static || (con.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
65 {
67 }
68 if ((con.CallingConvention & CallingConventions.Standard) != CallingConventions.Standard)
69 {
71 }
72 m_con = con;
73 m_constructorArgs = new object[constructorArgs.Length];
74 Array.Copy(constructorArgs, m_constructorArgs, constructorArgs.Length);
75 Type[] parameterTypes = con.GetParameterTypes();
76 if (parameterTypes.Length != constructorArgs.Length)
77 {
79 }
80 for (int i = 0; i < parameterTypes.Length; i++)
81 {
82 if (!ValidateType(parameterTypes[i]))
83 {
85 }
86 }
87 for (int i = 0; i < parameterTypes.Length; i++)
88 {
89 object obj = constructorArgs[i];
90 if (obj == null)
91 {
92 if (parameterTypes[i].IsValueType)
93 {
94 throw new ArgumentNullException($"{"constructorArgs"}[{i}]");
95 }
96 }
97 else
98 {
99 VerifyTypeAndPassedObjectType(parameterTypes[i], obj.GetType(), $"{"constructorArgs"}[{i}]");
100 }
101 }
102 MemoryStream output = new MemoryStream();
103 BinaryWriter binaryWriter = new BinaryWriter(output);
104 binaryWriter.Write((ushort)1);
105 for (int i = 0; i < constructorArgs.Length; i++)
106 {
107 EmitValue(binaryWriter, parameterTypes[i], constructorArgs[i]);
108 }
109 binaryWriter.Write((ushort)(namedProperties.Length + namedFields.Length));
110 for (int i = 0; i < namedProperties.Length; i++)
111 {
112 PropertyInfo propertyInfo = namedProperties[i];
113 if (propertyInfo == null)
114 {
115 throw new ArgumentNullException("namedProperties[" + i + "]");
116 }
117 Type propertyType = propertyInfo.PropertyType;
118 object obj2 = propertyValues[i];
119 if (obj2 == null && propertyType.IsValueType)
120 {
121 throw new ArgumentNullException("propertyValues[" + i + "]");
122 }
123 if (!ValidateType(propertyType))
124 {
126 }
127 if (!propertyInfo.CanWrite)
128 {
130 }
131 if (propertyInfo.DeclaringType != con.DeclaringType && !(con.DeclaringType is TypeBuilderInstantiation) && !con.DeclaringType.IsSubclassOf(propertyInfo.DeclaringType) && !TypeBuilder.IsTypeEqual(propertyInfo.DeclaringType, con.DeclaringType) && (!(propertyInfo.DeclaringType is TypeBuilder) || !con.DeclaringType.IsSubclassOf(((TypeBuilder)propertyInfo.DeclaringType).BakedRuntimeType)))
132 {
134 }
135 if (obj2 != null)
136 {
137 VerifyTypeAndPassedObjectType(propertyType, obj2.GetType(), $"{"propertyValues"}[{i}]");
138 }
139 binaryWriter.Write((byte)84);
140 EmitType(binaryWriter, propertyType);
141 EmitString(binaryWriter, namedProperties[i].Name);
142 EmitValue(binaryWriter, propertyType, obj2);
143 }
144 for (int i = 0; i < namedFields.Length; i++)
145 {
146 FieldInfo fieldInfo = namedFields[i];
147 if (fieldInfo == null)
148 {
149 throw new ArgumentNullException("namedFields[" + i + "]");
150 }
151 Type fieldType = fieldInfo.FieldType;
152 object obj3 = fieldValues[i];
153 if (obj3 == null && fieldType.IsValueType)
154 {
155 throw new ArgumentNullException("fieldValues[" + i + "]");
156 }
157 if (!ValidateType(fieldType))
158 {
160 }
161 if (fieldInfo.DeclaringType != con.DeclaringType && !(con.DeclaringType is TypeBuilderInstantiation) && !con.DeclaringType.IsSubclassOf(fieldInfo.DeclaringType) && !TypeBuilder.IsTypeEqual(fieldInfo.DeclaringType, con.DeclaringType) && (!(fieldInfo.DeclaringType is TypeBuilder) || !con.DeclaringType.IsSubclassOf(((TypeBuilder)namedFields[i].DeclaringType).BakedRuntimeType)))
162 {
164 }
165 if (obj3 != null)
166 {
167 VerifyTypeAndPassedObjectType(fieldType, obj3.GetType(), $"{"fieldValues"}[{i}]");
168 }
169 binaryWriter.Write((byte)83);
170 EmitType(binaryWriter, fieldType);
171 EmitString(binaryWriter, fieldInfo.Name);
172 EmitValue(binaryWriter, fieldType, obj3);
173 }
174 m_blob = ((MemoryStream)binaryWriter.BaseStream).ToArray();
175 }
176
177 private bool ValidateType(Type t)
178 {
179 if (t.IsPrimitive)
180 {
181 if (t != typeof(IntPtr))
182 {
183 return t != typeof(UIntPtr);
184 }
185 return false;
186 }
187 if (t == typeof(string) || t == typeof(Type))
188 {
189 return true;
190 }
191 if (t.IsEnum)
192 {
194 if ((uint)(typeCode - 5) <= 7u)
195 {
196 return true;
197 }
198 return false;
199 }
200 if (t.IsArray)
201 {
202 if (t.GetArrayRank() == 1)
203 {
204 return ValidateType(t.GetElementType());
205 }
206 return false;
207 }
208 return t == typeof(object);
209 }
210
211 private static void VerifyTypeAndPassedObjectType(Type type, Type passedType, string paramName)
212 {
213 if (type != typeof(object) && Type.GetTypeCode(passedType) != Type.GetTypeCode(type))
214 {
216 }
217 if (passedType == typeof(IntPtr) || passedType == typeof(UIntPtr))
218 {
219 throw new ArgumentException(SR.Format(SR.Argument_BadParameterTypeForCAB, passedType), paramName);
220 }
221 }
222
223 private static void EmitType(BinaryWriter writer, Type type)
224 {
225 if (type.IsPrimitive)
226 {
227 switch (Type.GetTypeCode(type))
228 {
229 case TypeCode.SByte:
230 writer.Write((byte)4);
231 break;
232 case TypeCode.Byte:
233 writer.Write((byte)5);
234 break;
235 case TypeCode.Char:
236 writer.Write((byte)3);
237 break;
238 case TypeCode.Boolean:
239 writer.Write((byte)2);
240 break;
241 case TypeCode.Int16:
242 writer.Write((byte)6);
243 break;
244 case TypeCode.UInt16:
245 writer.Write((byte)7);
246 break;
247 case TypeCode.Int32:
248 writer.Write((byte)8);
249 break;
250 case TypeCode.UInt32:
251 writer.Write((byte)9);
252 break;
253 case TypeCode.Int64:
254 writer.Write((byte)10);
255 break;
256 case TypeCode.UInt64:
257 writer.Write((byte)11);
258 break;
259 case TypeCode.Single:
260 writer.Write((byte)12);
261 break;
262 case TypeCode.Double:
263 writer.Write((byte)13);
264 break;
265 }
266 }
267 else if (type.IsEnum)
268 {
269 writer.Write((byte)85);
270 EmitString(writer, type.AssemblyQualifiedName);
271 }
272 else if (type == typeof(string))
273 {
274 writer.Write((byte)14);
275 }
276 else if (type == typeof(Type))
277 {
278 writer.Write((byte)80);
279 }
280 else if (type.IsArray)
281 {
282 writer.Write((byte)29);
283 EmitType(writer, type.GetElementType());
284 }
285 else
286 {
287 writer.Write((byte)81);
288 }
289 }
290
291 private static void EmitString(BinaryWriter writer, string str)
292 {
293 byte[] bytes = Encoding.UTF8.GetBytes(str);
294 uint num = (uint)bytes.Length;
295 if (num <= 127)
296 {
297 writer.Write((byte)num);
298 }
299 else if (num <= 16383)
300 {
301 writer.Write(BinaryPrimitives.ReverseEndianness((short)(num | 0x8000)));
302 }
303 else
304 {
305 writer.Write(BinaryPrimitives.ReverseEndianness(num | 0xC0000000u));
306 }
307 writer.Write(bytes);
308 }
309
310 private static void EmitValue(BinaryWriter writer, Type type, object value)
311 {
312 if (type.IsEnum)
313 {
315 {
316 case TypeCode.SByte:
317 writer.Write((sbyte)value);
318 break;
319 case TypeCode.Byte:
320 writer.Write((byte)value);
321 break;
322 case TypeCode.Int16:
323 writer.Write((short)value);
324 break;
325 case TypeCode.UInt16:
326 writer.Write((ushort)value);
327 break;
328 case TypeCode.Int32:
329 writer.Write((int)value);
330 break;
331 case TypeCode.UInt32:
332 writer.Write((uint)value);
333 break;
334 case TypeCode.Int64:
335 writer.Write((long)value);
336 break;
337 case TypeCode.UInt64:
338 writer.Write((ulong)value);
339 break;
340 }
341 return;
342 }
343 if (type == typeof(string))
344 {
345 if (value == null)
346 {
347 writer.Write(byte.MaxValue);
348 }
349 else
350 {
351 EmitString(writer, (string)value);
352 }
353 return;
354 }
355 if (type == typeof(Type))
356 {
357 if (value == null)
358 {
359 writer.Write(byte.MaxValue);
360 return;
361 }
362 string text = TypeNameBuilder.ToString((Type)value, TypeNameBuilder.Format.AssemblyQualifiedName);
363 if (text == null)
364 {
366 }
368 return;
369 }
370 if (type.IsArray)
371 {
372 if (value == null)
373 {
374 writer.Write(uint.MaxValue);
375 return;
376 }
379 writer.Write(array.Length);
380 for (int i = 0; i < array.Length; i++)
381 {
382 EmitValue(writer, elementType, array.GetValue(i));
383 }
384 return;
385 }
386 if (type.IsPrimitive)
387 {
388 switch (Type.GetTypeCode(type))
389 {
390 case TypeCode.SByte:
391 writer.Write((sbyte)value);
392 break;
393 case TypeCode.Byte:
394 writer.Write((byte)value);
395 break;
396 case TypeCode.Char:
397 writer.Write(Convert.ToUInt16((char)value));
398 break;
399 case TypeCode.Boolean:
400 writer.Write((byte)(((bool)value) ? 1u : 0u));
401 break;
402 case TypeCode.Int16:
403 writer.Write((short)value);
404 break;
405 case TypeCode.UInt16:
406 writer.Write((ushort)value);
407 break;
408 case TypeCode.Int32:
409 writer.Write((int)value);
410 break;
411 case TypeCode.UInt32:
412 writer.Write((uint)value);
413 break;
414 case TypeCode.Int64:
415 writer.Write((long)value);
416 break;
417 case TypeCode.UInt64:
418 writer.Write((ulong)value);
419 break;
420 case TypeCode.Single:
421 writer.Write((float)value);
422 break;
423 case TypeCode.Double:
424 writer.Write((double)value);
425 break;
426 }
427 return;
428 }
429 if (type == typeof(object))
430 {
431 Type type2 = ((value == null) ? typeof(string) : ((value is Type) ? typeof(Type) : value.GetType()));
432 if (type2 == typeof(object))
433 {
435 }
436 EmitType(writer, type2);
437 EmitValue(writer, type2, value);
438 return;
439 }
440 string p = "null";
441 if (value != null)
442 {
443 p = value.GetType().ToString();
444 }
446 }
447
448 internal void CreateCustomAttribute(ModuleBuilder mod, int tkOwner)
449 {
451 }
452}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static sbyte ReverseEndianness(sbyte value)
static ushort ToUInt16(object? value)
Definition Convert.cs:1177
static Type GetUnderlyingType(Type enumType)
Definition Enum.cs:309
virtual Stream BaseStream
virtual void Write(bool value)
static void EmitType(BinaryWriter writer, Type type)
CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, FieldInfo[] namedFields, object?[] fieldValues)
void CreateCustomAttribute(ModuleBuilder mod, int tkOwner)
static void EmitString(BinaryWriter writer, string str)
CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs)
static void EmitValue(BinaryWriter writer, Type type, object value)
CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, PropertyInfo[] namedProperties, object?[] propertyValues, FieldInfo[] namedFields, object?[] fieldValues)
CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, PropertyInfo[] namedProperties, object?[] propertyValues)
static void VerifyTypeAndPassedObjectType(Type type, Type passedType, string paramName)
int GetConstructorToken(ConstructorInfo con)
static bool IsTypeEqual(Type t1, Type t2)
static void DefineCustomAttribute(QCallModule module, int tkAssociate, int tkConstructor, byte[] attr, int attrLength)
virtual Type[] GetParameterTypes()
virtual CallingConventions CallingConvention
Definition MethodBase.cs:28
MethodAttributes Attributes
Definition MethodBase.cs:24
static string Argument_BadParameterTypeForCAB
Definition SR.cs:500
static string Argument_BadParameterCountsForConstructor
Definition SR.cs:498
static string Argument_BadPropertyForConstructorBuilder
Definition SR.cs:502
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_ConstantDoesntMatch
Definition SR.cs:530
static string Argument_BadTypeInCustomAttribute
Definition SR.cs:516
static string Argument_NotAWritableProperty
Definition SR.cs:804
static string Arg_ArrayLengthsDiffer
Definition SR.cs:44
static string Argument_BadFieldForConstructorBuilder
Definition SR.cs:482
static string Argument_InvalidTypeForCA
Definition SR.cs:746
static string Argument_BadConstructor
Definition SR.cs:476
static string Argument_BadConstructorCallConv
Definition SR.cs:478
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526
Type? GetElementType()
bool IsValueType
Definition Type.cs:234
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
bool IsArray
Definition Type.cs:71
virtual bool IsEnum
Definition Type.cs:227
virtual int GetArrayRank()
Definition Type.cs:490
static TypeCode GetTypeCode(Type? type)
Definition Type.cs:919
bool IsPrimitive
Definition Type.cs:231
virtual bool IsSubclassOf(Type c)
Definition Type.cs:1542
TypeCode
Definition TypeCode.cs:4