Skip to content

Commit 0afe253

Browse files
authored
fix: backports a fix to networkedlist/dictionary from #458 (#461)
1 parent 1348637 commit 0afe253

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

MLAPI/NetworkedVar/Collections/NetworkedDictionary.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,8 @@ public TValue this[TKey key]
393393
key = key,
394394
value = value
395395
};
396-
dirtyEvents.Add(dictionaryEvent);
397396

398-
if (NetworkingManager.Singleton.IsServer && OnDictionaryChanged != null)
399-
OnDictionaryChanged(dictionaryEvent);
397+
HandleAddDictionaryEvent(dictionaryEvent);
400398
}
401399
}
402400

@@ -424,10 +422,8 @@ public void Add(TKey key, TValue value)
424422
key = key,
425423
value = value
426424
};
427-
dirtyEvents.Add(dictionaryEvent);
428425

429-
if (NetworkingManager.Singleton.IsServer && OnDictionaryChanged != null)
430-
OnDictionaryChanged(dictionaryEvent);
426+
HandleAddDictionaryEvent(dictionaryEvent);
431427
}
432428

433429
/// <inheritdoc />
@@ -442,10 +438,8 @@ public void Add(KeyValuePair<TKey, TValue> item)
442438
key = item.Key,
443439
value = item.Value
444440
};
445-
dirtyEvents.Add(dictionaryEvent);
446441

447-
if (NetworkingManager.Singleton.IsServer && OnDictionaryChanged != null)
448-
OnDictionaryChanged(dictionaryEvent);
442+
HandleAddDictionaryEvent(dictionaryEvent);
449443
}
450444

451445
/// <inheritdoc />
@@ -458,10 +452,8 @@ public void Clear()
458452
{
459453
eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear
460454
};
461-
dirtyEvents.Add(dictionaryEvent);
462455

463-
if (NetworkingManager.Singleton.IsServer && OnDictionaryChanged != null)
464-
OnDictionaryChanged(dictionaryEvent);
456+
HandleAddDictionaryEvent(dictionaryEvent);
465457
}
466458

467459
/// <inheritdoc />
@@ -503,10 +495,8 @@ public bool Remove(TKey key)
503495
key = key,
504496
value = value
505497
};
506-
dirtyEvents.Add(dictionaryEvent);
507498

508-
if (NetworkingManager.Singleton.IsServer && OnDictionaryChanged != null)
509-
OnDictionaryChanged(dictionaryEvent);
499+
HandleAddDictionaryEvent(dictionaryEvent);
510500

511501
return true;
512502
}
@@ -524,11 +514,8 @@ public bool Remove(KeyValuePair<TKey, TValue> item)
524514
key = item.Key,
525515
value = item.Value
526516
};
527-
dirtyEvents.Add(dictionaryEvent);
528-
529-
if (NetworkingManager.Singleton.IsServer && OnDictionaryChanged != null)
530-
OnDictionaryChanged(dictionaryEvent);
531517

518+
HandleAddDictionaryEvent(dictionaryEvent);
532519
return true;
533520
}
534521

@@ -537,6 +524,23 @@ IEnumerator IEnumerable.GetEnumerator()
537524
{
538525
return dictionary.GetEnumerator();
539526
}
527+
528+
private void HandleAddDictionaryEvent(NetworkedDictionaryEvent<TKey, TValue> dictionaryEvent)
529+
{
530+
if (NetworkingManager.Singleton.IsServer)
531+
{
532+
if (NetworkingManager.Singleton.ConnectedClients.Count > 0)
533+
{
534+
dirtyEvents.Add(dictionaryEvent);
535+
}
536+
537+
OnDictionaryChanged?.Invoke(dictionaryEvent);
538+
}
539+
else
540+
{
541+
dirtyEvents.Add(dictionaryEvent);
542+
}
543+
}
540544
}
541545

542546
/// <summary>
@@ -547,7 +551,7 @@ IEnumerator IEnumerable.GetEnumerator()
547551
public struct NetworkedDictionaryEvent<TKey, TValue>
548552
{
549553
/// <summary>
550-
/// Enum representing the different operations available for triggering an event.
554+
/// Enum representing the different operations available for triggering an event.
551555
/// </summary>
552556
public enum NetworkedListEventType
553557
{

MLAPI/NetworkedVar/Collections/NetworkedList.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,8 @@ public void Add(T item)
414414
value = item,
415415
index = list.Count - 1
416416
};
417-
dirtyEvents.Add(listEvent);
418417

419-
if (NetworkingManager.Singleton.IsServer && OnListChanged != null)
420-
OnListChanged(listEvent);
418+
HandleAddListEvent(listEvent);
421419
}
422420

423421
/// <inheritdoc />
@@ -430,10 +428,8 @@ public void Clear()
430428
{
431429
eventType = NetworkedListEvent<T>.EventType.Clear
432430
};
433-
dirtyEvents.Add(listEvent);
434431

435-
if (NetworkingManager.Singleton.IsServer && OnListChanged != null)
436-
OnListChanged(listEvent);
432+
HandleAddListEvent(listEvent);
437433
}
438434

439435
/// <inheritdoc />
@@ -459,11 +455,8 @@ public bool Remove(T item)
459455
eventType = NetworkedListEvent<T>.EventType.Remove,
460456
value = item
461457
};
462-
dirtyEvents.Add(listEvent);
463-
464-
if (NetworkingManager.Singleton.IsServer && OnListChanged != null)
465-
OnListChanged(listEvent);
466458

459+
HandleAddListEvent(listEvent);
467460
return true;
468461
}
469462

@@ -491,10 +484,8 @@ public void Insert(int index, T item)
491484
index = index,
492485
value = item
493486
};
494-
dirtyEvents.Add(listEvent);
495487

496-
if (NetworkingManager.Singleton.IsServer && OnListChanged != null)
497-
OnListChanged(listEvent);
488+
HandleAddListEvent(listEvent);
498489
}
499490

500491
/// <inheritdoc />
@@ -508,10 +499,8 @@ public void RemoveAt(int index)
508499
eventType = NetworkedListEvent<T>.EventType.RemoveAt,
509500
index = index
510501
};
511-
dirtyEvents.Add(listEvent);
512502

513-
if (NetworkingManager.Singleton.IsServer && OnListChanged != null)
514-
OnListChanged(listEvent);
503+
HandleAddListEvent(listEvent);
515504
}
516505

517506

@@ -530,10 +519,25 @@ public T this[int index]
530519
index = index,
531520
value = value
532521
};
533-
dirtyEvents.Add(listEvent);
534522

535-
if (NetworkingManager.Singleton.IsServer && OnListChanged != null)
536-
OnListChanged(listEvent);
523+
HandleAddListEvent(listEvent);
524+
}
525+
}
526+
527+
private void HandleAddListEvent(NetworkedListEvent<T> listEvent)
528+
{
529+
if (NetworkingManager.Singleton.IsServer)
530+
{
531+
if (NetworkingManager.Singleton.ConnectedClients.Count > 0)
532+
{
533+
dirtyEvents.Add(listEvent);
534+
}
535+
536+
OnListChanged?.Invoke(listEvent);
537+
}
538+
else
539+
{
540+
dirtyEvents.Add(listEvent);
537541
}
538542
}
539543
}
@@ -545,7 +549,7 @@ public T this[int index]
545549
public struct NetworkedListEvent<T>
546550
{
547551
/// <summary>
548-
/// Enum representing the different operations available for triggering an event.
552+
/// Enum representing the different operations available for triggering an event.
549553
/// </summary>
550554
public enum EventType
551555
{

0 commit comments

Comments
 (0)