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
|
2023-02-27 13:32:38 +01:00
|
|
|
HOOK_CALLBACK_FN* CHookSystemManager::hookDynamic(const std::string& event, HOOK_CALLBACK_FN fn, HANDLE handle) {
|
2023-02-19 21:54:53 +01:00
|
|
|
const auto PVEC = getVecForEvent(event);
|
|
|
|
const auto PFN = &m_lCallbackFunctions.emplace_back(fn);
|
2023-02-27 13:32:38 +01:00
|
|
|
PVEC->emplace_back(SCallbackFNPtr{PFN, handle});
|
2023-02-19 21:54:53 +01:00
|
|
|
return PFN;
|
|
|
|
}
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
void CHookSystemManager::hookStatic(const std::string& event, HOOK_CALLBACK_FN* fn, HANDLE handle) {
|
2023-02-19 21:54:53 +01:00
|
|
|
const auto PVEC = getVecForEvent(event);
|
2023-02-27 13:32:38 +01:00
|
|
|
PVEC->emplace_back(SCallbackFNPtr{fn, handle});
|
2023-02-19 21:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CHookSystemManager::unhook(HOOK_CALLBACK_FN* fn) {
|
|
|
|
std::erase_if(m_lCallbackFunctions, [&](const auto& other) { return &other == fn; });
|
|
|
|
for (auto& [k, v] : m_lpRegisteredHooks) {
|
2023-02-27 13:32:38 +01:00
|
|
|
std::erase_if(v, [&](const auto& other) { return other.fn == fn; });
|
2023-02-19 21:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
void CHookSystemManager::emit(const std::vector<SCallbackFNPtr>* callbacks, 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;
|
|
|
|
|
|
|
|
for (auto& cb : *callbacks) {
|
|
|
|
|
|
|
|
m_bCurrentEventPlugin = false;
|
|
|
|
|
|
|
|
if (!cb.handle) {
|
|
|
|
// we don't guard hl hooks
|
|
|
|
(*cb.fn)(cb.fn, data);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bCurrentEventPlugin = true;
|
|
|
|
|
|
|
|
if (std::find(faultyHandles.begin(), faultyHandles.end(), cb.handle) != faultyHandles.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!setjmp(m_jbHookFaultJumpBuf))
|
|
|
|
(*cb.fn)(cb.fn, data);
|
|
|
|
else {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!faultyHandles.empty()) {
|
|
|
|
for (auto& h : faultyHandles)
|
|
|
|
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) {
|
2023-02-19 21:54:53 +01:00
|
|
|
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;
|
|
|
|
|
2023-09-20 09:26:20 +02:00
|
|
|
Debug::log(LOG, "[hookSystem] New hook event registered: {}", event);
|
2023-02-19 21:54:53 +01:00
|
|
|
|
2023-02-27 13:32:38 +01:00
|
|
|
return &m_lpRegisteredHooks.emplace_back(std::make_pair<>(event, std::vector<SCallbackFNPtr>{})).second;
|
2023-02-19 21:54:53 +01:00
|
|
|
}
|