From c01fa0608e4a6c361ba93d580dd78907a282dbc3 Mon Sep 17 00:00:00 2001 From: Raymond Lynch Date: Sat, 12 Jun 2021 11:07:03 -0400 Subject: [PATCH] Simplify hooks --- Dalamud/Game/Addon/DalamudSystemMenu.cs | 10 ++---- Dalamud/Game/ClientState/ClientState.cs | 2 +- Dalamud/Game/Internal/Framework.cs | 6 ++-- Dalamud/Game/Internal/Gui/ChatGui.cs | 6 ++-- Dalamud/Game/Internal/Gui/GameGui.cs | 12 +++---- Dalamud/Game/Internal/Network/GameNetwork.cs | 5 ++- .../Game/Internal/Resource/ResourceManager.cs | 5 ++- Dalamud/Hooking/Hook.cs | 35 +++++++++++++++---- Dalamud/Interface/InterfaceManager.cs | 6 ++-- 9 files changed, 51 insertions(+), 36 deletions(-) diff --git a/Dalamud/Game/Addon/DalamudSystemMenu.cs b/Dalamud/Game/Addon/DalamudSystemMenu.cs index a32024d6e..b783730d3 100644 --- a/Dalamud/Game/Addon/DalamudSystemMenu.cs +++ b/Dalamud/Game/Addon/DalamudSystemMenu.cs @@ -32,10 +32,7 @@ namespace Dalamud.Game.Addon var openSystemMenuAddress = this.dalamud.SigScanner.ScanText("E8 ?? ?? ?? ?? 32 C0 4C 8B AC 24 ?? ?? ?? ?? 48 8B 8D ?? ?? ?? ??"); - this.hookAgentHudOpenSystemMenu = new Hook( - openSystemMenuAddress, - new AgentHudOpenSystemMenuPrototype(this.AgentHudOpenSystemMenuDetour), - this); + this.hookAgentHudOpenSystemMenu = new Hook(openSystemMenuAddress, this.AgentHudOpenSystemMenuDetour); var atkValueChangeTypeAddress = this.dalamud.SigScanner.ScanText("E8 ?? ?? ?? ?? 45 84 F6 48 8D 4C 24 ??"); @@ -47,10 +44,7 @@ namespace Dalamud.Game.Addon this.atkValueSetString = Marshal.GetDelegateForFunctionPointer(atkValueSetStringAddress); var uiModuleRequestMainCommandAddress = this.dalamud.SigScanner.ScanText("40 53 56 48 81 EC ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? 48 33 C4 48 89 84 24 ?? ?? ?? ?? 48 8B 01 8B DA 48 8B F1 FF 90 ?? ?? ?? ??"); - this.hookUiModuleRequestMainCommand = new Hook( - uiModuleRequestMainCommandAddress, - new UiModuleRequestMainCommand(this.UiModuleRequestMainCommandDetour), - this); + this.hookUiModuleRequestMainCommand = new Hook(uiModuleRequestMainCommandAddress, this.UiModuleRequestMainCommandDetour); } private delegate void AgentHudOpenSystemMenuPrototype(void* thisPtr, AtkValue* atkValueArgs, uint menuSize); diff --git a/Dalamud/Game/ClientState/ClientState.cs b/Dalamud/Game/ClientState/ClientState.cs index 2134ec4bc..f2057a607 100644 --- a/Dalamud/Game/ClientState/ClientState.cs +++ b/Dalamud/Game/ClientState/ClientState.cs @@ -106,7 +106,7 @@ namespace Dalamud.Game.ClientState Log.Verbose("SetupTerritoryType address {SetupTerritoryType}", this.address.SetupTerritoryType); - this.setupTerritoryTypeHook = new Hook(this.address.SetupTerritoryType, new SetupTerritoryTypeDelegate(this.SetupTerritoryTypeDetour), this); + this.setupTerritoryTypeHook = new Hook(this.address.SetupTerritoryType, this.SetupTerritoryTypeDetour); dalamud.Framework.OnUpdateEvent += this.FrameworkOnOnUpdateEvent; dalamud.NetworkHandlers.CfPop += this.NetworkHandlersOnCfPop; diff --git a/Dalamud/Game/Internal/Framework.cs b/Dalamud/Game/Internal/Framework.cs index 7e0d3ab8a..7d13318f6 100644 --- a/Dalamud/Game/Internal/Framework.cs +++ b/Dalamud/Game/Internal/Framework.cs @@ -159,13 +159,13 @@ namespace Dalamud.Game.Internal // .rdata:00000001411F2000 dq offset Xiv__Framework__update var pUpdate = Marshal.ReadIntPtr(vtable, IntPtr.Size * 4); - this.updateHook = new Hook(pUpdate, new OnUpdateDetour(this.HandleFrameworkUpdate), this); + this.updateHook = new Hook(pUpdate, this.HandleFrameworkUpdate); var pDestroy = Marshal.ReadIntPtr(vtable, IntPtr.Size * 3); - this.destroyHook = new Hook(pDestroy, new OnDestroyDelegate(this.HandleFrameworkDestroy), this); + this.destroyHook = new Hook(pDestroy, this.HandleFrameworkDestroy); var pRealDestroy = Marshal.ReadIntPtr(vtable, IntPtr.Size * 2); - this.realDestroyHook = new Hook(pRealDestroy, new OnRealDestroyDelegate(this.HandleRealDestroy), this); + this.realDestroyHook = new Hook(pRealDestroy, this.HandleRealDestroy); } private bool HandleFrameworkUpdate(IntPtr framework) diff --git a/Dalamud/Game/Internal/Gui/ChatGui.cs b/Dalamud/Game/Internal/Gui/ChatGui.cs index 111cc259c..38d7a2783 100644 --- a/Dalamud/Game/Internal/Gui/ChatGui.cs +++ b/Dalamud/Game/Internal/Gui/ChatGui.cs @@ -45,9 +45,9 @@ namespace Dalamud.Game.Internal.Gui Log.Verbose("Chat manager address {ChatManager}", this.address.BaseAddress); - this.printMessageHook = new Hook(this.address.PrintMessage, new PrintMessageDelegate(this.HandlePrintMessageDetour), this); - this.populateItemLinkHook = new Hook(this.address.PopulateItemLinkObject, new PopulateItemLinkDelegate(this.HandlePopulateItemLinkDetour), this); - this.interactableLinkClickedHook = new Hook(this.address.InteractableLinkClicked, new InteractableLinkClickedDelegate(this.InteractableLinkClickedDetour)); + this.printMessageHook = new Hook(this.address.PrintMessage, this.HandlePrintMessageDetour); + this.populateItemLinkHook = new Hook(this.address.PopulateItemLinkObject, this.HandlePopulateItemLinkDetour); + this.interactableLinkClickedHook = new Hook(this.address.InteractableLinkClicked, this.InteractableLinkClickedDetour); } /// diff --git a/Dalamud/Game/Internal/Gui/GameGui.cs b/Dalamud/Game/Internal/Gui/GameGui.cs index b8bcf012b..be018f1e1 100644 --- a/Dalamud/Game/Internal/Gui/GameGui.cs +++ b/Dalamud/Game/Internal/Gui/GameGui.cs @@ -67,13 +67,13 @@ namespace Dalamud.Game.Internal.Gui this.PartyFinder = new PartyFinderGui(scanner, dalamud); this.Toast = new ToastGui(scanner, dalamud); - this.setGlobalBgmHook = new Hook(this.address.SetGlobalBgm, new SetGlobalBgmDelegate(this.HandleSetGlobalBgmDetour), this); - this.handleItemHoverHook = new Hook(this.address.HandleItemHover, new HandleItemHoverDelegate(this.HandleItemHoverDetour), this); + this.setGlobalBgmHook = new Hook(this.address.SetGlobalBgm, this.HandleSetGlobalBgmDetour); + this.handleItemHoverHook = new Hook(this.address.HandleItemHover, this.HandleItemHoverDetour); - this.handleItemOutHook = new Hook(this.address.HandleItemOut, new HandleItemOutDelegate(this.HandleItemOutDetour), this); + this.handleItemOutHook = new Hook(this.address.HandleItemOut, this.HandleItemOutDetour); - this.handleActionHoverHook = new Hook(this.address.HandleActionHover, new HandleActionHoverDelegate(this.HandleActionHoverDetour), this); - this.handleActionOutHook = new Hook(this.address.HandleActionOut, new HandleActionOutDelegate(this.HandleActionOutDetour), this); + this.handleActionHoverHook = new Hook(this.address.HandleActionHover, this.HandleActionHoverDetour); + this.handleActionOutHook = new Hook(this.address.HandleActionOut, this.HandleActionOutDetour); this.getUIObject = Marshal.GetDelegateForFunctionPointer(this.address.GetUIObject); @@ -81,7 +81,7 @@ namespace Dalamud.Game.Internal.Gui this.screenToWorldNative = Marshal.GetDelegateForFunctionPointer(this.address.ScreenToWorld); - this.toggleUiHideHook = new Hook(this.address.ToggleUiHide, new ToggleUiHideDelegate(this.ToggleUiHideDetour), this); + this.toggleUiHideHook = new Hook(this.address.ToggleUiHide, this.ToggleUiHideDetour); this.GetBaseUIObject = Marshal.GetDelegateForFunctionPointer(this.address.GetBaseUIObject); this.getUIObjectByName = Marshal.GetDelegateForFunctionPointer(this.address.GetUIObjectByName); diff --git a/Dalamud/Game/Internal/Network/GameNetwork.cs b/Dalamud/Game/Internal/Network/GameNetwork.cs index 1ecc09352..cb88c9dfc 100644 --- a/Dalamud/Game/Internal/Network/GameNetwork.cs +++ b/Dalamud/Game/Internal/Network/GameNetwork.cs @@ -37,9 +37,8 @@ namespace Dalamud.Game.Internal.Network Log.Verbose("ProcessZonePacketDown address {ProcessZonePacketDown}", this.address.ProcessZonePacketDown); Log.Verbose("ProcessZonePacketUp address {ProcessZonePacketUp}", this.address.ProcessZonePacketUp); - this.processZonePacketDownHook = new Hook(this.address.ProcessZonePacketDown, new ProcessZonePacketDownDelegate(this.ProcessZonePacketDownDetour), this); - - this.processZonePacketUpHook = new Hook(this.address.ProcessZonePacketUp, new ProcessZonePacketUpDelegate(this.ProcessZonePacketUpDetour), this); + this.processZonePacketDownHook = new Hook(this.address.ProcessZonePacketDown, this.ProcessZonePacketDownDetour); + this.processZonePacketUpHook = new Hook(this.address.ProcessZonePacketUp, this.ProcessZonePacketUpDetour); } /// diff --git a/Dalamud/Game/Internal/Resource/ResourceManager.cs b/Dalamud/Game/Internal/Resource/ResourceManager.cs index 0aa7db88c..1e80475c1 100644 --- a/Dalamud/Game/Internal/Resource/ResourceManager.cs +++ b/Dalamud/Game/Internal/Resource/ResourceManager.cs @@ -35,9 +35,8 @@ namespace Dalamud.Game.Internal.File Log.Verbose("GetResourceAsync address {GetResourceAsync}", this.address.GetResourceAsync); Log.Verbose("GetResourceSync address {GetResourceSync}", this.address.GetResourceSync); - this.getResourceAsyncHook = new Hook(this.address.GetResourceAsync, new GetResourceAsyncDelegate(this.GetResourceAsyncDetour), this); - - this.getResourceSyncHook = new Hook(this.address.GetResourceSync, new GetResourceSyncDelegate(this.GetResourceSyncDetour), this); + this.getResourceAsyncHook = new Hook(this.address.GetResourceAsync, this.GetResourceAsyncDetour); + this.getResourceSyncHook = new Hook(this.address.GetResourceSync, this.GetResourceSyncDetour); } [UnmanagedFunctionPointer(CallingConvention.ThisCall)] diff --git a/Dalamud/Hooking/Hook.cs b/Dalamud/Hooking/Hook.cs index 83a8646af..f709f2a63 100644 --- a/Dalamud/Hooking/Hook.cs +++ b/Dalamud/Hooking/Hook.cs @@ -26,15 +26,27 @@ namespace Dalamud.Hooking /// /// A memory address to install a hook. /// Callback function. Delegate must have a same original function prototype. - /// A callback object which can be accessed within the detour. - public Hook(IntPtr address, Delegate detour, object callbackParam = null) + public Hook(IntPtr address, T detour) { - this.hookInfo = LocalHook.Create(address, detour, callbackParam); // Installs a hook here + this.hookInfo = LocalHook.Create(address, detour, null); // Installs a hook here this.address = address; this.original = Marshal.GetDelegateForFunctionPointer(this.hookInfo.HookBypassAddress); HookInfo.TrackedHooks.Add(new HookInfo() { Delegate = detour, Hook = this, Assembly = Assembly.GetCallingAssembly() }); } + /// + /// Initializes a new instance of the class. + /// Hook is not activated until Enable() method is called. + /// + /// A memory address to install a hook. + /// Callback function. Delegate must have a same original function prototype. + /// A callback object which can be accessed within the detour. + [Obsolete("There is no need to specify new YourDelegateType or callbackParam", true)] + public Hook(IntPtr address, Delegate detour, object callbackParam = null) + : this(address, detour as T) + { + } + /// /// Gets a memory address of the target function. /// @@ -87,16 +99,27 @@ namespace Dalamud.Hooking /// A name of the module currently loaded in the memory. (e.g. ws2_32.dll). /// A name of the exported function name (e.g. send). /// Callback function. Delegate must have a same original function prototype. - /// A callback object which can be accessed within the detour. /// The hook with the supplied parameters. - public static Hook FromSymbol(string moduleName, string exportName, Delegate detour, object callbackParam = null) + public static Hook FromSymbol(string moduleName, string exportName, T detour) { // Get a function address from the symbol name. var address = LocalHook.GetProcAddress(moduleName, exportName); - return new Hook(address, detour, callbackParam); + return new Hook(address, detour); } + /// + /// Creates a hook. Hooking address is inferred by calling to GetProcAddress() function. + /// Hook is not activated until Enable() method is called. + /// + /// A name of the module currently loaded in the memory. (e.g. ws2_32.dll). + /// A name of the exported function name (e.g. send). + /// Callback function. Delegate must have a same original function prototype. + /// A callback object which can be accessed within the detour. + /// The hook with the supplied parameters. + [Obsolete("There is no need to specify new YourDelegateType or callbackParam", true)] + public static Hook FromSymbol(string moduleName, string exportName, Delegate detour, object callbackParam = null) => FromSymbol(moduleName, exportName, detour as T); + /// /// Remove a hook from the current process. /// diff --git a/Dalamud/Interface/InterfaceManager.cs b/Dalamud/Interface/InterfaceManager.cs index 0f2be86e8..fec66aa19 100644 --- a/Dalamud/Interface/InterfaceManager.cs +++ b/Dalamud/Interface/InterfaceManager.cs @@ -120,11 +120,11 @@ namespace Dalamud.Interface Log.Verbose("Present address {Present}", this.address.Present); Log.Verbose("ResizeBuffers address {ResizeBuffers}", this.address.ResizeBuffers); - this.setCursorHook = new Hook(setCursorAddr, new SetCursorDelegate(this.SetCursorDetour), this); + this.setCursorHook = new Hook(setCursorAddr, this.SetCursorDetour); - this.presentHook = new Hook(this.address.Present, new PresentDelegate(this.PresentDetour), this); + this.presentHook = new Hook(this.address.Present, this.PresentDetour); - this.resizeBuffersHook = new Hook(this.address.ResizeBuffers, new ResizeBuffersDelegate(this.ResizeBuffersDetour), this); + this.resizeBuffersHook = new Hook(this.address.ResizeBuffers, this.ResizeBuffersDetour); } [UnmanagedFunctionPointer(CallingConvention.ThisCall)]