From 268dcef5591500c5ae67f4a28d94a6eace426fee Mon Sep 17 00:00:00 2001 From: The412Banner Date: Sat, 31 Oct 2026 21:55:36 +0000 Subject: [PATCH] fix(dual-screen): DiRT 3 and DiRT Showdown telemetry no longer stops the game (error 10038) DiRT 3 and DiRT Showdown keep sending motion telemetry on the UDP socket they closed at the end of a race (and when leaving Joyride); sendto() fails with WSAENOTSOCK and the game stops with "sendto failed with error 10038". - tools/dirt-ws2: a 32-bit ws2_32.dll stand-in. Every export jumps to the same-ordinal export of Wine's own ws2_32.dll; only a sendto() failing with 10038 reports 0 bytes sent. Built in CI (components recipe dirt-ws2, clang + lld) with its exports checked against Wine 11's list, staged to /usr/local/lib/droiddeck-dirt. - droiddeck-game-env: for dirt3_game.exe / showdown.exe / showdown_avx.exe with the game's UDP/motion output on, and a Proton whose own ws2_32.dll lacks the fix, the stand-in replaces the prefix's syswow64/ws2_32.dll (Wine loads ws2_32 as a KnownDLL from there, so a copy beside the game is ignored) with WINEDLLOVERRIDES=ws2_32=n,b; the launch waits for the game and puts the prefix's own file back. A copy left by a killed launch is put back at the next launch in that prefix. - Automatic turns DiRT 3 and DiRT Showdown on like the other games again; the crash warning and the switched-on-by-hand memory from the previous build are gone. Same behaviour as DiRTTelemetryErrorFix by RoccoC (MIT), based on the fix by Carsten Gottschling. DroidDeck-Proton gets the same rule in its own ws2_32 (branch fix/dirt-telemetry-sendto); the swap is skipped there. --- .github/workflows/build.yml | 3 + .../launcher/session/SessionFiles.kt | 1 + .../launcher/session/SessionPrefs.kt | 7 - .../launcher/session/SettingsReset.kt | 2 +- .../launcher/session/TelemetryOutput.kt | 20 +-- .../launcher/ui/BottomTelemetryPage.kt | 7 +- app/src/main/res/values/strings.xml | 1 - .../launcher/session/TelemetryOutputTest.kt | 21 +-- tools/components/recipes.json | 15 ++ tools/components/recipes/dirt-ws2.sh | 3 + tools/dirt-ws2/README.md | 19 +++ tools/dirt-ws2/build.sh | 26 ++++ tools/dirt-ws2/gen.py | 74 ++++++++++ tools/dirt-ws2/kernel32.def | 7 + tools/dirt-ws2/ws2_32-exports.txt | 135 +++++++++++++++++ tools/dirt-ws2/ws2_32.c | 103 +++++++++++++ .../overlay/usr/local/bin/droiddeck-game-env | 139 +++++++++++++++++- tools/release/runtime_inputs.py | 2 +- tools/tests/test_game_environment.py | 132 +++++++++++++++++ 19 files changed, 664 insertions(+), 53 deletions(-) create mode 100644 tools/components/recipes/dirt-ws2.sh create mode 100644 tools/dirt-ws2/README.md create mode 100755 tools/dirt-ws2/build.sh create mode 100644 tools/dirt-ws2/gen.py create mode 100644 tools/dirt-ws2/kernel32.def create mode 100644 tools/dirt-ws2/ws2_32-exports.txt create mode 100644 tools/dirt-ws2/ws2_32.c diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bf6630fd52..f8d3a75278 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -274,6 +274,9 @@ jobs: if: steps.comet.outputs.cache-hit != 'true' run: python3 tools/components/prepare.py comet + - name: Build the DiRT 3 / DiRT Showdown ws2_32 stand-in + run: python3 tools/components/prepare.py dirt-ws2 + - name: Prepare graphics driver variants run: python3 tools/quest/build-driver.py "$ANDROID_HOME/ndk/$NDK_VERSION" - name: Record and pack prepared components diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index 1f94f3a7e9..9bc5e59fe1 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -170,6 +170,7 @@ object SessionFiles { "usr/local/lib/droiddeck/uruntime" to "usr/local/lib/droiddeck/uruntime", "usr/local/lib/droiddeck-pipewire.tzst" to "usr/local/lib/droiddeck-pipewire.tzst", "usr/local/share/licenses/uruntime/LICENSE" to "usr/local/share/licenses/uruntime/LICENSE", + "usr/local/lib/droiddeck-dirt/ws2_32.dll" to "usr/local/lib/droiddeck-dirt/ws2_32.dll", ) + mangoapp + msitools.map { it to it } + vr + fexPreloads).filter { (asset, _) -> val dir = asset.substringBeforeLast('/') runCatching { context.assets.list("linuxfs/$dir")?.contains(asset.substringAfterLast('/')) == true }.getOrDefault(false) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 3d0bfbd958..74b071efb4 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -132,13 +132,6 @@ object SessionPrefs { prefs(context).edit().putStringSet("telemetryOff", keys.toSet()).apply() } - /** Games the user switched on by hand, as TelemetryOutput keys; Automatic keeps them on. */ - fun telemetryOnByHand(context: Context): Set = prefs(context).getStringSet("telemetryOnByHand", null)?.toSet().orEmpty() - - fun setTelemetryOnByHand(context: Context, keys: Set) { - prefs(context).edit().putStringSet("telemetryOnByHand", keys.toSet()).apply() - } - fun dualScreenLauncher(context: Context): Boolean = prefs(context).getBoolean("dualScreenLauncher", true) fun setDualScreenLauncher(context: Context, on: Boolean) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt index 3bd1c09e7d..1756c631ac 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt @@ -26,7 +26,7 @@ object SettingsReset { "upscaler", "upscaleSharpness", "screenEffects", "textureAnisotropy", "textureLodBias", "steamDownloadsInBackground", "steam_offline", "bootMovie", "bootMovieSound", "bootMovieVolume", "bootMovieSeconds", "bootMovieApplyNext", "bootMovieSteamSeen", - "questDefaultsV1", "telemetryAuto", "telemetryOff", "telemetryOnByHand", "statsStrip", "statsDash", + "questDefaultsV1", "telemetryAuto", "telemetryOff", "statsStrip", "statsDash", ) internal val SESSION_PREFIXES = listOf( diff --git a/app/src/main/java/com/droiddeck/launcher/session/TelemetryOutput.kt b/app/src/main/java/com/droiddeck/launcher/session/TelemetryOutput.kt index 6673e8aecf..afd60b02d1 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/TelemetryOutput.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/TelemetryOutput.kt @@ -10,10 +10,7 @@ import java.io.File * In-game stats listener; the file as it was is kept beside it, and its element is put back when turned off. */ object TelemetryOutput { - data class Game(val name: String, val prefixId: String, val file: File, val on: Boolean) { - /** DiRT 3 and DiRT Showdown can stop at the end of a race with WinSock error 10038 while this output is on. */ - val raceEndCrash: Boolean get() = name.lowercase().replace(" ", "") in RACE_END_CRASH - } + data class Game(val name: String, val prefixId: String, val file: File, val on: Boolean) data class MenuGame(val name: String, val setting: String, val port: Int) @@ -31,7 +28,6 @@ object TelemetryOutput { private const val ADDRESS = "127.0.0.1" private const val EXTRADATA = "3" private val TAGS = listOf("udp", "motion") - private val RACE_END_CRASH = setOf("dirtshowdown", "dirt3") // Byte-for-byte: every byte maps to one char and back, whatever the file's own encoding is. private val BYTES = Charsets.ISO_8859_1 @@ -66,24 +62,16 @@ object TelemetryOutput { true }.getOrDefault(false) - /** - * Automatic: turns the output on for every found game the user has not switched off and that is not running. - * Games with the race-end crash are left to the user, and ones Automatic switched on go back to their own setting. - */ + /** Automatic: turns the output on for every found game the user has not switched off and that is not running. */ fun applyAutomatic(context: Context, running: String?) { if (!SessionPrefs.telemetryAuto(context)) return - val games = scan(Library.compatdataDirs(context)) - automaticTargets(games, SessionPrefs.telemetryOff(context), running).forEach { set(it, true) } - automaticRestores(games, SessionPrefs.telemetryOnByHand(context), running).forEach { set(it, false) } + automaticTargets(scan(Library.compatdataDirs(context)), SessionPrefs.telemetryOff(context), running).forEach { set(it, true) } } internal fun key(game: Game) = "${game.prefixId}/${game.name}" internal fun automaticTargets(games: List, off: Set, running: String?): List = - games.filter { !it.on && !it.raceEndCrash && key(it) !in off && !isRunning(it.prefixId, running) } - - internal fun automaticRestores(games: List, onByHand: Set, running: String?): List = - games.filter { it.raceEndCrash && it.on && key(it) !in onByHand && File(it.file.path + ORIGINAL_SUFFIX).isFile && !isRunning(it.prefixId, running) } + games.filter { !it.on && key(it) !in off && !isRunning(it.prefixId, running) } /** Games DroidDeck changed, which go back to their own setting when Automatic is switched off. */ internal fun restoreTargets(games: List, running: String?): List = diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt index da2d286cc7..19e8571624 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt @@ -271,18 +271,13 @@ fun BottomTelemetryPage(fps: () -> Float?, runningGame: () -> String? = { null } } for (game in outputs) { val locked = TelemetryOutput.isRunning(game.prefixId, running) - val hint = listOfNotNull( - if (locked) stringResource(R.string.bottom_telemetry_send_close, game.name) else null, - if (game.raceEndCrash) stringResource(R.string.bottom_telemetry_send_crash) else null, - ).joinToString("\n").ifEmpty { null } + val hint = if (locked) stringResource(R.string.bottom_telemetry_send_close, game.name) else null SwitchRow(game.name, hint, game.on, !locked) { want -> scope.launch { val ok = withContext(Dispatchers.IO) { val key = TelemetryOutput.key(game) val off = SessionPrefs.telemetryOff(context) - val byHand = SessionPrefs.telemetryOnByHand(context) SessionPrefs.setTelemetryOff(context, if (want) off - key else off + key) - SessionPrefs.setTelemetryOnByHand(context, if (want) byHand + key else byHand - key) TelemetryOutput.set(game, want) } outputFailed = if (ok) null else game.name diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cf986262bd..a38edb81da 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1916,7 +1916,6 @@ Main %1$s t Reserve %1$s t Tap the top strip or the dash to change its style. - This game can stop at the end of a race while this is on, so Automatic leaves it off. Send dashboard data Automatic Turns it on for every game listed here, including games found later. Games you switch off stay off. diff --git a/app/src/test/java/com/droiddeck/launcher/session/TelemetryOutputTest.kt b/app/src/test/java/com/droiddeck/launcher/session/TelemetryOutputTest.kt index fbd2e37514..cd65b2a577 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/TelemetryOutputTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/TelemetryOutputTest.kt @@ -101,28 +101,13 @@ class TelemetryOutputTest { assertTrue(TelemetryOutput.restoreTargets(TelemetryOutput.scan(listOf(compatdata)), "2848447563").isEmpty()) } - @Test fun automaticLeavesTheRaceEndCrashGamesToTheUser() { + @Test fun automaticTurnsOnDirt3AndShowdownLikeTheOtherGames() { val f = File("x") val showdownGame = TelemetryOutput.Game("DiRT Showdown", "2848447563", f, on = false) val dirt3 = TelemetryOutput.Game("DiRT3", "321040", f, on = false) val rally = TelemetryOutput.Game("DiRT Rally 2.0", "690790", f, on = false) - assertTrue(showdownGame.raceEndCrash) - assertTrue(dirt3.raceEndCrash) - assertFalse(rally.raceEndCrash) - assertEquals(listOf(rally), TelemetryOutput.automaticTargets(listOf(showdownGame, dirt3, rally), emptySet(), null)) - } - - @Test fun automaticPutsBackAShowdownItSwitchedOnButNotOneTheUserDid() { - val compatdata = tmp.newFolder("steamapps", "compatdata") - val config = configFile(compatdata, "2848447563", "DiRT Showdown").apply { writeText(showdown) } - val game = TelemetryOutput.scan(listOf(compatdata)).single() - assertTrue(TelemetryOutput.set(game, true)) - val on = TelemetryOutput.scan(listOf(compatdata)) - assertEquals(on, TelemetryOutput.automaticRestores(on, emptySet(), null)) - assertTrue(TelemetryOutput.automaticRestores(on, setOf("2848447563/DiRT Showdown"), null).isEmpty()) - assertTrue(TelemetryOutput.automaticRestores(on, emptySet(), "2848447563").isEmpty()) - on.forEach { TelemetryOutput.set(it, false) } - assertEquals(showdown, config.readText()) + assertEquals(listOf(showdownGame, dirt3, rally), TelemetryOutput.automaticTargets(listOf(showdownGame, dirt3, rally), emptySet(), null)) + assertEquals(listOf(rally), TelemetryOutput.automaticTargets(listOf(showdownGame, dirt3, rally), setOf("2848447563/DiRT Showdown", "321040/DiRT3"), null)) } private fun configFile(compatdata: File, id: String, game: String): File = diff --git a/tools/components/recipes.json b/tools/components/recipes.json index 602fb379ec..0dfb8e4264 100644 --- a/tools/components/recipes.json +++ b/tools/components/recipes.json @@ -222,5 +222,20 @@ "cmake", "xz-utils" ] + }, + "dirt-ws2": { + "script": "tools/components/recipes/dirt-ws2.sh", + "inputs": [ + "tools/dirt-ws2", + "tools/components/recipes/dirt-ws2.sh" + ], + "outputs": [ + "app/src/main/assets/linuxfs/usr/local/lib/droiddeck-dirt/ws2_32.dll" + ], + "packages": [ + "clang", + "lld", + "llvm" + ] } } diff --git a/tools/components/recipes/dirt-ws2.sh b/tools/components/recipes/dirt-ws2.sh new file mode 100644 index 0000000000..77a4070884 --- /dev/null +++ b/tools/components/recipes/dirt-ws2.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +set -euo pipefail +bash tools/dirt-ws2/build.sh app/src/main/assets/linuxfs/usr/local/lib/droiddeck-dirt diff --git a/tools/dirt-ws2/README.md b/tools/dirt-ws2/README.md new file mode 100644 index 0000000000..14f02478f1 --- /dev/null +++ b/tools/dirt-ws2/README.md @@ -0,0 +1,19 @@ +# DiRT 3 / DiRT Showdown ws2_32 stand-in + +DiRT 3 (`dirt3_game.exe`) and DiRT Showdown (`showdown.exe`, `showdown_avx.exe`) close their motion telemetry UDP +socket at the end of a race, and when leaving Joyride, but keep calling `sendto()` on it. `sendto()` fails with +WSAENOTSOCK and the game stops with "sendto failed with error 10038". The same happens on Windows. + +`ws2_32.dll` here is a 32-bit stand-in: every export jumps to the same-ordinal export of Wine's own `ws2_32.dll`; +only a `sendto()` that fails with 10038 reports 0 bytes sent instead. Same behaviour as +[DiRTTelemetryErrorFix](https://github.com/RoccoC/DiRTTelemetryErrorFix) by RoccoC (MIT), based on the original fix +by Carsten Gottschling. + +Wine loads `ws2_32` as a KnownDLL, so a copy beside the game is ignored. `droiddeck-game-env` puts the stand-in in +place of the prefix's `syswow64/ws2_32.dll` for these games while their telemetry output is on, adds +`WINEDLLOVERRIDES=ws2_32=n,b`, and puts Wine's file back when the game exits. It skips the swap when the game's +Proton already has the fix in its own `ws2_32.dll` (DroidDeck-Proton patch +`dirt-telemetry-sendto-closed-socket.patch`). + +`build.sh OUT` builds it with clang, lld and llvm-dlltool and checks its exports against `ws2_32-exports.txt` +(taken from Wine 11's `ws2_32.dll`). diff --git a/tools/dirt-ws2/build.sh b/tools/dirt-ws2/build.sh new file mode 100755 index 0000000000..3abe6d16cb --- /dev/null +++ b/tools/dirt-ws2/build.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Builds the 32-bit ws2_32.dll stand-in for DiRT 3 and DiRT Showdown into the given folder. +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +mkdir -p "$1" +out=$(cd "$1" && pwd) +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT + +tool() { + local found + found=$(command -v "$1" || ls /usr/lib/llvm-*/bin/"$1" 2>/dev/null | sort -V | tail -n1 || true) + [ -n "$found" ] || { echo "dirt-ws2: $1 not found" >&2; exit 1; } + echo "$found" +} +clang=$(tool clang) +dlltool=$(tool llvm-dlltool) +tool ld.lld > /dev/null + +python3 "$here/gen.py" generate "$here" "$work" +"$dlltool" -m i386 -k -d "$here/kernel32.def" -l "$work/libkernel32.a" +"$clang" --target=i686-w64-mingw32 -O2 -Wall -Wextra -Werror -ffreestanding -fno-stack-protector -fno-builtin \ + -nostdlib -shared -fuse-ld=lld -o "$work/ws2_32.dll" "$here/ws2_32.c" "$work/thunks.c" "$work/ws2_32.def" \ + -L"$work" -lkernel32 -Wl,--entry,DllMain@12 -Wl,--no-insert-timestamp +python3 "$here/gen.py" verify "$here" "$work/ws2_32.dll" +install -m 644 "$work/ws2_32.dll" "$out/ws2_32.dll" diff --git a/tools/dirt-ws2/gen.py b/tools/dirt-ws2/gen.py new file mode 100644 index 0000000000..3d2532b0e6 --- /dev/null +++ b/tools/dirt-ws2/gen.py @@ -0,0 +1,74 @@ +"""ws2_32 stand-in helpers. + +gen.py generate SRC OUT writes OUT/thunks.c (one jump per export) and OUT/ws2_32.def from SRC/ws2_32-exports.txt. +gen.py verify SRC DLL checks that DLL exports exactly the ordinals and names in SRC/ws2_32-exports.txt. +""" +import struct +import sys +from pathlib import Path + + +def exports_list(src): + rows = [] + for line in (Path(src) / 'ws2_32-exports.txt').read_text().splitlines(): + ordinal, name = line.split() + rows.append((int(ordinal), name)) + return rows + + +def generate(src, out): + rows = exports_list(src) + passed = [(o, n) for o, n in rows if n != 'sendto'] + c = ['/* Generated by tools/dirt-ws2/gen.py. */', + 'const unsigned short dirtfix_ordinals[%d] = {%s};' % (len(passed), ', '.join(str(o) for o, _ in passed)), + 'const unsigned int dirtfix_count = %d;' % len(passed), + 'void *dirtfix_real[%d];' % len(passed), + '__asm__(".text\\n"'] + for i, (o, _) in enumerate(passed): + c.append(' ".globl _dirtfix_t%d\\n_dirtfix_t%d:\\n\\tjmp *_dirtfix_real+%d\\n"' % (o, o, 4 * i)) + c.append(');') + (Path(out) / 'thunks.c').write_text('\n'.join(c) + '\n') + d = ['LIBRARY ws2_32.dll', 'EXPORTS'] + d += [' %s=%s @%d' % (n, 'dirtfix_sendto@24' if n == 'sendto' else 'dirtfix_t%d' % o, o) for o, n in rows] + (Path(out) / 'ws2_32.def').write_text('\n'.join(d) + '\n') + + +def pe_exports(path): + data = Path(path).read_bytes() + pe = struct.unpack_from('= 500) continue; + if (path[0] == '\\' && path[1] == '?' && path[2] == '?' && path[3] == '\\') path[1] = '\\'; + append(path, "\\i386-windows\\ws2_32.dll"); + if (GetFileAttributesW(path) == (DWORD)-1) continue; + if ((real = LoadLibraryW(path)) && real != self) return real; + } + n = GetSystemDirectoryW(path, 280); + if (!n || n >= 280) return 0; + path[n] = 0; + append(path, "\\ws2_32.dll"); + real = LoadLibraryW(path); + return real != self ? real : 0; +} + +BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *reserved) +{ + HMODULE real; + UINT i; + + (void)reserved; + if (reason != 1) return 1; + if (!(real = load_wine_ws2_32(self))) return 0; + for (i = 0; i < dirtfix_count; i++) dirtfix_real[i] = GetProcAddress(real, ORDINAL(dirtfix_ordinals[i])); + real_sendto = GetProcAddress(real, ORDINAL(20)); + real_WSAGetLastError = GetProcAddress(real, ORDINAL(111)); + real_WSASetLastError = GetProcAddress(real, ORDINAL(112)); + return real_sendto && real_WSAGetLastError && real_WSASetLastError; +} diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env index 72d8f98615..5fb1168059 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env @@ -4,6 +4,7 @@ import json import os import re import runpy +import signal import subprocess import sys import tempfile @@ -735,7 +736,121 @@ def directaudio_settings(env): return env -def start(launch, launch_env, digest): +DIRT_GAMES = {"dirt3_game": "dirt3", "showdown": "dirtshowdown", "showdown_avx": "dirtshowdown"} +DIRT_WS2 = "/usr/local/lib/droiddeck-dirt/ws2_32.dll" +DIRT_MARKER = b"droiddeck-dirt-ws2_32" +DIRT_FIXED = "showdown_avx.exe".encode("utf-16-le") +DIRT_SAVED = ".droiddeck-original" + + +def dirt_telemetry_on(prefix, game): + """Whether the game's UDP/motion output is on, as In-game stats or another listener sets it ("dbox" is its default).""" + try: + folders = [f for f in (Path(prefix) / "pfx/drive_c/users/steamuser/Documents/My Games").iterdir() + if f.name.lower().replace(" ", "") == game] + except OSError: + return False + for folder in folders: + try: + text = (folder / "hardwaresettings/hardware_settings_config.xml").read_text(encoding="latin-1") + except OSError: + continue + for tag in ("udp", "motion"): + element = re.search(r"<%s\s[^>]*>" % tag, text) + if element: + attrs = dict(re.findall(r'\s(\w+)="([^"]*)"', element.group(0))) + return attrs.get("enabled") == "true" and attrs.get("ip", "").lower() not in ("", "dbox") + return False + + +def proton_ws2(proton): + return Path(proton).parent / "files/lib/wine/i386-windows/ws2_32.dll" + + +def proton_has_dirt_fix(proton): + try: + return DIRT_FIXED in proton_ws2(proton).read_bytes() + except OSError: + return False + + +def prefix_ws2(prefix): + return Path(prefix) / "pfx/drive_c/windows/syswow64/ws2_32.dll" + + +def is_dirt_stand_in(path): + try: + return not path.is_symlink() and DIRT_MARKER in path.read_bytes() + except OSError: + return False + + +def dirt_ws2_wanted(command, env): + """The prefix to give the ws2_32 stand-in: a DiRT 3 / Showdown launch with telemetry on and a Proton without the fix.""" + prefix = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") + if len(command) < 3 or not prefix or not os.path.isfile(DIRT_WS2): + return None + game = DIRT_GAMES.get(os.path.splitext(os.path.basename(command[2]))[0].lower()) + if not game or not dirt_telemetry_on(prefix, game) or proton_has_dirt_fix(command[0]): + return None + return prefix if os.path.lexists(prefix_ws2(prefix)) else None + + +def swap_dirt_ws2(prefix): + """Puts the stand-in where the prefix's syswow64/ws2_32.dll is; that file is kept beside it.""" + target = prefix_ws2(prefix) + saved = target.with_name(target.name + DIRT_SAVED) + data = Path(DIRT_WS2).read_bytes() + if not is_dirt_stand_in(target): + if os.path.lexists(saved): + os.unlink(saved) + os.replace(target, saved) + staged = target.with_name(target.name + ".droiddeck-tmp") + staged.write_bytes(data) + os.replace(staged, target) + + +def restore_dirt_ws2(prefix, proton): + """Puts the prefix's own syswow64/ws2_32.dll back if the stand-in is still there; true when it changed something.""" + target = prefix_ws2(prefix) + saved = target.with_name(target.name + DIRT_SAVED) + if os.path.lexists(saved): + if is_dirt_stand_in(target) or not os.path.lexists(target): + os.replace(saved, target) + return True + os.unlink(saved) + return False + own = proton_ws2(proton) + if is_dirt_stand_in(target) and own.is_file(): + link = target.with_name(target.name + ".droiddeck-tmp") + if os.path.lexists(link): + os.unlink(link) + os.symlink(own, link) + os.replace(link, target) + return True + return False + + +def wait_forwarding_signals(process): + def forward(signum, frame): + try: + process.send_signal(signum) + except OSError: + pass + + for signum in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP): + signal.signal(signum, forward) + code = process.wait() + return 128 - code if code < 0 else code + + +def start(launch, launch_env, digest, after=None): + if after: + process = subprocess.Popen(launch, env=launch_env) + try: + return wait_forwarding_signals(process) + finally: + after() if digest: code = subprocess.run(launch, env=launch_env, check=False).returncode if code == 0: @@ -760,6 +875,7 @@ def main(): except OSError: digest = None app_id = game_id(env) + dirt_prefix = None if app_id: gl_default = gl_vsync(env) waiting = len(command) > 1 and command[1] == "waitforexitandrun" @@ -779,6 +895,10 @@ def main(): if components: own = fixes.get("WINEDLLOVERRIDES") fixes["WINEDLLOVERRIDES"] = components + ";" + own if own else components + dirt_prefix = dirt_ws2_wanted(command, env) + if dirt_prefix: + own = fixes.get("WINEDLLOVERRIDES") + fixes["WINEDLLOVERRIDES"] = own + ";ws2_32=n,b" if own else "ws2_32=n,b" path = Path.home() / ".config/droiddeck/game-environment.json" config = {} try: @@ -823,16 +943,29 @@ def main(): return 1 if xbox_result is not None: return xbox_result + after = None + prefix = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") + if app_id and prefix and len(command) > 1 and command[1] == "waitforexitandrun": + try: + if dirt_prefix: + swap_dirt_ws2(dirt_prefix) + after = lambda: restore_dirt_ws2(dirt_prefix, command[0]) + print("droiddeck-game-env: DiRT telemetry fix: ws2_32 stand-in in the prefix until the game exits", + file=sys.stderr) + elif restore_dirt_ws2(prefix, command[0]): + print("droiddeck-game-env: DiRT telemetry fix: put the prefix's own ws2_32 back", file=sys.stderr) + except OSError as error: + print("droiddeck-game-env: DiRT telemetry fix not applied (%s)" % error, file=sys.stderr) sync = load_sync() launch, launch_env = select_sync(command, env, sync) if launch[0] != command[0]: try: - return start(launch, launch_env, digest) + return start(launch, launch_env, digest, after) except OSError as error: print("droiddeck-game-env: %s could not start (%s); stock launch" % (launch[0], error), file=sys.stderr) launch, launch_env = command, without_pack(launch_env, env) record_stock(sync, launch, launch_env) - return start(launch, launch_env, digest) + return start(launch, launch_env, digest, after) if __name__ == "__main__": diff --git a/tools/release/runtime_inputs.py b/tools/release/runtime_inputs.py index 6eff505e85..a375bbed55 100644 --- a/tools/release/runtime_inputs.py +++ b/tools/release/runtime_inputs.py @@ -23,7 +23,7 @@ PACK_NAMES = ['proot', 'directaudio', 'tracking', 'xbox', 'comet', 'butler'] SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst', 'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio', 'tools/packs', 'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env', - 'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/components', 'tools/ci', 'tools/build_local.sh', + 'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/dirt-ws2', 'tools/components', 'tools/ci', 'tools/build_local.sh', 'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal', 'tools/quest', 'tools/vr/src', 'tools/vr/driver_droiddeck', 'tools/vr/layer', 'tools/vr/build.sh', 'app/src/main/cpp/waylandcomp/src/headpose_shm.h', diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index d9d2c30637..463873fc69 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -29,6 +29,138 @@ def config_text(entries): return '"InstallConfigStore" { "Software" { "Valve" { "Steam" { "CompatToolMapping" { %s } } } } }' % mapping +class DirtTelemetryFixTest(unittest.TestCase): + SHOWDOWN_ON = '' + SHOWDOWN_DEFAULT = '' + + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + root = Path(self.tmp.name) + self.prefix = root / "compatdata/2848447563" + self.proton = root / "Proton/proton" + self.own = root / "Proton/files/lib/wine/i386-windows/ws2_32.dll" + self.own.parent.mkdir(parents=True) + self.own.write_bytes(b"MZ wine ws2_32") + self.proton.write_text("") + self.target = self.prefix / "pfx/drive_c/windows/syswow64/ws2_32.dll" + self.target.parent.mkdir(parents=True) + self.target.symlink_to(self.own) + self.stand_in = root / "droiddeck-dirt/ws2_32.dll" + self.stand_in.parent.mkdir() + self.stand_in.write_bytes(b"MZ droiddeck-dirt-ws2_32 stand-in") + self.patch = mock.patch.dict(MODULE["dirt_ws2_wanted"].__globals__, {"DIRT_WS2": str(self.stand_in)}) + self.patch.start() + + def tearDown(self): + self.patch.stop() + self.tmp.cleanup() + + def config(self, folder, text): + path = self.prefix / "pfx/drive_c/users/steamuser/Documents/My Games" / folder / "hardwaresettings/hardware_settings_config.xml" + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text) + + def command(self, exe="showdown.exe"): + return [str(self.proton), "waitforexitandrun", "/root/Storage/games/DiRT Showdown/" + exe] + + def env(self): + return {"STEAM_COMPAT_DATA_PATH": str(self.prefix)} + + def test_telemetry_counts_as_on_when_the_game_sends_somewhere(self): + self.config("DiRT Showdown", self.SHOWDOWN_DEFAULT) + self.assertFalse(MODULE["dirt_telemetry_on"](self.prefix, "dirtshowdown")) + self.config("DiRT Showdown", self.SHOWDOWN_ON) + self.assertTrue(MODULE["dirt_telemetry_on"](self.prefix, "dirtshowdown")) + self.config("DiRT Showdown", self.SHOWDOWN_ON.replace('enabled="true"', 'enabled="false"')) + self.assertFalse(MODULE["dirt_telemetry_on"](self.prefix, "dirtshowdown")) + self.config("DiRT3", '') + self.assertTrue(MODULE["dirt_telemetry_on"](self.prefix, "dirt3")) + + def test_stand_in_is_wanted_only_for_the_dirt_games_with_telemetry_on_and_a_proton_without_the_fix(self): + wanted = MODULE["dirt_ws2_wanted"] + self.config("DiRT Showdown", self.SHOWDOWN_DEFAULT) + self.assertIsNone(wanted(self.command(), self.env())) + self.config("DiRT Showdown", self.SHOWDOWN_ON) + self.assertEqual(wanted(self.command(), self.env()), str(self.prefix)) + self.assertEqual(wanted(self.command("Showdown_AVX.exe"), self.env()), str(self.prefix)) + self.assertIsNone(wanted(self.command("DiRTRally2.exe"), self.env())) + self.own.write_bytes(b"MZ wine ws2_32 " + "showdown_avx.exe".encode("utf-16-le")) + self.assertIsNone(wanted(self.command(), self.env())) + + def test_swap_keeps_the_prefix_link_and_restore_puts_it_back(self): + MODULE["swap_dirt_ws2"](self.prefix) + saved = self.target.with_name("ws2_32.dll.droiddeck-original") + self.assertFalse(self.target.is_symlink()) + self.assertEqual(self.target.read_bytes(), self.stand_in.read_bytes()) + self.assertEqual(os.readlink(saved), str(self.own)) + self.assertEqual(self.own.read_bytes(), b"MZ wine ws2_32") + MODULE["swap_dirt_ws2"](self.prefix) + self.assertEqual(os.readlink(saved), str(self.own)) + self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, self.proton)) + self.assertEqual(os.readlink(self.target), str(self.own)) + self.assertFalse(os.path.lexists(saved)) + self.assertFalse(MODULE["restore_dirt_ws2"](self.prefix, self.proton)) + + def test_restore_keeps_a_file_proton_put_back_meanwhile(self): + MODULE["swap_dirt_ws2"](self.prefix) + newer = self.own.with_name("ws2_32-newer.dll") + newer.write_bytes(b"MZ newer wine ws2_32") + self.target.unlink() + self.target.symlink_to(newer) + self.assertFalse(MODULE["restore_dirt_ws2"](self.prefix, self.proton)) + self.assertEqual(os.readlink(self.target), str(newer)) + self.assertFalse(os.path.lexists(self.target.with_name("ws2_32.dll.droiddeck-original"))) + + def test_a_stand_in_left_without_the_saved_link_is_replaced_by_protons_own_file(self): + self.target.unlink() + self.target.write_bytes(self.stand_in.read_bytes()) + self.assertTrue(MODULE["restore_dirt_ws2"](self.prefix, self.proton)) + self.assertEqual(os.readlink(self.target), str(self.own)) + + def test_launch_swaps_the_stand_in_in_for_the_game_and_back_afterwards(self): + self.config("DiRT Showdown", self.SHOWDOWN_ON) + self.proton.write_text("#!" + sys.executable + "\nimport os, sys\n" + "target = os.path.join(os.environ['STEAM_COMPAT_DATA_PATH'], 'pfx/drive_c/windows/syswow64/ws2_32.dll')\n" + "print(os.environ.get('WINEDLLOVERRIDES', ''))\n" + "print(os.path.islink(target), b'droiddeck-dirt-ws2_32' in open(target, 'rb').read())\n") + self.proton.chmod(0o755) + runner = Path(self.tmp.name) / "run.py" + runner.write_text("import runpy, sys\nmain = runpy.run_path(sys.argv[1])['main']\nmain.__globals__['DIRT_WS2'] = sys.argv[2]\n" + "sys.argv = [sys.argv[1]] + sys.argv[3:]\nsys.exit(main())\n") + home = Path(self.tmp.name) / "home" + home.mkdir() + result = subprocess.run([sys.executable, str(runner), str(BIN / "droiddeck-game-env"), str(self.stand_in), *self.command()], + env={**os.environ, "HOME": str(home), "STEAM_COMPAT_DATA_PATH": str(self.prefix)}, text=True, capture_output=True) + self.assertEqual(result.returncode, 0, result.stderr) + overrides, state = result.stdout.splitlines()[-2:] + self.assertIn("ws2_32=n,b", overrides.split(";")) + self.assertEqual(state, "False True") + self.assertIn("DiRT telemetry fix: ws2_32 stand-in", result.stderr) + self.assertEqual(os.readlink(self.target), str(self.own)) + self.assertFalse(os.path.lexists(self.target.with_name("ws2_32.dll.droiddeck-original"))) + + def test_launch_with_telemetry_off_leaves_the_prefix_alone(self): + self.config("DiRT Showdown", self.SHOWDOWN_DEFAULT) + self.proton.write_text("#!" + sys.executable + "\nimport os\nprint(os.environ.get('WINEDLLOVERRIDES', ''))\n") + self.proton.chmod(0o755) + home = Path(self.tmp.name) / "home" + home.mkdir() + result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), *self.command()], + env={**os.environ, "HOME": str(home), "STEAM_COMPAT_DATA_PATH": str(self.prefix)}, text=True, capture_output=True) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertNotIn("ws2_32=n,b", result.stdout) + self.assertEqual(os.readlink(self.target), str(self.own)) + + def test_start_puts_the_file_back_after_the_game_exits(self): + calls = [] + code = MODULE["start"]([sys.executable, "-c", "raise SystemExit(3)"], dict(os.environ), None, lambda: calls.append("restored")) + self.assertEqual(code, 3) + self.assertEqual(calls, ["restored"]) + with self.assertRaises(OSError): + MODULE["start"]([str(Path(self.tmp.name) / "missing")], dict(os.environ), None, lambda: calls.append("again")) + self.assertEqual(calls, ["restored"]) + + class GameDebugLoggingTest(unittest.TestCase): def test_maintenance_precision_survives_run_with_performance_game_settings(self): with tempfile.TemporaryDirectory() as tmp: