@@ -22,6 +22,15 @@ public class NetworkedList<T> : IList<T>, INetworkedVar
22
22
/// The settings for this container
23
23
/// </summary>
24
24
public readonly NetworkedVarSettings Settings = new NetworkedVarSettings ( ) ;
25
+ /// <summary>
26
+ /// Delegate type for list changed event
27
+ /// </summary>
28
+ /// <param name="changeEvent">Struct containing information about the change event</param>
29
+ public delegate void OnListChangedDelegate ( NetworkedListEvent < T > changeEvent ) ;
30
+ /// <summary>
31
+ /// The callback to be invoked when the list gets changed
32
+ /// </summary>
33
+ public event OnListChangedDelegate OnListChanged ;
25
34
26
35
/// <summary>
27
36
/// Creates a NetworkedList with the default value and settings
@@ -136,35 +145,35 @@ public void WriteDelta(Stream stream)
136
145
switch ( dirtyEvents [ i ] . eventType )
137
146
{
138
147
//Fuck me these signatures are proper aids
139
- case NetworkedListEvent < T > . NetworkedListEventType . Add :
148
+ case NetworkedListEvent < T > . EventType . Add :
140
149
{
141
150
writer . WriteObjectPacked ( dirtyEvents [ i ] . value ) ; //BOX
142
151
}
143
152
break ;
144
- case NetworkedListEvent < T > . NetworkedListEventType . Insert :
153
+ case NetworkedListEvent < T > . EventType . Insert :
145
154
{
146
155
writer . WriteInt32Packed ( dirtyEvents [ i ] . index ) ;
147
156
writer . WriteObjectPacked ( dirtyEvents [ i ] . value ) ; //BOX
148
157
}
149
158
break ;
150
- case NetworkedListEvent < T > . NetworkedListEventType . Remove :
159
+ case NetworkedListEvent < T > . EventType . Remove :
151
160
{
152
161
writer . WriteObjectPacked ( dirtyEvents [ i ] . value ) ; //BOX
153
162
}
154
163
break ;
155
- case NetworkedListEvent < T > . NetworkedListEventType . RemoveAt :
164
+ case NetworkedListEvent < T > . EventType . RemoveAt :
156
165
{
157
166
writer . WriteInt32Packed ( dirtyEvents [ i ] . index ) ;
158
167
}
159
168
break ;
160
- case NetworkedListEvent < T > . NetworkedListEventType . Value :
169
+ case NetworkedListEvent < T > . EventType . Value :
161
170
{
162
171
writer . WriteInt32Packed ( dirtyEvents [ i ] . index ) ;
163
172
writer . WriteObjectPacked ( dirtyEvents [ i ] . value ) ; //BOX
164
173
}
165
174
166
175
break ;
167
- case NetworkedListEvent < T > . NetworkedListEventType . Clear :
176
+ case NetworkedListEvent < T > . EventType . Clear :
168
177
{
169
178
//Nothing has to be written
170
179
}
@@ -209,41 +218,91 @@ public void ReadDelta(Stream stream)
209
218
ushort deltaCount = reader . ReadUInt16Packed ( ) ;
210
219
for ( int i = 0 ; i < deltaCount ; i ++ )
211
220
{
212
- NetworkedListEvent < T > . NetworkedListEventType eventType = ( NetworkedListEvent < T > . NetworkedListEventType ) reader . ReadBits ( 3 ) ;
221
+ NetworkedListEvent < T > . EventType eventType = ( NetworkedListEvent < T > . EventType ) reader . ReadBits ( 3 ) ;
213
222
switch ( eventType )
214
223
{
215
- case NetworkedListEvent < T > . NetworkedListEventType . Add :
224
+ case NetworkedListEvent < T > . EventType . Add :
216
225
{
217
226
list . Add ( ( T ) reader . ReadObjectPacked ( typeof ( T ) ) ) ; //BOX
227
+
228
+ if ( OnListChanged != null )
229
+ OnListChanged ( new NetworkedListEvent < T >
230
+ {
231
+ eventType = eventType ,
232
+ index = list . Count - 1 ,
233
+ value = list [ list . Count - 1 ]
234
+ } ) ;
218
235
}
219
236
break ;
220
- case NetworkedListEvent < T > . NetworkedListEventType . Insert :
237
+ case NetworkedListEvent < T > . EventType . Insert :
221
238
{
222
239
int index = reader . ReadInt32Packed ( ) ;
223
240
list . Insert ( index , ( T ) reader . ReadObjectPacked ( typeof ( T ) ) ) ; //BOX
241
+
242
+ if ( OnListChanged != null )
243
+ OnListChanged ( new NetworkedListEvent < T >
244
+ {
245
+ eventType = eventType ,
246
+ index = index ,
247
+ value = list [ index ]
248
+ } ) ;
224
249
}
225
250
break ;
226
- case NetworkedListEvent < T > . NetworkedListEventType . Remove :
251
+ case NetworkedListEvent < T > . EventType . Remove :
227
252
{
228
- list . Remove ( ( T ) reader . ReadObjectPacked ( typeof ( T ) ) ) ; //BOX
253
+ T value = ( T ) reader . ReadObjectPacked ( typeof ( T ) ) ; //BOX
254
+ int index = list . IndexOf ( value ) ;
255
+ list . RemoveAt ( index ) ;
256
+
257
+ if ( OnListChanged != null )
258
+ OnListChanged ( new NetworkedListEvent < T >
259
+ {
260
+ eventType = eventType ,
261
+ index = index ,
262
+ value = value
263
+ } ) ;
229
264
}
230
265
break ;
231
- case NetworkedListEvent < T > . NetworkedListEventType . RemoveAt :
266
+ case NetworkedListEvent < T > . EventType . RemoveAt :
232
267
{
233
268
int index = reader . ReadInt32Packed ( ) ;
269
+ T value = list [ index ] ;
234
270
list . RemoveAt ( index ) ;
271
+
272
+ if ( OnListChanged != null )
273
+ OnListChanged ( new NetworkedListEvent < T >
274
+ {
275
+ eventType = eventType ,
276
+ index = index ,
277
+ value = value
278
+ } ) ;
235
279
}
236
280
break ;
237
- case NetworkedListEvent < T > . NetworkedListEventType . Value :
281
+ case NetworkedListEvent < T > . EventType . Value :
238
282
{
239
283
int index = reader . ReadInt32Packed ( ) ;
240
- if ( index < list . Count ) list [ index ] = ( T ) reader . ReadObjectPacked ( typeof ( T ) ) ; //BOX
284
+ T value = ( T ) reader . ReadObjectPacked ( typeof ( T ) ) ; //BOX
285
+ if ( index < list . Count ) list [ index ] = value ;
286
+
287
+ if ( OnListChanged != null )
288
+ OnListChanged ( new NetworkedListEvent < T >
289
+ {
290
+ eventType = eventType ,
291
+ index = index ,
292
+ value = value
293
+ } ) ;
241
294
}
242
295
break ;
243
- case NetworkedListEvent < T > . NetworkedListEventType . Clear :
296
+ case NetworkedListEvent < T > . EventType . Clear :
244
297
{
245
298
//Read nothing
246
299
list . Clear ( ) ;
300
+
301
+ if ( OnListChanged != null )
302
+ OnListChanged ( new NetworkedListEvent < T >
303
+ {
304
+ eventType = eventType ,
305
+ } ) ;
247
306
}
248
307
break ;
249
308
}
@@ -272,21 +331,30 @@ IEnumerator IEnumerable.GetEnumerator()
272
331
public void Add ( T item )
273
332
{
274
333
list . Add ( item ) ;
275
- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
334
+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
276
335
{
277
- eventType = NetworkedListEvent < T > . NetworkedListEventType . Add ,
278
- value = item
279
- } ) ;
336
+ eventType = NetworkedListEvent < T > . EventType . Add ,
337
+ value = item ,
338
+ index = list . Count - 1
339
+ } ;
340
+ dirtyEvents . Add ( listEvent ) ;
341
+
342
+ if ( OnListChanged != null )
343
+ OnListChanged ( listEvent ) ;
280
344
}
281
345
282
346
/// <inheritdoc />
283
347
public void Clear ( )
284
348
{
285
349
list . Clear ( ) ;
286
- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
350
+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
287
351
{
288
- eventType = NetworkedListEvent < T > . NetworkedListEventType . Clear
289
- } ) ;
352
+ eventType = NetworkedListEvent < T > . EventType . Clear
353
+ } ;
354
+ dirtyEvents . Add ( listEvent ) ;
355
+
356
+ if ( OnListChanged != null )
357
+ OnListChanged ( listEvent ) ;
290
358
}
291
359
292
360
/// <inheritdoc />
@@ -307,11 +375,15 @@ public bool Remove(T item)
307
375
bool state = list . Remove ( item ) ;
308
376
if ( state )
309
377
{
310
- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
378
+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
311
379
{
312
- eventType = NetworkedListEvent < T > . NetworkedListEventType . Remove ,
380
+ eventType = NetworkedListEvent < T > . EventType . Remove ,
313
381
value = item
314
- } ) ;
382
+ } ;
383
+ dirtyEvents . Add ( listEvent ) ;
384
+
385
+ if ( OnListChanged != null )
386
+ OnListChanged ( listEvent ) ;
315
387
}
316
388
return state ;
317
389
}
@@ -332,24 +404,35 @@ public int IndexOf(T item)
332
404
public void Insert ( int index , T item )
333
405
{
334
406
list . Insert ( index , item ) ;
335
- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
407
+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
336
408
{
337
- eventType = NetworkedListEvent < T > . NetworkedListEventType . Insert ,
338
- index = index ,
409
+ eventType = NetworkedListEvent < T > . EventType . Insert ,
410
+ index = index ,
339
411
value = item
340
- } ) ;
412
+ } ;
413
+ dirtyEvents . Add ( listEvent ) ;
414
+
415
+ if ( OnListChanged != null )
416
+ OnListChanged ( listEvent ) ;
341
417
}
342
418
343
419
/// <inheritdoc />
344
420
public void RemoveAt ( int index )
345
421
{
422
+ T value = list [ index ] ;
346
423
list . RemoveAt ( index ) ;
347
- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
424
+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
348
425
{
349
- eventType = NetworkedListEvent < T > . NetworkedListEventType . RemoveAt ,
350
- index = index
351
- } ) ;
426
+ eventType = NetworkedListEvent < T > . EventType . RemoveAt ,
427
+ index = index ,
428
+ value = value
429
+ } ;
430
+ dirtyEvents . Add ( listEvent ) ;
431
+
432
+ if ( OnListChanged != null )
433
+ OnListChanged ( listEvent ) ;
352
434
}
435
+
353
436
354
437
/// <inheritdoc />
355
438
public T this [ int index ]
@@ -361,30 +444,68 @@ public T this[int index]
361
444
set
362
445
{
363
446
list [ index ] = value ;
364
- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
447
+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
365
448
{
366
- eventType = NetworkedListEvent < T > . NetworkedListEventType . Value ,
449
+ eventType = NetworkedListEvent < T > . EventType . Value ,
367
450
index = index ,
368
451
value = value
369
- } ) ;
452
+ } ;
453
+ dirtyEvents . Add ( listEvent ) ;
454
+
455
+ if ( OnListChanged != null )
456
+ OnListChanged ( listEvent ) ;
370
457
}
371
458
}
372
459
}
373
460
374
- internal struct NetworkedListEvent < T >
461
+ /// <summary>
462
+ /// Struct containing event information about changes to a NetworkedList.
463
+ /// </summary>
464
+ /// <typeparam name="T">The type for the list that the event is about</typeparam>
465
+ public struct NetworkedListEvent < T >
375
466
{
376
- internal enum NetworkedListEventType
467
+ /// <summary>
468
+ /// Enum representing the different operations available for triggering an event.
469
+ /// </summary>
470
+ public enum EventType
377
471
{
472
+ /// <summary>
473
+ /// Add
474
+ /// </summary>
378
475
Add ,
476
+ /// <summary>
477
+ /// Insert
478
+ /// </summary>
379
479
Insert ,
480
+ /// <summary>
481
+ /// Remove
482
+ /// </summary>
380
483
Remove ,
484
+ /// <summary>
485
+ /// Remove at
486
+ /// </summary>
381
487
RemoveAt ,
488
+ /// <summary>
489
+ /// Value changed
490
+ /// </summary>
382
491
Value ,
492
+ /// <summary>
493
+ /// Clear
494
+ /// </summary>
383
495
Clear
384
496
}
385
497
386
- internal NetworkedListEventType eventType ;
387
- internal T value ;
388
- internal int index ;
498
+ /// <summary>
499
+ /// Enum representing the operation made to the list.
500
+ /// </summary>
501
+ public EventType eventType ;
502
+ /// <summary>
503
+ /// The value changed, added or removed if available.
504
+ /// </summary>
505
+ public T value ;
506
+ /// <summary>
507
+ /// the index changed, added or removed if a
508
+ /// </summary>
509
+ public int index ;
389
510
}
390
511
}
0 commit comments