upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/xgameruntime_test.cpp
diff options
context:
space:
mode:
authorTomgamer <tomanhorak22@gmail.com>2026-11-22 16:32:05 +0100
committerTomgamer <tomanhorak22@gmail.com>2026-11-22 16:32:05 +0100
commiteb35aed2e7e8ea1d4a11fd4e42ab80d853f05169 (patch)
tree22bd0e6fa6363235d1feedc1c9be678ab63bee4b /runtime/test/xgameruntime_test.cpp
parent759a90c42e58886153f67dcd2d5d41c44face012 (diff)
parent4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff)
Merge origin/main into shader-309-wave64-lds-limit
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/xgameruntime_test.cpp')
-rw-r--r--runtime/test/xgameruntime_test.cpp158
1 files changed, 158 insertions, 0 deletions
diff --git a/runtime/test/xgameruntime_test.cpp b/runtime/test/xgameruntime_test.cpp
index ac36f40d9..8f5057662 100644
--- a/runtime/test/xgameruntime_test.cpp
+++ b/runtime/test/xgameruntime_test.cpp
@@ -51,8 +51,36 @@ using AsyncCompleteFn = void(__stdcall*)(XAsyncBlock*, HRESULT, size_t);
51using AsyncResultSizeFn = HRESULT(__stdcall*)(XAsyncBlock*, size_t*); 51using AsyncResultSizeFn = HRESULT(__stdcall*)(XAsyncBlock*, size_t*);
52using AsyncResultFn = HRESULT(__stdcall*)(XAsyncBlock*, const void*, size_t, void*, size_t*); 52using AsyncResultFn = HRESULT(__stdcall*)(XAsyncBlock*, const void*, size_t, void*, size_t*);
53 53
54struct XVersion {
55 union {
56 struct {
57 uint16_t major;
58 uint16_t minor;
59 uint16_t build;
60 uint16_t revision;
61 };
62 uint64_t Value;
63 };
64};
65static_assert(sizeof(XVersion) == 8, "XVersion must be 8 bytes per GDK spec");
66
67struct XSystemAnalyticsInfo {
68 XVersion osVersion;
69 XVersion hostingOsVersion;
70 char family[64];
71 char form[64];
72};
73static_assert(sizeof(XSystemAnalyticsInfo) == 144, "XSystemAnalyticsInfo must be 144 bytes per GDK spec");
74
75using AnalyticsInfoFn = XSystemAnalyticsInfo(__stdcall*)();
76using PackageIdentifierFn = HRESULT(__stdcall*)(size_t, char*);
77using PackageMountPathFn = HRESULT(__stdcall*)(void*, size_t, char*);
78using PackagedProcessFn = bool(__stdcall*)();
79
54const char* const kNames[] = { 80const char* const kNames[] = {
55 "XGameRuntimeInitialize", "XGameRuntimeUninitialize", "XSystemGetDeviceType", "XSystemGetConsoleId", 81 "XGameRuntimeInitialize", "XGameRuntimeUninitialize", "XSystemGetDeviceType", "XSystemGetConsoleId",
82 "XSystemGetAnalyticsInfo", "XPackageGetCurrentProcessPackageIdentifier", "XPackageGetMountPath",
83 "XPackageIsPackagedProcess",
56 "XTaskQueueCreate", "XTaskQueueCreateComposite", "XTaskQueueGetPort", "XTaskQueueDuplicateHandle", 84 "XTaskQueueCreate", "XTaskQueueCreateComposite", "XTaskQueueGetPort", "XTaskQueueDuplicateHandle",
57 "XTaskQueueCloseHandle", "XTaskQueueTerminate", "XTaskQueueDispatch", "XTaskQueueSubmitCallback", 85 "XTaskQueueCloseHandle", "XTaskQueueTerminate", "XTaskQueueDispatch", "XTaskQueueSubmitCallback",
58 "XTaskQueueSubmitDelayedCallback", "XTaskQueueRegisterWaiter", "XTaskQueueUnregisterWaiter", 86 "XTaskQueueSubmitDelayedCallback", "XTaskQueueRegisterWaiter", "XTaskQueueUnregisterWaiter",
@@ -182,6 +210,89 @@ void check_run_time_load() {
182 void* out = reinterpret_cast<void*>(1); 210 void* out = reinterpret_cast<void*>(1);
183 check(query(&some_class, some_class, &out) == static_cast<HRESULT>(0x80070032) && out == nullptr, 211 check(query(&some_class, some_class, &out) == static_cast<HRESULT>(0x80070032) && out == nullptr,
184 "QueryApiImpl refuses a class it doesn't provide yet (HRESULT 0x80070032) and clears the out pointer"); 212 "QueryApiImpl refuses a class it doesn't provide yet (HRESULT 0x80070032) and clears the out pointer");
213
214 // XThreadingImpl and XErrorImpl, driven the way xgameruntime.lib's thunks in the package's DLLs do: QueryApiImpl
215 // with the class id as both ids, one vtable slot with `this` first, then Release (slot 2).
216 const GUID threading = {0x073b7dcb, 0x1fcf, 0x4030, {0x94, 0xbe, 0xe3, 0xc9, 0xeb, 0x62, 0x34, 0x28}};
217 const GUID error_cls = {0x8ca467f7, 0x22e8, 0x4096, {0x84, 0x56, 0xbb, 0x8a, 0xa1, 0x3f, 0x79, 0xd8}};
218 using Fn = void*;
219 auto slot = [](void* obj, int index) { return (*static_cast<Fn* const*>(obj))[index]; };
220 auto release = [&](void* obj) { reinterpret_cast<ULONG(STDMETHODCALLTYPE*)(void*)>(slot(obj, 2))(obj); };
221 auto get = [&](const GUID& cls) {
222 void* p = nullptr;
223 return query(&cls, cls, &p) == S_OK ? p : nullptr;
224 };
225 void* t = get(threading);
226 check(t != nullptr, "QueryApiImpl hands out XThreadingImpl for its class id");
227 if (t) {
228 void* again = get(threading);
229 check(again == t, "XThreadingImpl is one process-wide object");
230 release(again);
231 XTaskQueueHandle queue = nullptr;
232 using Create = HRESULT(STDMETHODCALLTYPE*)(void*, XTaskQueueDispatchMode, XTaskQueueDispatchMode,
233 XTaskQueueHandle*);
234 check(reinterpret_cast<Create>(slot(t, 12))(t, XTaskQueueDispatchMode::Manual, XTaskQueueDispatchMode::Manual,
235 &queue) == S_OK && queue,
236 "slot 12 is XTaskQueueCreate");
237 static std::atomic<int> s_ran{0};
238 using Submit = HRESULT(STDMETHODCALLTYPE*)(void*, XTaskQueueHandle, XTaskQueuePort, void*,
239 XTaskQueueCallback*);
240 XTaskQueueCallback* cb = [](void* ctx, bool canceled) {
241 if (!canceled) static_cast<std::atomic<int>*>(ctx)->fetch_add(1);
242 };
243 check(reinterpret_cast<Submit>(slot(t, 18))(t, queue, XTaskQueuePort::Work, &s_ran, cb) == S_OK,
244 "slot 18 is XTaskQueueSubmitCallback");
245 using Dispatch = bool(STDMETHODCALLTYPE*)(void*, XTaskQueueHandle, XTaskQueuePort, uint32_t);
246 check(reinterpret_cast<Dispatch>(slot(t, 16))(t, queue, XTaskQueuePort::Work, 0) && s_ran.load() == 1,
247 "slot 16 is XTaskQueueDispatch: the callback ran on the work port");
248 XTaskQueueHandle dup = nullptr;
249 using Dup = HRESULT(STDMETHODCALLTYPE*)(void*, XTaskQueueHandle, XTaskQueueHandle*);
250 check(reinterpret_cast<Dup>(slot(t, 15))(t, queue, &dup) == S_OK && dup,
251 "slot 15 is XTaskQueueDuplicateHandle");
252 using Close = void(STDMETHODCALLTYPE*)(void*, XTaskQueueHandle);
253 reinterpret_cast<Close>(slot(t, 17))(t, dup);
254 reinterpret_cast<Close>(slot(t, 17))(t, queue);
255 using SetSensitive = HRESULT(STDMETHODCALLTYPE*)(void*, bool);
256 using IsSensitive = bool(STDMETHODCALLTYPE*)(void*);
257 check(reinterpret_cast<SetSensitive>(slot(t, 27))(t, true) == S_OK &&
258 reinterpret_cast<IsSensitive>(slot(t, 30))(t) &&
259 reinterpret_cast<SetSensitive>(slot(t, 27))(t, false) == S_OK &&
260 !reinterpret_cast<IsSensitive>(slot(t, 30))(t),
261 "slots 27 and 30 set and read this thread's time-sensitive flag");
262 void* unk = nullptr;
263 const GUID iunknown = {0, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46}};
264 using QI = HRESULT(STDMETHODCALLTYPE*)(void*, REFIID, void**);
265 check(reinterpret_cast<QI>(slot(t, 0))(t, iunknown, &unk) == S_OK && unk == t &&
266 reinterpret_cast<QI>(slot(t, 0))(t, error_cls, &unk) == E_NOINTERFACE && !unk,
267 "QueryInterface answers IUnknown and the class id, not another class");
268 release(t);
269 release(t);
270 }
271 void* e = get(error_cls);
272 check(e != nullptr, "QueryApiImpl hands out XErrorImpl for its class id");
273 if (e) {
274 struct Seen {
275 HRESULT hr;
276 char msg[32];
277 } seen{};
278 using Callback = bool(CALLBACK*)(HRESULT, const char*, void*);
279 Callback on_error = [](HRESULT hr, const char* msg, void* ctx) {
280 auto* s = static_cast<Seen*>(ctx);
281 s->hr = hr;
282 strncpy_s(s->msg, msg ? msg : "", _TRUNCATE);
283 return true;
284 };
285 using SetCallback = void(STDMETHODCALLTYPE*)(void*, Callback, void*);
286 using Report = void(STDMETHODCALLTYPE*)(void*, HRESULT, const char*);
287 reinterpret_cast<SetCallback>(slot(e, 4))(e, on_error, &seen);
288 reinterpret_cast<Report>(slot(e, 3))(e, E_FAIL, "offline test");
289 check(seen.hr == E_FAIL && std::strcmp(seen.msg, "offline test") == 0,
290 "slot 3 is XErrorReport and reaches the callback slot 4 (XErrorSetCallback) set");
291 reinterpret_cast<SetCallback>(slot(e, 4))(e, nullptr, nullptr);
292 using SetOptions = HRESULT(STDMETHODCALLTYPE*)(void*, uint32_t, uint32_t);
293 check(reinterpret_cast<SetOptions>(slot(e, 5))(e, 0, 0) == S_OK, "slot 5 is XErrorSetOptions");
294 release(e);
295 }
185 check(uninit_api() == S_OK, "UninitializeApiImpl succeeds"); 296 check(uninit_api() == S_OK, "UninitializeApiImpl succeeds");
186 297
187 HMODULE k32 = load_w(L"kernel32.dll"); 298 HMODULE k32 = load_w(L"kernel32.dll");
@@ -229,6 +340,53 @@ int main() {
229 check(console_id(sizeof(id), id, &used) == S_OK && used == 39 && std::strlen(id) == 38, 340 check(console_id(sizeof(id), id, &used) == S_OK && used == 39 && std::strlen(id) == 38,
230 "XSystemGetConsoleId fills 39 bytes (38 characters and the terminator)"); 341 "XSystemGetConsoleId fills 39 bytes (38 characters and the terminator)");
231 342
343 auto analytics_info = get<AnalyticsInfoFn>("XSystemGetAnalyticsInfo");
344 XSystemAnalyticsInfo info = analytics_info();
345 check(info.osVersion.major == 10 && info.osVersion.minor == 0 && info.osVersion.build == 22000 &&
346 info.osVersion.revision == 0,
347 "XSystemGetAnalyticsInfo returns osVersion 10.0.22000.0");
348 check(info.hostingOsVersion.major == 10 && info.hostingOsVersion.minor == 0 &&
349 info.hostingOsVersion.build == 22000 && info.hostingOsVersion.revision == 0,
350 "XSystemGetAnalyticsInfo returns hostingOsVersion 10.0.22000.0");
351 check(std::strcmp(info.family, "Xbox") == 0, "XSystemGetAnalyticsInfo returns family 'Xbox'");
352 check(std::strcmp(info.form, "Xbox Series X") == 0, "XSystemGetAnalyticsInfo returns form 'Xbox Series X'");
353
354 auto package_id = get<PackageIdentifierFn>("XPackageGetCurrentProcessPackageIdentifier");
355 check(package_id(0, nullptr) == E_INVALIDARG, "XPackageGetCurrentProcessPackageIdentifier rejects null buffer and size 0");
356 char pkg_id[64] = {};
357 check(package_id(0, pkg_id) == E_INVALIDARG, "XPackageGetCurrentProcessPackageIdentifier rejects size 0 with buffer");
358 check(package_id(10, nullptr) == E_INVALIDARG, "XPackageGetCurrentProcessPackageIdentifier rejects null pointer with size");
359 check(package_id(16, pkg_id) == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER),
360 "XPackageGetCurrentProcessPackageIdentifier rejects insufficient buffer");
361 check(package_id(sizeof(pkg_id), pkg_id) == S_OK && std::strlen(pkg_id) == 32,
362 "XPackageGetCurrentProcessPackageIdentifier fills 33-byte package identifier");
363
364 SetEnvironmentVariableA("LEONIDA_PACKAGE_ID", "CustomTestPackageId_123456789012");
365 check(package_id(sizeof(pkg_id), pkg_id) == S_OK &&
366 std::strcmp(pkg_id, "CustomTestPackageId_123456789012") == 0,
367 "XPackageGetCurrentProcessPackageIdentifier respects LEONIDA_PACKAGE_ID override");
368 SetEnvironmentVariableA("LEONIDA_PACKAGE_ID", nullptr);
369
370 auto mount_path = get<PackageMountPathFn>("XPackageGetMountPath");
371 check(mount_path(nullptr, 0, nullptr) == E_INVALIDARG, "XPackageGetMountPath rejects null buffer and size 0");
372 char mnt_path[MAX_PATH] = {};
373 check(mount_path(nullptr, 0, mnt_path) == E_INVALIDARG, "XPackageGetMountPath rejects size 0 with buffer");
374 check(mount_path(nullptr, 10, nullptr) == E_INVALIDARG, "XPackageGetMountPath rejects null pointer with size");
375
376 SetEnvironmentVariableA("LEONIDA_GAME_DIR", "C:\\LeonidaGameDir");
377 check(mount_path(nullptr, 5, mnt_path) == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER),
378 "XPackageGetMountPath rejects insufficient buffer");
379 check(mount_path(nullptr, sizeof(mnt_path), mnt_path) == S_OK &&
380 std::strcmp(mnt_path, "C:\\LeonidaGameDir") == 0,
381 "XPackageGetMountPath returns configured game directory");
382 SetEnvironmentVariableA("LEONIDA_GAME_DIR", nullptr);
383
384 auto is_packaged = get<PackagedProcessFn>("XPackageIsPackagedProcess");
385 check(is_packaged() == true, "XPackageIsPackagedProcess returns true by default");
386 SetEnvironmentVariableA("LEONIDA_PACKAGED", "0");
387 check(is_packaged() == false, "XPackageIsPackagedProcess respects LEONIDA_PACKAGED=0 override");
388 SetEnvironmentVariableA("LEONIDA_PACKAGED", nullptr);
389
232 // Work on the thread pool, completions dispatched by the "game loop" on this thread. 390 // Work on the thread pool, completions dispatched by the "game loop" on this thread.
233 auto create = get<QueueCreateFn>("XTaskQueueCreate"); 391 auto create = get<QueueCreateFn>("XTaskQueueCreate");
234 auto close = get<QueueCloseFn>("XTaskQueueCloseHandle"); 392 auto close = get<QueueCloseFn>("XTaskQueueCloseHandle");