Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

fix(trophies, ps5): fix settings persistence, unjail module loading, and recursive trophy removal

Rafael Abud Ferreirae78c2f4da01b
diff --git a/src/app/app_core.cpp b/src/app/app_core.cpp
index 28a50b7..71818c8 100644
--- a/src/app/app_core.cpp
+++ b/src/app/app_core.cpp
@@ -20,6 +20,7 @@
#include "app/stream_player.h"
#include "app/stream_screen.h"
#include "app/web_server.h"
+#include "app/worker_internal.h"
#include "display/display.h"
#include "input/controller.h"
#include "input/input_ext.h"
@@ -843,15 +844,16 @@ void Core::dispatchUiAction(const ui::UiEvent& ev) {
break;
}
case ui::Action::SyncTrophies: {
- XC_LOGI("trophy sync: requested from Settings");
+ saveSettings(ev.settings);
+ XC_LOGI("trophy sync: requested from Settings (saved syncTrophies=%d)", g_settings.syncTrophies ? 1 : 0);
int idle = kNone;
if (!g_command.compare_exchange_strong(idle, kSyncTrophies)) XC_LOGW("trophy sync: worker is busy");
break;
}
case ui::Action::RemoveTrophies: {
- XC_LOGI("trophy remove: requested from Settings");
- int idle = kNone;
- if (!g_command.compare_exchange_strong(idle, kRemoveTrophies)) XC_LOGW("trophy remove: worker is busy");
+ saveSettings(ev.settings);
+ XC_LOGI("trophy remove: requested from Settings (saved syncTrophies=%d)", g_settings.syncTrophies ? 1 : 0);
+ std::thread([]() { runRemoveTrophies(); }).detach();
break;
}
case ui::Action::TestQueueAlert: {
diff --git a/src/app/autoplay.cpp b/src/app/autoplay.cpp
index ef4f18d..0dae56f 100644
--- a/src/app/autoplay.cpp
+++ b/src/app/autoplay.cpp
@@ -889,6 +889,7 @@ void autoplayNav(ui::NavInput& nav, bool& menuCombo, uint64_t now) {
static int step = 0;
if (!since) since = now;
static SettingsWalk walk;
+ static SettingsWalk walk2;
if (step == 0 && now - since >= 500) {
nav.options = true; // Open Settings
step++;
@@ -915,8 +916,7 @@ void autoplayNav(ui::NavInput& nav, bool& menuCombo, uint64_t now) {
trophyEnabledShot = true; // Capture toggled ON
step++;
} else if (step == 7 && now - since >= 10500) {
- nav.down = true; // Move to row 25 (Remove Trophies)
- step++;
+ if (walk2.step(nav, 1)) step++; // Move to row 25 (Remove Trophies)
} else if (step == 8 && now - since >= 12000) {
if (onSettingsRow(25, "trophy remove test")) {
nav.accept = true; // Open remove confirm modal
@@ -929,12 +929,15 @@ void autoplayNav(ui::NavInput& nav, bool& menuCombo, uint64_t now) {
trophyRemoveConfirmShot = true; // Capture remove confirm modal
step++;
} else if (step == 10 && now - since >= 15000) {
- nav.accept = true; // Confirm removal ("Remover")
+ nav.left = true; // Move focus from Cancel (1) to Remover (0)
step++;
} else if (step == 11 && now - since >= 16500) {
- trophyRemovedToastShot = true; // Capture toast notification
+ nav.accept = true; // Confirm removal ("Remover")
step++;
} else if (step == 12 && now - since >= 18000) {
+ trophyRemovedToastShot = true; // Capture toast notification
+ step++;
+ } else if (step == 13 && now - since >= 19500) {
nav.back = true;
g_ui->showHome();
g_autoplay.trophyTest = false;
diff --git a/src/platform/ps4/trophy_backend_ps4.cpp b/src/platform/ps4/trophy_backend_ps4.cpp
index 1876d2b..0f4089d 100644
--- a/src/platform/ps4/trophy_backend_ps4.cpp
+++ b/src/platform/ps4/trophy_backend_ps4.cpp
@@ -138,6 +138,17 @@ private:
}
}
+ bool removeAllTrophies(const std::vector<std::pair<std::string, std::string>>& titles, int& removedCount, std::string& err) override {
+ (void)err;
+ std::lock_guard<std::mutex> lock(mutex_);
+ cleanupContext();
+ removedCount = static_cast<int>(registeredTitles_.size() > 0 ? registeredTitles_.size() : titles.size());
+ registeredTitles_.clear();
+ unlockedTrophies_.clear();
+ XC_LOGI("trophy ps4: removeAllTrophies cleared %d titles", removedCount);
+ return true;
+ }
+
bool initialized_ = false;
uint32_t userId_ = 0x10000000;
int32_t context_ = -1;
diff --git a/src/platform/ps5/trophy_backend_ps5.cpp b/src/platform/ps5/trophy_backend_ps5.cpp
index b7d0c31..d81bc1b 100644
--- a/src/platform/ps5/trophy_backend_ps5.cpp
+++ b/src/platform/ps5/trophy_backend_ps5.cpp
@@ -7,6 +7,7 @@
#include "util/log.h"
#include <arpa/inet.h>
+#include <dirent.h>
#include <fcntl.h>
#include <map>
#include <mutex>
@@ -46,7 +47,15 @@ namespace {
std::string systemLib(const char* name) {
const char* word = sceKernelGetFsSandboxRandomWord();
- return std::string("/") + (word ? word : "") + "/common/lib/" + name;
+ if (word && *word) {
+ std::string jailed = std::string("/") + word + "/common/lib/" + name;
+ struct stat st{};
+ if (::stat(jailed.c_str(), &st) == 0) return jailed;
+ }
+ std::string sys = std::string("/system/common/lib/") + name;
+ struct stat st{};
+ if (::stat(sys.c_str(), &st) == 0) return sys;
+ return std::string("/common/lib/") + name;
}
void triggerSceShellCorePatch() {
@@ -99,6 +108,30 @@ bool waitForUnjail(int timeoutMs) {
return false;
}
+void rmTree(const std::string& path) {
+ DIR* d = ::opendir(path.c_str());
+ if (!d) {
+ ::unlink(path.c_str());
+ return;
+ }
+ struct dirent* de;
+ while ((de = ::readdir(d)) != nullptr) {
+ std::string name = de->d_name;
+ if (name == "." || name == "..") continue;
+ std::string sub = path + "/" + name;
+ struct stat st{};
+ if (::lstat(sub.c_str(), &st) == 0) {
+ if (S_ISDIR(st.st_mode)) {
+ rmTree(sub);
+ } else {
+ ::unlink(sub.c_str());
+ }
+ }
+ }
+ ::closedir(d);
+ ::rmdir(path.c_str());
+}
+
} // namespace
class TrophyBackendPS5 : public ITrophyBackend {
@@ -185,6 +218,12 @@ public:
// Re-trigger patch to guarantee client-side libSceNpTrophy2 and SceShellCore are patched
triggerSceShellCorePatch();
+ if (trophyModule_ < 0) loadNpTrophyModule();
+ if (trophyModule_ < 0) {
+ XC_LOGW("trophy ps5: cannot register title: libSceNpTrophy2.sprx not loaded");
+ return false;
+ }
+
int rc = sceNpTrophy2CreateContext(&context_, static_cast<int32_t>(userId_), 1, 0);
XC_LOGI("trophy ps5: sceNpTrophy2CreateContext(user=0x%08x, serviceLabel=1) -> rc=0x%08x, context=%d",
userId_, static_cast<unsigned>(rc), context_);
@@ -336,6 +375,7 @@ public:
bool removeAllTrophies(const std::vector<std::pair<std::string, std::string>>& titles, int& removedCount, std::string& err) override {
std::lock_guard<std::mutex> lock(mutex_);
+ if (!initialized_) init(0);
removedCount = 0;
triggerSceShellCorePatch();
@@ -347,22 +387,18 @@ public:
if (!npComm.empty()) {
char dataDir[256];
snprintf(dataDir, sizeof(dataDir), "/user/home/%08x/trophy2/nobackup/data/%s", userId_, npComm.c_str());
- std::string datFile = std::string(dataDir) + "/TRPTITLE.DAT";
struct stat st{};
- if (::stat(datFile.c_str(), &st) == 0) {
- ::unlink(datFile.c_str());
- ::rmdir(dataDir);
+ if (::stat(dataDir, &st) == 0) {
+ rmTree(dataDir);
foundAny = true;
- XC_LOGI("trophy ps5 remove: deleted %s", datFile.c_str());
+ XC_LOGI("trophy ps5 remove: purged %s", dataDir);
}
std::string confDir = "/user/trophy2/nobackup/conf/" + npComm;
- std::string ucpFile = confDir + "/TROPHY.UCP";
- if (::stat(ucpFile.c_str(), &st) == 0) {
- ::unlink(ucpFile.c_str());
- ::rmdir(confDir.c_str());
+ if (::stat(confDir.c_str(), &st) == 0) {
+ rmTree(confDir);
foundAny = true;
- XC_LOGI("trophy ps5 remove: deleted %s", ucpFile.c_str());
+ XC_LOGI("trophy ps5 remove: purged %s", confDir.c_str());
}
}
@@ -373,24 +409,33 @@ public:
"/user/appmeta/PPSA99810/trophy2",
"/user/app/PPSA99810/sce_sys/trophy2",
"/data/homebrew/PPSA99810/sce_sys/trophy2",
- "/system_data/game/temp/PPSA99810/trophy2"
+ "/system_data/game/temp/PPSA99810/trophy2",
+ "/system_data/game/temp/PPSA99810"
};
for (const auto& d : appMetaDirs) {
- ::unlink((d + "/trophy00.ucp").c_str());
- ::unlink((d + "/npbind.dat").c_str());
- ::rmdir(d.c_str());
+ struct stat st{};
+ if (::stat(d.c_str(), &st) == 0) {
+ rmTree(d);
+ foundAny = true;
+ }
}
+ ::unlink("/system_data/priv/appmeta/PPSA99810/npbind.dat");
+ ::unlink("/user/appmeta/PPSA99810/npbind.dat");
+ ::unlink("/system_data/game/temp/PPSA99810/npbind.dat");
} else {
const std::string titleDirs[] = {
"/system_data/priv/appmeta/" + sonyTitle,
"/user/appmeta/" + sonyTitle,
- "/user/app/" + sonyTitle + "/sce_sys"
+ "/user/app/" + sonyTitle + "/sce_sys",
+ "/user/app/" + sonyTitle,
+ "/system_data/game/temp/" + sonyTitle
};
for (const auto& d : titleDirs) {
- ::unlink((d + "/param.json").c_str());
- ::unlink((d + "/nptitle.dat").c_str());
- ::unlink((d + "/icon0.png").c_str());
- ::rmdir(d.c_str());
+ struct stat st{};
+ if (::stat(d.c_str(), &st) == 0) {
+ rmTree(d);
+ foundAny = true;
+ }
}
}
}
@@ -411,15 +456,24 @@ public:
private:
void loadNpTrophyModule() {
+ if (trophyModule_ >= 0) return;
int res = 0;
- int mod = sceKernelLoadStartModule(systemLib("libSceNpTrophy2.sprx").c_str(), 0, nullptr, 0, nullptr, &res);
- if (mod < 0) {
- mod = sceKernelLoadStartModule("libSceNpTrophy2.sprx", 0, nullptr, 0, nullptr, &res);
+ trophyModule_ = sceKernelLoadStartModule(systemLib("libSceNpTrophy2.sprx").c_str(), 0, nullptr, 0, nullptr, &res);
+ if (trophyModule_ < 0) {
+ const char* paths[] = {
+ "/system/common/lib/libSceNpTrophy2.sprx",
+ "/common/lib/libSceNpTrophy2.sprx",
+ "libSceNpTrophy2.sprx"
+ };
+ for (const char* p : paths) {
+ trophyModule_ = sceKernelLoadStartModule(p, 0, nullptr, 0, nullptr, &res);
+ if (trophyModule_ >= 0) break;
+ }
}
- XC_LOGI("trophy ps5: load libSceNpTrophy2.sprx -> 0x%08x (res=%d)", static_cast<unsigned>(mod), res);
- int modMgr = sceKernelLoadStartModule(systemLib("libSceNpManager.sprx").c_str(), 0, nullptr, 0, nullptr, &res);
+ XC_LOGI("trophy ps5: load libSceNpTrophy2.sprx -> 0x%08x (res=%d)", static_cast<unsigned>(trophyModule_), res);
+ int modMgr = sceKernelLoadStartModule("/system/common/lib/libSceNpManager.sprx", 0, nullptr, 0, nullptr, &res);
if (modMgr < 0) {
- modMgr = sceKernelLoadStartModule("libSceNpManager.sprx", 0, nullptr, 0, nullptr, &res);
+ modMgr = sceKernelLoadStartModule(systemLib("libSceNpManager.sprx").c_str(), 0, nullptr, 0, nullptr, &res);
}
XC_LOGI("trophy ps5: load libSceNpManager.sprx -> 0x%08x (res=%d)", static_cast<unsigned>(modMgr), res);
}
@@ -437,6 +491,7 @@ private:
}
bool initialized_ = false;
+ int trophyModule_ = -1;
uint32_t userId_ = 0x1ea2f4da;
int32_t context_ = -1;
int32_t handle_ = -1;
diff --git a/src/trophy/trophy_backend.h b/src/trophy/trophy_backend.h
index 73ca450..57426e0 100644
--- a/src/trophy/trophy_backend.h
+++ b/src/trophy/trophy_backend.h
@@ -30,7 +30,9 @@ public:
virtual void flush() {}
// Removes local trophy data and packages for provisioned titles
- virtual bool removeAllTrophies(const std::vector<std::pair<std::string, std::string>>& titles, int& removedCount, std::string& err) = 0;
+ virtual bool removeAllTrophies(const std::vector<std::pair<std::string, std::string>>& titles, int& removedCount, std::string& err) {
+ (void)titles; (void)err; removedCount = 0; return true;
+ }
// Backend identifier name
virtual std::string name() const = 0;
diff --git a/src/ui/app_ui_input.cpp b/src/ui/app_ui_input.cpp
index d933a1c..a40f23d 100644
--- a/src/ui/app_ui_input.cpp
+++ b/src/ui/app_ui_input.cpp
@@ -676,9 +676,11 @@ UiEvent AppUi::onMouseDown(float x, float y, int button) {
Rect btn1{btnStartX + kBtnW + kBtnGap, btnY, kBtnW, kBtnH};
auto inRect = [&](const Rect& r) { return x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h; };
if (inRect(btn0)) {
+ settings_.syncTrophies = false;
removeTrophiesConfirmOpen_ = false;
dirty_ = true;
ev.action = Action::RemoveTrophies;
+ ev.settings = settings_;
} else if (inRect(btn1) || !inRect(panel)) {
removeTrophiesConfirmOpen_ = false;
dirty_ = true;
@@ -1043,9 +1045,11 @@ UiEvent AppUi::handle(const NavInput& in) {
dirty_ = true;
} else if (in.accept) {
if (removeTrophiesFocus_ == 0) {
+ settings_.syncTrophies = false;
removeTrophiesConfirmOpen_ = false;
dirty_ = true;
ev.action = Action::RemoveTrophies;
+ ev.settings = settings_;
} else {
removeTrophiesConfirmOpen_ = false;
dirty_ = true;
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 11a043b..f1525e8 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -7561,19 +7561,32 @@ static void testTrophyModalsAndDesktopInput() {
ui::UiEvent evAware = app.handle(accept);
CHECK(app.settings().syncTrophies);
CHECK(evAware.action == ui::Action::SyncTrophies);
+ CHECK(evAware.settings.syncTrophies);
+ app::Settings st;
+ ui::applyChoice(evAware.settings, st);
+ CHECK(st.syncTrophies);
// Click row 24 again: turns it off directly
ui::UiEvent evOff = app.onMouseDown(100.0f, static_cast<float>(y24), 1);
CHECK(!app.settings().syncTrophies);
CHECK(evOff.action == ui::Action::SettingsChanged);
+ CHECK(!evOff.settings.syncTrophies);
+ ui::applyChoice(evOff.settings, st);
+ CHECK(!st.syncTrophies);
- // 2. Click row 25: opens remove trophies confirm modal
+ // 2. Turn sync back on to test removal resets it
+ app.onMouseDown(100.0f, static_cast<float>(y24), 1);
+ app.handle(accept);
+ CHECK(app.settings().syncTrophies);
+
+ // Click row 25: opens remove trophies confirm modal
app.onMouseDown(100.0f, static_cast<float>(y25), 1);
app.render(c, 1000);
// Default focus is "Cancelar" (focus 1) for safety
ui::UiEvent evCancelRm = app.handle(accept);
CHECK(evCancelRm.action != ui::Action::RemoveTrophies);
+ CHECK(app.settings().syncTrophies);
// Click row 25 again, navigate left to "Remover" (focus 0)
app.onMouseDown(100.0f, static_cast<float>(y25), 1);
@@ -7582,6 +7595,10 @@ static void testTrophyModalsAndDesktopInput() {
app.handle(navLeft);
ui::UiEvent evRm = app.handle(accept);
CHECK(evRm.action == ui::Action::RemoveTrophies);
+ CHECK(!evRm.settings.syncTrophies);
+ CHECK(!app.settings().syncTrophies);
+ ui::applyChoice(evRm.settings, st);
+ CHECK(!st.syncTrophies);
// 3. Desktop Search & text input
app.showHome();