Skip to content

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

C++

Install a game's PhysX package without a script

Leonardo Lobatoda784d738cd3
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cpp
index 8c6a741..576aae1 100644
--- a/src/homebrew/runtime_host.cpp
+++ b/src/homebrew/runtime_host.cpp
@@ -980,7 +980,12 @@ std::string RuntimeHost::InstallScriptsStep(const GameFiles& files) {
if (!script.ok) return "The game's install script could not be applied: " + script.message + ".";
// The redistributables those scripts run (PhysX and the like): packages Playden can install
// go into the bottle once; a package that fails is logged and the game still starts.
- const auto redist = rt::InstallRedistributables(files.root.string(), *install_dir, files.prefix, config_.unix_time());
+ // A game that needs PhysX may ship it with no script to say so (an imported folder).
+ const auto uses_physx = std::any_of(files.programs.begin(), files.programs.end(), [&files](const std::string& program) {
+ return rt::ProgramUsesPhysx(files.root.string(), program);
+ });
+ const auto redist = rt::InstallRedistributables(files.root.string(), *install_dir, files.prefix, config_.unix_time(), uses_physx);
+ if (redist.physx_missing) Log("redist app=" + std::to_string(record.runtime_number) + " physx needed, no package in the game folder");
// Names and reasons 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);
diff --git a/src/runtime/game_probe.cpp b/src/runtime/game_probe.cpp
index f826c56..7696fdf 100644
--- a/src/runtime/game_probe.cpp
+++ b/src/runtime/game_probe.cpp
@@ -426,6 +426,14 @@ std::string UnrealGameProgram(const std::string& install_root, const std::string
return FindShipping(root, folder, LowerStem(lower_file));
}
+bool ProgramUsesPhysx(const std::string& install_root, const std::string& executable) {
+ const auto facts = ReadPe((fs::path(install_root) / executable).string(), true);
+ if (!facts.valid) return false;
+ for (const auto* list : {&facts.imports, &facts.delay_imports})
+ if (std::find(list->begin(), list->end(), "physxloader.dll") != list->end()) return true;
+ return false;
+}
+
bool ProgramDrawsWithOpenGLOnly(const std::string& install_root, const std::string& executable) {
const auto facts = ReadPe((fs::path(install_root) / executable).string());
if (!facts.valid) return false;
diff --git a/src/runtime/game_probe.h b/src/runtime/game_probe.h
index d1bebcc..9564eda 100644
--- a/src/runtime/game_probe.h
+++ b/src/runtime/game_probe.h
@@ -43,6 +43,10 @@ GameProbe ProbeGame(const std::string& install_root, const std::string& executab
// Unlike GameProbe::apis, the libraries beside it don't count, so a launcher next to the game's DLLs is not one.
bool ProgramDrawsWithOpenGLOnly(const std::string& install_root, const std::string& executable);
+// Whether a program loads PhysX through NVIDIA's loader (PhysXLoader.dll, imported directly or delay-loaded), which
+// finds the engine the PhysX System Software installs. executable is relative to install_root, '/' separators.
+bool ProgramUsesPhysx(const std::string& install_root, const std::string& executable);
+
// The program a game's last run couldn't start, from the note the title's Wine leaves in the bottle: what a launcher
// without launcher mode asked for. Relative to host_root, as spelled in the note, when it's a program in the game's
// folder (console_root on the console, host_root as seen here) that draws or has an engine, so a crash reporter or an
diff --git a/src/runtime/redist_install.cpp b/src/runtime/redist_install.cpp
index 347695d..0287bfb 100644
--- a/src/runtime/redist_install.cpp
+++ b/src/runtime/redist_install.cpp
@@ -27,6 +27,7 @@ using platform::ManagedDirectory;
constexpr std::size_t kMaxPackageBytes = 256u * 1024u * 1024u;
constexpr std::size_t kMaxScripts = 32;
constexpr int kMaxDepth = 4;
+constexpr std::size_t kMaxPhysxPackages = 8;
constexpr const char* kStateFile = ".playden-redist";
constexpr const char* kMfcScanFile = ".playden-mfc";
constexpr const char* kEnvironmentKey = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
@@ -112,6 +113,34 @@ void FindScripts(const std::filesystem::path& folder, const std::string& relativ
}
}
+bool PhysxPackageName(const std::string& name) {
+ const auto lower = Lower(name);
+ if (lower.rfind("physx", 0) != 0 || lower.size() <= 4) return false;
+ const auto extension = lower.substr(lower.size() - 4);
+ return extension == ".msi" || extension == ".exe";
+}
+
+// PhysX's installers in the game's redistributable folders, top-level ones or ones a level down (a game whose
+// files sit in one folder of what was imported), by name.
+void FindPhysxPackages(const std::filesystem::path& folder, const std::string& relative, int depth, bool in_redist,
+ std::vector<std::string>* packages) {
+ std::error_code error;
+ std::vector<std::pair<std::string, bool>> children;
+ for (std::filesystem::directory_iterator it(folder, error), end; !error && it != end; it.increment(error)) {
+ std::error_code type_error;
+ if (it->is_symlink(type_error)) continue;
+ children.emplace_back(it->path().filename().string(), it->is_directory(type_error));
+ }
+ std::sort(children.begin(), children.end());
+ for (const auto& [name, directory] : children) {
+ if (packages->size() >= kMaxPhysxPackages) return;
+ const auto path = relative.empty() ? name : relative + "/" + name;
+ if (!directory && in_redist && PhysxPackageName(name)) packages->push_back(path);
+ else if (directory && depth < kMaxDepth && (in_redist || depth == 0 || RedistFolder(name)))
+ FindPhysxPackages(folder / name, path, depth + 1, in_redist || RedistFolder(name), packages);
+ }
+}
+
// Splits a command line into arguments (double quotes group, as Windows does for msiexec).
std::vector<std::string> Arguments(std::string_view command) {
std::vector<std::string> out;
@@ -703,7 +732,7 @@ std::string ExtendPath(const std::string& current, const std::vector<std::string
}
RedistInstallResult InstallRedistributables(const std::string& game_root, const std::string& install_dir,
- const std::string& prefix_dir, std::int64_t unix_time) {
+ const std::string& prefix_dir, std::int64_t unix_time, bool find_physx) {
RedistInstallResult result;
std::vector<std::string> scripts;
{
@@ -729,98 +758,116 @@ RedistInstallResult InstallRedistributables(const std::string& game_root, const
const auto failure_key = [](const std::string& hash, const std::filesystem::path& path) {
return hash + '\n' + path.parent_path().string();
};
- for (const auto& script : scripts) {
- std::string text, reason;
- std::vector<RedistEntry> entries;
- if (!ReadInstallScript((std::filesystem::path(game_root) / script).string(), &text) ||
- !ParseRunProcess(text, install_dir, &entries, &reason))
- continue;
- for (const auto& entry : entries) {
- RedistOutcome outcome;
- outcome.script = script;
- outcome.name = entry.name;
- std::string package;
- const auto action = ClassifyRedist(entry, install_dir, &package);
- if (action == RedistAction::kNotRun && InsideGame(entry.process, install_dir, &package) && package.size() > 4 &&
- Lower(package).compare(package.size() - 4, 4, ".exe") == 0) {
- // A setup program the console can't start may carry its package as is (PhysX's). One
- // installed before is known by the program's hash, without reading its package again.
- const auto path = std::filesystem::path(game_root) / package;
- const auto hash = HashArtifactFile(path);
- std::string embedded;
- const bool already = hash && installed.count(*hash);
- const auto earlier = hash ? failed.find(failure_key(*hash, path)) : failed.end();
- bool carries = false;
- if (hash && !already && earlier == failed.end() && !without_package.count(*hash)) {
- carries = FindEmbeddedMsiFile(path, &embedded);
- if (!carries) without_package.insert(*hash);
- }
- if (already || earlier != failed.end() || carries) {
- outcome.detail = package + " (its package)";
- if (already) {
- outcome.result = "already";
- } else if (earlier != failed.end()) {
+ const auto run = [&](const std::string& script, const RedistEntry& entry) {
+ RedistOutcome outcome;
+ outcome.script = script;
+ outcome.name = entry.name;
+ std::string package;
+ const auto action = ClassifyRedist(entry, install_dir, &package);
+ if (action == RedistAction::kNotRun && InsideGame(entry.process, install_dir, &package) && package.size() > 4 &&
+ Lower(package).compare(package.size() - 4, 4, ".exe") == 0) {
+ // A setup program the console can't start may carry its package as is (PhysX's). One
+ // installed before is known by the program's hash, without reading its package again.
+ const auto path = std::filesystem::path(game_root) / package;
+ const auto hash = HashArtifactFile(path);
+ std::string embedded;
+ const bool already = hash && installed.count(*hash);
+ const auto earlier = hash ? failed.find(failure_key(*hash, path)) : failed.end();
+ bool carries = false;
+ if (hash && !already && earlier == failed.end() && !without_package.count(*hash)) {
+ carries = FindEmbeddedMsiFile(path, &embedded);
+ if (!carries) without_package.insert(*hash);
+ }
+ if (already || earlier != failed.end() || carries) {
+ outcome.detail = package + " (its package)";
+ if (already) {
+ outcome.result = "already";
+ } else if (earlier != failed.end()) {
+ outcome.result = "failed";
+ outcome.detail = package + ": " + earlier->second;
+ } else {
+ std::string failure;
+ if (!InstallPackageBytes(embedded, path, prefix, entry, unix_time, &outcome.files, &failure)) {
+ outcome.result = "failed";
+ } else if (!RecordPackage(prefix, *hash, package)) {
outcome.result = "failed";
- outcome.detail = package + ": " + earlier->second;
+ failure = "installed, but could not be recorded";
} else {
- std::string failure;
- if (!InstallPackageBytes(embedded, path, prefix, entry, unix_time, &outcome.files, &failure)) {
- outcome.result = "failed";
- } else if (!RecordPackage(prefix, *hash, package)) {
- outcome.result = "failed";
- failure = "installed, but could not be recorded";
- } else {
- outcome.result = "installed";
- installed[*hash] = package;
- }
- if (outcome.result == "failed") {
- outcome.detail = package + ": " + failure;
- failed[failure_key(*hash, path)] = failure;
- }
+ outcome.result = "installed";
+ installed[*hash] = package;
+ }
+ if (outcome.result == "failed") {
+ outcome.detail = package + ": " + failure;
+ failed[failure_key(*hash, path)] = failure;
}
- result.outcomes.push_back(std::move(outcome));
- continue;
}
- }
- if (action != RedistAction::kMsi) {
- outcome.result = action == RedistAction::kCovered ? "covered" : "not-run";
- outcome.detail = FileName(entry.process);
- result.outcomes.push_back(std::move(outcome));
- continue;
- }
- outcome.detail = package;
- const auto path = std::filesystem::path(game_root) / package;
- const auto hash = HashArtifactFile(path);
- if (!hash) {
- outcome.result = "failed";
- outcome.detail = package + ": the package could not be read";
- result.outcomes.push_back(std::move(outcome));
- continue;
- }
- if (installed.count(*hash)) {
- outcome.result = "already";
result.outcomes.push_back(std::move(outcome));
- continue;
- }
- std::string failure;
- if (const auto earlier = failed.find(failure_key(*hash, path)); earlier != failed.end()) {
- outcome.result = "failed";
- failure = earlier->second;
- } else if (!InstallPackage(path, prefix, entry, unix_time, &outcome.files, &failure)) {
- outcome.result = "failed";
- } else if (!RecordPackage(prefix, *hash, package)) {
- outcome.result = "failed";
- failure = "installed, but could not be recorded";
- } else {
- outcome.result = "installed";
- installed[*hash] = package;
- }
- if (outcome.result == "failed") {
- outcome.detail = package + ": " + failure;
- failed[failure_key(*hash, path)] = failure;
+ return;
}
+ }
+ if (action != RedistAction::kMsi) {
+ outcome.result = action == RedistAction::kCovered ? "covered" : "not-run";
+ outcome.detail = FileName(entry.process);
+ result.outcomes.push_back(std::move(outcome));
+ return;
+ }
+ outcome.detail = package;
+ const auto path = std::filesystem::path(game_root) / package;
+ const auto hash = HashArtifactFile(path);
+ if (!hash) {
+ outcome.result = "failed";
+ outcome.detail = package + ": the package could not be read";
+ result.outcomes.push_back(std::move(outcome));
+ return;
+ }
+ if (installed.count(*hash)) {
+ outcome.result = "already";
result.outcomes.push_back(std::move(outcome));
+ return;
+ }
+ std::string failure;
+ if (const auto earlier = failed.find(failure_key(*hash, path)); earlier != failed.end()) {
+ outcome.result = "failed";
+ failure = earlier->second;
+ } else if (!InstallPackage(path, prefix, entry, unix_time, &outcome.files, &failure)) {
+ outcome.result = "failed";
+ } else if (!RecordPackage(prefix, *hash, package)) {
+ outcome.result = "failed";
+ failure = "installed, but could not be recorded";
+ } else {
+ outcome.result = "installed";
+ installed[*hash] = package;
+ }
+ if (outcome.result == "failed") {
+ outcome.detail = package + ": " + failure;
+ failed[failure_key(*hash, path)] = failure;
+ }
+ result.outcomes.push_back(std::move(outcome));
+ };
+ for (const auto& script : scripts) {
+ std::string text, reason;
+ std::vector<RedistEntry> entries;
+ if (!ReadInstallScript((std::filesystem::path(game_root) / script).string(), &text) ||
+ !ParseRunProcess(text, install_dir, &entries, &reason))
+ continue;
+ for (const auto& entry : entries) run(script, entry);
+ }
+ // A game that needs PhysX and has no script to install it (an imported folder): the package it ships. A script
+ // that names PhysX has said which one the game wants.
+ const auto names_physx = [](const RedistOutcome& outcome) {
+ return Lower(outcome.name).find("physx") != std::string::npos || Lower(outcome.detail).find("physx") != std::string::npos;
+ };
+ if (find_physx && std::none_of(result.outcomes.begin(), result.outcomes.end(), names_physx)) {
+ std::vector<std::string> packages;
+ FindPhysxPackages(game_root, std::string(), 0, false, &packages);
+ auto base = install_dir;
+ while (!base.empty() && base.back() == '\\') base.pop_back();
+ for (const auto& package : packages) {
+ auto windows = package;
+ std::replace(windows.begin(), windows.end(), '/', '\\');
+ run("(no script)", RedistEntry{"PhysX", base + "\\" + windows, "", ""});
}
+ result.physx_missing = packages.empty();
}
return result;
}
diff --git a/src/runtime/redist_install.h b/src/runtime/redist_install.h
index 00a7ac9..97170a7 100644
--- a/src/runtime/redist_install.h
+++ b/src/runtime/redist_install.h
@@ -38,6 +38,7 @@ struct RedistOutcome {
struct RedistInstallResult {
std::vector<RedistOutcome> outcomes;
+ bool physx_missing = false; // find_physx, and the game folder has no PhysX package
};
// Finds install scripts at the top of game_root and in its redistributable folders
@@ -46,8 +47,10 @@ struct RedistInstallResult {
// program in the game folder carries (FindEmbeddedMsi). A package installed before (the same
// SHA-256, listed in <prefix>/.playden-redist; a setup program's own) is not installed again. A
// package that fails is reported and left out; the others still install.
+// With find_physx (the game's program uses PhysX, ProgramUsesPhysx) and no script that names PhysX, PhysX's
+// packages in those folders are installed the same way, found by name: a game imported from a folder has no script.
RedistInstallResult InstallRedistributables(const std::string& game_root, const std::string& install_dir,
- const std::string& prefix_dir, std::int64_t unix_time);
+ const std::string& prefix_dir, std::int64_t unix_time, bool find_physx = false);
// Microsoft's MFC library, which Wine doesn't provide, for the games that import it (runtime/game_probe.h
// FindMfcImports): Playden downloads Microsoft's Visual C++ 2015-2022 redistributable once per console, keeps the
diff --git a/tests/game_probe_test.cpp b/tests/game_probe_test.cpp
index bc36d7d..e6543ed 100644
--- a/tests/game_probe_test.cpp
+++ b/tests/game_probe_test.cpp
@@ -753,6 +753,12 @@ int main() {
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());
+ // PhysX's loader, imported or delay-loaded (a game may not load it until it needs it); a bare engine DLL doesn't count.
+ WritePe(root / "Physx/Direct.exe", {false, {"KERNEL32.dll", "PhysXLoader.dll"}});
+ WritePe(root / "Physx/Delayed.exe", {false, {"KERNEL32.dll"}, false, false, "", 0x1400, false, {"PhysXLoader.dll"}});
+ WritePe(root / "Physx/None.exe", {false, {"KERNEL32.dll", "PhysXDevice.dll"}});
+ CHECK(ProgramUsesPhysx(root.string(), "Physx/Direct.exe") && ProgramUsesPhysx(root.string(), "Physx/Delayed.exe"));
+ CHECK(!ProgramUsesPhysx(root.string(), "Physx/None.exe") && !ProgramUsesPhysx(root.string(), "Physx/Missing.exe"));
// Neither: a game without MFC.
const auto plain = fresh();
WritePe(plain / "Game.exe", {true, {"KERNEL32.dll", "VCRUNTIME140.dll", "d3d11.dll"}});
diff --git a/tests/runtime_redist_test.cpp b/tests/runtime_redist_test.cpp
index b8e029d..a3dce3d 100644
--- a/tests/runtime_redist_test.cpp
+++ b/tests/runtime_redist_test.cpp
@@ -408,6 +408,73 @@ void TestInstall() {
std::filesystem::remove_all(base);
}
+// A game with no install script: PhysX's package is found by name, only when the game needs it.
+void TestFoundPhysx() {
+ char pattern[] = "/tmp/playden-redist-XXXXXX";
+ CHECK(::mkdtemp(pattern));
+ const auto base = std::filesystem::canonical(pattern); // /tmp is a symlink on macOS
+ const auto fresh_prefix = [&](const char* name) {
+ const auto prefix = base / name;
+ WriteText(prefix / "system.reg",
+ "WINE REGISTRY Version 2\n;; All keys relative to \\\\Machine\n\n#arch=win64\n");
+ WriteText(prefix / "user.reg", "WINE REGISTRY Version 2\n;; All keys relative to \\\\User\\\\S-1-5-21-0-0-0-1000\n\n#arch=win64\n");
+ std::filesystem::create_directories(prefix / "drive_c/program files (x86)");
+ std::filesystem::permissions(prefix, std::filesystem::perms::owner_all, std::filesystem::perm_options::replace);
+ for (const auto* hive : {"system.reg", "user.reg"})
+ std::filesystem::permissions(prefix / hive, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::replace);
+ return prefix;
+ };
+ const auto game = base / "game";
+ WriteText(game / "_Redist/Nvidia PhysX Legacy Driver/PhysX-9.13.0604-SystemSoftware-Legacy.msi", SamplePackage());
+ WriteText(game / "_Redist/Other/OtherRuntime.msi", SamplePackage()); // named otherwise: left alone
+ WriteText(game / "Binaries/PhysX-Tool.msi", SamplePackage()); // not a redistributable folder
+
+ // Not needed: nothing happens.
+ auto prefix = fresh_prefix("unneeded");
+ auto result = InstallRedistributables(game.string(), "Z:\\games\\1", prefix.string(), 1700000100);
+ CHECK(result.outcomes.empty() && !result.physx_missing && !std::filesystem::exists(prefix / ".playden-redist"));
+
+ // Needed: only PhysX's package, once.
+ prefix = fresh_prefix("needed");
+ result = InstallRedistributables(game.string(), "Z:\\games\\1", prefix.string(), 1700000100, true);
+ CHECK(result.outcomes.size() == 1 && result.outcomes[0].result == "installed" && result.outcomes[0].name == "PhysX" &&
+ result.outcomes[0].detail == "_Redist/Nvidia PhysX Legacy Driver/PhysX-9.13.0604-SystemSoftware-Legacy.msi");
+ CHECK(!result.physx_missing);
+ CHECK(ReadText(prefix / "drive_c/program files (x86)/Vendor Runtime/Common/Loader.dll") == "0123456789");
+ result = InstallRedistributables(game.string(), "Z:\\games\\1", prefix.string(), 1700000200, true);
+ CHECK(result.outcomes.size() == 1 && result.outcomes[0].result == "already");
+
+ // The game's files in one folder of what was imported: its redistributables are a level down.
+ const auto wrapped = base / "wrapped";
+ WriteText(wrapped / "My Game/_Redist/PhysX.msi", SamplePackage());
+ WriteText(wrapped / "My Game/Data/Deep/_Redist/PhysX.msi", "broken"); // too far down to be the game's
+ prefix = fresh_prefix("wrapped-prefix");
+ result = InstallRedistributables(wrapped.string(), "Z:\\games\\2", prefix.string(), 1700000100, true);
+ CHECK(result.outcomes.size() == 1 && result.outcomes[0].detail == "My Game/_Redist/PhysX.msi" &&
+ result.outcomes[0].result == "installed");
+
+ // A script that names PhysX has said which package the game wants.
+ const auto scripted = base / "scripted";
+ WriteText(scripted / "_CommonRedist/installscript.vdf",
+ "\"InstallScript\" { \"Run Process\" { \"PhysX\" { \"process 1\" \"msiexec\" "
+ "\"command 1\" \"/i \\\"%INSTALLDIR%\\\\_CommonRedist\\\\Chosen.msi\\\" /quiet\" } } } "
+ "\"kvsignatures\" { \"InstallScript\" \"00\" }");
+ WriteText(scripted / "_CommonRedist/Chosen.msi", SamplePackage());
+ WriteText(scripted / "_CommonRedist/PhysX-Other.msi", "broken");
+ prefix = fresh_prefix("scripted-prefix");
+ result = InstallRedistributables(scripted.string(), "Z:\\games\\3", prefix.string(), 1700000100, true);
+ CHECK(result.outcomes.size() == 1 && result.outcomes[0].detail == "_CommonRedist/Chosen.msi");
+
+ // Needed, and nothing to install.
+ const auto bare = base / "bare";
+ std::filesystem::create_directories(bare / "Binaries");
+ prefix = fresh_prefix("bare-prefix");
+ result = InstallRedistributables(bare.string(), "Z:\\games\\4", prefix.string(), 1700000100, true);
+ CHECK(result.outcomes.empty() && result.physx_missing);
+ std::filesystem::remove_all(base);
+}
+
// A bottle as Wine makes it: hives and the system folders, private to Playden.
std::filesystem::path MakeBottle(const std::filesystem::path& prefix) {
WriteText(prefix / "system.reg", "WINE REGISTRY Version 2\n;; All keys relative to \\\\Machine\n\n#arch=win64\n");
@@ -786,6 +853,7 @@ int main() {
TestScripts();
TestInstall();
TestEmbeddedPackage();
+ TestFoundPhysx();
TestPhysxPackage();
TestPhysxSetupProgram();
TestMfcFilter();