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 OnEnabled and OnDisabled hooks to MonkeyBase #24

Merged
merged 1 commit into from
Jul 7, 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
5 changes: 5 additions & 0 deletions MonkeyLoader/Configuration/ConfigKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class ConfigKey : IConfigKey
/// </summary>
public const string SetFromDefaultEventLabel = "Default";

/// <summary>
/// The event label used when a config item's value is set from getting loaded from a saved config.
/// </summary>
public const string SetFromLoadEventLabel = "Load";

/// <summary>
/// Gets the custom <see cref="IEqualityComparer{T}"/> for <see cref="IConfigKey"/>s.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion MonkeyLoader/Configuration/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ protected void DeserializeKey(IDefiningConfigKey key, JObject source, JsonSerial
return;

var value = token.ToObject(key.ValueType, jsonSerializer);
key.SetValue(value, "Load");
key.SetValue(value, ConfigKey.SetFromLoadEventLabel);
key.HasChanges = false;
}

Expand Down
2 changes: 1 addition & 1 deletion MonkeyLoader/MonkeyLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>MonkeyLoader</Title>
<Authors>Banane9</Authors>
<Version>0.17.5-beta</Version>
<Version>0.18.0-beta</Version>
<Description>A convenience and extendability focused mod loader using NuGet packages.</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
Expand Down
45 changes: 43 additions & 2 deletions MonkeyLoader/Patching/MonkeyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ internal set
_mod = value;
Logger = new Logger(_mod.Logger, Name);

if (CanBeDisabled)
_shouldBeEnabledKey = _mod.MonkeyToggles.GetToggle(this);
if (!CanBeDisabled)
return;

_shouldBeEnabledKey = _mod.MonkeyToggles.GetToggle(this);
_shouldBeEnabledKey.Changed += OnActiveStateChanged;
}
}

Expand Down Expand Up @@ -226,6 +229,26 @@ internal static TMonkey GetInstance<TMonkey>(Type type, Mod mod) where TMonkey :
/// </summary>
protected abstract IEnumerable<IFeaturePatch> GetFeaturePatches();

/// <summary>
/// Lets this monkey react to being disabled at runtime.<br/>
/// Will only ever be called when <see cref="CanBeDisabled">CanBeDisabled</see> is <c>true</c>.
/// </summary>
/// <remarks>
/// <i>By default:</i> does nothing.
/// </remarks>
protected virtual void OnDisabled()
{ }

/// <summary>
/// Lets this monkey react to being enabled at runtime.<br/>
/// Will only ever be called when <see cref="CanBeDisabled">CanBeDisabled</see> is <c>true</c>.
/// </summary>
/// <remarks>
/// <i>By default:</i> does nothing.
/// </remarks>
protected virtual void OnEnabled()
{ }

/// <summary>
/// Lets this monkey cleanup and shutdown.
/// </summary>
Expand Down Expand Up @@ -255,6 +278,24 @@ protected void ThrowIfRan()
throw new InvalidOperationException("A monkey's Run() method must only be called once!");
}

private void OnActiveStateChanged(object sender, ConfigKeyChangedEventArgs<bool> configKeyChangedEventArgs)
{
if (configKeyChangedEventArgs.Label is ConfigKey.SetFromLoadEventLabel or ConfigKey.SetFromDefaultEventLabel)
return;

try
{
if (configKeyChangedEventArgs.NewValue)
OnEnabled();
else
OnDisabled();
}
catch (Exception ex)
{
Logger.Error(() => ex.Format($"{(configKeyChangedEventArgs.NewValue ? nameof(OnEnabled) : nameof(OnDisabled))}() threw an Exception:"));
}
}

private void OnShutdownDone(bool applicationExiting)
{
try
Expand Down