Play your Steam library's Windows games on a jailbroken PS5.
C++
Apply shipped INI values to games already prepared
Leonardo Lobatod5d7fe95aeca
diff --git a/docs/community-recipes.md b/docs/community-recipes.mdindex 7e02bb9..73fe8c7 100644--- a/docs/community-recipes.md+++ b/docs/community-recipes.md@@ -152,6 +152,8 @@ applies its settings:- A missing file is not made: the game makes its own file from its defaults.Playden also leaves a link or a UTF-16 file alone.- File, section and key names ignore letter case, as on Windows.+- A game that Playden prepared with the recipe it made itself also gets the+ INI values of a recipe that Playden ships for the game later.Rules: the file is a `.ini` path in the game folder, with `/` between foldersand no `.` or `..` part. At most 16 values. Printable ASCII only. No `[` or `]`diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cppindex 11999a3..3fd8e6a 100644--- a/src/homebrew/runtime_host.cpp+++ b/src/homebrew/runtime_host.cpp@@ -700,6 +700,9 @@ struct RuntimeHost::GameFiles {std::filesystem::path root; // the game folder, as this process sees itstd::string prefix; // the game's bottle, as this process sees itstd::vector<std::string> programs;+ // The INI values to apply: the recipe's, or for a game still on the recipe Playden generated, those of the recipe+ // Playden now ships for it (they touch only the game's own files, which the generated recipe doesn't name).+ const std::vector<rt::IniValue>* ini = nullptr;};namespace {@@ -1079,8 +1082,8 @@ std::string RuntimeHost::RecipeRegistryStep(const GameFiles& files) {// Generic step: values the recipe puts in the game's own INI files (docs/community-recipes.md), only where the key// is missing or empty, for a setting the game's launcher would write before starting the game.std::string RuntimeHost::RecipeIniStep(const GameFiles& files) {- const auto& ini = files.recipe->ini;- if (ini.empty()) return {};+ if (!files.ini || files.ini->empty()) return {};+ const auto& ini = *files.ini;const auto applied = rt::ApplyRecipeIniValues(files.root.string(), ini);std::string changed, skipped;for (const auto& file : applied.changed) changed += (changed.empty() ? "" : ",") + file;@@ -1113,6 +1116,12 @@ std::string RuntimeHost::ApplyPreparationSteps(Composition& c, const rt::Runtimefor (const auto& candidate : c.recipes)if (RecipeFits(candidate, record) && candidate.id == record.recipe_id) files.recipe = &candidate;if (!files.recipe) return {}; // publication reports the missing recipe+ files.ini = &files.recipe->ini;+ if (rt::IsGeneratedRecipeId(files.recipe->id))+ for (const auto& candidate : c.recipes)+ if (rt::RecipeGame(candidate) == GameOf(record) && candidate.app_id == record.runtime_number &&+ !rt::IsGeneratedRecipeId(candidate.id))+ files.ini = &candidate.ini;files.saved = config_.settings_record ? config_.settings_record(GameOf(record)) : std::nullopt;const rt::SettingsPreset* preset = nullptr;if (files.saved)diff --git a/src/runtime/game_settings.cpp b/src/runtime/game_settings.cppindex 335574c..8d1eeab 100644--- a/src/runtime/game_settings.cpp+++ b/src/runtime/game_settings.cpp@@ -1369,8 +1369,9 @@ bool RecipeIniValueAllowed(const IniValue& value, std::string* why) {return false;}if (value.section.empty() || value.section.size() > 128 || !printable(value.section) ||- value.section.find_first_of("[]") != std::string::npos) {- *why = "section is not printable ASCII without brackets";+ value.section.find_first_of("[]") != std::string::npos || value.section.front() == ' ' ||+ value.section.back() == ' ') {+ *why = "section is not printable ASCII without brackets or surrounding spaces";return false;}const auto& key = value.key;diff --git a/src/runtime/recipe_ini.cpp b/src/runtime/recipe_ini.cppindex 2e570cc..3350c01 100644--- a/src/runtime/recipe_ini.cpp+++ b/src/runtime/recipe_ini.cpp@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-3.0-or-later#include "runtime/recipe_ini.h"+#include <algorithm>#include <cerrno>#include <fcntl.h>#include <filesystem>@@ -136,7 +137,11 @@ bool WriteAtomic(const fs::path& path, std::string_view bytes, mode_t mode) {bool SetIniValueIfEmpty(std::string* text, const std::string& section, const std::string& key, const std::string& value) {const std::string newline = text->find("\r\n") != std::string::npos ? "\r\n" : "\n";- const auto lines = Lines(*text);+ // A UTF-8 byte-order mark stays where it is, outside the first line.+ constexpr std::string_view kBom = "\xEF\xBB\xBF";+ const bool bom = std::string_view(*text).substr(0, kBom.size()) == kBom;+ auto lines = Lines(*text);+ if (bom) lines.front().begin = std::min(kBom.size(), lines.front().end);bool inside = false;std::optional<std::size_t> last_line; // the first matching section's last non-blank linestd::optional<std::size_t> empty_value; // where an empty value of the key starts@@ -171,8 +176,9 @@ bool SetIniValueIfEmpty(std::string* text, const std::string& section, const stdelse text->insert(at, newline + key + "=" + value);return true;}- if (!text->empty() && text->back() != '\n') text->append(newline);- if (!text->empty()) text->append(newline);+ const bool empty = text->size() == (bom ? kBom.size() : 0);+ if (!empty && text->back() != '\n') text->append(newline);+ if (!empty) text->append(newline);text->append("[" + section + "]" + newline + key + "=" + value + newline);return true;}diff --git a/tests/game_settings_test.cpp b/tests/game_settings_test.cppindex cbd66ec..ab0d4d3 100644--- a/tests/game_settings_test.cpp+++ b/tests/game_settings_test.cpp@@ -1319,6 +1319,7 @@ int main(int argc, char** argv) {CHECK(refuses(R"({"Config//a.ini":{"S":{"k":"v"}}})"));CHECK(refuses(R"({"Config/a.cfg":{"S":{"k":"v"}}})")); // only .ini filesCHECK(refuses(R"({"a.ini":{"S]":{"k":"v"}}})")); // would end the header early+ CHECK(refuses(R"({"a.ini":{" S ":{"k":"v"}}})")); // the file's [S] would never matchCHECK(refuses(R"({"a.ini":{"S":{"k=x":"v"}}})")); // would be another keyCHECK(refuses(R"({"a.ini":{"S":{";k":"v"}}})")); // would be a commentCHECK(refuses(R"({"a.ini":{"S":{"k":"two\nlines"}}})"));diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cppindex 63057bd..2d75d01 100644--- a/tests/homebrew_runtime_host_test.cpp+++ b/tests/homebrew_runtime_host_test.cpp@@ -3777,6 +3777,72 @@ void ShippedRecipeWritesGameIni(bool language_chosen) {host.Shutdown();}+// A copy of Arkham Asylum prepared before Playden shipped its recipe keeps the recipe Playden generated, and still+// gets the shipped recipe's INI values the next time it is played.+void GeneratedRecipeGetsShippedIni() {+ Fixture fixture;+ auto config = fixture.Config();+ const auto runtime = fixture.root / "runtime";+ Write(runtime / "presets.json", Read(fs::path(g_runtime_dir) / "presets.json"), 0644);+ Write(runtime / "recipes/steam-220-hl2-v1.json", Read(fs::path(g_runtime_dir) / "recipes/steam-220-hl2-v1.json"), 0644);+ Write(runtime / "recipes/recipes.lst", "steam-220-hl2-v1.json\n", 0644);+ config.runtime_dir = runtime.string();+ config.steamworks_dir = (fs::path(g_runtime_dir) / "steamworks").string();+ 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}; };+ const auto game = fixture.root / "data/homebrew/FAKE77031/games/35140";+ // A 32-bit PE header is all the generator reads.+ std::string pe(0x80 + 26, '\0');+ pe[0] = 'M'; pe[1] = 'Z'; pe[0x3c] = 0x40; pe[0x40] = 'P'; pe[0x41] = 'E';+ pe[0x44] = 0x4c; pe[0x45] = 0x01; pe[0x40 + 24] = 0x0b; pe[0x40 + 25] = 0x01;+ Write(game / "Binaries/ShippingPC-BmGame.exe", pe, 0644);+ const std::string shipped = "[Engine.Engine]\r\nbSubtitlesEnabled=FALSE\r\n";+ Write(game / "BmGame/Config/UserEngine.ini", shipped, 0644);+ RuntimePrepareRequest request{35140, "/data/homebrew/FAKE77031/games/35140", "internal", "Batman: Arkham Asylum GOTY Edition"};+ request.launches = {{"", "", "Binaries\\ShippingPC-BmGame.exe", "", ""}};+ const auto prepared = [](const RuntimeSnapshot& s) {+ return !s.preparing_app && (s.refusals.count(35140) || (!s.installs.empty() && s.installs[0].state == PreparationState::kReady));+ };+ {+ RuntimeHost host(config);+ CHECK(host.Start());+ CHECK(WaitFor(host, Started).kit_available);+ host.Prepare(request);+ const auto done = WaitFor(host, prepared, 60);+ if (done.refusals.count(35140)) std::fprintf(stderr, "Prepare refused: %s\n", done.refusals.at(35140).c_str());+ CHECK(!done.refusals.count(35140) && done.installs.size() == 1);+ CHECK(Read(config.state_root / "installs.json").find("\"recipe_id\":\"steam-35140-auto\"") != std::string::npos);+ CHECK(Read(game / "BmGame/Config/UserEngine.ini") == shipped);+ host.Shutdown();+ }+ // Playden now ships the recipe: the game keeps its own, and its files get the value.+ Write(runtime / "recipes/steam-35140-arkhamasylum-v1.json",+ Read(fs::path(g_runtime_dir) / "recipes/steam-35140-arkhamasylum-v1.json"), 0644);+ Write(runtime / "recipes/recipes.lst", "steam-220-hl2-v1.json\nsteam-35140-arkhamasylum-v1.json\n", 0644);+ RuntimeHost host(config);+ CHECK(host.Start());+ CHECK(WaitFor(host, Started).kit_available);+ host.Play(35140);+ WaitFor(host, [](const RuntimeSnapshot& s) { return s.plays_handled > 0; }, 30);+ CHECK(Read(config.state_root / "installs.json").find("\"recipe_id\":\"steam-35140-auto\"") != std::string::npos);+ CHECK(Read(game / "BmGame/Config/UserEngine.ini") == shipped + "Language=int\r\n");+ host.Shutdown();+}+// A shipped recipe's requirements are checked against the kit's template, as the kit's packager// checks the kit's own recipe: without PS5 audio in the template, a recipe that needs it is refused.void ShippedRecipeRequirementsFollowTheKit(bool template_audio_ps5) {@@ -5846,6 +5912,7 @@ int main(int argc, char** argv) {ShippedRecipePreparesWithAnotherKitsRecipe();ShippedRecipeWritesGameIni(false);ShippedRecipeWritesGameIni(true);+ GeneratedRecipeGetsShippedIni();ShippedRecipeRequirementsFollowTheKit(false);ShippedRecipeRequirementsFollowTheKit(true);MigrationRewritesInstallPathsAndRepublishes();diff --git a/tests/runtime_recipe_ini_test.cpp b/tests/runtime_recipe_ini_test.cppindex e5a9bbc..2d47ae4 100644--- a/tests/runtime_recipe_ini_test.cpp+++ b/tests/runtime_recipe_ini_test.cpp@@ -64,6 +64,13 @@ int main() {CHECK(Set("[A]\r\nx=1\r\n", "Engine.Engine", "Language", "int") == "[A]\r\nx=1\r\n\r\n[Engine.Engine]\r\nLanguage=int\r\n");CHECK(Set("[A]\nx=1", "B", "k", "v") == "[A]\nx=1\n\n[B]\nk=v\n");CHECK(Set("", "B", "k", "v") == "[B]\nk=v\n");+ // A UTF-8 byte-order mark stays in front and doesn't hide the first section.+ const std::string bom = "\xEF\xBB\xBF";+ CHECK(Set(bom + "[A]\r\nLanguage=fra\r\n", "A", "Language", "int", &changed) == bom + "[A]\r\nLanguage=fra\r\n" && !changed);+ CHECK(Set(bom + "[A]\r\nx=1\r\n", "A", "Language", "int") == bom + "[A]\r\nx=1\r\nLanguage=int\r\n");+ CHECK(Set(bom, "B", "k", "v") == bom + "[B]\nk=v\n");+ // Headers match with spaces inside the brackets.+ CHECK(Set("[ A ]\nLanguage=deu\n", "A", "Language", "int", &changed) == "[ A ]\nLanguage=deu\n" && !changed);const auto root = fs::temp_directory_path() / ("playden-recipe-ini-" + std::to_string(::getpid()));fs::remove_all(root);