diff options
Diffstat (limited to 'frame-steamos/template.py')
| -rw-r--r-- | frame-steamos/template.py | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/frame-steamos/template.py b/frame-steamos/template.py new file mode 100644 index 0000000..eff1810 --- /dev/null +++ b/frame-steamos/template.py @@ -0,0 +1,138 @@ +#!/usr/bin/python3 -I +# SPDX-FileCopyrightText: 2026 Tomasz Kramkowski <tomasz@kramkow.ski> +# SPDX-License-Identifier: CC0-1.0 + +"""A minimally intrusive Frame SteamOS patcher. + +This file is generated. + +See https://kramkow.ski/project/steamvr-patches.html and +https://the-tk.com/cgit/steamvr-patches/about/ for more information. +""" + +import argparse +import hashlib +import json +import os +import subprocess +import sys +from dataclasses import dataclass +from io import BytesIO +from pathlib import Path + +MARKER = "x-valve-please-fix" +RUNTIME = Path(os.environ.get("RUNTIME_DIRECTORY", "/run/patcher")) + + +@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: + result = BytesIO(data) + for edit in self.edits: + result.seek(edit.offset) + result.write(edit.replacement) + patched = result.getvalue() + if hashlib.sha256(patched).digest() != self.patched_hash: + raise ValueError("patched file checksum mismatch") + return patched + + +# 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 = {} +PATCHES = {name: Patch.from_dict(data) for name, data in PATCH_DATA.items()} + + +def start(): + skipped = False + for name, patch in PATCHES.items(): + target = Path(name).resolve(strict=True) + data = target.read_bytes() + checksum = hashlib.sha256(data).digest() + if checksum != patch.original_hash: + print( + f"Skipping {target}: unrecognised checksum {checksum.hex()}", + file=sys.stderr, + ) + skipped = True + continue + patched = patch.apply_to(data) + source = RUNTIME / hashlib.sha256(os.fsencode(target)).hexdigest() + source.parent.mkdir(parents=True, exist_ok=True) + source.write_bytes(patched) + attributes = target.stat() + os.chown(source, attributes.st_uid, attributes.st_gid) + source.chmod(attributes.st_mode) + subprocess.run( + ["mount", "-o", f"bind,ro,{MARKER}", str(source), str(target)], + check=True, + ) + print(f"Patched {target}", file=sys.stderr) + if skipped: + raise SystemExit(1) + + +def stop(): + result = subprocess.run( + [ + "findmnt", + "--mtab", + "--options", + MARKER, + "--json", + "--output", + "TARGET", + ], + capture_output=True, + text=True, + ) + if ( + result.returncode == 1 + and not result.stdout.strip() + and not result.stderr.strip() + ): + return + result.check_returncode() + for target in reversed( + [ + Path(row["target"]) + for row in json.loads(result.stdout).get("filesystems", []) + ] + ): + subprocess.run(["umount", str(target)], check=True) + print(f"Unmounted {target}", file=sys.stderr) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("action", choices=("start", "stop")) + args = parser.parse_args() + if args.action == "start": + start() + else: + stop() + + +if __name__ == "__main__": + main() |
