Runtime: gta6rt --run, and a stand-in game executable run through the loader

gta6rt --run <exe> [-- 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.
This commit is contained in:
IAmNotSiob
2026-11-21 23:50:12 +01:00
committed by root
parent 8bd0087d2d
commit 0ca9bb8bc0
8 changed files with 397 additions and 12 deletions

View File

@ -6,13 +6,21 @@
//
// gta6rt --init <image> [--game-dir DIR]
// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run.
//
// gta6rt --run <exe> [--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 <exe name> 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 <windows.h>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
#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 <image> [--game-dir DIR]\n"
" gta6rt --init <image> [--game-dir DIR]\n");
" gta6rt --init <image> [--game-dir DIR]\n"
" gta6rt --run <exe> [--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<std::string> 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<UINT>(code));
} catch (const std::exception& e) {
std::fprintf(stderr, "error: %s\n", e.what());
return 1;