diff options
Diffstat (limited to 'src/steamvr_patches/__init__.py')
| -rw-r--r-- | src/steamvr_patches/__init__.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/steamvr_patches/__init__.py b/src/steamvr_patches/__init__.py index 8ad589a..0daea79 100644 --- a/src/steamvr_patches/__init__.py +++ b/src/steamvr_patches/__init__.py @@ -40,6 +40,7 @@ class PatchSet(BaseModel): target: str targets: list[FilePatch] + template: Path | None = None @field_validator("targets") @classmethod @@ -52,6 +53,8 @@ class PatchSet(BaseModel): @classmethod def parse(cls, config: dict, directory: Path) -> Self: result = cls.model_validate(config) + if result.template is not None: + result.template = directory / result.template for target in result.targets: if target.compiled is not None: target.compiled.sources = [ @@ -87,8 +90,9 @@ def compile_patch(config: FilePatch, source: BinaryIO, output: Path) -> dict: } -def render(patches: dict[str, dict]) -> str: - template = files("steamvr_patches").joinpath("template.py").read_text() +def render(patches: dict[str, dict], template: str | None = None) -> str: + if template is None: + template = files("steamvr_patches").joinpath("template.py").read_text() placeholder = "PATCH_DATA = {}" if template.count(placeholder) != 1: raise ValueError("missing or duplicate runtime patch-table placeholder") @@ -121,11 +125,17 @@ def generate(patch_data: Path, input_root: Path) -> str: patches = {} with tempfile.TemporaryDirectory(prefix="generate-patcher-") as temporary: for index, config in enumerate(definition.targets): - with (input_root / config.path).open("rb") as source: + relative = ( + config.path.relative_to("/") + if config.path.is_absolute() + else config.path + ) + with (input_root / relative).open("rb") as source: patches[config.path.as_posix()] = compile_patch( config, source, Path(temporary) / str(index) ) - return render(patches) + template = definition.template.read_text() if definition.template else None + return render(patches, template) def main() -> None: |
