From 150f0d6e60482ca2d8a745d4e267582ba9b62f2b Mon Sep 17 00:00:00 2001 From: Faraphel Date: Wed, 20 Jul 2022 23:47:28 +0200 Subject: [PATCH] added FormatOriginalTrackLayer, allowing for patching originals tracks name like the custom tracks (with tags name, ...) --- .../message/Common.bmg.json | 8 +++ .../UI/RaceLanguage.d/message/Common.bmg.json | 8 +++ .../Layer/FormatOriginalTrackLayer.py | 49 +++++++++++++++++++ .../Operation/BmgTxtEditor/Layer/__init__.py | 6 ++- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py diff --git a/Pack/MKWFaraphel/essentials/_PATCH/files/Scene/UI/CommonLanguageMenu.d/message/Common.bmg.json b/Pack/MKWFaraphel/essentials/_PATCH/files/Scene/UI/CommonLanguageMenu.d/message/Common.bmg.json index 2fc9b70..451c387 100644 --- a/Pack/MKWFaraphel/essentials/_PATCH/files/Scene/UI/CommonLanguageMenu.d/message/Common.bmg.json +++ b/Pack/MKWFaraphel/essentials/_PATCH/files/Scene/UI/CommonLanguageMenu.d/message/Common.bmg.json @@ -7,6 +7,14 @@ { "mode": "ctfile", "template": "{{ ('\\c{YOR2}\\x'+hex(65296+getattr(track, 'score'))[2:]+'\\c{off} ') if hasattr(track, 'score') else '' }}{{ (prefix+' ') if prefix else '' }}{{ getattr(track, 'name', '') }}{{ (' ('+suffix +')') if suffix else '' }}" + }, + { + "mode": "format-original-track", + "template": "{{ (prefix+' ') if prefix else '' }}{{ getattr(track, 'name', '') }}{{ (' ('+suffix +')') if suffix else '' }}" + }, + { + "mode": "patch", + "patchs": {"LE-FORCE-COPY": null} } ] }, diff --git a/Pack/MKWFaraphel/essentials/_PATCH/files/Scene/UI/RaceLanguage.d/message/Common.bmg.json b/Pack/MKWFaraphel/essentials/_PATCH/files/Scene/UI/RaceLanguage.d/message/Common.bmg.json index 90b684c..78fb30b 100644 --- a/Pack/MKWFaraphel/essentials/_PATCH/files/Scene/UI/RaceLanguage.d/message/Common.bmg.json +++ b/Pack/MKWFaraphel/essentials/_PATCH/files/Scene/UI/RaceLanguage.d/message/Common.bmg.json @@ -7,6 +7,14 @@ { "mode": "ctfile", "template": "{{ ('\\c{YOR2}\\x'+hex(65296+getattr(track, 'score'))[2:]+'\\c{off} ') if hasattr(track, 'score') else '' }}{{ (prefix+' ') if prefix else '' }}{{ getattr(track, 'name', '') }}{{ (' ('+suffix +')') if suffix else '' }}\n{{ '\\n'.join(getattr(track, 'author')) if isinstance(getattr(track, 'author', ''), list) else getattr(track, 'author', '/') }}" + }, + { + "mode": "format-original-track", + "template": "{{ (prefix+' ') if prefix else '' }}{{ getattr(track, 'name', '') }}{{ (' ('+suffix +')') if suffix else '' }}\nNintendo" + }, + { + "mode": "patch", + "patchs": {"LE-FORCE-COPY": null} } ] }, diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py new file mode 100644 index 0000000..e1e9123 --- /dev/null +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py @@ -0,0 +1,49 @@ +import re + +from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import * +from source.mkw.Track import Track +from source.wt import bmg + +Patch: any + + +class FormatOriginalTrackLayer(AbstractLayer): + """ + Represent a layer that patch a bmg with all the originals track formatted + """ + + mode = "format-original-track" + + def __init__(self, template: str): + self.template = template + + def patch_bmg(self, patch: "Patch", decoded_content: str) -> str: + originals_track = bmg.cat_data(decoded_content, filters={"TRACKS+ARENAS": None}) + new_bmg_lines: list[str] = [] + + for line in originals_track.split("\n"): + if (match := re.match(r"^ {2}(?P.*?)\t= (?P.*)$", line, re.DOTALL)) is None: + # check if the line match a bmg definition, else ignore + # bmg definition is : 2 spaces, a bmg id, a tab, an equal sign, a space and the bmg text + continue + + id = match.group("id") + name = match.group("value") + + if (id[0] == "T" and int(id[1]) <= 4) or (id[0] == "U" and int(id[1]) == 1): tag = "Wii" + # If the cup is in the 4 originals tracks cups, use the wii tags + # If the cup is in the originals arena cup, use the wii tags + else: tag, name = name.split(" ", 1) + + patched_name = Track( + name=name, + tags=[tag] + ).repr_format( + patch.mod_config, + self.template + ) + + new_bmg_lines.append(f" {id}\t={patched_name}") + + return decoded_content + "\n" + ("\n".join(new_bmg_lines)) + "\n" + # add every new line to the end of the decoded_bmg, old bmg_id will be overwritten. diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py index cdaf9e0..6557f67 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod + Patch: any @@ -11,5 +12,6 @@ class AbstractLayer(ABC): """ -from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import IDLayer, RegexLayer, CTFileLayer -__all__ = ["AbstractLayer", "IDLayer", "RegexLayer", "CTFileLayer"] +from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import IDLayer, RegexLayer, CTFileLayer, PatchLayer, \ + FormatOriginalTrackLayer +__all__ = ["AbstractLayer", "IDLayer", "RegexLayer", "CTFileLayer", "PatchLayer", "FormatOriginalTrackLayer"]