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",
"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": {}
}
}

View file

@ -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": {}
}
}

View file

@ -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": {}
}
}

View file

@ -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": {}
}
}

View file

@ -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": {}
}
}

View file

@ -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": {}
}
}

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

View file

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

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"]

View file

@ -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"])
raise InvalidBmgLayerMode(layer["mode"])

View file

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

View file

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

View file

@ -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"]
"Special", "StrEditor", "BmgTxtEditor", "ImageEditor", "BmgEncoder", "BmgDecoder"]