From 7f4352dc43df5d53c83b197603ee1383233c37a8 Mon Sep 17 00:00:00 2001 From: MidoriKami Date: Wed, 17 Dec 2025 16:38:34 -0800 Subject: [PATCH 1/2] Add address resolver --- .../Game/Addon/Lifecycle/AddonLifecycle.cs | 18 ++++ .../Game/Addon/Lifecycle/AddonVirtualTable.cs | 91 ++++++++++--------- Dalamud/Plugin/Services/IAddonLifecycle.cs | 21 +++-- 3 files changed, 81 insertions(+), 49 deletions(-) diff --git a/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs b/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs index 716ce1bfb..78cea1a0f 100644 --- a/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs +++ b/Dalamud/Game/Addon/Lifecycle/AddonLifecycle.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Runtime.CompilerServices; using Dalamud.Game.Addon.Lifecycle.AddonArgTypes; @@ -132,6 +133,19 @@ internal unsafe class AddonLifecycle : IInternalDisposableService } } + /// + /// Resolves a virtual table address to the original virtual table address. + /// + /// The modified address to resolve. + /// The original address. + internal AtkUnitBase.AtkUnitBaseVirtualTable* GetOriginalVirtualTable(AtkUnitBase.AtkUnitBaseVirtualTable* tableAddress) + { + var matchedTable = AllocatedTables.FirstOrDefault(table => table.ModifiedVirtualTable == tableAddress); + if (matchedTable == null) return null; + + return matchedTable.OriginalVirtualTable; + } + private void OnAddonInitialize(AtkUnitBase* addon) { try @@ -246,4 +260,8 @@ internal class AddonLifecyclePluginScoped : IInternalDisposableService, IAddonLi }); } } + + /// + public unsafe nint GetOriginalVirtualTable(nint virtualTableAddress) + => (nint)this.addonLifecycleService.GetOriginalVirtualTable((AtkUnitBase.AtkUnitBaseVirtualTable*)virtualTableAddress); } diff --git a/Dalamud/Game/Addon/Lifecycle/AddonVirtualTable.cs b/Dalamud/Game/Addon/Lifecycle/AddonVirtualTable.cs index 47ff92c3d..975ff027d 100644 --- a/Dalamud/Game/Addon/Lifecycle/AddonVirtualTable.cs +++ b/Dalamud/Game/Addon/Lifecycle/AddonVirtualTable.cs @@ -16,6 +16,16 @@ namespace Dalamud.Game.Addon.Lifecycle; /// internal unsafe class AddonVirtualTable : IDisposable { + /// + /// The original virtual table address for this addon. + /// + internal readonly AtkUnitBase.AtkUnitBaseVirtualTable* OriginalVirtualTable; + + /// + /// The modified virtual address for this addon. + /// + internal readonly AtkUnitBase.AtkUnitBaseVirtualTable* ModifiedVirtualTable; + // This need to be at minimum the largest virtual table size of all addons // Copying extra entries is not problematic, and is considered safe. private const int VirtualTableEntryCount = 200; @@ -45,9 +55,6 @@ internal unsafe class AddonVirtualTable : IDisposable private readonly AtkUnitBase* atkUnitBase; - private readonly AtkUnitBase.AtkUnitBaseVirtualTable* originalVirtualTable; - private readonly AtkUnitBase.AtkUnitBaseVirtualTable* modifiedVirtualTable; - // Pinned Function Delegates, as these functions get assigned to an unmanaged virtual table, // the CLR needs to know they are in use, or it will invalidate them causing random crashing. private readonly AtkUnitBase.Delegates.Dtor destructorFunction; @@ -78,16 +85,16 @@ internal unsafe class AddonVirtualTable : IDisposable this.lifecycleService = lifecycleService; // Save original virtual table - this.originalVirtualTable = addon->VirtualTable; + this.OriginalVirtualTable = addon->VirtualTable; // Create copy of original table // Note this will copy any derived/overriden functions that this specific addon has. // Note: currently there are 73 virtual functions, but there's no harm in copying more for when they add new virtual functions to the game - this.modifiedVirtualTable = (AtkUnitBase.AtkUnitBaseVirtualTable*)IMemorySpace.GetUISpace()->Malloc(0x8 * VirtualTableEntryCount, 8); - NativeMemory.Copy(addon->VirtualTable, this.modifiedVirtualTable, 0x8 * VirtualTableEntryCount); + this.ModifiedVirtualTable = (AtkUnitBase.AtkUnitBaseVirtualTable*)IMemorySpace.GetUISpace()->Malloc(0x8 * VirtualTableEntryCount, 8); + NativeMemory.Copy(addon->VirtualTable, this.ModifiedVirtualTable, 0x8 * VirtualTableEntryCount); // Overwrite the addons existing virtual table with our own - addon->VirtualTable = this.modifiedVirtualTable; + addon->VirtualTable = this.ModifiedVirtualTable; // Pin each of our listener functions this.destructorFunction = this.OnAddonDestructor; @@ -108,30 +115,30 @@ internal unsafe class AddonVirtualTable : IDisposable this.focusFunction = this.OnAddonFocus; // Overwrite specific virtual table entries - this.modifiedVirtualTable->Dtor = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.destructorFunction); - this.modifiedVirtualTable->OnSetup = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onSetupFunction); - this.modifiedVirtualTable->Finalizer = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.finalizerFunction); - this.modifiedVirtualTable->Draw = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.drawFunction); - this.modifiedVirtualTable->Update = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.updateFunction); - this.modifiedVirtualTable->OnRefresh = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onRefreshFunction); - this.modifiedVirtualTable->OnRequestedUpdate = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onRequestedUpdateFunction); - this.modifiedVirtualTable->ReceiveEvent = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onReceiveEventFunction); - this.modifiedVirtualTable->Open = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.openFunction); - this.modifiedVirtualTable->Close = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.closeFunction); - this.modifiedVirtualTable->Show = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.showFunction); - this.modifiedVirtualTable->Hide = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.hideFunction); - this.modifiedVirtualTable->OnMove = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onMoveFunction); - this.modifiedVirtualTable->OnMouseOver = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onMouseOverFunction); - this.modifiedVirtualTable->OnMouseOut = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onMouseOutFunction); - this.modifiedVirtualTable->Focus = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.focusFunction); + this.ModifiedVirtualTable->Dtor = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.destructorFunction); + this.ModifiedVirtualTable->OnSetup = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onSetupFunction); + this.ModifiedVirtualTable->Finalizer = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.finalizerFunction); + this.ModifiedVirtualTable->Draw = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.drawFunction); + this.ModifiedVirtualTable->Update = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.updateFunction); + this.ModifiedVirtualTable->OnRefresh = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onRefreshFunction); + this.ModifiedVirtualTable->OnRequestedUpdate = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onRequestedUpdateFunction); + this.ModifiedVirtualTable->ReceiveEvent = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onReceiveEventFunction); + this.ModifiedVirtualTable->Open = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.openFunction); + this.ModifiedVirtualTable->Close = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.closeFunction); + this.ModifiedVirtualTable->Show = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.showFunction); + this.ModifiedVirtualTable->Hide = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.hideFunction); + this.ModifiedVirtualTable->OnMove = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onMoveFunction); + this.ModifiedVirtualTable->OnMouseOver = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onMouseOverFunction); + this.ModifiedVirtualTable->OnMouseOut = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.onMouseOutFunction); + this.ModifiedVirtualTable->Focus = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.focusFunction); } /// public void Dispose() { // Ensure restoration is done atomically. - Interlocked.Exchange(ref *(nint*)&this.atkUnitBase->VirtualTable, (nint)this.originalVirtualTable); - IMemorySpace.Free(this.modifiedVirtualTable, 0x8 * VirtualTableEntryCount); + Interlocked.Exchange(ref *(nint*)&this.atkUnitBase->VirtualTable, (nint)this.OriginalVirtualTable); + IMemorySpace.Free(this.ModifiedVirtualTable, 0x8 * VirtualTableEntryCount); } private AtkEventListener* OnAddonDestructor(AtkUnitBase* thisPtr, byte freeFlags) @@ -144,7 +151,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - result = this.originalVirtualTable->Dtor(thisPtr, freeFlags); + result = this.OriginalVirtualTable->Dtor(thisPtr, freeFlags); } catch (Exception e) { @@ -153,7 +160,7 @@ internal unsafe class AddonVirtualTable : IDisposable if ((freeFlags & 1) == 1) { - IMemorySpace.Free(this.modifiedVirtualTable, 0x8 * VirtualTableEntryCount); + IMemorySpace.Free(this.ModifiedVirtualTable, 0x8 * VirtualTableEntryCount); AddonLifecycle.AllocatedTables.Remove(this); } } @@ -182,7 +189,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->OnSetup(addon, valueCount, values); + this.OriginalVirtualTable->OnSetup(addon, valueCount, values); } catch (Exception e) { @@ -209,7 +216,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->Finalizer(thisPtr); + this.OriginalVirtualTable->Finalizer(thisPtr); } catch (Exception e) { @@ -234,7 +241,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->Draw(addon); + this.OriginalVirtualTable->Draw(addon); } catch (Exception e) { @@ -265,7 +272,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->Update(addon, delta); + this.OriginalVirtualTable->Update(addon, delta); } catch (Exception e) { @@ -299,7 +306,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - result = this.originalVirtualTable->OnRefresh(addon, valueCount, values); + result = this.OriginalVirtualTable->OnRefresh(addon, valueCount, values); } catch (Exception e) { @@ -333,7 +340,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->OnRequestedUpdate(addon, numberArrayData, stringArrayData); + this.OriginalVirtualTable->OnRequestedUpdate(addon, numberArrayData, stringArrayData); } catch (Exception e) { @@ -369,7 +376,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->ReceiveEvent(addon, eventType, eventParam, atkEvent, atkEventData); + this.OriginalVirtualTable->ReceiveEvent(addon, eventType, eventParam, atkEvent, atkEventData); } catch (Exception e) { @@ -398,7 +405,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - result = this.originalVirtualTable->Open(thisPtr, depthLayer); + result = this.OriginalVirtualTable->Open(thisPtr, depthLayer); } catch (Exception e) { @@ -432,7 +439,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - result = this.originalVirtualTable->Close(thisPtr, fireCallback); + result = this.OriginalVirtualTable->Close(thisPtr, fireCallback); } catch (Exception e) { @@ -466,7 +473,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->Show(thisPtr, silenceOpenSoundEffect, unsetShowHideFlags); + this.OriginalVirtualTable->Show(thisPtr, silenceOpenSoundEffect, unsetShowHideFlags); } catch (Exception e) { @@ -500,7 +507,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->Hide(thisPtr, unkBool, callHideCallback, setShowHideFlags); + this.OriginalVirtualTable->Hide(thisPtr, unkBool, callHideCallback, setShowHideFlags); } catch (Exception e) { @@ -527,7 +534,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->OnMove(thisPtr); + this.OriginalVirtualTable->OnMove(thisPtr); } catch (Exception e) { @@ -554,7 +561,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->OnMouseOver(thisPtr); + this.OriginalVirtualTable->OnMouseOver(thisPtr); } catch (Exception e) { @@ -581,7 +588,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->OnMouseOut(thisPtr); + this.OriginalVirtualTable->OnMouseOut(thisPtr); } catch (Exception e) { @@ -608,7 +615,7 @@ internal unsafe class AddonVirtualTable : IDisposable try { - this.originalVirtualTable->Focus(thisPtr); + this.OriginalVirtualTable->Focus(thisPtr); } catch (Exception e) { diff --git a/Dalamud/Plugin/Services/IAddonLifecycle.cs b/Dalamud/Plugin/Services/IAddonLifecycle.cs index 1269b13dc..aeff29811 100644 --- a/Dalamud/Plugin/Services/IAddonLifecycle.cs +++ b/Dalamud/Plugin/Services/IAddonLifecycle.cs @@ -17,7 +17,7 @@ public interface IAddonLifecycle : IDalamudService /// The event type that triggered the message. /// Information about what addon triggered the message. public delegate void AddonEventDelegate(AddonEvent type, AddonArgs args); - + /// /// Register a listener that will trigger on the specified event and any of the specified addons. /// @@ -25,7 +25,7 @@ public interface IAddonLifecycle : IDalamudService /// Addon names that will trigger the handler to be invoked. /// The handler to invoke. void RegisterListener(AddonEvent eventType, IEnumerable addonNames, AddonEventDelegate handler); - + /// /// Register a listener that will trigger on the specified event only for the specified addon. /// @@ -33,14 +33,14 @@ public interface IAddonLifecycle : IDalamudService /// The addon name that will trigger the handler to be invoked. /// The handler to invoke. void RegisterListener(AddonEvent eventType, string addonName, AddonEventDelegate handler); - + /// /// Register a listener that will trigger on the specified event for any addon. /// /// Event type to trigger on. /// The handler to invoke. void RegisterListener(AddonEvent eventType, AddonEventDelegate handler); - + /// /// Unregister listener from specified event type and specified addon names. /// @@ -51,7 +51,7 @@ public interface IAddonLifecycle : IDalamudService /// Addon names to deregister. /// Optional specific handler to remove. void UnregisterListener(AddonEvent eventType, IEnumerable addonNames, [Optional] AddonEventDelegate handler); - + /// /// Unregister all listeners for the specified event type and addon name. /// @@ -62,7 +62,7 @@ public interface IAddonLifecycle : IDalamudService /// Addon name to deregister. /// Optional specific handler to remove. void UnregisterListener(AddonEvent eventType, string addonName, [Optional] AddonEventDelegate handler); - + /// /// Unregister an event type handler.
This will only remove a handler that is added via . ///
@@ -72,10 +72,17 @@ public interface IAddonLifecycle : IDalamudService /// Event type to deregister. /// Optional specific handler to remove. void UnregisterListener(AddonEvent eventType, [Optional] AddonEventDelegate handler); - + /// /// Unregister all events that use the specified handlers. /// /// Handlers to remove. void UnregisterListener(params AddonEventDelegate[] handlers); + + /// + /// Resolves an addons virtual table address back to the original unmodified table address. + /// + /// The address of a modified addons virtual table. + /// The address of the addons original virtual table. + nint GetOriginalVirtualTable(nint virtualTableAddress); } From 37fa40ab587002b4dd8180eb2872d5b5ef10fc37 Mon Sep 17 00:00:00 2001 From: MidoriKami Date: Wed, 17 Dec 2025 16:44:04 -0800 Subject: [PATCH 2/2] Make stylecop happy --- .../Game/Addon/Lifecycle/AddonVirtualTable.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Dalamud/Game/Addon/Lifecycle/AddonVirtualTable.cs b/Dalamud/Game/Addon/Lifecycle/AddonVirtualTable.cs index 975ff027d..736415738 100644 --- a/Dalamud/Game/Addon/Lifecycle/AddonVirtualTable.cs +++ b/Dalamud/Game/Addon/Lifecycle/AddonVirtualTable.cs @@ -16,16 +16,6 @@ namespace Dalamud.Game.Addon.Lifecycle; /// internal unsafe class AddonVirtualTable : IDisposable { - /// - /// The original virtual table address for this addon. - /// - internal readonly AtkUnitBase.AtkUnitBaseVirtualTable* OriginalVirtualTable; - - /// - /// The modified virtual address for this addon. - /// - internal readonly AtkUnitBase.AtkUnitBaseVirtualTable* ModifiedVirtualTable; - // This need to be at minimum the largest virtual table size of all addons // Copying extra entries is not problematic, and is considered safe. private const int VirtualTableEntryCount = 200; @@ -133,6 +123,16 @@ internal unsafe class AddonVirtualTable : IDisposable this.ModifiedVirtualTable->Focus = (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.focusFunction); } + /// + /// Gets the original virtual table address for this addon. + /// + internal AtkUnitBase.AtkUnitBaseVirtualTable* OriginalVirtualTable { get; private set; } + + /// + /// Gets the modified virtual address for this addon. + /// + internal AtkUnitBase.AtkUnitBaseVirtualTable* ModifiedVirtualTable { get; private set; } + /// public void Dispose() {