From db51b7d3f999ab57898cf039f8289d807b63523c Mon Sep 17 00:00:00 2001 From: ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:41:02 -0500 Subject: Mods: mod.json manifests with archetype swaps, and the Mandy prison-intro mod gta6rt reads mods//mod.json next to gta6rt.exe (or --mods DIR), checks each and logs it. An archetype swap names two models the game has (by joaat name or 0x hash): when the game asks for `from` it gets `to`. The swaps are loaded into leonida::mods::active(); they apply once the game's archetype lookup is hooked, which needs the executable's address for it (docs/modding.md, Day one). mods/mandy_prison_intro swaps Lucia's player model player_story000_00 (0xE472B687) for cs_mandy (0xD7C0CDFF), the ids measured in the packs tables; mods_test checks joaat reproduces both. Conditions narrower than "always" are refused until a hook provides mission state. No game data in either. Co-Authored-By: Claude Opus 5.5 --- runtime/src/main.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'runtime/src/main.cpp') diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 49917bf18..ff15ec451 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -26,6 +26,7 @@ #include #include "base_reservation.h" +#include "mods.h" #include "pe_loader.h" #include "../shims/d3d12_trace.h" #include "../shims/file_system.h" @@ -46,16 +47,16 @@ const char* binding_name(rt::Binding b) { int usage() { std::fprintf(stderr, - "usage: gta6rt --dry-run [--game-dir DIR] [--overlay DIR] [--trace FILE]\n" - " gta6rt --init [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE]\n" - " gta6rt --run [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); + "usage: gta6rt --dry-run [--game-dir DIR] [--overlay DIR] [--mods DIR] [--trace FILE]\n" + " gta6rt --init [--game-dir DIR] [--overlay DIR] [--mods DIR] [--trace-d3d12] [--trace FILE]\n" + " gta6rt --run [--game-dir DIR] [--overlay DIR] [--mods DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); return 2; } } // namespace int wmain(int argc, wchar_t** argv) { - std::filesystem::path image, game_dir, overlay_dir; + std::filesystem::path image, game_dir, overlay_dir, mods_dir; 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; @@ -72,6 +73,7 @@ int wmain(int argc, wchar_t** argv) { else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; 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 (image.empty()) image = a; else return usage(); } @@ -95,6 +97,27 @@ int wmain(int argc, wchar_t** argv) { // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; rt::fs::configure(game_dir, overlay_dir); + // Mods (docs/modding.md): \mods unless --mods names another. Loaded and reported here; + // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version. + if (mods_dir.empty()) { + std::wstring self(32768, L'\0'); + const DWORD n = GetModuleFileNameW(nullptr, self.data(), static_cast(self.size())); + if (n && n < self.size()) mods_dir = std::filesystem::path(self.substr(0, n)).parent_path() / L"mods"; + } + { + const leonida::mods::LoadResult mods = leonida::mods::load_mods(mods_dir); + for (const std::string& e : mods.errors) std::fprintf(stderr, "[rt] mods: skipped %s\n", e.c_str()); + for (const auto& m : mods.mods) { + std::fprintf(stderr, "[rt] mods: %s%s\n", m.name.c_str(), m.enabled ? "" : " (disabled)"); + for (const auto& sw : m.archetype_swaps) + std::fprintf(stderr, "[rt] mods: archetype %s (0x%08X) -> %s (0x%08X)\n", sw.from_name.c_str(), + sw.from, sw.to_name.c_str(), sw.to); + } + leonida::mods::set_active(leonida::mods::ArchetypeSwaps(mods.mods)); + if (leonida::mods::active().size()) + std::fprintf(stderr, "[rt] mods: %zu archetype swap(s) loaded; not applied yet: the archetype lookup hook " + "for this executable is not known (docs/modding.md)\n", leonida::mods::active().size()); + } rt::d3d12_trace::set_enabled(trace_d3d12); if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" -- cgit v1.2.3 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 --- docs/modding.md | 22 +++++-- runtime/CMakeLists.txt | 7 ++ runtime/src/code_hooks.h | 138 +++++++++++++++++++++++++++++++++++++++ runtime/src/main.cpp | 37 +++++++++-- runtime/test/code_hooks_test.cpp | 105 +++++++++++++++++++++++++++++ 5 files changed, 300 insertions(+), 9 deletions(-) create mode 100644 runtime/src/code_hooks.h create mode 100644 runtime/test/code_hooks_test.cpp (limited to 'runtime/src/main.cpp') diff --git a/docs/modding.md b/docs/modding.md index a23dcf45f..9d99eb356 100644 --- a/docs/modding.md +++ b/docs/modding.md @@ -1,6 +1,6 @@ # Mods -**Status: the mod format and loader are in place; swaps are not applied yet.** `gta6rt` reads every +**Status: the mod format, loader and hook are in place; only the lookup's address in the executable is missing.** `gta6rt` reads every `mods//mod.json`, checks it and logs what it would change. A swap takes effect once the game's archetype lookup is hooked, and that needs the executable (RT-5): its address differs per version. @@ -85,6 +85,20 @@ The swap needs one function in the executable: the archetype lookup by name hash 3. **Cross-check.** Called with `0xD7C0CDFF` it must return a valid archetype too (`cs_mandy` is a live model id: a `story000.rpf` cutscene table uses it). -Once its RVA is known for 1.0.2.1, the loader installs a hook there that calls -`leonida::mods::active().remap(hash)` on the argument before the original runs. That is the only -version-specific piece; the manifests stay as they are. +Then pass it to the runtime: + +```powershell +gta6rt.exe --dry-run game_scarlett_master.exe --archetype-lookup 1A2B3C0 +``` + +`--archetype-lookup RVA[,ARG]` (hex RVA; `ARG` 0 when the hash is the first argument, 1 when it is the +second, as in a member function) hooks the function right after the image is mapped, before any of its +code runs (`runtime/src/code_hooks.h`). It does not patch the function: every direct `call` and `jmp` +to it in the image's code is pointed at a thunk that passes the hash through +`leonida::mods::active().remap()` and continues into the original with every other argument as it was. +The log line reports how many call sites were redirected, so a `--dry-run` with a candidate RVA shows at +once whether it is the lookup (hundreds of callers) or not (a handful, or none). ctest `code_hooks` runs +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. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 069d53aaa..851330b99 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2236,6 +2236,13 @@ if(TARGET Vulkan::Headers) add_executable(mods_test test/mods_test.cpp) target_link_libraries(mods_test PRIVATE Vulkan::Headers) add_test(NAME mods COMMAND mods_test) + if(WIN32) + # code_hooks.h: the remapping thunk and direct-call redirection the archetype hook uses (x64 Windows) + add_executable(code_hooks_test test/code_hooks_test.cpp) + target_link_libraries(code_hooks_test PRIVATE Vulkan::Headers) + target_compile_definitions(code_hooks_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + add_test(NAME code_hooks COMMAND code_hooks_test) + endif() # The subgroup size a pipeline stage asks for, from synthetic index entries and device properties. add_executable(subgroup_size_test test/subgroup_size_test.cpp) target_link_libraries(subgroup_size_test PRIVATE Vulkan::Headers) 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 diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index ff15ec451..a6f41e8d1 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -26,6 +27,7 @@ #include #include "base_reservation.h" +#include "code_hooks.h" #include "mods.h" #include "pe_loader.h" #include "../shims/d3d12_trace.h" @@ -47,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] [--trace FILE]\n" - " gta6rt --init [--game-dir DIR] [--overlay DIR] [--mods DIR] [--trace-d3d12] [--trace FILE]\n" - " gta6rt --run [--game-dir DIR] [--overlay DIR] [--mods DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); + "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"); return 2; } @@ -57,6 +59,8 @@ int usage() { 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; 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; @@ -74,6 +78,15 @@ 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"--archetype-lookup" && i + 1 < argc) { + const std::wstring v = argv[++i]; + const auto comma = v.find(L','); + wchar_t* end = nullptr; + const unsigned long rva = std::wcstoul(v.substr(0, comma).c_str(), &end, 16); + if (!end || *end || rva == 0) return usage(); + archetype_lookup_rva = static_cast(rva); + if (comma != std::wstring::npos) archetype_lookup_arg = v.substr(comma + 1) == L"1" ? 1 : 0; + } else if (image.empty()) image = a; else return usage(); } @@ -115,8 +128,9 @@ int wmain(int argc, wchar_t** argv) { } leonida::mods::set_active(leonida::mods::ArchetypeSwaps(mods.mods)); if (leonida::mods::active().size()) - std::fprintf(stderr, "[rt] mods: %zu archetype swap(s) loaded; not applied yet: the archetype lookup hook " - "for this executable is not known (docs/modding.md)\n", 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 " + "this executable (docs/modding.md)\n", leonida::mods::active().size()); } rt::d3d12_trace::set_enabled(trace_d3d12); if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { @@ -128,6 +142,19 @@ int wmain(int argc, wchar_t** argv) { try { rt::Loader loader(game_dir); 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 (archetype_lookup_rva) { + const auto r = leonida::hooks::hook_image_calls( + main_module.base, archetype_lookup_rva, + [](uint32_t hash) { return leonida::mods::active().remap(hash); }, archetype_lookup_arg); + if (r.ok) + std::fprintf(stderr, "[rt] mods: archetype lookup at RVA 0x%X hooked (argument %d): %zu direct " + "call(s) redirected\n", archetype_lookup_rva, archetype_lookup_arg, r.redirected); + else + std::fprintf(stderr, "[rt] mods: archetype lookup at RVA 0x%X not hooked: %s\n", archetype_lookup_rva, + r.error); + } if (run) rt::process::set_command_line(image.filename().wstring(), game_args); if (init || run) { loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs diff --git a/runtime/test/code_hooks_test.cpp b/runtime/test/code_hooks_test.cpp new file mode 100644 index 000000000..6c1fb4fb1 --- /dev/null +++ b/runtime/test/code_hooks_test.cpp @@ -0,0 +1,105 @@ +// code_hooks.h: a remapping thunk and direct-call redirection, on small x64 functions written into memory. No game +// files: the archetype lookup they stand in for takes its model hash in ecx (docs/modding.md). +#include "../src/code_hooks.h" +#include "../src/mods.h" + +#include +#include + +namespace { + +int g_checks = 0, g_failures = 0; + +void check(bool ok, const char* what) { + ++g_checks; + if (!ok) { + ++g_failures; + std::printf("FAIL %s\n", what); + } +} + +uint32_t remap_lucia(uint32_t hash) { + return hash == leonida::mods::joaat("player_story000_00") ? leonida::mods::joaat("cs_mandy") : hash; +} + +void put(uint8_t* at, std::initializer_list bytes) { + for (uint8_t b : bytes) *at++ = b; +} + +// `call rel32` (or `jmp` with e9) at `at` towards `to`. +void branch(uint8_t* at, uint8_t opcode, const uint8_t* to) { + at[0] = opcode; + const int32_t rel = static_cast(to - (at + 5)); + std::memcpy(at + 1, &rel, 4); +} + +// sub rsp,0x28 / call to / add rsp,0x28 / ret +void caller(uint8_t* at, const uint8_t* to) { + put(at, {0x48, 0x83, 0xEC, 0x28}); + branch(at + 4, 0xE8, to); + put(at + 9, {0x48, 0x83, 0xC4, 0x28, 0xC3}); +} + +} // namespace + +int main() { + using namespace leonida::hooks; + uint8_t* mem = alloc_near(reinterpret_cast(&remap_lucia), 4096); + check(mem != nullptr, "executable memory near the module"); + if (!mem) return 1; + std::memset(mem, 0xCC, 4096); + + uint8_t* target0 = mem; // returns its first argument: mov eax, ecx ; ret + put(target0, {0x89, 0xC8, 0xC3}); + uint8_t* target1 = mem + 16; // returns its second argument: mov eax, edx ; ret + put(target1, {0x89, 0xD0, 0xC3}); + uint8_t* target2 = mem + 32; // returns its third argument: mov eax, r8d ; ret + put(target2, {0x44, 0x89, 0xC0, 0xC3}); + + uint8_t* call0 = mem + 64; + caller(call0, target0); + uint8_t* tail0 = mem + 96; // jmp target0 (a tail call) + branch(tail0, 0xE9, target0); + uint8_t* decoy = mem + 112; // a call to target0 + 1: not the hooked function, must stay as it is + caller(decoy, target0 + 1); + uint8_t* call1 = mem + 144; + caller(call1, target1); + uint8_t* call2 = mem + 176; + caller(call2, target2); + uint8_t decoy_before[16]; + std::memcpy(decoy_before, decoy, 16); + + uint8_t* thunk0 = make_remap_thunk(target0, 0, &remap_lucia, target0); + uint8_t* thunk1 = make_remap_thunk(target1, 1, &remap_lucia, target1); + uint8_t* thunk2 = make_remap_thunk(target2, 0, &remap_lucia, target2); + check(thunk0 && thunk1 && thunk2, "thunks are allocated near their targets"); + if (!thunk0 || !thunk1 || !thunk2) return 1; + + const std::size_t n0 = redirect_direct_branches(mem + 64, 112, target0, thunk0); + const std::size_t n1 = redirect_direct_branches(call1, 16, target1, thunk1); + const std::size_t n2 = redirect_direct_branches(call2, 16, target2, thunk2); + check(n0 == 2, "the call and the tail jump to the function are redirected, the decoy is not"); + check(n1 == 1 && n2 == 1, "one call each in the other callers"); + check(std::memcmp(decoy, decoy_before, 16) == 0, "a branch to another address is left byte for byte"); + FlushInstructionCache(GetCurrentProcess(), mem, 4096); + + using Fn1 = uint32_t (*)(uint32_t); + using Fn2 = uint32_t (*)(uint64_t, uint32_t); + using Fn3 = uint32_t (*)(uint32_t, uint32_t, uint32_t); + const uint32_t lucia = 0xE472B687u, mandy = 0xD7C0CDFFu; + check(reinterpret_cast(call0)(lucia) == mandy, "through the call, Lucia's model id becomes cs_mandy's"); + check(reinterpret_cast(call0)(1234) == 1234, "any other id passes through unchanged"); + check(reinterpret_cast(tail0)(lucia) == mandy, "a tail jump is remapped as well"); + check(reinterpret_cast(call1)(0x1111, lucia) == mandy, "argument 1 (edx) is remapped when asked"); + check(reinterpret_cast(call2)(lucia, 2, 777) == 777, "r8 reaches the original unchanged"); + + // hook_image_calls refuses what is not a mapped image or not code + check(!hook_image_calls(mem, 0, &remap_lucia, 0).ok, "memory without a PE header is refused"); + auto* self = reinterpret_cast(GetModuleHandleW(nullptr)); + check(!hook_image_calls(self, 0x7FFFFFF0u, &remap_lucia, 0).ok, "an RVA outside the image is refused"); + 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"); + + std::printf("code_hooks_test: %d checks, %d failures\n", g_checks, g_failures); + return g_failures == 0 ? 0 : 1; +} -- 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/main.cpp') 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