2023-02-27 13:32:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#define HANDLE void*
|
|
|
|
|
|
|
|
class CFunctionHook {
|
|
|
|
public:
|
|
|
|
CFunctionHook(HANDLE owner, void* source, void* destination);
|
|
|
|
~CFunctionHook();
|
|
|
|
|
|
|
|
bool hook();
|
|
|
|
bool unhook();
|
|
|
|
|
2023-12-06 23:54:56 +01:00
|
|
|
CFunctionHook(const CFunctionHook&) = delete;
|
|
|
|
CFunctionHook(CFunctionHook&&) = delete;
|
2023-02-27 13:32:38 +01:00
|
|
|
CFunctionHook& operator=(const CFunctionHook&) = delete;
|
2023-12-06 23:54:56 +01:00
|
|
|
CFunctionHook& operator=(CFunctionHook&&) = delete;
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
void* m_pOriginal = nullptr;
|
|
|
|
|
|
|
|
private:
|
2023-02-27 19:34:44 +01:00
|
|
|
void* m_pSource = nullptr;
|
|
|
|
void* m_pFunctionAddr = nullptr;
|
|
|
|
void* m_pTrampolineAddr = nullptr;
|
|
|
|
void* m_pDestination = nullptr;
|
|
|
|
size_t m_iHookLen = 0;
|
|
|
|
size_t m_iTrampoLen = 0;
|
|
|
|
HANDLE m_pOwner = nullptr;
|
|
|
|
bool m_bActive = false;
|
|
|
|
|
|
|
|
std::vector<std::pair<size_t, std::string>> m_vTrampolineRIPUses;
|
|
|
|
|
2023-02-27 20:17:58 +01:00
|
|
|
void* m_pOriginalBytes = nullptr;
|
|
|
|
|
2023-02-27 19:34:44 +01:00
|
|
|
size_t probeMinimumJumpSize(void* start, size_t min);
|
|
|
|
size_t getInstructionLenAt(void* start);
|
2023-02-27 13:32:38 +01:00
|
|
|
|
|
|
|
friend class CHookSystem;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CHookSystem {
|
|
|
|
public:
|
|
|
|
CFunctionHook* initHook(HANDLE handle, void* source, void* destination);
|
|
|
|
bool removeHook(CFunctionHook* hook);
|
|
|
|
|
|
|
|
void removeAllHooksFrom(HANDLE handle);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::unique_ptr<CFunctionHook>> m_vHooks;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline std::unique_ptr<CHookSystem> g_pFunctionHookSystem;
|