36 lines
1.2 KiB
C++
36 lines
1.2 KiB
C++
#pragma once
|
|
#include <cstdint>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
namespace FunctionHooking {
|
|
class Detour {
|
|
private:
|
|
void* source = nullptr; //The function to be hooked.
|
|
void* destination = nullptr; //The function we are redirecting execution to.
|
|
|
|
std::vector<uint8_t> overwritten_bytes{};
|
|
std::vector<uint8_t> hook_bytes{};
|
|
void createHook(void* source_address, void* destination_address);
|
|
public:
|
|
void removeHook();
|
|
Detour(void* source_address, void* destination_address);
|
|
Detour() = default;
|
|
|
|
//TODO This is *technically* not thread safe.
|
|
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);
|
|
}
|
|
};
|
|
} |