From 40d52502e5a283605928497986bff8c20c95f324 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 14 Sep 2026 14:08:17 +0200 Subject: [PATCH] Discard the Studio edit copy when a drawer throws A drawer that threw after mutating a reference value in place left that half-made mutation in the live edit copy, where the next unrelated change saved it. DrawDocument now reports a drawer failure, and the window drops the edit copy uncommitted so the next frame re-clones from the cache; the error row says nothing from that frame was saved. The drawer contract no longer invites in-place mutation: a change returns the new value and sets GUI.changed, and an in-place mutation without GUI.changed is not saved reliably. Detecting such a mutation would mean serializing the document every frame, so it is documented, not detected. Co-Authored-By: Claude Opus 5 --- .../Caching/Editor/CultCacheStudioDrawers.cs | 20 +++++++++++++------ .../Caching/Editor/CultCacheStudioWindow.cs | 10 ++++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/GameCult.Unity/Assets/Caching/Editor/CultCacheStudioDrawers.cs b/src/GameCult.Unity/Assets/Caching/Editor/CultCacheStudioDrawers.cs index b704d0a8..50f5be9d 100644 --- a/src/GameCult.Unity/Assets/Caching/Editor/CultCacheStudioDrawers.cs +++ b/src/GameCult.Unity/Assets/Caching/Editor/CultCacheStudioDrawers.cs @@ -10,10 +10,13 @@ using Object = UnityEngine.Object; namespace GameCult.Unity.Caching.Editor { - // An IMGUI drawer claimed through [CultInspectorDrawer]. Returns the value's next value; a reference value may be - // mutated in place (it is always the edit's private copy). type is the value's declared type. member is the member - // the value belongs to, also for list elements and dictionary keys and values, and null for a bare value. - // inspector.DrawDefault hands the value back to built-in drawing; inspector.DrawValue draws a sub-value with claims. + // An IMGUI drawer claimed through [CultInspectorDrawer]. A change returns the new value and sets GUI.changed, as IMGUI + // controls do; only a frame that reports a change is saved. Mutating value in place without GUI.changed is not saved + // reliably: it lingers in the edit copy until some later change saves it. A drawer that throws discards the whole + // frame's edit copy, so nothing it or any other drawer did that frame is saved. type is the value's declared type. + // member is the member the value belongs to, also for list elements and dictionary keys and values, and null for a + // bare value. inspector.DrawDefault hands the value back to built-in drawing; inspector.DrawValue draws a sub-value + // with claims. public interface ICultInspectorDrawer { object Draw(CultInspector inspector, string label, Type type, object value, MemberInfo member); @@ -30,6 +33,7 @@ namespace GameCult.Unity.Caching.Editor private readonly Dictionary _foldouts = new Dictionary(StringComparer.Ordinal); private readonly Dictionary _notices = new Dictionary(StringComparer.Ordinal); private string _path = string.Empty; + private bool _drawerFailed; internal CultInspector(CultInspectorModel model) { @@ -53,9 +57,12 @@ namespace GameCult.Unity.Caching.Editor } } - internal void DrawDocument(CultInspectorEdit edit) + // False when a drawer threw: the edit copy may hold part of its in-place mutation and must be discarded, not saved. + internal bool DrawDocument(CultInspectorEdit edit) { + _drawerFailed = false; DrawMembers(edit.Document, edit.Source.Descriptor.DocumentType, edit.Source.Key.Value); + return !_drawerFailed; } // Draws a sub-value of the value being drawn, claims applied. @@ -122,7 +129,8 @@ namespace GameCult.Unity.Caching.Editor catch (Exception exception) { GUI.changed = changed; - return ErrorRow(label, claim.Drawer.Name + " failed: " + exception.Message, value); + _drawerFailed = true; + return ErrorRow(label, claim.Drawer.Name + " failed: " + exception.Message + " Nothing edited this frame was saved.", value); } } finally diff --git a/src/GameCult.Unity/Assets/Caching/Editor/CultCacheStudioWindow.cs b/src/GameCult.Unity/Assets/Caching/Editor/CultCacheStudioWindow.cs index cffe5a07..5d980cb7 100644 --- a/src/GameCult.Unity/Assets/Caching/Editor/CultCacheStudioWindow.cs +++ b/src/GameCult.Unity/Assets/Caching/Editor/CultCacheStudioWindow.cs @@ -208,15 +208,17 @@ namespace GameCult.Unity.Caching.Editor _inspectorScroll = EditorGUILayout.BeginScrollView(_inspectorScroll); // Drawers edit the edit's private copy. A commit spends the edit, admitted or refused, and the next frame - // begins a new one from whatever record the cache then holds. + // begins a new one from whatever record the cache then holds. A frame in which a drawer threw is discarded + // the same way, uncommitted: its copy may hold a half-made mutation. if (_edit == null || !_edit.IsFor(record)) _edit = _model.BeginEdit(record); var readOnly = ReadOnly; using (new EditorGUI.DisabledScope(readOnly)) { EditorGUI.BeginChangeCheck(); - _inspector.DrawDocument(_edit); - if (EditorGUI.EndChangeCheck() && !readOnly && !_edit.Commit(_cache, out var error)) - SetStatus(error, MessageType.Error); + var drawn = _inspector.DrawDocument(_edit); + var changed = EditorGUI.EndChangeCheck(); + if (!drawn) _edit = null; + else if (changed && !readOnly && !_edit.Commit(_cache, out var error)) SetStatus(error, MessageType.Error); } EditorGUILayout.EndScrollView();