implemented decode_data in img (tpl -> png) and added img-decode operation (useful if a texture modification need the original game image as a base)

This commit is contained in:
Faraphel 2022-07-14 18:30:06 +02:00
parent 5ddae33393
commit ebb74a61df
2 changed files with 35 additions and 7 deletions

View file

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

View file

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