123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- namespace Pathfinding.Util {
-
-
-
-
- public struct Guid {
- const string hex = "0123456789ABCDEF";
- public static readonly Guid zero = new Guid(new byte[16]);
- public static readonly string zeroString = new Guid(new byte[16]).ToString();
- readonly ulong _a, _b;
- public Guid (byte[] bytes) {
-
- ulong a = ((ulong)bytes[0] << 8*0) |
- ((ulong)bytes[1] << 8*1) |
- ((ulong)bytes[2] << 8*2) |
- ((ulong)bytes[3] << 8*3) |
- ((ulong)bytes[4] << 8*4) |
- ((ulong)bytes[5] << 8*5) |
- ((ulong)bytes[6] << 8*6) |
- ((ulong)bytes[7] << 8*7);
- ulong b = ((ulong)bytes[8] << 8*0) |
- ((ulong)bytes[9] << 8*1) |
- ((ulong)bytes[10] << 8*2) |
- ((ulong)bytes[11] << 8*3) |
- ((ulong)bytes[12] << 8*4) |
- ((ulong)bytes[13] << 8*5) |
- ((ulong)bytes[14] << 8*6) |
- ((ulong)bytes[15] << 8*7);
-
- _a = System.BitConverter.IsLittleEndian ? a : SwapEndianness(a);
- _b = System.BitConverter.IsLittleEndian ? b : SwapEndianness(b);
- }
- public Guid (string str) {
- _a = 0;
- _b = 0;
- if (str.Length < 32)
- throw new System.FormatException("Invalid Guid format");
- int counter = 0;
- int i = 0;
- int offset = 15*4;
- for (; counter < 16; i++) {
- if (i >= str.Length)
- throw new System.FormatException("Invalid Guid format. String too short");
- char c = str[i];
- if (c == '-') continue;
-
- int value = hex.IndexOf(char.ToUpperInvariant(c));
- if (value == -1)
- throw new System.FormatException("Invalid Guid format : "+c+" is not a hexadecimal character");
- _a |= (ulong)value << offset;
-
- offset -= 4;
- counter++;
- }
- offset = 15*4;
- for (; counter < 32; i++) {
- if (i >= str.Length)
- throw new System.FormatException("Invalid Guid format. String too short");
- char c = str[i];
- if (c == '-') continue;
-
- int value = hex.IndexOf(char.ToUpperInvariant(c));
- if (value == -1)
- throw new System.FormatException("Invalid Guid format : "+c+" is not a hexadecimal character");
- _b |= (ulong)value << offset;
-
- offset -= 4;
- counter++;
- }
- }
- public static Guid Parse (string input) {
- return new Guid(input);
- }
-
- static ulong SwapEndianness (ulong value) {
- var b1 = (value >> 0) & 0xff;
- var b2 = (value >> 8) & 0xff;
- var b3 = (value >> 16) & 0xff;
- var b4 = (value >> 24) & 0xff;
- var b5 = (value >> 32) & 0xff;
- var b6 = (value >> 40) & 0xff;
- var b7 = (value >> 48) & 0xff;
- var b8 = (value >> 56) & 0xff;
- return b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 | b5 << 24 | b6 << 16 | b7 << 8 | b8 << 0;
- }
- public byte[] ToByteArray () {
- var bytes = new byte[16];
- byte[] ba = System.BitConverter.GetBytes(!System.BitConverter.IsLittleEndian ? SwapEndianness(_a) : _a);
- byte[] bb = System.BitConverter.GetBytes(!System.BitConverter.IsLittleEndian ? SwapEndianness(_b) : _b);
- for (int i = 0; i < 8; i++) {
- bytes[i] = ba[i];
- bytes[i+8] = bb[i];
- }
- return bytes;
- }
- private static System.Random random = new System.Random();
- public static Guid NewGuid () {
- var bytes = new byte[16];
- random.NextBytes(bytes);
- return new Guid(bytes);
- }
- public static bool operator == (Guid lhs, Guid rhs) {
- return lhs._a == rhs._a && lhs._b == rhs._b;
- }
- public static bool operator != (Guid lhs, Guid rhs) {
- return lhs._a != rhs._a || lhs._b != rhs._b;
- }
- public override bool Equals (System.Object _rhs) {
- if (!(_rhs is Guid)) return false;
- var rhs = (Guid)_rhs;
- return _a == rhs._a && _b == rhs._b;
- }
- public override int GetHashCode () {
- ulong ab = _a ^ _b;
- return (int)(ab >> 32) ^ (int)ab;
- }
- private static System.Text.StringBuilder text;
- public override string ToString () {
- if (text == null) {
- text = new System.Text.StringBuilder();
- }
- lock (text) {
- text.Length = 0;
- text.Append(_a.ToString("x16")).Append('-').Append(_b.ToString("x16"));
- return text.ToString();
- }
- }
- }
- }
|