From b7076a76199bc64b4c2a4bff50a1ed27c60d551e Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 1 Jan 2023 22:53:24 +0100 Subject: [PATCH] feat(binding): use asm hooking for entity focus --- .../Objects/UnitManagerBinding.cs | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Core/NosSmooth.LocalBinding/Objects/UnitManagerBinding.cs b/src/Core/NosSmooth.LocalBinding/Objects/UnitManagerBinding.cs index a595144..2fa89c4 100644 --- a/src/Core/NosSmooth.LocalBinding/Objects/UnitManagerBinding.cs +++ b/src/Core/NosSmooth.LocalBinding/Objects/UnitManagerBinding.cs @@ -11,6 +11,7 @@ using NosSmooth.LocalBinding.Options; using NosSmooth.LocalBinding.Structs; using Reloaded.Hooks.Definitions; using Reloaded.Hooks.Definitions.X86; +using Reloaded.Hooks.Internal.Testing; using Remora.Results; namespace NosSmooth.LocalBinding.Objects; @@ -56,7 +57,7 @@ public class UnitManagerBinding bindingOptions.UnitManagerOffsets ); - var entityFocusHookResult = bindingManager.CreateHookFromPattern + var entityFocusHookResult = bindingManager.CreateCustomAsmHookFromPattern ("UnitManager.EntityFocus", binding.FocusEntityDetour, bindingOptions.EntityFocusHook); if (!entityFocusHookResult.IsDefined(out var entityFocusHook)) { @@ -72,7 +73,9 @@ public class UnitManagerBinding private readonly NosBindingManager _bindingManager; - private IHook _focusHook = null!; + private NosAsmHook _focusHook = null!; + + private bool _callingFocus; private UnitManagerBinding ( @@ -113,7 +116,7 @@ public class UnitManagerBinding /// public void DisableHooks() { - _focusHook.Disable(); + _focusHook.Hook.Disable(); } /// @@ -121,7 +124,7 @@ public class UnitManagerBinding /// public void EnableHooks() { - _focusHook.EnableOrActivate(); + _focusHook.Hook.EnableOrActivate(); } /// @@ -133,7 +136,9 @@ public class UnitManagerBinding { try { - _focusHook.OriginalFunction(Address, entityAddress); + _callingFocus = true; + _focusHook.OriginalFunction.GetWrapper()(Address, entityAddress); + _callingFocus = false; } catch (Exception e) { @@ -145,19 +150,19 @@ public class UnitManagerBinding private nuint FocusEntityDetour(nuint unitManagerPtr, nuint entityId) { - MapBaseObj? obj = null; - if (entityId != nuint.Zero) + if (_callingFocus) { - obj = new MapBaseObj(_bindingManager.Memory, entityId); + // in case this is being called from UnitManagerBinding.FocusEntity, + // do not handle. + return 1; } - var result = EntityFocus?.Invoke(obj); - - if (result ?? true) + MapBaseObj? obj = null; + if (entityId != nuint.Zero) { - return _focusHook.OriginalFunction(unitManagerPtr, entityId); + obj = new MapBaseObj(_bindingManager.Memory, entityId); } - return 0; + return (EntityFocus?.Invoke(obj) ?? true) ? (nuint)1 : 0; } } \ No newline at end of file -- 2.49.0