-
Notifications
You must be signed in to change notification settings - Fork 4
/
vmthook.h
67 lines (53 loc) · 1.22 KB
/
vmthook.h
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 <vector>
class c_vmt
{
int count( )
{
int vfunc_count{ };
while ( m_original[vfunc_count] )
vfunc_count++;
return vfunc_count;
}
uintptr_t* m_table;
uintptr_t* m_original;
std::vector< uintptr_t > m_custom_table;
public:
c_vmt( void* table )
{
if ( !table )
return;
m_table = reinterpret_cast< uintptr_t* >( table );
m_original = *reinterpret_cast< uintptr_t** >( m_table );
int vfunc_count = count( );
for ( int i = -1; i < vfunc_count; ++i )
m_custom_table.push_back( m_original[i] );
auto data = m_custom_table.data( );
*this->m_table = uintptr_t( &data[1] );
}
~c_vmt( ) { restore( ); }
template< typename T = uintptr_t > T get_function( int index )
{
return( (T)( m_custom_table.at( index + 1 ) ) );
}
template< typename T = uintptr_t > T get_original( int index )
{
return( (T)( m_original[index] ) );
}
void hook( int index, uintptr_t new_func )
{
m_custom_table.at( index + 1 ) = new_func;
}
void unhook( int index )
{
m_custom_table.at( index + 1 ) = m_original[index];
}
void hook( int index, void* new_func )
{
hook( index, reinterpret_cast< uintptr_t >( new_func ) );
}
void restore( ) const
{
*m_table = uintptr_t( m_original );
}
};