Play your Steam library's Windows games on a jailbroken PS5.
C++
Wait for Steam instead of failing a download, keep queue order
Leonardo Lobato003c92d8dbde
diff --git a/src/homebrew/simulation.cpp b/src/homebrew/simulation.cppindex 9095bf0..d1bd249 100644--- a/src/homebrew/simulation.cpp+++ b/src/homebrew/simulation.cpp@@ -3116,8 +3116,7 @@ void Simulation::rebuild_view() {// focus moves in that same order, so Up and Down follow what is on screen.const auto active = std::find_if(jobs.begin(), jobs.end(), [](const Job* job) {return job->state == JobState::Downloading || job->state == JobState::Verifying ||- job->state == JobState::Pausing || job->state == JobState::Paused ||- job->state == JobState::Failed || job->state == JobState::Interrupted;+ job->state == JobState::Pausing;});if (active != jobs.end()) std::rotate(jobs.begin(), active, active + 1);for (const auto* job_pointer : jobs) {diff --git a/src/homebrew/view.cpp b/src/homebrew/view.cppindex 639eaff..26a602f 100644--- a/src/homebrew/view.cpp+++ b/src/homebrew/view.cpp@@ -1754,10 +1754,11 @@ class Render::Implstd::stable_sort(jobs.begin(), jobs.end(), [](const Job* left, const Job* right) {return left->queue_order < right->queue_order;});+ // The card shows the running transfer, or else the first one in queue order (a failed or paused+ // download does not jump ahead of the queue).const auto active = std::find_if(jobs.begin(), jobs.end(), [](const Job* job) {return job->state == JobState::Downloading || job->state == JobState::Verifying ||- job->state == JobState::Pausing || job->state == JobState::Paused ||- job->state == JobState::Failed || job->state == JobState::Interrupted;+ job->state == JobState::Pausing;});// The card shows a transfer; finished downloads only follow the queue in the list below.const Job* primary = active != jobs.end() ? *active :diff --git a/src/steam/backend/steam_install_runner.cpp b/src/steam/backend/steam_install_runner.cppindex 9a7c3ac..db1c901 100644--- a/src/steam/backend/steam_install_runner.cpp+++ b/src/steam/backend/steam_install_runner.cpp@@ -86,6 +86,7 @@ InstallRunner::Result SteamInstallRunner::Install(const StoredSession& session,if (!info.ok()) {log("app_info", CmErrorName(info.error), "");result.failure = "Could not reach Steam. Resume to try again.";+ result.retry_later = info.error == CmError::kNetwork || info.error == CmError::kTimeout;return result;}const auto buffer = info.buffers.find(id);@@ -108,6 +109,7 @@ InstallRunner::Result SteamInstallRunner::Install(const StoredSession& session,if (!entitlements.ok()) {log("entitlements", CmErrorName(entitlements.error), "");result.failure = "Could not read this account's licenses. Resume to try again.";+ result.retry_later = entitlements.error == CmError::kNetwork || entitlements.error == CmError::kTimeout;return result;}if (entitlements.skipped_packages)@@ -139,6 +141,7 @@ InstallRunner::Result SteamInstallRunner::Install(const StoredSession& session,if (!runtime::WriteSteamworksFacts(root, depots, dlc)) log("steamworks_facts", "write-failed", root.c_str());}result.canceled = outcome.error == InstallError::kCanceled;+ result.retry_later = outcome.error == InstallError::kNetwork;result.failure = InstallFailureText(outcome.error);result.install_bytes = outcome.progress.install_total;return result;diff --git a/src/steam/install_service.cpp b/src/steam/install_service.cppindex 0051acc..935dbe5 100644--- a/src/steam/install_service.cpp+++ b/src/steam/install_service.cpp@@ -71,6 +71,9 @@ constexpr const char* kDefaultVolume = ".playden-default-volume";constexpr const char* kDismissed = ".playden-downloads-dismissed";constexpr const char* kVolumeUnavailable = "The game's storage is not available.";constexpr std::chrono::seconds kSessionRetry{5};+// A download that could not reach Steam waits this long before it tries again by itself.+constexpr std::chrono::seconds kNetworkRetry{30};+constexpr const char* kWaitingForSteam = "Waiting to reach Steam. It tries again by itself.";bool Exists(const std::string& path) {struct stat info {};@@ -530,6 +533,7 @@ void InstallService::Run() {continue;}job.view.state = InstallJobState::kRunning;+ if (job.view.failure == kWaitingForSteam) job.view.failure.clear();job.rate.Reset();job.view.bytes_per_second.reset(); job.view.eta_seconds.reset();next = job.view.id;@@ -580,7 +584,7 @@ void InstallService::Run() {bool remove_files = false;{- std::lock_guard<std::mutex> lock(mutex_);+ std::unique_lock<std::mutex> lock(mutex_);active_.reset();auto* job = Find(*next);if (job && job->cancel_requested) {@@ -595,6 +599,16 @@ void InstallService::Run() {} else if (job && job->view.state == InstallJobState::kRunning && result.canceled && stopping_) {// Playden is closing: the download stays queued, so the next start carries on with it.job->view.state = InstallJobState::kQueued;+ } else if (job && job->view.state == InstallJobState::kRunning && !result.ok && !result.canceled && result.retry_later) {+ // Steam or the network is out of reach (it often is for a moment after Playden starts): the+ // download keeps its place and tries again shortly instead of failing.+ job->view.state = InstallJobState::kQueued;+ job->view.failure = kWaitingForSteam;+ job->rate.Reset(); job->view.bytes_per_second.reset(); job->view.eta_seconds.reset();+ Publish();+ // Any request (Pause, Resume, Install, Shutdown) also ends the wait.+ if (!stopping_) wake_.wait_for(lock, kNetworkRetry);+ continue;} else if (job && job->view.state == InstallJobState::kRunning) {// A Pause or Resume that arrived while the run was stopping already chose the state.job->view.state = result.canceled ? InstallJobState::kPaused : InstallJobState::kFailed;diff --git a/src/steam/install_service.h b/src/steam/install_service.hindex 8f14d08..bc76e70 100644--- a/src/steam/install_service.h+++ b/src/steam/install_service.h@@ -95,6 +95,7 @@ class InstallRunner {bool canceled = false;std::string failure;std::uint64_t install_bytes = 0;+ bool retry_later = false; // Steam or the network was out of reach: the job waits and tries again};virtual Result Install(const StoredSession& session, GameId id, const std::string& root,const std::atomic<bool>& cancel,diff --git a/tests/homebrew_library_model_test.cpp b/tests/homebrew_library_model_test.cppindex 88ccbbb..be299d0 100644--- a/tests/homebrew_library_model_test.cpp+++ b/tests/homebrew_library_model_test.cpp@@ -1441,6 +1441,20 @@ void game_collections_sheet_adds_and_removes_the_game() {"a rename shows in the open sheet");}+void live_download_card_keeps_queue_order() {+ using namespace playden::homebrew;+ auto model = make_live();+ // The download retried a moment ago waits in the queue; a failed one behind it must not take the card.+ model.UpdateInstalls({{42, JobState::Queued, 0, 100, {}}, {43, JobState::Failed, 0, 100, "Could not reach Steam."}}, {});+ model.action("r1");+ model.action("r1");+ check(model.state().navigation.route == Route::Downloads, "Downloads open");+ check(!model.view().rows.empty() && model.view().rows[0].key == "job-42" && model.view().rows[1].key == "job-43",+ "the first download in the queue keeps the top spot ahead of a failed one");+ model.UpdateInstalls({{42, JobState::Queued, 0, 100, {}}, {43, JobState::Downloading, 5, 100, {}}}, {});+ check(model.view().rows[0].key == "job-43", "a running download takes the top spot");+}+void live_download_reorder_reaches_service() {using namespace playden::homebrew;auto model = make_live();@@ -1476,6 +1490,7 @@ int main() {live_install_confirmation_checks_space_and_unknowns();live_install_notifications();live_completed_downloads_follow_the_queue();+ live_download_card_keeps_queue_order();game_collections_sheet_adds_and_removes_the_game();live_log_viewer();live_cloud_saves();diff --git a/tests/install_service_test.cpp b/tests/install_service_test.cppindex b6754af..b6b62c4 100644--- a/tests/install_service_test.cpp+++ b/tests/install_service_test.cpp@@ -46,6 +46,7 @@ class Runner final : public InstallRunner {std::uint64_t held = 0; // bytes the fake install reports on diskstd::atomic<int> installs{0};std::atomic<int> canceled{0};+ std::atomic<int> unreachable{0}; // this many runs find Steam out of reachstd::mutex mutex;std::vector<std::string> removed;std::vector<std::string> install_roots;@@ -56,6 +57,7 @@ class Runner final : public InstallRunner {std::lock_guard<std::mutex> lock(mutex);install_roots.push_back(root);}+ if (unreachable > 0) { --unreachable; return {false, false, "Could not reach Steam.", 0, true}; }if (telemetry) { progress(9000000000ull, 10000000000ull, held); progress(9100000000ull, 10000000000ull, held); }else progress(10, 100, held);while (block) {@@ -260,9 +262,31 @@ void CompletedDownloadsList(const std::filesystem::path& root) {std::filesystem::remove_all(root);}+// A download that cannot reach Steam keeps its place and waits instead of failing.+void UnreachableSteamWaits(const std::filesystem::path& root) {+ std::filesystem::remove_all(root);+ Sessions sessions;+ Runner runner;+ runner.unreachable = 1;+ InstallService service(runner, sessions, root.string());+ service.Install(80);+ service.Install(81);+ AwaitAt(__LINE__, service, [](const InstallSnapshot& s) {+ const auto* job = JobOf(s, 80);+ return job && job->state == InstallJobState::kQueued && job->failure.find("Waiting to reach Steam") == 0;+ });+ const auto waiting = service.Snapshot();+ CHECK(waiting.jobs.size() == 2 && waiting.jobs[0].id == 80 && waiting.jobs[1].id == 81 &&+ waiting.jobs[1].state == InstallJobState::kQueued);+ service.Resume(80); // a request ends the wait at once+ AwaitAt(__LINE__, service, [](const InstallSnapshot& s) { return Installed(s, 80) && Installed(s, 81); });+ std::filesystem::remove_all(root);+}+int main() {SpaceAdmission(std::filesystem::temp_directory_path() / ("playden-space-service-" + std::to_string(::getpid())));LiveTelemetryDecaysWithoutCallbacks(std::filesystem::temp_directory_path() / ("playden-rate-service-" + std::to_string(::getpid())));+ UnreachableSteamWaits(std::filesystem::temp_directory_path() / ("playden-unreachable-service-" + std::to_string(::getpid())));CompletedDownloadsList(std::filesystem::temp_directory_path() / ("playden-completed-service-" + std::to_string(::getpid())));const auto requests = ParseInstallRequests("uninstall 439490\n439490\ncancel 108600\n108600\n");CHECK(requests.size() == 4);