diff --git a/source/mkw/ExtractedGame.py b/source/mkw/ExtractedGame.py index 565f3c4..9ac09dd 100644 --- a/source/mkw/ExtractedGame.py +++ b/source/mkw/ExtractedGame.py @@ -90,7 +90,7 @@ class ExtractedGame: # write ctfile data to a file in the cache ct_file = (cache_directory / "ctfile.lpar") - ct_file.write_text(mod_config.get_ctfile()) + ct_file.write_text(mod_config.get_ctfile(template="-")) # TODO: the lpar file should be customizable for lecode_file in (self.path / "files/rel/").glob("lecode-*.bin"): diff --git a/source/mkw/ModConfig.py b/source/mkw/ModConfig.py index 3b88dde..976a76d 100644 --- a/source/mkw/ModConfig.py +++ b/source/mkw/ModConfig.py @@ -165,9 +165,10 @@ class ModConfig: yield from self.get_ordered_cups() yield from self.get_unordered_cups() - def get_ctfile(self) -> str: + def get_ctfile(self, template: str) -> str: """ Return the ct_file generated from the ModConfig + :template: template for the track name :return: ctfile content """ lecode_flags = filter(lambda v: v is not None, [ @@ -187,7 +188,7 @@ class ModConfig: for cup in self.get_cups(): # get all the cup ctfile, use "-" for the template since the track's name are not used here - ctfile += cup.get_ctfile(mod_config=self, template="-") + ctfile += cup.get_ctfile(mod_config=self, template=template) return ctfile diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/CTFileLayer.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/CTFileLayer.py new file mode 100644 index 0000000..004cbb4 --- /dev/null +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/CTFileLayer.py @@ -0,0 +1,21 @@ +from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import * +from source.wt import ctc + +Patch: any + + +class CTFileLayer(AbstractLayer): + """ + Represent a layer that replace bmg entry by their ID + """ + + mode = "ctfile" + + def __init__(self, template: dict[str, str]): + self.template = template + + def patch_bmg(self, patch: "Patch", decoded_content: str) -> str: + return decoded_content + "\n" + ( + ctc.bmg_ctfile(patch.mod_config.get_ctfile(template=self.template)) + ) + "\n" + # add new bmg definition at the end of the bmg file, overwritting old id. diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py index 3cef67b..cdaf9e0 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py @@ -11,5 +11,5 @@ class AbstractLayer(ABC): """ -from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import IDLayer, RegexLayer -__all__ = ["AbstractLayer", "IDLayer", "RegexLayer"] +from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import IDLayer, RegexLayer, CTFileLayer +__all__ = ["AbstractLayer", "IDLayer", "RegexLayer", "CTFileLayer"] diff --git a/source/wt/ctc.py b/source/wt/ctc.py new file mode 100644 index 0000000..508d8f1 --- /dev/null +++ b/source/wt/ctc.py @@ -0,0 +1,17 @@ +from source.wt import * + +tools_path = tools_szs_dir / "wctct" + + +_tools_run = get_tools_run_function(tools_path) +_tools_run_popen = get_tools_run_popen_function(tools_path) + + +def bmg_ctfile(ctfile: "Path | str") -> str: + + process = _tools_run_popen("BMG", "-", "--lecode") + stdout, _ = process.communicate(input=ctfile.encode("utf-8")) + if process.returncode != 0: + raise WTError(tools_path, process.returncode) + + return stdout.decode("utf-8")