upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/main.cpp
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 22:53:19 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 22:53:19 -0500
commit9e6444fadb93c9b800ed403f91fcfee1a88d3560 (patch)
treebe1cf4a298295b74268c5a652210ca34b5069b7e /runtime/src/main.cpp
parent2d3369a4c82e6f36c5190c4c534d3df600e135ec (diff)
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp37
1 files changed, 32 insertions, 5 deletions
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 @@
19#include <windows.h> 19#include <windows.h>
20 20
21#include <cstdio> 21#include <cstdio>
22#include <cstdlib>
22#include <filesystem> 23#include <filesystem>
23#include <map> 24#include <map>
24#include <string> 25#include <string>
@@ -26,6 +27,7 @@
26#include <vector> 27#include <vector>
27 28
28#include "base_reservation.h" 29#include "base_reservation.h"
30#include "code_hooks.h"
29#include "mods.h" 31#include "mods.h"
30#include "pe_loader.h" 32#include "pe_loader.h"
31#include "../shims/d3d12_trace.h" 33#include "../shims/d3d12_trace.h"
@@ -47,9 +49,9 @@ const char* binding_name(rt::Binding b) {
47 49
48int usage() { 50int usage() {
49 std::fprintf(stderr, 51 std::fprintf(stderr,
50 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--trace FILE]\n" 52 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace FILE]\n"
51 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--trace-d3d12] [--trace FILE]\n" 53 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace-d3d12] [--trace FILE]\n"
52 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); 54 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n");
53 return 2; 55 return 2;
54} 56}
55 57
@@ -57,6 +59,8 @@ int usage() {
57 59
58int wmain(int argc, wchar_t** argv) { 60int wmain(int argc, wchar_t** argv) {
59 std::filesystem::path image, game_dir, overlay_dir, mods_dir; 61 std::filesystem::path image, game_dir, overlay_dir, mods_dir;
62 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
63 int archetype_lookup_arg = 0;
60 bool dry_run = false, init = false, run = false, trace_d3d12 = false; 64 bool dry_run = false, init = false, run = false, trace_d3d12 = false;
61 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) 65 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
62 std::wstring trace_file; 66 std::wstring trace_file;
@@ -74,6 +78,15 @@ int wmain(int argc, wchar_t** argv) {
74 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; 78 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i];
75 else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i]; 79 else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i];
76 else if (a == L"--mods" && i + 1 < argc) mods_dir = argv[++i]; 80 else if (a == L"--mods" && i + 1 < argc) mods_dir = argv[++i];
81 else if (a == L"--archetype-lookup" && i + 1 < argc) {
82 const std::wstring v = argv[++i];
83 const auto comma = v.find(L',');
84 wchar_t* end = nullptr;
85 const unsigned long rva = std::wcstoul(v.substr(0, comma).c_str(), &end, 16);
86 if (!end || *end || rva == 0) return usage();
87 archetype_lookup_rva = static_cast<uint32_t>(rva);
88 if (comma != std::wstring::npos) archetype_lookup_arg = v.substr(comma + 1) == L"1" ? 1 : 0;
89 }
77 else if (image.empty()) image = a; 90 else if (image.empty()) image = a;
78 else return usage(); 91 else return usage();
79 } 92 }
@@ -115,8 +128,9 @@ int wmain(int argc, wchar_t** argv) {
115 } 128 }
116 leonida::mods::set_active(leonida::mods::ArchetypeSwaps(mods.mods)); 129 leonida::mods::set_active(leonida::mods::ArchetypeSwaps(mods.mods));
117 if (leonida::mods::active().size()) 130 if (leonida::mods::active().size())
118 std::fprintf(stderr, "[rt] mods: %zu archetype swap(s) loaded; not applied yet: the archetype lookup hook " 131 std::fprintf(stderr, archetype_lookup_rva ? "[rt] mods: %zu archetype swap(s) loaded\n"
119 "for this executable is not known (docs/modding.md)\n", leonida::mods::active().size()); 132 : "[rt] mods: %zu archetype swap(s) loaded; not applied: no --archetype-lookup RVA for "
133 "this executable (docs/modding.md)\n", leonida::mods::active().size());
120 } 134 }
121 rt::d3d12_trace::set_enabled(trace_d3d12); 135 rt::d3d12_trace::set_enabled(trace_d3d12);
122 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { 136 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) {
128 try { 142 try {
129 rt::Loader loader(game_dir); 143 rt::Loader loader(game_dir);
130 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code 144 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code
145 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA
146 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup.
147 if (archetype_lookup_rva) {
148 const auto r = leonida::hooks::hook_image_calls(
149 main_module.base, archetype_lookup_rva,
150 [](uint32_t hash) { return leonida::mods::active().remap(hash); }, archetype_lookup_arg);
151 if (r.ok)
152 std::fprintf(stderr, "[rt] mods: archetype lookup at RVA 0x%X hooked (argument %d): %zu direct "
153 "call(s) redirected\n", archetype_lookup_rva, archetype_lookup_arg, r.redirected);
154 else
155 std::fprintf(stderr, "[rt] mods: archetype lookup at RVA 0x%X not hooked: %s\n", archetype_lookup_rva,
156 r.error);
157 }
131 if (run) rt::process::set_command_line(image.filename().wstring(), game_args); 158 if (run) rt::process::set_command_line(image.filename().wstring(), game_args);
132 if (init || run) { 159 if (init || run) {
133 loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs 160 loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs