From 9e6444fadb93c9b800ed403f91fcfee1a88d3560 Mon Sep 17 00:00:00 2001 From: ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:53:19 -0500 Subject: Mods: the archetype hook, --archetype-lookup RVA[,ARG] code_hooks.h hooks a function of the mapped game image without patching it: every direct call/jmp rel32 to it in the image's executable sections is pointed at a thunk that saves rcx/rdx/r8/r9 and xmm0-3, passes the hash (argument 0 or 1) through leonida::mods::active().remap() and jumps on to the original. gta6rt installs it right after mapping, before any image code runs, and logs how many call sites it redirected, so a --dry-run with a candidate RVA tells whether it is the archetype lookup. ctest code_hooks runs the thunk on x64 functions written into memory (call and tail jump remapped, r8 preserved, a branch elsewhere left byte for byte). With the executable's lookup RVA this applies mods/mandy_prison_intro; docs/modding.md says how to find it. Co-Authored-By: Claude Opus 5.5 --- runtime/src/code_hooks.h | 138 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 runtime/src/code_hooks.h (limited to 'runtime/src/code_hooks.h') diff --git a/runtime/src/code_hooks.h b/runtime/src/code_hooks.h new file mode 100644 index 000000000..6b661b2d1 --- /dev/null +++ b/runtime/src/code_hooks.h @@ -0,0 +1,138 @@ +// Hooks on a function of the loaded game image, by redirecting its direct callers (docs/modding.md, "Day one"). +// +// Patching inside a function needs an instruction decoder to move its prologue. Redirecting the callers does not: +// every `call rel32` (E8) and `jmp rel32` (E9) in the image's executable sections whose target is the function is +// pointed at a thunk instead. The thunk saves the argument registers (rcx, rdx, r8, r9, xmm0-3), passes one 32-bit +// argument through a C++ function, restores the rest and jumps to the original. A call through a pointer (vtable, +// import slot) is not redirected, which suits RAGE's archetype lookup: a static function called directly. +// +// A byte pattern can look like E8/E9 + rel32 inside another instruction; it is only rewritten when its target is +// exactly the hooked function, so a false match must also point there. x64 Windows only. +#pragma once + +#include + +#include +#include +#include +#include + +namespace leonida::hooks { + +using RemapFn = uint32_t (*)(uint32_t); + +// Executable memory within +-2 GiB of `close_to`, so a rel32 branch from the image reaches it. nullptr if none is free. +inline uint8_t* alloc_near(const void* close_to, std::size_t size) { + SYSTEM_INFO si; + GetSystemInfo(&si); + const uintptr_t gran = si.dwAllocationGranularity; + const uintptr_t center = reinterpret_cast(close_to) & ~(gran - 1); + const uintptr_t span = 0x70000000; // stay well inside the rel32 range from anywhere in a large image + for (uintptr_t delta = gran; delta < span; delta += gran) { + for (int side = 0; side < 2; ++side) { + const uintptr_t at = side ? center + delta : center - delta; + if (side == 0 && center < delta) continue; + if (void* p = VirtualAlloc(reinterpret_cast(at), size, MEM_RESERVE | MEM_COMMIT, + PAGE_EXECUTE_READWRITE)) + return static_cast(p); + } + } + return nullptr; +} + +// The thunk: argument `arg` (0 = rcx, 1 = rdx; the low 32 bits) goes through `fn`, then the original runs with every +// other argument register as the caller left it. Stack: the caller's call leaves rsp = 8 mod 16; four pushes and +// 0x68 bytes make it 16-aligned for the call to `fn`, with its 32-byte shadow space and room for xmm0-3. +inline uint8_t* make_remap_thunk(const void* close_to, int arg, RemapFn fn, const void* original) { + if (arg != 0 && arg != 1) return nullptr; + uint8_t* t = alloc_near(close_to, 4096); + if (!t) return nullptr; + uint8_t* p = t; + auto put = [&](std::initializer_list bytes) { for (uint8_t b : bytes) *p++ = b; }; + auto put64 = [&](uint64_t v) { std::memcpy(p, &v, 8); p += 8; }; + put({0x51, 0x52, 0x41, 0x50, 0x41, 0x51}); // push rcx, rdx, r8, r9 + put({0x48, 0x83, 0xEC, 0x68}); // sub rsp, 0x68 + put({0xF3, 0x0F, 0x7F, 0x44, 0x24, 0x20}); // movdqu [rsp+0x20], xmm0 + put({0xF3, 0x0F, 0x7F, 0x4C, 0x24, 0x30}); // movdqu [rsp+0x30], xmm1 + put({0xF3, 0x0F, 0x7F, 0x54, 0x24, 0x40}); // movdqu [rsp+0x40], xmm2 + put({0xF3, 0x0F, 0x7F, 0x5C, 0x24, 0x50}); // movdqu [rsp+0x50], xmm3 + if (arg == 1) put({0x89, 0xD1}); // mov ecx, edx + put({0x48, 0xB8}); put64(reinterpret_cast(fn)); // mov rax, fn + put({0xFF, 0xD0}); // call rax + put({0xF3, 0x0F, 0x6F, 0x44, 0x24, 0x20}); // movdqu xmm0, [rsp+0x20] + put({0xF3, 0x0F, 0x6F, 0x4C, 0x24, 0x30}); // movdqu xmm1, [rsp+0x30] + put({0xF3, 0x0F, 0x6F, 0x54, 0x24, 0x40}); // movdqu xmm2, [rsp+0x40] + put({0xF3, 0x0F, 0x6F, 0x5C, 0x24, 0x50}); // movdqu xmm3, [rsp+0x50] + put({0x48, 0x83, 0xC4, 0x68}); // add rsp, 0x68 + put({0x41, 0x59, 0x41, 0x58, 0x5A, 0x59}); // pop r9, r8, rdx, rcx + put({0x89, static_cast(arg == 0 ? 0xC1 : 0xC2)}); // mov ecx|edx, eax (the remapped value) + put({0x48, 0xB8}); put64(reinterpret_cast(original)); // mov rax, original + put({0xFF, 0xE0}); // jmp rax + DWORD old = 0; + VirtualProtect(t, 4096, PAGE_EXECUTE_READ, &old); + FlushInstructionCache(GetCurrentProcess(), t, p - t); + return t; +} + +// Points every E8/E9 rel32 in [begin, begin + size) whose target is `target` at `thunk`. The range must be writable. +// Returns how many were rewritten. +inline std::size_t redirect_direct_branches(uint8_t* begin, std::size_t size, const uint8_t* target, + const uint8_t* thunk) { + std::size_t n = 0; + for (std::size_t i = 0; i + 5 <= size; ++i) { + if (begin[i] != 0xE8 && begin[i] != 0xE9) continue; + int32_t rel; + std::memcpy(&rel, begin + i + 1, 4); + const uint8_t* next = begin + i + 5; + if (next + rel != target) continue; + const intptr_t to = thunk - next; + if (to < INT32_MIN || to > INT32_MAX) continue; + const int32_t rel2 = static_cast(to); + std::memcpy(begin + i + 1, &rel2, 4); + ++n; + i += 4; + } + return n; +} + +struct HookResult { + bool ok = false; + std::size_t redirected = 0; + const char* error = ""; +}; + +// Hooks the function at `target_rva` of the mapped image at `base`: one thunk, then every direct call or jump to it +// in the image's executable sections. Runs before any image code (main.cpp), so no thread is executing the bytes. +inline HookResult hook_image_calls(uint8_t* base, uint32_t target_rva, RemapFn fn, int arg) { + HookResult r; + const auto* dos = reinterpret_cast(base); + if (dos->e_magic != IMAGE_DOS_SIGNATURE) return r.error = "not a mapped PE image", r; + const auto* nt = reinterpret_cast(base + dos->e_lfanew); + if (nt->Signature != IMAGE_NT_SIGNATURE || target_rva >= nt->OptionalHeader.SizeOfImage) + return r.error = "the RVA is outside the image", r; + const IMAGE_SECTION_HEADER* sec = IMAGE_FIRST_SECTION(nt); + bool in_code = false; + for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i) + if ((sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) && target_rva >= sec[i].VirtualAddress && + target_rva < sec[i].VirtualAddress + sec[i].Misc.VirtualSize) + in_code = true; + if (!in_code) return r.error = "the RVA is not in an executable section", r; + uint8_t* target = base + target_rva; + uint8_t* thunk = make_remap_thunk(target, arg, fn, target); + if (!thunk) return r.error = "no memory within 2 GiB of the image for the thunk", r; + for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i) { + if (!(sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE)) continue; + uint8_t* begin = base + sec[i].VirtualAddress; + const std::size_t size = sec[i].Misc.VirtualSize; + DWORD old = 0; + if (!VirtualProtect(begin, size, PAGE_EXECUTE_READWRITE, &old)) return r.error = "VirtualProtect failed", r; + r.redirected += redirect_direct_branches(begin, size, target, thunk); + DWORD ignored = 0; + VirtualProtect(begin, size, old, &ignored); + FlushInstructionCache(GetCurrentProcess(), begin, size); + } + r.ok = true; + return r; +} + +} // namespace leonida::hooks -- cgit v1.2.3 From 98fa503f8a19c3ed65df1f4f2f0e4571aa42c4f7 Mon Sep 17 00:00:00 2001 From: ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> Date: Sun, 22 Nov 2026 23:14:27 -0500 Subject: 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 --- docs/modding.md | 33 ++++++++++++++++++ mods/first_person/mod.json | 6 ++++ runtime/src/code_hooks.h | 48 +++++++++++++++++++++++++-- runtime/src/main.cpp | 29 ++++++++++++++-- runtime/src/mods.h | 72 ++++++++++++++++++++++++++++++++++++++++ runtime/test/code_hooks_test.cpp | 23 +++++++++++++ runtime/test/mods_test.cpp | 28 ++++++++++++++++ 7 files changed, 234 insertions(+), 5 deletions(-) create mode 100644 mods/first_person/mod.json (limited to 'runtime/src/code_hooks.h') diff --git a/docs/modding.md b/docs/modding.md index b658aee22..3414923c5 100644 --- a/docs/modding.md +++ b/docs/modding.md @@ -105,3 +105,36 @@ the thunk on small x64 functions in memory. That RVA is the only version-specific piece; it can go in `game/xbox-1.0.2.1.json` once confirmed, and the manifests stay as they are. + +## First-person view (`mods/first_person`) + +```json +{ "camera": { "view_mode": "first_person", "first_person_value": 4, "contexts": [0] } } +``` + +`camera.view_mode` is `first_person` or `default` (leave the game's choice). The game picks a view mode per +camera context (on foot, in a vehicle, ...) with a getter that takes the context and returns an enum; the +mod answers `first_person_value` for the listed `contexts` (every context when the list is left out) and +the game's own answer otherwise. When several enabled mods have a `camera` section, the last folder wins. + +**Evidence that the engine has a first-person view.** The camera metadata classes GTA V uses +(`camFirstPersonShooterCameraMetadata`, `camFollowPedCameraMetadata`, ...) do not appear by `joaat` in the +carved `common`, `data` or `script` streams, but the game's own tuning data refers to a first-person view +in several systems: a ladder/mount tunable `FirstPersonDirectionDot`, a `UseFirstPersonCameraMatrix` flag on +every cloth-collision capsule (horse saddle and satchel capsules among them, so the code is RDR2's branch of +the engine, which shipped a first-person mode), a hit-reaction `RecoveryDirection` of `FIRST_PERSON_VIEW`, +and a `firstpersonshooter_hud` UI texture. So the mod switches on a mode the engine has rather than building +a camera. Whether the game exposes it to the player is not known. + +**Assumptions to confirm in the executable:** that the view mode is chosen through one getter of the form +`mode = get(context)`, that context 0 is on foot, and that first person is enum value 4 (GTA V's +`camViewModeSettings` order: third person near, medium, far, cinematic, first person). All three are +settings in `mod.json`, so a different answer changes the manifest, not the runtime. + +**Day one:** find the getter, then run `gta6rt --dry-run --view-mode-getter RVA` and check the log +line's count of redirected calls. Leads: the code that reads the view-mode input (GTA V's +`INPUT_NEXT_CAMERA`, an action hash), the switch over the five view-mode values that selects a camera +metadata per mode, and the callers of the `UseFirstPersonCameraMatrix` check in the cloth code (they ask +whether the active view is first person). The hook (`hook_image_results` in `runtime/src/code_hooks.h`) runs +the original and overrides its result; it is for getters with at most four register arguments and an +integer result. ctest `code_hooks` and `mods` cover the thunk and the override. diff --git a/mods/first_person/mod.json b/mods/first_person/mod.json new file mode 100644 index 000000000..d7c4849b4 --- /dev/null +++ b/mods/first_person/mod.json @@ -0,0 +1,6 @@ +{ + "name": "First-person view on foot", + "enabled": true, + "game_version": "1.0.2.1", + "camera": { "view_mode": "first_person", "first_person_value": 4, "contexts": [0] } +} 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 return t; } +using ResultFn = uint32_t (*)(uint32_t result, uint32_t arg0); + +// A thunk that runs the original and then passes (its 32-bit result, its first argument) through `fn`, returning +// what `fn` returns: for a getter such as "the view mode of context N". Only for functions with at most four +// register arguments (a stack argument would be one slot off) and an integer result. +// Stack: rsp = 8 mod 16 at entry; one push and 0x20 bytes of shadow space make it 16-aligned for both calls. +inline uint8_t* make_result_thunk(const void* close_to, ResultFn fn, const void* original) { + uint8_t* t = alloc_near(close_to, 4096); + if (!t) return nullptr; + uint8_t* p = t; + auto put = [&](std::initializer_list bytes) { for (uint8_t b : bytes) *p++ = b; }; + auto put64 = [&](uint64_t v) { std::memcpy(p, &v, 8); p += 8; }; + put({0x51}); // push rcx (the first argument, for fn) + put({0x48, 0x83, 0xEC, 0x20}); // sub rsp, 0x20 + put({0x48, 0xB8}); put64(reinterpret_cast(original)); // mov rax, original + put({0xFF, 0xD0}); // call rax (rcx, rdx, r8, r9 still the caller's) + put({0x48, 0x8B, 0x54, 0x24, 0x20}); // mov rdx, [rsp+0x20] (the saved first argument) + put({0x89, 0xC1}); // mov ecx, eax (the original's result) + put({0x48, 0xB8}); put64(reinterpret_cast(fn)); // mov rax, fn + put({0xFF, 0xD0}); // call rax + put({0x48, 0x83, 0xC4, 0x20}); // add rsp, 0x20 + put({0x59, 0xC3}); // pop rcx ; ret + DWORD old = 0; + VirtualProtect(t, 4096, PAGE_EXECUTE_READ, &old); + FlushInstructionCache(GetCurrentProcess(), t, p - t); + return t; +} + // Points every E8/E9 rel32 in [begin, begin + size) whose target is `target` at `thunk`. The range must be writable. // Returns how many were rewritten. inline std::size_t redirect_direct_branches(uint8_t* begin, std::size_t size, const uint8_t* target, @@ -103,7 +131,10 @@ struct HookResult { // Hooks the function at `target_rva` of the mapped image at `base`: one thunk, then every direct call or jump to it // in the image's executable sections. Runs before any image code (main.cpp), so no thread is executing the bytes. -inline HookResult hook_image_calls(uint8_t* base, uint32_t target_rva, RemapFn fn, int arg) { +// Validates `target_rva` as code of the mapped image at `base`, builds the thunk with `make(target)` and points the +// image's direct calls and jumps to the target at it. +template +inline HookResult redirect_image_calls(uint8_t* base, uint32_t target_rva, MakeThunk make) { HookResult r; const auto* dos = reinterpret_cast(base); 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 in_code = true; if (!in_code) return r.error = "the RVA is not in an executable section", r; uint8_t* target = base + target_rva; - uint8_t* thunk = make_remap_thunk(target, arg, fn, target); + uint8_t* thunk = make(target); if (!thunk) return r.error = "no memory within 2 GiB of the image for the thunk", r; for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i) { 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 return r; } +// The argument-remapping hook (archetype lookup): argument `arg` of every direct call goes through `fn`. +inline HookResult hook_image_calls(uint8_t* base, uint32_t target_rva, RemapFn fn, int arg) { + if (arg != 0 && arg != 1) return HookResult{false, 0, "only argument 0 or 1 can be remapped"}; + return redirect_image_calls(base, target_rva, + [&](uint8_t* target) { return make_remap_thunk(target, arg, fn, target); }); +} + +// The result-override hook (view-mode getter): every direct call's result goes through `fn` with the first argument. +inline HookResult hook_image_results(uint8_t* base, uint32_t target_rva, ResultFn fn) { + return redirect_image_calls(base, target_rva, + [&](uint8_t* target) { return make_result_thunk(target, fn, target); }); +} + } // namespace leonida::hooks diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index a6f41e8d1..b75baf15f 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -49,9 +49,9 @@ const char* binding_name(rt::Binding b) { int usage() { std::fprintf(stderr, - "usage: gta6rt --dry-run [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace FILE]\n" - " gta6rt --init [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace-d3d12] [--trace FILE]\n" - " gta6rt --run [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); + "usage: gta6rt --dry-run [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n" + " gta6rt --init [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n" + " gta6rt --run [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); return 2; } @@ -61,6 +61,7 @@ int wmain(int argc, wchar_t** argv) { std::filesystem::path image, game_dir, overlay_dir, mods_dir; uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash int archetype_lookup_arg = 0; + uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context bool dry_run = false, init = false, run = false, trace_d3d12 = false; std::vector game_args; // wide: path::string() throws on a character the code page lacks (RT-84) std::wstring trace_file; @@ -78,6 +79,12 @@ int wmain(int argc, wchar_t** argv) { else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i]; else if (a == L"--mods" && i + 1 < argc) mods_dir = argv[++i]; + else if (a == L"--view-mode-getter" && i + 1 < argc) { + wchar_t* end = nullptr; + const unsigned long rva = std::wcstoul(argv[++i], &end, 16); + if (!end || *end || rva == 0) return usage(); + view_mode_getter_rva = static_cast(rva); + } else if (a == L"--archetype-lookup" && i + 1 < argc) { const std::wstring v = argv[++i]; const auto comma = v.find(L','); @@ -127,6 +134,12 @@ int wmain(int argc, wchar_t** argv) { sw.from, sw.to_name.c_str(), sw.to); } leonida::mods::set_active(leonida::mods::ArchetypeSwaps(mods.mods)); + const leonida::mods::CameraView camera = leonida::mods::camera_of(mods.mods); + leonida::mods::set_active_camera(camera); + if (camera.first_person) + std::fprintf(stderr, "[rt] mods: first-person view (value %u) for %s%s\n", camera.first_person_value, + camera.contexts.empty() ? "every context" : "the listed contexts", + view_mode_getter_rva ? "" : "; not applied: no --view-mode-getter RVA for this executable"); if (leonida::mods::active().size()) std::fprintf(stderr, archetype_lookup_rva ? "[rt] mods: %zu archetype swap(s) loaded\n" : "[rt] mods: %zu archetype swap(s) loaded; not applied: no --archetype-lookup RVA for " @@ -144,6 +157,16 @@ int wmain(int argc, wchar_t** argv) { rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup. + if (view_mode_getter_rva) { + const auto r = leonida::hooks::hook_image_results(main_module.base, view_mode_getter_rva, + &leonida::mods::override_view_mode); + if (r.ok) + std::fprintf(stderr, "[rt] mods: view-mode getter at RVA 0x%X hooked: %zu direct call(s) redirected\n", + view_mode_getter_rva, r.redirected); + else + std::fprintf(stderr, "[rt] mods: view-mode getter at RVA 0x%X not hooked: %s\n", view_mode_getter_rva, + r.error); + } if (archetype_lookup_rva) { const auto r = leonida::hooks::hook_image_calls( main_module.base, archetype_lookup_rva, diff --git a/runtime/src/mods.h b/runtime/src/mods.h index 5f23e9883..c308ed7db 100644 --- a/runtime/src/mods.h +++ b/runtime/src/mods.h @@ -49,12 +49,24 @@ struct ArchetypeSwap { std::string when = "always"; }; +// "camera": the view mode the game's view-mode getter answers (docs/modding.md, "First-person view"). The getter +// takes a context (on foot, in a vehicle, ...) and returns an enum; `first_person_value` is that enum's first-person +// value, 4 in GTA V's camViewModeSettings, to be confirmed in the executable. `contexts`: the getter's first-argument +// values to override; empty = every context. +struct CameraView { + bool set = false; // the mod has a "camera" section + bool first_person = false; // "view_mode": "first_person" (else "default": leave the game's choice) + uint32_t first_person_value = 4; + std::vector contexts; +}; + struct Mod { std::string folder; // the folder name under mods/ std::string name; std::string game_version; bool enabled = true; std::vector archetype_swaps; + CameraView camera; }; struct LoadResult { @@ -135,6 +147,40 @@ inline bool parse_mod(std::string_view text, const std::string& folder, Mod& mod mod.archetype_swaps.push_back(std::move(swap)); } } + if (const Json* cam = root.find("camera")) { + if (cam->kind != Json::Kind::object) { + error = "\"camera\" must be an object"; + return false; + } + mod.camera.set = true; + const Json* view = cam->find("view_mode"); + if (!view || view->kind != Json::Kind::string || + (view->string != "first_person" && view->string != "default")) { + error = "\"camera\": \"view_mode\" must be \"first_person\" or \"default\""; + return false; + } + mod.camera.first_person = view->string == "first_person"; + if (const Json* v = cam->find("first_person_value")) { + if (v->kind != Json::Kind::number || v->number < 0 || v->number > 255 || v->number != static_cast(v->number)) { + error = "\"camera\": \"first_person_value\" must be a whole number 0-255"; + return false; + } + mod.camera.first_person_value = static_cast(v->number); + } + if (const Json* c = cam->find("contexts")) { + if (c->kind != Json::Kind::array) { + error = "\"camera\": \"contexts\" must be a list of numbers"; + return false; + } + for (const Json& x : c->array) { + if (x.kind != Json::Kind::number || x.number < 0 || x.number != static_cast(x.number)) { + error = "\"camera\": \"contexts\" must be a list of whole numbers"; + return false; + } + mod.camera.contexts.push_back(static_cast(x.number)); + } + } + } return true; } @@ -199,4 +245,30 @@ inline ArchetypeSwaps& active_slot() { inline void set_active(ArchetypeSwaps swaps) { active_slot() = std::move(swaps); } inline const ArchetypeSwaps& active() { return active_slot(); } +// The camera override of the enabled mods: the last one (in folder order) with a "camera" section decides. +inline CameraView camera_of(const std::vector& mods) { + CameraView view; + for (const Mod& m : mods) + if (m.enabled && m.camera.set) view = m.camera; + return view; +} + +inline CameraView& camera_slot() { + static CameraView view; + return view; +} +inline void set_active_camera(CameraView view) { camera_slot() = std::move(view); } + +// What the hooked view-mode getter returns: the mod's first-person value for an overridden context, else the game's. +inline uint32_t override_view_mode(uint32_t result, uint32_t context) { + const CameraView& v = camera_slot(); + if (!v.first_person) return result; + if (!v.contexts.empty()) { + bool listed = false; + for (uint32_t c : v.contexts) listed = listed || c == context; + if (!listed) return result; + } + return v.first_person_value; +} + } // namespace leonida::mods diff --git a/runtime/test/code_hooks_test.cpp b/runtime/test/code_hooks_test.cpp index 6c1fb4fb1..cb5e266da 100644 --- a/runtime/test/code_hooks_test.cpp +++ b/runtime/test/code_hooks_test.cpp @@ -100,6 +100,29 @@ int main() { check(!hook_image_calls(self, 0, &remap_lucia, 0).ok, "an RVA in the headers (not code) is refused"); check(!make_remap_thunk(target0, 2, &remap_lucia, target0), "only argument 0 or 1 can be remapped"); + // The result-override thunk (a view-mode getter): the original returns 2 ("third person, far") for every + // context; the override answers 4 ("first person") for context 0 only and leaves the others as they were. + uint8_t* getter = mem + 512; + put(getter, {0xB8, 0x02, 0x00, 0x00, 0x00, 0xC3}); // mov eax, 2 ; ret + uint8_t* call_getter = mem + 544; + caller(call_getter, getter); + static int original_calls = 0; + auto first_person_on_foot = [](uint32_t result, uint32_t context) -> uint32_t { + ++original_calls; + return context == 0 ? 4u : result; + }; + uint8_t* rthunk = make_result_thunk(getter, first_person_on_foot, getter); + check(rthunk != nullptr, "a result thunk is allocated"); + if (rthunk) { + check(redirect_direct_branches(call_getter, 16, getter, rthunk) == 1, "the getter's caller is redirected"); + FlushInstructionCache(GetCurrentProcess(), mem, 4096); + using Ctx = uint32_t (*)(uint32_t); + check(reinterpret_cast(call_getter)(0) == 4, "context 0 gets the overridden result"); + check(reinterpret_cast(call_getter)(3) == 2, "another context keeps the original's result"); + check(original_calls == 2, "the override sees every call, after the original ran"); + } + check(!hook_image_results(mem, 0, first_person_on_foot).ok, "hook_image_results refuses memory without a PE"); + std::printf("code_hooks_test: %d checks, %d failures\n", g_checks, g_failures); return g_failures == 0 ? 0 : 1; } diff --git a/runtime/test/mods_test.cpp b/runtime/test/mods_test.cpp index d6d369426..c61182c3c 100644 --- a/runtime/test/mods_test.cpp +++ b/runtime/test/mods_test.cpp @@ -56,6 +56,34 @@ int main() { check(!parse_mod("[1,2]", "m", mod, error), "a manifest that is not an object is refused"); check(parse_mod("{}", "folder_name", mod, error) && mod.name == "folder_name", "the name defaults to the folder"); + // "camera": first-person view (docs/modding.md, "First-person view") + check(parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":4,"contexts":[0]}})", "fp", mod, + error) && mod.camera.set && mod.camera.first_person && mod.camera.first_person_value == 4 && + mod.camera.contexts.size() == 1 && mod.camera.contexts[0] == 0, + "a first-person camera section parses"); + check(parse_mod(R"({"camera":{"view_mode":"default"}})", "d", mod, error) && mod.camera.set && + !mod.camera.first_person, + "\"default\" leaves the game's view mode"); + check(!parse_mod(R"({"camera":{"view_mode":"top_down"}})", "x", mod, error), "an unknown view mode is refused"); + check(!parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":4.5}})", "x", mod, error), + "a fractional enum value is refused"); + check(!parse_mod(R"({"camera":{"view_mode":"first_person","contexts":"all"}})", "x", mod, error), + "contexts must be a list"); + + Mod fp_on_foot, fp_everywhere, off; + check(parse_mod(R"({"camera":{"view_mode":"first_person","contexts":[0]}})", "a", fp_on_foot, error) && + parse_mod(R"({"camera":{"view_mode":"first_person","first_person_value":7}})", "b", fp_everywhere, + error) && + parse_mod(R"({"enabled":false,"camera":{"view_mode":"default"}})", "c", off, error), + "three camera mods parse"); + set_active_camera(camera_of({fp_on_foot})); + check(override_view_mode(2, 0) == 4, "on foot (context 0) the view becomes first person"); + check(override_view_mode(2, 1) == 2, "a context the mod does not list keeps the game's view"); + set_active_camera(camera_of({fp_on_foot, fp_everywhere, off})); + check(override_view_mode(1, 5) == 7, "the last enabled camera mod decides (a disabled one is skipped)"); + set_active_camera(camera_of({})); + check(override_view_mode(3, 0) == 3, "with no camera mod the game's view mode passes through"); + const auto dir = std::filesystem::temp_directory_path() / "leonida_mods_test"; std::filesystem::remove_all(dir); write(dir / "a_mandy" / "mod.json", -- cgit v1.2.3