From adc5d1a5799c28b0fd863b630982adf8ee80a2d1 Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Mon, 25 Nov 2024 20:37:50 +0100 Subject: [PATCH] Add EnabledToggle as property for Monkeys --- MonkeyLoader/Meta/IMonkey.cs | 12 +++++++++++- MonkeyLoader/Patching/MonkeyBase.cs | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/MonkeyLoader/Meta/IMonkey.cs b/MonkeyLoader/Meta/IMonkey.cs index 109a7fa..36be528 100644 --- a/MonkeyLoader/Meta/IMonkey.cs +++ b/MonkeyLoader/Meta/IMonkey.cs @@ -4,6 +4,7 @@ using MonkeyLoader.Patching; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace MonkeyLoader.Meta { @@ -43,11 +44,13 @@ public interface IMonkey : IRun, IShutdown, IComparable, INestedIdentif /// /// Gets whether this monkey can be disabled, that is, whether it's - /// permitted to set Enabled to false. + /// permitted to set Enabled to false, + /// and there is an EnabledToggle. /// /// /// true if this monkey respects the config. /// + [MemberNotNullWhen(true, nameof(EnabledToggle))] public bool CanBeDisabled { get; } /// @@ -64,6 +67,13 @@ public interface IMonkey : IRun, IShutdown, IComparable, INestedIdentif /// public bool Enabled { get; set; } + /// + /// Gets the this monkey's toggle + /// if it can be disabled. + /// + /// The toggle config item if this monkey can be disabled; otherwise, null. + public IDefiningConfigKey? EnabledToggle { get; } + /// /// Gets the impacts this (pre-)patcher has on certain features ordered by descending impact. /// diff --git a/MonkeyLoader/Patching/MonkeyBase.cs b/MonkeyLoader/Patching/MonkeyBase.cs index 5783049..ff8f0e2 100644 --- a/MonkeyLoader/Patching/MonkeyBase.cs +++ b/MonkeyLoader/Patching/MonkeyBase.cs @@ -20,9 +20,7 @@ public abstract class MonkeyBase : IMonkey private readonly Lazy _fullId; private readonly Lazy _harmony; - private Mod _mod = null!; - private IDefiningConfigKey? _shouldBeEnabledKey; /// public AssemblyName AssemblyName { get; } @@ -31,7 +29,7 @@ public abstract class MonkeyBase : IMonkey /// /// By default: false. /// - [MemberNotNullWhen(true, nameof(_shouldBeEnabledKey))] + [MemberNotNullWhen(true, nameof(EnabledToggle), nameof(EnabledToggle))] public virtual bool CanBeDisabled => false; /// @@ -40,21 +38,23 @@ public abstract class MonkeyBase : IMonkey /// public bool Enabled { - get => !CanBeDisabled || _shouldBeEnabledKey.GetValue(); + get => !CanBeDisabled || EnabledToggle.GetValue(); set { - if (!CanBeDisabled) + if (CanBeDisabled) { - if (!value) - throw new NotSupportedException("This monkey can't be disabled!"); - else - return; + EnabledToggle.SetValue(value, "SetMonkeyEnabled"); + return; } - _shouldBeEnabledKey.SetValue(value, "SetMonkeyEnabled"); + if (!value) + throw new NotSupportedException("This monkey can't be disabled!"); } } + /// + public IDefiningConfigKey? EnabledToggle { get; private set; } + /// /// Gets whether this monkey's Run() method failed when it was called. /// @@ -109,8 +109,8 @@ internal set if (!CanBeDisabled) return; - _shouldBeEnabledKey = _mod.MonkeyToggles.GetToggle(this); - _shouldBeEnabledKey.Changed += OnActiveStateChanged; + EnabledToggle = _mod.MonkeyToggles.GetToggle(this); + EnabledToggle.Changed += OnActiveStateChanged; } }