Skip to content

Commit 7efedce

Browse files
committed
Add event for when a NetworkedDictionary is changed.
1 parent 126562a commit 7efedce

File tree

2 files changed

+125
-20
lines changed

2 files changed

+125
-20
lines changed

MLAPI/Data/NetworkedCollections/NetworkedDictionary.cs

Lines changed: 124 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using System.IO;
44
using MLAPI.Serialization;
@@ -24,6 +24,17 @@ public class NetworkedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INet
2424
private NetworkedBehaviour networkedBehaviour;
2525
private readonly List<NetworkedDictionaryEvent<TKey, TValue>> dirtyEvents = new List<NetworkedDictionaryEvent<TKey, TValue>>();
2626

27+
/// <summary>
28+
/// Delegate type for dictionary changed event
29+
/// </summary>
30+
/// <param name="changeEvent">Struct containing information about the change event</param>
31+
public delegate void OnDictionaryChangedDelegate(NetworkedDictionaryEvent<TKey, TValue> changeEvent);
32+
/// <summary>
33+
/// The callback to be invoked when the dictionary gets changed
34+
/// </summary>
35+
public event OnDictionaryChangedDelegate OnDictionaryChanged;
36+
37+
2738
/// <summary>
2839
/// Creates a NetworkedDictionary with the default value and settings
2940
/// </summary>
@@ -90,25 +101,53 @@ public void ReadDelta(Stream stream)
90101
TKey key = (TKey)reader.ReadObjectPacked(typeof(TKey));
91102
TValue value = (TValue)reader.ReadObjectPacked(typeof(TValue));
92103
dictionary.Add(key, value);
104+
105+
if (OnDictionaryChanged != null)
106+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
107+
eventType = eventType,
108+
key = key,
109+
value = value
110+
});
93111
}
94112
break;
95113
case NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Remove:
96114
{
97115
TKey key = (TKey)reader.ReadObjectPacked(typeof(TKey));
116+
TValue value;
117+
dictionary.TryGetValue(key, out value);
98118
dictionary.Remove(key);
119+
120+
if (OnDictionaryChanged != null)
121+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
122+
eventType = eventType,
123+
key = key,
124+
value = value
125+
});
99126
}
100127
break;
101128
case NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.RemovePair:
102129
{
103130
TKey key = (TKey)reader.ReadObjectPacked(typeof(TKey));
104131
TValue value = (TValue)reader.ReadObjectPacked(typeof(TValue));
105132
dictionary.Remove(new KeyValuePair<TKey, TValue>(key, value));
133+
134+
if (OnDictionaryChanged != null)
135+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
136+
eventType = eventType,
137+
key = key,
138+
value = value
139+
});
106140
}
107141
break;
108142
case NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear:
109143
{
110144
//read nothing
111145
dictionary.Clear();
146+
147+
if (OnDictionaryChanged != null)
148+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
149+
eventType = eventType
150+
});
112151
}
113152
break;
114153
case NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Value:
@@ -117,6 +156,13 @@ public void ReadDelta(Stream stream)
117156
TValue value = (TValue)reader.ReadObjectPacked(typeof(TValue));
118157
if (dictionary.ContainsKey(key))
119158
dictionary[key] = value;
159+
160+
if (OnDictionaryChanged != null)
161+
OnDictionaryChanged(new NetworkedDictionaryEvent<TKey, TValue> {
162+
eventType = eventType,
163+
key = key,
164+
value = value
165+
});
120166
}
121167
break;
122168
default:
@@ -276,12 +322,16 @@ public TValue this[TKey key]
276322
set
277323
{
278324
dictionary[key] = value;
279-
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
325+
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
280326
{
281327
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Value,
282328
key = key,
283329
value = value
284-
});
330+
};
331+
dirtyEvents.Add(dictionaryEvent);
332+
333+
if (OnDictionaryChanged != null)
334+
OnDictionaryChanged(dictionaryEvent);
285335
}
286336
}
287337

@@ -301,34 +351,46 @@ public TValue this[TKey key]
301351
public void Add(TKey key, TValue value)
302352
{
303353
dictionary.Add(key, value);
304-
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
354+
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
305355
{
306356
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add,
307357
key = key,
308358
value = value
309-
});
359+
};
360+
dirtyEvents.Add(dictionaryEvent);
361+
362+
if (OnDictionaryChanged != null)
363+
OnDictionaryChanged(dictionaryEvent);
310364
}
311365

312366
/// <inheritdoc />
313367
public void Add(KeyValuePair<TKey, TValue> item)
314368
{
315369
dictionary.Add(item);
316-
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
370+
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
317371
{
318372
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add,
319373
key = item.Key,
320374
value = item.Value
321-
});
375+
};
376+
dirtyEvents.Add(dictionaryEvent);
377+
378+
if (OnDictionaryChanged != null)
379+
OnDictionaryChanged(dictionaryEvent);
322380
}
323381

324382
/// <inheritdoc />
325383
public void Clear()
326384
{
327385
dictionary.Clear();
328-
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
386+
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
329387
{
330388
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear
331-
});
389+
};
390+
dirtyEvents.Add(dictionaryEvent);
391+
392+
if (OnDictionaryChanged != null)
393+
OnDictionaryChanged(dictionaryEvent);
332394
}
333395

334396
/// <inheritdoc />
@@ -358,14 +420,21 @@ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
358420
/// <inheritdoc />
359421
public bool Remove(TKey key)
360422
{
423+
TValue value;
424+
dictionary.TryGetValue(key, out value);
361425
bool state = dictionary.Remove(key);
362426
if (state)
363427
{
364-
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
428+
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
365429
{
366430
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Remove,
367-
key = key
368-
});
431+
key = key,
432+
value = value
433+
};
434+
dirtyEvents.Add(dictionaryEvent);
435+
436+
if (OnDictionaryChanged != null)
437+
OnDictionaryChanged(dictionaryEvent);
369438
}
370439
return state;
371440
}
@@ -376,12 +445,16 @@ public bool Remove(KeyValuePair<TKey, TValue> item)
376445
bool state = dictionary.Remove(item);
377446
if (state)
378447
{
379-
dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>()
448+
NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent = new NetworkedDictionaryEvent<TKey, TValue>()
380449
{
381450
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.RemovePair,
382451
key = item.Key,
383452
value = item.Value
384-
});
453+
};
454+
dirtyEvents.Add(dictionaryEvent);
455+
456+
if (OnDictionaryChanged != null)
457+
OnDictionaryChanged(dictionaryEvent);
385458
}
386459
return state;
387460
}
@@ -392,19 +465,51 @@ IEnumerator IEnumerable.GetEnumerator()
392465
}
393466
}
394467

395-
internal struct NetworkedDictionaryEvent<TKey, TValue>
468+
/// <summary>
469+
/// Struct containing event information about changes to a NetworkedDictionary.
470+
/// </summary>
471+
/// <typeparam name="TKey">The type for the dictionary key that the event is about</typeparam>
472+
/// <typeparam name="TValue">The type for the dictionary value that the event is about</typeparam>
473+
public struct NetworkedDictionaryEvent<TKey, TValue>
396474
{
397-
internal enum NetworkedListEventType
475+
/// <summary>
476+
/// Enum representing the different operations available for triggering an event.
477+
/// </summary>
478+
public enum NetworkedListEventType
398479
{
480+
/// <summary>
481+
/// Add
482+
/// </summary>
399483
Add,
484+
/// <summary>
485+
/// Remove
486+
/// </summary>
400487
Remove,
488+
/// <summary>
489+
/// Remove pair
490+
/// </summary>
401491
RemovePair,
492+
/// <summary>
493+
/// Clear
494+
/// </summary>
402495
Clear,
496+
/// <summary>
497+
/// Value changed
498+
/// </summary>
403499
Value
404500
}
405501

406-
internal NetworkedListEventType eventType;
407-
internal TKey key;
408-
internal TValue value;
502+
/// <summary>
503+
/// Enum representing the operation made to the dictionary.
504+
/// </summary>
505+
public NetworkedListEventType eventType;
506+
/// <summary>
507+
/// the key changed, added or removed if available.
508+
/// </summary>
509+
public TKey key;
510+
/// <summary>
511+
/// The value changed, added or removed if available.
512+
/// </summary>
513+
public TValue value;
409514
}
410515
}

MLAPI/Data/NetworkedCollections/NetworkedList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public enum EventType
504504
/// </summary>
505505
public T value;
506506
/// <summary>
507-
/// the index changed, added or removed if a
507+
/// the index changed, added or removed if available
508508
/// </summary>
509509
public int index;
510510
}

0 commit comments

Comments
 (0)