Mirror Networking
Mirror.SyncList< T > Class Template Reference
Inheritance diagram for Mirror.SyncList< T >:
Mirror.SyncObject

Classes

struct  Enumerator
 

Public Member Functions

delegate void SyncListChanged (Operation op, int itemIndex, T oldItem, T newItem)
 
 SyncList (IEqualityComparer< T > comparer)
 
 SyncList (IList< T > objects, IEqualityComparer< T > comparer=null)
 
override void ClearChanges ()
 Discard all the queued changes More...
 
override void Reset ()
 Resets the SyncObject so that it can be re-used 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...
 
void Add (T item)
 
void AddRange (IEnumerable< T > range)
 
void Clear ()
 
bool Contains (T item)
 
void CopyTo (T[] array, int index)
 
int IndexOf (T item)
 
int FindIndex (Predicate< T > match)
 
Find (Predicate< T > match)
 
List< T > FindAll (Predicate< T > match)
 
void Insert (int index, T item)
 
void InsertRange (int index, IEnumerable< T > range)
 
bool Remove (T item)
 
void RemoveAt (int index)
 
int RemoveAll (Predicate< T > match)
 
Enumerator GetEnumerator ()
 
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...
 

Properties

int Count [get]
 
bool IsReadOnly [get]
 
this[int i] [get, set]
 

Events

SyncListChanged 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 SyncList.cs.

Constructor & Destructor Documentation

◆ SyncList() [1/3]

Definition at line 46 of file SyncList.cs.

46: this(EqualityComparer<T>.Default) {}

◆ SyncList() [2/3]

Mirror.SyncList< T >.SyncList ( IEqualityComparer< T >  comparer)

Definition at line 48 of file SyncList.cs.

49 {
50 this.comparer = comparer ?? EqualityComparer<T>.Default;
51 objects = new List<T>();
52 }

◆ SyncList() [3/3]

Mirror.SyncList< T >.SyncList ( IList< T >  objects,
IEqualityComparer< T >  comparer = null 
)

Definition at line 54 of file SyncList.cs.

55 {
56 this.comparer = comparer ?? EqualityComparer<T>.Default;
57 this.objects = objects;
58 }

Member Function Documentation

◆ Add()

void Mirror.SyncList< T >.Add ( item)

Definition at line 245 of file SyncList.cs.

246 {
247 objects.Add(item);
248 AddOperation(Operation.OP_ADD, objects.Count - 1, default, item);
249 }

◆ AddRange()

void Mirror.SyncList< T >.AddRange ( IEnumerable< T >  range)

Definition at line 251 of file SyncList.cs.

252 {
253 foreach (T entry in range)
254 {
255 Add(entry);
256 }
257 }

◆ Clear()

void Mirror.SyncList< T >.Clear ( )

Definition at line 259 of file SyncList.cs.

260 {
261 objects.Clear();
262 AddOperation(Operation.OP_CLEAR, 0, default, default);
263 }

◆ ClearChanges()

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

Discard all the queued changes

Implements Mirror.SyncObject.

◆ Find()

T Mirror.SyncList< T >.Find ( Predicate< T >  match)

Definition at line 285 of file SyncList.cs.

286 {
287 int i = FindIndex(match);
288 return (i != -1) ? objects[i] : default;
289 }

◆ FindAll()

List< T > Mirror.SyncList< T >.FindAll ( Predicate< T >  match)

Definition at line 291 of file SyncList.cs.

292 {
293 List<T> results = new List<T>();
294 for (int i = 0; i < objects.Count; ++i)
295 if (match(objects[i]))
296 results.Add(objects[i]);
297 return results;
298 }

◆ FindIndex()

int Mirror.SyncList< T >.FindIndex ( Predicate< T >  match)

Definition at line 277 of file SyncList.cs.

278 {
279 for (int i = 0; i < objects.Count; ++i)
280 if (match(objects[i]))
281 return i;
282 return -1;
283 }

◆ IndexOf()

int Mirror.SyncList< T >.IndexOf ( item)

Definition at line 269 of file SyncList.cs.

270 {
271 for (int i = 0; i < objects.Count; ++i)
272 if (comparer.Equals(item, objects[i]))
273 return i;
274 return -1;
275 }

◆ Insert()

void Mirror.SyncList< T >.Insert ( int  index,
item 
)

Definition at line 300 of file SyncList.cs.

301 {
302 objects.Insert(index, item);
303 AddOperation(Operation.OP_INSERT, index, default, item);
304 }

◆ InsertRange()

void Mirror.SyncList< T >.InsertRange ( int  index,
IEnumerable< T >  range 
)

Definition at line 306 of file SyncList.cs.

307 {
308 foreach (T entry in range)
309 {
310 Insert(index, entry);
311 index++;
312 }
313 }

◆ OnDeserializeAll()

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

Reads a full copy of the object

Implements Mirror.SyncObject.

Definition at line 145 of file SyncList.cs.

146 {
147 // This list can now only be modified by synchronization
148 IsReadOnly = true;
149
150 // if init, write the full list content
151 int count = (int)reader.ReadUInt();
152
153 objects.Clear();
154 changes.Clear();
155
156 for (int i = 0; i < count; i++)
157 {
158 T obj = reader.Read<T>();
159 objects.Add(obj);
160 }
161
162 // We will need to skip all these changes
163 // the next time the list is synchronized
164 // because they have already been applied
165 changesAhead = (int)reader.ReadUInt();
166 }

◆ OnDeserializeDelta()

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

Reads the changes made to the object since last sync

Implements Mirror.SyncObject.

Definition at line 168 of file SyncList.cs.

169 {
170 // This list can now only be modified by synchronization
171 IsReadOnly = true;
172
173 int changesCount = (int)reader.ReadUInt();
174
175 for (int i = 0; i < changesCount; i++)
176 {
177 Operation operation = (Operation)reader.ReadByte();
178
179 // apply the operation only if it is a new change
180 // that we have not applied yet
181 bool apply = changesAhead == 0;
182 int index = 0;
183 T oldItem = default;
184 T newItem = default;
185
186 switch (operation)
187 {
188 case Operation.OP_ADD:
189 newItem = reader.Read<T>();
190 if (apply)
191 {
192 index = objects.Count;
193 objects.Add(newItem);
194 }
195 break;
196
197 case Operation.OP_CLEAR:
198 if (apply)
199 {
200 objects.Clear();
201 }
202 break;
203
204 case Operation.OP_INSERT:
205 index = (int)reader.ReadUInt();
206 newItem = reader.Read<T>();
207 if (apply)
208 {
209 objects.Insert(index, newItem);
210 }
211 break;
212
213 case Operation.OP_REMOVEAT:
214 index = (int)reader.ReadUInt();
215 if (apply)
216 {
217 oldItem = objects[index];
218 objects.RemoveAt(index);
219 }
220 break;
221
222 case Operation.OP_SET:
223 index = (int)reader.ReadUInt();
224 newItem = reader.Read<T>();
225 if (apply)
226 {
227 oldItem = objects[index];
228 objects[index] = newItem;
229 }
230 break;
231 }
232
233 if (apply)
234 {
235 Callback?.Invoke(operation, index, oldItem, newItem);
236 }
237 // we just skipped this change
238 else
239 {
240 changesAhead--;
241 }
242 }
243 }

◆ OnSerializeAll()

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

Write a full copy of the object

Implements Mirror.SyncObject.

Definition at line 95 of file SyncList.cs.

96 {
97 // if init, write the full list content
98 writer.WriteUInt((uint)objects.Count);
99
100 for (int i = 0; i < objects.Count; i++)
101 {
102 T obj = objects[i];
103 writer.Write(obj);
104 }
105
106 // all changes have been applied already
107 // thus the client will need to skip all the pending changes
108 // or they would be applied again.
109 // So we write how many changes are pending
110 writer.WriteUInt((uint)changes.Count);
111 }

◆ OnSerializeDelta()

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

Write the changes made to the object since last sync

Implements Mirror.SyncObject.

Definition at line 113 of file SyncList.cs.

114 {
115 // write all the queued up changes
116 writer.WriteUInt((uint)changes.Count);
117
118 for (int i = 0; i < changes.Count; i++)
119 {
120 Change change = changes[i];
121 writer.WriteByte((byte)change.operation);
122
123 switch (change.operation)
124 {
125 case Operation.OP_ADD:
126 writer.Write(change.item);
127 break;
128
129 case Operation.OP_CLEAR:
130 break;
131
132 case Operation.OP_REMOVEAT:
133 writer.WriteUInt((uint)change.index);
134 break;
135
136 case Operation.OP_INSERT:
137 case Operation.OP_SET:
138 writer.WriteUInt((uint)change.index);
139 writer.Write(change.item);
140 break;
141 }
142 }
143 }

◆ Remove()

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

Definition at line 315 of file SyncList.cs.

316 {
317 int index = IndexOf(item);
318 bool result = index >= 0;
319 if (result)
320 {
321 RemoveAt(index);
322 }
323 return result;
324 }

◆ RemoveAll()

int Mirror.SyncList< T >.RemoveAll ( Predicate< T >  match)

Definition at line 333 of file SyncList.cs.

334 {
335 List<T> toRemove = new List<T>();
336 for (int i = 0; i < objects.Count; ++i)
337 if (match(objects[i]))
338 toRemove.Add(objects[i]);
339
340 foreach (T entry in toRemove)
341 {
342 Remove(entry);
343 }
344
345 return toRemove.Count;
346 }

◆ RemoveAt()

void Mirror.SyncList< T >.RemoveAt ( int  index)

Definition at line 326 of file SyncList.cs.

327 {
328 T oldItem = objects[index];
329 objects.RemoveAt(index);
330 AddOperation(Operation.OP_REMOVEAT, index, oldItem, default);
331 }

◆ Reset()

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

Resets the SyncObject so that it can be re-used

Implements Mirror.SyncObject.

Definition at line 64 of file SyncList.cs.

65 {
66 IsReadOnly = false;
67 changes.Clear();
68 changesAhead = 0;
69 objects.Clear();
70 }

Property Documentation

◆ Count

int Mirror.SyncList< T >.Count
get

Definition at line 14 of file SyncList.cs.

◆ IsReadOnly

bool Mirror.SyncList< T >.IsReadOnly
get

Definition at line 15 of file SyncList.cs.

15{ get; private set; }

◆ this[int i]

T Mirror.SyncList< T >.this[int i]
getset

Definition at line 348 of file SyncList.cs.

349 {
350 get => objects[i];
351 set
352 {
353 if (!comparer.Equals(objects[i], value))
354 {
355 T oldItem = objects[i];
356 objects[i] = value;
357 AddOperation(Operation.OP_SET, i, oldItem, value);
358 }
359 }
360 }

Event Documentation

◆ Callback

SyncListChanged Mirror.SyncList< T >.Callback

Definition at line 16 of file SyncList.cs.