From 5d1e70cd203046d90cb2ef4c953386551a31206a Mon Sep 17 00:00:00 2001 From: coffeemeist3r Date: Wed, 21 Oct 2026 23:57:09 -0500 Subject: [PATCH] Keep FEX's TSO on for Unity games A FEX preset that turns TSO off (Performance, Extreme, Extreme-gn, Denuvo) breaks Unity players under FEX: - Megabonk (Unity 2023.2, IL2CPP, native Linux build, "Linux x86 (FEX)") froze on about half of its launches, every thread parked on a futex. With TSO on: 5 of 5 starts, and a played run stayed responsive. - Crab Game (Windows IL2CPP build under Proton arm64) crashed on 1 of 5 starts with an access violation in UnityPlayer.dll on a garbage pointer. With TSO on: 5 of 5 starts. A Unity player (UnityPlayer.dll, UnityPlayer.so or GameAssembly.so beside the executable) now keeps FEX_TSOENABLED=1 over the shared preset, the way .NET games already keep full TSO. The FEX tool applies the environment half of the game-file fixes too, so native Linux builds get it as well. A FEX preset chosen for the game itself still wins. Related: #1386 (its Mono crash happens with TSO already on) --- docs/development/game-environment.md | 6 +++++ .../overlay/usr/local/bin/droiddeck-fex | 20 +++++++++++--- .../overlay/usr/local/bin/droiddeck-game-env | 9 +++++++ tools/tests/test_droiddeck_fex.py | 26 +++++++++++++++++++ tools/tests/test_game_environment.py | 21 +++++++++++++++ 5 files changed, 78 insertions(+), 4 deletions(-) diff --git a/docs/development/game-environment.md b/docs/development/game-environment.md index a6e02b8e2..62530ca48 100644 --- a/docs/development/game-environment.md +++ b/docs/development/game-environment.md @@ -52,6 +52,12 @@ environment entries and is intentionally narrower than **Reset game profile**. Values are literal strings, with no shell expansion; quotes are only needed when the consuming program expects them. +A Unity game under FEX (`UnityPlayer.dll` for Windows builds under Proton, +`UnityPlayer.so` or `GameAssembly.so` for native Linux builds started by +**Linux x86 (FEX)**) keeps FEX's TSO on when the Default profile's FEX preset +turns it off: without TSO such games can deadlock or crash (seen with Megabonk +and Crab Game). A FEX preset chosen for the game itself still applies. + ## Defaults and available suggestions The initial configuration enables Mesa shader caching with diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex b/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex index 331dcfecc..0c05d4089 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex @@ -671,15 +671,27 @@ def launch(command, env, mode, decide=None): return os.execve(fex["bin"], [fex["bin"], program] + command[1:], guest) -def game_environment(env): +def game_environment(env, command): try: game_env = runpy.run_path(os.path.join(BIN_DIR, "droiddeck-game-env")) env = game_env["game_gpu"](env) app = game_env["game_id"](env) or env.get("SteamAppId") or env.get("SteamGameId") if not app: return env - with open(GAME_ENVIRONMENT, encoding="utf-8") as source: - return game_env["apply_config"](env, json.load(source), str(app)) + fixes = {} + game = game_env["Game"](resolve(command[0], env), command[1:]) + for check in game_env["FIXES"]: + found = check(game) + # Environment fixes only: the launch arguments some fixes add were chosen for Windows builds. + if found and found[0]: + fixes.update(found[0]) + say(found[2]) + try: + with open(GAME_ENVIRONMENT, encoding="utf-8") as source: + config = json.load(source) + except FileNotFoundError: + config = {"version": 1} + return game_env["apply_config"](env, config, str(app), fixes) except FileNotFoundError: return env except Exception as error: @@ -810,7 +822,7 @@ def main(argv): return 0 if not command: return 2 - return launch(command, game_environment(dict(os.environ)), os.environ.get("DROIDDECK_FEX", "auto")) + return launch(command, game_environment(dict(os.environ), command), os.environ.get("DROIDDECK_FEX", "auto")) if action == "run": try: mode, decide, command = parse_run(args) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env index a38d77d8c..22b5248f3 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env @@ -87,6 +87,15 @@ def dotnet_memory_ordering(game): return DOTNET_FEX, [], ".NET runtime: FEX with full TSO and without multiblock" +@fix +def unity_memory_ordering(game): + """A Unity player under FEX without TSO deadlocks or crashes: Megabonk (Linux, IL2CPP) froze with every + thread parked on a futex on about half of all launches, Crab Game (Windows under Proton, IL2CPP) died with + an access violation in UnityPlayer.dll. Presets that turn TSO off keep it on for Unity games.""" + if {"unityplayer.so", "gameassembly.so", "unityplayer.dll"} & game.files: + return {"FEX_TSOENABLED": "1"}, [], "Unity: FEX with TSO" + + @fix def godot_vulkan(game): if is_godot(game) and not game.has_arg("--rendering-driver"): diff --git a/tools/tests/test_droiddeck_fex.py b/tools/tests/test_droiddeck_fex.py index fed546ddd..fb8f9ad73 100644 --- a/tools/tests/test_droiddeck_fex.py +++ b/tools/tests/test_droiddeck_fex.py @@ -690,6 +690,32 @@ class LaunchTest(FexTestCase): command, env, mode = G["launch"].call_args[0] self.assertEqual((command, mode), (["/game/run.sh", "-a"], "auto")) + def test_steam_games_get_their_settings_and_linux_unity_keeps_tso(self): + config = self.home / ".config/droiddeck/game-environment.json" + unity = self.write(self.tmp / "Megabonk/Megabonk.x86_64", elf(62), 0o755) + self.write(self.tmp / "Megabonk/UnityPlayer.so", elf(62)) + self.write(self.tmp / "Megabonk/GameAssembly.so", elf(62)) + other = self.write(self.tmp / "Other/game.x86_64", elf(62), 0o755) + self.write(config, json.dumps({"version": 1, "shared": {"FEX_TSOENABLED": "0", "FEX_MULTIBLOCK": "1"}, + "games": {"20": {"FEX_TSOENABLED": "0"}}})) + + def started(program, app): + with mock.patch.dict(G, {"launch": mock.Mock(return_value=0), "GAME_ENVIRONMENT": str(config)}), \ + mock.patch.dict(os.environ, {"SteamAppId": app}, clear=True): + _, said = self.quiet(FEX["main"], ["steam", "waitforexitandrun", str(program), "-arg"]) + command, env, _ = G["launch"].call_args[0] + self.assertEqual(command, [str(program), "-arg"]) + return env.get("FEX_TSOENABLED"), env.get("FEX_MULTIBLOCK"), said + + tso, multiblock, said = started(unity, "10") + self.assertEqual((tso, multiblock), ("1", "1")) + self.assertIn("Unity: FEX with TSO", said) + self.assertEqual(started(other, "10")[:2], ("0", "1")) + self.assertEqual(started(unity, "20")[:2], ("0", "1")) + config.unlink() + self.assertEqual(started(unity, "10")[:2], ("1", None)) + self.assertEqual(started(other, "10")[:2], (None, None)) + class AppInfoTest(unittest.TestCase): COMPAT = runpy.run_path(str(BIN / "steam-compatibility")) diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index c5c308c8c..bbae1b3a7 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -357,6 +357,27 @@ class GameEnvironmentTest(unittest.TestCase): (asylum / name).touch() self.assertEqual(MODULE["engine_fixes"](str(asylum / "BmLauncher.exe"), []), ({}, [], [])) + def test_unity_players_keep_tso_over_the_preset(self): + with tempfile.TemporaryDirectory() as tmp: + def game(*names): + for name in names: + (Path(tmp) / name).parent.mkdir(parents=True, exist_ok=True) + (Path(tmp) / name).touch() + return str(Path(tmp) / names[0]) + + il2cpp = game("Megabonk/Megabonk.x86_64", "Megabonk/UnityPlayer.so", "Megabonk/GameAssembly.so") + mono = game("Mono/Game.x86_64", "Mono/UnityPlayer.so") + windows = game("Crab Game/Crab Game.exe", "Crab Game/UnityPlayer.dll", "Crab Game/GameAssembly.dll") + windows_mono = game("RoR2/Risk of Rain 2.exe", "RoR2/UnityPlayer.dll", "RoR2/MonoBleedingEdge/mono-2.0-bdwgc.dll") + for exe in (il2cpp, mono, windows, windows_mono): + env, extra, notes = MODULE["engine_fixes"](exe, []) + self.assertEqual((env, extra, notes), ({"FEX_TSOENABLED": "1"}, [], ["Unity: FEX with TSO"])) + performance = {"version": 1, "shared": {"FEX_TSOENABLED": "0", "FEX_VECTORTSOENABLED": "0"}} + result = MODULE["apply_config"]({}, performance, "3405340", env) + self.assertEqual((result["FEX_TSOENABLED"], result["FEX_VECTORTSOENABLED"]), ("1", "0")) + performance["games"] = {"3405340": {"FEX_TSOENABLED": "0"}} + self.assertEqual(MODULE["apply_config"]({}, performance, "3405340", env)["FEX_TSOENABLED"], "0") + def test_a_new_fix_only_needs_registering(self): with tempfile.TemporaryDirectory() as tmp: (Path(tmp) / "Game.exe").touch()