86 lines
3.0 KiB
C#
86 lines
3.0 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace NEG.Utils
|
|
{
|
|
[Serializable]
|
|
public struct SerializableGuid : IComparable, IComparable<SerializableGuid>, IEquatable<SerializableGuid>
|
|
{
|
|
[SerializeField] private uint value0;
|
|
[SerializeField] private uint value1;
|
|
[SerializeField] private uint value2;
|
|
[SerializeField] private uint value3;
|
|
|
|
// public SerializableGuid(string hexRepresentation)
|
|
// {
|
|
// value0 = 0U;
|
|
// value1 = 0U;
|
|
// value2 = 0U;
|
|
// value3 = 0U;
|
|
// TryParse(hexRepresentation, out this);
|
|
// }
|
|
|
|
public static bool operator ==(SerializableGuid x, SerializableGuid y)
|
|
{
|
|
return (int)x.value0 == (int)y.value0 && (int)x.value1 == (int)y.value1 &&
|
|
(int)x.value2 == (int)y.value2 && (int)x.value3 == (int)y.value3;
|
|
}
|
|
|
|
public static bool operator !=(SerializableGuid x, SerializableGuid y) => !(x == y);
|
|
|
|
public static bool operator <(SerializableGuid x, SerializableGuid y)
|
|
{
|
|
if ((int)x.value0 != (int)y.value0)
|
|
return x.value0 < y.value0;
|
|
if ((int)x.value1 != (int)y.value1)
|
|
return x.value1 < y.value1;
|
|
return (int)x.value2 != (int)y.value2 ? x.value2 < y.value2 : x.value3 < y.value3;
|
|
}
|
|
|
|
public static bool operator > (SerializableGuid x, SerializableGuid y) => !(x < y) && !(x == y);
|
|
|
|
public override bool Equals(object obj) => obj is SerializableGuid guid && Equals(guid);
|
|
|
|
public bool Equals(SerializableGuid obj) => this == obj;
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return ((((((int)value0 * 397) ^ (int)value1) * 397) ^ (int)value2) * 397) ^
|
|
(int)value3;
|
|
}
|
|
|
|
public int CompareTo(object obj) => obj == null ? 1 : CompareTo((SerializableGuid)obj);
|
|
|
|
public int CompareTo(SerializableGuid rhs)
|
|
{
|
|
if (this < rhs)
|
|
return -1;
|
|
return this > rhs ? 1 : 0;
|
|
}
|
|
|
|
public bool Empty() => value0 == 0U && value1 == 0U && value2 == 0U && value3 == 0U;
|
|
|
|
// public static bool TryParse(string hex, out SerializableGuid result)
|
|
// {
|
|
// result = HexToSerializableGuidInternal(hex);
|
|
// return !result.Empty();
|
|
// }
|
|
|
|
public static SerializableGuid Generate()
|
|
{
|
|
byte[] array = Guid.NewGuid().ToByteArray();
|
|
|
|
var guid = new SerializableGuid
|
|
{
|
|
value0 = BitConverter.ToUInt32(new ReadOnlySpan<byte>(array, 0, 4)),
|
|
value1 = BitConverter.ToUInt32(new ReadOnlySpan<byte>(array, 4, 4)),
|
|
value2 = BitConverter.ToUInt32(new ReadOnlySpan<byte>(array, 8, 4)),
|
|
value3 = BitConverter.ToUInt32(new ReadOnlySpan<byte>(array, 12, 4))
|
|
};
|
|
return guid;
|
|
}
|
|
|
|
//public override string ToString() => SerializableGuidToHexInternal(ref this);
|
|
}
|
|
}
|