diff --git a/src/ll/api/memory/Hook.h b/src/ll/api/memory/Hook.h index fb4e02e9ec..cb5fe06295 100644 --- a/src/ll/api/memory/Hook.h +++ b/src/ll/api/memory/Hook.h @@ -99,8 +99,7 @@ LLNDAPI FuncPtr resolveIdentifier(std::string_view identifier, bool disableError template concept FuncPtrType = std::is_function_v> || std::is_member_function_pointer_v; -template - requires(FuncPtrType || std::is_same_v) +template constexpr FuncPtr resolveIdentifier(T identifier) { return toFuncPtr(identifier); } @@ -111,10 +110,14 @@ constexpr FuncPtr resolveIdentifier(std::string_view identifier) { return resolveIdentifier(identifier); } -// redirect to resolveIdentifier(uintptr_t) template constexpr FuncPtr resolveIdentifier(uintptr_t address) { - return resolveIdentifier(address); + return toFuncPtr(address); +} + +template +constexpr FuncPtr resolveIdentifier(void* address) { + return address; } template @@ -175,7 +178,7 @@ struct __declspec(empty_bases) Hook {}; \ template \ struct _ConstDetector { \ - static constexpr bool value = false; \ + [[maybe_unused]] static constexpr bool value = false; \ explicit constexpr _ConstDetector(T) {} \ }; \ template \ @@ -183,7 +186,7 @@ struct __declspec(empty_bases) Hook {}; [[maybe_unused]] _ConstDetector(_RawFuncType) -> _ConstDetector<_RawFuncType>; \ template <> \ struct _ConstDetector<_RawConstFuncType> { \ - static constexpr bool value = true; \ + [[maybe_unused]] static constexpr bool value = true; \ explicit constexpr _ConstDetector(_RawConstFuncType) {} \ }; \ template , int> = 0> \ @@ -200,11 +203,13 @@ struct __declspec(empty_bases) Hook {}; \ template \ static consteval void detector() { \ - if constexpr (::ll::memory::virtualDetector<_OriginFuncType, IDENTIFIER>()) { \ - static_assert( \ - ::ll::concepts::always_false, \ - #IDENTIFIER " is a virtual function, you need use prefix $ workaround to hook it." \ - ); \ + if constexpr (requires { ::ll::memory::virtualDetector(); }) { \ + if constexpr (::ll::memory::virtualDetector()) { \ + static_assert( \ + ::ll::concepts::always_false, \ + #IDENTIFIER " is a virtual function, you need use prefix $ workaround to hook it." \ + ); \ + } \ } \ } \ \ @@ -216,7 +221,7 @@ struct __declspec(empty_bases) Hook {}; STATIC RET_TYPE detour(__VA_ARGS__); \ \ static int hook(bool suspendThreads = true) { \ - detector(); \ + detector<_OriginFuncType>(); \ _HookTarget = ::ll::memory::resolveIdentifier<_OriginFuncType>(IDENTIFIER); \ if (_HookTarget == nullptr) { \ return -1; \ diff --git a/src/ll/test/HookTest.cpp b/src/ll/test/HookTest.cpp index d511e52990..81c157c724 100644 --- a/src/ll/test/HookTest.cpp +++ b/src/ll/test/HookTest.cpp @@ -1,9 +1,11 @@ -#include "ll/api/memory/Hook.h" - #include +#include "ll/api/memory/Hook.h" +#include "ll/api/memory/Memory.h" + +// NOLINTBEGIN namespace { -class TestClass { +class TestOverloadClass { protected: int some = 0; @@ -14,8 +16,36 @@ class TestClass { }; // they will choose non-const version if possible -LL_TYPE_INSTANCE_HOOK(TestHook, HookPriority::Normal, TestClass, &TestClass::test, int, int a, int b) { +LL_TYPE_INSTANCE_HOOK(TestHook, HookPriority::Normal, TestOverloadClass, &TestOverloadClass::test, int, int a, int b) { + some = 1; + return origin(a, b); +} + +class TestPtrClass { +protected: + int some = 0; + +public: + int test(int a, int b) { return a + b; } +}; + +// they will choose non-const version if possible +LL_TYPE_INSTANCE_HOOK( + TestPtrHook, + HookPriority::Normal, + TestPtrClass, + ll::memory::toFuncPtr(&TestPtrClass::test), + int, + __int64 a, + __int64 b +) { + some = 1; + return origin(a, b); +} + +LL_TYPE_INSTANCE_HOOK(TestUintptrHook, HookPriority::Normal, TestPtrClass, 0x1145141919810, int, __int64 a, __int64 b) { some = 1; - return a + b; + return origin(a, b); } } // namespace +// NOLINTEND