Mirror Networking
Mirror.SyncSet< T > Class Template Reference
Inheritance diagram for Mirror.SyncSet< T >:
Mirror.SyncObject Mirror.SyncHashSet< T > Mirror.SyncSortedSet< T >

Public Member Functions

delegate void SyncSetChanged (Operation op, T item)
 
 SyncSet (ISet< T > objects)
 
override void Reset ()
 Resets the SyncObject so that it can be re-used More...
 
override void ClearChanges ()
 Discard all the queued changes More...
 
override void OnSerializeAll (NetworkWriter writer)
 Write a full copy of the object More...
 
override void OnSerializeDelta (NetworkWriter writer)
 Write the changes made to the object since last sync More...
 
override void OnDeserializeAll (NetworkReader reader)
 Reads a full copy of the object More...
 
override void OnDeserializeDelta (NetworkReader reader)
 Reads the changes made to the object since last sync More...
 
bool Add (T item)
 
void Clear ()
 
bool Contains (T item)
 
void CopyTo (T[] array, int index)
 
bool Remove (T item)
 
IEnumerator< T > GetEnumerator ()
 
void ExceptWith (IEnumerable< T > other)
 
void IntersectWith (IEnumerable< T > other)
 
bool IsProperSubsetOf (IEnumerable< T > other)
 
bool IsProperSupersetOf (IEnumerable< T > other)
 
bool IsSubsetOf (IEnumerable< T > other)
 
bool IsSupersetOf (IEnumerable< T > other)
 
bool Overlaps (IEnumerable< T > other)
 
bool SetEquals (IEnumerable< T > other)
 
void SymmetricExceptWith (IEnumerable< T > other)
 
void UnionWith (IEnumerable< T > other)
 
abstract void ClearChanges ()
 Discard all the queued changes More...
 
abstract void OnSerializeAll (NetworkWriter writer)
 Write a full copy of the object More...
 
abstract void OnSerializeDelta (NetworkWriter writer)
 Write the changes made to the object since last sync More...
 
abstract void OnDeserializeAll (NetworkReader reader)
 Reads a full copy of the object More...
 
abstract void OnDeserializeDelta (NetworkReader reader)
 Reads the changes made to the object since last sync More...
 
abstract void Reset ()
 Resets the SyncObject so that it can be re-used More...
 

Protected Attributes

readonly ISet< T > objects
 

Properties

int Count [get]
 
bool IsReadOnly [get]
 

Events

SyncSetChanged Callback
 

Additional Inherited Members

- Public Attributes inherited from Mirror.SyncObject
Action OnDirty
 Used internally to set owner NetworkBehaviour's dirty mask bit when changed. More...
 
Func< bool > IsRecording = () => true
 Used internally to check if we are currently tracking changes. More...
 

Detailed Description

Definition at line 7 of file SyncSet.cs.

Constructor & Destructor Documentation

◆ SyncSet()

Mirror.SyncSet< T >.SyncSet ( ISet< T >  objects)

Definition at line 43 of file SyncSet.cs.

44 {
45 this.objects = objects;
46 }

Member Function Documentation

◆ Add()

bool Mirror.SyncSet< T >.Add ( item)

Definition at line 204 of file SyncSet.cs.

205 {
206 if (objects.Add(item))
207 {
208 AddOperation(Operation.OP_ADD, item);
209 return true;
210 }
211 return false;
212 }

◆ Clear()

void Mirror.SyncSet< T >.Clear ( )

Definition at line 222 of file SyncSet.cs.

223 {
224 objects.Clear();
225 AddOperation(Operation.OP_CLEAR);
226 }

◆ ClearChanges()

override void Mirror.SyncSet< T >.ClearChanges ( )
virtual

Discard all the queued changes

Implements Mirror.SyncObject.

◆ ExceptWith()

void Mirror.SyncSet< T >.ExceptWith ( IEnumerable< T >  other)

Definition at line 246 of file SyncSet.cs.

247 {
248 if (other == this)
249 {
250 Clear();
251 return;
252 }
253
254 // remove every element in other from this
255 foreach (T element in other)
256 {
257 Remove(element);
258 }
259 }

◆ IntersectWith()

void Mirror.SyncSet< T >.IntersectWith ( IEnumerable< T >  other)

Definition at line 261 of file SyncSet.cs.

262 {
263 if (other is ISet<T> otherSet)
264 {
265 IntersectWithSet(otherSet);
266 }
267 else
268 {
269 HashSet<T> otherAsSet = new HashSet<T>(other);
270 IntersectWithSet(otherAsSet);
271 }
272 }

◆ OnDeserializeAll()

override void Mirror.SyncSet< T >.OnDeserializeAll ( NetworkReader  reader)
virtual

Reads a full copy of the object

Implements Mirror.SyncObject.

Definition at line 127 of file SyncSet.cs.

128 {
129 // This list can now only be modified by synchronization
130 IsReadOnly = true;
131
132 // if init, write the full list content
133 int count = (int)reader.ReadUInt();
134
135 objects.Clear();
136 changes.Clear();
137
138 for (int i = 0; i < count; i++)
139 {
140 T obj = reader.Read<T>();
141 objects.Add(obj);
142 }
143
144 // We will need to skip all these changes
145 // the next time the list is synchronized
146 // because they have already been applied
147 changesAhead = (int)reader.ReadUInt();
148 }

◆ OnDeserializeDelta()

override void Mirror.SyncSet< T >.OnDeserializeDelta ( NetworkReader  reader)
virtual

Reads the changes made to the object since last sync

Implements Mirror.SyncObject.

Definition at line 150 of file SyncSet.cs.

151 {
152 // This list can now only be modified by synchronization
153 IsReadOnly = true;
154
155 int changesCount = (int)reader.ReadUInt();
156
157 for (int i = 0; i < changesCount; i++)
158 {
159 Operation operation = (Operation)reader.ReadByte();
160
161 // apply the operation only if it is a new change
162 // that we have not applied yet
163 bool apply = changesAhead == 0;
164 T item = default;
165
166 switch (operation)
167 {
168 case Operation.OP_ADD:
169 item = reader.Read<T>();
170 if (apply)
171 {
172 objects.Add(item);
173 }
174 break;
175
176 case Operation.OP_CLEAR:
177 if (apply)
178 {
179 objects.Clear();
180 }
181 break;
182
183 case Operation.OP_REMOVE:
184 item = reader.Read<T>();
185 if (apply)
186 {
187 objects.Remove(item);
188 }
189 break;
190 }
191
192 if (apply)
193 {
194 Callback?.Invoke(operation, item);
195 }
196 // we just skipped this change
197 else
198 {
199 changesAhead--;
200 }
201 }
202 }

◆ OnSerializeAll()

override void Mirror.SyncSet< T >.OnSerializeAll ( NetworkWriter  writer)
virtual

Write a full copy of the object

Implements Mirror.SyncObject.

Definition at line 84 of file SyncSet.cs.

85 {
86 // if init, write the full list content
87 writer.WriteUInt((uint)objects.Count);
88
89 foreach (T obj in objects)
90 {
91 writer.Write(obj);
92 }
93
94 // all changes have been applied already
95 // thus the client will need to skip all the pending changes
96 // or they would be applied again.
97 // So we write how many changes are pending
98 writer.WriteUInt((uint)changes.Count);
99 }

◆ OnSerializeDelta()

override void Mirror.SyncSet< T >.OnSerializeDelta ( NetworkWriter  writer)
virtual

Write the changes made to the object since last sync

Implements Mirror.SyncObject.

Definition at line 101 of file SyncSet.cs.

102 {
103 // write all the queued up changes
104 writer.WriteUInt((uint)changes.Count);
105
106 for (int i = 0; i < changes.Count; i++)
107 {
108 Change change = changes[i];
109 writer.WriteByte((byte)change.operation);
110
111 switch (change.operation)
112 {
113 case Operation.OP_ADD:
114 writer.Write(change.item);
115 break;
116
117 case Operation.OP_CLEAR:
118 break;
119
120 case Operation.OP_REMOVE:
121 writer.Write(change.item);
122 break;
123 }
124 }
125 }

◆ Remove()

bool Mirror.SyncSet< T >.Remove ( item)

Definition at line 232 of file SyncSet.cs.

233 {
234 if (objects.Remove(item))
235 {
236 AddOperation(Operation.OP_REMOVE, item);
237 return true;
238 }
239 return false;
240 }

◆ Reset()

override void Mirror.SyncSet< T >.Reset ( )
virtual

Resets the SyncObject so that it can be re-used

Implements Mirror.SyncObject.

Definition at line 48 of file SyncSet.cs.

49 {
50 IsReadOnly = false;
51 changes.Clear();
52 changesAhead = 0;
53 objects.Clear();
54 }

◆ SymmetricExceptWith()

void Mirror.SyncSet< T >.SymmetricExceptWith ( IEnumerable< T >  other)

Definition at line 300 of file SyncSet.cs.

301 {
302 if (other == this)
303 {
304 Clear();
305 }
306 else
307 {
308 foreach (T element in other)
309 {
310 if (!Remove(element))
311 {
312 Add(element);
313 }
314 }
315 }
316 }

◆ UnionWith()

void Mirror.SyncSet< T >.UnionWith ( IEnumerable< T >  other)

Definition at line 319 of file SyncSet.cs.

320 {
321 if (other != this)
322 {
323 foreach (T element in other)
324 {
325 Add(element);
326 }
327 }
328 }

Member Data Documentation

◆ objects

readonly ISet<T> Mirror.SyncSet< T >.objects
protected

Definition at line 11 of file SyncSet.cs.

Property Documentation

◆ Count

int Mirror.SyncSet< T >.Count
get

Definition at line 13 of file SyncSet.cs.

◆ IsReadOnly

bool Mirror.SyncSet< T >.IsReadOnly
get

Definition at line 14 of file SyncSet.cs.

14{ get; private set; }

Event Documentation

◆ Callback

SyncSetChanged Mirror.SyncSet< T >.Callback

Definition at line 15 of file SyncSet.cs.