2023-02-19 21:54:53 +01:00
|
|
|
#include "HookSystemManager.hpp"
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
#include "../plugins/PluginSystem.hpp"
|
|
|
|
|
2023-02-19 21:54:53 +01:00
|
|
|
CHookSystemManager::CHookSystemManager() {
|
|
|
|
; //
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the pointer to the function
|
2024-05-05 18:16:00 +02:00
|
|
|
SP<HOOK_CALLBACK_FN> CHookSystemManager::hookDynamic(const std::string& event, HOOK_CALLBACK_FN fn, HANDLE handle) {
|
|
|
|
SP<HOOK_CALLBACK_FN> hookFN = makeShared<HOOK_CALLBACK_FN>(fn);
|
2024-04-20 21:16:42 +02:00
|
|
|
m_mRegisteredHooks[event].emplace_back(SCallbackFNPtr{.fn = hookFN, .handle = handle});
|
|
|
|
return hookFN;
|
2023-02-19 21:54:53 +01:00
|
|
|
}
|
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
void CHookSystemManager::unhook(SP<HOOK_CALLBACK_FN> fn) {
|
2024-04-20 21:16:42 +02:00
|
|
|
for (auto& [k, v] : m_mRegisteredHooks) {
|
|
|
|
std::erase_if(v, [&](const auto& other) {
|
2024-05-05 18:16:00 +02:00
|
|
|
SP<HOOK_CALLBACK_FN> fn_ = other.fn.lock();
|
2023-02-19 21:54:53 +01:00
|
|
|
|
2024-04-20 21:16:42 +02:00
|
|
|
return fn_.get() == fn.get();
|
|
|
|
});
|
2023-02-19 21:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 21:16:42 +02:00
|
|
|
void CHookSystemManager::emit(std::vector<SCallbackFNPtr>* const callbacks, SCallbackInfo& info, std::any data) {
|
2023-02-19 21:54:53 +01:00
|
|
|
if (callbacks->empty())
|
|
|
|
return;
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
std::vector<HANDLE> faultyHandles;
|
2024-06-07 18:31:27 +02:00
|
|
|
volatile bool needsDeadCleanup = false;
|
2023-02-27 13:32:38 +01:00
|
|
|
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& cb : *callbacks) {
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
m_bCurrentEventPlugin = false;
|
|
|
|
|
|
|
|
if (!cb.handle) {
|
|
|
|
// we don't guard hl hooks
|
2024-04-20 21:16:42 +02:00
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
if (SP<HOOK_CALLBACK_FN> fn = cb.fn.lock())
|
2024-04-20 21:16:42 +02:00
|
|
|
(*fn)(fn.get(), info, data);
|
|
|
|
else
|
|
|
|
needsDeadCleanup = true;
|
2023-02-27 13:32:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bCurrentEventPlugin = true;
|
|
|
|
|
|
|
|
if (std::find(faultyHandles.begin(), faultyHandles.end(), cb.handle) != faultyHandles.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try {
|
2024-04-20 21:16:42 +02:00
|
|
|
if (!setjmp(m_jbHookFaultJumpBuf)) {
|
2024-05-05 18:16:00 +02:00
|
|
|
if (SP<HOOK_CALLBACK_FN> fn = cb.fn.lock())
|
2024-04-20 21:16:42 +02:00
|
|
|
(*fn)(fn.get(), info, data);
|
|
|
|
else
|
|
|
|
needsDeadCleanup = true;
|
|
|
|
} else {
|
2023-02-27 13:32:38 +01:00
|
|
|
// this module crashed.
|
|
|
|
throw std::exception();
|
|
|
|
}
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
// TODO: this works only once...?
|
|
|
|
faultyHandles.push_back(cb.handle);
|
2023-09-20 09:26:20 +02:00
|
|
|
Debug::log(ERR, "[hookSystem] Hook from plugin {:x} caused a SIGSEGV, queueing for unloading.", (uintptr_t)cb.handle);
|
2023-02-27 13:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 21:16:42 +02:00
|
|
|
if (needsDeadCleanup)
|
|
|
|
std::erase_if(*callbacks, [](const auto& fn) { return !fn.fn.lock(); });
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
if (!faultyHandles.empty()) {
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& h : faultyHandles)
|
2023-02-27 13:32:38 +01:00
|
|
|
g_pPluginSystem->unloadPlugin(g_pPluginSystem->getPluginByHandle(h), true);
|
|
|
|
}
|
2023-02-19 21:54:53 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
std::vector<SCallbackFNPtr>* CHookSystemManager::getVecForEvent(const std::string& event) {
|
2024-04-20 21:16:42 +02:00
|
|
|
if (!m_mRegisteredHooks.contains(event))
|
|
|
|
Debug::log(LOG, "[hookSystem] New hook event registered: {}", event);
|
2023-02-19 21:54:53 +01:00
|
|
|
|
2024-04-20 21:16:42 +02:00
|
|
|
return &m_mRegisteredHooks[event];
|
2024-06-07 18:31:27 +02:00
|
|
|
}
|