Skip to content

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

C++

Keep finished downloads in Downloads after the queue

Leonardo Lobatod9d79e4533b3
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cpp
index af17c0f..c113187 100644
--- a/src/homebrew/controller.cpp
+++ b/src/homebrew/controller.cpp
@@ -1084,6 +1084,8 @@ void Controller::TickInstalls() {
case InstallRequestKind::Cancel: installs_->Cancel(request.game_id); break;
case InstallRequestKind::Uninstall: BeginUninstall(request.game_id, request.delete_saves); break;
case InstallRequestKind::SetDefaultVolume: installs_->SetDefaultVolume(request.volume_id); break;
+ case InstallRequestKind::DismissCompleted: installs_->DismissCompleted(request.game_id); break;
+ case InstallRequestKind::ClearCompleted: installs_->ClearCompleted(); break;
}
}
const auto snapshot = installs_->Snapshot();
@@ -1134,7 +1136,10 @@ void Controller::TickInstalls() {
}
std::vector<LiveInstalledGame> installed;
for (const auto& game : snapshot.installed) installed.push_back({game.id, game.install_bytes, game.volume_id, game.available});
- model_.UpdateInstalls(jobs, installed);
+ std::vector<LiveCompletedDownload> completed;
+ for (const auto& finished : snapshot.completed)
+ completed.push_back({finished.id, finished.install_bytes, finished.volume_id, finished.finished_at});
+ model_.UpdateInstalls(jobs, installed, completed);
}
void Controller::TickStoreDetails() {
if (!store_details_) return;
diff --git a/src/homebrew/simulation.cpp b/src/homebrew/simulation.cpp
index 816fb24..931a46f 100644
--- a/src/homebrew/simulation.cpp
+++ b/src/homebrew/simulation.cpp
@@ -452,7 +452,7 @@ std::string action_label(const std::string& key) {
{"inject-logs-ready", "Inject ready logs"}, {"account-auth", "Authentication"},
{"account-last-sync", "Last library sync"}, {"account-sync", "Sync now"},
{"account-recover", "Reconnect account"}, {"account-switch", "Switch account"},
- {"account-reauth", "Sign in again"}, {"job-resume-all", "Resume all"},
+ {"account-reauth", "Sign in again"}, {"job-resume-all", "Resume all"}, {"job-clear-completed", "Clear completed"},
{"about-build", "Build"}, {"about-framework", "Framework"}, {"about-platform", "Platform"},
{"about-kit", "Compatibility kit"}, {"about-kit-source", "Built from"},
{"about-back", "Back"}, {"inject-library-refresh", "Refresh library fixture"},
@@ -946,7 +946,8 @@ void Simulation::UpdateCloud(const std::vector<LiveCloudGame>& games) {
rebuild_view();
}
-void Simulation::UpdateInstalls(const std::vector<LiveInstallJob>& jobs, const std::vector<LiveInstalledGame>& installed) {
+void Simulation::UpdateInstalls(const std::vector<LiveInstallJob>& jobs, const std::vector<LiveInstalledGame>& installed,
+ const std::vector<LiveCompletedDownload>& completed) {
if (!live()) return;
std::map<GameId, JobState> job_states;
for (const auto& job : jobs) job_states[job.game_id] = job.state;
@@ -998,6 +999,20 @@ void Simulation::UpdateInstalls(const std::vector<LiveInstallJob>& jobs, const s
}
projected.push_back(std::move(job));
}
+ // Finished downloads follow the queue, newest first, until the player dismisses them.
+ for (const auto& finished : completed) {
+ if (live_job_states_.contains(finished.game_id) || !live_installed_ids_.contains(finished.game_id)) continue;
+ Job job;
+ job.id = std::to_string(finished.game_id);
+ job.game_id = finished.game_id;
+ job.queue_order = order++;
+ job.state = JobState::Downloaded;
+ job.completed_bytes = job.total_bytes = finished.install_bytes;
+ job.volume_id = finished.volume_id;
+ job.finished_at = finished.finished_at;
+ job.completion_applied = true;
+ projected.push_back(std::move(job));
+ }
std::vector<Installation> installations;
for (const auto& game : installed)
installations.push_back({game.game_id, game.volume_id, game.install_bytes, game.available,
@@ -4096,7 +4111,11 @@ void Simulation::activate() {
if (behind && (job->state == JobState::Queued || job->state == JobState::Paused || job->state == JobState::Interrupted))
actions.insert(actions.begin(), "job-now-" + id);
if (job && job->state == JobState::Queued) { actions.push_back("job-up-" + id); actions.push_back("job-down-" + id); }
- if (job && job->state == JobState::Downloaded) { actions.push_back("job-open-" + id); actions.push_back("job-dismiss-" + id); }
+ if (job && job->state == JobState::Downloaded) {
+ actions.push_back("job-open-" + id); actions.push_back("job-dismiss-" + id);
+ const auto finished = std::count_if(state_.jobs.begin(), state_.jobs.end(), [](const Job& other) { return other.state == JobState::Downloaded; });
+ if (finished > 1) actions.push_back("job-clear-completed");
+ }
else if (job && job->state != JobState::Cancelled) actions.push_back("job-cancel-" + id);
actions.push_back("logs"); actions.push_back("cancel");
open_modal({"job-actions", "Download actions", selected.key, std::move(actions), 0, false});
@@ -4319,9 +4338,15 @@ void Simulation::activate() {
state_.navigation.selected_game_id = job->game_id; state_.navigation.detail_id = job->game_id; state_.navigation.detail_origin = Route::Downloads; state_.navigation.route = Route::Details;
}
close_modal();
+ } else if (action_name == "job-clear-completed") {
+ close_modal();
+ if (live()) pending_install_requests_.push_back({InstallRequestKind::ClearCompleted, 0});
+ state_.jobs.erase(std::remove_if(state_.jobs.begin(), state_.jobs.end(), [](const Job& value) { return value.state == JobState::Downloaded; }), state_.jobs.end());
} else if (action_name.rfind("job-dismiss-", 0) == 0) {
const auto id = action_name.substr(12);
close_modal();
+ if (const auto* job = find_job(state_, id); live() && job && job->state == JobState::Downloaded)
+ pending_install_requests_.push_back({InstallRequestKind::DismissCompleted, job->game_id});
state_.jobs.erase(std::remove_if(state_.jobs.begin(), state_.jobs.end(), [&id](const Job& value) { return value.id == id && value.state == JobState::Downloaded; }), state_.jobs.end());
} else if (action_name.rfind("job-cancel-", 0) == 0) {
pending_destructive_action_ = action_name;
diff --git a/src/homebrew/simulation.hpp b/src/homebrew/simulation.hpp
index 65c2722..e69587c 100644
--- a/src/homebrew/simulation.hpp
+++ b/src/homebrew/simulation.hpp
@@ -30,7 +30,8 @@ enum class JobStage { Download, Verify, Install, Uninstall };
enum class SessionState { Idle, Preparing, Launching, Running, Stopping, Failed, Crashed };
enum class RuntimeState { Unavailable, Checking, Ready, Failed };
enum class LogState { Loading, Ready, Failed };
-enum class InstallRequestKind { Install, Pause, Resume, DownloadNow, MoveUp, MoveDown, Cancel, Uninstall, SetDefaultVolume };
+enum class InstallRequestKind { Install, Pause, Resume, DownloadNow, MoveUp, MoveDown, Cancel, Uninstall, SetDefaultVolume,
+ DismissCompleted, ClearCompleted };
struct InstallRequest {
InstallRequestKind kind = InstallRequestKind::Install;
GameId game_id = 0;
@@ -60,6 +61,13 @@ struct LiveInstalledGame {
std::string volume_id;
bool available = true; // false while the game's drive is absent
};
+// A recent finished download, listed in Downloads after the queue.
+struct LiveCompletedDownload {
+ GameId game_id = 0;
+ std::uint64_t install_bytes = 0;
+ std::string volume_id;
+ std::int64_t finished_at = 0; // Unix seconds
+};
// A drive the real install service can use. Space is absent when the platform cannot report it.
struct LiveVolume {
std::string id;
@@ -279,6 +287,7 @@ struct Job {
bool reservation_held = false;
bool completion_applied = false;
std::uint64_t held_bytes = 0; // live: installed bytes already on the drive
+ std::int64_t finished_at = 0; // live: when a Downloaded job finished (Unix seconds), 0 unknown
};
struct Volume {
@@ -494,7 +503,8 @@ public:
bool UpdateLibrary(LibraryUpdate update);
// Live mode: requests for the real install service, and its state for display.
[[nodiscard]] std::vector<InstallRequest> TakeInstallRequests();
- void UpdateInstalls(const std::vector<LiveInstallJob>& jobs, const std::vector<LiveInstalledGame>& installed);
+ void UpdateInstalls(const std::vector<LiveInstallJob>& jobs, const std::vector<LiveInstalledGame>& installed,
+ const std::vector<LiveCompletedDownload>& completed = {});
// Live mode: Steam Cloud save requests for the cloud service, and its state for display.
[[nodiscard]] std::vector<CloudRequest> TakeCloudRequests();
void UpdateCloud(const std::vector<LiveCloudGame>& games);
diff --git a/src/homebrew/view.cpp b/src/homebrew/view.cpp
index 95c893f..747cb99 100644
--- a/src/homebrew/view.cpp
+++ b/src/homebrew/view.cpp
@@ -1521,7 +1521,9 @@ class Render::Impl
return status;
const State &state = *current_state_;
const auto job = std::find_if(state.jobs.begin(), state.jobs.end(), [id](const Job &item) {
- return item.game_id == id && item.state != JobState::Cancelled && item.stage != JobStage::Uninstall;
+ // A finished download listed in Downloads is the installation now, not a transfer.
+ return item.game_id == id && item.state != JobState::Cancelled && item.state != JobState::Downloaded &&
+ item.stage != JobStage::Uninstall;
});
if (job != state.jobs.end())
status.job = &*job;
@@ -1755,7 +1757,9 @@ class Render::Impl
job->state == JobState::Pausing || job->state == JobState::Paused ||
job->state == JobState::Failed || job->state == JobState::Interrupted;
});
- const Job* primary = active == jobs.end() ? (jobs.empty() ? nullptr : jobs.front()) : *active;
+ // The card shows a transfer; finished downloads only follow the queue in the list below.
+ const Job* primary = active != jobs.end() ? *active :
+ (jobs.empty() || jobs.front()->state == JobState::Downloaded ? nullptr : jobs.front());
section_label("Transfers", 96.0F, 146.0F);
const gfx::Rect active_card{96.0F, 180.0F, 1000.0F, 230.0F};
@@ -1834,9 +1838,21 @@ class Render::Impl
scene_.rounded_rect(row, 8.0F, kPanel);
dynamic_text(game == nullptr ? std::string_view("Unknown game") : std::string_view(game->title),
122.0F, row_y + 42.0F, 30.0F, kText, 560.0F);
- ui::text(scene_, fonts_.regular, job_state_label(job->state), 712.0F, row_y + 42.0F,
+ const bool finished = job->state == JobState::Downloaded;
+ std::string state_text = job_state_label(job->state);
+ if (finished && job->finished_at > 0 && !state.simulation_mode)
+ {
+ // Short enough to stay clear of the size on the right.
+ const std::int64_t now = static_cast<std::int64_t>(state.clock_ticks);
+ const std::int64_t seconds = std::max<std::int64_t>(0, now - job->finished_at);
+ state_text += " · " + (seconds < 60 ? std::string("just now")
+ : seconds < 3600 ? std::to_string(seconds / 60) + " min ago"
+ : relative_time(job->finished_at, now));
+ }
+ ui::text(scene_, fonts_.regular, state_text, 712.0F, row_y + 42.0F,
22.0F, job->state == JobState::Failed ? kRed : kSecondary);
- const std::string bytes = byte_count(job->completed_bytes) + " / " + byte_count(job->total_bytes);
+ const std::string bytes = finished ? byte_count(job->total_bytes)
+ : byte_count(job->completed_bytes) + " / " + byte_count(job->total_bytes);
ui::text(scene_, fonts_.regular, bytes, 1068.0F, row_y + 42.0F, 22.0F, kSecondary,
gfx::Align::right);
const float progress = job->total_bytes == 0 ? 0.0F :
diff --git a/src/steam/install_service.cpp b/src/steam/install_service.cpp
index 3c64342..0051acc 100644
--- a/src/steam/install_service.cpp
+++ b/src/steam/install_service.cpp
@@ -41,6 +41,23 @@ std::vector<InstallRequest> ParseInstallRequests(const std::string& text) {
}
return requests;
}
+
+std::vector<CompletedDownload> CompletedDownloads(const std::vector<InstalledGame>& installed,
+ const std::vector<std::pair<GameId, std::int64_t>>& dismissed,
+ std::int64_t now) {
+ std::vector<CompletedDownload> completed;
+ for (const auto& game : installed) {
+ if (game.finished_at <= 0 || game.finished_at > now || now - game.finished_at > kCompletedDays * 24 * 60 * 60) continue;
+ if (std::find(dismissed.begin(), dismissed.end(), std::make_pair(game.id, game.finished_at)) != dismissed.end()) continue;
+ completed.push_back({game.id, game.install_bytes, game.volume_id, game.finished_at});
+ }
+ std::stable_sort(completed.begin(), completed.end(), [](const CompletedDownload& left, const CompletedDownload& right) {
+ return left.finished_at > right.finished_at;
+ });
+ if (completed.size() > kMaxCompleted) completed.resize(kMaxCompleted);
+ return completed;
+}
+
namespace {
std::int64_t RateNowMs() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
@@ -51,6 +68,7 @@ constexpr const char* kRecord = ".playden-install.json";
constexpr const char* kJournal = ".playden-download";
constexpr const char* kRegistry = ".playden-games";
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};
@@ -59,6 +77,16 @@ bool Exists(const std::string& path) {
return stat(path.c_str(), &info) == 0;
}
+// The install record is written when an install finishes, so its time is the finish time.
+std::int64_t RecordTime(const std::string& path) {
+ struct stat info {};
+ return stat(path.c_str(), &info) == 0 ? static_cast<std::int64_t>(info.st_mtime) : 0;
+}
+
+std::int64_t WallNow() {
+ return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
+}
+
std::uint64_t RecordedBytes(const std::string& record_path) {
FILE* file = std::fopen(record_path.c_str(), "r");
if (!file) return 0;
@@ -189,7 +217,8 @@ void InstallService::Scan() {
}
const auto root = RootOn(entry.volume_id, entry.id);
if (Exists(root + "/" + kRecord)) {
- installed_.push_back({entry.id, RecordedBytes(root + "/" + kRecord), root, entry.volume_id, true});
+ installed_.push_back({entry.id, RecordedBytes(root + "/" + kRecord), root, entry.volume_id, true,
+ RecordTime(root + "/" + kRecord)});
} else if ((Exists(root + "/" + kJournal) || entry.queued) && jobs_.size() < kMaxJobs) {
// A job that failed or waited before writing anything (no journal yet) stays in
// Downloads too, so a failure while Steam connects doesn't lose the queued game. A job that
@@ -209,6 +238,17 @@ void InstallService::Scan() {
}
std::sort(installed_.begin(), installed_.end(),
[](const InstalledGame& left, const InstalledGame& right) { return left.id < right.id; });
+ // "<app id>\t<finish time>" per finished download the player took off Completed.
+ if (FILE* file = std::fopen((primary + "/" + kDismissed).c_str(), "r")) {
+ char line[64];
+ while (std::fgets(line, sizeof(line), file) && dismissed_.size() < kMaxJobs * 4) {
+ GameId id = 0;
+ long long finished = 0;
+ if (std::sscanf(line, "%u\t%lld", &id, &finished) == 2 && id != 0 && finished > 0)
+ dismissed_.emplace_back(id, static_cast<std::int64_t>(finished));
+ }
+ std::fclose(file);
+ }
++revision_;
}
@@ -240,6 +280,46 @@ void InstallService::SaveDefaultVolume() {
if (std::fclose(file) == 0) std::rename(temporary.c_str(), path.c_str());
}
+void InstallService::SaveDismissed() {
+ // Only dismissals that still hide an install are kept.
+ dismissed_.erase(std::remove_if(dismissed_.begin(), dismissed_.end(), [this](const std::pair<GameId, std::int64_t>& entry) {
+ return std::none_of(installed_.begin(), installed_.end(), [&entry](const InstalledGame& game) {
+ return game.id == entry.first && game.finished_at == entry.second;
+ });
+ }), dismissed_.end());
+ const auto& primary = volumes_.front().games_root;
+ mkdir(primary.c_str(), 0777);
+ const auto path = primary + "/" + kDismissed;
+ const auto temporary = path + ".tmp";
+ FILE* file = std::fopen(temporary.c_str(), "w");
+ if (!file) return;
+ for (const auto& [id, finished] : dismissed_)
+ std::fprintf(file, "%u\t%lld\n", id, static_cast<long long>(finished));
+ if (std::fclose(file) == 0) std::rename(temporary.c_str(), path.c_str());
+}
+
+void InstallService::Dismiss(GameId id) {
+ for (const auto& game : installed_) {
+ if (game.id != id || game.finished_at <= 0) continue;
+ const std::pair<GameId, std::int64_t> entry{id, game.finished_at};
+ if (std::find(dismissed_.begin(), dismissed_.end(), entry) == dismissed_.end()) dismissed_.push_back(entry);
+ }
+}
+
+void InstallService::DismissCompleted(GameId id) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ Dismiss(id);
+ SaveDismissed();
+ ++revision_;
+}
+
+void InstallService::ClearCompleted() {
+ std::lock_guard<std::mutex> lock(mutex_);
+ for (const auto& completed : CompletedDownloads(installed_, dismissed_, WallNow())) Dismiss(completed.id);
+ SaveDismissed();
+ ++revision_;
+}
+
InstallService::Job* InstallService::Find(GameId id) {
for (auto& job : jobs_)
if (job.view.id == id) return &job;
@@ -407,6 +487,7 @@ InstallSnapshot InstallService::Snapshot() {
snapshot.revision = revision_;
for (const auto& job : jobs_) snapshot.jobs.push_back(job.view);
snapshot.installed = installed_;
+ snapshot.completed = CompletedDownloads(installed_, dismissed_, WallNow());
snapshot.volumes = volumes_;
snapshot.default_volume_id = default_volume_id_;
return snapshot;
@@ -510,7 +591,7 @@ void InstallService::Run() {
const auto volume_id = job->view.volume_id;
jobs_.erase(std::remove_if(jobs_.begin(), jobs_.end(), [&](const Job& entry) { return entry.view.id == *next; }),
jobs_.end());
- installed_.push_back({*next, result.install_bytes, next_root, volume_id, true});
+ installed_.push_back({*next, result.install_bytes, next_root, volume_id, true, WallNow()});
} 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;
diff --git a/src/steam/install_service.h b/src/steam/install_service.h
index 4949ecd..8f14d08 100644
--- a/src/steam/install_service.h
+++ b/src/steam/install_service.h
@@ -14,6 +14,7 @@
#include <optional>
#include <string>
#include <thread>
+#include <utility>
#include <vector>
namespace playden::steam {
@@ -55,12 +56,31 @@ struct InstalledGame {
std::string root;
std::string volume_id;
bool available = true; // false while the game's volume is absent
+ std::int64_t finished_at = 0; // Unix seconds the install finished (its record's time); 0 unknown
};
+// A recent finished download that Downloads lists after the queue until the player dismisses it.
+struct CompletedDownload {
+ GameId id = 0;
+ std::uint64_t install_bytes = 0;
+ std::string volume_id;
+ std::int64_t finished_at = 0;
+};
+
+// Installs that finished at most `kCompletedDays` before `now` and were not dismissed (a dismissal
+// names the finish time, so installing the game again lists it again), newest first, at most
+// `kMaxCompleted`.
+inline constexpr std::int64_t kCompletedDays = 7;
+inline constexpr std::size_t kMaxCompleted = 20;
+std::vector<CompletedDownload> CompletedDownloads(const std::vector<InstalledGame>& installed,
+ const std::vector<std::pair<GameId, std::int64_t>>& dismissed,
+ std::int64_t now);
+
struct InstallSnapshot {
std::uint64_t revision = 0;
std::vector<InstallJobView> jobs;
std::vector<InstalledGame> installed;
+ std::vector<CompletedDownload> completed;
std::vector<InstallVolume> volumes;
std::string default_volume_id;
};
@@ -114,6 +134,9 @@ class InstallService {
void Uninstall(GameId id);
// Chooses the volume for installs queued from now on; ignored unless it names an available volume.
void SetDefaultVolume(const std::string& volume_id);
+ // Takes a finished download, or every one, off the Completed list in Downloads.
+ void DismissCompleted(GameId id);
+ void ClearCompleted();
InstallSnapshot Snapshot();
void Shutdown();
@@ -135,6 +158,8 @@ class InstallService {
void Publish();
void SaveRegistry();
void SaveDefaultVolume();
+ void SaveDismissed();
+ void Dismiss(GameId id);
const InstallVolume* FindVolume(const std::string& id) const;
std::string RootOn(const std::string& volume_id, GameId id) const;
std::string LockedRootFor(GameId id) const;
@@ -147,6 +172,7 @@ class InstallService {
std::condition_variable wake_;
std::deque<Job> jobs_;
std::vector<InstalledGame> installed_;
+ std::vector<std::pair<GameId, std::int64_t>> dismissed_; // finished downloads taken off Completed
std::deque<std::string> removals_; // folders to delete
std::optional<GameId> active_;
std::atomic<bool> active_cancel_{false};
diff --git a/tests/homebrew_library_model_test.cpp b/tests/homebrew_library_model_test.cpp
index 592e0e2..cc5f4cc 100644
--- a/tests/homebrew_library_model_test.cpp
+++ b/tests/homebrew_library_model_test.cpp
@@ -1353,6 +1353,53 @@ void live_saves_tab() {
"More > Manage saves opens the game's saves");
}
+void live_completed_downloads_follow_the_queue() {
+ using namespace playden::homebrew;
+ auto model = make_live();
+ model.SetWallClock(100'000);
+ model.UpdateAccount({AuthState::SignedIn, "123", "Synthetic", {}, {}, true});
+ check(model.UpdateLibrary({{game(42, "Finished"), game(43, "Downloading"), game(45, "Earlier"), game(46, "Removed")},
+ false, {}, false, false, {}}), "library loaded");
+ // 46 is listed by the service but no longer installed; 43 is downloading again.
+ model.UpdateInstalls({{43, JobState::Downloading, 10, 100, {}, "internal"}},
+ {{42, 100, "internal", true}, {43, 50, "internal", true}, {45, 300, "m2", true}},
+ {{42, 100, "internal", 99'880}, {45, 300, "m2", 92'800}, {43, 50, "internal", 99'000}, {46, 9, "internal", 99'990}});
+ const auto& jobs = model.state().jobs;
+ check(jobs.size() == 3 && jobs[0].game_id == 43 && jobs[0].state == JobState::Downloading, "the queue comes first");
+ check(jobs[1].game_id == 42 && jobs[1].state == JobState::Downloaded && jobs[1].finished_at == 99'880 &&
+ jobs[1].completed_bytes == 100 && jobs[1].total_bytes == 100 && jobs[2].game_id == 45 && jobs[2].volume_id == "m2",
+ "finished downloads follow it, newest first, without games downloading again or uninstalled");
+ check(model.state().volumes.empty() || std::all_of(model.state().volumes.begin(), model.state().volumes.end(),
+ [](const Volume& volume) { return volume.reserved_bytes == 0 || volume.id == "internal"; }),
+ "finished downloads reserve no space");
+
+ model.action("r1");
+ model.action("r1");
+ check(model.state().navigation.route == Route::Downloads, "Downloads open");
+ check(has_row(model, "job-43") && has_row(model, "job-42") && has_row(model, "job-45"), "every entry has a row");
+ activate(model, "job-42");
+ check(has_row(model, "job-open-42") && has_row(model, "job-dismiss-42") && has_row(model, "job-clear-completed") &&
+ !has_row(model, "job-cancel-42"), "a finished download offers Open, Dismiss and Clear completed");
+ activate(model, "job-dismiss-42");
+ auto requests = model.TakeInstallRequests();
+ check(requests.size() == 1 && requests[0].kind == InstallRequestKind::DismissCompleted && requests[0].game_id == 42,
+ "Dismiss tells the download service");
+ check(!has_row(model, "job-42") && has_row(model, "job-45"), "the dismissed entry goes at once");
+
+ activate(model, "job-45");
+ check(!has_row(model, "job-clear-completed"), "one finished download needs no Clear completed");
+ model.action("circle");
+ model.UpdateInstalls({}, {{42, 100, "internal", true}, {43, 50, "internal", true}, {45, 300, "m2", true}},
+ {{43, 50, "internal", 99'950}, {45, 300, "m2", 92'800}});
+ activate(model, "job-43");
+ activate(model, "job-clear-completed");
+ requests = model.TakeInstallRequests();
+ check(requests.size() == 1 && requests[0].kind == InstallRequestKind::ClearCompleted, "Clear completed tells the service");
+ check(std::none_of(model.state().jobs.begin(), model.state().jobs.end(),
+ [](const Job& job) { return job.state == JobState::Downloaded; }) && has_row(model, "downloads-empty"),
+ "Clear completed empties the list");
+}
+
void live_download_reorder_reaches_service() {
using namespace playden::homebrew;
auto model = make_live();
@@ -1387,6 +1434,7 @@ int main() {
live_uninstall_removes_title_and_keeps_saves();
live_install_confirmation_checks_space_and_unknowns();
live_install_notifications();
+ live_completed_downloads_follow_the_queue();
live_log_viewer();
live_cloud_saves();
live_saves_tab();
diff --git a/tests/install_service_test.cpp b/tests/install_service_test.cpp
index c58a5d9..b6754af 100644
--- a/tests/install_service_test.cpp
+++ b/tests/install_service_test.cpp
@@ -200,9 +200,70 @@ void LiveTelemetryDecaysWithoutCallbacks(const std::filesystem::path& root) {
} // namespace
+// Finished downloads stay listed after the queue for a week, newest first, until dismissed.
+void CompletedDownloadsList(const std::filesystem::path& root) {
+ constexpr std::int64_t kDay = 24 * 60 * 60;
+ const std::int64_t now = 1'800'000'000;
+ std::vector<InstalledGame> installed = {
+ {1, 10, "/g/1", "internal", true, now - 60},
+ {2, 20, "/g/2", "m2", true, now - 3 * kDay},
+ {3, 30, "/g/3", "internal", true, now - 8 * kDay}, // older than a week
+ {4, 40, "/g/4", "internal", true, 0}, // unknown finish time
+ {5, 50, "/g/5", "internal", true, now - 10},
+ };
+ auto completed = CompletedDownloads(installed, {}, now);
+ CHECK(completed.size() == 3 && completed[0].id == 5 && completed[1].id == 1 && completed[2].id == 2);
+ CHECK(completed[2].volume_id == "m2" && completed[2].install_bytes == 20 && completed[2].finished_at == now - 3 * kDay);
+ // A dismissal names the finish time: the same game installed again is listed again.
+ completed = CompletedDownloads(installed, {{5, now - 10}, {1, now - 999}}, now);
+ CHECK(completed.size() == 2 && completed[0].id == 1 && completed[1].id == 2);
+ std::vector<InstalledGame> many;
+ for (GameId id = 1; id <= kMaxCompleted + 5; ++id) many.push_back({id, 1, "/g", "internal", true, now - id});
+ completed = CompletedDownloads(many, {}, now);
+ CHECK(completed.size() == kMaxCompleted && completed.front().id == 1 && completed.back().id == kMaxCompleted);
+
+ // The service lists an install as it finishes, keeps dismissals across a restart, and Clear
+ // dismisses everything listed.
+ std::filesystem::remove_all(root);
+ Sessions sessions;
+ {
+ Runner runner;
+ InstallService service(runner, sessions, root.string());
+ service.Install(70);
+ service.Install(71);
+ AwaitAt(__LINE__, service, [](const InstallSnapshot& s) { return Installed(s, 70) && Installed(s, 71); });
+ const auto snapshot = service.Snapshot();
+ CHECK(snapshot.completed.size() == 2 && snapshot.completed[0].install_bytes == 4096);
+ const auto revision = snapshot.revision;
+ service.DismissCompleted(70);
+ const auto dismissed = service.Snapshot();
+ CHECK(dismissed.revision > revision);
+ CHECK(dismissed.completed.size() == 1 && dismissed.completed[0].id == 71);
+ CHECK(Installed(dismissed, 70));
+ }
+ {
+ Runner runner;
+ InstallService restarted(runner, sessions, root.string());
+ const auto snapshot = restarted.Snapshot();
+ CHECK(snapshot.completed.size() == 1 && snapshot.completed[0].id == 71 && snapshot.completed[0].finished_at > 0);
+ restarted.ClearCompleted();
+ CHECK(restarted.Snapshot().completed.empty());
+ // Uninstalling drops the game's dismissal with it.
+ restarted.Uninstall(70);
+ restarted.DismissCompleted(71);
+ }
+ {
+ std::ifstream dismissed(root / ".playden-downloads-dismissed");
+ const std::string text((std::istreambuf_iterator<char>(dismissed)), std::istreambuf_iterator<char>());
+ CHECK(text.find("71\t") == 0 && text.find("70\t") == std::string::npos);
+ }
+ 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())));
+ 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);
CHECK(requests[0].action == InstallRequest::Action::kUninstall && requests[0].app == 439490);