added bmg regex operation support + decoding and encoding are now only done at the start and end of a whole operation instead at every Layer

This commit is contained in:
Faraphel 2022-07-12 22:42:41 +02:00
parent bd52034cc6
commit cab9b7745d

View file

@ -6,6 +6,7 @@ from typing import IO
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
from source.mkw.Patch import * from source.mkw.Patch import *
from source.safe_eval import safe_eval
from source.wt import img, bmg from source.wt import img, bmg
@ -197,7 +198,12 @@ class PatchOperation:
font_image_path, font_image_path,
size=self.get_font_size(image) size=self.get_font_size(image)
) )
draw.text(self.get_layer_position(image), text=self.text, fill=self.color, font=font) draw.text(
self.get_layer_position(image),
text=patch.safe_eval(self.text, multiple=True),
fill=self.color,
font=font
)
return image return image
@ -248,10 +254,13 @@ class PatchOperation:
self.layers = layers self.layers = layers
def patch(self, patch: "Patch", file_name: str, file_content: IO) -> (str, IO): def patch(self, patch: "Patch", file_name: str, file_content: IO) -> (str, IO):
for layer in self.layers: decoded_content = bmg.decode_data(file_content.read())
file_content = self.Layer(layer).patch_bmg(patch, file_content)
return file_name, file_content for layer in self.layers:
decoded_content = self.Layer(layer).patch_bmg(patch, decoded_content)
patch_content = BytesIO(bmg.encode_data(decoded_content))
return file_name, patch_content
class Layer: class Layer:
""" """
@ -271,7 +280,7 @@ class PatchOperation:
class AbstractLayer(ABC): class AbstractLayer(ABC):
@abstractmethod @abstractmethod
def patch_bmg(self, patch: "Patch", file_content: IO) -> IO: def patch_bmg(self, patch: "Patch", decoded_content: str) -> str:
""" """
Patch a bmg with the actual layer. Return the new bmg content. Patch a bmg with the actual layer. Return the new bmg content.
""" """
@ -286,17 +295,12 @@ class PatchOperation:
def __init__(self, template: dict[str, str]): def __init__(self, template: dict[str, str]):
self.template = template self.template = template
def patch_bmg(self, patch: "Patch", file_content: IO) -> IO: def patch_bmg(self, patch: "Patch", decoded_content: str) -> str:
decoded_bmg = bmg.decode_data(file_content.read()) return decoded_content + "\n" + ("\n".join(
[f" {id}\t= {patch.safe_eval(repl, multiple=True)}" for id, repl in self.template.items()]
decoded_bmg += "\n" + ("\n".join(
[f" {id}\t= {replacement}" for id, replacement 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.
patch_content = BytesIO(bmg.encode_data(decoded_bmg))
return patch_content
class RegexLayer(AbstractLayer): class RegexLayer(AbstractLayer):
""" """
Represent a layer that replace bmg entry by matching them with a regex Represent a layer that replace bmg entry by matching them with a regex
@ -307,5 +311,26 @@ class PatchOperation:
def __init__(self, template: dict[str, str]): def __init__(self, template: dict[str, str]):
self.template = template self.template = template
def patch_bmg(self, patch: "Patch", file_content: IO) -> IO: def patch_bmg(self, patch: "Patch", decoded_content: str) -> str:
return file_content new_bmg_lines: list[str] = []
for line in decoded_content.split("\n"):
if (match := re.match(r"^ {2}(?P<id>.*?)\t= (?P<value>.*)$", line, re.DOTALL)) is None:
# check if the line match a bmg definition, else ignore
# bmg definition is : 2 spaces, a bmg id, a tab, an equal sign, a space and the bmg text
continue
new_bmg_id: str = match.group("id")
new_bmg_def: str = match.group("value")
for pattern, repl in self.template.items():
new_bmg_def = re.sub(
pattern,
patch.safe_eval(repl, multiple=True),
new_bmg_def,
flags=re.DOTALL
)
# match a pattern from the template, and replace it with its repl
new_bmg_lines.append(f" {new_bmg_id}\t={new_bmg_def}")
return decoded_content + "\n" + ("\n".join(new_bmg_lines)) + "\n"
# add every new line to the end of the decoded_bmg, old bmg_id will be overwritten.