Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EnumDataContract.cs
Go to the documentation of this file.
3using System.Linq;
6using System.Xml;
7
9
10internal sealed class EnumDataContract : DataContract
11{
13 {
15
17
19
21
22 private bool _isULong;
23
24 private bool _isFlags;
25
26 private readonly bool _hasDataContract;
27
29
31 {
32 get
33 {
34 return _members;
35 }
36 set
37 {
39 }
40 }
41
43 {
44 get
45 {
46 return _values;
47 }
48 set
49 {
50 _values = value;
51 }
52 }
53
54 internal bool IsFlags
55 {
56 get
57 {
58 return _isFlags;
59 }
60 set
61 {
63 }
64 }
65
66 internal bool IsULong
67 {
68 get
69 {
70 return _isULong;
71 }
72 set
73 {
75 }
76 }
77
79 {
80 get
81 {
82 return _childElementNames;
83 }
84 set
85 {
87 }
88 }
89
91 {
94 Add(typeof(sbyte), "byte");
95 Add(typeof(byte), "unsignedByte");
96 Add(typeof(short), "short");
97 Add(typeof(ushort), "unsignedShort");
98 Add(typeof(int), "int");
99 Add(typeof(uint), "unsignedInt");
100 Add(typeof(long), "long");
101 Add(typeof(ulong), "unsignedLong");
102 }
103
104 internal static void Add(Type type, string localName)
105 {
106 XmlQualifiedName xmlQualifiedName = DataContract.CreateQualifiedName(localName, "http://www.w3.org/2001/XMLSchema");
109 }
110
111 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
133
135 {
137 }
138
139 [MemberNotNull("_members")]
140 private void ImportDataMembers()
141 {
142 Type underlyingType = base.UnderlyingType;
143 FieldInfo[] fields = underlyingType.GetFields(BindingFlags.Static | BindingFlags.Public);
145 List<DataMember> members = new List<DataMember>(fields.Length);
146 List<long> list = new List<long>(fields.Length);
147 foreach (FieldInfo fieldInfo in fields)
148 {
149 bool flag = false;
151 {
152 object[] array = fieldInfo.GetCustomAttributes(Globals.TypeOfEnumMemberAttribute, inherit: false).ToArray();
153 if (array != null && array.Length != 0)
154 {
155 if (array.Length > 1)
156 {
158 }
161 if (enumMemberAttribute.IsValueSetExplicitly)
162 {
163 if (enumMemberAttribute.Value == null || enumMemberAttribute.Value.Length == 0)
164 {
166 }
168 }
169 else
170 {
172 }
174 flag = true;
175 }
176 object[] array2 = fieldInfo.GetCustomAttributes(Globals.TypeOfDataMemberAttribute, inherit: false).ToArray();
177 if (array2 != null && array2.Length != 0)
178 {
180 }
181 }
182 else if (!fieldInfo.IsNotSerialized)
183 {
187 flag = true;
188 }
189 if (flag)
190 {
191 object value = fieldInfo.GetValue(null);
192 if (_isULong)
193 {
194 list.Add((long)Convert.ToUInt64(value, null));
195 }
196 else
197 {
198 list.Add(Convert.ToInt64(value, null));
199 }
200 }
201 }
204 _values = list;
205 }
206 }
207
209
211
213 {
214 get
215 {
216 return _helper.Members;
217 }
218 set
219 {
221 }
222 }
223
225 {
226 get
227 {
228 return _helper.Values;
229 }
230 set
231 {
233 }
234 }
235
236 public bool IsFlags
237 {
238 get
239 {
240 return _helper.IsFlags;
241 }
242 set
243 {
245 }
246 }
247
248 public bool IsULong
249 {
250 get
251 {
252 return _helper.IsULong;
253 }
254 set
255 {
257 }
258 }
259
261 {
262 get
263 {
265 }
266 set
267 {
269 }
270 }
271
272 internal override bool CanContainReferences => false;
273
274 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
280
282 {
283 long num = (IsULong ? ((long)Convert.ToUInt64(value, null)) : Convert.ToInt64(value, null));
284 for (int i = 0; i < Values.Count; i++)
285 {
286 if (num == Values[i])
287 {
288 writer.WriteString(ChildElementNames[i].Value);
289 return;
290 }
291 }
292 if (IsFlags)
293 {
294 int num2 = -1;
295 bool flag = true;
296 for (int j = 0; j < Values.Count; j++)
297 {
298 long num3 = Values[j];
299 if (num3 == 0L)
300 {
301 num2 = j;
302 continue;
303 }
304 if (num == 0L)
305 {
306 break;
307 }
308 if ((num3 & num) == num3)
309 {
310 if (flag)
311 {
312 flag = false;
313 }
314 else
315 {
316 writer.WriteString(DictionaryGlobals.Space.Value);
317 }
318 writer.WriteString(ChildElementNames[j].Value);
319 num &= ~num3;
320 }
321 }
322 if (num != 0L)
323 {
325 }
326 if (flag && num2 >= 0)
327 {
328 writer.WriteString(ChildElementNames[num2].Value);
329 }
330 return;
331 }
333 }
334
335 internal object ReadEnumValue(XmlReaderDelegator reader)
336 {
337 string text = reader.ReadElementContentAsString();
338 long num = 0L;
339 int i = 0;
340 if (IsFlags)
341 {
342 for (; i < text.Length && text[i] == ' '; i++)
343 {
344 }
345 int num2 = i;
346 int num3 = 0;
347 for (; i < text.Length; i++)
348 {
349 if (text[i] == ' ')
350 {
351 num3 = i - num2;
352 if (num3 > 0)
353 {
354 num |= ReadEnumValue(text, num2, num3);
355 }
356 for (i++; i < text.Length && text[i] == ' '; i++)
357 {
358 }
359 num2 = i;
360 if (i == text.Length)
361 {
362 break;
363 }
364 }
365 }
366 num3 = i - num2;
367 if (num3 > 0)
368 {
369 num |= ReadEnumValue(text, num2, num3);
370 }
371 }
372 else
373 {
374 if (text.Length == 0)
375 {
377 }
378 num = ReadEnumValue(text, 0, text.Length);
379 }
380 if (IsULong)
381 {
382 return Enum.ToObject(base.UnderlyingType, (object)(ulong)num);
383 }
384 return Enum.ToObject(base.UnderlyingType, (object)num);
385 }
386
387 private long ReadEnumValue(string value, int index, int count)
388 {
389 for (int i = 0; i < Members.Count; i++)
390 {
391 string name = Members[i].Name;
392 if (name.Length == count && string.CompareOrdinal(value, index, name, 0, count) == 0)
393 {
394 return Values[i];
395 }
396 }
398 }
399
400 internal string GetStringFromEnumValue(long value)
401 {
402 if (IsULong)
403 {
404 return XmlConvert.ToString((ulong)value);
405 }
406 return XmlConvert.ToString(value);
407 }
408
409 internal long GetEnumValueFromString(string value)
410 {
411 if (IsULong)
412 {
413 return (long)XmlConverter.ToUInt64(value);
414 }
415 return XmlConverter.ToInt64(value);
416 }
417
418 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
423
424 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
426 {
427 object obj = ReadEnumValue(xmlReader);
428 context?.AddNewObject(obj);
429 return obj;
430 }
431}
void Add(TKey key, TValue value)
static long ToInt64(object? value)
Definition Convert.cs:1623
static ulong ToUInt64(object? value)
Definition Convert.cs:1738
static Type GetUnderlyingType(Type enumType)
Definition Enum.cs:309
static object ToObject(Type enumType, object value)
Definition Enum.cs:874
static void CheckAndAddMember(List< DataMember > members, DataMember memberContract, Dictionary< string, DataMember > memberNamesTable)
static void ThrowInvalidDataContractException(string message, Type type)
static string GetClrTypeFullName(Type type)
static XmlQualifiedName CreateQualifiedName(string localName, string ns)
static XmlQualifiedName GetStableName(Type type)
static bool TryGetDCAttribute(Type type, [NotNullWhen(true)] out DataContractAttribute dataContractAttribute)
static void ThrowInvalidDataContractException(string message, Type type)
static readonly XmlDictionaryString Space
static readonly Dictionary< XmlQualifiedName, Type > s_nameToType
EnumDataContractCriticalHelper([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.NonPublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicProperties)] Type type)
static readonly Dictionary< Type, XmlQualifiedName > s_typeToName
object ReadEnumValue(XmlReaderDelegator reader)
override void WriteXmlValue(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
long ReadEnumValue(string value, int index, int count)
readonly EnumDataContractCriticalHelper _helper
void WriteEnumValue(XmlWriterDelegator writer, object value)
override object ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
static SerializationException CreateSerializationException(string errorMessage)
static string EnumTypeCannotHaveIsReference
Definition SR.cs:86
static string DataMemberOnEnumField
Definition SR.cs:62
static string InvalidEnumValueOnWrite
Definition SR.cs:148
static string InvalidEnumMemberValue
Definition SR.cs:144
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string TooManyEnumMembers
Definition SR.cs:282
static string InvalidEnumValueOnRead
Definition SR.cs:146
Definition SR.cs:7
static string ToString(bool value)
static long ToInt64(string value)
static ulong ToUInt64(string value)