Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
shims/xapu_queue.cpp
shims/xapu_chad.cpp
shims/dstorage.cpp
shims/win32_env.cpp
)
# These sources use __try to handle access violations. MSVC always catches them; clang-cl needs asynchronous
# exceptions (as for tiny_dll below). A source file property also applies to dstorage_test.
Expand Down Expand Up @@ -635,6 +636,17 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
target_compile_options(console_registry_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(console_registry_test PRIVATE /OPT:NOREF)

# Win32 localization, process priority boost, and named pipe shims.
add_executable(win32_env_test
test/win32_env_test.cpp
shims/win32_env.cpp
shims/registry.cpp
)
target_include_directories(win32_env_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
target_compile_definitions(win32_env_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(win32_env_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(win32_env_test PRIVATE /OPT:NOREF)

# Synthetic tests for the atomic API reference lifecycle.
add_executable(api_ref_state_test test/api_ref_state_test.cpp)
target_include_directories(api_ref_state_test PRIVATE
Expand Down Expand Up @@ -1812,6 +1824,11 @@ if(BUILD_TESTING)
if(WIN32 AND TARGET console_registry_test)
add_test(NAME console_registry COMMAND console_registry_test)
endif()
if(WIN32 AND TARGET win32_env_test)
add_test(NAME win32_env COMMAND win32_env_test)
add_test(NAME win32_env_override COMMAND win32_env_test --expect-override)
set_tests_properties(win32_env_override PROPERTIES ENVIRONMENT "LEONIDA_GEO=jp;LEONIDA_LOCALE=en-GB")
endif()
if(WIN32 AND TARGET api_ref_state_test)
add_test(NAME api_ref_state COMMAND api_ref_state_test)
set_tests_properties(api_ref_state PROPERTIES TIMEOUT 30)
Expand Down
9 changes: 9 additions & 0 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ document names.
| `LEONIDA_TRACE_XGAMESAVE` | unset | *Diagnostic.* `1` logs one `[rt] xgamesave:` line to stderr per `XGameSave*` and `XGameSaveFiles*` call (`shims/xgameruntime_gamesave.cpp`): the call, its handles, container and blob names, sizes and HRESULT, after `t=<ms since the process started>ms tid=<thread>`. An async call also logs when its job is scheduled, when its work runs (with how long it waited on the queue), when it completes and when the title collects it with the `...Result` call. Blob contents are never logged. Read once per process; unset, a call costs one load. |
| `LEONIDA_STACKS` | unset | Comma-separated whole seconds after the title's entry point is called (e.g. `90,180`). At each time `--run` suspends every other thread in turn just long enough to copy its context and stack, then unwinds the copy (title frames through the unwind tables the loader registers) and prints to stderr `[rt] stacks t=<s> thread <tid> (<thread description, or start address>):` and up to 40 `#i module+0xRVA` frames, innermost first; a chain identical to an earlier thread's prints `same as thread <tid>`. |

### Win32 localization overrides (`shims/win32_env.cpp`)

Read once per process by `shims/win32_env.cpp`; unset or invalid values fall back to the host (`US` / `en-US` if the host call fails).

| variable | default | what it changes |
|---|---|---|
| `LEONIDA_GEO` | unset | Overrides `GetUserDefaultGeoName`: accepts 2 ASCII letters (uppercased, e.g. `jp` -> `JP`) or 3 ASCII digits (`001`..`999`). |
| `LEONIDA_LOCALE` | unset | Overrides `GetUserDefaultLocaleName` and `LocaleNameToLCID(nullptr, ...)`: accepts an ASCII tag up to `LOCALE_NAME_MAX_LENGTH - 1` characters (`A-Z`, `a-z`, `0-9`, `-`, `_`, e.g. `en-GB`). |

### Switches of the D3D12 layer for a retail boot

Read once per process, by the Vulkan D3D12 layer (`shims/d3d12_vulkan_device.cpp`) unless the row names another file.
Expand Down
261 changes: 261 additions & 0 deletions runtime/shims/win32_env.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
// Win32 localization, process priority-boost, and named-pipe shims for mapped executables.
//
// game_scarlett_master.exe imports GetUserDefaultGeoName (api-ms-win-core-localization-l1-2-3.dll),
// GetUserDefaultLocaleName / LocaleNameToLCID (api-ms-win-core-localization-l1-2-0.dll), SetProcessPriorityBoost
// (api-ms-win-core-processthreads-l1-1-2.dll), and WaitNamedPipeW / SetNamedPipeHandleState
// (api-ms-win-core-namedpipe-l1-1-0.dll). Bound straight to the host:
// - SetProcessPriorityBoost(GetCurrentProcess(), ...) mutates the host gta6rt scheduler priority-boost policy;
// - GetUserDefaultGeoName (Win10 1709+) can be absent on older hosts or Wine;
// - GetUserDefaultLocaleName and LocaleNameToLCID(nullptr, ...) reflect arbitrary host locales with no override;
// - WaitNamedPipeW(nullptr, ...) faults on Windows 11, while WaitNamedPipeA(nullptr, ...) fails with
// ERROR_BAD_PATHNAME and SetNamedPipeHandleState(nullptr, ...) fails with ERROR_INVALID_HANDLE.
#include <windows.h>

#include <atomic>
#include <cctype>
#include <cstring>
#include <cwchar>
#include <string>

#include "shims.h"

namespace rt::win32_env {
namespace {

using GetUserDefaultGeoNameFn = int(WINAPI*)(LPWSTR, int);

INIT_ONCE g_init_once = INIT_ONCE_STATIC_INIT;
GetUserDefaultGeoNameFn g_host_get_geo_name = nullptr;
bool g_has_geo_override = false;
wchar_t g_geo_override[8]{};
bool g_has_locale_override = false;
wchar_t g_locale_override[LOCALE_NAME_MAX_LENGTH]{};
std::atomic<bool> g_guest_priority_boost_disabled{false};

bool is_valid_geo_code(const char* text, size_t len) {
if (len == 2) {
return std::isalpha(static_cast<unsigned char>(text[0])) &&
std::isalpha(static_cast<unsigned char>(text[1]));
}
if (len == 3) {
return std::isdigit(static_cast<unsigned char>(text[0])) &&
std::isdigit(static_cast<unsigned char>(text[1])) &&
std::isdigit(static_cast<unsigned char>(text[2]));
}
return false;
}

bool is_valid_locale_tag(const char* text, size_t len) {
if (len == 0 || len >= LOCALE_NAME_MAX_LENGTH) return false;
for (size_t i = 0; i < len; ++i) {
const unsigned char c = static_cast<unsigned char>(text[i]);
if (!std::isalnum(c) && c != '-' && c != '_') return false;
}
return true;
}

BOOL CALLBACK init_env_once(PINIT_ONCE, PVOID, PVOID*) {
if (const HMODULE kb = GetModuleHandleW(L"kernelbase.dll")) {
g_host_get_geo_name = reinterpret_cast<GetUserDefaultGeoNameFn>(GetProcAddress(kb, "GetUserDefaultGeoName"));
}
if (!g_host_get_geo_name) {
if (const HMODULE k32 = GetModuleHandleW(L"kernel32.dll")) {
g_host_get_geo_name =
reinterpret_cast<GetUserDefaultGeoNameFn>(GetProcAddress(k32, "GetUserDefaultGeoName"));
}
}

char geo_buf[16]{};
const DWORD geo_len = GetEnvironmentVariableA("LEONIDA_GEO", geo_buf, static_cast<DWORD>(sizeof(geo_buf)));
if (geo_len > 0 && geo_len < sizeof(geo_buf) && is_valid_geo_code(geo_buf, geo_len)) {
for (DWORD i = 0; i < geo_len; ++i) {
const char c = geo_buf[i];
g_geo_override[i] = static_cast<wchar_t>(
(c >= 'a' && c <= 'z') ? (c - 32) : c);
}
g_geo_override[geo_len] = L'\0';
g_has_geo_override = true;
}

char loc_buf[LOCALE_NAME_MAX_LENGTH]{};
const DWORD loc_len = GetEnvironmentVariableA("LEONIDA_LOCALE", loc_buf, static_cast<DWORD>(sizeof(loc_buf)));
if (loc_len > 0 && loc_len < sizeof(loc_buf) && is_valid_locale_tag(loc_buf, loc_len)) {
for (DWORD i = 0; i < loc_len; ++i) g_locale_override[i] = static_cast<wchar_t>(loc_buf[i]);
g_locale_override[loc_len] = L'\0';
g_has_locale_override = true;
}

BOOL host_disabled = FALSE;
if (::GetProcessPriorityBoost(GetCurrentProcess(), &host_disabled)) {
g_guest_priority_boost_disabled.store(host_disabled != FALSE, std::memory_order_relaxed);
}
return TRUE;
}

void ensure_initialized() { InitOnceExecuteOnce(&g_init_once, init_env_once, nullptr, nullptr); }

int write_wide_zstring(const wchar_t* src, LPWSTR dst, int cap, bool allow_zero_cap_query) {
if (cap < 0 || (!dst && cap > 0)) {
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
const int needed = static_cast<int>(std::wcslen(src) + 1);
if (!dst) {
if (allow_zero_cap_query) return needed;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
if (cap < needed) {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
std::wmemcpy(dst, src, static_cast<size_t>(needed));
return needed;
}

int WINAPI get_user_default_geo_name(LPWSTR geo_name, int geo_name_count) {
ensure_initialized();
if (geo_name_count < 0 || (!geo_name && geo_name_count > 0)) {
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (g_has_geo_override) {
return write_wide_zstring(g_geo_override, geo_name, geo_name_count, /*allow_zero_cap_query=*/true);
}
if (g_host_get_geo_name) {
const int r = g_host_get_geo_name(geo_name, geo_name_count);
if (r > 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) return r;
}
return write_wide_zstring(L"US", geo_name, geo_name_count, /*allow_zero_cap_query=*/true);
}

int WINAPI get_user_default_locale_name(LPWSTR locale_name, int locale_name_count) {
ensure_initialized();
if (locale_name_count < 0 || (!locale_name && locale_name_count > 0)) {
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (g_has_locale_override) {
return write_wide_zstring(g_locale_override, locale_name, locale_name_count, /*allow_zero_cap_query=*/false);
}
if (!locale_name) {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
const int r = ::GetUserDefaultLocaleName(locale_name, locale_name_count);
if (r > 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER || GetLastError() == ERROR_INVALID_PARAMETER) return r;
return write_wide_zstring(L"en-US", locale_name, locale_name_count, /*allow_zero_cap_query=*/false);
}

LCID WINAPI locale_name_to_lcid(LPCWSTR name, DWORD flags) {
ensure_initialized();
if (!name && g_has_locale_override) {
return ::LocaleNameToLCID(g_locale_override, flags);
}
return ::LocaleNameToLCID(name, flags);
}

bool is_current_process_handle(HANDLE process) {
if (!process) return false;
if (process == GetCurrentProcess()) return true;
return GetProcessId(process) == GetCurrentProcessId();
}

BOOL WINAPI set_process_priority_boost(HANDLE process, BOOL disable_priority_boost) {
ensure_initialized();
if (!process) {
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
if (is_current_process_handle(process)) {
g_guest_priority_boost_disabled.store(disable_priority_boost != FALSE, std::memory_order_relaxed);
return TRUE;
}
return ::SetProcessPriorityBoost(process, disable_priority_boost);
}

BOOL WINAPI get_process_priority_boost(HANDLE process, PBOOL disable_priority_boost) {
ensure_initialized();
if (!process) {
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
if (!disable_priority_boost) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (is_current_process_handle(process)) {
*disable_priority_boost = g_guest_priority_boost_disabled.load(std::memory_order_relaxed) ? TRUE : FALSE;
return TRUE;
}
return ::GetProcessPriorityBoost(process, disable_priority_boost);
}

BOOL WINAPI wait_named_pipe_w(LPCWSTR named_pipe_name, DWORD timeout) {
if (!named_pipe_name) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
return ::WaitNamedPipeW(named_pipe_name, timeout);
}

BOOL WINAPI wait_named_pipe_a(LPCSTR named_pipe_name, DWORD timeout) {
if (!named_pipe_name) {
SetLastError(ERROR_BAD_PATHNAME);
return FALSE;
}
return ::WaitNamedPipeA(named_pipe_name, timeout);
}

BOOL WINAPI set_named_pipe_handle_state(HANDLE named_pipe, LPDWORD mode, LPDWORD max_collection_count,
LPDWORD collect_data_timeout) {
if (!named_pipe || named_pipe == INVALID_HANDLE_VALUE) {
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
return ::SetNamedPipeHandleState(named_pipe, mode, max_collection_count, collect_data_timeout);
}

} // namespace
} // namespace rt::win32_env

namespace {

using namespace rt::win32_env;

#define LOC_GEO_API(dll) {dll, "GetUserDefaultGeoName", reinterpret_cast<void*>(&get_user_default_geo_name)}

#define LOC_LOCALE_API(dll) \
{dll, "GetUserDefaultLocaleName", reinterpret_cast<void*>(&get_user_default_locale_name)}, \
{dll, "LocaleNameToLCID", reinterpret_cast<void*>(&locale_name_to_lcid)}

#define PRIORITY_BOOST_API(dll) \
{dll, "SetProcessPriorityBoost", reinterpret_cast<void*>(&set_process_priority_boost)}, \
{dll, "GetProcessPriorityBoost", reinterpret_cast<void*>(&get_process_priority_boost)}

#define NAMED_PIPE_API(dll) \
{dll, "WaitNamedPipeW", reinterpret_cast<void*>(&wait_named_pipe_w)}, \
{dll, "WaitNamedPipeA", reinterpret_cast<void*>(&wait_named_pipe_a)}, \
{dll, "SetNamedPipeHandleState", reinterpret_cast<void*>(&set_named_pipe_handle_state)}

} // namespace

SHIM_TABLE(win32_env,
LOC_GEO_API("kernel32.dll"),
LOC_GEO_API("kernelbase.dll"),
LOC_GEO_API("api-ms-win-core-localization-l1-2-3.dll"),
LOC_LOCALE_API("kernel32.dll"),
LOC_LOCALE_API("kernelbase.dll"),
LOC_LOCALE_API("api-ms-win-core-localization-l1-2-0.dll"),
LOC_LOCALE_API("api-ms-win-core-localization-l1-2-1.dll"),
LOC_LOCALE_API("api-ms-win-core-localization-l1-2-2.dll"),
LOC_LOCALE_API("api-ms-win-core-localization-l1-2-3.dll"),
PRIORITY_BOOST_API("kernel32.dll"),
PRIORITY_BOOST_API("kernelbase.dll"),
PRIORITY_BOOST_API("api-ms-win-core-processthreads-l1-1-0.dll"),
PRIORITY_BOOST_API("api-ms-win-core-processthreads-l1-1-1.dll"),
PRIORITY_BOOST_API("api-ms-win-core-processthreads-l1-1-2.dll"),
NAMED_PIPE_API("kernel32.dll"),
NAMED_PIPE_API("kernelbase.dll"),
NAMED_PIPE_API("api-ms-win-core-namedpipe-l1-1-0.dll"),
);
Loading
Loading