Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

fix(library): show each home row once; restore cached rows only at instant start

Rafael Abud Ferreira0a5a2d6c2d93
diff --git a/src/app/library.cpp b/src/app/library.cpp
index a363c3b..dbf6b25 100644
--- a/src/app/library.cpp
+++ b/src/app/library.cpp
@@ -302,7 +302,7 @@ void Library::sortOwned() {
});
}
-bool Library::loadCache() {
+bool Library::loadCache(bool withLayout) {
std::string text;
if (cachePath_.empty() || !platform::readFile(cachePath_, text)) return false;
auto j = json::parse(text);
@@ -338,7 +338,13 @@ bool Library::loadCache() {
purchasable_.emplace_back(o["p"].str(), o["t"].str());
purchasableSet_.insert(o["p"].str());
}
- if (layout_.empty() && (*j)["layout"].size() > 0) {
+ // The home rows: only for the instant start. load() builds them fresh,
+ // and adding these too showed every row twice (and the cache grew with
+ // each start). A cache written that way shows each row once here, and
+ // the next save keeps only the fresh rows.
+ size_t layoutRows = 0;
+ if (withLayout && layout_.empty()) {
+ std::set<std::string> titles;
for (const auto& rowObj : (*j)["layout"].items()) {
RowIds r;
r.title = rowObj["title"].str();
@@ -348,14 +354,15 @@ bool Library::loadCache() {
for (const auto& it : rowObj["items"].items()) {
r.items.emplace_back(it["p"].str(), it["t"].str());
}
- if (!r.items.empty()) layout_.push_back(std::move(r));
+ if (!r.items.empty() && titles.insert(r.title).second) layout_.push_back(std::move(r));
}
+ layoutRows = layout_.size();
}
- ownershipKnown_ = !ownedTitles_.empty() || !layout_.empty();
+ ownershipKnown_ = !ownedTitles_.empty();
sortOwned();
- XC_LOGI("library: cache: %zu of the account's games, %zu to buy, %zu layout rows, %zu consoles known",
- owned_.size(), purchasable_.size(), layout_.size(), platform_.size());
- return ownershipKnown_;
+ XC_LOGI("library: cache: %zu of the account's games, %zu to buy, %zu home rows, %zu consoles known",
+ owned_.size(), purchasable_.size(), layoutRows, platform_.size());
+ return true;
}
void Library::saveCache() const {
@@ -377,7 +384,9 @@ void Library::saveCache() const {
root.set("gamePass", gamePass); // the next start tells the games added since
root.set("purchasable", itemsToJson(purchasable_));
json::Value layoutArr = json::Value::array();
+ std::set<std::string> layoutTitles;
for (const auto& r : layout_) {
+ if (!layoutTitles.insert(r.title).second) continue; // each row once
json::Value rowObj = json::Value::object();
rowObj.set("title", r.title);
rowObj.set("badges", r.badges);
@@ -448,8 +457,9 @@ bool Library::load(xcloud::GssvClient& gssv, const std::string& language, const
XC_LOGW("%s", e.c_str());
lastErr = e;
}
- // The account's games from the previous run, until loadOwned() refreshes them.
- loadCache();
+ // The account's games from the previous run, until loadOwned() refreshes
+ // them. Not its home rows: those are built below.
+ loadCache(false);
// "Jump back in": recently played games, Game Pass or the account's own
// (rows() leaves out those it can't play; they also go first in "Your
diff --git a/src/app/library.h b/src/app/library.h
index fbf1502..eb32fe5 100644
--- a/src/app/library.h
+++ b/src/app/library.h
@@ -87,7 +87,10 @@ public:
};
Progress progress() const { return progress_; }
const std::string& subscription() const { return subscription_; }
- bool loadCache();
+ // Reads the previous run's cache; true when one was read. `withLayout`
+ // also brings back its home rows: only for the instant start (nothing
+ // loaded yet). load() reads it without them, as it builds the rows fresh.
+ bool loadCache(bool withLayout = false);
void saveCache() const;
private:
diff --git a/src/app/worker.cpp b/src/app/worker.cpp
index 8814dbd..b733128 100644
--- a/src/app/worker.cpp
+++ b/src/app/worker.cpp
@@ -603,7 +603,7 @@ void tryInstantCachedBoot(auth::AuthManager& am) {
std::lock_guard<std::mutex> lock(g_settingsMutex);
library->setFavorites(g_settings.favorites);
}
- if (library->loadCache() && !library->rows().empty()) {
+ if (library->loadCache(true) && !library->rows().empty()) {
{
std::lock_guard<std::mutex> lock(s_libraryMutex);
s_library = library;
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 50a7a3f..6d226b4 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -2,6 +2,7 @@
// Copyright (C) 2026 RafaelNGP
// Offline unit tests for the portable core (no network).
#include "app/auto_deband.h"
+#include "app/library.h"
#include "app/settings.h"
#include "app/update_check.h"
#include "app/updater.h"
@@ -873,6 +874,93 @@ static void testPrivacyAndRobustness() {
CHECK(!app::unzip(storedZip({{"PPSA99810/eboot.bin", "a"}, {"PPSA99810/eboot.bin", "b"}}), "PPSA99810/", files, err));
}
+// The home rows come from the network on every load(): the cache's rows
+// (kept for the instant start) are never added to them, and a cache that
+// holds every row twice heals at the next save.
+static void testLibraryRowsOnce() {
+ std::string dir = "/tmp/xcloud-library-test";
+ std::system(("rm -rf " + dir + " && mkdir -p " + dir).c_str());
+ std::string cache = dir + "/library.json";
+ net::setTestTransport([](const net::Request& req) {
+ net::Response r;
+ r.status = 200;
+ if (req.url.find("/sigls/") != std::string::npos) {
+ std::string id = req.url.substr(req.url.find("id=") + 3);
+ id = id.substr(0, id.find('&'));
+ r.body = R"([{"siglId":")" + id + R"(","title":"List )" + id + R"("},{"id":"P1"},{"id":"P2"}])";
+ } else if (req.url.find("/v3/products") != std::string::npos) {
+ r.body = R"({"Products":{"P1":{"XCloudTitleId":"T1","ProductTitle":"Game One"},)"
+ R"("P2":{"XCloudTitleId":"T2","ProductTitle":"Game Two"}}})";
+ } else {
+ r.status = 503; // the account's titles: not needed here
+ }
+ return r;
+ });
+ auto titlesOf = [](const std::vector<ui::GameRow>& rows) {
+ std::vector<std::string> t;
+ for (const auto& r : rows) t.push_back(r.title);
+ return t;
+ };
+ auto unique = [](std::vector<std::string> t) {
+ std::sort(t.begin(), t.end());
+ return std::adjacent_find(t.begin(), t.end()) == t.end();
+ };
+ auto layoutSize = [&] {
+ std::string text;
+ if (!xc::platform::readFile(cache, text)) return size_t(0);
+ auto j = json::parse(text);
+ return j ? (*j)["layout"].size() : size_t(0);
+ };
+ xcloud::GssvClient gssv;
+ std::string err;
+ size_t fresh = 0;
+ {
+ app::Library lib;
+ lib.setCachePath(cache);
+ CHECK(lib.load(gssv, "en-US", [] {}, err));
+ fresh = lib.rows().size();
+ CHECK(fresh >= 4 && unique(titlesOf(lib.rows())));
+ lib.saveCache();
+ CHECK(layoutSize() == fresh);
+ }
+ {
+ // The next start: the cache has rows, load() still shows each once.
+ app::Library lib;
+ lib.setCachePath(cache);
+ CHECK(lib.load(gssv, "en-US", [] {}, err));
+ CHECK(lib.rows().size() == fresh && unique(titlesOf(lib.rows())));
+ lib.saveCache();
+ CHECK(layoutSize() == fresh);
+ }
+ {
+ // A cache written with every row twice (the old bug).
+ std::string text;
+ CHECK(xc::platform::readFile(cache, text));
+ auto j = json::parse(text);
+ CHECK(j.has_value());
+ if (j) {
+ json::Value doubled = json::Value::array();
+ for (int k = 0; k < 2; ++k)
+ for (const auto& row : (*j)["layout"].items()) doubled.push(row);
+ j->set("layout", doubled);
+ xc::platform::writeFileAtomic(cache, j->dump());
+ }
+ CHECK(layoutSize() == 2 * fresh);
+ app::Library instant; // the instant start: each row once
+ instant.setCachePath(cache);
+ CHECK(instant.loadCache(true));
+ CHECK(instant.rows().size() == fresh && unique(titlesOf(instant.rows())));
+ app::Library lib;
+ lib.setCachePath(cache);
+ CHECK(lib.load(gssv, "en-US", [] {}, err));
+ CHECK(lib.rows().size() == fresh);
+ lib.saveCache();
+ CHECK(layoutSize() == fresh); // healed
+ }
+ net::setTestTransport({});
+ std::system(("rm -rf " + dir).c_str());
+}
+
static void testStoreLeftovers() {
// ProsperoStore replaced the app folder and kept the old one: the
// sign-in and settings come back from it, once.
@@ -1930,6 +2018,7 @@ int main() {
testLoginCountry();
testPrivacyAndRobustness();
testStoreLeftovers();
+ testLibraryRowsOnce();
testHostPacer();
testSubscriptionPlan();
testMicrosoftHost();