mirror of
https://github.com/Jous99/F4MP.git
synced 2026-01-12 08:40:53 +01:00
sibir hook.h y gameprt.h modificados
This commit is contained in:
parent
7d805879b4
commit
e99cfe5c24
2 changed files with 203 additions and 0 deletions
81
Client/common/include/GamePtr.h
Normal file
81
Client/common/include/GamePtr.h
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#ifndef COMMON_GAMEPTR_H
|
||||
#define COMMON_GAMEPTR_H
|
||||
|
||||
#include <Windows.h>
|
||||
#include <utility>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Memory {
|
||||
|
||||
class GamePtr_Manager {
|
||||
public:
|
||||
GamePtr_Manager();
|
||||
|
||||
static uintptr_t s_baseAddress;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class GamePtr {
|
||||
public :
|
||||
GamePtr(uintptr_t offset) : offset(offset + Memory::GamePtr_Manager::s_baseAddress){}
|
||||
|
||||
T * GetPtr() const {
|
||||
return reinterpret_cast <T *>(offset);
|
||||
}
|
||||
|
||||
T * operator->() const
|
||||
{
|
||||
return GetPtr();
|
||||
}
|
||||
|
||||
operator T *() const
|
||||
{
|
||||
return GetPtr();
|
||||
}
|
||||
|
||||
bool operator!() const {
|
||||
return !GetPtr();
|
||||
}
|
||||
|
||||
const T * GetConstPtr() const {
|
||||
return reinterpret_cast <T *>(offset);
|
||||
}
|
||||
|
||||
uintptr_t GetUIntPtr() const
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
private:
|
||||
uintptr_t offset;
|
||||
|
||||
GamePtr();
|
||||
GamePtr(GamePtr & rhs);
|
||||
GamePtr & operator=(GamePtr & rhs);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class GameAddr{
|
||||
public:
|
||||
GameAddr(uintptr_t offset) : offset(reinterpret_cast<ConversionType *>(offset + Memory::GamePtr_Manager::s_baseAddress)){}
|
||||
|
||||
operator T() {
|
||||
return reinterpret_cast<T>(offset);
|
||||
}
|
||||
|
||||
uintptr_t GetUIntPtr(){
|
||||
return reinterpret_cast<uintptr_t >(offset);
|
||||
}
|
||||
private:
|
||||
struct ConversionType {};
|
||||
|
||||
ConversionType * offset;
|
||||
|
||||
GameAddr();
|
||||
GameAddr(GameAddr & rhs);
|
||||
GameAddr & operator=(GameAddr & rhs);
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
122
Client/common/include/Hook.h
Normal file
122
Client/common/include/Hook.h
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#ifndef COMMON_HOOK_H
|
||||
#define COMMON_HOOK_H
|
||||
|
||||
#include <Windows.h>
|
||||
#include <cstdint>
|
||||
#include "detours.h" // Asegúrate de tener detours.lib en tu proyecto
|
||||
#include "Types.h"
|
||||
#include "Exceptions.h"
|
||||
|
||||
namespace Hooks {
|
||||
|
||||
enum class CallConvention
|
||||
{
|
||||
stdcall_t,
|
||||
cdecl_t,
|
||||
fastcall_t // Añadido fastcall por si interceptas funciones internas del engine
|
||||
};
|
||||
|
||||
// Helper para definir la convención de llamada
|
||||
template <CallConvention cc, typename retn, typename ...args>
|
||||
struct convention;
|
||||
|
||||
template <typename retn, typename ...args>
|
||||
struct convention<CallConvention::stdcall_t, retn, args...>
|
||||
{
|
||||
typedef retn (__stdcall *type)(args ...);
|
||||
};
|
||||
|
||||
template <typename retn, typename ...args>
|
||||
struct convention<CallConvention::cdecl_t, retn, args...>
|
||||
{
|
||||
typedef retn (__cdecl *type)(args ...);
|
||||
};
|
||||
|
||||
// Fallout 4 usa mucho fastcall (__rcall en x64)
|
||||
template <typename retn, typename ...args>
|
||||
struct convention<CallConvention::fastcall_t, retn, args...>
|
||||
{
|
||||
typedef retn (__fastcall *type)(args ...);
|
||||
};
|
||||
|
||||
template <CallConvention cc, typename retn, typename ...args>
|
||||
class Hook
|
||||
{
|
||||
typedef typename convention<cc, retn, args...>::type type;
|
||||
|
||||
uintptr_t orig_; // Usamos uintptr_t para compatibilidad x64
|
||||
type detour_;
|
||||
|
||||
bool is_applied_;
|
||||
bool has_open_transaction_;
|
||||
|
||||
void transaction_begin()
|
||||
{
|
||||
if (DetourTransactionBegin() != NO_ERROR)
|
||||
throw Exceptions::Core::Exceptions::DetourException("A pending transaction already exists");
|
||||
has_open_transaction_ = true;
|
||||
}
|
||||
|
||||
void transaction_commit()
|
||||
{
|
||||
if (DetourTransactionCommit() != NO_ERROR)
|
||||
throw Exceptions::Core::Exceptions::DetourException("Error committing detour transaction");
|
||||
has_open_transaction_ = false;
|
||||
}
|
||||
|
||||
static void update_thread(HANDLE hThread)
|
||||
{
|
||||
DetourUpdateThread(hThread);
|
||||
}
|
||||
|
||||
public:
|
||||
Hook() : orig_(0), detour_(0), is_applied_(false), has_open_transaction_(false) {}
|
||||
|
||||
~Hook()
|
||||
{
|
||||
if (has_open_transaction_) DetourTransactionAbort();
|
||||
remove();
|
||||
}
|
||||
|
||||
// pFunc ahora acepta uintptr_t para que funcione con GameAddr::GetUIntPtr()
|
||||
void apply(uintptr_t pFunc, type detour)
|
||||
{
|
||||
if (is_applied_) return;
|
||||
|
||||
detour_ = detour;
|
||||
orig_ = pFunc;
|
||||
|
||||
transaction_begin();
|
||||
update_thread(GetCurrentThread());
|
||||
|
||||
// Detours requiere un puntero al puntero de la función original
|
||||
DetourAttach(reinterpret_cast<void **>(&orig_), reinterpret_cast<void *>(detour_));
|
||||
|
||||
transaction_commit();
|
||||
is_applied_ = true;
|
||||
}
|
||||
|
||||
void remove()
|
||||
{
|
||||
if (!is_applied_) return;
|
||||
|
||||
transaction_begin();
|
||||
update_thread(GetCurrentThread());
|
||||
DetourDetach(reinterpret_cast<void **>(&orig_), reinterpret_cast<void *>(detour_));
|
||||
transaction_commit();
|
||||
|
||||
is_applied_ = false;
|
||||
}
|
||||
|
||||
// Esta es la función que llamas desde tu lambda en main.cpp
|
||||
retn call_orig(args ... p)
|
||||
{
|
||||
return reinterpret_cast<type>(orig_)(p...);
|
||||
}
|
||||
|
||||
uintptr_t get_orig() const { return orig_; }
|
||||
bool is_applied() const { return is_applied_; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Reference in a new issue