Skip to content

Commit

Permalink
Add EnabledToggle as property for Monkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Nov 25, 2024
1 parent e79233f commit adc5d1a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
12 changes: 11 additions & 1 deletion MonkeyLoader/Meta/IMonkey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using MonkeyLoader.Patching;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace MonkeyLoader.Meta
{
Expand Down Expand Up @@ -43,11 +44,13 @@ public interface IMonkey : IRun, IShutdown, IComparable<IMonkey>, INestedIdentif

/// <summary>
/// Gets whether this monkey can be disabled, that is, whether it's
/// permitted to set <see cref="Enabled">Enabled</see> to <c>false.</c>
/// permitted to set <see cref="Enabled">Enabled</see> to <c>false</c>,
/// and there is an <see cref="EnabledToggle">EnabledToggle</see>.
/// </summary>
/// <value>
/// <c>true</c> if this monkey respects the <see cref="Mod.MonkeyToggles"/> config.
/// </value>
[MemberNotNullWhen(true, nameof(EnabledToggle))]
public bool CanBeDisabled { get; }

/// <summary>
Expand All @@ -64,6 +67,13 @@ public interface IMonkey : IRun, IShutdown, IComparable<IMonkey>, INestedIdentif
/// </remarks>
public bool Enabled { get; set; }

/// <summary>
/// Gets the this monkey's <see cref="MonkeyTogglesConfigSection.GetToggle">toggle</see>
/// if it <see cref="CanBeDisabled">can be disabled</see>.
/// </summary>
/// <value>The toggle config item if this monkey <see cref="CanBeDisabled">can be disabled</see>; otherwise, <c>null</c>.</value>
public IDefiningConfigKey<bool>? EnabledToggle { get; }

/// <summary>
/// Gets the impacts this (pre-)patcher has on certain features ordered by descending impact.
/// </summary>
Expand Down
24 changes: 12 additions & 12 deletions MonkeyLoader/Patching/MonkeyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public abstract class MonkeyBase : IMonkey
private readonly Lazy<string> _fullId;

private readonly Lazy<Harmony> _harmony;

private Mod _mod = null!;
private IDefiningConfigKey<bool>? _shouldBeEnabledKey;

/// <inheritdoc/>
public AssemblyName AssemblyName { get; }
Expand All @@ -31,7 +29,7 @@ public abstract class MonkeyBase : IMonkey
/// <remarks>
/// <i>By default</i>: <c>false</c>.
/// </remarks>
[MemberNotNullWhen(true, nameof(_shouldBeEnabledKey))]
[MemberNotNullWhen(true, nameof(EnabledToggle), nameof(EnabledToggle))]
public virtual bool CanBeDisabled => false;

/// <inheritdoc/>
Expand All @@ -40,21 +38,23 @@ public abstract class MonkeyBase : IMonkey
/// <inheritdoc/>
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!");
}
}

/// <inheritdoc/>
public IDefiningConfigKey<bool>? EnabledToggle { get; private set; }

/// <summary>
/// Gets whether this monkey's <see cref="Run">Run</see>() method failed when it was called.
/// </summary>
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit adc5d1a

Please sign in to comment.