From e764f451d39443ca6b660107d1098232f5ffa63b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 2 Oct 2023 22:55:39 +0200 Subject: [PATCH] inject: add ability to finetune injections --- src/inject_exec.c | 54 +++++++++++++++++++++++------------------------ src/inject_util.c | 9 +++++++- src/inject_util.h | 7 +++--- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/inject_exec.c b/src/inject_exec.c index e3091f39..f58596f6 100644 --- a/src/inject_exec.c +++ b/src/inject_exec.c @@ -13,45 +13,45 @@ static void Inject_Shell(void); static void Inject_Camera(void) { - INJECT(0x004105A0, Camera_Initialise); - INJECT(0x00410650, Camera_Move); - INJECT(0x004109D0, Camera_Clip); - INJECT(0x00410AB0, Camera_Shift); - INJECT(0x00410C10, Camera_GoodPosition); - INJECT(0x00410C60, Camera_SmartShift); - INJECT(0x004113F0, Camera_Chase); - INJECT(0x004114E0, Camera_ShiftClamp); - INJECT(0x00411680, Camera_Combat); - INJECT(0x00411810, Camera_Look); - INJECT(0x00411A00, Camera_Fixed); - INJECT(0x00411AA0, Camera_Update); - INJECT(0x004126A0, Camera_LoadCutsceneFrame); - INJECT(0x00412290, Camera_UpdateCutscene); + INJECT(1, 0x004105A0, Camera_Initialise); + INJECT(1, 0x00410650, Camera_Move); + INJECT(1, 0x004109D0, Camera_Clip); + INJECT(1, 0x00410AB0, Camera_Shift); + INJECT(1, 0x00410C10, Camera_GoodPosition); + INJECT(1, 0x00410C60, Camera_SmartShift); + INJECT(1, 0x004113F0, Camera_Chase); + INJECT(1, 0x004114E0, Camera_ShiftClamp); + INJECT(1, 0x00411680, Camera_Combat); + INJECT(1, 0x00411810, Camera_Look); + INJECT(1, 0x00411A00, Camera_Fixed); + INJECT(1, 0x00411AA0, Camera_Update); + INJECT(1, 0x004126A0, Camera_LoadCutsceneFrame); + INJECT(1, 0x00412290, Camera_UpdateCutscene); } static void Inject_Matrix(void) { - INJECT(0x00401000, Matrix_GenerateW2V); - INJECT(0x004011D0, Matrix_LookAt); - INJECT(0x004012D0, Matrix_RotX); - INJECT(0x00401380, Matrix_RotY); - INJECT(0x00401430, Matrix_RotZ); - INJECT(0x004014E0, Matrix_RotYXZ); - INJECT(0x004016C0, Matrix_RotYXZpack); - INJECT(0x004018B0, Matrix_TranslateRel); + INJECT(1, 0x00401000, Matrix_GenerateW2V); + INJECT(1, 0x004011D0, Matrix_LookAt); + INJECT(1, 0x004012D0, Matrix_RotX); + INJECT(1, 0x00401380, Matrix_RotY); + INJECT(1, 0x00401430, Matrix_RotZ); + INJECT(1, 0x004014E0, Matrix_RotYXZ); + INJECT(1, 0x004016C0, Matrix_RotYXZpack); + INJECT(1, 0x004018B0, Matrix_TranslateRel); } static void Inject_Math(void) { - INJECT(0x00457C10, Math_Atan); - INJECT(0x00457C58, Math_Cos); - INJECT(0x00457C5E, Math_Sin); - INJECT(0x00457C93, Math_Sqrt); + INJECT(1, 0x00457C10, Math_Atan); + INJECT(1, 0x00457C58, Math_Cos); + INJECT(1, 0x00457C5E, Math_Sin); + INJECT(1, 0x00457C93, Math_Sqrt); } static void Inject_Shell(void) { - INJECT(0x0044E890, Shell_ExitSystem); + INJECT(1, 0x0044E890, Shell_ExitSystem); } void Inject_Exec(void) diff --git a/src/inject_util.c b/src/inject_util.c index 0bd381a1..20f105e8 100644 --- a/src/inject_util.c +++ b/src/inject_util.c @@ -4,11 +4,18 @@ #include -void Inject_Impl(void (*from)(void), void (*to)(void)) +void InjectImpl(bool enable, void (*from)(void), void (*to)(void)) { if (from == to) { return; } + + if (!enable) { + void (*aux)(void) = from; + from = to; + to = aux; + } + DWORD tmp; LOG_DEBUG("Patching %p to %p", from, to); VirtualProtect(from, sizeof(JMP), PAGE_EXECUTE_READWRITE, &tmp); diff --git a/src/inject_util.h b/src/inject_util.h index ad4c1606..f48fdb17 100644 --- a/src/inject_util.h +++ b/src/inject_util.h @@ -1,5 +1,6 @@ #pragma once +#include #include #pragma pack(push, 1) @@ -9,12 +10,12 @@ typedef struct { } JMP; #pragma pack(pop) -void Inject_Impl(void (*from)(void), void (*to)(void)); +void InjectImpl(bool enable, void (*from)(void), void (*to)(void)); #define VAR_U_(address, type) (*(type *)(address)) #define VAR_I_(address, type, value) (*(type *)(address)) #define ARRAY_(address, type, length) (*(type(*) length)(address)) -#define INJECT(from, to) \ +#define INJECT(enable, from, to) \ { \ - Inject_Impl((void (*)(void))from, (void (*)(void))to); \ + InjectImpl(enable, (void (*)(void))from, (void (*)(void))to); \ }