Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

fix(ownership): universal generic free game acquisition, immediate UI/cache persistence, loading bar dismissal, and boot-time recent ordering

Rafael Abud Ferreiraa22466220185
diff --git a/src/app/app_core.cpp b/src/app/app_core.cpp
index 23463c7..e4ab895 100644
--- a/src/app/app_core.cpp
+++ b/src/app/app_core.cpp
@@ -658,9 +658,14 @@ bool coreStartup() {
}
auto initialLib = std::make_shared<Library>();
initialLib->setCachePath(cacheFile);
- if (initialLib->loadCache(true) && !initialLib->rows().empty()) {
+ initialLib->setPlaytime(g_settings.titlePlaytime);
+ initialLib->setFavorites(g_settings.favorites);
+ initialLib->setWishlist(g_settings.wishlist);
+ if (initialLib->loadCache(true)) {
setInitialLibrary(initialLib);
- g_ui->setRows(initialLib->rows());
+ if (!initialLib->rows().empty()) {
+ g_ui->setRows(initialLib->rows());
+ }
g_ui->setOwned(initialLib->owned(), initialLib->purchasable(), initialLib->ownedKnown());
g_ui->setMembership(initialLib->gamePassMember());
g_ui->setSearchPools(initialLib->gamePassSearchPool(), initialLib->librarySearchPool());
diff --git a/src/app/library.cpp b/src/app/library.cpp
index 2a94ba9..f634e5e 100644
--- a/src/app/library.cpp
+++ b/src/app/library.cpp
@@ -97,6 +97,8 @@ json::Value itemsToJson(const std::vector<std::pair<std::string, std::string>>&
return a;
}
+} // namespace
+
bool isStoreBigId(const std::string& s) {
if (s.size() != 12) return false;
for (char c : s) {
@@ -105,6 +107,8 @@ bool isStoreBigId(const std::string& s) {
return true;
}
+namespace {
+
bool namesSeries(const std::string& title) {
return title.find("Series X|S") != std::string::npos || title.find("Series X/S") != std::string::npos;
}
@@ -175,6 +179,7 @@ void dedupeByTitle(std::vector<ui::GameTile>& tiles) {
for (auto& t : tiles) {
if (t.productId.empty() || t.name.empty()) continue;
+ if (isStoreBigId(t.name) || (t.name == t.productId && isStoreBigId(t.productId))) continue;
std::string norm = xcloud::normalizeBaseTitle(t.name);
std::string tid = t.titleId;
@@ -531,6 +536,9 @@ ui::GameTile Library::tile(const std::string& productId, const std::string& titl
if (t.titleId == "None") t.titleId.clear();
if (t.titleId.empty() && !productId.empty()) t.titleId = productId;
t.name = p.title;
+ if (t.name.empty() && !isStoreBigId(productId)) {
+ t.name = productId;
+ }
t.publisher = p.publisher;
t.description = p.description;
t.tileUrl = p.tileUrl.empty() ? p.posterUrl : p.tileUrl;
@@ -659,6 +667,10 @@ bool Library::isPcOnly(const std::string& productId) const {
}
bool Library::hasStreamingSupport(const ui::GameTile& t) const {
+ if ((!t.productId.empty() && acquiredProducts_.count(t.productId)) ||
+ (!t.titleId.empty() && acquiredTitles_.count(t.titleId))) {
+ return true;
+ }
if (t.titleId.empty() || t.titleId == "None") return false;
if (t.titleId == t.productId && isStoreBigId(t.productId)) {
auto ct = cloudTitleOf_.find(t.productId);
@@ -673,9 +685,26 @@ std::vector<ui::GameTile> Library::tiles(const std::vector<Item>& items) const {
ui::GameTile t = tile(pid, tid);
if (xcloud::isNonGameAddon(t.titleId, t.name)) continue;
if (t.productId.empty() || t.titleId.empty() || t.name.empty()) continue;
+ if (isStoreBigId(t.name) || (t.name == t.productId && isStoreBigId(t.productId))) {
+ auto pit = products_.find(t.productId);
+ if (pit != products_.end() && !pit->second.title.empty() && !isStoreBigId(pit->second.title)) {
+ t.name = pit->second.title;
+ }
+ }
if (isStoreBigId(t.name) || (t.name == t.productId && isStoreBigId(t.productId))) continue;
if (isPcOnly(t.productId) || xcloud::isPcTitle(t.name)) continue;
if (!hasStreamingSupport(t)) continue;
+ bool isAcq = (!t.productId.empty() && acquiredProducts_.count(t.productId)) ||
+ (!t.titleId.empty() && acquiredTitles_.count(t.titleId));
+ if (isAcq) {
+ if (t.titleId.empty() || t.titleId == "None") {
+ auto ct = cloudTitleOf_.find(t.productId);
+ if (ct != cloudTitleOf_.end()) t.titleId = ct->second;
+ }
+ t.playable = true;
+ t.purchasable = false;
+ t.freeInStore = false;
+ }
out.push_back(std::move(t));
}
dedupeByTitle(out);
@@ -780,19 +809,58 @@ std::vector<ui::GameTile> Library::librarySearchPool() const {
return tiles(items);
}
+void Library::setPlaytime(const std::map<std::string, uint64_t>& playtime) {
+ playtime_ = playtime;
+ sortOwned();
+}
+
+void Library::recordRecent(const std::string& productIdOrTitleId) {
+ if (productIdOrTitleId.empty()) return;
+ std::string pid = productIdOrTitleId;
+ for (const auto& [p, t] : cloudTitleOf_) {
+ if (t == productIdOrTitleId) { pid = p; break; }
+ }
+ auto it = std::find(recent_.begin(), recent_.end(), pid);
+ if (it != recent_.end()) recent_.erase(it);
+ recent_.insert(recent_.begin(), pid);
+ sortOwned();
+ saveCache();
+}
+
void Library::sortOwned() {
- // Recently played first (newest first), then alphabetical.
- auto rank = [&](const std::string& pid) {
+ // Recently played first (newest first in recent_), then titles with accumulated playtime, then alphabetical.
+ auto rank = [&](const std::string& pid, const std::string& tid) -> size_t {
auto it = std::find(recent_.begin(), recent_.end(), pid);
- return it == recent_.end() ? recent_.size() : static_cast<size_t>(it - recent_.begin());
+ if (it != recent_.end()) return static_cast<size_t>(it - recent_.begin());
+ if (!tid.empty()) {
+ auto itT = std::find(recent_.begin(), recent_.end(), tid);
+ if (itT != recent_.end()) return static_cast<size_t>(itT - recent_.begin());
+ }
+ return static_cast<size_t>(1000000000);
+ };
+ auto playtime = [&](const std::string& pid, const std::string& tid) -> uint64_t {
+ if (!playtime_.empty()) {
+ auto itP = playtime_.find(pid);
+ if (itP != playtime_.end() && itP->second > 0) return itP->second;
+ if (!tid.empty()) {
+ auto itT = playtime_.find(tid);
+ if (itT != playtime_.end() && itT->second > 0) return itT->second;
+ }
+ }
+ return 0;
};
auto name = [&](const std::string& pid) {
auto it = products_.find(pid);
return it == products_.end() ? std::string() : it->second.title;
};
std::stable_sort(owned_.begin(), owned_.end(), [&](const auto& a, const auto& b) {
- size_t ra = rank(a.first), rb = rank(b.first);
+ size_t ra = rank(a.first, a.second), rb = rank(b.first, b.second);
if (ra != rb) return ra < rb;
+ if (ra >= 1000000000 && rb >= 1000000000) {
+ uint64_t pa = playtime(a.first, a.second);
+ uint64_t pb = playtime(b.first, b.second);
+ if (pa != pb) return pa > pb;
+ }
return name(a.first) < name(b.first);
});
}
@@ -840,6 +908,12 @@ bool Library::loadCache(bool withLayout) {
gamePassMember_ = (*j)["gamePassMember"].asBool();
if (subscription_.empty()) subscription_ = (*j)["subscription"].str();
for (const auto& p : (*j)["ownedProducts"].items()) ownedProducts_.insert(p.str());
+ for (const auto& p : (*j)["acquiredProducts"].items()) acquiredProducts_.insert(p.str());
+ for (const auto& t : (*j)["acquiredTitles"].items()) acquiredTitles_.insert(t.str());
+ recent_.clear();
+ for (const auto& r : (*j)["recent"].items()) {
+ if (!r.str().empty()) recent_.push_back(r.str());
+ }
bool corruptedGamePass = (*j)["gamePass"].items().size() > 1050;
if (corruptedGamePass) {
XC_LOGW("library: cache gamePass has %zu items (likely contaminated), ignoring cached gamePass",
@@ -909,6 +983,63 @@ bool Library::loadCache(bool withLayout) {
}
}
}
+ if (recent_.empty()) {
+ for (const auto& rowObj : (*j)["layout"].items()) {
+ if (rowObj["recent"].asBool(false)) {
+ for (const auto& it : rowObj["items"].items()) {
+ std::string pid = it["p"].str();
+ if (!pid.empty() && std::find(recent_.begin(), recent_.end(), pid) == recent_.end()) {
+ recent_.push_back(pid);
+ }
+ }
+ }
+ }
+ }
+ for (const auto& pid : acquiredProducts_) {
+ ownedProducts_.insert(pid);
+ purchasableSet_.erase(pid);
+ freeInStore_.erase(pid);
+ if (std::find(recent_.begin(), recent_.end(), pid) == recent_.end()) {
+ recent_.insert(recent_.begin(), pid);
+ }
+ bool inOwned = false;
+ for (const auto& item : owned_) {
+ if (item.first == pid) { inOwned = true; break; }
+ }
+ if (!inOwned && !pid.empty()) {
+ std::string tid;
+ auto it = cloudTitleOf_.find(pid);
+ if (it != cloudTitleOf_.end()) tid = it->second;
+ if (tid.empty()) {
+ auto pit = products_.find(pid);
+ if (pit != products_.end() && !pit->second.xcloudTitleId.empty() && pit->second.xcloudTitleId != "None") {
+ tid = pit->second.xcloudTitleId;
+ }
+ }
+ owned_.emplace_back(pid, tid);
+ }
+ }
+ for (const auto& tid : acquiredTitles_) {
+ if (!tid.empty()) {
+ ownedTitles_.insert(tid);
+ for (const auto& [p, t] : cloudTitleOf_) {
+ if (t == tid) {
+ acquiredProducts_.insert(p);
+ ownedProducts_.insert(p);
+ purchasableSet_.erase(p);
+ freeInStore_.erase(p);
+ }
+ }
+ }
+ }
+ purchasable_.erase(std::remove_if(purchasable_.begin(), purchasable_.end(),
+ [&](const Item& it) {
+ return acquiredProducts_.count(it.first) > 0 ||
+ ownedProducts_.count(it.first) > 0 ||
+ (!it.second.empty() && (acquiredTitles_.count(it.second) > 0 || ownedTitles_.count(it.second) > 0));
+ }), purchasable_.end());
+ purchasableSet_.clear();
+ for (const auto& p : purchasable_) purchasableSet_.insert(p.first);
size_t codCount = 0;
for (const auto& o : owned_) {
if (o.second.find("CALLOFDUTY") != std::string::npos) ++codCount;
@@ -920,7 +1051,7 @@ bool Library::loadCache(bool withLayout) {
owned_.clear();
ownershipKnown_ = false;
} else {
- ownershipKnown_ = !ownedTitles_.empty() || !ownedProducts_.empty() || !owned_.empty();
+ ownershipKnown_ = !ownedTitles_.empty() || !ownedProducts_.empty() || !owned_.empty() || !acquiredProducts_.empty();
}
sortOwned();
XC_LOGI("library: cache: %zu of the account's games, %zu to buy, %zu home rows, %zu consoles known",
@@ -938,6 +1069,14 @@ void Library::saveCache() const {
for (const auto& t : ownedTitles_) titles.push(t);
for (const auto& p : ownedProducts_) prods.push(p);
root.set("ownedTitles", titles);
+ json::Value acqP = json::Value::array(), acqT = json::Value::array();
+ for (const auto& p : acquiredProducts_) acqP.push(p);
+ for (const auto& t : acquiredTitles_) acqT.push(t);
+ root.set("acquiredProducts", acqP);
+ root.set("acquiredTitles", acqT);
+ json::Value recentArr = json::Value::array();
+ for (const auto& r : recent_) recentArr.push(r);
+ root.set("recent", recentArr);
root.set("gamePassMember", gamePassMember_);
if (!subscription_.empty()) root.set("subscription", subscription_);
root.set("ownedProducts", prods);
@@ -990,22 +1129,71 @@ void Library::saveCache() const {
if (!platform::writeFileAtomic(cachePath_, root.dump())) XC_LOGW("library: could not write %s", cachePath_.c_str());
}
-void Library::markAsOwned(const std::string& productId, const std::string& titleId) {
- if (!productId.empty()) ownedProducts_.insert(productId);
- if (!titleId.empty()) ownedTitles_.insert(titleId);
- if (!productId.empty() && !products_.count(productId)) {
- xcloud::Product p;
+void Library::markAsOwned(const std::string& productId, const std::string& titleId, const std::string& name) {
+ std::string tid = titleId;
+ if ((tid.empty() || tid == "None") && !productId.empty()) {
+ auto it = cloudTitleOf_.find(productId);
+ if (it != cloudTitleOf_.end()) tid = it->second;
+ }
+ if (!productId.empty()) {
+ acquiredProducts_.insert(productId);
+ ownedProducts_.insert(productId);
+ purchasableSet_.erase(productId);
+ freeInStore_.erase(productId);
+ if (!tid.empty() && tid != "None") {
+ cloudTitleOf_[productId] = tid;
+ }
+ auto& p = products_[productId];
p.productId = productId;
- p.xcloudTitleId = titleId;
- p.title = productId;
- products_[productId] = p;
+ if (!tid.empty() && tid != "None") p.xcloudTitleId = tid;
+ if (!name.empty() && (p.title.empty() || isStoreBigId(p.title) || p.title == productId)) {
+ p.title = name;
+ } else if (p.title.empty() && !isStoreBigId(productId)) {
+ p.title = productId;
+ }
}
+ if (!tid.empty() && tid != "None") {
+ acquiredTitles_.insert(tid);
+ ownedTitles_.insert(tid);
+ for (const auto& [p, t] : cloudTitleOf_) {
+ if (t == tid) {
+ acquiredProducts_.insert(p);
+ ownedProducts_.insert(p);
+ purchasableSet_.erase(p);
+ freeInStore_.erase(p);
+ }
+ }
+ }
+ purchasable_.erase(std::remove_if(purchasable_.begin(), purchasable_.end(),
+ [&](const Item& item) {
+ return (!productId.empty() && item.first == productId) ||
+ (!tid.empty() && item.second == tid) ||
+ acquiredProducts_.count(item.first) > 0 ||
+ (!item.second.empty() && acquiredTitles_.count(item.second) > 0);
+ }),
+ purchasable_.end());
+ purchasableSet_.clear();
+ for (const auto& p : purchasable_) purchasableSet_.insert(p.first);
+
bool inOwned = false;
- for (const auto& item : owned_) {
- if (item.first == productId) { inOwned = true; break; }
+ for (auto& item : owned_) {
+ if ((!productId.empty() && item.first == productId) || (!tid.empty() && item.second == tid)) {
+ inOwned = true;
+ if (item.second.empty() && !tid.empty()) item.second = tid;
+ break;
+ }
}
if (!inOwned && !productId.empty()) {
- owned_.emplace_back(productId, titleId);
+ owned_.emplace_back(productId, tid);
+ }
+ if (!productId.empty()) {
+ auto rit = std::find(recent_.begin(), recent_.end(), productId);
+ if (rit != recent_.end()) recent_.erase(rit);
+ recent_.insert(recent_.begin(), productId);
+ } else if (!tid.empty()) {
+ auto rit = std::find(recent_.begin(), recent_.end(), tid);
+ if (rit != recent_.end()) recent_.erase(rit);
+ recent_.insert(recent_.begin(), tid);
}
ownershipKnown_ = true;
sortOwned();
@@ -1168,9 +1356,12 @@ bool Library::load(xcloud::GssvClient& gssv, const std::string& language, const
if (gssv.listTitles(recent, lastErr, true)) {
RowIds r{ui::tr(ui::Str::JumpBackIn), false, false, {}, true};
std::vector<std::string> ids;
+ std::vector<std::string> freshRecent;
for (const auto& t : recent) {
if (t.productId.empty()) continue;
- recent_.push_back(t.productId);
+ if (std::find(freshRecent.begin(), freshRecent.end(), t.productId) == freshRecent.end()) {
+ freshRecent.push_back(t.productId);
+ }
if (!t.xboxTitleId.empty()) xboxTitleOf_[t.productId] = t.xboxTitleId;
// Its inputs come with it: the first cards show keyboard and
// mouse without waiting for the whole list (or a cache that has it).
@@ -1178,6 +1369,17 @@ bool Library::load(xcloud::GssvClient& gssv, const std::string& language, const
r.items.emplace_back(t.productId, t.titleId);
if (!products_.count(t.productId)) ids.push_back(t.productId);
}
+ for (const auto& pid : recent_) {
+ if (!pid.empty() && std::find(freshRecent.begin(), freshRecent.end(), pid) == freshRecent.end()) {
+ freshRecent.push_back(pid);
+ }
+ }
+ recent_ = std::move(freshRecent);
+ for (const auto& pid : acquiredProducts_) {
+ auto it = std::find(recent_.begin(), recent_.end(), pid);
+ if (it != recent_.end()) recent_.erase(it);
+ recent_.insert(recent_.begin(), pid);
+ }
sortOwned();
// The first cards are the first thing seen: full details (hero art,
// description) for them in this same round trip.
@@ -1306,12 +1508,18 @@ void Library::loadOwned(xcloud::GssvClient gssv, const Changed& changed, const s
if (!titlesPtr) {
if (!gssv.listTitles(fetchedTitles, err, false)) {
XC_LOGW("%s", err.c_str()); // keep what the cache said
+ progress_ = {};
+ changed();
return;
}
titlesPtr = &fetchedTitles;
}
const auto& titles = *titlesPtr;
- if (stop && *stop) return;
+ if (stop && *stop) {
+ progress_ = {};
+ changed();
+ return;
+ }
std::set<std::string> ownedTitles, ownedProducts;
std::vector<Item> mine;
std::set<std::string> seenMine;
@@ -1332,16 +1540,22 @@ void Library::loadOwned(xcloud::GssvClient gssv, const Changed& changed, const s
};
for (const auto& t : titles) {
- member |= t.hasEntitlement && t.viaSubscription;
+ bool isAcquired = (!t.productId.empty() && acquiredProducts_.count(t.productId)) ||
+ (!t.titleId.empty() && acquiredTitles_.count(t.titleId));
+ for (const auto& alt : t.alternateProductIds) {
+ if (!alt.empty() && acquiredProducts_.count(alt)) { isAcquired = true; break; }
+ }
+ bool entitled = t.hasEntitlement || isAcquired;
+ member |= entitled && t.viaSubscription;
if (xcloud::isNonGameAddon(t.titleId)) continue;
- if (t.hasEntitlement) ownedTitles.insert(t.titleId);
+ if (entitled) ownedTitles.insert(t.titleId);
registerCloudTitle(t.productId, t.titleId, t.xboxTitleId, t.isFreeInStore, t.mouseKeyboard);
for (const auto& alt : t.alternateProductIds) {
registerCloudTitle(alt, t.titleId, t.xboxTitleId, t.isFreeInStore, t.mouseKeyboard);
}
- if (t.hasEntitlement) {
+ if (entitled) {
if (!t.productId.empty()) ownedProducts.insert(t.productId);
for (const auto& alt : t.alternateProductIds) {
if (!alt.empty()) ownedProducts.insert(alt);
@@ -1350,7 +1564,7 @@ void Library::loadOwned(xcloud::GssvClient gssv, const Changed& changed, const s
// like Fortnite, without the subscription) are its games too.
// Also games outside the Game Pass catalog.
if (!t.productId.empty()) {
- if (!gamePass_.count(t.productId) || !t.viaSubscription) {
+ if (!gamePass_.count(t.productId) || !t.viaSubscription || isAcquired) {
if (seenMine.insert(t.productId).second) {
mine.emplace_back(t.productId, t.titleId);
}
@@ -1381,6 +1595,43 @@ void Library::loadOwned(xcloud::GssvClient gssv, const Changed& changed, const s
toBuy[g.productId] = g.cloudTitleId;
}
}
+ for (const auto& pid : acquiredProducts_) {
+ toBuy.erase(pid);
+ ownedProducts.insert(pid);
+ if (seenMine.insert(pid).second) {
+ std::string tid;
+ auto it = cloudTitleOf_.find(pid);
+ if (it != cloudTitleOf_.end()) tid = it->second;
+ if (tid.empty()) {
+ auto pit = products_.find(pid);
+ if (pit != products_.end() && !pit->second.xcloudTitleId.empty() && pit->second.xcloudTitleId != "None") {
+ tid = pit->second.xcloudTitleId;
+ }
+ }
+ mine.emplace_back(pid, tid);
+ }
+ }
+ for (const auto& tid : acquiredTitles_) {
+ if (!tid.empty()) {
+ ownedTitles.insert(tid);
+ for (const auto& [p, t] : cloudTitleOf_) {
+ if (t == tid) {
+ ownedProducts.insert(p);
+ toBuy.erase(p);
+ }
+ }
+ }
+ }
+ for (auto it = toBuy.begin(); it != toBuy.end(); ) {
+ bool isOwnedOrAcquired = acquiredProducts_.count(it->first) > 0 ||
+ ownedProducts.count(it->first) > 0 ||
+ (!it->second.empty() && (acquiredTitles_.count(it->second) > 0 || ownedTitles.count(it->second) > 0));
+ if (isOwnedOrAcquired) {
+ it = toBuy.erase(it);
+ } else {
+ ++it;
+ }
+ }
// Games to buy: the most popular in the cloud first, then the rest.
std::vector<Item> buy;
for (const auto& id : popular_) {
@@ -1398,20 +1649,46 @@ void Library::loadOwned(xcloud::GssvClient gssv, const Changed& changed, const s
if (!products_.count(pid)) missing.push_back(pid);
if (!missing.empty() && !xcloud::fetchProducts(missing, market_, language_, products_, err, false))
XC_LOGW("%s", err.c_str());
- if (stop && *stop) return;
+ if (stop && *stop) {
+ progress_ = {};
+ changed();
+ return;
+ }
ownedTitles_ = std::move(ownedTitles);
ownershipFresh_ = true;
ownedProducts_ = std::move(ownedProducts);
owned_ = std::move(mine);
purchasable_ = std::move(buy);
+ purchasable_.erase(std::remove_if(purchasable_.begin(), purchasable_.end(),
+ [&](const Item& item) {
+ return acquiredProducts_.count(item.first) > 0 ||
+ ownedProducts_.count(item.first) > 0 ||
+ (!item.second.empty() && (acquiredTitles_.count(item.second) > 0 || ownedTitles_.count(item.second) > 0));
+ }), purchasable_.end());
purchasableSet_.clear();
for (const auto& p : purchasable_) purchasableSet_.insert(p.first);
+ for (const auto& pid : acquiredProducts_) {
+ purchasableSet_.erase(pid);
+ freeInStore_.erase(pid);
+ }
+ for (const auto& [pid, tid] : cloudTitleOf_) {
+ if (acquiredTitles_.count(tid) || ownedTitles_.count(tid)) {
+ purchasableSet_.erase(pid);
+ freeInStore_.erase(pid);
+ }
+ }
+ for (const auto& pid : acquiredProducts_) {
+ if (!pid.empty() && std::find(recent_.begin(), recent_.end(), pid) == recent_.end()) {
+ recent_.insert(recent_.begin(), pid);
+ }
+ }
ownershipKnown_ = true;
gamePassMember_ = member;
sortOwned();
saveCache();
XC_LOGI("library: account can stream %zu titles; %zu outside Game Pass; %zu more to buy", ownedTitles_.size(),
owned_.size(), purchasable_.size());
+ progress_ = {};
changed();
}
@@ -1474,6 +1751,7 @@ void Library::loadCatalogNames(const Changed& changed, const std::atomic<bool>*
changed(); // each batch: the lists fill in as names arrive
}
progress_ = {};
+ changed();
allGames_.erase(std::remove_if(allGames_.begin(), allGames_.end(),
[&](const std::string& id) {
return isPcOnly(id);
diff --git a/src/app/library.h b/src/app/library.h
index 89d20f8..bac391d 100644
--- a/src/app/library.h
+++ b/src/app/library.h
@@ -37,6 +37,7 @@
namespace xc::app {
void dedupeByTitle(std::vector<ui::GameTile>& tiles);
+bool isStoreBigId(const std::string& s);
class Library {
public:
@@ -99,7 +100,17 @@ public:
bool loadCache(bool withLayout = false);
void saveCache() const;
// Marks a product/title as owned immediately upon successful purchase/claim.
- void markAsOwned(const std::string& productId, const std::string& titleId = {});
+ void markAsOwned(const std::string& productId, const std::string& titleId = {}, const std::string& name = {});
+ // Sets accumulated playtime for ranking in 'Recent' sort mode.
+ void setPlaytime(const std::map<std::string, uint64_t>& playtime);
+ // Records a product or title ID as recently played, placing it at the front of recent list.
+ void recordRecent(const std::string& productIdOrTitleId);
+ const std::set<std::string>& acquiredProducts() const { return acquiredProducts_; }
+ const std::set<std::string>& acquiredTitles() const { return acquiredTitles_; }
+ void setAcquired(const std::set<std::string>& prods, const std::set<std::string>& titles) {
+ acquiredProducts_.insert(prods.begin(), prods.end());
+ acquiredTitles_.insert(titles.begin(), titles.end());
+ }
// Delta-sync: discovers new/updated titles, only fetches missing metadata, updates rows in-place.
size_t syncDelta(xcloud::GssvClient& gssv, const Changed& changed, std::string& err);
void markUnclaimable(const std::string& productId);
@@ -141,6 +152,8 @@ private:
bool ownershipFresh_ = false; // the account's list of this start (not the cache's) is in
std::set<std::string> knownGamePass_; // the Game Pass catalog at the last start (cache)
std::set<std::string> ownedTitles_, ownedProducts_;
+ std::set<std::string> acquiredTitles_, acquiredProducts_;
+ std::map<std::string, uint64_t> playtime_;
std::vector<Item> owned_; // outside Game Pass
std::vector<Item> purchasable_; // streamable once bought
std::set<std::string> purchasableSet_;
diff --git a/src/app/worker.cpp b/src/app/worker.cpp
index da3ecc1..cd72ee1 100644
--- a/src/app/worker.cpp
+++ b/src/app/worker.cpp
@@ -384,6 +384,19 @@ std::string stream(xcloud::GssvClient& gssv) {
XC_LOGI("playtime: updated for %s (%s) to %llu seconds (%s)", g_stageTitle.c_str(),
g_stageProduct.c_str(), static_cast<unsigned long long>(newTotal),
ui::formatPlaytime(newTotal).c_str());
+
+ std::shared_ptr<app::Library> lib;
+ {
+ std::lock_guard<std::mutex> lockLib(s_libraryMutex);
+ lib = s_library;
+ }
+ if (lib) {
+ if (!g_stageProduct.empty()) lib->recordRecent(g_stageProduct);
+ else if (!g_stageTitle.empty()) lib->recordRecent(g_stageTitle);
+ lib->setPlaytime(g_settings.titlePlaytime);
+ g_ui->setRows(lib->rows());
+ g_ui->setOwned(lib->owned(), lib->purchasable(), lib->ownedKnown());
+ }
}
if (bestRtt > 0) {
// Remembered per region: Settings shows it, the region fallback uses it.
@@ -893,6 +906,10 @@ void loadLibrary(auth::AuthManager& am, xcloud::GssvClient& gssv, bool forceRefr
}
if (!forceRefresh && existingLib && existingLib->ownedKnown()) {
+ {
+ std::lock_guard<std::mutex> lock(g_settingsMutex);
+ existingLib->setPlaytime(g_settings.titlePlaytime);
+ }
XC_LOGI("library: delta-sync against active library (forceRefresh=%d)", forceRefresh);
if (g_ui->screen() == ui::Screen::Splash) {
g_ui->showSplash(ui::tr(ui::Str::LoadingGames));
@@ -936,6 +953,9 @@ void loadLibrary(auth::AuthManager& am, xcloud::GssvClient& gssv, bool forceRefr
auto library = std::make_shared<app::Library>();
{
std::lock_guard<std::mutex> lock(s_libraryMutex);
+ if (existingLib) {
+ library->setAcquired(existingLib->acquiredProducts(), existingLib->acquiredTitles());
+ }
s_library = library;
}
library->setCachePath(platform::dataDir() + "/library_cache.json");
@@ -944,6 +964,7 @@ void loadLibrary(auth::AuthManager& am, xcloud::GssvClient& gssv, bool forceRefr
std::lock_guard<std::mutex> lock(g_settingsMutex);
library->setFavorites(g_settings.favorites);
library->setWishlist(g_settings.wishlist);
+ library->setPlaytime(g_settings.titlePlaytime);
}
setPriceMarket(gssv.session().market.empty() ? "US" : gssv.session().market, ui::catalogLanguage());
setQueueClient(&gssv);
@@ -1575,7 +1596,8 @@ void worker() {
lib = s_library;
}
if (lib) {
- lib->markAsOwned(tile.productId, tile.titleId);
+ g_ui->setLoading(true, 0, 0);
+ lib->markAsOwned(tile.productId, tile.titleId, tile.name);
g_ui->markTileOwned(tile.productId, tile.titleId);
if (!shell::caps().console) {
g_ui->showToast(ui::tr(ui::Str::ClaimSuccess));
@@ -1586,9 +1608,14 @@ void worker() {
lib->loadOwned(gssv, [lib]() {
g_ui->setRows(lib->rows());
g_ui->setOwned(lib->owned(), lib->purchasable(), lib->ownedKnown());
+ auto p = lib->progress();
+ g_ui->setLoading(p.active, p.done, p.total);
});
+ g_ui->setLoading(false, 0, 0);
}
tile.playable = true;
+ tile.purchasable = false;
+ tile.freeInStore = false;
g_ui->showDetails(tile);
break;
}
diff --git a/src/ui/app_ui.cpp b/src/ui/app_ui.cpp
index 45d738e..e6a52b7 100644
--- a/src/ui/app_ui.cpp
+++ b/src/ui/app_ui.cpp
@@ -1363,8 +1363,67 @@ void AppUi::markTileOwned(const std::string& productId, const std::string& title
if (detail_.productId == productId || (!titleId.empty() && detail_.titleId == titleId)) {
detail_.playable = true;
detail_.purchasable = false;
+ detail_.freeInStore = false;
dirty_ = true;
}
+ auto updateTileFlags = [&](GameTile& t) {
+ if ((!productId.empty() && t.productId == productId) || (!titleId.empty() && t.titleId == titleId)) {
+ t.playable = true;
+ t.purchasable = false;
+ t.freeInStore = false;
+ }
+ };
+ for (auto& row : rows_) {
+ for (auto& t : row.tiles) updateTileFlags(t);
+ }
+ for (auto& row : allRows_) {
+ for (auto& t : row.tiles) updateTileFlags(t);
+ }
+ for (auto* list : {&gamePassPool_, &libraryPool_, &results_}) {
+ for (auto& t : *list) updateTileFlags(t);
+ }
+
+ GameTile acquiredTile;
+ if ((!productId.empty() && detail_.productId == productId) || (!titleId.empty() && detail_.titleId == titleId)) {
+ acquiredTile = detail_;
+ }
+ for (auto it = allPurchasable_.begin(); it != allPurchasable_.end(); ) {
+ if ((!productId.empty() && it->productId == productId) || (!titleId.empty() && it->titleId == titleId)) {
+ if (acquiredTile.productId.empty()) acquiredTile = *it;
+ it = allPurchasable_.erase(it);
+ } else {
+ ++it;
+ }
+ }
+ if (acquiredTile.productId.empty()) {
+ for (const auto* list : {&results_, &libraryPool_, &gamePassPool_}) {
+ for (const auto& t : *list) {
+ if ((!productId.empty() && t.productId == productId) || (!titleId.empty() && t.titleId == titleId)) {
+ acquiredTile = t;
+ break;
+ }
+ }
+ if (!acquiredTile.productId.empty()) break;
+ }
+ }
+ if (!acquiredTile.productId.empty()) {
+ acquiredTile.playable = true;
+ acquiredTile.purchasable = false;
+ acquiredTile.freeInStore = false;
+ for (auto it = allOwned_.begin(); it != allOwned_.end(); ++it) {
+ if (it->productId == acquiredTile.productId || (!acquiredTile.titleId.empty() && it->titleId == acquiredTile.titleId)) {
+ if (acquiredTile.name.empty()) acquiredTile.name = it->name;
+ if (acquiredTile.heroUrl.empty()) acquiredTile.heroUrl = it->heroUrl;
+ if (acquiredTile.tileUrl.empty()) acquiredTile.tileUrl = it->tileUrl;
+ allOwned_.erase(it);
+ break;
+ }
+ }
+ allOwned_.insert(allOwned_.begin(), acquiredTile);
+ }
+ if (!productId.empty()) wishlist_.erase(productId);
+ rebuild(true);
+ dirty_ = true;
}
void AppUi::markUnclaimable(const std::string& productId) {
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 52d40c5..0e27d58 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -6028,7 +6028,251 @@ static void testBloco3Corrections() {
}
}
+static void testBlock4OwnershipAndRecents() {
+ using namespace xc;
+ std::string testDir = "/tmp/test_bloco4_" + std::to_string(platform::nowMs());
+ std::system(("rm -rf " + testDir + " && mkdir -p " + testDir).c_str());
+ std::string cacheFile = testDir + "/library_cache.json";
+
+ // 1. Universal License Acquisition (markAsOwned) with 12-character Store BigId and cross-gen purging
+ {
+ app::Library lib;
+ lib.setCachePath(cacheFile);
+
+ // Preload catalog games including real 12-character Microsoft Store BigID (Just Dance)
+ // and an alternate edition sharing the same titleId (TID_JD)
+ CHECK(app::isStoreBigId("9P368QBR2C5S"));
+ std::string jsonText = "{\"version\":2,\"market\":\"US\",\"language\":\"en-US\",\"products\":{"
+ "\"9P368QBR2C5S\":{\"title\":\"Just Dance 2026\",\"xt\":\"TID_JD\"},"
+ "\"9P368QBRALT0\":{\"title\":\"Just Dance 2026 Deluxe\",\"xt\":\"TID_JD\"},"
+ "\"PROD_STORE1\":{\"title\":\"Store Game 1\",\"xt\":\"TID_SG1\"},"
+ "\"PROD_OWNED1\":{\"title\":\"Owned Game 1\",\"xt\":\"TID_OG1\"}},"
+ "\"owned\":[{\"p\":\"PROD_OWNED1\",\"t\":\"TID_OG1\"}],"
+ "\"purchasable\":[{\"p\":\"9P368QBR2C5S\",\"t\":\"TID_JD\"},{\"p\":\"9P368QBRALT0\",\"t\":\"TID_JD\"},{\"p\":\"PROD_STORE1\",\"t\":\"TID_SG1\"}],"
+ "\"freeInStore\":[\"9P368QBR2C5S\"],"
+ "\"ownedTitles\":[\"TID_OG1\"],\"ownedProducts\":[\"PROD_OWNED1\"]}";
+ platform::writeFileAtomic(cacheFile, jsonText);
+ CHECK(lib.loadCache(false));
+
+ CHECK(lib.owned().size() == 1);
+ CHECK(lib.purchasable().size() == 2); // 9P368QBR2C5S and 9P368QBRALT0 deduplicate to 1 Just Dance tile, + Store Game 1 = 2
+
+ // User acquires 12-character Store BigID Just Dance (9P368QBR2C5S)
+ lib.markAsOwned("9P368QBR2C5S", "TID_JD", "Just Dance 2026");
+
+ // Verify it is immediately added to owned, removed from purchasable, and freeInStore
+ CHECK(lib.owned().size() == 2);
+ // Both the acquired edition AND alternate cross-gen edition sharing TID_JD must be purged from purchasable
+ CHECK(lib.purchasable().size() == 1);
+ CHECK(lib.purchasable().front().productId == "PROD_STORE1");
+
+ bool jdFound = false;
+ for (const auto& t : lib.owned()) {
+ if (t.productId == "9P368QBR2C5S") {
+ jdFound = true;
+ CHECK(t.name == "Just Dance 2026");
+ CHECK(t.playable);
+ CHECK(!t.purchasable);
+ CHECK(!t.freeInStore);
+ break;
+ }
+ }
+ CHECK(jdFound);
+
+ // Verify 9P368QBR2C5S is placed at front of recent_ (first in owned)
+ CHECK(lib.owned().front().productId == "9P368QBR2C5S");
+ }
+
+ // 2. Resilience against slow GSSV entitlement propagation during loadOwned & loading bar dismissal
+ {
+ net::setTestTransport([](const net::Request& req) {
+ net::Response r;
+ r.status = 200;
+ if (req.url.find("/v2/titles") != std::string::npos) {
+ // Microsoft has NOT propagated entitlement for 9P368QBR2C5S yet! hasEntitlement is false
+ r.body = R"({"results":[
+ {"titleId":"TID_OG1","details":{"productId":"PROD_OWNED1","hasEntitlement":true,"isFreeInStore":false,"userPrograms":[]}},
+ {"titleId":"TID_JD","details":{"productId":"9P368QBR2C5S","hasEntitlement":false,"isFreeInStore":true,"userPrograms":[]}},
+ {"titleId":"TID_SG1","details":{"productId":"PROD_STORE1","hasEntitlement":false,"isFreeInStore":false,"userPrograms":[]}}
+ ]})";
+ } else if (req.url.find("/v3/products") != std::string::npos) {
+ r.body = R"({"Products":{
+ "9P368QBR2C5S":{"XCloudTitleId":"TID_JD","ProductTitle":"Just Dance 2026"},
+ "PROD_STORE1":{"XCloudTitleId":"TID_SG1","ProductTitle":"Store Game 1"},
+ "PROD_OWNED1":{"XCloudTitleId":"TID_OG1","ProductTitle":"Owned Game 1"}
+ }})";
+ } else {
+ r.status = 503;
+ }
+ return r;
+ });
+
+ app::Library lib;
+ lib.setCachePath(cacheFile);
+ CHECK(lib.loadCache(false));
+
+ // Before loadOwned, 9P368QBR2C5S was marked as owned
+ CHECK(lib.owned().size() == 2);
+
+ xcloud::GssvClient gssv;
+ std::atomic<bool> stop{false};
+ bool progressSawActive = false;
+ lib.loadOwned(gssv, [&]() {
+ if (lib.progress().active) progressSawActive = true;
+ }, &stop);
+
+ // Verify progress is cleared to inactive after loadOwned completes (no infinite spinner!)
+ CHECK(progressSawActive);
+ CHECK(!lib.progress().active);
+
+ // Verify loadCatalogNames also guarantees clearing progress even when missing is empty
+ lib.loadCatalogNames([&]() {}, &stop);
+ CHECK(!lib.progress().active);
+
+ // Verify 9P368QBR2C5S is STILL retained in owned, NOT dropped to purchasable!
+ CHECK(lib.owned().size() == 2);
+ bool foundStore1 = false;
+ bool foundJdInPurchasable = false;
+ for (const auto& t : lib.purchasable()) {
+ if (t.productId == "PROD_STORE1") foundStore1 = true;
+ if (t.productId == "9P368QBR2C5S" || t.productId == "9P368QBRALT0") foundJdInPurchasable = true;
+ }
+ CHECK(foundStore1);
+ CHECK(!foundJdInPurchasable);
+
+ bool jdFound = false;
+ for (const auto& t : lib.owned()) {
+ if (t.productId == "9P368QBR2C5S") {
+ jdFound = true;
+ CHECK(t.playable);
+ CHECK(!t.purchasable);
+ break;
+ }
+ }
+ CHECK(jdFound);
+
+ net::setTestTransport({});
+ }
+
+ // 3. Cache roundtrip and instant-resume recency ordering on boot
+ {
+ // Fresh library instance loading from cache
+ app::Library bootLib;
+ bootLib.setCachePath(cacheFile);
+ std::map<std::string, uint64_t> playtime = {
+ {"PROD_OWNED1", 1200}
+ };
+ bootLib.setPlaytime(playtime);
+ CHECK(bootLib.loadCache(true));
+
+ // 9P368QBR2C5S was acquired and at front of recent; PROD_OWNED1 has playtime
+ auto owned = bootLib.owned();
+ CHECK(owned.size() == 2);
+ CHECK(owned[0].productId == "9P368QBR2C5S");
+ CHECK(owned[1].productId == "PROD_OWNED1");
+ bool bootFoundStore1 = false;
+ bool bootFoundJdInPurchasable = false;
+ for (const auto& t : bootLib.purchasable()) {
+ if (t.productId == "PROD_STORE1") bootFoundStore1 = true;
+ if (t.productId == "9P368QBR2C5S" || t.productId == "9P368QBRALT0") bootFoundJdInPurchasable = true;
+ }
+ CHECK(bootFoundStore1);
+ CHECK(!bootFoundJdInPurchasable);
+
+ // 4. L3 Refresh in-memory continuity across Library instances (setAcquired)
+ app::Library l3Lib;
+ l3Lib.setAcquired(bootLib.acquiredProducts(), bootLib.acquiredTitles());
+ CHECK(l3Lib.acquiredProducts().count("9P368QBR2C5S") == 1);
+ CHECK(l3Lib.acquiredTitles().count("TID_JD") == 1);
+ }
+
+ // 5. "Recentes primeiro" sorting with playtime tie-breaking
+ {
+ app::Library sortLib;
+ sortLib.setCachePath(testDir + "/sort_cache.json");
+ std::string sortJson = "{\"version\":2,\"market\":\"US\",\"language\":\"en-US\",\"products\":{"
+ "\"PID_A\":{\"title\":\"Alpha Game\",\"xt\":\"TID_A\"},"
+ "\"PID_B\":{\"title\":\"Beta Game\",\"xt\":\"TID_B\"},"
+ "\"PID_C\":{\"title\":\"Charlie Game\",\"xt\":\"TID_C\"},"
+ "\"PID_D\":{\"title\":\"Delta Game\",\"xt\":\"TID_D\"}},"
+ "\"owned\":[{\"p\":\"PID_A\",\"t\":\"TID_A\"},{\"p\":\"PID_B\",\"t\":\"TID_B\"},"
+ "{\"p\":\"PID_C\",\"t\":\"TID_C\"},{\"p\":\"PID_D\",\"t\":\"TID_D\"}],"
+ "\"purchasable\":[],\"ownedTitles\":[\"TID_A\",\"TID_B\",\"TID_C\",\"TID_D\"],"
+ "\"ownedProducts\":[\"PID_A\",\"PID_B\",\"PID_C\",\"PID_D\"]}";
+ platform::writeFileAtomic(testDir + "/sort_cache.json", sortJson);
+ CHECK(sortLib.loadCache(false));
+
+ // Without recents or playtime: strictly alphabetical A -> B -> C -> D
+ auto initialOwned = sortLib.owned();
+ CHECK(initialOwned.size() == 4);
+ CHECK(initialOwned[0].productId == "PID_A");
+ CHECK(initialOwned[1].productId == "PID_B");
+ CHECK(initialOwned[2].productId == "PID_C");
+ CHECK(initialOwned[3].productId == "PID_D");
+
+ // Now set playtime for Charlie (7200s) and Delta (3600s)
+ sortLib.setPlaytime({{"PID_C", 7200}, {"PID_D", 3600}});
+ auto ptOwned = sortLib.owned();
+ // Charlie (7200) > Delta (3600) > Alpha (0, alphabetical) > Beta (0, alphabetical)
+ CHECK(ptOwned[0].productId == "PID_C");
+ CHECK(ptOwned[1].productId == "PID_D");
+ CHECK(ptOwned[2].productId == "PID_A");
+ CHECK(ptOwned[3].productId == "PID_B");
+
+ // Now record Beta as most recently played
+ sortLib.recordRecent("PID_B");
+ auto recentOwned = sortLib.owned();
+ // Beta (recent) > Charlie (played 7200) > Delta (played 3600) > Alpha (unplayed)
+ CHECK(recentOwned[0].productId == "PID_B");
+ CHECK(recentOwned[1].productId == "PID_C");
+ CHECK(recentOwned[2].productId == "PID_D");
+ CHECK(recentOwned[3].productId == "PID_A");
+ }
+
+ // 6. AppUi::markTileOwned moves 12-character Store BigID tile from allPurchasable_ to allOwned_ and rebuilds
+ {
+ ui::Fonts dummyFonts;
+ ui::ImageCache images([] {});
+ ui::AppUi appUi(dummyFonts, images);
+ ui::GameTile t1, t2;
+ t1.productId = "PID_OWNED";
+ t1.titleId = "TID_OWNED";
+ t1.name = "Owned Title";
+ t1.playable = true;
+
+ t2.productId = "9P368QBR2C5S";
+ t2.titleId = "TID_JD";
+ t2.name = "Just Dance 2026";
+ t2.playable = false;
+ t2.purchasable = true;
+ t2.freeInStore = true;
+
+ appUi.setOwned({t1}, {t2}, true);
+ ui::GameTile out;
+ CHECK(appUi.firstTile(out));
+ CHECK(out.productId == "PID_OWNED");
+
+ // Before claim, 9P368QBR2C5S is purchasable
+ CHECK(appUi.purchasableAt(0, out));
+ CHECK(out.productId == "9P368QBR2C5S");
+
+ // Claim 9P368QBR2C5S
+ appUi.markTileOwned("9P368QBR2C5S", "TID_JD");
+
+ // Now 9P368QBR2C5S should be in owned, playable, and not purchasable
+ CHECK(!appUi.purchasableAt(0, out)); // purchasable is now empty!
+ CHECK(appUi.findTile("TID_JD", out));
+ CHECK(out.productId == "9P368QBR2C5S");
+ CHECK(out.playable);
+ CHECK(!out.purchasable);
+ CHECK(!out.freeInStore);
+ }
+
+ std::system(("rm -rf " + testDir).c_str());
+}
+
int main() {
+ testBlock4OwnershipAndRecents();
testBloco3Corrections();
testBloco2Corrections();
testBlock1Corrections();