Make generated and reflective document members one rule

The generator and CultDocumentRegistry now discover members by the same
steps: most-derived declaration first, override chain collapsed to one
member, attributes read up the override chain, rejections in one order
with one text (GCC001 / InvalidOperationException).

- Accessors are named per document, so documents sharing a [CultName] or
  [CultIndex] base member no longer emit duplicate methods (CS0111).
- Duplicate [Key] slots are refused, including new-hidden fields and
  properties; a hidden persisted member with a different slot is refused.
- String [Key("x")] gets its own message instead of "has no [Key]".
- A persisted property whose set accessor is private, protected or init,
  or a readonly field, is refused instead of CS0272 in generated code.
- Overrides: MessagePack's DynamicObjectResolver reads the base
  declaration's [Key]/[IgnoreMember] (it throws "key is duplicated" for a
  re-keyed override and serializes an override marked [IgnoreMember]).
  An override that changes them is refused in both paths; an override
  that repeats or omits them is accepted and byte-pinned.
- Typed Serialize<T>/Deserialize<T> used static Options and skipped
  generated codecs and declared resolvers; deleted. CultNetDocumentBinding
  and CultMeshSnapshots route through the untyped path with their registry.

DocumentShapeSweepTests compiles each shape through the generator and
compares generated vs reflective schema id, catalog, name/index values
and payload against MessagePack's own bytes, or equal rejection text.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-13 22:00:16 +02:00
commit 7c4bc2bdc0
12 changed files with 602 additions and 127 deletions

View file

@ -50,8 +50,8 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
return null;
}
var unkeyedMembers = new List<string>();
var members = DiscoverMembers(typeSymbol, unkeyedMembers);
var rejections = new List<string>();
var members = DiscoverMembers(typeSymbol, rejections);
var nameMember = members.FirstOrDefault(member => member.IsName);
var canConstructForDeserialization = typeSymbol.TypeKind == TypeKind.Struct ||
typeSymbol.InstanceConstructors.Any(constructor =>
@ -71,55 +71,139 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
members.Where(member => member.IndexAlias != null)
.Select(member => new IndexAccessorShape(member.IndexAlias!, member.IndexAccessorMethodName!))
.ToImmutableArray(),
members.ToImmutableArray(),
members,
canConstructForDeserialization,
hasDenseSlots,
unkeyedMembers.Select(name => UnkeyedMemberMessage(typeSymbol.Name, name)).ToImmutableArray(),
rejections.ToImmutableArray(),
typeSymbol.Locations.FirstOrDefault());
}
// The same members CultDocumentRegistry reflects: public instance fields and get/set properties, inherited included.
private static ImmutableArray<MemberShape> DiscoverMembers(INamedTypeSymbol typeSymbol, List<string> unkeyedMembers)
// Mirrors CultDocumentRegistry.DiscoverMembers step for step: public instance fields and get/set properties, most-derived
// declaration first, a property's attributes resolved up its override chain, rejections in the same order and text.
private static ImmutableArray<MemberShape> DiscoverMembers(INamedTypeSymbol typeSymbol, List<string> rejections)
{
var shaped = new List<MemberShape>();
for (var type = typeSymbol; type != null && type.SpecialType != SpecialType.System_Object; type = type.BaseType)
var documentName = typeSymbol.Name;
var documentStem = SanitizeIdentifier(typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
.Replace("global::", string.Empty).Replace(".", "_"));
var candidates = new List<MemberCandidate>();
var seenRoots = new HashSet<ISymbol>(SymbolEqualityComparer.Default);
var depth = 0;
for (var type = typeSymbol; type != null && type.SpecialType != SpecialType.System_Object; type = type.BaseType, depth++)
{
foreach (var member in type.GetMembers())
{
ITypeSymbol memberType;
if (member is IFieldSymbol field && !field.IsStatic && field.DeclaredAccessibility == Accessibility.Public)
memberType = field.Type;
else if (member is IPropertySymbol property && !property.IsStatic && !property.IsIndexer && !property.IsOverride &&
property.DeclaredAccessibility == Accessibility.Public && property.GetMethod != null && property.SetMethod != null)
memberType = property.Type;
else
continue;
if (IsIgnored(member))
continue;
var slot = GetExplicitSlot(member);
if (slot == null)
unkeyedMembers.Add(member.Name);
else
shaped.Add(MemberShapeSeed.From(member, memberType).WithSlot(slot.Value));
if (member is IFieldSymbol field && !field.IsStatic && !field.IsConst && !field.IsImplicitlyDeclared &&
field.DeclaredAccessibility == Accessibility.Public)
{
if (!IsIgnored(field))
candidates.Add(new MemberCandidate(field, field.Type, depth, !field.IsReadOnly, null));
}
else if (member is IPropertySymbol property && !property.IsStatic && !property.IsIndexer &&
property.DeclaredAccessibility == Accessibility.Public && seenRoots.Add(RootOf(property)))
{
var setter = OverrideChain(property).Select(declaration => declaration.SetMethod).FirstOrDefault(method => method != null);
if (OverrideChain(property).All(declaration => declaration.GetMethod == null) || setter == null)
continue;
var root = RootOf(property);
var divergentRoot = OverrideChain(property).Any(declaration =>
!Equals(KeyValue(declaration), KeyValue(root)) || IsIgnored(declaration) != IsIgnored(root))
? root
: null;
if (divergentRoot == null && IsIgnored(property))
continue;
candidates.Add(new MemberCandidate(property, property.Type, depth,
!setter.IsInitOnly && setter.DeclaredAccessibility is Accessibility.Public or Accessibility.Internal or Accessibility.ProtectedOrInternal,
divergentRoot));
}
}
}
return shaped.OrderBy(member => member.Slot).ToImmutableArray();
var keyed = new List<(MemberCandidate Candidate, int Slot)>();
foreach (var candidate in candidates.OrderBy(candidate => candidate.Depth).ThenBy(candidate => candidate.Member.Name, StringComparer.Ordinal))
{
var keyValue = KeyValue(candidate.Member);
if (candidate.DivergentRoot != null)
rejections.Add(DivergentOverrideMessage(documentName, Qualified(candidate.Member), Qualified(candidate.DivergentRoot)));
else if (keyValue is string)
rejections.Add(StringKeyMessage(documentName, candidate.Member.Name));
else if (keyValue is not int slot)
rejections.Add(UnkeyedMemberMessage(documentName, candidate.Member.Name));
else
{
if (!candidate.Writable)
rejections.Add(NotWritableMessage(documentName, candidate.Member.Name));
keyed.Add((candidate, slot));
}
}
foreach (var group in keyed.GroupBy(entry => entry.Slot).Where(group => group.Count() > 1).OrderBy(group => group.Key))
{
var pair = group.Take(2).ToArray();
rejections.Add(DuplicateSlotMessage(documentName, Qualified(pair[0].Candidate.Member), Qualified(pair[1].Candidate.Member), group.Key));
}
foreach (var group in keyed.GroupBy(entry => entry.Candidate.Member.Name)
.Where(group => group.Select(entry => entry.Slot).Distinct().Count() > 1)
.OrderBy(group => group.Key, StringComparer.Ordinal))
{
var pair = group.Take(2).ToArray();
rejections.Add(HiddenMemberMessage(documentName, Qualified(pair[0].Candidate.Member), Qualified(pair[1].Candidate.Member)));
}
return keyed
.Select(entry => MemberShapeSeed.From(entry.Candidate.Member, entry.Candidate.Type).WithSlot(entry.Slot, documentStem))
.OrderBy(member => member.Slot)
.ToImmutableArray();
}
// CultDocumentRegistry.UnkeyedMemberMessage throws the same text.
private static IPropertySymbol RootOf(IPropertySymbol property)
{
while (property.OverriddenProperty != null)
property = property.OverriddenProperty;
return property;
}
private static IEnumerable<IPropertySymbol> OverrideChain(IPropertySymbol property)
{
for (IPropertySymbol? declaration = property; declaration != null; declaration = declaration.OverriddenProperty)
yield return declaration;
}
private static string Qualified(ISymbol member) => member.ContainingType.Name + "." + member.Name;
// CultDocumentRegistry throws the same texts.
private static string UnkeyedMemberMessage(string documentTypeName, string memberName) =>
$"Cult document {documentTypeName} member {memberName} has no [Key]; every persisted member of a [CultDocument] type needs an explicit [Key(n)].";
private static string StringKeyMessage(string documentTypeName, string memberName) =>
$"Cult document {documentTypeName} member {memberName} has a string [Key]; string keys are not supported; use integer [Key(n)].";
private static string NotWritableMessage(string documentTypeName, string memberName) =>
$"Cult document {documentTypeName} member {memberName} is not writable; a persisted member needs a non-readonly field or a public or internal set accessor.";
private static string DuplicateSlotMessage(string documentTypeName, string first, string second, int slot) =>
$"Cult document {documentTypeName} members {first} and {second} share [Key({slot})]; every persisted member needs a distinct [Key(n)].";
private static string DivergentOverrideMessage(string documentTypeName, string member, string root) =>
$"Cult document {documentTypeName} member {member} overrides {root} with a different [Key] or [IgnoreMember]; MessagePack reads the base declaration's, so an override must repeat or omit them.";
private static object? KeyValue(ISymbol member)
{
var key = GetMemberAttribute(member, "MessagePack.KeyAttribute");
return key != null && key.ConstructorArguments.Length > 0 ? key.ConstructorArguments[0].Value : null;
}
private static string HiddenMemberMessage(string documentTypeName, string first, string second) =>
$"Cult document {documentTypeName} member {first} hides persisted member {second}; persisted member names must be unique.";
#pragma warning disable RS2008
private static readonly DiagnosticDescriptor UnkeyedMember = new(
"GCC001", "Cult document member has no [Key]", "{0}", "GameCult.Caching", DiagnosticSeverity.Error, isEnabledByDefault: true);
private static readonly DiagnosticDescriptor RejectedMember = new(
"GCC001", "Cult document member cannot be persisted", "{0}", "GameCult.Caching", DiagnosticSeverity.Error, isEnabledByDefault: true);
#pragma warning restore RS2008
private static bool IsIgnored(ISymbol member)
{
return GetAttribute(member, "MessagePack.IgnoreMemberAttribute") != null;
return GetMemberAttribute(member, "MessagePack.IgnoreMemberAttribute") != null;
}
private static AttributeData? GetAttribute(ISymbol symbol, string fullyQualifiedName)
@ -128,18 +212,12 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
attribute.AttributeClass?.ToDisplayString() == fullyQualifiedName);
}
private static int? GetExplicitSlot(ISymbol member)
// Attribute.GetCustomAttributes(inherit: true) semantics: an override without the attribute inherits its base declaration's.
private static AttributeData? GetMemberAttribute(ISymbol member, string fullyQualifiedName)
{
var keyAttribute = GetAttribute(member, "MessagePack.KeyAttribute");
if (keyAttribute == null || keyAttribute.ConstructorArguments.Length == 0)
{
return null;
}
var argument = keyAttribute.ConstructorArguments[0];
return argument.Kind == TypedConstantKind.Primitive && argument.Value is int slot
? slot
: null;
if (member is not IPropertySymbol property)
return GetAttribute(member, fullyQualifiedName);
return OverrideChain(property).Select(declaration => GetAttribute(declaration, fullyQualifiedName)).FirstOrDefault(attribute => attribute != null);
}
private static void EmitProvider(SourceProductionContext context, Compilation compilation, ImmutableArray<DocumentShape> shapes)
@ -152,11 +230,11 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
.ToArray();
foreach (var shape in shaped)
{
foreach (var message in shape.UnkeyedMemberMessages)
context.ReportDiagnostic(Diagnostic.Create(UnkeyedMember, shape.Location, message));
foreach (var message in shape.Rejections)
context.ReportDiagnostic(Diagnostic.Create(RejectedMember, shape.Location, message));
}
var documents = shaped.Where(shape => shape.UnkeyedMemberMessages.IsEmpty).ToArray();
var documents = shaped.Where(shape => shape.Rejections.IsEmpty).ToArray();
if (documents.Length == 0)
{
@ -410,13 +488,30 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
namedType.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T;
}
private sealed class MemberCandidate
{
public MemberCandidate(ISymbol member, ITypeSymbol type, int depth, bool writable, IPropertySymbol? divergentRoot)
{
Member = member;
Type = type;
Depth = depth;
Writable = writable;
DivergentRoot = divergentRoot;
}
public ISymbol Member { get; }
public ITypeSymbol Type { get; }
public int Depth { get; }
public bool Writable { get; }
public IPropertySymbol? DivergentRoot { get; }
}
private sealed class MemberShapeSeed
{
private MemberShapeSeed(
string name,
string schemaTypeName,
string typeSyntaxName,
string sortKey,
bool isName,
string? indexAlias,
bool isReference,
@ -427,7 +522,6 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
Name = name;
SchemaTypeName = schemaTypeName;
TypeSyntaxName = typeSyntaxName;
SortKey = sortKey;
IsName = isName;
IndexAlias = indexAlias;
IsReference = isReference;
@ -439,7 +533,6 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
public string Name { get; }
public string SchemaTypeName { get; }
public string TypeSyntaxName { get; }
public string SortKey { get; }
public bool IsName { get; }
public string? IndexAlias { get; }
public bool IsReference { get; }
@ -447,9 +540,11 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
public bool CanBeNull { get; }
public string? TargetSchemaName { get; }
public MemberShape WithSlot(int slot)
// Accessors are named per document: one provider class holds every document of the assembly, and documents sharing a
// base member would otherwise emit the same method twice.
public MemberShape WithSlot(int slot, string documentStem)
{
var accessorStem = SanitizeIdentifier(SortKey);
var accessorStem = documentStem + "_" + SanitizeIdentifier(Name);
return new MemberShape(
Name,
slot,
@ -467,7 +562,7 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
public static MemberShapeSeed From(ISymbol member, ITypeSymbol memberType)
{
var referenceAttribute = GetAttribute(member, "GameCult.Caching.CultReferenceAttribute");
var referenceAttribute = GetMemberAttribute(member, "GameCult.Caching.CultReferenceAttribute");
var explicitTarget = referenceAttribute?.ConstructorArguments.Length > 0
? referenceAttribute.ConstructorArguments[0].Value as INamedTypeSymbol
: null;
@ -478,7 +573,7 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
var targetSchemaName = targetType == null
? null
: GetAttribute(targetType, "GameCult.Caching.CultDocumentAttribute")?.ConstructorArguments[0].Value as string;
var indexAttribute = GetAttribute(member, "GameCult.Caching.CultIndexAttribute");
var indexAttribute = GetMemberAttribute(member, "GameCult.Caching.CultIndexAttribute");
var indexAlias = indexAttribute == null
? null
: indexAttribute.ConstructorArguments.Length == 0 || indexAttribute.ConstructorArguments[0].Value is not string alias || string.IsNullOrWhiteSpace(alias)
@ -489,8 +584,7 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
member.Name,
GetSchemaTypeName(memberType),
memberType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
BuildAccessorStem(member),
GetAttribute(member, "GameCult.Caching.CultNameAttribute") != null,
GetMemberAttribute(member, "GameCult.Caching.CultNameAttribute") != null,
indexAlias,
targetType != null || referenceAttribute != null,
many,
@ -512,13 +606,6 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
? namedType.TypeArguments[0] as INamedTypeSymbol
: null;
}
private static string BuildAccessorStem(ISymbol member)
{
return member.ContainingType == null
? member.Name
: member.ContainingType.Name + "_" + member.Name;
}
}
private sealed class DocumentShape
@ -534,10 +621,10 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
ImmutableArray<MemberShape> members,
bool canConstructForDeserialization,
bool hasDenseSlots,
ImmutableArray<string> unkeyedMemberMessages,
ImmutableArray<string> rejections,
Location? location)
{
UnkeyedMemberMessages = unkeyedMemberMessages;
Rejections = rejections;
Location = location;
DocumentTypeName = documentTypeName;
SchemaName = schemaName;
@ -566,7 +653,7 @@ public sealed class CultDocumentMessagePackGenerator : IIncrementalGenerator
public ImmutableArray<MemberShape> Members { get; }
public bool CanConstructForDeserialization { get; }
public bool HasDenseSlots { get; }
public ImmutableArray<string> UnkeyedMemberMessages { get; }
public ImmutableArray<string> Rejections { get; }
public Location? Location { get; }
}

View file

@ -69,16 +69,6 @@ public static class CultDocumentMessagePackSerialization
$"Formatter resolver {resolverType.FullName} needs a public static Instance or a public parameterless constructor and must implement {nameof(IFormatterResolver)}.");
}
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 SerializeUntyped(value, type, CultDocumentRegistry.Shared);

View file

@ -900,31 +900,126 @@ namespace GameCult.Caching
public int[] DefaultedMissingSlots { get; }
}
// CultDocumentMessagePackGenerator.DiscoverMembers mirrors this step for step and reports the same rejections as GCC001.
// Public instance fields and get/set properties, most-derived declaration first; GetCustomAttributes inherits a property's
// attributes up its override chain, as MessagePack's own resolver reads them.
private static IReadOnlyList<PersistedMember> DiscoverMembers(Type type)
{
var members = new List<PersistedMember>();
foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
const BindingFlags declared = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public;
var candidates = new List<(MemberInfo Member, Type MemberType, int Depth, bool Writable, PropertyInfo? DivergentRoot)>();
var seenRoots = new HashSet<(Type, string)>();
var depth = 0;
for (var current = type; current != null && current != typeof(object); current = current.BaseType, depth++)
{
if (!IsIgnored(field))
members.Add(PersistedMember.FromMember(field, field.FieldType, value => field.GetValue(value), RequireKey(type, field)));
foreach (var field in current.GetFields(declared))
{
if (!IsIgnored(field))
candidates.Add((field, field.FieldType, depth, !field.IsInitOnly, null));
}
foreach (var property in current.GetProperties(declared))
{
if (property.GetIndexParameters().Length > 0 || !seenRoots.Add(RootOf(property)))
continue;
var chain = OverrideChain(property).ToArray();
var setter = chain.Select(declaration => declaration.SetMethod).FirstOrDefault(method => method != null);
if (chain.All(declaration => declaration.GetMethod == null) || setter == null)
continue;
// MessagePack's resolver reads the base declaration's [Key]/[IgnoreMember]; an override that changes them is refused.
var root = chain[chain.Length - 1];
var divergentRoot = chain.Any(declaration =>
!Equals(GetKeyValue(declaration), GetKeyValue(root)) || IsIgnored(declaration) != IsIgnored(root))
? root
: null;
if (divergentRoot == null && IsIgnored(property))
continue;
candidates.Add((property, property.PropertyType, depth, IsWritable(setter), divergentRoot));
}
}
foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
var rejections = new List<string>();
var keyed = new List<(MemberInfo Member, Type MemberType, int Slot)>();
foreach (var candidate in candidates.OrderBy(candidate => candidate.Depth).ThenBy(candidate => candidate.Member.Name, StringComparer.Ordinal))
{
if (property.GetMethod != null && property.SetMethod != null && !IsIgnored(property))
members.Add(PersistedMember.FromMember(property, property.PropertyType, value => property.GetValue(value), RequireKey(type, property)));
var key = GetKeyValue(candidate.Member);
if (candidate.DivergentRoot != null)
rejections.Add(DivergentOverrideMessage(type.Name, Qualified(candidate.Member), Qualified(candidate.DivergentRoot)));
else if (key is string)
rejections.Add(StringKeyMessage(type.Name, candidate.Member.Name));
else if (key is not int slot)
rejections.Add(UnkeyedMemberMessage(type.Name, candidate.Member.Name));
else
{
if (!candidate.Writable)
rejections.Add(NotWritableMessage(type.Name, candidate.Member.Name));
keyed.Add((candidate.Member, candidate.MemberType, slot));
}
}
return members.OrderBy(member => member.Slot).ToArray();
foreach (var group in keyed.GroupBy(entry => entry.Slot).Where(group => group.Count() > 1).OrderBy(group => group.Key))
{
var pair = group.Take(2).ToArray();
rejections.Add(DuplicateSlotMessage(type.Name, Qualified(pair[0].Member), Qualified(pair[1].Member), group.Key));
}
foreach (var group in keyed.GroupBy(entry => entry.Member.Name)
.Where(group => group.Select(entry => entry.Slot).Distinct().Count() > 1)
.OrderBy(group => group.Key, StringComparer.Ordinal))
{
var pair = group.Take(2).ToArray();
rejections.Add(HiddenMemberMessage(type.Name, Qualified(pair[0].Member), Qualified(pair[1].Member)));
}
if (rejections.Count > 0)
throw new InvalidOperationException(rejections[0]);
return keyed
.Select(entry => PersistedMember.FromMember(
entry.Member,
entry.MemberType,
entry.Member is FieldInfo field ? field.GetValue : ((PropertyInfo)entry.Member).GetValue,
entry.Slot))
.OrderBy(member => member.Slot)
.ToArray();
}
// The generator reports the same text as diagnostic GCC001.
private static (Type, string) RootOf(PropertyInfo property) =>
((property.GetMethod ?? property.SetMethod)!.GetBaseDefinition().DeclaringType!, property.Name);
private static IEnumerable<PropertyInfo> OverrideChain(PropertyInfo property)
{
var root = RootOf(property);
for (var current = property.DeclaringType; current != null; current = current.BaseType)
{
var declaration = current.GetProperty(property.Name, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (declaration != null && declaration.GetIndexParameters().Length == 0 && RootOf(declaration) == root)
yield return declaration;
}
}
private static bool IsWritable(MethodInfo setter) =>
(setter.IsPublic || setter.IsAssembly || setter.IsFamilyOrAssembly) &&
!setter.ReturnParameter.GetRequiredCustomModifiers().Any(modifier => modifier.FullName == "System.Runtime.CompilerServices.IsExternalInit");
private static string Qualified(MemberInfo member) => member.DeclaringType!.Name.Split('`')[0] + "." + member.Name;
internal static string UnkeyedMemberMessage(string documentTypeName, string memberName) =>
$"Cult document {documentTypeName} member {memberName} has no [Key]; every persisted member of a [CultDocument] type needs an explicit [Key(n)].";
private static int RequireKey(Type documentType, MemberInfo member) =>
GetKeyValue(member) ?? throw new InvalidOperationException(UnkeyedMemberMessage(documentType.Name, member.Name));
private static string StringKeyMessage(string documentTypeName, string memberName) =>
$"Cult document {documentTypeName} member {memberName} has a string [Key]; string keys are not supported; use integer [Key(n)].";
private static string NotWritableMessage(string documentTypeName, string memberName) =>
$"Cult document {documentTypeName} member {memberName} is not writable; a persisted member needs a non-readonly field or a public or internal set accessor.";
private static string DuplicateSlotMessage(string documentTypeName, string first, string second, int slot) =>
$"Cult document {documentTypeName} members {first} and {second} share [Key({slot})]; every persisted member needs a distinct [Key(n)].";
private static string DivergentOverrideMessage(string documentTypeName, string member, string root) =>
$"Cult document {documentTypeName} member {member} overrides {root} with a different [Key] or [IgnoreMember]; MessagePack reads the base declaration's, so an override must repeat or omit them.";
private static string HiddenMemberMessage(string documentTypeName, string first, string second) =>
$"Cult document {documentTypeName} member {first} hides persisted member {second}; persisted member names must be unique.";
private static bool IsIgnored(MemberInfo member)
{
@ -935,24 +1030,19 @@ namespace GameCult.Caching
});
}
private static int? GetKeyValue(MemberInfo member)
private static object? GetKeyValue(MemberInfo member)
{
foreach (var attribute in member.GetCustomAttributes())
{
var name = attribute.GetType().FullName;
if (name == "MessagePack.KeyAttribute")
{
var property = attribute.GetType().GetProperty("IntKey");
if (property?.GetValue(attribute) is int intKey)
if (attribute.GetType().GetProperty("IntKey")?.GetValue(attribute) is int intKey)
{
return intKey;
}
var ctorArg = attribute.GetType().GetProperty("StringKey");
if (ctorArg?.GetValue(attribute) is string)
{
return null;
}
return attribute.GetType().GetProperty("StringKey")?.GetValue(attribute) as string;
}
}

View file

@ -1023,7 +1023,7 @@ namespace GameCult.Mesh
}
else
{
documents.Add(CultDocumentMessagePackSerialization.Deserialize<TDocument>(record.Payload));
documents.Add((TDocument)CultDocumentMessagePackSerialization.DeserializeUntyped(typeof(TDocument), record.Payload));
}
}

View file

@ -80,7 +80,8 @@ namespace GameCult.Networking
Func<byte[], T>? payloadDeserializer = null)
where T : class
{
var descriptor = (registry ?? CultDocumentRegistry.Shared).GetRequired<T>();
var documents = registry ?? CultDocumentRegistry.Shared;
var descriptor = documents.GetRequired<T>();
return new CultNetDocumentBinding(
typeof(T),
string.IsNullOrWhiteSpace(schemaId) ? descriptor.SchemaId : schemaId!,
@ -89,11 +90,11 @@ namespace GameCult.Networking
var typed = (T)document;
return payloadSerializer != null
? payloadSerializer(typed)
: CultDocumentMessagePackSerialization.Serialize(typed);
: CultDocumentMessagePackSerialization.SerializeUntyped(typed, typeof(T), documents);
},
payload => payloadDeserializer != null
? payloadDeserializer(payload)
: CultDocumentMessagePackSerialization.Deserialize<T>(payload));
: (T)CultDocumentMessagePackSerialization.DeserializeUntyped(typeof(T), payload, documents));
}
/// <summary>
@ -111,7 +112,8 @@ namespace GameCult.Networking
throw new ArgumentNullException(nameof(documentType));
}
var descriptor = (registry ?? CultDocumentRegistry.Shared).GetRequired(documentType);
var documents = registry ?? CultDocumentRegistry.Shared;
var descriptor = documents.GetRequired(documentType);
return new CultNetDocumentBinding(
documentType,
string.IsNullOrWhiteSpace(schemaId) ? descriptor.SchemaId : schemaId!,
@ -125,11 +127,11 @@ namespace GameCult.Networking
return payloadSerializer != null
? payloadSerializer(document)
: CultDocumentMessagePackSerialization.SerializeUntyped(document, documentType);
: CultDocumentMessagePackSerialization.SerializeUntyped(document, documentType, documents);
},
payload => payloadDeserializer != null
? payloadDeserializer(payload)
: CultDocumentMessagePackSerialization.DeserializeUntyped(documentType, payload));
: CultDocumentMessagePackSerialization.DeserializeUntyped(documentType, payload, documents));
}
}