mirror of
https://github.com/Faraphel/Atlas-Install.git
synced 2025-07-04 03:38:26 +02:00
moved safe_eval from Patch to ModConfig for more global access
This commit is contained in:
parent
5ea1d87974
commit
d25bc524c7
9 changed files with 29 additions and 24 deletions
|
@ -11,7 +11,7 @@ from source.mkw.Track import Track
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from source.mkw.OriginalTrack import OriginalTrack
|
from source.mkw.OriginalTrack import OriginalTrack
|
||||||
from source.safe_eval import safe_eval
|
from source.safe_eval import safe_eval, multiple_safe_eval
|
||||||
from source.wt.szs import SZSPath
|
from source.wt.szs import SZSPath
|
||||||
|
|
||||||
CT_ICON_SIZE: int = 128
|
CT_ICON_SIZE: int = 128
|
||||||
|
@ -106,6 +106,20 @@ class ModConfig:
|
||||||
macros=json.loads(macros_file.read_text(encoding="utf8")) if macros_file.exists() else None,
|
macros=json.loads(macros_file.read_text(encoding="utf8")) if macros_file.exists() else None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def safe_eval(self, template: str, multiple: bool = False, env: dict[str, any] = None) -> str:
|
||||||
|
"""
|
||||||
|
Safe eval with a patch environment
|
||||||
|
:param multiple: should the expression be a multiple safe eval or a single safe eval
|
||||||
|
:param env: other variable that are allowed in the safe_eval
|
||||||
|
:param template: template to evaluate
|
||||||
|
:return: the result of the evaluation
|
||||||
|
"""
|
||||||
|
return (multiple_safe_eval if multiple else safe_eval)(
|
||||||
|
template,
|
||||||
|
env={"mod_config": self} | (env if env is not None else {}),
|
||||||
|
macros=self.macros,
|
||||||
|
)
|
||||||
|
|
||||||
def get_mod_directory(self) -> Path:
|
def get_mod_directory(self) -> Path:
|
||||||
"""
|
"""
|
||||||
Get the directory of the mod
|
Get the directory of the mod
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
from typing import Generator, IO
|
from typing import Generator, IO
|
||||||
|
|
||||||
from source.mkw.Patch import *
|
from source.mkw.Patch import *
|
||||||
from source.safe_eval import safe_eval, multiple_safe_eval
|
|
||||||
|
|
||||||
|
|
||||||
class Patch:
|
class Patch:
|
||||||
|
@ -17,20 +16,6 @@ class Patch:
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<{self.__class__.__name__} {self.path}>"
|
return f"<{self.__class__.__name__} {self.path}>"
|
||||||
|
|
||||||
def safe_eval(self, template: str, multiple: bool = False, env: dict[str, any] = None) -> str:
|
|
||||||
"""
|
|
||||||
Safe eval with a patch environment
|
|
||||||
:param multiple: should the expression be a multiple safe eval or a single safe eval
|
|
||||||
:param env: other variable that are allowed in the safe_eval
|
|
||||||
:param template: template to evaluate
|
|
||||||
:return: the result of the evaluation
|
|
||||||
"""
|
|
||||||
return (multiple_safe_eval if multiple else safe_eval)(
|
|
||||||
template,
|
|
||||||
env={"mod_config": self.mod_config} | (env if env is not None else {}),
|
|
||||||
macros=self.mod_config.macros,
|
|
||||||
)
|
|
||||||
|
|
||||||
def install(self, extracted_game: "ExtractedGame") -> Generator[dict, None, None]:
|
def install(self, extracted_game: "ExtractedGame") -> Generator[dict, None, None]:
|
||||||
"""
|
"""
|
||||||
patch a game with this Patch
|
patch a game with this Patch
|
||||||
|
|
|
@ -24,7 +24,8 @@ class PatchDirectory(PatchObject):
|
||||||
"""
|
"""
|
||||||
yield {"description": f"Patching {self}"}
|
yield {"description": f"Patching {self}"}
|
||||||
|
|
||||||
if self.patch.safe_eval(self.configuration["if"], env={"extracted_game": extracted_game}) != "True": return
|
if self.patch.mod_config.safe_eval(self.configuration["if"], env={"extracted_game": extracted_game}) != "True":
|
||||||
|
return
|
||||||
|
|
||||||
match self.configuration["mode"]:
|
match self.configuration["mode"]:
|
||||||
# if the mode is copy, then simply patch the subfile into the game with the same path
|
# if the mode is copy, then simply patch the subfile into the game with the same path
|
||||||
|
|
|
@ -31,7 +31,8 @@ class PatchFile(PatchObject):
|
||||||
yield {"description": f"Patching {self}"}
|
yield {"description": f"Patching {self}"}
|
||||||
|
|
||||||
# check if the file should be patched considering the "if" configuration
|
# check if the file should be patched considering the "if" configuration
|
||||||
if self.patch.safe_eval(self.configuration["if"], env={"extracted_game": extracted_game}) != "True": return
|
if self.patch.mod_config.safe_eval(self.configuration["if"], env={"extracted_game": extracted_game}) != "True":
|
||||||
|
return
|
||||||
|
|
||||||
# check if the path to the game_subpath is inside a szs, and if yes extract it
|
# check if the path to the game_subpath is inside a szs, and if yes extract it
|
||||||
for szs_subpath in filter(lambda path: path.suffix == ".d",
|
for szs_subpath in filter(lambda path: path.suffix == ".d",
|
||||||
|
|
|
@ -16,6 +16,6 @@ class IDLayer(AbstractLayer):
|
||||||
|
|
||||||
def patch_bmg(self, patch: "Patch", decoded_content: str) -> str:
|
def patch_bmg(self, patch: "Patch", decoded_content: str) -> str:
|
||||||
return decoded_content + "\n" + ("\n".join(
|
return decoded_content + "\n" + ("\n".join(
|
||||||
[f" {id}\t= {patch.safe_eval(repl, multiple=True)}" for id, repl in self.template.items()]
|
[f" {id}\t= {patch.mod_config.safe_eval(repl, multiple=True)}" for id, repl in self.template.items()]
|
||||||
)) + "\n"
|
)) + "\n"
|
||||||
# add new bmg definition at the end of the bmg file, overwritting old id.
|
# add new bmg definition at the end of the bmg file, overwritting old id.
|
||||||
|
|
|
@ -28,7 +28,7 @@ class RegexLayer(AbstractLayer):
|
||||||
for pattern, repl in self.template.items():
|
for pattern, repl in self.template.items():
|
||||||
value = re.sub(
|
value = re.sub(
|
||||||
pattern,
|
pattern,
|
||||||
patch.safe_eval(repl, multiple=True),
|
patch.mod_config.safe_eval(repl, multiple=True),
|
||||||
value,
|
value,
|
||||||
flags=re.DOTALL
|
flags=re.DOTALL
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,6 +3,9 @@ from PIL import ImageDraw, Image
|
||||||
from source.mkw.Patch.PatchOperation.Operation.ImageEditor.Layer import *
|
from source.mkw.Patch.PatchOperation.Operation.ImageEditor.Layer import *
|
||||||
|
|
||||||
|
|
||||||
|
Patch: any
|
||||||
|
|
||||||
|
|
||||||
class ColorLayer(AbstractLayer):
|
class ColorLayer(AbstractLayer):
|
||||||
"""
|
"""
|
||||||
Represent a layer that fill a rectangle with a certain color on the image
|
Represent a layer that fill a rectangle with a certain color on the image
|
||||||
|
|
|
@ -38,7 +38,7 @@ class TextLayer(AbstractLayer):
|
||||||
)
|
)
|
||||||
draw.text(
|
draw.text(
|
||||||
self.get_layer_position(image),
|
self.get_layer_position(image),
|
||||||
text=patch.safe_eval(self.text, multiple=True),
|
text=patch.mod_config.safe_eval(self.text, multiple=True),
|
||||||
fill=self.color,
|
fill=self.color,
|
||||||
font=font
|
font=font
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,7 +4,6 @@ from source.mkw import Tag, Slot
|
||||||
from source.mkw.MKWColor import bmg_color_text
|
from source.mkw.MKWColor import bmg_color_text
|
||||||
from source.safe_eval import multiple_safe_eval
|
from source.safe_eval import multiple_safe_eval
|
||||||
|
|
||||||
|
|
||||||
ModConfig: any
|
ModConfig: any
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,13 +52,14 @@ class Track:
|
||||||
:return: formatted representation of the track
|
:return: formatted representation of the track
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return multiple_safe_eval(
|
return mod_config.safe_eval(
|
||||||
template,
|
template,
|
||||||
env={
|
env={
|
||||||
"track": self,
|
"track": self,
|
||||||
"prefix": self.get_prefix(mod_config, ""),
|
"prefix": self.get_prefix(mod_config, ""),
|
||||||
"suffix": self.get_suffix(mod_config, "")
|
"suffix": self.get_suffix(mod_config, "")
|
||||||
}
|
},
|
||||||
|
multiple=True
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_tag_template(self, templates: dict[str, str], default: any = None) -> any:
|
def get_tag_template(self, templates: dict[str, str], default: any = None) -> any:
|
||||||
|
@ -71,6 +71,7 @@ class Track:
|
||||||
"""
|
"""
|
||||||
for tag in filter(lambda tag: tag in templates, self.tags):
|
for tag in filter(lambda tag: tag in templates, self.tags):
|
||||||
template: str = templates[tag]
|
template: str = templates[tag]
|
||||||
|
# TODO: this should try to use mod_config for save_eval
|
||||||
return multiple_safe_eval(template, env={"TAG": tag, "bmg_color_text": bmg_color_text})
|
return multiple_safe_eval(template, env={"TAG": tag, "bmg_color_text": bmg_color_text})
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue