Use abstract class instead of interface

This commit is contained in:
MidoriKami 2023-09-22 12:17:54 -07:00
parent bd81d23097
commit fd3bd6dc5b
14 changed files with 95 additions and 76 deletions

View file

@ -160,7 +160,7 @@ internal unsafe class AddonEventManager : IDisposable, IServiceType
/// </summary>
/// <param name="eventType">Event type that triggered this call.</param>
/// <param name="addonInfo">Addon that triggered this call.</param>
private void OnAddonFinalize(AddonEvent eventType, IAddonArgs addonInfo)
private void OnAddonFinalize(AddonEvent eventType, AddonArgs addonInfo)
{
// It shouldn't be possible for this event to be anything other than PreFinalize.
if (eventType != AddonEvent.PreFinalize) return;

View file

@ -0,0 +1,46 @@
using Dalamud.Memory;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Dalamud.Game.Addon;
/// <summary>
/// Base class for AddonLifecycle AddonArgTypes.
/// </summary>
public abstract unsafe class AddonArgs
{
/// <summary>
/// Constant string representing the name of an addon that is invalid.
/// </summary>
public const string InvalidAddon = "NullAddon";
private string? addonName;
/// <summary>
/// Gets the name of the addon this args referrers to.
/// </summary>
public string AddonName => this.GetAddonName();
/// <summary>
/// Gets the pointer to the addons AtkUnitBase.
/// </summary>
public nint Addon { get; init; }
/// <summary>
/// Gets the type of these args.
/// </summary>
public abstract AddonArgsType Type { get; }
/// <summary>
/// Helper method for ensuring the name of the addon is valid.
/// </summary>
/// <returns>The name of the addon for this object. <see cref="InvalidAddon"/> when invalid.</returns>
private string GetAddonName()
{
if (this.Addon == nint.Zero) return InvalidAddon;
var addonPointer = (AtkUnitBase*)this.Addon;
if (addonPointer->Name is null) return InvalidAddon;
return this.addonName ??= MemoryHelper.ReadString((nint)addonPointer->Name, 0x20);
}
}

View file

@ -1,13 +1,10 @@
namespace Dalamud.Game.Addon.AddonArgTypes;
namespace Dalamud.Game.Addon;
/// <summary>
/// Addon argument data for Finalize events.
/// </summary>
public class AddonDrawArgs : IAddonArgs
public class AddonDrawArgs : AddonArgs
{
/// <inheritdoc/>
public nint Addon { get; init; }
/// <inheritdoc/>
public AddonArgsType Type => AddonArgsType.Draw;
public override AddonArgsType Type => AddonArgsType.Draw;
}

View file

@ -1,13 +1,10 @@
namespace Dalamud.Game.Addon.AddonArgTypes;
namespace Dalamud.Game.Addon;
/// <summary>
/// Addon argument data for Finalize events.
/// </summary>
public class AddonFinalizeArgs : IAddonArgs
public class AddonFinalizeArgs : AddonArgs
{
/// <inheritdoc/>
public nint Addon { get; init; }
/// <inheritdoc/>
public AddonArgsType Type => AddonArgsType.Finalize;
public override AddonArgsType Type => AddonArgsType.Finalize;
}

View file

@ -1,15 +1,15 @@
namespace Dalamud.Game.Addon.AddonArgTypes;
using System;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Dalamud.Game.Addon;
/// <summary>
/// Addon argument data for Finalize events.
/// </summary>
public class AddonRefreshArgs : IAddonArgs
public class AddonRefreshArgs : AddonArgs
{
/// <inheritdoc/>
public nint Addon { get; init; }
/// <inheritdoc/>
public AddonArgsType Type => AddonArgsType.Refresh;
public override AddonArgsType Type => AddonArgsType.Refresh;
/// <summary>
/// Gets the number of AtkValues.
@ -20,4 +20,9 @@ public class AddonRefreshArgs : IAddonArgs
/// Gets the address of the AtkValue array.
/// </summary>
public nint AtkValues { get; init; }
/// <summary>
/// Gets the AtkValues in the form of a span.
/// </summary>
public unsafe Span<AtkValue> AtkValueSpan => new(this.AtkValues.ToPointer(), (int)this.AtkValueCount);
}

View file

@ -1,15 +1,12 @@
namespace Dalamud.Game.Addon.AddonArgTypes;
namespace Dalamud.Game.Addon;
/// <summary>
/// Addon argument data for Finalize events.
/// </summary>
public class AddonRequestedUpdateArgs : IAddonArgs
public class AddonRequestedUpdateArgs : AddonArgs
{
/// <inheritdoc/>
public nint Addon { get; init; }
/// <inheritdoc/>
public AddonArgsType Type => AddonArgsType.RequestedUpdate;
public override AddonArgsType Type => AddonArgsType.RequestedUpdate;
/// <summary>
/// Gets the NumberArrayData** for this event.

View file

@ -1,15 +1,16 @@
namespace Dalamud.Game.Addon.AddonArgTypes;
using System;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Dalamud.Game.Addon;
/// <summary>
/// Addon argument data for Setup events.
/// </summary>
public class AddonSetupArgs : IAddonArgs
public class AddonSetupArgs : AddonArgs
{
/// <inheritdoc/>
public nint Addon { get; init; }
/// <inheritdoc/>
public AddonArgsType Type => AddonArgsType.Setup;
public override AddonArgsType Type => AddonArgsType.Setup;
/// <summary>
/// Gets the number of AtkValues.
@ -20,4 +21,9 @@ public class AddonSetupArgs : IAddonArgs
/// Gets the address of the AtkValue array.
/// </summary>
public nint AtkValues { get; init; }
/// <summary>
/// Gets the AtkValues in the form of a span.
/// </summary>
public unsafe Span<AtkValue> AtkValueSpan => new(this.AtkValues.ToPointer(), (int)this.AtkValueCount);
}

View file

@ -1,15 +1,12 @@
namespace Dalamud.Game.Addon.AddonArgTypes;
namespace Dalamud.Game.Addon;
/// <summary>
/// Addon argument data for Finalize events.
/// </summary>
public class AddonUpdateArgs : IAddonArgs
public class AddonUpdateArgs : AddonArgs
{
/// <inheritdoc/>
public nint Addon { get; init; }
/// <inheritdoc/>
public AddonArgsType Type => AddonArgsType.Update;
public override AddonArgsType Type => AddonArgsType.Update;
/// <summary>
/// Gets the time since the last update.

View file

@ -1,7 +1,7 @@
namespace Dalamud.Game.Addon;
/// <summary>
/// Enumeration for available AddonLifecycle arg data
/// Enumeration for available AddonLifecycle arg data.
/// </summary>
public enum AddonArgsType
{

View file

@ -3,7 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Dalamud.Game.Addon.AddonArgTypes;
using Dalamud.Hooking;
using Dalamud.Hooking.Internal;
using Dalamud.IoC;
@ -128,7 +127,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
this.onAddonRequestedUpdateHook.Enable();
}
private void InvokeListeners(AddonEvent eventType, IAddonArgs args)
private void InvokeListeners(AddonEvent eventType, AddonArgs args)
{
// Match on string.empty for listeners that want events for all addons.
foreach (var listener in this.eventListeners.Where(listener => listener.EventType == eventType && (listener.AddonName == args.AddonName || listener.AddonName == string.Empty)))
@ -141,7 +140,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
{
try
{
this.InvokeListeners(AddonEvent.PreSetup, new AddonSetupArgs()
this.InvokeListeners(AddonEvent.PreSetup, new AddonSetupArgs
{
Addon = (nint)addon,
AtkValueCount = valueCount,
@ -157,7 +156,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
try
{
this.InvokeListeners(AddonEvent.PostSetup, new AddonSetupArgs()
this.InvokeListeners(AddonEvent.PostSetup, new AddonSetupArgs
{
Addon = (nint)addon,
AtkValueCount = valueCount,

View file

@ -1,25 +0,0 @@
using Dalamud.Memory;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Dalamud.Game.Addon;
/// <summary>
/// Interface representing the argument data for AddonLifecycle events.
/// </summary>
public unsafe interface IAddonArgs
{
/// <summary>
/// Gets the name of the addon this args referrers to.
/// </summary>
string AddonName => this.Addon == nint.Zero ? "NullAddon" : MemoryHelper.ReadString((nint)((AtkUnitBase*)this.Addon)->Name, 0x20);
/// <summary>
/// Gets the pointer to the addons AtkUnitBase.
/// </summary>
nint Addon { get; init; }
/// <summary>
/// Gets the type of these args.
/// </summary>
AddonArgsType Type { get; }
}

View file

@ -255,7 +255,7 @@ public sealed unsafe class DtrBar : IDisposable, IServiceType, IDtrBar
}
}
private void OnDtrPostDraw(AddonEvent eventType, IAddonArgs addonInfo)
private void OnDtrPostDraw(AddonEvent eventType, AddonArgs addonInfo)
{
var addon = (AtkUnitBase*)addonInfo.Addon;
@ -300,7 +300,7 @@ public sealed unsafe class DtrBar : IDisposable, IServiceType, IDtrBar
}
}
private void OnAddonRequestedUpdateDetour(AddonEvent eventType, IAddonArgs addonInfo)
private void OnAddonRequestedUpdateDetour(AddonEvent eventType, AddonArgs addonInfo)
{
var addon = (AtkUnitBase*)addonInfo.Addon;

View file

@ -100,32 +100,32 @@ internal class AddonLifecycleAgingStep : IAgingStep
}
}
private void PostSetup(AddonEvent eventType, IAddonArgs addonInfo)
private void PostSetup(AddonEvent eventType, AddonArgs addonInfo)
{
if (this.currentStep is TestStep.CharacterSetup) this.currentStep++;
}
private void PostUpdate(AddonEvent eventType, IAddonArgs addonInfo)
private void PostUpdate(AddonEvent eventType, AddonArgs addonInfo)
{
if (this.currentStep is TestStep.CharacterUpdate) this.currentStep++;
}
private void PostDraw(AddonEvent eventType, IAddonArgs addonInfo)
private void PostDraw(AddonEvent eventType, AddonArgs addonInfo)
{
if (this.currentStep is TestStep.CharacterDraw) this.currentStep++;
}
private void PostRefresh(AddonEvent eventType, IAddonArgs addonInfo)
private void PostRefresh(AddonEvent eventType, AddonArgs addonInfo)
{
if (this.currentStep is TestStep.CharacterRefresh) this.currentStep++;
}
private void PostRequestedUpdate(AddonEvent eventType, IAddonArgs addonInfo)
private void PostRequestedUpdate(AddonEvent eventType, AddonArgs addonInfo)
{
if (this.currentStep is TestStep.CharacterRequestedUpdate) this.currentStep++;
}
private void PreFinalize(AddonEvent eventType, IAddonArgs addonInfo)
private void PreFinalize(AddonEvent eventType, AddonArgs addonInfo)
{
if (this.currentStep is TestStep.CharacterFinalize) this.currentStep++;
}

View file

@ -15,7 +15,7 @@ public interface IAddonLifecycle
/// </summary>
/// <param name="eventType">The event type that triggered the message.</param>
/// <param name="args">Information about what addon triggered the message.</param>
public delegate void AddonEventDelegate(AddonEvent eventType, IAddonArgs args);
public delegate void AddonEventDelegate(AddonEvent eventType, AddonArgs args);
/// <summary>
/// Register a listener that will trigger on the specified event and any of the specified addons.