upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/console_registry.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 20:26:00 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 20:26:00 -0500
commit154e4ec034902deffa4b89b2cbca9a949f38baec (patch)
tree1b2ef5656de42c172291bc6009efa4c510650815 /runtime/shims/console_registry.cpp
parenteb6a08cd359d98b5777e70d54d0ae8a75879495a (diff)
parent8a242efebf823aeb23c72d17c3d666daf0bbd3e2 (diff)
Merge the #984 result (main + RT-PROTON-STORY-LOAD) into #939
The base is 8a242efeb, the merge of main and #984. 68 files conflicted; they were resolved in seven groups by one rule: the same change in two forms keeps the base's form, everything only #939 has is kept, and where the two solve one problem in two ways the base's is the frame and #939's plugs into it. A, the Xbox proxy (d3d12_xs_device.cpp/.h, d3d12_xs.cpp, d3d12_xs_pso_stream.cpp/.h, d3d12_xs_alloc_size.h, d3d12_xs_desc_clean.h, device_proxy_test.cpp, queue_proxy_test.cpp): the base's files with #939's additions ported in. Device slot 57 asks #939's NativeGraphicsFactory first and falls back to the base's proxy_create_graphics_pipeline_from_native_code on E_NOTIMPL. Kept from #939: the providers (texture lease, raw texture view, resource by address, constant view, descriptor mapping, depth-stencil UAV, texture pages), the title-backed descriptor mirror, slot 13's feature answers, GPU labels on list slots 71/72. Dropped: #939's duplicates of the boot ladder routes, its fallback window, its slot 133 stand-in. A2 (xs_proxy_mock_test.cpp): the base's test with #939's slot 70 raw-buffer case; presentation cases follow #939's rule that a device which ever had a handler has no queue fallback. B1, the Vulkan device (d3d12_vulkan_device.cpp, d3d12_com.cpp/.h, shader_library.h, queue_families.h, pipeline_cache_file.h, startup_trace.h): one lazy-pipeline mechanism, the base's; #939's pending_base_compile and first_use_backend_pipelines dropped, prepare_on_bind and cold_graphics_compile kept. create_texture keeps the base's signature; #939's deferred backing is folded into the base's placed-memory binder. #939's quad lists as triangle indices. B2, the D3D12 tests (d3d12_existing_heap, a8_dimensions, pipeline_cache_seed, title_va, rect_list, shader_library, loader_tls_index0, draw, compute, xmem_placed, shader_library_corpus): the base's tests where both carry the same case, #939's cases added (rebound tables, linear texture map, large vertex batch, executable shaders, default A8 views). C, the backend (vulkan_replay.h, vulkan_backend.h, vulkan_bindless.h, vulkan_scratch.h, vulkan_present.h, rect_list_geometry.h, vk_test_device.h, vk_pipeline_check.cpp, vk_store_test.cpp, bindless_sizing_test.cpp, replay_texture_barrier_test.cpp): the base's prepare_draw and lazy compute, its #1055 release rule and its rectangle generator; #939's rectangle variants, GPU quad index expansion, 1D/3D and depth-stencil UAVs, the label fence on the base's submit. D, memory, services, presentation (xmem.cpp/.h, xg_xs.cpp, xg_xs_test.cpp, title_va_alias.h and its test, main.cpp, frame_pacer.h, xbox_present.cpp/.h; xgameruntime_offline.cpp for one comment): the base's xmem with #939's label hooks, file-backed sections and pin_mapped_data; both texture computers in xg_xs.cpp, the base's answering first; --first-run-ui kept without host speech in its profile. E, build, tools, docs (runtime/CMakeLists.txt, make_game.py, tools/rdna2spirv.py, tools/shader_disasm.py, scripts, tests, docs; docs/README.md and tests/test_exe_shaders_stage.py beyond the conflicts): the base's v_dot4 decode, #281's disassembler form and tests/semantic/test_fspx_dot4_op_sel_disasm.py dropped; the base's executable-shader step with #939's cache fix; 366 registered tests. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/console_registry.cpp')
-rw-r--r--runtime/shims/console_registry.cpp160
1 files changed, 160 insertions, 0 deletions
diff --git a/runtime/shims/console_registry.cpp b/runtime/shims/console_registry.cpp
new file mode 100644
index 000000000..60633f43f
--- /dev/null
+++ b/runtime/shims/console_registry.cpp
@@ -0,0 +1,160 @@
1// Registry shim: the console model the title reads from the registry.
2//
3// GTA VI does not only call XSystemGetDeviceType. Code linked into the executable (and into libHttpClient.GDK.dll
4// and XCurl.dll, which carry the same statically linked GDK helper) opens
5// HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\OEM
6// with RegOpenKeyW / RegOpenKeyExW, reads the DWORD "ConsoleMode" with RegQueryValueExW (0x64 Series X, 0x60 Series
7// S; a missing value means a PC, and a PC takes the PC memory and resolution budgets), and calls RegCloseKey. Under
8// Proton the value could be written into the prefix, and on a Windows PC it needs administrator rights. Leonida
9// answers it itself, from the same setting as XSystemGetDeviceType (LEONIDA_DEVICE_TYPE, console_identity.h).
10//
11// Only that one key and that one value are ours: a pseudo key stands for the key, and everything else (every other
12// key, value, handle) goes to the host's advapi32 unchanged. For a device type that has no ConsoleMode (a PC, an
13// Xbox One) the key is not ours either, so the host registry answers and a missing key stays missing.
14//
15// Bound for ADVAPI32.dll and for the registry API sets (XCurl.dll imports api-ms-win-core-registry-l1-1-0 and
16// l2-1-0, measured with tools/pe_inspect.py); rt::shims::owns_dll keeps advapi32 and the api sets host modules.
17//
18// Ported with credit from VIctory crates/victory-hle/src/modules/xsystem.rs (mod device_type, commit 2849477,
19// GPL-3.0-or-later): the pseudo-key design and the way a null buffer, the size and the type are answered. The
20// hook in xbox-emulator crates/compat/compat-common/src/gdk/hook.rs (commit 0284e83, GPL-3.0) did the same with
21// an import patch. Written here in C++ against the documented RegQueryValueExW contract.
22#include <windows.h>
23
24#include <atomic>
25#include <cstdio>
26#include <cstring>
27
28#include "console_identity.h"
29#include "console_registry.h"
30#include "shims.h"
31
32namespace rt::shims::console_registry {
33namespace {
34
35// HKEY_LOCAL_MACHINE as the title passes it: the predefined handle value (sign-extended 0x80000002).
36const HKEY kLocalMachine = HKEY_LOCAL_MACHINE;
37constexpr wchar_t kOemKey[] = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\OEM";
38constexpr wchar_t kConsoleMode[] = L"ConsoleMode";
39
40// The pseudo key's handle value: the address of a static, so it is never a kernel handle value.
41char g_oem_key;
42
43std::atomic<const HostRegistry*> g_test_host{nullptr};
44
45HostRegistry load_advapi32() {
46 HostRegistry h{};
47 if (const HMODULE m = LoadLibraryExW(L"advapi32.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) {
48 h.open_key = reinterpret_cast<decltype(h.open_key)>(GetProcAddress(m, "RegOpenKeyW"));
49 h.open_key_ex = reinterpret_cast<decltype(h.open_key_ex)>(GetProcAddress(m, "RegOpenKeyExW"));
50 h.query_value_ex = reinterpret_cast<decltype(h.query_value_ex)>(GetProcAddress(m, "RegQueryValueExW"));
51 h.close_key = reinterpret_cast<decltype(h.close_key)>(GetProcAddress(m, "RegCloseKey"));
52 }
53 return h;
54}
55
56const HostRegistry& host() {
57 if (const HostRegistry* t = g_test_host.load()) return *t;
58 static const HostRegistry real = load_advapi32(); // thread-safe; a failed load leaves null pointers
59 return real;
60}
61
62bool equals_ignore_case(LPCWSTR a, const wchar_t* b) {
63 return a && CompareStringOrdinal(a, -1, b, -1, TRUE) == CSTR_EQUAL;
64}
65
66// Whether the title asks for our key: HKLM and the OEM path (case-insensitive, as the registry is), and only while the
67// configured device type has a ConsoleMode to give.
68bool is_ours(HKEY key, LPCWSTR sub_key) {
69 DWORD mode;
70 return key == kLocalMachine && equals_ignore_case(sub_key, kOemKey) &&
71 console_identity::console_mode_for(console_identity::configured_device_type(), mode);
72}
73
74} // namespace
75
76HKEY pseudo_key() { return reinterpret_cast<HKEY>(&g_oem_key); }
77
78void set_host_registry_for_test(const HostRegistry* h) { g_test_host.store(h); }
79
80LSTATUS WINAPI reg_open_key_w(HKEY key, LPCWSTR sub_key, PHKEY result) {
81 if (key == pseudo_key()) {
82 // The OEM key has no subkeys; opening it with no subkey path gives the same key, as the registry does.
83 if (!result) return ERROR_INVALID_PARAMETER;
84 if (sub_key && *sub_key) return ERROR_FILE_NOT_FOUND;
85 *result = pseudo_key();
86 return ERROR_SUCCESS;
87 }
88 if (result && is_ours(key, sub_key)) {
89 *result = pseudo_key();
90 return ERROR_SUCCESS;
91 }
92 return host().open_key ? host().open_key(key, sub_key, result) : ERROR_CALL_NOT_IMPLEMENTED;
93}
94
95LSTATUS WINAPI reg_open_key_ex_w(HKEY key, LPCWSTR sub_key, DWORD options, REGSAM desired, PHKEY result) {
96 if (key == pseudo_key()) {
97 if (!result) return ERROR_INVALID_PARAMETER;
98 if (sub_key && *sub_key) return ERROR_FILE_NOT_FOUND;
99 *result = pseudo_key();
100 return ERROR_SUCCESS;
101 }
102 if (result && is_ours(key, sub_key)) {
103 *result = pseudo_key();
104 return ERROR_SUCCESS;
105 }
106 return host().open_key_ex ? host().open_key_ex(key, sub_key, options, desired, result)
107 : ERROR_CALL_NOT_IMPLEMENTED;
108}
109
110LSTATUS WINAPI reg_query_value_ex_w(HKEY key, LPCWSTR value_name, LPDWORD reserved, LPDWORD type, LPBYTE data,
111 LPDWORD size) {
112 if (key != pseudo_key())
113 return host().query_value_ex ? host().query_value_ex(key, value_name, reserved, type, data, size)
114 : ERROR_CALL_NOT_IMPLEMENTED;
115
116 // Our key holds one value. The default (unnamed) value and every other name do not exist.
117 DWORD mode;
118 if (!equals_ignore_case(value_name, kConsoleMode) ||
119 !console_identity::console_mode_for(console_identity::configured_device_type(), mode))
120 return ERROR_FILE_NOT_FOUND;
121
122 if (data && !size) return ERROR_INVALID_PARAMETER; // a buffer with no size to say how big it is
123 if (type) *type = REG_DWORD;
124 if (!size) return ERROR_SUCCESS; // the caller wants only the type (or nothing)
125
126 const DWORD room = *size;
127 *size = sizeof(DWORD);
128 if (!data) return ERROR_SUCCESS; // a size query
129 if (room < sizeof(DWORD)) return ERROR_MORE_DATA;
130 std::memcpy(data, &mode, sizeof(DWORD)); // the buffer may be unaligned
131 // Once per distinct answer, so a boot log shows what the title was told without a line per read.
132 static std::atomic<DWORD> logged{0};
133 if (logged.exchange(mode) != mode)
134 std::fprintf(stderr, "[rt] registry: ConsoleMode answered 0x%x (LEONIDA_DEVICE_TYPE=%u)\n",
135 static_cast<unsigned>(mode), console_identity::configured_device_type());
136 return ERROR_SUCCESS;
137}
138
139LSTATUS WINAPI reg_close_key(HKEY key) {
140 if (key == pseudo_key()) return ERROR_SUCCESS;
141 return host().close_key ? host().close_key(key) : ERROR_CALL_NOT_IMPLEMENTED;
142}
143
144} // namespace rt::shims::console_registry
145
146#define REG_ENTRIES(dll) \
147 {dll, "RegOpenKeyW", reinterpret_cast<void*>(&rt::shims::console_registry::reg_open_key_w)}, \
148 {dll, "RegOpenKeyExW", reinterpret_cast<void*>(&rt::shims::console_registry::reg_open_key_ex_w)}, \
149 {dll, "RegQueryValueExW", reinterpret_cast<void*>(&rt::shims::console_registry::reg_query_value_ex_w)}, \
150 {dll, "RegCloseKey", reinterpret_cast<void*>(&rt::shims::console_registry::reg_close_key)}
151
152// The classic import library and the API sets the registry functions live in.
153SHIM_TABLE(console_registry,
154 REG_ENTRIES("advapi32.dll"),
155 REG_ENTRIES("api-ms-win-core-registry-l1-1-0.dll"),
156 REG_ENTRIES("api-ms-win-core-registry-l1-1-1.dll"),
157 REG_ENTRIES("api-ms-win-core-registry-l1-1-2.dll"),
158 REG_ENTRIES("api-ms-win-core-registry-l2-1-0.dll"),
159 REG_ENTRIES("api-ms-win-core-registry-l2-1-1.dll"),
160);