Files
DroidDeck/tools/dirt-ws2/gen.py
The412Banner 268dcef559 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.
2026-10-31 21:55:36 +00:00

75 lines
3.2 KiB
Python

"""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('<I', data, 0x3c)[0]
machine, sections = struct.unpack_from('<HH', data, pe + 4)
optional_size = struct.unpack_from('<H', data, pe + 20)[0]
optional = pe + 24
export_rva = struct.unpack_from('<I', data, optional + 96)[0]
table = [struct.unpack_from('<IIII', data, optional + optional_size + 40 * i + 8) for i in range(sections)]
def offset(rva):
for size, address, raw_size, raw in table:
if address <= rva < address + max(size, raw_size):
return rva - address + raw
raise ValueError('RVA %#x outside the sections' % rva)
base, functions, names, function_rva, name_rva, ordinal_rva = struct.unpack_from('<IIIIII', data, offset(export_rva) + 16)
named = {}
for i in range(names):
text = struct.unpack_from('<I', data, offset(name_rva) + 4 * i)[0]
index = struct.unpack_from('<H', data, offset(ordinal_rva) + 2 * i)[0]
named[index] = data[offset(text):].split(b'\0')[0].decode()
result = [(base + i, named.get(i)) for i in range(functions) if struct.unpack_from('<I', data, offset(function_rva) + 4 * i)[0]]
return machine, result
def verify(src, dll):
machine, found = pe_exports(dll)
if machine != 0x14c:
sys.exit('%s is not a 32-bit x86 DLL (machine %#x)' % (dll, machine))
expected = exports_list(src)
if found != expected:
sys.exit('%s exports differ from ws2_32-exports.txt: %d found, %d expected' % (dll, len(found), len(expected)))
if b'droiddeck-dirt-ws2_32' not in Path(dll).read_bytes():
sys.exit('%s lacks the stand-in marker' % dll)
print('%s: %d exports match, marker present' % (dll, len(found)))
if __name__ == '__main__':
{'generate': generate, 'verify': verify}[sys.argv[1]](sys.argv[2], sys.argv[3])