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:
authorIAmNotSiob <273832158+IAmNotSiob@users.noreply.github.com>2026-11-21 23:50:12 +0100
committerroot <root@m29531.contaboserver.net>2026-11-21 23:50:31 +0100
commit0ca9bb8bc034ac2f5f322ce5b428af964bffca4e (patch)
treedf89c76031bd3b2dbdc63a2e8172e07d659fb4d1 /runtime/src/main.cpp
parent8bd0087d2d0e45fc18fa78fb40b70ecfc6648854 (diff)
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.
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp48
1 files changed, 37 insertions, 11 deletions
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 @@
6// 6//
7// gta6rt --init <image> [--game-dir DIR] 7// gta6rt --init <image> [--game-dir DIR]
8// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run. 8// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run.
9//
10// gta6rt --run <exe> [--game-dir DIR] [-- game arguments...]
11// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and
12// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run
13// (shims/process.cpp). gta6rt prints the binding report first and returns the game's exit code.
9#include <windows.h> 14#include <windows.h>
10 15
11#include <cstdio> 16#include <cstdio>
12#include <map> 17#include <map>
13#include <string> 18#include <string>
19#include <vector>
14 20
15#include "pe_loader.h" 21#include "pe_loader.h"
22#include "../shims/process.h"
23#include "../shims/shims.h"
16 24
17namespace { 25namespace {
18 26
@@ -28,7 +36,8 @@ const char* binding_name(rt::Binding b) {
28int usage() { 36int usage() {
29 std::fprintf(stderr, 37 std::fprintf(stderr,
30 "usage: gta6rt --dry-run <image> [--game-dir DIR]\n" 38 "usage: gta6rt --dry-run <image> [--game-dir DIR]\n"
31 " gta6rt --init <image> [--game-dir DIR]\n"); 39 " gta6rt --init <image> [--game-dir DIR]\n"
40 " gta6rt --run <exe> [--game-dir DIR] [-- game arguments...]\n");
32 return 2; 41 return 2;
33} 42}
34 43
@@ -36,28 +45,29 @@ int usage() {
36 45
37int wmain(int argc, wchar_t** argv) { 46int wmain(int argc, wchar_t** argv) {
38 std::filesystem::path image, game_dir; 47 std::filesystem::path image, game_dir;
39 bool dry_run = false, init = false; 48 bool dry_run = false, init = false, run = false;
49 std::vector<std::string> game_args;
40 for (int i = 1; i < argc; ++i) { 50 for (int i = 1; i < argc; ++i) {
41 std::wstring a = argv[i]; 51 std::wstring a = argv[i];
52 if (a == L"--") {
53 for (++i; i < argc; ++i) game_args.push_back(std::filesystem::path(argv[i]).string());
54 break;
55 }
42 if (a == L"--dry-run") dry_run = true; 56 if (a == L"--dry-run") dry_run = true;
43 else if (a == L"--init") init = true; 57 else if (a == L"--init") init = true;
58 else if (a == L"--run") run = true;
44 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; 59 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i];
45 else if (image.empty()) image = a; 60 else if (image.empty()) image = a;
46 else return usage(); 61 else return usage();
47 } 62 }
48 if (image.empty()) return usage(); 63 if (image.empty()) return usage();
49 if (!dry_run && !init) { 64 if (!dry_run && !init && !run) return usage();
50 std::fprintf(stderr,
51 "Only --dry-run and --init exist so far: running game code needs the shims listed by "
52 "--dry-run first.\n");
53 return 2;
54 }
55 if (game_dir.empty()) game_dir = image.parent_path(); 65 if (game_dir.empty()) game_dir = image.parent_path();
56 66
57 try { 67 try {
58 rt::Loader loader(game_dir); 68 rt::Loader loader(game_dir);
59 loader.load(image); 69 rt::Module& main_module = loader.load(image);
60 if (init) { 70 if (init || run) {
61 loader.initialise(); 71 loader.initialise();
62 std::printf("%zu module(s) initialised\n", loader.modules().size()); 72 std::printf("%zu module(s) initialised\n", loader.modules().size());
63 } 73 }
@@ -76,7 +86,23 @@ int wmain(int argc, wchar_t** argv) {
76 } 86 }
77 } 87 }
78 std::printf("%zu module(s) mapped, %d unimplemented import(s)\n", loader.modules().size(), unimplemented); 88 std::printf("%zu module(s) mapped, %d unimplemented import(s)\n", loader.modules().size(), unimplemented);
79 return 0; 89 if (!run) return 0;
90 if (!main_module.entry_rva) {
91 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str());
92 return 1;
93 }
94 rt::process::set_command_line(image.filename().string(), game_args);
95 std::printf("running %s\n", main_module.name.c_str());
96 std::fflush(stdout);
97 const int code = rt::process::run_entry(main_module.base + main_module.entry_rva);
98 std::fflush(stdout);
99 std::printf("game exited with code %d\n", code);
100 rt::shims::report_called_unimplemented();
101 std::fflush(stdout);
102 std::fflush(stderr);
103 // Not `return`: a normal host exit runs the CRT's atexit table, which the game shares with gta6rt (its
104 // handlers would run against a game that has stopped) and whose last word decides the exit status.
105 ExitProcess(static_cast<UINT>(code));
80 } catch (const std::exception& e) { 106 } catch (const std::exception& e) {
81 std::fprintf(stderr, "error: %s\n", e.what()); 107 std::fprintf(stderr, "error: %s\n", e.what());
82 return 1; 108 return 1;