removed the need of temporary file by using stdin as input data

This commit is contained in:
Faraphel 2022-07-10 23:56:57 +02:00
parent d456430714
commit 804c9f38f6
3 changed files with 43 additions and 25 deletions

View file

@ -5,7 +5,7 @@ from typing import IO
from PIL import Image, ImageDraw, ImageFont
from source.mkw.Patch import *
from source.wt.img import IMGPath
from source.wt import img
class PatchOperation:
@ -225,20 +225,11 @@ class PatchOperation:
patched_file_name = file_name.rsplit(".", 1)[0]
patch_content = BytesIO()
# write the image to a temporary directory
tmp_file = Path(f"./.tmp/{file_name}")
tmp_file.parent.mkdir(parents=True, exist_ok=True)
file_content.seek(0)
tmp_file.write_bytes(file_content.read())
# write the encoded image into the file
patch_content.write(
IMGPath(tmp_file).get_encoded_data(self.encoding)
img.encode_data(file_content.read(), self.encoding)
)
# delete the temporary directory when finished
tmp_file.unlink()
patch_content.seek(0)
return patched_file_name, patch_content

View file

@ -14,13 +14,13 @@ def _tools_run(*args) -> bytes:
return _run(tools_path, *args)
def _tools_run_popen(*args) -> subprocess.Popen:
def _tools_run_popen(*args, **kwargs) -> subprocess.Popen:
"""
Return a popen of command with wbmgt
:param args: command arguments
:return: the process of the command
"""
return _run_popen(tools_path, *args)
return _run_popen(tools_path, *args, **kwargs)
class BMGPath:

View file

@ -1,10 +1,45 @@
from source.wt import *
from source.wt import _run
from source.wt import _run, _run_popen
tools_path = tools_szs_dir / ("wimgt.exe" if system == "win64" else "wimgt")
@better_wt_error(tools_path)
def _tools_run(*args) -> bytes:
"""
Return a command with wimgt and return the output
:param args: command arguments
:return: the output of the command
"""
return _run(tools_path, *args)
def _tools_run_popen(*args) -> subprocess.Popen:
"""
Return a popen of command with wimgt
:param args: command arguments
:return: the process of the command
"""
return _run_popen(tools_path, *args)
@better_wt_error(tools_path)
def encode_data(image_data: bytes, transform: str = "CMPR") -> bytes:
"""
Convert the image data and return the encoded image data
:param image_data: the original image data
:param transform: the type of the image encoding
:return: the encoded image data
"""
process = _tools_run_popen("ENCODE", "-", "--transform", transform, "--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
@ -14,20 +49,12 @@ class IMGPath:
def __init__(self, path: Path | str):
self.path: Path = Path(path)
@better_wt_error(tools_path)
def _run(self, *args) -> bytes:
"""
Return a command with wimgt and return the output
:param args: command arguments
:return: the output of the command
"""
return _run(tools_path, *args)
def get_encoded_data(self, transform: str = "CMPR") -> bytes:
"""
Convert the image return the encoded image data
Convert the image and return the encoded image data
:transform: the type of the image encoding
:return: the data of the encoded image
"""
# using "-" for destination allow for output in the stdout
return self._run("ENCODE", self.path, "--transform", transform, "--DEST", "-")
return _tools_run("ENCODE", self.path, "--transform", transform, "--DEST", "-")