Skip to content

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

C++

Remember finished preparations for the Queue screen

Leonardo Lobato435ccc550bfc
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cpp
index 0e4c862..5bbae90 100644
--- a/src/homebrew/runtime_host.cpp
+++ b/src/homebrew/runtime_host.cpp
@@ -28,6 +28,7 @@
#include <cstring>
#include <ctime>
#include <dirent.h>
+#include <fstream>
#include <fcntl.h>
#include <pthread.h>
#include <sys/stat.h>
@@ -442,6 +443,90 @@ bool RuntimeHost::PrepareNext(std::uint32_t app_id) {
return true;
}
+void RuntimeHost::DismissPrepared(std::uint32_t app_id, std::int64_t finished_at) {
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (stopping_) return;
+ prepared_dismissals_.push_back({app_id, finished_at});
+ }
+ wake_.notify_all();
+}
+
+void RuntimeHost::ClearPrepared() {
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (stopping_) return;
+ prepared_clear_requested_ = true;
+ }
+ wake_.notify_all();
+}
+
+namespace {
+constexpr const char* kPreparedHistory = "prepared-history";
+} // namespace
+
+void RuntimeHost::LoadPreparedHistory() {
+ prepared_history_.clear();
+ 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)});
+}
+
+void RuntimeHost::SavePreparedHistory() {
+ // Only what Queue could still list is kept.
+ const auto now = config_.unix_time();
+ std::erase_if(prepared_history_, [now](const PreparedGame& entry) {
+ return entry.finished_at > now || now - entry.finished_at > kPreparedDays * 24 * 60 * 60;
+ });
+ std::string text;
+ for (const auto& entry : prepared_history_)
+ text += std::to_string(entry.app_id) + "\t" + std::to_string(entry.finished_at) + "\n";
+ const auto path = config_.state_root / kPreparedHistory;
+ const auto temporary = path.string() + ".playden-partial";
+ const int out = open(temporary.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0600);
+ if (out < 0) {
+ Log("prepared history not saved: " + std::string(std::strerror(errno)));
+ return;
+ }
+ std::size_t written = 0;
+ while (written < text.size()) {
+ const auto count = write(out, text.data() + written, text.size() - written);
+ if (count <= 0) break;
+ written += static_cast<std::size_t>(count);
+ }
+ const bool ok = written == text.size() && fsync(out) == 0;
+ if (close(out) != 0 || !ok || rename(temporary.c_str(), path.c_str()) != 0) {
+ unlink(temporary.c_str());
+ Log("prepared history not saved");
+ }
+}
+
+std::vector<PreparedGame> RuntimeHost::ListPrepared(const Composition* c) const {
+ // A game uninstalled, being removed or being prepared again is not listed as Ready.
+ std::set<std::uint32_t> ready;
+ if (c)
+ for (const auto& record : c->repository.Snapshot())
+ if (record.state == rt::PreparationState::kReady && !record.journal) ready.insert(record.app_id);
+ const auto now = config_.unix_time();
+ std::vector<PreparedGame> listed;
+ for (auto it = prepared_history_.rbegin(); it != prepared_history_.rend() && listed.size() < kMaxPrepared; ++it)
+ if (ready.count(it->app_id) && it->finished_at <= now && now - it->finished_at <= kPreparedDays * 24 * 60 * 60)
+ listed.push_back(*it);
+ std::stable_sort(listed.begin(), listed.end(),
+ [](const PreparedGame& left, const PreparedGame& right) { return left.finished_at > right.finished_at; });
+ return listed;
+}
+
+void RuntimeHost::PublishPrepared(const Composition* c) {
+ auto listed = ListPrepared(c);
+ Publish([&](RuntimeSnapshot& s) {
+ if (s.preparing_app) std::erase_if(listed, [&s](const PreparedGame& entry) { return entry.app_id == *s.preparing_app; });
+ if (s.prepared != listed) s.prepared = std::move(listed);
+ });
+}
+
void RuntimeHost::HoldPreparations(bool hold) {
{
std::lock_guard<std::mutex> lock(mutex_);
@@ -886,6 +971,7 @@ void RuntimeHost::PublishInstalls(const Composition& composition, const std::str
installs.push_back(std::move(view));
}
Publish([&](RuntimeSnapshot& s) { s.installs = std::move(installs); });
+ PublishPrepared(&composition);
}
// The complete OWNER protocol for the publish-profile step: persist the exact
@@ -3108,6 +3194,10 @@ void RuntimeHost::Run() {
});
}
}
+ if (private_ready) {
+ LoadPreparedHistory();
+ PublishPrepared(composition.get());
+ }
startup_hold.reset();
for (;;) {
std::vector<std::uint32_t> plays;
@@ -3120,6 +3210,8 @@ void RuntimeHost::Run() {
bool measure = false;
std::vector<std::uint32_t> checks;
bool names_changed = false;
+ std::vector<PreparedGame> dismissals;
+ bool clear_prepared = false;
{
std::unique_lock<std::mutex> lock(mutex_);
// Preparation changes install records that Handoff reads on the
@@ -3130,7 +3222,7 @@ void RuntimeHost::Run() {
cleanup_requested_ || measure_requested_ || !activity_requests_.empty() || title_names_changed_ ||
(!remove_requests_.empty() && !snapshot_.handoff_ready) ||
(!kept_delete_requests_.empty() && !snapshot_.handoff_ready) ||
- !scan_requests_.empty() ||
+ !scan_requests_.empty() || !prepared_dismissals_.empty() || prepared_clear_requested_ ||
(!maintenance_requests_.empty() && !snapshot_.handoff_ready);
};
if (launch_poll_) wake_.wait_for(lock, kLaunchPollInterval, ready);
@@ -3151,8 +3243,17 @@ void RuntimeHost::Run() {
checks.swap(activity_requests_);
names_changed = title_names_changed_;
title_names_changed_ = false;
+ dismissals.swap(prepared_dismissals_);
+ clear_prepared = prepared_clear_requested_;
+ prepared_clear_requested_ = false;
}
const auto batch_hold = hold();
+ if (private_ready && (clear_prepared || !dismissals.empty())) {
+ if (clear_prepared) prepared_history_.clear();
+ for (const auto& dismissal : dismissals) std::erase(prepared_history_, dismissal);
+ SavePreparedHistory();
+ PublishPrepared(composition.get());
+ }
if (names_changed && composition) PublishInstalls(*composition);
for (const auto& [app, install_path] : scans) {
const auto kit = composition && composition->kit
@@ -3226,6 +3327,10 @@ void RuntimeHost::Run() {
PublishKeptBottles(*composition);
}
Log("remove result app=" + std::to_string(app) + " " + (refusal.empty() ? "removed" : refusal));
+ if (refusal.empty() && std::erase_if(prepared_history_, [app](const PreparedGame& entry) { return entry.app_id == app; })) {
+ SavePreparedHistory();
+ PublishPrepared(composition.get());
+ }
ClearWork();
Publish([&](RuntimeSnapshot& s) {
s.removing_app.reset();
@@ -3304,16 +3409,25 @@ void RuntimeHost::Run() {
}
Log("prepare result app=" + std::to_string(request.app_id) + " " +
(!refusal.empty() ? refusal : awaiting ? "awaiting registration" : "ready"));
+ // Only a preparation that ends Ready is listed as done; one still waiting for the PS5 is listed
+ // once a later check finds the title registered.
+ if (composition && refusal.empty() && !awaiting) {
+ prepared_history_.push_back({request.app_id, config_.unix_time()});
+ SavePreparedHistory();
+ }
if (composition) {
const auto leftovers = LeftoverRequests(*composition, false);
Publish([&](RuntimeSnapshot& s) { s.leftover_requests = leftovers; });
}
ClearWork();
+ // Listed as done as it leaves preparing_app (PublishPrepared leaves out the game being prepared).
+ auto prepared = composition ? ListPrepared(composition.get()) : std::vector<PreparedGame>{};
Publish([&](RuntimeSnapshot& s) {
s.preparing_app.reset();
++s.prepares_handled;
if (refusal.empty()) s.refusals.erase(request.app_id);
else s.refusals[request.app_id] = refusal;
+ if (composition) s.prepared = std::move(prepared);
});
}
}
diff --git a/src/homebrew/runtime_host.hpp b/src/homebrew/runtime_host.hpp
index 831338a..c176abe 100644
--- a/src/homebrew/runtime_host.hpp
+++ b/src/homebrew/runtime_host.hpp
@@ -217,6 +217,16 @@ struct RuntimeGameSetup {
};
+// A preparation that ended Ready. Queue lists it after the waiting games until the player dismisses it;
+// a dismissal names the finish time, so preparing the game again lists it again.
+struct PreparedGame {
+ std::uint32_t app_id = 0;
+ std::int64_t finished_at = 0; // Unix seconds
+ bool operator==(const PreparedGame&) const = default;
+};
+inline constexpr std::int64_t kPreparedDays = 7;
+inline constexpr std::size_t kMaxPrepared = 20;
+
struct RuntimeSnapshot {
std::uint64_t revision = 0;
RuntimeHostStatus status = RuntimeHostStatus::kStarting;
@@ -230,6 +240,9 @@ struct RuntimeSnapshot {
std::map<std::uint32_t, std::string> refusals;
std::uint64_t prepares_handled = 0;
std::optional<std::uint32_t> preparing_app; // the game being prepared now
+ // Preparations that ended Ready at most kPreparedDays ago and were not dismissed, newest first, at most
+ // kMaxPrepared; only games whose title is still Ready.
+ std::vector<PreparedGame> prepared;
// Games whose preparation is asked for but not started yet, in the order they will start: each
// waits behind another game's preparation, or for the runtime to finish starting.
std::vector<std::uint32_t> queued_prepares;
@@ -319,6 +332,10 @@ class RuntimeHost {
bool PrepareNext(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,
+ // including those past the kMaxPrepared the snapshot lists.
+ void DismissPrepared(std::uint32_t app_id, std::int64_t finished_at);
+ void ClearPrepared();
// Asks the console whether a game's PS5 title runs (from any origin, Home
// included). A game with no prepared title reports stopped.
void CheckActivity(std::uint32_t app_id);
@@ -438,6 +455,11 @@ class RuntimeHost {
std::string RestoreKeptSaves(Composition& composition, std::uint32_t app_id);
std::string DeleteKept(Composition& composition, std::uint32_t app_id);
void PublishKeptBottles(const Composition& composition);
+ // Finished preparations, in prepared_history_ and the state root's prepared-history file (worker thread).
+ void LoadPreparedHistory();
+ void SavePreparedHistory();
+ std::vector<PreparedGame> ListPrepared(const Composition* composition) const;
+ void PublishPrepared(const Composition* composition);
// With an any-title kit, lists every recorded game title in the helper's allow-list so their
// titles may start; empty on success (or with no such kit), otherwise why not.
std::string SyncTitleAllowList(Composition& composition);
@@ -478,6 +500,9 @@ class RuntimeHost {
bool cleanup_requested_ = false;
bool measure_requested_ = false;
bool prepares_held_ = false;
+ std::vector<PreparedGame> prepared_dismissals_; // guarded by mutex_
+ bool prepared_clear_requested_ = false; // guarded by mutex_
+ std::vector<PreparedGame> prepared_history_; // worker thread only, oldest first
std::map<std::uint32_t, std::string> title_names_; // guarded by mutex_
std::string steam_account_name_; // guarded by mutex_
std::uint64_t steam_id_ = 0; // guarded by mutex_
diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cpp
index 6e56a93..54f10eb 100644
--- a/tests/homebrew_runtime_host_test.cpp
+++ b/tests/homebrew_runtime_host_test.cpp
@@ -514,6 +514,7 @@ void PreparationEndsAwaitingRegistration() {
if (waiting.refusals.count(220)) std::fprintf(stderr, "Prepare refused: %s\n", waiting.refusals.at(220).c_str());
CHECK(!waiting.refusals.count(220) && waiting.installs[0].awaiting_registration);
CHECK(waiting.installs[0].state != PreparationState::kReady && waiting.installs[0].recovery_pending);
+ CHECK(waiting.prepared.empty()); // not done until the PS5 registers it
CHECK(std::chrono::steady_clock::now() - began < std::chrono::seconds(20)); // no polling for the console
const auto submissions = fixture.registry_mutations;
CHECK(submissions > 0 && fixture.submitted && !fixture.registered);
@@ -535,6 +536,11 @@ void PreparationEndsAwaitingRegistration() {
CHECK(!ready.refusals.count(220) && ready.installs[0].state == PreparationState::kReady);
CHECK(!ready.installs[0].awaiting_registration && !ready.installs[0].recovery_pending);
CHECK(fixture.registry_mutations == submissions);
+ // Registered: now it is listed as done, and Clear completed takes it off.
+ const auto listed = WaitFor(host, [](const RuntimeSnapshot& s) { return !s.prepared.empty(); });
+ CHECK(listed.prepared.size() == 1 && listed.prepared[0].app_id == 220);
+ host.ClearPrepared();
+ CHECK(WaitFor(host, [](const RuntimeSnapshot& s) { return s.prepared.empty(); }).prepared.empty());
host.Shutdown();
}
@@ -625,7 +631,35 @@ void PreparesExistingDownloadToReady() {
const auto again = WaitFor(host, [](const RuntimeSnapshot& s) { return s.refusals.count(220) != 0; });
CHECK(again.refusals.at(220) == "This game is already prepared.");
CHECK(fixture.registry_mutations == 2);
+ // Queue lists the finished preparation once; the refused second request adds nothing.
+ CHECK((again.prepared == std::vector<PreparedGame>{{220, 1760000000}}));
host.Shutdown();
+
+ // The list survives a restart. A dismissal names its finish time; another time leaves it listed.
+ {
+ RuntimeHost restarted(config);
+ CHECK(restarted.Start());
+ CHECK((WaitFor(restarted, Started).prepared == std::vector<PreparedGame>{{220, 1760000000}}));
+ restarted.DismissPrepared(220, 1759999999);
+ restarted.DismissPrepared(220, 1760000000);
+ CHECK(WaitFor(restarted, [](const RuntimeSnapshot& s) { return s.prepared.empty(); }).prepared.empty());
+ restarted.Shutdown();
+ }
+ {
+ RuntimeHost restarted(config);
+ CHECK(restarted.Start());
+ CHECK(WaitFor(restarted, Started).prepared.empty());
+ restarted.Shutdown();
+ }
+ // Past kPreparedDays a preparation is no longer listed.
+ Write(fixture.root / "data/runtime-state/prepared-history", "220\t1760000000\n", 0600);
+ config.unix_time = [] { return std::int64_t{1760000000} + kPreparedDays * 24 * 60 * 60 + 1; };
+ {
+ RuntimeHost restarted(config);
+ CHECK(restarted.Start());
+ CHECK(WaitFor(restarted, Started).prepared.empty());
+ restarted.Shutdown();
+ }
}
// A folder made over FTP belongs to root and is 0755, so the app user can't write into it
@@ -2124,7 +2158,7 @@ void RemovesTitleKeepingSaves() {
RuntimeHost host(config);
CHECK(host.Start());
CHECK(WaitFor(host, Started).kit_available);
- prepare(host);
+ CHECK(prepare(host).prepared.size() == 1);
host.SetTitleNames({{220, "Half-Life 2"}});
Write(bottle / save, "player-save", 0600);
for (const auto* part : {"library/prefixes/game/drive_c/users", "library/prefixes/game/drive_c/users/steamuser",
@@ -2146,6 +2180,7 @@ void RemovesTitleKeepingSaves() {
CHECK(removed.removal_failures.at(220).find("has not confirmed") != std::string::npos);
CHECK(removed.installs.size() == 1 && removed.installs[0].state == PreparationState::kRemoving);
CHECK(removed.installs[0].step == "unregister");
+ CHECK(removed.prepared.empty()); // no longer Ready
CHECK(!fs::exists(fixture.root / "data/homebrew/FAKE78001"));
CHECK(fs::is_directory(fixture.root / "data/runtime-state/trash/FAKE78001-00000000-0000-4000-8000-000000000002"));
CHECK(*remover_calls == 1);