Play your Steam library's Windows games on a jailbroken PS5.
C++
Show move checks in the Queue and resume at the switch
Leonardo Lobatof462416cd07b
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cppindex fd1e6db..656c01c 100644--- a/src/homebrew/controller.cpp+++ b/src/homebrew/controller.cpp@@ -2132,6 +2132,8 @@ void Controller::TickInstalls() {: job.step == steam::InstallStep::kSwitching ? Step::Switching: job.step == steam::InstallStep::kDeletingOld ? Step::DeletingOld: Step::Copying;+ live.checked_files = job.checked_files;+ live.check_files = job.check_files;}jobs.push_back(std::move(live));}diff --git a/src/homebrew/simulation.cpp b/src/homebrew/simulation.cppindex 7e4a253..766908a 100644--- a/src/homebrew/simulation.cpp+++ b/src/homebrew/simulation.cpp@@ -177,6 +177,13 @@ AuthState parse_auth_state(const std::string& state) {return AuthState::SignedOut;}+// "61,384": a count with its thousands grouped.+std::string grouped_count(std::uint64_t count) {+ auto digits = std::to_string(count);+ for (auto at = digits.size(); at > 3; at -= 3) digits.insert(at - 3, ",");+ return digits;+}+std::string format_bytes(std::uint64_t bytes) {if (bytes == 0) return {};char text[32];@@ -2853,6 +2860,14 @@ void Simulation::UpdateInstalls(const std::vector<LiveInstallJob>& jobs, const scase Step::Switching: job.state_text = "Switching to the copy"; break;case Step::DeletingOld: job.state_text = "Deleting the old copy"; break;}+ // A check of a game with many files takes minutes: how far it got. The switch checks the copy+ // again before the title follows it, and says so while it does.+ const bool counting = live_job.check_files > 0 &&+ (live_job.move_step == Step::Verifying ||+ (live_job.move_step == Step::Switching && live_job.checked_files < live_job.check_files));+ if (counting)+ job.state_text = "Checking the copy · " + grouped_count(live_job.checked_files) + " of " ++ grouped_count(live_job.check_files) + " files";}}projected.push_back(std::move(job));diff --git a/src/homebrew/simulation.hpp b/src/homebrew/simulation.hppindex 8d8c81c..6f12a7f 100644--- a/src/homebrew/simulation.hpp+++ b/src/homebrew/simulation.hpp@@ -125,6 +125,9 @@ struct LiveInstallJob {bool move = false;std::string from_volume_id;enum class MoveStep { Copying, Verifying, Switching, DeletingOld } move_step = MoveStep::Copying;+ // While the move checks its copy: the files checked so far, of how many (0 of 0: no count).+ std::uint64_t checked_files = 0;+ std::uint64_t check_files = 0;};struct LiveInstalledGame {GameId game_id;diff --git a/src/installs/game_move.cpp b/src/installs/game_move.cppindex 7d346df..3a443bb 100644--- a/src/installs/game_move.cpp+++ b/src/installs/game_move.cpp@@ -12,6 +12,7 @@#include <array>#include <tuple>#include <cerrno>+#include <chrono>#include <cstdio>#include <dirent.h>#include <fcntl.h>@@ -282,7 +283,9 @@ struct Comparison {std::vector<std::string> gone; // planned files the source no longer hasbool changed = false; // `fresh` is not the plan saved};-Comparison Compare(const CopyPlan& old, const CopyPlan& fresh, const std::string& to) {+// `checked` hears each file of `fresh` compared (how many, of how many).+Comparison Compare(const CopyPlan& old, const CopyPlan& fresh, const std::string& to,+ const std::function<void(std::uint64_t, std::uint64_t)>& checked = {}) {Comparison result;std::unordered_map<std::string, const CopyFile*> planned;for (const auto& file : old.files) planned.emplace(file.path, &file);@@ -298,6 +301,7 @@ Comparison Compare(const CopyPlan& old, const CopyPlan& fresh, const std::stringresult.done[index] = 1;result.done_bytes += file.bytes;}+ if (checked) checked(index + 1, fresh.files.size());}std::unordered_set<std::string> present;for (const auto& file : fresh.files) present.insert(file.path);@@ -362,7 +366,8 @@ bool GameMover::Reached(MoveCheckpoint checkpoint) const {InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from, const std::string& to,const std::string& volume_id, const std::atomic<bool>& cancel,const std::function<void(std::uint64_t, std::uint64_t, std::uint64_t)>& progress,- const std::function<void(InstallStep)>& step) {+ const std::function<void(InstallStep)>& step,+ const std::function<void(std::uint64_t, std::uint64_t)>& checked) {InstallRunner::Result result;const auto key = ToKey(id);const auto fail = [&](const std::string& failure, const std::string& why) {@@ -411,13 +416,39 @@ InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from,return fail("A folder on the destination is in the way of the move.", to + " has a record, plan source " + saved->source);return fail("The move's copy is of another folder. Cancel it, then move the game again.", "plan source " + saved->source);}- // The source as it is now: a resume keeps what is copied of files the game hasn't changed since.- auto build = stores::local::BuildCopyPlan(from);- if (!build.ok) return fail(build.failure, "plan errno=" + std::to_string(build.error));- build.plan.source = from;- CopyPlan plan = std::move(build.plan);+ // Stopped at the switch (the copy was whole and checked when its marker was written): the title may already+ // play from the copy, and if it doesn't, the switch checks the copy against the game again with the title+ // stopped. Walking the game and comparing it first would only do that twice. The copy is still checked+ // (every planned file at its size): one that lost files since goes the whole way again.+ bool at_switch = switching && saved && saved->source == from;+ if (at_switch) {+ if (step) step(InstallStep::kVerifying);+ for (std::size_t index = 0; index < saved->files.size(); ++index) {+ const auto& file = saved->files[index];+ const auto size = FileSize(to + "/" + file.path);+ if (!size || *size != file.bytes) {+ Log("move copy changed at the switch game=" + key + " path=" + file.path);+ at_switch = false;+ break;+ }+ if (checked) checked(index + 1, saved->files.size());+ }+ }+ CopyPlan plan;Comparison state;- if (!saved) {+ if (at_switch) {+ plan = *saved;+ state.done.assign(plan.files.size(), 1);+ state.done_bytes = plan.total;+ Log("move resumes at the switch game=" + key + " to=" + to);+ } else {+ // The source as it is now: a resume keeps what is copied of files the game hasn't changed since.+ auto build = stores::local::BuildCopyPlan(from);+ if (!build.ok) return fail(build.failure, "plan errno=" + std::to_string(build.error));+ build.plan.source = from;+ plan = std::move(build.plan);+ }+ if (!at_switch && !saved) {if (const auto refused = room_for(plan.total, Parent(to)); !refused.empty())return fail(refused, "space for " + std::to_string(plan.total) + " bytes");Destination made(from, to, options_.hold_effective_uid);@@ -430,7 +461,7 @@ InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from,state.done.assign(plan.files.size(), 0);Log("move planned game=" + key + " from=" + from + " to=" + to + " files=" + std::to_string(plan.files.size()) +" bytes=" + std::to_string(plan.total));- } else {+ } else if (!at_switch) {state = Compare(*saved, plan, to);if (state.changed) {Destination stale(from, to, options_.hold_effective_uid);@@ -440,7 +471,7 @@ InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from,}}if (progress) progress(state.done_bytes, plan.total, state.done_bytes);- if (const auto refused = room_for(plan.total - state.done_bytes, to); !refused.empty())+ if (const auto refused = at_switch ? std::string() : room_for(plan.total - state.done_bytes, to); !refused.empty())return fail(refused, "space for " + std::to_string(plan.total - state.done_bytes) + " bytes");Destination destination(from, to, options_.hold_effective_uid);@@ -453,9 +484,9 @@ InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from,if (!destination.Folder(dir)) return false;return true;};- if (!prepare()) return fail("Could not write the game files.", "folders errno=" + std::to_string(errno));+ if (!at_switch && !prepare()) return fail("Could not write the game files.", "folders errno=" + std::to_string(errno));- if (step) step(InstallStep::kCopying);+ if (step && !at_switch) step(InstallStep::kCopying);std::vector<char> buffer(options_.buffer_bytes);SourceWalk walk(from);std::uint64_t copied = state.done_bytes;@@ -509,10 +540,14 @@ InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from,}}- if (step) step(InstallStep::kVerifying);- for (const auto& file : plan.files) {- const auto size = FileSize(to + "/" + file.path);- if (!size || *size != file.bytes) return fail("Some copied files are missing. Resume to copy them again.", "verify " + file.path);+ if (!at_switch) {+ if (step) step(InstallStep::kVerifying);+ for (std::size_t index = 0; index < plan.files.size(); ++index) {+ const auto& file = plan.files[index];+ const auto size = FileSize(to + "/" + file.path);+ if (!size || *size != file.bytes) return fail("Some copied files are missing. Resume to copy them again.", "verify " + file.path);+ if (checked) checked(index + 1, plan.files.size());+ }}if (!Reached(MoveCheckpoint::kCopied)) {result.canceled = true;@@ -545,13 +580,19 @@ InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from,// With the title stopped: files the game changed or added since they were copied are copied again,// files it deleted go from the copy, then the file list and the install record (last) follow.relocation.move_files = [&]() -> std::string {+ const auto started = std::chrono::steady_clock::now();+ const auto ms = [&] {+ return std::to_string(+ std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - started).count());+ };auto rebuilt = stores::local::BuildCopyPlan(from);if (!rebuilt.ok) {Log("move recheck failed game=" + key + " errno=" + std::to_string(rebuilt.error) + ": " + rebuilt.failure);return rebuilt.failure;}rebuilt.plan.source = from;- const auto now = Compare(plan, rebuilt.plan, to);+ Log("move rechecks game=" + key + " files=" + std::to_string(rebuilt.plan.files.size()) + " walk_ms=" + ms());+ const auto now = Compare(plan, rebuilt.plan, to, checked);if (now.changed) {if (!RemoveStale(destination, rebuilt.plan, now, to)) return "Could not write the game files. Check the drive, then Resume.";if (const auto written = save_plan(rebuilt.plan); !written.empty()) return written;@@ -585,6 +626,7 @@ InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from,bool synced = SyncFolder(to);for (const auto& dir : plan.dirs) synced = SyncFolder(to + "/" + dir) && synced;if (!synced) return "The move could not be saved on the storage. Resume to try again.";+ Log("move rechecked game=" + key + " ms=" + ms());return {};};GameRelocationResult relocated;@@ -607,6 +649,9 @@ InstallRunner::Result GameMover::Copy(const GameId& id, const std::string& from,return fail(relocated.message.empty() ? "The game's PS5 title could not follow the move." : relocated.message,"relocation " + relocated.message);}+ // The copy's record goes last: without it the copy isn't whole, and the old folder must stay.+ if (Missing(to + "/" + kInstallRecordName))+ return fail("The game's install record is missing from the copy. Check the drive, then Resume.", "no record in the copy");Log("move switched game=" + key + " to=" + to + (relocated.message.empty() ? "" : " note=" + relocated.message));if (!Reached(MoveCheckpoint::kSwitched)) {result.canceled = true;diff --git a/src/installs/game_move.h b/src/installs/game_move.hindex c55b6f7..e234af5 100644--- a/src/installs/game_move.h+++ b/src/installs/game_move.h@@ -85,7 +85,8 @@ class GameMover final : public MoveRunner {InstallRunner::Result Copy(const GameId& id, const std::string& from, const std::string& to, const std::string& volume_id,const std::atomic<bool>& cancel,const std::function<void(std::uint64_t done, std::uint64_t total, std::uint64_t held)>& progress,- const std::function<void(InstallStep)>& step) override;+ const std::function<void(InstallStep)>& step,+ const std::function<void(std::uint64_t done, std::uint64_t total)>& checked) override;std::string Finish(const std::string& from, const std::string& to) override;std::string Discard(const std::string& from, const std::string& to) override;diff --git a/src/installs/install_service.cpp b/src/installs/install_service.cppindex d905660..e1a95f8 100644--- a/src/installs/install_service.cpp+++ b/src/installs/install_service.cpp@@ -1441,13 +1441,26 @@ void InstallService::RunMove(GameId id, const std::string& from, const std::striif (!job || job->view.step == step) return;job->view.step = step;job->view.step_since = std::chrono::steady_clock::now();+ job->view.checked_files = job->view.check_files = 0;// From the switch on the title may name the copy: the move can't stop half way.if (step == InstallStep::kSwitching && !job->view.locked) job->view.locked = true;++revision_;};+ // A check of tens of thousands of files reports each one: the view changes once a percent, and at the end.+ const auto checked = [this, id](std::uint64_t done, std::uint64_t total) {+ std::lock_guard<std::mutex> lock(mutex_);+ auto* job = Find(id);+ if (!job) return;+ const auto percent = [](std::uint64_t value, std::uint64_t of) { return of == 0 ? 0 : value * 100 / of; };+ const bool changed = total != job->view.check_files || done == total ||+ percent(done, total) != percent(job->view.checked_files, job->view.check_files);+ job->view.checked_files = done;+ job->view.check_files = total;+ if (changed) ++revision_;+ };InstallRunner::Result result;if (finishing) result.ok = true;- else result = mover->Copy(id, from, to, volume_id, active_cancel_, progress, step);+ else result = mover->Copy(id, from, to, volume_id, active_cancel_, progress, step, checked);bool discard = false, finish = false;{diff --git a/src/installs/install_service.h b/src/installs/install_service.hindex bf351f5..0f1dc43 100644--- a/src/installs/install_service.h+++ b/src/installs/install_service.h@@ -103,6 +103,10 @@ struct InstallJobView {std::string server_name; // while running: where the download comes from, a display name (never a host)InstallStep step = InstallStep::kStarting; // while runningstd::chrono::steady_clock::time_point step_since{};+ // A move checking its copy (kVerifying, or kSwitching while the title's files are checked again): the files+ // checked so far, of how many. 0 total: the step counts nothing.+ std::uint64_t checked_files = 0;+ std::uint64_t check_files = 0;};struct InstalledGame {@@ -215,11 +219,13 @@ class MoveRunner {// has the game's PS5 title follow it. ok: the game plays from `to` (install_bytes from its record); the// folder at `from` is still there. Called again after any interruption, it carries on from what is// copied. Reports kCopying, kVerifying and kSwitching; a stop or failure before the title follows- // leaves the game playing from `from`. retry_later: the title runs or the runtime is busy.+ // leaves the game playing from `from`. retry_later: the title runs or the runtime is busy. `checked` counts+ // the files a check has gone through, of how many, from the start of each check.virtual InstallRunner::Result Copy(const GameId& id, const std::string& from, const std::string& to,const std::string& volume_id, const std::atomic<bool>& cancel,const std::function<void(std::uint64_t done, std::uint64_t total, std::uint64_t held)>& progress,- const std::function<void(InstallStep)>& step) = 0;+ const std::function<void(InstallStep)>& step,+ const std::function<void(std::uint64_t done, std::uint64_t total)>& checked) = 0;// Once the service lists the game at `to`: deletes the old folder at `from`, then the copy's journal.// Empty, or why not (a short sentence). Safe to run again.virtual std::string Finish(const std::string& from, const std::string& to) = 0;diff --git a/tests/game_move_test.cpp b/tests/game_move_test.cppindex a2bd4be..519c4e2 100644--- a/tests/game_move_test.cpp+++ b/tests/game_move_test.cpp@@ -199,7 +199,7 @@ struct Fixture {// what it said then.InstallRunner::Result CopyUntil(const std::atomic<bool>& cancel) {GameMover direct(options);- return direct.Copy(kGame, (internal / "730").string(), (usb / "730").string(), "usb0", cancel, {}, {});+ return direct.Copy(kGame, (internal / "730").string(), (usb / "730").string(), "usb0", cancel, {}, {}, {});}void RegistryMoving() {Write(internal / ".playden-games", "730\tinternal\t" + std::to_string(game_bytes) + "\tmove=usb0\tfrom=internal\n");@@ -452,6 +452,65 @@ void ResumesAfterEachStep() {}}+// The checks count their files: every copied file before the switch, and again with the title stopped.+void ChecksCountTheirFiles() {+ Fixture f;+ std::vector<std::pair<std::uint64_t, std::uint64_t>> counts;+ std::vector<InstallStep> steps;+ GameMover mover(f.options);+ const std::atomic<bool> never{false};+ const auto result = mover.Copy(kGame, (f.internal / "730").string(), (f.usb / "730").string(), "usb0", never, {},+ [&](InstallStep step) { steps.push_back(step); },+ [&](std::uint64_t done, std::uint64_t total) { counts.emplace_back(done, total); });+ CHECK(result.ok);+ CHECK((steps == std::vector<InstallStep>{InstallStep::kCopying, InstallStep::kVerifying, InstallStep::kSwitching}));+ // Six game files, checked once while verifying and once more at the switch.+ using Count = std::pair<std::uint64_t, std::uint64_t>;+ CHECK(counts.size() == 12 && counts[0] == Count(1, 6) && counts[5] == Count(6, 6) && counts[11] == counts[5]);+ CHECK(f.Logged("move rechecks game=730 files=6") && f.Logged("move rechecked game=730"));+}++// Stopped once the title followed the copy (Playden quit before the service knew): the move goes straight+// back to the switch, without walking or checking the files again, and finishes.+void ResumesAtTheSwitchWithoutCheckingAgain() {+ Fixture f;+ f.options.checkpoint = [](MoveCheckpoint reached) { return reached != MoveCheckpoint::kSwitched; };+ const std::atomic<bool> never{false};+ CHECK(f.CopyUntil(never).canceled);+ CHECK(f.Relocations() == 1);+ f.options.checkpoint = {};+ // As RuntimeHost::Relocate for a title that already names the copy: nothing to move.+ f.options.relocate = [&](const GameRelocation& relocation) {+ std::lock_guard<std::mutex> lock(f.mutex);+ f.relocated.push_back(relocation.to);+ return GameRelocationResult{};+ };+ // A file the game would have rewritten meanwhile is the title's business now: the copy is what it plays.+ Write(f.internal / "730/readme.txt", "changed!");+ std::vector<InstallStep> steps;+ std::size_t counted = 0;+ GameMover mover(f.options);+ f.files = 0;+ const auto result = mover.Copy(kGame, (f.internal / "730").string(), (f.usb / "730").string(), "usb0", never, {},+ [&](InstallStep step) { steps.push_back(step); },+ [&](std::uint64_t, std::uint64_t) { ++counted; });+ CHECK(result.ok && result.install_bytes == f.game_bytes);+ // Only the copy is checked, by size: no file is copied.+ CHECK((steps == std::vector<InstallStep>{InstallStep::kVerifying, InstallStep::kSwitching}) && counted == 6 && f.files == 0);+ CHECK(f.Relocations() == 2 && f.Logged("move resumes at the switch game=730"));+ CHECK(Read(f.usb / "730/readme.txt") == "read me");+ // A copy that lost a file since goes the whole way again: the file comes back before the old folder may go.+ fs::remove(f.usb / "730/data/level1.pak");+ f.files = 0;+ CHECK(mover.Copy(kGame, (f.internal / "730").string(), (f.usb / "730").string(), "usb0", never, {}, {}, {}).ok);+ CHECK(f.Logged("move copy changed at the switch game=730 path=data/level1.pak"));+ CHECK(fs::file_size(f.usb / "730/data/level1.pak") == 300000 && f.files >= 1);+ // Without the copy's record the copy isn't whole: the move doesn't finish.+ fs::remove(f.usb / "730" / kInstallRecordName);+ const auto refused = mover.Copy(kGame, (f.internal / "730").string(), (f.usb / "730").string(), "usb0", never, {}, {}, {});+ CHECK(!refused.ok && refused.failure.find("install record is missing") != std::string::npos);+}+void FinishesDeletingTheOldFolder() {Fixture f;// Stopped after the copy was listed and the old folder deleted, before its journal went.@@ -713,6 +772,8 @@ int main() {AFileRewrittenAtItsSizeIsCopiedAgain();AFileDeletedWhilePausedNeverComesBack();ResumesAfterEachStep();+ ChecksCountTheirFiles();+ ResumesAtTheSwitchWithoutCheckingAgain();FinishesDeletingTheOldFolder();OldFolderLeftAfterTheSwitchIsDeletedAtStart();TheOldFolderStaysUntilTheRegistryIsSaved();diff --git a/tests/homebrew_library_model_test.cpp b/tests/homebrew_library_model_test.cppindex 45c9108..ed28206 100644--- a/tests/homebrew_library_model_test.cpp+++ b/tests/homebrew_library_model_test.cpp@@ -2590,6 +2590,26 @@ void live_move_job_actions_say_move() {model.UpdateInstalls({job}, {{SteamId(42), 100, "usb", true}});activate(model, "job-42");check(!find_row(model, "job-pause-42") && !find_row(model, "job-cancel-42"), "move job: a switching move can't be stopped");+ activate(model, "cancel");+ // A check says how far it got; the switch says so while it checks the copy again, then switches.+ const auto state_text = [&model] {+ const auto found = std::find_if(model.state().jobs.begin(), model.state().jobs.end(),+ [](const Job& entry) { return entry.game_id == SteamId(42); });+ return found == model.state().jobs.end() ? std::string() : found->state_text;+ };+ job.move_step = LiveInstallJob::MoveStep::Verifying;+ job.checked_files = 12'345;+ job.check_files = 61'384;+ model.UpdateInstalls({job}, {{SteamId(42), 100, "internal", true}});+ check(state_text() == "Checking the copy · 12,345 of 61,384 files", "move job: the check counts its files");+ job.move_step = LiveInstallJob::MoveStep::Switching;+ job.checked_files = 900;+ job.check_files = 1'000;+ model.UpdateInstalls({job}, {{SteamId(42), 100, "internal", true}});+ check(state_text() == "Checking the copy · 900 of 1,000 files", "move job: the switch's check counts too");+ job.checked_files = 1'000;+ model.UpdateInstalls({job}, {{SteamId(42), 100, "internal", true}});+ check(state_text() == "Switching to the copy", "move job: then it switches");}void live_completed_downloads_follow_the_queue() {