Skip to content

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

C++

Find the game files that need Microsoft's MFC

Leonardo Lobatof55237acb71b
diff --git a/src/runtime/game_probe.cpp b/src/runtime/game_probe.cpp
index 393df48..408f88a 100644
--- a/src/runtime/game_probe.cpp
+++ b/src/runtime/game_probe.cpp
@@ -88,10 +88,12 @@ struct PeFacts {
std::string entry_section;
// A writable section shared between processes that doesn't fill whole console pages.
bool unaligned_shared = false;
+ bool pe64 = false; // PE32+ (x64), otherwise PE32 (x86)
std::vector<std::string> imports; // lower case
+ std::vector<std::string> delay_imports; // lower case; read only when asked
};
-PeFacts ReadPe(const std::string& path) {
+PeFacts ReadPe(const std::string& path, bool delay_imports = false) {
PeFacts facts;
Program program(path);
unsigned char dos[64];
@@ -114,7 +116,9 @@ PeFacts ReadPe(const std::string& path) {
return at + 8 <= optional.size() ? U32(optional.data() + at) : 0;
};
const std::uint32_t import_rva = directory(1);
+ const std::uint32_t delay_rva = directory(13);
facts.clr = directory(14) != 0;
+ facts.pe64 = magic == 0x20b;
std::vector<unsigned char> table(sections * 40u);
if (!program.ReadAt(pe + 24 + optional_size, table.data(), table.size())) return facts;
@@ -155,6 +159,21 @@ PeFacts ReadPe(const std::string& path) {
const auto got = program.ReadSome(*name_at, reinterpret_cast<unsigned char*>(name), sizeof(name) - 1);
facts.imports.push_back(Lower(std::string(name, strnlen(name, got))));
}
+ // Delay-load descriptors (32 bytes each): the DLL's name at +4, an address relative to the image when
+ // bit 0 of the attributes is set (the only form linkers have written since Visual C++ 7).
+ auto delay = delay_imports && delay_rva ? file_offset(delay_rva) : std::nullopt;
+ for (unsigned count = 0; delay && count < 512; ++count, *delay += 32) {
+ unsigned char row[32];
+ if (!program.ReadAt(*delay, row, sizeof(row))) break;
+ const std::uint32_t name_rva = U32(row + 4);
+ if (name_rva == 0) break;
+ if (!(U32(row) & 1)) continue;
+ const auto name_at = file_offset(name_rva);
+ if (!name_at) continue;
+ char name[128] = {};
+ const auto got = program.ReadSome(*name_at, reinterpret_cast<unsigned char*>(name), sizeof(name) - 1);
+ facts.delay_imports.push_back(Lower(std::string(name, strnlen(name, got))));
+ }
return facts;
}
@@ -254,8 +273,46 @@ std::string FindShipping(const fs::path& root, const std::string& relative, int
return {};
}
+// Microsoft's MFC library, which Wine doesn't provide: mfc140.dll, mfc140u.dll, mfcm140.dll, mfcm140u.dll (and
+// whatever later 140 builds add).
+bool MfcLibrary(const std::string& lower) {
+ return (lower.rfind("mfc140", 0) == 0 || lower.rfind("mfcm140", 0) == 0) && EndsWith(lower, ".dll");
+}
+
+void ScanMfc(const fs::path& root, const std::string& relative, int depth, MfcImports* out, std::size_t* seen) {
+ const auto entries = Entries(relative.empty() ? root : root / relative);
+ for (const auto& [name, directory] : entries) {
+ if (directory) continue;
+ const auto lower = Lower(name);
+ if (!EndsWith(lower, ".exe") && !EndsWith(lower, ".dll")) continue;
+ if (*seen >= kMfcScanPrograms) {
+ out->truncated = true;
+ return;
+ }
+ ++*seen;
+ const auto path = relative.empty() ? name : relative + "/" + name;
+ const auto facts = ReadPe((root / path).string(), true);
+ if (!facts.valid) continue;
+ auto& program = facts.pe64 ? out->x64_program : out->x86_program;
+ if (!program.empty()) continue;
+ for (const auto* list : {&facts.imports, &facts.delay_imports})
+ if (std::any_of(list->begin(), list->end(), MfcLibrary)) program = path;
+ }
+ if (depth >= kMfcScanDepth) return;
+ for (const auto& [name, directory] : entries)
+ if (directory && !out->truncated) ScanMfc(root, relative.empty() ? name : relative + "/" + name, depth + 1, out, seen);
+}
+
} // namespace
+MfcImports FindMfcImports(const std::string& install_root) {
+ MfcImports out;
+ std::size_t seen = 0;
+ ScanMfc(fs::path(install_root), std::string(), 0, &out, &seen);
+ out.programs = seen;
+ return out;
+}
+
GameProbe ProbeGame(const std::string& install_root, const std::string& executable) {
GameProbe probe;
const fs::path root(install_root);
diff --git a/src/runtime/game_probe.h b/src/runtime/game_probe.h
index a525f5c..d5105e2 100644
--- a/src/runtime/game_probe.h
+++ b/src/runtime/game_probe.h
@@ -1,5 +1,6 @@
#pragma once
+#include <cstddef>
#include <set>
#include <string>
@@ -35,6 +36,20 @@ inline constexpr unsigned long long kProbeScanBytes = 256ull * 1024ull * 1024ull
// executable is relative to install_root with '/' separators, as spelled on disk.
GameProbe ProbeGame(const std::string& install_root, const std::string& executable);
+// Which of a game's programs and libraries import Microsoft's MFC library (mfc140*.dll, mfcm140*.dll, directly
+// or delay-loaded), which Wine doesn't provide: Playden installs Microsoft's own into the game's bottle. Every
+// .exe and .dll of the game folder is read, kMfcScanDepth folders deep and at most kMfcScanPrograms of them
+// (headers only), without following links. The first program found per architecture, relative to the root.
+struct MfcImports {
+ std::string x86_program; // a PE32 one
+ std::string x64_program; // a PE32+ one
+ std::size_t programs = 0; // programs read
+ bool truncated = false; // stopped at kMfcScanPrograms
+};
+inline constexpr int kMfcScanDepth = 6;
+inline constexpr std::size_t kMfcScanPrograms = 4096;
+MfcImports FindMfcImports(const std::string& install_root);
+
// How a game without a shipped recipe starts, from its probe.
struct GameDefaults {
std::string graphics = "auto"; // auto, dxvk or opengl
diff --git a/tests/game_probe_test.cpp b/tests/game_probe_test.cpp
index e564c3e..038edee 100644
--- a/tests/game_probe_test.cpp
+++ b/tests/game_probe_test.cpp
@@ -32,6 +32,7 @@ struct PeSpec {
std::string text; // bytes placed in the code section (names loaded at run time)
std::size_t data_at = 0x1400; // file offset of the section holding the import table
bool shared_data = false; // the data section is writable and shared, on 4 KB pages
+ std::vector<std::string> delay_imports; // delay-loaded DLLs (descriptors at RVA 0x2a00)
};
void Put32(std::string* bytes, std::size_t at, std::uint32_t value) {
@@ -75,6 +76,14 @@ void WritePe(const fs::path& path, const PeSpec& spec) {
bytes.replace(spec.data_at + (name_rva - 0x2000), spec.imports[i].size(), spec.imports[i]);
name_rva += static_cast<std::uint32_t>(spec.imports[i].size() + 1);
}
+ if (!spec.delay_imports.empty()) Put32(&bytes, directories + 13 * 8, 0x2a00);
+ for (std::size_t i = 0; i < spec.delay_imports.size(); ++i) {
+ const auto row = spec.data_at + 0xa00 + i * 32;
+ Put32(&bytes, row, 1); // addresses are relative to the image
+ Put32(&bytes, row + 4, name_rva);
+ bytes.replace(spec.data_at + (name_rva - 0x2000), spec.delay_imports[i].size(), spec.delay_imports[i]);
+ name_rva += static_cast<std::uint32_t>(spec.delay_imports[i].size() + 1);
+ }
std::ofstream(path, std::ios::binary) << bytes;
}
@@ -502,6 +511,33 @@ int main() {
CHECK(recipe.launch_entries[0].executable == "Launcher.exe" && recipe.launch_entries[0].id == kProgramEntryId);
}
+ { // MFC: a Unity plugin deep in the game's data imports mfc140u.dll (Moonlighter); per architecture.
+ const auto root = fresh();
+ WritePe(root / "Moonlighter.exe", {true, {"UnityPlayer.dll", "KERNEL32.dll"}});
+ WritePe(root / "Moonlighter_Data/Plugins/UnityNativeChromaSDK.dll", {true, {"KERNEL32.dll", "MFC140U.dll"}});
+ fs::create_directories(root / "Moonlighter_Data/Managed");
+ auto found = FindMfcImports(root.string());
+ CHECK(found.x64_program == "Moonlighter_Data/Plugins/UnityNativeChromaSDK.dll" && found.x86_program.empty());
+ CHECK(found.programs == 2 && !found.truncated);
+ // A 32-bit helper that delay-loads mfcm140.dll counts for x86; other DLLs named alike don't.
+ WritePe(root / "Tools/Editor.exe", {false, {"KERNEL32.dll"}, false, false, "", 0x1400, false, {"mfcm140.dll"}});
+ WritePe(root / "Tools/Other.dll", {false, {"mfc42.dll", "mfc140x.lib", "msvcp140.dll"}});
+ found = FindMfcImports(root.string());
+ CHECK(found.x86_program == "Tools/Editor.exe" && !found.x64_program.empty());
+ // Neither: a game without MFC.
+ const auto plain = fresh();
+ WritePe(plain / "Game.exe", {true, {"KERNEL32.dll", "VCRUNTIME140.dll", "d3d11.dll"}});
+ found = FindMfcImports(plain.string());
+ CHECK(found.x86_program.empty() && found.x64_program.empty() && found.programs == 1);
+ // Too deep is not looked into; a link is not followed.
+ WritePe(plain / "a/b/c/d/e/f/g/Deep.dll", {true, {"mfc140.dll"}});
+ fs::create_directory_symlink(root, plain / "linked");
+ found = FindMfcImports(plain.string());
+ CHECK(found.x86_program.empty() && found.x64_program.empty());
+ WritePe(plain / "a/b/c/d/e/f/Six.dll", {true, {"mfc140.dll"}});
+ CHECK(FindMfcImports(plain.string()).x64_program == "a/b/c/d/e/f/Six.dll");
+ }
+
// The player's words for what was found.
CHECK(DetectedLabel("unity-il2cpp,d3d11,d3d12 graphics=dxvk") == "Unity \xC2\xB7 Direct3D 11");
CHECK(DetectedLabel("d3d9 graphics=dxvk") == "Direct3D 9");