upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 23:14:27 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 23:14:27 -0500
commit98fa503f8a19c3ed65df1f4f2f0e4571aa42c4f7 (patch)
tree9f17c922e8deebaa4d0cd0a8eb59125349f5bda0 /runtime/src
parent0db7fed551c4d48d7b97a9c4d609fc11d17ae19c (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')
-rw-r--r--runtime/src/code_hooks.h48
-rw-r--r--runtime/src/main.cpp29
-rw-r--r--runtime/src/mods.h72
3 files changed, 144 insertions, 5 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
77using 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.
83inline 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.
79inline std::size_t redirect_direct_branches(uint8_t* begin, std::size_t size, const uint8_t* target, 107inline 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.
106inline 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.
136template <class MakeThunk>
137inline 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`.
170inline 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.
177inline 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
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) {
49 49
50int usage() { 50int usage() {
51 std::fprintf(stderr, 51 std::fprintf(stderr,
52 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace FILE]\n" 52 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n"
53 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace-d3d12] [--trace FILE]\n" 53 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n"
54 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); 54 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n");
55 return 2; 55 return 2;
56} 56}
57 57
@@ -61,6 +61,7 @@ int wmain(int argc, wchar_t** argv) {
61 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 62 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
63 int archetype_lookup_arg = 0; 63 int archetype_lookup_arg = 0;
64 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context
64 bool dry_run = false, init = false, run = false, trace_d3d12 = false; 65 bool dry_run = false, init = false, run = false, trace_d3d12 = false;
65 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) 66 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
66 std::wstring trace_file; 67 std::wstring trace_file;
@@ -78,6 +79,12 @@ int wmain(int argc, wchar_t** argv) {
78 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; 79 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i];
79 else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i]; 80 else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i];
80 else if (a == L"--mods" && i + 1 < argc) mods_dir = argv[++i]; 81 else if (a == L"--mods" && i + 1 < argc) mods_dir = argv[++i];
82 else if (a == L"--view-mode-getter" && i + 1 < argc) {
83 wchar_t* end = nullptr;
84 const unsigned long rva = std::wcstoul(argv[++i], &end, 16);
85 if (!end || *end || rva == 0) return usage();
86 view_mode_getter_rva = static_cast<uint32_t>(rva);
87 }
81 else if (a == L"--archetype-lookup" && i + 1 < argc) { 88 else if (a == L"--archetype-lookup" && i + 1 < argc) {
82 const std::wstring v = argv[++i]; 89 const std::wstring v = argv[++i];
83 const auto comma = v.find(L','); 90 const auto comma = v.find(L',');
@@ -127,6 +134,12 @@ int wmain(int argc, wchar_t** argv) {
127 sw.from, sw.to_name.c_str(), sw.to); 134 sw.from, sw.to_name.c_str(), sw.to);
128 } 135 }
129 leonida::mods::set_active(leonida::mods::ArchetypeSwaps(mods.mods)); 136 leonida::mods::set_active(leonida::mods::ArchetypeSwaps(mods.mods));
137 const leonida::mods::CameraView camera = leonida::mods::camera_of(mods.mods);
138 leonida::mods::set_active_camera(camera);
139 if (camera.first_person)
140 std::fprintf(stderr, "[rt] mods: first-person view (value %u) for %s%s\n", camera.first_person_value,
141 camera.contexts.empty() ? "every context" : "the listed contexts",
142 view_mode_getter_rva ? "" : "; not applied: no --view-mode-getter RVA for this executable");
130 if (leonida::mods::active().size()) 143 if (leonida::mods::active().size())
131 std::fprintf(stderr, archetype_lookup_rva ? "[rt] mods: %zu archetype swap(s) loaded\n" 144 std::fprintf(stderr, archetype_lookup_rva ? "[rt] mods: %zu archetype swap(s) loaded\n"
132 : "[rt] mods: %zu archetype swap(s) loaded; not applied: no --archetype-lookup RVA for " 145 : "[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) {
144 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code 157 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 158 // 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. 159 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup.
160 if (view_mode_getter_rva) {
161 const auto r = leonida::hooks::hook_image_results(main_module.base, view_mode_getter_rva,
162 &leonida::mods::override_view_mode);
163 if (r.ok)
164 std::fprintf(stderr, "[rt] mods: view-mode getter at RVA 0x%X hooked: %zu direct call(s) redirected\n",
165 view_mode_getter_rva, r.redirected);
166 else
167 std::fprintf(stderr, "[rt] mods: view-mode getter at RVA 0x%X not hooked: %s\n", view_mode_getter_rva,
168 r.error);
169 }
147 if (archetype_lookup_rva) { 170 if (archetype_lookup_rva) {
148 const auto r = leonida::hooks::hook_image_calls( 171 const auto r = leonida::hooks::hook_image_calls(
149 main_module.base, archetype_lookup_rva, 172 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 {
49 std::string when = "always"; 49 std::string when = "always";
50}; 50};
51 51
52// "camera": the view mode the game's view-mode getter answers (docs/modding.md, "First-person view"). The getter
53// takes a context (on foot, in a vehicle, ...) and returns an enum; `first_person_value` is that enum's first-person
54// value, 4 in GTA V's camViewModeSettings, to be confirmed in the executable. `contexts`: the getter's first-argument
55// values to override; empty = every context.
56struct CameraView {
57 bool set = false; // the mod has a "camera" section
58 bool first_person = false; // "view_mode": "first_person" (else "default": leave the game's choice)
59 uint32_t first_person_value = 4;
60 std::vector<uint32_t> contexts;
61};
62
52struct Mod { 63struct Mod {
53 std::string folder; // the folder name under mods/ 64 std::string folder; // the folder name under mods/
54 std::string name; 65 std::string name;
55 std::string game_version; 66 std::string game_version;
56 bool enabled = true; 67 bool enabled = true;
57 std::vector<ArchetypeSwap> archetype_swaps; 68 std::vector<ArchetypeSwap> archetype_swaps;
69 CameraView camera;
58}; 70};
59 71
60struct LoadResult { 72struct LoadResult {
@@ -135,6 +147,40 @@ inline bool parse_mod(std::string_view text, const std::string& folder, Mod& mod
135 mod.archetype_swaps.push_back(std::move(swap)); 147 mod.archetype_swaps.push_back(std::move(swap));
136 } 148 }
137 } 149 }
150 if (const Json* cam = root.find("camera")) {
151 if (cam->kind != Json::Kind::object) {
152 error = "\"camera\" must be an object";
153 return false;
154 }
155 mod.camera.set = true;
156 const Json* view = cam->find("view_mode");
157 if (!view || view->kind != Json::Kind::string ||
158 (view->string != "first_person" && view->string != "default")) {
159 error = "\"camera\": \"view_mode\" must be \"first_person\" or \"default\"";
160 return false;
161 }
162 mod.camera.first_person = view->string == "first_person";
163 if (const Json* v = cam->find("first_person_value")) {
164 if (v->kind != Json::Kind::number || v->number < 0 || v->number > 255 || v->number != static_cast<int>(v->number)) {
165 error = "\"camera\": \"first_person_value\" must be a whole number 0-255";
166 return false;
167 }
168 mod.camera.first_person_value = static_cast<uint32_t>(v->number);
169 }
170 if (const Json* c = cam->find("contexts")) {
171 if (c->kind != Json::Kind::array) {
172 error = "\"camera\": \"contexts\" must be a list of numbers";
173 return false;
174 }
175 for (const Json& x : c->array) {
176 if (x.kind != Json::Kind::number || x.number < 0 || x.number != static_cast<int>(x.number)) {
177 error = "\"camera\": \"contexts\" must be a list of whole numbers";
178 return false;
179 }
180 mod.camera.contexts.push_back(static_cast<uint32_t>(x.number));
181 }
182 }
183 }
138 return true; 184 return true;
139} 185}
140 186
@@ -199,4 +245,30 @@ inline ArchetypeSwaps& active_slot() {
199inline void set_active(ArchetypeSwaps swaps) { active_slot() = std::move(swaps); } 245inline void set_active(ArchetypeSwaps swaps) { active_slot() = std::move(swaps); }
200inline const ArchetypeSwaps& active() { return active_slot(); } 246inline const ArchetypeSwaps& active() { return active_slot(); }
201 247
248// The camera override of the enabled mods: the last one (in folder order) with a "camera" section decides.
249inline CameraView camera_of(const std::vector<Mod>& mods) {
250 CameraView view;
251 for (const Mod& m : mods)
252 if (m.enabled && m.camera.set) view = m.camera;
253 return view;
254}
255
256inline CameraView& camera_slot() {
257 static CameraView view;
258 return view;
259}
260inline void set_active_camera(CameraView view) { camera_slot() = std::move(view); }
261
262// What the hooked view-mode getter returns: the mod's first-person value for an overridden context, else the game's.
263inline uint32_t override_view_mode(uint32_t result, uint32_t context) {
264 const CameraView& v = camera_slot();
265 if (!v.first_person) return result;
266 if (!v.contexts.empty()) {
267 bool listed = false;
268 for (uint32_t c : v.contexts) listed = listed || c == context;
269 if (!listed) return result;
270 }
271 return v.first_person_value;
272}
273
202} // namespace leonida::mods 274} // namespace leonida::mods