summaryrefslogtreecommitdiff
path: root/src/managers/HookSystemManager.cpp
diff options
context:
space:
mode:
authorVaxry <43317083+vaxerski@users.noreply.github.com>2023-02-19 20:54:53 +0000
committerGitHub <noreply@github.com>2023-02-19 20:54:53 +0000
commit1b56cc4e99c391a94fc05a24e1f71c8e425168f1 (patch)
tree110cf0d60d9f01bcbcf3624458afa5ededb5fe8a /src/managers/HookSystemManager.cpp
parent6e16627cbcb5d4712195c97e106f2198076c8aff (diff)
Added an Event Hook System (#1578)
* added an eventHookSystem * Add all socket2 events to hooks
Diffstat (limited to 'src/managers/HookSystemManager.cpp')
-rw-r--r--src/managers/HookSystemManager.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/managers/HookSystemManager.cpp b/src/managers/HookSystemManager.cpp
new file mode 100644
index 00000000..02775a24
--- /dev/null
+++ b/src/managers/HookSystemManager.cpp
@@ -0,0 +1,44 @@
+#include "HookSystemManager.hpp"
+
+CHookSystemManager::CHookSystemManager() {
+ ; //
+}
+
+// returns the pointer to the function
+HOOK_CALLBACK_FN* CHookSystemManager::hookDynamic(const std::string& event, HOOK_CALLBACK_FN fn) {
+ const auto PVEC = getVecForEvent(event);
+ const auto PFN = &m_lCallbackFunctions.emplace_back(fn);
+ PVEC->emplace_back(PFN);
+ return PFN;
+}
+
+void CHookSystemManager::hookStatic(const std::string& event, HOOK_CALLBACK_FN* fn) {
+ const auto PVEC = getVecForEvent(event);
+ PVEC->emplace_back(fn);
+}
+
+void CHookSystemManager::unhook(HOOK_CALLBACK_FN* fn) {
+ std::erase_if(m_lCallbackFunctions, [&](const auto& other) { return &other == fn; });
+ for (auto& [k, v] : m_lpRegisteredHooks) {
+ std::erase_if(v, [&](const auto& other) { return other == fn; });
+ }
+}
+
+void CHookSystemManager::emit(const std::vector<HOOK_CALLBACK_FN*>* callbacks, std::any data) {
+ if (callbacks->empty())
+ return;
+
+ for (auto& cb : *callbacks)
+ (*cb)(cb, data);
+}
+
+std::vector<HOOK_CALLBACK_FN*>* CHookSystemManager::getVecForEvent(const std::string& event) {
+ auto IT = std::find_if(m_lpRegisteredHooks.begin(), m_lpRegisteredHooks.end(), [&](const auto& other) { return other.first == event; });
+
+ if (IT != m_lpRegisteredHooks.end())
+ return &IT->second;
+
+ Debug::log(LOG, "[hookSystem] New hook event registered: %s", event.c_str());
+
+ return &m_lpRegisteredHooks.emplace_back(std::make_pair<>(event, std::vector<HOOK_CALLBACK_FN*>{})).second;
+} \ No newline at end of file