diff options
| author | Vaxry <vaxry@vaxry.net> | 2024-04-20 20:16:42 +0100 |
|---|---|---|
| committer | Vaxry <vaxry@vaxry.net> | 2024-04-20 20:16:42 +0100 |
| commit | 4ad739ec63c9a11f0537a884ae2a4c56d6bab10b (patch) | |
| tree | 0842db7489691fc6e7cbb3cd6d3321dd0d4790b6 /src/managers/HookSystemManager.hpp | |
| parent | 1055e6bee677ef9c4ab4d09e43155e00a98a3b49 (diff) | |
HookSystem: improve callback safety
Diffstat (limited to 'src/managers/HookSystemManager.hpp')
| -rw-r--r-- | src/managers/HookSystemManager.hpp | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/managers/HookSystemManager.hpp b/src/managers/HookSystemManager.hpp index ca8e9006..4b27fadf 100644 --- a/src/managers/HookSystemManager.hpp +++ b/src/managers/HookSystemManager.hpp @@ -16,8 +16,8 @@ typedef std::function<void(void*, SCallbackInfo& info, std::any data)> HOOK_CALLBACK_FN; struct SCallbackFNPtr { - HOOK_CALLBACK_FN* fn = nullptr; - HANDLE handle = nullptr; + std::weak_ptr<HOOK_CALLBACK_FN> fn; + HANDLE handle = nullptr; }; #define EMIT_HOOK_EVENT(name, param) \ @@ -40,21 +40,21 @@ class CHookSystemManager { public: CHookSystemManager(); - // returns the pointer to the function - HOOK_CALLBACK_FN* hookDynamic(const std::string& event, HOOK_CALLBACK_FN fn, HANDLE handle = nullptr); - void hookStatic(const std::string& event, HOOK_CALLBACK_FN* fn, HANDLE handle = nullptr); - void unhook(HOOK_CALLBACK_FN* fn); + // returns the pointer to the function. + // losing this pointer (letting it get destroyed) + // will equal to unregistering the callback. + [[nodiscard("Losing this pointer instantly unregisters the callback")]] std::shared_ptr<HOOK_CALLBACK_FN> hookDynamic(const std::string& event, HOOK_CALLBACK_FN fn, + HANDLE handle = nullptr); + void unhook(std::shared_ptr<HOOK_CALLBACK_FN> fn); - void emit(const std::vector<SCallbackFNPtr>* callbacks, SCallbackInfo& info, std::any data = 0); + void emit(std::vector<SCallbackFNPtr>* const callbacks, SCallbackInfo& info, std::any data = 0); std::vector<SCallbackFNPtr>* getVecForEvent(const std::string& event); bool m_bCurrentEventPlugin = false; jmp_buf m_jbHookFaultJumpBuf; private: - // todo: this is slow. Maybe static ptrs should be somehow allowed. unique ptr for vec? - std::list<std::pair<std::string, std::vector<SCallbackFNPtr>>> m_lpRegisteredHooks; - std::list<HOOK_CALLBACK_FN> m_lCallbackFunctions; + std::unordered_map<std::string, std::vector<SCallbackFNPtr>> m_mRegisteredHooks; }; inline std::unique_ptr<CHookSystemManager> g_pHookSystem;
\ No newline at end of file |
