49 lines
1.5 KiB
C++
49 lines
1.5 KiB
C++
#pragma once
|
|
#include <cstdint>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
namespace FunctionHooking {
|
|
class Detour;
|
|
}
|
|
|
|
class FunctionHooking::Detour {
|
|
private:
|
|
void* source = nullptr;
|
|
void* destination = nullptr;
|
|
std::vector<uint8_t> overwritten_bytes{};
|
|
std::vector<uint8_t> hook_bytes{};
|
|
private:
|
|
void CreateHook(void* source_address, void* destination_address);
|
|
void RemoveHook();
|
|
public:
|
|
/// @returns True if our hook is present.
|
|
/// @note This allows you to know if something has overwritten our hook.
|
|
[[nodiscard]] bool Valid();
|
|
|
|
template <typename T, typename... Args>
|
|
inline typename std::enable_if<!std::is_void<T>::value, T>::type CallOriginal(Args... args) {
|
|
RemoveHook();
|
|
const T result = reinterpret_cast<T(*)(Args...)>(source)(args...);
|
|
CreateHook(source, destination);
|
|
return result;
|
|
}
|
|
|
|
template <typename T, typename... Args>
|
|
inline typename std::enable_if<std::is_void<T>::value, void>::type CallOriginal(Args... args) {
|
|
RemoveHook();
|
|
reinterpret_cast<void(*)(Args...)>(source)(args...);
|
|
CreateHook(source, destination);
|
|
}
|
|
|
|
public:
|
|
/// @returns True if the function is detour hooked.
|
|
/// @param function_address The function to check.
|
|
static bool Hooked(void* function_address);
|
|
public:
|
|
/// Create a new detour hook.
|
|
/// @param source_address The function to be hooked.
|
|
/// @param destination_address The function we are redirecting execution to.
|
|
Detour(void* source_address, void* destination_address);
|
|
~Detour();
|
|
}; |