Play your Steam library's Windows games on a jailbroken PS5.
C++
Keep settings changed during a game's setup
Leonardo Lobato5853eacf00cc
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cppindex 41859a3..09040c1 100644--- a/src/homebrew/runtime_host.cpp+++ b/src/homebrew/runtime_host.cpp@@ -1544,6 +1544,70 @@ rt::KitCapabilities RuntimeHost::TitleKit(const Composition& c, const rt::Runtim: kit;}+// With no working folder set, the profile runs the program from its own folder+// (prospero_profile.cpp), so that is where dxvk.conf goes.+static std::string WorkingFolder(const std::string& working, std::string program, bool in_install_root) {+ if (!working.empty() && working != ".") return working;+ if (in_install_root) return {};+ std::replace(program.begin(), program.end(), '\\', '/');+ const auto slash = program.rfind('/');+ return slash == std::string::npos ? std::string() : program.substr(0, slash);+}++// next starts as the install record: it comes back naming the program and folder the settings choose,+// checked on disk; the recipe stays the one the game was prepared with. Empty, or why not (for the player).+std::string RuntimeHost::FollowChosenLaunch(const rt::GameRecipe& recipe, const rt::SettingsPreset* preset,+ const rt::KitCapabilities& kit, const rt::GameSettingsRecord& saved,+ rt::RuntimeInstall* next) {+ const auto record = *next;+ rt::EffectiveSettings desired;+ rt::SettingsError ignored;+ const auto same = [](std::string left, std::string right) {+ std::replace(left.begin(), left.end(), '\\', '/');+ std::replace(right.begin(), right.end(), '\\', '/');+ return (left == "." ? std::string() : left) == (right == "." ? std::string() : right);+ };+ if (rt::BuildEffectiveSettings(recipe, preset, saved.overrides, kit, &desired, &ignored) && desired.launch) {+ const auto& launch = *desired.launch;+ if (launch.id != record.launch_entry_id || !same(launch.executable, record.launch_path) ||+ !same(launch.working_directory, record.launch_working_directory) ||+ launch.in_install_root != record.launch_in_install_root) {+ const auto root = HostPath(record.install_path).string();+ const auto architecture = rt::GameProgramArchitecture(root, launch.executable);+ if (architecture.empty())+ return "The program " + launch.executable + " chosen in Game settings is missing or not a Windows program.";+ if (const auto refusal = rt::GameProgramRefusal(root, launch.executable, kit, rt::RecipeChoicesFrom(saved).try_anyway);+ !refusal.empty())+ return refusal;+ // Checked here, before a publication starts: one that fails part way leaves the title+ // waiting for recovery.+ const auto folder = WorkingFolder(launch.working_directory, launch.executable, launch.in_install_root);+ struct stat folder_info{};+ if (!folder.empty() && (lstat((HostPath(record.install_path) / folder).c_str(), &folder_info) != 0 ||+ !S_ISDIR(folder_info.st_mode)))+ return "The program folder " + folder + " chosen in Game settings is missing.";+ // A kit that admits unowned game folders gets them as they are (mods, FTP edits).+ const auto records_ownership = kit.game_root_unowned ? std::optional<bool>(false)+ : platform::RecordsOwnership(HostPath(record.install_path));+ if (records_ownership && *records_ownership)+ if (const auto refused = TightenWorkingFolder(HostPath(record.install_path),+ WorkingFolder(launch.working_directory, launch.executable,+ launch.in_install_root));+ !refused.empty())+ return refused;+ next->launch_entry_id = launch.id;+ next->launch_path = launch.executable;+ next->launch_working_directory = launch.working_directory == "." ? std::string() : launch.working_directory;+ next->launch_in_install_root = launch.in_install_root && next->launch_working_directory.empty();+ // The recipe stays the one the game was prepared with: its graphics and engine arguments+ // came from the first program's files, and only a preparation sees Steam's launch options.+ Log("program switch app=" + std::to_string(record.runtime_number) + " from=" + record.launch_entry_id + ":" ++ record.launch_path + " to=" + next->launch_entry_id + ":" + next->launch_path + " recipe kept=" + record.recipe_id);+ }+ }+ return {};+}+// The complete OWNER protocol for the publish-profile step: persist the exact// requested plan, arm OWNER, then capture, publish, register and commit Ready.// Recovery journals resume their saved request or frozen operation; nothing is@@ -1581,20 +1645,45 @@ rt::PackageActionResult RuntimeHost::DrivePublication(Composition& c, const rt::input.runtime_dir = config_.runtime_dir;rt::SettingsLaunchPlan plan;std::string reason;- if (input.record.revision != record.settings_revision)- return failed("settings-changed", "The game's settings changed during preparation; prepare it again.");+ if (input.record.revision < record.settings_revision)+ return failed("settings-older", std::string("The game's saved settings are older than the ones its setup started with.") + kRecreateHint);+ // Settings saved since the title was reserved: nothing is published yet, so this publication takes+ // them up, the program they choose included. The title and the bottle stay, as for a Ready game.+ auto current = record;+ if (input.record.revision > record.settings_revision) {+ if (const auto refused = FollowChosenLaunch(*recipe, input.preset, input.kit, input.record, ¤t); !refused.empty())+ return failed("settings-plan", refused);+ // Its architecture comes from the file, whatever the sheet says now, as for a Ready game.+ if (current.launch_entry_id == rt::kProgramEntryId) {+ const auto architecture = rt::GameProgramArchitecture(HostPath(current.install_path).string(), current.launch_path);+ if (!architecture.empty()) input.record.overrides["architecture"] = architecture;+ }+ current.settings_revision = input.record.revision;+ if (input.kit.game_root_unowned) {+ const auto working = WorkingFolder(current.launch_working_directory, current.launch_path, current.launch_in_install_root);+ if (const auto unwritable = EnsureWritable(current.install_path, working); !unwritable.empty())+ return failed("settings-plan", unwritable);+ }+ }// The first start may come from the PS5 Home screen, so the game's Steam files are set up now.- if (const auto steamworks = ApplyPreparationSteps(c, record); !steamworks.empty()) return failed("steamworks", steamworks);+ if (const auto steamworks = ApplyPreparationSteps(c, current); !steamworks.empty()) return failed("steamworks", steamworks);if (!rt::BuildSettingsLaunchPlan(input, &plan, &reason)) {Log("publication plan refused: " + reason);return failed("settings-plan", reason);}+ // Written only once the settings can be published, so a refusal leaves the record as it was.+ if (current.settings_revision != record.settings_revision) {+ if (const auto updated = c.records.Update(current); !updated.ok)+ return failed("settings-record", updated.message);+ Log("prepare settings app=" + std::to_string(record.runtime_number) + " revision=" ++ std::to_string(record.settings_revision) + "->" + std::to_string(current.settings_revision));+ }const auto transaction = config_.uuid();if (!transaction) return failed("random-unavailable", "No secure random source for the publication transaction.");rt::SettingsPublicationRequest request;request.transaction_id = *transaction;request.kit_id = c.kit->view.kit_id;- request.install = record;+ request.install = current;request.plan = std::move(plan);request.registry_unix_time = config_.unix_time();const auto requested = c.owner->Request(request, c.request_directory);@@ -1609,16 +1698,6 @@ rt::PackageActionResult RuntimeHost::DrivePublication(Composition& c, const rt::return resumed;}-// With no working folder set, the profile runs the program from its own folder-// (prospero_profile.cpp), so that is where dxvk.conf goes.-static std::string WorkingFolder(const std::string& working, std::string program, bool in_install_root) {- if (!working.empty() && working != ".") return working;- if (in_install_root) return {};- std::replace(program.begin(), program.end(), '\\', '/');- const auto slash = program.rfind('/');- return slash == std::string::npos ? std::string() : program.substr(0, slash);-}-// The game root or its working folder when either isn't a folder (a stale record, an unplugged// drive): nothing can be published for it. root is the host path of the record's install_path.static std::optional<std::filesystem::path> MissingGameFolder(const std::filesystem::path& root,@@ -1939,53 +2018,7 @@ std::string RuntimeHost::PublishSavedSettings(Composition& c, const rt::RuntimeI// the install record names the new program when this publication commits (Reconcile), so a// crash in between resumes with it. Callers run this only while the game is stopped.auto next = record;- {- rt::EffectiveSettings desired;- rt::SettingsError ignored;- const auto same = [](std::string left, std::string right) {- std::replace(left.begin(), left.end(), '\\', '/');- std::replace(right.begin(), right.end(), '\\', '/');- return (left == "." ? std::string() : left) == (right == "." ? std::string() : right);- };- if (rt::BuildEffectiveSettings(*recipe, preset, saved.overrides, kit, &desired, &ignored) && desired.launch) {- const auto& launch = *desired.launch;- if (launch.id != record.launch_entry_id || !same(launch.executable, record.launch_path) ||- !same(launch.working_directory, record.launch_working_directory) ||- launch.in_install_root != record.launch_in_install_root) {- const auto root = HostPath(record.install_path).string();- const auto architecture = rt::GameProgramArchitecture(root, launch.executable);- if (architecture.empty())- return "The program " + launch.executable + " chosen in Game settings is missing or not a Windows program.";- if (const auto refusal = rt::GameProgramRefusal(root, launch.executable, kit, rt::RecipeChoicesFrom(saved).try_anyway);- !refusal.empty())- return refusal;- // Checked here, before a publication starts: one that fails part way leaves the title- // waiting for recovery.- const auto folder = WorkingFolder(launch.working_directory, launch.executable, launch.in_install_root);- struct stat folder_info{};- if (!folder.empty() && (lstat((HostPath(record.install_path) / folder).c_str(), &folder_info) != 0 ||- !S_ISDIR(folder_info.st_mode)))- return "The program folder " + folder + " chosen in Game settings is missing.";- // A kit that admits unowned game folders gets them as they are (mods, FTP edits).- const auto records_ownership = kit.game_root_unowned ? std::optional<bool>(false)- : platform::RecordsOwnership(HostPath(record.install_path));- if (records_ownership && *records_ownership)- if (const auto refused = TightenWorkingFolder(HostPath(record.install_path),- WorkingFolder(launch.working_directory, launch.executable,- launch.in_install_root));- !refused.empty())- return refused;- next.launch_entry_id = launch.id;- next.launch_path = launch.executable;- next.launch_working_directory = launch.working_directory == "." ? std::string() : launch.working_directory;- next.launch_in_install_root = launch.in_install_root && next.launch_working_directory.empty();- // The recipe stays the one the game was prepared with: its graphics and engine arguments- // came from the first program's files, and only a preparation sees Steam's launch options.- Log("program switch app=" + std::to_string(record.runtime_number) + " from=" + record.launch_entry_id + ":" +- record.launch_path + " to=" + next.launch_entry_id + ":" + next.launch_path + " recipe kept=" + record.recipe_id);- }- }- }+ if (const auto refused = FollowChosenLaunch(*recipe, preset, kit, saved, &next); !refused.empty()) return refused;// An unowned folder is published as it is, so one made over FTP (root's, 0755) is opened up// before dxvk.conf is written into it, as preparation does.if (kit.game_root_unowned) {diff --git a/src/homebrew/runtime_host.hpp b/src/homebrew/runtime_host.hppindex 3aeca46..6c68ee5 100644--- a/src/homebrew/runtime_host.hpp+++ b/src/homebrew/runtime_host.hpp@@ -546,6 +546,11 @@ class RuntimeHost {void LoadGeneratedRecipes(Composition& composition);void PublishRecipes(const Composition& composition);runtime::PackageActionResult DrivePublication(Composition& composition, const runtime::RuntimeInstall& record);+ // Moves *next (an install record) to the program and folder the saved settings choose, after checking+ // them on disk. Empty on success, otherwise why not (for the player). Call only while the game is stopped.+ std::string FollowChosenLaunch(const runtime::GameRecipe& recipe, const runtime::SettingsPreset* preset,+ const runtime::KitCapabilities& kit, const runtime::GameSettingsRecord& saved,+ runtime::RuntimeInstall* next);// Puts the saved settings into a Ready title when they are newer than its publication:// Steam files, then a journaled settings update (profile, dxvk.conf, registry). Call only// while the title is stopped. Empty on success or when nothing changed; *published saysdiff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cppindex 0f24e61..d133a64 100644--- a/tests/homebrew_runtime_host_test.cpp+++ b/tests/homebrew_runtime_host_test.cpp@@ -1904,6 +1904,158 @@ void SettingsSavedDuringPrepareReachReadyTitle() {host.Shutdown();}+// Settings saved before the game's profile is written are the ones it publishes: the preparation+// takes them up, a program chosen meanwhile included, and a game that stopped there finishes when+// it is prepared again. Settings older than the preparation's are refused.+void SettingsSavedBeforeProfileArePublished() {+ Fixture fixture;+ auto config = fixture.Config();+ config.host_namespace_prefix = fixture.root;+ config.package_paths.console_data_root = "/data";+ config.package_paths.host_data_root = fixture.root / "data";+ config.package_operations.hash_file = HashArtifactFile;+ config.package_operations.hash_bytes = HashArtifactBytes;+ config.package_operations.free_space_unverifiable = true;+ config.package_operations.validate_bottle = [](const fs::path& bottle) {+ return fs::is_regular_file(bottle / "system.reg") && fs::is_regular_file(bottle / "user.reg");+ };+ auto kit = SyntheticKit(fixture.root / "kit");+ kit.capabilities = {"dxvk", "offline_bottle", "per_game_title", kKitDxvkConfig, "wow64_i386"};+ config.admit_kit = [&kit](std::string*) { return std::optional<RuntimeKit>(kit); };+ unsigned uuids = 0;+ config.uuid = [&uuids]() -> std::optional<std::string> {+ ++uuids; return std::string("00000000-0000-4000-8000-00000000000") + std::to_string(uuids);+ };+ std::int64_t now = 1760000000;+ config.unix_time = [&now] { return now; };+ config.session_data_root = fixture.root / "data";+ std::mutex mutex;+ GameSettingsRecord saved;+ std::optional<GameSettingsRecord> before_profile; // saved as the profile's step comes up, once+ config.settings_record = [&](const playden::GameId&) -> std::optional<GameSettingsRecord> {+ std::lock_guard<std::mutex> lock(mutex);+ return saved;+ };+ config.log = [&](const std::string& line) {+ std::lock_guard<std::mutex> lock(mutex);+ if (before_profile && line.rfind("prepare step app=220 from=validate-artifacts", 0) == 0) {+ saved = *before_profile;+ before_profile.reset();+ }+ };+ const auto game = fixture.root / "data/homebrew/FAKE77031/games/220";+ Write(game / "hl2.exe", "existing-game", 0644);+ std::string pe(0x80 + 26, '\0');+ pe[0] = 'M'; pe[1] = 'Z'; pe[0x3c] = 0x40; pe[0x40] = 'P'; pe[0x41] = 'E';+ pe[0x44] = 0x64; pe[0x45] = static_cast<char>(0x86); pe[0x40 + 24] = 0x0b; pe[0x40 + 25] = 0x02;+ Write(game / "Tool.exe", pe, 0644);+ CHECK(chmod(game.c_str(), 0755) == 0);+ const auto profile = fixture.root / "data/playden/games/steam-220/library/profiles/playden-steam-220.profile";+ const auto set = [&](std::uint64_t revision, std::map<std::string, std::string> overrides) {+ GameSettingsRecord record;+ record.revision = revision;+ record.overrides = std::move(overrides);+ return record;+ };+ const auto save = [&](const GameSettingsRecord& record) {+ std::lock_guard<std::mutex> lock(mutex);+ saved = record;+ };+ const auto stopped = [](const RuntimeSnapshot& s) {+ return !s.preparing_app && (s.refusals.count(220) || (!s.installs.empty() && s.installs[0].state == PreparationState::kReady));+ };++ RuntimeHost host(config);+ CHECK(host.Start());+ CHECK(WaitFor(host, Started).kit_available);++ // Settings older than the preparation's: refused, and nothing is published from them.+ save(set(5, {{"resolution", "1280x720"}}));+ before_profile = set(4, {{"resolution", "1920x1080"}});+ host.Prepare(Hl2());+ auto done = WaitFor(host, stopped, 60);+ CHECK(done.refusals.count(220) && done.refusals.at(220).find("older") != std::string::npos);+ CHECK(done.installs[0].settings_revision == 5 && !fs::exists(profile));++ // A program that isn't there is refused, and the record stays as it was.+ save(set(6, {{"program", "Missing.exe"}, {"architecture", "pe64"}}));+ host.Prepare(Hl2());+ done = WaitFor(host, [](const RuntimeSnapshot& s) {+ return !s.preparing_app && s.refusals.count(220) && s.refusals.at(220).find("Missing.exe") != std::string::npos;+ }, 60);+ CHECK(done.installs[0].settings_revision == 5 && done.installs[0].launch_path == "hl2.exe" && !fs::exists(profile));++ // Newer settings finish the game that stopped: the profile and the record have them.+ // The program's architecture comes from its file, whatever the settings say.+ save(set(7, {{"program", "Tool.exe"}, {"architecture", "pe32"}, {"resolution", "1280x720"}}));+ host.Prepare(Hl2());+ done = WaitFor(host, [](const RuntimeSnapshot& s) {+ return !s.preparing_app && !s.installs.empty() && s.installs[0].state == PreparationState::kReady;+ }, 60);+ CHECK(done.installs[0].settings_revision == 7 && done.installs[0].launch_path == "Tool.exe" &&+ done.installs[0].launch_entry_id == kProgramEntryId);+ CHECK(Read(profile).find("desktop = 1280x720") != std::string::npos &&+ Read(profile).find("\\games\\220\\Tool.exe\n") != std::string::npos &&+ Read(profile).find("\narchitecture = pe64\n") != std::string::npos);+ host.Shutdown();+}++// The same change during a first preparation needs no second try.+void SettingsSavedDuringFirstPrepareFinish() {+ Fixture fixture;+ auto config = fixture.Config();+ config.host_namespace_prefix = fixture.root;+ config.package_paths.console_data_root = "/data";+ config.package_paths.host_data_root = fixture.root / "data";+ config.package_operations.hash_file = HashArtifactFile;+ config.package_operations.hash_bytes = HashArtifactBytes;+ config.package_operations.free_space_unverifiable = true;+ config.package_operations.validate_bottle = [](const fs::path& bottle) {+ return fs::is_regular_file(bottle / "system.reg") && fs::is_regular_file(bottle / "user.reg");+ };+ auto kit = SyntheticKit(fixture.root / "kit");+ kit.capabilities = {"dxvk", "offline_bottle", "per_game_title", kKitDxvkConfig, "wow64_i386"};+ config.admit_kit = [&kit](std::string*) { return std::optional<RuntimeKit>(kit); };+ unsigned uuids = 0;+ config.uuid = [&uuids]() -> std::optional<std::string> {+ ++uuids; return std::string("00000000-0000-4000-8000-00000000000") + std::to_string(uuids);+ };+ std::int64_t now = 1760000000;+ config.unix_time = [&now] { return now; };+ config.session_data_root = fixture.root / "data";+ std::mutex mutex;+ GameSettingsRecord saved;+ config.settings_record = [&](const playden::GameId&) -> std::optional<GameSettingsRecord> {+ std::lock_guard<std::mutex> lock(mutex);+ return saved;+ };+ // The player changes a setting (a Steam app ID, say) while the game's files are being set up.+ config.log = [&](const std::string& line) {+ std::lock_guard<std::mutex> lock(mutex);+ if (line.rfind("prepare step app=220 from=validate-artifacts", 0) == 0) {+ saved.revision = 1;+ saved.steam_app_id = 440;+ saved.overrides = {{"resolution", "1280x720"}};+ }+ };+ const auto game = fixture.root / "data/homebrew/FAKE77031/games/220";+ Write(game / "hl2.exe", "existing-game", 0644);+ CHECK(chmod(game.c_str(), 0755) == 0);+ const auto profile = fixture.root / "data/playden/games/steam-220/library/profiles/playden-steam-220.profile";++ RuntimeHost host(config);+ CHECK(host.Start());+ CHECK(WaitFor(host, Started).kit_available);+ host.Prepare(Hl2());+ const auto done = WaitFor(host, [](const RuntimeSnapshot& s) {+ return !s.preparing_app && (s.refusals.count(220) || (!s.installs.empty() && s.installs[0].state == PreparationState::kReady));+ }, 60);+ if (done.refusals.count(220)) std::fprintf(stderr, "Refused: %s\n", done.refusals.at(220).c_str());+ CHECK(!done.refusals.count(220) && done.installs[0].state == PreparationState::kReady);+ CHECK(done.installs[0].settings_revision == 1 && Read(profile).find("desktop = 1280x720") != std::string::npos);+ host.Shutdown();+}+// Settings saved after preparation reach the Ready title through a journaled update, from// maintenance or from Play, only while the title is stopped; maintenance clears the shader// cache, backs up the bottle's user folders and repairs the bottle.@@ -6074,6 +6226,8 @@ int main(int argc, char** argv) {KitWithoutUnownedRootsKeepsTightening();for (int route = 0; route <= 5; ++route) OwnedPublicationOpenedUpRecovers(route);SettingsSavedDuringPrepareReachReadyTitle();+ SettingsSavedBeforeProfileArePublished();+ SettingsSavedDuringFirstPrepareFinish();KitRespinKeepsWaitingSettingsUpdate();StaleTitleWaitsForUpdateBefore120Hz();FreshPrepareOn120HzKitPublishesIt();