diff --git a/Assets/Mirror/Core/SyncSet.cs b/Assets/Mirror/Core/SyncSet.cs index 13d4302ae..b6cccc449 100644 --- a/Assets/Mirror/Core/SyncSet.cs +++ b/Assets/Mirror/Core/SyncSet.cs @@ -7,18 +7,18 @@ namespace Mirror public class SyncSet : SyncObject, ISet { public delegate void SyncSetChanged(Operation op, T item); + public event SyncSetChanged Callback; protected readonly ISet objects; public int Count => objects.Count; public bool IsReadOnly => !IsWritable(); - public event SyncSetChanged Callback; public enum Operation : byte { OP_ADD, - OP_CLEAR, - OP_REMOVE + OP_REMOVE, + OP_CLEAR } struct Change @@ -59,9 +59,7 @@ public override void Reset() void AddOperation(Operation op, T item, bool checkAccess) { if (checkAccess && IsReadOnly) - { throw new InvalidOperationException("SyncSets can only be modified by the owner."); - } Change change = new Change { @@ -86,9 +84,7 @@ public override void OnSerializeAll(NetworkWriter writer) writer.WriteUInt((uint)objects.Count); foreach (T obj in objects) - { writer.Write(obj); - } // all changes have been applied already // thus the client will need to skip all the pending changes @@ -112,13 +108,11 @@ public override void OnSerializeDelta(NetworkWriter writer) case Operation.OP_ADD: writer.Write(change.item); break; - - case Operation.OP_CLEAR: - break; - case Operation.OP_REMOVE: writer.Write(change.item); break; + case Operation.OP_CLEAR: + break; } } } @@ -171,18 +165,6 @@ public override void OnDeserializeDelta(NetworkReader reader) } break; - 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, false); - } - break; - case Operation.OP_REMOVE: item = reader.Read(); if (apply) @@ -195,6 +177,18 @@ public override void OnDeserializeDelta(NetworkReader reader) AddOperation(Operation.OP_REMOVE, item, false); } break; + + 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, false); + } + break; } if (!apply) @@ -218,9 +212,7 @@ public bool Add(T item) void ICollection.Add(T item) { if (objects.Add(item)) - { AddOperation(Operation.OP_ADD, item, true); - } } public void Clear() @@ -257,17 +249,13 @@ public void ExceptWith(IEnumerable other) // remove every element in other from this foreach (T element in other) - { Remove(element); - } } public void IntersectWith(IEnumerable other) { if (other is ISet otherSet) - { IntersectWithSet(otherSet); - } else { HashSet otherAsSet = new HashSet(other); @@ -280,12 +268,8 @@ void IntersectWithSet(ISet otherSet) List elements = new List(objects); foreach (T element in elements) - { if (!otherSet.Contains(element)) - { Remove(element); - } - } } public bool IsProperSubsetOf(IEnumerable other) => objects.IsProperSubsetOf(other); @@ -304,38 +288,26 @@ void IntersectWithSet(ISet otherSet) public void SymmetricExceptWith(IEnumerable other) { if (other == this) - { Clear(); - } else - { foreach (T element in other) - { if (!Remove(element)) - { Add(element); - } - } - } } // custom implementation so we can do our own Clear/Add/Remove for delta public void UnionWith(IEnumerable other) { if (other != this) - { foreach (T element in other) - { Add(element); - } - } } } public class SyncHashSet : SyncSet { - public SyncHashSet() : this(EqualityComparer.Default) {} - public SyncHashSet(IEqualityComparer comparer) : base(new HashSet(comparer ?? EqualityComparer.Default)) {} + public SyncHashSet() : this(EqualityComparer.Default) { } + public SyncHashSet(IEqualityComparer comparer) : base(new HashSet(comparer ?? EqualityComparer.Default)) { } // allocation free enumerator public new HashSet.Enumerator GetEnumerator() => ((HashSet)objects).GetEnumerator(); @@ -343,8 +315,8 @@ public SyncHashSet(IEqualityComparer comparer) : base(new HashSet(comparer public class SyncSortedSet : SyncSet { - public SyncSortedSet() : this(Comparer.Default) {} - public SyncSortedSet(IComparer comparer) : base(new SortedSet(comparer ?? Comparer.Default)) {} + public SyncSortedSet() : this(Comparer.Default) { } + public SyncSortedSet(IComparer comparer) : base(new SortedSet(comparer ?? Comparer.Default)) { } // allocation free enumerator public new SortedSet.Enumerator GetEnumerator() => ((SortedSet)objects).GetEnumerator();