Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EnabledToggle as property for Monkeys #49

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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