Show unkeyed structs by their writable surface, not a lock rule

The unkeyed struct rule found auto-properties by the compiler's
<Name>k__BackingField name and marked the whole struct Unsupported when
one was get-only or a field was readonly. Record structs fell out with a
generic reason, and settable or init properties beside a field were
silently hidden. Editing is in place, so the lock protected nothing.

An unkeyed struct now shows its public fields (readonly ones read-only)
and its public properties that have a setter (init-only ones read-only).
Get-only properties are computed or kept by the in-place edit, so they are
not rows; vector swizzles stay out of the inspector.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-14 14:34:56 +02:00
commit 7d59021b57
3 changed files with 35 additions and 26 deletions

View file

@ -535,21 +535,20 @@ namespace GameCult.Caching
var keyed = KeyedMembers(type);
if (keyed.Length > 0)
return new CultInspectorShape(type, CultInspectorValueKind.Nested) { Members = keyed };
// An unkeyed struct is edited in place through its public fields. State it cannot write in place (a readonly field,
// a get-only auto-property) makes it unsupported until a drawer claims it.
// An unkeyed struct is edited in place: its public fields, readonly ones read-only, then its public properties with
// a setter, init-only ones read-only. Get-only properties are computed (a vector's swizzles) or kept by the
// in-place edit, so they are not rows.
if (type.IsValueType)
{
var locked = type.GetFields(Public).FirstOrDefault(field => field.IsInitOnly)?.Name
?? type.GetProperties(Public).FirstOrDefault(property => property.GetSetMethod() == null &&
type.GetField($"<{property.Name}>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance) != null)?.Name;
if (locked != null)
return Unsupported(type, $"{type.Name}.{locked} is readonly or get-only, so {type.Name} cannot be edited in place; it needs a drawer.");
var fields = type.GetFields(Public)
.OrderBy(field => field.MetadataToken)
.Select((field, slot) => new CultInspectorMember(field, field.FieldType, slot, true, MetadataOf(field)))
var members = type.GetFields(Public).OrderBy(field => field.MetadataToken)
.Select(field => (Member: (MemberInfo)field, Type: field.FieldType, Assignable: !field.IsInitOnly))
.Concat(type.GetProperties(Public).OrderBy(property => property.MetadataToken)
.Where(property => property.GetIndexParameters().Length == 0 && property.GetGetMethod() != null && property.GetSetMethod() != null)
.Select(property => (Member: (MemberInfo)property, Type: property.PropertyType, Assignable: !IsInitOnly(property.GetSetMethod()!))))
.Select((member, slot) => new CultInspectorMember(member.Member, member.Type, slot, member.Assignable, MetadataOf(member.Member)))
.ToArray();
if (fields.Length > 0)
return new CultInspectorShape(type, CultInspectorValueKind.Nested) { Members = fields };
if (members.Length > 0)
return new CultInspectorShape(type, CultInspectorValueKind.Nested) { Members = members };
}
return Unsupported(type, "no drawer for " + type.FullName);
@ -592,6 +591,11 @@ namespace GameCult.Caching
private static int Ancestry(Type? type) => type == null ? 0 : 1 + Ancestry(type.BaseType);
// `init` is a setter whose return carries the IsExternalInit modreq; the language contract, matched by name because
// netstandard2.1 does not ship the type.
private static bool IsInitOnly(MethodInfo setter) =>
setter.ReturnParameter.GetRequiredCustomModifiers().Any(modifier => modifier.FullName == "System.Runtime.CompilerServices.IsExternalInit");
private static Type TypeOf(MemberInfo member) => member is FieldInfo field ? field.FieldType : ((PropertyInfo)member).PropertyType;
private static bool Assignable(MemberInfo member, bool allowPrivate)

View file

@ -45,7 +45,7 @@ catalog slots, `CultInspector*` metadata, whether a member is assignable), the
shape of any value (string, integer, float, bool, enum, `CultRecordRef<T>` with
its candidate records, rank-1 list or array, dictionary, `[Union]` with only
its declared subtypes, nested keyed object, struct edited in place through its
public fields, or unsupported with a reason), drawer claim resolution
public fields and settable properties, or unsupported with a reason), drawer claim resolution
(`CultInspectorDrawerClaims`), and the edit decisions: the key a dictionary
entry keeps and the notice when a key is refused (null, an empty record
reference, or a duplicate: serialized the same as another key or equal by the
@ -59,8 +59,9 @@ renderer and drawers to show and edit the same documents under the same rules.
## Drawers
Built in: every model shape above. A struct without `[Key]` members folds out
to its public fields and is edited in place; a struct with a readonly field or
a get-only auto-property is unsupported until a drawer claims it. Unity object
to its public fields and its public properties with a setter, and is edited in
place; readonly fields and `init` properties show read-only, and get-only
properties are not shown (record structs work as written). Unity object
references draw as object fields. A value no drawer claims and no shape covers
shows a red error row with the model's reason, as does a multi-dimensional
array.