iw4x-client/iw4/Utils/Hooking.hpp

104 lines
2.2 KiB
C++
Raw Normal View History

2015-12-23 08:45:53 -05:00
#pragma once
#define HOOK_JUMP true
#define HOOK_CALL false
namespace Utils
{
class Hook
{
public:
Hook() : Place(nullptr), Stub(nullptr), Initialized(false), Installed(false), UseJump(false) { ZeroMemory(Hook::Buffer, sizeof(Hook::Buffer)); }
Hook(void* place, void* stub, bool useJump = true) : Hook() { Hook::Initialize(place, stub, useJump); }
Hook(DWORD place, void* stub, bool useJump = true) : Hook((void*)place, stub, useJump) {}
~Hook();
Hook* Initialize(void* place, void* stub, bool useJump = true);
Hook* Initialize(DWORD place, void* stub, bool useJump = true);
Hook* Install();
Hook* Uninstall();
void* GetAddress();
void Quick();
2015-12-26 21:56:00 -05:00
template <typename T> static T Call(DWORD function)
{
__asm
{
mov eax, function
call eax
}
}
2015-12-23 08:45:53 -05:00
static void Nop(void* place, size_t length);
static void Nop(DWORD place, size_t length);
template <typename T> static void Set(void* place, T value)
{
*(T*)place = value;
FlushInstructionCache(GetCurrentProcess(), place, sizeof(T));
}
template <typename T> static void Set(DWORD place, T value)
{
return Set<T>((void*)place, value);
}
2015-12-23 09:00:01 -05:00
template <typename T> static void Xor(void* place, T value)
{
*(T*)place ^= value;
FlushInstructionCache(GetCurrentProcess(), place, sizeof(T));
}
template <typename T> static void Xor(DWORD place, T value)
{
return Xor<T>((void*)place, value);
}
template <typename T> static void Or(void* place, T value)
{
*(T*)place |= value;
FlushInstructionCache(GetCurrentProcess(), place, sizeof(T));
}
template <typename T> static void Or(DWORD place, T value)
{
return Or<T>((void*)place, value);
}
template <typename T> static void And(void* place, T value)
{
*(T*)place &= value;
FlushInstructionCache(GetCurrentProcess(), place, sizeof(T));
}
template <typename T> static void And(DWORD place, T value)
{
return And<T>((void*)place, value);
}
2015-12-23 08:45:53 -05:00
template <typename T> static T Get(void* place)
{
return *(T*)place;
}
template <typename T> static T Get(DWORD place)
{
return Get<T>((void*)place);
}
private:
bool Initialized;
bool Installed;
void* Place;
void* Stub;
2015-12-23 11:12:15 -05:00
void* Original;
2015-12-23 08:45:53 -05:00
char Buffer[5];
bool UseJump;
std::mutex StateMutex;
};
}