diff --git a/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-JAP.bin.json b/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-JAP.bin.json index 6d71e2a..0643fd9 100644 --- a/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-JAP.bin.json +++ b/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-JAP.bin.json @@ -1,3 +1,3 @@ { - "if": "getattr(mod_config, 'extension', False) == 'JAP'" + "if": "str(getattr(getattr(getattr(extracted_game, 'original_game'), 'wit_path'), 'region')) == 'Region.JAP'" } \ No newline at end of file diff --git a/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-KOR.bin.json b/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-KOR.bin.json index 48e61df..1693c55 100644 --- a/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-KOR.bin.json +++ b/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-KOR.bin.json @@ -1,3 +1,3 @@ { - "if": "getattr(mod_config, 'extension', False) == 'KOR'" + "if": "str(getattr(getattr(getattr(extracted_game, 'original_game'), 'wit_path'), 'region')) == 'Region.KOR'" } \ No newline at end of file diff --git a/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-PAL.bin.json b/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-PAL.bin.json index 655183d..7df6e25 100644 --- a/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-PAL.bin.json +++ b/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-PAL.bin.json @@ -1,3 +1,3 @@ { - "if": "getattr(mod_config, 'extension', False) == 'PAL'" + "if": "str(getattr(getattr(getattr(extracted_game, 'original_game'), 'wit_path'), 'region')) == 'Region.PAL'" } \ No newline at end of file diff --git a/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-USA.bin.json b/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-USA.bin.json index 8281653..550244a 100644 --- a/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-USA.bin.json +++ b/Pack/MKWFaraphel/essentials/_PATCH/files/rel/lecode-USA.bin.json @@ -1,3 +1,3 @@ { - "if": "getattr(mod_config, 'extension', False) == 'USA'" + "if": "str(getattr(getattr(getattr(extracted_game, 'original_game'), 'wit_path'), 'region')) == 'Region.USA'" } \ No newline at end of file diff --git a/source/mkw/ExtractedGame.py b/source/mkw/ExtractedGame.py index bcffb98..413e0c5 100644 --- a/source/mkw/ExtractedGame.py +++ b/source/mkw/ExtractedGame.py @@ -11,8 +11,9 @@ class ExtractedGame: Class that represents an extracted game """ - def __init__(self, path: Path | str): + def __init__(self, path: Path | str, original_game: "Game" = None): self.path = Path(path) + self.original_game = original_game def extract_autoadd(self, destination_path: Path | str) -> Generator[dict, None, None]: """ diff --git a/source/mkw/Game.py b/source/mkw/Game.py index 2594bd9..9d4a3a9 100644 --- a/source/mkw/Game.py +++ b/source/mkw/Game.py @@ -99,7 +99,7 @@ class Game: cache_directory.mkdir(parents=True, exist_ok=True) # get the directory where the game will be extracted - extracted_game = ExtractedGame(self.get_output_directory(dest, mod_config)) + extracted_game = ExtractedGame(self.get_output_directory(dest, mod_config), self) if not self.is_mkw(): raise NotMKWGameError(self.wit_path.path) if not self.is_vanilla(): raise NotVanillaError(self.wit_path.path) diff --git a/source/mkw/Patch.py b/source/mkw/Patch.py index 6a62935..1967e66 100644 --- a/source/mkw/Patch.py +++ b/source/mkw/Patch.py @@ -6,6 +6,8 @@ from abc import abstractmethod, ABC from typing import Generator, IO from io import BytesIO +from source.safe_eval import safe_eval + class PathOutsidePatch(Exception): def __init__(self, forbidden_path: Path, allowed_range: Path): @@ -38,6 +40,19 @@ class Patch: def __repr__(self) -> str: return f"<{self.__class__.__name__} {self.path}>" + def safe_eval(self, template: str, extracted_game: "ExtractedGame") -> str: + """ + Safe eval with a patch environment + :param extracted_game: the extracted game to patch + :param template: template to evaluate + :return: the result of the evaluation + """ + return safe_eval( + template, + extra_token_map={"extracted_game": "extracted_game"}, + env={"extracted_game": extracted_game}, + ) + def install(self, extracted_game: "ExtractedGame") -> Generator[dict, None, None]: """ patch a game with this Patch @@ -336,7 +351,7 @@ class PatchFile(PatchObject): print(f"special file : {self} [install to {game_subpath}]") return - if not safe_eval(self.configuration["if"]): return + if self.patch.safe_eval(self.configuration["if"], extracted_game) == "False": return # apply operation on the file patch_source: Path = self.get_source_path(game_subpath) @@ -397,7 +412,7 @@ class PatchDirectory(PatchObject): """ yield {"description": f"Patching {self}"} - if not safe_eval(self.configuration["if"]): return + if self.patch.safe_eval(self.configuration["if"], extracted_game) == "False": return match self.configuration["mode"]: # if the mode is copy, then simply patch the subfile into the game with the same path @@ -425,7 +440,6 @@ class PatchDirectory(PatchObject): # TODO : extract SZS # TODO : implement TPL # TODO : implement BMG -# TODO : safe_eval configuration_example = {