From bed93a054c6bdbe780b2a6c311d55c64745585d5 Mon Sep 17 00:00:00 2001 From: beauxq Date: Wed, 6 Dec 2023 19:31:32 -0800 Subject: [PATCH] define interface that has only the bare minimum required for `Patch.create_rom_file` --- Patch.py | 4 ++-- worlds/Files.py | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Patch.py b/Patch.py index fac5a406a3..0915457000 100644 --- a/Patch.py +++ b/Patch.py @@ -8,7 +8,7 @@ if __name__ == "__main__": import ModuleUpdate ModuleUpdate.update() -from worlds.Files import AutoPatchRegister, APProcedurePatch +from worlds.Files import AutoPatchRegister, APPatch class RomMeta(TypedDict): @@ -20,7 +20,7 @@ class RomMeta(TypedDict): def create_rom_file(patch_file: str) -> Tuple[RomMeta, str]: auto_handler = AutoPatchRegister.get_handler(patch_file) if auto_handler: - handler: APProcedurePatch = auto_handler(patch_file) + handler: APPatch = auto_handler(patch_file) target = os.path.splitext(patch_file)[0]+handler.result_file_ending handler.patch(target) return {"server": handler.server, diff --git a/worlds/Files.py b/worlds/Files.py index c1149684a0..e9058ffea3 100644 --- a/worlds/Files.py +++ b/worlds/Files.py @@ -1,5 +1,6 @@ from __future__ import annotations +import abc import json import struct import zipfile @@ -17,7 +18,7 @@ del threading del os -class AutoPatchRegister(type): +class AutoPatchRegister(abc.ABCMeta): patch_types: ClassVar[Dict[str, AutoPatchRegister]] = {} file_endings: ClassVar[Dict[str, AutoPatchRegister]] = {} @@ -142,15 +143,26 @@ class APContainer: } -class APProcedurePatch(APContainer, metaclass=AutoPatchRegister): +class APPatch(APContainer, abc.ABC, metaclass=AutoPatchRegister): """ - An APContainer that defines a procedure to produce the desired file. + An abstract `APContainer` that defines the requirements for an object + to be used by the `Patch.create_rom_file` function. + """ + result_file_ending: str = ".sfc" + + @abc.abstractmethod + def patch(self, target: str) -> None: + """ create the output file with the file name `target` """ + + +class APProcedurePatch(APPatch): + """ + An APPatch that defines a procedure to produce the desired file. """ procedure: List[Tuple[str, List[Any]]] hash: Optional[str] # base checksum of source file source_data: bytes patch_file_ending: str = "" - result_file_ending: str = ".sfc" files: Dict[str, bytes] = {} @classmethod