Guid.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. namespace PF {
  2. /** Simple implementation of a GUID.
  3. * \version Since 3.6.4 this struct works properly on platforms with different endianness such as Wii U.
  4. */
  5. public struct Guid {
  6. const string hex = "0123456789ABCDEF";
  7. public static readonly Guid zero = new Guid(new byte[16]);
  8. public static readonly string zeroString = new Guid(new byte[16]).ToString();
  9. readonly ulong _a, _b;
  10. public Guid (byte[] bytes) {
  11. // Pack 128 bits into 2 longs
  12. ulong a = ((ulong)bytes[0] << 8*0) |
  13. ((ulong)bytes[1] << 8*1) |
  14. ((ulong)bytes[2] << 8*2) |
  15. ((ulong)bytes[3] << 8*3) |
  16. ((ulong)bytes[4] << 8*4) |
  17. ((ulong)bytes[5] << 8*5) |
  18. ((ulong)bytes[6] << 8*6) |
  19. ((ulong)bytes[7] << 8*7);
  20. ulong b = ((ulong)bytes[8] << 8*0) |
  21. ((ulong)bytes[9] << 8*1) |
  22. ((ulong)bytes[10] << 8*2) |
  23. ((ulong)bytes[11] << 8*3) |
  24. ((ulong)bytes[12] << 8*4) |
  25. ((ulong)bytes[13] << 8*5) |
  26. ((ulong)bytes[14] << 8*6) |
  27. ((ulong)bytes[15] << 8*7);
  28. // Need to swap endianness on e.g Wii U
  29. _a = System.BitConverter.IsLittleEndian ? a : SwapEndianness(a);
  30. _b = System.BitConverter.IsLittleEndian ? b : SwapEndianness(b);
  31. }
  32. public Guid (string str) {
  33. _a = 0;
  34. _b = 0;
  35. if (str.Length < 32)
  36. throw new System.FormatException("Invalid Guid format");
  37. int counter = 0;
  38. int i = 0;
  39. int offset = 15*4;
  40. for (; counter < 16; i++) {
  41. if (i >= str.Length)
  42. throw new System.FormatException("Invalid Guid format. String too short");
  43. char c = str[i];
  44. if (c == '-') continue;
  45. //Neat trick, perhaps a bit slow, but one will probably not use Guid parsing that much
  46. int value = hex.IndexOf(char.ToUpperInvariant(c));
  47. if (value == -1)
  48. throw new System.FormatException("Invalid Guid format : "+c+" is not a hexadecimal character");
  49. _a |= (ulong)value << offset;
  50. //SetByte (counter,(byte)value);
  51. offset -= 4;
  52. counter++;
  53. }
  54. offset = 15*4;
  55. for (; counter < 32; i++) {
  56. if (i >= str.Length)
  57. throw new System.FormatException("Invalid Guid format. String too short");
  58. char c = str[i];
  59. if (c == '-') continue;
  60. //Neat trick, perhaps a bit slow, but one will probably not use Guid parsing that much
  61. int value = hex.IndexOf(char.ToUpperInvariant(c));
  62. if (value == -1)
  63. throw new System.FormatException("Invalid Guid format : "+c+" is not a hexadecimal character");
  64. _b |= (ulong)value << offset;
  65. //SetByte (counter,(byte)value);
  66. offset -= 4;
  67. counter++;
  68. }
  69. }
  70. public static Guid Parse (string input) {
  71. return new Guid(input);
  72. }
  73. /** Swaps between little and big endian */
  74. static ulong SwapEndianness (ulong value) {
  75. var b1 = (value >> 0) & 0xff;
  76. var b2 = (value >> 8) & 0xff;
  77. var b3 = (value >> 16) & 0xff;
  78. var b4 = (value >> 24) & 0xff;
  79. var b5 = (value >> 32) & 0xff;
  80. var b6 = (value >> 40) & 0xff;
  81. var b7 = (value >> 48) & 0xff;
  82. var b8 = (value >> 56) & 0xff;
  83. return b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 | b5 << 24 | b6 << 16 | b7 << 8 | b8 << 0;
  84. }
  85. public byte[] ToByteArray () {
  86. var bytes = new byte[16];
  87. byte[] ba = System.BitConverter.GetBytes(!System.BitConverter.IsLittleEndian ? SwapEndianness(_a) : _a);
  88. byte[] bb = System.BitConverter.GetBytes(!System.BitConverter.IsLittleEndian ? SwapEndianness(_b) : _b);
  89. for (int i = 0; i < 8; i++) {
  90. bytes[i] = ba[i];
  91. bytes[i+8] = bb[i];
  92. }
  93. return bytes;
  94. }
  95. private static System.Random random = new System.Random();
  96. public static Guid NewGuid () {
  97. var bytes = new byte[16];
  98. random.NextBytes(bytes);
  99. return new Guid(bytes);
  100. }
  101. public static bool operator == (Guid lhs, Guid rhs) {
  102. return lhs._a == rhs._a && lhs._b == rhs._b;
  103. }
  104. public static bool operator != (Guid lhs, Guid rhs) {
  105. return lhs._a != rhs._a || lhs._b != rhs._b;
  106. }
  107. public override bool Equals (System.Object _rhs) {
  108. if (!(_rhs is Guid)) return false;
  109. var rhs = (Guid)_rhs;
  110. return _a == rhs._a && _b == rhs._b;
  111. }
  112. public override int GetHashCode () {
  113. ulong ab = _a ^ _b;
  114. return (int)(ab >> 32) ^ (int)ab;
  115. }
  116. private static System.Text.StringBuilder text;
  117. public override string ToString () {
  118. if (text == null) {
  119. text = new System.Text.StringBuilder();
  120. }
  121. lock (text) {
  122. text.Length = 0;
  123. text.Append(_a.ToString("x16")).Append('-').Append(_b.ToString("x16"));
  124. return text.ToString();
  125. }
  126. }
  127. }
  128. }