-
Notifications
You must be signed in to change notification settings - Fork 2
fix delegates lifetime: unsubscribe stale handlers on network despawn #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| using BepInEx.Configuration; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
| using Unity.Netcode; | ||
| using UnityEngine; | ||
| using LogLevel = BepInEx.Logging.LogLevel; | ||
|
|
@@ -36,7 +38,9 @@ public bool SyncEnabled | |
| } | ||
|
|
||
| private readonly NetworkVariable<bool> _syncEnabled = new(); | ||
| private NetworkList<SyncedEntryDelta> _deltas = null!; | ||
| private readonly NetworkList<SyncedEntryDelta> _deltas = new(); | ||
| private readonly List<(ConfigFile, EventHandler<SettingChangedEventArgs>)> _configFileDelegates = new(); | ||
| private readonly List<(SyncedEntryBase, EventHandler)> _entrySyncEnabledDelegates = new(); | ||
ratijas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [MemberNotNull(nameof(EntryContainer))] | ||
| private void EnsureEntryContainer() | ||
|
|
@@ -45,12 +49,6 @@ private void EnsureEntryContainer() | |
| throw new InvalidOperationException("Entry container has not been assigned."); | ||
| } | ||
|
|
||
| private void Awake() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, do not do this. Revert.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the time I wrote the code, I knew field declaration initialisers existed and I intentionally didn't use one (hence |
||
| { | ||
| EnsureEntryContainer(); | ||
| _deltas = new NetworkList<SyncedEntryDelta>(); | ||
| } | ||
|
|
||
| public override void OnNetworkSpawn() | ||
| { | ||
| EnsureEntryContainer(); | ||
|
|
@@ -59,28 +57,30 @@ public override void OnNetworkSpawn() | |
| { | ||
| _syncEnabled.Value = true; | ||
|
|
||
| foreach (var syncedEntryBase in EntryContainer.Values) | ||
| foreach (var (syncedEntryBase, index) in EntryContainer.Values.Select((e, i) => (e, i))) | ||
|
||
| { | ||
| var currentIndex = _deltas.Count; | ||
| _deltas.Add(syncedEntryBase.ToDelta()); | ||
|
|
||
| syncedEntryBase.BoxedEntry.ConfigFile.SettingChanged += (_, args) => | ||
| void configFileDelegate(object sender, SettingChangedEventArgs args) | ||
ratijas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (!ReferenceEquals(syncedEntryBase.BoxedEntry, args.ChangedSetting)) return; | ||
| _deltas[currentIndex] = syncedEntryBase.ToDelta(); | ||
| }; | ||
| _deltas[index] = syncedEntryBase.ToDelta(); | ||
| } | ||
| var configFile = syncedEntryBase.BoxedEntry.ConfigFile; | ||
| configFile.SettingChanged += configFileDelegate; | ||
| _configFileDelegates.Add((configFile, configFileDelegate)); | ||
|
|
||
| syncedEntryBase.SyncEnabledChanged += (_, args) => | ||
| void entrySyncEnabledDelegate(object sender, EventArgs args) | ||
| { | ||
| _deltas[currentIndex] = syncedEntryBase.ToDelta(); | ||
| }; | ||
| _deltas[index] = syncedEntryBase.ToDelta(); | ||
| } | ||
| syncedEntryBase.SyncEnabledChanged += entrySyncEnabledDelegate; | ||
| _entrySyncEnabledDelegates.Add((syncedEntryBase, entrySyncEnabledDelegate)); | ||
| } | ||
|
|
||
| InitialSyncCompletedHandler?.Invoke(this, EventArgs.Empty); | ||
| return; | ||
| } | ||
|
|
||
| if (IsClient) | ||
| else if (IsClient) | ||
| { | ||
| _syncEnabled.OnValueChanged += OnSyncEnabledChanged; | ||
| _deltas.OnListChanged += OnClientDeltaListChanged; | ||
|
|
@@ -94,6 +94,48 @@ public override void OnNetworkSpawn() | |
|
|
||
| InitialSyncCompletedHandler?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
| base.OnNetworkSpawn(); | ||
| } | ||
|
|
||
| public override void OnNetworkDespawn() | ||
| { | ||
| EnsureEntryContainer(); | ||
|
|
||
| if (IsServer) | ||
| { | ||
| foreach (var (configFile, configFileDelegate) in _configFileDelegates) | ||
| { | ||
| configFile.SettingChanged -= configFileDelegate; | ||
| } | ||
|
|
||
| foreach (var (syncedEntryBase, entrySyncEnabledDelegate) in _entrySyncEnabledDelegates) | ||
| { | ||
| syncedEntryBase.SyncEnabledChanged -= entrySyncEnabledDelegate; | ||
| } | ||
|
|
||
| _configFileDelegates.Clear(); | ||
| _entrySyncEnabledDelegates.Clear(); | ||
| // NetworkList and NetworkVariable can not be modified now because the network manager has already shut down. | ||
| // See https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/pull/3502 | ||
| // Should not be needed anyway, since this component isn't being reused between network sessions. | ||
| // _deltas.Clear(); | ||
| // _syncEnabled.Value = false; | ||
| } | ||
| else if (IsClient) | ||
| { | ||
| DisableOverrides(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been copied from OnDestroy, I don't like the duplication. Please can you explain why this is necessary?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't even remember for sure. But it perfectly mirrors whatever setup is being performed in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mainly don't like the possibility that the clean-up is run twice. |
||
|
|
||
| foreach (var delta in _deltas) | ||
| { | ||
| ResetOverrideValue(delta); | ||
| } | ||
|
|
||
| _deltas.OnListChanged -= OnClientDeltaListChanged; | ||
| _syncEnabled.OnValueChanged -= OnSyncEnabledChanged; | ||
| } | ||
|
|
||
| base.OnNetworkDespawn(); | ||
| } | ||
|
|
||
| public override void OnDestroy() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.