2using System.Collections;
3using System.Collections.Generic;
9 public delegate
void SyncSetChanged(Operation op, T item);
11 protected readonly ISet<T> objects;
13 public int Count => objects.Count;
14 public bool IsReadOnly {
get;
private set; }
15 public event SyncSetChanged Callback;
17 public enum Operation :
byte
26 internal Operation operation;
35 readonly List<Change> changes =
new List<Change>();
45 this.objects = objects;
60 void AddOperation(Operation op, T item)
64 throw new InvalidOperationException(
"SyncSets can only be modified at the server");
67 Change change =
new Change
79 Callback?.Invoke(op, item);
82 void AddOperation(Operation op) => AddOperation(op,
default);
87 writer.WriteUInt((uint)objects.Count);
89 foreach (T obj
in objects)
98 writer.WriteUInt((uint)changes.Count);
104 writer.WriteUInt((uint)changes.Count);
106 for (
int i = 0; i < changes.Count; i++)
108 Change change = changes[i];
109 writer.WriteByte((
byte)change.operation);
111 switch (change.operation)
113 case Operation.OP_ADD:
114 writer.Write(change.item);
117 case Operation.OP_CLEAR:
120 case Operation.OP_REMOVE:
121 writer.Write(change.item);
133 int count = (int)reader.ReadUInt();
138 for (
int i = 0; i < count; i++)
140 T obj = reader.Read<T>();
147 changesAhead = (int)reader.ReadUInt();
155 int changesCount = (int)reader.ReadUInt();
157 for (
int i = 0; i < changesCount; i++)
159 Operation operation = (Operation)reader.ReadByte();
163 bool apply = changesAhead == 0;
168 case Operation.OP_ADD:
169 item = reader.Read<T>();
176 case Operation.OP_CLEAR:
183 case Operation.OP_REMOVE:
184 item = reader.Read<T>();
187 objects.Remove(item);
194 Callback?.Invoke(operation, item);
204 public bool Add(T item)
206 if (objects.Add(item))
208 AddOperation(Operation.OP_ADD, item);
214 void ICollection<T>.Add(T item)
216 if (objects.Add(item))
218 AddOperation(Operation.OP_ADD, item);
225 AddOperation(Operation.OP_CLEAR);
228 public bool Contains(T item) => objects.Contains(item);
230 public void CopyTo(T[] array,
int index) => objects.CopyTo(array, index);
232 public bool Remove(T item)
234 if (objects.Remove(item))
236 AddOperation(Operation.OP_REMOVE, item);
242 public IEnumerator<T> GetEnumerator() => objects.GetEnumerator();
244 IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
246 public void ExceptWith(IEnumerable<T> other)
255 foreach (T element
in other)
261 public void IntersectWith(IEnumerable<T> other)
263 if (other is ISet<T> otherSet)
265 IntersectWithSet(otherSet);
269 HashSet<T> otherAsSet =
new HashSet<T>(other);
270 IntersectWithSet(otherAsSet);
274 void IntersectWithSet(ISet<T> otherSet)
276 List<T> elements =
new List<T>(objects);
278 foreach (T element
in elements)
280 if (!otherSet.Contains(element))
287 public bool IsProperSubsetOf(IEnumerable<T> other) => objects.IsProperSubsetOf(other);
289 public bool IsProperSupersetOf(IEnumerable<T> other) => objects.IsProperSupersetOf(other);
291 public bool IsSubsetOf(IEnumerable<T> other) => objects.IsSubsetOf(other);
293 public bool IsSupersetOf(IEnumerable<T> other) => objects.IsSupersetOf(other);
295 public bool Overlaps(IEnumerable<T> other) => objects.Overlaps(other);
297 public bool SetEquals(IEnumerable<T> other) => objects.SetEquals(other);
300 public void SymmetricExceptWith(IEnumerable<T> other)
308 foreach (T element
in other)
310 if (!Remove(element))
319 public void UnionWith(IEnumerable<T> other)
323 foreach (T element
in other)
333 public SyncHashSet() :
this(EqualityComparer<T>.Default) {}
334 public SyncHashSet(IEqualityComparer<T> comparer) : base(
new HashSet<T>(comparer ?? EqualityComparer<T>.Default)) {}
337 public new HashSet<T>.Enumerator GetEnumerator() => ((HashSet<T>)objects).GetEnumerator();
343 public SyncSortedSet(IComparer<T> comparer) : base(
new SortedSet<T>(comparer ?? Comparer<T>.Default)) {}
346 public new SortedSet<T>.Enumerator GetEnumerator() => ((SortedSet<T>)objects).GetEnumerator();
Network Reader for most simple types like floats, ints, buffers, structs, etc. Use NetworkReaderPool....
Network Writer for most simple types like floats, ints, buffers, structs, etc. Use NetworkWriterPool....
SyncObjects sync state between server and client. E.g. SyncLists.
Action OnDirty
Used internally to set owner NetworkBehaviour's dirty mask bit when changed.
Func< bool > IsRecording
Used internally to check if we are currently tracking changes.
override void OnDeserializeDelta(NetworkReader reader)
Reads the changes made to the object since last sync
override void OnSerializeDelta(NetworkWriter writer)
Write the changes made to the object since last sync
override void OnSerializeAll(NetworkWriter writer)
Write a full copy of the object
override void Reset()
Resets the SyncObject so that it can be re-used
override void OnDeserializeAll(NetworkReader reader)
Reads a full copy of the object
override void ClearChanges()
Discard all the queued changes