#!/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": "7695e718ee0d070df596663d737b6a183aa650a2c66b90a035cc7f227a7a49e1", "patched_hash": "c0c9813a1cc184dbd4891ecf3085709f0a9137b3fdc138ce2aa14a0b65ebe34b", "edits": [ (0x4056c1, "84c00f842f570000eb06"), (0x4056d1, "488dbbe00d0000e90a570000"), (0x40addb, "e840db0600"), (0x40ade0, "e9dca8ffff9090"), (0x41dfa4, "31c09090909090"), (0x41e8b9, "31c09090909090"), (0x41e8f4, "909090909090"), (0x4056e1, "498b5500b80300000089424ceb22"), (0x405711, "89828c0000008982a4000000eb12"), (0x405731, "4c8bbd18feffffe902200100"), (0x417738, "e9a4dffeff9090"), (0x40e750, "e98b9b10009090"), (0x20, "0090500000000000"), (0x38, "0d00"), ( 0x509000, "0600000004000000009050000000000000905100000000000090510000000000" "d802000000000000d80200000000000008000000000000000300000004000000" "e002000000000000e002000000000000e0020000000000001c00000000000000" "1c0000000000000001000000000000000400000004000000fc02000000000000" "fc02000000000000fc0200000000000044000000000000004400000000000000" "0400000000000000010000000400000000000000000000000000000000000000" "0000000000000000f851150000000000f8511500000000000010000000000000" "0100000005000000f851150000000000f861150000000000f861150000000000" "f66e390000000000f66e39000000000000100000000000000100000006000000" "00c14e000000000000e14e000000000000e14e000000000028b0010000000000" "00bf010000000000001000000000000001000000060000004071500000000000" "40a150000000000040a15000000000004811000000000000a0ee000000000000" "001000000000000007000000040000008882500000000000f0e04e0000000000" "f0e04e0000000000000000000000000030000000000000000800000000000000" "0200000006000000c058500000000000c078500000000000c078500000000000" "00030000000000000003000000000000080000000000000050e5746404000000" "30380c000000000030380c000000000030380c00000000005c93010000000000" "5c93010000000000040000000000000051e57464060000000000000000000000" "0000000000000000000000000000000000000000000000000000000000000000" "010000000000000052e574640400000000c14e000000000000e14e0000000000" "00e14e000000000028b001000000000000bf0100000000000100000000000000" "0100000005000000009050000000000000905100000000000090510000000000" "cc03000000000000cc030000000000000010000000000000" ), ( 0x5092e0, "575652514883ec08e8140000004883c408595a5e5f554889f04189d1e95664ef" "ff4154554889fd53488b7e484885ff0f84b2000000488b07480378b0488b07ff" "50304889c34885c00f8499000000488b004889dfff50404989c44885c00f8484" "000000488b85b0120000488b6808488b8598030000488b95a00300004839c274" "0b4c392074614883c010ebf0488b034889dfff5070488b95f0000000488bb5f8" "0000004889c1488b85400100004839d674214c3922740a4883c2104883c004eb" "ec48394a08730448894a08810800000100eb145b4c89e64889efba000001005d" "415ce999d7f4ff5b5d415cc3" ), ], }, 'bin/linux64/vrserver': { "original_hash": "f2caabae6e7e5e00f712766d5aa5089ef1905a4ee70575a200ea4fb063471b6a", "patched_hash": "b68d1c1919b3c18635cab0670d6f670df4bd97b8449ca8082ffa7e094f16108f", "edits": [ (0x2c12d6, "9090909090"), (0x2c14c7, "9090909090"), (0x2c155c, "9090909090"), (0x2c1676, "9090909090"), (0x2c174f, "9090909090"), (0x2c15e9, "9090909090"), ], }, 'drivers/vrlink/bin/linux64/driver_vrlink.so': { "original_hash": "df21d2496e78f5490fa1e0eb1f1b32546381a4aeee5dd6aa985982af8f6c07d7", "patched_hash": "82f1d370c1f5d2dd73a07e0f6f53843cb9db45ef585cd98a175ee5ad4c2f0a0a", "edits": [ (0xd92ed, "89836c0d000090909090"), (0x13c482, "83bb4c090000007458e9702c0000"), (0x13c4e3, "488b8328090000e9ec210000"), (0x13e6d4, "e9a9ddffff9090"), (0x13d8e5, "e927000000"), (0x13d911, "488b9d70ffffff4c01f3e9d6feffff"), (0x112bf1, "57488b7f184885ff740feb04"), (0x112c01, "488b076a015eff50285fe90013feff"), (0x11a3f1, "4c89a30804000031c0894308c3"), (0x11a441, "e8abffffff6690"), (0x11a454, "e89887ffff"), (0x11b4b1, "89d02b47084189c6c1e004"), (0x11b4bc, "0f8853050000"), (0x11b7c4, "0f84f718fdff"), (0xed0c1, "410fb644240988430ee946e90200"), ], }, } 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:]))