From 0ca9bb8bc034ac2f5f322ce5b428af964bffca4e Mon Sep 17 00:00:00 2001 From: IAmNotSiob <273832158+IAmNotSiob@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:50:12 +0100 Subject: Runtime: gta6rt --run, and a stand-in game executable run through the loader gta6rt --run [-- args] maps and binds the executable, initialises its modules and calls its entry point on a game thread. shims/process.cpp answers the process-level imports through the game's own import table: argv/argc and GetCommandLine are the game's (UCRT api-set/ucrtbase and msvcrt forms), and exit/_exit/quick_exit/ExitProcess/abort end the run with that exit code instead of the host. gta6rt then exits with the game's code directly, so the game's atexit handlers don't run during host shutdown. tiny_game.exe (CTest run_tiny_game, run_tiny_game_exit_code) does what a title does in its first second: arguments, a static initializer, a 4 MiB allocation, a file round trip, a thread, and xgameruntime.dll imports, which the shim registry answers instead of the link-time stub. thread_local and a C++ exception are opt-in (LEONIDA_RUN_TLS / LEONIDA_RUN_EH) until #93 and #176. --- runtime/src/main.cpp | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'runtime/src/main.cpp') diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 4477c4a69..d79ff97dc 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -6,13 +6,21 @@ // // gta6rt --init [--game-dir DIR] // The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run. +// +// gta6rt --run [--game-dir DIR] [-- game arguments...] +// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and +// command line are plus the arguments after "--"; exit/ExitProcess from the game end the run +// (shims/process.cpp). gta6rt prints the binding report first and returns the game's exit code. #include #include #include #include +#include #include "pe_loader.h" +#include "../shims/process.h" +#include "../shims/shims.h" namespace { @@ -28,7 +36,8 @@ const char* binding_name(rt::Binding b) { int usage() { std::fprintf(stderr, "usage: gta6rt --dry-run [--game-dir DIR]\n" - " gta6rt --init [--game-dir DIR]\n"); + " gta6rt --init [--game-dir DIR]\n" + " gta6rt --run [--game-dir DIR] [-- game arguments...]\n"); return 2; } @@ -36,28 +45,29 @@ int usage() { int wmain(int argc, wchar_t** argv) { std::filesystem::path image, game_dir; - bool dry_run = false, init = false; + bool dry_run = false, init = false, run = false; + std::vector game_args; for (int i = 1; i < argc; ++i) { std::wstring a = argv[i]; + if (a == L"--") { + for (++i; i < argc; ++i) game_args.push_back(std::filesystem::path(argv[i]).string()); + break; + } if (a == L"--dry-run") dry_run = true; else if (a == L"--init") init = true; + else if (a == L"--run") run = true; else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; else if (image.empty()) image = a; else return usage(); } if (image.empty()) return usage(); - if (!dry_run && !init) { - std::fprintf(stderr, - "Only --dry-run and --init exist so far: running game code needs the shims listed by " - "--dry-run first.\n"); - return 2; - } + if (!dry_run && !init && !run) return usage(); if (game_dir.empty()) game_dir = image.parent_path(); try { rt::Loader loader(game_dir); - loader.load(image); - if (init) { + rt::Module& main_module = loader.load(image); + if (init || run) { loader.initialise(); std::printf("%zu module(s) initialised\n", loader.modules().size()); } @@ -76,7 +86,23 @@ int wmain(int argc, wchar_t** argv) { } } std::printf("%zu module(s) mapped, %d unimplemented import(s)\n", loader.modules().size(), unimplemented); - return 0; + if (!run) return 0; + if (!main_module.entry_rva) { + std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str()); + return 1; + } + rt::process::set_command_line(image.filename().string(), game_args); + std::printf("running %s\n", main_module.name.c_str()); + std::fflush(stdout); + const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); + std::fflush(stdout); + std::printf("game exited with code %d\n", code); + rt::shims::report_called_unimplemented(); + std::fflush(stdout); + std::fflush(stderr); + // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its + // handlers would run against a game that has stopped) and whose last word decides the exit status. + ExitProcess(static_cast(code)); } catch (const std::exception& e) { std::fprintf(stderr, "error: %s\n", e.what()); return 1; -- cgit v1.2.3