Play your Steam library's Windows games on a jailbroken PS5.
C++
Prepare a game again when it was installed on another drive
Leonardo Lobato6408e2a4e45d
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cppindex e29d2db..75af3f2 100644--- a/src/homebrew/controller.cpp+++ b/src/homebrew/controller.cpp@@ -300,8 +300,10 @@ std::optional<steam::CloudLocation> Controller::CloudLocationFor(GameId id, bool// PS5 title waits for registration: the bottle is complete and the game runs from Home.if (runtime_) {const auto installs_shared = runtime_->Shared();+ // A title made for another folder (its game moved drives) is about to be replaced: only check.for (const auto& view : installs_shared->installs)- if (view.app_id == id && (view.state == runtime::PreparationState::kReady || view.awaiting_registration))+ if (view.app_id == id && (view.state == runtime::PreparationState::kReady || view.awaiting_registration) &&+ (view.install_path.empty() || view.install_path == root->second))location.prefix_root = view.prefix_root;}// Not set up yet (or still being prepared): there is no bottle the game reads, so Cloud only@@ -746,12 +748,23 @@ void Controller::TickRuntime() {view->state == PreparationState::kRegistering) &&!view->recovery_pending && !view->awaiting_registration);};+ // A game installed again on another drive still has the title made for its old folder; Prepare+ // makes it again for the new one.+ const auto moved = [this](const RuntimeInstallView* view) {+ if (view && view->moving) return true; // a move that stopped part way: Prepare finishes it+ if (!view || view->install_path.empty() || view->recovery_pending) return false;+ const auto root = game_roots_.find(view->app_id);+ return root != game_roots_.end() && root->second != view->install_path;+ };std::vector<GameId> direct;for (const auto id : plays) {- if (usable && game_roots_.count(id) && (unprepared(record_of(id)) || needs_work(record_of(id))) &&+ if (usable && game_roots_.count(id) &&+ (unprepared(record_of(id)) || needs_work(record_of(id)) || moved(record_of(id))) &&!play_after_prepare_.count(id)) {auto& pending = play_after_prepare_[id];- if (const auto* view = record_of(id); needs_work(view))+ if (const auto* view = record_of(id); moved(view))+ pending.words = "Preparing the game on its new drive…";+ else if (needs_work(view))pending.words = !view->title_current ? "Updating runtime…" : "Repairing runtime…";auto_prepare_.erase(id);++play_notes_revision_;@@ -763,7 +776,11 @@ void Controller::TickRuntime() {// Finished downloads are prepared one at a time, never over a game that already has a record.if (prepared.status != RuntimeHostStatus::kStarting)for (auto it = auto_prepare_.begin(); it != auto_prepare_.end();) {- if (!usable || !game_roots_.count(*it) || record_of(*it)) { it = auto_prepare_.erase(it); continue; }+ if (!usable || !game_roots_.count(*it) || (record_of(*it) && !moved(record_of(*it)))) {+ it = auto_prepare_.erase(it);+ continue;+ }+ if (cloud_busy_.count(*it)) { ++it; continue; } // a move waits for saves in transit// A quit to Home waits for the running preparation; the next one would be cut off.if (prepared.preparing_app || model_.state().quit_waiting) break;play_after_prepare_.erase(*it);@@ -849,6 +866,8 @@ void Controller::TickRuntime() {auto& pending = it->second;if (!pending.queued) {if (prepared.preparing_app && *prepared.preparing_app != id) { ++it; continue; }+ // A move removes the old title and bottle: saves still in transit finish first.+ if (moved(record_of(id)) && cloud_busy_.count(id)) { ++it; continue; }pending.queued = true;pending.refusal = refusal_of(id);pending.prepares_handled = prepared.prepares_handled;@@ -858,7 +877,8 @@ void Controller::TickRuntime() {continue;}if (prepared.preparing_app == id) { pending.seen_preparing = true; ++it; continue; }- if (ready(record_of(id))) {+ // A title still made for the game's old folder waits for the preparation that moves it.+ if (ready(record_of(id)) && !moved(record_of(id))) {plays.push_back(id);it = play_after_prepare_.erase(it);++play_notes_revision_;@@ -962,6 +982,13 @@ void Controller::TickRuntime() {game.status = "Unavailable"; game.detail = snapshot.reason;} else if (snapshot.removing_app == id) {game.status = "Removing"; game.detail = "Removing the game's PS5 title";+ } else if (snapshot.preparing_app == id && record && record->moving) {+ // Prepare removes the title made for the game's old folder (installed again on another drive).+ game.status = "Preparing"; game.detail = "Removing the PS5 title made for the game's old drive";+ } else if (record && record->moving) {+ // That removal stopped part way: Play or Prepare finishes the move, never an uninstall of the new download.+ game.status = "Needs repair"; game.can_prepare = usable;+ game.detail = "Play finishes moving the PS5 title to the game's new drive.";} else if (record && record->state == runtime::PreparationState::kRemoving) {game.status = "Removal incomplete"; game.can_prepare = true;game.detail = record->failure.empty() ? "Retry finishes removing the PS5 title, then uninstalls the game."diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cppindex 53a39c3..f678761 100644--- a/src/homebrew/runtime_host.cpp+++ b/src/homebrew/runtime_host.cpp@@ -725,6 +725,8 @@ void RuntimeHost::PublishInstalls(const Composition& composition, const std::strfor (const auto& record : composition.repository.Snapshot()) {RuntimeInstallView view;view.install_id = record.install_id; view.app_id = record.app_id; view.title_id = record.title_id;+ view.install_path = record.install_path;+ view.moving = record.journal && record.journal->operation == "remove-move";if (!record.data_root.empty()) view.prefix_root = record.data_root + "/library/prefixes/game";view.state = record.state;if (record.journal) view.step = record.journal->step;@@ -1391,8 +1393,31 @@ std::string RuntimeHost::RunPrepare(Composition& c, const RuntimePrepareRequest&std::optional<rt::RuntimeInstall> existing;for (const auto& record : c.repository.Snapshot())if (record.app_id == request.app_id) existing = record;- if (existing && existing->state == rt::PreparationState::kRemoving)+ // A title being removed because its game moved drives is finished here; any other removal by Uninstall.+ const bool moving = existing && existing->state == rt::PreparationState::kRemoving && existing->journal &&+ existing->journal->operation == "remove-move";+ if (existing && existing->state == rt::PreparationState::kRemoving && !moving)return "This game's PS5 title is still being removed. Uninstall the game again to finish.";+ // RunRemove runs inline, so removals_handled (the controller's uninstall progress) does not move.+ // A pending move finishes whatever the paths say now (the game may be back in its first folder).+ if (existing && (moving || (!existing->journal && existing->install_path != request.install_path))) {+ // Installed again on another drive (an uninstall that stopped part way can leave the title behind):+ // the old title starts files that are no longer the game's. It goes, keeping its saves, and the+ // game is prepared from where it is now; the saves come back after (RestoreKeptSaves), as with+ // Recreate bottle: the Windows user folders, not the bottle's registry or other folders.+ struct stat info{};+ if (lstat(HostPath(request.install_path).c_str(), &info) != 0 || !S_ISDIR(info.st_mode))+ return "The game's folder is missing; download it again.";+ Log("moved install app=" + std::to_string(request.app_id) + " title=" + existing->title_id + " from=" ++ existing->install_path + " to=" + request.install_path);+ // Recorded as a move, so a removal that stops part way is finished by the next Play or Prepare+ // and never turns into an uninstall of the game's new download.+ if (const auto removed = RunRemove(c, request.app_id, false, true); !removed.empty()) {+ Log("moved install app=" + std::to_string(request.app_id) + " removal stopped: " + removed);+ return "The game's old PS5 title could not be removed yet. Play tries again.";+ }+ existing.reset();+ }rt::InstallServiceResult started = rt::InstallServiceResult::Success();bool game_root_owned = true;const auto tighten_working = [&](const std::string& working) -> std::string {@@ -1706,6 +1731,8 @@ namespace {// Kept bottles: <games>/<install id>.kept, identified by this marker inside.constexpr char kKeptMarker[] = "playden-kept.json";constexpr char kKeptSuffix[] = ".kept";+// In a bottle once its kept saves were brought back: a kept copy still beside it is then older.+constexpr char kRestoredMarker[] = ".playden-saves-restored";// Save backups: <games>/<install id>.saves/<Unix time>/users, the newest few kept.constexpr char kSaveBackupSuffix[] = ".saves";constexpr std::size_t kSaveBackupsKept = 3;@@ -2041,7 +2068,7 @@ void RuntimeHost::PublishKeptBottles(const Composition& c) {// removal continues where it stopped: retire the title folder out of ShadowMount's// scan root, unregister the title through the one-shot remover, keep or delete the// bottle, then drop the record once the console no longer lists the title.-std::string RuntimeHost::RunRemove(Composition& c, std::uint32_t app, bool delete_saves) {+std::string RuntimeHost::RunRemove(Composition& c, std::uint32_t app, bool delete_saves, bool moving) {std::optional<rt::RuntimeInstall> found;for (const auto& record : c.repository.Snapshot())if (record.app_id == app) found = record;@@ -2080,10 +2107,16 @@ std::string RuntimeHost::RunRemove(Composition& c, std::uint32_t app, bool deletconst auto operation = config_.uuid();if (!operation) return "No secure random source for the removal.";record.state = rt::PreparationState::kRemoving;- record.journal = rt::PreparationJournal{*operation, delete_saves ? "remove-saves" : "remove", "retire"};+ record.journal = rt::PreparationJournal{*operation, delete_saves ? "remove-saves" : moving ? "remove-move" : "remove",+ "retire"};record.last_failure.reset();if (auto failed = save("start"); !failed.empty()) return failed;}+ // An uninstall that deletes saves takes over a removal its game's move started (that one keeps them).+ if (resuming && delete_saves && record.journal->operation == "remove-move") {+ record.journal->operation = "remove-saves";+ if (auto failed = save("adopt"); !failed.empty()) return failed;+ }delete_saves = record.journal->operation == "remove-saves";const auto fail = [&](const std::string& code, const std::string& message) {record.last_failure = rt::RuntimeFailure{code, message};@@ -2175,14 +2208,25 @@ std::string RuntimeHost::RunRemove(Composition& c, std::uint32_t app, bool deletfsync(fd) == 0;if (fd >= 0) close(fd);if (!written) return fail("bottle-keep", "The game's saves could not be marked as kept.");- if (lstat(kept.c_str(), &info) == 0) {- // An older kept copy was never brought back; the newer bottle replaces it.+ const bool restored = lstat((bottle / kRestoredMarker).c_str(), &info) == 0;+ if (lstat(kept.c_str(), &info) == 0 && !restored) {+ // The kept copy's saves were never brought back into this bottle (a preparation after a move or+ // reinstall stopped before that): the kept copy holds them and stays. The newer bottle is set+ // aside beside it, never deleted.+ const auto aside = bottle.parent_path() / (record.install_id + ".set-aside-" + record.journal->operation_id);+ Log("remove keeps the unrestored kept bottle install=" + record.install_id + "; newer bottle set aside");+ if (rename(bottle.c_str(), aside.c_str()) != 0)+ return fail("bottle-keep", "The game's newer bottle could not be set aside (error " + std::to_string(errno) + ").");+ } else if (lstat(kept.c_str(), &info) == 0) {+ // An older kept copy whose saves this bottle already has: the bottle replaces it.Log("remove replaces older kept bottle install=" + record.install_id);RemoveTreeNoFollow(kept, error);if (error) return fail("bottle-keep", "An older copy of the game's saves could not be replaced.");- }- if (rename(bottle.c_str(), kept.c_str()) != 0)+ if (rename(bottle.c_str(), kept.c_str()) != 0)+ return fail("bottle-keep", "The game's saves could not be kept (error " + std::to_string(errno) + ").");+ } else if (rename(bottle.c_str(), kept.c_str()) != 0) {return fail("bottle-keep", "The game's saves could not be kept (error " + std::to_string(errno) + ").");+ }}if (auto failed = advance("erase"); !failed.empty()) return failed;}@@ -2192,6 +2236,16 @@ std::string RuntimeHost::RunRemove(Composition& c, std::uint32_t app, bool deletstruct stat info{};confirmed.source_path = lstat(source.c_str(), &info) != 0 && errno == ENOENT ? rt::TitleAvailability::kFree: rt::TitleAvailability::kOccupied;+ // Saves an uninstall deletes after a move had already kept them (it took over past the bottle step):+ // gone before the record, so a failure keeps the journal and the next attempt deletes them again.+ if (delete_saves) {+ const auto kept = HostPath(record.data_root).parent_path() / (record.install_id + kKeptSuffix);+ std::error_code error;+ struct stat kept_info{};+ if (lstat(kept.c_str(), &kept_info) == 0) RemoveTreeNoFollow(kept, error);+ if (!error) RemoveTreeNoFollow(SaveBackupRoot(record.install_id), error);+ if (error) return fail("bottle-delete", "The game's saves and settings could not be deleted: " + error.message());+ }const auto erased = c.repository.EraseAfterUnregister(record.install_id, confirmed);if (!erased.ok()) return fail("erase", "The removal could not be completed: " + erased.message());// The title is gone; a stale entry would only let a later title with its ID start early.@@ -2242,6 +2296,12 @@ std::string RuntimeHost::RestoreKeptSaves(Composition& c, std::uint32_t app) {return "The saves kept from the previous install could not be brought back: " + reason;}}+ {+ const int fd = open((bottle / kRestoredMarker).c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0600);+ const bool marked = fd >= 0 && fsync(fd) == 0;+ if (fd >= 0) close(fd);+ if (!marked) return "The saves were brought back, but that could not be recorded.";+ }std::error_code error;RemoveTreeNoFollow(kept, error);Log("restore kept saves app=" + std::to_string(app) + " kept_removed=" + std::to_string(!error));diff --git a/src/homebrew/runtime_host.hpp b/src/homebrew/runtime_host.hppindex f69ce86..e3c04bb 100644--- a/src/homebrew/runtime_host.hpp+++ b/src/homebrew/runtime_host.hpp@@ -124,6 +124,11 @@ struct RuntimeInstallView {std::string install_id;std::uint32_t app_id = 0;std::string title_id;+ // The game folder the title starts (console path). When the game is installed again on another+ // drive it no longer matches, and Prepare makes the title again.+ std::string install_path;+ // Its title is being removed to follow the game to another drive; Prepare finishes that.+ bool moving = false;// The game's private bottle (console path); Cloud saves under Windows// folders live here.std::string prefix_root;@@ -347,7 +352,9 @@ class RuntimeHost {const std::string& session_id);void RunPlay(Composition& composition, std::uint32_t app_id);// Removes a game's PS5 title; empty when it has none left, otherwise why not.- std::string RunRemove(Composition& composition, std::uint32_t app_id, bool delete_saves);+ // moving: the title goes because its game was installed again elsewhere (journal "remove-move"),+ // so Prepare, not Uninstall, finishes a removal that stopped part way.+ std::string RunRemove(Composition& composition, std::uint32_t app_id, bool delete_saves, bool moving = false);// Whether the console still lists the title (app.db or /user/app); unknown counts as listed.bool TitleListed(Composition& composition, const std::string& title_id);// Moves the saves of a kept bottle into the game's new bottle and drops the kept copy.diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cppindex 8a52860..06f1800 100644--- a/tests/homebrew_runtime_host_test.cpp+++ b/tests/homebrew_runtime_host_test.cpp@@ -1477,6 +1477,102 @@ void KitRespinKeepsWaitingSettingsUpdate() {// scan root, the title is unregistered through the remover, the bottle is kept// (or deleted on request) and the record goes once the console no longer lists// the title. Every step is journaled and continues after a failure or restart.+// A game installed again on another drive (its uninstall stopped part way and left the title):+// Prepare removes the old title, keeping its saves, and prepares the game where it is now.+void PreparesGameInstalledAgainElsewhere() {+ Fixture fixture;+ auto config = fixture.Config();+ config.host_namespace_prefix = fixture.root;+ config.package_paths.console_data_root = "/data";+ config.package_paths.host_data_root = fixture.root / "data";+ config.package_operations.hash_file = HashArtifactFile;+ config.package_operations.hash_bytes = HashArtifactBytes;+ config.package_operations.free_space_unverifiable = true;+ config.package_operations.validate_bottle = [](const fs::path& bottle) {+ return fs::is_regular_file(bottle / "system.reg") && fs::is_regular_file(bottle / "user.reg");+ };+ const auto kit = SyntheticKit(fixture.root / "kit");+ config.admit_kit = [&kit](std::string*) { return std::optional<RuntimeKit>(kit); };+ unsigned uuids = 0;+ config.uuid = [&uuids]() -> std::optional<std::string> {+ ++uuids; return std::string("00000000-0000-4000-8000-0000000000") + (uuids < 10 ? "0" : "") + std::to_string(uuids);+ };+ config.unix_time = [] { return std::int64_t{1760000000}; };+ config.removal_confirm_polls = 3;+ config.removal_poll_interval = std::chrono::milliseconds(1);+ std::vector<std::string> removed_titles;+ bool remover_works = false; // silent at first: the console never confirms+ config.unregister_title = [&fixture, &removed_titles, &remover_works](const std::string& title, const std::string&) {+ removed_titles.push_back(title);+ if (!remover_works) return TitleRemoverResult{true, std::nullopt, "no answer"};+ fixture.registered = false; fixture.submitted = false;+ return TitleRemoverResult{true, 0, "answered"};+ };+ Write(fixture.root / "data/homebrew/FAKE77031/games/220/hl2.exe", "existing-game", 0644);+ const auto bottle = fixture.root / "data/playden/games/steam-220";+ const auto save = fs::path("library/prefixes/game/drive_c/users/steamuser/AppData/Roaming/Game/save.dat");+ const auto prepare = [](RuntimeHost& host, const RuntimePrepareRequest& request) {+ const auto before = host.Snapshot().prepares_handled;+ host.Prepare(request);+ const auto done = WaitFor(host, [before](const RuntimeSnapshot& s) {+ return !s.preparing_app && s.prepares_handled > before;+ }, 60);+ if (done.refusals.count(220)) std::fprintf(stderr, "Prepare refused: %s\n", done.refusals.at(220).c_str());+ return done;+ };+ RuntimeHost host(config);+ CHECK(host.Start());+ CHECK(WaitFor(host, Started).kit_available);+ auto first = prepare(host, Hl2());+ CHECK(!first.refusals.count(220) && first.installs.size() == 1 &&+ first.installs[0].install_path == "/data/homebrew/FAKE77031/games/220");+ Write(bottle / save, "player-save", 0600);+ // The download went away with an uninstall that stopped; the game is on the USB drive now.+ fs::remove_all(fixture.root / "data/homebrew/FAKE77031/games/220");+ auto elsewhere = Hl2();+ elsewhere.install_path = "/mnt/usb0/playden/games/220";+ elsewhere.volume_id = "usb0";+ // Not there yet: nothing is removed.+ auto refused = prepare(host, elsewhere);+ CHECK(refused.refusals.count(220) && refused.refusals.at(220).find("missing") != std::string::npos);+ CHECK(removed_titles.empty() && refused.installs.size() == 1);+ const auto usb = fixture.root / "mnt/usb0/playden/games/220";+ Write(usb / "hl2.exe", "existing-game", 0644);+ for (const auto& folder : {fixture.root / "mnt", fixture.root / "mnt/usb0", fixture.root / "mnt/usb0/playden",+ fixture.root / "mnt/usb0/playden/games", usb})+ CHECK(chmod(folder.c_str(), 0755) == 0);+ // The console doesn't confirm the old title is gone: the move waits, recorded as a move, so the+ // game page offers to finish it rather than an uninstall.+ const auto stopped = prepare(host, elsewhere);+ CHECK(stopped.refusals.count(220) && stopped.refusals.at(220).find("Play tries again") != std::string::npos);+ CHECK(stopped.installs.size() == 1 && stopped.installs[0].state == PreparationState::kRemoving &&+ stopped.installs[0].moving);+ remover_works = true;+ const auto moved = prepare(host, elsewhere);+ CHECK(!moved.refusals.count(220) && moved.installs.size() == 1 && !moved.installs[0].moving);+ CHECK(moved.installs[0].install_path == "/mnt/usb0/playden/games/220");+ CHECK(removed_titles.size() == 2 && removed_titles[1] == first.installs[0].title_id);+ CHECK(Read(bottle / save) == "player-save" && moved.kept_bottles.empty());+ // The move brought the saves back and says so in the bottle.+ CHECK(fs::exists(bottle / ".playden-saves-restored"));+ // A kept copy whose saves never came back (a preparation stopped before that, so no mark): an+ // uninstall keeping saves leaves it as it is and sets the newer bottle aside beside it.+ fs::remove(bottle / ".playden-saves-restored");+ const auto kept = fixture.root / "data/playden/games/steam-220.kept";+ Write(kept / save, "original-save", 0600);+ const auto before = host.Snapshot().removals_handled;+ host.Remove(220, false);+ const auto removed = WaitFor(host, [before](const RuntimeSnapshot& s) { return s.removals_handled > before; }, 30);+ CHECK(!removed.removal_failures.count(220) && removed.installs.empty());+ CHECK(Read(kept / save) == "original-save" && !fs::exists(bottle));+ bool aside = false;+ for (const auto& entry : fs::directory_iterator(fixture.root / "data/playden/games"))+ aside |= entry.path().filename().string().rfind("steam-220.set-aside-", 0) == 0 &&+ Read(entry.path() / save) == "player-save";+ CHECK(aside);+ host.Shutdown();+}+void RemovesTitleKeepingSaves() {Fixture fixture;auto config = fixture.Config();@@ -2077,6 +2173,7 @@ int main(int argc, char** argv) {SettingsSavedDuringPrepareReachReadyTitle();KitRespinKeepsWaitingSettingsUpdate();RemovesTitleKeepingSaves();+ PreparesGameInstalledAgainElsewhere();ShippedRecipePreparesWithAnotherKitsRecipe();ShippedRecipeRequirementsFollowTheKit(false);ShippedRecipeRequirementsFollowTheKit(true);