added FormatOriginalTrackLayer, allowing for patching originals tracks name like the custom tracks (with tags name, ...)

This commit is contained in:
Faraphel 2022-07-20 23:47:28 +02:00
parent 1d88f89f3d
commit 150f0d6e60
4 changed files with 69 additions and 2 deletions

View file

@ -7,6 +7,14 @@
{ {
"mode": "ctfile", "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 '' }}" "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}
} }
] ]
}, },

View file

@ -7,6 +7,14 @@
{ {
"mode": "ctfile", "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', '/') }}" "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}
} }
] ]
}, },

View file

@ -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<id>.*?)\t= (?P<value>.*)$", 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.

View file

@ -1,5 +1,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
Patch: any Patch: any
@ -11,5 +12,6 @@ class AbstractLayer(ABC):
""" """
from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import IDLayer, RegexLayer, CTFileLayer from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import IDLayer, RegexLayer, CTFileLayer, PatchLayer, \
__all__ = ["AbstractLayer", "IDLayer", "RegexLayer", "CTFileLayer"] FormatOriginalTrackLayer
__all__ = ["AbstractLayer", "IDLayer", "RegexLayer", "CTFileLayer", "PatchLayer", "FormatOriginalTrackLayer"]