Skip to content
This repository was archived by the owner on Nov 5, 2018. It is now read-only.

Commit

Permalink
Moved vmt methods into .cpp file
Browse files Browse the repository at this point in the history
  • Loading branch information
McSwaggens committed Feb 17, 2017
1 parent ba92060 commit 406a400
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 37 deletions.
41 changes: 41 additions & 0 deletions src/Utils/vmt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "vmt.h"

#include <algorithm>
#include <memory.h>

VMT::VMT(void* interface)
{
this->interface = reinterpret_cast<uintptr_t**>(interface);

size_t method_count = 0;

while (reinterpret_cast<uintptr_t*>(*this->interface)[method_count])
method_count++;

original_vmt = *this->interface;

vmt = new uintptr_t[sizeof(uintptr_t) * method_count];

memcpy(vmt, original_vmt, sizeof(uintptr_t) * method_count);
}

void VMT::HookVM(void* method, size_t methodIndex)
{
vmt[methodIndex] = reinterpret_cast<uintptr_t>(method);
}

template<typename Fn>
Fn VMT::GetOriginalMethod(size_t methodIndex)
{
return reinterpret_cast<Fn>(original_vmt[methodIndex]);
}

void VMT::ApplyVMT()
{
*this->interface = vmt;
}

void VMT::ReleaseVMT()
{
*this->interface = original_vmt;
}
43 changes: 6 additions & 37 deletions src/Utils/vmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,15 @@ class VMT
public:
// New virtual method table
uintptr_t** interface = nullptr;

uintptr_t* original_vmt = nullptr;

uint32_t methodCount = 0;

VMT(void* interface)
{
this->interface = reinterpret_cast<uintptr_t**>(interface);

size_t method_count = 0;

while (reinterpret_cast<uintptr_t*>(*this->interface)[method_count])
method_count++;

original_vmt = *this->interface;

vmt = new uintptr_t[sizeof(uintptr_t) * method_count];

memcpy(vmt, original_vmt, sizeof(uintptr_t) * method_count);
}

VMT(void* interface);

// Hook virtual method
void HookVM(void* method, size_t methodIndex)
{
vmt[methodIndex] = reinterpret_cast<uintptr_t>(method);
}

void HookVM(void* method, size_t methodIndex);
template<typename Fn>
Fn GetOriginalMethod(size_t methodIndex)
{
return reinterpret_cast<Fn>(original_vmt[methodIndex]);
}

void ApplyVMT()
{
*this->interface = vmt;
}

void ReleaseVMT()
{
*this->interface = original_vmt;
}
Fn GetOriginalMethod(size_t methodIndex);
void ApplyVMT();
void ReleaseVMT();
};

0 comments on commit 406a400

Please sign in to comment.