Skip to content

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

C++

Bring older bottles up to the kit's media backend on Play

Leonardo Lobato24e70b5c50e2
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cpp
index 1803423..64dad7b 100644
--- a/src/homebrew/runtime_host.cpp
+++ b/src/homebrew/runtime_host.cpp
@@ -388,6 +388,13 @@ void RuntimeHost::SetSteamAccount(std::string account_name, std::uint64_t steam_
}
std::string RuntimeHost::ApplySteamworks(Composition& c, const rt::RuntimeInstall& record) {
+ if (c.kit) {
+ std::string reason;
+ if (!rt::ApplyMediaRegistry((c.kit->view.root / c.kit->view.runtime.path).string(),
+ (HostPath(record.data_root) / "library/prefixes/game").string(),
+ config_.unix_time(), &reason))
+ return "The bottle's media registrations could not be updated: " + reason + ".";
+ }
const rt::GameRecipe* recipe = nullptr;
for (const auto& candidate : c.recipes)
if (candidate.app_id == record.app_id && candidate.id == record.recipe_id) recipe = &candidate;
diff --git a/src/runtime/game_settings.cpp b/src/runtime/game_settings.cpp
index c1fc649..f0e168f 100644
--- a/src/runtime/game_settings.cpp
+++ b/src/runtime/game_settings.cpp
@@ -2022,7 +2022,7 @@ bool RegistryEqual(std::string_view left, std::string_view right) {
}
std::string RegistryIdentity(const RegistryValue& value) {
- auto identity = RegistryHeader(value.key) + "\n" + value.name;
+ auto identity = RegistryHeader(value.key) + "\n" + (value.default_value ? "@" : "\"" + value.name);
std::transform(identity.begin(), identity.end(), identity.begin(), AsciiLower);
return identity;
}
@@ -2039,14 +2039,15 @@ bool ApplyRegistryValues(std::string* user_reg, const std::vector<RegistryValue>
std::string text = *user_reg;
std::set<std::string> identities;
for (const auto& value : values) {
- if (value.key.empty() || value.name.empty() || value.name.find('"') != std::string::npos ||
+ if (value.key.empty() || (!value.default_value && value.name.empty()) ||
+ (value.default_value && !value.name.empty()) || value.name.find('"') != std::string::npos ||
HasControl(value.key) || HasControl(value.name) || HasControl(value.data))
return fail("bad registry value");
if (!identities.insert(RegistryIdentity(value)).second) return fail("duplicate registry value");
}
for (const auto& value : values) {
const auto header = RegistryHeader(value.key);
- const auto line_prefix = "\"" + value.name + "\"=";
+ const auto line_prefix = value.default_value ? std::string("@=") : "\"" + value.name + "\"=";
std::size_t section = std::string::npos;
std::size_t body = std::string::npos;
std::size_t end = text.size();
@@ -2095,11 +2096,27 @@ bool ApplyRegistryValues(std::string* user_reg, const std::vector<RegistryValue>
at = newline + 1;
}
if (line != std::string::npos) {
- auto line_end = text.find('\n', line);
- const auto terminator = line_end == std::string::npos ? std::string() :
- (line_end > line && text[line_end - 1] == '\r' ? "\r\n" : "\n");
- line_end = line_end == std::string::npos ? text.size() : line_end + 1;
+ auto at = line;
+ std::size_t line_end = line;
+ std::string logical, terminator;
+ while (true) {
+ const auto newline = text.find('\n', at);
+ auto content_end = std::min(newline, end);
+ terminator = newline == std::string::npos || newline >= end ? std::string() : "\n";
+ if (content_end > at && text[content_end - 1] == '\r') {
+ --content_end;
+ if (!terminator.empty()) terminator = "\r\n";
+ }
+ const bool continuation = content_end > at && text[content_end - 1] == '\\';
+ logical += text.substr(at, content_end - at - (continuation ? 1 : 0));
+ line_end = newline == std::string::npos || newline >= end ? end : newline + 1;
+ if (!continuation) break;
+ if (line_end >= end) return fail("unterminated registry assignment");
+ at = line_end;
+ while (at < end && (text[at] == ' ' || text[at] == '\t')) ++at;
+ }
const auto existing_prefix = text.substr(line, line_prefix.size());
+ if (!value.data.empty() && logical == existing_prefix + value.data) continue;
text.replace(line, line_end - line,
value.data.empty() ? std::string() : existing_prefix + value.data + terminator);
} else if (!value.data.empty()) {
@@ -2163,6 +2180,62 @@ bool ApplyBottleRegistryValues(const std::vector<RegistryValue>& values, const s
return ApplyRegistryFileValues(values, prefix_dir, unix_time, reason, hive);
}
+bool ApplyMediaRegistry(const std::string& runtime_dir, const std::string& prefix_dir,
+ std::int64_t unix_time, std::string* reason) {
+ const auto fail = [&](const char* why) { if (reason) *reason = why; return false; };
+ auto directory = ManagedDirectory::Open(runtime_dir, DirectoryPolicy::kApprovedTree);
+ if (!directory) return fail("cannot open kit media metadata");
+ for (const auto* child : {"share", "wine", "media-build"}) {
+ auto next = directory->OpenChild(child, DirectoryPolicy::kApprovedTree);
+ if (!next) return errno == ENOENT ? true : fail("cannot open kit media metadata");
+ directory = std::move(next);
+ }
+ std::string text;
+ const auto state = ReadRegularAt(*directory, "media-registry.json", 1024 * 1024,
+ &text);
+ if (state == RelativeRead::kMissing) return true;
+ if (state != RelativeRead::kPresent) return fail("cannot read kit media metadata");
+ const auto root = storage_json::Parse(text);
+ const auto* schema = root ? root->Find("schema") : nullptr;
+ const auto* entries = root ? root->Find("values") : nullptr;
+ if (!schema || schema->text() != "playden-media-registry/1" || !entries || !entries->is_array() ||
+ entries->array().empty()) return fail("invalid kit media metadata");
+ std::vector<RegistryValue> values;
+ bool native = false, wow = false;
+ for (const auto& entry : entries->array()) {
+ const auto* key = entry.Find("key"), *name = entry.Find("name"), *data = entry.Find("data");
+ if (!key || !key->is_string() || !name || !name->is_string() || !data || !data->is_string() || data->text().empty())
+ return fail("invalid kit media value");
+ const std::string base = "Software\\Classes\\";
+ if (key->text().compare(0, base.size(), base) != 0) return fail("media value outside Classes");
+ if (key->text().compare(base.size(), 12, "Wow6432Node\\") == 0) wow = true;
+ else native = true;
+ values.push_back({key->text(), name->text(), data->text(), name->text().empty()});
+ }
+ if (!native || !wow) return fail("media metadata lacks both registry views");
+ if (!ApplyBottleRegistryValues(values, prefix_dir, unix_time, "system.reg", reason)) return false;
+ // Once a prefix exists, Wine loads a builtin DLL only through its file in system32 or syswow64 (kit bottles never
+ // run wineboot), so a bottle made from an earlier template also needs winegstreamer.dll itself. An existing copy
+ // is kept: Wine loads the runtime's builtin through it.
+ constexpr std::size_t kMaxMediaDllBytes = 64u * 1024u * 1024u;
+ for (const auto& [arch, folder] : {std::pair{"x86_64-windows", "system32"}, std::pair{"i386-windows", "syswow64"}}) {
+ auto bottle = ManagedDirectory::Open(std::filesystem::path(prefix_dir) / "drive_c/windows" / folder,
+ DirectoryPolicy::kApprovedTree);
+ if (!bottle) return fail("cannot open the bottle's windows folders");
+ struct stat existing {};
+ if (bottle->InspectChild("winegstreamer.dll", &existing)) continue;
+ if (errno != ENOENT) return fail("cannot inspect the bottle's winegstreamer.dll");
+ auto source = ManagedDirectory::Open(std::filesystem::path(runtime_dir) / "lib/wine" / arch,
+ DirectoryPolicy::kApprovedTree);
+ std::string dll;
+ if (!source || ReadRegularAt(*source, "winegstreamer.dll", kMaxMediaDllBytes, &dll) != RelativeRead::kPresent)
+ return fail("the kit's winegstreamer.dll is missing");
+ if (!WriteAtomicAt(*bottle, "winegstreamer.dll", dll, 0644, false, false, false))
+ return fail("cannot copy winegstreamer.dll into the bottle");
+ }
+ return true;
+}
+
bool ParseUe3InstallGuid(std::string_view install_info, std::string* guid) {
static constexpr std::string_view open = "<InstallGuidString>", close = "</InstallGuidString>";
const auto start = install_info.find(open);
diff --git a/src/runtime/game_settings.h b/src/runtime/game_settings.h
index ac3f871..88bf911 100644
--- a/src/runtime/game_settings.h
+++ b/src/runtime/game_settings.h
@@ -92,6 +92,7 @@ struct RegistryValue {
std::string key; // e.g. Software\Wine\AppDefaults\hl2.exe (single backslashes)
std::string name; // e.g. Version
std::string data; // Wine's text form: "\"win7\"" or "dword:00000078"; empty removes
+ bool default_value = false; // Wine uses @= for a key's unnamed value
};
struct GameRecipe {
@@ -297,6 +298,11 @@ bool ApplyRegistrySettings(const EffectiveSettings& settings, const std::string&
bool ApplyBottleRegistryValues(const std::vector<RegistryValue>& values, const std::string& prefix_dir,
std::int64_t unix_time, const std::string& hive, std::string* reason);
+// Merge the staged kit media values into a stopped bottle, and copy the kit's winegstreamer.dll into its system32
+// and syswow64 when missing (Wine finds a builtin only through that file). Older kits without the file are unchanged.
+bool ApplyMediaRegistry(const std::string& runtime_dir, const std::string& prefix_dir,
+ std::int64_t unix_time, std::string* reason);
+
// The <InstallGuidString> of an Unreal Engine 3 (UDK) Binaries/InstallInfo.xml: exactly one,
// a 36-character 8-4-4-4-12 hex GUID once surrounding whitespace is trimmed.
bool ParseUe3InstallGuid(std::string_view install_info, std::string* guid);
diff --git a/tests/game_settings_test.cpp b/tests/game_settings_test.cpp
index ab9f868..e4023e2 100644
--- a/tests/game_settings_test.cpp
+++ b/tests/game_settings_test.cpp
@@ -517,6 +517,19 @@ int main(int argc, char** argv) {
std::filesystem::remove_all(eula_root);
}
+ // Wine REG_BINARY continuations form one logical assignment. Equal values keep their original bytes.
+ std::string binary_hive = "WINE REGISTRY Version 2\n\n[Software\\\\Classes\\\\Media]\n"
+ "\"FilterData\"=hex:01,02,\\\n 03,04\n\"keep\"=\"yes\"\n";
+ const auto binary_original = binary_hive;
+ CHECK(ApplyRegistryValues(&binary_hive, {{"Software\\Classes\\Media", "FilterData", "hex:01,02,03,04"}}, 1, &reason));
+ CHECK(binary_hive == binary_original);
+ CHECK(ApplyRegistryValues(&binary_hive, {{"Software\\Classes\\Media", "FilterData", "hex:05,06"}}, 1, &reason));
+ CHECK(binary_hive.find("03,04") == std::string::npos && binary_hive.find("hex:05,06\n") != std::string::npos);
+ CHECK(binary_hive.find("\"keep\"=\"yes\"") != std::string::npos);
+ const auto binary_updated = binary_hive;
+ CHECK(ApplyRegistryValues(&binary_hive, {{"Software\\Classes\\Media", "FilterData", "hex:05,06"}}, 2, &reason));
+ CHECK(binary_hive == binary_updated);
+
// A Steam install script's Registry section goes into the bottle (Fallout: New Vegas's launcher
// offers only "Install" without it); its Run Process entries are not run.
{
@@ -808,6 +821,54 @@ int main(int argc, char** argv) {
CHECK(!ApplyGameSettings(nested, settings, apply_target, prospero, &reason));
CHECK(!std::filesystem::exists(outside / "dxvk.conf"));
+ // Fresh and existing bottles converge on both media views; repeating does not rewrite either hive or backup.
+ const auto media_runtime = root / "media-runtime";
+ std::filesystem::create_directories(media_runtime / "share/wine/media-build");
+ const auto media_manifest = media_runtime / "share/wine/media-build/media-registry.json";
+ const std::string media_json = R"({"schema":"playden-media-registry/1","values":[
+ {"key":"Software\\Classes\\CLSID\\{317df618-5e5a-468a-9f15-d827a9a08162}\\InprocServer32","name":"","data":"\"C:\\windows\\system32\\winegstreamer.dll\""},
+ {"key":"Software\\Classes\\Wow6432Node\\CLSID\\{317df618-5e5a-468a-9f15-d827a9a08162}\\InprocServer32","name":"","data":"\"C:\\windows\\syswow64\\winegstreamer.dll\""}]})";
+ std::ofstream(media_manifest) << media_json;
+ for (const auto* arch : {"x86_64-windows", "i386-windows"}) {
+ std::filesystem::create_directories(media_runtime / "lib/wine" / arch);
+ std::ofstream(media_runtime / "lib/wine" / arch / "winegstreamer.dll") << "builtin " << arch;
+ }
+ std::string upgraded;
+ for (const auto* kind : {"old", "fresh"}) {
+ const auto bottle = root / kind;
+ std::filesystem::create_directory(bottle);
+ std::filesystem::permissions(bottle, std::filesystem::perms::owner_all);
+ const std::string original = upgraded.empty()
+ ? "WINE REGISTRY Version 2\n\n[Software\\\\Game]\n\"keep\"=\"yes\"\n" : upgraded;
+ std::ofstream(bottle / "system.reg") << original;
+ for (const auto* folder : {"system32", "syswow64"}) std::filesystem::create_directories(bottle / "drive_c/windows" / folder);
+ // A fresh bottle's template already has the DLL; it is kept as it is.
+ if (std::string(kind) == "fresh") std::ofstream(bottle / "drive_c/windows/syswow64/winegstreamer.dll") << "template";
+ CHECK(ApplyMediaRegistry(media_runtime.string(), bottle.string(), 123, &reason));
+ CHECK(ReadText(bottle / "drive_c/windows/system32/winegstreamer.dll") == "builtin x86_64-windows");
+ CHECK(ReadText(bottle / "drive_c/windows/syswow64/winegstreamer.dll") ==
+ (std::string(kind) == "old" ? "builtin i386-windows" : "template"));
+ const auto after = ReadText(bottle / "system.reg");
+ CHECK(after.find("@=\"C:") != std::string::npos && after.find("Wow6432Node") != std::string::npos);
+ CHECK(after.find("\"keep\"=\"yes\"") != std::string::npos);
+ if (std::string(kind) == "old") CHECK(ReadText(bottle / "system.reg.playden-backup") == original);
+ else CHECK(!std::filesystem::exists(bottle / "system.reg.playden-backup"));
+ if (upgraded.empty()) upgraded = after;
+ else CHECK(after == upgraded);
+ const auto stamp = std::filesystem::last_write_time(bottle / "system.reg");
+ CHECK(ApplyMediaRegistry(media_runtime.string(), bottle.string(), 999, &reason));
+ CHECK(ReadText(bottle / "system.reg") == after && std::filesystem::last_write_time(bottle / "system.reg") == stamp);
+ if (std::string(kind) == "old") CHECK(ReadText(bottle / "system.reg.playden-backup") == original);
+ else CHECK(!std::filesystem::exists(bottle / "system.reg.playden-backup"));
+ }
+ std::ofstream(media_manifest) << R"({"schema":"playden-media-registry/1","values":[]})";
+ CHECK(!ApplyMediaRegistry(media_runtime.string(), (root / "old").string(), 123, &reason));
+ CHECK(ReadText(root / "old/system.reg") == upgraded);
+ std::filesystem::remove(media_manifest);
+ CHECK(ApplyMediaRegistry(media_runtime.string(), (root / "old").string(), 123, &reason));
+ std::filesystem::create_symlink(root / "old/system.reg", media_manifest);
+ CHECK(!ApplyMediaRegistry(media_runtime.string(), (root / "old").string(), 123, &reason));
+
// An oversized registry file is rejected before backup or replacement.
const auto prefix = root / "prefix";
std::filesystem::create_directory(prefix);
diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cpp
index 717bccc..3eee172 100644
--- a/tests/homebrew_runtime_host_test.cpp
+++ b/tests/homebrew_runtime_host_test.cpp
@@ -766,8 +766,27 @@ void PlaysReadyTitleThroughHandoff() {
return !s.preparing_app && !s.installs.empty() && s.installs[0].state == PreparationState::kReady;
}, 60);
+ // A kit update adds media metadata after this bottle was made. The next Play merges it before handoff.
+ const auto media = kit.view.root / kit.view.runtime.path / "share/wine/media-build/media-registry.json";
+ const auto hive = fixture.root / "data/playden/games/steam-220/library/prefixes/game/system.reg";
+ CHECK(Read(hive).find("winegstreamer") == std::string::npos);
+ Write(media, R"({"schema":"playden-media-registry/1","values":[
+ {"key":"Software\\Classes\\CLSID\\media\\InprocServer32","name":"","data":"\"winegstreamer.dll\""},
+ {"key":"Software\\Classes\\Wow6432Node\\CLSID\\media\\InprocServer32","name":"","data":"\"winegstreamer.dll\""}]})", 0644);
+ // The bottle predates the media DLLs as well; the same Play copies them in.
+ const auto windows = fixture.root / "data/playden/games/steam-220/library/prefixes/game/drive_c/windows";
+ for (const auto* arch : {"x86_64-windows", "i386-windows"})
+ Write(kit.view.root / kit.view.runtime.path / "lib/wine" / arch / "winegstreamer.dll", std::string("dll ") + arch, 0644);
+ for (const auto* folder : {"system32", "syswow64"}) std::filesystem::create_directories(windows / folder);
+
host.Play(220);
const auto prepared = WaitFor(host, [](const RuntimeSnapshot& s) { return s.handoff_ready; }, 30);
+ const auto upgraded_hive = Read(hive);
+ CHECK(upgraded_hive.find("@=\"winegstreamer.dll\"") != std::string::npos &&
+ upgraded_hive.find("Wow6432Node") != std::string::npos);
+ CHECK(Read(hive.string() + ".playden-backup") == "WINE REGISTRY Version 2\n");
+ CHECK(Read(windows / "system32/winegstreamer.dll") == "dll x86_64-windows");
+ CHECK(Read(windows / "syswow64/winegstreamer.dll") == "dll i386-windows");
CHECK(prepared.launch && prepared.launch->app_id == 220 && prepared.launch->title_id == "FAKE78001");
// Cloud saves under Windows folders use this game's private bottle.
CHECK(prepared.installs[0].prefix_root == "/data/playden/games/steam-220/library/prefixes/game");
@@ -821,6 +840,7 @@ void PlaysReadyTitleThroughHandoff() {
host.Play(220);
WaitFor(host, [](const RuntimeSnapshot& s) { return s.handoff_ready; }, 30);
CHECK(host.Handoff());
+ CHECK(Read(hive) == upgraded_hive);
CHECK(launches.size() == 2 && launches[1].second[0] != launches[0].second[0]);
CHECK(!host.Snapshot().play_refusals.count(220));
// The system closes Playden when the game starts. If the game is then closed