Skip to content

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

C++

Stop Project Zomboid running out of open files

Leonardo Lobato348782a6b1f6
diff --git a/COMPATIBILITY.md b/COMPATIBILITY.md
index 2a875e5..6e6d6be 100644
--- a/COMPATIBILITY.md
+++ b/COMPATIBILITY.md
@@ -179,6 +179,7 @@ uses: Direct3D 8–11 goes through DXVK to Vulkan, and Direct3D 12 through vkd3d
| [Portal](https://store.steampowered.com/app/400) | ✅ Works | 0.13 | Source · D3D9 | For the menus, set Controller tuning › Pointer stick = Right stick and map the touchpad to left click in Button mapping. No button opens the options (Esc) during play. ([#18](https://github.com/leolobato/playden-ps5/issues/18), [#143](https://github.com/leolobato/playden-ps5/issues/143), firmware 13.60) |
| [Portal 2](https://store.steampowered.com/app/620) | ✅ Works | 0.13 | Source · D3D9 · 32-bit | Default settings, DualSense; gameplay confirmed. ([#36](https://github.com/leolobato/playden-ps5/issues/36), [#144](https://github.com/leolobato/playden-ps5/issues/144), firmware 13.60) |
| [Project Warlock](https://store.steampowered.com/app/893680) | ✅ Works | 0.10 | Unity · D3D11 · 64-bit | Plays the first level with the sticks. The D-pad does not move the menu focus. |
+| [Project Zomboid](https://store.steampowered.com/app/108600) | ✅ Works | 0.19 | Java (LWJGL) · OpenGL · 64-bit | Plays with the controller. If its terms screen ignores the pad the first time, use Game settings › Controller preset = Mouse. ([#194](https://github.com/leolobato/playden-ps5/issues/194)) |
| [Prototype](https://store.steampowered.com/app/10150) | ✅ Works | 0.19 | D3D9 · 32-bit | Default settings. Imported from USB. ([#554](https://github.com/leolobato/playden-ps5/issues/554)) |
| [R.E.P.O.](https://store.steampowered.com/app/3241660) | ✅ Works | 0.10 | Unity · 64-bit | Reaches the menu and plays single player with a USB mouse and keyboard; multiplayer does not work. Controller not tested. ([#8](https://github.com/leolobato/playden-ps5/issues/8), firmware 9.40) |
| Ratchet & Clank (rac-rs) | ✅ Works | 0.13 | rac-rs · Vulkan · 64-bit | Imported rac-rs 0.4.1 build. Game settings › Program = `rac-rs.exe`, Library overrides = `explorerframe=d`. ([#313](https://github.com/leolobato/playden-ps5/issues/313), firmware 12.40) |
@@ -327,7 +328,6 @@ uses: Direct3D 8–11 goes through DXVK to Vulkan, and Direct3D 12 through vkd3d
| [OMORI](https://store.steampowered.com/app/1150690) | ❌ Broken | 0.19 | RPG Maker (NW.js) · 32-bit | Opens a black window. |
| [Order of the Sinking Star](https://store.steampowered.com/app/499170) | ❌ Broken | 0.12 | — · D3D12 · vkd3d-proton · 64-bit | vkd3d-proton starts, then the game says it needs a GPU with Direct3D feature level 12_0 and quits. Also after Recreate bottle. ([#95](https://github.com/leolobato/playden-ps5/issues/95), firmware 13.60) |
| [Overwatch](https://store.steampowered.com/app/2357570) | ❌ Broken | 0.13 | — · D3D12 · 64-bit | Crashes at start. ([#183](https://github.com/leolobato/playden-ps5/pull/183), firmware 13.00) |
-| [Project Zomboid](https://store.steampowered.com/app/108600) | ❌ Broken | 0.16 | Java (LWJGL) · OpenGL · 64-bit | Stops about 20 seconds after start: it runs out of open files ([#194](https://github.com/leolobato/playden-ps5/issues/194)). |
| [Scrap Mechanic](https://store.steampowered.com/app/387990) | ❌ Broken | 0.13 | — | Stops at start because it needs a signed-in Steam session, even for single player: "Steam user must be logged in" with Offline emulation, "SteamAPI_Init() failed" with the game's own files ([#115](https://github.com/leolobato/playden-ps5/issues/115)). ([#156](https://github.com/leolobato/playden-ps5/issues/156), firmware 13.60) |
| [Severed Steel](https://store.steampowered.com/app/1227690) | ❌ Broken | 0.11 | Unreal 4 · D3D12/D3D11 · 64-bit | The main menu works with the left stick, on Direct3D 12 (vkd3d-proton) and on the *DirectX11 version* entry. Starting the campaign stays on a black screen (the game keeps running, no crash). The default entry starts too: Playden no longer adds `-dx11` next to its `-d3d12`. |
| [Shin Megami Tensei V: Vengeance Demo](https://store.steampowered.com/app/3185590) | ❌ Broken | 0.12 | Unreal 4 · D3D12 · 64-bit | The game's copy protection (Denuvo) stops it: it crashes inside the protected code about two seconds after start, before any graphics, and returns to the console home screen. Also with Direct3D 12 = **vkd3d-proton**. Tested with the demo. ([#37](https://github.com/leolobato/playden-ps5/issues/37)) |
diff --git a/cmake/HomebrewUI.cmake b/cmake/HomebrewUI.cmake
index 5044885..47f78bb 100644
--- a/cmake/HomebrewUI.cmake
+++ b/cmake/HomebrewUI.cmake
@@ -69,6 +69,7 @@ add_library(playden_steam_cm_codec STATIC
"${CMAKE_SOURCE_DIR}/src/runtime/prospero_profile.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/game_settings.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/recipe_ini.cpp"
+ "${CMAKE_SOURCE_DIR}/src/runtime/recipe_files.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/install_script.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/msi_package.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/lzx_decoder.cpp"
@@ -582,6 +583,9 @@ if(BUILD_TESTING)
add_executable(runtime_recipe_ini_test tests/runtime_recipe_ini_test.cpp)
target_link_libraries(runtime_recipe_ini_test PRIVATE playden_steam_cm_codec)
add_test(NAME runtime_recipe_ini COMMAND runtime_recipe_ini_test)
+ add_executable(runtime_recipe_files_test tests/runtime_recipe_files_test.cpp)
+ target_link_libraries(runtime_recipe_files_test PRIVATE playden_steam_cm_codec)
+ add_test(NAME runtime_recipe_files COMMAND runtime_recipe_files_test)
# Inject failures only at the POSIX I/O boundary of this test's production
# compilation; the shipped settings implementation has ordinary syscalls.
add_executable(runtime_settings_publication_test tests/runtime_settings_publication_test.cpp)
diff --git a/docs/community-recipes.md b/docs/community-recipes.md
index 0531786..8f2f9a8 100644
--- a/docs/community-recipes.md
+++ b/docs/community-recipes.md
@@ -70,6 +70,7 @@ Recipe fields:
| `registry` | Values the bottle gets under `HKEY_CURRENT_USER` when the game's files are prepared, as a Steam install script's would be: `{"Software\\Wine\\AppDefaults\\game.exe\\Direct3D": {"VideoMemorySize": "4096"}}`. Only some keys are allowed; see [Registry values](#registry-values). |
| `ini` | Values the game's own INI files get when the game's files are prepared, written only when the key is missing or empty: `{"BmGame/Config/UserEngine.ini": {"Engine.Engine": {"Language": "int"}}}`. See [INI values](#ini-values). |
| `steam_api_copies` | Shipped recipes only. Paths in the game folder that get a copy of the game's own Steam library while the emulator answers the game: `["Sandfall/Binaries/Win64/steam_api64.dll"]`. See [Steam library copies](#steam-library-copies). |
+| `game_files` | Shipped recipes only. Paths in the game folder that get a file Playden ships: `["zombie/DebugFileWatcher.class"]`. See [Game files](#game-files). |
Community fields:
@@ -183,6 +184,31 @@ Rules: the path is a `steam_api.dll` or `steam_api64.dll` in a folder of the gam
or `..` part. At most 4 paths, each once. A community recipe with this field is refused: it places a file that Game
settings doesn't show, and a community recipe is only offered, so the game wouldn't start until the player chose it.
+## Game files
+
+Use `game_files` only for a game that can't run on the console without a changed file, and only when nothing else in
+this file can do it. Project Zomboid watches its folders for changes by holding one handle on each of its 1704 media
+folders. A PS5 title can hold about 250 open files, so the game quits while it starts. The game looks for classes in
+its own folder first, so a watcher that watches nothing stands in for the game's. That only stops the game reloading
+files that change while it runs.
+
+Playden copies each file from `runtime/recipes/files/<recipe id>/<path>` to `<path>` in the game folder when it
+prepares the game or applies its settings:
+
+- A file already at the path stays, whatever it holds. A game version that ships its own keeps it, and a newer file
+ from Playden doesn't replace an older one there.
+- Missing folders are made. A link or a file in the way of a folder is left alone and the file is skipped.
+- Without the file in Playden's package, or with something in the way, the game starts without it. Playden logs
+ `recipe files ... skipped=`.
+- A game that Playden prepared with the recipe it made itself also gets the files of a recipe that Playden ships for
+ the game later.
+- Uninstalling the game removes the file with the rest of the folder.
+
+Rules: the path is inside the game folder, with `/` between folders and no `.` or `..` part. At most 4 paths, each
+once. A community recipe with this field is refused, for the same reason as `steam_api_copies`. The source of a file
+that isn't plain text goes in `tools/recipe-files/<recipe id>`, with what builds it. Write it yourself: no part of
+the game goes in Playden's repository.
+
## Shipped recipes
A recipe Playden ships lives in `runtime/recipes/` and is used for that game
diff --git a/runtime/recipes/files/steam-108600-projectzomboid-v1/zombie/DebugFileWatcher.class b/runtime/recipes/files/steam-108600-projectzomboid-v1/zombie/DebugFileWatcher.class
new file mode 100644
index 0000000..75ca9e6
Binary files /dev/null and b/runtime/recipes/files/steam-108600-projectzomboid-v1/zombie/DebugFileWatcher.class differ
diff --git a/runtime/recipes/steam-108600-projectzomboid-v1.json b/runtime/recipes/steam-108600-projectzomboid-v1.json
index b9540b9..df1f231 100644
--- a/runtime/recipes/steam-108600-projectzomboid-v1.json
+++ b/runtime/recipes/steam-108600-projectzomboid-v1.json
@@ -17,5 +17,6 @@
"steam_features": "emulate",
"windows_version": "win7"
},
- "locked": []
+ "locked": [],
+ "game_files": ["zombie/DebugFileWatcher.class"]
}
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cpp
index 0ee01b8..7d1cd1a 100644
--- a/src/homebrew/runtime_host.cpp
+++ b/src/homebrew/runtime_host.cpp
@@ -10,6 +10,7 @@
#include "runtime/prospero_profile.h"
#include "runtime/settings_publication_journal.h"
#include "runtime/dotnet_heap.h"
+#include "runtime/recipe_files.h"
#include "runtime/recipe_ini.h"
#include "runtime/game_probe.h"
#include "runtime/install_script.h"
@@ -715,6 +716,8 @@ struct RuntimeHost::GameFiles {
const std::vector<rt::IniValue>* ini = nullptr;
// Likewise, the steam_api DLL copies to keep.
const std::vector<std::string>* steam_api_copies = nullptr;
+ // Likewise, the files Playden ships for the game, and the recipe that names them.
+ const rt::GameRecipe* game_files_recipe = nullptr;
};
namespace {
@@ -1179,6 +1182,20 @@ std::string RuntimeHost::RecipeIniStep(const GameFiles& files) {
return applied.ok ? std::string() : applied.message;
}
+// Generic step: files Playden ships for a game that can't run on the console without them (docs/community-recipes.md).
+std::string RuntimeHost::RecipeFilesStep(const GameFiles& files) {
+ if (!files.game_files_recipe || files.game_files_recipe->game_files.empty()) return {};
+ const auto& recipe = *files.game_files_recipe;
+ const auto applied = rt::ApplyRecipeFiles(files.root.string(), config_.runtime_dir + "/recipes/files/" + recipe.id, recipe.game_files);
+ std::string changed, skipped;
+ for (const auto& file : applied.changed) changed += (changed.empty() ? "" : ",") + file;
+ for (const auto& file : applied.skipped) skipped += (skipped.empty() ? "" : ",") + file;
+ Log("recipe files app=" + std::to_string(files.record.runtime_number) + " files=" + std::to_string(recipe.game_files.size()) +
+ " changed=" + (changed.empty() ? "-" : changed) + (skipped.empty() ? std::string() : " skipped=" + skipped) +
+ (applied.ok ? std::string() : " failed=" + applied.message));
+ return applied.ok ? std::string() : applied.message;
+}
+
// Generic step: 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).
std::string RuntimeHost::DotnetHeapStep(const GameFiles& files) {
@@ -1193,8 +1210,8 @@ 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, Wine
-// Mono, the bottle's Environment, MFC, the Visual C++ runtime key, the recipe's registry and INI values, the .NET
-// heap) and Steam-file ones, which act only on the Steam files a game has (SteamStub, Steamworks and steam.dll,
+// Mono, the bottle's Environment, MFC, the Visual C++ runtime key, the recipe's registry and INI values and files, 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);
@@ -1204,12 +1221,14 @@ std::string RuntimeHost::ApplyPreparationSteps(Composition& c, const rt::Runtime
if (!files.recipe) return {}; // publication reports the missing recipe
files.ini = &files.recipe->ini;
files.steam_api_copies = &files.recipe->steam_api_copies;
+ files.game_files_recipe = files.recipe;
if (rt::IsGeneratedRecipeId(files.recipe->id))
for (const auto& candidate : c.recipes)
if (rt::RecipeGame(candidate) == GameOf(record) && candidate.app_id == record.runtime_number &&
!rt::IsGeneratedRecipeId(candidate.id)) {
files.ini = &candidate.ini;
files.steam_api_copies = &candidate.steam_api_copies;
+ files.game_files_recipe = &candidate;
}
files.saved = config_.settings_record ? config_.settings_record(GameOf(record)) : std::nullopt;
const rt::SettingsPreset* preset = nullptr;
@@ -1249,6 +1268,7 @@ std::string RuntimeHost::ApplyPreparationSteps(Composition& c, const rt::Runtime
VcRuntimeStep(files);
if (auto refused = RecipeRegistryStep(files); !refused.empty()) return refused;
if (auto refused = RecipeIniStep(files); !refused.empty()) return refused;
+ if (auto refused = RecipeFilesStep(files); !refused.empty()) return refused;
return DotnetHeapStep(files);
}
diff --git a/src/homebrew/runtime_host.hpp b/src/homebrew/runtime_host.hpp
index eda116b..1c49ee0 100644
--- a/src/homebrew/runtime_host.hpp
+++ b/src/homebrew/runtime_host.hpp
@@ -642,6 +642,7 @@ class RuntimeHost {
std::string BottleEnvironmentStep(const GameFiles& files);
std::string RecipeRegistryStep(const GameFiles& files);
std::string RecipeIniStep(const GameFiles& files);
+ std::string RecipeFilesStep(const GameFiles& files);
std::string DotnetHeapStep(const GameFiles& files);
void MfcStep(const GameFiles& files); // an upgrade: never stops the start
// Steam-file steps.
diff --git a/src/runtime/game_settings.cpp b/src/runtime/game_settings.cpp
index bb07221..07d5644 100644
--- a/src/runtime/game_settings.cpp
+++ b/src/runtime/game_settings.cpp
@@ -1444,10 +1444,10 @@ bool RecipeRegistryValueAllowed(const std::string& key, const std::string& name,
constexpr std::size_t kMaxRecipeIniValues = 16;
constexpr std::size_t kMaxRecipeSteamApiCopies = 4;
+constexpr std::size_t kMaxRecipeGameFiles = 4;
-// A path in steam_api_copies (docs/community-recipes.md): a steam_api DLL inside the game folder, named with '/' and
-// no "." or ".." part.
-bool RecipeSteamApiCopyAllowed(const std::string& path) {
+// A path inside the game folder, named with '/' and no "." or ".." part; *name gets its last part.
+bool RecipePathInGameFolder(const std::string& path, std::string* name) {
if (path.empty() || path.size() > 200 || path.find_first_of("\\:*?\"<>|") != std::string::npos) return false;
for (const unsigned char c : path)
if (c < 0x20 || c > 0x7e) return false;
@@ -1458,6 +1458,14 @@ bool RecipeSteamApiCopyAllowed(const std::string& path) {
if (part.empty() || part == "." || part == "..") return false;
start = end + 1;
}
+ *name = part;
+ return true;
+}
+
+// A path in steam_api_copies (docs/community-recipes.md): a steam_api DLL in a folder of the game.
+bool RecipeSteamApiCopyAllowed(const std::string& path) {
+ std::string part;
+ if (!RecipePathInGameFolder(path, &part)) return false;
const auto name = LowerAscii(part);
return path.find('/') != std::string::npos && (name == "steam_api.dll" || name == "steam_api64.dll");
}
@@ -1657,6 +1665,20 @@ bool ParseGameRecipe(std::string_view text, GameRecipe* recipe, std::string* rea
out.steam_api_copies.push_back(*item.string());
}
}
+ if (const auto* game_files = root->get("game_files")) {
+ if (!game_files->array()) return fail("game_files must be a list");
+ // Like steam_api_copies, only Playden's own recipes place a file the player can't see in Game settings.
+ if (IsCommunityRecipeId(out.id)) return fail("a community recipe can't carry game_files");
+ std::set<std::string> identities;
+ for (const auto& item : *game_files->array()) {
+ std::string name;
+ if (!item.string() || !RecipePathInGameFolder(*item.string(), &name))
+ return fail("game_files needs paths inside the game folder");
+ if (out.game_files.size() >= kMaxRecipeGameFiles) return fail("game_files has too many paths");
+ if (!identities.insert(LowerAscii(*item.string())).second) return fail("game_files names a path twice");
+ out.game_files.push_back(*item.string());
+ }
+ }
if (out.dll_overrides.size() && !IsProsperoDllOverrides(out.dll_overrides)) return fail("recipe dll_overrides are invalid");
if (out.architecture != "pe32" && out.architecture != "pe64") return fail("architecture must be pe32 or pe64");
for (const auto& [id, value] : out.defaults) {
@@ -1846,6 +1868,12 @@ std::string SerializeGameRecipe(const GameRecipe& recipe) {
text += (index ? "," : "") + quoted(recipe.steam_api_copies[index]);
text += "]";
}
+ if (!recipe.game_files.empty()) {
+ text += ",\"game_files\":[";
+ for (std::size_t index = 0; index < recipe.game_files.size(); ++index)
+ text += (index ? "," : "") + quoted(recipe.game_files[index]);
+ text += "]";
+ }
return text + "}\n";
}
@@ -3574,6 +3602,7 @@ bool ParseCommunityRecipe(std::string_view text, CommunityRecipe* community, std
CommunityRecipe out;
if (!ParseGameRecipe(text, &out.recipe, reason)) return false;
if (!out.recipe.steam_api_copies.empty()) return fail("a community recipe can't carry steam_api_copies");
+ if (!out.recipe.game_files.empty()) return fail("a community recipe can't carry game_files");
const auto root = json::parse(text);
std::uint64_t appid = 0;
if (!ExactUnsigned(root->get("steam_appid"), std::numeric_limits<std::uint32_t>::max(), &appid, false) ||
diff --git a/src/runtime/game_settings.h b/src/runtime/game_settings.h
index 510f377..855aae5 100644
--- a/src/runtime/game_settings.h
+++ b/src/runtime/game_settings.h
@@ -192,6 +192,9 @@ struct GameRecipe {
// Where the game's own steam_api DLL must also be on disk while the emulator answers it, for a game that hashes the
// file it finds there (docs/community-recipes.md). Shipped recipes only.
std::vector<std::string> steam_api_copies;
+ // Files Playden ships in runtime/recipes/files/<recipe id>, placed at these paths in the game folder when its files
+ // are prepared, for a game that can't run on the console without them (runtime/recipe_files.h). Shipped recipes only.
+ std::vector<std::string> game_files;
// What the kit's template bottle must provide ("audio": "ps5"); see UnmetRecipeRequirements.
std::map<std::string, std::string> requirements;
};
diff --git a/src/runtime/recipe_files.cpp b/src/runtime/recipe_files.cpp
new file mode 100644
index 0000000..bfeef66
--- /dev/null
+++ b/src/runtime/recipe_files.cpp
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "runtime/recipe_files.h"
+
+#include <cerrno>
+#include <fcntl.h>
+#include <filesystem>
+#include <optional>
+#include <string_view>
+#include <sys/stat.h>
+#include <unistd.h>
+
+namespace playden::runtime {
+namespace {
+namespace fs = std::filesystem;
+
+constexpr off_t kMaxRecipeFile = 1024 * 1024;
+
+std::optional<std::string> ReadRegular(const fs::path& path) {
+ const int file = ::open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
+ if (file < 0) return std::nullopt;
+ struct stat info {};
+ std::string bytes;
+ bool ok = ::fstat(file, &info) == 0 && S_ISREG(info.st_mode) && info.st_size <= kMaxRecipeFile;
+ if (ok) {
+ bytes.resize(static_cast<std::size_t>(info.st_size));
+ std::size_t done = 0;
+ while (ok && done < bytes.size()) {
+ const auto count = ::read(file, bytes.data() + done, bytes.size() - done);
+ if (count < 0 && errno == EINTR) continue;
+ if (count <= 0) ok = false;
+ else done += static_cast<std::size_t>(count);
+ }
+ }
+ ::close(file);
+ if (!ok) return std::nullopt;
+ return bytes;
+}
+
+// Game files are open to FTP, whatever drive they are on, and the title can't set its umask.
+constexpr mode_t kFolderMode = 0777;
+constexpr mode_t kFileMode = 0666;
+
+enum class Folders { kReady, kInTheWay, kFailed };
+
+Folders MakeFoldersTo(const fs::path& root, const std::string& relative) {
+ auto folder = root;
+ for (const auto& part : fs::path(relative).parent_path()) {
+ folder /= part;
+ struct stat info {};
+ if (::lstat(folder.c_str(), &info) == 0) {
+ if (!S_ISDIR(info.st_mode)) return Folders::kInTheWay;
+ continue;
+ }
+ if (errno != ENOENT) return Folders::kFailed;
+ if (::mkdir(folder.c_str(), kFolderMode) != 0 || ::chmod(folder.c_str(), kFolderMode) != 0) return Folders::kFailed;
+ }
+ return Folders::kReady;
+}
+
+bool WriteAtomic(const fs::path& path, std::string_view bytes) {
+ const auto temporary = path.parent_path() / (".playden-" + path.filename().string() + ".tmp");
+ ::unlink(temporary.c_str());
+ const int file = ::open(temporary.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, kFileMode);
+ if (file < 0) return false;
+ std::size_t done = 0;
+ bool ok = true;
+ while (ok && done < bytes.size()) {
+ const auto count = ::write(file, bytes.data() + done, bytes.size() - done);
+ if (count < 0 && errno == EINTR) continue;
+ if (count <= 0) ok = false;
+ else done += static_cast<std::size_t>(count);
+ }
+ ok = ok && ::fchmod(file, kFileMode) == 0 && ::fsync(file) == 0;
+ ok = ::close(file) == 0 && ok;
+ if (!ok || ::rename(temporary.c_str(), path.c_str()) != 0) {
+ ::unlink(temporary.c_str());
+ return false;
+ }
+ return true;
+}
+} // namespace
+
+RecipeFilesResult ApplyRecipeFiles(const std::string& game_root, const std::string& source_dir,
+ const std::vector<std::string>& paths) {
+ RecipeFilesResult result;
+ const fs::path root(game_root);
+ for (const auto& relative : paths) {
+ const auto path = root / relative;
+ struct stat info {};
+ if (::lstat(path.c_str(), &info) == 0) {
+ // Playden's own from an earlier start needs nothing; anything else is the game's.
+ const auto current = S_ISREG(info.st_mode) ? ReadRegular(path) : std::nullopt;
+ const auto source = ReadRegular(fs::path(source_dir) / relative);
+ if (!current || !source || *current != *source) result.skipped.push_back(relative + ": the game has its own");
+ continue;
+ }
+ const auto source = ReadRegular(fs::path(source_dir) / relative);
+ if (!source) {
+ result.skipped.push_back(relative + ": Playden has no copy");
+ continue;
+ }
+ const auto folders = MakeFoldersTo(root, relative);
+ if (folders == Folders::kInTheWay) {
+ result.skipped.push_back(relative + ": something else is in the way of its folder");
+ continue;
+ }
+ if (folders == Folders::kFailed || !WriteAtomic(path, *source)) {
+ result.ok = false;
+ result.message = "Playden could not put a file this game needs at " + relative + ".";
+ return result;
+ }
+ result.changed.push_back(relative);
+ }
+ return result;
+}
+
+} // namespace playden::runtime
diff --git a/src/runtime/recipe_files.h b/src/runtime/recipe_files.h
new file mode 100644
index 0000000..1929b73
--- /dev/null
+++ b/src/runtime/recipe_files.h
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#pragma once
+
+#include <string>
+#include <vector>
+
+namespace playden::runtime {
+
+// A file Playden ships for one game, placed in the game's folder when its files are prepared (GameRecipe::game_files),
+// for a game that can't run on the console without it. Project Zomboid opens a handle on each of its 1704 media folders
+// to watch them for changes, past what a title can hold open, and quits; its class path looks in the game's folder
+// first, so a watcher that watches nothing can stand in for the game's.
+
+struct RecipeFilesResult {
+ bool ok = true;
+ std::string message; // on failure: a sentence for the player
+ std::vector<std::string> changed; // files (relative) that were written
+ std::vector<std::string> skipped; // "<file>: <why>" for files left alone
+};
+
+// Copies each of paths from source_dir to the same relative path in game_root, making its missing folders. A file
+// already there stays, whatever it holds: an update that brings its own file must not lose it. Without its source, or
+// with a link or a file in the way of a folder, a file is skipped: the game then starts, or stops, as it would have.
+// Links are never followed. Idempotent; the game must be stopped.
+RecipeFilesResult ApplyRecipeFiles(const std::string& game_root, const std::string& source_dir,
+ const std::vector<std::string>& paths);
+
+} // namespace playden::runtime
diff --git a/tests/game_settings_test.cpp b/tests/game_settings_test.cpp
index 5f6dfcf..0d7cf4a 100644
--- a/tests/game_settings_test.cpp
+++ b/tests/game_settings_test.cpp
@@ -1621,6 +1621,34 @@ int main(int argc, char** argv) {
shared.replace(shared.size() - 1, 1, R"(,"steam_appid":1903340,"executable":"g.exe","community":{"format_version":1,"status":"works"}})");
CHECK(!ParseCommunityRecipe(shared, &community, &reason) && reason.find("steam_api_copies") != std::string::npos);
}
+ // Project Zomboid runs out of open files watching its folders, so Playden ships a watcher that watches nothing.
+ {
+ GameRecipe zomboid;
+ CHECK(ParseGameRecipe(ReadText(source / "runtime/recipes/steam-108600-projectzomboid-v1.json"), &zomboid, &reason));
+ CHECK(zomboid.game_files == std::vector<std::string>{"zombie/DebugFileWatcher.class"});
+ const auto shipped = ReadText(source / "runtime/recipes/files/steam-108600-projectzomboid-v1/zombie/DebugFileWatcher.class");
+ CHECK(shipped.size() > 4 && shipped.compare(0, 4, "\xCA\xFE\xBA\xBE") == 0);
+ // Java 17, so that it loads on every Java the game could bring.
+ CHECK(static_cast<unsigned char>(shipped[6]) == 0 && static_cast<unsigned char>(shipped[7]) == 61);
+ // The field survives a round trip, and only names a path inside the game's folders.
+ GameRecipe again;
+ CHECK(ParseGameRecipe(SerializeGameRecipe(zomboid), &again, &reason) && again.game_files == zomboid.game_files);
+ const auto with_files = [](const std::string& paths, const std::string& id = "steam-108600-test-v1") {
+ return R"({"recipe_id":")" + id + R"(","recipe_version":1,"app_id":108600,"launch_entries":[{"id":"game","label":"G","executable":"g.exe"}],)"
+ R"("architecture":"pe64","runtime":"wine-wow64","prefix":"game","graphics":"opengl","game_files":)" + paths + "}";
+ };
+ GameRecipe parsed;
+ CHECK(ParseGameRecipe(with_files(R"(["a.class","b/c.class"])"), &parsed, &reason));
+ for (const auto* bad : {R"("a.class")", R"(["../a.class"])", R"(["b/../a.class"])", R"(["/a.class"])", R"(["b\\a.class"])",
+ R"([""])", R"([1])", R"(["a.class","A.CLASS"])", R"(["a","b","c","d","e"])"})
+ CHECK(!ParseGameRecipe(with_files(bad), &parsed, &reason));
+ // A community recipe can't place a file in the game folder.
+ CHECK(!ParseGameRecipe(with_files(R"(["a.class"])", "community-108600-test"), &parsed, &reason));
+ CommunityRecipe community;
+ auto shared = with_files(R"(["a.class"])");
+ shared.replace(shared.size() - 1, 1, R"(,"steam_appid":108600,"executable":"g.exe","community":{"format_version":1,"status":"works"}})");
+ CHECK(!ParseCommunityRecipe(shared, &community, &reason) && reason.find("game_files") != std::string::npos);
+ }
// Batman: Arkham City is Asylum's sibling: BatmanAC.exe starts BmLauncher.exe and exits too while UserEngine.ini has
// no Language.
{
diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cpp
index 6e43e4c..8d1d0ce 100644
--- a/tests/homebrew_runtime_host_test.cpp
+++ b/tests/homebrew_runtime_host_test.cpp
@@ -4464,6 +4464,77 @@ void GeneratedRecipeGetsShippedSteamApiCopy() {
host.Shutdown();
}
+// Project Zomboid prepared before Playden shipped its recipe keeps the recipe Playden generated, and still gets the
+// file the shipped recipe names the next time it is played. A game that brings its own keeps it.
+void GeneratedRecipeGetsShippedGameFile() {
+ Fixture fixture;
+ auto config = fixture.Config();
+ const auto runtime = fixture.root / "runtime";
+ Write(runtime / "presets.json", Read(fs::path(g_runtime_dir) / "presets.json"), 0644);
+ Write(runtime / "recipes/steam-220-hl2-v1.json", Read(fs::path(g_runtime_dir) / "recipes/steam-220-hl2-v1.json"), 0644);
+ Write(runtime / "recipes/recipes.lst", "steam-220-hl2-v1.json\n", 0644);
+ config.runtime_dir = runtime.string();
+ config.steamworks_dir = (fs::path(g_runtime_dir) / "steamworks").string();
+ config.host_namespace_prefix = fixture.root;
+ 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}; };
+ const auto game = fixture.root / "data/homebrew/FAKE77031/games/108600";
+ std::string pe(0x80 + 26, '\0');
+ pe[0] = 'M'; pe[1] = 'Z'; pe[0x3c] = 0x40; pe[0x40] = 'P'; pe[0x41] = 'E';
+ pe[0x44] = 0x64; pe[0x45] = static_cast<char>(0x86); pe[0x40 + 24] = 0x0b; pe[0x40 + 25] = 0x02;
+ const std::string placed = "zombie/DebugFileWatcher.class";
+ Write(game / "ProjectZomboid64.exe", pe, 0644);
+ CHECK(chmod(game.c_str(), 0755) == 0);
+ RuntimePrepareRequest request{108600, "/data/homebrew/FAKE77031/games/108600", "internal", "Project Zomboid"};
+ request.launches = {{"", "", "ProjectZomboid64.exe", "", ""}};
+ const auto prepared = [](const RuntimeSnapshot& s) {
+ return !s.preparing_app && (s.refusals.count(108600) || (!s.installs.empty() && s.installs[0].state == PreparationState::kReady));
+ };
+ {
+ RuntimeHost host(config);
+ CHECK(host.Start());
+ CHECK(WaitFor(host, Started).kit_available);
+ host.Prepare(request);
+ const auto done = WaitFor(host, prepared, 60);
+ if (done.refusals.count(108600)) std::fprintf(stderr, "Prepare refused: %s\n", done.refusals.at(108600).c_str());
+ CHECK(!done.refusals.count(108600) && done.installs.size() == 1);
+ CHECK(Read(config.state_root / "installs.json").find("\"recipe_id\":\"steam-108600-auto\"") != std::string::npos);
+ CHECK(!fs::exists(game / placed));
+ host.Shutdown();
+ }
+ // Playden now ships the recipe and the file: the game keeps its own recipe, and its folder gets the file.
+ const auto shipped = Read(fs::path(g_runtime_dir) / "recipes/files/steam-108600-projectzomboid-v1" / placed);
+ Write(runtime / "recipes/steam-108600-projectzomboid-v1.json",
+ Read(fs::path(g_runtime_dir) / "recipes/steam-108600-projectzomboid-v1.json"), 0644);
+ Write(runtime / "recipes/files/steam-108600-projectzomboid-v1" / placed, shipped, 0644);
+ Write(runtime / "recipes/recipes.lst", "steam-220-hl2-v1.json\nsteam-108600-projectzomboid-v1.json\n", 0644);
+ RuntimeHost host(config);
+ CHECK(host.Start());
+ CHECK(WaitFor(host, Started).kit_available);
+ host.Play(108600);
+ WaitFor(host, [](const RuntimeSnapshot& s) { return s.plays_handled > 0; }, 30);
+ CHECK(Read(config.state_root / "installs.json").find("\"recipe_id\":\"steam-108600-auto\"") != std::string::npos);
+ CHECK(Read(game / placed) == shipped);
+ Write(game / placed, "the game's own", 0644);
+ host.Play(108600);
+ WaitFor(host, [](const RuntimeSnapshot& s) { return s.plays_handled > 1; }, 30);
+ CHECK(Read(game / placed) == "the game's own");
+ host.Shutdown();
+}
+
// A shipped recipe's requirements are checked against the kit's template, as the kit's packager
// checks the kit's own recipe: without PS5 audio in the template, a recipe that needs it is refused.
void ShippedRecipeRequirementsFollowTheKit(bool template_audio_ps5) {
@@ -6545,6 +6616,7 @@ int main(int argc, char** argv) {
ShippedRecipeWritesGameIni(true);
GeneratedRecipeGetsShippedIni();
GeneratedRecipeGetsShippedSteamApiCopy();
+ GeneratedRecipeGetsShippedGameFile();
RunsSetupProgramOnce();
ShippedRecipeRequirementsFollowTheKit(false);
ShippedRecipeRequirementsFollowTheKit(true);
diff --git a/tests/runtime_recipe_files_test.cpp b/tests/runtime_recipe_files_test.cpp
new file mode 100644
index 0000000..bd1a96f
--- /dev/null
+++ b/tests/runtime_recipe_files_test.cpp
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "runtime/recipe_files.h"
+
+#include <cstdio>
+#include <cstdlib>
+#include <filesystem>
+#include <fstream>
+#include <iterator>
+#include <string>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define CHECK(condition) \
+ do { \
+ if (!(condition)) { \
+ std::fprintf(stderr, "%s:%d: CHECK failed: %s\n", __FILE__, __LINE__, #condition); \
+ std::exit(1); \
+ } \
+ } while (false)
+
+namespace fs = std::filesystem;
+using namespace playden::runtime;
+
+namespace {
+void Write(const fs::path& path, const std::string& bytes) {
+ fs::create_directories(path.parent_path());
+ std::ofstream(path, std::ios::binary) << bytes;
+}
+std::string Read(const fs::path& path) {
+ std::ifstream in(path, std::ios::binary);
+ return {std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
+}
+mode_t Mode(const fs::path& path) {
+ struct stat info {};
+ CHECK(::lstat(path.c_str(), &info) == 0);
+ return info.st_mode & 07777;
+}
+} // namespace
+
+int main() {
+ char pattern[] = "/private/tmp/playden-recipe-files-XXXXXX";
+ const fs::path base = ::mkdtemp(pattern);
+ const auto source = base / "source";
+ const std::string relative = "zombie/DebugFileWatcher.class";
+ Write(source / relative, "ours");
+
+ // Missing folders are made, open to FTP like the rest of the game's files, whatever the umask.
+ {
+ const auto game = base / "new";
+ fs::create_directories(game);
+ const auto mask = ::umask(022);
+ const auto result = ApplyRecipeFiles(game.string(), source.string(), {relative});
+ ::umask(mask);
+ CHECK(result.ok && result.changed == std::vector<std::string>{relative} && result.skipped.empty());
+ CHECK(Read(game / relative) == "ours");
+ CHECK(Mode(game / "zombie") == 0777 && Mode(game / relative) == 0666);
+ CHECK(!fs::exists(game / "zombie/.playden-DebugFileWatcher.class.tmp"));
+ // A second run has nothing to do and nothing to report.
+ const auto again = ApplyRecipeFiles(game.string(), source.string(), {relative});
+ CHECK(again.ok && again.changed.empty() && again.skipped.empty());
+ }
+ // A file already there that isn't Playden's stays: a game version that ships its own must keep it.
+ {
+ const auto game = base / "own";
+ Write(game / relative, "the game's");
+ const auto result = ApplyRecipeFiles(game.string(), source.string(), {relative});
+ CHECK(result.ok && result.changed.empty() && result.skipped.size() == 1);
+ CHECK(Read(game / relative) == "the game's");
+ }
+ // Links are not followed, as the file or as a folder on the way to it.
+ {
+ const auto game = base / "file-link";
+ const auto outside = base / "outside-file";
+ Write(outside, "outside");
+ fs::create_directories(game / "zombie");
+ fs::create_symlink(outside, game / relative);
+ const auto result = ApplyRecipeFiles(game.string(), source.string(), {relative});
+ CHECK(result.ok && result.changed.empty() && result.skipped.size() == 1 && Read(outside) == "outside");
+ }
+ {
+ const auto game = base / "folder-link";
+ const auto outside = base / "outside-folder";
+ fs::create_directories(game);
+ fs::create_directories(outside);
+ fs::create_directory_symlink(outside, game / "zombie");
+ const auto result = ApplyRecipeFiles(game.string(), source.string(), {relative});
+ CHECK(result.ok && result.changed.empty() && result.skipped.size() == 1);
+ CHECK(!fs::exists(outside / "DebugFileWatcher.class"));
+ }
+ // A file in the way of the folder stays too.
+ {
+ const auto game = base / "file-in-the-way";
+ Write(game / "zombie", "a file");
+ const auto result = ApplyRecipeFiles(game.string(), source.string(), {relative});
+ CHECK(result.ok && result.changed.empty() && result.skipped.size() == 1 && Read(game / "zombie") == "a file");
+ }
+ // A package without the source starts the game as it would have, with no empty folder left behind.
+ {
+ const auto game = base / "no-source";
+ fs::create_directories(game);
+ const auto result = ApplyRecipeFiles(game.string(), (base / "nothing").string(), {relative});
+ CHECK(result.ok && result.changed.empty() && result.skipped.size() == 1 && !fs::exists(game / "zombie"));
+ }
+ // A folder that can't be written to fails with a sentence for the player. Root writes anyway.
+ if (::geteuid() != 0) {
+ const auto game = base / "read-only";
+ fs::create_directories(game);
+ CHECK(::chmod(game.c_str(), 0555) == 0);
+ const auto result = ApplyRecipeFiles(game.string(), source.string(), {relative});
+ CHECK(::chmod(game.c_str(), 0755) == 0);
+ CHECK(!result.ok && result.message.find(relative) != std::string::npos);
+ }
+ fs::remove_all(base);
+ return 0;
+}
diff --git a/tools/recipe-files/steam-108600-projectzomboid-v1/build.sh b/tools/recipe-files/steam-108600-projectzomboid-v1/build.sh
new file mode 100755
index 0000000..c2b94bf
--- /dev/null
+++ b/tools/recipe-files/steam-108600-projectzomboid-v1/build.sh
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+# Builds the class in runtime/recipes/files from the source here, with a JDK 17 or newer. A Java 17 class also loads on
+# the Java 25 the game ships with now.
+# SPDX-License-Identifier: GPL-3.0-or-later
+set -euo pipefail
+here=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
+out=$(mktemp -d)
+trap 'rm -rf "$out"' EXIT
+javac --release 17 -g:none -proc:none -d "$out" "$here/zombie/DebugFileWatcher.java" "$here/zombie/PredicatedFileWatcher.java"
+cp "$out/zombie/DebugFileWatcher.class" "$here/../../../runtime/recipes/files/steam-108600-projectzomboid-v1/zombie/"
diff --git a/tools/recipe-files/steam-108600-projectzomboid-v1/zombie/DebugFileWatcher.java b/tools/recipe-files/steam-108600-projectzomboid-v1/zombie/DebugFileWatcher.java
new file mode 100644
index 0000000..fe55ebf
--- /dev/null
+++ b/tools/recipe-files/steam-108600-projectzomboid-v1/zombie/DebugFileWatcher.java
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Stands in for Project Zomboid's folder watcher, which holds a handle on each of the game's 1704 media folders: more
+// than a PS5 title can hold open. It only reloads files changed while the game runs. This one watches nothing.
+package zombie;
+
+public final class DebugFileWatcher {
+ public static final DebugFileWatcher instance = new DebugFileWatcher();
+ private boolean initialized;
+
+ private DebugFileWatcher() {
+ }
+
+ public void init() {
+ initialized = true;
+ }
+
+ public boolean isInitialized() {
+ return initialized;
+ }
+
+ public void add(PredicatedFileWatcher watcher) {
+ }
+
+ public void addDirectory(String path) {
+ }
+
+ public void addDirectoryRecurse(String path) {
+ }
+
+ public void remove(PredicatedFileWatcher watcher) {
+ }
+
+ public void update() {
+ }
+}
diff --git a/tools/recipe-files/steam-108600-projectzomboid-v1/zombie/PredicatedFileWatcher.java b/tools/recipe-files/steam-108600-projectzomboid-v1/zombie/PredicatedFileWatcher.java
new file mode 100644
index 0000000..7ff0632
--- /dev/null
+++ b/tools/recipe-files/steam-108600-projectzomboid-v1/zombie/PredicatedFileWatcher.java
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Only so that DebugFileWatcher compiles without the game. Not shipped: the game has its own.
+package zombie;
+
+public final class PredicatedFileWatcher {
+}