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.cpp49
1 files changed, 47 insertions, 2 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 6357dd0b0..8e3767740 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -161,10 +161,43 @@ extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_t
161 reserve_tls_index0_at_start; 161 reserve_tls_index0_at_start;
162#pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0") 162#pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0")
163 163
164// Select the settings used to display the offline first-run UI. Keep explicit caller settings.
165bool configure_first_run_ui(const std::filesystem::path& package, std::filesystem::path& overlay) {
166 // A variable that is set but empty is not a setting: Windows cannot hold one, but a caller on Wine can pass one
167 // (NAME= in the environment), and asking for the size alone reports it as present there.
168 const auto set_default = [](const wchar_t* name, const wchar_t* value) {
169 wchar_t first[2]{};
170 return GetEnvironmentVariableW(name, first, 2) != 0 || _wputenv_s(name, value) == 0;
171 };
172 const struct { const wchar_t* name; const wchar_t* value; } settings[] = {
173 {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"},
174 {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"},
175 {L"LEONIDA_GPU_LABELS", L"1"}, {L"LEONIDA_FRAME_PACING", L"1"},
176 {L"LEONIDA_KEYBOARD_PAD", L"always"},
177 };
178 for (const auto& setting : settings) {
179 if (!set_default(setting.name, setting.value)) return false;
180 }
181 std::wstring executable(32768, L'\0');
182 const DWORD length = GetModuleFileNameW(nullptr, executable.data(), static_cast<DWORD>(executable.size()));
183 if (!length || length >= executable.size()) return false;
184 executable.resize(length);
185 const auto state = std::filesystem::path(executable).parent_path() / L"first-run-ui";
186 if (overlay.empty()) overlay = state / L"overlay";
187 if (!GetEnvironmentVariableW(L"LEONIDA_XMEM_BACKING_DIR", nullptr, 0)) {
188 std::error_code error;
189 const auto backing = state / L"xmem";
190 std::filesystem::create_directories(backing, error);
191 if (error || !set_default(L"LEONIDA_XMEM_BACKING_DIR", backing.c_str())) return false;
192 }
193 return SetCurrentDirectoryW(package.c_str()) != FALSE;
194}
195
164int usage() { 196int usage() {
165 std::fprintf(stderr, 197 std::fprintf(stderr,
166 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n" 198 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n"
167 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n" 199 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n"
200 " gta6rt --first-run-ui <exe> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n"
168 " 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"); 201 " 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");
169 return 2; 202 return 2;
170} 203}
@@ -187,7 +220,7 @@ int wmain(int argc, wchar_t** argv) {
187 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash 220 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
188 int archetype_lookup_arg = 0; 221 int archetype_lookup_arg = 0;
189 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context 222 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context
190 bool dry_run = false, init = false, run = false, trace_d3d12 = false; 223 bool dry_run = false, init = false, run = false, trace_d3d12 = false, first_run_ui = false;
191 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) 224 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
192 std::wstring trace_file; 225 std::wstring trace_file;
193 for (int i = 1; i < argc; ++i) { 226 for (int i = 1; i < argc; ++i) {
@@ -198,6 +231,7 @@ int wmain(int argc, wchar_t** argv) {
198 } 231 }
199 if (a == L"--dry-run") dry_run = true; 232 if (a == L"--dry-run") dry_run = true;
200 else if (a == L"--init") init = true; 233 else if (a == L"--init") init = true;
234 else if (a == L"--first-run-ui") first_run_ui = true;
201 else if (a == L"--run") run = true; 235 else if (a == L"--run") run = true;
202 else if (a == L"--trace-d3d12") trace_d3d12 = true; 236 else if (a == L"--trace-d3d12") trace_d3d12 = true;
203 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; 237 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i];
@@ -224,7 +258,8 @@ int wmain(int argc, wchar_t** argv) {
224 } 258 }
225 if (image.empty()) return usage(); 259 if (image.empty()) return usage();
226 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. 260 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point.
227 if (dry_run + init + run != 1) return usage(); 261 if (dry_run + init + run + first_run_ui != 1) return usage();
262 run = run || first_run_ui;
228 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h). 263 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h).
229 if ((init || run) && !rt::host_cpu::check_skipped()) 264 if ((init || run) && !rt::host_cpu::check_skipped())
230 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) { 265 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) {
@@ -239,6 +274,11 @@ int wmain(int argc, wchar_t** argv) {
239 std::filesystem::path a = std::filesystem::absolute(p, ec); 274 std::filesystem::path a = std::filesystem::absolute(p, ec);
240 return ec ? p : a; 275 return ec ? p : a;
241 }; 276 };
277 if (first_run_ui) {
278 image = absolute_or_same(image);
279 if (!mods_dir.empty()) mods_dir = absolute_or_same(mods_dir);
280 if (!trace_file.empty()) trace_file = absolute_or_same(trace_file).wstring();
281 }
242 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); 282 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir);
243 // An unattended run can select a fresh write root even when its launcher supplies --overlay. 283 // An unattended run can select a fresh write root even when its launcher supplies --overlay.
244 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) { 284 if (const DWORD size = GetEnvironmentVariableW(L"LEONIDA_WRITE_DIR", nullptr, 0)) {
@@ -264,6 +304,10 @@ int wmain(int argc, wchar_t** argv) {
264 if (!rt::base_reservation::inherited()) 304 if (!rt::base_reservation::inherited())
265 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range); 305 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range);
266 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. 306 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package.
307 if (first_run_ui && !configure_first_run_ui(game_dir, overlay_dir)) {
308 std::fprintf(stderr, "cannot initialize the first-run UI paths or graphics settings\n");
309 return 1;
310 }
267 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; 311 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay";
268 rt::fs::configure(game_dir, overlay_dir); 312 rt::fs::configure(game_dir, overlay_dir);
269#ifdef LEONIDA_VULKAN_DEVICE 313#ifdef LEONIDA_VULKAN_DEVICE
@@ -312,6 +356,7 @@ int wmain(int argc, wchar_t** argv) {
312 // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h). 356 // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h).
313 leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases); 357 leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases);
314 leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range); 358 leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range);
359 rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data);
315 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { 360 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) {
316 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" 361 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n"
317 : "error: cannot create the trace file\n"); 362 : "error: cannot create the trace file\n");