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
82 changes: 71 additions & 11 deletions runtime/shims/xgameruntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,12 +1338,31 @@ bool STDMETHODCALLTYPE pkg_unregister_progress(void*, void* monitor, XTaskQueueR
bool STDMETHODCALLTYPE pkg_unregister_installed(void*, XTaskQueueRegistrationToken, bool) { return true; }
bool STDMETHODCALLTYPE pkg_uninstall_package(void*, const char*) { return false; }

// Slot 39 (IXPackageImpl3): HRESULT XPackageEnumeratePackages(XPackageKind kind, XPackageEnumerationScope scope,
// void* context, XPackageEnumerationCallback* callback), the callback a `bool CALLBACK (void* context, const
// XPackageDetails* details)` that is called once per package. The title enumerates content packages (kind 1) and the
// offline install has none to list, so this is S_OK without a callback, as VIctory's xpackage.rs answers the same slot
// (0x138) for this title. Shortcut: a console would also list this installation for kind 0 (Game); no caller seen asks
// for it. A content package LEONIDA_CONTENT_PACKAGES maps is mounted by its identifier (slots 23, 37), not listed here.
// Slots 29/36/40 (IXPackageImpl / IXPackageImpl2 / IXPackageImpl3): HRESULT XPackageRegisterPackageInstalled(
// XTaskQueueHandle queue, void* context, XPackageInstalledCallback* callback, XTaskQueueRegistrationToken* token).
// Nothing installs offline, so the callback never fires; returns a fresh non-zero token accepted by slot 22.
HRESULT STDMETHODCALLTYPE pkg_register_installed(void*, XTaskQueueHandle, void*, void* callback,
XTaskQueueRegistrationToken* token) {
HRESULT hr = S_OK;
if (!token) hr = E_POINTER;
else if (!callback) hr = E_INVALIDARG;
if (token) token->token = SUCCEEDED(hr) ? g_progress_tokens.fetch_add(1) + 1 : 0;
static std::atomic<bool> s_logged{false};
if (!s_logged.exchange(true))
std::fprintf(stderr, "[rt] xgameruntime: XPackage RegisterPackageInstalled first call -> %s (0x%08lx), "
"token %llu, callback never called (offline)\n",
hr_name(hr), static_cast<unsigned long>(hr),
static_cast<unsigned long long>(token ? token->token : 0));
return hr;
}

// Slots 28/35/39 (IXPackageImpl / IXPackageImpl2 / IXPackageImpl3): HRESULT XPackageEnumeratePackages(
// XPackageKind kind, XPackageEnumerationScope scope, void* context, XPackageEnumerationCallback* callback),
// the callback a `bool CALLBACK (void* context, const XPackageDetails* details)` that is called once per package.
// The title enumerates content packages (kind 1) and the offline install has none to list, so this is S_OK without a
// callback, as VIctory's xpackage.rs answers the same slot (0x138) for this title. Shortcut: a console would also list
// this installation for kind 0 (Game); no caller seen asks for it. A content package LEONIDA_CONTENT_PACKAGES maps is
// mounted by its identifier (slots 23, 37), not listed here.
HRESULT STDMETHODCALLTYPE pkg_enumerate_packages(void*, uint32_t kind, uint32_t scope, void* context, void* callback) {
const HRESULT hr = callback ? S_OK : E_INVALIDARG;
static std::atomic<bool> s_logged{false};
Expand Down Expand Up @@ -1584,15 +1603,18 @@ const void* const kPackageVtable[] = {
reinterpret_cast<const void*>(&pkg_mount_path_size), // 24 XPackageGetMountPathSize
reinterpret_cast<const void*>(&pkg_mount_path), // 25 XPackageGetMountPath
reinterpret_cast<const void*>(&pkg_close_mount), // 26 XPackageCloseMountHandle
PN(27), PN(28), PN(29), PN(30), PN(31), // 27 _unknown_, 28 EnumeratePackages, 29 RegisterPackageInstalled,
// 30 GetWriteStats, 31 _unknown_
PN(27), // 27 _unknown_
reinterpret_cast<const void*>(&pkg_enumerate_packages), // 28 XPackageEnumeratePackages (IXPackageImpl)
reinterpret_cast<const void*>(&pkg_register_installed), // 29 XPackageRegisterPackageInstalled (IXPackageImpl)
PN(30), PN(31), // 30 GetWriteStats, 31 _unknown_
PN(32), PN(33), // 32 UninstallUWPInstance, 33 EnumerateFeatures
reinterpret_cast<const void*>(&pkg_uninstall_package), // 34 XPackageUninstallPackage (bool)
PN(35), PN(36), // IXPackageImpl2: EnumeratePackages, RegisterPackageInstalled,
reinterpret_cast<const void*>(&pkg_enumerate_packages), // 35 XPackageEnumeratePackages (IXPackageImpl2)
reinterpret_cast<const void*>(&pkg_register_installed), // 36 XPackageRegisterPackageInstalled (IXPackageImpl2)
reinterpret_cast<const void*>(&pkg_mount_with_ui_async), // 37 XPackageMountWithUiAsync
reinterpret_cast<const void*>(&pkg_mount_with_ui_result), // 38 XPackageMountWithUiResult
reinterpret_cast<const void*>(&pkg_enumerate_packages), // 39 XPackageEnumeratePackages (IXPackageImpl3)
PN(40), // 40 RegisterPackageInstalled (IXPackageImpl3)
reinterpret_cast<const void*>(&pkg_enumerate_packages), // 39 XPackageEnumeratePackages (IXPackageImpl3)
reinterpret_cast<const void*>(&pkg_register_installed), // 40 XPackageRegisterPackageInstalled (IXPackageImpl3)
PN(41), // IXPackageImpl4: XPackageGetPackageKind
};
#undef PN
Expand All @@ -1604,6 +1626,35 @@ RuntimeObject& package_object() {
return object;
}

// Flat C XPackage exports forwarding to the offline COM implementation above.
HRESULT __stdcall XPackageEnumeratePackages(uint32_t kind, uint32_t scope, void* context, void* callback) {
return pkg_enumerate_packages(nullptr, kind, scope, context, callback);
}
HRESULT __stdcall XPackageCreateInstallationMonitor(const char* id, uint32_t count, const ChunkSelector* selectors,
uint32_t interval_ms, XTaskQueueHandle queue, void** monitor) {
return pkg_create_monitor(nullptr, id, count, selectors, interval_ms, queue, monitor);
}
void __stdcall XPackageCloseInstallationMonitorHandle(void* handle) { pkg_close_monitor(nullptr, handle); }
void __stdcall XPackageGetInstallationProgress(void* monitor, InstallationProgress* progress) {
pkg_installation_progress(nullptr, monitor, progress);
}
bool __stdcall XPackageUpdateInstallationMonitor(void* monitor) { return pkg_update_monitor(nullptr, monitor); }
HRESULT __stdcall XPackageRegisterInstallationProgressChanged(void* monitor, void* context, void* callback,
XTaskQueueRegistrationToken* token) {
return pkg_register_progress(nullptr, monitor, context, callback, token);
}
bool __stdcall XPackageUnregisterInstallationProgressChanged(void* monitor, XTaskQueueRegistrationToken token,
bool wait) {
return pkg_unregister_progress(nullptr, monitor, token, wait);
}
HRESULT __stdcall XPackageRegisterPackageInstalled(XTaskQueueHandle queue, void* context, void* callback,
XTaskQueueRegistrationToken* token) {
return pkg_register_installed(nullptr, queue, context, callback, token);
}
bool __stdcall XPackageUnregisterPackageInstalled(XTaskQueueRegistrationToken token, bool wait) {
return pkg_unregister_installed(nullptr, token, wait);
}

// ---- XGameImpl (asked for as IXGameImpl3), offline ----
//
// IXGameImpl3 : IXGameImpl2 : IXGameImpl (xgameruntime include/xgame.idl); the class id is IXGameImpl's. Three
Expand Down Expand Up @@ -1820,6 +1871,15 @@ SHIM_TABLE(xgameruntime,
XGR(XPackageGetMountPathSize),
XGR(XPackageGetMountPath),
XGR(XPackageIsPackagedProcess),
XGR(XPackageEnumeratePackages),
XGR(XPackageCreateInstallationMonitor),
XGR(XPackageCloseInstallationMonitorHandle),
XGR(XPackageGetInstallationProgress),
XGR(XPackageUpdateInstallationMonitor),
XGR(XPackageRegisterInstallationProgressChanged),
XGR(XPackageUnregisterInstallationProgressChanged),
XGR(XPackageRegisterPackageInstalled),
XGR(XPackageUnregisterPackageInstalled),
// XTaskQueue (third_party/xtaskqueue)
XGR(XTaskQueueCreate),
XGR(XTaskQueueCreateComposite),
Expand Down
93 changes: 83 additions & 10 deletions runtime/test/xgameruntime_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,27 +822,100 @@ void check_run_time_load() {
"slot 26 (XPackageCloseMountHandle) closes the handle");
SetEnvironmentVariableA("LEONIDA_CONTENT_PACKAGES", nullptr);
using Stub = HRESULT(STDMETHODCALLTYPE*)(void*);
check(reinterpret_cast<Stub>(slot(pk, 20))(pk) == E_NOTIMPL && reinterpret_cast<Stub>(slot(pk, 40))(pk) == E_NOTIMPL &&
check(reinterpret_cast<Stub>(slot(pk, 20))(pk) == E_NOTIMPL && reinterpret_cast<Stub>(slot(pk, 30))(pk) == E_NOTIMPL &&
reinterpret_cast<Stub>(slot(pk, 41))(pk) == E_NOTIMPL,
"the slots without an offline answer (20, 40, 41) are E_NOTIMPL");
// Slot 39, XPackageEnumeratePackages(kind, scope, context, callback): the title enumerates content packages
// (kind 1); there are none offline, so S_OK and the callback never runs. No callback is E_INVALIDARG.
"the slots without an offline answer (20, 30, 41) are E_NOTIMPL");
// Slots 28/35/39, XPackageEnumeratePackages(kind, scope, context, callback): the title enumerates content
// packages (kind 1); there are none offline, so S_OK and the callback never runs. No callback is E_INVALIDARG.
{
using PackageCallback = bool(CALLBACK*)(void*, const void*);
using EnumeratePackages = HRESULT(STDMETHODCALLTYPE*)(void*, uint32_t, uint32_t, void*, PackageCallback);
const auto enumerate_packages = reinterpret_cast<EnumeratePackages>(slot(pk, 39));
int listed = 0;
const PackageCallback count_package = [](void* ctx, const void*) -> bool {
++*static_cast<int*>(ctx);
return true;
};
check(enumerate_packages(pk, 1, 0, &listed, count_package) == S_OK &&
enumerate_packages(pk, 1, 1, &listed, count_package) == S_OK &&
enumerate_packages(pk, 0, 0, &listed, count_package) == S_OK && listed == 0 &&
enumerate_packages(pk, 1, 0, &listed, nullptr) == E_INVALIDARG,
"slot 39 (XPackageEnumeratePackages): S_OK and no callback (no content packages offline); "
bool all_ok = true;
for (const int s : {28, 35, 39}) {
const auto enumerate_packages = reinterpret_cast<EnumeratePackages>(slot(pk, s));
all_ok = all_ok && enumerate_packages(pk, 1, 0, &listed, count_package) == S_OK &&
enumerate_packages(pk, 1, 1, &listed, count_package) == S_OK &&
enumerate_packages(pk, 0, 0, &listed, count_package) == S_OK &&
enumerate_packages(pk, 1, 0, &listed, nullptr) == E_INVALIDARG;
}
check(all_ok && listed == 0,
"slots 28/35/39 (XPackageEnumeratePackages): S_OK and no callback (no content packages offline); "
"a null callback is E_INVALIDARG");
}
// Slots 29/36/40, XPackageRegisterPackageInstalled(queue, context, callback, token): returns S_OK and a fresh
// non-zero registration token accepted by slot 22; callback never runs offline.
{
using RegisterInstalled = HRESULT(STDMETHODCALLTYPE*)(void*, XTaskQueueHandle, void*, void*,
XTaskQueueRegistrationToken*);
using InstalledCallback = void(CALLBACK*)(void*, const void*);
static std::atomic<int> s_installed_calls{0};
const InstalledCallback on_installed = [](void*, const void*) { s_installed_calls.fetch_add(1); };
XTaskQueueRegistrationToken prev{0};
bool reg_ok = true;
for (const int s : {29, 36, 40}) {
const auto register_installed = reinterpret_cast<RegisterInstalled>(slot(pk, s));
XTaskQueueRegistrationToken tok{0};
reg_ok = reg_ok &&
register_installed(pk, nullptr, nullptr, reinterpret_cast<void*>(on_installed), &tok) == S_OK &&
tok.token != 0 && tok.token != prev.token &&
register_installed(pk, nullptr, nullptr, nullptr, &tok) == E_INVALIDARG && tok.token == 0 &&
register_installed(pk, nullptr, nullptr, reinterpret_cast<void*>(on_installed), nullptr) ==
E_POINTER;
prev = tok;
}
check(reg_ok && s_installed_calls.load() == 0,
"slots 29/36/40 (XPackageRegisterPackageInstalled): S_OK, distinct non-zero tokens, no callback");
}
// Flat C XPackage exports in xgameruntime.dll forward to the same offline implementation.
{
using FlatEnumFn = HRESULT(__stdcall*)(uint32_t, uint32_t, void*, void*);
using FlatCreateMonFn = HRESULT(__stdcall*)(const char*, uint32_t, const void*, uint32_t, XTaskQueueHandle,
void**);
using FlatCloseMonFn = void(__stdcall*)(void*);
using FlatProgressFn = void(__stdcall*)(void*, void*);
using FlatUpdateFn = bool(__stdcall*)(void*);
using FlatRegProgFn = HRESULT(__stdcall*)(void*, void*, void*, XTaskQueueRegistrationToken*);
using FlatUnregProgFn = bool(__stdcall*)(void*, XTaskQueueRegistrationToken, bool);
using FlatRegInstFn = HRESULT(__stdcall*)(XTaskQueueHandle, void*, void*, XTaskQueueRegistrationToken*);
using FlatUnregInstFn = bool(__stdcall*)(XTaskQueueRegistrationToken, bool);
const auto find_xgr = [](const char* name) { return rt::shims::find("xgameruntime.dll", name); };
const auto flat_enum = reinterpret_cast<FlatEnumFn>(find_xgr("XPackageEnumeratePackages"));
const auto flat_create = reinterpret_cast<FlatCreateMonFn>(find_xgr("XPackageCreateInstallationMonitor"));
const auto flat_close = reinterpret_cast<FlatCloseMonFn>(find_xgr("XPackageCloseInstallationMonitorHandle"));
const auto flat_progress = reinterpret_cast<FlatProgressFn>(find_xgr("XPackageGetInstallationProgress"));
const auto flat_update = reinterpret_cast<FlatUpdateFn>(find_xgr("XPackageUpdateInstallationMonitor"));
const auto flat_reg_prog =
reinterpret_cast<FlatRegProgFn>(find_xgr("XPackageRegisterInstallationProgressChanged"));
const auto flat_unreg_prog =
reinterpret_cast<FlatUnregProgFn>(find_xgr("XPackageUnregisterInstallationProgressChanged"));
const auto flat_reg_inst = reinterpret_cast<FlatRegInstFn>(find_xgr("XPackageRegisterPackageInstalled"));
const auto flat_unreg_inst =
reinterpret_cast<FlatUnregInstFn>(find_xgr("XPackageUnregisterPackageInstalled"));
char own_id[64] = {};
reinterpret_cast<Ident>(slot(pk, 3))(pk, sizeof(own_id), own_id);
void* mon = nullptr;
struct FlatProg {
uint64_t total, installed, launch;
bool launchable, completed;
} fp{};
const auto dummy_cb = reinterpret_cast<void*>(+[](void*, const void*) -> bool { return true; });
XTaskQueueRegistrationToken tp{0}, ti{0};
check(flat_enum && flat_create && flat_close && flat_progress && flat_update && flat_reg_prog &&
flat_unreg_prog && flat_reg_inst && flat_unreg_inst &&
flat_enum(1, 0, nullptr, dummy_cb) == S_OK && flat_enum(1, 0, nullptr, nullptr) == E_INVALIDARG &&
flat_create(own_id, 0, nullptr, 0, nullptr, &mon) == S_OK && mon != nullptr &&
(flat_progress(mon, &fp), fp.completed && fp.launchable && fp.total != 0 && fp.installed == fp.total) &&
!flat_update(mon) && flat_reg_prog(mon, nullptr, dummy_cb, &tp) == S_OK && tp.token != 0 &&
flat_unreg_prog(mon, tp, true) && flat_reg_inst(nullptr, nullptr, dummy_cb, &ti) == S_OK &&
ti.token != 0 && flat_unreg_inst(ti, true),
"flat C XPackage enumeration, monitor, and install-registration exports resolve and work offline");
if (flat_close && mon) flat_close(mon);
}
struct Progress {
uint64_t total, installed, launch;
bool launchable, completed;
Expand Down
Loading