diff --git a/include/fxn/invoker.hpp b/include/fxn/invoker.hpp index 61b8335..eed3af7 100644 --- a/include/fxn/invoker.hpp +++ b/include/fxn/invoker.hpp @@ -15,13 +15,17 @@ namespace fxn static schedule_native_t _schedule_native; typedef bool(*native_hook_t)(fxn::NativeContext* context, bool& call_original); - typedef void(*register_native_hook_t)(std::uint64_t hash, native_hook_t hook); + typedef size_t(*register_native_hook_t)(std::uint64_t hash, native_hook_t hook); static register_native_hook_t _register_native_hook; + typedef void(*unregister_native_hook_t)(std::uint64_t hash, size_t index); + static unregister_native_hook_t _unregister_native_hook; + static void initialize(std::string_view module_name); static void get_native_handler(std::uint64_t hash); static void schedule_native(fxn::NativeContext* context, bool wait); - static void register_native_hook(std::uint64_t hash, native_hook_t hook); + static size_t register_native_hook(std::uint64_t hash, native_hook_t hook); + static void unregister_native_hook(std::uint64_t hash, size_t index); }; template @@ -31,6 +35,32 @@ namespace fxn using native_hash = std::integral_constant; return_type operator()(Args... args); + + static return_type invoke(Args... args) + { + invoker inv; + return inv(args...); + } + }; + + template + struct native_hooker + { + private: + std::size_t _hook; + + public: + using native_hash = std::integral_constant; + + native_hooker(fxn::invoker_base::native_hook_t hook) + { + _hook = fxn::invoker_base::register_native_hook(native_hash::value, hook); + } + + ~native_hooker() + { + fxn::invoker_base::unregister_native_hook(native_hash::value, _hook); + } }; } @@ -42,6 +72,7 @@ namespace fxn inline invoker_base::get_native_handler_t invoker_base::_get_native_handler = nullptr; inline invoker_base::schedule_native_t invoker_base::_schedule_native = nullptr; inline invoker_base::register_native_hook_t invoker_base::_register_native_hook = nullptr; + inline invoker_base::unregister_native_hook_t invoker_base::_unregister_native_hook = nullptr; inline void invoker_base::initialize(std::string_view module_name) { @@ -63,9 +94,14 @@ namespace fxn GetProcAddress(mod, "fxn_register_native_hook") ); + invoker_base::_unregister_native_hook = reinterpret_cast( + GetProcAddress(mod, "fxn_unregister_native_hook") + ); + if (!invoker_base::_get_native_handler || !invoker_base::_schedule_native || - !invoker_base::_register_native_hook) + !invoker_base::_register_native_hook || + !invoker_base::_unregister_native_hook) { exit(EXIT_FAILURE); } @@ -81,9 +117,14 @@ namespace fxn invoker_base::_schedule_native(context, wait); } - inline void invoker_base::register_native_hook(std::uint64_t hash, native_hook_t hook) + inline size_t invoker_base::register_native_hook(std::uint64_t hash, native_hook_t hook) { - invoker_base::_register_native_hook(hash, hook); + return invoker_base::_register_native_hook(hash, hook); + } + + inline void invoker_base::unregister_native_hook(std::uint64_t hash, size_t index) + { + invoker_base::_unregister_native_hook(hash, index); } template diff --git a/src/detail/exports.hpp b/src/detail/exports.hpp index 1cedaee..07a1eec 100644 --- a/src/detail/exports.hpp +++ b/src/detail/exports.hpp @@ -6,7 +6,8 @@ extern "C" { typedef bool (*native_hook_t)(fxn::NativeContext* context, bool& call_original); - __declspec(dllexport) void* fxn_native_invoker_get_handler(std::uint64_t hash); - __declspec(dllexport) void fxn_schedule_native(fxn::NativeContext* context, bool wait); - __declspec(dllexport) void fxn_register_native_hook(std::uint64_t hash, native_hook_t hook); + __declspec(dllexport) void* fxn_native_invoker_get_handler(std::uint64_t hash); + __declspec(dllexport) void fxn_schedule_native(fxn::NativeContext* context, bool wait); + __declspec(dllexport) size_t fxn_register_native_hook(std::uint64_t hash, native_hook_t hook); + __declspec(dllexport) void fxn_unregister_native_hook(std::uint64_t hash, size_t index); } \ No newline at end of file diff --git a/src/impl/exports.cpp b/src/impl/exports.cpp index 177118b..09e911e 100644 --- a/src/impl/exports.cpp +++ b/src/impl/exports.cpp @@ -17,13 +17,17 @@ extern "C" manager.Schedule(context, wait); } - __declspec(dllexport) void fxn_register_native_hook(std::uint64_t hash, native_hook_t hook) + __declspec(dllexport) size_t fxn_register_native_hook(std::uint64_t hash, native_hook_t hook) { static auto& manager = fxn::FxnManager::GetInstance(); - manager.RegisterNativeHook(hash, [hook](fxn::NativeContext* context, bool& call_original) - { - return hook(context, call_original); - }); + return manager.RegisterNativeHook(hash, hook); + } + + __declspec(dllexport) void fxn_unregister_native_hook(std::uint64_t hash, size_t index) + { + static auto& manager = fxn::FxnManager::GetInstance(); + + manager.UnregisterNativeHook(hash, index); } } \ No newline at end of file