Skip to content

Commit

Permalink
Partially revert "Rework NotifyUpdater"
Browse files Browse the repository at this point in the history
This commit partially reverts commit 34c0a7a.
  • Loading branch information
nicoco007 committed Mar 3, 2024
1 parent 328aee1 commit 6b2d876
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
41 changes: 26 additions & 15 deletions BeatSaberMarkupLanguage/Components/NotifyUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using UnityEngine;

namespace BeatSaberMarkupLanguage.Components
{
internal class NotifyUpdater
public class NotifyUpdater : MonoBehaviour
{
private readonly Dictionary<string, PropertyAction> actionDict = new();
private readonly INotifyPropertyChanged notifyHost;
internal INotifyPropertyChanged NotifyHost;

internal NotifyUpdater(INotifyPropertyChanged notifyHost)
{
this.notifyHost = notifyHost;
this.notifyHost.PropertyChanged += NotifyHost_PropertyChanged;
}
private readonly Dictionary<string, PropertyAction> actionDict = new();

internal bool AddAction(string propertyName, Action<object> action)
{
Expand All @@ -24,17 +20,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 @@ -44,6 +40,25 @@ internal bool AddAction(string propertyName, Action<object> action)
return true;
}

private void Start()
{
if (NotifyHost != null)
{
this.NotifyHost.PropertyChanged += NotifyHost_PropertyChanged;
}
}

private void OnDestroy()
{
if (NotifyHost != null)
{
this.NotifyHost.PropertyChanged -= NotifyHost_PropertyChanged;
}

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
Expand All @@ -58,10 +73,6 @@ private void NotifyHost_PropertyChanged(object sender, PropertyChangedEventArgs
{
propertyAction.Invoke(sender);
}
else
{
Logger.Log.Warn($"PropertyChanged invoked for '{e.PropertyName}' on object of type '{sender.GetType().FullName}' but no such property is registered!");
}
}

private class PropertyAction
Expand Down
13 changes: 5 additions & 8 deletions BeatSaberMarkupLanguage/Parser/BSMLParserParams.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using BeatSaberMarkupLanguage.Components;
using UnityEngine;

namespace BeatSaberMarkupLanguage.Parser
{
public class BSMLParserParams
{
public object host;
public Dictionary<string, BSMLAction> actions = new();
public Dictionary<string, BSMLValue> values = new();

private readonly Dictionary<string, Action> events = new();
private readonly Dictionary<string, List<GameObject>> objectsWithTag = new();

internal BSMLParserParams(object host)
{
this.host = host;
this.NotifyUpdater = host is INotifyPropertyChanged notifyHost ? new NotifyUpdater(notifyHost) : null;
}

internal NotifyUpdater NotifyUpdater { get; }
public object host { get; }

public Dictionary<string, BSMLAction> actions { get; } = new();

public Dictionary<string, BSMLValue> values { get; } = new();

public void AddEvent(string ids, Action action)
{
Expand Down
32 changes: 28 additions & 4 deletions BeatSaberMarkupLanguage/TypeHandlers/TypeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,52 @@ public override void HandleType(ComponentTypeWithData componentType, BSMLParserP
{
if (componentType.component is T obj)
{
NotifyUpdater updater = null;
foreach (KeyValuePair<string, string> pair in componentType.data)
{
if (CachedSetters.TryGetValue(pair.Key, out Action<T, string> action))
{
action.Invoke(obj, pair.Value);
if (componentType.valueMap.TryGetValue(pair.Key, out BSMLValue value))
{
BindValue(componentType, parserParams, value, val => action.Invoke(obj, val.InvariantToString()));
updater = BindValue(componentType, parserParams, value, val => action.Invoke(obj, val.InvariantToString()), updater);
}
}
}
}
}

protected static void BindValue(ComponentTypeWithData componentType, BSMLParserParams parserParams, BSMLValue value, Action<object> onChange)
protected static NotifyUpdater GetOrCreateNotifyUpdater(ComponentTypeWithData componentType, BSMLParserParams parserParams)
{
NotifyUpdater notifyUpdater = parserParams.NotifyUpdater;
NotifyUpdater updater = null;

if (notifyUpdater != null && value is BSMLPropertyValue prop)
if (parserParams.host is System.ComponentModel.INotifyPropertyChanged notifyHost && !componentType.component.gameObject.TryGetComponent(out updater))
{
updater = componentType.component.gameObject.AddComponent<NotifyUpdater>();
updater.NotifyHost = notifyHost;
}

return updater;
}

protected static NotifyUpdater BindValue(ComponentTypeWithData componentType, BSMLParserParams parserParams, BSMLValue value, Action<object> onChange, NotifyUpdater notifyUpdater = null)
{
if (value is not BSMLPropertyValue prop)
{
return notifyUpdater;
}

if (notifyUpdater == null)
{
notifyUpdater = GetOrCreateNotifyUpdater(componentType, parserParams);
}

if (notifyUpdater != null)
{
notifyUpdater.AddAction(prop.PropertyInfo.Name, onChange);
}

return notifyUpdater;
}
}

Expand Down

0 comments on commit 6b2d876

Please sign in to comment.