mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
fix(SyncDictionary): Clear after Callback (#3789)
This commit is contained in:
parent
05d68f91af
commit
877067af76
@ -195,12 +195,14 @@ public override void OnDeserializeDelta(NetworkReader reader)
|
||||
case Operation.OP_CLEAR:
|
||||
if (apply)
|
||||
{
|
||||
objects.Clear();
|
||||
// add dirty + changes.
|
||||
// ClientToServer needs to set dirty in server OnDeserialize.
|
||||
// no access check: server OnDeserialize can always
|
||||
// write, even for ClientToServer (for broadcasting).
|
||||
AddOperation(Operation.OP_CLEAR, default, default, false);
|
||||
// clear after invoking the callback so users can iterate the dictionary
|
||||
// and take appropriate action on the items before they are wiped.
|
||||
objects.Clear();
|
||||
}
|
||||
break;
|
||||
|
||||
@ -231,8 +233,10 @@ public override void OnDeserializeDelta(NetworkReader reader)
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
objects.Clear();
|
||||
AddOperation(Operation.OP_CLEAR, default, default, true);
|
||||
// clear after invoking the callback so users can iterate the dictionary
|
||||
// and take appropriate action on the items before they are wiped.
|
||||
objects.Clear();
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key) => objects.ContainsKey(key);
|
||||
|
@ -89,9 +89,19 @@ public void TestAdd()
|
||||
[Test]
|
||||
public void TestClear()
|
||||
{
|
||||
// Verifies that the clear method works and that the data is still present for the Callback.
|
||||
bool called = false;
|
||||
clientSyncDictionary.Callback = (op, index, item) =>
|
||||
{
|
||||
called = true;
|
||||
|
||||
Assert.That(op, Is.EqualTo(SyncDictionary<int, string>.Operation.OP_CLEAR));
|
||||
Assert.That(clientSyncDictionary.Count, Is.EqualTo(3));
|
||||
};
|
||||
serverSyncDictionary.Clear();
|
||||
SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary);
|
||||
Assert.That(serverSyncDictionary, Is.EquivalentTo(new SyncDictionary<int, string>()));
|
||||
Assert.That(called, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
Loading…
Reference in New Issue
Block a user