diff --git a/source/wszst/bmg.py b/source/wszst/bmg.py index e98f1df..3568dbf 100644 --- a/source/wszst/bmg.py +++ b/source/wszst/bmg.py @@ -1,15 +1,20 @@ import subprocess +from . import error + +WBMGT_PATH = "./tools/szs/wbmgt" +@error.better_wszst_error(wszst_tools=WBMGT_PATH) def encode(file: str) -> None: """ Encode a txt file into a bmg file :param file: txt file to convert """ - subprocess.run(["./tools/szs/wbmgt", "ENCODE", file, "--overwrite"], + subprocess.run([WBMGT_PATH, "ENCODE", file, "--overwrite"], creationflags=subprocess.CREATE_NO_WINDOW) +@error.better_wszst_error(wszst_tools=WBMGT_PATH) def cat(path: str, subfile: str = ".d/message/Common.bmg") -> str: """ read a bmg file @@ -17,6 +22,6 @@ def cat(path: str, subfile: str = ".d/message/Common.bmg") -> str: :param subfile: path to a subdirectory :return: bmg definition """ - return subprocess.run(["./tools/szs/wbmgt", "CAT", path + subfile], + return subprocess.run([WBMGT_PATH, "CAT", path + subfile], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode() diff --git a/source/wszst/ctc.py b/source/wszst/ctc.py index 6a5969a..576ea07 100644 --- a/source/wszst/ctc.py +++ b/source/wszst/ctc.py @@ -1,6 +1,10 @@ import subprocess +from . import error + +WCTCT_PATH = "./tools/szs/wctct" +@error.better_wszst_error(wszst_tools=WCTCT_PATH) def patch_bmg(bmgs: list, ctfile: str = "./file/CTFILE.txt"): """ Patch a bmg file with a ctfile with OVERWRITE option @@ -11,5 +15,5 @@ def patch_bmg(bmgs: list, ctfile: str = "./file/CTFILE.txt"): bmg_cmd = [] for bmg in bmgs: bmg_cmd.extend(["--patch-bmg", f"OVERWRITE={bmg}"]) return subprocess.run( - ["tools/szs/wctct", "bmg", "--le-code", "--long", ctfile, *bmg_cmd], + [WCTCT_PATH, "bmg", "--le-code", "--long", ctfile, *bmg_cmd], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE).stdout.decode() \ No newline at end of file diff --git a/source/wszst/error.py b/source/wszst/error.py new file mode 100644 index 0000000..f8bad21 --- /dev/null +++ b/source/wszst/error.py @@ -0,0 +1,32 @@ +import subprocess + + +class WSZSTError(Exception): + def __init__(self, wszst_tools, error_id=0): + error_message = subprocess.run([wszst_tools, "ERROR", str(error_id)], + stdout=subprocess.PIPE, check=True, + creationflags=subprocess.CREATE_NO_WINDOW).stdout.decode() + super().__init__(f"({wszst_tools}) Error ({error_id}) : {error_message}") + + +def better_wszst_error(wszst_tools): + """ + raise a better message when an error occur while using one of the wiimm's tools. + :param wszst_tools: tools used + :return: function with better error when exception occur + """ + def gen_wrapped_func(func): + def wrapped_func(*args, **kwargs): + """ + function that will be returned instead of the function, will call it in a thread + :param func: function + :param args: args of the original function + :param kwargs: kwargs of the original function + """ + try: + return func(*args, **kwargs) + except subprocess.CalledProcessError as e: + raise WSZSTError(wszst_tools, e.returncode) + + return wrapped_func + return gen_wrapped_func diff --git a/source/wszst/img.py b/source/wszst/img.py index adabd24..4e88617 100644 --- a/source/wszst/img.py +++ b/source/wszst/img.py @@ -1,11 +1,15 @@ import subprocess +from . import error + +WIMGT_PATH = "./tools/szs/wimgt" +@error.better_wszst_error(wszst_tools=WIMGT_PATH) def encode(file: str, format: str) -> None: """ Encode an .png image into a new format :param file: .png image :param format: new image format """ - subprocess.run(["./tools/szs/wimgt", "ENCODE", file, "-x", format, "--overwrite"], + subprocess.run([WIMGT_PATH, "ENCODE", file, "-x", format, "--overwrite"], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) diff --git a/source/wszst/lec.py b/source/wszst/lec.py index 06a4b4e..a2c9c4e 100644 --- a/source/wszst/lec.py +++ b/source/wszst/lec.py @@ -1,6 +1,10 @@ import subprocess +from . import error + +WLECT_PATH = "./tools/szs/wlect" +@error.better_wszst_error(wszst_tools=WLECT_PATH) def patch(lecode_file: str = f"./file/lecode-PAL.bin", dest_lecode_file: str = f"./files/rel/lecode-PAL.bin", game_track_path: str = "./files/Race/Course/", @@ -17,6 +21,6 @@ def patch(lecode_file: str = f"./file/lecode-PAL.bin", :param lpar_path: where is the lpar_path (game modification like speed, speedometer, ...) """ subprocess.run( - ["./tools/szs/wlect", "patch", lecode_file, "-od", dest_lecode_file, "--track-dir", game_track_path, + [WLECT_PATH, "patch", lecode_file, "-od", dest_lecode_file, "--track-dir", game_track_path, "--move-tracks", move_track_path, "--le-define", ctfile_path, "--lpar", lpar_path, "--overwrite"], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) diff --git a/source/wszst/szs.py b/source/wszst/szs.py index 4309622..733727f 100644 --- a/source/wszst/szs.py +++ b/source/wszst/szs.py @@ -1,6 +1,10 @@ import subprocess +from . import error + +WSZST_PATH = "./tools/szs/wszst" +@error.better_wszst_error(wszst_tools=WSZST_PATH) def extract(file: str, dest_dir: str = None) -> None: """ Extract an szs in a directory @@ -8,17 +12,18 @@ def extract(file: str, dest_dir: str = None) -> None: :param dest_dir: directory where to extract the file """ if dest_dir is None: dest_dir = file - subprocess.run(["./tools/szs/wszst", "EXTRACT", file, "--DEST", dest_dir + ".d"], + subprocess.run([WSZST_PATH, "EXTRACT", file, "--DEST", dest_dir + ".d"], creationflags=subprocess.CREATE_NO_WINDOW) +@error.better_wszst_error(wszst_tools=WSZST_PATH) def analyze(file: str) -> dict: """ return dictionnary with information about the track :param file: track file :return: directory """ - ana_track = subprocess.run(["./tools/szs/wszst", "ANALYZE", file], check=True, + ana_track = subprocess.run([WSZST_PATH, "ANALYZE", file], check=True, creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE).stdout.decode() dict_track = {} @@ -30,17 +35,19 @@ def analyze(file: str) -> dict: return dict_track +@error.better_wszst_error(wszst_tools=WSZST_PATH) def sha1(file, autoadd_path: str = "./file/auto-add/") -> str: """ :param autoadd_path: directory where is autoadd directory :param file: track file to check sha1 :return: track's sha1 """ - return subprocess.run(["./tools/szs/wszst", "SHA1", file, "--autoadd-path", autoadd_path], + return subprocess.run([WSZST_PATH, "SHA1", file, "--autoadd-path", autoadd_path], check=True, creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE).stdout.decode().split(" ")[0] +@error.better_wszst_error(wszst_tools=WSZST_PATH) def normalize(src_file: str, dest_dir: str = "./file/Track/", dest_name: str = "%N.szs", output_format: str = "szs", autoadd_path: str = "./file/auto-add/") -> None: """ @@ -51,25 +58,27 @@ def normalize(src_file: str, dest_dir: str = "./file/Track/", dest_name: str = " :param output_format: format of the destination track :param autoadd_path: path of the auto-add directory """ - subprocess.run(["./tools/szs/wszst", "NORMALIZE", src_file, "--DEST", dest_dir + dest_name, "--" + output_format, + subprocess.run([WSZST_PATH, "NORMALIZE", src_file, "--DEST", dest_dir + dest_name, "--" + output_format, "--overwrite", "--autoadd-path", autoadd_path], creationflags=subprocess.CREATE_NO_WINDOW, stderr=subprocess.PIPE) +@error.better_wszst_error(wszst_tools=WSZST_PATH) def create(file: str) -> None: """ convert a directory into a szs file :param file: create a .szs file from the directory {file}.d """ - subprocess.run(["./tools/szs/wszst", "CREATE", file + ".d", "-d", file, "--overwrite"], + subprocess.run([WSZST_PATH, "CREATE", file + ".d", "-d", file, "--overwrite"], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) +@error.better_wszst_error(wszst_tools=WSZST_PATH) def autoadd(path: str, dest_dir: str) -> None: """ Create an auto_add directory from a game file :param path: the game's path :param dest_dir: directory where to store autoadd file """ - subprocess.run(["./tools/szs/wszst", "AUTOADD", path + "/files/Race/Course/", "--DEST", dest_dir], + subprocess.run([WSZST_PATH, "AUTOADD", path + "/files/Race/Course/", "--DEST", dest_dir], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) \ No newline at end of file diff --git a/source/wszst/wit.py b/source/wszst/wit.py index 63f4f52..6b5687f 100644 --- a/source/wszst/wit.py +++ b/source/wszst/wit.py @@ -1,16 +1,21 @@ import subprocess +from . import error + +WIT_PATH = "./tools/wit/wit" +@error.better_wszst_error(wszst_tools=WIT_PATH) def extract(file: str, dst_dir: str) -> None: """ extract the game into a directory :param file: game's file to extract (can be WBFS, ISO, CISO) :param dst_dir: where to extract the game """ - subprocess.run(["./tools/wit/wit", "EXTRACT", file, "--DEST", dst_dir], + subprocess.run([WIT_PATH, "EXTRACT", file, "--DEST", dst_dir], creationflags=subprocess.CREATE_NO_WINDOW) +@error.better_wszst_error(wszst_tools=WIT_PATH) def edit(file: str, region_ID: str = "P", name: str = "Mario Kart Wii") -> None: """ Edit game property like region or name @@ -19,10 +24,11 @@ def edit(file: str, region_ID: str = "P", name: str = "Mario Kart Wii") -> None: :param name: new name """ subprocess.run( - ["./tools/wit/wit", "EDIT", file, "--id", f"RMC{region_ID}60", "--name", name, "--modify", "ALL"], + [WIT_PATH, "EDIT", file, "--id", f"RMC{region_ID}60", "--name", name, "--modify", "ALL"], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) +@error.better_wszst_error(wszst_tools=WIT_PATH) def copy(src_path: str, dst_path: str, format: str = "ISO") -> None: """ Copy the game into an another format @@ -30,5 +36,5 @@ def copy(src_path: str, dst_path: str, format: str = "ISO") -> None: :param dst_path: new game path :param format: format for the new game """ - subprocess.run(["./tools/wit/wit", "COPY", src_path, "--DEST", dst_path, f"--{format.lower()}", "--overwrite"], + subprocess.run([WIT_PATH, "COPY", src_path, "--DEST", dst_path, f"--{format.lower()}", "--overwrite"], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) diff --git a/source/wszst/wstrt.py b/source/wszst/wstrt.py index 9f5a236..e27dee6 100644 --- a/source/wszst/wstrt.py +++ b/source/wszst/wstrt.py @@ -1,10 +1,14 @@ import subprocess +from . import error + +WSTRT_PATH = "./tools/szs/wstrt" +@error.better_wszst_error(wszst_tools=WSTRT_PATH) def patch(path: str) -> None: """ Patch the main.dol file :param path: path to the game """ - subprocess.run(["./tools/szs/wstrt", "patch", path + "/sys/main.dol", "--clean-dol", "--add-lecode"], + subprocess.run([WSTRT_PATH, "patch", path + "/sys/main.dol", "--clean-dol", "--add-lecode"], creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) \ No newline at end of file