Skip to content

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

C++

Remember a game's MFC scan in its bottle

Leonardo Lobato2b20a6d385bb
diff --git a/src/runtime/redist_install.cpp b/src/runtime/redist_install.cpp
index 9aac2be..559ca9b 100644
--- a/src/runtime/redist_install.cpp
+++ b/src/runtime/redist_install.cpp
@@ -14,6 +14,7 @@
#include <functional>
#include <map>
#include <set>
+#include <sys/stat.h>
#include <unistd.h>
namespace playden::runtime {
@@ -26,6 +27,7 @@ constexpr std::size_t kMaxPackageBytes = 256u * 1024u * 1024u;
constexpr std::size_t kMaxScripts = 32;
constexpr int kMaxDepth = 4;
constexpr const char* kStateFile = ".playden-redist";
+constexpr const char* kMfcScanFile = ".playden-mfc";
constexpr const char* kEnvironmentKey = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
std::string Lower(std::string_view text) {
@@ -462,6 +464,12 @@ MfcInstallResult InstallMfcRuntime(const VcRedist& redist, const std::string& ca
// Kept for other games: written beside, then renamed into place.
std::error_code error;
std::filesystem::create_directories(folder, error);
+ // Copies an earlier write left behind (Playden stopped part way).
+ std::vector<std::filesystem::path> stale;
+ for (std::filesystem::directory_iterator it(folder, error), end; !error && it != end; it.increment(error))
+ if (it->path().filename().string().rfind(file_name + ".part-", 0) == 0) stale.push_back(it->path());
+ for (const auto& path : stale) std::filesystem::remove(path, error);
+ error.clear();
const auto temporary = folder / (file_name + ".part-" + std::to_string(static_cast<long long>(::getpid())));
const int file = error ? -1 : ::open(temporary.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0644);
std::size_t written = 0;
@@ -487,9 +495,9 @@ MfcInstallResult InstallMfcRuntime(const VcRedist& redist, const std::string& ca
if (!ReadBurnMsiPackage(bundle, redist.package_id, &package, &reason)) return fail(reason);
bundle = std::string(); // its packages are copied out; the console's memory is better spent elsewhere
const auto beside = [&](const std::string& name, std::string* bytes) {
- for (const auto& [candidate, data] : package.beside)
+ for (auto& [candidate, data] : package.beside)
if (Lower(candidate) == Lower(name)) {
- *bytes = data;
+ *bytes = std::move(data); // each cabinet is read once
return true;
}
return false;
@@ -506,6 +514,57 @@ MfcInstallResult InstallMfcRuntime(const VcRedist& redist, const std::string& ca
return out;
}
+bool MfcInstalled(const VcRedist& redist, const std::string& prefix_dir) {
+ return InstalledPackages(prefix_dir).count(redist.sha256) != 0;
+}
+
+std::optional<MfcScanRecord> ReadMfcScan(const std::string& prefix_dir) {
+ std::string text;
+ if (!ReadFile(std::filesystem::path(prefix_dir) / kMfcScanFile, 64 * 1024, &text)) return std::nullopt;
+ MfcScanRecord record;
+ bool versioned = false;
+ for (std::size_t at = 0; at < text.size();) {
+ auto end = text.find('\n', at);
+ if (end == std::string::npos) end = text.size();
+ const auto line = text.substr(at, end - at);
+ at = end + 1;
+ const auto space = line.find(' ');
+ const auto key = line.substr(0, space), value = space == std::string::npos ? std::string() : line.substr(space + 1);
+ if (key == "mfc-scan" && value == "1") versioned = true;
+ else if (key == "files") record.fingerprint = value;
+ else if (key == "x86") record.x86_program = value;
+ else if (key == "x64") record.x64_program = value;
+ }
+ if (!versioned) return std::nullopt;
+ return record;
+}
+
+bool WriteMfcScan(const std::string& prefix_dir, const MfcScanRecord& record) {
+ const auto line = [](const char* key, const std::string& value) {
+ return value.empty() || value.find('\n') != std::string::npos ? std::string() : std::string(key) + " " + value + "\n";
+ };
+ const auto text = "mfc-scan 1\n" + line("files", record.fingerprint) + line("x86", record.x86_program) +
+ line("x64", record.x64_program);
+ auto directory = ManagedDirectory::Open(prefix_dir, DirectoryPolicy::kApprovedTree);
+ return directory && WriteFileAt(*directory, kMfcScanFile, text);
+}
+
+std::string GameFilesFingerprint(const std::string& game_root) {
+ std::string out;
+ for (const char* name : {".playden-install.json", ".playden-files"}) {
+ struct stat info {};
+ if (::lstat((std::filesystem::path(game_root) / name).c_str(), &info) != 0 || !S_ISREG(info.st_mode)) continue;
+#if defined(__APPLE__)
+ const auto nanoseconds = static_cast<long long>(info.st_mtimespec.tv_sec) * 1000000000ll + info.st_mtimespec.tv_nsec;
+#else
+ const auto nanoseconds = static_cast<long long>(info.st_mtim.tv_sec) * 1000000000ll + info.st_mtim.tv_nsec;
+#endif
+ out += std::string(out.empty() ? "" : ",") + name + ":" + std::to_string(static_cast<long long>(info.st_size)) + ":" +
+ std::to_string(nanoseconds);
+ }
+ return out;
+}
+
std::string ExtendPath(const std::string& current, const std::vector<std::string>& prepend,
const std::vector<std::string>& append) {
std::vector<std::string> entries;
diff --git a/src/runtime/redist_install.h b/src/runtime/redist_install.h
index b8bdbac..e1fb21d 100644
--- a/src/runtime/redist_install.h
+++ b/src/runtime/redist_install.h
@@ -83,6 +83,24 @@ struct MfcInstallResult {
MfcInstallResult InstallMfcRuntime(const VcRedist& redist, const std::string& cache_root, const std::string& prefix_dir,
const RangeFetch& fetch, std::int64_t unix_time);
+// Whether the bottle at prefix_dir lists redist's MFC as installed.
+bool MfcInstalled(const VcRedist& redist, const std::string& prefix_dir);
+
+// The latest MFC scan of a game (runtime/game_probe.h FindMfcImports), kept in its bottle (.playden-mfc) so Play
+// doesn't walk the game folder each time: the programs found, per architecture, and the game files' fingerprint
+// then.
+struct MfcScanRecord {
+ std::string fingerprint;
+ std::string x86_program;
+ std::string x64_program;
+};
+std::optional<MfcScanRecord> ReadMfcScan(const std::string& prefix_dir);
+bool WriteMfcScan(const std::string& prefix_dir, const MfcScanRecord& record);
+// What changes when a game's files are installed or updated: the size and time of the install record and file
+// list Playden's downloads and copies write at the game's root (.playden-install.json, .playden-files). Empty for
+// a folder without them (a game added over FTP), whose scan is never reused.
+std::string GameFilesFingerprint(const std::string& game_root);
+
// Adds folders to a PATH value: prepend in order before the existing entries, append after
// them, each only when not already listed (case-insensitive).
std::string ExtendPath(const std::string& current, const std::vector<std::string>& prepend,
diff --git a/tests/runtime_redist_test.cpp b/tests/runtime_redist_test.cpp
index 608182d..ea7eeda 100644
--- a/tests/runtime_redist_test.cpp
+++ b/tests/runtime_redist_test.cpp
@@ -477,6 +477,45 @@ void TestMfcInstall() {
std::filesystem::remove_all(base);
}
+void TestMfcScanRecord() {
+ char pattern[] = "/tmp/playden-mfc-scan-XXXXXX";
+ CHECK(::mkdtemp(pattern));
+ const auto base = std::filesystem::canonical(pattern);
+ const auto prefix = MakeBottle(base / "prefix");
+ CHECK(!ReadMfcScan(prefix.string()));
+ CHECK(WriteMfcScan(prefix.string(), {"files-1", "", "Game_Data/Plugins/Chroma.dll"}));
+ auto read = ReadMfcScan(prefix.string());
+ CHECK(read && read->fingerprint == "files-1" && read->x86_program.empty() && read->x64_program == "Game_Data/Plugins/Chroma.dll");
+ CHECK(WriteMfcScan(prefix.string(), {"", "", ""})); // scanned, no MFC, no fingerprint
+ read = ReadMfcScan(prefix.string());
+ CHECK(read && read->fingerprint.empty() && read->x64_program.empty());
+ // The fingerprint follows the install record and file list Playden writes at the game's root.
+ const auto game = base / "game";
+ std::filesystem::create_directories(game);
+ CHECK(GameFilesFingerprint(game.string()).empty());
+ WriteText(game / ".playden-install.json", "{}");
+ const auto first = GameFilesFingerprint(game.string());
+ CHECK(!first.empty() && GameFilesFingerprint(game.string()) == first);
+ WriteText(game / ".playden-files", "a.exe\n");
+ const auto second = GameFilesFingerprint(game.string());
+ CHECK(second != first);
+ WriteText(game / ".playden-files", "a.exe\nb.dll\n");
+ CHECK(GameFilesFingerprint(game.string()) != second);
+ // A stale part of an earlier download in the cache goes when the next copy is kept.
+ const auto bundle = MfcBundle(true);
+ const auto redist = SyntheticRedist(true, bundle);
+ const auto folder = base / "cache/vc-14.44.35211/x64";
+ WriteText(folder / "VC_redist.x64.exe.part-1", "partial");
+ const RangeFetch fetch = [&](const std::string&, std::uint64_t offset, std::uint64_t length, std::string*) {
+ return std::optional<std::string>(bundle.substr(offset, length));
+ };
+ CHECK(!MfcInstalled(redist, prefix.string()));
+ CHECK(InstallMfcRuntime(redist, (base / "cache").string(), prefix.string(), fetch, 1700000000).result == "installed");
+ CHECK(MfcInstalled(redist, prefix.string()));
+ CHECK(!std::filesystem::exists(folder / "VC_redist.x64.exe.part-1") && ReadText(folder / "VC_redist.x64.exe") == bundle);
+ std::filesystem::remove_all(base);
+}
+
// Microsoft's own bundles, when PLAYDEN_TEST_VC_REDIST_X64 / _X86 name copies: read, mapped and installed (from a
// cache seeded with the copy, so nothing is downloaded).
void TestMicrosoftVcRedist() {
@@ -573,6 +612,7 @@ int main() {
TestPhysxPackage();
TestMfcFilter();
TestMfcInstall();
+ TestMfcScanRecord();
TestMicrosoftVcRedist();
return 0;
}