chore(SyncSet): code formatting

This commit is contained in:
MrGadget 2024-03-24 10:07:08 -04:00
parent c9896c6f3e
commit f86acf6bed

View File

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