-
Notifications
You must be signed in to change notification settings - Fork 2
/
detourhook.hpp
92 lines (79 loc) · 1.75 KB
/
detourhook.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Created by BenCat07 on
* 11th of May 2020
*
*/
#pragma once
#include <cstdint>
#include "tier0/valve_minmax_off.h"
#include <memory>
#include "tier0/valve_minmax_on.h"
#include "bytepatch.hpp"
#define foffset(p, i) ( (unsigned char *) (&p))[i]
class DetourHook
{
uintptr_t original_address;
std::unique_ptr<BytePatch> patch;
void *hook_fn;
public:
DetourHook()
{
}
DetourHook(uintptr_t original, void *hook_func)
{
Init(original, hook_func);
}
void Init(uintptr_t original, void *hook_func)
{
original_address = original;
hook_fn = hook_func;
uintptr_t rel_addr = ((uintptr_t) hook_func - ((uintptr_t) original_address)) - 5;
patch.reset(new BytePatch
(
original,
{
0xE9,
foffset(rel_addr, 0),
foffset(rel_addr, 1),
foffset(rel_addr, 2),
foffset(rel_addr, 3)
}
));
InitBytepatch();
}
void InitBytepatch()
{
if (patch)
{
(*patch).Patch();
}
}
void Shutdown()
{
if (patch)
{
(*patch).Shutdown();
}
}
~DetourHook()
{
Shutdown();
}
// Gets the original function with no patches
void *GetOriginalFunc() const
{
if (patch)
{
// Unpatch
(*patch).Shutdown();
return (void *) original_address;
}
// No patch, no func.
return nullptr;
}
// Restore the patches
inline void RestorePatch()
{
InitBytepatch();
}
};