This commit is contained in:
Simon Lübeß
2022-11-16 13:09:26 +01:00
parent a42544f171
commit 0155a38ae6
8 changed files with 367 additions and 21 deletions
+53
View File
@@ -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;
}
}
}