#!/usr/bin/env python3 # SPDX-FileCopyrightText: 2026 Tomasz Kramkowski # SPDX-License-Identifier: CC0-1.0 """Patching wrapper script. This file is generated. See https://kramkow.ski/project/steamvr-patches.html and https://the-tk.com/cgit/steamvr-patches/about/ for more information. Use at your own risk! Installation (where is the thing this patcher targets, e.g. steamvr): 1. Put this `patch-` script somewhere, e.g. `~/.local/libexec/patch-` 2. Mark it executable 3. Change the launch options of the specified to: `/absolute/path/to/patch- %command%`. If you already have something in there, I'm sure you can figure it out. 4. Launch normally. You should see a desktop notification with some diagnostics which specify if the script actually did/tried to do something. """ import hashlib import os import stat import subprocess import sys import tempfile from dataclasses import dataclass from io import BytesIO from pathlib import Path NAME = Path(__file__).name MARKER = b"Valve, please fix." @dataclass(frozen=True) class Edit: offset: int replacement: bytes @dataclass(frozen=True) class Patch: original_hash: bytes patched_hash: bytes edits: tuple[Edit, ...] @classmethod def from_dict(cls, data: dict) -> "Patch": return cls( bytes.fromhex(data["original_hash"]), bytes.fromhex(data["patched_hash"]), tuple( Edit(offset, bytes.fromhex(replacement)) for offset, replacement in data["edits"] ), ) def apply_to(self, data: bytes) -> bytes: payload = data.removesuffix(MARKER) digest = sha256(payload) if digest == self.patched_hash: return payload + MARKER if digest != self.original_hash: if payload != data: raise LookupError( "already patched with another set of patches: " "verify file integrity and restart" ) raise LookupError("unrecognized file hash: this wrapper may need updating") result = BytesIO(payload) for edit in self.edits: result.seek(edit.offset) result.write(edit.replacement) patched = result.getvalue() if sha256(patched) != self.patched_hash: raise ValueError("patched file checksum mismatch") return patched + MARKER # Please refer to the source code (linked at the top of this file) if you want # to know how this data was generated PATCH_DATA = { 'bin/linux64/vrcompositor': { "original_hash": "4c380adc27832db9afaf83e446080354338bcb4c96f0f0cc561c90cd4d79168f", "patched_hash": "6cf972aa359f853b24f8a827a2e4f29442909e37227703369fdc39d0ff366a73", "edits": [ (0x404941, "84c00f842f570000eb06"), (0x404951, "488dbbe00d0000e90a570000"), (0x40a05b, "e830db0600"), (0x40a060, "e9dca8ffff9090"), (0x41d224, "31c09090909090"), (0x41db39, "31c09090909090"), (0x41db74, "909090909090"), (0x404961, "498b5500b80300000089424ceb22"), (0x404991, "89828c0000008982a4000000eb12"), (0x4049b1, "4c8bbd18feffffe902200100"), (0x4169b8, "e9a4dffeff9090"), (0x40d9d0, "e90ba910009090"), (0x20, "0090500000000000"), (0x38, "0d00"), ( 0x509000, "0600000004000000009050000000000000905100000000000090510000000000" "d802000000000000d80200000000000008000000000000000300000004000000" "e002000000000000e002000000000000e0020000000000001c00000000000000" "1c0000000000000001000000000000000400000004000000fc02000000000000" "fc02000000000000fc0200000000000044000000000000004400000000000000" "0400000000000000010000000400000000000000000000000000000000000000" "0000000000000000b050150000000000b0501500000000000010000000000000" "0100000005000000b050150000000000b060150000000000b060150000000000" "ae62390000000000ae6239000000000000100000000000000100000006000000" "60b34e000000000060d34e000000000060d34e000000000028b0010000000000" "a0bc01000000000000100000000000000100000006000000a063500000000000" "a093500000000000a0935000000000004811000000000000a0ee000000000000" "0010000000000000070000000400000060b34e000000000060d34e0000000000" "60d34e0000000000000000000000000030000000000000000800000000000000" "0200000006000000204b500000000000206b500000000000206b500000000000" "00030000000000000003000000000000080000000000000050e5746404000000" "a8370c0000000000a8370c0000000000a8370c00000000004493010000000000" "4493010000000000040000000000000051e57464060000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "010000000000000052e574640400000060b34e000000000060d34e0000000000" "60d34e000000000028b0010000000000a0bc0100000000000100000000000000" "0100000005000000009050000000000000905100000000000090510000000000" "cc03000000000000cc030000000000000010000000000000" ), ( 0x5092e0, "575652514883ec08e8140000004883c408595a5e5f554889f04189d1e9d656ef" "ff4154554889fd53488b7e484885ff0f84b2000000488b07480378b0488b07ff" "50304889c34885c00f8499000000488b004889dfff50404989c44885c00f8484" "000000488b85b0120000488b6808488b8598030000488b95a00300004839c274" "0b4c392074614883c010ebf0488b034889dfff5070488b95f0000000488bb5f8" "0000004889c1488b85400100004839d674214c3922740a4883c2104883c004eb" "ec48394a08730448894a08810800000100eb145b4c89e64889efba000001005d" "415ce909caf4ff5b5d415cc3" ), ], }, 'bin/linux64/vrserver': { "original_hash": "a7c9246f8fe36749caa0bfe3984efbb5fa7dbbb28e5263a5c1744f5ba6b0c10b", "patched_hash": "b14314f1fbfe8acb164583853e32c9a6870317bb9cbb278d9cea21862aaebd8f", "edits": [ (0x2c1386, "9090909090"), (0x2c1577, "9090909090"), (0x2c160c, "9090909090"), (0x2c1726, "9090909090"), (0x2c17ff, "9090909090"), (0x2c1699, "9090909090"), ], }, 'drivers/vrlink/bin/linux64/driver_vrlink.so': { "original_hash": "a33d1da2d1c9851a61e19b87ffc89bbbb6e8b1e503e189e3e19bccd2e6cd3b7f", "patched_hash": "c7d8adbbcd44d577f7891c15e53d6000eaf0b257fde642b09ff286bd00be9468", "edits": [ (0xd91dd, "89836c0d000090909090"), (0x13c2d2, "83bb4c090000007458e9702c0000"), (0x13c333, "488b8328090000e9ec210000"), (0x13e524, "e9a9ddffff9090"), (0x13d735, "e927000000"), (0x13d761, "488b9d70ffffff4c01f3e9d6feffff"), (0x1129f1, "57488b7f184885ff740feb04"), (0x112a01, "488b076a015eff50285fe91013feff"), (0x11a251, "4c89a30804000031c0894308c3"), (0x11a297, "e8b5ffffff6690"), (0x11a2aa, "e84287ffff"), (0x11b301, "89d02b47084189c6c1e004"), (0x11b30c, "0f8853050000"), (0x11b614, "0f841719fdff"), (0xecf31, "410fb644240988430ee926e90200"), ], }, } PATCHES: dict[str, Patch] = { path: Patch.from_dict(data) for path, data in PATCH_DATA.items() } def sha256(data): return hashlib.sha256(data).digest() def notify(message): print(f"{NAME}: {message}", file=sys.stderr) try: subprocess.run( ["notify-send", NAME, message], check=False, timeout=3, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) except (OSError, subprocess.TimeoutExpired): pass def replace(path, data): mode = stat.S_IMODE(path.stat().st_mode) with tempfile.NamedTemporaryFile( dir=path.parent, prefix=path.name + ".", delete=False ) as f: temporary = Path(f.name) try: f.write(data) f.flush() os.fchmod(f.fileno(), mode) os.fsync(f.fileno()) os.replace(temporary, path) finally: temporary.unlink(missing_ok=True) def main(command): diagnostics = [] if not command: print(f"Usage: {NAME} command [args...]", file=sys.stderr) return 2 try: root = Path.cwd() pending = [] for relative, patch in PATCHES.items(): path = (root / relative).resolve() try: original = path.read_bytes() except FileNotFoundError as e: diagnostics.append(f"Skipped `{relative}`: {e}") continue try: updated = patch.apply_to(original) except ValueError as e: raise ValueError(f"Patching `{relative}` failed: {e}") from e except LookupError as e: diagnostics.append(f"Skipped `{relative}`: {e}") continue if updated != original: pending.append((path, updated)) for path, updated in pending: replace(path, updated) diagnostics.append(f"Patched {path}") if diagnostics: notify("\n".join(diagnostics)) except (OSError, ValueError) as e: notify("\n".join([f"Encountered an error: {e}", *diagnostics])) return 1 os.execvp(command[0], command) if __name__ == "__main__": sys.exit(main(sys.argv[1:]))