Move AddonArgs to it's own file

This commit is contained in:
MidoriKami 2023-09-10 19:26:28 -07:00
parent c9a5c7c4c5
commit ca58a1bf4f
3 changed files with 34 additions and 32 deletions

View file

@ -0,0 +1,22 @@
using Dalamud.Memory;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Dalamud.Game.AddonLifecycle;
/// <summary>
/// Addon argument data for use in event subscribers.
/// </summary>
public unsafe class AddonArgs
{
private string? addonName;
/// <summary>
/// Gets the name of the addon this args referrers to.
/// </summary>
public string AddonName => this.Addon == nint.Zero ? "NullAddon" : this.addonName ??= MemoryHelper.ReadString((nint)((AtkUnitBase*)this.Addon)->Name, 0x20);
/// <summary>
/// Gets the pointer to the addons AtkUnitBase.
/// </summary>
required public nint Addon { get; init; }
}

View file

@ -127,7 +127,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
this.onAddonRequestedUpdateHook.Enable(); this.onAddonRequestedUpdateHook.Enable();
} }
private void InvokeListeners(AddonEvent eventType, IAddonLifecycle.AddonArgs args) private void InvokeListeners(AddonEvent eventType, AddonArgs args)
{ {
// Match on string.empty for listeners that want events for all addons. // 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))) foreach (var listener in this.eventListeners.Where(listener => listener.EventType == eventType && (listener.AddonName == args.AddonName || listener.AddonName == string.Empty)))
@ -140,7 +140,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
{ {
try try
{ {
this.InvokeListeners(AddonEvent.PreSetup, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PreSetup, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -151,7 +151,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
try try
{ {
this.InvokeListeners(AddonEvent.PostSetup, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PostSetup, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -165,7 +165,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
{ {
try try
{ {
this.InvokeListeners(AddonEvent.PreFinalize, new IAddonLifecycle.AddonArgs { Addon = (nint)atkUnitBase[0] }); this.InvokeListeners(AddonEvent.PreFinalize, new AddonArgs { Addon = (nint)atkUnitBase[0] });
} }
catch (Exception e) catch (Exception e)
{ {
@ -179,7 +179,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
{ {
try try
{ {
this.InvokeListeners(AddonEvent.PreDraw, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PreDraw, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -190,7 +190,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
try try
{ {
this.InvokeListeners(AddonEvent.PostDraw, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PostDraw, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -202,7 +202,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
{ {
try try
{ {
this.InvokeListeners(AddonEvent.PreUpdate, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PreUpdate, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -213,7 +213,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
try try
{ {
this.InvokeListeners(AddonEvent.PostUpdate, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PostUpdate, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -225,7 +225,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
{ {
try try
{ {
this.InvokeListeners(AddonEvent.PreRefresh, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PreRefresh, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -236,7 +236,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
try try
{ {
this.InvokeListeners(AddonEvent.PostRefresh, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PostRefresh, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -248,7 +248,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
{ {
try try
{ {
this.InvokeListeners(AddonEvent.PreRequestedUpdate, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PreRequestedUpdate, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {
@ -259,7 +259,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
try try
{ {
this.InvokeListeners(AddonEvent.PostRequestedUpdate, new IAddonLifecycle.AddonArgs { Addon = (nint)addon }); this.InvokeListeners(AddonEvent.PostRequestedUpdate, new AddonArgs { Addon = (nint)addon });
} }
catch (Exception e) catch (Exception e)
{ {

View file

@ -2,8 +2,6 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Dalamud.Game.AddonLifecycle; using Dalamud.Game.AddonLifecycle;
using Dalamud.Memory;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Dalamud.Plugin.Services; namespace Dalamud.Plugin.Services;
@ -79,22 +77,4 @@ public interface IAddonLifecycle
/// </summary> /// </summary>
/// <param name="handlers">Handlers to remove.</param> /// <param name="handlers">Handlers to remove.</param>
void UnregisterListener(params AddonEventDelegate[] handlers); void UnregisterListener(params AddonEventDelegate[] handlers);
/// <summary>
/// Addon argument data for use in event subscribers.
/// </summary>
public unsafe class AddonArgs
{
private string? addonName;
/// <summary>
/// Gets the name of the addon this args referrers to.
/// </summary>
public string AddonName => this.Addon == nint.Zero ? "NullAddon" : this.addonName ??= MemoryHelper.ReadString((nint)((AtkUnitBase*)this.Addon)->Name, 0x20);
/// <summary>
/// Gets the pointer to the addons AtkUnitBase.
/// </summary>
required public nint Addon { get; init; }
}
} }