123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- namespace WingServerCommon.Utilities
- {
- public static class IdHelper
- {
-
-
-
-
- public static T Generate<T>()
- {
- var guid = Guid.NewGuid();
- var type = typeof(T);
- if (typeof(T) == typeof(string))
- {
- return (T)(object)guid.ToString("N").ToUpper();
- }
- if (typeof(T) == typeof(byte[]))
- {
- var buffer = guid.ToByteArray();
- return (T)Convert.ChangeType(buffer, type);
- }
- if (typeof(T) == typeof(FasterUId))
- {
- var guidData = Guid.NewGuid().ToByteArray();
- return (T)(object)new FasterUId(guidData);
- }
- throw new InvalidOperationException($"Operation [Generate<{type.Name}>] is not supported.");
- }
-
-
-
-
-
-
- public static FasterUId Generate(long part1, long part2)
- {
- return new FasterUId(part1, part2);
- }
- }
-
-
-
- public struct FasterUId : IEquatable<FasterUId>
- {
- public static readonly FasterUId Empty = new FasterUId(0, 0);
- public long Part1 { get; set; }
- public long Part2 { get; set; }
- public FasterUId(long part1, long part2)
- {
- Part1 = part1;
- Part2 = part2;
- }
- public FasterUId(string id)
- {
- if (Guid.TryParse(id, out var guid))
- {
- var guidData = guid.ToByteArray();
- using (var ms = new MemoryStream(guidData))
- {
- var part1Data = new byte[8];
- var part2Data = new byte[8];
- ms.Read(part1Data, 0, part1Data.Length);
- ms.Read(part2Data, 0, part2Data.Length);
- Part1 = BitConverter.ToInt64(part1Data, 0);
- Part2 = BitConverter.ToInt64(part2Data, 0);
- }
- }
- else
- {
- throw new InvalidDataException("Can not convert to FasterId, the string is not matched.");
- }
- }
-
-
-
-
- public FasterUId(byte[] data)
- {
- if (data.Length != 16)
- {
- throw new InvalidDataException("Can not convert to FasterId, the data size is not matched.");
- }
- using (var ms = new MemoryStream(data))
- {
- var part1Data = new byte[8];
- var part2Data = new byte[8];
- ms.Read(part1Data, 0, part1Data.Length);
- ms.Read(part2Data, 0, part2Data.Length);
- Part1 = BitConverter.ToInt64(part1Data, 0);
- Part2 = BitConverter.ToInt64(part2Data, 0);
- }
- }
-
-
-
-
-
- public bool Equals(FasterUId other)
- {
- return Part1 == other.Part1 && Part2 == other.Part2;
- }
- public override bool Equals(object obj)
- {
- return obj is FasterUId other && Equals(other);
- }
- public override int GetHashCode()
- {
- var hashCode = Part1.GetHashCode();
- hashCode = (hashCode * 397) ^ Part2.GetHashCode();
- return hashCode;
- }
- public static bool operator ==(FasterUId left, FasterUId right)
- {
- return left.Equals(right);
- }
- public static bool operator !=(FasterUId left, FasterUId right)
- {
- return !left.Equals(right);
- }
- public static implicit operator byte[](FasterUId id)
- {
- using (var ms = new MemoryStream())
- {
- var part1Data = BitConverter.GetBytes(id.Part1);
- var part2Data = BitConverter.GetBytes(id.Part2);
- ms.Write(part1Data, 0, part1Data.Length);
- ms.Write(part2Data, 0, part2Data.Length);
- return ms.ToArray();
- }
- }
- public static explicit operator FasterUId(byte[] data)
- {
- if (data.Length != 16)
- {
- throw new InvalidDataException("Can not convert to FasterId, the data size is not matched.");
- }
- using (var ms = new MemoryStream(data))
- {
- var part1Data = new byte[8];
- var part2Data = new byte[8];
- ms.Read(part1Data, 0, part1Data.Length);
- ms.Read(part2Data, 0, part2Data.Length);
- return new FasterUId();
- }
- }
- public override string ToString()
- {
- using (var ms = new MemoryStream())
- {
- var part1Data = BitConverter.GetBytes(Part1);
- var part2Data = BitConverter.GetBytes(Part2);
- ms.Write(part1Data, 0, part1Data.Length);
- ms.Write(part2Data, 0, part2Data.Length);
- return new Guid(ms.ToArray()).ToString("N").ToUpper();
- }
- }
- }
- }
|