This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
YimMenu/src/detour_hook.cpp
Yimura 335e9325b0
feat(logger): use AsyncLogger instead of g3log (#882)
* feat(exception_handler): skip certain exception codes
* feat(zydis): Disable BUILD_DOXYGEN and FEATURE_ENCODER
* feat(cmake): link dbghelp
* feat(exception_handler): implement stack dumper
* feat(logger): differentiate between wine and non-wine
* feat(logger): add NO_COLOR env var support
* fix(logger): fix wine logging (#960)
* feat(exception_handler): added string logging of exception
* fix(logger): exception access violation NO_COLOR

--------

Co-authored-by: Aure7138 <100095051+Aure7138@users.noreply.github.com>
Co-authored-by: tupoy-ya <72797377+tupoy-ya@users.noreply.github.com>
2023-02-08 22:36:55 +00:00

77 lines
1.9 KiB
C++

#include "common.hpp"
#include "detour_hook.hpp"
#include "memory/handle.hpp"
#include <MinHook.h>
namespace big
{
detour_hook::detour_hook(std::string name, void* detour) :
m_name(std::move(name)),
m_detour(detour)
{
}
detour_hook::detour_hook(std::string name, void* target, void* detour) :
m_name(std::move(name)),
m_target(target),
m_detour(detour)
{
create_hook();
}
void detour_hook::set_target_and_create_hook(void* target)
{
m_target = target;
create_hook();
}
void detour_hook::create_hook()
{
if (!m_target) return;
fix_hook_address();
if (auto status = MH_CreateHook(m_target, m_detour, &m_original); status != MH_OK)
throw std::runtime_error(std::format("Failed to create hook '{}' at 0x{:X} (error: {})", m_name, uintptr_t(m_target), MH_StatusToString(status)));
}
detour_hook::~detour_hook() noexcept
{
if (!m_target) return;
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 << ")";
}
void detour_hook::enable()
{
if (!m_target) return;
if (auto status = MH_QueueEnableHook(m_target); status != MH_OK)
throw std::runtime_error(std::format("Failed to enable hook 0x{:X} ({})", uintptr_t(m_target), MH_StatusToString(status)));
}
void detour_hook::disable()
{
if (!m_target) return;
if (auto status = MH_QueueDisableHook(m_target); status != MH_OK)
LOG(WARNING) << "Failed to disable hook '" << m_name << "'.";
}
DWORD exp_handler(PEXCEPTION_POINTERS exp, std::string const& name)
{
return exp->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER
: EXCEPTION_CONTINUE_SEARCH;
}
void detour_hook::fix_hook_address()
{
auto ptr = memory::handle(m_target);
while (ptr.as<std::uint8_t&>() == 0xE9)
ptr = ptr.add(1).rip();
m_target = ptr.as<void*>();
}
}