Play your Steam library's Windows games on a jailbroken PS5.
C++
Fix queue edge cases found in review
Leonardo Lobato50a819ca3d5d
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cppindex 5271d1e..3b50ab4 100644--- a/src/homebrew/controller.cpp+++ b/src/homebrew/controller.cpp@@ -687,8 +687,11 @@ void Controller::TickRuntime() {runtime_->HoldPreparations(model_.state().quit_waiting);for (const auto id : model_.TakeRuntimePrepareRequests()) RequestPrepare(id);// A game Playden still holds back moves to the front once it is asked for.- for (const auto id : model_.TakePrepareNextRequests())- if (!runtime_->PrepareNext(id)) prepare_next_pending_ = id;+ // The latest choice wins: a game Playden still holds back moves first once it is asked for.+ for (const auto id : model_.TakePrepareNextRequests()) {+ if (runtime_->PrepareNext(id)) prepare_next_pending_.reset();+ else prepare_next_pending_ = id;+ }for (const auto& done : model_.TakePreparedDismissals()) runtime_->DismissPrepared(done.game_id, done.finished_at);if (model_.TakePreparedClear()) runtime_->ClearPrepared();if (model_.TakeRuntimeCleanupRequest()) runtime_->CleanUpRequests();@@ -1191,6 +1194,13 @@ void Controller::TickRuntime() {void Controller::BeginUninstall(GameId id, bool delete_saves) {// Without the runtime there is no PS5 title to remove: the download goes as before.if (!runtime_) { if (installs_) installs_->Uninstall(id); return; }+ // A preparation still waiting would make a title for files about to be deleted: it leaves the queue.+ // One already running finishes first (TickUninstalls waits), so its title is removed with the rest.+ runtime_->WithdrawPrepare(id);+ std::erase(auto_prepare_, id);+ prepare_after_details_.erase(id);+ play_after_prepare_.erase(id);+ if (prepare_next_pending_ == id) prepare_next_pending_.reset();// With automatic sync off, nothing moves to or from Steam Cloud unless the player syncs from Saves.if (uninstalls_.Begin(id, delete_saves, cloud_ && game_roots_.count(id) && !saves_root_.empty() && model_.state().cloud_saves_auto))++play_notes_revision_;@@ -1207,6 +1217,7 @@ void Controller::TickUninstalls() {const auto& snapshot = *snapshot_shared;if (snapshot.status == RuntimeHostStatus::kStarting) return;for (const auto id : ids) {+ if (snapshot.preparing_app == id) continue; // its title is removed once the preparation endsUninstallFacts facts;facts.has_title = std::any_of(snapshot.installs.begin(), snapshot.installs.end(),[id](const RuntimeInstallView& view) { return view.app_id == id; });diff --git a/src/homebrew/queue_layout.cpp b/src/homebrew/queue_layout.cppindex abdd78a..6d8d706 100644--- a/src/homebrew/queue_layout.cpp+++ b/src/homebrew/queue_layout.cpp@@ -56,6 +56,7 @@ QueueSection downloads(const State& state) {else if (job->state == JobState::Queued) ++section.waiting;else if (job->state == JobState::Paused || job->state == JobState::Interrupted) ++paused;else if (job->state == JobState::Failed) ++failed;+ else if (job->state == JobState::Downloaded && job->kind != "install") ++section.updates;if (card != jobs.end() && job == *card) section.active = entry(*job);// A staged Playden update is a Downloaded job that still asks for a restart: it stays with the work.else if (job->state == JobState::Downloaded && job->kind == "install") finished.push_back(job);@@ -76,6 +77,7 @@ std::string downloads_words(const QueueSection& section, std::size_t paused, stdif (section.waiting) join(text, std::to_string(section.waiting) + " waiting");if (paused) join(text, std::to_string(paused) + " paused");if (failed) join(text, std::to_string(failed) + " failed");+ if (section.updates) join(text, "Playden update ready");return text;}@@ -171,13 +173,14 @@ void summarize(QueueSection& section, const std::string& expanded_words, const sconst std::size_t more = section.rows.size() - section.completed; // unfinished, after the cardif (section.expanded) {text = expanded_words;- if (section.running && !section.waiting && !section.attention) join(text, "Nobody waiting");+ if (section.running && !section.waiting && !section.attention) join(text, "No games waiting");} else if (section.active) {text = more == 0 ? "No other games queued": more == 1 ? "1 more game queued" : std::to_string(more) + " more games queued";join(text, attention_words);} else {- join(text, attention_words);+ // Nothing runs: what waits, needs attention or is ready.+ text = expanded_words;}if (section.completed) join(text, std::to_string(section.completed) + " completed");if (text.empty()) text = section.unfinished() ? expanded_words : idle;@@ -236,6 +239,7 @@ QueueLayout BuildQueueLayout(const State& state) {if (download.waiting + prepare.waiting) join(summary, std::to_string(download.waiting + prepare.waiting) + " waiting");if (download.attention + prepare.attention)join(summary, need_attention(download.attention + prepare.attention));+ if (download.updates) join(summary, "Playden update ready");if (summary.empty() && !layout.nothing())summary = "No active work · " + std::to_string(download.completed + prepare.completed) + " completed";layout.summary = summary;diff --git a/src/homebrew/queue_layout.hpp b/src/homebrew/queue_layout.hppindex 0c9c14b..2048cad 100644--- a/src/homebrew/queue_layout.hpp+++ b/src/homebrew/queue_layout.hpp@@ -40,6 +40,7 @@ struct QueueSection {std::size_t waiting = 0; // queued to run, card included when it only waitsstd::size_t attention = 0; // paused, failed or blocked: not ordinary waiting workstd::size_t completed = 0;+ std::size_t updates = 0; // a downloaded Playden update waiting for a restartbool expanded = false;std::string summary; // the heading's right-hand wordsdiff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cppindex 0866bd2..e99d15f 100644--- a/src/homebrew/runtime_host.cpp+++ b/src/homebrew/runtime_host.cpp@@ -443,6 +443,20 @@ bool RuntimeHost::PrepareNext(std::uint32_t app_id) {return true;}+bool RuntimeHost::WithdrawPrepare(std::uint32_t app_id) {+ {+ std::lock_guard<std::mutex> lock(mutex_);+ const auto queued = std::find_if(prepare_requests_.begin(), prepare_requests_.end(),+ [&](const auto& waiting) { return waiting.app_id == app_id; });+ if (queued == prepare_requests_.end()) return false;+ prepare_requests_.erase(queued);+ std::erase(snapshot_.queued_prepares, app_id);+ ++snapshot_.revision;+ }+ published_.notify_all();+ return true;+}+void RuntimeHost::DismissPrepared(std::uint32_t app_id, std::int64_t finished_at) {{std::lock_guard<std::mutex> lock(mutex_);@@ -470,8 +484,12 @@ void RuntimeHost::LoadPreparedHistory() {std::ifstream in(config_.state_root / kPreparedHistory);std::uint32_t app = 0;long long finished = 0;- while (prepared_history_.size() < kMaxPrepared * 8 && in >> app >> finished)- if (app != 0 && finished > 0) prepared_history_.push_back({app, static_cast<std::int64_t>(finished)});+ // Oldest first: a long file keeps its newest entries.+ while (in >> app >> finished) {+ if (app == 0 || finished <= 0) continue;+ prepared_history_.push_back({app, static_cast<std::int64_t>(finished)});+ if (prepared_history_.size() > kMaxPrepared * 8) prepared_history_.erase(prepared_history_.begin());+ }}void RuntimeHost::SavePreparedHistory() {@@ -480,6 +498,9 @@ void RuntimeHost::SavePreparedHistory() {std::erase_if(prepared_history_, [now](const PreparedGame& entry) {return entry.finished_at > now || now - entry.finished_at > kPreparedDays * 24 * 60 * 60;});+ if (prepared_history_.size() > kMaxPrepared * 8)+ prepared_history_.erase(prepared_history_.begin(),+ prepared_history_.end() - static_cast<std::ptrdiff_t>(kMaxPrepared * 8));std::string text;for (const auto& entry : prepared_history_)text += std::to_string(entry.app_id) + "\t" + std::to_string(entry.finished_at) + "\n";diff --git a/src/homebrew/runtime_host.hpp b/src/homebrew/runtime_host.hppindex c176abe..8bd456f 100644--- a/src/homebrew/runtime_host.hpp+++ b/src/homebrew/runtime_host.hpp@@ -330,6 +330,9 @@ class RuntimeHost {// Moves a queued preparation to the front of the queue. The one running now finishes first.// False when the game is not queued (it started or was never asked for): nothing changes.bool PrepareNext(std::uint32_t app_id);+ // Takes a queued preparation out of the queue (its game is being uninstalled). False when it is not+ // queued: it started already, or was never asked for.+ bool WithdrawPrepare(std::uint32_t app_id);// While held, no queued preparation starts (a quit to Home waits for the running one only).void HoldPreparations(bool hold);// Takes a finished preparation off RuntimeSnapshot::prepared; ClearPrepared takes every one off,diff --git a/src/homebrew/view.cpp b/src/homebrew/view.cppindex 2556516..2d45daa 100644--- a/src/homebrew/view.cpp+++ b/src/homebrew/view.cpp@@ -2101,7 +2101,10 @@ class Render::Implconstexpr float line_y = 902.0F, baseline = 946.0F;scene_.rounded_rect({kSafeX, line_y, 1824.0F - kSafeX, 1.5F}, 0.0F, kText.with_alpha(0.12F));ui::text(scene_, fonts_.display, ui::upper("Storage"), kSafeX, baseline, 22.0F, kSecondary, gfx::Align::left, 2.6F);- float x = kSafeX + fonts_.display.measure(ui::upper("Storage"), 22.0F) + 2.6F * 7.0F + 36.0F;+ const float first_x = kSafeX + fonts_.display.measure(ui::upper("Storage"), 22.0F) + 2.6F * 7.0F + 36.0F;+ struct Drive { const ViewRow *row; const Volume *volume; std::string free; float width; };+ std::vector<Drive> drives;+ int focused = -1;for (const ViewRow &row : view.rows){if (row.key.rfind("storage-", 0) != 0)@@ -2111,30 +2114,59 @@ class Render::Implif (volume == state.volumes.end())continue;const std::string free = drive_free_words(*volume);- const float name_width = fonts_.regular.measure(volume->label, 24.0F);- const float free_width = fonts_.regular.measure(free, 22.0F);- const float bar_width = volume->capacity_known ? 72.0F : 0.0F;- const float item_width = name_width + (bar_width > 0.0F ? bar_width + 16.0F : 0.0F) + 16.0F + free_width + 34.0F;- if (x + item_width > 1824.0F)+ const float width = fonts_.regular.measure(volume->label, 24.0F) + (volume->capacity_known ? 88.0F : 0.0F) + 16.0F ++ fonts_.regular.measure(free, 22.0F) + 34.0F;+ if (row.key == state.navigation.focus_key ||+ (!state.navigation.modals.empty() && row.key == state.navigation.modals.front().opener_focus_key))+ focused = static_cast<int>(drives.size());+ drives.push_back({&row, &*volume, free, width});+ }+ // Drives that don't fit wait past the end; the line starts later so the focused one shows.+ constexpr float room_end = 1824.0F - 80.0F;+ std::size_t begin = 0;+ const auto fits = [&](std::size_t from, std::size_t to) {+ float x = first_x;+ for (std::size_t index = from; index <= to; ++index)+ x += drives[index].width + 40.0F;+ return x - 40.0F <= room_end;+ };+ while (focused > 0 && begin < static_cast<std::size_t>(focused) && !fits(begin, static_cast<std::size_t>(focused)))+ ++begin;+ float x = first_x;+ if (begin > 0)+ {+ const std::string earlier = "‹ " + std::to_string(begin);+ ui::text(scene_, fonts_.regular, earlier, first_x, baseline, 22.0F, kMuted);+ x += fonts_.regular.measure(earlier, 22.0F) + 28.0F;+ }+ std::size_t index = begin;+ for (; index < drives.size(); ++index)+ {+ const Drive &drive = drives[index];+ if (x + drive.width > room_end)break;- const gfx::Rect box{x - 12.0F, baseline - 32.0F, item_width + 24.0F, 46.0F};- const float amount = focus_of(row.key);+ const Volume *volume = drive.volume;+ const gfx::Rect box{x - 12.0F, baseline - 32.0F, drive.width + 24.0F, 46.0F};+ const float amount = focus_of(drive.row->key);focus_glow(scene_, box, amount, true);ui::text(scene_, fonts_.regular, volume->label, x, baseline, 24.0F, volume->available ? kText : kSecondary);- float cursor = x + name_width + 16.0F;- if (bar_width > 0.0F)+ float cursor = x + fonts_.regular.measure(volume->label, 24.0F) + 16.0F;+ if (volume->capacity_known){- draw_usage_bar(*volume, cursor, baseline - 12.0F, bar_width, 6.0F);- cursor += bar_width + 16.0F;+ draw_usage_bar(*volume, cursor, baseline - 12.0F, 72.0F, 6.0F);+ cursor += 88.0F;}- ui::text(scene_, fonts_.regular, free, cursor, baseline, 22.0F,+ ui::text(scene_, fonts_.regular, drive.free, cursor, baseline, 22.0F,volume->available && volume->writable ? kSecondary : kAmber);- cursor += free_width + 14.0F;+ cursor += fonts_.regular.measure(drive.free, 22.0F) + 14.0F;scene_.line(cursor, baseline - 15.0F, cursor + 6.0F, baseline - 9.0F, 2.0F, kSecondary);scene_.line(cursor + 6.0F, baseline - 9.0F, cursor, baseline - 3.0F, 2.0F, kSecondary);focus_ring(scene_, box, amount, true);- x += item_width + 40.0F;+ x += drive.width + 40.0F;}+ if (index < drives.size())+ ui::text(scene_, fonts_.regular, "+" + std::to_string(drives.size() - index) + " ›", 1824.0F, baseline, 22.0F, kMuted,+ gfx::Align::right);}// What the strip says after a drive's name: its free space, or why there is none to report.diff --git a/tests/homebrew_library_model_test.cpp b/tests/homebrew_library_model_test.cppindex 8e977f5..d8612e0 100644--- a/tests/homebrew_library_model_test.cpp+++ b/tests/homebrew_library_model_test.cpp@@ -2340,6 +2340,11 @@ void live_queue_history_and_empty_states() {activate(model, "prep-done-15-3000");activate(model, "prep-clear-completed");check(model.TakePreparedClear() && model.TakeInstallRequests().empty(), "Clear completed clears only preparations");+ // Games waiting with none being prepared (Steam details still on their way) still count when collapsed.+ model.UpdatePreparationQueue({std::nullopt, {14}, {{15, 4'000}}, {}});+ activate(model, "queue-downloads");+ check(queue_row(model, "queue-preparation")->value == "1 waiting · 1 completed", "a collapsed section counts what waits");+ model.UpdatePreparationQueue({});check(!has_row(model, "queue-preparation") && has_row(model, "job-23"), "Preparation empties; Downloads keeps its entry");}diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cppindex 54f10eb..2b88446 100644--- a/tests/homebrew_runtime_host_test.cpp+++ b/tests/homebrew_runtime_host_test.cpp@@ -391,14 +391,17 @@ void PrepareNextReordersWaitingPreparations() {CHECK(host.PrepareNext(30)); // already first: nothing movesCHECK(!host.PrepareNext(99));CHECK((host.Snapshot().queued_prepares == std::vector<std::uint32_t>{30, 10, 20, 40}));+ // An uninstall takes a waiting game out; the others keep their order.+ CHECK(host.WithdrawPrepare(20) && !host.WithdrawPrepare(20));+ CHECK((host.Snapshot().queued_prepares == std::vector<std::uint32_t>{30, 10, 40}));{std::lock_guard<std::mutex> lock(gate_mutex);block = false;}gate_changed.notify_all();- const auto done = WaitFor(host, [](const RuntimeSnapshot& s) { return s.prepares_handled == 4; });+ const auto done = WaitFor(host, [](const RuntimeSnapshot& s) { return s.prepares_handled == 3; });CHECK(done.queued_prepares.empty() && !done.preparing_app);- CHECK((log.Order() == std::vector<std::uint32_t>{30, 10, 20, 40}));+ CHECK((log.Order() == std::vector<std::uint32_t>{30, 10, 40}));CHECK(!host.PrepareNext(30)); // handled: no longer queuedhost.Shutdown();}