Skip to content

Commit

Permalink
Use CallHook for AddonSetup
Browse files Browse the repository at this point in the history
  • Loading branch information
MidoriKami committed Sep 22, 2023
1 parent 0636a03 commit bd81d23
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
10 changes: 10 additions & 0 deletions Dalamud/Game/AddonLifecycle/AddonArgTypes/AddonSetupArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ public class AddonSetupArgs : IAddonArgs

/// <inheritdoc/>
public AddonArgsType Type => AddonArgsType.Setup;

/// <summary>
/// Gets the number of AtkValues.
/// </summary>
public uint AtkValueCount { get; init; }

/// <summary>
/// Gets the address of the AtkValue array.
/// </summary>
public nint AtkValues { get; init; }
}
26 changes: 17 additions & 9 deletions Dalamud/Game/AddonLifecycle/AddonLifecycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
private readonly Framework framework = Service<Framework>.Get();

private readonly AddonLifecycleAddressResolver address;
private readonly Hook<AddonSetupDelegate> onAddonSetupHook;
private readonly CallHook<AddonSetupDelegate> onAddonSetupHook;
private readonly Hook<AddonFinalizeDelegate> onAddonFinalizeHook;
private readonly CallHook<AddonDrawDelegate> onAddonDrawHook;
private readonly CallHook<AddonUpdateDelegate> onAddonUpdateHook;
Expand All @@ -46,15 +46,15 @@ private AddonLifecycle(SigScanner sigScanner)

this.framework.Update += this.OnFrameworkUpdate;

this.onAddonSetupHook = Hook<AddonSetupDelegate>.FromAddress(this.address.AddonSetup, this.OnAddonSetup);
this.onAddonSetupHook = new CallHook<AddonSetupDelegate>(this.address.AddonSetup, this.OnAddonSetup);
this.onAddonFinalizeHook = Hook<AddonFinalizeDelegate>.FromAddress(this.address.AddonFinalize, this.OnAddonFinalize);
this.onAddonDrawHook = new CallHook<AddonDrawDelegate>(this.address.AddonDraw, this.OnAddonDraw);
this.onAddonUpdateHook = new CallHook<AddonUpdateDelegate>(this.address.AddonUpdate, this.OnAddonUpdate);
this.onAddonRefreshHook = Hook<AddonOnRefreshDelegate>.FromAddress(this.address.AddonOnRefresh, this.OnAddonRefresh);
this.onAddonRequestedUpdateHook = new CallHook<AddonOnRequestedUpdateDelegate>(this.address.AddonOnRequestedUpdate, this.OnRequestedUpdate);
}

private delegate nint AddonSetupDelegate(AtkUnitBase* addon);
private delegate void AddonSetupDelegate(AtkUnitBase* addon, uint valueCount, AtkValue* values);

private delegate void AddonFinalizeDelegate(AtkUnitManager* unitManager, AtkUnitBase** atkUnitBase);

Expand Down Expand Up @@ -137,29 +137,37 @@ private void InvokeListeners(AddonEvent eventType, IAddonArgs args)
}
}

private nint OnAddonSetup(AtkUnitBase* addon)
private void OnAddonSetup(AtkUnitBase* addon, uint valueCount, AtkValue* values)
{
try
{
this.InvokeListeners(AddonEvent.PreSetup, new AddonSetupArgs { Addon = (nint)addon });
this.InvokeListeners(AddonEvent.PreSetup, new AddonSetupArgs()
{
Addon = (nint)addon,
AtkValueCount = valueCount,
AtkValues = (nint)values,
});
}
catch (Exception e)
{
Log.Error(e, "Exception in OnAddonSetup pre-setup invoke.");
}

var result = this.onAddonSetupHook.Original(addon);
addon->OnSetup(valueCount, values);

try
{
this.InvokeListeners(AddonEvent.PostSetup, new AddonSetupArgs { Addon = (nint)addon });
this.InvokeListeners(AddonEvent.PostSetup, new AddonSetupArgs()
{
Addon = (nint)addon,
AtkValueCount = valueCount,
AtkValues = (nint)values,
});
}
catch (Exception e)
{
Log.Error(e, "Exception in OnAddonSetup post-setup invoke.");
}

return result;
}

private void OnAddonFinalize(AtkUnitManager* unitManager, AtkUnitBase** atkUnitBase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class AddonLifecycleAddressResolver : BaseAddressResolver
/// <param name="sig">The signature scanner to facilitate setup.</param>
protected override void Setup64Bit(SigScanner sig)
{
this.AddonSetup = sig.ScanText("E8 ?? ?? ?? ?? 8B 83 ?? ?? ?? ?? C1 E8 14");
this.AddonSetup = sig.ScanText("FF 90 ?? ?? ?? ?? 48 8B 93 ?? ?? ?? ?? 80 8B");
this.AddonFinalize = sig.ScanText("E8 ?? ?? ?? ?? 48 8B 7C 24 ?? 41 8B C6");
this.AddonDraw = sig.ScanText("FF 90 ?? ?? ?? ?? 83 EB 01 79 C1");
this.AddonUpdate = sig.ScanText("FF 90 ?? ?? ?? ?? 40 88 AF");
Expand Down

0 comments on commit bd81d23

Please sign in to comment.