summaryrefslogtreecommitdiff
path: root/src/managers/HookSystemManager.cpp
blob: c5e4cdd8afcd6f85635ceb8e99da4ed1570a43a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "HookSystemManager.hpp"

#include "../plugins/PluginSystem.hpp"

CHookSystemManager::CHookSystemManager() {
    ; //
}

// returns the pointer to the function
HOOK_CALLBACK_FN* CHookSystemManager::hookDynamic(const std::string& event, HOOK_CALLBACK_FN fn, HANDLE handle) {
    const auto PVEC = getVecForEvent(event);
    const auto PFN  = &m_lCallbackFunctions.emplace_back(fn);
    PVEC->emplace_back(SCallbackFNPtr{PFN, handle});
    return PFN;
}

void CHookSystemManager::hookStatic(const std::string& event, HOOK_CALLBACK_FN* fn, HANDLE handle) {
    const auto PVEC = getVecForEvent(event);
    PVEC->emplace_back(SCallbackFNPtr{fn, handle});
}

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 == fn; });
    }
}

void CHookSystemManager::emit(const std::vector<SCallbackFNPtr>* callbacks, std::any data) {
    if (callbacks->empty())
        return;

    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);
            Debug::log(ERR, " [hookSystem] Hook from plugin %lx caused a SIGSEGV, queueing for unloading.", cb.handle);
        }
    }

    if (!faultyHandles.empty()) {
        for (auto& h : faultyHandles)
            g_pPluginSystem->unloadPlugin(g_pPluginSystem->getPluginByHandle(h), true);
    }
}

std::vector<SCallbackFNPtr>* 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<SCallbackFNPtr>{})).second;
}