TmpMenu/src/vmt_hook.cpp

41 lines
713 B
C++
Raw Normal View History

2019-03-21 20:18:31 +01:00
#include "vmt_hook.hpp"
#include "common.hpp"
2019-03-21 20:18:31 +01:00
namespace big
{
vmt_hook::vmt_hook(void* obj, std::size_t num_funcs) :
m_object(static_cast<void***>(obj)),
m_num_funcs(num_funcs),
m_original_table(*m_object),
m_new_table(std::make_unique<void*[]>(m_num_funcs))
2019-03-21 20:18:31 +01:00
{
2022-11-24 21:49:05 +00:00
std::copy_n(m_original_table, m_num_funcs, m_new_table.get());
}
vmt_hook::~vmt_hook()
{
disable();
2019-03-21 20:18:31 +01:00
}
void vmt_hook::hook(std::size_t index, void* func)
{
2022-11-24 21:49:05 +00:00
m_new_table[index] = func;
2019-03-21 20:18:31 +01:00
}
void vmt_hook::unhook(std::size_t index)
{
2022-11-24 21:49:05 +00:00
m_new_table[index] = m_original_table[index];
2019-03-21 20:18:31 +01:00
}
void vmt_hook::enable()
{
2022-11-24 21:49:05 +00:00
*m_object = m_new_table.get();
2019-03-21 20:18:31 +01:00
}
void vmt_hook::disable()
{
*m_object = m_original_table;
}
}