Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Stop uninstalls that can't go on instead of hanging

Leonardo Lobato36f8906a87b6
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cpp
index 15985c4..91d1c30 100644
--- a/src/homebrew/controller.cpp
+++ b/src/homebrew/controller.cpp
@@ -599,6 +599,7 @@ void Controller::TickCloud() {
continue;
}
// Busy from now on, before Cloud reports it, so Play cannot slip in.
+ const bool busy_before = cloud_busy_.count(request.game_id) != 0;
cloud_busy_.insert(request.game_id);
if (request.kind == CloudRequestKind::Sync) uninstalls_.SyncDispatched(request.game_id);
std::uint64_t serial = 0;
@@ -610,6 +611,13 @@ void Controller::TickCloud() {
case CloudRequestKind::RemoveLocal: serial = cloud_->RemoveLocal(*location); break;
default: break;
}
+ if (serial == 0) {
+ // Cloud took nothing: no transfer runs for the game, and an uninstall waiting on it stops.
+ if (!busy_before) cloud_busy_.erase(request.game_id);
+ if (request.kind == CloudRequestKind::Sync)
+ if (const auto stopped = uninstalls_.SyncRefused(request.game_id); stopped.kind == UninstallAction::Kind::kStop)
+ StopUninstall(request.game_id, stopped.reason);
+ }
// The answer is the game's first settled report after this serial.
if (const auto foreground = cloud_foreground_.find(request.game_id); foreground != cloud_foreground_.end()) {
if (serial == 0) {
@@ -1169,7 +1177,7 @@ void Controller::TickRuntime() {
queue.refused[id] = refusal;
// Uninstalls in the order they run; only those at work say their step.
for (const auto id : uninstalls_.Active())
- queue.uninstalls.push_back({id, uninstalls_.Started(id) ? UninstallStatus(id) : std::string()});
+ queue.uninstalls.push_back({id, uninstalls_.Started(id, files_deleting_) ? UninstallStatus(id) : std::string()});
model_.UpdatePreparationQueue(std::move(queue));
}
if (snapshot.revision == runtime_revision_ && install_revision_ == runtime_install_revision_ &&
@@ -1439,7 +1447,7 @@ std::string Controller::UninstallStatus(GameId id) const {
const auto step = uninstalls_.Step(id);
if (!step) return {};
// Only the uninstalls at work say what they do; the others wait their turn.
- if (!uninstalls_.Started(id)) return "Waiting to uninstall";
+ if (!uninstalls_.Started(id, files_deleting_)) return "Waiting to uninstall";
switch (*step) {
case UninstallStep::kSync: return "Syncing saves…";
case UninstallStep::kRemoveTitle: return "Removing PS5 title…";
@@ -1486,7 +1494,10 @@ void Controller::TickUninstalls() {
// The install service lists a game as installed no more once it starts deleting it: the
// uninstall is over once the deletion is too. Without the service there is nothing to wait for.
const auto* location = LocationOf(id);
- facts.download_gone = (!location && !files_deleting_.count(id)) || (location && location->managed() && !installs_);
+ facts.download_gone = (!location && std::find(files_deleting_.begin(), files_deleting_.end(), id) == files_deleting_.end()) ||
+ (location && location->managed() && !installs_);
+ // As the install service sees it: a game missing from a drive that is gone can't be deleted.
+ facts.drive_absent = location && location->managed() && !location->available && !drives_present_.count(location->volume_id);
const auto action = uninstalls_.Advance(id, facts);
switch (action.kind) {
case UninstallAction::Kind::kNone: break;
@@ -1498,8 +1509,14 @@ void Controller::TickUninstalls() {
case UninstallAction::Kind::kRemoveDownload:
if (location && !location->managed()) {
// Its outcome is watched: a refusal stops the uninstall (TickImports).
- if (id.store == Store::kLocal && local_)
- if (const auto number = local_->Remove(id.value)) import_requests_[number] = {ImportRequestKind::Remove, id};
+ if (id.store == Store::kLocal && local_) {
+ if (const auto number = local_->Remove(id.value)) {
+ import_requests_[number] = {ImportRequestKind::Remove, id};
+ } else {
+ uninstalls_.Abandon(id);
+ StopUninstall(id, "Playden could not take the game out of the library now. Uninstall again to try again.");
+ }
+ }
} else if (installs_) {
installs_->Uninstall(id);
}
@@ -1602,7 +1619,24 @@ void Controller::TickImports() {
for (auto it = import_requests_.begin(); it != import_requests_.end();) {
const auto outcome = std::find_if(snapshot->outcomes.begin(), snapshot->outcomes.end(),
[&](const stores::local::ImportOutcome& o) { return o.request == it->first; });
- if (outcome == snapshot->outcomes.end()) { ++it; continue; }
+ if (outcome == snapshot->outcomes.end()) {
+ // An uninstall's removal answered so long ago its outcome scrolled off the list: the game still
+ // in the library means it was refused; one gone ends as usual once its location is gone.
+ const bool lost = snapshot->outcomes.size() >= stores::local::LocalLibrarySource::kMaxOutcomes &&
+ it->first < snapshot->outcomes.front().request;
+ const auto id = it->second.game_id;
+ if (lost && it->second.kind == ImportRequestKind::Remove && uninstalls_.Step(id)) {
+ if (std::any_of(snapshot->games.begin(), snapshot->games.end(),
+ [&id](const stores::local::ImportEntry& entry) { return GameId::Local(entry.id) == id; })) {
+ uninstalls_.Abandon(id);
+ StopUninstall(id, "Playden could not take the game out of the library. Uninstall again to try again.");
+ }
+ it = import_requests_.erase(it);
+ continue;
+ }
+ ++it;
+ continue;
+ }
if (!outcome->ok && it->second.kind == ImportRequestKind::Remove && uninstalls_.Step(it->second.game_id)) {
// An uninstall's last step was refused: the game stays, and the uninstall says why.
uninstalls_.Abandon(it->second.game_id);
@@ -1765,7 +1799,10 @@ void Controller::TickInstalls() {
StopUninstall(failure.id, failure.reason + " Uninstall the game again to try again.");
}
const auto snapshot = installs_->Snapshot();
- files_deleting_ = {snapshot.uninstalling.begin(), snapshot.uninstalling.end()};
+ files_deleting_ = snapshot.uninstalling;
+ drives_present_.clear();
+ for (const auto& volume : snapshot.volumes)
+ if (volume.available) drives_present_.insert(volume.id);
ProjectLocations(snapshot.installed);
// A download that finished in this session is prepared next, so the game is ready to play.
std::set<GameId> downloading;
diff --git a/src/homebrew/controller.hpp b/src/homebrew/controller.hpp
index f1f2ab2..8e32aa8 100644
--- a/src/homebrew/controller.hpp
+++ b/src/homebrew/controller.hpp
@@ -272,8 +272,10 @@ class Controller {
void TickUninstalls();
// What a game's uninstall does now ("Waiting to uninstall" until its turn); empty without one.
std::string UninstallStatus(GameId id) const;
- // Games whose files the install service is deleting now (it lists them as installed no more).
- std::set<GameId> files_deleting_;
+ // Games whose files the install service is deleting (it lists them as installed no more), in the
+ // order it deletes them, and the drives it has now.
+ std::vector<GameId> files_deleting_;
+ std::set<std::string> drives_present_;
// The uninstalls with their status, in order, as last projected.
std::vector<std::pair<GameId, std::string>> uninstall_statuses_;
std::uint64_t kept_revision_ = 0;
diff --git a/src/homebrew/uninstall_flow.cpp b/src/homebrew/uninstall_flow.cpp
index e6fcc56..3a5dc8e 100644
--- a/src/homebrew/uninstall_flow.cpp
+++ b/src/homebrew/uninstall_flow.cpp
@@ -69,6 +69,11 @@ UninstallAction UninstallFlow::CloudRefused(GameId id) {
return Stop(id, "The game's PS5 title may still be running. Close it, then uninstall again.");
}
+UninstallAction UninstallFlow::SyncRefused(GameId id) {
+ if (!WaitingForCloud(id)) return {};
+ return Stop(id, "Steam Cloud could not take the sync now. Uninstall again to try again.");
+}
+
void UninstallFlow::CloudSkipped(GameId id) {
if (const auto found = entries_.find(id); found != entries_.end() && found->second.step == UninstallStep::kSync)
found->second.step = UninstallStep::kRemoveTitle;
@@ -78,6 +83,12 @@ UninstallAction UninstallFlow::Advance(GameId id, const UninstallFacts& facts) {
const auto found = entries_.find(id);
if (found == entries_.end()) return {};
auto& entry = found->second;
+ // Nothing can be deleted on a drive that is not there: the uninstall stops before it removes the
+ // PS5 title (which would leave the files without one), or before it asks for the download.
+ static constexpr const char* kDriveAbsent = "Its drive is not connected. Connect it, then uninstall again.";
+ if (facts.drive_absent && entry.step != UninstallStep::kRemoveDownload &&
+ !(entry.step == UninstallStep::kRemoveTitle && entry.remove_sent))
+ return Stop(id, kDriveAbsent);
if (entry.step == UninstallStep::kRemoveTitle) {
if (!entry.remove_sent) {
if (!facts.has_title) {
@@ -108,6 +119,7 @@ UninstallAction UninstallFlow::Advance(GameId id, const UninstallFacts& facts) {
if (entry.step == UninstallStep::kRemoveDownload) {
// The uninstall lasts until the files are gone; a failure to delete them comes back as Abandon.
if (!entry.download_sent) {
+ if (facts.drive_absent) return Stop(id, kDriveAbsent);
entry.download_sent = true;
return {UninstallAction::Kind::kRemoveDownload, false, {}};
}
@@ -125,15 +137,22 @@ std::optional<UninstallStep> UninstallFlow::Step(GameId id) const {
return found == entries_.end() ? std::nullopt : std::optional<UninstallStep>(found->second.step);
}
-bool UninstallFlow::Started(GameId id) const {
+bool UninstallFlow::Started(GameId id, const std::vector<GameId>& deletions) const {
const auto found = entries_.find(id);
if (found == entries_.end()) return false;
const auto& entry = found->second;
switch (entry.step) {
case UninstallStep::kSync: return entry.sync_dispatched;
case UninstallStep::kRemoveTitle: return entry.remove_sent;
- case UninstallStep::kRemoveDownload: return true;
+ case UninstallStep::kRemoveDownload: break;
}
+ if (!entry.download_sent) return false;
+ // Not listed: removed elsewhere (an imported game leaving the library), or just asked for.
+ if (std::find(deletions.begin(), deletions.end(), id) == deletions.end()) return true;
+ for (const auto other : deletions)
+ if (const auto listed = entries_.find(other);
+ listed != entries_.end() && listed->second.step == UninstallStep::kRemoveDownload && listed->second.download_sent)
+ return other == id;
return false;
}
diff --git a/src/homebrew/uninstall_flow.hpp b/src/homebrew/uninstall_flow.hpp
index 9a997bf..fa0f395 100644
--- a/src/homebrew/uninstall_flow.hpp
+++ b/src/homebrew/uninstall_flow.hpp
@@ -37,6 +37,7 @@ struct UninstallFacts {
bool saves_moving = false; // a Cloud transfer runs for the game
bool session_active = false; // the game's title may be running
bool download_gone = false; // the game's files are no longer installed
+ bool drive_absent = false; // the game's files are on a drive that is not connected
};
class UninstallFlow {
@@ -57,14 +58,18 @@ class UninstallFlow {
UninstallAction CloudRefused(GameId id);
// The game has nothing Cloud could sync (no folder): continue.
void CloudSkipped(GameId id);
+ // Cloud did not take the sync (stopping, or too many waiting): stop, so later uninstalls go on.
+ UninstallAction SyncRefused(GameId id);
// What to do next for a game, given what the runtime reports now.
UninstallAction Advance(GameId id, const UninstallFacts& facts);
// The download removal failed outside the flow: the uninstall is over.
void Abandon(GameId id);
std::optional<UninstallStep> Step(GameId id) const;
// Whether the uninstall works now: its sync was sent, or its PS5 title or download is being
- // removed. One that waits its turn does not.
- bool Started(GameId id) const;
+ // removed. One that waits its turn does not. `deletions` lists the games whose files the install
+ // service deletes, in its order: it deletes one at a time, so of the games listed only the first
+ // of these uninstalls is at work.
+ bool Started(GameId id, const std::vector<GameId>& deletions = {}) const;
// Uninstalls in the order they were asked for.
std::vector<GameId> Active() const;
diff --git a/tests/homebrew_uninstall_flow_test.cpp b/tests/homebrew_uninstall_flow_test.cpp
index 805235b..9c152d8 100644
--- a/tests/homebrew_uninstall_flow_test.cpp
+++ b/tests/homebrew_uninstall_flow_test.cpp
@@ -220,6 +220,49 @@ int main() {
fifo.Abandon(SteamId(20));
CHECK(fifo.Begin(SteamId(20), false, false) && fifo.Begin(SteamId(5), false, false));
CHECK(fifo.Active() == std::vector<GameId>({SteamId(30), SteamId(10), SteamId(20), SteamId(5)}));
+
+ // The install service deletes one game at a time, in its order: only the first of these uninstalls
+ // it lists is at work; one not listed (an imported game leaving the library) is at work too.
+ UninstallFlow deleting;
+ for (const auto app : {70u, 71u, 72u}) {
+ CHECK(deleting.Begin(SteamId(app), false, false));
+ CHECK(deleting.Advance(SteamId(app), UninstallFacts{}).kind == Kind::kRemoveDownload);
+ }
+ const std::vector<GameId> queued{SteamId(99), SteamId(71), SteamId(70)};
+ CHECK(deleting.Started(SteamId(71), queued) && !deleting.Started(SteamId(70), queued));
+ CHECK(deleting.Started(SteamId(72), queued));
+ CHECK(deleting.Started(SteamId(70)));
+
+ // A drive that is not connected stops the uninstall before its PS5 title is removed...
+ UninstallFlow absent;
+ UninstallFacts unplugged;
+ unplugged.has_title = true;
+ unplugged.drive_absent = true;
+ CHECK(absent.Begin(SteamId(80), false, false));
+ action = absent.Advance(SteamId(80), unplugged);
+ CHECK(action.kind == Kind::kStop && action.reason == "Its drive is not connected. Connect it, then uninstall again.");
+ CHECK(!absent.Step(SteamId(80)));
+ // ...lets a removal already sent finish, then stops before asking for the download.
+ CHECK(absent.Begin(SteamId(81), false, false));
+ unplugged.drive_absent = false;
+ CHECK(absent.Advance(SteamId(81), unplugged).kind == Kind::kRemoveTitle);
+ unplugged.drive_absent = true;
+ unplugged.removing = true;
+ CHECK(absent.Advance(SteamId(81), unplugged).kind == Kind::kNone && absent.Step(SteamId(81)) == UninstallStep::kRemoveTitle);
+ UninstallFacts removed_unplugged;
+ removed_unplugged.removals_handled = 1;
+ removed_unplugged.drive_absent = true;
+ CHECK(absent.Advance(SteamId(81), removed_unplugged).kind == Kind::kStop && !absent.Step(SteamId(81)));
+
+ // A sync Cloud did not take stops that uninstall, and the next one's sync goes out.
+ UninstallFlow refused;
+ CHECK(refused.Begin(SteamId(90), false, true) && refused.Begin(SteamId(91), false, true));
+ CHECK(refused.SyncRefused(SteamId(90)).kind == Kind::kNone); // not issued yet
+ CHECK(refused.TakeSyncRequests() == std::vector<GameId>({SteamId(90)}));
+ refused.SyncDispatched(SteamId(90));
+ action = refused.SyncRefused(SteamId(90));
+ CHECK(action.kind == Kind::kStop && action.reason.find("Uninstall again") != std::string::npos && !refused.Step(SteamId(90)));
+ CHECK(refused.TakeSyncRequests() == std::vector<GameId>({SteamId(91)}));
std::puts("uninstall flow tests passed");
return 0;
}