2019-03-21 20:18:31 +01:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "detour_hook.hpp"
|
|
|
|
#include "logger.hpp"
|
|
|
|
#include "memory/handle.hpp"
|
2020-02-22 18:37:42 -05:00
|
|
|
#include <..\MinHook\include\MinHook.h>
|
2019-03-21 20:18:31 +01:00
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
2022-10-30 19:32:51 +01:00
|
|
|
detour_hook::detour_hook(std::string name, void* detour) :
|
|
|
|
m_name(std::move(name)),
|
|
|
|
m_detour(detour)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-22 18:37:42 -05:00
|
|
|
detour_hook::detour_hook(std::string name, void* target, void* detour) :
|
2019-03-21 20:18:31 +01:00
|
|
|
m_name(std::move(name)),
|
|
|
|
m_target(target),
|
|
|
|
m_detour(detour)
|
2022-10-30 19:32:51 +01:00
|
|
|
{
|
|
|
|
create_hook();
|
|
|
|
}
|
|
|
|
|
|
|
|
void detour_hook::set_target_and_create_hook(void* target)
|
|
|
|
{
|
|
|
|
m_target = target;
|
|
|
|
create_hook();
|
|
|
|
}
|
|
|
|
|
|
|
|
void detour_hook::create_hook()
|
2019-03-21 20:18:31 +01:00
|
|
|
{
|
|
|
|
fix_hook_address();
|
2022-08-09 14:39:55 -04:00
|
|
|
if (auto status = MH_CreateHook(m_target, m_detour, &m_original); status != MH_OK)
|
2022-10-24 14:08:37 +02:00
|
|
|
throw std::runtime_error(std::format("Failed to create hook '{}' at 0x{:X} (error: {})", m_name, uintptr_t(m_target), MH_StatusToString(status)));
|
2019-03-21 20:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
detour_hook::~detour_hook() noexcept
|
|
|
|
{
|
2022-08-09 14:39:55 -04:00
|
|
|
if (auto status = MH_RemoveHook(m_target); status != MH_OK)
|
|
|
|
LOG(FATAL) << "Failed to remove hook '" << m_name << "' at 0x" << HEX_TO_UPPER(uintptr_t(m_target)) << "(error: " << m_name << ")";
|
2019-03-21 20:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void detour_hook::enable()
|
|
|
|
{
|
2022-08-09 14:39:55 -04:00
|
|
|
if (auto status = MH_QueueEnableHook(m_target); status != MH_OK)
|
2022-10-24 14:08:37 +02:00
|
|
|
throw std::runtime_error(std::format("Failed to enable hook 0x{:X} ({})", uintptr_t(m_target), MH_StatusToString(status)));
|
2019-03-21 20:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void detour_hook::disable()
|
|
|
|
{
|
2022-08-09 14:39:55 -04:00
|
|
|
if (auto status = MH_QueueDisableHook(m_target); status != MH_OK)
|
2020-02-22 18:37:42 -05:00
|
|
|
LOG(WARNING) << "Failed to disable hook '" << m_name << "'.";
|
2019-03-21 20:18:31 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 18:37:42 -05:00
|
|
|
DWORD exp_handler(PEXCEPTION_POINTERS exp, std::string const& name)
|
2019-03-21 20:18:31 +01:00
|
|
|
{
|
|
|
|
return exp->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION
|
|
|
|
? EXCEPTION_EXECUTE_HANDLER
|
|
|
|
: EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
void detour_hook::fix_hook_address()
|
|
|
|
{
|
2022-07-05 16:54:45 -04:00
|
|
|
__try {
|
2019-03-21 20:18:31 +01:00
|
|
|
auto ptr = memory::handle(m_target);
|
|
|
|
while (ptr.as<std::uint8_t&>() == 0xE9)
|
|
|
|
ptr = ptr.add(1).rip();
|
|
|
|
m_target = ptr.as<void*>();
|
|
|
|
}
|
2022-07-05 16:54:45 -04:00
|
|
|
__except (exp_handler(GetExceptionInformation(), m_name)) {
|
|
|
|
[this]() {
|
2022-10-24 14:08:37 +02:00
|
|
|
throw std::runtime_error(std::format("Failed to fix hook address for '{}'", m_name));
|
2019-03-21 20:18:31 +01:00
|
|
|
}();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|