// XStoreImpl through QueryApiImpl, called the way xgameruntime.lib's XStore* thunks call it: QueryApiImpl with the // class id (or a later interface id), one vtable slot with `this` first. Slots follow xgameruntime-docs' layout; // arguments follow the public GDK reference. No game data; nothing online. #include #include #include #include #include #include #include #include #include #include "shims.h" namespace rt::shims { long xgameruntime_store_open_handles(); } namespace { int g_checks = 0; int g_failures = 0; void check(bool condition, const char* what) { ++g_checks; std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); if (!condition) ++g_failures; } using Handle = void*; struct GameLicense { // XStoreGameLicense with the measured sizes of xgameruntime_store.cpp char skuStoreId[18]; bool isActive; bool isTrialOwnedByThisUser; bool isDiscLicense; bool isTrial; uint32_t trialTimeRemainingInSeconds; char trialUniqueId[64]; time_t expirationDate; }; static_assert(sizeof(GameLicense) == 104, "XStoreGameLicense is 104 bytes"); static_assert(offsetof(GameLicense, isTrialOwnedByThisUser) == 0x13, "measured flag offset"); static_assert(offsetof(GameLicense, trialUniqueId) == 0x1C, "measured trial-id offset"); static_assert(offsetof(GameLicense, expirationDate) == 0x60, "measured expiration offset"); struct CanAcquire { char licensableSku[5]; uint32_t status; }; constexpr GUID kStoreClass = {0x0dd112ac, 0x7c24, 0x448c, {0xb9, 0x2b, 0x39, 0x60, 0xfb, 0x5b, 0xd3, 0x0c}}; constexpr GUID kStoreImpl6 = {0xb09d803c, 0x2414, 0x4a05, {0x82, 0xc6, 0x66, 0xdf, 0xdc, 0x9e, 0x9a, 0x44}}; constexpr GUID kStoreImpl2 = {0x60b09f4e, 0x1b85, 0x45b1, {0x82, 0x6c, 0x16, 0x91, 0x18, 0xe2, 0x30, 0xe1}}; constexpr GUID kOther = {0x973a344e, 0x24bf, 0x4d0f, {0x84, 0x57, 0x56, 0xc5, 0x34, 0x89, 0x2b, 0x29}}; // XGameImpl using Query = HRESULT(WINAPI*)(const GUID*, REFIID, void**); using Fn = void*; Fn slot(void* obj, int index) { return (*static_cast(obj))[index]; } template F at(void* obj, int index) { return reinterpret_cast(slot(obj, index)); } bool finished(XAsyncBlock* async) { return XAsyncGetStatus(async, true) != E_PENDING; } using CreateContext = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, Handle*); using Close = void(STDMETHODCALLTYPE*)(void*, Handle); using CtxAsync = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, XAsyncBlock*); using CtxIdAsync = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, const char*, XAsyncBlock*); using LicenseResult = HRESULT(STDMETHODCALLTYPE*)(void*, XAsyncBlock*, GameLicense*); using CanResult = HRESULT(STDMETHODCALLTYPE*)(void*, XAsyncBlock*, CanAcquire*); using HandleResult = HRESULT(STDMETHODCALLTYPE*)(void*, XAsyncBlock*, Handle*); using Count = HRESULT(STDMETHODCALLTYPE*)(void*, XAsyncBlock*, uint32_t*); using Items = HRESULT(STDMETHODCALLTYPE*)(void*, XAsyncBlock*, uint32_t, void*); using IsValid = bool(STDMETHODCALLTYPE*)(void*, Handle); using ProductsAsync = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, uint32_t, const char**, size_t, const char**, size_t, XAsyncBlock*); using Enumerate = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, void*, void*); using Register = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, XTaskQueueHandle, void*, void(CALLBACK*)(void*), XTaskQueueRegistrationToken*); using Unregister = bool(STDMETHODCALLTYPE*)(void*, Handle, XTaskQueueRegistrationToken, bool); using NoArgs = HRESULT(STDMETHODCALLTYPE*)(void*); int g_callbacks = 0; void CALLBACK on_change(void*) { ++g_callbacks; } bool CALLBACK on_product(void*, void*) { ++g_callbacks; return true; } } // namespace int main() { std::setvbuf(stdout, nullptr, _IONBF, 0); std::printf("xgameruntime_store_test: XStoreImpl through QueryApiImpl\n"); auto query = reinterpret_cast(rt::shims::find("xgameruntime.dll", "QueryApiImpl")); check(query != nullptr, "QueryApiImpl is registered"); if (!query) return 1; void* store = nullptr; check(query(&kStoreClass, kStoreClass, &store) == S_OK && store, "QueryApiImpl hands out XStoreImpl for its class id"); if (!store) return 1; void* store6 = nullptr; void* store2 = nullptr; check(query(&kStoreClass, kStoreImpl6, &store6) == S_OK && store6 == store && query(&kStoreClass, kStoreImpl2, &store2) == S_OK && store2 == store, "IXStoreImpl2 and IXStoreImpl6 answer with the same object"); void* none = reinterpret_cast(1); check(query(&kStoreClass, kOther, &none) == E_NOINTERFACE && none == nullptr, "another interface id is refused"); Handle context = nullptr; check(at(store, 3)(store, reinterpret_cast(0x1000), &context) == S_OK && context, "XStoreCreateContext"); // ---- the game license, as a title checks it at boot ---- XAsyncBlock async{}; GameLicense license; std::memset(&license, 0xCD, sizeof(license)); check(at(store, 28)(store, context, &async) == S_OK && finished(&async), "XStoreQueryGameLicenseAsync completes"); size_t license_size = 0; check(XAsyncGetResultSize(&async, &license_size) == S_OK && license_size == 104, "the game license result size is the measured 104 bytes"); check(at(store, 29)(store, &async, &license) == S_OK, "XStoreQueryGameLicenseResult"); check(license.isActive && !license.isTrial && !license.isTrialOwnedByThisUser && !license.isDiscLicense && license.trialTimeRemainingInSeconds == 0 && license.trialUniqueId[0] == '\0' && std::strcmp(license.skuStoreId, "0010") == 0, "the game license is active, not a trial, every other field cleared"); check(at(store, 28)(store, nullptr, &async) == E_INVALIDARG, "a null store context is refused"); // ---- add-ons and updates: none ---- async = XAsyncBlock{}; uint32_t count = 99; check(at(store, 30)(store, context, &async) == S_OK && finished(&async) && at(store, 31)(store, &async, &count) == S_OK && count == 0 && at(store, 32)(store, &async, 0, nullptr) == S_OK, "no add-on licenses"); async = XAsyncBlock{}; count = 99; check(at(store, 55)(store, context, &async) == S_OK && finished(&async) && at(store, 56)(store, &async, &count) == S_OK && count == 0, "no game or DLC package updates"); // ---- package licenses ---- async = XAsyncBlock{}; Handle package_license = nullptr; check(at(store, 20)(store, context, "SomePackage", &async) == S_OK && finished(&async) && at(store, 21)(store, &async, &package_license) == S_OK && package_license, "XStoreAcquireLicenseForPackage hands out a license"); check(at(store, 22)(store, package_license), "the package license is valid"); async = XAsyncBlock{}; CanAcquire can{}; check(at(store, 26)(store, context, "SomePackage", &async) == S_OK && finished(&async) && at(store, 27)(store, &async, &can) == S_OK && can.status == 1, "XStoreCanAcquireLicenseForPackage: Licensable"); XTaskQueueRegistrationToken token{}; check(at(store, 68)(store, package_license, nullptr, nullptr, on_change, &token) == S_OK && token.token != 0 && at(store, 69)(store, package_license, token, false), "license-lost registration hands out a token; unregistering is never pending"); at(store, 23)(store, package_license); check(!at(store, 22)(store, package_license), "a closed license is not valid"); // ---- product queries are empty ---- async = XAsyncBlock{}; Handle products = nullptr; const char* ids[] = {"9NBLGGH4R315"}; check(at(store, 7)(store, context, 1, ids, 1, nullptr, 0, &async) == S_OK && finished(&async) && at(store, 8)(store, &async, &products) == S_OK && products, "XStoreQueryProducts completes with a query handle"); check(at(store, 15)(store, products, nullptr, reinterpret_cast(&on_product)) == S_OK && !at(store, 16)(store, products), "the query has no products and no further pages"); at(store, 19)(store, products); // ---- what needs the service is started and completes with E_FAIL (review M11) ---- // A caller that ignores the Async HRESULT and waits on the block must get an answer, not hang: the block completes. using PurchaseUiAsync = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, const char*, const char*, const char*, XAsyncBlock*); async = XAsyncBlock{}; check(at(store, 49)(store, context, "9NBLGGH4R315", "name", nullptr, &async) == S_OK && finished(&async) && XAsyncGetStatus(&async, false) == E_FAIL, "XStoreShowPurchaseUIAsync starts and completes with E_FAIL offline (the block does not hang)"); using RateAsync = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, XAsyncBlock*); async = XAsyncBlock{}; check(at(store, 51)(store, context, &async) == S_OK && finished(&async) && XAsyncGetStatus(&async, false) == E_FAIL, "XStoreShowRateAndReviewUIAsync (block as 2nd argument) completes with E_FAIL"); using RedeemAsync = HRESULT(STDMETHODCALLTYPE*)(void*, Handle, const char*, const char**, size_t, bool, XAsyncBlock*); async = XAsyncBlock{}; check(at(store, 53)(store, context, "token", nullptr, 0, false, &async) == S_OK && finished(&async) && XAsyncGetStatus(&async, false) == E_FAIL, "XStoreShowRedeemTokenUIAsync (block as 6th argument, on the stack) completes with E_FAIL"); check(at(store, 50)(store) == E_FAIL, "its Result partner returns E_FAIL"); check(at(store, 46)(store) == E_NOTIMPL, "an undocumented slot answers E_NOTIMPL"); check(at(store, 66)(store, context, nullptr, nullptr, on_change, &token) == S_OK && at(store, 67)(store, context, token, true), "license-changed registration"); check(g_callbacks == 0, "no callback ran: nothing changes offline"); at(store, 4)(store, context); at(store, 4)(store, context); // twice: ignored check(rt::shims::xgameruntime_store_open_handles() == 0, "every handle the test opened is closed"); std::printf("xgameruntime_store_test: %d checks, %d failures\n", g_checks, g_failures); return g_failures == 0 ? 0 : 1; }