diff options
| author | edwin042331-hue <edwin042331@gmail.com> | 2026-11-22 14:39:18 -0400 |
|---|---|---|
| committer | edwin042331-hue <edwin042331@gmail.com> | 2026-11-22 14:39:18 -0400 |
| commit | 355e67cbc45b163f96ee7b0091174ebde5c64e45 (patch) | |
| tree | 0ef3196a49993b454965df41ce57812811b76f4a /runtime/shims/process.cpp | |
| parent | 542d02800492c58003c37a755389528321f2aaa8 (diff) | |
| parent | ec8ec4c9a46c6623f1a9c125afd3566da97f1e0c (diff) | |
Merge remote-tracking branch 'origin/main' into perf/wave64-emulate-dead-scc-exec-fold
# Conflicts:
# make_game.py
Diffstat (limited to 'runtime/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index d3f76a684..9da50e4e9 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp | |||
| @@ -38,8 +38,6 @@ std::wstring g_wcmdline; | |||
| 38 | int g_argc = 0; | 38 | int g_argc = 0; |
| 39 | char** g_argv_ptr = nullptr; | 39 | char** g_argv_ptr = nullptr; |
| 40 | wchar_t** g_wargv_ptr = nullptr; | 40 | wchar_t** g_wargv_ptr = nullptr; |
| 41 | char** g_env = nullptr; | ||
| 42 | wchar_t** g_wenv = nullptr; | ||
| 43 | 41 | ||
| 44 | std::atomic<bool> g_running{false}; | 42 | std::atomic<bool> g_running{false}; |
| 45 | std::atomic<bool> g_exited{false}; | 43 | std::atomic<bool> g_exited{false}; |
| @@ -130,17 +128,40 @@ C* after_program_name(C* p) { | |||
| 130 | char* __cdecl game_get_narrow_winmain_command_line() { return after_program_name(g_cmdline.data()); } | 128 | char* __cdecl game_get_narrow_winmain_command_line() { return after_program_name(g_cmdline.data()); } |
| 131 | wchar_t* __cdecl game_get_wide_winmain_command_line() { return after_program_name(g_wcmdline.data()); } | 129 | wchar_t* __cdecl game_get_wide_winmain_command_line() { return after_program_name(g_wcmdline.data()); } |
| 132 | 130 | ||
| 131 | // The host CRT's environment tables, read when the game asks (RT-136). _environ and _wenviron are not stable: the host | ||
| 132 | // CRT builds the wide table only when a wide environment function first runs (a host started through main() has | ||
| 133 | // none until then), and _putenv/_wputenv reallocate both, so a pointer taken in set_command_line would be null or | ||
| 134 | // dangling by the time a wide-character startup calls __wgetmainargs. A lookup of a name no one sets makes the CRT | ||
| 135 | // create the table if it does not exist yet (from the other table or the OS block) without changing it. | ||
| 136 | // Like the msvcrt.dll original, the returned table is the CRT's own and is only valid until the next _putenv: a CRT | ||
| 137 | // startup reads it once, before the game has other threads, and a host that edits its environment concurrently with | ||
| 138 | // a game's startup is not covered. | ||
| 139 | char** current_environment() { | ||
| 140 | if (!_environ) { | ||
| 141 | size_t size = 0; | ||
| 142 | (void)getenv_s(&size, nullptr, 0, "LEONIDA_RT_ENV_INIT"); | ||
| 143 | } | ||
| 144 | return _environ; | ||
| 145 | } | ||
| 146 | wchar_t** current_wide_environment() { | ||
| 147 | if (!_wenviron) { | ||
| 148 | size_t size = 0; | ||
| 149 | (void)_wgetenv_s(&size, nullptr, 0, L"LEONIDA_RT_ENV_INIT"); | ||
| 150 | } | ||
| 151 | return _wenviron; | ||
| 152 | } | ||
| 153 | |||
| 133 | // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) | 154 | // msvcrt.dll: int __getmainargs(int* argc, char*** argv, char*** env, int expand, void* startinfo) |
| 134 | int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { | 155 | int __cdecl game_getmainargs(int* argc, char*** argv, char*** env, int, void*) { |
| 135 | *argc = g_argc; | 156 | *argc = g_argc; |
| 136 | *argv = g_argv_ptr; | 157 | *argv = g_argv_ptr; |
| 137 | if (env) *env = g_env; | 158 | if (env) *env = current_environment(); |
| 138 | return 0; | 159 | return 0; |
| 139 | } | 160 | } |
| 140 | int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) { | 161 | int __cdecl game_wgetmainargs(int* argc, wchar_t*** argv, wchar_t*** env, int, void*) { |
| 141 | *argc = g_argc; | 162 | *argc = g_argc; |
| 142 | *argv = g_wargv_ptr; | 163 | *argv = g_wargv_ptr; |
| 143 | if (env) *env = g_wenv; | 164 | if (env) *env = current_wide_environment(); |
| 144 | return 0; | 165 | return 0; |
| 145 | } | 166 | } |
| 146 | 167 | ||
| @@ -179,8 +200,6 @@ void set_command_line(const std::wstring& program, const std::vector<std::wstrin | |||
| 179 | g_argc = static_cast<int>(g_args.size()); | 200 | g_argc = static_cast<int>(g_args.size()); |
| 180 | g_argv_ptr = g_argv.data(); | 201 | g_argv_ptr = g_argv.data(); |
| 181 | g_wargv_ptr = g_wargv.data(); | 202 | g_wargv_ptr = g_wargv.data(); |
| 182 | g_env = _environ; | ||
| 183 | g_wenv = _wenviron; | ||
| 184 | } | 203 | } |
| 185 | 204 | ||
| 186 | int run_entry(void* entry, size_t stack_bytes) { | 205 | int run_entry(void* entry, size_t stack_bytes) { |