Skip to content

Commit

Permalink
Core: APPatch interface (#2808)
Browse files Browse the repository at this point in the history
define interface that has only the bare minimum required
for `Patch.create_rom_file`
  • Loading branch information
beauxq authored Feb 14, 2024
1 parent f178d43 commit 475e803
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import ModuleUpdate
ModuleUpdate.update()

from worlds.Files import AutoPatchRegister, APDeltaPatch
from worlds.Files import AutoPatchRegister, APPatch


class RomMeta(TypedDict):
Expand All @@ -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: APDeltaPatch = 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,
Expand Down
20 changes: 16 additions & 4 deletions worlds/Files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import abc
import json
import zipfile
import os
Expand All @@ -15,7 +16,7 @@
del os


class AutoPatchRegister(type):
class AutoPatchRegister(abc.ABCMeta):
patch_types: ClassVar[Dict[str, AutoPatchRegister]] = {}
file_endings: ClassVar[Dict[str, AutoPatchRegister]] = {}

Expand Down Expand Up @@ -112,14 +113,25 @@ def get_manifest(self) -> Dict[str, Any]:
}


class APDeltaPatch(APContainer, metaclass=AutoPatchRegister):
"""An APContainer that additionally has delta.bsdiff4
class APPatch(APContainer, abc.ABC, metaclass=AutoPatchRegister):
"""
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 APDeltaPatch(APPatch):
"""An APPatch that additionally has delta.bsdiff4
containing a delta patch to get the desired file, often a rom."""

hash: Optional[str] # base checksum of source file
patch_file_ending: str = ""
delta: Optional[bytes] = None
result_file_ending: str = ".sfc"
source_data: bytes

def __init__(self, *args: Any, patched_path: str = "", **kwargs: Any) -> None:
Expand Down

0 comments on commit 475e803

Please sign in to comment.