Rewrite CultCache as attribute-first storage

This commit is contained in:
Meta Vens 2026-05-08 01:13:15 +01:00
commit da94389ee2
29 changed files with 1496 additions and 2488 deletions

View file

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace GameCult.Caching.MessagePack.Generator
{
[Generator]
public class CultDocumentMessagePackGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var documentTypes = context.SyntaxProvider
.CreateSyntaxProvider(
static (node, _) => node is ClassDeclarationSyntax cds && cds.AttributeLists.Count > 0,
static (ctx, _) => ctx.SemanticModel.GetDeclaredSymbol(ctx.Node) as INamedTypeSymbol)
.Where(static symbol => symbol is not null)!
.Select((symbol, _) => symbol!)
.Where(static symbol => symbol.GetAttributes().Any(attribute =>
attribute.AttributeClass?.ToDisplayString() == "GameCult.Caching.CultDocumentAttribute"));
context.RegisterSourceOutput(documentTypes.Collect(), (spc, types) =>
{
var builder = new StringBuilder();
builder.AppendLine("// <auto-generated/>");
builder.AppendLine("#nullable enable");
builder.AppendLine("using MessagePack.Formatters;");
builder.AppendLine("namespace GameCult.Caching.MessagePack");
builder.AppendLine("{");
builder.AppendLine(" public static partial class GeneratedCultDocumentResolvers");
builder.AppendLine(" {");
builder.AppendLine(" public static IMessagePackFormatter<T>? GetFormatter<T>()");
builder.AppendLine(" {");
builder.AppendLine(" return null;");
builder.AppendLine(" }");
builder.AppendLine(" }");
builder.AppendLine("}");
spc.AddSource("GeneratedCultDocumentResolvers.g.cs", SourceText.From(builder.ToString(), Encoding.UTF8));
});
}
}
}

View file

@ -1,264 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace GameCult.Caching.MessagePack.Generator
{
/// <summary>
/// Generates MessagePack formatters and resolver mappings for concrete <c>DatabaseEntry</c> types.
/// </summary>
[Generator]
public class DatabaseEntryMessagePackGenerator : IIncrementalGenerator
{
/// <summary>
/// Configures the incremental generator pipeline.
/// </summary>
/// <param name="ctx">The generator initialization context.</param>
public void Initialize(IncrementalGeneratorInitializationContext ctx)
{
// Select DatabaseEntry subclass declarations
var databaseEntryTypes = ctx.SyntaxProvider
.CreateSyntaxProvider(
static (s, _) => s is ClassDeclarationSyntax cds && cds.BaseList != null,
static (ctx, _) => ctx.SemanticModel.GetDeclaredSymbol(ctx.Node) as INamedTypeSymbol)
.Where(static s => s is not null)!
.Select((s, _) => s!)
.Where(symbol =>
{
if (symbol.IsAbstract) return false;
var baseType = symbol.BaseType;
while (baseType != null)
{
if (baseType.ToDisplayString() == "GameCult.Caching.DatabaseEntry")
return true;
baseType = baseType.BaseType;
}
return false;
});
// 2. Distinct types
var collected = databaseEntryTypes.Collect();
// 3. Register output
ctx.RegisterSourceOutput(collected, (spc, types) =>
{
//if (types.Length == 0) return;
var sbResolvers = new StringBuilder();
var sbFormatters = new StringBuilder();
sbResolvers.AppendLine("// <auto-generated/>");
sbResolvers.AppendLine("#nullable enable");
sbResolvers.AppendLine("using System;");
sbResolvers.AppendLine("using MessagePack.Formatters;");
sbResolvers.AppendLine("namespace GameCult.Caching.MessagePack");
sbResolvers.AppendLine("{");
sbResolvers.AppendLine(" static partial class GeneratedDatabaseEntryResolvers");
sbResolvers.AppendLine(" {");
sbResolvers.AppendLine(" public static IMessagePackFormatter<T>? GetFormatter<T>()");
sbResolvers.AppendLine(" {");
sbResolvers.AppendLine(" var t = typeof(T);");
foreach (var type in types)
{
var fullName = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var formatterName = $"{Sanitize(type)}__MessagePackFormatter";
var fmtSrc = GenerateFormatterFor(type, formatterName);
sbFormatters.AppendLine(fmtSrc);
sbResolvers.AppendLine(
$" if (t == typeof({fullName})) return (IMessagePackFormatter<T>)(object){formatterName}.Instance;");
}
sbResolvers.AppendLine(" return null;");
sbResolvers.AppendLine(" }");
sbResolvers.AppendLine(" }");
sbResolvers.AppendLine("}");
spc.AddSource("GeneratedDatabaseEntryFormatters.g.cs", SourceText.From(sbFormatters.ToString(), Encoding.UTF8));
spc.AddSource("GeneratedDatabaseEntryResolvers.g.cs", SourceText.From(sbResolvers.ToString(), Encoding.UTF8));
});
}
private static string Sanitize(ISymbol symbol)
{
var s = symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
return s.Replace(".", "_")
.Replace("global::", string.Empty)
.Replace("<", "_")
.Replace(">", "_")
.Replace(",", "_")
.Replace(" ", "_");
}
// Collect members, order them, generate the formatter
private static string GenerateFormatterFor(INamedTypeSymbol type, string formatterName)
{
var sb = new StringBuilder();
sb.AppendLine("// <auto-generated/>");
sb.AppendLine("#nullable enable");
sb.AppendLine("using System;");
sb.AppendLine("using MessagePack;");
sb.AppendLine("using MessagePack.Formatters;");
sb.AppendLine();
var ns = "GameCult.Caching.MessagePack";
sb.AppendLine($"namespace {ns}");
sb.AppendLine("{");
var typeFull = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
sb.AppendLine($" internal sealed class {formatterName} : IMessagePackFormatter<{typeFull}>");
sb.AppendLine(" {");
sb.AppendLine($" public static readonly {formatterName} Instance = new {formatterName}();");
sb.AppendLine($" private {formatterName}() {{ }}");
sb.AppendLine();
var members = CollectMembers(type);
var slots = AssignSlots(type, members);
var maxSlot = slots.Count == 0 ? -1 : slots.Max(m => m.slot);
// Serialize
sb.AppendLine($" public void Serialize(ref MessagePackWriter writer, {typeFull}? value, MessagePackSerializerOptions options)");
sb.AppendLine(" {");
sb.AppendLine(" if (value == null) { writer.WriteNil(); return; }");
sb.AppendLine($" writer.WriteArrayHeader({maxSlot + 1});");
for (int i = 0; i <= maxSlot; i++)
{
var member = slots.FirstOrDefault(m => m.slot == i);
if (member.name == null)
{
sb.AppendLine(" writer.WriteNil();");
continue;
}
var tname = member.typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var access = $"value.{member.name}";
sb.AppendLine(
tname == "global::System.Guid"
? $" options.Resolver.GetFormatterWithVerify<System.Guid>().Serialize(ref writer, {access}, options);"
: $" options.Resolver.GetFormatterWithVerify<{tname}>().Serialize(ref writer, {access}, options);");
}
sb.AppendLine(" }");
// Deserialize
sb.AppendLine();
sb.AppendLine($" public {typeFull}? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)");
sb.AppendLine(" {");
sb.AppendLine(" if (reader.TryReadNil()) return null;");
sb.AppendLine($" var count = reader.ReadArrayHeader();");
sb.AppendLine($" var obj = new {typeFull}();");
sb.AppendLine(" for (var i = 0; i < count; i++)");
sb.AppendLine(" {");
sb.AppendLine(" switch (i)");
sb.AppendLine(" {");
foreach (var (slot, name, tsym) in slots)
{
var tname = tsym.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
sb.AppendLine($" case {slot}:");
sb.AppendLine(
tname == "global::System.Guid"
? $" obj.{name} = options.Resolver.GetFormatterWithVerify<System.Guid>().Deserialize(ref reader, options);"
: $" obj.{name} = options.Resolver.GetFormatterWithVerify<{tname}>().Deserialize(ref reader, options);");
sb.AppendLine(" break;");
}
sb.AppendLine(" default:");
sb.AppendLine(" reader.Skip();");
sb.AppendLine(" break;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine(" return obj;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine("}");
return sb.ToString();
}
private static List<(string name, ITypeSymbol typeSymbol, int? key, bool ignore)> CollectMembers(INamedTypeSymbol type)
{
var members = new List<(string, ITypeSymbol, int?, bool)>();
foreach (var f in type.GetMembers().OfType<IFieldSymbol>())
{
if (f.IsStatic) continue;
var ignore = f.GetAttributes().Any(a => a.AttributeClass?.ToDisplayString() == "MessagePack.IgnoreMemberAttribute");
var key = GetKeyValue(f);
members.Add((f.Name, f.Type, key, ignore));
}
foreach (var p in type.GetMembers().OfType<IPropertySymbol>())
{
if (p.IsStatic || p.SetMethod == null || p.GetMethod == null) continue;
var ignore = p.GetAttributes().Any(a => a.AttributeClass?.ToDisplayString() == "MessagePack.IgnoreMemberAttribute");
var key = GetKeyValue(p);
members.Add((p.Name, p.Type, key, ignore));
}
return members;
}
private static List<(int slot, string name, ITypeSymbol typeSymbol)> AssignSlots(
INamedTypeSymbol type,
List<(string name, ITypeSymbol typeSymbol, int? key, bool ignore)> members)
{
members = members.Where(m => !m.ignore).ToList();
var slots = new List<(int, string, ITypeSymbol)>();
var id = FindIdMember(type);
if (id != null)
{
slots.Add((0, id.Value.name, id.Value.typeSymbol));
members.RemoveAll(m => m.name == id.Value.name);
}
foreach (var member in members.Where(m => m.key.HasValue).OrderBy(m => m.key!.Value))
{
slots.Add((member.key!.Value, member.name, member.typeSymbol));
}
var nextSlot = slots.Count == 0 ? 0 : slots.Max(s => s.Item1) + 1;
foreach (var member in members.Where(m => !m.key.HasValue))
{
slots.Add((nextSlot, member.name, member.typeSymbol));
nextSlot++;
}
return slots.OrderBy(s => s.Item1).ToList();
}
private static int? GetKeyValue(ISymbol sym)
{
foreach (var attr in sym.GetAttributes())
{
var n = attr.AttributeClass?.ToDisplayString();
if (n == "MessagePack.KeyAttribute" || n == "MessagePack.Key")
{
if (attr.ConstructorArguments.Length >= 1 && attr.ConstructorArguments[0].Value is int i)
return i;
}
}
return null;
}
private static (string name, ITypeSymbol typeSymbol)? FindIdMember(INamedTypeSymbol type)
{
var cur = type;
while (cur != null)
{
foreach (var f in cur.GetMembers().OfType<IFieldSymbol>())
{
if ((f.Name == "ID") && f.Type.ToDisplayString() == "System.Guid")
return (f.Name, f.Type);
}
cur = cur.BaseType;
}
return null;
}
}
}

View file

@ -1,95 +0,0 @@
using System;
using System.Reflection;
namespace GameCult.Caching.MessagePack;
/// <summary>
/// Represents a canonical CultCache payload envelope for cross-runtime exchange.
/// </summary>
public sealed class CultCacheEnvelope
{
/// <summary>
/// Gets or sets the logical document key for the serialized entry.
/// </summary>
public string Key { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the shared document type identifier for the serialized entry.
/// </summary>
public string Type { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the raw MessagePack payload bytes for the entry.
/// </summary>
public byte[] Payload { get; set; } = Array.Empty<byte>();
/// <summary>
/// Gets or sets the RFC3339 timestamp associated with the serialized payload.
/// </summary>
public string StoredAt { get; set; } = string.Empty;
}
/// <summary>
/// Exposes canonical MessagePack payload helpers for <see cref="DatabaseEntry"/> values.
/// </summary>
public static class CultCacheEnvelopeSerialization
{
/// <summary>
/// Serializes a concrete entry to the canonical MessagePack payload bytes used by CultCache.
/// </summary>
public static byte[] SerializePayload(DatabaseEntry entry)
{
if (entry == null) throw new ArgumentNullException(nameof(entry));
var method = typeof(CultCacheEnvelopeSerialization)
.GetMethod(nameof(SerializeGeneric), BindingFlags.NonPublic | BindingFlags.Static)
?.MakeGenericMethod(entry.GetType())
?? throw new InvalidOperationException("Failed to locate typed CultCache envelope serializer.");
return (byte[])method.Invoke(null, [entry])!;
}
/// <summary>
/// Deserializes canonical MessagePack payload bytes into a concrete entry type.
/// </summary>
public static T DeserializePayload<T>(byte[] payload) where T : DatabaseEntry
{
if (payload == null) throw new ArgumentNullException(nameof(payload));
return DatabaseEntrySerialization.Deserialize<T>(payload);
}
/// <summary>
/// Deserializes canonical MessagePack payload bytes into an abstract <see cref="DatabaseEntry"/>.
/// </summary>
public static DatabaseEntry DeserializePayload(byte[] payload)
{
if (payload == null) throw new ArgumentNullException(nameof(payload));
return DatabaseEntrySerialization.Deserialize<DatabaseEntry>(payload);
}
/// <summary>
/// Creates a full exchange envelope for a concrete entry using the supplied metadata.
/// </summary>
public static CultCacheEnvelope CreateEnvelope(
DatabaseEntry entry,
string key,
string type,
string storedAt)
{
if (entry == null) throw new ArgumentNullException(nameof(entry));
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("Envelope key must be non-empty.", nameof(key));
if (string.IsNullOrWhiteSpace(type)) throw new ArgumentException("Envelope type must be non-empty.", nameof(type));
if (string.IsNullOrWhiteSpace(storedAt)) throw new ArgumentException("Envelope storedAt must be non-empty.", nameof(storedAt));
return new CultCacheEnvelope
{
Key = key,
Type = type,
Payload = SerializePayload(entry),
StoredAt = storedAt
};
}
private static byte[] SerializeGeneric<T>(T entry) where T : DatabaseEntry
{
return DatabaseEntrySerialization.Serialize(entry);
}
}

View file

@ -0,0 +1,146 @@
using System;
using GameCult.Caching;
using MessagePack;
using MessagePack.Formatters;
using MessagePack.Resolvers;
namespace GameCult.Caching.MessagePack;
public sealed class CultRecordRefFormatter<T> : IMessagePackFormatter<CultRecordRef<T>>
{
public void Serialize(ref MessagePackWriter writer, CultRecordRef<T> value, MessagePackSerializerOptions options)
{
options.Resolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.Key.Value, options);
}
public CultRecordRef<T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
var key = options.Resolver.GetFormatterWithVerify<string>().Deserialize(ref reader, options) ?? string.Empty;
return new CultRecordRef<T>(new CultRecordKey(key));
}
}
public sealed class CultDocumentResolver : IFormatterResolver
{
public static readonly CultDocumentResolver Instance = new();
private CultDocumentResolver() { }
public IMessagePackFormatter<T>? GetFormatter<T>()
{
var generated = GeneratedCultDocumentResolvers.GetFormatter<T>();
if (generated != null)
{
return generated;
}
var type = typeof(T);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(CultRecordRef<>))
{
var formatterType = typeof(CultRecordRefFormatter<>).MakeGenericType(type.GetGenericArguments()[0]);
return (IMessagePackFormatter<T>)Activator.CreateInstance(formatterType)!;
}
return null;
}
}
public static class CultDocumentMessagePackSerialization
{
public static readonly MessagePackSerializerOptions Options =
MessagePackSerializerOptions.Standard
.WithResolver(CompositeResolver.Create(
CultDocumentResolver.Instance,
StandardResolver.Instance))
.WithSecurity(MessagePackSecurity.UntrustedData);
public static byte[] Serialize<T>(T value)
{
return MessagePackSerializer.Serialize(value, Options);
}
public static T Deserialize<T>(byte[] payload)
{
return MessagePackSerializer.Deserialize<T>(payload, Options);
}
public static byte[] SerializeUntyped(object value, Type type)
{
return MessagePackSerializer.Serialize(type, value, Options);
}
public static object DeserializeUntyped(Type type, byte[] payload)
{
return MessagePackSerializer.Deserialize(type, payload, Options);
}
}
public static class GeneratedCultDocumentResolvers
{
public static IMessagePackFormatter<T>? GetFormatter<T>() => null;
}
public class SingleFileMessagePackBackingStore : SingleFileBackingStore
{
public SingleFileMessagePackBackingStore(string filePath) : base(filePath)
{
}
protected override byte[] SerializeSnapshot(CultPersistedStoreSnapshot snapshot)
{
return CultDocumentMessagePackSerialization.Serialize(snapshot);
}
protected override CultPersistedStoreSnapshot DeserializeSnapshot(byte[] data)
{
return CultDocumentMessagePackSerialization.Deserialize<CultPersistedStoreSnapshot>(data);
}
protected override byte[] SerializePayload(object document)
{
return CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType());
}
protected override object DeserializePayload(Type documentType, byte[] payload)
{
return CultDocumentMessagePackSerialization.DeserializeUntyped(documentType, payload);
}
}
public class MultiFileMessagePackBackingStore : MultiFileBackingStore
{
public MultiFileMessagePackBackingStore(string path) : base(path)
{
}
protected override byte[] SerializeRecord(CultPersistedRecord record)
{
return CultDocumentMessagePackSerialization.Serialize(record);
}
protected override CultPersistedRecord DeserializeRecord(byte[] data)
{
return CultDocumentMessagePackSerialization.Deserialize<CultPersistedRecord>(data);
}
protected override byte[] SerializeCatalog(CultSchemaCatalogEntry[] catalog)
{
return CultDocumentMessagePackSerialization.Serialize(catalog);
}
protected override CultSchemaCatalogEntry[] DeserializeCatalog(byte[] data)
{
return CultDocumentMessagePackSerialization.Deserialize<CultSchemaCatalogEntry[]>(data);
}
protected override byte[] SerializePayload(object document)
{
return CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType());
}
protected override object DeserializePayload(Type documentType, byte[] payload)
{
return CultDocumentMessagePackSerialization.DeserializeUntyped(documentType, payload);
}
public override string Extension => "msgpack";
}

View file

@ -1,53 +0,0 @@
using System;
using System.Collections.Concurrent;
using MessagePack;
using MessagePack.Formatters;
namespace GameCult.Caching.MessagePack
{
/// <summary>
/// Resolver that knows how to resolve:<br/>
/// 1. Source-generated singleton formatters for DatabaseEntry subclasses<br/>
/// 2. Generic DatabaseLink&lt;T&gt; formatter
/// </summary>
public sealed class DatabaseEntryResolver : IFormatterResolver
{
/// <summary>
/// Shared singleton instance of the resolver.
/// </summary>
public static readonly DatabaseEntryResolver Instance = new DatabaseEntryResolver();
private readonly ConcurrentDictionary<Type, object> _formatterCache = new();
private DatabaseEntryResolver() { }
/// <summary>
/// Resolves a formatter for a cache entry type or a <see cref="DatabaseLink{T}"/>.
/// </summary>
/// <typeparam name="T">The requested serialized type.</typeparam>
/// <returns>The formatter if one is available; otherwise, <c>null</c>.</returns>
public IMessagePackFormatter<T>? GetFormatter<T>()
{
// 1. Check generated resolver mapping
var fmt = GeneratedDatabaseEntryResolvers.GetFormatter<T>();
if (fmt != null) return fmt;
// 2. Provide generic DatabaseLink<T> formatter
var t = typeof(T);
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(DatabaseLink<>))
{
return (IMessagePackFormatter<T>)_formatterCache.GetOrAdd(
t,
static linkType =>
{
var valueType = linkType.GetGenericArguments()[0];
var formatterType = typeof(DatabaseLinkFormatter<>).MakeGenericType(valueType);
return Activator.CreateInstance(formatterType)!;
});
}
// 3. Nothing found
return null;
}
}
}

View file

@ -1,24 +0,0 @@
using MessagePack;
using MessagePack.Resolvers;
namespace GameCult.Caching.MessagePack;
internal static class DatabaseEntrySerialization
{
public static readonly MessagePackSerializerOptions Options =
MessagePackSerializerOptions.Standard
.WithResolver(CompositeResolver.Create(
DatabaseEntryResolver.Instance,
StandardResolver.Instance))
.WithSecurity(MessagePackSecurity.UntrustedData);
public static byte[] Serialize<T>(T value)
{
return MessagePackSerializer.Serialize(value, Options);
}
public static T Deserialize<T>(byte[] payload)
{
return MessagePackSerializer.Deserialize<T>(payload, Options);
}
}

View file

@ -1,33 +0,0 @@
using System;
using MessagePack;
using MessagePack.Formatters;
namespace GameCult.Caching.MessagePack
{
/// <summary>
/// Serializes <see cref="DatabaseLink{T}"/> values as their linked identifier only.
/// </summary>
/// <typeparam name="T">The target entry type.</typeparam>
public sealed class DatabaseLinkFormatter<T> : IMessagePackFormatter<DatabaseLink<T>?> where T : DatabaseEntry
{
/// <inheritdoc />
public void Serialize(ref MessagePackWriter writer, DatabaseLink<T>? value, MessagePackSerializerOptions options)
{
if (value == null) { writer.WriteNil(); return; }
writer.WriteArrayHeader(1);
var fmt = options.Resolver.GetFormatterWithVerify<Guid>();
fmt.Serialize(ref writer, value.LinkID, options);
}
/// <inheritdoc />
public DatabaseLink<T>? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
if (reader.TryReadNil()) return null;
var c = reader.ReadArrayHeader();
if (c != 1) throw new MessagePackSerializationException($"Invalid DatabaseLink<{typeof(T).Name}> array length {c}, expected 1");
var id = options.Resolver.GetFormatterWithVerify<Guid>().Deserialize(ref reader, options);
var link = new DatabaseLink<T> { LinkID = id };
return link;
}
}
}

View file

@ -1,12 +0,0 @@
// This stub ensures the project compiles before the source generator runs.
using MessagePack.Formatters;
namespace GameCult.Caching.MessagePack
{
static partial class GeneratedDatabaseEntryResolvers
{
// The generator will emit the real implementation.
public static IMessagePackFormatter<T>? GetFormatter<T>() => null;
}
}

View file

@ -1,54 +0,0 @@
using MessagePack;
namespace GameCult.Caching.MessagePack;
/// <summary>
/// Persists all cache entries to a single MessagePack file.
/// </summary>
public class SingleFileMessagePackBackingStore : SingleFileBackingStore
{
/// <summary>
/// Initializes a single-file MessagePack backing store.
/// </summary>
/// <param name="filePath">The path to the backing file.</param>
public SingleFileMessagePackBackingStore(string filePath) : base(filePath) { }
/// <inheritdoc />
public override byte[] Serialize(DatabaseEntry[] entries)
{
return DatabaseEntrySerialization.Serialize(entries);
}
/// <inheritdoc />
public override DatabaseEntry[] Deserialize(byte[] data)
{
return DatabaseEntrySerialization.Deserialize<DatabaseEntry[]>(data);
}
}
/// <summary>
/// Persists each cache entry to its own MessagePack file.
/// </summary>
public class MultiFileMessagePackBackingStore : MultiFileBackingStore
{
/// <summary>
/// Initializes a multi-file MessagePack backing store rooted at the supplied directory.
/// </summary>
/// <param name="path">The root directory for stored entry files.</param>
public MultiFileMessagePackBackingStore(string path) : base(path) { }
/// <inheritdoc />
public override byte[] Serialize(DatabaseEntry entry)
{
return DatabaseEntrySerialization.Serialize(entry);
}
/// <inheritdoc />
public override DatabaseEntry Deserialize(byte[] data)
{
return DatabaseEntrySerialization.Deserialize<DatabaseEntry>(data);
}
/// <inheritdoc />
public override string Extension => "msgpack";
}

View file

@ -1,39 +1,37 @@
# GameCult.Caching.MessagePack
`GameCult.Caching.MessagePack` provides MessagePack-backed persistence for `GameCult.Caching`.
## Scope
The package focuses on:
- MessagePack storage for `DatabaseEntry` models
- single-file and multi-file store implementations
- support for generated formatters for concrete cache-entry types
- formatter support for `DatabaseLink<T>`
- raw persisted-envelope helpers for exact payload relay
`GameCult.Caching.MessagePack` provides MessagePack persistence for the
attribute-first `GameCult.Caching` stack.
## Included Types
- `SingleFileMessagePackBackingStore`
- `MultiFileMessagePackBackingStore`
- `DatabaseEntryResolver`
- `DatabaseLinkFormatter<T>`
- `CultCacheEnvelope`
- `CultCacheEnvelopeSerialization`
- `CultDocumentMessagePackSerialization`
- `CultDocumentResolver`
- `CultRecordRefFormatter<T>`
## Why The Generator Matters
## What It Does
`DatabaseEntry` is abstract. Concrete subclasses need MessagePack formatters to be serializable without hand-writing formatters for each model.
- stores CultCache snapshots and records in MessagePack
- serializes plain attributed document payloads
- keeps explicit `CultRecordRef<T>` values compact on disk/wire
- preserves the cache/store split where metadata lives outside the domain model
That is the role of:
## Example Model
- `GameCult.Caching.MessagePack.Generator`
- `GameCult.Caching.MessagePack.Analyzers`
```csharp
using GameCult.Caching;
using MessagePack;
In practice:
- this package gives you the stores and resolver support
- the generator package produces the actual concrete formatters
[CultDocument("gamecult.item_data", "gamecult.item_data.v1")]
[MessagePackObject]
public sealed class ItemData
{
[Key(0)] [CultName] public string Name = string.Empty;
[Key(1)] public int Value;
}
```
## Typical Usage
@ -48,94 +46,11 @@ cache.AddBackingStore(store);
await cache.PullAllBackingStoresAsync();
```
## Single-File vs Multi-File
## Notes
### `SingleFileMessagePackBackingStore`
Use this when you want one snapshot file containing all entries.
Use cases:
- compact settings data
- simple local persistence
- write-all-at-once workflows
```csharp
var store = new SingleFileMessagePackBackingStore("cache.msgpack");
cache.AddBackingStore(store);
```
### `MultiFileMessagePackBackingStore`
Use this when you want one file per entry.
Use cases:
- large datasets
- debugging persisted entries individually
- change observation through the multi-file base store
```csharp
var store = new MultiFileMessagePackBackingStore("Data");
cache.AddBackingStore(store, typeof(PlayerData));
```
## `DatabaseLink<T>`
`DatabaseLink<T>` is a lightweight reference type used by the cache layer. The MessagePack formatter serializes only the linked `Guid`, not the fully loaded target object.
That means:
- links stay compact on disk
- linked objects are resolved through the active `CultCache`
- deserialized links depend on `DatabaseLinkBase.Cache` pointing at the active cache
## Example Model
```csharp
using GameCult.Caching;
using MessagePack;
[MessagePackObject]
public class ItemData : DatabaseEntry, INamedEntry
{
[Key(1)] public string Name = string.Empty;
[Key(2)] public int Value;
[IgnoreMember]
public string EntryName
{
get => Name;
set => Name = value;
}
}
```
## Practical Notes
- For polymorphic `DatabaseEntry` serialization, use this package with the generator/analyzer package.
- The backing stores build on the semantics of `CultCache`, including primary-store routing and type-specific store routing.
- `MultiFileMessagePackBackingStore` inherits the rename-safe and atomic-write behavior of the hardened `MultiFileBackingStore` base class.
## Raw Envelope Relay
If a neighboring process already shares the same payload contract and just
needs the exact persisted MessagePack bytes, use `CultCacheEnvelope` and
`CultCacheEnvelopeSerialization`.
That gives you:
- the stable cache key
- the explicit entry/document type
- the exact payload bytes
- the stored-at timestamp
This is the seam the newer CultNet raw document/snapshot lane uses for
bit-compatible neighbors. It keeps the transport from decoding bytes into a
generic sludge object only to re-encode the same thing again five seconds
later.
One important constraint: concrete `DatabaseEntry` subclasses still need real
MessagePack formatters. If a consumer assembly has not generated or registered
formatters for a subtype, supply an explicit payload codec at the integration
boundary instead of assuming the abstract resolver will become psychic.
- Payload bytes are MessagePack. Store metadata and schema catalogs are also
persisted through MessagePack in this package.
- The generator package now targets attributed document types instead of
`DatabaseEntry` subclasses.
- The raw CultNet document lane should treat these payload bytes as already
blessed, not decode and re-encode them for sport.

View file

@ -5,6 +5,7 @@
<ItemGroup>
<ProjectReference Include="..\GameCult.Caching\GameCult.Caching.csproj" />
<ProjectReference Include="..\GameCult.Caching.MessagePack\GameCult.Caching.MessagePack.csproj" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View file

@ -1,238 +1,77 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using GameCult.Caching.MessagePack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace GameCult.Caching.NewtonsoftJson;
/// <summary>
/// Persists all cache entries to a single JSON file using Newtonsoft.Json.
/// </summary>
public class SingleFileNewtonsoftJsonBackingStore : SingleFileBackingStore
{
private readonly KnownDatabaseEntryTypes _knownTypes = new();
/// <summary>
/// Initializes a single-file Newtonsoft.Json backing store.
/// </summary>
/// <param name="filePath">The path to the backing file.</param>
public SingleFileNewtonsoftJsonBackingStore(string filePath) : base(filePath)
{
}
/// <summary>
/// Registers a concrete <see cref="DatabaseEntry"/> type for serialization.
/// </summary>
/// <typeparam name="T">The entry type to register.</typeparam>
public void RegisterType<T>() where T : DatabaseEntry
protected override byte[] SerializeSnapshot(CultPersistedStoreSnapshot snapshot)
{
_knownTypes.RegisterType(typeof(T));
return System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(snapshot, Formatting.Indented));
}
/// <summary>
/// Registers all concrete <see cref="DatabaseEntry"/> types from the supplied assembly.
/// </summary>
/// <param name="assembly">The assembly to scan.</param>
public void RegisterAssembly(Assembly assembly)
protected override CultPersistedStoreSnapshot DeserializeSnapshot(byte[] data)
{
_knownTypes.RegisterAssembly(assembly);
var json = System.Text.Encoding.UTF8.GetString(data);
return JsonConvert.DeserializeObject<CultPersistedStoreSnapshot>(json)
?? new CultPersistedStoreSnapshot();
}
/// <summary>
/// Rebuilds the known-type map from the currently loaded assemblies.
/// </summary>
public void RefreshKnownTypes()
protected override byte[] SerializePayload(object document)
{
_knownTypes.RegisterLoadedAssemblies();
return CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType());
}
/// <inheritdoc />
public override byte[] Serialize(DatabaseEntry[] entries)
protected override object DeserializePayload(Type documentType, byte[] payload)
{
var serializer = JsonSerializer.Create(KnownDatabaseEntryTypes.SerializerSettings);
var payload = entries.Select(entry => _knownTypes.ToEnvelope(entry, serializer)).ToArray();
var json = JsonConvert.SerializeObject(payload, KnownDatabaseEntryTypes.SerializerSettings);
return Encoding.UTF8.GetBytes(json);
}
/// <inheritdoc />
public override DatabaseEntry[] Deserialize(byte[] data)
{
_knownTypes.RegisterLoadedAssemblies();
var json = Encoding.UTF8.GetString(data);
var serializer = JsonSerializer.Create(KnownDatabaseEntryTypes.SerializerSettings);
var payload = JsonConvert.DeserializeObject<DatabaseEntryEnvelope[]>(json, KnownDatabaseEntryTypes.SerializerSettings) ?? [];
return payload.Select(envelope => _knownTypes.FromEnvelope(envelope, serializer)).ToArray();
return CultDocumentMessagePackSerialization.DeserializeUntyped(documentType, payload);
}
}
/// <summary>
/// Persists each cache entry to its own JSON file using Newtonsoft.Json.
/// </summary>
public class MultiFileNewtonsoftJsonBackingStore : MultiFileBackingStore
{
private readonly KnownDatabaseEntryTypes _knownTypes = new();
/// <summary>
/// Initializes a multi-file Newtonsoft.Json backing store rooted at the supplied directory.
/// </summary>
/// <param name="path">The root directory for stored entry files.</param>
public MultiFileNewtonsoftJsonBackingStore(string path) : base(path)
{
}
/// <summary>
/// Registers a concrete <see cref="DatabaseEntry"/> type for serialization.
/// </summary>
/// <typeparam name="T">The entry type to register.</typeparam>
public void RegisterType<T>() where T : DatabaseEntry
protected override byte[] SerializeRecord(CultPersistedRecord record)
{
_knownTypes.RegisterType(typeof(T));
return System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(record, Formatting.Indented));
}
/// <summary>
/// Registers all concrete <see cref="DatabaseEntry"/> types from the supplied assembly.
/// </summary>
/// <param name="assembly">The assembly to scan.</param>
public void RegisterAssembly(Assembly assembly)
protected override CultPersistedRecord DeserializeRecord(byte[] data)
{
_knownTypes.RegisterAssembly(assembly);
var json = System.Text.Encoding.UTF8.GetString(data);
return JsonConvert.DeserializeObject<CultPersistedRecord>(json)
?? new CultPersistedRecord();
}
/// <summary>
/// Rebuilds the known-type map from the currently loaded assemblies.
/// </summary>
public void RefreshKnownTypes()
protected override byte[] SerializeCatalog(CultSchemaCatalogEntry[] catalog)
{
_knownTypes.RegisterLoadedAssemblies();
return System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(catalog, Formatting.Indented));
}
/// <inheritdoc />
public override byte[] Serialize(DatabaseEntry entry)
protected override CultSchemaCatalogEntry[] DeserializeCatalog(byte[] data)
{
var serializer = JsonSerializer.Create(KnownDatabaseEntryTypes.SerializerSettings);
var payload = _knownTypes.ToEnvelope(entry, serializer);
var json = JsonConvert.SerializeObject(payload, KnownDatabaseEntryTypes.SerializerSettings);
return Encoding.UTF8.GetBytes(json);
var json = System.Text.Encoding.UTF8.GetString(data);
return JsonConvert.DeserializeObject<CultSchemaCatalogEntry[]>(json)
?? Array.Empty<CultSchemaCatalogEntry>();
}
/// <inheritdoc />
public override DatabaseEntry Deserialize(byte[] data)
protected override byte[] SerializePayload(object document)
{
_knownTypes.RegisterLoadedAssemblies();
var json = Encoding.UTF8.GetString(data);
var serializer = JsonSerializer.Create(KnownDatabaseEntryTypes.SerializerSettings);
var payload = JsonConvert.DeserializeObject<DatabaseEntryEnvelope>(json, KnownDatabaseEntryTypes.SerializerSettings)
?? throw new JsonSerializationException("Failed to deserialize DatabaseEntry envelope.");
return _knownTypes.FromEnvelope(payload, serializer);
return CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType());
}
protected override object DeserializePayload(Type documentType, byte[] payload)
{
return CultDocumentMessagePackSerialization.DeserializeUntyped(documentType, payload);
}
/// <inheritdoc />
public override string Extension => "json";
}
internal sealed class KnownDatabaseEntryTypes
{
internal static readonly JsonSerializerSettings SerializerSettings = new()
{
Formatting = Formatting.Indented
};
private readonly ConcurrentDictionary<string, Type> _discriminatorToType = new(StringComparer.Ordinal);
private readonly ConcurrentDictionary<Type, string> _typeToDiscriminator = new();
public KnownDatabaseEntryTypes()
{
RegisterLoadedAssemblies();
}
public void RegisterType(Type type)
{
if (!typeof(DatabaseEntry).IsAssignableFrom(type) || type.IsAbstract)
{
throw new ArgumentException($"Type '{type.FullName}' must be a non-abstract DatabaseEntry subtype.", nameof(type));
}
var discriminator = GetDiscriminator(type);
_discriminatorToType[discriminator] = type;
_typeToDiscriminator[type] = discriminator;
}
public void RegisterAssembly(Assembly assembly)
{
foreach (var type in SafeGetTypes(assembly).Where(IsConcreteDatabaseEntry))
{
RegisterType(type);
}
}
public void RegisterLoadedAssemblies()
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(assembly => !assembly.IsDynamic))
{
RegisterAssembly(assembly);
}
}
public DatabaseEntryEnvelope ToEnvelope(DatabaseEntry entry, JsonSerializer serializer)
{
var type = entry.GetType();
if (!_typeToDiscriminator.TryGetValue(type, out var discriminator))
{
RegisterType(type);
discriminator = _typeToDiscriminator[type];
}
return new DatabaseEntryEnvelope
{
Type = discriminator,
Data = JObject.FromObject(entry, serializer)
};
}
public DatabaseEntry FromEnvelope(DatabaseEntryEnvelope envelope, JsonSerializer serializer)
{
if (!_discriminatorToType.TryGetValue(envelope.Type, out var type))
{
throw new JsonSerializationException($"Unknown DatabaseEntry discriminator '{envelope.Type}'. Register the containing assembly or entry type before deserializing.");
}
return (DatabaseEntry?)envelope.Data.ToObject(type, serializer)
?? throw new JsonSerializationException($"Failed to deserialize DatabaseEntry discriminator '{envelope.Type}'.");
}
private static bool IsConcreteDatabaseEntry(Type type)
{
return typeof(DatabaseEntry).IsAssignableFrom(type) && !type.IsAbstract;
}
private static IEnumerable<Type> SafeGetTypes(Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types.Where(type => type != null)!;
}
}
private static string GetDiscriminator(Type type)
{
return $"{type.Assembly.GetName().Name}:{type.FullName}";
}
}
internal sealed class DatabaseEntryEnvelope
{
public string Type { get; set; } = string.Empty;
public JObject Data { get; set; } = new();
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,101 @@
using System;
namespace GameCult.Caching
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class CultDocumentAttribute : Attribute
{
public CultDocumentAttribute(string schemaName, string schemaVersion)
{
SchemaName = string.IsNullOrWhiteSpace(schemaName)
? throw new ArgumentException("SchemaName must be non-empty.", nameof(schemaName))
: schemaName;
SchemaVersion = string.IsNullOrWhiteSpace(schemaVersion)
? throw new ArgumentException("SchemaVersion must be non-empty.", nameof(schemaVersion))
: schemaVersion;
}
public string SchemaName { get; }
public string SchemaVersion { get; }
}
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class CultGlobalAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true)]
public sealed class CultNameAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class CultIndexAttribute : Attribute
{
public CultIndexAttribute(string? alias = null)
{
Alias = alias;
}
public string? Alias { get; }
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true)]
public sealed class CultReferenceAttribute : Attribute
{
public CultReferenceAttribute(Type? targetType = null, bool many = false)
{
TargetType = targetType;
Many = many;
}
public Type? TargetType { get; }
public bool Many { get; }
}
public readonly struct CultRecordKey : IEquatable<CultRecordKey>
{
public CultRecordKey(string value)
{
Value = value ?? string.Empty;
}
public string Value { get; }
public override string ToString() => Value;
public bool Equals(CultRecordKey other) => string.Equals(Value, other.Value, StringComparison.Ordinal);
public override bool Equals(object? obj) => obj is CultRecordKey other && Equals(other);
public override int GetHashCode() => StringComparer.Ordinal.GetHashCode(Value);
}
public readonly struct CultRecordHandle<T>
{
public CultRecordHandle(CultRecordKey key)
{
Key = key;
}
public CultRecordKey Key { get; }
public override string ToString() => Key.ToString();
}
public readonly struct CultRecordRef<T>
{
public CultRecordRef(CultRecordKey key)
{
Key = key;
}
public CultRecordKey Key { get; }
public T? Resolve(CultCache cache)
{
if (cache == null) throw new ArgumentNullException(nameof(cache));
return cache.Get(Key) is T typed ? typed : default;
}
public override string ToString() => Key.ToString();
}
}

View file

@ -1,69 +0,0 @@
using System;
namespace GameCult.Caching
{
/// <summary>
/// Interface for database entries that have a human-readable name.
/// </summary>
public interface INamedEntry
{
/// <summary>
/// Human-readable name of the entry.
/// </summary>
string EntryName { get; set; }
}
/// <summary>
/// Base abstract class for all database entries in the cache.
/// For optional MessagePack integration, define concrete subclasses with [MessagePackObject] and [Key(n)] on fields.
/// The ID field can be serialized via a custom formatter in MessagePack stores.
/// </summary>
public abstract class DatabaseEntry
{
/// <summary>
/// Unique identifier for the entry. Handled by custom formatters in MessagePack integration.
/// </summary>
public Guid ID = Guid.NewGuid();
/// <inheritdoc/>
public override int GetHashCode()
{
return ID.GetHashCode();
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj is DatabaseEntry entry) return entry.ID == ID;
return false;
}
}
/// <summary>
/// Generic link to a <see cref="DatabaseEntry"/> in the cache.
/// Value is computed from the cache; serializes only LinkID.
/// </summary>
public class DatabaseLink<T> : DatabaseLinkBase where T : DatabaseEntry
{
/// <summary>
/// Computed value from the cache (not serialized; regenerated post-deserialize).
/// </summary>
public T? Value => Cache?.Get<T>(LinkID);
}
/// <summary>
/// Base for database links. LinkID is serialized directly; Value is computed in subclasses.
/// </summary>
public class DatabaseLinkBase
{
/// <summary>
/// ID of the linked entry.
/// </summary>
public Guid LinkID;
/// <summary>
/// Static reference to the cache (not serialized).
/// </summary>
public static CultCache? Cache;
}
}

View file

@ -1,241 +1,130 @@
# GameCult.Caching
`GameCult.Caching` provides a typed in-memory cache for `DatabaseEntry` models and a persistence abstraction for backing stores.
`GameCult.Caching` is an attribute-driven typed cache with pluggable stores.
Domain models stay clean. The cache owns record keys, schema ids, timestamps,
and persistence metadata instead of smearing that sludge across every class.
## Scope
## Authoring Model
The library is designed for applications that need:
- fast in-memory lookup by `Guid`
- optional lookup by human-readable name
- optional lookup by indexed fields or properties
- persistence through one or more pluggable stores
- a common model base type for runtime and storage code
It is a domain cache with persistence adapters.
## Main Concepts
### `DatabaseEntry`
All cacheable models inherit from `DatabaseEntry`.
Cacheable models are plain classes annotated with cache intent.
```csharp
using GameCult.Caching;
using MessagePack;
public class ItemData : DatabaseEntry, INamedEntry
[CultDocument("gamecult.item_data", "gamecult.item_data.v1")]
[MessagePackObject]
public sealed class ItemData
{
[Key(0)]
[CultName]
public string Name = string.Empty;
public string Category = string.Empty;
public int Value;
public string EntryName
{
get => Name;
set => Name = value;
}
[Key(1)]
[CultIndex]
public string Category = string.Empty;
[Key(2)]
public int Value;
}
```
### `INamedEntry`
Available cache attributes:
If a type implements `INamedEntry`, `CultCache` maintains a name-to-id lookup for it. That enables:
- `CultDocument(schemaName, schemaVersion)`
- `CultName`
- `CultIndex(alias?)`
- `CultGlobal`
- `CultReference(targetType?, many: false)`
- `GetIdByName<T>(...)`
- `GetByName<T>(...)`
## Main Concepts
### Registered Indexes
### Record keys and handles
Indexes are opt-in. Register an index once for a field or property, and the cache maintains a value-to-id lookup map for that member.
`CultCache` gives each stored document a cache-owned `CultRecordKey`. You work
with typed handles instead of forcing storage identity into the domain object.
```csharp
var cache = new CultCache();
cache.RegisterIndex<ItemData>("Category");
```
After registration:
```csharp
var consumable = cache.GetByIndex<ItemData>("Category", "Consumable");
```
### Global Entries
If a `DatabaseEntry` type is marked with `GlobalSettingsAttribute`, the cache treats it as a singleton-style global entry and exposes it through `GetGlobal<T>()`.
## Core Features
- ID-based lookup
- optional name-based lookup
- optional field/property indexes
- global singleton-style entries
- pluggable backing stores
- event propagation from backing stores into the cache
- support for both single-file and multi-file persistence strategies
## Basic Usage
```csharp
using GameCult.Caching;
var cache = new CultCache();
cache.RegisterIndex<ItemData>("Category");
await cache.AddAsync(new ItemData
var handle = await cache.AddAsync(new ItemData
{
ID = Guid.NewGuid(),
Name = "Potion",
Category = "Consumable",
Value = 50
});
var potion = cache.GetByName<ItemData>("Potion");
var samePotion = cache.GetByIndex<ItemData>("Category", "Consumable");
var potion = cache.Get<ItemData>(handle.Key);
```
## Important Concept: Backing Store Routing
### Name and index lookups
`CultCache` can have:
- generic backing stores
- type/domain-specific backing stores
### Domain-Specific Stores
If you register a store with one or more domain types:
Name and index lookups are driven from attributes, not manual registration.
```csharp
cache.AddBackingStore(playerStore, typeof(PlayerData));
cache.AddBackingStore(settingsStore, typeof(AppSettings));
var byName = cache.GetByName<ItemData>("Potion");
var byIndex = cache.GetByIndex<ItemData>("Category", "Consumable");
```
then writes for those types are routed directly to those stores.
### Explicit references
### Generic Stores
If you register stores without domains:
References stay explicit in the domain. The cache will resolve them on demand,
but it will not quietly build an entire haunted object graph behind your back.
```csharp
cache.AddBackingStore(primaryStore);
cache.AddBackingStore(mirrorStore);
```
the first generic store becomes the primary write target for entries that do not have a type-specific store.
The later generic stores subscribe to earlier ones and mirror their changes.
That means:
- generic store registration order matters
- the first generic store is effectively the source of truth for generic writes
- later generic stores behave like downstream replicas
### How Multiple Backing Stores Behave
With this setup:
```csharp
cache.AddBackingStore(primaryStore);
cache.AddBackingStore(reportingMirror);
cache.AddBackingStore(playerStore, typeof(PlayerData));
```
the behavior is:
- `PlayerData` writes go to `playerStore`
- other entry types go to `primaryStore`
- `reportingMirror` mirrors change events from `primaryStore`
- `PullAllBackingStoresAsync()` pulls from all registered stores
This is not a multi-master design. If you need symmetric writes to multiple stores, implement that outside `CultCache`.
## Persistence Semantics
`AddAsync` persists to the appropriate backing store before the cache treats the write as committed. If persistence fails, the call throws instead of updating only the in-memory state.
Effects:
- memory and disk are less likely to diverge silently
- callers can react to persistence failures explicitly
## Backing Store Base Types
### `CacheBackingStore`
Abstract base for persistence implementations. It exposes:
- `PullAll()`
- `Push(DatabaseEntry entry)`
- `Delete(DatabaseEntry entry)`
- `PushAll(bool soft = false)`
### `SingleFileBackingStore`
Stores the entire set of entries in one file.
Use cases:
- small datasets
- settings-style data
- atomic snapshot persistence
### `MultiFileBackingStore`
Stores one file per entry in type-specific directories.
Use cases:
- large sets of independent entries
- change observation with `FileSystemWatcher`
- workflows where per-entry files are useful
## Rename Behavior In `MultiFileBackingStore`
For `INamedEntry` types, the filename is derived from the entry name. The backing store removes the stale old file before committing the new one.
## Example: Custom Backing Store
```csharp
using System.IO;
using System.Text;
using GameCult.Caching;
public sealed class TextItemStore : MultiFileBackingStore
[CultDocument("gamecult.quest_data", "gamecult.quest_data.v1")]
[MessagePackObject]
public sealed class QuestData
{
public TextItemStore(string path) : base(path)
{
}
public override byte[] Serialize(DatabaseEntry entry)
{
var item = (ItemData)entry;
return Encoding.UTF8.GetBytes($"{item.ID}|{item.Name}|{item.Category}|{item.Value}");
}
public override DatabaseEntry Deserialize(byte[] data)
{
var parts = Encoding.UTF8.GetString(data).Split('|');
return new ItemData
{
ID = Guid.Parse(parts[0]),
Name = parts[1],
Category = parts[2],
Value = int.Parse(parts[3])
};
}
public override string Extension => "item";
[Key(0)] public string Title = string.Empty;
[Key(1)] [CultReference] public CultRecordRef<ItemData> Reward;
}
```
## Typical Startup Sequence
## Persistence Shape
CultCache persistence is store-shaped, not entry-shaped:
1. store header
2. embedded schema catalog
3. records
Each persisted record carries:
- `key`
- `schemaId`
- `storedAt`
- `payload`
The domain payload stays free of storage metadata.
## Backing Stores
Base abstractions:
- `CacheBackingStore`
- `SingleFileBackingStore`
- `MultiFileBackingStore`
Included implementations in sibling packages:
- `SingleFileMessagePackBackingStore`
- `MultiFileMessagePackBackingStore`
- `SingleFileNewtonsoftJsonBackingStore`
- `MultiFileNewtonsoftJsonBackingStore`
`MultiFileBackingStore` derives filenames from the `CultName` member when one
exists, so renames replace stale files instead of leaving little corpses
behind.
## Typical Startup
```csharp
using GameCult.Caching;
using GameCult.Caching.MessagePack;
var cache = new CultCache();
cache.RegisterIndex<PlayerData>("Email");
var store = new TextItemStore("Data");
cache.AddBackingStore(store, typeof(ItemData));
var store = new MultiFileMessagePackBackingStore("Data");
cache.AddBackingStore(store);
await cache.PullAllBackingStoresAsync();
```

View file

@ -5,55 +5,27 @@ using System.Reflection;
namespace GameCult.Caching
{
/// <summary>
/// Provides cached reflection helpers used by the cache infrastructure.
/// </summary>
public static class ReflectionExtensions
internal static class ReflectionExtensions
{
private static Dictionary<Type, Type[]> ParentTypes = new Dictionary<Type, Type[]>();
/// <summary>
/// Gets all base types for the supplied type, caching the result.
/// </summary>
/// <param name="type">The type whose parent types should be returned.</param>
/// <returns>An array of base types ordered from immediate parent upward.</returns>
public static Type[] GetParentTypes(this Type type)
public static IEnumerable<Type> GetAttributedDocumentTypes()
{
if (ParentTypes.ContainsKey(type))
return ParentTypes[type];
return ParentTypes[type] = type.GetParents().ToArray();
return AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly => !assembly.IsDynamic)
.SelectMany(SafeGetTypes)
.Where(type => type is { IsAbstract: false, IsInterface: false })
.Where(type => type.GetCustomAttribute<CultDocumentAttribute>() != null);
}
private static IEnumerable<Type> GetParents(this Type type)
public static IEnumerable<Type> SafeGetTypes(Assembly assembly)
{
// is there any base type?
if (type == null)
try
{
yield break;
return assembly.GetTypes();
}
// return all inherited types
var currentBaseType = type.BaseType;
while (currentBaseType != null)
catch (ReflectionTypeLoadException ex)
{
yield return currentBaseType;
currentBaseType = currentBaseType.BaseType;
return ex.Types.Where(type => type != null)!;
}
}
private static Dictionary<Type, Type[]> ChildClasses = new Dictionary<Type, Type[]>();
/// <summary>
/// Gets all loaded types assignable to the supplied type, caching the result.
/// </summary>
/// <param name="type">The base type or interface to search from.</param>
/// <returns>An array of matching loaded types.</returns>
public static Type[] GetAllChildClasses(this Type type)
{
if (ChildClasses.ContainsKey(type))
return ChildClasses[type];
return ChildClasses[type] = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(ass => ass.GetTypes()).Where(type.IsAssignableFrom).ToArray();
}
}
}

View file

@ -6,14 +6,14 @@
"required": [
"schemaVersion",
"messageId",
"documentType",
"documentKey"
"schemaId",
"recordKey"
],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.document_delete.v0" },
"messageId": { "type": "string", "minLength": 1 },
"documentType": { "type": "string", "minLength": 1 },
"documentKey": { "type": "string", "minLength": 1 }
"schemaId": { "type": "string", "minLength": 1 },
"recordKey": { "type": "string", "minLength": 1 }
}
}

View file

@ -4,17 +4,16 @@
"title": "CultNet Document Record",
"type": "object",
"required": [
"documentType",
"documentKey",
"schemaId",
"recordKey",
"storedAt",
"payload"
],
"additionalProperties": false,
"properties": {
"documentType": { "type": "string", "minLength": 1 },
"documentKey": { "type": "string", "minLength": 1 },
"schemaId": { "type": "string", "minLength": 1 },
"recordKey": { "type": "string", "minLength": 1 },
"storedAt": { "type": "string", "minLength": 1 },
"payloadSchemaVersion": { "type": "string", "minLength": 1 },
"payload": {},
"sourceRuntimeId": { "type": "string", "minLength": 1 },
"sourceAgentId": { "type": "string", "minLength": 1 },

View file

@ -4,18 +4,17 @@
"title": "CultNet Raw Document Record",
"type": "object",
"required": [
"documentType",
"documentKey",
"schemaId",
"recordKey",
"storedAt",
"payloadEncoding",
"payload"
],
"additionalProperties": false,
"properties": {
"documentType": { "type": "string", "minLength": 1 },
"documentKey": { "type": "string", "minLength": 1 },
"schemaId": { "type": "string", "minLength": 1 },
"recordKey": { "type": "string", "minLength": 1 },
"storedAt": { "type": "string", "minLength": 1 },
"payloadSchemaVersion": { "type": "string", "minLength": 1 },
"payloadEncoding": { "const": "messagepack" },
"payload": {
"type": "string",

View file

@ -11,11 +11,11 @@
"properties": {
"schemaVersion": { "const": "cultnet.snapshot_request.v0" },
"messageId": { "type": "string", "minLength": 1 },
"documentTypes": {
"schemaIds": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
},
"documentKeys": {
"recordKeys": {
"type": "array",
"items": { "type": "string", "minLength": 1 }
}

View file

@ -8,9 +8,6 @@ using GameCult.Caching.MessagePack;
namespace GameCult.Networking
{
/// <summary>
/// Optional metadata attached when CultNet emits a document message.
/// </summary>
public sealed class CultNetDocumentMessageOptions
{
public string? StoredAt { get; set; }
@ -20,90 +17,60 @@ namespace GameCult.Networking
public string[]? Tags { get; set; }
}
/// <summary>
/// Maps a concrete CultCache entry type to a shared CultNet document contract.
/// </summary>
public sealed class CultNetDocumentBinding
{
internal CultNetDocumentBinding(
Type entryType,
string documentType,
string? payloadSchemaVersion,
Func<DatabaseEntry, string> documentKeySelector,
Func<DatabaseEntry, byte[]> payloadSerializer,
Func<byte[], DatabaseEntry> payloadDeserializer)
Type documentType,
string schemaId,
Func<object, byte[]> payloadSerializer,
Func<byte[], object> payloadDeserializer)
{
EntryType = entryType ?? throw new ArgumentNullException(nameof(entryType));
DocumentType = string.IsNullOrWhiteSpace(documentType)
? throw new ArgumentException("DocumentType must be non-empty.", nameof(documentType))
: documentType;
PayloadSchemaVersion = payloadSchemaVersion;
DocumentKeySelector = documentKeySelector ?? throw new ArgumentNullException(nameof(documentKeySelector));
PayloadSerializer = payloadSerializer ?? throw new ArgumentNullException(nameof(payloadSerializer));
PayloadDeserializer = payloadDeserializer ?? throw new ArgumentNullException(nameof(payloadDeserializer));
DocumentType = documentType;
SchemaId = schemaId;
PayloadSerializer = payloadSerializer;
PayloadDeserializer = payloadDeserializer;
}
public Type EntryType { get; }
public string DocumentType { get; }
public string? PayloadSchemaVersion { get; }
public Func<DatabaseEntry, string> DocumentKeySelector { get; }
public Func<DatabaseEntry, byte[]> PayloadSerializer { get; }
public Func<byte[], DatabaseEntry> PayloadDeserializer { get; }
public Type DocumentType { get; }
public string SchemaId { get; }
public Func<object, byte[]> PayloadSerializer { get; }
public Func<byte[], object> PayloadDeserializer { get; }
public static CultNetDocumentBinding ForEntry<T>(
string documentType,
string? payloadSchemaVersion = null,
Func<T, string>? documentKeySelector = null,
public static CultNetDocumentBinding ForDocument<T>(
CultDocumentRegistry? registry = null,
Func<T, byte[]>? payloadSerializer = null,
Func<byte[], T>? payloadDeserializer = null)
where T : DatabaseEntry
where T : class
{
var descriptor = (registry ?? CultDocumentRegistry.Shared).GetRequired<T>();
return new CultNetDocumentBinding(
typeof(T),
documentType,
payloadSchemaVersion,
entry =>
descriptor.SchemaId,
document =>
{
if (entry is not T typedEntry)
{
throw new InvalidOperationException(
$"Document binding for {typeof(T).Name} received {entry.GetType().Name}.");
}
return documentKeySelector != null
? documentKeySelector(typedEntry)
: typedEntry.ID.ToString("D", CultureInfo.InvariantCulture);
},
entry =>
{
if (entry is not T typedEntry)
{
throw new InvalidOperationException(
$"Document binding for {typeof(T).Name} received {entry.GetType().Name}.");
}
var typed = (T)document;
return payloadSerializer != null
? payloadSerializer(typedEntry)
: CultCacheEnvelopeSerialization.SerializePayload(typedEntry);
? payloadSerializer(typed)
: CultDocumentMessagePackSerialization.Serialize(typed);
},
payload => payloadDeserializer != null
? payloadDeserializer(payload)
: CultCacheEnvelopeSerialization.DeserializePayload<T>(payload));
: CultDocumentMessagePackSerialization.Deserialize<T>(payload));
}
}
/// <summary>
/// Creates and applies CultNet raw document/snapshot messages for bound CultCache entry types.
/// </summary>
public sealed class CultNetDocumentRegistry
{
private readonly Dictionary<string, CultNetDocumentBinding> _bindingsByDocumentType =
new Dictionary<string, CultNetDocumentBinding>(StringComparer.Ordinal);
private readonly Dictionary<Type, CultNetDocumentBinding> _bindingsByEntryType =
new Dictionary<Type, CultNetDocumentBinding>();
private readonly CultDocumentRegistry _documents;
private readonly Dictionary<string, CultNetDocumentBinding> _bindingsBySchemaId =
new(StringComparer.Ordinal);
private readonly Dictionary<Type, CultNetDocumentBinding> _bindingsByType = new();
public CultNetDocumentRegistry(IEnumerable<CultNetDocumentBinding>? bindings = null)
public CultNetDocumentRegistry(
CultDocumentRegistry? documents = null,
IEnumerable<CultNetDocumentBinding>? bindings = null)
{
_documents = documents ?? CultDocumentRegistry.Shared;
if (bindings == null)
{
return;
@ -117,86 +84,73 @@ namespace GameCult.Networking
public CultNetDocumentRegistry Register(CultNetDocumentBinding binding)
{
if (binding == null) throw new ArgumentNullException(nameof(binding));
_bindingsByDocumentType[binding.DocumentType] = binding;
_bindingsByEntryType[binding.EntryType] = binding;
_bindingsBySchemaId[binding.SchemaId] = binding;
_bindingsByType[binding.DocumentType] = binding;
return this;
}
public CultNetDocumentBinding? GetByDocumentType(string documentType)
public CultNetDocumentBinding? GetBySchemaId(string schemaId)
{
if (documentType == null) throw new ArgumentNullException(nameof(documentType));
return _bindingsByDocumentType.TryGetValue(documentType, out var binding) ? binding : null;
return _bindingsBySchemaId.TryGetValue(schemaId, out var binding) ? binding : null;
}
public CultNetDocumentBinding? GetByEntryType(Type entryType)
public CultNetDocumentBinding? GetByDocumentType(Type documentType)
{
if (entryType == null) throw new ArgumentNullException(nameof(entryType));
return _bindingsByEntryType.TryGetValue(entryType, out var binding) ? binding : null;
return _bindingsByType.TryGetValue(documentType, out var binding) ? binding : null;
}
public CultNetDocumentDeleteMessage CreateDocumentDeleteMessage(
string messageId,
string documentType,
string documentKey)
string schemaId,
string recordKey)
{
return new CultNetDocumentDeleteMessage
{
MessageId = RequireNonEmpty(messageId, nameof(messageId)),
DocumentType = RequireNonEmpty(documentType, nameof(documentType)),
DocumentKey = RequireNonEmpty(documentKey, nameof(documentKey))
SchemaId = RequireNonEmpty(schemaId, nameof(schemaId)),
RecordKey = RequireNonEmpty(recordKey, nameof(recordKey))
};
}
public CultNetDocumentPutRawMessage CreateRawDocumentPutMessage(
public CultNetDocumentPutRawMessage CreateRawDocumentPutMessage<T>(
string messageId,
DatabaseEntry entry,
CultRecordHandle<T> handle,
T document,
CultNetDocumentMessageOptions? options = null)
where T : class
{
if (entry == null) throw new ArgumentNullException(nameof(entry));
var binding = RequireBinding(entry.GetType());
var descriptor = _documents.GetRequired<T>();
var binding = GetByDocumentType(typeof(T)) ?? CultNetDocumentBinding.ForDocument<T>(_documents);
var storedAt = ResolveStoredAt(options);
var documentKey = binding.DocumentKeySelector(entry);
return new CultNetDocumentPutRawMessage
{
MessageId = RequireNonEmpty(messageId, nameof(messageId)),
Document = CreateRawDocumentRecord(binding, entry, documentKey, storedAt, options)
};
}
public CultNetDocumentPutRawMessage CreateRawDocumentPutMessageFromEnvelope(
string messageId,
CultCacheEnvelope envelope)
{
if (envelope == null) throw new ArgumentNullException(nameof(envelope));
var binding = RequireBinding(envelope.Type);
return new CultNetDocumentPutRawMessage
{
MessageId = RequireNonEmpty(messageId, nameof(messageId)),
Document = new CultNetRawDocumentRecord
{
DocumentType = envelope.Type,
DocumentKey = envelope.Key,
StoredAt = envelope.StoredAt,
PayloadSchemaVersion = binding.PayloadSchemaVersion,
SchemaId = descriptor.SchemaId,
RecordKey = handle.Key.Value,
StoredAt = storedAt,
PayloadEncoding = "messagepack",
Payload = envelope.Payload.ToArray()
Payload = binding.PayloadSerializer(document),
SourceRuntimeId = options?.SourceRuntimeId,
SourceAgentId = options?.SourceAgentId,
SourceRole = options?.SourceRole,
Tags = options?.Tags
}
};
}
public CultNetSnapshotRequestMessage CreateSnapshotRequest(
string messageId,
IEnumerable<string>? documentTypes = null,
IEnumerable<string>? documentKeys = null)
IEnumerable<string>? schemaIds = null,
IEnumerable<string>? recordKeys = null)
{
return new CultNetSnapshotRequestMessage
{
MessageId = RequireNonEmpty(messageId, nameof(messageId)),
DocumentTypes = documentTypes?.ToArray(),
DocumentKeys = documentKeys?.ToArray()
SchemaIds = schemaIds?.ToArray(),
RecordKeys = recordKeys?.ToArray()
};
}
@ -206,42 +160,62 @@ namespace GameCult.Networking
CultNetSnapshotRequestMessage? filter = null,
CultNetDocumentMessageOptions? options = null)
{
if (cache == null) throw new ArgumentNullException(nameof(cache));
var requestedDocumentTypes = filter?.DocumentTypes != null
? new HashSet<string>(filter.DocumentTypes, StringComparer.Ordinal)
var requestedSchemaIds = filter?.SchemaIds != null
? new HashSet<string>(filter.SchemaIds, StringComparer.Ordinal)
: null;
var requestedDocumentKeys = filter?.DocumentKeys != null
? new HashSet<string>(filter.DocumentKeys, StringComparer.Ordinal)
var requestedRecordKeys = filter?.RecordKeys != null
? new HashSet<string>(filter.RecordKeys, StringComparer.Ordinal)
: null;
var storedAt = ResolveStoredAt(options);
var documents = new List<CultNetRawDocumentRecord>();
foreach (var entry in cache.AllEntries)
foreach (var document in cache.AllEntries)
{
var binding = GetByEntryType(entry.GetType());
if (binding == null)
var descriptor = _documents.GetRequired(document.GetType());
if (requestedSchemaIds != null && !requestedSchemaIds.Contains(descriptor.SchemaId))
{
continue;
}
if (requestedDocumentTypes != null && !requestedDocumentTypes.Contains(binding.DocumentType))
var handleMethod = typeof(CultCache)
.GetMethod(nameof(CultCache.TryGetHandle))!
.MakeGenericMethod(document.GetType());
var handleObject = handleMethod.Invoke(cache, new[] { document });
if (handleObject == null)
{
continue;
}
var documentKey = binding.DocumentKeySelector(entry);
if (requestedDocumentKeys != null && !requestedDocumentKeys.Contains(documentKey))
var keyProperty = handleObject.GetType().GetProperty("Value");
var handleValue = keyProperty?.GetValue(handleObject) ?? handleObject;
var recordKeyProperty = handleValue.GetType().GetProperty("Key");
var recordKey = recordKeyProperty?.GetValue(handleValue);
var valueProperty = recordKey?.GetType().GetProperty("Value");
var key = (string?)(valueProperty?.GetValue(recordKey)) ?? string.Empty;
if (requestedRecordKeys != null && !requestedRecordKeys.Contains(key))
{
continue;
}
documents.Add(CreateRawDocumentRecord(
binding,
entry,
documentKey,
storedAt,
options));
var binding = GetByDocumentType(document.GetType()) ??
new CultNetDocumentBinding(
document.GetType(),
descriptor.SchemaId,
value => CultDocumentMessagePackSerialization.SerializeUntyped(value, value.GetType()),
payload => CultDocumentMessagePackSerialization.DeserializeUntyped(document.GetType(), payload));
documents.Add(new CultNetRawDocumentRecord
{
SchemaId = descriptor.SchemaId,
RecordKey = key,
StoredAt = storedAt,
PayloadEncoding = "messagepack",
Payload = binding.PayloadSerializer(document),
SourceRuntimeId = options?.SourceRuntimeId,
SourceAgentId = options?.SourceAgentId,
SourceRole = options?.SourceRole,
Tags = options?.Tags
});
}
return new CultNetSnapshotResponseRawMessage
@ -251,68 +225,56 @@ namespace GameCult.Networking
};
}
public async Task<DatabaseEntry> ApplyRawDocumentPutMessageAsync(
public async Task<object> ApplyRawDocumentPutMessageAsync(
CultCache cache,
CultNetDocumentPutRawMessage message)
{
if (cache == null) throw new ArgumentNullException(nameof(cache));
if (message == null) throw new ArgumentNullException(nameof(message));
if (message.Document == null) throw new ArgumentException("CultNet raw document message is missing its document payload.", nameof(message));
if (message.Document == null)
{
throw new ArgumentException("CultNet raw document message is missing its document payload.", nameof(message));
}
var binding = RequireBinding(message.Document.DocumentType);
ValidateRawDocumentRecord(message.Document);
var descriptor = _documents.GetRequiredBySchemaId(message.Document.SchemaId);
var binding = GetBySchemaId(message.Document.SchemaId) ??
new CultNetDocumentBinding(
descriptor.DocumentType,
descriptor.SchemaId,
value => CultDocumentMessagePackSerialization.SerializeUntyped(value, value.GetType()),
payload => CultDocumentMessagePackSerialization.DeserializeUntyped(descriptor.DocumentType, payload));
var document = binding.PayloadDeserializer(message.Document.Payload);
var entry = binding.PayloadDeserializer(message.Document.Payload);
if (!binding.EntryType.IsInstanceOfType(entry))
{
throw new InvalidOperationException(
$"CultNet raw document type \"{message.Document.DocumentType}\" expected {binding.EntryType.Name} but decoded {entry.GetType().Name}.");
}
var expectedKey = binding.DocumentKeySelector(entry);
if (!string.Equals(expectedKey, message.Document.DocumentKey, StringComparison.Ordinal))
{
throw new InvalidOperationException(
$"CultNet raw document key mismatch for \"{message.Document.DocumentType}\": payload resolved to \"{expectedKey}\" but wire metadata said \"{message.Document.DocumentKey}\".");
}
await cache.AddAsync(entry);
return entry;
var addMethod = typeof(CultCache).GetMethod(nameof(CultCache.AddAsync))!
.MakeGenericMethod(descriptor.DocumentType);
var handleType = typeof(CultRecordHandle<>).MakeGenericType(descriptor.DocumentType);
var optionalHandle = Activator.CreateInstance(typeof(Nullable<>).MakeGenericType(handleType), new object[] { Activator.CreateInstance(handleType, new object[] { new CultRecordKey(message.Document.RecordKey) })! });
var task = (Task)addMethod.Invoke(cache, [document, optionalHandle])!;
await task.ConfigureAwait(false);
return document;
}
public async Task<T> ApplyRawDocumentPutMessageAsync<T>(
CultCache cache,
CultNetDocumentPutRawMessage message)
where T : DatabaseEntry
where T : class
{
var entry = await ApplyRawDocumentPutMessageAsync(cache, message);
if (entry is not T typedEntry)
{
throw new InvalidOperationException(
$"CultNet raw document message resolved to {entry.GetType().Name}, not {typeof(T).Name}.");
}
return typedEntry;
return (T)await ApplyRawDocumentPutMessageAsync(cache, message).ConfigureAwait(false);
}
public async Task<IReadOnlyList<DatabaseEntry>> ApplyRawSnapshotResponseAsync(
public async Task<IReadOnlyList<object>> ApplyRawSnapshotResponseAsync(
CultCache cache,
CultNetSnapshotResponseRawMessage response)
{
if (cache == null) throw new ArgumentNullException(nameof(cache));
if (response == null) throw new ArgumentNullException(nameof(response));
var applied = new List<DatabaseEntry>(response.Documents.Length);
var applied = new List<object>(response.Documents.Length);
foreach (var document in response.Documents)
{
var entry = await ApplyRawDocumentPutMessageAsync(
applied.Add(await ApplyRawDocumentPutMessageAsync(
cache,
new CultNetDocumentPutRawMessage
{
MessageId = response.MessageId,
Document = document
});
applied.Add(entry);
}).ConfigureAwait(false));
}
return applied;
@ -321,54 +283,9 @@ namespace GameCult.Networking
public async Task<IReadOnlyList<T>> ApplyRawSnapshotResponseAsync<T>(
CultCache cache,
CultNetSnapshotResponseRawMessage response)
where T : DatabaseEntry
where T : class
{
var applied = await ApplyRawSnapshotResponseAsync(cache, response);
return applied.OfType<T>().ToArray();
}
private CultNetRawDocumentRecord CreateRawDocumentRecord(
CultNetDocumentBinding binding,
DatabaseEntry entry,
string documentKey,
string storedAt,
CultNetDocumentMessageOptions? options)
{
var envelope = new CultCacheEnvelope
{
Key = documentKey,
Type = binding.DocumentType,
StoredAt = storedAt,
Payload = binding.PayloadSerializer(entry)
};
return new CultNetRawDocumentRecord
{
DocumentType = envelope.Type,
DocumentKey = envelope.Key,
StoredAt = envelope.StoredAt,
PayloadSchemaVersion = binding.PayloadSchemaVersion,
PayloadEncoding = "messagepack",
Payload = envelope.Payload,
SourceRuntimeId = options?.SourceRuntimeId,
SourceAgentId = options?.SourceAgentId,
SourceRole = options?.SourceRole,
Tags = options?.Tags
};
}
private CultNetDocumentBinding RequireBinding(Type entryType)
{
return GetByEntryType(entryType)
?? throw new InvalidOperationException(
$"No CultNet document binding is registered for {entryType.FullName}.");
}
private CultNetDocumentBinding RequireBinding(string documentType)
{
return GetByDocumentType(documentType)
?? throw new InvalidOperationException(
$"No CultNet document binding is registered for \"{documentType}\".");
return (await ApplyRawSnapshotResponseAsync(cache, response).ConfigureAwait(false)).OfType<T>().ToArray();
}
private static void ValidateRawDocumentRecord(CultNetRawDocumentRecord document)
@ -379,14 +296,13 @@ namespace GameCult.Networking
$"CultNet raw document payloadEncoding must be \"messagepack\", not \"{document.PayloadEncoding}\".");
}
RequireNonEmpty(document.SchemaId, nameof(document.SchemaId));
RequireNonEmpty(document.RecordKey, nameof(document.RecordKey));
RequireNonEmpty(document.StoredAt, nameof(document.StoredAt));
if (document.Payload == null || document.Payload.Length == 0)
{
throw new InvalidOperationException("CultNet raw document payload must be non-empty.");
}
RequireNonEmpty(document.DocumentType, nameof(document.DocumentType));
RequireNonEmpty(document.DocumentKey, nameof(document.DocumentKey));
RequireNonEmpty(document.StoredAt, nameof(document.StoredAt));
}
private static string ResolveStoredAt(CultNetDocumentMessageOptions? options)

View file

@ -114,10 +114,9 @@ namespace GameCult.Networking
[MessagePackObject]
public class CultNetRawDocumentRecord
{
[Key("documentType")] public string DocumentType { get; set; } = string.Empty;
[Key("documentKey")] public string DocumentKey { get; set; } = string.Empty;
[Key("schemaId")] public string SchemaId { get; set; } = string.Empty;
[Key("recordKey")] public string RecordKey { get; set; } = string.Empty;
[Key("storedAt")] public string StoredAt { get; set; } = string.Empty;
[Key("payloadSchemaVersion")] public string? PayloadSchemaVersion { get; set; }
[Key("payloadEncoding")] public string PayloadEncoding { get; set; } = "messagepack";
[Key("payload")] public byte[] Payload { get; set; } = Array.Empty<byte>();
[Key("sourceRuntimeId")] public string? SourceRuntimeId { get; set; }
@ -131,8 +130,8 @@ namespace GameCult.Networking
{
[Key("schemaVersion")] public string SchemaVersion { get; set; } = CultNetSchemaVersions.DocumentDelete;
[Key("messageId")] public string MessageId { get; set; } = string.Empty;
[Key("documentType")] public string DocumentType { get; set; } = string.Empty;
[Key("documentKey")] public string DocumentKey { get; set; } = string.Empty;
[Key("schemaId")] public string SchemaId { get; set; } = string.Empty;
[Key("recordKey")] public string RecordKey { get; set; } = string.Empty;
}
[MessagePackObject]
@ -148,8 +147,8 @@ namespace GameCult.Networking
{
[Key("schemaVersion")] public string SchemaVersion { get; set; } = CultNetSchemaVersions.SnapshotRequest;
[Key("messageId")] public string MessageId { get; set; } = string.Empty;
[Key("documentTypes")] public string[]? DocumentTypes { get; set; }
[Key("documentKeys")] public string[]? DocumentKeys { get; set; }
[Key("schemaIds")] public string[]? SchemaIds { get; set; }
[Key("recordKeys")] public string[]? RecordKeys { get; set; }
}
[MessagePackObject]

View file

@ -1,39 +1,26 @@
using System;
using GameCult.Caching;
using MessagePack;
namespace GameCult.Networking
{
/// <summary>
/// Player data stored in the database.
/// For MessagePack serialization, uses [MessagePackObject] and [Key(n)] attributes.
/// </summary>
public class PlayerData : DatabaseEntry, INamedEntry
[CultDocument("gamecult.player_data", "gamecult.player_data.v1")]
[MessagePackObject]
public class PlayerData
{
[Key(0)]
[CultIndex]
public Guid PlayerId = Guid.NewGuid();
/// <summary>
/// Email address of the player.
/// </summary>
[Key(1)]
[CultIndex]
public string Email = string.Empty;
/// <summary>
/// Password hash of the player.
/// </summary>
[Key(2)]
public string PasswordHash = string.Empty;
/// <summary>
/// Username of the player.
/// </summary>
[Key(3)]
[CultName]
public string Username = string.Empty;
/// <inheritdoc/>
[IgnoreMember]
public string EntryName
{
get => Username;
set => Username = value;
}
}
}

View file

@ -84,7 +84,8 @@ Schema-v0 message families include:
The raw document/snapshot lane is meant for neighbors that already share the
same payload schema and MessagePack semantics. It carries exact payload bytes
plus explicit type metadata; it does not guess what a blob "probably" means.
plus `schemaId`/record-key metadata; it does not guess what a blob "probably"
means.
## Authentication Model

View file

@ -59,7 +59,6 @@ namespace GameCult.Networking
{
_database = cache;
_security = security ?? ServerSecurityOptions.FromEnvironment();
_database.RegisterIndex<PlayerData>("Email");
_cleanupSubscription = Observable.Timer(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(60))
.Subscribe(_ => CleanupExpiredSessions());
}
@ -256,7 +255,7 @@ namespace GameCult.Networking
return;
}
if (_database.GetIdByName<PlayerData>(message.Name) != null)
if (_database.GetByName<PlayerData>(message.Name) != null)
{
message.Peer?.Send(new ErrorMessage { Error = "Username Taken" });
return;
@ -310,13 +309,13 @@ namespace GameCult.Networking
return;
}
if (_database.GetIdByIndex<PlayerData>("Email", email) != null)
if (_database.GetByIndex<PlayerData>("Email", email) != null)
{
peer.Send(new ErrorMessage { Error = "Email Taken" });
return;
}
if (_database.GetIdByName<PlayerData>(name) != null)
if (_database.GetByName<PlayerData>(name) != null)
{
peer.Send(new ErrorMessage { Error = "Username Taken" });
return;
@ -330,15 +329,15 @@ namespace GameCult.Networking
var newUserData = new PlayerData
{
ID = Guid.NewGuid(),
PlayerId = Guid.NewGuid(),
Email = email,
PasswordHash = Argon2.Hash(password, memoryCost: 16384),
Username = name
};
await _database.AddAsync(newUserData);
AttachUser(user, newUserData.ID);
SendSessionToken(peer, newUserData.ID);
AttachUser(user, newUserData.PlayerId);
SendSessionToken(peer, newUserData.PlayerId);
}
private void HandleVerify(NetPeer peer, User user, VerifyMessage verify)
@ -350,7 +349,7 @@ namespace GameCult.Networking
return;
}
if (_database.Get<PlayerData>(playerId) == null)
if (_database.GetByIndex<PlayerData>("PlayerId", playerId.ToString("D")) == null)
{
peer.Send(new ErrorMessage { Error = "Session Not Found" });
return;
@ -387,18 +386,18 @@ namespace GameCult.Networking
return;
}
AttachUser(user, userData.ID);
SendSessionToken(peer, userData.ID);
AttachUser(user, userData.PlayerId);
SendSessionToken(peer, userData.PlayerId);
}
private bool IsVerified(User? user) =>
user != null &&
user.PlayerId != Guid.Empty &&
user.SessionExpiresAt > DateTimeOffset.UtcNow &&
_database.Get<PlayerData>(user.PlayerId) != null;
_database.GetByIndex<PlayerData>("PlayerId", user.PlayerId.ToString("D")) != null;
private PlayerData? SessionData(User user) =>
IsVerified(user) ? _database.Get<PlayerData>(user.PlayerId) : null;
IsVerified(user) ? _database.GetByIndex<PlayerData>("PlayerId", user.PlayerId.ToString("D")) : null;
private bool CheckRateLimit(string ip)
{

View file

@ -1,13 +1,11 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using GameCult.Caching;
using GameCult.Caching.MessagePack;
using GameCult.Caching.NewtonsoftJson;
using Newtonsoft.Json;
using MessagePack;
using NUnit.Framework;
namespace GameCult.Caching.Tests
@ -23,13 +21,11 @@ namespace GameCult.Caching.Tests
try
{
var store = new MultiFileNewtonsoftJsonBackingStore(rootPath);
store.RegisterType<NamedTestEntry>();
var cache = new CultCache();
cache.AddBackingStore(store);
var entry = new NamedTestEntry
{
ID = Guid.NewGuid(),
Name = "OriginalName",
Value = "value"
};
@ -64,29 +60,26 @@ namespace GameCult.Caching.Tests
try
{
var writeStore = new MultiFileNewtonsoftJsonBackingStore(rootPath);
writeStore.RegisterType<NamedTestEntry>();
var writeCache = new CultCache();
writeCache.AddBackingStore(writeStore);
var entry = new NamedTestEntry
{
ID = Guid.NewGuid(),
Name = "RoundTrip",
Value = "payload"
};
await writeCache.AddAsync(entry);
var handle = await writeCache.AddAsync(entry);
var readStore = new MultiFileNewtonsoftJsonBackingStore(rootPath);
readStore.RegisterType<NamedTestEntry>();
var readCache = new CultCache();
readCache.AddBackingStore(readStore);
await readCache.PullAllBackingStoresAsync();
var loaded = readCache.Get(entry.ID);
var loaded = readCache.Get<NamedTestEntry>(handle.Key);
Assert.That(loaded, Is.Not.Null);
Assert.That(GetEntryValue(loaded!), Is.EqualTo("payload"));
Assert.That(loaded!.Value, Is.EqualTo("payload"));
}
finally
{
@ -105,30 +98,27 @@ namespace GameCult.Caching.Tests
try
{
var writeStore = new SingleFileNewtonsoftJsonBackingStore(filePath);
writeStore.RegisterType<NamedTestEntry>();
var writeCache = new CultCache();
writeCache.AddBackingStore(writeStore);
var entry = new NamedTestEntry
{
ID = Guid.NewGuid(),
Name = "SingleFile",
Value = "payload"
};
await writeCache.AddAsync(entry);
var handle = await writeCache.AddAsync(entry);
writeStore.PushAll();
var readStore = new SingleFileNewtonsoftJsonBackingStore(filePath);
readStore.RegisterType<NamedTestEntry>();
var readCache = new CultCache();
readCache.AddBackingStore(readStore);
await readCache.PullAllBackingStoresAsync();
var loaded = readCache.Get(entry.ID);
var loaded = readCache.Get<NamedTestEntry>(handle.Key);
Assert.That(loaded, Is.Not.Null);
Assert.That(GetEntryValue(loaded!), Is.EqualTo("payload"));
Assert.That(loaded!.Value, Is.EqualTo("payload"));
}
finally
{
@ -140,123 +130,34 @@ namespace GameCult.Caching.Tests
}
[Test]
public void MultiFileNewtonsoftJsonBackingStore_Rejects_UnknownDiscriminator()
public void MessagePackSerialization_RoundTrips_CultRecordRef()
{
var rootPath = Path.Combine(Path.GetTempPath(), $"cultlib-tests-{Guid.NewGuid():N}");
Directory.CreateDirectory(rootPath);
var reference = new CultRecordRef<NamedTestEntry>(new CultRecordKey("record-1"));
try
{
var store = new MultiFileNewtonsoftJsonBackingStore(rootPath);
store.RegisterType<NamedTestEntry>();
var payload = """
{
"Type": "Unknown.Assembly:Unknown.Type",
"Data": {
"ID": "00000000-0000-0000-0000-000000000001",
"Name": "Bad",
"Value": "Bad"
}
}
""";
var payload = CultDocumentMessagePackSerialization.Serialize(reference);
var roundTrip = CultDocumentMessagePackSerialization.Deserialize<CultRecordRef<NamedTestEntry>>(payload);
Assert.That(
() => store.Deserialize(Encoding.UTF8.GetBytes(payload)),
Throws.TypeOf<JsonSerializationException>().With.Message.Contains("Unknown DatabaseEntry discriminator"));
}
finally
{
if (Directory.Exists(rootPath))
{
Directory.Delete(rootPath, true);
}
}
Assert.That(roundTrip.Key.Value, Is.EqualTo("record-1"));
}
[Test]
public async Task MultiFileNewtonsoftJsonBackingStore_Delete_Removes_File()
public void MessagePackSerialization_Rejects_InvalidPayload()
{
var rootPath = Path.Combine(Path.GetTempPath(), $"cultlib-tests-{Guid.NewGuid():N}");
Directory.CreateDirectory(rootPath);
try
{
var store = new MultiFileNewtonsoftJsonBackingStore(rootPath);
store.RegisterType<NamedTestEntry>();
var cache = new CultCache();
cache.AddBackingStore(store);
var entry = new NamedTestEntry
{
ID = Guid.NewGuid(),
Name = "DeleteMe",
Value = "payload"
};
await cache.AddAsync(entry);
cache.Remove(entry);
var files = Directory.GetFiles(rootPath, "*.json", SearchOption.AllDirectories)
.Select(Path.GetFileName)
.ToArray();
Assert.That(files, Does.Not.Contain("DeleteMe.json"));
}
finally
{
if (Directory.Exists(rootPath))
{
Directory.Delete(rootPath, true);
}
}
}
[Test]
public void MessagePackDatabaseEntrySerialization_RoundTrips_DatabaseLink()
{
var serializationType = typeof(MultiFileMessagePackBackingStore).Assembly
.GetType("GameCult.Caching.MessagePack.DatabaseEntrySerialization", throwOnError: true)!;
var serialize = serializationType.GetMethod("Serialize", BindingFlags.Public | BindingFlags.Static)!
.MakeGenericMethod(typeof(DatabaseLink<DatabaseEntry>));
var deserialize = serializationType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static)!
.MakeGenericMethod(typeof(DatabaseLink<DatabaseEntry>));
var link = new DatabaseLink<DatabaseEntry> { LinkID = Guid.NewGuid() };
var payload = (byte[])serialize.Invoke(null, [link])!;
var roundTrip = (DatabaseLink<DatabaseEntry>)deserialize.Invoke(null, [payload])!;
Assert.That(roundTrip.LinkID, Is.EqualTo(link.LinkID));
}
[Test]
public void MessagePackDatabaseEntrySerialization_Rejects_InvalidPayload()
{
var serializationType = typeof(MultiFileMessagePackBackingStore).Assembly
.GetType("GameCult.Caching.MessagePack.DatabaseEntrySerialization", throwOnError: true)!;
var deserialize = serializationType.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Static)!
.MakeGenericMethod(typeof(DatabaseEntry));
Assert.That(
() => deserialize.Invoke(null, [new byte[] { 0xC1 }]),
Throws.TypeOf<TargetInvocationException>()
.With.InnerException.InstanceOf<global::MessagePack.MessagePackSerializationException>());
() => CultDocumentMessagePackSerialization.Deserialize<CultRecordRef<NamedTestEntry>>(new byte[] { 0xC1 }),
Throws.TypeOf<MessagePackSerializationException>());
}
private sealed class NamedTestEntry : DatabaseEntry, INamedEntry
[CultDocument("tests.named_entry", "tests.named_entry.v1")]
[MessagePackObject]
internal sealed class NamedTestEntry
{
[Key(0)]
[CultName]
public string Name = string.Empty;
[Key(1)]
public string Value = string.Empty;
public string EntryName
{
get => Name;
set => Name = value;
}
}
private static string GetEntryValue(DatabaseEntry entry)
{
return entry.GetType().GetField("Value")?.GetValue(entry) as string ?? string.Empty;
}
}
}

View file

@ -55,16 +55,14 @@ static async Task ServeAsync(ServeConfig config)
{
var schemaRegistration = LoadSchemaRegistration(config.SchemaPath);
var documentRegistry = new CultNetDocumentRegistry()
.Register(CultNetDocumentBinding.ForEntry<CultNetInteropNote>(
InteropPeerShared.InteropDocumentType,
payloadSchemaVersion: InteropPeerShared.InteropSchemaVersion,
documentKeySelector: note => note.DocumentId,
.Register(CultNetDocumentBinding.ForDocument<CultNetInteropNote>(
payloadSerializer: SerializeInteropNotePayload,
payloadDeserializer: DeserializeInteropNotePayload));
var customSchemaRegistry = new CultNetSchemaRegistry().Register(schemaRegistration);
var cache = new CultCache();
await cache.AddAsync(BuildInteropNote(config.RuntimeId, config.DisplayName));
var localNote = BuildInteropNote(config.RuntimeId, config.DisplayName);
await cache.AddAsync(localNote, new CultRecordHandle<CultNetInteropNote>(new CultRecordKey(localNote.DocumentId)));
using var udpSocket = CreateDiscoverySocket(config.DiscoveryPort, config.DiscoveryGroup);
using var tcpListener = new TcpListener(IPAddress.Parse(config.BindHost), config.TcpPort);
@ -173,10 +171,7 @@ static async Task DialAsync(DialConfig config)
var schemaRegistration = LoadSchemaRegistration(config.SchemaPath);
var cache = new CultCache();
var documentRegistry = new CultNetDocumentRegistry()
.Register(CultNetDocumentBinding.ForEntry<CultNetInteropNote>(
InteropPeerShared.InteropDocumentType,
payloadSchemaVersion: InteropPeerShared.InteropSchemaVersion,
documentKeySelector: note => note.DocumentId,
.Register(CultNetDocumentBinding.ForDocument<CultNetInteropNote>(
payloadSerializer: SerializeInteropNotePayload,
payloadDeserializer: DeserializeInteropNotePayload));
@ -209,8 +204,8 @@ static async Task DialAsync(DialConfig config)
await SendMessageAsync(stream, new CultNetSnapshotRequestMessage
{
MessageId = $"{config.RuntimeId}-snapshot",
DocumentTypes = [InteropPeerShared.InteropDocumentType],
DocumentKeys = [$"note:{remoteHello.RuntimeId}"]
SchemaIds = [schemaRegistration.SchemaId],
RecordKeys = [$"note:{remoteHello.RuntimeId}"]
});
var snapshot = await ExpectMessageAsync<CultNetSnapshotResponseRawMessage>(stream, CultNetSchemaVersions.SnapshotResponseRaw);
await documentRegistry.ApplyRawSnapshotResponseAsync(cache, snapshot);
@ -504,7 +499,6 @@ static CultNetInteropNote BuildInteropNote(string runtimeId, string displayName)
var documentId = $"note:{runtimeId}";
return new CultNetInteropNote
{
ID = CreateDeterministicGuid(documentId),
SchemaVersion = InteropPeerShared.InteropSchemaVersion,
DocumentId = documentId,
AuthorRuntimeId = runtimeId,
@ -532,7 +526,6 @@ static CultNetInteropNote DeserializeInteropNotePayload(byte[] payload)
var decoded = MessagePackSerializer.Deserialize<CultNetInteropNotePayload>(payload, CultNetSchemaMessageSerialization.Options);
return new CultNetInteropNote
{
ID = CreateDeterministicGuid(decoded.DocumentId),
SchemaVersion = decoded.SchemaVersion,
DocumentId = decoded.DocumentId,
AuthorRuntimeId = decoded.AuthorRuntimeId,
@ -542,12 +535,6 @@ static CultNetInteropNote DeserializeInteropNotePayload(byte[] payload)
};
}
static Guid CreateDeterministicGuid(string value)
{
var bytes = System.Security.Cryptography.MD5.HashData(System.Text.Encoding.UTF8.GetBytes(value));
return new Guid(bytes);
}
static async Task WaitForeverAsync(CancellationToken cancellationToken)
{
try
@ -678,14 +665,16 @@ static class InteropPeerShared
};
}
public sealed class CultNetInteropNote : DatabaseEntry
[CultDocument(InteropPeerShared.InteropDocumentType, InteropPeerShared.InteropSchemaVersion)]
[MessagePackObject]
public sealed class CultNetInteropNote
{
public string SchemaVersion { get; set; } = InteropPeerShared.InteropSchemaVersion;
public string DocumentId { get; set; } = string.Empty;
public string AuthorRuntimeId { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Body { get; set; } = string.Empty;
public string[] Tags { get; set; } = Array.Empty<string>();
[Key(0)] public string SchemaVersion { get; set; } = InteropPeerShared.InteropSchemaVersion;
[Key(1)] [CultName] public string DocumentId { get; set; } = string.Empty;
[Key(2)] public string AuthorRuntimeId { get; set; } = string.Empty;
[Key(3)] public string Title { get; set; } = string.Empty;
[Key(4)] public string Body { get; set; } = string.Empty;
[Key(5)] public string[] Tags { get; set; } = Array.Empty<string>();
}
[MessagePackObject]

View file

@ -174,10 +174,9 @@ namespace GameCult.Networking.Tests
[
new CultNetRawDocumentRecord
{
DocumentType = "ghostlight.agent-state",
DocumentKey = "world/main",
SchemaId = "sha256:ghostlight-agent-state",
RecordKey = "world/main",
StoredAt = "2026-05-06T12:34:56.0000000+00:00",
PayloadSchemaVersion = "ghostlight.agent_state.v0",
PayloadEncoding = "messagepack",
Payload = [0x91, 0xA3, 0x66, 0x6F, 0x6F],
SourceRuntimeId = "voidbot",
@ -193,7 +192,7 @@ namespace GameCult.Networking.Tests
Assert.That(roundTrip.MessageId, Is.EqualTo("snapshot-1"));
Assert.That(roundTrip.Documents, Has.Length.EqualTo(1));
Assert.That(roundTrip.Documents[0].DocumentType, Is.EqualTo("ghostlight.agent-state"));
Assert.That(roundTrip.Documents[0].SchemaId, Is.EqualTo("sha256:ghostlight-agent-state"));
Assert.That(roundTrip.Documents[0].PayloadEncoding, Is.EqualTo("messagepack"));
Assert.That(roundTrip.Documents[0].Payload, Is.EqualTo(message.Documents[0].Payload));
Assert.That(roundTrip.Documents[0].Tags, Is.EqualTo(["swarm", "dream"]));
@ -205,27 +204,25 @@ namespace GameCult.Networking.Tests
var sourceCache = new CultCache();
var targetCache = new CultCache();
var registry = new CultNetDocumentRegistry()
.Register(CultNetDocumentBinding.ForEntry<PlayerData>(
"gamecult.player_data",
payloadSchemaVersion: "gamecult.player_data.v0",
.Register(CultNetDocumentBinding.ForDocument<PlayerData>(
payloadSerializer: SerializePlayerDataPayload,
payloadDeserializer: DeserializePlayerDataPayload));
var sourceEntry = new PlayerData
{
ID = Guid.NewGuid(),
PlayerId = Guid.NewGuid(),
Email = "cult@example.test",
PasswordHash = "not-a-real-hash",
Username = "CultGhost"
};
await sourceCache.AddAsync(sourceEntry);
var handle = await sourceCache.AddAsync(sourceEntry);
var expectedPayload = SerializePlayerDataPayload(sourceEntry);
var request = registry.CreateSnapshotRequest(
"request-1",
documentTypes: ["gamecult.player_data"],
documentKeys: [sourceEntry.ID.ToString("D")]);
schemaIds: [sourceCache.Registry.GetRequired<PlayerData>().SchemaId],
recordKeys: [handle.Key.Value]);
var response = registry.CreateRawSnapshotResponse(sourceCache, "snapshot-1", request);
var serializedResponse = CultNetSchemaMessageSerialization.Serialize(response);
var roundTrip = (CultNetSnapshotResponseRawMessage)CultNetSchemaMessageSerialization.Deserialize(serializedResponse);
@ -234,7 +231,7 @@ namespace GameCult.Networking.Tests
Assert.That(roundTrip.Documents[0].Payload, Is.EqualTo(expectedPayload));
await registry.ApplyRawSnapshotResponseAsync(targetCache, roundTrip);
var replicated = targetCache.Get<PlayerData>(sourceEntry.ID);
var replicated = targetCache.GetByIndex<PlayerData>("PlayerId", sourceEntry.PlayerId.ToString("D"));
Assert.That(replicated, Is.Not.Null);
Assert.That(SerializePlayerDataPayload(replicated!), Is.EqualTo(expectedPayload));
@ -296,7 +293,7 @@ namespace GameCult.Networking.Tests
[MessagePack.MessagePackObject]
public sealed class PlayerDataPayload
{
[MessagePack.Key(0)] public Guid Id { get; set; }
[MessagePack.Key(0)] public Guid PlayerId { get; set; }
[MessagePack.Key(1)] public string Email { get; set; } = string.Empty;
[MessagePack.Key(2)] public string PasswordHash { get; set; } = string.Empty;
[MessagePack.Key(3)] public string Username { get; set; } = string.Empty;
@ -306,7 +303,7 @@ namespace GameCult.Networking.Tests
{
return MessagePack.MessagePackSerializer.Serialize(new PlayerDataPayload
{
Id = entry.ID,
PlayerId = entry.PlayerId,
Email = entry.Email,
PasswordHash = entry.PasswordHash,
Username = entry.Username
@ -318,7 +315,7 @@ namespace GameCult.Networking.Tests
var decoded = MessagePack.MessagePackSerializer.Deserialize<PlayerDataPayload>(payload);
return new PlayerData
{
ID = decoded.Id,
PlayerId = decoded.PlayerId,
Email = decoded.Email,
PasswordHash = decoded.PasswordHash,
Username = decoded.Username