Skip to content

Commit

Permalink
fully migrate assembly to safetyhook
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirteenAG committed Jan 23, 2024
1 parent f9bf647 commit 8fdfc2e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 39 deletions.
1 change: 0 additions & 1 deletion source/common.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "injector/injector.hpp"
#include "injector/calling.hpp"
#include "injector/hooking.hpp"
#include "injector/assembly.hpp"
#include "includes/assembly2.hpp"
#include "injector/utility.hpp"
#include "Hooking.Patterns.h"
Expand Down
4 changes: 2 additions & 2 deletions source/extrainfo.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public:
{
struct MS_PAUSED_HOOK
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
static std::wstring extra = L"";
regs.eax += 0x78;
Expand All @@ -50,7 +50,7 @@ public:
regs.eax = (uintptr_t)extra.c_str();
}
}
}; injector::MakeInline2<MS_PAUSED_HOOK>(pattern.get_first(0));
}; injector::MakeInline<MS_PAUSED_HOOK>(pattern.get_first(0));
}
}
};
Expand Down
6 changes: 3 additions & 3 deletions source/fixes.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public:
static auto nTimeToWaitBeforeCenteringCameraOnFootPad = FusionFixSettings.GetRef("PREF_PADCAMCENTERDELAY");
struct OnFootCamCenteringHook
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
static float f = 0.0f;
f = regs.xmm1.f32[0];
Expand Down Expand Up @@ -241,10 +241,10 @@ public:
};

if (reg != 0x48)
injector::MakeInline2<OnFootCamCenteringHook>(pattern.get_first(0), pattern.get_first(6));
injector::MakeInline<OnFootCamCenteringHook>(pattern.get_first(0), pattern.get_first(6));
else
{
injector::MakeInline2<OnFootCamCenteringHook>(pattern.get_first(-2), pattern.get_first(6));
injector::MakeInline<OnFootCamCenteringHook>(pattern.get_first(-2), pattern.get_first(6));
injector::WriteMemory<uint16_t>(pattern.get_first(3), 0xDB85, true);
}
}
Expand Down
16 changes: 8 additions & 8 deletions source/frameratevigilante.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public:
{
struct FramerateVigilanteHook1
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
float f = std::clamp(*(float*)(regs.ebp + 0x08), 1.0f / 150.0f, FLT_MAX);
*(float*)(regs.ebp + 0x08) = f;
regs.xmm0.f32[0] = f;
}
}; injector::MakeInline2<FramerateVigilanteHook1>(pattern.get_first(0));
}; injector::MakeInline<FramerateVigilanteHook1>(pattern.get_first(0));
}
else
{
Expand All @@ -57,35 +57,35 @@ public:
static auto f1032790 = *pattern.get_first<float*>(4);
struct LoadingTextSpeed
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
regs.xmm0.f32[0] = (*f1032790) / 10.0f;
}
}; injector::MakeInline2<LoadingTextSpeed>(pattern.get_first(0), pattern.get_first(8));
}; injector::MakeInline<LoadingTextSpeed>(pattern.get_first(0), pattern.get_first(8));
}

pattern = hook::pattern("F3 0F 59 05 ? ? ? ? F3 0F 59 05 ? ? ? ? F3 0F 59 05 ? ? ? ? F3 0F 58 05 ? ? ? ? F3 0F 11 05");
if (!pattern.empty())
{
struct LoadingTextSpeed2
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
regs.xmm0.f32[0] *= (1000.0f) / 10.0f;
}
}; injector::MakeInline2<LoadingTextSpeed2>(pattern.get_first(0), pattern.get_first(8));
}; injector::MakeInline<LoadingTextSpeed2>(pattern.get_first(0), pattern.get_first(8));
}

pattern = hook::pattern("F3 0F 58 0D ? ? ? ? 0F 5B C0 F3 0F 11 0D");
if (!pattern.empty())
{
struct LoadingTextSparks
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
regs.xmm1.f32[0] += (0.085f) / 10.0f;
}
}; injector::MakeInline2<LoadingTextSparks>(pattern.get_first(0), pattern.get_first(8));
}; injector::MakeInline<LoadingTextSparks>(pattern.get_first(0), pattern.get_first(8));
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions source/ikeeponwalking.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public:
pattern = hook::pattern("80 F9 7F 76 57");
struct SprintHook
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
if ((*(uint8_t*)(regs.eax + 4) ^ *(uint8_t*)(regs.eax + 6)) <= 127)
{
Expand Down Expand Up @@ -58,7 +58,7 @@ public:
else if (shouldRun && !GetAsyncKeyState(nWalkKey)) // hold
*(float*)(regs.esp + (flag ? 0x18 : 0x1C)) = 1.0f;
}
}; injector::MakeInline2<SprintHook>(pattern.get_first(0));
}; injector::MakeInline<SprintHook>(pattern.get_first(0));

pattern = find_pattern("77 5F 8B 8E", "77 46 8B 8F");
static raw_mem GamepadCB(pattern.get_first(0), { 0x90, 0x90 }); // NOP
Expand Down
80 changes: 61 additions & 19 deletions source/includes/assembly2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@

namespace injector
{
// Lowest level stuff (actual assembly) goes on the following namespace
// PRIVATE! Skip this, not interesting for you.
namespace injector_asm2
using reg_pack = SafetyHookContext;
namespace injector_asm
{
// Wrapper functor, so the assembly can use some templating
template<class T>
struct wrapper
{
static void call(SafetyHookContext* regs)
static void call(reg_pack* regs)
{
T fun; fun(*regs);
}
};

// Constructs a SafetyHookContext and calls the wrapper functor
// Constructs a reg_pack and calls the wrapper functor
template<class W> // where W is of type wrapper
inline void make_SafetyHookContext_and_call(memory_pointer_tr at)
inline void make_reg_pack_and_call(memory_pointer_tr at)
{
static std::unique_ptr<SafetyHookMid> pack;
auto m = safetyhook::create_mid(at.get<void>(), [](SafetyHookContext& ctx)
static std::vector<SafetyHookMid> pack;
auto m = safetyhook::create_mid(at.get<void>(), [](reg_pack& ctx)
{
W::call(&ctx);
});
pack.reset(new SafetyHookMid(std::move(m)));
pack.emplace_back(std::move(m));
}
};

Expand All @@ -37,23 +36,23 @@ namespace injector
* Makes inline assembly (but not assembly, an actual functor of type FuncT) at address
*/
template<class FuncT>
void MakeInline2(memory_pointer_tr at)
void MakeInline(memory_pointer_tr at)
{
MakeNOP(at, 5);
typedef injector_asm2::wrapper<FuncT> functor;
typedef injector_asm::wrapper<FuncT> functor;
if(false) functor::call(nullptr); // To instantiate the template, if not done _asm will fail
injector_asm2::make_SafetyHookContext_and_call<functor>(at);
injector_asm::make_reg_pack_and_call<functor>(at);
}

/*
* MakeInline
* Same as above, but it NOPs everything between at and end (exclusive), then performs MakeInline
*/
template<class FuncT>
void MakeInline2(memory_pointer_tr at, memory_pointer_tr end)
void MakeInline(memory_pointer_tr at, memory_pointer_tr end)
{
MakeRangedNOP(at, end);
MakeInline2<FuncT>(at);
MakeInline<FuncT>(at);
}

/*
Expand All @@ -62,29 +61,72 @@ namespace injector
* On this case the functor can be passed as argument since there will be one func instance for each at,end not just for each FuncT
*/
template<uintptr_t at, uintptr_t end, class FuncT>
void MakeInline2(FuncT func)
void MakeInline(FuncT func)
{
static std::unique_ptr<FuncT> static_func;
static_func.reset(new FuncT(std::move(func)));

// Encapsulates the call to static_func
struct Caps
{
void operator()(SafetyHookContext& regs)
void operator()(reg_pack& regs)
{ (*static_func)(regs); }
};

// Does the actual MakeInline
return MakeInline2<Caps>(lazy_pointer<at>::get(), lazy_pointer<end>::get());
return MakeInline<Caps>(lazy_pointer<at>::get(), lazy_pointer<end>::get());
}

/*
* MakeInline
* Same as above, but (end) is calculated by the length of a call instruction
*/
template<uintptr_t at, class FuncT>
void MakeInline2(FuncT func)
void MakeInline(FuncT func)
{
return MakeInline2<at, at+5, FuncT>(func);
MakeNOP(at, 5);
return MakeInline<at, at+5, FuncT>(func);
}
};

namespace injector
{
static inline constexpr auto JMPSIZE = 14;
static inline constexpr auto CALLSIZE = 16;

inline injector::memory_pointer_raw MakeAbsCALL64(injector::memory_pointer_tr at, injector::memory_pointer_raw dest, bool vp = true)
{
injector::WriteMemory<uint16_t>(at, 0x15FF, vp);
injector::WriteMemory<uint32_t>(at + sizeof(uint16_t), 2, vp);
injector::WriteMemory<uint16_t>(at + sizeof(uint16_t) + sizeof(uint32_t), 0x08EB, vp);
injector::WriteMemory<uint64_t>(at + sizeof(uint16_t) + sizeof(uint32_t) + sizeof(uint16_t), dest.as_int(), vp);
return at.as_int() + CALLSIZE;
}

inline injector::memory_pointer_raw MakeAbsJMP64(injector::memory_pointer_tr at, injector::memory_pointer_raw dest, bool vp = true)
{
injector::WriteMemory<uint16_t>(at, 0x25FF, vp);
injector::WriteMemory<uint32_t>(at + sizeof(uint16_t), 0, vp);
injector::WriteMemory<uint64_t>(at + sizeof(uint16_t) + sizeof(uint32_t), dest.as_int(), vp);
return at.as_int() + JMPSIZE;
}

inline injector::memory_pointer_raw ReadRelativeAddress(memory_pointer_tr at, size_t sizeof_addr = 4, bool vp = true)
{
uintptr_t base = (uintptr_t)GetModuleHandleA(NULL);
switch (sizeof_addr)
{
case 1: return (base + ReadMemory<int8_t>(at, vp));
case 2: return (base + ReadMemory<int16_t>(at, vp));
case 4: return (base + ReadMemory<int32_t>(at, vp));
}
return nullptr;
}

inline bool UnprotectMemory(memory_pointer_tr addr, size_t size)
{
DWORD out_oldprotect = 0;
return VirtualProtect(addr.get(), size, PAGE_EXECUTE_READWRITE, &out_oldprotect) != 0;
}
};

8 changes: 4 additions & 4 deletions source/rawinput.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public:
static auto reg = *pattern.get_first<uint8_t>(3);
struct CCamFpsWeaponHook
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
auto inv = FusionFixSettings.Get("PREF_INVERT_MOUSE");
static auto ri = FusionFixSettings.GetRef("PREF_RAWINPUT");
Expand All @@ -246,14 +246,14 @@ public:
}
regs.xmm0.f32[0] = *(float*)(ptr + 0x144);
}
}; injector::MakeInline2<CCamFpsWeaponHook>(pattern.get_first(0), pattern.get_first(8));
}; injector::MakeInline<CCamFpsWeaponHook>(pattern.get_first(0), pattern.get_first(8));

// First Person Vehicle Camera
pattern = find_pattern("F3 0F 10 86 ? ? ? ? F3 0F 10 0D ? ? ? ? 0F 2F C8 76 0F F3 0F 10 0D ? ? ? ? 0F 2F C1", "F3 0F 10 87 ? ? ? ? F3 0F 10 0D ? ? ? ? 0F 2F C8 76 0D F3 0F 10 0D ? ? ? ? 0F 2F C1 77 03 0F 28 C1 F3 0F 11 87");
static auto reg2 = *pattern.get_first<uint8_t>(3);
struct CCamFollowVehicleHook
{
void operator()(SafetyHookContext& regs)
void operator()(injector::reg_pack& regs)
{
auto inv = FusionFixSettings.Get("PREF_INVERT_MOUSE");
static auto ri = FusionFixSettings.GetRef("PREF_RAWINPUT");
Expand All @@ -266,7 +266,7 @@ public:
}
regs.xmm0.f32[0] = *(float*)(ptr + 0x1B0);
}
}; injector::MakeInline2<CCamFollowVehicleHook>(pattern.get_first(0), pattern.get_first(8));
}; injector::MakeInline<CCamFollowVehicleHook>(pattern.get_first(0), pattern.get_first(8));

// Script
{
Expand Down

0 comments on commit 8fdfc2e

Please sign in to comment.