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.

This commit is contained in:
Faraphel 2022-07-16 22:32:41 +02:00
parent 43018abdca
commit 8d0544a084
15 changed files with 94 additions and 26 deletions

View file

@ -2,7 +2,8 @@
"mode": "edit", "mode": "edit",
"operation": { "operation": {
"bmg-edit": { "bmg-decode": {},
"bmgtxt-edit": {
"layers": [{ "layers": [{
"mode": "regex", "mode": "regex",
"template": { "template": {
@ -21,6 +22,7 @@
"(.*)Mario Kart Wii(.*)": "\\1{{getattr(mod_config, 'nickname')}} {{getattr(mod_config, 'version')}}\\2" "(.*)Mario Kart Wii(.*)": "\\1{{getattr(mod_config, 'nickname')}} {{getattr(mod_config, 'version')}}\\2"
} }
}] }]
} },
"bmg-encode": {}
} }
} }

View file

@ -1,7 +1,8 @@
{ {
"mode": "edit", "mode": "edit",
"operation": { "operation": {
"bmg-edit": { "bmg-decode": {},
"bmgtxt-edit": {
"layers": [{ "layers": [{
"mode": "id", "mode": "id",
"template": { "template": {
@ -43,6 +44,7 @@
"M67": "\\c{YOR4}Game : 64 races" "M67": "\\c{YOR4}Game : 64 races"
} }
}] }]
} },
"bmg-encode": {}
} }
} }

View file

@ -1,7 +1,8 @@
{ {
"mode": "edit", "mode": "edit",
"operation": { "operation": {
"bmg-edit": { "bmg-decode": {},
"bmgtxt-edit": {
"layers": [{ "layers": [{
"mode": "id", "mode": "id",
"template": { "template": {
@ -43,6 +44,7 @@
"M67": "\\c{YOR4}Partido : 64 pistas" "M67": "\\c{YOR4}Partido : 64 pistas"
} }
}] }]
} },
"bmg-encode": {}
} }
} }

View file

@ -1,7 +1,8 @@
{ {
"mode": "edit", "mode": "edit",
"operation": { "operation": {
"bmg-edit": { "bmg-decode": {},
"bmgtxt-edit": {
"layers": [{ "layers": [{
"mode": "id", "mode": "id",
"template": { "template": {
@ -43,6 +44,7 @@
"M67": "\\c{YOR4}Partie : 64 courses" "M67": "\\c{YOR4}Partie : 64 courses"
} }
}] }]
} },
"bmg-encode": {}
} }
} }

View file

@ -1,7 +1,8 @@
{ {
"mode": "edit", "mode": "edit",
"operation": { "operation": {
"bmg-edit": { "bmg-decode": {},
"bmgtxt-edit": {
"layers": [{ "layers": [{
"mode": "id", "mode": "id",
"template": { "template": {
@ -43,6 +44,7 @@
"M67": "\\c{YOR4}Spiel : 64 Tracks" "M67": "\\c{YOR4}Spiel : 64 Tracks"
} }
}] }]
} },
"bmg-encode": {}
} }
} }

View file

@ -1,7 +1,8 @@
{ {
"mode": "edit", "mode": "edit",
"operation": { "operation": {
"bmg-edit": { "bmg-decode": {},
"bmgtxt-edit": {
"layers": [{ "layers": [{
"mode": "id", "mode": "id",
"template": { "template": {
@ -43,6 +44,7 @@
"M67": "\\c{YOR4}Gioco : 64 pistas" "M67": "\\c{YOR4}Gioco : 64 pistas"
} }
}] }]
} },
"bmg-encode": {}
} }
} }

View file

@ -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

View file

@ -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

View file

@ -1,4 +1,4 @@
from source.mkw.Patch.PatchOperation.Operation.BmgEditor.Layer import * from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import *
Patch: any Patch: any

View file

@ -1,6 +1,6 @@
import re import re
from source.mkw.Patch.PatchOperation.Operation.BmgEditor.Layer import * from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import *
Patch: any Patch: any

View file

@ -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"] __all__ = ["AbstractLayer", "IDLayer", "RegexLayer"]

View file

@ -3,16 +3,15 @@ from typing import IO
from source.mkw.Patch import * from source.mkw.Patch import *
from source.mkw.Patch.PatchOperation.Operation import * from source.mkw.Patch.PatchOperation.Operation import *
from source.mkw.Patch.PatchOperation.Operation.BmgEditor.Layer import * from source.mkw.Patch.PatchOperation.Operation.BmgTxtEditor.Layer import *
from source.wt import bmg
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]): def __init__(self, layers: list[dict]):
""" """
@ -21,12 +20,12 @@ class BmgEditor(AbstractOperation):
self.layers = layers self.layers = layers
def patch(self, patch: "Patch", file_name: str, file_content: IO) -> (str, IO): 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: for layer in self.layers:
decoded_content = self.Layer(layer).patch_bmg(patch, decoded_content) 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 return file_name, patch_content
class Layer: class Layer:
@ -43,4 +42,4 @@ class BmgEditor(AbstractOperation):
AbstractLayer.__subclasses__()): AbstractLayer.__subclasses__()):
layer.pop("mode") layer.pop("mode")
return subclass(**layer) return subclass(**layer)
raise InvalidBmgLayerMode(layer["mode"]) raise InvalidBmgLayerMode(layer["mode"])

View file

@ -5,6 +5,9 @@ from source.mkw.Patch.PatchOperation.Operation import *
from source.wt import img from source.wt import img
Patch: any
class ImageDecoder(AbstractOperation): class ImageDecoder(AbstractOperation):
""" """
decode a game image to a image file decode a game image to a image file

View file

@ -3,7 +3,7 @@ from typing import IO
from source.mkw.Patch.PatchOperation.Operation import * from source.mkw.Patch.PatchOperation.Operation import *
Patch: str Patch: any
class Rename(AbstractOperation): class Rename(AbstractOperation):

View file

@ -17,6 +17,6 @@ class AbstractOperation(ABC):
from source.mkw.Patch.PatchOperation.Operation import ImageDecoder, ImageEncoder, Rename, Special, StrEditor, \ from source.mkw.Patch.PatchOperation.Operation import ImageDecoder, ImageEncoder, Rename, Special, StrEditor, \
BmgEditor, ImageEditor BmgTxtEditor, ImageEditor, BmgEncoder, BmgDecoder
__all__ = ["AbstractOperation", "ImageDecoder", "ImageEncoder", "Rename", __all__ = ["AbstractOperation", "ImageDecoder", "ImageEncoder", "Rename",
"Special", "StrEditor", "BmgEditor", "ImageEditor"] "Special", "StrEditor", "BmgTxtEditor", "ImageEditor", "BmgEncoder", "BmgDecoder"]