Play your Steam library's Windows games on a jailbroken PS5.
C++
Uninstall games in the order asked, one step at a time
Leonardo Lobato8e20d40fb4b5
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cppindex 11b7e80..e9941c9 100644--- a/src/homebrew/controller.cpp+++ b/src/homebrew/controller.cpp@@ -1308,9 +1308,8 @@ void Controller::TickRuntime() {if (const auto pending = play_after_prepare_.find(id); pending != play_after_prepare_.end()) {game.play_status = pending->second.words; game.can_prepare = false;}- if (const auto step = uninstalls_.Step(id)) {- game.uninstall_status = *step == UninstallStep::kSync ? "Syncing saves…"- : *step == UninstallStep::kRemoveTitle ? "Removing PS5 title…" : "Uninstalling…";+ if (auto status = UninstallStatus(id); !status.empty()) {+ game.uninstall_status = std::move(status);game.can_play = false; game.can_prepare = false;}// A retry is in flight: do not republish its previous refusal as a new outcome.@@ -1433,13 +1432,37 @@ void Controller::StopUninstall(GameId id, const std::string& reason) {++play_notes_revision_;model_.NotifyUninstallStopped(id, reason);}+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";+ switch (*step) {+ case UninstallStep::kSync: return "Syncing saves…";+ case UninstallStep::kRemoveTitle: return "Removing PS5 title…";+ case UninstallStep::kRemoveDownload: break;+ }+ // An imported game played from its drive leaves the library; its files stay.+ const auto* location = LocationOf(id);+ return location && !location->managed() ? "Removing from Playden…" : "Deleting files…";+}void Controller::TickUninstalls() {if (!runtime_) return;+ // A status that changed (a sync sent, a title removed) is projected again.+ const auto project = [this] {+ std::vector<std::pair<GameId, std::string>> statuses;+ for (const auto id : uninstalls_.Active()) statuses.emplace_back(id, UninstallStatus(id));+ if (statuses != uninstall_statuses_) {+ uninstall_statuses_ = std::move(statuses);+ ++play_notes_revision_;+ }+ };const auto ids = uninstalls_.Active();- if (ids.empty()) return;+ if (ids.empty()) return project();const auto snapshot_shared = runtime_->Shared();const auto& snapshot = *snapshot_shared;if (snapshot.status == RuntimeHostStatus::kStarting) return;+ // In the order they were asked for: the first that can go takes the one title removal at a time.for (const auto id : ids) {if (IsNumber(snapshot, snapshot.preparing_app, id)) continue; // its title is removed once the preparation endsUninstallFacts facts;@@ -1457,6 +1480,10 @@ void Controller::TickUninstalls() {facts.removal_failure = failure->second;facts.saves_moving = cloud_busy_.count(id) != 0;facts.session_active = SessionActive(id);+ // 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_);const auto action = uninstalls_.Advance(id, facts);switch (action.kind) {case UninstallAction::Kind::kNone: break;@@ -1466,16 +1493,20 @@ void Controller::TickUninstalls() {break;// The title is gone, so no title points at the files about to be deleted.case UninstallAction::Kind::kRemoveDownload:- if (const auto* location = LocationOf(id); location && !location->managed()) {- if (id.store == Store::kLocal && local_) local_->Remove(id.value);+ 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};} else if (installs_) {installs_->Uninstall(id);}++play_notes_revision_;break;case UninstallAction::Kind::kStop: StopUninstall(id, action.reason); break;+ case UninstallAction::Kind::kFinished: ++play_notes_revision_; break;}}+ project();}void Controller::ProjectLocations(const std::vector<steam::InstalledGame>& managed) {auto external = external_locations_ ? external_locations_() : std::vector<installs::InstalledLocation>{};@@ -1566,7 +1597,11 @@ void Controller::TickImports() {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->ok) {+ 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);+ StopUninstall(it->second.game_id, outcome->message);+ } else if (!outcome->ok) {model_.NotifyImportFailed(it->second, outcome->message);} else if (it->second.kind == ImportRequestKind::Add && !outcome->id.empty()) {// A game just added: the program chosen with it goes into its Game settings.@@ -1719,9 +1754,12 @@ void Controller::TickInstalls() {}}// An uninstall whose files could not be deleted lists the game again and says why.- for (const auto& failure : installs_->TakeRemovalFailures())+ for (const auto& failure : installs_->TakeRemovalFailures()) {+ uninstalls_.Abandon(failure.id);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()};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.hppindex ef9b213..f1f2ab2 100644--- a/src/homebrew/controller.hpp+++ b/src/homebrew/controller.hpp@@ -270,6 +270,12 @@ class Controller {void BeginUninstall(GameId id, bool delete_saves);void StopUninstall(GameId id, const std::string& reason);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_;+ // 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;std::vector<LiveKeptBottle> kept_projected_;std::map<GameId, std::string> play_notes_;diff --git a/src/homebrew/uninstall_flow.cpp b/src/homebrew/uninstall_flow.cppindex 9f50ff7..e6fcc56 100644--- a/src/homebrew/uninstall_flow.cpp+++ b/src/homebrew/uninstall_flow.cpp@@ -3,6 +3,8 @@// SPDX-License-Identifier: MIT#include "homebrew/uninstall_flow.hpp"+#include <algorithm>+namespace playden::homebrew {bool UninstallFlow::Begin(GameId id, bool delete_saves, bool cloud_available) {@@ -10,18 +12,22 @@ bool UninstallFlow::Begin(GameId id, bool delete_saves, bool cloud_available) {Entry entry;entry.delete_saves = delete_saves;entry.step = cloud_available && !without_sync_.erase(id) ? UninstallStep::kSync : UninstallStep::kRemoveTitle;+ entry.order = next_order_++;entries_[id] = entry;return true;}std::vector<GameId> UninstallFlow::TakeSyncRequests() {- std::vector<GameId> ids;- for (auto& [id, entry] : entries_)- if (entry.step == UninstallStep::kSync && !entry.sync_issued) {+ // One sync at a time: the next waits until the one out settles, so the order holds and only+ // the game being synced says so.+ for (const auto& [id, entry] : entries_)+ if (entry.step == UninstallStep::kSync && entry.sync_issued) return {};+ for (const auto id : Active())+ if (auto& entry = entries_[id]; entry.step == UninstallStep::kSync) {entry.sync_issued = true;- ids.push_back(id);+ return {id};}- return ids;+ return {};}bool UninstallFlow::WaitingForCloud(GameId id) const {@@ -81,7 +87,7 @@ UninstallAction UninstallFlow::Advance(GameId id, const UninstallFacts& facts) {if (facts.saves_moving || facts.session_active) return {};// The runtime counts removals for all games together, so only one may be in flight to tell whose finished.for (const auto& [other_id, other] : entries_)- if (other_id != id && other.remove_sent) return {};+ if (other_id != id && other.step == UninstallStep::kRemoveTitle && other.remove_sent) return {};entry.remove_sent = true;entry.removals_before = facts.removals_handled;return {UninstallAction::Kind::kRemoveTitle, entry.delete_saves, {}};@@ -100,20 +106,43 @@ 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) {+ entry.download_sent = true;+ return {UninstallAction::Kind::kRemoveDownload, false, {}};+ }+ if (!facts.download_gone) return {};entries_.erase(found);- return {UninstallAction::Kind::kRemoveDownload, false, {}};+ return {UninstallAction::Kind::kFinished, false, {}};}return {};}+void UninstallFlow::Abandon(GameId id) { entries_.erase(id); }+std::optional<UninstallStep> UninstallFlow::Step(GameId id) const {const auto found = entries_.find(id);return found == entries_.end() ? std::nullopt : std::optional<UninstallStep>(found->second.step);}+bool UninstallFlow::Started(GameId id) 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;+ }+ return false;+}+std::vector<GameId> UninstallFlow::Active() const {+ std::vector<std::pair<std::uint64_t, GameId>> ordered;+ for (const auto& [id, entry] : entries_) ordered.emplace_back(entry.order, id);+ std::sort(ordered.begin(), ordered.end());std::vector<GameId> ids;- for (const auto& [id, entry] : entries_) ids.push_back(id);+ for (const auto& [order, id] : ordered) ids.push_back(id);return ids;}} // namespace playden::homebrewdiff --git a/src/homebrew/uninstall_flow.hpp b/src/homebrew/uninstall_flow.hppindex a55db0b..9a997bf 100644--- a/src/homebrew/uninstall_flow.hpp+++ b/src/homebrew/uninstall_flow.hpp@@ -16,10 +16,13 @@ namespace playden::homebrew {// Steam Cloud syncs first (a conflict or a failure stops and asks the player), then// the game's PS5 title is removed (journaled by the runtime host), and only then the// Steam download. A failure stops the uninstall with the reason; nothing is skipped.+// Uninstalls go first in, first out: syncs and title removals are handed out one at a+// time, in the order the uninstalls were asked for.enum class UninstallStep { kSync, kRemoveTitle, kRemoveDownload };struct UninstallAction {- enum class Kind { kNone, kRemoveTitle, kRemoveDownload, kStop } kind = Kind::kNone;+ // kFinished: the download is gone and the uninstall is over.+ enum class Kind { kNone, kRemoveTitle, kRemoveDownload, kStop, kFinished } kind = Kind::kNone;bool delete_saves = false;std::string reason; // kStop: what the player is told};@@ -33,6 +36,7 @@ struct UninstallFacts {std::string removal_failure; // the runtime's latest removal refusal or failurebool saves_moving = false; // a Cloud transfer runs for the gamebool session_active = false; // the game's title may be running+ bool download_gone = false; // the game's files are no longer installed};class UninstallFlow {@@ -40,7 +44,8 @@ class UninstallFlow {// Starts an uninstall; false when one already runs for the game. With Cloud the// saves sync first, unless the player chose to go on after a failed sync.bool Begin(GameId id, bool delete_saves, bool cloud_available);- // Games whose Cloud sync should be issued now (each once).+ // The game whose Cloud sync should be issued now (each once), the first waiting one in+ // order, while no other sync is out.std::vector<GameId> TakeSyncRequests();bool WaitingForCloud(GameId id) const;// The sync went to the Cloud service: from now on its results settle the uninstall@@ -54,7 +59,13 @@ class UninstallFlow {void CloudSkipped(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;+ // Uninstalls in the order they were asked for.std::vector<GameId> Active() const;private:@@ -64,10 +75,13 @@ class UninstallFlow {bool sync_issued = false;bool sync_dispatched = false;bool remove_sent = false;+ bool download_sent = false;std::uint64_t removals_before = 0;+ std::uint64_t order = 0; // when it was asked for};UninstallAction Stop(GameId id, std::string reason);std::map<GameId, Entry> entries_;std::set<GameId> without_sync_;+ std::uint64_t next_order_ = 0;};} // namespace playden::homebrewdiff --git a/tests/homebrew_uninstall_flow_test.cpp b/tests/homebrew_uninstall_flow_test.cppindex 56bd8eb..805235b 100644--- a/tests/homebrew_uninstall_flow_test.cpp+++ b/tests/homebrew_uninstall_flow_test.cpp@@ -44,6 +44,11 @@ int main() {UninstallFacts gone;gone.removals_handled = 8;CHECK(flow.Advance(SteamId(42), gone).kind == Kind::kRemoveDownload);+ // The uninstall lasts until the files are gone, and the download is asked for once.+ CHECK(flow.Step(SteamId(42)) == UninstallStep::kRemoveDownload && flow.Started(SteamId(42)));+ CHECK(flow.Advance(SteamId(42), gone).kind == Kind::kNone);+ gone.download_gone = true;+ CHECK(flow.Advance(SteamId(42), gone).kind == Kind::kFinished);CHECK(!flow.Step(SteamId(42)) && flow.Active().empty());// A failed removal stops with the runtime's reason and keeps the download.@@ -140,6 +145,9 @@ int main() {UninstallFacts second_done;second_done.removals_handled = 7;CHECK(flow.Advance(SteamId(61), second_done).kind == Kind::kRemoveDownload);+ // A download that could not be deleted ends the uninstall outside the flow.+ flow.Abandon(SteamId(60));+ flow.Abandon(SteamId(61));CHECK(!flow.Step(SteamId(60)) && !flow.Step(SteamId(61)));// A stopped removal frees the slot for the waiting game.@@ -158,18 +166,60 @@ int main() {// One game's Cloud conflict stops only that game; the other goes on to its removal.CHECK(flow.Begin(SteamId(64), false, true) && flow.Begin(SteamId(65), false, true));- CHECK(flow.TakeSyncRequests() == std::vector<GameId>({SteamId(64), SteamId(65)}));+ CHECK(flow.TakeSyncRequests() == std::vector<GameId>({SteamId(64)}));flow.SyncDispatched(SteamId(64));- flow.SyncDispatched(SteamId(65));CHECK(flow.CloudSettled(SteamId(64), CloudStatus::Conflict).kind == Kind::kStop);CHECK(!flow.Step(SteamId(64)) && flow.Step(SteamId(65)) == UninstallStep::kSync);+ CHECK(flow.TakeSyncRequests() == std::vector<GameId>({SteamId(65)}));+ flow.SyncDispatched(SteamId(65));CHECK(flow.CloudSettled(SteamId(65), CloudStatus::Synced).kind == Kind::kNone);UninstallFacts after_sync;after_sync.has_title = true;CHECK(flow.Advance(SteamId(65), after_sync).kind == Kind::kRemoveTitle);after_sync = {};after_sync.removals_handled = 1;- CHECK(flow.Advance(SteamId(65), after_sync).kind == Kind::kRemoveDownload && !flow.Step(SteamId(65)));+ CHECK(flow.Advance(SteamId(65), after_sync).kind == Kind::kRemoveDownload && flow.Step(SteamId(65)) == UninstallStep::kRemoveDownload);++ // First in, first out: uninstalls go in the order they were asked for, whatever their app IDs,+ // and only the one at work says it has started.+ UninstallFlow fifo;+ for (const auto app : {30u, 10u, 20u}) CHECK(fifo.Begin(SteamId(app), false, true));+ CHECK(fifo.Active() == std::vector<GameId>({SteamId(30), SteamId(10), SteamId(20)}));+ // One sync at a time, in that order: the next goes out once the one before settles.+ CHECK(fifo.TakeSyncRequests() == std::vector<GameId>({SteamId(30)}));+ CHECK(fifo.TakeSyncRequests().empty());+ CHECK(!fifo.Started(SteamId(30))); // issued, not yet sent to Cloud+ fifo.SyncDispatched(SteamId(30));+ CHECK(fifo.Started(SteamId(30)) && !fifo.Started(SteamId(10)) && !fifo.Started(SteamId(20)));+ CHECK(fifo.CloudSettled(SteamId(30), CloudStatus::Synced).kind == Kind::kNone);+ CHECK(!fifo.Started(SteamId(30))); // synced, waiting for its title removal+ CHECK(fifo.TakeSyncRequests() == std::vector<GameId>({SteamId(10)}));+ fifo.SyncDispatched(SteamId(10));+ CHECK(fifo.CloudSettled(SteamId(10), CloudStatus::UpToDate).kind == Kind::kNone);+ CHECK(fifo.TakeSyncRequests() == std::vector<GameId>({SteamId(20)}));+ fifo.CloudSkipped(SteamId(20));+ CHECK(fifo.TakeSyncRequests().empty());+ // Title removals follow the same order: walking Active() hands the one slot to the first.+ UninstallFacts listed;+ listed.has_title = true;+ std::vector<GameId> removed;+ for (const auto id : fifo.Active())+ if (fifo.Advance(id, listed).kind == Kind::kRemoveTitle) removed.push_back(id);+ CHECK(removed == std::vector<GameId>({SteamId(30)}));+ CHECK(fifo.Started(SteamId(30)) && !fifo.Started(SteamId(10)) && !fifo.Started(SteamId(20)));+ UninstallFacts removed_one;+ removed_one.removals_handled = 1;+ CHECK(fifo.Advance(SteamId(30), removed_one).kind == Kind::kRemoveDownload);+ removed.clear();+ listed.removals_handled = 1;+ for (const auto id : fifo.Active())+ if (fifo.Advance(id, listed).kind == Kind::kRemoveTitle) removed.push_back(id);+ CHECK(removed == std::vector<GameId>({SteamId(10)}));+ // A game asked for again after a stop goes to the back of the line.+ CHECK(fifo.CloudRefused(SteamId(20)).kind == Kind::kNone);+ 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)}));std::puts("uninstall flow tests passed");return 0;}