1
- using System . Collections ;
1
+ using System . Collections ;
2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using MLAPI . Serialization ;
@@ -24,6 +24,17 @@ public class NetworkedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INet
24
24
private NetworkedBehaviour networkedBehaviour ;
25
25
private readonly List < NetworkedDictionaryEvent < TKey , TValue > > dirtyEvents = new List < NetworkedDictionaryEvent < TKey , TValue > > ( ) ;
26
26
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
+
27
38
/// <summary>
28
39
/// Creates a NetworkedDictionary with the default value and settings
29
40
/// </summary>
@@ -90,25 +101,53 @@ public void ReadDelta(Stream stream)
90
101
TKey key = ( TKey ) reader . ReadObjectPacked ( typeof ( TKey ) ) ;
91
102
TValue value = ( TValue ) reader . ReadObjectPacked ( typeof ( TValue ) ) ;
92
103
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
+ } ) ;
93
111
}
94
112
break ;
95
113
case NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . Remove :
96
114
{
97
115
TKey key = ( TKey ) reader . ReadObjectPacked ( typeof ( TKey ) ) ;
116
+ TValue value ;
117
+ dictionary . TryGetValue ( key , out value ) ;
98
118
dictionary . Remove ( key ) ;
119
+
120
+ if ( OnDictionaryChanged != null )
121
+ OnDictionaryChanged ( new NetworkedDictionaryEvent < TKey , TValue > {
122
+ eventType = eventType ,
123
+ key = key ,
124
+ value = value
125
+ } ) ;
99
126
}
100
127
break ;
101
128
case NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . RemovePair :
102
129
{
103
130
TKey key = ( TKey ) reader . ReadObjectPacked ( typeof ( TKey ) ) ;
104
131
TValue value = ( TValue ) reader . ReadObjectPacked ( typeof ( TValue ) ) ;
105
132
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
+ } ) ;
106
140
}
107
141
break ;
108
142
case NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . Clear :
109
143
{
110
144
//read nothing
111
145
dictionary . Clear ( ) ;
146
+
147
+ if ( OnDictionaryChanged != null )
148
+ OnDictionaryChanged ( new NetworkedDictionaryEvent < TKey , TValue > {
149
+ eventType = eventType
150
+ } ) ;
112
151
}
113
152
break ;
114
153
case NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . Value :
@@ -117,6 +156,13 @@ public void ReadDelta(Stream stream)
117
156
TValue value = ( TValue ) reader . ReadObjectPacked ( typeof ( TValue ) ) ;
118
157
if ( dictionary . ContainsKey ( key ) )
119
158
dictionary [ key ] = value ;
159
+
160
+ if ( OnDictionaryChanged != null )
161
+ OnDictionaryChanged ( new NetworkedDictionaryEvent < TKey , TValue > {
162
+ eventType = eventType ,
163
+ key = key ,
164
+ value = value
165
+ } ) ;
120
166
}
121
167
break ;
122
168
default :
@@ -276,12 +322,16 @@ public TValue this[TKey key]
276
322
set
277
323
{
278
324
dictionary [ key ] = value ;
279
- dirtyEvents . Add ( new NetworkedDictionaryEvent < TKey , TValue > ( )
325
+ NetworkedDictionaryEvent < TKey , TValue > dictionaryEvent = new NetworkedDictionaryEvent < TKey , TValue > ( )
280
326
{
281
327
eventType = NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . Value ,
282
328
key = key ,
283
329
value = value
284
- } ) ;
330
+ } ;
331
+ dirtyEvents . Add ( dictionaryEvent ) ;
332
+
333
+ if ( OnDictionaryChanged != null )
334
+ OnDictionaryChanged ( dictionaryEvent ) ;
285
335
}
286
336
}
287
337
@@ -301,34 +351,46 @@ public TValue this[TKey key]
301
351
public void Add ( TKey key , TValue value )
302
352
{
303
353
dictionary . Add ( key , value ) ;
304
- dirtyEvents . Add ( new NetworkedDictionaryEvent < TKey , TValue > ( )
354
+ NetworkedDictionaryEvent < TKey , TValue > dictionaryEvent = new NetworkedDictionaryEvent < TKey , TValue > ( )
305
355
{
306
356
eventType = NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . Add ,
307
357
key = key ,
308
358
value = value
309
- } ) ;
359
+ } ;
360
+ dirtyEvents . Add ( dictionaryEvent ) ;
361
+
362
+ if ( OnDictionaryChanged != null )
363
+ OnDictionaryChanged ( dictionaryEvent ) ;
310
364
}
311
365
312
366
/// <inheritdoc />
313
367
public void Add ( KeyValuePair < TKey , TValue > item )
314
368
{
315
369
dictionary . Add ( item ) ;
316
- dirtyEvents . Add ( new NetworkedDictionaryEvent < TKey , TValue > ( )
370
+ NetworkedDictionaryEvent < TKey , TValue > dictionaryEvent = new NetworkedDictionaryEvent < TKey , TValue > ( )
317
371
{
318
372
eventType = NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . Add ,
319
373
key = item . Key ,
320
374
value = item . Value
321
- } ) ;
375
+ } ;
376
+ dirtyEvents . Add ( dictionaryEvent ) ;
377
+
378
+ if ( OnDictionaryChanged != null )
379
+ OnDictionaryChanged ( dictionaryEvent ) ;
322
380
}
323
381
324
382
/// <inheritdoc />
325
383
public void Clear ( )
326
384
{
327
385
dictionary . Clear ( ) ;
328
- dirtyEvents . Add ( new NetworkedDictionaryEvent < TKey , TValue > ( )
386
+ NetworkedDictionaryEvent < TKey , TValue > dictionaryEvent = new NetworkedDictionaryEvent < TKey , TValue > ( )
329
387
{
330
388
eventType = NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . Clear
331
- } ) ;
389
+ } ;
390
+ dirtyEvents . Add ( dictionaryEvent ) ;
391
+
392
+ if ( OnDictionaryChanged != null )
393
+ OnDictionaryChanged ( dictionaryEvent ) ;
332
394
}
333
395
334
396
/// <inheritdoc />
@@ -358,14 +420,21 @@ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
358
420
/// <inheritdoc />
359
421
public bool Remove ( TKey key )
360
422
{
423
+ TValue value ;
424
+ dictionary . TryGetValue ( key , out value ) ;
361
425
bool state = dictionary . Remove ( key ) ;
362
426
if ( state )
363
427
{
364
- dirtyEvents . Add ( new NetworkedDictionaryEvent < TKey , TValue > ( )
428
+ NetworkedDictionaryEvent < TKey , TValue > dictionaryEvent = new NetworkedDictionaryEvent < TKey , TValue > ( )
365
429
{
366
430
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 ) ;
369
438
}
370
439
return state ;
371
440
}
@@ -376,12 +445,16 @@ public bool Remove(KeyValuePair<TKey, TValue> item)
376
445
bool state = dictionary . Remove ( item ) ;
377
446
if ( state )
378
447
{
379
- dirtyEvents . Add ( new NetworkedDictionaryEvent < TKey , TValue > ( )
448
+ NetworkedDictionaryEvent < TKey , TValue > dictionaryEvent = new NetworkedDictionaryEvent < TKey , TValue > ( )
380
449
{
381
450
eventType = NetworkedDictionaryEvent < TKey , TValue > . NetworkedListEventType . RemovePair ,
382
451
key = item . Key ,
383
452
value = item . Value
384
- } ) ;
453
+ } ;
454
+ dirtyEvents . Add ( dictionaryEvent ) ;
455
+
456
+ if ( OnDictionaryChanged != null )
457
+ OnDictionaryChanged ( dictionaryEvent ) ;
385
458
}
386
459
return state ;
387
460
}
@@ -392,19 +465,51 @@ IEnumerator IEnumerable.GetEnumerator()
392
465
}
393
466
}
394
467
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 >
396
474
{
397
- internal enum NetworkedListEventType
475
+ /// <summary>
476
+ /// Enum representing the different operations available for triggering an event.
477
+ /// </summary>
478
+ public enum NetworkedListEventType
398
479
{
480
+ /// <summary>
481
+ /// Add
482
+ /// </summary>
399
483
Add ,
484
+ /// <summary>
485
+ /// Remove
486
+ /// </summary>
400
487
Remove ,
488
+ /// <summary>
489
+ /// Remove pair
490
+ /// </summary>
401
491
RemovePair ,
492
+ /// <summary>
493
+ /// Clear
494
+ /// </summary>
402
495
Clear ,
496
+ /// <summary>
497
+ /// Value changed
498
+ /// </summary>
403
499
Value
404
500
}
405
501
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 ;
409
514
}
410
515
}
0 commit comments