Add AddonOnRequestedUpdate

This commit is contained in:
MidoriKami 2023-09-07 11:47:35 -07:00
parent 8cb76a7438
commit 6a0401646f
3 changed files with 47 additions and 0 deletions

View file

@ -39,4 +39,14 @@ public enum AddonEvent
/// Event that is fired before an addon is finalized. /// Event that is fired before an addon is finalized.
/// </summary> /// </summary>
PreFinalize, PreFinalize,
/// <summary>
/// Event that is fired before an addon begins a requested update.
/// </summary>
PreRequestedUpdate,
/// <summary>
/// Event that is fired after an addon finishes a requested update.
/// </summary>
PostRequestedUpdate,
} }

View file

@ -30,6 +30,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
private readonly Hook<AddonFinalizeDelegate> onAddonFinalizeHook; private readonly Hook<AddonFinalizeDelegate> onAddonFinalizeHook;
private readonly CallHook<AddonDrawDelegate> onAddonDrawHook; private readonly CallHook<AddonDrawDelegate> onAddonDrawHook;
private readonly CallHook<AddonUpdateDelegate> onAddonUpdateHook; private readonly CallHook<AddonUpdateDelegate> onAddonUpdateHook;
// private readonly CallHook<AddonOnRequestedUpdate> onAddonRequestedUpdateHook; // See Note in Ctor
private readonly ConcurrentBag<AddonLifecycleEventListener> newEventListeners = new(); private readonly ConcurrentBag<AddonLifecycleEventListener> newEventListeners = new();
private readonly ConcurrentBag<AddonLifecycleEventListener> removeEventListeners = new(); private readonly ConcurrentBag<AddonLifecycleEventListener> removeEventListeners = new();
@ -47,6 +48,9 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
this.onAddonFinalizeHook = Hook<AddonFinalizeDelegate>.FromAddress(this.address.AddonFinalize, this.OnAddonFinalize); this.onAddonFinalizeHook = Hook<AddonFinalizeDelegate>.FromAddress(this.address.AddonFinalize, this.OnAddonFinalize);
this.onAddonDrawHook = new CallHook<AddonDrawDelegate>(this.address.AddonDraw, this.OnAddonDraw); this.onAddonDrawHook = new CallHook<AddonDrawDelegate>(this.address.AddonDraw, this.OnAddonDraw);
this.onAddonUpdateHook = new CallHook<AddonUpdateDelegate>(this.address.AddonUpdate, this.OnAddonUpdate); this.onAddonUpdateHook = new CallHook<AddonUpdateDelegate>(this.address.AddonUpdate, this.OnAddonUpdate);
// todo: reenable this. WARNING: This hook overwrites a system that SimpleTweaks uses, causing SimpleTweaks to report exceptions.
// this.onAddonRequestedUpdateHook = new CallHook<AddonOnRequestedUpdate>(this.address.AddonOnRequestedUpdate, this.OnRequestedUpdate);
} }
private delegate nint AddonSetupDelegate(AtkUnitBase* addon); private delegate nint AddonSetupDelegate(AtkUnitBase* addon);
@ -57,6 +61,8 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
private delegate void AddonUpdateDelegate(AtkUnitBase* addon, float delta); private delegate void AddonUpdateDelegate(AtkUnitBase* addon, float delta);
private delegate void AddonOnRequestedUpdate(AtkUnitBase* addon, NumberArrayData** numberArrayData, StringArrayData** stringArrayData);
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() public void Dispose()
{ {
@ -66,6 +72,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
this.onAddonFinalizeHook.Dispose(); this.onAddonFinalizeHook.Dispose();
this.onAddonDrawHook.Dispose(); this.onAddonDrawHook.Dispose();
this.onAddonUpdateHook.Dispose(); this.onAddonUpdateHook.Dispose();
// this.onAddonRequestedUpdateHook.Dispose(); // See Note in Ctor
} }
/// <summary> /// <summary>
@ -113,6 +120,7 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
this.onAddonFinalizeHook.Enable(); this.onAddonFinalizeHook.Enable();
this.onAddonDrawHook.Enable(); this.onAddonDrawHook.Enable();
this.onAddonUpdateHook.Enable(); this.onAddonUpdateHook.Enable();
// this.onAddonRequestedUpdateHook.Enable(); // See Note in Ctor
} }
private void InvokeListeners(AddonEvent eventType, IAddonLifecycle.AddonArgs args) private void InvokeListeners(AddonEvent eventType, IAddonLifecycle.AddonArgs args)
@ -217,6 +225,29 @@ internal unsafe class AddonLifecycle : IDisposable, IServiceType
Log.Error(e, "Exception in OnAddonUpdate post-update invoke."); Log.Error(e, "Exception in OnAddonUpdate post-update invoke.");
} }
} }
private void OnRequestedUpdate(AtkUnitBase* addon, NumberArrayData** numberArrayData, StringArrayData** stringArrayData)
{
try
{
this.InvokeListeners(AddonEvent.PreRequestedUpdate, new IAddonLifecycle.AddonArgs { Addon = (nint)addon });
}
catch (Exception e)
{
Log.Error(e, "Exception in OnRequestedUpdate pre-requestedUpdate invoke.");
}
addon->OnUpdate(numberArrayData, stringArrayData);
try
{
this.InvokeListeners(AddonEvent.PostRequestedUpdate, new IAddonLifecycle.AddonArgs { Addon = (nint)addon });
}
catch (Exception e)
{
Log.Error(e, "Exception in OnRequestedUpdate post-requestedUpdate invoke.");
}
}
} }
/// <summary> /// <summary>

View file

@ -24,6 +24,11 @@ internal class AddonLifecycleAddressResolver : BaseAddressResolver
/// Gets the address of the addon update hook invoked by virtual function call. /// Gets the address of the addon update hook invoked by virtual function call.
/// </summary> /// </summary>
public nint AddonUpdate { get; private set; } public nint AddonUpdate { get; private set; }
/// <summary>
/// Gets the address of the addon onRequestedUpdate hook invoked by virtual function call.
/// </summary>
public nint AddonOnRequestedUpdate { get; private set; }
/// <summary> /// <summary>
/// Scan for and setup any configured address pointers. /// Scan for and setup any configured address pointers.
@ -35,5 +40,6 @@ internal class AddonLifecycleAddressResolver : BaseAddressResolver
this.AddonFinalize = sig.ScanText("E8 ?? ?? ?? ?? 48 8B 7C 24 ?? 41 8B C6"); this.AddonFinalize = sig.ScanText("E8 ?? ?? ?? ?? 48 8B 7C 24 ?? 41 8B C6");
this.AddonDraw = sig.ScanText("FF 90 ?? ?? ?? ?? 83 EB 01 79 C1"); this.AddonDraw = sig.ScanText("FF 90 ?? ?? ?? ?? 83 EB 01 79 C1");
this.AddonUpdate = sig.ScanText("FF 90 ?? ?? ?? ?? 40 88 AF"); this.AddonUpdate = sig.ScanText("FF 90 ?? ?? ?? ?? 40 88 AF");
this.AddonOnRequestedUpdate = sig.ScanText("FF 90 90 01 00 00 48 8B 5C 24 30 48 83 C4 20");
} }
} }