diff --git a/source/wt/bmg.py b/source/wt/bmg.py index f254279..9ebd762 100644 --- a/source/wt/bmg.py +++ b/source/wt/bmg.py @@ -1,12 +1,54 @@ from source.wt import * -tools_path = tools_szs_dir / ("wimgt.exe" if system == "win64" else "wimgt") +tools_path = tools_szs_dir / ("wbmgt.exe" if system == "win64" else "wbmgt") _tools_run = get_tools_run_function(tools_path) _tools_run_popen = get_tools_run_popen_function(tools_path) +def decode_data(bmg_data: bytes) -> str: + """ + Decode a bmg file content into a txt content + """ + process = _tools_run_popen("DECODE", "-", "--single-line", "--DEST", "-") + stdout, _ = process.communicate(input=bmg_data) + if process.returncode != 0: + raise WTError(tools_path, process.returncode) + + return stdout.decode("utf-8") + + +def encode_data(txt_data: str) -> bytes: + """ + Encode a txt file content into a bmg content + """ + process = _tools_run_popen("ENCODE", "-", "--DEST", "-") + stdout, _ = process.communicate(input=txt_data.encode("utf-8")) + if process.returncode != 0: + raise WTError(tools_path, process.returncode) + + return stdout + + +def patch_data(bmg_data: bytes, patchs: dict[str, str | None]) -> bytes: + """ + Patch a file with LE-COPY. This copy the original tracks name into the new lecode track name id + :patchs: dictionary of patchs bmg key and value + """ + args = [] + for key, value in patchs.items(): + args.append("--patch-bmg") + args.append(key if value is None else f"{key}={value}") + + process = _tools_run_popen("PATCH", "-", *args, "--DEST", "-") + stdout, _ = process.communicate(input=bmg_data) + if process.returncode != 0: + raise WTError(tools_path, process.returncode) + + return stdout + + class BMGPath: """ Represent a path to a bmg file (game file containing text data)