Decide dictionary key edits in the inspection model

A refused key and an unavailable fresh key are now the model's answer:
ReplaceKey returns the kept key with the notice text, FreshKey returns
null with its notice, and IMGUI only shows them.

A key is taken when another serializes the same or when the dictionary's
own comparer calls them equal, so 0.0 against -0.0 is refused instead of
throwing in BuildDictionary.

Keys never actually compared by bytes: the Studio handed the model the
document codec, whose registry guard throws for any non-document value,
so every key fell back to ToString. CultCacheMessagePack now builds the
model with a value serializer, and the Studio and tests both use it.

Tests pin the comparer rule, the unchanged-key rule (an old empty
reference key survives an edit of its value) and serialized identity
(keys whose ToString collides); each fails with its rule removed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-14 14:03:00 +02:00
commit 2a46409017
5 changed files with 138 additions and 70 deletions

View file

@ -291,18 +291,8 @@ namespace GameCult.Unity.Caching.Editor
var item = DrawValue("Value", shape.ValueType, entries[i].Value, member, path + "{" + i + "}.value");
if (EditorGUI.EndChangeCheck())
{
var index = i;
var refusal = Model.RefuseKey(key, entries[i].Key, entries.Where((_, j) => j != index).Select(e => e.Key));
if (refusal != null)
{
_notices[path] = "Refused " + refusal + " on entry " + i + "; its key was kept.";
key = entries[i].Key;
}
else
{
_notices.Remove(path);
}
key = Model.ReplaceKey(shape.Type, entries.Select(e => e.Key).ToArray(), i, key, out var notice);
Notice(path, notice);
entries[i] = new KeyValuePair<object, object>(key, item);
changed = true;
}
@ -320,20 +310,16 @@ namespace GameCult.Unity.Caching.Editor
if (GUILayout.Button("Add"))
{
var fresh = Model.FreshKey(shape.KeyType, entries.Select(e => e.Key), Records);
if (fresh == null)
{
_notices[path] = "No unused " + shape.KeyType.Name + " key is available; nothing was added.";
}
else
var fresh = Model.FreshKey(shape.Type, entries.Select(e => e.Key).ToArray(), Records, out var notice);
Notice(path, notice);
if (fresh != null)
{
entries.Add(new KeyValuePair<object, object>(fresh, Model.CreateDefault(shape.ValueType)));
_notices.Remove(path);
changed = true;
}
}
if (_notices.TryGetValue(path, out var notice)) EditorGUILayout.HelpBox(notice, MessageType.Warning);
DrawNotice(path);
EditorGUI.indentLevel--;
return changed ? Model.BuildDictionary(shape.Type, entries) : dictionary;
}
@ -474,6 +460,18 @@ namespace GameCult.Unity.Caching.Editor
return expanded;
}
// The model's notice for the control at path, kept until that control's next answer.
private void Notice(string path, string notice)
{
if (notice == null) _notices.Remove(path);
else _notices[path] = notice;
}
private void DrawNotice(string path)
{
if (_notices.TryGetValue(path, out var notice)) EditorGUILayout.HelpBox(notice, MessageType.Warning);
}
private static object ErrorRow(string label, string message, object value)
{
EditorGUILayout.LabelField(label, message, ErrorStyle);

View file

@ -273,10 +273,7 @@ namespace GameCult.Unity.Caching.Editor
if (!File.Exists(path)) throw new IOException("the store wrote no file");
}
var registry = _cache.Registry;
_model = new CultInspectorModel(registry,
(value, type) => CultDocumentMessagePackSerialization.SerializeUntyped(value, type, registry),
(type, bytes) => CultDocumentMessagePackSerialization.DeserializeUntyped(type, bytes, registry));
_model = CultCacheMessagePack.CreateInspectorModel(_cache.Registry);
_inspector = new CultInspector(_model);
_path = path;
EditorPrefs.SetString(LastPathKey, path);