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:
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp81
1 files changed, 77 insertions, 4 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 6c4d0e641..753dd6d28 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,8 @@
26#include <vector> 27#include <vector>
27 28
28#include "base_reservation.h" 29#include "base_reservation.h"
30#include "code_hooks.h"
31#include "mods.h"
29#include "pe_loader.h" 32#include "pe_loader.h"
30#include "../shims/d3d12_trace.h" 33#include "../shims/d3d12_trace.h"
31#include "../shims/file_system.h" 34#include "../shims/file_system.h"
@@ -56,16 +59,19 @@ extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_t
56 59
57int usage() { 60int usage() {
58 std::fprintf(stderr, 61 std::fprintf(stderr,
59 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n" 62 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n"
60 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE]\n" 63 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n"
61 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); 64 " 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");
62 return 2; 65 return 2;
63} 66}
64 67
65} // namespace 68} // namespace
66 69
67int wmain(int argc, wchar_t** argv) { 70int wmain(int argc, wchar_t** argv) {
68 std::filesystem::path image, game_dir, overlay_dir; 71 std::filesystem::path image, game_dir, overlay_dir, mods_dir;
72 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
73 int archetype_lookup_arg = 0;
74 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context
69 bool dry_run = false, init = false, run = false, trace_d3d12 = false; 75 bool dry_run = false, init = false, run = false, trace_d3d12 = false;
70 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) 76 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
71 std::wstring trace_file; 77 std::wstring trace_file;
@@ -82,6 +88,22 @@ int wmain(int argc, wchar_t** argv) {
82 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; 88 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i];
83 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; 89 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i];
84 else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i]; 90 else if (a == L"--overlay" && i + 1 < argc) overlay_dir = argv[++i];
91 else if (a == L"--mods" && i + 1 < argc) mods_dir = argv[++i];
92 else if (a == L"--view-mode-getter" && i + 1 < argc) {
93 wchar_t* end = nullptr;
94 const unsigned long rva = std::wcstoul(argv[++i], &end, 16);
95 if (!end || *end || rva == 0) return usage();
96 view_mode_getter_rva = static_cast<uint32_t>(rva);
97 }
98 else if (a == L"--archetype-lookup" && i + 1 < argc) {
99 const std::wstring v = argv[++i];
100 const auto comma = v.find(L',');
101 wchar_t* end = nullptr;
102 const unsigned long rva = std::wcstoul(v.substr(0, comma).c_str(), &end, 16);
103 if (!end || *end || rva == 0) return usage();
104 archetype_lookup_rva = static_cast<uint32_t>(rva);
105 if (comma != std::wstring::npos) archetype_lookup_arg = v.substr(comma + 1) == L"1" ? 1 : 0;
106 }
85 else if (image.empty()) image = a; 107 else if (image.empty()) image = a;
86 else return usage(); 108 else return usage();
87 } 109 }
@@ -105,6 +127,34 @@ int wmain(int argc, wchar_t** argv) {
105 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. 127 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package.
106 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; 128 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay";
107 rt::fs::configure(game_dir, overlay_dir); 129 rt::fs::configure(game_dir, overlay_dir);
130 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here;
131 // the archetype swaps take effect once the game's archetype lookup is hooked for this executable version.
132 if (mods_dir.empty()) {
133 std::wstring self(32768, L'\0');
134 const DWORD n = GetModuleFileNameW(nullptr, self.data(), static_cast<DWORD>(self.size()));
135 if (n && n < self.size()) mods_dir = std::filesystem::path(self.substr(0, n)).parent_path() / L"mods";
136 }
137 {
138 const leonida::mods::LoadResult mods = leonida::mods::load_mods(mods_dir);
139 for (const std::string& e : mods.errors) std::fprintf(stderr, "[rt] mods: skipped %s\n", e.c_str());
140 for (const auto& m : mods.mods) {
141 std::fprintf(stderr, "[rt] mods: %s%s\n", m.name.c_str(), m.enabled ? "" : " (disabled)");
142 for (const auto& sw : m.archetype_swaps)
143 std::fprintf(stderr, "[rt] mods: archetype %s (0x%08X) -> %s (0x%08X)\n", sw.from_name.c_str(),
144 sw.from, sw.to_name.c_str(), sw.to);
145 }
146 leonida::mods::set_active(leonida::mods::ArchetypeSwaps(mods.mods));
147 const leonida::mods::CameraView camera = leonida::mods::camera_of(mods.mods);
148 leonida::mods::set_active_camera(camera);
149 if (camera.first_person)
150 std::fprintf(stderr, "[rt] mods: first-person view (value %u) for %s%s\n", camera.first_person_value,
151 camera.contexts.empty() ? "every context" : "the listed contexts",
152 view_mode_getter_rva ? "" : "; not applied: no --view-mode-getter RVA for this executable");
153 if (leonida::mods::active().size())
154 std::fprintf(stderr, archetype_lookup_rva ? "[rt] mods: %zu archetype swap(s) loaded\n"
155 : "[rt] mods: %zu archetype swap(s) loaded; not applied: no --archetype-lookup RVA for "
156 "this executable (docs/modding.md)\n", leonida::mods::active().size());
157 }
108 rt::d3d12_trace::set_enabled(trace_d3d12); 158 rt::d3d12_trace::set_enabled(trace_d3d12);
109 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { 159 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) {
110 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" 160 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n"
@@ -129,6 +179,29 @@ int wmain(int argc, wchar_t** argv) {
129 main_module.name.c_str(), why.c_str()); 179 main_module.name.c_str(), why.c_str());
130 return 1; 180 return 1;
131 } 181 }
182 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA
183 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup.
184 if (view_mode_getter_rva) {
185 const auto r = leonida::hooks::hook_image_results(main_module.base, view_mode_getter_rva,
186 &leonida::mods::override_view_mode);
187 if (r.ok)
188 std::fprintf(stderr, "[rt] mods: view-mode getter at RVA 0x%X hooked: %zu direct call(s) redirected\n",
189 view_mode_getter_rva, r.redirected);
190 else
191 std::fprintf(stderr, "[rt] mods: view-mode getter at RVA 0x%X not hooked: %s\n", view_mode_getter_rva,
192 r.error);
193 }
194 if (archetype_lookup_rva) {
195 const auto r = leonida::hooks::hook_image_calls(
196 main_module.base, archetype_lookup_rva,
197 [](uint32_t hash) { return leonida::mods::active().remap(hash); }, archetype_lookup_arg);
198 if (r.ok)
199 std::fprintf(stderr, "[rt] mods: archetype lookup at RVA 0x%X hooked (argument %d): %zu direct "
200 "call(s) redirected\n", archetype_lookup_rva, archetype_lookup_arg, r.redirected);
201 else
202 std::fprintf(stderr, "[rt] mods: archetype lookup at RVA 0x%X not hooked: %s\n", archetype_lookup_rva,
203 r.error);
204 }
132 if (run) rt::process::set_command_line(image.filename().wstring(), game_args); 205 if (run) rt::process::set_command_line(image.filename().wstring(), game_args);
133 if (init || run) { 206 if (init || run) {
134 loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs 207 loader.initialise(); // TLS callbacks, then DllMain(DLL_PROCESS_ATTACH) for DLLs