Skip to content

Commit

Permalink
Revert slightly more
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Mar 7, 2024
1 parent 6ac832f commit 487e1d6
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions BeatSaberMarkupLanguage/Components/NotifyUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,27 @@ namespace BeatSaberMarkupLanguage.Components
{
public class NotifyUpdater : MonoBehaviour
{
internal INotifyPropertyChanged NotifyHost;

private readonly Dictionary<string, PropertyAction> 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<object> action)
{
Expand All @@ -20,17 +38,17 @@ internal bool AddAction(string propertyName, Action<object> 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;
}

Expand All @@ -40,27 +58,14 @@ internal bool AddAction(string propertyName, Action<object> 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))
{
Expand Down

0 comments on commit 487e1d6

Please sign in to comment.