Mirror Networking
Mirror.SyncIDictionary< TKey, TValue > Class Template Reference
Inheritance diagram for Mirror.SyncIDictionary< TKey, TValue >:
Mirror.SyncObject Mirror.SyncDictionary< TKey, TValue >

Public Member Functions

delegate void SyncDictionaryChanged (Operation op, TKey key, TValue item)
 
override void Reset ()
 Resets the SyncObject so that it can be re-used More...
 
override void ClearChanges ()
 Discard all the queued changes More...
 
 SyncIDictionary (IDictionary< TKey, TValue > objects)
 
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 Clear ()
 
bool ContainsKey (TKey key)
 
bool Remove (TKey key)
 
bool TryGetValue (TKey key, out TValue value)
 
void Add (TKey key, TValue value)
 
void Add (KeyValuePair< TKey, TValue > item)
 
bool Contains (KeyValuePair< TKey, TValue > item)
 
void CopyTo (KeyValuePair< TKey, TValue >[] array, int arrayIndex)
 
bool Remove (KeyValuePair< TKey, TValue > item)
 
IEnumerator< KeyValuePair< TKey, TValue > > 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...
 

Protected Attributes

readonly IDictionary< TKey, TValue > objects
 

Properties

int Count [get]
 
bool IsReadOnly [get]
 
ICollection< TKey > Keys [get]
 
ICollection< TValue > Values [get]
 
TValue this[TKey i] [get, set]
 

Events

SyncDictionaryChanged 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 6 of file SyncDictionary.cs.

Constructor & Destructor Documentation

◆ SyncIDictionary()

Mirror.SyncIDictionary< TKey, TValue >.SyncIDictionary ( IDictionary< TKey, TValue >  objects)

Definition at line 64 of file SyncDictionary.cs.

65 {
66 this.objects = objects;
67 }

Member Function Documentation

◆ Add()

void Mirror.SyncIDictionary< TKey, TValue >.Add ( TKey  key,
TValue  value 
)

Definition at line 254 of file SyncDictionary.cs.

255 {
256 objects.Add(key, value);
257 AddOperation(Operation.OP_ADD, key, value);
258 }

◆ Clear()

void Mirror.SyncIDictionary< TKey, TValue >.Clear ( )

Definition at line 216 of file SyncDictionary.cs.

217 {
218 objects.Clear();
219 AddOperation(Operation.OP_CLEAR, default, default);
220 }

◆ ClearChanges()

override void Mirror.SyncIDictionary< TKey, TValue >.ClearChanges ( )
virtual

Discard all the queued changes

Implements Mirror.SyncObject.

◆ Contains()

bool Mirror.SyncIDictionary< TKey, TValue >.Contains ( KeyValuePair< TKey, TValue >  item)

Definition at line 262 of file SyncDictionary.cs.

263 {
264 return TryGetValue(item.Key, out TValue val) && EqualityComparer<TValue>.Default.Equals(val, item.Value);
265 }

◆ CopyTo()

void Mirror.SyncIDictionary< TKey, TValue >.CopyTo ( KeyValuePair< TKey, TValue >[]  array,
int  arrayIndex 
)

Definition at line 267 of file SyncDictionary.cs.

268 {
269 if (arrayIndex < 0 || arrayIndex > array.Length)
270 {
271 throw new System.ArgumentOutOfRangeException(nameof(arrayIndex), "Array Index Out of Range");
272 }
273 if (array.Length - arrayIndex < Count)
274 {
275 throw new System.ArgumentException("The number of items in the SyncDictionary is greater than the available space from arrayIndex to the end of the destination array");
276 }
277
278 int i = arrayIndex;
279 foreach (KeyValuePair<TKey, TValue> item in objects)
280 {
281 array[i] = item;
282 i++;
283 }
284 }

◆ OnDeserializeAll()

override void Mirror.SyncIDictionary< TKey, TValue >.OnDeserializeAll ( NetworkReader  reader)
virtual

Reads a full copy of the object

Implements Mirror.SyncObject.

Definition at line 134 of file SyncDictionary.cs.

135 {
136 // This list can now only be modified by synchronization
137 IsReadOnly = true;
138
139 // if init, write the full list content
140 int count = (int)reader.ReadUInt();
141
142 objects.Clear();
143 changes.Clear();
144
145 for (int i = 0; i < count; i++)
146 {
147 TKey key = reader.Read<TKey>();
148 TValue obj = reader.Read<TValue>();
149 objects.Add(key, obj);
150 }
151
152 // We will need to skip all these changes
153 // the next time the list is synchronized
154 // because they have already been applied
155 changesAhead = (int)reader.ReadUInt();
156 }

◆ OnDeserializeDelta()

override void Mirror.SyncIDictionary< TKey, TValue >.OnDeserializeDelta ( NetworkReader  reader)
virtual

Reads the changes made to the object since last sync

Implements Mirror.SyncObject.

Definition at line 158 of file SyncDictionary.cs.

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

◆ OnSerializeAll()

override void Mirror.SyncIDictionary< TKey, TValue >.OnSerializeAll ( NetworkWriter  writer)
virtual

Write a full copy of the object

Implements Mirror.SyncObject.

Definition at line 92 of file SyncDictionary.cs.

93 {
94 // if init, write the full list content
95 writer.WriteUInt((uint)objects.Count);
96
97 foreach (KeyValuePair<TKey, TValue> syncItem in objects)
98 {
99 writer.Write(syncItem.Key);
100 writer.Write(syncItem.Value);
101 }
102
103 // all changes have been applied already
104 // thus the client will need to skip all the pending changes
105 // or they would be applied again.
106 // So we write how many changes are pending
107 writer.WriteUInt((uint)changes.Count);
108 }

◆ OnSerializeDelta()

override void Mirror.SyncIDictionary< TKey, TValue >.OnSerializeDelta ( NetworkWriter  writer)
virtual

Write the changes made to the object since last sync

Implements Mirror.SyncObject.

Definition at line 110 of file SyncDictionary.cs.

111 {
112 // write all the queued up changes
113 writer.WriteUInt((uint)changes.Count);
114
115 for (int i = 0; i < changes.Count; i++)
116 {
117 Change change = changes[i];
118 writer.WriteByte((byte)change.operation);
119
120 switch (change.operation)
121 {
122 case Operation.OP_ADD:
123 case Operation.OP_REMOVE:
124 case Operation.OP_SET:
125 writer.Write(change.key);
126 writer.Write(change.item);
127 break;
128 case Operation.OP_CLEAR:
129 break;
130 }
131 }
132 }

◆ Remove() [1/2]

bool Mirror.SyncIDictionary< TKey, TValue >.Remove ( KeyValuePair< TKey, TValue >  item)

Definition at line 286 of file SyncDictionary.cs.

287 {
288 bool result = objects.Remove(item.Key);
289 if (result)
290 {
291 AddOperation(Operation.OP_REMOVE, item.Key, item.Value);
292 }
293 return result;
294 }

◆ Remove() [2/2]

bool Mirror.SyncIDictionary< TKey, TValue >.Remove ( TKey  key)

Definition at line 224 of file SyncDictionary.cs.

225 {
226 if (objects.TryGetValue(key, out TValue item) && objects.Remove(key))
227 {
228 AddOperation(Operation.OP_REMOVE, key, item);
229 return true;
230 }
231 return false;
232 }

◆ Reset()

override void Mirror.SyncIDictionary< TKey, TValue >.Reset ( )
virtual

Resets the SyncObject so that it can be re-used

Implements Mirror.SyncObject.

Definition at line 44 of file SyncDictionary.cs.

45 {
46 IsReadOnly = false;
47 changes.Clear();
48 changesAhead = 0;
49 objects.Clear();
50 }

Member Data Documentation

◆ objects

readonly IDictionary<TKey, TValue> Mirror.SyncIDictionary< TKey, TValue >.objects
protected

Definition at line 10 of file SyncDictionary.cs.

Property Documentation

◆ Count

int Mirror.SyncIDictionary< TKey, TValue >.Count
get

Definition at line 12 of file SyncDictionary.cs.

◆ IsReadOnly

bool Mirror.SyncIDictionary< TKey, TValue >.IsReadOnly
get

Definition at line 13 of file SyncDictionary.cs.

13{ get; private set; }

◆ Keys

ICollection<TKey> Mirror.SyncIDictionary< TKey, TValue >.Keys
get

Definition at line 52 of file SyncDictionary.cs.

◆ this[TKey i]

TValue Mirror.SyncIDictionary< TKey, TValue >.this[TKey i]
getset

Definition at line 234 of file SyncDictionary.cs.

235 {
236 get => objects[i];
237 set
238 {
239 if (ContainsKey(i))
240 {
241 objects[i] = value;
242 AddOperation(Operation.OP_SET, i, value);
243 }
244 else
245 {
246 objects[i] = value;
247 AddOperation(Operation.OP_ADD, i, value);
248 }
249 }
250 }

◆ Values

ICollection<TValue> Mirror.SyncIDictionary< TKey, TValue >.Values
get

Definition at line 54 of file SyncDictionary.cs.

Event Documentation

◆ Callback

SyncDictionaryChanged Mirror.SyncIDictionary< TKey, TValue >.Callback

Definition at line 14 of file SyncDictionary.cs.