#!/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": "2916c9993a27d49cdc693a540eefda3ca1d50a569a53edf1bc3b1ea7e19e688e", "patched_hash": "75cb7cabf767b1d7451fe8e5a0a0dc9fea9611febda90c4a8a3f7a4b0c97e33e", "edits": [ (0x4098d1, "498b5500b80300000089424ceb02"), (0x4098e1, "89828c0000008982a4000000eb02"), (0x4098f1, "4c8bbd18feffffe9f2200100"), (0x41b9e8, "e9e4defeff9090"), (0x433d6b, "e970b50e009090"), (0x412a00, "e918c910009090"), (0x20, "0000510000000000"), (0x38, "0d00"), ( 0x510000, "0600000004000000000051000000000000005200000000000000520000000000" "d802000000000000d80200000000000008000000000000000300000004000000" "e002000000000000e002000000000000e0020000000000001c00000000000000" "1c0000000000000001000000000000000400000004000000fc02000000000000" "fc02000000000000fc0200000000000044000000000000004400000000000000" "0400000000000000010000000400000000000000000000000000000000000000" "0000000000000000687515000000000068751500000000000010000000000000" "0100000005000000687515000000000068851500000000006885150000000000" "96b439000000000096b439000000000000100000000000000100000006000000" "002a4f0000000000004a4f0000000000004a4f000000000080b0010000000000" "00b60100000000000010000000000000010000000600000080da500000000000" "800a510000000000800a510000000000481100000000000020f1000000000000" "00100000000000000700000004000000002a4f0000000000004a4f0000000000" "004a4f0000000000000000000000000030000000000000000800000000000000" "020000000600000020c250000000000020e250000000000020e2500000000000" "00030000000000000003000000000000080000000000000050e5746404000000" "10410c000000000010410c000000000010410c00000000001495010000000000" "1495010000000000040000000000000051e57464060000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "010000000000000052e5746404000000002a4f0000000000004a4f0000000000" "004a4f000000000080b001000000000000b60100000000000100000000000000" "0100000005000000000051000000000000005200000000000000520000000000" "f604000000000000f6040000000000000010000000000000" ), ( 0x5102e0, "505152565741504151415241534883ec08488b7dc04889c68b5518e83e000000" "4883c408415b415a415941585f5e5a5958837d18014989c7e9554af1ff575652" "514883ec08e8010100004883c408595a5e5f554889f04189d1e9c936efff83fa" "010f87e300000041554154555351488b9ed80000004885db0f84c4000000488b" "034889fd4889dfff90d800000083c00383f8010f87a9000000488b03488b78b0" "4801df488b07ff50304989c44885c00f848d000000488b004c89e7ff50404885" "c0747f488b9500110000488b8d081100004839d1740b48390274674883c210eb" "f0498b04244c89e7ff50704989c54885c0744f8b9d3802000083fb0177444883" "c32448c1e3044801eb48833b0074334881c5c81500004889efe8a28ec3ff498b" "04244889da4c89e74c89ee31c9ff90800000005a4889ef5b5d415c415de96e92" "c3ff585b5d415c415dc3c34154554889fd53488b7e484885ff0f84b200000048" "8b07480378b0488b07ff50304889c34885c00f8499000000488b004889dfff50" "404989c44885c00f8484000000488b85b0120000488b6808488b859803000048" "8b95a00300004839c2740b4c392074614883c010ebf0488b034889dfff507048" "8b95f0000000488bb5f80000004889c1488b85400100004839d674214c392274" "0a4883c2104883c004ebec48394a08730448894a08810800000100eb145b4c89" "e64889efba000001005d415ce97fcff4ff5b5d415cc3" ), ], }, 'drivers/vrlink/bin/linux64/driver_vrlink.so': { "original_hash": "7b6a49016989091e36b1276b949aecdb2bf0365353380b12dc7e9b66fb7d797d", "patched_hash": "5a0b66140c462fc9153bf1b4e8d82e2dd8899a60f9fb97149350681fceaf88ac", "edits": [ (0x1177c1, "89d02b47084189c6c1e004"), (0x1177cc, "0f88ce050000"), (0x10eb51, "57488b7f184885ff740feb04"), (0x10eb61, "488b076a015eff50285fe99018feff"), (0x116701, "4c89a30804000031c0894308c3"), (0x116751, "e8abffffff6690"), (0x116764, "e8e883ffff"), ], }, } 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:]))