From e2c1c186c3a8cf19cce553070fab05159519bd69 Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Mon, 23 Aug 2021 18:31:26 +1000 Subject: [PATCH] Add wrapper to handle LemonAction issue --- .../MelonLoader/MelonLoaderConfigHandler.cs | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Loader/MelonLoader/MelonLoaderConfigHandler.cs b/src/Loader/MelonLoader/MelonLoaderConfigHandler.cs index 5071f6b..d11b4f4 100644 --- a/src/Loader/MelonLoader/MelonLoaderConfigHandler.cs +++ b/src/Loader/MelonLoader/MelonLoaderConfigHandler.cs @@ -34,17 +34,34 @@ namespace UnityExplorer.Loader.ML } } - public override void RegisterConfigElement(ConfigElement config) + // This wrapper exists to handle the arbitrary "LemonAction" delegates which ML now uses in 0.4.4+. + // Reflection is required since the delegate type changed between 0.4.3 and 0.4.4. + // A wrapper class is required to link the MelonPreferences_Entry and the delegate instance. + public class EntryDelegateWrapper { - var entry = prefCategory.CreateEntry(config.Name, config.Value, null, config.Description, config.IsInternal, false); - - entry.OnValueChangedUntyped += () => + public MelonPreferences_Entry entry; + public ConfigElement config; + + public EntryDelegateWrapper(MelonPreferences_Entry entry, ConfigElement config) + { + this.entry = entry; + this.config = config; + var evt = entry.GetType().GetEvent("OnValueChangedUntyped"); + evt.AddEventHandler(entry, Delegate.CreateDelegate(evt.EventHandlerType, this, GetType().GetMethod("OnChanged"))); + } + + public void OnChanged() { if ((entry.Value == null && config.Value == null) || config.Value.Equals(entry.Value)) return; - config.Value = entry.Value; - }; + } + } + + public override void RegisterConfigElement(ConfigElement config) + { + var entry = prefCategory.CreateEntry(config.Name, config.Value, null, config.Description, config.IsInternal, false); + new EntryDelegateWrapper(entry, config); } public override void SetConfigValue(ConfigElement config, T value)