From 8d0544a084bd84a95e2d8d95b28061e2633886a6 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sat, 16 Jul 2022 22:32:41 +0200 Subject: [PATCH] in order to stay constitent with the operation style, bmg-edit have been splitted into bmg-decode, bmg-encode and bmgtxt-edit. This will also allow more possibility. --- .../message/Menu.bmg.json | 6 +++-- .../specific/_PATCH/files/Scene/UI/en.json | 6 +++-- .../specific/_PATCH/files/Scene/UI/es.json | 6 +++-- .../specific/_PATCH/files/Scene/UI/fr.json | 6 +++-- .../specific/_PATCH/files/Scene/UI/ge.json | 6 +++-- .../specific/_PATCH/files/Scene/UI/it.json | 6 +++-- .../PatchOperation/Operation/BmgDecoder.py | 27 +++++++++++++++++++ .../PatchOperation/Operation/BmgEncoder.py | 27 +++++++++++++++++++ .../Layer/IDLayer.py | 2 +- .../Layer/RegexLayer.py | 2 +- .../Layer/__init__.py | 2 +- .../{BmgEditor => BmgTxtEditor}/__init__.py | 15 +++++------ .../PatchOperation/Operation/ImageDecoder.py | 3 +++ .../Patch/PatchOperation/Operation/Rename.py | 2 +- .../PatchOperation/Operation/__init__.py | 4 +-- 15 files changed, 94 insertions(+), 26 deletions(-) create mode 100644 source/mkw/Patch/PatchOperation/Operation/BmgDecoder.py create mode 100644 source/mkw/Patch/PatchOperation/Operation/BmgEncoder.py rename source/mkw/Patch/PatchOperation/Operation/{BmgEditor => BmgTxtEditor}/Layer/IDLayer.py (87%) rename source/mkw/Patch/PatchOperation/Operation/{BmgEditor => BmgTxtEditor}/Layer/RegexLayer.py (95%) rename source/mkw/Patch/PatchOperation/Operation/{BmgEditor => BmgTxtEditor}/Layer/__init__.py (77%) rename source/mkw/Patch/PatchOperation/Operation/{BmgEditor => BmgTxtEditor}/__init__.py (74%) diff --git a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/CommonLanguageMenu.d/message/Menu.bmg.json b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/CommonLanguageMenu.d/message/Menu.bmg.json index 8cb4aea..0e2a801 100644 --- a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/CommonLanguageMenu.d/message/Menu.bmg.json +++ b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/CommonLanguageMenu.d/message/Menu.bmg.json @@ -2,7 +2,8 @@ "mode": "edit", "operation": { - "bmg-edit": { + "bmg-decode": {}, + "bmgtxt-edit": { "layers": [{ "mode": "regex", "template": { @@ -21,6 +22,7 @@ "(.*)Mario Kart Wii(.*)": "\\1{{getattr(mod_config, 'nickname')}} {{getattr(mod_config, 'version')}}\\2" } }] - } + }, + "bmg-encode": {} } } \ No newline at end of file diff --git a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/en.json b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/en.json index de95c2c..1f5b07e 100644 --- a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/en.json +++ b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/en.json @@ -1,7 +1,8 @@ { "mode": "edit", "operation": { - "bmg-edit": { + "bmg-decode": {}, + "bmgtxt-edit": { "layers": [{ "mode": "id", "template": { @@ -43,6 +44,7 @@ "M67": "\\c{YOR4}Game : 64 races" } }] - } + }, + "bmg-encode": {} } } \ No newline at end of file diff --git a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/es.json b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/es.json index 53b8383..5674c6c 100644 --- a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/es.json +++ b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/es.json @@ -1,7 +1,8 @@ { "mode": "edit", "operation": { - "bmg-edit": { + "bmg-decode": {}, + "bmgtxt-edit": { "layers": [{ "mode": "id", "template": { @@ -43,6 +44,7 @@ "M67": "\\c{YOR4}Partido : 64 pistas" } }] - } + }, + "bmg-encode": {} } } \ No newline at end of file diff --git a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/fr.json b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/fr.json index a5f564d..2122fd6 100644 --- a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/fr.json +++ b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/fr.json @@ -1,7 +1,8 @@ { "mode": "edit", "operation": { - "bmg-edit": { + "bmg-decode": {}, + "bmgtxt-edit": { "layers": [{ "mode": "id", "template": { @@ -43,6 +44,7 @@ "M67": "\\c{YOR4}Partie : 64 courses" } }] - } + }, + "bmg-encode": {} } } \ No newline at end of file diff --git a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/ge.json b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/ge.json index a21f1a7..b6e8dda 100644 --- a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/ge.json +++ b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/ge.json @@ -1,7 +1,8 @@ { "mode": "edit", "operation": { - "bmg-edit": { + "bmg-decode": {}, + "bmgtxt-edit": { "layers": [{ "mode": "id", "template": { @@ -43,6 +44,7 @@ "M67": "\\c{YOR4}Spiel : 64 Tracks" } }] - } + }, + "bmg-encode": {} } } \ No newline at end of file diff --git a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/it.json b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/it.json index f97697d..e9cbcb9 100644 --- a/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/it.json +++ b/Pack/MKWFaraphel/specific/_PATCH/files/Scene/UI/it.json @@ -1,7 +1,8 @@ { "mode": "edit", "operation": { - "bmg-edit": { + "bmg-decode": {}, + "bmgtxt-edit": { "layers": [{ "mode": "id", "template": { @@ -43,6 +44,7 @@ "M67": "\\c{YOR4}Gioco : 64 pistas" } }] - } + }, + "bmg-encode": {} } } \ No newline at end of file diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgDecoder.py b/source/mkw/Patch/PatchOperation/Operation/BmgDecoder.py new file mode 100644 index 0000000..0de772d --- /dev/null +++ b/source/mkw/Patch/PatchOperation/Operation/BmgDecoder.py @@ -0,0 +1,27 @@ +from io import BytesIO +from typing import IO + +from source.mkw.Patch.PatchOperation.Operation import * +from source.wt import bmg + + +Patch: any + + +class BmgDecoder(AbstractOperation): + """ + decode a bmg file to a txt file + """ + + type = "bmg-decode" + + def patch(self, patch: "Patch", file_name: str, file_content: IO) -> (str, IO): + """ + Patch a file to decode it to a txt file + :param patch: the patch that is applied + :param file_name: the file_name of the file + :param file_content: the content of the file + :return: the new name and new content of the file + """ + patch_content = BytesIO(bmg.decode_data(file_content.read()).encode("utf-8")) + return f"{file_name}.txt", patch_content diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgEncoder.py b/source/mkw/Patch/PatchOperation/Operation/BmgEncoder.py new file mode 100644 index 0000000..4c85392 --- /dev/null +++ b/source/mkw/Patch/PatchOperation/Operation/BmgEncoder.py @@ -0,0 +1,27 @@ +from io import BytesIO +from typing import IO + +from source.mkw.Patch.PatchOperation.Operation import * +from source.wt import bmg + +Patch: any + + +class BmgEncoder(AbstractOperation): + """ + encode a bmg file to a txt file + """ + + type = "bmg-encode" + + def patch(self, patch: "Patch", file_name: str, file_content: IO) -> (str, IO): + """ + Patch a file to encode it to a bmg file + :param patch: the patch that is applied + :param file_name: the file_name of the file + :param file_content: the content of the file + :return: the new name and new content of the file + """ + patched_file_name = file_name.rsplit(".", 1)[0] + patch_content = BytesIO(bmg.encode_data(file_content.read().decode("utf-8"))) + return patched_file_name, patch_content diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgEditor/Layer/IDLayer.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/IDLayer.py similarity index 87% rename from source/mkw/Patch/PatchOperation/Operation/BmgEditor/Layer/IDLayer.py rename to source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/IDLayer.py index 6eda617..c331a08 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgEditor/Layer/IDLayer.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/IDLayer.py @@ -1,4 +1,4 @@ -from source.mkw.Patch.PatchOperation.Operation.BmgEditor.Layer import * +from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import * Patch: any diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgEditor/Layer/RegexLayer.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/RegexLayer.py similarity index 95% rename from source/mkw/Patch/PatchOperation/Operation/BmgEditor/Layer/RegexLayer.py rename to source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/RegexLayer.py index 673842b..fbf5f95 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgEditor/Layer/RegexLayer.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/RegexLayer.py @@ -1,6 +1,6 @@ import re -from source.mkw.Patch.PatchOperation.Operation.BmgEditor.Layer import * +from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import * Patch: any diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgEditor/Layer/__init__.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py similarity index 77% rename from source/mkw/Patch/PatchOperation/Operation/BmgEditor/Layer/__init__.py rename to source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/__init__.py index ebb4e2c..3cef67b 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgEditor/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.BmgEditor.Layer import IDLayer, RegexLayer +from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import IDLayer, RegexLayer __all__ = ["AbstractLayer", "IDLayer", "RegexLayer"] diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgEditor/__init__.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/__init__.py similarity index 74% rename from source/mkw/Patch/PatchOperation/Operation/BmgEditor/__init__.py rename to source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/__init__.py index 993c998..6d50bd9 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgEditor/__init__.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/__init__.py @@ -3,16 +3,15 @@ from typing import IO from source.mkw.Patch import * from source.mkw.Patch.PatchOperation.Operation import * -from source.mkw.Patch.PatchOperation.Operation.BmgEditor.Layer import * -from source.wt import bmg +from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import * -class BmgEditor(AbstractOperation): +class BmgTxtEditor(AbstractOperation): """ - edit a bmg + edit a decoded bmg """ - type = "bmg-edit" + type = "bmgtxt-edit" def __init__(self, layers: list[dict]): """ @@ -21,12 +20,12 @@ class BmgEditor(AbstractOperation): self.layers = layers def patch(self, patch: "Patch", file_name: str, file_content: IO) -> (str, IO): - decoded_content = bmg.decode_data(file_content.read()) + decoded_content: str = file_content.read().decode("utf-8") for layer in self.layers: decoded_content = self.Layer(layer).patch_bmg(patch, decoded_content) - patch_content = BytesIO(bmg.encode_data(decoded_content)) + patch_content: IO = BytesIO(decoded_content.encode("utf-8")) return file_name, patch_content class Layer: @@ -43,4 +42,4 @@ class BmgEditor(AbstractOperation): AbstractLayer.__subclasses__()): layer.pop("mode") return subclass(**layer) - raise InvalidBmgLayerMode(layer["mode"]) \ No newline at end of file + raise InvalidBmgLayerMode(layer["mode"]) diff --git a/source/mkw/Patch/PatchOperation/Operation/ImageDecoder.py b/source/mkw/Patch/PatchOperation/Operation/ImageDecoder.py index ac13e37..986c6e9 100644 --- a/source/mkw/Patch/PatchOperation/Operation/ImageDecoder.py +++ b/source/mkw/Patch/PatchOperation/Operation/ImageDecoder.py @@ -5,6 +5,9 @@ from source.mkw.Patch.PatchOperation.Operation import * from source.wt import img +Patch: any + + class ImageDecoder(AbstractOperation): """ decode a game image to a image file diff --git a/source/mkw/Patch/PatchOperation/Operation/Rename.py b/source/mkw/Patch/PatchOperation/Operation/Rename.py index f0a0457..d9751ad 100644 --- a/source/mkw/Patch/PatchOperation/Operation/Rename.py +++ b/source/mkw/Patch/PatchOperation/Operation/Rename.py @@ -3,7 +3,7 @@ from typing import IO from source.mkw.Patch.PatchOperation.Operation import * -Patch: str +Patch: any class Rename(AbstractOperation): diff --git a/source/mkw/Patch/PatchOperation/Operation/__init__.py b/source/mkw/Patch/PatchOperation/Operation/__init__.py index 68a1600..c2cdf44 100644 --- a/source/mkw/Patch/PatchOperation/Operation/__init__.py +++ b/source/mkw/Patch/PatchOperation/Operation/__init__.py @@ -17,6 +17,6 @@ class AbstractOperation(ABC): from source.mkw.Patch.PatchOperation.Operation import ImageDecoder, ImageEncoder, Rename, Special, StrEditor, \ - BmgEditor, ImageEditor + BmgTxtEditor, ImageEditor, BmgEncoder, BmgDecoder __all__ = ["AbstractOperation", "ImageDecoder", "ImageEncoder", "Rename", - "Special", "StrEditor", "BmgEditor", "ImageEditor"] \ No newline at end of file + "Special", "StrEditor", "BmgTxtEditor", "ImageEditor", "BmgEncoder", "BmgDecoder"]