@@ -22,6 +22,15 @@ public class NetworkedList<T> : IList<T>, INetworkedVar
2222 /// The settings for this container
2323 /// </summary>
2424 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 ;
2534
2635 /// <summary>
2736 /// Creates a NetworkedList with the default value and settings
@@ -136,35 +145,35 @@ public void WriteDelta(Stream stream)
136145 switch ( dirtyEvents [ i ] . eventType )
137146 {
138147 //Fuck me these signatures are proper aids
139- case NetworkedListEvent < T > . NetworkedListEventType . Add :
148+ case NetworkedListEvent < T > . EventType . Add :
140149 {
141150 writer . WriteObjectPacked ( dirtyEvents [ i ] . value ) ; //BOX
142151 }
143152 break ;
144- case NetworkedListEvent < T > . NetworkedListEventType . Insert :
153+ case NetworkedListEvent < T > . EventType . Insert :
145154 {
146155 writer . WriteInt32Packed ( dirtyEvents [ i ] . index ) ;
147156 writer . WriteObjectPacked ( dirtyEvents [ i ] . value ) ; //BOX
148157 }
149158 break ;
150- case NetworkedListEvent < T > . NetworkedListEventType . Remove :
159+ case NetworkedListEvent < T > . EventType . Remove :
151160 {
152161 writer . WriteObjectPacked ( dirtyEvents [ i ] . value ) ; //BOX
153162 }
154163 break ;
155- case NetworkedListEvent < T > . NetworkedListEventType . RemoveAt :
164+ case NetworkedListEvent < T > . EventType . RemoveAt :
156165 {
157166 writer . WriteInt32Packed ( dirtyEvents [ i ] . index ) ;
158167 }
159168 break ;
160- case NetworkedListEvent < T > . NetworkedListEventType . Value :
169+ case NetworkedListEvent < T > . EventType . Value :
161170 {
162171 writer . WriteInt32Packed ( dirtyEvents [ i ] . index ) ;
163172 writer . WriteObjectPacked ( dirtyEvents [ i ] . value ) ; //BOX
164173 }
165174
166175 break ;
167- case NetworkedListEvent < T > . NetworkedListEventType . Clear :
176+ case NetworkedListEvent < T > . EventType . Clear :
168177 {
169178 //Nothing has to be written
170179 }
@@ -209,41 +218,91 @@ public void ReadDelta(Stream stream)
209218 ushort deltaCount = reader . ReadUInt16Packed ( ) ;
210219 for ( int i = 0 ; i < deltaCount ; i ++ )
211220 {
212- NetworkedListEvent < T > . NetworkedListEventType eventType = ( NetworkedListEvent < T > . NetworkedListEventType ) reader . ReadBits ( 3 ) ;
221+ NetworkedListEvent < T > . EventType eventType = ( NetworkedListEvent < T > . EventType ) reader . ReadBits ( 3 ) ;
213222 switch ( eventType )
214223 {
215- case NetworkedListEvent < T > . NetworkedListEventType . Add :
224+ case NetworkedListEvent < T > . EventType . Add :
216225 {
217226 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+ } ) ;
218235 }
219236 break ;
220- case NetworkedListEvent < T > . NetworkedListEventType . Insert :
237+ case NetworkedListEvent < T > . EventType . Insert :
221238 {
222239 int index = reader . ReadInt32Packed ( ) ;
223240 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+ } ) ;
224249 }
225250 break ;
226- case NetworkedListEvent < T > . NetworkedListEventType . Remove :
251+ case NetworkedListEvent < T > . EventType . Remove :
227252 {
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+ } ) ;
229264 }
230265 break ;
231- case NetworkedListEvent < T > . NetworkedListEventType . RemoveAt :
266+ case NetworkedListEvent < T > . EventType . RemoveAt :
232267 {
233268 int index = reader . ReadInt32Packed ( ) ;
269+ T value = list [ index ] ;
234270 list . RemoveAt ( index ) ;
271+
272+ if ( OnListChanged != null )
273+ OnListChanged ( new NetworkedListEvent < T >
274+ {
275+ eventType = eventType ,
276+ index = index ,
277+ value = value
278+ } ) ;
235279 }
236280 break ;
237- case NetworkedListEvent < T > . NetworkedListEventType . Value :
281+ case NetworkedListEvent < T > . EventType . Value :
238282 {
239283 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+ } ) ;
241294 }
242295 break ;
243- case NetworkedListEvent < T > . NetworkedListEventType . Clear :
296+ case NetworkedListEvent < T > . EventType . Clear :
244297 {
245298 //Read nothing
246299 list . Clear ( ) ;
300+
301+ if ( OnListChanged != null )
302+ OnListChanged ( new NetworkedListEvent < T >
303+ {
304+ eventType = eventType ,
305+ } ) ;
247306 }
248307 break ;
249308 }
@@ -272,21 +331,30 @@ IEnumerator IEnumerable.GetEnumerator()
272331 public void Add ( T item )
273332 {
274333 list . Add ( item ) ;
275- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
334+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
276335 {
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 ) ;
280344 }
281345
282346 /// <inheritdoc />
283347 public void Clear ( )
284348 {
285349 list . Clear ( ) ;
286- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
350+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
287351 {
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 ) ;
290358 }
291359
292360 /// <inheritdoc />
@@ -307,11 +375,15 @@ public bool Remove(T item)
307375 bool state = list . Remove ( item ) ;
308376 if ( state )
309377 {
310- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
378+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
311379 {
312- eventType = NetworkedListEvent < T > . NetworkedListEventType . Remove ,
380+ eventType = NetworkedListEvent < T > . EventType . Remove ,
313381 value = item
314- } ) ;
382+ } ;
383+ dirtyEvents . Add ( listEvent ) ;
384+
385+ if ( OnListChanged != null )
386+ OnListChanged ( listEvent ) ;
315387 }
316388 return state ;
317389 }
@@ -332,24 +404,35 @@ public int IndexOf(T item)
332404 public void Insert ( int index , T item )
333405 {
334406 list . Insert ( index , item ) ;
335- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
407+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
336408 {
337- eventType = NetworkedListEvent < T > . NetworkedListEventType . Insert ,
338- index = index ,
409+ eventType = NetworkedListEvent < T > . EventType . Insert ,
410+ index = index ,
339411 value = item
340- } ) ;
412+ } ;
413+ dirtyEvents . Add ( listEvent ) ;
414+
415+ if ( OnListChanged != null )
416+ OnListChanged ( listEvent ) ;
341417 }
342418
343419 /// <inheritdoc />
344420 public void RemoveAt ( int index )
345421 {
422+ T value = list [ index ] ;
346423 list . RemoveAt ( index ) ;
347- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
424+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
348425 {
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 ) ;
352434 }
435+
353436
354437 /// <inheritdoc />
355438 public T this [ int index ]
@@ -361,30 +444,68 @@ public T this[int index]
361444 set
362445 {
363446 list [ index ] = value ;
364- dirtyEvents . Add ( new NetworkedListEvent < T > ( )
447+ NetworkedListEvent < T > listEvent = new NetworkedListEvent < T > ( )
365448 {
366- eventType = NetworkedListEvent < T > . NetworkedListEventType . Value ,
449+ eventType = NetworkedListEvent < T > . EventType . Value ,
367450 index = index ,
368451 value = value
369- } ) ;
452+ } ;
453+ dirtyEvents . Add ( listEvent ) ;
454+
455+ if ( OnListChanged != null )
456+ OnListChanged ( listEvent ) ;
370457 }
371458 }
372459 }
373460
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 >
375466 {
376- internal enum NetworkedListEventType
467+ /// <summary>
468+ /// Enum representing the different operations available for triggering an event.
469+ /// </summary>
470+ public enum EventType
377471 {
472+ /// <summary>
473+ /// Add
474+ /// </summary>
378475 Add ,
476+ /// <summary>
477+ /// Insert
478+ /// </summary>
379479 Insert ,
480+ /// <summary>
481+ /// Remove
482+ /// </summary>
380483 Remove ,
484+ /// <summary>
485+ /// Remove at
486+ /// </summary>
381487 RemoveAt ,
488+ /// <summary>
489+ /// Value changed
490+ /// </summary>
382491 Value ,
492+ /// <summary>
493+ /// Clear
494+ /// </summary>
383495 Clear
384496 }
385497
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 ;
389510 }
390511}
0 commit comments