Xbox Cloud Gaming app for PS5 Homebrew
C
fix(library): eliminate redundant title list network query, fix small library cache preservation, and restore cache continuity on reload
Rafael Abud Ferreirae70826885442
diff --git a/src/app/library.cpp b/src/app/library.cppindex 8260f23..1ec95b9 100644--- a/src/app/library.cpp+++ b/src/app/library.cpp@@ -639,10 +639,14 @@ bool Library::loadCache(bool withLayout) {}}}- bool corruptedOwned = (owned_.size() <= 37 && ownedTitles_.size() > 100);+ size_t codCount = 0;+ for (const auto& o : owned_) {+ if (o.second.find("CALLOFDUTY") != std::string::npos) ++codCount;+ }+ bool corruptedOwned = (codCount > 20);if (corruptedOwned) {- XC_LOGW("library: cache owned has only %zu items despite %zu streamable titles, invalidating cached ownership",- owned_.size(), ownedTitles_.size());+ XC_LOGW("library: cache owned has %zu duplicate Call of Duty entries (v1.5.0 bug), invalidating cached ownership",+ codCount);owned_.clear();ownershipKnown_ = false;} else {@@ -791,6 +795,7 @@ size_t Library::syncDelta(xcloud::GssvClient& gssv, const Changed& changed, std:gamePass_.clear();gamePass_.insert(allSigl.productIds.begin(), allSigl.productIds.end());allGames_ = allSigl.productIds;+ seedKnownGames(cloudTitleOf_, xboxTitleOf_, freeInStore_, gamePass_, allGames_, products_);for (const auto& pid : allSigl.productIds) {if (!pid.empty() && !products_.count(pid)) {newProducts.push_back(pid);@@ -831,7 +836,7 @@ size_t Library::syncDelta(xcloud::GssvClient& gssv, const Changed& changed, std:}}}- loadOwned(gssv, changed);+ loadOwned(gssv, changed, nullptr, &titles);loadCatalogNames(changed, nullptr);saveCache();changed();@@ -872,21 +877,9 @@ bool Library::load(xcloud::GssvClient& gssv, const std::string& language, constlastErr = e;}seedKnownGames(cloudTitleOf_, xboxTitleOf_, freeInStore_, gamePass_, allGames_, products_);- if (!forceRefresh) {- // The account's games from the previous run, until loadOwned() refreshes- // them. Not its home rows: those are built below.- loadCache(false);- } else {- // Force refresh: read previous gamePass set for new additions delta, but bypass cached product tiles- std::string text;- if (!cachePath_.empty() && platform::readFile(cachePath_, text)) {- if (auto j = json::parse(text)) {- for (const auto& p : (*j)["gamePass"].items()) knownGamePass_.insert(p.str());- for (const auto& t : (*j)["ownedTitles"].items()) ownedTitles_.insert(t.str());- for (const auto& p : (*j)["ownedProducts"].items()) ownedProducts_.insert(p.str());- }- }- }+ // 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@@ -1013,15 +1006,21 @@ bool Library::load(xcloud::GssvClient& gssv, const std::string& language, constreturn true;}-void Library::loadOwned(xcloud::GssvClient gssv, const Changed& changed, const std::atomic<bool>* stop) {+void Library::loadOwned(xcloud::GssvClient gssv, const Changed& changed, const std::atomic<bool>* stop,+ const std::vector<xcloud::Title>* preloadedTitles) {progress_ = {true, 0, 0}; // the account's list: no measure until the nameschanged();- std::vector<xcloud::Title> titles;std::string err;- if (!gssv.listTitles(titles, err, false)) {- XC_LOGW("%s", err.c_str()); // keep what the cache said- return;+ std::vector<xcloud::Title> fetchedTitles;+ const std::vector<xcloud::Title>* titlesPtr = preloadedTitles;+ if (!titlesPtr) {+ if (!gssv.listTitles(fetchedTitles, err, false)) {+ XC_LOGW("%s", err.c_str()); // keep what the cache said+ return;+ }+ titlesPtr = &fetchedTitles;}+ const auto& titles = *titlesPtr;if (stop && *stop) return;std::set<std::string> ownedTitles, ownedProducts;std::vector<Item> mine;diff --git a/src/app/library.h b/src/app/library.hindex fcd3be6..1f4655b 100644--- a/src/app/library.h+++ b/src/app/library.h@@ -58,7 +58,8 @@ public:void loadFirstScreen(const std::string& xblAuth, const Changed& changed, const std::atomic<bool>* stop = nullptr);// The account's titles: what it can play, what it could buy. Takes its own// GssvClient (a copy) so it can run on another thread.- void loadOwned(xcloud::GssvClient gssv, const Changed& changed, const std::atomic<bool>* stop = nullptr);+ void loadOwned(xcloud::GssvClient gssv, const Changed& changed, const std::atomic<bool>* stop = nullptr,+ const std::vector<xcloud::Title>* preloadedTitles = nullptr);// Names and art of the whole Game Pass catalog and of the games to buy.void loadCatalogNames(const Changed& changed, const std::atomic<bool>* stop = nullptr);// Console generation of every known game; `xblAuth` from the profile.diff --git a/src/app/worker.cpp b/src/app/worker.cppindex b12e9ce..77fd611 100644--- a/src/app/worker.cpp+++ b/src/app/worker.cpp@@ -892,7 +892,7 @@ void loadLibrary(auth::AuthManager& am, xcloud::GssvClient& gssv, bool forceRefrexistingLib = s_library;}- if (!forceRefresh && existingLib && existingLib->ownedKnown() && !existingLib->owned().empty()) {+ if (!forceRefresh && existingLib && existingLib->ownedKnown()) {XC_LOGI("library: delta-sync against active library (forceRefresh=%d)", forceRefresh);g_ui->setLoading(true, 0, 0);auto publish = [](const app::Library& lib) {diff --git a/tests/test_main.cpp b/tests/test_main.cppindex 8d4c511..8748cae 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -5122,7 +5122,7 @@ static void testCallOfDutyModernWarfare4CatalogAndRefresh() {CHECK(libOwnedAll.owned().size() >= 4);}- // 8. Test loadCache auto-healing of contaminated gamePass and truncated owned+ // 8. Test loadCache auto-healing of contaminated gamePass and duplicate COD entries{std::string badCache = testDir + "/bad_cache.json";std::string gpItems = "";@@ -5135,20 +5135,46 @@ static void testCallOfDutyModernWarfare4CatalogAndRefresh() {if (i > 0) otItems += ",";otItems += "\"T_FAKE_" + std::to_string(i) + "\"";}+ std::string codItems = "";+ for (int i = 0; i < 25; ++i) {+ if (i > 0) codItems += ",";+ codItems += "{\"p\":\"COD_" + std::to_string(i) + "\",\"t\":\"CALLOFDUTYHQ\"}";+ }std::string badJson = "{\"version\":2,\"market\":\"US\",\"language\":\"en-US\",\"products\":{},"- "\"owned\":[{\"p\":\"COD\",\"t\":\"CALLOFDUTYHQ\"},{\"p\":\"FORTNITE\",\"t\":\"FORTNITE\"}],"+ "\"owned\":[" + codItems + ",{\"p\":\"FORTNITE\",\"t\":\"FORTNITE\"}],""\"purchasable\":[],\"ownedTitles\":[" + otItems + "],\"ownedProducts\":[],\"gamePass\":[" + gpItems + "]}";platform::writeFileAtomic(badCache, badJson);app::Library healLib;healLib.setCachePath(badCache);CHECK(healLib.loadCache(false));- // Verify ownership was invalidated due to truncated owned list vs 150 titles+ // Verify ownership was invalidated due to duplicate COD entries from v1.5.0 bugCHECK(!healLib.ownedKnown());// Verify contaminated gamePass was discardedCHECK(healLib.gamePassSearchPool().empty());}+ // 9. Test loadCache preserves legitimate cache for users with few owned games (<= 37) and Game Pass+ {+ std::string legitCache = testDir + "/legit_cache.json";+ std::string otItems = "";+ for (int i = 0; i < 200; ++i) {+ if (i > 0) otItems += ",";+ otItems += "\"T_FAKE_" + std::to_string(i) + "\"";+ }+ std::string legitJson = "{\"version\":2,\"market\":\"US\",\"language\":\"en-US\",\"products\":{},"+ "\"owned\":[{\"p\":\"FORTNITE\",\"t\":\"FORTNITE\"},{\"p\":\"CYBERPUNK\",\"t\":\"CYBERPUNK2077\"}],"+ "\"purchasable\":[],\"ownedTitles\":[" + otItems + "],\"ownedProducts\":[\"FORTNITE\",\"CYBERPUNK\"],"+ "\"gamePass\":[\"GP_GAME1\",\"GP_GAME2\"]}";+ platform::writeFileAtomic(legitCache, legitJson);++ app::Library legitLib;+ legitLib.setCachePath(legitCache);+ CHECK(legitLib.loadCache(false));+ // Verify legitimate ownership with <= 37 games is PRESERVED, not discarded!+ CHECK(legitLib.ownedKnown());+ }+net::setTestTransport({});std::system(("rm -rf " + testDir).c_str());}