2026-05-08 01:50:15 +01:00
|
|
|
using MessagePack;
|
|
|
|
|
using MessagePack.Formatters;
|
|
|
|
|
|
|
|
|
|
namespace GameCult.Caching;
|
|
|
|
|
|
2026-09-14 21:01:07 +02:00
|
|
|
// An unset reference (the empty key) is nil on the wire and default(CultRecordRef<T>) in memory; a "" read from older
|
|
|
|
|
// stores is the same unset reference, so load then save is byte-stable.
|
2026-05-08 01:50:15 +01:00
|
|
|
public sealed class CultRecordRefFormatter<T> : IMessagePackFormatter<CultRecordRef<T>>
|
|
|
|
|
{
|
|
|
|
|
public void Serialize(ref MessagePackWriter writer, CultRecordRef<T> value, MessagePackSerializerOptions options)
|
|
|
|
|
{
|
2026-09-14 21:01:07 +02:00
|
|
|
if (value.Key.Value.Length == 0) writer.WriteNil();
|
|
|
|
|
else writer.Write(value.Key.Value);
|
2026-05-08 01:50:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CultRecordRef<T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
|
|
|
|
{
|
2026-09-14 21:01:07 +02:00
|
|
|
var key = reader.TryReadNil() ? null : reader.ReadString();
|
|
|
|
|
return string.IsNullOrEmpty(key) ? default : new CultRecordRef<T>(new CultRecordKey(key!));
|
2026-05-08 01:50:15 +01:00
|
|
|
}
|
|
|
|
|
}
|