Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Fill in any game's page on demand; titlehub 404s no longer drop batches

Rafael Abud Ferreiraf5fefb04eb4c
diff --git a/src/app/cli_main.cpp b/src/app/cli_main.cpp
index db81827..3cd9636 100644
--- a/src/app/cli_main.cpp
+++ b/src/app/cli_main.cpp
@@ -360,13 +360,13 @@ int cmdUiPreview(auth::AuthManager& am, const std::string& dir) {
}
save("library_buy", 3000);
press([](ui::NavInput& n) { n.accept = true; }, 1);
- if (std::string id = app.detailWanted(); !id.empty()) { // as the app's price thread does
+ if (std::string id = app.detailWanted(platform::nowMs()); !id.empty()) { // as the app's price thread does
std::map<std::string, xcloud::Product> full;
std::string e;
if (xcloud::fetchProducts({id}, gssv.session().market.empty() ? "US" : gssv.session().market,
ui::catalogLanguage(), full, e, true) &&
full.count(id))
- app.setDetailInfo(id, full[id].description, full[id].publisher, full[id].categories);
+ app.setDetailInfo(id, full[id].description, full[id].publisher, full[id].categories, full[id].heroUrl);
}
save("details_buy", 2000);
press([](ui::NavInput& n) { n.back = true; }, 1);
diff --git a/src/app/ps5_main.cpp b/src/app/ps5_main.cpp
index 3a9b99e..90bab04 100644
--- a/src/app/ps5_main.cpp
+++ b/src/app/ps5_main.cpp
@@ -75,7 +75,8 @@ app::StreamPlayer* g_player = nullptr;
// in, plays that title for that long (pressing A at 15 s and 20 s), saves
// decoded frames and logs "AUTOPLAY END". Options, comma-separated: nosimd,
// dump, repeat, idle (no A presses), threads=N (H.264 decoder threads),
-// rumbletest (rumbles the pad for 1.5 s at start).
+// rumbletest (rumbles the pad for 1.5 s at start), detailtest (opens a game to
+// buy far down the list instead of playing, saves detail.ppm).
// The title "BENCH" decodes <dataDir>/sample.h264 instead.
std::string g_autoplayTitle;
@@ -83,6 +84,7 @@ int g_autoplaySeconds = 0;
bool g_autoplayDump = false;
int g_autoplayRuns = 1;
bool g_autoplayIdle = false;
+bool g_autoplayDetailTest = false; // open a game to buy far down the list, save its page
int g_decodeThreads = 1;
std::atomic<bool> g_syntheticA{false};
@@ -104,6 +106,7 @@ void loadAutoplay() {
if (opt == "repeat") g_autoplayRuns = 2;
if (opt == "idle") g_autoplayIdle = true;
if (opt == "rumbletest") input::setRumble(200, 200, 1500);
+ if (opt == "detailtest") g_autoplayDetailTest = true;
if (opt.rfind("threads=", 0) == 0) g_decodeThreads = std::atoi(opt.c_str() + 8);
}
g_autoplayTitle = title;
@@ -375,13 +378,15 @@ void priceLoop() {
}
// The open page's description, for games that only have the light
// catalog data (games to buy, search results).
- if (std::string id = market.empty() ? std::string() : g_ui->detailWanted(); !id.empty()) {
+ if (std::string id = market.empty() ? std::string() : g_ui->detailWanted(platform::nowMs()); !id.empty()) {
std::map<std::string, xcloud::Product> full;
std::string err;
- if (xcloud::fetchProducts({id}, market, language, full, err, true) && full.count(id)) {
+ bool ok = xcloud::fetchProducts({id}, market, language, full, err, true) && full.count(id);
+ if (ok) {
const auto& p = full[id];
- g_ui->setDetailInfo(id, p.description, p.publisher, p.categories);
+ g_ui->setDetailInfo(id, p.description, p.publisher, p.categories, p.heroUrl);
}
+ XC_LOGI("details of %s: %s", id.c_str(), ok ? "ok" : err.empty() ? "not in the catalog" : err.c_str());
}
std::vector<std::string> ids = market.empty() ? std::vector<std::string>() : g_ui->pricesWanted(20);
if (ids.empty()) {
@@ -480,7 +485,7 @@ void loadLibrary(xcloud::GssvClient& gssv) {
library->loadPlatforms(xblAuth, changed, &g_stopHydration);
library->hydrate(changed, &g_stopHydration);
});
- if (!g_autoplayTitle.empty() && g_autoplayTitle != "BENCH") {
+ if (!g_autoplayTitle.empty() && g_autoplayTitle != "BENCH" && !g_autoplayDetailTest) {
platform::sleepMs(6000); // leave the home screen up for ui.ppm
ui::GameTile tile;
tile.titleId = g_autoplayTitle;
@@ -744,6 +749,21 @@ int main(int argc, char** argv) {
saveCanvas("launch.ppm");
}
}
+ if (g_autoplayDetailTest && uiSaved) {
+ // A game to buy far down the list (no prefetched details): its
+ // page must fill in on its own.
+ static uint64_t openedAt = 0;
+ ui::GameTile tile;
+ if (!openedAt && g_ui->purchasableAt(40, tile)) {
+ XC_LOGI("autoplay: opening %s (%s)", tile.name.c_str(), tile.productId.c_str());
+ g_ui->showDetails(tile);
+ openedAt = now;
+ } else if (openedAt && now - openedAt > 8000) {
+ saveCanvas("detail.ppm");
+ g_autoplayDetailTest = false;
+ XC_LOGI("AUTOPLAY END: detail test");
+ }
+ }
if (!g_autoplayTitle.empty() && !uiSaved && g_ui->screen() == ui::Screen::Home) {
if (!homeSince) homeSince = now;
if (now - homeSince > 5000) {
diff --git a/src/ui/app_ui.cpp b/src/ui/app_ui.cpp
index 7b37573..868f25f 100644
--- a/src/ui/app_ui.cpp
+++ b/src/ui/app_ui.cpp
@@ -267,22 +267,40 @@ std::vector<std::string> AppUi::pricesWanted(size_t max) {
return out;
}
-std::string AppUi::detailWanted() {
+std::string AppUi::detailWanted(uint64_t nowMs) {
std::lock_guard<std::mutex> lock(mutex_);
- if (screen_ != Screen::Details || detail_.productId.empty() || !detail_.description.empty()) return {};
- return detailsAsked_.insert(detail_.productId).second ? detail_.productId : std::string();
+ if (screen_ != Screen::Details || detail_.productId.empty()) return {};
+ if (!detail_.description.empty() && !detail_.heroUrl.empty()) return {};
+ if (detail_.productId == detailAskedFor_ && nowMs - detailAskedAt_ < 5000) return {};
+ detailAskedFor_ = detail_.productId;
+ detailAskedAt_ = nowMs;
+ return detail_.productId;
}
void AppUi::setDetailInfo(const std::string& productId, const std::string& description, const std::string& publisher,
- const std::vector<std::string>& categories) {
+ const std::vector<std::string>& categories, const std::string& heroUrl) {
std::lock_guard<std::mutex> lock(mutex_);
+ detailInfo_[productId] = {description, publisher, heroUrl, categories};
if (detail_.productId != productId) return;
if (!description.empty()) detail_.description = description;
if (!publisher.empty()) detail_.publisher = publisher;
if (!categories.empty()) detail_.categories = categories;
+ if (!heroUrl.empty()) detail_.heroUrl = heroUrl;
dirty_ = true;
}
+bool AppUi::purchasableAt(size_t index, GameTile& out) const {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (index >= purchasable_.size()) return false;
+ out = purchasable_[index];
+ return true;
+}
+
+void AppUi::showDetails(const GameTile& tile) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ openDetails(tile);
+}
+
const GameTile* AppUi::libraryTile(int index) const {
if (index < 0) return nullptr;
size_t i = static_cast<size_t>(index);
@@ -299,6 +317,13 @@ Tab AppUi::tab() const {
void AppUi::openDetails(const GameTile& tile) {
// Caller holds mutex_.
detail_ = tile;
+ auto info = detailInfo_.find(tile.productId);
+ if (info != detailInfo_.end()) {
+ if (detail_.description.empty()) detail_.description = info->second.description;
+ if (detail_.heroUrl.empty()) detail_.heroUrl = info->second.heroUrl;
+ if (detail_.publisher.empty()) detail_.publisher = info->second.publisher;
+ if (detail_.categories.empty()) detail_.categories = info->second.categories;
+ }
screen_ = Screen::Details;
dirty_ = true;
}
diff --git a/src/ui/app_ui.h b/src/ui/app_ui.h
index 5406e2a..e4797ea 100644
--- a/src/ui/app_ui.h
+++ b/src/ui/app_ui.h
@@ -99,12 +99,17 @@ public:
// Games to buy on screen (or about to be) whose price hasn't been asked
// for yet; marks them asked.
std::vector<std::string> pricesWanted(size_t max);
- // The game whose page is open, if its description hasn't arrived: asked
- // once per game (empty otherwise).
- std::string detailWanted();
- // Fills in the open page when it shows `productId`.
+ // The game whose page is open, if its description or hero art hasn't
+ // arrived; asked again every 5 s while missing (empty otherwise).
+ std::string detailWanted(uint64_t nowMs);
+ // Fills in the open page when it shows `productId`, and keeps the details
+ // for the next time that game's page opens.
void setDetailInfo(const std::string& productId, const std::string& description, const std::string& publisher,
- const std::vector<std::string>& categories);
+ const std::vector<std::string>& categories, const std::string& heroUrl);
+ // Opens a game's page (autoplay tests).
+ void showDetails(const GameTile& tile);
+ // The `index`-th game to buy, if loaded (autoplay tests).
+ bool purchasableAt(size_t index, GameTile& out) const;
void showHome(const std::string& toast = {});
void showLaunching(const GameTile& game, const std::string& status);
void setLaunchStatus(const std::string& status);
@@ -207,7 +212,14 @@ private:
std::string defaultRegion_;
std::map<std::string, int> regionMs_;
std::map<std::string, std::pair<std::string, std::string>> prices_;
- std::set<std::string> pricesAsked_, detailsAsked_;
+ std::set<std::string> pricesAsked_;
+ std::string detailAskedFor_;
+ uint64_t detailAskedAt_ = 0;
+ struct DetailInfo {
+ std::string description, publisher, heroUrl;
+ std::vector<std::string> categories;
+ };
+ std::map<std::string, DetailInfo> detailInfo_; // fetched on demand, by product id
int settingsRow_ = 0;
uint64_t signOutHoldStart_ = 0; // TOUCHPAD hold in progress
bool signOutLatched_ = false; // fired; wait for release
diff --git a/src/xcloud/titlehub.cpp b/src/xcloud/titlehub.cpp
index f9804cd..42fd44e 100644
--- a/src/xcloud/titlehub.cpp
+++ b/src/xcloud/titlehub.cpp
@@ -9,48 +9,72 @@
namespace xc::xcloud {
+namespace {
+
+// One request. A single id titlehub doesn't know fails the whole batch with
+// HTTP 404: then the batch is split until the bad id is alone (and dropped).
+bool fetchBatch(const std::string& auth, const std::vector<std::string>& ids, std::map<std::string, std::string>& out,
+ std::string& err) {
+ json::Value list = json::Value::array();
+ for (const auto& id : ids) list.push(id);
+ json::Value body = json::Value::object();
+ body.set("pfns", nullptr);
+ body.set("titleIds", list);
+ net::Request req;
+ req.method = "POST";
+ req.url = "https://titlehub.xboxlive.com/titles/batch/decoration/detail";
+ req.headers = {{"Authorization", auth},
+ {"x-xbl-contract-version", "2"},
+ {"Content-Type", "application/json"},
+ {"Accept-Language", "en-US"}};
+ req.body = body.dump();
+ auto r = net::perform(req);
+ if (r.status == 404 || r.status == 400) {
+ if (ids.size() == 1) { // that id is unknown: remembered as such, no badge
+ out[ids[0]] = "";
+ return true;
+ }
+ size_t half = ids.size() / 2;
+ std::vector<std::string> a(ids.begin(), ids.begin() + static_cast<long>(half));
+ std::vector<std::string> b(ids.begin() + static_cast<long>(half), ids.end());
+ return fetchBatch(auth, a, out, err) && fetchBatch(auth, b, out, err);
+ }
+ auto j = json::parse(r.body);
+ if (!r.ok() || !j) {
+ err = "titlehub: " + (r.status ? "HTTP " + std::to_string(r.status) : r.error);
+ return false;
+ }
+ for (const auto& t : (*j)["titles"].items()) {
+ bool has360 = false, hasOne = false, hasSeries = false, optimized = false;
+ for (const auto& d : t["devices"].items()) {
+ has360 |= d.str() == "Xbox360";
+ hasOne |= d.str() == "XboxOne";
+ hasSeries |= d.str() == "XboxSeries";
+ }
+ for (const auto& a : t["detail"]["attributes"].items()) optimized |= a["name"].str() == "ConsoleGen9Optimized";
+ // No console in "devices": the id is the PC / Play Anywhere entry
+ // (ARK, #IDARB). Every cloud title is a console game, and one that
+ // isn't marked for Series X|S is the Xbox One version.
+ const char* code = has360 ? kPlatform360
+ : optimized || (hasSeries && !hasOne) ? kPlatformSeries
+ : kPlatformOne;
+ // The id asked for may be an older one titlehub maps to its current
+ // id: record both.
+ out[t["titleId"].str()] = code;
+ if (!t["modernTitleId"].str().empty()) out[t["modernTitleId"].str()] = code;
+ }
+ return true;
+}
+
+} // namespace
+
bool fetchPlatforms(const std::string& auth, const std::vector<std::string>& xboxTitleIds,
std::map<std::string, std::string>& out, std::string& err) {
constexpr size_t kBatch = 100;
for (size_t i = 0; i < xboxTitleIds.size(); i += kBatch) {
- json::Value ids = json::Value::array();
- for (size_t k = i; k < xboxTitleIds.size() && k < i + kBatch; ++k) ids.push(xboxTitleIds[k]);
- json::Value body = json::Value::object();
- body.set("pfns", nullptr);
- body.set("titleIds", ids);
- net::Request req;
- req.method = "POST";
- req.url = "https://titlehub.xboxlive.com/titles/batch/decoration/detail";
- req.headers = {{"Authorization", auth},
- {"x-xbl-contract-version", "2"},
- {"Content-Type", "application/json"},
- {"Accept-Language", "en-US"}};
- req.body = body.dump();
- auto r = net::perform(req);
- auto j = json::parse(r.body);
- if (!r.ok() || !j) {
- err = "titlehub: " + (r.status ? "HTTP " + std::to_string(r.status) : r.error);
- return false;
- }
- for (const auto& t : (*j)["titles"].items()) {
- bool has360 = false, hasOne = false, hasSeries = false, optimized = false;
- for (const auto& d : t["devices"].items()) {
- has360 |= d.str() == "Xbox360";
- hasOne |= d.str() == "XboxOne";
- hasSeries |= d.str() == "XboxSeries";
- }
- for (const auto& a : t["detail"]["attributes"].items()) optimized |= a["name"].str() == "ConsoleGen9Optimized";
- // No console in "devices": the id is the PC / Play Anywhere entry
- // (ARK, #IDARB). Every cloud title is a console game, and one that
- // isn't marked for Series X|S is the Xbox One version.
- const char* code = has360 ? kPlatform360
- : optimized || (hasSeries && !hasOne) ? kPlatformSeries
- : kPlatformOne;
- // The id asked for may be an older one titlehub maps to its
- // current id: record both.
- out[t["titleId"].str()] = code;
- if (!t["modernTitleId"].str().empty()) out[t["modernTitleId"].str()] = code;
- }
+ std::vector<std::string> batch(xboxTitleIds.begin() + static_cast<long>(i),
+ xboxTitleIds.begin() + static_cast<long>(std::min(xboxTitleIds.size(), i + kBatch)));
+ if (!fetchBatch(auth, batch, out, err)) return false;
}
return true;
}
diff --git a/tools/ps5/autotest.sh b/tools/ps5/autotest.sh
index f353904..f15cf3f 100755
--- a/tools/ps5/autotest.sh
+++ b/tools/ps5/autotest.sh
@@ -37,7 +37,7 @@ import io, sys
from ftplib import FTP, error_perm, error_reply
host, port, d, line = sys.argv[1], int(sys.argv[2]), sys.argv[3], sys.argv[4]
f = FTP(); f.connect(host, port, timeout=15); f.login(); f.cwd(d)
-for name in ("xcloud.log", "frame.ppm", "frame2.ppm", "stream.aus", "ui.ppm", "launch.ppm", "screen.ppm"):
+for name in ("xcloud.log", "frame.ppm", "frame2.ppm", "stream.aus", "ui.ppm", "launch.ppm", "screen.ppm", "detail.ppm"):
try: f.sendcmd(f"DELE {name}")
except (error_perm, error_reply): pass
f.storbinary("STOR autoplay.txt", io.BytesIO(line.encode() + b"\n"))
@@ -82,7 +82,7 @@ PY
if [[ $status != *"$title_id"* ]]; then result=crashed; break; fi
fi
done
-fetch xcloud.log frame.ppm frame2.ppm stream.aus ui.ppm launch.ppm screen.ppm || true
+fetch xcloud.log frame.ppm frame2.ppm stream.aus ui.ppm launch.ppm screen.ppm detail.ppm || true
ctl kill "$title_id" >/dev/null 2>&1 || true
# Remove the request so a manual launch behaves normally.
python3 - "$PS5_HOST" "${FTP_PORT:-2121}" "/data/homebrew/$title_id" <<'PY'