mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-06 05:01:53 +00:00
UUID
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using Bon;
|
||||
using System;
|
||||
using Bon.Integrated;
|
||||
using System.Collections;
|
||||
|
||||
namespace GlitchyEngine.Core
|
||||
{
|
||||
[BonTarget]
|
||||
struct UUID : IHashable
|
||||
{
|
||||
[BonInclude]
|
||||
private uint64 _uuid;
|
||||
|
||||
private static Random s_Random = new .() ~ delete _;
|
||||
|
||||
static this()
|
||||
{
|
||||
gBonEnv.typeHandlers.Add(typeof(UUID),
|
||||
((.)new => Serialize, (.)new => Deserialize));
|
||||
}
|
||||
|
||||
/// Creates a new random UUID.
|
||||
public this()
|
||||
{
|
||||
_uuid = s_Random.NextU64();
|
||||
}
|
||||
|
||||
/// Creates a new UUID with the given value.
|
||||
public this(uint64 uuid)
|
||||
{
|
||||
_uuid = uuid;
|
||||
}
|
||||
|
||||
public int GetHashCode()
|
||||
{
|
||||
return (int)_uuid;
|
||||
}
|
||||
|
||||
static void Serialize(BonWriter writer, ValueView val, BonEnvironment env)
|
||||
{
|
||||
UUID uuid = *(UUID*)val.dataPtr;
|
||||
|
||||
Bon.Integrated.Serialize.[Friend]Integer(typeof(uint64), writer, ValueView(typeof(uint64), &uuid._uuid));
|
||||
}
|
||||
|
||||
public static Result<void> Deserialize(BonReader reader, ValueView val, BonEnvironment env)
|
||||
{
|
||||
Bon.Integrated.Deserialize.[Friend]Integer!(typeof(uint64), reader, val);
|
||||
|
||||
return .Ok;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user