forked from Detegr/openRBRVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hook.hpp
67 lines (62 loc) · 1.34 KB
/
Hook.hpp
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
#pragma once
#include <MinHook.h>
#include <stdexcept>
// RAII wrapper for MinHook
template <typename T>
struct Hook {
T call;
T src;
explicit Hook()
: call(nullptr)
, src(nullptr)
{
}
explicit Hook(T src, T tgt)
: src(src)
{
if (MH_CreateHook(reinterpret_cast<void*>(src), reinterpret_cast<void*>(tgt), reinterpret_cast<void**>(&call)) != MH_OK) {
throw std::exception("Could not hook");
}
Enable();
}
void Enable()
{
if (MH_EnableHook(src) != MH_OK) {
throw std::exception("Could not disable hook");
}
}
void Disable()
{
if (MH_DisableHook(src) != MH_OK) {
throw std::exception("Could not disable hook");
}
}
Hook(const Hook&) = delete;
Hook(Hook&& rhs)
{
call = rhs.call;
src = rhs.src;
rhs.call = nullptr;
rhs.src = nullptr;
return *this;
}
Hook& operator=(const Hook&) = delete;
Hook& operator=(Hook&& rhs) noexcept
{
call = rhs.call;
src = rhs.src;
rhs.call = nullptr;
rhs.src = nullptr;
return *this;
}
~Hook()
{
if (src)
MH_DisableHook(src);
}
};
template <typename T, typename F>
T* GetVtable(F* obj)
{
return (T*)(*(uintptr_t*)obj);
}