diff --git a/source/mkw/Patch/PatchOperation.py b/source/mkw/Patch/PatchOperation.py index 2a32096..99a0583 100644 --- a/source/mkw/Patch/PatchOperation.py +++ b/source/mkw/Patch/PatchOperation.py @@ -261,16 +261,28 @@ class PatchOperation: """ # remove the last extension of the filename patched_file_name = file_name.rsplit(".", 1)[0] - patch_content = BytesIO() + patch_content = BytesIO(img.encode_data(file_content.read(), self.encoding)) - # write the encoded image into the file - patch_content.write( - img.encode_data(file_content.read(), self.encoding) - ) - - patch_content.seek(0) return patched_file_name, patch_content + class ImageDecoder(Operation): + """ + decode a game image to a image file + """ + + type = "img-decode" + + def patch(self, patch: "Patch", file_name: str, file_content: IO) -> (str, IO): + """ + Patch a file to encode it in a game image file + :param patch: the patch that is applied + :param file_name: the file_name of the file + :param file_content: the content of the file + :return: the new name and new content of the file + """ + patch_content = BytesIO(img.decode_data(file_content.read())) + return f"{file_name}.png", patch_content + class BmgEditor(Operation): """ edit a bmg diff --git a/source/wt/img.py b/source/wt/img.py index b4801b4..00b925e 100644 --- a/source/wt/img.py +++ b/source/wt/img.py @@ -24,6 +24,22 @@ def encode_data(image_data: bytes, transform: str = "CMPR") -> bytes: return stdout +@better_wt_error(tools_path) +def decode_data(image_data: bytes) -> bytes: + """ + Convert the game image data and return the decoded image data + :param image_data: the original game image data + :return: the decoded image data + """ + process = _tools_run_popen("DECODE", "-", "--DEST", "-") + stdout, _ = process.communicate(input=image_data) + + if process.returncode != 0: + raise WTError(tools_path, process.returncode) + + return stdout + + class IMGPath: """ Represent a path to a normal image, that can be converted into game image data