Skip to content

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

C++

Keep settings changes working when a program's folder is gone

Leonardo Lobato20fa7eab0eaf
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cpp
index eabf244..0ee01b8 100644
--- a/src/homebrew/runtime_host.cpp
+++ b/src/homebrew/runtime_host.cpp
@@ -1782,6 +1782,37 @@ std::string RuntimeHost::TightenWorkingFolder(const std::filesystem::path& root,
return {};
}
+// The publication being replaced is checked before its settings change, and that needs the folder its
+// program ran in. A folder the player deleted comes back empty so another program can still be chosen.
+// The folders it made, outermost first; empty when none was missing or the game folder itself is gone.
+static std::vector<std::filesystem::path> RestorePublishedFolder(const std::filesystem::path& root,
+ const std::string& working) {
+ std::vector<std::filesystem::path> made;
+ struct stat info{};
+ if (working.empty() || lstat(root.c_str(), &info) != 0 || !S_ISDIR(info.st_mode)) return made;
+ for (const auto& part : std::filesystem::path(working))
+ if (part.empty() || part == "." || part == "..") return made;
+ // A link above the game folder is for the publication check to refuse too.
+ std::filesystem::path above;
+ for (const auto& part : root) {
+ above /= part;
+ if (lstat(above.c_str(), &info) == 0 && S_ISLNK(info.st_mode)) return made;
+ }
+ auto folder = root;
+ for (const auto& part : std::filesystem::path(working)) {
+ folder /= part;
+ // Something else there, such as a link, is for the publication check to refuse: nothing is made through it.
+ if (lstat(folder.c_str(), &info) == 0) {
+ if (!S_ISDIR(info.st_mode)) break;
+ continue;
+ }
+ if (errno != ENOENT || mkdir(folder.c_str(), 0755) != 0) break;
+ chmod(folder.c_str(), 0755); // whatever the umask: the publication check admits 0755
+ made.push_back(folder);
+ }
+ return made;
+}
+
std::string RuntimeHost::EnsureWritable(const std::string& install_path, const std::string& working) {
std::vector<std::filesystem::path> folders;
for (const auto& folder : config_.writable_folders) folders.push_back(HostPath(folder));
@@ -2133,6 +2164,12 @@ std::string RuntimeHost::PublishSavedSettings(Composition& c, const rt::RuntimeI
if (const auto marked = MarkSetupProgram(c, record.install_id, setup_program); !marked.empty()) return marked;
const auto transaction = config_.uuid();
if (!transaction) return "No secure random source for the settings update.";
+ std::vector<std::filesystem::path> restored_folders;
+ if (record.moved_from.empty())
+ restored_folders = RestorePublishedFolder(HostPath(record.install_path),
+ WorkingFolder(record.launch_working_directory, record.launch_path,
+ record.launch_in_install_root));
+ for (const auto& folder : restored_folders) Log("settings update restored missing folder " + folder.string());
rt::SettingsPublicationRequest request;
request.transaction_id = *transaction;
request.kit_id = c.kit->view.kit_id;
@@ -2145,6 +2182,8 @@ std::string RuntimeHost::PublishSavedSettings(Composition& c, const rt::RuntimeI
" revision=" + std::to_string(record.settings_revision) + "->" + std::to_string(saved.revision) + " state=" +
std::to_string(static_cast<int>(requested.state)) + " code=" + requested.code + " message=" + requested.message);
if (requested.state != rt::PackageActionState::kComplete) {
+ // A later try would not know these were made for it.
+ for (auto folder = restored_folders.rbegin(); folder != restored_folders.rend(); ++folder) rmdir(folder->c_str());
if (code) *code = requested.code;
return "The settings could not be applied: " + requested.message;
}
@@ -2153,6 +2192,15 @@ std::string RuntimeHost::PublishSavedSettings(Composition& c, const rt::RuntimeI
const auto resumed = c.owner->ResumeRequested(*armed, c.request_directory, config_.host_namespace_prefix);
Log("settings update resume app=" + std::to_string(record.runtime_number) + " state=" +
std::to_string(static_cast<int>(resumed.state)) + " code=" + resumed.code + " message=" + resumed.message);
+ // Removed again unless the new settings start the program in or below them, and the title's start check
+ // needs them; a folder with anything in it stays.
+ if (resumed.state == rt::PackageActionState::kComplete) {
+ const auto used = (HostPath(record.install_path) /
+ WorkingFolder(next.launch_working_directory, next.launch_path, next.launch_in_install_root))
+ .lexically_normal().generic_string() + "/";
+ for (auto folder = restored_folders.rbegin(); folder != restored_folders.rend(); ++folder)
+ if (used.rfind(folder->lexically_normal().generic_string() + "/", 0) != 0) rmdir(folder->c_str());
+ }
// The title's start check now finds the new folder.
if (moved && resumed.state == rt::PackageActionState::kComplete)
if (const auto current = c.records.Find(record.install_id))
diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cpp
index 9154816..6e43e4c 100644
--- a/tests/homebrew_runtime_host_test.cpp
+++ b/tests/homebrew_runtime_host_test.cpp
@@ -3647,7 +3647,9 @@ void SteamEntryWithoutFolderStartsInInstallRoot() {
const auto kit = SyntheticKit(fixture.root / "kit");
config.admit_kit = [&kit](std::string*) { return std::optional<RuntimeKit>(kit); };
unsigned uuids = 0;
- config.uuid = [&uuids]() -> std::optional<std::string> {
+ bool no_random = false;
+ config.uuid = [&uuids, &no_random]() -> std::optional<std::string> {
+ if (no_random) return std::nullopt;
++uuids; return std::string("00000000-0000-4000-8000-00000000000") + std::to_string(uuids);
};
config.unix_time = [] { return std::int64_t{1760000000}; };
@@ -3694,6 +3696,105 @@ void SteamEntryWithoutFolderStartsInInstallRoot() {
CHECK(applied.maintenance.at(646570).ok && applied.installs[0].launch_in_install_root &&
applied.installs[0].launch_working_directory.empty());
CHECK(Read(profile).find("working_directory = Z:\\data\\homebrew\\FAKE77031\\games\\646570\n") != std::string::npos);
+ // A published folder the player deleted doesn't stop the settings changing, and doesn't come back.
+ std::filesystem::create_directories(game / "extras/deep");
+ CHECK(chmod((game / "extras").c_str(), 0755) == 0 && chmod((game / "extras/deep").c_str(), 0755) == 0);
+ saved.overrides = {{"program_folder", "extras/deep"}};
+ applied = apply(3);
+ CHECK(applied.maintenance.at(646570).ok && applied.installs[0].launch_working_directory == "extras/deep");
+ std::filesystem::remove_all(game / "extras");
+ saved.overrides = {{"program_folder", "."}};
+ applied = apply(4);
+ if (!applied.maintenance.at(646570).ok) std::fprintf(stderr, "apply: %s\n", applied.maintenance.at(646570).message.c_str());
+ CHECK(applied.maintenance.at(646570).ok && applied.installs[0].launch_in_install_root);
+ CHECK(!std::filesystem::exists(game / "extras"));
+ // The new settings still start the program there: the folder stays, as the title's start check needs it.
+ std::filesystem::create_directories(game / "extras/deep");
+ CHECK(chmod((game / "extras").c_str(), 0755) == 0 && chmod((game / "extras/deep").c_str(), 0755) == 0);
+ saved.overrides = {{"program_folder", "extras/deep"}};
+ applied = apply(5);
+ CHECK(applied.maintenance.at(646570).ok);
+ std::filesystem::remove_all(game / "extras");
+ saved.overrides = {{"program_folder", "extras/deep"}, {"arguments", "-windowed"}};
+ applied = apply(6);
+ if (!applied.maintenance.at(646570).ok) std::fprintf(stderr, "apply: %s\n", applied.maintenance.at(646570).message.c_str());
+ CHECK(applied.maintenance.at(646570).ok && std::filesystem::is_directory(game / "extras/deep"));
+ // A try that fails before its publication leaves no folder behind for the next try to keep.
+ std::filesystem::remove_all(game / "extras");
+ no_random = true;
+ applied = apply(7);
+ no_random = false;
+ CHECK(!applied.maintenance.at(646570).ok && !std::filesystem::exists(game / "extras"));
+ std::filesystem::create_directories(game / "extras/deep");
+ CHECK(chmod((game / "extras").c_str(), 0755) == 0 && chmod((game / "extras/deep").c_str(), 0755) == 0);
+ // A link in the deleted folder's place: nothing is made through it, and the check refuses it.
+ const auto outside = fixture.root / "outside";
+ std::filesystem::create_directories(outside);
+ std::filesystem::remove_all(game / "extras");
+ std::filesystem::create_directory_symlink(outside, game / "extras");
+ saved.overrides = {{"program_folder", "."}};
+ applied = apply(8);
+ CHECK(!applied.maintenance.at(646570).ok && !std::filesystem::exists(outside / "deep"));
+ host.Shutdown();
+}
+
+// A link above the game folder: a settings change makes no folder through it.
+void SettingsChangeMakesNoFolderThroughLinkAboveGame() {
+ 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");
+ };
+ const auto kit = SyntheticKit(fixture.root / "kit");
+ 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);
+ };
+ config.unix_time = [] { return std::int64_t{1760000000}; };
+ config.session_data_root = fixture.root / "data";
+ const auto games = fixture.root / "data/homebrew/FAKE77031/games";
+ const auto game = games / "646570";
+ 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 / "game.exe", pe, 0644);
+ CHECK(chmod(game.c_str(), 0755) == 0);
+ fs::create_directories(game / "extras");
+ CHECK(chmod((game / "extras").c_str(), 0755) == 0);
+ GameSettingsRecord saved;
+ config.settings_record = [&](const playden::GameId&) -> std::optional<GameSettingsRecord> { return saved; };
+ RuntimePrepareRequest request{646570, "/data/homebrew/FAKE77031/games/646570", "internal", "Game"};
+ request.launches = {{"", "Play", "game.exe", "", ""}};
+ RuntimeHost host(config);
+ CHECK(host.Start());
+ CHECK(WaitFor(host, Started).kit_available);
+ host.Prepare(request);
+ WaitFor(host, [](const RuntimeSnapshot& s) {
+ return !s.preparing_app && !s.installs.empty() && s.installs[0].state == PreparationState::kReady;
+ }, 60);
+ fixture.activity->store(static_cast<int>(NativeActivity::kStopped));
+ const auto apply = [&](std::uint64_t revision) {
+ saved.revision = revision;
+ const auto before = host.Snapshot().maintenance_handled;
+ host.Maintain(646570, RuntimeMaintenance::kApplySettings);
+ return WaitFor(host, [before](const RuntimeSnapshot& s) { return s.maintenance_handled > before; }, 60);
+ };
+ saved.overrides = {{"program_folder", "extras"}};
+ CHECK(apply(1).maintenance.at(646570).ok);
+ fs::remove_all(game / "extras");
+ const auto elsewhere = fixture.root / "games-elsewhere";
+ fs::rename(games, elsewhere);
+ fs::create_directory_symlink(elsewhere, games);
+ saved.overrides = {{"program_folder", "."}};
+ apply(2);
+ CHECK(!fs::exists(elsewhere / "646570/extras"));
host.Shutdown();
}
@@ -6408,6 +6509,7 @@ int main(int argc, char** argv) {
for (int scenario = 0; scenario <= 3; ++scenario) OlderEngineDefaultsArePublishedOnPlay(scenario);
UpdatedFilesSetUpTheLaunchAgain();
PreparesProgramChosenInSettings();
+ SettingsChangeMakesNoFolderThroughLinkAboveGame();
PreparesSteamProgramChosenAfterDownload();
SteamEntryWithoutFolderStartsInInstallRoot();
PreparesUdkGameWithEulaAccepted();