Skip to content

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

C++

Write Steam install script registry keys into bottles

Leonardo Lobato7935d6f0a83a
diff --git a/cmake/HomebrewUI.cmake b/cmake/HomebrewUI.cmake
index 0fc7fd4..e0efa60 100644
--- a/cmake/HomebrewUI.cmake
+++ b/cmake/HomebrewUI.cmake
@@ -68,6 +68,7 @@ add_library(playden_steam_cm_codec STATIC
"${CMAKE_SOURCE_DIR}/src/steam/backend/app_info.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/prospero_profile.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/game_settings.cpp"
+ "${CMAKE_SOURCE_DIR}/src/runtime/install_script.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/game_probe.cpp")
target_include_directories(playden_steam_cm_codec PUBLIC "${CMAKE_SOURCE_DIR}/src")
target_include_directories(playden_steam_cm_codec PRIVATE "${PLAYDEN_STB_IMAGE_INCLUDE_DIR}")
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cpp
index e76f10c..40853ed 100644
--- a/src/homebrew/runtime_host.cpp
+++ b/src/homebrew/runtime_host.cpp
@@ -8,6 +8,7 @@
#include "runtime/prospero_profile.h"
#include "runtime/settings_publication_journal.h"
#include "runtime/dotnet_heap.h"
+#include "runtime/install_script.h"
#include "runtime/settings_publication_owner.h"
#include "runtime/session_storage.h"
#include "runtime/steam_stub.h"
@@ -457,6 +458,21 @@ std::string RuntimeHost::ApplySteamworks(Composition& c, const rt::RuntimeInstal
Log("ue3 eula app=" + std::to_string(record.app_id) + " guid=" + (eula.guid.empty() ? "-" : eula.guid) +
" written=" + (eula.written ? "1" : "0") + (eula.ok ? std::string() : " failed=" + eula.message));
if (!eula.ok) return "The game's EULA could not be marked accepted: " + eula.message + ".";
+ // Registry keys Steam's install script writes on a game's first start (Fallout: New Vegas's
+ // launcher wants its install path there).
+ if (const auto install_dir = rt::ProsperoWindowsPath(record.install_path)) {
+ const auto script = rt::ApplyInstallScripts(root.string(), *install_dir,
+ (HostPath(record.data_root) / "library/prefixes/game").string(),
+ config_.unix_time());
+ if (!script.found.empty()) {
+ std::string names;
+ for (const auto& name : script.found) names += (names.empty() ? "" : ",") + name;
+ Log("install script app=" + std::to_string(record.app_id) + " found=" + names +
+ " values=" + std::to_string(script.values) + " skipped=" + std::to_string(script.skipped.size()) +
+ (script.ok ? std::string() : " failed=" + script.message));
+ }
+ if (!script.ok) return "The game's install script could not be applied: " + script.message + ".";
+ }
// Self-contained .NET games reserve more address space than the console grants unless
// their garbage collector is given a heap limit (runtime/dotnet_heap.h).
const auto dotnet = rt::LimitDotnetHeap(root.string());
diff --git a/src/runtime/game_settings.cpp b/src/runtime/game_settings.cpp
index ab46efe..3e2ea12 100644
--- a/src/runtime/game_settings.cpp
+++ b/src/runtime/game_settings.cpp
@@ -2042,6 +2042,11 @@ bool ApplyRegistrySettings(const EffectiveSettings& settings, const std::string&
return ApplyRegistryFileValues(RenderRegistryValues(settings), prefix_dir, unix_time, reason);
}
+bool ApplyBottleRegistryValues(const std::vector<RegistryValue>& values, const std::string& prefix_dir,
+ std::int64_t unix_time, const std::string& hive, std::string* reason) {
+ return ApplyRegistryFileValues(values, prefix_dir, unix_time, reason, hive);
+}
+
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 51178f7..7f88e80 100644
--- a/src/runtime/game_settings.h
+++ b/src/runtime/game_settings.h
@@ -287,6 +287,11 @@ bool ApplyRegistryValues(std::string* user_reg, const std::vector<RegistryValue>
bool ApplyRegistrySettings(const EffectiveSettings& settings, const std::string& prefix_dir, std::int64_t unix_time,
std::string* reason);
+// Applies values to one hive of the stopped bottle (user.reg or system.reg) the same way:
+// backed up to <hive>.playden-backup first, replaced atomically, untouched when nothing changes.
+bool ApplyBottleRegistryValues(const std::vector<RegistryValue>& values, const std::string& prefix_dir,
+ std::int64_t unix_time, const std::string& hive, 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/src/runtime/install_script.cpp b/src/runtime/install_script.cpp
new file mode 100644
index 0000000..0c3754a
--- /dev/null
+++ b/src/runtime/install_script.cpp
@@ -0,0 +1,201 @@
+#include "runtime/install_script.h"
+
+#include "steam/backend/vdf.h"
+
+#include <algorithm>
+#include <cerrno>
+#include <cstdio>
+#include <cstdlib>
+#include <filesystem>
+#include <fcntl.h>
+#include <unistd.h>
+
+namespace playden::runtime {
+namespace {
+
+constexpr std::size_t kMaxScriptBytes = 1024 * 1024;
+constexpr std::size_t kMaxScripts = 8;
+
+std::string Lower(std::string_view text) {
+ std::string out(text);
+ std::transform(out.begin(), out.end(), out.begin(),
+ [](unsigned char value) { return static_cast<char>(value >= 'A' && value <= 'Z' ? value + 32 : value); });
+ return out;
+}
+
+// UTF-16 (either byte order, with its mark) to UTF-8; other text is returned as it is.
+std::string ScriptText(std::string_view bytes) {
+ if (bytes.size() >= 3 && static_cast<unsigned char>(bytes[0]) == 0xef && static_cast<unsigned char>(bytes[1]) == 0xbb &&
+ static_cast<unsigned char>(bytes[2]) == 0xbf)
+ return std::string(bytes.substr(3));
+ if (bytes.size() < 2) return std::string(bytes);
+ const auto first = static_cast<unsigned char>(bytes[0]), second = static_cast<unsigned char>(bytes[1]);
+ const bool little = first == 0xff && second == 0xfe, big = first == 0xfe && second == 0xff;
+ if (!little && !big) return std::string(bytes);
+ std::string out;
+ for (std::size_t index = 2; index + 1 < bytes.size(); index += 2) {
+ const auto low = static_cast<unsigned char>(bytes[index + (little ? 0 : 1)]);
+ const auto high = static_cast<unsigned char>(bytes[index + (little ? 1 : 0)]);
+ const unsigned unit = low | (high << 8);
+ // Characters outside the Basic Multilingual Plane never matter to a registry path; drop them.
+ if (unit >= 0xd800 && unit <= 0xdfff) continue;
+ if (unit < 0x80) {
+ out += static_cast<char>(unit);
+ } else if (unit < 0x800) {
+ out += static_cast<char>(0xc0 | (unit >> 6));
+ out += static_cast<char>(0x80 | (unit & 0x3f));
+ } else {
+ out += static_cast<char>(0xe0 | (unit >> 12));
+ out += static_cast<char>(0x80 | ((unit >> 6) & 0x3f));
+ out += static_cast<char>(0x80 | (unit & 0x3f));
+ }
+ }
+ return out;
+}
+
+bool Ascii(std::string_view text) {
+ return std::all_of(text.begin(), text.end(),
+ [](unsigned char value) { return value >= 0x20 && value < 0x7f; });
+}
+
+// %INSTALLDIR% (any case) replaced; false when another %TOKEN% is left.
+bool Substitute(std::string_view text, const std::string& install_dir, std::string* out) {
+ out->clear();
+ for (std::size_t at = 0; at < text.size();) {
+ if (text[at] != '%') {
+ *out += text[at++];
+ continue;
+ }
+ const auto end = text.find('%', at + 1);
+ if (end == std::string_view::npos || Lower(text.substr(at, end - at + 1)) != "%installdir%") return false;
+ *out += install_dir;
+ at = end + 1;
+ }
+ return true;
+}
+
+// Wine's text form of a REG_SZ: quoted, with backslashes and quotes escaped.
+std::string RegistryString(std::string_view text) {
+ std::string out = "\"";
+ for (const char value : text) {
+ if (value == '\\' || value == '"') out += '\\';
+ out += value;
+ }
+ return out + "\"";
+}
+
+bool ReadScript(const std::filesystem::path& path, std::string* text) {
+ const int file = ::open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
+ if (file < 0) return false;
+ char buffer[8192];
+ bool ok = true;
+ while (true) {
+ const auto count = ::read(file, buffer, sizeof(buffer));
+ if (count < 0 && errno == EINTR) continue;
+ if (count < 0 || text->size() + static_cast<std::size_t>(count) > kMaxScriptBytes) { ok = false; break; }
+ if (count == 0) break;
+ text->append(buffer, static_cast<std::size_t>(count));
+ }
+ ::close(file);
+ return ok;
+}
+
+} // namespace
+
+bool ParseInstallScript(std::string_view text, const std::string& install_dir, InstallScriptValues* values,
+ std::string* reason) {
+ const auto fail = [&](const char* why) {
+ if (reason) *reason = why;
+ return false;
+ };
+ auto utf8 = ScriptText(text);
+ while (!utf8.empty() && utf8.back() == '\0') utf8.pop_back();
+ const auto root = steam::ParseVdf(utf8);
+ if (!root) return fail("not a valid install script");
+ // The top level is "InstallScript" { ... } (any case); a bare section list is read the same way.
+ const steam::VdfNode* script = root->children.size() == 1 && root->children.front().second.is_dict
+ ? &root->children.front().second
+ : &*root;
+ const auto* registry = script->Find("Registry");
+ if (!registry) return true;
+ for (const auto& [path, key] : registry->children) {
+ if (!key.is_dict) continue;
+ const auto separator = path.find('\\');
+ const auto hive = Lower(path.substr(0, separator));
+ if (separator == std::string::npos || separator + 1 >= path.size()) continue;
+ const bool machine = hive == "hkey_local_machine" || hive == "hklm";
+ if (!machine && hive != "hkey_current_user" && hive != "hkcu") continue;
+ const auto subkey = path.substr(separator + 1);
+ if (!Ascii(subkey) || subkey.find('%') != std::string::npos) continue;
+ std::vector<std::string> keys{subkey};
+ // Steam is a 32-bit program, so HKLM\Software lands in the 32-bit view; a 64-bit game
+ // looking in its own view finds it too.
+ if (machine && Lower(subkey).rfind("software\\", 0) == 0 && Lower(subkey).rfind("software\\wow6432node\\", 0) != 0)
+ keys.push_back("Software\\Wow6432Node\\" + subkey.substr(9));
+ auto& target = machine ? values->machine : values->user;
+ for (const auto& [type, entries] : key.children) {
+ const auto kind = Lower(type);
+ if (!entries.is_dict || (kind != "string" && kind != "dword")) continue;
+ for (const auto& [name, data] : entries.children) {
+ if (data.is_dict || name.empty() || !Ascii(name)) continue;
+ std::string value;
+ if (!Substitute(data.value, install_dir, &value) || !Ascii(value)) continue;
+ if (kind == "dword") {
+ char* end = nullptr;
+ errno = 0;
+ const auto number = std::strtoull(value.c_str(), &end, 0);
+ if (value.empty() || errno || *end || number > 0xffffffffull) continue;
+ char text_value[16];
+ std::snprintf(text_value, sizeof(text_value), "dword:%08llx", number);
+ value = text_value;
+ } else {
+ value = RegistryString(value);
+ }
+ for (const auto& name_key : keys) target.push_back({name_key, name, value});
+ }
+ }
+ }
+ return true;
+}
+
+InstallScriptResult ApplyInstallScripts(const std::string& game_root, const std::string& install_dir,
+ const std::string& prefix_dir, std::int64_t unix_time) {
+ InstallScriptResult result;
+ std::vector<std::filesystem::path> scripts;
+ std::error_code error;
+ for (std::filesystem::directory_iterator it(game_root, error), end; !error && it != end; it.increment(error)) {
+ const auto name = Lower(it->path().filename().string());
+ std::error_code type_error;
+ if (name.rfind("installscript", 0) == 0 && name.size() > 4 && name.substr(name.size() - 4) == ".vdf" &&
+ it->is_regular_file(type_error) && !it->is_symlink(type_error))
+ scripts.push_back(it->path());
+ }
+ std::sort(scripts.begin(), scripts.end());
+ if (scripts.size() > kMaxScripts) scripts.resize(kMaxScripts);
+ InstallScriptValues values;
+ for (const auto& path : scripts) {
+ const auto name = path.filename().string();
+ result.found.push_back(name);
+ std::string text, reason;
+ InstallScriptValues parsed;
+ if (!ReadScript(path, &text) || !ParseInstallScript(text, install_dir, &parsed, &reason)) {
+ result.skipped.push_back(name);
+ continue;
+ }
+ values.machine.insert(values.machine.end(), parsed.machine.begin(), parsed.machine.end());
+ values.user.insert(values.user.end(), parsed.user.begin(), parsed.user.end());
+ }
+ result.values = values.machine.size() + values.user.size();
+ for (const auto& [hive, list] : {std::pair<const char*, const std::vector<RegistryValue>*>{"system.reg", &values.machine},
+ std::pair<const char*, const std::vector<RegistryValue>*>{"user.reg", &values.user}}) {
+ std::string reason;
+ if (!list->empty() && !ApplyBottleRegistryValues(*list, prefix_dir, unix_time, hive, &reason)) {
+ result.ok = false;
+ result.message = reason;
+ return result;
+ }
+ }
+ return result;
+}
+
+} // namespace playden::runtime
diff --git a/src/runtime/install_script.h b/src/runtime/install_script.h
new file mode 100644
index 0000000..d8382ec
--- /dev/null
+++ b/src/runtime/install_script.h
@@ -0,0 +1,44 @@
+#pragma once
+
+#include "runtime/game_settings.h"
+
+#include <cstdint>
+#include <string>
+#include <string_view>
+#include <vector>
+
+namespace playden::runtime {
+
+// A Steam install script (installscript.vdf in the game folder): Steam applies its Registry
+// section on a game's first start, and some games look for those keys (Fallout: New Vegas's
+// launcher offers only "Install" without HKLM\SOFTWARE\Bethesda Softworks\FalloutNV
+// "Installed Path"). Playden writes them into the bottle instead. Run Process entries
+// (redistributable installers) are not run: Wine's built-ins stand in for them.
+
+// The values a script's Registry section sets, by hive. %INSTALLDIR% becomes install_dir (the
+// game folder's Windows path). HKLM\Software keys go to both registry views, as Steam, a
+// 32-bit program, writes them where 32-bit and 64-bit games find them. Only "string" and
+// "dword" values are taken; a value with another %TOKEN% or non-ASCII text, and keys under
+// other roots, are skipped. Text may be UTF-8 or UTF-16 with a byte order mark.
+struct InstallScriptValues {
+ std::vector<RegistryValue> machine; // system.reg (HKEY_LOCAL_MACHINE)
+ std::vector<RegistryValue> user; // user.reg (HKEY_CURRENT_USER)
+};
+bool ParseInstallScript(std::string_view text, const std::string& install_dir, InstallScriptValues* values,
+ std::string* reason);
+
+// Applies the Registry sections of the install scripts at the top of game_root (file names
+// starting with "installscript" and ending ".vdf", any case) to the stopped bottle at
+// prefix_dir, backing each hive up first. An unchanged hive is not written. found lists the
+// scripts read; a script that cannot be read or parsed is skipped and named in skipped.
+struct InstallScriptResult {
+ std::vector<std::string> found;
+ std::vector<std::string> skipped;
+ std::size_t values = 0;
+ bool ok = true;
+ std::string message; // on failure
+};
+InstallScriptResult ApplyInstallScripts(const std::string& game_root, const std::string& install_dir,
+ const std::string& prefix_dir, std::int64_t unix_time);
+
+} // namespace playden::runtime
diff --git a/tests/game_settings_test.cpp b/tests/game_settings_test.cpp
index bb97131..8f1e119 100644
--- a/tests/game_settings_test.cpp
+++ b/tests/game_settings_test.cpp
@@ -1,5 +1,6 @@
#include "runtime/game_settings.h"
#include "runtime/game_probe.h"
+#include "runtime/install_script.h"
#include "runtime/settings_publication.h"
#include <algorithm>
@@ -509,6 +510,66 @@ int main(int argc, char** argv) {
std::filesystem::remove_all(eula_root);
}
+ // 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.
+ {
+ const std::string fnv =
+ "\"InstallScript\"\n{\n \"Registry\"\n {\n"
+ " \"HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Bethesda Softworks\\\\FalloutNV\"\n {\n"
+ " \"string\"\n {\n \"Installed Path\" \"%INSTALLDIR%\\\\\"\n"
+ " }\n }\n }\n \"Run Process\"\n {\n \"DirectX\"\n {\n"
+ " \"process 1\" \"%INSTALLDIR%\\\\Redists\\\\directx\\\\DXSETUP.exe\"\n }\n }\n}\n";
+ InstallScriptValues values;
+ CHECK(ParseInstallScript(fnv, "Z:\\data\\games\\22380", &values, &reason));
+ CHECK(values.user.empty() && values.machine.size() == 2);
+ CHECK(values.machine[0].key == "SOFTWARE\\Bethesda Softworks\\FalloutNV" && values.machine[0].name == "Installed Path" &&
+ values.machine[0].data == "\"Z:\\\\data\\\\games\\\\22380\\\\\"");
+ CHECK(values.machine[1].key == "Software\\Wow6432Node\\Bethesda Softworks\\FalloutNV" &&
+ values.machine[1].data == values.machine[0].data);
+ // UTF-16 with its mark, HKCU, dwords; another %TOKEN%, other roots and other types are skipped.
+ const std::string mixed =
+ "\"InstallScript\" { \"Registry\" { \"HKEY_CURRENT_USER\\\\Software\\\\Studio\\\\Game\" { "
+ "\"dword\" { \"Installed\" \"1\" \"Bad\" \"x\" } \"string\" { \"Lang\" \"%LANGUAGE%\" \"Ok\" \"yes\" } "
+ "\"binary\" { \"Blob\" \"00ff\" } } \"HKEY_CLASSES_ROOT\\\\.sav\" { \"string\" { \"\" \"Game.Save\" } } } }";
+ std::string utf16 = "\xff\xfe";
+ for (const char value : mixed) utf16 += std::string{value, '\0'};
+ values = {};
+ CHECK(ParseInstallScript(utf16, "Z:\\g", &values, &reason));
+ CHECK(values.machine.empty() && values.user.size() == 2);
+ CHECK(values.user[0].key == "Software\\Studio\\Game" && values.user[0].name == "Installed" &&
+ values.user[0].data == "dword:00000001");
+ CHECK(values.user[1].name == "Ok" && values.user[1].data == "\"yes\"");
+ values = {};
+ CHECK(!ParseInstallScript("\"InstallScript\" {", "Z:\\g", &values, &reason));
+
+ const auto script_root = std::filesystem::canonical(std::filesystem::temp_directory_path()) /
+ ("playden-install-script-" + std::to_string(::getpid()));
+ std::filesystem::remove_all(script_root);
+ const auto game = script_root / "game", bottle = script_root / "bottle";
+ std::filesystem::create_directories(game);
+ std::filesystem::create_directories(bottle);
+ std::filesystem::permissions(script_root, std::filesystem::perms::owner_all);
+ std::filesystem::permissions(bottle, std::filesystem::perms::owner_all);
+ const std::string system_reg = "WINE REGISTRY Version 2\n;; All keys relative to REGISTRY\\\\Machine\n\n#arch=win64\n";
+ std::ofstream(bottle / "system.reg") << system_reg;
+ ::chmod((bottle / "system.reg").c_str(), 0600);
+ // No script: nothing found, nothing written.
+ auto applied = ApplyInstallScripts(game.string(), "Z:\\data\\games\\22380", bottle.string(), 1791500000);
+ CHECK(applied.ok && applied.found.empty() && !std::filesystem::exists(bottle / "system.reg.playden-backup"));
+ std::ofstream(game / "InstallScript.vdf") << fnv;
+ applied = ApplyInstallScripts(game.string(), "Z:\\data\\games\\22380", bottle.string(), 1791500000);
+ CHECK(applied.ok && applied.found == std::vector<std::string>{"InstallScript.vdf"} && applied.values == 2);
+ const auto written = ReadText(bottle / "system.reg");
+ CHECK(written.find("\n[SOFTWARE\\\\Bethesda Softworks\\\\FalloutNV] 1791500000\n"
+ "\"Installed Path\"=\"Z:\\\\data\\\\games\\\\22380\\\\\"\n") != std::string::npos);
+ CHECK(written.find("\n[Software\\\\Wow6432Node\\\\Bethesda Softworks\\\\FalloutNV] 1791500000\n") != std::string::npos);
+ CHECK(ReadText(bottle / "system.reg.playden-backup") == system_reg);
+ // Again: already there, so the hive is left as it is.
+ applied = ApplyInstallScripts(game.string(), "Z:\\data\\games\\22380", bottle.string(), 1791600000);
+ CHECK(applied.ok && ReadText(bottle / "system.reg") == written);
+ std::filesystem::remove_all(script_root);
+ }
+
// A prospero-win-profiles sidecar becomes a community preset for its game.
CommunityPreset community;
CHECK(ParseProfileSidecar(R"({"schema_version":1,"profile":"half-life-2",