If an error is raised with wiimm's tools, a better error will be shown

This commit is contained in:
Faraphel 2021-10-26 17:17:16 +02:00
parent cff27f6198
commit 4d4c2ff2bb
8 changed files with 83 additions and 15 deletions

View file

@ -1,15 +1,20 @@
import subprocess import subprocess
from . import error
WBMGT_PATH = "./tools/szs/wbmgt"
@error.better_wszst_error(wszst_tools=WBMGT_PATH)
def encode(file: str) -> None: def encode(file: str) -> None:
""" """
Encode a txt file into a bmg file Encode a txt file into a bmg file
:param file: txt file to convert :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) creationflags=subprocess.CREATE_NO_WINDOW)
@error.better_wszst_error(wszst_tools=WBMGT_PATH)
def cat(path: str, subfile: str = ".d/message/Common.bmg") -> str: def cat(path: str, subfile: str = ".d/message/Common.bmg") -> str:
""" """
read a bmg file 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 :param subfile: path to a subdirectory
:return: bmg definition :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, creationflags=subprocess.CREATE_NO_WINDOW,
check=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode() check=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode()

View file

@ -1,6 +1,10 @@
import subprocess 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"): def patch_bmg(bmgs: list, ctfile: str = "./file/CTFILE.txt"):
""" """
Patch a bmg file with a ctfile with OVERWRITE option 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 = [] bmg_cmd = []
for bmg in bmgs: bmg_cmd.extend(["--patch-bmg", f"OVERWRITE={bmg}"]) for bmg in bmgs: bmg_cmd.extend(["--patch-bmg", f"OVERWRITE={bmg}"])
return subprocess.run( 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() creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE).stdout.decode()

32
source/wszst/error.py Normal file
View file

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

View file

@ -1,11 +1,15 @@
import subprocess 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: def encode(file: str, format: str) -> None:
""" """
Encode an .png image into a new format Encode an .png image into a new format
:param file: .png image :param file: .png image
:param format: new image format :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) creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)

View file

@ -1,6 +1,10 @@
import subprocess 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", def patch(lecode_file: str = f"./file/lecode-PAL.bin",
dest_lecode_file: str = f"./files/rel/lecode-PAL.bin", dest_lecode_file: str = f"./files/rel/lecode-PAL.bin",
game_track_path: str = "./files/Race/Course/", 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, ...) :param lpar_path: where is the lpar_path (game modification like speed, speedometer, ...)
""" """
subprocess.run( 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"], "--move-tracks", move_track_path, "--le-define", ctfile_path, "--lpar", lpar_path, "--overwrite"],
creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)

View file

@ -1,6 +1,10 @@
import subprocess 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: def extract(file: str, dest_dir: str = None) -> None:
""" """
Extract an szs in a directory 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 :param dest_dir: directory where to extract the file
""" """
if dest_dir is None: dest_dir = 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) creationflags=subprocess.CREATE_NO_WINDOW)
@error.better_wszst_error(wszst_tools=WSZST_PATH)
def analyze(file: str) -> dict: def analyze(file: str) -> dict:
""" """
return dictionnary with information about the track return dictionnary with information about the track
:param file: track file :param file: track file
:return: directory :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() creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE).stdout.decode()
dict_track = {} dict_track = {}
@ -30,17 +35,19 @@ def analyze(file: str) -> dict:
return dict_track return dict_track
@error.better_wszst_error(wszst_tools=WSZST_PATH)
def sha1(file, autoadd_path: str = "./file/auto-add/") -> str: def sha1(file, autoadd_path: str = "./file/auto-add/") -> str:
""" """
:param autoadd_path: directory where is autoadd directory :param autoadd_path: directory where is autoadd directory
:param file: track file to check sha1 :param file: track file to check sha1
:return: track's 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, check=True, creationflags=subprocess.CREATE_NO_WINDOW,
stdout=subprocess.PIPE).stdout.decode().split(" ")[0] 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", 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: 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 output_format: format of the destination track
:param autoadd_path: path of the auto-add directory :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], "--overwrite", "--autoadd-path", autoadd_path],
creationflags=subprocess.CREATE_NO_WINDOW, stderr=subprocess.PIPE) creationflags=subprocess.CREATE_NO_WINDOW, stderr=subprocess.PIPE)
@error.better_wszst_error(wszst_tools=WSZST_PATH)
def create(file: str) -> None: def create(file: str) -> None:
""" """
convert a directory into a szs file convert a directory into a szs file
:param file: create a .szs file from the directory {file}.d :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) 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: def autoadd(path: str, dest_dir: str) -> None:
""" """
Create an auto_add directory from a game file Create an auto_add directory from a game file
:param path: the game's path :param path: the game's path
:param dest_dir: directory where to store autoadd file :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) creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)

View file

@ -1,16 +1,21 @@
import subprocess 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: def extract(file: str, dst_dir: str) -> None:
""" """
extract the game into a directory extract the game into a directory
:param file: game's file to extract (can be WBFS, ISO, CISO) :param file: game's file to extract (can be WBFS, ISO, CISO)
:param dst_dir: where to extract the game :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) 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: def edit(file: str, region_ID: str = "P", name: str = "Mario Kart Wii") -> None:
""" """
Edit game property like region or name 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 :param name: new name
""" """
subprocess.run( 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) 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: def copy(src_path: str, dst_path: str, format: str = "ISO") -> None:
""" """
Copy the game into an another format 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 dst_path: new game path
:param format: format for the new game :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) creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)

View file

@ -1,10 +1,14 @@
import subprocess import subprocess
from . import error
WSTRT_PATH = "./tools/szs/wstrt"
@error.better_wszst_error(wszst_tools=WSTRT_PATH)
def patch(path: str) -> None: def patch(path: str) -> None:
""" """
Patch the main.dol file Patch the main.dol file
:param path: path to the game :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) creationflags=subprocess.CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)