Compare commits

...
Author SHA1 Message Date
ates 44387cd9f6 Merge branch 'main' into track-e/418-offline-gdk-runtime
Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-11-26 11:07:04 +03:00
ates 88fa2d1a99 Deliver queued callbacks on xgameruntime shutdown
Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-11-26 09:53:14 +03:00
ates 8742734a0a Implement offline xgameruntime task queues and services
Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-11-26 09:18:48 +03:00
7 changed files with 1118 additions and 109 deletions

No files matched your search

+1
View File
@@ -10,6 +10,7 @@ Guest execution uses original, purpose-built test programs. Commercial Xbox game
remains unsupported.
### Added
- Implemented offline `xgameruntime.dll` task queues and async completion, local user handles, synthetic game-license queries, console identity, and bottle-local storage (#418). Added a synthetic conformance executable for Lockhart and Anaconda launches. Only the marked conformance title receives an active synthetic SKU. The build also produces an unmarked control with the same executable name. Queue shutdown delivers accepted callbacks, and bottle-local storage is keyed by executable path.
- Added a visual `Progress Tracker` section to `README.md` with an M0–M6 Mermaid milestone pipeline, a Track A–F live status board (`🟩`/`🟨`/`⬜`), and direct GitHub label links for open M1 issues.
- Added a required `Zen` field to `.github/ISSUE_TEMPLATE/track-handoff.yml` so every track handoff issue states its core purpose briefly in plain English up front.
- Added Section 0 (`First-Time Contributor Onboarding`) to `AGENTS.md` (`CLAUDE.md` symlink) giving coding agents a turnkey step-by-step onboarding flow (preflight, `bx-cli doctor`, unclaimed `status: ready` issue discovery, claiming, scoped implementation, local verification including hosts without CrossOver, DCO commit, and PR CI watching).
+2
View File
@@ -1,4 +1,6 @@
bx_add_shim_dll(xgameruntime xgameruntime xgameruntime.def
xgameruntime.cpp
task_queue.cpp
)
target_link_libraries(xgameruntime PRIVATE bx_runtime_veh kernel32)
target_compile_options(xgameruntime PRIVATE -fexceptions)
+263
View File
@@ -0,0 +1,263 @@
#include "task_queue.h"
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <new>
#include <unordered_map>
namespace bx::gdk {
class Queue : public std::enable_shared_from_this<Queue> {
public:
struct Lane {
Queue* owner;
DispatchMode mode;
PTP_WORK worker = nullptr;
std::mutex mutex;
std::condition_variable ready;
std::unique_ptr<Task> first;
Task* last = nullptr;
// Keep the queue alive while Windows has outstanding worker callbacks.
QueueRef worker_owner;
size_t workers = 0;
bool serial_running = false;
bool closed = false;
Lane(Queue* owner, DispatchMode mode) : owner(owner), mode(mode) {}
~Lane() {
if (worker) CloseThreadpoolWork(worker);
while (first) pop();
}
HRESULT initialize() {
if (mode == DispatchMode::parallel || mode == DispatchMode::serialized) {
worker = CreateThreadpoolWork(run_worker, this, nullptr);
if (!worker) return HRESULT_FROM_WIN32(GetLastError());
}
return S_OK;
}
bool push(std::unique_ptr<Task> task, bool is_completion) {
std::unique_lock lock(mutex);
if (closed && !is_completion) return false;
if (mode == DispatchMode::immediate || (closed && mode == DispatchMode::manual)) {
lock.unlock();
task->run(owner->shared_from_this());
return true;
}
auto* tail = task.get();
(last ? last->next : first) = std::move(task);
last = tail;
ready.notify_one();
if (mode == DispatchMode::parallel) schedule();
schedule_serial();
return true;
}
void schedule() {
if (workers++ == 0) worker_owner = owner->shared_from_this();
SubmitThreadpoolWork(worker);
}
void schedule_serial() {
if (mode == DispatchMode::serialized && first && !serial_running && workers == 0) {
schedule();
}
}
std::unique_ptr<Task> pop() {
auto task = std::move(first);
first = std::move(task->next);
if (!first) last = nullptr;
return task;
}
void close(const QueueRef& queue) {
std::unique_lock lock(mutex);
closed = true;
ready.notify_all();
if (mode != DispatchMode::manual) return;
while (first) {
auto task = pop();
lock.unlock();
task->cancel(queue);
lock.lock();
}
}
bool dispatch(uint32_t timeout) {
if (mode == DispatchMode::immediate) return false;
std::unique_lock lock(mutex);
const auto has_task = [this] { return closed || (first && !serial_running); };
if (timeout == INFINITE) {
ready.wait(lock, has_task);
} else if (!ready.wait_for(lock, std::chrono::milliseconds(timeout), has_task)) {
return false;
}
if (closed) return false;
auto task = pop();
if (mode == DispatchMode::serialized) serial_running = true;
lock.unlock();
task->run(owner->shared_from_this());
if (mode == DispatchMode::serialized) {
lock.lock();
serial_running = false;
schedule_serial();
ready.notify_all();
}
return true;
}
static void CALLBACK run_worker(PTP_CALLBACK_INSTANCE, void* context, PTP_WORK) {
auto& lane = *static_cast<Lane*>(context);
QueueRef queue;
std::unique_lock lock(lane.mutex);
queue = lane.worker_owner;
while (lane.first && !lane.serial_running) {
auto task = lane.pop();
const bool serial = lane.mode == DispatchMode::serialized;
lane.serial_running = serial;
lock.unlock();
task->run(queue);
lock.lock();
lane.serial_running = false;
if (!serial) break;
}
if (--lane.workers == 0) lane.worker_owner.reset();
}
};
Queue(DispatchMode work, DispatchMode completion) : lanes{{this, work}, {this, completion}} {}
Lane lanes[2];
};
namespace {
std::mutex queues_mutex;
std::unordered_map<uintptr_t, QueueRef> handles;
uintptr_t next_handle = 1;
QueueRef process_queue;
HRESULT make_queue(DispatchMode work, DispatchMode completion, QueueRef& queue) {
auto candidate = std::make_shared<Queue>(work, completion);
for (auto& lane : candidate->lanes) {
const HRESULT hr = lane.initialize();
if (FAILED(hr)) return hr;
}
queue = std::move(candidate);
return S_OK;
}
void* add_handle(const QueueRef& queue) {
const auto id = next_handle;
next_handle += 2;
handles.emplace(id, queue);
return reinterpret_cast<void*>(id);
}
struct CallbackTask final : Task {
using Callback = void (CALLBACK*)(void*, bool);
Callback callback;
void* context;
CallbackTask(Callback callback, void* context) : callback(callback), context(context) {}
void run(const QueueRef&) override { callback(context, false); }
void cancel(const QueueRef&) override { callback(context, true); }
};
} // namespace
QueueRef acquire_queue(void* handle) {
std::lock_guard lock(queues_mutex);
if (!handle) {
try {
if (!process_queue) make_queue(DispatchMode::parallel, DispatchMode::parallel, process_queue);
} catch (const std::bad_alloc&) {
return {};
}
return process_queue;
}
const auto it = handles.find(reinterpret_cast<uintptr_t>(handle));
return it == handles.end() ? QueueRef{} : it->second;
}
bool submit(const QueueRef& queue, Port port, std::unique_ptr<Task> task, bool is_completion) {
return queue->lanes[static_cast<uint32_t>(port)].push(std::move(task), is_completion);
}
} // namespace bx::gdk
using namespace bx::gdk;
extern "C" {
HRESULT WINAPI XTaskQueueCreate(DispatchMode work, DispatchMode completion, void** out) {
if (!out) return E_POINTER;
*out = nullptr;
if (work > DispatchMode::immediate || completion > DispatchMode::immediate) return E_INVALIDARG;
try {
QueueRef queue;
const auto hr = make_queue(work, completion, queue);
if (FAILED(hr)) return hr;
std::lock_guard lock(queues_mutex);
*out = add_handle(queue);
return S_OK;
} catch (const std::bad_alloc&) {
return E_OUTOFMEMORY;
}
}
void WINAPI XTaskQueueCloseHandle(void* handle) {
QueueRef queue;
bool close_queue = false;
{
std::lock_guard lock(queues_mutex);
const auto it = handles.find(reinterpret_cast<uintptr_t>(handle));
if (it == handles.end()) return;
queue = std::move(it->second);
close_queue = queue != process_queue;
handles.erase(it);
}
// The frozen ABI exposes no termination or duplicate-handle API. Drain manual
// completion callbacks on close and let accepted pool work finish normally.
if (close_queue) {
for (auto& lane : queue->lanes) lane.close(queue);
}
}
bool WINAPI XTaskQueueDispatch(void* handle, Port port, uint32_t timeout) {
if (!handle || port > Port::completion) return false;
const auto queue = acquire_queue(handle);
return queue && queue->lanes[static_cast<uint32_t>(port)].dispatch(timeout);
}
HRESULT WINAPI XTaskQueueSubmitCallback(void* handle, Port port, void* context, CallbackTask::Callback callback) {
if (!handle || port > Port::completion || !callback) return E_INVALIDARG;
const auto queue = acquire_queue(handle);
if (!queue) return E_HANDLE;
auto task = std::unique_ptr<Task>(new (std::nothrow) CallbackTask(callback, context));
if (!task) return E_OUTOFMEMORY;
return submit(queue, port, std::move(task)) ? S_OK : E_HANDLE;
}
bool WINAPI XTaskQueueGetCurrentProcessTaskQueue(void** out) {
if (!out) return false;
*out = nullptr;
const auto queue = acquire_queue(nullptr);
if (!queue) return false;
try {
std::lock_guard lock(queues_mutex);
*out = add_handle(queue);
return true;
} catch (const std::bad_alloc&) {
return false;
}
}
} // extern "C"
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include <windows.h>
#include <cstdint>
#include <memory>
namespace bx::gdk {
enum class DispatchMode : uint32_t { manual, parallel, serialized, immediate };
enum class Port : uint32_t { work, completion };
class Queue;
using QueueRef = std::shared_ptr<Queue>;
struct Task {
virtual ~Task() = default;
virtual void run(const QueueRef& queue) = 0;
virtual void cancel(const QueueRef& queue) { run(queue); }
std::unique_ptr<Task> next;
};
QueueRef acquire_queue(void* handle);
bool submit(const QueueRef& queue, Port port, std::unique_ptr<Task> task, bool is_completion = false);
}
+532 -109
View File
@@ -1,141 +1,564 @@
#include <windows.h>
#include <cstdint>
#include "task_queue.h"
#include <condition_variable>
#include <cstddef>
#include <cstring>
#include <cwchar>
#include <mutex>
#include <new>
#include <unordered_set>
#include "bx_config.h"
#include "bx_log.h"
/* Track E (M1-E1): Offline GDK XGameRuntime shim (Lockhart/Anaconda aware). */
namespace {
using namespace bx::gdk;
// ABI values and layouts checked against Microsoft's public PC GDK 2510.5.6322.
constexpr HRESULT runtime_not_initialized = static_cast<HRESULT>(0x89240100u);
constexpr HRESULT runtime_invalid_handle = static_cast<HRESULT>(0x8924010cu);
constexpr HRESULT illegal_method_call = static_cast<HRESULT>(0x8000000eu);
constexpr HRESULT insufficient_buffer = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
struct AsyncBlock {
void* queue;
void* context;
void (CALLBACK* callback)(AsyncBlock*);
unsigned char internal[4 * sizeof(void*)];
};
struct RuntimeOptions {
uint32_t config_source;
const char* config;
};
struct GameLicense {
char store_sku_id[18];
bool active;
bool trial_owned_by_user;
bool disc;
bool trial;
uint32_t trial_seconds;
char trial_id[64];
int64_t expiration_date;
};
static_assert(offsetof(GameLicense, active) == 18);
static_assert(offsetof(GameLicense, expiration_date) == 96);
static_assert(sizeof(GameLicense) == 104);
struct SpaceInfo {
uint64_t available_free_bytes;
uint64_t total_free_bytes;
uint64_t used_bytes;
uint64_t total_bytes;
};
enum class Operation : uint32_t { add_user, game_license };
struct AsyncState {
const AsyncBlock* owner;
uint64_t sequence;
HRESULT status;
Operation operation;
uint32_t runtime_generation;
bool cancel_requested;
bool callback_pending;
bool result_consumed;
bool entitled;
};
static_assert(sizeof(AsyncState) == sizeof(AsyncBlock::internal));
std::mutex state_mutex;
std::condition_variable state_changed;
size_t runtime_refs = 0;
uint32_t runtime_generation = 0;
uint64_t next_operation = 1;
uintptr_t next_handle = 2;
std::unordered_set<void*> users;
std::unordered_set<void*> stores;
HRESULT create_handle(std::unordered_set<void*>& handles, void** out) {
auto handle = reinterpret_cast<void*>(next_handle);
next_handle += 2;
try {
handles.insert(handle);
} catch (const std::bad_alloc&) {
return E_OUTOFMEMORY;
}
*out = handle;
return S_OK;
}
AsyncState read_state(const AsyncBlock* block) {
AsyncState state{};
std::memcpy(&state, block->internal, sizeof(state));
return state;
}
struct StatusWaiter {
uint64_t sequence;
HRESULT status;
bool callback_pending;
StatusWaiter* next;
};
StatusWaiter* status_waiters = nullptr;
void write_state(AsyncBlock* block, const AsyncState& state) {
std::memcpy(block->internal, &state, sizeof(state));
for (auto* waiter = status_waiters; waiter; waiter = waiter->next) {
if (waiter->sequence == state.sequence) {
waiter->status = state.status;
waiter->callback_pending = state.callback_pending;
}
}
state_changed.notify_all();
}
class CompletionTask;
CompletionTask* running_callbacks = nullptr;
class CompletionTask final : public Task {
public:
AsyncBlock* block;
decltype(AsyncBlock::callback) callback;
uint64_t sequence = 0;
DWORD thread = 0;
CompletionTask* running_next = nullptr;
explicit CompletionTask(AsyncBlock* block) : block(block), callback(block->callback) {}
void run(const QueueRef&) override {
{
std::lock_guard lock(state_mutex);
auto state = read_state(block);
thread = GetCurrentThreadId();
running_next = running_callbacks;
running_callbacks = this;
state.callback_pending = false;
write_state(block, state);
}
callback(block);
// The callback may free or reuse the block. Only access our own task now.
std::lock_guard lock(state_mutex);
auto** link = &running_callbacks;
while (*link != this) link = &(*link)->running_next;
*link = running_next;
state_changed.notify_all();
}
};
bool fixture_entitlement();
bool callback_running(uint64_t sequence, DWORD caller);
class AsyncTask final : public Task {
public:
AsyncBlock* block;
Operation operation;
std::unique_ptr<CompletionTask> completion;
bool queued = false;
AsyncTask(AsyncBlock* block, Operation operation) : block(block), operation(operation) {}
~AsyncTask() override {
if (!queued) return;
// Submission lost the race with queue close. No callback was accepted.
std::lock_guard lock(state_mutex);
auto state = read_state(block);
state.status = E_HANDLE;
state.callback_pending = false;
write_state(block, state);
}
void run(const QueueRef& queue) override {
finish(queue, false);
}
void cancel(const QueueRef& queue) override {
finish(queue, true);
}
private:
void finish(const QueueRef& queue, bool canceled) {
const bool entitled = !canceled && operation == Operation::game_license && fixture_entitlement();
{
std::lock_guard lock(state_mutex);
auto state = read_state(block);
state.status = (canceled || state.cancel_requested) ? E_ABORT : S_OK;
state.entitled = entitled;
if (completion) completion->sequence = state.sequence;
queued = false;
write_state(block, state);
}
if (completion) submit(queue, Port::completion, std::move(completion), true);
}
};
HRESULT begin(AsyncBlock* block, Operation operation, void* store = nullptr) {
if (!block) return E_POINTER;
const auto queue = acquire_queue(block->queue);
if (!queue) return block->queue ? E_HANDLE : E_OUTOFMEMORY;
auto work = std::unique_ptr<AsyncTask>(new (std::nothrow) AsyncTask(block, operation));
if (!work) return E_OUTOFMEMORY;
if (block->callback) {
work->completion.reset(new (std::nothrow) CompletionTask(block));
if (!work->completion) return E_OUTOFMEMORY;
}
{
std::lock_guard lock(state_mutex);
if (!runtime_refs) return runtime_not_initialized;
if (operation == Operation::game_license && !stores.contains(store)) {
return runtime_invalid_handle;
}
const auto previous = read_state(block);
if (previous.owner == block &&
(previous.status == E_PENDING || previous.callback_pending ||
callback_running(previous.sequence, GetCurrentThreadId()))) {
return illegal_method_call;
}
write_state(block, {block, next_operation++, E_PENDING, operation, runtime_generation,
false, block->callback != nullptr, false, false});
work->queued = true;
}
return submit(queue, Port::work, std::move(work)) ? S_OK : E_HANDLE;
}
HRESULT check_result(AsyncBlock* block, Operation expected, AsyncState& state) {
if (!block) return E_POINTER;
state = read_state(block);
if (state.owner != block || state.operation != expected || state.result_consumed) {
return illegal_method_call;
}
if (!runtime_refs || state.runtime_generation != runtime_generation) return runtime_not_initialized;
return state.status;
}
bool callback_running(uint64_t sequence, DWORD caller) {
for (auto* callback = running_callbacks; callback; callback = callback->running_next) {
if (callback->sequence == sequence && callback->thread != caller) return true;
}
return false;
}
HRESULT copy_text(const char* text, size_t size, char* out, size_t* used) {
if (used) *used = 0;
if (!out) return E_POINTER;
const auto required = std::strlen(text) + 1;
if (size < required) return insufficient_buffer;
std::memcpy(out, text, required);
if (used) *used = required;
return S_OK;
}
constexpr wchar_t storage_root[] = L"C:\\bx_pls";
constexpr wchar_t fixture_name[] = L"test_xgameruntime.exe";
HRESULT executable_name(wchar_t (&module)[MAX_PATH], const wchar_t*& name) {
const auto size = GetModuleFileNameW(nullptr, module, MAX_PATH);
if (!size) return HRESULT_FROM_WIN32(GetLastError());
if (size == MAX_PATH) return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
const auto slash = std::wcsrchr(module, L'\\');
name = slash ? slash + 1 : module;
return S_OK;
}
bool fixture_entitlement() {
// The original conformance executable explicitly identifies its synthetic SKU.
wchar_t module[MAX_PATH];
const wchar_t* name = nullptr;
return SUCCEEDED(executable_name(module, name)) &&
std::wcscmp(name, fixture_name) == 0 &&
GetProcAddress(GetModuleHandleW(nullptr), "BetterXenSyntheticStoreFixture") != nullptr;
}
HRESULT ensure_directory(const wchar_t* path) {
if (CreateDirectoryW(path, nullptr)) return S_OK;
const auto error = GetLastError();
if (error != ERROR_ALREADY_EXISTS) return HRESULT_FROM_WIN32(error);
const auto attributes = GetFileAttributesW(path);
if (attributes == INVALID_FILE_ATTRIBUTES) return HRESULT_FROM_WIN32(GetLastError());
if ((attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) != FILE_ATTRIBUTE_DIRECTORY) {
return HRESULT_FROM_WIN32(ERROR_DIRECTORY);
}
return S_OK;
}
HRESULT storage_path(wchar_t (&path)[MAX_PATH]) {
wchar_t module[MAX_PATH];
const wchar_t* name = nullptr;
const auto hr = executable_name(module, name);
if (FAILED(hr)) return hr;
// Hash the full Windows path so identically named workloads in separate
// directories get distinct storage within the same bottle.
wchar_t normalized[MAX_PATH];
if (!LCMapStringEx(LOCALE_NAME_INVARIANT, LCMAP_LOWERCASE, module, -1,
normalized, MAX_PATH, nullptr, nullptr, 0)) {
return HRESULT_FROM_WIN32(GetLastError());
}
uint64_t path_id = 14695981039346656037ULL;
for (auto* ch = normalized; *ch; ++ch) {
path_id ^= static_cast<uint16_t>(*ch);
path_id *= 1099511628211ULL;
}
const auto length = std::swprintf(path, MAX_PATH, L"%ls\\%016llx", storage_root,
static_cast<unsigned long long>(path_id));
return length < 0 ? HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE) : S_OK;
}
HRESULT create_storage(const wchar_t* path) {
const auto hr = ensure_directory(storage_root);
return FAILED(hr) ? hr : ensure_directory(path);
}
} // namespace
extern "C" {
HRESULT WINAPI XGameRuntimeInitialize(void) {
bx_log_emit(BX_LOG_INFO, "xgameruntime", "XGameRuntimeInitialize", "ok");
HRESULT WINAPI XGameRuntimeInitialize() {
std::lock_guard lock(state_mutex);
if (!runtime_refs++) ++runtime_generation;
return S_OK;
}
HRESULT WINAPI XGameRuntimeInitializeWithOptions(const void* /*options*/) {
bx_log_emit(BX_LOG_INFO, "xgameruntime", "XGameRuntimeInitializeWithOptions", "ok");
return S_OK;
}
void WINAPI XGameRuntimeUninitialize(void) {
bx_log_emit(BX_LOG_INFO, "xgameruntime", "XGameRuntimeUninitialize", "ok");
}
HRESULT WINAPI XTaskQueueCreate(uint32_t /*work_mode*/, uint32_t /*completion_mode*/, void** out_queue) {
BX_STUB_LOG("xgameruntime.dll", "XTaskQueueCreate");
if (out_queue) *out_queue = reinterpret_cast<void*>(0x1001);
return S_OK;
}
void WINAPI XTaskQueueCloseHandle(void* /*queue*/) {}
BOOL WINAPI XTaskQueueDispatch(void* /*queue*/, uint32_t /*port*/, uint32_t /*timeout_ms*/) {
return FALSE;
}
HRESULT WINAPI XTaskQueueSubmitCallback(void* /*queue*/, uint32_t /*port*/, void* /*ctx*/, void* /*cb*/) {
BX_STUB_LOG("xgameruntime.dll", "XTaskQueueSubmitCallback");
return S_OK;
}
BOOL WINAPI XTaskQueueGetCurrentProcessTaskQueue(void** out_queue) {
if (out_queue) *out_queue = reinterpret_cast<void*>(0x1000);
return TRUE;
}
HRESULT WINAPI XAsyncGetStatus(void* /*async*/, BOOL /*wait*/) { return S_OK; }
HRESULT WINAPI XAsyncGetResultSize(void* /*async*/, size_t* out_size) {
if (out_size) *out_size = 0;
return S_OK;
}
void WINAPI XAsyncCancel(void* /*async*/) {}
HRESULT WINAPI XUserAddAsync(uint32_t /*opts*/, void* /*async*/) { BX_STUB_LOG("xgameruntime.dll", "XUserAddAsync"); return S_OK; }
HRESULT WINAPI XUserAddResult(void* /*async*/, void** out_user) { if (out_user) *out_user = reinterpret_cast<void*>(0x2001); return S_OK; }
void WINAPI XUserCloseHandle(void* /*user*/) {}
HRESULT WINAPI XUserGetId(void* /*user*/, uint64_t* out_id) { if (out_id) *out_id = 0x0009000000000001ULL; return S_OK; }
HRESULT WINAPI XUserGetGamertag(void* /*user*/, uint32_t /*comp*/, size_t size, char* out, size_t* used) {
static const char kTag[] = "BetterXenLocal";
size_t len = sizeof(kTag);
if (out && size > 0) {
size_t copy = len < size ? len : size - 1;
std::memcpy(out, kTag, copy);
out[copy] = '\0';
HRESULT WINAPI XGameRuntimeInitializeWithOptions(const RuntimeOptions* options) {
if (!options) return E_POINTER;
if (options->config_source > 2 || (options->config_source == 0) != (options->config == nullptr)) {
return E_INVALIDARG;
}
if (used) *used = len;
if (options->config_source != 0) {
BX_STUB_LOG("xgameruntime.dll", "XGameRuntimeInitializeWithOptions");
return E_NOTIMPL;
}
return XGameRuntimeInitialize();
}
void WINAPI XGameRuntimeUninitialize() {
std::lock_guard lock(state_mutex);
if (runtime_refs && !--runtime_refs) {
users.clear();
stores.clear();
}
}
HRESULT WINAPI XAsyncGetStatus(AsyncBlock* block, bool wait) {
if (!block) return E_POINTER;
std::unique_lock lock(state_mutex);
const auto state = read_state(block);
if (state.owner != block) return E_INVALIDARG;
if (!wait) return state.status;
// A callback can recycle the same block before this waiter wakes up.
StatusWaiter waiter{state.sequence, state.status, state.callback_pending, status_waiters};
status_waiters = &waiter;
const auto caller = GetCurrentThreadId();
state_changed.wait(lock, [&] {
return waiter.status != E_PENDING && !waiter.callback_pending &&
!callback_running(waiter.sequence, caller);
});
auto** link = &status_waiters;
while (*link != &waiter) link = &(*link)->next;
*link = waiter.next;
return waiter.status;
}
HRESULT WINAPI XAsyncGetResultSize(AsyncBlock* block, size_t* size) {
if (!size) return E_POINTER;
*size = 0;
if (!block) return E_POINTER;
std::lock_guard lock(state_mutex);
const auto state = read_state(block);
if (state.owner != block || state.result_consumed) return illegal_method_call;
if (SUCCEEDED(state.status)) {
*size = state.operation == Operation::add_user ? sizeof(void*) : sizeof(GameLicense);
}
return state.status;
}
void WINAPI XAsyncCancel(AsyncBlock* block) {
if (!block) return;
std::lock_guard lock(state_mutex);
auto state = read_state(block);
if (state.owner == block && state.status == E_PENDING) {
state.cancel_requested = true;
write_state(block, state);
}
}
HRESULT WINAPI XUserAddAsync(uint32_t options, AsyncBlock* block) {
if (options & ~7u) return E_INVALIDARG;
return begin(block, Operation::add_user);
}
HRESULT WINAPI XUserAddResult(AsyncBlock* block, void** out) {
if (!out) return E_POINTER;
*out = nullptr;
std::lock_guard lock(state_mutex);
AsyncState state{};
const auto hr = check_result(block, Operation::add_user, state);
if (FAILED(hr)) return hr;
const auto result = create_handle(users, out);
if (SUCCEEDED(result)) {
state.result_consumed = true;
write_state(block, state);
}
return result;
}
void WINAPI XUserCloseHandle(void* user) {
std::lock_guard lock(state_mutex);
users.erase(user);
}
HRESULT WINAPI XUserGetId(void* user, uint64_t* id) {
if (!id) return E_POINTER;
std::lock_guard lock(state_mutex);
if (!users.contains(user)) return runtime_invalid_handle;
*id = 0x0009000000000001ULL;
return S_OK;
}
HRESULT WINAPI XUserGetTokenAndSignatureAsync(void*, uint32_t, const char*, const char*, size_t, const void*, size_t, const void*, void*) { BX_STUB_LOG("xgameruntime.dll", "XUserGetTokenAndSignatureAsync"); return E_NOTIMPL; }
HRESULT WINAPI XUserGetTokenAndSignatureResultSize(void*, size_t* s) { if (s) *s = 0; return E_NOTIMPL; }
HRESULT WINAPI XUserGetTokenAndSignatureResult(void*, size_t, void*, void**, size_t*) { return E_NOTIMPL; }
HRESULT WINAPI XUserResolvePrivilegeWithUiAsync(void*, uint32_t, uint32_t, void*) { return S_OK; }
HRESULT WINAPI XUserResolvePrivilegeWithUiResult(void*) { return S_OK; }
HRESULT WINAPI XStoreCreateContext(void* /*user*/, void** out_ctx) { if (out_ctx) *out_ctx = reinterpret_cast<void*>(0x3001); return S_OK; }
void WINAPI XStoreCloseContextHandle(void* /*ctx*/) {}
HRESULT WINAPI XStoreQueryGameLicenseAsync(void*, void*) { return S_OK; }
HRESULT WINAPI XStoreQueryGameLicenseResult(void*, void* out_lic) { if (out_lic) std::memset(out_lic, 0, 64); return S_OK; }
HRESULT WINAPI XStoreAcquireLicenseForPackageAsync(void*, const char*, void*) { return S_OK; }
HRESULT WINAPI XStoreAcquireLicenseForPackageResult(void*, void** out_lic) { if (out_lic) *out_lic = reinterpret_cast<void*>(0x3002); return S_OK; }
HRESULT WINAPI XStoreAcquireLicenseForDurablesAsync(void*, const char*, void*) { return S_OK; }
HRESULT WINAPI XStoreAcquireLicenseForDurablesResult(void*, void** out_lic) { if (out_lic) *out_lic = reinterpret_cast<void*>(0x3003); return S_OK; }
HRESULT WINAPI XStoreQueryEntitledProductsAsync(void*, uint32_t, void*) { return S_OK; }
HRESULT WINAPI XStoreQueryEntitledProductsResult(void*, void** out_q) { if (out_q) *out_q = nullptr; return S_OK; }
HRESULT WINAPI XStoreQueryAssociatedProductsAsync(void*, uint32_t, void*) { return S_OK; }
HRESULT WINAPI XStoreQueryAssociatedProductsResult(void*, void** out_q) { if (out_q) *out_q = nullptr; return S_OK; }
HRESULT WINAPI XUserGetGamertag(void* user, uint32_t component, size_t size, char* out, size_t* used) {
std::lock_guard lock(state_mutex);
if (!users.contains(user)) return runtime_invalid_handle;
if (component > 3) return E_INVALIDARG;
return copy_text(component == 2 ? "" : "BetterXenLocal", size, out, used);
}
HRESULT WINAPI XStoreCreateContext(void* user, void** out) {
if (!out) return E_POINTER;
*out = nullptr;
std::lock_guard lock(state_mutex);
if (!runtime_refs) return runtime_not_initialized;
if (user && !users.contains(user)) return runtime_invalid_handle;
return create_handle(stores, out);
}
void WINAPI XStoreCloseContextHandle(void* store) {
std::lock_guard lock(state_mutex);
stores.erase(store);
}
HRESULT WINAPI XStoreQueryGameLicenseAsync(void* store, AsyncBlock* block) {
return begin(block, Operation::game_license, store);
}
HRESULT WINAPI XStoreQueryGameLicenseResult(AsyncBlock* block, GameLicense* out) {
if (!out) return E_POINTER;
std::lock_guard lock(state_mutex);
AsyncState state{};
const auto hr = check_result(block, Operation::game_license, state);
if (FAILED(hr)) return hr;
std::memset(out, 0, sizeof(*out));
if (state.entitled) {
constexpr char sku[] = "BXTEST000001/0001";
static_assert(sizeof(sku) <= sizeof(out->store_sku_id));
std::memcpy(out->store_sku_id, sku, sizeof(sku));
out->active = true;
out->expiration_date = INT64_MAX;
}
state.result_consumed = true;
write_state(block, state);
return S_OK;
}
HRESULT WINAPI XSystemGetConsoleId(size_t size, char* out, size_t* used) {
static const char kId[] = "00000000.00000000.00000000.00000000.00";
if (out && size > 0) {
size_t copy = sizeof(kId) < size ? sizeof(kId) : size - 1;
std::memcpy(out, kId, copy);
out[copy] = '\0';
}
if (used) *used = sizeof(kId);
constexpr char id[39] = "00000000.00000000.00000000.00000000.00";
if (used) *used = 0;
if (!out) return E_POINTER;
if (size < sizeof(id)) return insufficient_buffer;
std::memcpy(out, id, sizeof(id));
if (used) *used = sizeof(id);
return S_OK;
}
uint32_t WINAPI XSystemGetDeviceType(void) {
const bx_runtime_config_t* cfg = bx_get_runtime_config();
/* Public GDK XSystemDeviceType enum: XboxScarlettLockhart = 6, XboxScarlettAnaconda = 7. */
return (cfg && cfg->profile == BX_PROFILE_ANACONDA) ? 7u : 6u;
uint32_t WINAPI XSystemGetDeviceType() {
const auto* config = bx_get_runtime_config();
return config && config->profile == BX_PROFILE_ANACONDA ? 7u : 6u;
}
HRESULT WINAPI XSystemGetXboxLiveSandboxId(size_t size, char* out, size_t* used) {
static const char kSb[] = "RETAIL";
if (out && size > 0) {
size_t copy = sizeof(kSb) < size ? sizeof(kSb) : size - 1;
std::memcpy(out, kSb, copy);
out[copy] = '\0';
}
if (used) *used = sizeof(kSb);
return copy_text("RETAIL", size, out, used);
}
HRESULT WINAPI XPersistentLocalStorageGetPathSize(size_t* size) {
if (!size) return E_POINTER;
*size = 0;
wchar_t path[MAX_PATH];
const auto hr = storage_path(path);
if (FAILED(hr)) return hr;
const auto bytes = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, path, -1, nullptr, 0, nullptr, nullptr);
if (!bytes) return HRESULT_FROM_WIN32(GetLastError());
*size = static_cast<size_t>(bytes);
return S_OK;
}
HRESULT WINAPI XPersistentLocalStorageGetPathSize(size_t* out_size) {
if (out_size) *out_size = 16;
return S_OK;
}
HRESULT WINAPI XPersistentLocalStorageGetPath(size_t size, char* out, size_t* used) {
static const char kPath[] = "C:\\bx_pls";
if (out && size > 0) {
size_t copy = sizeof(kPath) < size ? sizeof(kPath) : size - 1;
std::memcpy(out, kPath, copy);
out[copy] = '\0';
}
if (used) *used = sizeof(kPath);
return S_OK;
}
HRESULT WINAPI XPersistentLocalStorageGetSpaceInfo(void* out_info) {
if (out_info) std::memset(out_info, 0, 32);
if (used) *used = 0;
if (!out) return E_POINTER;
size_t required;
auto hr = XPersistentLocalStorageGetPathSize(&required);
if (FAILED(hr)) return hr;
if (size < required) return insufficient_buffer;
wchar_t path[MAX_PATH];
hr = storage_path(path);
if (FAILED(hr)) return hr;
hr = create_storage(path);
if (FAILED(hr)) return hr;
const auto bytes = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, path, -1, out,
static_cast<int>(required), nullptr, nullptr);
if (!bytes) return HRESULT_FROM_WIN32(GetLastError());
if (used) *used = static_cast<size_t>(bytes);
return S_OK;
}
HRESULT WINAPI XGameUiShowPlayerProfileCardAsync(void*, uint64_t, void*) { return E_NOTIMPL; }
HRESULT WINAPI XGameUiShowTextEntryAsync(void*, const char*, const char*, const char*, uint32_t, uint32_t, void*) { return E_NOTIMPL; }
HRESULT WINAPI XNetworkingQueryPreferredLocalUdpMultiplayerPortAsync(void*) { return E_NOTIMPL; }
HRESULT WINAPI XNetworkingQuerySecurityInformationForUrlAsync(const char*, void*) { return E_NOTIMPL; }
HRESULT WINAPI XPersistentLocalStorageGetSpaceInfo(SpaceInfo* out) {
if (!out) return E_POINTER;
wchar_t path[MAX_PATH];
auto hr = storage_path(path);
if (FAILED(hr)) return hr;
hr = create_storage(path);
if (FAILED(hr)) return hr;
ULARGE_INTEGER available, total, free;
if (!GetDiskFreeSpaceExW(path, &available, &total, &free)) {
return HRESULT_FROM_WIN32(GetLastError());
}
// The bottle shares its volume with other data and has no title storage quota.
*out = {available.QuadPart, free.QuadPart,
total.QuadPart >= free.QuadPart ? total.QuadPart - free.QuadPart : 0, total.QuadPart};
return S_OK;
}
#define OFFLINE_STUB(symbol, args) \
HRESULT WINAPI symbol args { BX_STUB_LOG("xgameruntime.dll", #symbol); return E_NOTIMPL; }
OFFLINE_STUB(XUserGetTokenAndSignatureAsync, (void*, uint32_t, const char*, const char*, size_t, const void*, size_t, const void*, AsyncBlock*))
OFFLINE_STUB(XUserGetTokenAndSignatureResultSize, (AsyncBlock*, size_t*))
OFFLINE_STUB(XUserGetTokenAndSignatureResult, (AsyncBlock*, size_t, void*, void**, size_t*))
OFFLINE_STUB(XUserResolvePrivilegeWithUiAsync, (void*, uint32_t, uint32_t, AsyncBlock*))
OFFLINE_STUB(XUserResolvePrivilegeWithUiResult, (AsyncBlock*))
OFFLINE_STUB(XStoreAcquireLicenseForPackageAsync, (void*, const char*, AsyncBlock*))
OFFLINE_STUB(XStoreAcquireLicenseForPackageResult, (AsyncBlock*, void**))
OFFLINE_STUB(XStoreAcquireLicenseForDurablesAsync, (void*, const char*, AsyncBlock*))
OFFLINE_STUB(XStoreAcquireLicenseForDurablesResult, (AsyncBlock*, void**))
OFFLINE_STUB(XStoreQueryEntitledProductsAsync, (void*, uint32_t, uint32_t, AsyncBlock*))
OFFLINE_STUB(XStoreQueryEntitledProductsResult, (AsyncBlock*, void**))
OFFLINE_STUB(XStoreQueryAssociatedProductsAsync, (void*, uint32_t, uint32_t, AsyncBlock*))
OFFLINE_STUB(XStoreQueryAssociatedProductsResult, (AsyncBlock*, void**))
OFFLINE_STUB(XGameUiShowPlayerProfileCardAsync, (void*, uint64_t, AsyncBlock*))
OFFLINE_STUB(XGameUiShowTextEntryAsync, (const char*, const char*, const char*, uint32_t, uint32_t, AsyncBlock*))
OFFLINE_STUB(XNetworkingQueryPreferredLocalUdpMultiplayerPortAsync, (AsyncBlock*))
OFFLINE_STUB(XNetworkingQuerySecurityInformationForUrlAsync, (const char*, AsyncBlock*))
#undef OFFLINE_STUB
} // extern "C"
@@ -0,0 +1,11 @@
add_executable(test_xgameruntime main.cpp)
target_link_libraries(test_xgameruntime PRIVATE xgameruntime kernel32)
# Keep the same executable name to exercise the marker check and storage isolation.
add_executable(test_xgameruntime_unentitled main.cpp)
target_compile_definitions(test_xgameruntime_unentitled PRIVATE BX_TEST_UNENTITLED)
target_link_libraries(test_xgameruntime_unentitled PRIVATE xgameruntime kernel32)
set_target_properties(test_xgameruntime_unentitled PROPERTIES
OUTPUT_NAME test_xgameruntime
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unentitled"
)
@@ -0,0 +1,283 @@
#include <windows.h>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace {
struct Async {
void* queue;
void* context;
void (CALLBACK* callback)(Async*);
unsigned char reserved[32];
};
struct GameLicense {
char sku[18];
bool active, trial_owner, disc, trial;
uint32_t trial_remaining;
char trial_id[64];
int64_t expiration;
};
struct RuntimeOptions {
uint32_t source;
const char* config;
};
struct SpaceInfo {
uint64_t available, free, used, total;
};
static_assert(sizeof(GameLicense) == 104);
static_assert(offsetof(GameLicense, active) == 18);
extern "C" {
HRESULT WINAPI XGameRuntimeInitialize();
HRESULT WINAPI XGameRuntimeInitializeWithOptions(const RuntimeOptions*);
void WINAPI XGameRuntimeUninitialize();
HRESULT WINAPI XTaskQueueCreate(uint32_t, uint32_t, void**);
void WINAPI XTaskQueueCloseHandle(void*);
bool WINAPI XTaskQueueDispatch(void*, uint32_t, uint32_t);
HRESULT WINAPI XTaskQueueSubmitCallback(void*, uint32_t, void*, void (CALLBACK*)(void*, bool));
bool WINAPI XTaskQueueGetCurrentProcessTaskQueue(void**);
HRESULT WINAPI XUserAddAsync(uint32_t, Async*);
HRESULT WINAPI XUserAddResult(Async*, void**);
void WINAPI XUserCloseHandle(void*);
HRESULT WINAPI XUserGetId(void*, uint64_t*);
HRESULT WINAPI XUserGetGamertag(void*, uint32_t, size_t, char*, size_t*);
HRESULT WINAPI XStoreCreateContext(void*, void**);
void WINAPI XStoreCloseContextHandle(void*);
HRESULT WINAPI XStoreQueryGameLicenseAsync(void*, Async*);
HRESULT WINAPI XStoreQueryGameLicenseResult(Async*, GameLicense*);
HRESULT WINAPI XAsyncGetStatus(Async*, bool);
HRESULT WINAPI XAsyncGetResultSize(Async*, size_t*);
void WINAPI XAsyncCancel(Async*);
uint32_t WINAPI XSystemGetDeviceType();
HRESULT WINAPI XSystemGetConsoleId(size_t, char*, size_t*);
HRESULT WINAPI XPersistentLocalStorageGetPathSize(size_t*);
HRESULT WINAPI XPersistentLocalStorageGetPath(size_t, char*, size_t*);
HRESULT WINAPI XPersistentLocalStorageGetSpaceInfo(SpaceInfo*);
}
#define CHECK(expr) do { \
if (!(expr)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #expr); std::exit(1); } \
} while (false)
constexpr uint32_t manual = 0, pool = 1, serialized = 2, immediate = 3;
constexpr uint32_t work = 0, completion = 1;
constexpr DWORD timeout_ms = 5000;
struct Completion {
unsigned count = 0;
DWORD thread = 0;
HRESULT status = E_PENDING;
};
void CALLBACK completed(Async* async) {
auto* result = static_cast<Completion*>(async->context);
++result->count;
result->thread = GetCurrentThreadId();
result->status = XAsyncGetStatus(async, true);
}
void CALLBACK record_thread(void* context, bool canceled) {
CHECK(!canceled);
*static_cast<DWORD*>(context) = GetCurrentThreadId();
}
void CALLBACK record_canceled(void* context, bool canceled) {
CHECK(canceled);
++*static_cast<unsigned*>(context);
}
struct SerialCallbacks {
std::atomic<unsigned> count{0};
HANDLE done;
};
void CALLBACK serial_callback(void* context, bool canceled) {
CHECK(!canceled);
auto& state = *static_cast<SerialCallbacks*>(context);
const unsigned order = state.count.load();
Sleep(10);
CHECK(state.count.fetch_add(1) == order);
if (order == 7) SetEvent(state.done);
}
void check_storage() {
size_t required = 0, used = 0;
char path[MAX_PATH * 4]{};
CHECK(XPersistentLocalStorageGetPathSize(&required) == S_OK);
CHECK(required > 1 && required <= sizeof(path));
CHECK(FAILED(XPersistentLocalStorageGetPath(required - 1, path, nullptr)));
CHECK(XPersistentLocalStorageGetPath(sizeof(path), path, &used) == S_OK);
CHECK(used == required && std::strlen(path) + 1 == required);
CHECK(std::strncmp(path, "C:\\bx_pls\\", 10) == 0);
const auto attributes = GetFileAttributesA(path);
CHECK(attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY));
char file[MAX_PATH * 4 + 16];
std::snprintf(file, sizeof(file), "%s\\write-check", path);
auto handle = CreateFileA(file, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
CHECK(handle != INVALID_HANDLE_VALUE);
DWORD bytes;
CHECK(WriteFile(handle, "BX", 2, &bytes, nullptr) && bytes == 2);
CHECK(CloseHandle(handle) && DeleteFileA(file));
SpaceInfo space{};
CHECK(XPersistentLocalStorageGetSpaceInfo(&space) == S_OK);
CHECK(space.total > 0 && space.available <= space.free && space.free <= space.total);
std::printf("storage path: %s\n", path);
}
} // namespace
#ifndef BX_TEST_UNENTITLED
extern "C" __declspec(dllexport) void BetterXenSyntheticStoreFixture() {}
#endif
int main() {
char profile[16]{};
CHECK(GetEnvironmentVariableA("BX_PROFILE", profile, sizeof(profile)) > 0);
const bool anaconda = std::strcmp(profile, "anaconda") == 0;
CHECK(anaconda || std::strcmp(profile, "lockhart") == 0);
CHECK(XSystemGetDeviceType() == (anaconda ? 7u : 6u));
#ifdef BX_TEST_UNENTITLED
constexpr bool entitled = false;
#else
constexpr bool entitled = true;
#endif
Async uninitialized{};
CHECK(FAILED(XUserAddAsync(1, &uninitialized)));
const RuntimeOptions options{};
CHECK(XGameRuntimeInitializeWithOptions(&options) == S_OK);
CHECK(XGameRuntimeInitialize() == S_OK);
XGameRuntimeUninitialize();
void* process_queue = nullptr;
CHECK(XTaskQueueGetCurrentProcessTaskQueue(&process_queue) && process_queue);
XTaskQueueCloseHandle(process_queue);
void* queue = nullptr;
CHECK(XTaskQueueCreate(manual, manual, &queue) == S_OK && queue);
const auto main_thread = GetCurrentThreadId();
DWORD task_thread = 0;
CHECK(XTaskQueueSubmitCallback(queue, work, &task_thread, record_thread) == S_OK);
CHECK(task_thread == 0 && XTaskQueueDispatch(queue, work, 0));
CHECK(task_thread == main_thread && !XTaskQueueDispatch(queue, work, 0));
Completion result{};
Async async{queue, &result, completed, {}};
CHECK(XUserAddAsync(1, &async) == S_OK);
CHECK(XAsyncGetStatus(&async, false) == E_PENDING);
CHECK(FAILED(XUserAddAsync(1, &async)));
size_t size = 0;
CHECK(XAsyncGetResultSize(&async, &size) == E_PENDING);
CHECK(XTaskQueueDispatch(queue, work, 0) && result.count == 0);
CHECK(XAsyncGetResultSize(&async, &size) == S_OK && size == sizeof(void*));
CHECK(XTaskQueueDispatch(queue, completion, 0));
CHECK(result.count == 1 && result.status == S_OK && result.thread == main_thread);
void* user = nullptr;
CHECK(XUserAddResult(&async, &user) == S_OK && user);
void* duplicate = nullptr;
CHECK(FAILED(XUserAddResult(&async, &duplicate)) && !duplicate);
uint64_t id = 0;
char tag[16]{};
CHECK(XUserGetId(user, &id) == S_OK && id == 0x0009000000000001ULL);
CHECK(FAILED(XUserGetGamertag(user, 0, 1, tag, nullptr)));
CHECK(XUserGetGamertag(user, 0, sizeof(tag), tag, &size) == S_OK);
CHECK(std::strcmp(tag, "BetterXenLocal") == 0 && size == sizeof("BetterXenLocal"));
void* store = nullptr;
CHECK(XStoreCreateContext(user, &store) == S_OK && store);
XUserCloseHandle(user);
CHECK(FAILED(XUserGetId(user, &id)));
CHECK(XStoreQueryGameLicenseAsync(store, &async) == S_OK);
XStoreCloseContextHandle(store);
CHECK(FAILED(XStoreQueryGameLicenseAsync(store, &uninitialized)));
CHECK(XTaskQueueDispatch(queue, work, 0));
CHECK(XTaskQueueDispatch(queue, completion, 0) && result.count == 2);
CHECK(XAsyncGetResultSize(&async, &size) == S_OK && size == sizeof(GameLicense));
struct {
GameLicense license;
uint64_t canary = 0x123456789abcdef0ULL;
} payload;
CHECK(XStoreQueryGameLicenseResult(&async, &payload.license) == S_OK);
CHECK(payload.canary == 0x123456789abcdef0ULL);
CHECK(payload.license.active == entitled && !payload.license.trial && !payload.license.disc);
if (entitled) CHECK(std::strcmp(payload.license.sku, "BXTEST000001/0001") == 0);
CHECK(XUserAddAsync(0, &async) == S_OK);
XAsyncCancel(&async);
CHECK(XTaskQueueDispatch(queue, work, 0));
CHECK(XTaskQueueDispatch(queue, completion, 0) && result.count == 3);
CHECK(result.status == E_ABORT && XAsyncGetStatus(&async, true) == E_ABORT);
CHECK(XUserAddResult(&async, &user) == E_ABORT && !user);
CHECK(!XTaskQueueDispatch(queue, completion, 0));
CHECK(XUserAddAsync(0, &async) == S_OK);
unsigned dropped = 0;
CHECK(XTaskQueueSubmitCallback(queue, work, &dropped, record_canceled) == S_OK);
CHECK(XTaskQueueSubmitCallback(queue, completion, &dropped, record_canceled) == S_OK);
XTaskQueueCloseHandle(queue);
CHECK(dropped == 2 && result.count == 4 && result.status == E_ABORT);
CHECK(XAsyncGetStatus(&async, true) == E_ABORT);
CHECK(!XTaskQueueDispatch(queue, completion, 0));
CHECK(XUserAddAsync(0, &async) == E_HANDLE && result.count == 4);
CHECK(XTaskQueueCreate(manual, manual, &queue) == S_OK);
async = {queue, &result, completed, {}};
CHECK(XUserAddAsync(0, &async) == S_OK);
CHECK(XTaskQueueDispatch(queue, work, 0) && result.count == 4);
XTaskQueueCloseHandle(queue);
CHECK(result.count == 5 && result.status == S_OK);
CHECK(XUserAddResult(&async, &user) == S_OK);
XUserCloseHandle(user);
CHECK(XTaskQueueCreate(pool, manual, &queue) == S_OK);
async = {queue, &result, completed, {}};
CHECK(XUserAddAsync(0, &async) == S_OK);
CHECK(XTaskQueueDispatch(queue, completion, timeout_ms) && result.count == 6);
CHECK(XUserAddResult(&async, &user) == S_OK);
XUserCloseHandle(user);
XTaskQueueCloseHandle(queue);
async = {nullptr, &result, completed, {}};
CHECK(XUserAddAsync(0, &async) == S_OK);
CHECK(XAsyncGetStatus(&async, true) == S_OK);
CHECK(result.count == 7 && result.thread != main_thread);
CHECK(XUserAddResult(&async, &user) == S_OK);
XUserCloseHandle(user);
CHECK(XTaskQueueCreate(serialized, immediate, &queue) == S_OK);
task_thread = 0;
CHECK(XTaskQueueSubmitCallback(queue, completion, &task_thread, record_thread) == S_OK);
CHECK(task_thread == main_thread);
CHECK(!XTaskQueueDispatch(queue, completion, INFINITE));
SerialCallbacks serial{0, CreateEventW(nullptr, TRUE, FALSE, nullptr)};
CHECK(serial.done);
for (unsigned i = 0; i < 8; ++i) {
CHECK(XTaskQueueSubmitCallback(queue, work, &serial, serial_callback) == S_OK);
}
CHECK(WaitForSingleObject(serial.done, timeout_ms) == WAIT_OBJECT_0 && serial.count == 8);
CloseHandle(serial.done);
XTaskQueueCloseHandle(queue);
char console_id[39];
CHECK(XSystemGetConsoleId(sizeof(console_id), console_id, &size) == S_OK);
CHECK(size == sizeof(console_id) && console_id[38] == '\0');
CHECK(std::strcmp(console_id, "00000000.00000000.00000000.00000000.00") == 0);
CHECK(FAILED(XSystemGetConsoleId(sizeof(console_id) - 1, console_id, nullptr)));
check_storage();
XGameRuntimeUninitialize();
CHECK(FAILED(XUserAddAsync(1, &uninitialized)));
std::printf("xgameruntime conformance passed (%s, synthetic license %s)\n",
profile, entitled ? "active" : "inactive");
return 0;
}