|
| 1 | +// Unity C# reference source |
| 2 | +// Copyright (c) Unity Technologies. For terms of use, see |
| 3 | +// https://unity3d.com/legal/licenses/Unity_Reference_Only_License |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.Experimental.RMGUI; |
| 9 | +using Object = UnityEngine.Object; |
| 10 | + |
| 11 | +namespace UnityEditor |
| 12 | +{ |
| 13 | + class DataWatchHandle : IDataWatchHandle |
| 14 | + { |
| 15 | + public readonly int id; |
| 16 | + public WeakReference service; |
| 17 | + |
| 18 | + public Object watched { get; private set; } |
| 19 | + |
| 20 | + public DataWatchHandle(int id, DataWatchService service, Object watched) |
| 21 | + { |
| 22 | + this.id = id; |
| 23 | + this.service = new WeakReference(service); |
| 24 | + this.watched = watched; |
| 25 | + } |
| 26 | + |
| 27 | + public bool disposed |
| 28 | + { |
| 29 | + get |
| 30 | + { |
| 31 | + return ReferenceEquals(watched, null); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public void Dispose() |
| 36 | + { |
| 37 | + if (disposed) |
| 38 | + { |
| 39 | + throw new InvalidOperationException("DataWatchHandle was already disposed of"); |
| 40 | + } |
| 41 | + if (service != null && service.IsAlive) |
| 42 | + { |
| 43 | + (service.Target as DataWatchService).RemoveWatch(this); |
| 44 | + } |
| 45 | + service = null; |
| 46 | + watched = null; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + internal class DataWatchService : IDataWatchService |
| 51 | + { |
| 52 | + private HashSet<Object> m_DirtySet = new HashSet<Object>(); |
| 53 | + |
| 54 | + struct Spy |
| 55 | + { |
| 56 | + public readonly int handleID; |
| 57 | + public readonly VisualElement watcher; |
| 58 | + public readonly Action onDataChanged; |
| 59 | + |
| 60 | + public Spy(int handleID, VisualElement watcher, Action onDataChanged) |
| 61 | + { |
| 62 | + this.handleID = handleID; |
| 63 | + this.watcher = watcher; |
| 64 | + this.onDataChanged = onDataChanged; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + struct Watchers |
| 69 | + { |
| 70 | + public List<Spy> spyList; |
| 71 | + public ChangeTrackerHandle tracker; |
| 72 | + } |
| 73 | + |
| 74 | + private Dictionary<int, DataWatchHandle> m_Handles = new Dictionary<int, DataWatchHandle>(); |
| 75 | + private Dictionary<Object, Watchers> m_Watched = new Dictionary<Object, Watchers>(); |
| 76 | + |
| 77 | + public DataWatchService() |
| 78 | + { |
| 79 | + // TODO probably just do this when panel becomes active and stop when being inactive |
| 80 | + Undo.postprocessModifications += PostProcessUndo; |
| 81 | + } |
| 82 | + |
| 83 | + ~DataWatchService() |
| 84 | + { |
| 85 | + Undo.postprocessModifications -= PostProcessUndo; |
| 86 | + } |
| 87 | + |
| 88 | + // callback to watch undo stack for changes |
| 89 | + // TODO: add the hook before animation strips some changes in record mode |
| 90 | + public UndoPropertyModification[] PostProcessUndo(UndoPropertyModification[] modifications) |
| 91 | + { |
| 92 | + foreach (var m in modifications) |
| 93 | + { |
| 94 | + var cv = m.currentValue; |
| 95 | + if (cv == null) |
| 96 | + continue; |
| 97 | + if (m_Watched.ContainsKey(cv.target)) |
| 98 | + { |
| 99 | + m_DirtySet.Add(cv.target); |
| 100 | + } |
| 101 | + } |
| 102 | + return modifications; |
| 103 | + } |
| 104 | + |
| 105 | + // go through all trackers and poll their native revisions |
| 106 | + public void PollNativeData() |
| 107 | + { |
| 108 | + foreach (var w in m_Watched) |
| 109 | + { |
| 110 | + // Unity Object can be destroyed without us knowing |
| 111 | + // They will compare to null but the C# object is still valid |
| 112 | + // Other wise we check the object for changes |
| 113 | + if (w.Key == null || w.Value.tracker.PollForChanges()) |
| 114 | + { |
| 115 | + m_DirtySet.Add(w.Key); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public void ProcessNotificationQueue() |
| 121 | + { |
| 122 | + PollNativeData(); |
| 123 | + |
| 124 | + // copy because dirty call could come in while we roll along |
| 125 | + var tmpDirty = m_DirtySet; |
| 126 | + m_DirtySet = new HashSet<Object>(); |
| 127 | + |
| 128 | + // since callbacks when being invoked we make copies for safe iteration |
| 129 | + var tmpSpys = new List<Spy>(); |
| 130 | + |
| 131 | + // get the set of Dirties from C++ and clear it. |
| 132 | + foreach (var o in tmpDirty) |
| 133 | + { |
| 134 | + Watchers watchers; |
| 135 | + if (m_Watched.TryGetValue(o, out watchers)) |
| 136 | + { |
| 137 | + tmpSpys.Clear(); |
| 138 | + tmpSpys.AddRange(watchers.spyList); |
| 139 | + |
| 140 | + foreach (Spy spy in tmpSpys) |
| 141 | + { |
| 142 | + if (spy.watcher.panel != null) |
| 143 | + { |
| 144 | + // for any watches trigger callbacks |
| 145 | + spy.onDataChanged(); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + Debug.Log("Leaking Data Spies from element: " + spy.watcher); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + tmpDirty.Clear(); |
| 156 | + } |
| 157 | + |
| 158 | + static int s_WatchID; |
| 159 | + |
| 160 | + public IDataWatchHandle AddWatch(VisualElement watcher, Object watched, Action onDataChanged) |
| 161 | + { |
| 162 | + if (watched == null) |
| 163 | + throw new ArgumentException("Object watched cannot be null"); |
| 164 | + |
| 165 | + DataWatchHandle handle = new DataWatchHandle(++s_WatchID, this, watched); |
| 166 | + m_Handles[handle.id] = handle; |
| 167 | + |
| 168 | + Watchers watchers; |
| 169 | + if (!m_Watched.TryGetValue(watched, out watchers)) |
| 170 | + { |
| 171 | + watchers = new Watchers |
| 172 | + { |
| 173 | + spyList = new List<Spy>(), |
| 174 | + tracker = ChangeTrackerHandle.AcquireTracker(watched), |
| 175 | + }; |
| 176 | + m_Watched[watched] = watchers; |
| 177 | + } |
| 178 | + watchers.spyList.Add(new Spy(handle.id, watcher, onDataChanged)); |
| 179 | + return handle; |
| 180 | + } |
| 181 | + |
| 182 | + public void RemoveWatch(IDataWatchHandle handle) |
| 183 | + { |
| 184 | + DataWatchHandle handleImpl = (DataWatchHandle)handle; |
| 185 | + |
| 186 | + if (m_Handles.Remove(handleImpl.id)) |
| 187 | + { |
| 188 | + Watchers watchers; |
| 189 | + if (m_Watched.TryGetValue(handleImpl.watched, out watchers)) |
| 190 | + { |
| 191 | + List<Spy> spyList = watchers.spyList; |
| 192 | + for (int i = 0; i < spyList.Count; i++) |
| 193 | + { |
| 194 | + Spy spy = spyList[i]; |
| 195 | + if (spy.handleID == handleImpl.id) |
| 196 | + { |
| 197 | + spyList.RemoveAt(i); |
| 198 | + if (watchers.spyList.Count == 0) |
| 199 | + { |
| 200 | + watchers.tracker.ReleaseTracker(); |
| 201 | + m_Watched.Remove(handleImpl.watched); |
| 202 | + } |
| 203 | + return; |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + throw new ArgumentException("Data watch was not registered"); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | + |
0 commit comments