Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ObjectStorage.cs
Go to the documentation of this file.
5using System.IO;
7using System.Xml;
9
10namespace System.Data.Common;
11
12internal sealed class ObjectStorage : DataStorage
13{
14 private enum Families
15 {
17 NUMBER,
18 STRING,
19 BOOLEAN,
20 ARRAY
21 }
22
23 private sealed class TempAssemblyComparer : IEqualityComparer<KeyValuePair<Type, XmlRootAttribute>>
24 {
26
28 {
29 }
30
32 {
33 if (x.Key == y.Key)
34 {
35 if (x.Value != null || y.Value != null)
36 {
37 if (x.Value != null && y.Value != null && x.Value.ElementName == y.Value.ElementName && x.Value.Namespace == y.Value.Namespace && x.Value.DataType == y.Value.DataType)
38 {
39 return x.Value.IsNullable == y.Value.IsNullable;
40 }
41 return false;
42 }
43 return true;
44 }
45 return false;
46 }
47
49 {
50 return obj.Key.GetHashCode() + obj.Value.ElementName.GetHashCode();
51 }
52 }
53
54 private object[] _values;
55
56 private readonly bool _implementsIXmlSerializable;
57
58 private static readonly object s_tempAssemblyCacheLock = new object();
59
61
63
69
70 public override object Aggregate(int[] records, AggregateType kind)
71 {
73 }
74
75 public override int Compare(int recordNo1, int recordNo2)
76 {
77 object obj = _values[recordNo1];
78 object obj2 = _values[recordNo2];
79 if (obj == obj2)
80 {
81 return 0;
82 }
83 if (obj == null)
84 {
85 return -1;
86 }
87 if (obj2 == null)
88 {
89 return 1;
90 }
92 {
93 try
94 {
95 return comparable.CompareTo(obj2);
96 }
97 catch (ArgumentException e)
98 {
100 }
101 }
103 }
104
105 public override int CompareValueTo(int recordNo1, object value)
106 {
107 object obj = Get(recordNo1);
108 if (obj is IComparable && value.GetType() == obj.GetType())
109 {
110 return ((IComparable)obj).CompareTo(value);
111 }
112 if (obj == value)
113 {
114 return 0;
115 }
116 if (obj == null)
117 {
118 if (_nullValue == value)
119 {
120 return 0;
121 }
122 return -1;
123 }
124 if (_nullValue == value || value == null)
125 {
126 return 1;
127 }
129 }
130
131 private int CompareTo(object valueNo1, object valueNo2)
132 {
133 if (valueNo1 == null)
134 {
135 return -1;
136 }
137 if (valueNo2 == null)
138 {
139 return 1;
140 }
141 if (valueNo1 == valueNo2)
142 {
143 return 0;
144 }
145 if (valueNo1 == _nullValue)
146 {
147 return -1;
148 }
149 if (valueNo2 == _nullValue)
150 {
151 return 1;
152 }
154 {
155 try
156 {
157 return ((IComparable)valueNo1).CompareTo(valueNo2);
158 }
159 catch (ArgumentException e)
160 {
162 }
163 }
165 }
166
167 private int CompareWithFamilies(object valueNo1, object valueNo2)
168 {
169 Families family = GetFamily(valueNo1.GetType());
170 Families family2 = GetFamily(valueNo2.GetType());
171 if (family < family2)
172 {
173 return -1;
174 }
175 if (family > family2)
176 {
177 return 1;
178 }
179 switch (family)
180 {
181 case Families.BOOLEAN:
182 valueNo1 = Convert.ToBoolean(valueNo1, base.FormatProvider);
183 valueNo2 = Convert.ToBoolean(valueNo2, base.FormatProvider);
184 break;
185 case Families.DATETIME:
186 valueNo1 = Convert.ToDateTime(valueNo1, base.FormatProvider);
187 valueNo2 = Convert.ToDateTime(valueNo1, base.FormatProvider);
188 break;
189 case Families.NUMBER:
190 valueNo1 = Convert.ToDouble(valueNo1, base.FormatProvider);
191 valueNo2 = Convert.ToDouble(valueNo2, base.FormatProvider);
192 break;
193 case Families.ARRAY:
194 {
197 if (array.Length > array2.Length)
198 {
199 return 1;
200 }
201 if (array.Length < array2.Length)
202 {
203 return -1;
204 }
205 for (int i = 0; i < array.Length; i++)
206 {
207 int num = CompareTo(array.GetValue(i), array2.GetValue(i));
208 if (num != 0)
209 {
210 return num;
211 }
212 }
213 return 0;
214 }
215 default:
216 valueNo1 = valueNo1.ToString();
217 valueNo2 = valueNo2.ToString();
218 break;
219 }
220 return ((IComparable)valueNo1).CompareTo(valueNo2);
221 }
222
223 public override void Copy(int recordNo1, int recordNo2)
224 {
226 }
227
228 public override object Get(int recordNo)
229 {
230 object obj = _values[recordNo];
231 if (obj != null)
232 {
233 return obj;
234 }
235 return _nullValue;
236 }
237
238 private Families GetFamily(Type dataType)
239 {
240 switch (Type.GetTypeCode(dataType))
241 {
242 case TypeCode.Boolean:
243 return Families.BOOLEAN;
244 case TypeCode.Char:
245 return Families.STRING;
246 case TypeCode.SByte:
247 return Families.STRING;
248 case TypeCode.Byte:
249 return Families.STRING;
250 case TypeCode.Int16:
251 return Families.NUMBER;
252 case TypeCode.UInt16:
253 return Families.NUMBER;
254 case TypeCode.Int32:
255 return Families.NUMBER;
256 case TypeCode.UInt32:
257 return Families.NUMBER;
258 case TypeCode.Int64:
259 return Families.NUMBER;
260 case TypeCode.UInt64:
261 return Families.NUMBER;
262 case TypeCode.Single:
263 return Families.NUMBER;
264 case TypeCode.Double:
265 return Families.NUMBER;
266 case TypeCode.Decimal:
267 return Families.NUMBER;
268 case TypeCode.DateTime:
269 return Families.DATETIME;
270 case TypeCode.String:
271 return Families.STRING;
272 default:
273 if (typeof(TimeSpan) == dataType)
274 {
275 return Families.DATETIME;
276 }
277 if (dataType.IsArray)
278 {
279 return Families.ARRAY;
280 }
281 return Families.STRING;
282 }
283 }
284
285 public override bool IsNull(int record)
286 {
287 return _values[record] == null;
288 }
289
290 public override void Set(int recordNo, object value)
291 {
292 if (_nullValue == value)
293 {
294 _values[recordNo] = null;
295 return;
296 }
298 {
300 return;
301 }
303 if (_dataType == typeof(Guid) && type == typeof(string))
304 {
305 _values[recordNo] = new Guid((string)value);
306 return;
307 }
308 if (_dataType == typeof(byte[]))
309 {
310 if (type == typeof(bool))
311 {
313 return;
314 }
315 if (type == typeof(char))
316 {
318 return;
319 }
320 if (type == typeof(short))
321 {
323 return;
324 }
325 if (type == typeof(int))
326 {
328 return;
329 }
330 if (type == typeof(long))
331 {
333 return;
334 }
335 if (type == typeof(ushort))
336 {
338 return;
339 }
340 if (type == typeof(uint))
341 {
343 return;
344 }
345 if (type == typeof(ulong))
346 {
348 return;
349 }
350 if (type == typeof(float))
351 {
353 return;
354 }
355 if (type == typeof(double))
356 {
358 return;
359 }
361 }
363 }
364
365 public override void SetCapacity(int capacity)
366 {
367 object[] array = new object[capacity];
368 if (_values != null)
369 {
371 }
372 _values = array;
373 }
374
375 [MethodImpl(MethodImplOptions.NoInlining)]
376 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
377 public override object ConvertXmlToObject(string s)
378 {
379 Type dataType = _dataType;
380 if (dataType == typeof(byte[]))
381 {
382 return Convert.FromBase64String(s);
383 }
384 if (dataType == typeof(Type))
385 {
386 return Type.GetType(s);
387 }
388 if (dataType == typeof(Guid))
389 {
390 return new Guid(s);
391 }
392 if (dataType == typeof(Uri))
393 {
394 return new Uri(s);
395 }
397 {
400 using XmlTextReader reader = new XmlTextReader(input);
401 ((IXmlSerializable)obj).ReadXml(reader);
402 return obj;
403 }
404 StringReader textReader = new StringReader(s);
406 return xmlSerializer.Deserialize(textReader);
407 }
408
409 [MethodImpl(MethodImplOptions.NoInlining)]
410 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
412 {
413 object obj = null;
414 bool flag = false;
415 bool flag2 = false;
416 if (xmlAttrib == null)
417 {
418 Type type = null;
419 string attribute = xmlReader.GetAttribute("InstanceType", "urn:schemas-microsoft-com:xml-msdata");
420 if (attribute == null || attribute.Length == 0)
421 {
422 string text = xmlReader.GetAttribute("type", "http://www.w3.org/2001/XMLSchema-instance");
423 if (text != null && text.Length > 0)
424 {
425 string[] array = text.Split(':');
426 if (array.Length == 2 && xmlReader.LookupNamespace(array[0]) == "http://www.w3.org/2001/XMLSchema")
427 {
428 text = array[1];
429 }
431 flag = true;
432 }
433 else if (_dataType == typeof(object))
434 {
435 flag2 = true;
436 }
437 }
438 if (flag2)
439 {
440 obj = xmlReader.ReadString();
441 }
442 else if (attribute == "Type")
443 {
444 obj = Type.GetType(xmlReader.ReadString());
445 xmlReader.Read();
446 }
447 else
448 {
449 if (null == type)
450 {
452 }
453 if (type == typeof(char) || type == typeof(Guid))
454 {
455 flag = true;
456 }
457 if (type == typeof(object))
458 {
460 }
462 if (!flag)
463 {
465 ((IXmlSerializable)obj).ReadXml(xmlReader);
466 }
467 else
468 {
469 if (type == typeof(string) && xmlReader.NodeType == XmlNodeType.Element && xmlReader.IsEmptyElement)
470 {
471 obj = string.Empty;
472 }
473 else
474 {
475 obj = xmlReader.ReadString();
476 obj = ((!(type != typeof(byte[]))) ? Convert.FromBase64String(obj.ToString()) : SqlConvert.ChangeTypeForXML(obj, type));
477 }
478 xmlReader.Read();
479 }
480 }
481 }
482 else
483 {
485 obj = xmlSerializer.Deserialize(xmlReader);
486 }
487 return obj;
488 }
489
490 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
491 public override string ConvertObjectToXml(object value)
492 {
493 if (value == null || value == _nullValue)
494 {
495 return string.Empty;
496 }
497 Type dataType = _dataType;
498 if (dataType == typeof(byte[]) || (dataType == typeof(object) && value is byte[]))
499 {
500 return Convert.ToBase64String((byte[])value);
501 }
502 if (dataType == typeof(Type) || (dataType == typeof(object) && value is Type))
503 {
504 return ((Type)value).AssemblyQualifiedName;
505 }
506 if (!DataStorage.IsTypeCustomType(value.GetType()))
507 {
508 return (string)SqlConvert.ChangeTypeForXML(value, typeof(string));
509 }
510 if (Type.GetTypeCode(value.GetType()) != TypeCode.Object)
511 {
512 return value.ToString();
513 }
514 StringWriter stringWriter = new StringWriter(base.FormatProvider);
516 {
518 {
519 ((IXmlSerializable)value).WriteXml(writer);
520 }
521 return stringWriter.ToString();
522 }
524 xmlSerializer.Serialize(stringWriter, value);
525 return stringWriter.ToString();
526 }
527
528 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
530 {
531 if (xmlAttrib == null)
532 {
533 ((IXmlSerializable)value).WriteXml(xmlWriter);
534 return;
535 }
537 xmlSerializer.Serialize(xmlWriter, value);
538 }
539
540 protected override object GetEmptyStorage(int recordCount)
541 {
542 return new object[recordCount];
543 }
544
545 protected override void CopyValue(int record, object store, BitArray nullbits, int storeIndex)
546 {
547 object[] array = (object[])store;
549 bool flag = IsNull(record);
550 nullbits.Set(storeIndex, flag);
551 if (!flag && array[storeIndex] is DateTime { Kind: DateTimeKind.Local } dateTime)
552 {
553 array[storeIndex] = DateTime.SpecifyKind(dateTime.ToUniversalTime(), DateTimeKind.Local);
554 }
555 }
556
557 protected override void SetStorage(object store, BitArray nullbits)
558 {
559 _values = (object[])store;
560 for (int i = 0; i < _values.Length; i++)
561 {
562 if (_values[i] is DateTime { Kind: DateTimeKind.Local } value)
563 {
564 _values[i] = DateTime.SpecifyKind(value, DateTimeKind.Utc).ToLocalTime();
565 }
566 }
567 }
568
570 {
571 if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type) && !typeof(IXmlSerializable).IsAssignableFrom(type))
572 {
574 }
575 }
576
577 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
579 {
581 return s_serializerFactory.CreateSerializer(type);
582 }
583
584 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
586 {
587 XmlSerializer value = null;
590 if (dictionary == null || !dictionary.TryGetValue(key, out value))
591 {
593 {
595 if (dictionary == null || !dictionary.TryGetValue(key, out value))
596 {
598 if (dictionary != null)
599 {
602 {
603 dictionary2.Add(item.Key, item.Value);
604 }
606 }
607 else
608 {
610 }
612 key.Value.ElementName = attribute.ElementName;
613 key.Value.Namespace = attribute.Namespace;
614 key.Value.DataType = attribute.DataType;
615 key.Value.IsNullable = attribute.IsNullable;
616 value = s_serializerFactory.CreateSerializer(type, attribute);
617 dictionary.Add(key, value);
619 }
620 }
621 }
622 return value;
623 }
624}
static ? object CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
Definition Activator.cs:17
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static byte[] GetBytes(bool value)
void Add(TKey key, TValue value)
static DateTime ToDateTime(DateTime value)
Definition Convert.cs:2211
static string ToBase64String(byte[] inArray)
Definition Convert.cs:2675
static double ToDouble(object? value)
Definition Convert.cs:1991
static bool ToBoolean([NotNullWhen(true)] object? value)
Definition Convert.cs:508
static unsafe byte[] FromBase64String(string s)
Definition Convert.cs:2904
static InvalidOperationException InvalidOperation(string error)
Definition ADP.cs:746
static Type GetType(string value)
static bool IsTypeCustomType(Type type)
static StorageType GetStorageType(Type dataType)
static readonly IEqualityComparer< KeyValuePair< Type, XmlRootAttribute > > s_default
int GetHashCode(KeyValuePair< Type, XmlRootAttribute > obj)
bool Equals(KeyValuePair< Type, XmlRootAttribute > x, KeyValuePair< Type, XmlRootAttribute > y)
override int Compare(int recordNo1, int recordNo2)
ObjectStorage(DataColumn column, Type type)
static Dictionary< KeyValuePair< Type, XmlRootAttribute >, XmlSerializer > s_tempAssemblyCache
override void Set(int recordNo, object value)
static XmlSerializer GetXmlSerializer(Type type)
int CompareWithFamilies(object valueNo1, object valueNo2)
override void SetCapacity(int capacity)
override void Copy(int recordNo1, int recordNo2)
override object Get(int recordNo)
override object GetEmptyStorage(int recordCount)
override void SetStorage(object store, BitArray nullbits)
override string ConvertObjectToXml(object value)
static readonly XmlSerializerFactory s_serializerFactory
override void CopyValue(int record, object store, BitArray nullbits, int storeIndex)
override bool IsNull(int record)
static XmlSerializer GetXmlSerializer(Type type, XmlRootAttribute attribute)
override object ConvertXmlToObject(string s)
static void VerifyIDynamicMetaObjectProvider(Type type)
int CompareTo(object valueNo1, object valueNo2)
static readonly object s_tempAssemblyCacheLock
override int CompareValueTo(int recordNo1, object value)
override object Aggregate(int[] records, AggregateType kind)
Families GetFamily(Type dataType)
override void ConvertObjectToXml(object value, XmlWriter xmlWriter, XmlRootAttribute xmlAttrib)
override object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute xmlAttrib)
static object ChangeTypeForXML(object value, Type type)
static Exception AggregateException(AggregateType aggregateType, Type type)
static Exception CanNotDeserializeObjectType()
static Exception StorageSetFailed()
static Exception TraceExceptionWithoutRethrow(Exception e)
static void EnsureTypeIsAllowed(Type type, TypeLimiter capturedLimiter=null)
static Type XsdtoClr(string xsdTypeName)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static string Xml_DynamicWithoutXmlSerializable
Definition SR.cs:116
Definition SR.cs:7
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
bool IsArray
Definition Type.cs:71
static TypeCode GetTypeCode(Type? type)
Definition Type.cs:919
virtual bool IsInstanceOfType([NotNullWhen(true)] object? o)
Definition Type.cs:1020
TypeCode
Definition TypeCode.cs:4
static DateTime SpecifyKind(DateTime value, DateTimeKind kind)
Definition DateTime.cs:756