From f9630fa13b8136d1edd16bec6ae4586e7271c67e Mon Sep 17 00:00:00 2001 From: NewSoupVi <57900059+NewSoupVi@users.noreply.github.com> Date: Tue, 25 Nov 2025 00:38:42 +0100 Subject: [PATCH] Core: Add a bunch of validation to AutoPatchRegister (#5431) * Add a bunch of validation to AutoPatchRegister * slightly change it * lmao --- worlds/Files.py | 28 ++++++++++++++++++++++++++-- worlds/adventure/Rom.py | 4 ++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/worlds/Files.py b/worlds/Files.py index a41424183f..ddd1f4e1ce 100644 --- a/worlds/Files.py +++ b/worlds/Files.py @@ -21,6 +21,10 @@ if TYPE_CHECKING: from Utils import Version +class ImproperlyConfiguredAutoPatchError(Exception): + pass + + class AutoPatchRegister(abc.ABCMeta): patch_types: ClassVar[Dict[str, AutoPatchRegister]] = {} file_endings: ClassVar[Dict[str, AutoPatchRegister]] = {} @@ -30,8 +34,28 @@ class AutoPatchRegister(abc.ABCMeta): new_class = super().__new__(mcs, name, bases, dct) if "game" in dct: AutoPatchRegister.patch_types[dct["game"]] = new_class - if not dct["patch_file_ending"]: - raise Exception(f"Need an expected file ending for {name}") + + if not callable(getattr(new_class, "patch", None)): + raise ImproperlyConfiguredAutoPatchError( + f"Container {new_class} uses metaclass AutoPatchRegister, but does not have a patch method defined." + ) + + patch_file_ending = dct.get("patch_file_ending") + if patch_file_ending == ".zip": + raise ImproperlyConfiguredAutoPatchError( + f'Auto patch container {new_class} uses file ending ".zip", which is not allowed.' + ) + if patch_file_ending is None: + raise ImproperlyConfiguredAutoPatchError( + f"Need an expected file ending for auto patch container {new_class}" + ) + + existing_handler = AutoPatchRegister.file_endings.get(patch_file_ending) + if existing_handler: + raise ImproperlyConfiguredAutoPatchError( + f"Two auto patch containers are using the same file extension: {new_class}, {existing_handler}" + ) + AutoPatchRegister.file_endings[dct["patch_file_ending"]] = new_class return new_class diff --git a/worlds/adventure/Rom.py b/worlds/adventure/Rom.py index cb104c56d8..e603ed06bc 100644 --- a/worlds/adventure/Rom.py +++ b/worlds/adventure/Rom.py @@ -8,7 +8,7 @@ import bsdiff4 import Utils from settings import get_settings -from worlds.Files import APPatch, AutoPatchRegister +from worlds.Files import APPatch from .Locations import LocationData ADVENTUREHASH: str = "157bddb7192754a45372be196797f284" @@ -78,7 +78,7 @@ class BatNoTouchLocation: return ret_dict -class AdventureDeltaPatch(APPatch, metaclass=AutoPatchRegister): +class AdventureDeltaPatch(APPatch): hash = ADVENTUREHASH game = "Adventure" patch_file_ending = ".apadvn"