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

Refactor Inspector / Event Handler Monkey classes #82

Merged
merged 8 commits into from
Dec 20, 2024
10 changes: 8 additions & 2 deletions MonkeyLoader.Resonite.Integration/ResoniteEventHandlerMonkeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ void ICancelableEventHandler<TEvent>.Handle(TEvent eventData)
/// Determines whether the given event should be handled
/// by this event handler based on its data.
/// </summary>
/// <remarks>
/// <i>By default:</i> Returns this monkey's <see cref="MonkeyBase{TMonkey}.Enabled">Enabled</see> state.
/// </remarks>
/// <param name="eventData">An object containing all the relevant information for the event.</param>
/// <returns><c>true</c> if this event handler applies to the event; otherwise, <c>false</c>.</returns>
protected abstract bool AppliesTo(TEvent eventData);
protected virtual bool AppliesTo(TEvent eventData) => Enabled;

/// <summary>
/// Handles the given event based on its data, if <see cref="AppliesTo">AppliesTo</see> returned <c>true</c>.
Expand Down Expand Up @@ -110,9 +113,12 @@ void IEventHandler<TEvent>.Handle(TEvent eventData)
/// Determines whether the given event should be handled
/// by this event handler based on its data.
/// </summary>
/// <remarks>
/// <i>By default:</i> Returns this monkey's <see cref="MonkeyBase{TMonkey}.Enabled">Enabled</see> state.
/// </remarks>
/// <param name="eventData">An object containing all the relevant information for the event.</param>
/// <returns><c>true</c> if this event handler applies to the event; otherwise, <c>false</c>.</returns>
protected abstract bool AppliesTo(TEvent eventData);
protected virtual bool AppliesTo(TEvent eventData) => Enabled;

/// <summary>
/// Handles the given event based on its data, if <see cref="AppliesTo">AppliesTo</see> returned <c>true</c>.
Expand Down
7 changes: 6 additions & 1 deletion MonkeyLoader.Resonite.Integration/UI/BuildInspectorEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FrooxEngine;
using System;
using MonkeyLoader.Resonite.Events;
using MonkeyLoader.Events;

namespace MonkeyLoader.Resonite.UI
{
Expand All @@ -27,8 +28,12 @@ internal BuildInspectorBodyEvent(UIBuilder ui, WorkerInspector inspector, Worker
}

/// <summary>
/// Represents the base class for the events fired during construction of a <see cref="Inspector"/>
/// Represents the base class for the events fired during construction of a <see cref="WorkerInspector"/>.
/// </summary>
/// <remarks>
/// This base class is dispatched as an event as well.
/// </remarks>
[DispatchableBaseEvent]
public abstract class BuildInspectorEvent : BuildUIEvent
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using MonkeyLoader.Configuration;
using FrooxEngine;
using MonkeyLoader.Configuration;
using MonkeyLoader.Resonite.Configuration;
using MonkeyLoader.Resonite.UI.Inspectors;
using System;

namespace MonkeyLoader.Resonite.UI
Expand All @@ -9,30 +11,35 @@ namespace MonkeyLoader.Resonite.UI
/// </summary>
public sealed class DefaultInspectorHeaderConfig : SingletonConfigSection<DefaultInspectorHeaderConfig>
{
private static readonly DefiningConfigKey<int> _destroyOffset = new("DestroyOffset", "The Order Offset of the Destroy button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 12, valueValidator: ValidateRange)
private readonly DefiningConfigKey<int> _destroyOffset = new("DestroyOffset", "The Order Offset of the Destroy button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 12)
{
new ConfigKeyRange<int>(0, 16),
new ConfigKeySessionShare<int, long>(IntToLong, LongToInt, 12)
OffsetRange,
MakeOffsetRangeShare(12)
};

private static readonly DefiningConfigKey<int> _duplicateOffset = new("DuplicateOffset", "The Order Offset of the Duplicate button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 11, valueValidator: ValidateRange)
private readonly DefiningConfigKey<int> _duplicateOffset = new("DuplicateOffset", "The Order Offset of the Duplicate button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 11)
{
new ConfigKeyRange<int>(0, 16),
new ConfigKeySessionShare<int, long>(IntToLong, LongToInt, 11)
OffsetRange,
MakeOffsetRangeShare(11)
};

private static readonly DefiningConfigKey<int> _openContainerOffset = new("OpenContainerOffset", "The Order Offset of the Open Container button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 4, valueValidator: ValidateRange)
private readonly DefiningConfigKey<int> _openContainerOffset = new("OpenContainerOffset", "The Order Offset of the Open Container button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 4)
{
new ConfigKeyRange<int>(0, 16),
new ConfigKeySessionShare<int, long>(IntToLong, LongToInt, 10)
OffsetRange,
MakeOffsetRangeShare(10)
};

private readonly DefiningConfigKey<int> _workerNameOffset = new("NameOffset", "The Order Offset of the Worker Name button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 6, valueValidator: ValidateRange)
private readonly DefiningConfigKey<int> _workerNameOffset = new("NameOffset", "The Order Offset of the Worker Name button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 6)
{
new ConfigKeyRange<int>(0, 16),
new ConfigKeySessionShare<int, long>(IntToLong, LongToInt, 6)
OffsetRange,
MakeOffsetRangeShare(6)
};

/// <summary>
/// Gets the range component used for the offset of <see cref="WorkerInspector"/> header items.
/// </summary>
public static ConfigKeyRange<int> OffsetRange { get; } = new ConfigKeyRange<int>(0, 16);

/// <inheritdoc/>
public override string Description => "Options for the default inspector header generation.";

Expand Down Expand Up @@ -62,11 +69,17 @@ public sealed class DefaultInspectorHeaderConfig : SingletonConfigSection<Defaul
/// </summary>
public ConfigKeySessionShare<int, long> WorkerNameOffset => _workerNameOffset.Components.Get<ConfigKeySessionShare<int, long>>();

/// <summary>
/// Makes a new session share component used for the offset of
/// <see cref="WorkerInspector"/> header items, optionally using the given default.
/// </summary>
/// <param name="defaultValue">The default value for the shared config item for users that don't have it themselves.</param>
/// <returns>The newly created share component with the optional default.</returns>
public static ConfigKeySessionShare<int, long> MakeOffsetRangeShare(int defaultValue = default)
=> new(IntToLong, LongToInt, defaultValue);

private static long IntToLong(int value) => value;

private static int LongToInt(long value) => (int)value;

private static bool ValidateRange(int value)
=> value is >= 0 and <= 16;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace MonkeyLoader.Resonite.UI.Inspectors
/// <summary>
/// Represents the base class for the events fired during construction of a <see cref="MemberEditor"/>
/// </summary>
/// <remarks>
/// This base class is dispatched as an event as well.
/// </remarks>
[DispatchableBaseEvent]
public abstract class BuildMemberEditorEvent : CancelableBuildUIEvent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using Elements.Core;
using FrooxEngine;
using FrooxEngine;
using FrooxEngine.UIX;
using HarmonyLib;
using MonkeyLoader.Events;
using MonkeyLoader.Patching;
using MonoMod.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace MonkeyLoader.Resonite.UI
namespace MonkeyLoader.Resonite.UI.Inspectors
{
[HarmonyPatchCategory(nameof(CustomInspectorInjector))]
[HarmonyPatch(typeof(WorkerInspector), nameof(WorkerInspector.BuildUIForComponent))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
using System.Text;
using System.Threading.Tasks;

namespace MonkeyLoader.Resonite.UI
namespace MonkeyLoader.Resonite.UI.Inspectors
{
internal sealed class DefaultInspectorHeaderHandler
: ConfiguredResoniteEventHandlerMonkey<DefaultInspectorHeaderHandler, DefaultInspectorHeaderConfig, BuildInspectorHeaderEvent>
{
public override int Priority => HarmonyLib.Priority.Normal;

protected override bool AppliesTo(BuildInspectorHeaderEvent eventData) => true;

protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => [];

protected override void Handle(BuildInspectorHeaderEvent eventData)
{
var ui = eventData.UI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@
using System.Text;
using System.Threading.Tasks;

namespace MonkeyLoader.Resonite.UI
namespace MonkeyLoader.Resonite.UI.Inspectors
{
internal sealed class OpenLinkedDynamicVariableSpace
: ResoniteInspectorMonkey<OpenLinkedDynamicVariableSpace, BuildInspectorHeaderEvent>
{
public override int Priority => HarmonyLib.Priority.First;

public override bool CanBeDisabled => true;
public override int Priority => HarmonyLib.Priority.First;

public OpenLinkedDynamicVariableSpace() : base(typeof(DynamicVariableBase<>))
{ }

protected override bool AppliesTo(BuildInspectorHeaderEvent eventData) => Enabled;

protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => [];

protected override void Handle(BuildInspectorHeaderEvent eventData)
{
if (Traverse.Create(eventData.Worker).Field("handler").Field("_currentSpace").GetValue() is not DynamicVariableSpace space)
Expand Down
15 changes: 11 additions & 4 deletions MonkeyLoader.Resonite.Integration/UI/ResoniteInspectorMonkeys.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FrooxEngine;
using MonkeyLoader.Events;
using MonkeyLoader.Patching;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -19,7 +20,7 @@ public abstract class ResoniteInspectorMonkey<TMonkey, TEvent> : ResoniteEventHa
where TMonkey : ResoniteInspectorMonkey<TMonkey, TEvent>, new()
where TEvent : BuildInspectorEvent
{
private readonly Dictionary<Type, bool> _matchCache = new();
private readonly Dictionary<Type, bool> _matchCache = [];

/// <summary>
/// Gets the <see cref="Type.GetGenericTypeDefinition">generic type definition</see> of the base type.
Expand All @@ -41,11 +42,15 @@ protected ResoniteInspectorMonkey(Type baseType)
}

/// <remarks>
/// Ensures that the worker given in the event derives from the <see cref="BaseType">base type</see>.
/// Ensures that this monkey is <see cref="MonkeyBase{T}.Enabled">enabled</see>
/// and that the worker given in the event derives from the <see cref="BaseType">base type</see>.
/// </remarks>
/// <inheritdoc/>
protected override bool AppliesTo(TEvent eventData)
{
if (!base.AppliesTo(eventData))
return false;

var type = eventData.Worker.GetType();

if (!_matchCache.TryGetValue(type, out var matches))
Expand Down Expand Up @@ -91,9 +96,11 @@ protected ResoniteInspectorMonkey()
{ }

/// <remarks>
/// Ensures that the worker given in the event is a <typeparamref name="TWorker"/>.
/// Ensures that this monkey is <see cref="MonkeyBase{T}.Enabled">enabled</see>
/// and that the worker given in the event is a <typeparamref name="TWorker"/>.
/// </remarks>
/// <inheritdoc/>
protected override bool AppliesTo(TEvent eventData) => eventData.Worker is TWorker;
protected override bool AppliesTo(TEvent eventData)
=> base.AppliesTo(eventData) && eventData.Worker is TWorker;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ internal sealed class ButtonDelegateTooltipResolver : ResoniteCancelableEventHan
public override int Priority => HarmonyLib.Priority.HigherThanNormal;
public override bool SkipCanceled => true;

protected override bool AppliesTo(ResolveTooltipLabelEvent eventData) => Enabled;

protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => [];

protected override void Handle(ResolveTooltipLabelEvent eventData)
{
ISyncDelegate pressed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out st
return label is not null;
}

/// <inheritdoc/>
protected override bool AppliesTo(ResolveTooltipLabelEvent eventData) => Enabled;

/// <inheritdoc/>
protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => [];

/// <inheritdoc/>
protected override void Handle(ResolveTooltipLabelEvent eventData)
{
Expand Down
Loading