From 487e1d65da06c8e4e0649f1f9214f2649cf9454b Mon Sep 17 00:00:00 2001 From: Nicolas Gnyra Date: Thu, 7 Mar 2024 17:42:09 -0500 Subject: [PATCH] Revert slightly more --- .../Components/NotifyUpdater.cs | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/BeatSaberMarkupLanguage/Components/NotifyUpdater.cs b/BeatSaberMarkupLanguage/Components/NotifyUpdater.cs index c8daf9ce..ea274b2d 100644 --- a/BeatSaberMarkupLanguage/Components/NotifyUpdater.cs +++ b/BeatSaberMarkupLanguage/Components/NotifyUpdater.cs @@ -8,9 +8,27 @@ namespace BeatSaberMarkupLanguage.Components { public class NotifyUpdater : MonoBehaviour { - internal INotifyPropertyChanged NotifyHost; - private readonly Dictionary actionDict = new(); + private INotifyPropertyChanged notifyHost; + + internal INotifyPropertyChanged NotifyHost + { + get => notifyHost; + set + { + if (notifyHost != null) + { + this.notifyHost.PropertyChanged -= NotifyHost_PropertyChanged; + } + + notifyHost = value; + + if (notifyHost != null) + { + this.notifyHost.PropertyChanged += NotifyHost_PropertyChanged; + } + } + } internal bool AddAction(string propertyName, Action action) { @@ -20,17 +38,17 @@ internal bool AddAction(string propertyName, Action action) } else { - PropertyInfo prop = NotifyHost.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + PropertyInfo prop = notifyHost.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (prop == null) { - Logger.Log.Error($"No property '{propertyName}' on object of type '{NotifyHost.GetType().FullName}'"); + Logger.Log.Error($"No property '{propertyName}' on object of type '{notifyHost.GetType().FullName}'"); return false; } if (prop.GetMethod == null) { - Logger.Log.Error($"Property '{propertyName}' on object of type '{NotifyHost.GetType().FullName}' does not have a getter"); + Logger.Log.Error($"Property '{propertyName}' on object of type '{notifyHost.GetType().FullName}' does not have a getter"); return false; } @@ -40,27 +58,14 @@ internal bool AddAction(string propertyName, Action action) return true; } - private void Start() - { - if (NotifyHost != null) - { - this.NotifyHost.PropertyChanged += NotifyHost_PropertyChanged; - } - } - - private void OnDestroy() + private void NotifyHost_PropertyChanged(object sender, PropertyChangedEventArgs e) { - if (NotifyHost != null) + if (this == null) { - this.NotifyHost.PropertyChanged -= NotifyHost_PropertyChanged; + this.notifyHost.PropertyChanged -= NotifyHost_PropertyChanged; + return; } - actionDict.Clear(); - NotifyHost = null; - } - - private void NotifyHost_PropertyChanged(object sender, PropertyChangedEventArgs e) - { // https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.propertychangedeventargs.propertyname?view=netframework-4.7.2#remarks if (string.IsNullOrEmpty(e.PropertyName)) {