diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:14:27 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:14:27 -0500 |
| commit | 98fa503f8a19c3ed65df1f4f2f0e4571aa42c4f7 (patch) | |
| tree | 9f17c922e8deebaa4d0cd0a8eb59125349f5bda0 /runtime/src/code_hooks.h | |
| parent | 0db7fed551c4d48d7b97a9c4d609fc11d17ae19c (diff) | |
Mods: first-person view, the base (camera section, view-mode getter hook)
mod.json gains "camera": {"view_mode": "first_person"|"default", "first_person_value", "contexts"}.
gta6rt --view-mode-getter RVA hooks the game's view-mode getter with a result-override thunk (code_hooks.h:
the original runs, then its result and first argument go through leonida::mods::override_view_mode), so the listed
contexts answer first person and the others keep the game's mode. mods/first_person asks for it on foot.
docs/modding.md records why switching a mode is enough: the game's tuning data refers to a first-person view
(FirstPersonDirectionDot, UseFirstPersonCameraMatrix on cloth capsules, FIRST_PERSON_VIEW hit-reaction recovery, a
firstpersonshooter HUD texture; RDR2's engine branch), while GTA V's camera class names are not present by joaat.
The context numbering and the first-person enum value (4, GTA V's) are manifest settings to confirm in the
executable. ctest mods and code_hooks cover the parser, the override and the thunk on x64 code in memory.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/code_hooks.h')
| -rw-r--r-- | runtime/src/code_hooks.h | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/runtime/src/code_hooks.h b/runtime/src/code_hooks.h index 6b661b2d1..a079f95dd 100644 --- a/runtime/src/code_hooks.h +++ b/runtime/src/code_hooks.h | |||
| @@ -74,6 +74,34 @@ inline uint8_t* make_remap_thunk(const void* close_to, int arg, RemapFn fn, cons | |||
| 74 | return t; | 74 | return t; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | using ResultFn = uint32_t (*)(uint32_t result, uint32_t arg0); | ||
| 78 | |||
| 79 | // A thunk that runs the original and then passes (its 32-bit result, its first argument) through `fn`, returning | ||
| 80 | // what `fn` returns: for a getter such as "the view mode of context N". Only for functions with at most four | ||
| 81 | // register arguments (a stack argument would be one slot off) and an integer result. | ||
| 82 | // Stack: rsp = 8 mod 16 at entry; one push and 0x20 bytes of shadow space make it 16-aligned for both calls. | ||
| 83 | inline uint8_t* make_result_thunk(const void* close_to, ResultFn fn, const void* original) { | ||
| 84 | uint8_t* t = alloc_near(close_to, 4096); | ||
| 85 | if (!t) return nullptr; | ||
| 86 | uint8_t* p = t; | ||
| 87 | auto put = [&](std::initializer_list<uint8_t> bytes) { for (uint8_t b : bytes) *p++ = b; }; | ||
| 88 | auto put64 = [&](uint64_t v) { std::memcpy(p, &v, 8); p += 8; }; | ||
| 89 | put({0x51}); // push rcx (the first argument, for fn) | ||
| 90 | put({0x48, 0x83, 0xEC, 0x20}); // sub rsp, 0x20 | ||
| 91 | put({0x48, 0xB8}); put64(reinterpret_cast<uint64_t>(original)); // mov rax, original | ||
| 92 | put({0xFF, 0xD0}); // call rax (rcx, rdx, r8, r9 still the caller's) | ||
| 93 | put({0x48, 0x8B, 0x54, 0x24, 0x20}); // mov rdx, [rsp+0x20] (the saved first argument) | ||
| 94 | put({0x89, 0xC1}); // mov ecx, eax (the original's result) | ||
| 95 | put({0x48, 0xB8}); put64(reinterpret_cast<uint64_t>(fn)); // mov rax, fn | ||
| 96 | put({0xFF, 0xD0}); // call rax | ||
| 97 | put({0x48, 0x83, 0xC4, 0x20}); // add rsp, 0x20 | ||
| 98 | put({0x59, 0xC3}); // pop rcx ; ret | ||
| 99 | DWORD old = 0; | ||
| 100 | VirtualProtect(t, 4096, PAGE_EXECUTE_READ, &old); | ||
| 101 | FlushInstructionCache(GetCurrentProcess(), t, p - t); | ||
| 102 | return t; | ||
| 103 | } | ||
| 104 | |||
| 77 | // Points every E8/E9 rel32 in [begin, begin + size) whose target is `target` at `thunk`. The range must be writable. | 105 | // Points every E8/E9 rel32 in [begin, begin + size) whose target is `target` at `thunk`. The range must be writable. |
| 78 | // Returns how many were rewritten. | 106 | // Returns how many were rewritten. |
| 79 | inline std::size_t redirect_direct_branches(uint8_t* begin, std::size_t size, const uint8_t* target, | 107 | inline std::size_t redirect_direct_branches(uint8_t* begin, std::size_t size, const uint8_t* target, |
| @@ -103,7 +131,10 @@ struct HookResult { | |||
| 103 | 131 | ||
| 104 | // Hooks the function at `target_rva` of the mapped image at `base`: one thunk, then every direct call or jump to it | 132 | // Hooks the function at `target_rva` of the mapped image at `base`: one thunk, then every direct call or jump to it |
| 105 | // in the image's executable sections. Runs before any image code (main.cpp), so no thread is executing the bytes. | 133 | // in the image's executable sections. Runs before any image code (main.cpp), so no thread is executing the bytes. |
| 106 | inline HookResult hook_image_calls(uint8_t* base, uint32_t target_rva, RemapFn fn, int arg) { | 134 | // Validates `target_rva` as code of the mapped image at `base`, builds the thunk with `make(target)` and points the |
| 135 | // image's direct calls and jumps to the target at it. | ||
| 136 | template <class MakeThunk> | ||
| 137 | inline HookResult redirect_image_calls(uint8_t* base, uint32_t target_rva, MakeThunk make) { | ||
| 107 | HookResult r; | 138 | HookResult r; |
| 108 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(base); | 139 | const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(base); |
| 109 | if (dos->e_magic != IMAGE_DOS_SIGNATURE) return r.error = "not a mapped PE image", r; | 140 | if (dos->e_magic != IMAGE_DOS_SIGNATURE) return r.error = "not a mapped PE image", r; |
| @@ -118,7 +149,7 @@ inline HookResult hook_image_calls(uint8_t* base, uint32_t target_rva, RemapFn f | |||
| 118 | in_code = true; | 149 | in_code = true; |
| 119 | if (!in_code) return r.error = "the RVA is not in an executable section", r; | 150 | if (!in_code) return r.error = "the RVA is not in an executable section", r; |
| 120 | uint8_t* target = base + target_rva; | 151 | uint8_t* target = base + target_rva; |
| 121 | uint8_t* thunk = make_remap_thunk(target, arg, fn, target); | 152 | uint8_t* thunk = make(target); |
| 122 | if (!thunk) return r.error = "no memory within 2 GiB of the image for the thunk", r; | 153 | if (!thunk) return r.error = "no memory within 2 GiB of the image for the thunk", r; |
| 123 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i) { | 154 | for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i) { |
| 124 | if (!(sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE)) continue; | 155 | if (!(sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE)) continue; |
| @@ -135,4 +166,17 @@ inline HookResult hook_image_calls(uint8_t* base, uint32_t target_rva, RemapFn f | |||
| 135 | return r; | 166 | return r; |
| 136 | } | 167 | } |
| 137 | 168 | ||
| 169 | // The argument-remapping hook (archetype lookup): argument `arg` of every direct call goes through `fn`. | ||
| 170 | inline HookResult hook_image_calls(uint8_t* base, uint32_t target_rva, RemapFn fn, int arg) { | ||
| 171 | if (arg != 0 && arg != 1) return HookResult{false, 0, "only argument 0 or 1 can be remapped"}; | ||
| 172 | return redirect_image_calls(base, target_rva, | ||
| 173 | [&](uint8_t* target) { return make_remap_thunk(target, arg, fn, target); }); | ||
| 174 | } | ||
| 175 | |||
| 176 | // The result-override hook (view-mode getter): every direct call's result goes through `fn` with the first argument. | ||
| 177 | inline HookResult hook_image_results(uint8_t* base, uint32_t target_rva, ResultFn fn) { | ||
| 178 | return redirect_image_calls(base, target_rva, | ||
| 179 | [&](uint8_t* target) { return make_result_thunk(target, fn, target); }); | ||
| 180 | } | ||
| 181 | |||
| 138 | } // namespace leonida::hooks | 182 | } // namespace leonida::hooks |