Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ValueHandle.cs
Go to the documentation of this file.
3using System.Text;
4
5namespace System.Xml;
6
7internal sealed class ValueHandle
8{
9 private readonly XmlBufferReader _bufferReader;
10
12
13 private int _offset;
14
15 private int _length;
16
18
19 private static readonly string[] s_constStrings = new string[6] { "string", "number", "array", "object", "boolean", "null" };
20
22
28
34
36 {
37 _type = type;
38 }
39
40 public void SetDictionaryValue(int key)
41 {
42 SetValue(ValueHandleType.Dictionary, key, 0);
43 }
44
45 public void SetCharValue(int ch)
46 {
47 SetValue(ValueHandleType.Char, ch, 0);
48 }
49
50 public void SetQNameValue(int prefix, int key)
51 {
53 }
54
55 public void SetValue(ValueHandleType type, int offset, int length)
56 {
57 _type = type;
60 }
61
62 public bool IsWhitespace()
63 {
64 switch (_type)
65 {
66 case ValueHandleType.UTF8:
68 case ValueHandleType.Dictionary:
70 case ValueHandleType.Char:
71 {
72 int @char = GetChar();
73 if (@char <= 65535)
74 {
75 return XmlConverter.IsWhitespace((char)@char);
76 }
77 return false;
78 }
79 case ValueHandleType.EscapedUTF8:
81 case ValueHandleType.Unicode:
83 case ValueHandleType.True:
84 case ValueHandleType.False:
85 case ValueHandleType.Zero:
86 case ValueHandleType.One:
87 return false;
88 case ValueHandleType.ConstString:
89 return s_constStrings[_offset].Length == 0;
90 default:
91 return _length == 0;
92 }
93 }
94
95 public Type ToType()
96 {
97 switch (_type)
98 {
99 case ValueHandleType.True:
100 case ValueHandleType.False:
101 return typeof(bool);
102 case ValueHandleType.Zero:
103 case ValueHandleType.One:
104 case ValueHandleType.Int8:
105 case ValueHandleType.Int16:
106 case ValueHandleType.Int32:
107 return typeof(int);
108 case ValueHandleType.Int64:
109 return typeof(long);
110 case ValueHandleType.UInt64:
111 return typeof(ulong);
112 case ValueHandleType.Single:
113 return typeof(float);
114 case ValueHandleType.Double:
115 return typeof(double);
116 case ValueHandleType.Decimal:
117 return typeof(decimal);
118 case ValueHandleType.DateTime:
119 return typeof(DateTime);
120 case ValueHandleType.Empty:
121 case ValueHandleType.UTF8:
122 case ValueHandleType.EscapedUTF8:
123 case ValueHandleType.Dictionary:
124 case ValueHandleType.Char:
125 case ValueHandleType.Unicode:
126 case ValueHandleType.QName:
127 case ValueHandleType.ConstString:
128 return typeof(string);
129 case ValueHandleType.Base64:
130 return typeof(byte[]);
131 case ValueHandleType.List:
132 return typeof(object[]);
133 case ValueHandleType.UniqueId:
134 return typeof(UniqueId);
135 case ValueHandleType.Guid:
136 return typeof(Guid);
137 case ValueHandleType.TimeSpan:
138 return typeof(TimeSpan);
139 default:
141 }
142 }
143
144 public bool ToBoolean()
145 {
146 switch (_type)
147 {
148 case ValueHandleType.False:
149 return false;
150 case ValueHandleType.True:
151 return true;
152 case ValueHandleType.UTF8:
154 case ValueHandleType.Int8:
155 switch (GetInt8())
156 {
157 case 0:
158 return false;
159 case 1:
160 return true;
161 }
162 break;
163 }
165 }
166
167 public int ToInt()
168 {
170 switch (type)
171 {
172 case ValueHandleType.Zero:
173 return 0;
174 case ValueHandleType.One:
175 return 1;
176 case ValueHandleType.Int8:
177 return GetInt8();
178 case ValueHandleType.Int16:
179 return GetInt16();
180 case ValueHandleType.Int32:
181 return GetInt32();
182 case ValueHandleType.Int64:
183 {
184 long @int = GetInt64();
185 if (@int >= int.MinValue && @int <= int.MaxValue)
186 {
187 return (int)@int;
188 }
189 break;
190 }
191 }
192 if (type == ValueHandleType.UInt64)
193 {
194 ulong uInt = GetUInt64();
195 if (uInt <= int.MaxValue)
196 {
197 return (int)uInt;
198 }
199 }
200 if (type == ValueHandleType.UTF8)
201 {
203 }
205 }
206
207 public long ToLong()
208 {
210 switch (type)
211 {
212 case ValueHandleType.Zero:
213 return 0L;
214 case ValueHandleType.One:
215 return 1L;
216 case ValueHandleType.Int8:
217 return GetInt8();
218 case ValueHandleType.Int16:
219 return GetInt16();
220 case ValueHandleType.Int32:
221 return GetInt32();
222 case ValueHandleType.Int64:
223 return GetInt64();
224 case ValueHandleType.UInt64:
225 {
226 ulong uInt = GetUInt64();
227 if (uInt <= long.MaxValue)
228 {
229 return (long)uInt;
230 }
231 break;
232 }
233 }
234 if (type == ValueHandleType.UTF8)
235 {
237 }
239 }
240
241 public ulong ToULong()
242 {
244 switch (type)
245 {
246 case ValueHandleType.Zero:
247 return 0uL;
248 case ValueHandleType.One:
249 return 1uL;
250 case ValueHandleType.Int8:
251 case ValueHandleType.Int16:
252 case ValueHandleType.Int32:
253 case ValueHandleType.Int64:
254 {
255 long num = ToLong();
256 if (num >= 0)
257 {
258 return (ulong)num;
259 }
260 break;
261 }
262 }
263 return type switch
264 {
268 };
269 }
270
271 public float ToSingle()
272 {
274 switch (type)
275 {
276 case ValueHandleType.Single:
277 return GetSingle();
278 case ValueHandleType.Double:
279 {
280 double @double = GetDouble();
281 if ((@double >= -3.4028234663852886E+38 && @double <= 3.4028234663852886E+38) || !double.IsFinite(@double))
282 {
283 return (float)@double;
284 }
285 break;
286 }
287 }
288 return type switch
289 {
291 ValueHandleType.One => 1f,
296 };
297 }
298
314
315 public decimal ToDecimal()
316 {
318 switch (type)
319 {
320 case ValueHandleType.Decimal:
321 return GetDecimal();
322 case ValueHandleType.Zero:
323 return 0m;
324 case ValueHandleType.One:
325 return 1m;
326 case ValueHandleType.Int8:
327 case ValueHandleType.Int16:
328 case ValueHandleType.Int32:
329 case ValueHandleType.Int64:
330 return ToLong();
331 default:
332 return type switch
333 {
337 };
338 }
339 }
340
342 {
343 if (_type == ValueHandleType.DateTime)
344 {
346 }
347 if (_type == ValueHandleType.UTF8)
348 {
350 }
352 }
353
355 {
356 if (_type == ValueHandleType.UniqueId)
357 {
358 return GetUniqueId();
359 }
360 if (_type == ValueHandleType.UTF8)
361 {
363 }
365 }
366
368 {
369 if (_type == ValueHandleType.TimeSpan)
370 {
371 return new TimeSpan(GetInt64());
372 }
373 if (_type == ValueHandleType.UTF8)
374 {
376 }
378 }
379
380 public Guid ToGuid()
381 {
382 if (_type == ValueHandleType.Guid)
383 {
384 return GetGuid();
385 }
386 if (_type == ValueHandleType.UTF8)
387 {
389 }
390 return XmlConverter.ToGuid(GetString());
391 }
392
393 public override string ToString()
394 {
395 return GetString();
396 }
397
398 public byte[] ToByteArray()
399 {
400 if (_type == ValueHandleType.Base64)
401 {
402 byte[] array = new byte[_length];
404 return array;
405 }
406 if (_type == ValueHandleType.UTF8 && _length % 4 == 0)
407 {
408 try
409 {
410 int num = _length / 4 * 3;
411 if (_length > 0 && _bufferReader.Buffer[_offset + _length - 1] == 61)
412 {
413 num--;
414 if (_bufferReader.Buffer[_offset + _length - 2] == 61)
415 {
416 num--;
417 }
418 }
419 byte[] array2 = new byte[num];
421 if (bytes != array2.Length)
422 {
423 byte[] array3 = new byte[bytes];
425 array2 = array3;
426 }
427 return array2;
428 }
429 catch (FormatException)
430 {
431 }
432 }
433 try
434 {
436 }
437 catch (FormatException ex2)
438 {
439 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(ex2.Message, ex2.InnerException));
440 }
441 }
442
443 public string GetString()
444 {
445 switch (_type)
446 {
447 case ValueHandleType.UTF8:
448 return GetCharsText();
449 case ValueHandleType.False:
450 return "false";
451 case ValueHandleType.True:
452 return "true";
453 case ValueHandleType.Zero:
454 return "0";
455 case ValueHandleType.One:
456 return "1";
457 case ValueHandleType.Int8:
458 case ValueHandleType.Int16:
459 case ValueHandleType.Int32:
460 return XmlConverter.ToString(ToInt());
461 case ValueHandleType.Int64:
463 case ValueHandleType.UInt64:
465 case ValueHandleType.Single:
467 case ValueHandleType.Double:
469 case ValueHandleType.Decimal:
471 case ValueHandleType.DateTime:
473 case ValueHandleType.Empty:
474 return string.Empty;
475 case ValueHandleType.Unicode:
476 return GetUnicodeCharsText();
477 case ValueHandleType.EscapedUTF8:
478 return GetEscapedCharsText();
479 case ValueHandleType.Char:
480 return GetCharText();
481 case ValueHandleType.Dictionary:
482 return GetDictionaryString().Value;
483 case ValueHandleType.Base64:
484 {
485 byte[] array = ToByteArray();
486 return Base64Encoding.GetString(array, 0, array.Length);
487 }
488 case ValueHandleType.List:
489 return XmlConverter.ToString(ToList());
490 case ValueHandleType.UniqueId:
492 case ValueHandleType.Guid:
493 return XmlConverter.ToString(ToGuid());
494 case ValueHandleType.TimeSpan:
496 case ValueHandleType.QName:
497 return GetQNameDictionaryText();
498 case ValueHandleType.ConstString:
499 return s_constStrings[_offset];
500 default:
502 }
503 }
504
505 public bool Equals2(string str, bool checkLower)
506 {
507 if (_type != ValueHandleType.UTF8)
508 {
509 return GetString() == str;
510 }
511 if (_length != str.Length)
512 {
513 return false;
514 }
515 byte[] buffer = _bufferReader.Buffer;
516 for (int i = 0; i < _length; i++)
517 {
518 byte b = buffer[i + _offset];
519 if (b != str[i] && (!checkLower || char.ToLowerInvariant((char)b) != str[i]))
520 {
521 return false;
522 }
523 }
524 return true;
525 }
526
528 {
529 switch (_type)
530 {
531 case ValueHandleType.Int8:
532 case ValueHandleType.Int16:
533 case ValueHandleType.Int32:
534 writer.WriteInt32Text(ToInt());
535 break;
536 case ValueHandleType.Int64:
537 writer.WriteInt64Text(GetInt64());
538 break;
539 case ValueHandleType.UInt64:
540 writer.WriteUInt64Text(GetUInt64());
541 break;
542 case ValueHandleType.Single:
543 writer.WriteFloatText(GetSingle());
544 break;
545 case ValueHandleType.Double:
546 writer.WriteDoubleText(GetDouble());
547 break;
548 case ValueHandleType.Decimal:
549 writer.WriteDecimalText(GetDecimal());
550 break;
551 case ValueHandleType.DateTime:
552 writer.WriteDateTimeText(ToDateTime());
553 break;
554 case ValueHandleType.UTF8:
555 writer.WriteEscapedText(_bufferReader.Buffer, _offset, _length);
556 break;
557 case ValueHandleType.Base64:
559 break;
560 case ValueHandleType.UniqueId:
561 writer.WriteUniqueIdText(ToUniqueId());
562 break;
563 case ValueHandleType.Guid:
564 writer.WriteGuidText(ToGuid());
565 break;
566 case ValueHandleType.TimeSpan:
567 writer.WriteTimeSpanText(ToTimeSpan());
568 break;
569 default:
570 writer.WriteEscapedText(GetString());
571 break;
572 case ValueHandleType.Empty:
573 break;
574 }
575 }
576
577 public object[] ToList()
578 {
580 }
581
582 public object ToObject()
583 {
584 switch (_type)
585 {
586 case ValueHandleType.True:
587 case ValueHandleType.False:
588 return ToBoolean();
589 case ValueHandleType.Zero:
590 case ValueHandleType.One:
591 case ValueHandleType.Int8:
592 case ValueHandleType.Int16:
593 case ValueHandleType.Int32:
594 return ToInt();
595 case ValueHandleType.Int64:
596 return ToLong();
597 case ValueHandleType.UInt64:
598 return GetUInt64();
599 case ValueHandleType.Single:
600 return ToSingle();
601 case ValueHandleType.Double:
602 return ToDouble();
603 case ValueHandleType.Decimal:
604 return ToDecimal();
605 case ValueHandleType.DateTime:
606 return ToDateTime();
607 case ValueHandleType.Empty:
608 case ValueHandleType.UTF8:
609 case ValueHandleType.EscapedUTF8:
610 case ValueHandleType.Dictionary:
611 case ValueHandleType.Char:
612 case ValueHandleType.Unicode:
613 case ValueHandleType.ConstString:
614 return ToString();
615 case ValueHandleType.Base64:
616 return ToByteArray();
617 case ValueHandleType.List:
618 return ToList();
619 case ValueHandleType.UniqueId:
620 return ToUniqueId();
621 case ValueHandleType.Guid:
622 return ToGuid();
623 case ValueHandleType.TimeSpan:
624 return ToTimeSpan();
625 default:
627 }
628 }
629
630 public bool TryReadBase64(byte[] buffer, int offset, int count, out int actual)
631 {
632 if (_type == ValueHandleType.Base64)
633 {
636 _offset += actual;
637 _length -= actual;
638 return true;
639 }
640 if (_type == ValueHandleType.UTF8 && count >= 3 && _length % 4 == 0)
641 {
642 try
643 {
644 int num = Math.Min(count / 3 * 4, _length);
646 _offset += num;
647 _length -= num;
648 return true;
649 }
650 catch (FormatException)
651 {
652 }
653 }
654 actual = 0;
655 return false;
656 }
657
658 public bool TryReadChars(char[] chars, int offset, int count, out int actual)
659 {
660 if (_type == ValueHandleType.Unicode)
661 {
663 }
664 if (_type != ValueHandleType.UTF8)
665 {
666 actual = 0;
667 return false;
668 }
669 int num = offset;
670 int num2 = count;
671 byte[] buffer = _bufferReader.Buffer;
672 int num3 = _offset;
673 int num4 = _length;
674 bool flag = false;
676 while (true)
677 {
678 if (num2 > 0 && num4 > 0)
679 {
680 byte b = buffer[num3];
681 if (b < 128)
682 {
683 chars[num] = (char)b;
684 num3++;
685 num4--;
686 num++;
687 num2--;
688 continue;
689 }
690 }
691 if (num2 == 0 || num4 == 0 || flag)
692 {
693 break;
694 }
695 int chars2;
696 int num5;
697 try
698 {
699 if (num2 >= uTF8Encoding.GetMaxCharCount(num4) || num2 >= uTF8Encoding.GetCharCount(buffer, num3, num4))
700 {
701 chars2 = uTF8Encoding.GetChars(buffer, num3, num4, chars, num);
702 num5 = num4;
703 }
704 else
705 {
706 Decoder decoder = uTF8Encoding.GetDecoder();
707 num5 = Math.Min(num2, num4);
708 chars2 = decoder.GetChars(buffer, num3, num5, chars, num);
709 while (chars2 == 0)
710 {
711 if (num5 >= 3 && num2 < 2)
712 {
713 flag = true;
714 break;
715 }
716 chars2 = decoder.GetChars(buffer, num3 + num5, 1, chars, num);
717 num5++;
718 }
719 num5 = uTF8Encoding.GetByteCount(chars, num, chars2);
720 }
721 }
723 {
725 }
726 num3 += num5;
727 num4 -= num5;
728 num += chars2;
729 num2 -= chars2;
730 }
731 _offset = num3;
732 _length = num4;
733 actual = count - num2;
734 return true;
735 }
736
737 private bool TryReadUnicodeChars(char[] chars, int offset, int count, out int actual)
738 {
739 int num = Math.Min(count, _length / 2);
740 for (int i = 0; i < num; i++)
741 {
742 chars[offset + i] = (char)_bufferReader.GetInt16(_offset + i * 2);
743 }
744 _offset += num * 2;
745 _length -= num * 2;
746 actual = num;
747 return true;
748 }
749
751 {
752 if (_type == ValueHandleType.Dictionary)
753 {
755 return true;
756 }
757 value = null;
758 return false;
759 }
760
762 {
763 if (_type == ValueHandleType.Base64)
764 {
765 length = _length;
766 return true;
767 }
768 length = 0;
769 return false;
770 }
771
772 private string GetCharsText()
773 {
774 if (_length == 1 && _bufferReader.GetByte(_offset) == 49)
775 {
776 return "1";
777 }
779 }
780
781 private string GetUnicodeCharsText()
782 {
784 }
785
786 private string GetEscapedCharsText()
787 {
789 }
790
791 private string GetCharText()
792 {
793 int @char = GetChar();
794 if (@char > 65535)
795 {
798 return new string(span);
799 }
800 return ((char)@char).ToString();
801 }
802
803 private int GetChar()
804 {
805 return _offset;
806 }
807
808 private int GetInt8()
809 {
811 }
812
813 private int GetInt16()
814 {
816 }
817
818 private int GetInt32()
819 {
821 }
822
823 private long GetInt64()
824 {
826 }
827
828 private ulong GetUInt64()
829 {
831 }
832
833 private float GetSingle()
834 {
836 }
837
838 private double GetDouble()
839 {
841 }
842
843 private decimal GetDecimal()
844 {
846 }
847
849 {
851 }
852
853 private Guid GetGuid()
854 {
856 }
857
858 private void GetBase64(byte[] buffer, int offset, int count)
859 {
861 }
862
867
872}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static byte Min(byte val1, byte val2)
Definition Math.cs:912
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
unsafe string GetString(byte *bytes, int byteCount)
Definition Encoding.cs:973
static string GetString(PrefixHandleType type)
static PrefixHandleType GetAlphaPrefix(int index)
void Sign(XmlSigningNodeWriter writer)
static Base64Encoding s_base64Encoding
string GetQNameDictionaryText()
bool TryReadChars(char[] chars, int offset, int count, out int actual)
bool TryReadUnicodeChars(char[] chars, int offset, int count, out int actual)
void SetConstantValue(ValueHandleConstStringType constStringType)
ValueHandleType _type
void SetValue(ValueHandleType type)
bool TryGetByteArrayLength(out int length)
void SetDictionaryValue(int key)
void SetCharValue(int ch)
XmlDictionaryString GetDictionaryString()
bool TryReadBase64(byte[] buffer, int offset, int count, out int actual)
void GetBase64(byte[] buffer, int offset, int count)
override string ToString()
ValueHandle(XmlBufferReader bufferReader)
bool Equals2(string str, bool checkLower)
static readonly string[] s_constStrings
void SetValue(ValueHandleType type, int offset, int length)
readonly XmlBufferReader _bufferReader
Definition ValueHandle.cs:9
static Base64Encoding Base64Encoding
bool TryGetDictionaryString([NotNullWhen(true)] out XmlDictionaryString value)
void SetQNameValue(int prefix, int key)
XmlDictionaryString GetDictionaryString(int key)
bool IsWhitespaceUnicode(int offset, int length)
unsafe decimal GetDecimal(int offset)
unsafe float GetSingle(int offset)
object[] GetList(int offset, int count)
string GetUnicodeString(int offset, int length)
string GetEscapedString(int offset, int length)
unsafe double GetDouble(int offset)
bool IsWhitespaceUTF8(int offset, int length)
UniqueId GetUniqueId(int offset)
static XmlBufferReader Empty
void GetBase64(int srcOffset, byte[] buffer, int dstOffset, int count)
string GetString(int offset, int length)
static float ToSingle(string value)
static bool ToBoolean(string value)
static bool IsWhitespace(string s)
static decimal ToDecimal(string value)
static int ToInt32(string value)
static long ToInt64(string value)
static Guid ToGuid(string value)
static string ToString(byte[] buffer, int offset, int count)
static TimeSpan ToTimeSpan(string value)
static ulong ToUInt64(string value)
static double ToDouble(string value)
static DateTime ToDateTime(long value)
static string StripWhitespace(string s)
static UniqueId ToUniqueId(string value)
static XmlException CreateEncodingException(byte[] buffer, int offset, int count, Exception exception)