Skip to content

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

C++

Give games that need MFC Microsoft's MFC on Play

Leonardo Lobato544b93e6c191
diff --git a/src/homebrew/ps5.cpp b/src/homebrew/ps5.cpp
index 0f0d05e..a625dd7 100644
--- a/src/homebrew/ps5.cpp
+++ b/src/homebrew/ps5.cpp
@@ -51,6 +51,7 @@
#include "homebrew/store_fetch.hpp"
#include <memory>
#include <optional>
+#include <strings.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -506,6 +507,47 @@ int main() {
(generic_icon ? " uses the generic icon: no cover" : " has no cover or generic icon"));
return generic_icon;
};
+ // What Playden downloads for a game's bottle (Microsoft's Visual C++ redistributable, for games that need
+ // MFC): ranged HTTPS GETs on the worker, with its own client made on first use. Redirects are followed by
+ // hand to other HTTPS addresses; the bytes are checked against their pinned SHA-256 afterwards.
+ config.fetch_range = [client = std::make_shared<std::unique_ptr<playden::steam::HttpClient>>(),
+ trust = paths.trust_store](const std::string& url, std::uint64_t offset,
+ std::uint64_t length,
+ std::string* error) -> std::optional<std::string> {
+ if (!*client) *client = std::make_unique<playden::steam::HttpClient>(trust);
+ static const std::atomic<bool> never{false};
+ std::string target = url;
+ for (int hop = 0; hop <= 5; ++hop) {
+ playden::steam::HttpRequest request;
+ request.url = target;
+ request.headers = {{"User-Agent", "Playden"},
+ {"Range", "bytes=" + std::to_string(offset) + "-" + std::to_string(offset + length - 1)}};
+ request.timeout_ms = playden::steam::kMaxHttpTimeoutMs;
+ request.max_response_bytes = static_cast<std::size_t>(length) + 1;
+ const auto response = (*client)->Perform(request, never);
+ const long status = response.status_code;
+ if (status == 301 || status == 302 || status == 303 || status == 307 || status == 308) {
+ target.clear();
+ for (const auto& [name, value] : response.headers)
+ if (strcasecmp(name.c_str(), "location") == 0) target = value;
+ if (target.rfind("https://", 0) != 0) {
+ *error = "redirected to an address Playden doesn't follow";
+ return std::nullopt;
+ }
+ continue;
+ }
+ if (!response.ok() || (status != 206 && !(status == 200 && offset == 0)) ||
+ response.body.size() != length) {
+ *error = "status " + std::to_string(status) + " error " +
+ std::to_string(static_cast<int>(response.error)) + " bytes " +
+ std::to_string(response.body.size());
+ return std::nullopt;
+ }
+ return std::string(response.body.begin(), response.body.end());
+ }
+ *error = "too many redirects";
+ return std::nullopt;
+ };
// An any-title kit's helper serves the game titles listed here (root-owned).
config.write_title_allow_list = playden::ps5::WriteTitleAllowList;
config.hold_effective_uid = playden::ps5::HoldEffectiveUid;
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cpp
index 8847200..ad5daee 100644
--- a/src/homebrew/runtime_host.cpp
+++ b/src/homebrew/runtime_host.cpp
@@ -9,6 +9,7 @@
#include "runtime/prospero_profile.h"
#include "runtime/settings_publication_journal.h"
#include "runtime/dotnet_heap.h"
+#include "runtime/game_probe.h"
#include "runtime/install_script.h"
#include "runtime/layout_migration.h"
#include "runtime/redist_install.h"
@@ -890,6 +891,62 @@ std::string RuntimeHost::InstallScriptsStep(const GameFiles& files) {
return {};
}
+// Generic step: Microsoft's MFC for a game whose files import it (Wine has none; a Unity plugin of Moonlighter's
+// stops the game without it), downloaded once per console and installed into the bottle once. An upgrade, not a
+// condition: a failed download or install is logged, the game still starts, and the next Play tries again.
+void RuntimeHost::MfcStep(const GameFiles& files) {
+ const auto needs = rt::FindMfcImports(files.root.string());
+ const auto app = std::to_string(files.record.runtime_number);
+ if (needs.truncated) Log("mfc scan app=" + app + " stopped after " + std::to_string(needs.programs) + " programs");
+ if (needs.x86_program.empty() && needs.x64_program.empty()) return;
+ // Paths come from the game's files: one line each, whatever they hold.
+ const auto field = [](std::string text) {
+ if (text.size() > 240) text.resize(240);
+ for (auto& value : text)
+ if (static_cast<unsigned char>(value) < 0x20 || value == 0x7f) value = '?';
+ return text;
+ };
+ struct stat hive {};
+ if (::lstat((files.prefix + "/system.reg").c_str(), &hive) != 0) {
+ Log("mfc upgrade app=" + app + " skipped: the bottle is not made yet");
+ return;
+ }
+ const auto cache = HostPath(config_.package_paths.console_data_root + "/playden/redist").string();
+ for (const auto& [architecture, program] : {std::pair<std::string, std::string>{"x86", needs.x86_program},
+ std::pair<std::string, std::string>{"x64", needs.x64_program}}) {
+ if (program.empty()) continue;
+ const auto redist = std::find_if(config_.vc_redists.begin(), config_.vc_redists.end(),
+ [&](const rt::VcRedist& candidate) { return candidate.architecture == architecture; });
+ const auto line = "mfc upgrade app=" + app + " arch=" + architecture + " program=" + field(program);
+ if (redist == config_.vc_redists.end()) {
+ Log(line + " skipped: no redistributable for it");
+ continue;
+ }
+ // A download that failed a moment ago (the same preparation, or a Play right after) isn't tried again yet;
+ // a kept copy still installs.
+ const auto began = std::chrono::steady_clock::now();
+ const auto failed = redist_download_failed_.find(redist->sha256);
+ const bool waits = failed != redist_download_failed_.end() && began - failed->second < config_.redist_retry_wait;
+ const auto result = rt::InstallMfcRuntime(*redist, cache, files.prefix, waits ? rt::RangeFetch{} : config_.fetch_range,
+ config_.unix_time());
+ if (result.result == "already") continue;
+ const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - began).count();
+ if (result.result != "installed") {
+ if (result.download_failed) redist_download_failed_[redist->sha256] = std::chrono::steady_clock::now();
+ if (waits && !result.download_failed)
+ Log(line + " skipped: its download failed " +
+ std::to_string(std::chrono::duration_cast<std::chrono::seconds>(began - failed->second).count()) +
+ " s ago; it is tried again from " + std::to_string(config_.redist_retry_wait.count()) + " s");
+ else
+ Log(line + " skipped: " + result.detail);
+ continue;
+ }
+ redist_download_failed_.erase(redist->sha256);
+ Log(line + " installed files=" + std::to_string(result.files) + " from=" + result.source + " ms=" + std::to_string(ms) +
+ (result.cache_problem.empty() ? std::string() : " not kept: " + result.cache_problem));
+ }
+}
+
// Generic step: registry values the recipe carries (docs/community-recipes.md), written the way an install
// script's are.
std::string RuntimeHost::RecipeRegistryStep(const GameFiles& files) {
@@ -917,7 +974,7 @@ std::string RuntimeHost::DotnetHeapStep(const GameFiles& files) {
}
// The steps run in this order for every game, whatever its store: generic ones (media, EULA, Direct3D 12,
-// the bottle's Environment, the recipe's registry values, the .NET heap) and Steam-file ones, which act only
+// the bottle's Environment, MFC, the recipe's registry values, the .NET heap) and Steam-file ones, which act only
// on the Steam files a game has (SteamStub, Steamworks and steam.dll, install scripts and redistributables).
std::string RuntimeHost::ApplyPreparationSteps(Composition& c, const rt::RuntimeInstall& record, bool steam_account_known) {
MediaStep(c, record);
@@ -957,6 +1014,7 @@ std::string RuntimeHost::ApplyPreparationSteps(Composition& c, const rt::Runtime
if (auto refused = Direct3D12Step(c, files); !refused.empty()) return refused;
if (auto refused = BottleEnvironmentStep(files); !refused.empty()) return refused;
if (auto refused = InstallScriptsStep(files); !refused.empty()) return refused;
+ MfcStep(files);
if (auto refused = RecipeRegistryStep(files); !refused.empty()) return refused;
return DotnetHeapStep(files);
}
diff --git a/src/homebrew/runtime_host.hpp b/src/homebrew/runtime_host.hpp
index cb124a3..631713a 100644
--- a/src/homebrew/runtime_host.hpp
+++ b/src/homebrew/runtime_host.hpp
@@ -10,6 +10,7 @@
#include "runtime/install_service.h"
#include "runtime/launch_service.h"
#include "runtime/package_installer.h"
+#include "runtime/redist_install.h"
#include <algorithm>
#include <chrono>
@@ -111,6 +112,14 @@ struct RuntimeHostConfig {
// nothing (the kit's icon stays). Runs on the worker; may use the network.
// Named by the game, so a runtime number never reaches a store's artwork.
std::function<std::optional<std::string>(const GameId& game)> title_icon;
+ // One ranged HTTPS GET for what Playden downloads for a game's bottle: Microsoft's Visual C++ redistributable,
+ // for a game whose files import MFC (runtime/redist_install.h). Runs on the worker, while Play or a preparation
+ // waits. Empty: nothing is downloaded, and such games start without MFC.
+ runtime::RangeFetch fetch_range;
+ // The redistributables MFC comes from, by architecture; kept under <console data>/playden/redist.
+ std::vector<runtime::VcRedist> vc_redists = runtime::MicrosoftVcRedists();
+ // How long a failed download is not tried again (a preparation runs its steps more than once).
+ std::chrono::seconds redist_retry_wait{60};
// Native start of a prepared title with its arguments ("session=<UUID>").
// Called only on the application thread, from Handoff. Empty disables Play.
std::function<ps5::TitleResult(const std::string& title_id, const std::vector<std::string>& arguments)> launch_title;
@@ -553,6 +562,7 @@ class RuntimeHost {
std::string BottleEnvironmentStep(const GameFiles& files);
std::string RecipeRegistryStep(const GameFiles& files);
std::string DotnetHeapStep(const GameFiles& files);
+ void MfcStep(const GameFiles& files); // an upgrade: never stops the start
// Steam-file steps.
std::string SteamStubStep(const GameFiles& files);
std::string SteamworksStep(const GameFiles& files, bool steam_account_known); // and the legacy steam.dll
@@ -632,6 +642,9 @@ class RuntimeHost {
void LearnNumber(const GameId& game, std::uint32_t runtime_number);
RuntimeHostConfig config_;
+ // When each redistributable's download last failed (worker only): a preparation runs its steps more than once,
+ // and Play shouldn't wait for a network that just failed.
+ std::map<std::string, std::chrono::steady_clock::time_point> redist_download_failed_;
mutable std::mutex mutex_;
std::condition_variable wake_;
std::condition_variable published_; // notified on every Publish
diff --git a/src/runtime/redist_install.cpp b/src/runtime/redist_install.cpp
index ded554d..9aac2be 100644
--- a/src/runtime/redist_install.cpp
+++ b/src/runtime/redist_install.cpp
@@ -447,12 +447,17 @@ MfcInstallResult InstallMfcRuntime(const VcRedist& redist, const std::string& ca
chunk.reset();
}
}
- if (!chunk) return fail("the download failed at byte " + std::to_string(offset) + ": " + error);
+ if (!chunk) {
+ out.download_failed = true;
+ return fail("the download failed at byte " + std::to_string(offset) + ": " + error);
+ }
bundle += *chunk;
offset += length;
}
- if (HashArtifactBytes(bundle).value_or("") != redist.sha256)
+ if (HashArtifactBytes(bundle).value_or("") != redist.sha256) {
+ out.download_failed = true;
return fail("the download is not the redistributable Playden expects (SHA-256 mismatch)");
+ }
out.source = "download";
// Kept for other games: written beside, then renamed into place.
std::error_code error;
diff --git a/src/runtime/redist_install.h b/src/runtime/redist_install.h
index f7cb1af..b8bdbac 100644
--- a/src/runtime/redist_install.h
+++ b/src/runtime/redist_install.h
@@ -73,6 +73,7 @@ struct MfcInstallResult {
std::string source; // "download" or "cache", when installed
std::size_t files = 0;
std::string cache_problem; // the download installed but could not be kept for other games: why
+ bool download_failed = false; // failed because the bundle could not be downloaded (or wasn't the pinned one)
};
// Installs redist's MFC files into the stopped bottle at prefix_dir, once: the bottle's .playden-redist lists the
diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cpp
index 271e0f4..d9426ac 100644
--- a/tests/homebrew_runtime_host_test.cpp
+++ b/tests/homebrew_runtime_host_test.cpp
@@ -1,5 +1,6 @@
#include "homebrew/runtime_host.hpp"
#include "runtime_host_fixture.h"
+#include "msi_fixture.h"
#include "platform/managed_directory.h"
#include "platform/ps5/title_allow_list.h"
#include "platform/storage.h"
@@ -3200,6 +3201,146 @@ void PreparesUdkGameWithEulaAccepted() {
host.Shutdown();
}
+// A Windows DLL that imports one other DLL: a PE32 or PE32+ header, one section, an import table.
+std::string ImportingDll(bool pe64, const std::string& import) {
+ std::string bytes(0x600, '\0');
+ const auto put16 = [&](std::size_t at, unsigned value) { bytes[at] = char(value & 0xff); bytes[at + 1] = char(value >> 8); };
+ const auto put32 = [&](std::size_t at, std::uint32_t value) { put16(at, value & 0xffff); put16(at + 2, value >> 16); };
+ const std::size_t optional_size = pe64 ? 0xf0 : 0xe0, optional = 0x58;
+ bytes[0] = 'M'; bytes[1] = 'Z'; put32(0x3c, 0x40); bytes[0x40] = 'P'; bytes[0x41] = 'E';
+ put16(0x44, pe64 ? 0x8664 : 0x014c);
+ put16(0x46, 1);
+ put16(0x54, static_cast<unsigned>(optional_size));
+ put16(optional, pe64 ? 0x20b : 0x10b);
+ put32(optional + (pe64 ? 112 : 96) + 8, 0x1000); // imports at the section's start
+ const std::size_t row = optional + optional_size;
+ bytes.replace(row, 5, ".data");
+ put32(row + 8, 0x200); put32(row + 12, 0x1000); put32(row + 16, 0x200); put32(row + 20, 0x400);
+ put32(0x400 + 12, 0x1100); // the descriptor's DLL name
+ bytes.replace(0x500, import.size(), import);
+ return bytes;
+}
+
+// A game whose plugin imports Microsoft's MFC gets MFC's files in its bottle from the Visual C++ redistributable.
+// An upgrade, not a condition: the preparation that can't download it still ends Ready, and the next Play installs
+// it (also for a game prepared before Playden knew of MFC); later Plays neither download nor install it again.
+void PlayInstallsMfcForGameThatImportsIt() {
+ Fixture fixture;
+ auto config = fixture.Config();
+ config.host_namespace_prefix = fixture.root;
+ config.session_data_root = fixture.root / "data";
+ 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}; };
+ std::string session;
+ config.launch_title = [&session](const std::string&, const std::vector<std::string>& arguments) {
+ session = arguments.at(0).substr(8); // "session=<UUID>"
+ return TitleResult{true, 0, "launch"};
+ };
+ std::vector<std::string> lines;
+ std::mutex lines_mutex;
+ config.log = [&](const std::string& line) {
+ std::lock_guard<std::mutex> lock(lines_mutex);
+ lines.push_back(line);
+ };
+ const auto mfc_lines = [&] {
+ std::lock_guard<std::mutex> lock(lines_mutex);
+ std::vector<std::string> found;
+ for (const auto& line : lines)
+ if (line.rfind("mfc ", 0) == 0) found.push_back(line);
+ return found;
+ };
+ // Microsoft's bundles stand in for by synthetic ones, at their own pinned hashes.
+ const auto x64_bundle = playden::test::msi::MfcBundle(true), x86_bundle = playden::test::msi::MfcBundle(false);
+ config.vc_redists = {
+ {"x86", "https://download.example/x86/VC_redist.x86.exe", *HashArtifactBytes(x86_bundle), x86_bundle.size(),
+ "vcRuntimeAdditional_x86", "14.44.35211"},
+ {"x64", "https://download.example/x64/VC_redist.x64.exe", *HashArtifactBytes(x64_bundle), x64_bundle.size(),
+ "vcRuntimeAdditional_x64", "14.44.35211"}};
+ config.redist_retry_wait = std::chrono::seconds(1);
+ std::atomic<bool> online{false};
+ std::atomic<int> fetches{0};
+ config.fetch_range = [&](const std::string& url, std::uint64_t offset, std::uint64_t length,
+ std::string* error) -> std::optional<std::string> {
+ ++fetches;
+ if (!online) {
+ *error = "offline";
+ return std::nullopt;
+ }
+ const auto& bundle = url.find("x64") != std::string::npos ? x64_bundle : x86_bundle;
+ return bundle.substr(offset, length);
+ };
+ const auto game = fixture.root / "data/homebrew/FAKE77031/games/220";
+ Write(game / "hl2.exe", "existing-game", 0644);
+ Write(game / "bin/chroma.dll", ImportingDll(true, "MFC140U.dll"), 0644);
+ 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, "Prepare refused: %s\n", done.refusals.at(220).c_str());
+ CHECK(!done.refusals.count(220) && done.installs.size() == 1);
+ auto logged = mfc_lines();
+ CHECK(logged.size() == 2);
+ CHECK(logged[0] == "mfc upgrade app=220 arch=x64 program=bin/chroma.dll skipped: the download failed at byte 0: offline");
+ // Three attempts; the preparation's later pass over its steps doesn't wait for the network again.
+ CHECK(logged[1] == "mfc upgrade app=220 arch=x64 program=bin/chroma.dll skipped: its download failed 0 s ago; "
+ "it is tried again from 1 s");
+ CHECK(fetches == 3);
+ const auto windows = fixture.root / "data/playden/games/steam-220/library/prefixes/game/drive_c/windows";
+ CHECK(!fs::exists(windows / "system32/mfc140u.dll"));
+
+ // The game updates with a 32-bit tool that needs MFC as well; the next Play installs both.
+ Write(game / "tools/editor.dll", ImportingDll(false, "mfc140.dll"), 0644);
+ online = true;
+ std::this_thread::sleep_for(std::chrono::milliseconds(1100));
+ host.Play(220);
+ WaitFor(host, [](const RuntimeSnapshot& s) { return s.handoff_ready; }, 30);
+ CHECK(Read(windows / "system32/mfc140u.dll") == "MFC64U" && Read(windows / "system32/mfcm140.dll") == "MFCM64");
+ CHECK(Read(windows / "syswow64/mfc140u.dll") == "MFC32U");
+ CHECK(!fs::exists(windows / "system32/msvcp140.dll") && !fs::exists(windows / "syswow64/msvcp140.dll"));
+ CHECK(Read(fixture.root / "data/playden/redist/vc-14.44.35211/x64/VC_redist.x64.exe") == x64_bundle);
+ CHECK(Read(fixture.root / "data/playden/redist/vc-14.44.35211/x86/VC_redist.x86.exe") == x86_bundle);
+ logged = mfc_lines();
+ CHECK(logged.size() >= 3);
+ CHECK(logged[logged.size() - 2].rfind("mfc upgrade app=220 arch=x86 program=tools/editor.dll installed files=2 from=download ms=", 0) == 0);
+ CHECK(logged.back().rfind("mfc upgrade app=220 arch=x64 program=bin/chroma.dll installed files=2 from=download ms=", 0) == 0);
+ CHECK(host.Handoff());
+ // The game runs and exits (the wrapper's session record).
+ const auto sessions = fixture.root / "data/playden/games/steam-220/logs/sessions";
+ fs::create_directories(sessions);
+ CHECK(chmod((fixture.root / "data/playden/games/steam-220/logs").c_str(), 0700) == 0);
+ CHECK(chmod(sessions.c_str(), 0700) == 0);
+ Write(sessions / (session + ".json"),
+ R"({"schema_version":1,"session_id":")" + session +
+ R"(","install_id":"steam-220","title_id":"FAKE78001","origin":"playden","settings_revision":0,)"
+ R"("runtime_bundle_id":"wine-wow64","phase":"exited"})" "\n", 0600);
+ WaitFor(host, [](const RuntimeSnapshot& s) { return s.launch && s.launch->state == LaunchState::kExited; }, 30);
+ // Installed once: the next Play only finds it there, without the kept download.
+ const auto count = logged.size();
+ const int fetched = fetches;
+ fs::remove_all(fixture.root / "data/playden/redist");
+ host.Play(220);
+ WaitFor(host, [](const RuntimeSnapshot& s) { return s.handoff_ready; }, 30);
+ CHECK(mfc_lines().size() == count && fetches == fetched);
+ CHECK(Read(windows / "system32/mfc140u.dll") == "MFC64U");
+ host.Shutdown();
+}
+
// Playden ships more recipes than the one the kit carries: a game with its own shipped recipe
// (FF XIII) is prepared by a kit packaged for HL2, with the recipe's bottle values written.
void ShippedRecipePreparesWithAnotherKitsRecipe() {
@@ -4895,6 +5036,7 @@ int main(int argc, char** argv) {
PreparesSteamProgramChosenAfterDownload();
SteamEntryWithoutFolderStartsInInstallRoot();
PreparesUdkGameWithEulaAccepted();
+ PlayInstallsMfcForGameThatImportsIt();
ResumesInterruptedCaptureFromSavedRequest();
ResumesInterruptedCaptureFromSavedRequest(true);
RefreshesInstalledTitleToRespunKit();