fixed safe_eval for the Patch, and the region condition in the MKWF patch

This commit is contained in:
Faraphel 2022-06-29 11:55:47 +02:00
parent 5481b75cbf
commit 40de14d201
7 changed files with 24 additions and 9 deletions

View file

@ -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'"
}

View file

@ -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'"
}

View file

@ -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'"
}

View file

@ -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'"
}

View file

@ -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]:
"""

View file

@ -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)

View file

@ -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 = {