forked from leftspace89/pPlat
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadDetour.hpp
More file actions
172 lines (144 loc) · 3.34 KB
/
adDetour.hpp
File metadata and controls
172 lines (144 loc) · 3.34 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#pragma once
#pragma comment(lib, "detours.lib")
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <type_traits>
#include <vector>
#include <detours.h>
using namespace std;
namespace pPlat
{
namespace Native
{
enum class convention_type
{
stdcall_t,
cdecl_t,
thiscall_t,
fastcall_t,
winapi_t,
thiscall_r
};
enum class convention_type_n
{
winapi_t
};
template <convention_type tp, typename retn, typename convention, typename ...args>
struct convention;
template <typename retn, typename ...args>
struct convention<convention_type::stdcall_t, retn, args...>
{
typedef retn(__stdcall *type)(args ...);
};
template <typename retn, typename ...args>
struct convention<convention_type::cdecl_t, retn, args...>
{
typedef retn(__cdecl *type)(args ...);
};
template <typename retn, typename ...args>
struct convention<convention_type::thiscall_t, retn, args...>
{
typedef retn(__stdcall *type)(args ...);
};
template <typename retn, typename ...args>
struct convention<convention_type::fastcall_t, retn, args...>
{
typedef retn(__fastcall *type)(args ...);
};
template <typename retn, typename ...args>
struct convention<convention_type::winapi_t, retn, args...>
{
typedef retn(WINAPI *type)(args ...);
};
template <typename retn, typename ...args>
struct convention<convention_type::thiscall_r, retn, args...>
{
typedef retn(__thiscall *type)(args ...);
};
typedef struct DetourEntry
{
void* orig;
void* target;
void* detour;
char* fncName;
DetourEntry(void* o, void* t, void* d, char* f) : orig(o), target(t), detour(d), fncName(f) {}
} DetourEntry;
class Detour
{
private:
std::vector<DetourEntry> m_detourList;
public:
void* Install(void* target, void* detour, char* fncName);
bool RemoveHooks() const;
};
template <DWORD ptr,convention_type tp, typename retn, typename ...args>
class MAKE_CALL
{
typedef typename convention<tp, retn, args...>::type type;
public:
type _orig;
MAKE_CALL() : _orig(0)
{
DWORD mdd = (DWORD)GetModuleHandleA(0) + ptr;
_orig = (type)mdd;
}
retn Call(args ... p)
{
return _orig(p...);
}
};
template <convention_type tp, typename retn, typename ...args>
class MAKE_HOOK
{
typedef typename convention<tp, retn, args...>::type type;
bool _isApplied;
public:
type _orig;
type _detour;
MAKE_HOOK() : _isApplied(false), _orig(0), _detour(0)
{
}
template <typename T>
MAKE_HOOK(T pFunc, type detour)
{
Apply<T>(pFunc, detour);
}
~MAKE_HOOK()
{
Remove();
}
template <typename T>
void Apply(T pFunc, type detour)
{
static auto Instance = new Detour();
_detour = detour;
_orig = (type)Instance->Install((void*)pFunc, (void*)_detour, "null_xdkappa");
_isApplied = _orig != nullptr;
if (_orig == nullptr)
{
//Console::PrintLn("Faulty Hook, aborting: %s @%08x-%08x", __FUNCTION__, pFunc, _detour);
}
}
bool Remove()
{
if (!_isApplied)
return false;
_isApplied = false;
//return DetourRemove((PBYTE)_orig, (PBYTE)_detour) > 0;
return true;
}
retn CallOriginal(args ... p)
{
return _orig(p...);
}
retn CallDetour(args ... p)
{
return _detour(p...);
}
bool IsApplied() const
{
return _isApplied;
}
};
}
}