From fd3bd6dc5b9c9db01360016e0b553b50505cb176 Mon Sep 17 00:00:00 2001 From: MidoriKami <9083275+MidoriKami@users.noreply.github.com> Date: Fri, 22 Sep 2023 12:17:54 -0700 Subject: [PATCH] Use abstract class instead of interface --- .../AddonEventManager/AddonEventManager.cs | 2 +- .../AddonLifecycle/AddonArgTypes/AddonArgs.cs | 46 +++++++++++++++++++ .../AddonArgTypes/AddonDrawArgs.cs | 9 ++-- .../AddonArgTypes/AddonFinalizeArgs.cs | 9 ++-- .../AddonArgTypes/AddonRefreshArgs.cs | 17 ++++--- .../AddonArgTypes/AddonRequestedUpdateArgs.cs | 9 ++-- .../AddonArgTypes/AddonSetupArgs.cs | 18 +++++--- .../AddonArgTypes/AddonUpdateArgs.cs | 9 ++-- Dalamud/Game/AddonLifecycle/AddonArgsType.cs | 2 +- Dalamud/Game/AddonLifecycle/AddonLifecycle.cs | 7 ++- Dalamud/Game/AddonLifecycle/IAddonArgs.cs | 25 ---------- Dalamud/Game/Gui/Dtr/DtrBar.cs | 4 +- .../AgingSteps/AddonLifecycleAgingStep.cs | 12 ++--- Dalamud/Plugin/Services/IAddonLifecycle.cs | 2 +- 14 files changed, 95 insertions(+), 76 deletions(-) create mode 100644 Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonArgs.cs delete mode 100644 Dalamud/Game/AddonLifecycle/IAddonArgs.cs diff --git a/Dalamud/Game/AddonEventManager/AddonEventManager.cs b/Dalamud/Game/AddonEventManager/AddonEventManager.cs index 730a7a404..89554074a 100644 --- a/Dalamud/Game/AddonEventManager/AddonEventManager.cs +++ b/Dalamud/Game/AddonEventManager/AddonEventManager.cs @@ -160,7 +160,7 @@ internal unsafe class AddonEventManager : IDisposable, IServiceType /// /// Event type that triggered this call. /// Addon that triggered this call. - 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; diff --git a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonArgs.cs b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonArgs.cs new file mode 100644 index 000000000..949d3fde9 --- /dev/null +++ b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonArgs.cs @@ -0,0 +1,46 @@ +using Dalamud.Memory; +using FFXIVClientStructs.FFXIV.Component.GUI; + +namespace Dalamud.Game.Addon; + +/// +/// Base class for AddonLifecycle AddonArgTypes. +/// +public abstract unsafe class AddonArgs +{ + /// + /// Constant string representing the name of an addon that is invalid. + /// + public const string InvalidAddon = "NullAddon"; + + private string? addonName; + + /// + /// Gets the name of the addon this args referrers to. + /// + public string AddonName => this.GetAddonName(); + + /// + /// Gets the pointer to the addons AtkUnitBase. + /// + public nint Addon { get; init; } + + /// + /// Gets the type of these args. + /// + public abstract AddonArgsType Type { get; } + + /// + /// Helper method for ensuring the name of the addon is valid. + /// + /// The name of the addon for this object. when invalid. + 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); + } +} diff --git a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonDrawArgs.cs b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonDrawArgs.cs index 614a7ac2a..d93001d1c 100644 --- a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonDrawArgs.cs +++ b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonDrawArgs.cs @@ -1,13 +1,10 @@ -namespace Dalamud.Game.Addon.AddonArgTypes; +namespace Dalamud.Game.Addon; /// /// Addon argument data for Finalize events. /// -public class AddonDrawArgs : IAddonArgs +public class AddonDrawArgs : AddonArgs { /// - public nint Addon { get; init; } - - /// - public AddonArgsType Type => AddonArgsType.Draw; + public override AddonArgsType Type => AddonArgsType.Draw; } diff --git a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonFinalizeArgs.cs b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonFinalizeArgs.cs index aa31fb051..ed7aa1b3c 100644 --- a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonFinalizeArgs.cs +++ b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonFinalizeArgs.cs @@ -1,13 +1,10 @@ -namespace Dalamud.Game.Addon.AddonArgTypes; +namespace Dalamud.Game.Addon; /// /// Addon argument data for Finalize events. /// -public class AddonFinalizeArgs : IAddonArgs +public class AddonFinalizeArgs : AddonArgs { /// - public nint Addon { get; init; } - - /// - public AddonArgsType Type => AddonArgsType.Finalize; + public override AddonArgsType Type => AddonArgsType.Finalize; } diff --git a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonRefreshArgs.cs b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonRefreshArgs.cs index ab4f37c3c..60ccaf8ea 100644 --- a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonRefreshArgs.cs +++ b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonRefreshArgs.cs @@ -1,15 +1,15 @@ -namespace Dalamud.Game.Addon.AddonArgTypes; +using System; +using FFXIVClientStructs.FFXIV.Component.GUI; + +namespace Dalamud.Game.Addon; /// /// Addon argument data for Finalize events. /// -public class AddonRefreshArgs : IAddonArgs +public class AddonRefreshArgs : AddonArgs { /// - public nint Addon { get; init; } - - /// - public AddonArgsType Type => AddonArgsType.Refresh; + public override AddonArgsType Type => AddonArgsType.Refresh; /// /// Gets the number of AtkValues. @@ -20,4 +20,9 @@ public class AddonRefreshArgs : IAddonArgs /// Gets the address of the AtkValue array. /// public nint AtkValues { get; init; } + + /// + /// Gets the AtkValues in the form of a span. + /// + public unsafe Span AtkValueSpan => new(this.AtkValues.ToPointer(), (int)this.AtkValueCount); } diff --git a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonRequestedUpdateArgs.cs b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonRequestedUpdateArgs.cs index dfd0dac5e..a31369aaf 100644 --- a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonRequestedUpdateArgs.cs +++ b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonRequestedUpdateArgs.cs @@ -1,15 +1,12 @@ -namespace Dalamud.Game.Addon.AddonArgTypes; +namespace Dalamud.Game.Addon; /// /// Addon argument data for Finalize events. /// -public class AddonRequestedUpdateArgs : IAddonArgs +public class AddonRequestedUpdateArgs : AddonArgs { /// - public nint Addon { get; init; } - - /// - public AddonArgsType Type => AddonArgsType.RequestedUpdate; + public override AddonArgsType Type => AddonArgsType.RequestedUpdate; /// /// Gets the NumberArrayData** for this event. diff --git a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonSetupArgs.cs b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonSetupArgs.cs index 4b467deb8..17c87967a 100644 --- a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonSetupArgs.cs +++ b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonSetupArgs.cs @@ -1,15 +1,16 @@ -namespace Dalamud.Game.Addon.AddonArgTypes; +using System; + +using FFXIVClientStructs.FFXIV.Component.GUI; + +namespace Dalamud.Game.Addon; /// /// Addon argument data for Setup events. /// -public class AddonSetupArgs : IAddonArgs +public class AddonSetupArgs : AddonArgs { /// - public nint Addon { get; init; } - - /// - public AddonArgsType Type => AddonArgsType.Setup; + public override AddonArgsType Type => AddonArgsType.Setup; /// /// Gets the number of AtkValues. @@ -20,4 +21,9 @@ public class AddonSetupArgs : IAddonArgs /// Gets the address of the AtkValue array. /// public nint AtkValues { get; init; } + + /// + /// Gets the AtkValues in the form of a span. + /// + public unsafe Span AtkValueSpan => new(this.AtkValues.ToPointer(), (int)this.AtkValueCount); } diff --git a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonUpdateArgs.cs b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonUpdateArgs.cs index ede588001..993883d77 100644 --- a/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonUpdateArgs.cs +++ b/Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonUpdateArgs.cs @@ -1,15 +1,12 @@ -namespace Dalamud.Game.Addon.AddonArgTypes; +namespace Dalamud.Game.Addon; /// /// Addon argument data for Finalize events. /// -public class AddonUpdateArgs : IAddonArgs +public class AddonUpdateArgs : AddonArgs { /// - public nint Addon { get; init; } - - /// - public AddonArgsType Type => AddonArgsType.Update; + public override AddonArgsType Type => AddonArgsType.Update; /// /// Gets the time since the last update. diff --git a/Dalamud/Game/AddonLifecycle/AddonArgsType.cs b/Dalamud/Game/AddonLifecycle/AddonArgsType.cs index ac325229d..8a07d445b 100644 --- a/Dalamud/Game/AddonLifecycle/AddonArgsType.cs +++ b/Dalamud/Game/AddonLifecycle/AddonArgsType.cs @@ -1,7 +1,7 @@ namespace Dalamud.Game.Addon; /// -/// Enumeration for available AddonLifecycle arg data +/// Enumeration for available AddonLifecycle arg data. /// public enum AddonArgsType { diff --git a/Dalamud/Game/AddonLifecycle/AddonLifecycle.cs b/Dalamud/Game/AddonLifecycle/AddonLifecycle.cs index 75b5b3753..17afbaeac 100644 --- a/Dalamud/Game/AddonLifecycle/AddonLifecycle.cs +++ b/Dalamud/Game/AddonLifecycle/AddonLifecycle.cs @@ -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, diff --git a/Dalamud/Game/AddonLifecycle/IAddonArgs.cs b/Dalamud/Game/AddonLifecycle/IAddonArgs.cs deleted file mode 100644 index ba77a2c6d..000000000 --- a/Dalamud/Game/AddonLifecycle/IAddonArgs.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Dalamud.Memory; -using FFXIVClientStructs.FFXIV.Component.GUI; - -namespace Dalamud.Game.Addon; - -/// -/// Interface representing the argument data for AddonLifecycle events. -/// -public unsafe interface IAddonArgs -{ - /// - /// Gets the name of the addon this args referrers to. - /// - string AddonName => this.Addon == nint.Zero ? "NullAddon" : MemoryHelper.ReadString((nint)((AtkUnitBase*)this.Addon)->Name, 0x20); - - /// - /// Gets the pointer to the addons AtkUnitBase. - /// - nint Addon { get; init; } - - /// - /// Gets the type of these args. - /// - AddonArgsType Type { get; } -} diff --git a/Dalamud/Game/Gui/Dtr/DtrBar.cs b/Dalamud/Game/Gui/Dtr/DtrBar.cs index 6b74e47cd..880bc0625 100644 --- a/Dalamud/Game/Gui/Dtr/DtrBar.cs +++ b/Dalamud/Game/Gui/Dtr/DtrBar.cs @@ -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; diff --git a/Dalamud/Interface/Internal/Windows/SelfTest/AgingSteps/AddonLifecycleAgingStep.cs b/Dalamud/Interface/Internal/Windows/SelfTest/AgingSteps/AddonLifecycleAgingStep.cs index 0821e62de..a9948430f 100644 --- a/Dalamud/Interface/Internal/Windows/SelfTest/AgingSteps/AddonLifecycleAgingStep.cs +++ b/Dalamud/Interface/Internal/Windows/SelfTest/AgingSteps/AddonLifecycleAgingStep.cs @@ -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++; } diff --git a/Dalamud/Plugin/Services/IAddonLifecycle.cs b/Dalamud/Plugin/Services/IAddonLifecycle.cs index 5290395ab..e89c57931 100644 --- a/Dalamud/Plugin/Services/IAddonLifecycle.cs +++ b/Dalamud/Plugin/Services/IAddonLifecycle.cs @@ -15,7 +15,7 @@ public interface IAddonLifecycle /// /// The event type that triggered the message. /// Information about what addon triggered the message. - public delegate void AddonEventDelegate(AddonEvent eventType, IAddonArgs args); + public delegate void AddonEventDelegate(AddonEvent eventType, AddonArgs args); /// /// Register a listener that will trigger on the specified event and any of the specified addons.