From 52e6ca36f072372922e42781ccb2b397e7f9c2e3 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Sun, 14 Apr 2024 17:00:33 -0400 Subject: [PATCH] feat(SyncDictionary): Added OnChange Action --- Assets/Mirror/Core/SyncDictionary.cs | 12 ++++++++ .../SyncCollections/SyncDictionaryTest.cs | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Assets/Mirror/Core/SyncDictionary.cs b/Assets/Mirror/Core/SyncDictionary.cs index d22d39531..3cf73f519 100644 --- a/Assets/Mirror/Core/SyncDictionary.cs +++ b/Assets/Mirror/Core/SyncDictionary.cs @@ -15,6 +15,14 @@ public class SyncIDictionary : SyncObject, IDictionaryThis is called after the item is removed with TKey. TValue is the OLD item public Action OnRemove; + /// + /// This is called for all changes to the Dictionary. + /// For OP_ADD, TValue is the NEW value of the entry. + /// For OP_SET and OP_REMOVE, TValue is the OLD value of the entry. + /// For OP_CLEAR, both TKey and TValue are default. + /// + public Action OnChange; + /// This is called before the data is cleared public Action OnClear; @@ -320,15 +328,19 @@ void AddOperation(Operation op, TKey key, TValue item, TValue oldItem, bool chec { case Operation.OP_ADD: OnAdd?.Invoke(key); + OnChange?.Invoke(op, key, item); break; case Operation.OP_SET: OnSet?.Invoke(key, oldItem); + OnChange?.Invoke(op, key, oldItem); break; case Operation.OP_REMOVE: OnRemove?.Invoke(key, oldItem); + OnChange?.Invoke(op, key, oldItem); break; case Operation.OP_CLEAR: OnClear?.Invoke(); + OnChange?.Invoke(op, default, default); break; } diff --git a/Assets/Mirror/Tests/Editor/SyncCollections/SyncDictionaryTest.cs b/Assets/Mirror/Tests/Editor/SyncCollections/SyncDictionaryTest.cs index 1a41af07d..6aaec21fa 100644 --- a/Assets/Mirror/Tests/Editor/SyncCollections/SyncDictionaryTest.cs +++ b/Assets/Mirror/Tests/Editor/SyncCollections/SyncDictionaryTest.cs @@ -290,10 +290,20 @@ public void CallbackTest() Assert.That(clientSyncDictionary[key], Is.EqualTo("yay")); }; + bool changeActionCalled = false; + clientSyncDictionary.OnChange = (op, key, item) => + { + changeActionCalled = true; + Assert.That(op, Is.EqualTo(SyncDictionary.Operation.OP_ADD)); + Assert.That(key, Is.EqualTo(3)); + Assert.That(clientSyncDictionary[key], Is.EqualTo("yay")); + }; + serverSyncDictionary.Add(3, "yay"); SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary); Assert.That(called, Is.True); Assert.That(actionCalled, Is.True); + Assert.That(changeActionCalled, Is.True); } [Test] @@ -320,9 +330,19 @@ public void ServerCallbackTest() Assert.That(serverSyncDictionary[key], Is.EqualTo("yay")); }; + bool changeActionCalled = false; + serverSyncDictionary.OnChange = (op, key, item) => + { + changeActionCalled = true; + Assert.That(op, Is.EqualTo(SyncDictionary.Operation.OP_ADD)); + Assert.That(key, Is.EqualTo(3)); + Assert.That(serverSyncDictionary[key], Is.EqualTo("yay")); + }; + serverSyncDictionary[3] = "yay"; Assert.That(called, Is.True); Assert.That(actionCalled, Is.True); + Assert.That(changeActionCalled, Is.True); } [Test] @@ -348,10 +368,20 @@ public void CallbackRemoveTest() Assert.That(!clientSyncDictionary.ContainsKey(1)); }; + bool changeActionCalled = false; + clientSyncDictionary.OnChange = (op, key, item) => + { + changeActionCalled = true; + Assert.That(op, Is.EqualTo(SyncDictionary.Operation.OP_REMOVE)); + Assert.That(key, Is.EqualTo(1)); + Assert.That(item, Is.EqualTo("World")); + }; + serverSyncDictionary.Remove(1); SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary); Assert.That(called, Is.True); Assert.That(actionCalled, Is.True); + Assert.That(changeActionCalled, Is.True); } [Test]