From a01b390ce0671f44889701b29969def65ecb4b0d Mon Sep 17 00:00:00 2001 From: raphael60650 Date: Sun, 18 Jul 2021 11:03:35 +0200 Subject: [PATCH] splitted all class in source into directory --- source/CT_Config.py | 169 -------- source/CT_Config/__init__.py | 15 + source/CT_Config/add_ordered_cup.py | 12 + source/CT_Config/add_unordered_track.py | 11 + source/CT_Config/create_ctfile.py | 29 ++ source/CT_Config/get_cticon.py | 55 +++ source/CT_Config/load_ctconfig_file.py | 11 + source/CT_Config/load_ctconfig_json.py | 32 ++ source/CT_Config/search_tracks.py | 19 + source/Cup.py | 36 -- source/Cup/__init__.py | 22 + source/Cup/get_ctfile_cup.py | 9 + source/Cup/load_from_json.py | 7 + source/Game.py | 501 ----------------------- source/Game/__init__.py | 24 ++ source/Game/convert_to.py | 7 + source/Game/exception.py | 23 ++ source/Game/extract.py | 39 ++ source/Game/install_mod.py | 136 ++++++ source/Game/patch_autoadd.py | 16 + source/Game/patch_bmg.py | 99 +++++ source/Game/patch_file.py | 48 +++ source/Game/patch_image.py | 10 + source/Game/patch_img_desc.py | 25 ++ source/Game/patch_track.py | 123 ++++++ source/{Gui.py => Gui/__init__.py} | 157 +------ source/Gui/check_update.py | 52 +++ source/Gui/log_error.py | 9 + source/Gui/progress.py | 22 + source/Gui/restart.py | 10 + source/Gui/state_button.py | 15 + source/Gui/translate.py | 29 ++ source/Option.py | 34 -- source/Option/__init__.py | 14 + source/Option/edit.py | 7 + source/Option/load_from_file.py | 9 + source/Option/load_from_json.py | 3 + source/Option/save_to_file.py | 7 + source/StateButton.py | 0 source/Track.py | 120 ------ source/Track/__init__.py | 29 ++ source/Track/__repr__.py | 2 + source/Track/check_sha1.py | 8 + source/Track/convert_wu8_to_szs.py | 5 + source/Track/download_wu8.py | 26 ++ source/Track/get_ctfile.py | 23 ++ source/Track/get_track_formatted_name.py | 30 ++ source/Track/get_track_name.py | 6 + source/Track/load_from_json.py | 3 + source/definition.py | 6 + 50 files changed, 1103 insertions(+), 1001 deletions(-) delete mode 100644 source/CT_Config.py create mode 100644 source/CT_Config/__init__.py create mode 100644 source/CT_Config/add_ordered_cup.py create mode 100644 source/CT_Config/add_unordered_track.py create mode 100644 source/CT_Config/create_ctfile.py create mode 100644 source/CT_Config/get_cticon.py create mode 100644 source/CT_Config/load_ctconfig_file.py create mode 100644 source/CT_Config/load_ctconfig_json.py create mode 100644 source/CT_Config/search_tracks.py delete mode 100644 source/Cup.py create mode 100644 source/Cup/__init__.py create mode 100644 source/Cup/get_ctfile_cup.py create mode 100644 source/Cup/load_from_json.py delete mode 100644 source/Game.py create mode 100644 source/Game/__init__.py create mode 100644 source/Game/convert_to.py create mode 100644 source/Game/exception.py create mode 100644 source/Game/extract.py create mode 100644 source/Game/install_mod.py create mode 100644 source/Game/patch_autoadd.py create mode 100644 source/Game/patch_bmg.py create mode 100644 source/Game/patch_file.py create mode 100644 source/Game/patch_image.py create mode 100644 source/Game/patch_img_desc.py create mode 100644 source/Game/patch_track.py rename source/{Gui.py => Gui/__init__.py} (63%) create mode 100644 source/Gui/check_update.py create mode 100644 source/Gui/log_error.py create mode 100644 source/Gui/progress.py create mode 100644 source/Gui/restart.py create mode 100644 source/Gui/state_button.py create mode 100644 source/Gui/translate.py delete mode 100644 source/Option.py create mode 100644 source/Option/__init__.py create mode 100644 source/Option/edit.py create mode 100644 source/Option/load_from_file.py create mode 100644 source/Option/load_from_json.py create mode 100644 source/Option/save_to_file.py delete mode 100644 source/StateButton.py delete mode 100644 source/Track.py create mode 100644 source/Track/__init__.py create mode 100644 source/Track/__repr__.py create mode 100644 source/Track/check_sha1.py create mode 100644 source/Track/convert_wu8_to_szs.py create mode 100644 source/Track/download_wu8.py create mode 100644 source/Track/get_ctfile.py create mode 100644 source/Track/get_track_formatted_name.py create mode 100644 source/Track/get_track_name.py create mode 100644 source/Track/load_from_json.py diff --git a/source/CT_Config.py b/source/CT_Config.py deleted file mode 100644 index 85d00f1..0000000 --- a/source/CT_Config.py +++ /dev/null @@ -1,169 +0,0 @@ -from .Cup import * -import math -from PIL import Image, ImageFont, ImageDraw -import json -import os - - -def get_cup_icon(cup_id, font_path: str = "./file/SuperMario256.ttf", cup_icon_dir: str = "./file/cup_icon"): - """ - :param cup_icon_dir: directory to cup icon - :param font_path: path to the font used to generate icon - :param cup_id: id of the cup - :return: cup icon - """ - if os.path.exists(f"{cup_icon_dir}/{cup_id}.png"): - cup_icon = Image.open(f"{cup_icon_dir}/{cup_id}.png").resize((128, 128)) - - else: - cup_icon = Image.new("RGBA", (128, 128)) - draw = ImageDraw.Draw(cup_icon) - font = ImageFont.truetype(font_path, 90) - draw.text((4 - 2, 4 - 2), "CT", (0, 0, 0), font=font) - draw.text((4 + 2, 4 - 2), "CT", (0, 0, 0), font=font) - draw.text((4 - 2, 4 + 2), "CT", (0, 0, 0), font=font) - draw.text((4 + 2, 4 + 2), "CT", (0, 0, 0), font=font) - draw.text((4, 4), "CT", (255, 165, 0), font=font) - - font = ImageFont.truetype(font_path, 60) - draw.text((5 - 2, 80 - 2), "%03i" % cup_id, (0, 0, 0), font=font) - draw.text((5 + 2, 80 - 2), "%03i" % cup_id, (0, 0, 0), font=font) - draw.text((5 - 2, 80 + 2), "%03i" % cup_id, (0, 0, 0), font=font) - draw.text((5 + 2, 80 + 2), "%03i" % cup_id, (0, 0, 0), font=font) - - draw.text((5, 80), "%03i" % cup_id, (255, 165, 0), font=font) - return cup_icon - - -class CT_Config: - def __init__(self, version: str = None): - self.version = version - self.ordered_cups = [] - self.unordered_tracks = [] - self.all_tracks = [] - self.all_version: set = {version} - - def load_ctconfig_json(self, ctconfig_json: dict): - """ - :param ctconfig_json: json of the ctconfig to load - :return: ? - """ - self.ordered_cups = [] - self.unordered_tracks = [] - self.all_tracks = [] - - for cup_json in ctconfig_json["cup"].values(): # tracks with defined order - cup = Cup() - cup.load_from_json(cup_json) - if not cup.locked: # locked cup are not useful (they are original track or random track) - self.ordered_cups.append(cup) - self.all_tracks.extend(cup.tracks) - - for track_json in ctconfig_json["tracks_list"]: # unordered tracks - track = Track() - track.load_from_json(track_json) - self.unordered_tracks.append(track) - self.all_tracks.append(track) - - self.version = ctconfig_json["version"] - - self.all_version = set() - for track in self.all_tracks: - self.all_version.add(track.since_version) - self.all_version = sorted(self.all_version) - - def load_ctconfig_file(self, ctconfig_file: str = "./ct_config.json"): - """ - :param ctconfig_file: path to the ctconfig file - :return: ? - """ - with open(ctconfig_file, encoding="utf-8") as f: - ctconfig_json = json.load(f) - self.load_ctconfig_json(ctconfig_json) - - def add_ordered_cup(self, cup: Cup): - """ - :param cup: a Cup object to add as an ordered cup - :return: ? - """ - self.ordered_cups.append(cup) - for track in cup.tracks: - self.all_version.add(track.since_version) - self.all_tracks.append(track) - - def add_unordered_track(self, track: Track): - """ - :param track: a Track object to add as an unordered tracks - :return: ? - """ - self.unordered_tracks.append(track) - self.all_version.add(track.since_version) - self.all_tracks.append(track) - - def search_tracks(self, values_list=False, not_value=False, **kwargs): - """ - :param values_list: search track with a value list instead of a single value - :param not_value: search track that does not have value - :param kwargs: any track property = any value - :return: track list respecting condition - """ - track = self.all_tracks.copy() - - if values_list: - if not_value: filter_func = lambda track: getattr(track, keyword) not in value - else: filter_func = lambda track: getattr(track, keyword) in value - else: - if not_value: filter_func = lambda track: getattr(track, keyword) != value - else: filter_func = lambda track: getattr(track, keyword) == value - - for keyword, value in kwargs.items(): - track = list(filter(filter_func, track)) - return track - - def create_ctfile(self, directory="./file/"): - """ - :param directory: create CTFILE.txt and RCTFILE.txt in this directory - :return: None - """ - with open(directory+"CTFILE.txt", "w", encoding="utf-8") as ctfile, \ - open(directory+"RCTFILE.txt", "w", encoding="utf-8") as rctfile: - header = ( - "#CT-CODE\n" - "[RACING-TRACK-LIST]\n" - "%LE-FLAGS=1\n" - "%WIIMM-CUP=1\n" - "N N$SWAP | N$F_WII\n\n") - ctfile.write(header); rctfile.write(header) - - # generate cup for undefined track - unordered_cups = [] - for i, track in enumerate(self.unordered_tracks): - if i % 4 == 0: - _actual_cup = Cup(name=f"TL{i // 4}") - unordered_cups.append(_actual_cup) - _actual_cup.tracks[i % 4] = track - - # all cups - for cup in self.ordered_cups + unordered_cups: - ctfile.write(cup.get_ctfile_cup(race=False)) - rctfile.write(cup.get_ctfile_cup(race=True)) - - def get_cticon(self): - """ - get all cup icon into a single image - :return: ct_icon image - """ - CT_ICON_WIDTH = 128 - icon_files = ["left", "right"] - - total_cup_count = math.ceil(len(self.all_tracks) / 4) - ct_icon = Image.new("RGBA", (CT_ICON_WIDTH, CT_ICON_WIDTH * (total_cup_count + 2))) # +2 because of left and right arrow - - icon_files.extend([str(i) for i, cup in enumerate(self.ordered_cups)]) # adding ordered cup id - icon_files.extend(["_"] * ((len(self.unordered_tracks) // 4) + 1)) # creating unordered track icon - - for i, id in enumerate(icon_files): - cup_icon = get_cup_icon(i) - ct_icon.paste(cup_icon, (0, i * CT_ICON_WIDTH)) - - return ct_icon # ct_icon.save("./file/ct_icons.tpl.png") diff --git a/source/CT_Config/__init__.py b/source/CT_Config/__init__.py new file mode 100644 index 0000000..3f87714 --- /dev/null +++ b/source/CT_Config/__init__.py @@ -0,0 +1,15 @@ +class CT_Config: + def __init__(self, version: str = None): + self.version = version + self.ordered_cups = [] + self.unordered_tracks = [] + self.all_tracks = [] + self.all_version = {version} + + from .add_ordered_cup import add_ordered_cup + from .add_unordered_track import add_unordered_track + from .create_ctfile import create_ctfile + from .get_cticon import get_cticon + from .load_ctconfig_file import load_ctconfig_file + from .load_ctconfig_json import load_ctconfig_json + from .search_tracks import search_tracks diff --git a/source/CT_Config/add_ordered_cup.py b/source/CT_Config/add_ordered_cup.py new file mode 100644 index 0000000..27a08b2 --- /dev/null +++ b/source/CT_Config/add_ordered_cup.py @@ -0,0 +1,12 @@ +from source.Cup import Cup + + +def add_ordered_cup(self, cup: Cup): + """ + :param cup: a Cup object to add as an ordered cup + :return: ? + """ + self.ordered_cups.append(cup) + for track in cup.tracks: + self.all_version.add(track.since_version) + self.all_tracks.append(track) \ No newline at end of file diff --git a/source/CT_Config/add_unordered_track.py b/source/CT_Config/add_unordered_track.py new file mode 100644 index 0000000..5f007b5 --- /dev/null +++ b/source/CT_Config/add_unordered_track.py @@ -0,0 +1,11 @@ +from source.Track import Track + + +def add_unordered_track(self, track: Track): + """ + :param track: a Track object to add as an unordered tracks + :return: ? + """ + self.unordered_tracks.append(track) + self.all_version.add(track.since_version) + self.all_tracks.append(track) \ No newline at end of file diff --git a/source/CT_Config/create_ctfile.py b/source/CT_Config/create_ctfile.py new file mode 100644 index 0000000..c981a44 --- /dev/null +++ b/source/CT_Config/create_ctfile.py @@ -0,0 +1,29 @@ +from source.Cup import Cup + +def create_ctfile(self, directory="./file/"): + """ + :param directory: create CTFILE.txt and RCTFILE.txt in this directory + :return: None + """ + with open(directory + "CTFILE.txt", "w", encoding="utf-8") as ctfile, \ + open(directory + "RCTFILE.txt", "w", encoding="utf-8") as rctfile: + header = ( + "#CT-CODE\n" + "[RACING-TRACK-LIST]\n" + "%LE-FLAGS=1\n" + "%WIIMM-CUP=1\n" + "N N$SWAP | N$F_WII\n\n") + ctfile.write(header); rctfile.write(header) + + # generate cup for undefined track + unordered_cups = [] + for i, track in enumerate(self.unordered_tracks): + if i % 4 == 0: + _actual_cup = Cup(name=f"TL{i // 4}") + unordered_cups.append(_actual_cup) + _actual_cup.tracks[i % 4] = track + + # all cups + for cup in self.ordered_cups + unordered_cups: + ctfile.write(cup.get_ctfile_cup(race=False)) + rctfile.write(cup.get_ctfile_cup(race=True)) \ No newline at end of file diff --git a/source/CT_Config/get_cticon.py b/source/CT_Config/get_cticon.py new file mode 100644 index 0000000..3d16ff9 --- /dev/null +++ b/source/CT_Config/get_cticon.py @@ -0,0 +1,55 @@ +from PIL import Image, ImageFont, ImageDraw +import math +import os + + +def get_cup_icon(cup_id, font_path: str = "./file/SuperMario256.ttf", cup_icon_dir: str = "./file/cup_icon"): + """ + :param cup_icon_dir: directory to cup icon + :param font_path: path to the font used to generate icon + :param cup_id: id of the cup + :return: cup icon + """ + if os.path.exists(f"{cup_icon_dir}/{cup_id}.png"): + cup_icon = Image.open(f"{cup_icon_dir}/{cup_id}.png").resize((128, 128)) + + else: + cup_icon = Image.new("RGBA", (128, 128)) + draw = ImageDraw.Draw(cup_icon) + font = ImageFont.truetype(font_path, 90) + draw.text((4 - 2, 4 - 2), "CT", (0, 0, 0), font=font) + draw.text((4 + 2, 4 - 2), "CT", (0, 0, 0), font=font) + draw.text((4 - 2, 4 + 2), "CT", (0, 0, 0), font=font) + draw.text((4 + 2, 4 + 2), "CT", (0, 0, 0), font=font) + draw.text((4, 4), "CT", (255, 165, 0), font=font) + + font = ImageFont.truetype(font_path, 60) + draw.text((5 - 2, 80 - 2), "%03i" % cup_id, (0, 0, 0), font=font) + draw.text((5 + 2, 80 - 2), "%03i" % cup_id, (0, 0, 0), font=font) + draw.text((5 - 2, 80 + 2), "%03i" % cup_id, (0, 0, 0), font=font) + draw.text((5 + 2, 80 + 2), "%03i" % cup_id, (0, 0, 0), font=font) + + draw.text((5, 80), "%03i" % cup_id, (255, 165, 0), font=font) + return cup_icon + + +def get_cticon(self): + """ + get all cup icon into a single image + :return: ct_icon image + """ + CT_ICON_WIDTH = 128 + icon_files = ["left", "right"] + + total_cup_count = math.ceil(len(self.all_tracks) / 4) + ct_icon = Image.new("RGBA", + (CT_ICON_WIDTH, CT_ICON_WIDTH * (total_cup_count + 2))) # +2 because of left and right arrow + + icon_files.extend([str(i) for i, cup in enumerate(self.ordered_cups)]) # adding ordered cup id + icon_files.extend(["_"] * ((len(self.unordered_tracks) // 4) + 1)) # creating unordered track icon + + for i, id in enumerate(icon_files): + cup_icon = get_cup_icon(i) + ct_icon.paste(cup_icon, (0, i * CT_ICON_WIDTH)) + + return ct_icon diff --git a/source/CT_Config/load_ctconfig_file.py b/source/CT_Config/load_ctconfig_file.py new file mode 100644 index 0000000..0c57c21 --- /dev/null +++ b/source/CT_Config/load_ctconfig_file.py @@ -0,0 +1,11 @@ +import json + + +def load_ctconfig_file(self, ctconfig_file: str = "./ct_config.json"): + """ + :param ctconfig_file: path to the ctconfig file + :return: ? + """ + with open(ctconfig_file, encoding="utf-8") as f: + ctconfig_json = json.load(f) + self.load_ctconfig_json(ctconfig_json) \ No newline at end of file diff --git a/source/CT_Config/load_ctconfig_json.py b/source/CT_Config/load_ctconfig_json.py new file mode 100644 index 0000000..862228a --- /dev/null +++ b/source/CT_Config/load_ctconfig_json.py @@ -0,0 +1,32 @@ +from source.Cup import Cup +from source.Track import Track + + +def load_ctconfig_json(self, ctconfig_json: dict): + """ + :param ctconfig_json: json of the ctconfig to load + :return: ? + """ + self.ordered_cups = [] + self.unordered_tracks = [] + self.all_tracks = [] + + for cup_json in ctconfig_json["cup"].values(): # tracks with defined order + cup = Cup() + cup.load_from_json(cup_json) + if not cup.locked: # locked cup are not useful (they are original track or random track) + self.ordered_cups.append(cup) + self.all_tracks.extend(cup.tracks) + + for track_json in ctconfig_json["tracks_list"]: # unordered tracks + track = Track() + track.load_from_json(track_json) + self.unordered_tracks.append(track) + self.all_tracks.append(track) + + self.version = ctconfig_json["version"] + + self.all_version = set() + for track in self.all_tracks: + self.all_version.add(track.since_version) + self.all_version = sorted(self.all_version) \ No newline at end of file diff --git a/source/CT_Config/search_tracks.py b/source/CT_Config/search_tracks.py new file mode 100644 index 0000000..966cbcb --- /dev/null +++ b/source/CT_Config/search_tracks.py @@ -0,0 +1,19 @@ +def search_tracks(self, values_list=False, not_value=False, **kwargs): + """ + :param values_list: search track with a value list instead of a single value + :param not_value: search track that does not have value + :param kwargs: any track property = any value + :return: track list respecting condition + """ + track = self.all_tracks.copy() + + if values_list: + if not_value: filter_func = lambda track: getattr(track, keyword) not in value + else: filter_func = lambda track: getattr(track, keyword) in value + else: + if not_value: filter_func = lambda track: getattr(track, keyword) != value + else: filter_func = lambda track: getattr(track, keyword) == value + + for keyword, value in kwargs.items(): + track = list(filter(filter_func, track)) + return track \ No newline at end of file diff --git a/source/Cup.py b/source/Cup.py deleted file mode 100644 index 79b2716..0000000 --- a/source/Cup.py +++ /dev/null @@ -1,36 +0,0 @@ -from .Track import * - - -class Cup: - def __init__(self, name: str = None, - track1: Track = None, - track2: Track = None, - track3: Track = None, - track4: Track = None, locked: bool = False, - *args, **kwargs): - - self.name = name - self.tracks = [ - track1 if track1 else Track(), - track2 if track2 else Track(), - track3 if track3 else Track(), - track4 if track4 else Track() - ] - self.locked = locked - - def load_from_json(self, cup: dict): - for key, value in cup.items(): # load all value in the json as class attribute - if key != "tracks": setattr(self, key, value) - else: # if the key is tracks - for i, track_json in value.items(): # load all tracks from their json - self.tracks[int(i)].load_from_json(track_json) - - def get_ctfile_cup(self, race=False): - """ - :param race: is it a text used for Race_*.szs ? - :return: ctfile definition for the cup - """ - ctfile_cup = f'\nC "{self.name}"\n' - for track in self.tracks: - ctfile_cup += track.get_ctfile_track(race) - return ctfile_cup diff --git a/source/Cup/__init__.py b/source/Cup/__init__.py new file mode 100644 index 0000000..554e5cc --- /dev/null +++ b/source/Cup/__init__.py @@ -0,0 +1,22 @@ +from source.Track import Track + + +class Cup: + def __init__(self, name: str = None, + track1: Track = None, + track2: Track = None, + track3: Track = None, + track4: Track = None, locked: bool = False, + *args, **kwargs): + + self.name = name + self.tracks = [ + track1 if track1 else Track(), + track2 if track2 else Track(), + track3 if track3 else Track(), + track4 if track4 else Track() + ] + self.locked = locked + + from .get_ctfile_cup import get_ctfile_cup + from .load_from_json import load_from_json diff --git a/source/Cup/get_ctfile_cup.py b/source/Cup/get_ctfile_cup.py new file mode 100644 index 0000000..59ab5a1 --- /dev/null +++ b/source/Cup/get_ctfile_cup.py @@ -0,0 +1,9 @@ +def get_ctfile_cup(self, race=False): + """ + :param race: is it a text used for Race_*.szs ? + :return: ctfile definition for the cup + """ + ctfile_cup = f'\nC "{self.name}"\n' + for track in self.tracks: + ctfile_cup += track.get_ctfile_track(race) + return ctfile_cup diff --git a/source/Cup/load_from_json.py b/source/Cup/load_from_json.py new file mode 100644 index 0000000..b0a06cd --- /dev/null +++ b/source/Cup/load_from_json.py @@ -0,0 +1,7 @@ +def load_from_json(self, cup: dict): + for key, value in cup.items(): # load all value in the json as class attribute + if key != "tracks": + setattr(self, key, value) + else: # if the key is tracks + for i, track_json in value.items(): # load all tracks from their json + self.tracks[int(i)].load_from_json(track_json) \ No newline at end of file diff --git a/source/Game.py b/source/Game.py deleted file mode 100644 index bfe1195..0000000 --- a/source/Game.py +++ /dev/null @@ -1,501 +0,0 @@ -from . import wszst -from .definition import * - -from threading import Thread -from PIL import Image -import subprocess -import shutil -import json -import glob -import os - -region_id_to_name = { - "J": "JAP", - "P": "PAL", - "K": "KO", - "E": "USA" -} - - -class InvalidGamePath(Exception): - def __init__(self): - super().__init__("This path is not valid !") - - -class InvalidFormat(Exception): - def __init__(self): - super().__init__("This game format is not supported !") - - -class TooMuchDownloadFailed(Exception): - def __init__(self): - super().__init__("Too much download failed !") - - -class TooMuchSha1CheckFailed(Exception): - def __init__(self): - super().__init__("Too much sha1 check failed !") - - -class CantConvertTrack(Exception): - def __init__(self): - super().__init__("Can't convert track, check if download are enabled.") - - -def patch_img_desc(img_desc_path: str = "./file/img_desc", dest_dir: str = "./file"): - il = Image.open(img_desc_path+"/illustration.png") - il_16_9 = il.resize((832, 456)) - il_4_3 = il.resize((608, 456)) - - for file_lang in glob.glob(img_desc_path+"??.png"): - img_lang = Image.open(file_lang) - img_lang_16_9 = img_lang.resize((832, 456)) - img_lang_4_3 = img_lang.resize((608, 456)) - - new_16_9 = Image.new("RGBA", (832, 456), (0, 0, 0, 255)) - new_16_9.paste(il_16_9, (0, 0), il_16_9) - new_16_9.paste(img_lang_16_9, (0, 0), img_lang_16_9) - new_16_9.save(dest_dir+f"/strapA_16_9_832x456{get_filename(get_nodir(file_lang))}.png") - - new_4_3 = Image.new("RGBA", (608, 456), (0, 0, 0, 255)) - new_4_3.paste(il_4_3, (0, 0), il_4_3) - new_4_3.paste(img_lang_4_3, (0, 0), img_lang_4_3) - new_4_3.save(dest_dir+f"/strapA_608x456{get_filename(get_nodir(file_lang))}.png") - - -def patch_image(fc, gui): - for i, file in enumerate(fc["img"]): - gui.progress(statut=gui.translate("Converting images") + f"\n({i + 1}/{len(fc['img'])}) {file}", add=1) - subprocess.run(["./tools/szs/wimgt", "ENCODE", "./file/" + file, "-x", fc["img"][file], "--overwrite"], - creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) - - -def patch_bmg(gamefile: str, gui): # gamefile est le fichier .szs trouvé dans le /files/Scene/UI/ du jeu - NINTENDO_CWF_REPLACE = "Wiimmfi" - MAINMENU_REPLACE = f"MKWFaraphel {gui.ctconfig.version}" - menu_replacement = { - "CWF de Nintendo": NINTENDO_CWF_REPLACE, - "Wi-Fi Nintendo": NINTENDO_CWF_REPLACE, - "CWF Nintendo": NINTENDO_CWF_REPLACE, - "Nintendo WFC": NINTENDO_CWF_REPLACE, - "Wi-Fi": NINTENDO_CWF_REPLACE, - "インターネット": NINTENDO_CWF_REPLACE, - - "Menu principal": MAINMENU_REPLACE, - "Menú principal": MAINMENU_REPLACE, - "Main Menu": MAINMENU_REPLACE, - "トップメニュー": MAINMENU_REPLACE, - - "Mario Kart Wii": MAINMENU_REPLACE, - } - - bmglang = gamefile[-len("E.txt"):-len(".txt")] # Langue du fichier - gui.progress(statut=gui.translate("Patching text", " ", bmglang), add=1) - - subprocess.run(["./tools/szs/wszst", "EXTRACT", get_nodir(gamefile), "-d", get_nodir(gamefile) + ".d", - "--overwrite"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(gamefile)) - - # Menu.bmg - bmgmenu = subprocess.run(["./tools/szs/wbmgt", "CAT", get_nodir(gamefile) + ".d/message/Menu.bmg"], - creationflags=CREATE_NO_WINDOW, cwd=get_dir(gamefile), - check=True, stdout=subprocess.PIPE).stdout.decode() - - # Common.bmg - bmgtracks = subprocess.run(["./tools/szs/wbmgt", "CAT", get_nodir(gamefile) + ".d/message/Common.bmg"], - creationflags=CREATE_NO_WINDOW, cwd=get_dir(gamefile), - check=True, stdout=subprocess.PIPE).stdout.decode() - trackheader = "#--- standard track names" - trackend = "2328" - bmgtracks = bmgtracks[bmgtracks.find(trackheader) + len(trackheader):bmgtracks.find(trackend)] - - with open("./file/ExtraCommon.txt", "w", encoding="utf8") as f: - f.write("#BMG\n\n" - f" 703e\t= \\\\c{{white}}{gui.translate('Random: All tracks', lang=bmglang)}\n" - f" 703f\t= \\\\c{{white}}{gui.translate('Random: Original tracks', lang=bmglang)}\n" - f" 7040\t= \\\\c{{white}}{gui.translate('Random: Custom Tracks', lang=bmglang)}\n" - f" 7041\t= \\\\c{{white}}{gui.translate('Random: New tracks', lang=bmglang)}\n") - - for bmgtrack in bmgtracks.split("\n"): - if "=" in bmgtrack: - - prefix = "" - if "T" in bmgtrack[:bmgtrack.find("=")]: - sTid = bmgtrack.find("T") - Tid = bmgtrack[sTid:sTid + 3] - if Tid[1] in "1234": - prefix = trackname_color["Wii"] + " " # Si la course est original à la wii - Tid = hex(bmgID_track_move[Tid])[2:] - - else: # Arena - sTid = bmgtrack.find("U") + 1 - Tid = bmgtrack[sTid:sTid + 2] - Tid = hex((int(Tid[0]) - 1) * 5 + (int(Tid[1]) - 1) + 0x7020)[2:] - - Tname = bmgtrack[bmgtrack.find("= ") + 2:] - f.write(f" {Tid}\t= {prefix}{Tname}\n") - - if not (os.path.exists("./file/tmp/")): os.makedirs("./file/tmp/") - - filecopy(gamefile + ".d/message/Common.bmg", "./file/tmp/Common.bmg") - bmgcommon = subprocess.run( - ["tools/szs/wctct", "bmg", "--le-code", "--long", "./file/CTFILE.txt", "--patch-bmg", - "OVERWRITE=./file/tmp/Common.bmg", "--patch-bmg", "OVERWRITE=./file/ExtraCommon.txt"], - creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE).stdout.decode() - rbmgcommon = subprocess.run( - ["tools/szs/wctct", "bmg", "--le-code", "--long", "./file/RCTFILE.txt", "--patch-bmg", - "OVERWRITE=./file/tmp/Common.bmg", "--patch-bmg", "OVERWRITE=./file/ExtraCommon.txt"], - creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE).stdout.decode() - - shutil.rmtree(gamefile + ".d") - os.remove("./file/tmp/Common.bmg") - os.remove("./file/ExtraCommon.txt") - - def finalise(file, bmgtext, replacement_list=None): - if replacement_list: - for text, colored_text in replacement_list.items(): bmgtext = bmgtext.replace(text, colored_text) - with open(file, "w", encoding="utf-8") as f: - f.write(bmgtext) - subprocess.run(["./tools/szs/wbmgt", "ENCODE", get_nodir(file), "--overwrite"], - creationflags=CREATE_NO_WINDOW, cwd=get_dir(file)) - os.remove(file) - - finalise(f"./file/Menu_{bmglang}.txt", bmgmenu, menu_replacement) - finalise(f"./file/Common_{bmglang}.txt", bmgcommon) - finalise(f"./file/Common_R{bmglang}.txt", rbmgcommon) - - -def patch_track(gui): - max_process = gui.intvar_process_track.get() - process_list = {} - error_count, error_max = 0, 3 - - def add_process(track): - nonlocal error_count, error_max, process_list - track_file = track.get_track_name() - total_track = len(gui.ctconfig.all_tracks) - - process_list[track_file] = None # Used for showing track in progress even if there's no process - gui.progress(statut=gui.translate("Converting tracks", f"\n({i + 1}/{total_track})\n", - "\n".join(process_list.keys())), add=1) - - for _track in [track.file_szs, track.file_wu8]: - if os.path.exists(_track): - if os.path.getsize(_track) < 1000: # File under this size are corrupted - os.remove(_track) - - if not gui.boolvar_disable_download.get(): - while True: - download_returncode = track.download_wu8() - if download_returncode == -1: # can't download - error_count += 1 - if error_count > error_max: # Too much track wasn't correctly converted - """messagebox.showerror( - gui.translate("Error"), - gui.translate("Too much tracks had a download issue.")) - return -1""" - raise TooMuchDownloadFailed() - else: - """messagebox.showwarning(gui.translate("Warning"), - gui.translate("Can't download this track !", - f" ({error_count} / {error_max})"))""" - elif download_returncode == 2: - break # if download is disabled, do not check sha1 - - if track.sha1: - if not gui.boolvar_dont_check_track_sha1.get(): - if not track.check_sha1(): # Check si le sha1 du fichier est le bon - error_count += 1 - if error_count > error_max: # Too much track wasn't correctly converted - """messagebox.showerror( - gui.translate("Error"), - gui.translate("Too much tracks had an issue during sha1 check."))""" - raise TooMuchSha1CheckFailed() - continue - - break - - if not ( - os.path.exists(track.file_szs)) or download_returncode == 3: # returncode 3 is track has been updated - if os.path.exists(track.file_wu8): - process_list[track_file] = track.convert_wu8_to_szs() - else: - """messagebox.showerror(gui.translate("Error"), - gui.translate("Can't convert track.\nEnable track download and retry."))""" - raise CantConvertTrack() - elif gui.boolvar_del_track_after_conv.get(): - os.remove(track.file_wu8) - return 0 - - def clean_process(): - nonlocal error_count, error_max, process_list - - for track_file, process in process_list.copy().items(): - if process is not None: - if process.poll() is None: - pass # if the process is still running - else: # process ended - process_list.pop(track_file) - stderr = process.stderr.read() - if b"wszst: ERROR" in stderr: # Error occured - os.remove(track.file_szs) - error_count += 1 - if error_count > error_max: # Too much track wasn't correctly converted - """messagebox.showerror( - gui.translate("Error"), - gui.translate("Too much track had a conversion issue."))""" - raise CantConvertTrack - else: # if the error max hasn't been reach - """messagebox.showwarning( - gui.translate("Warning"), - gui.translate("The track", " ", track.file_wu8, - "do not have been properly converted.", - f" ({error_count} / {error_max})"))""" - else: - if gui.boolvar_del_track_after_conv.get(): os.remove(track.file_wu8) - else: - process_list.pop(track_file) - if not (any(process_list.values())): return 1 # si il n'y a plus de processus - - if len(process_list): - return 1 - else: - return 0 - - for i, track in enumerate(gui.ctconfig.all_tracks): - while True: - if len(process_list) < max_process: - returncode = add_process(track) - if returncode == 0: - break - elif returncode == -1: - return -1 # if error occur, stop function - elif clean_process() == -1: - return -1 - - while True: - returncode = clean_process() - if returncode == 1: - break # End the process if all process ended - elif returncode == 0: - pass - else: - return -1 - - return 0 - - -class Game: - def __init__(self, path: str, region_ID: str = "P", game_ID: str = "RMCP01"): - if not os.path.exists(path): raise InvalidGamePath() - self.extension = get_extension(path).upper() - self.path = path - self.region = region_id_to_name[region_ID] - self.region_ID = region_ID - self.game_ID = game_ID - - def extract_game(self): - if self.extension == "DOL": - self.path = os.path.realpath(self.path + "/../../") # main.dol is in PATH/sys/, so go back 2 dir upper - - elif self.extension in ["ISO", "WBFS", "CSIO"]: - # Fiding a directory name that doesn't already exist - directory_name, i = "MKWiiFaraphel", 1 - while True: - path_dir = os.path.realpath(self.path + f"/../{directory_name}") - if not (os.path.exists(path_dir)): break - directory_name, i = f"MKWiiFaraphel ({i})", i + 1 - - wszst.extract(self.path, path_dir) - - self.path = path_dir - if os.path.exists(self.path + "/DATA"): self.path += "/DATA" - self.extension = "DOL" - - else: - raise InvalidFormat() - - if glob.glob(self.path + "/files/rel/lecode-???.bin"): # if a LECODE file is already here - raise Warning("ROM Already patched") # warning already patched - - with open(self.path + "/setup.txt") as f: setup = f.read() - setup = setup[setup.find("!part-id = ") + len("!part-id = "):] - self.game_ID = setup[:setup.find("\n")] - - self.region_ID = self.game_ID[3] - self.region = region_id_to_name[self.region_ID] if self.region_ID in region_id_to_name else self.region - - def patch_autoadd(self, auto_add_dir: str = "./file/auto-add"): - if os.path.exists(auto_add_dir): shutil.rmtree(auto_add_dir) - if not os.path.exists(self.path + "/tmp/"): os.makedirs(self.path + "/tmp/") - subprocess.run(["./tools/szs/wszst", "AUTOADD", get_nodir(self.path) + "/files/Race/Course/", - "--DEST", get_nodir(self.path) + "/tmp/auto-add/"], - creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path), - check=True, stdout=subprocess.PIPE) - shutil.move(self.path + "/tmp/auto-add/", auto_add_dir) - shutil.rmtree(self.path + "/tmp/") - - def install_mod(self, gui): - def func(): - try: - with open("./fs.json") as f: fs = json.load(f) - - # This part is used to estimate the max_step - extracted_file = [] - max_step, step = 1, 0 - - def count_rf(path): - nonlocal max_step - max_step += 1 - if get_extension(path) == "szs": - if not (os.path.realpath(path) in extracted_file): - extracted_file.append(os.path.realpath(path)) - max_step += 1 - - for fp in fs: - for f in glob.glob(self.path + "/files/" + fp, recursive=True): - if type(fs[fp]) == str: - count_rf(path=f) - elif type(fs[fp]) == dict: - for nf in fs[fp]: - if type(fs[fp][nf]) == str: - count_rf(path=f) - elif type(fs[fp][nf]) == list: - for ffp in fs[fp][nf]: count_rf(path=f) - ### - extracted_file = [] - max_step += 4 # PATCH main.dol and PATCH lecode.bin, converting, changing ID - gui.progress(show=True, indeter=False, statut=gui.translate("Installing mod"), max=max_step, step=0) - - def replace_file(path, file, subpath="/"): - gui.progress(statut=gui.translate("Editing", "\n", get_nodir(path)), add=1) - extension = get_extension(path) - - if extension == "szs": - if not (os.path.realpath(path) in extracted_file): - subprocess.run(["./tools/szs/wszst", "EXTRACT", get_nodir(path), "-d", get_nodir(path) + ".d", - "--overwrite"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(path), - check=True, stdout=subprocess.PIPE) - extracted_file.append(os.path.realpath(path)) - - szs_extract_path = path + ".d" - if os.path.exists(szs_extract_path + subpath): - if subpath[-1] == "/": - filecopy(f"./file/{file}", szs_extract_path + subpath + file) - else: - filecopy(f"./file/{file}", szs_extract_path + subpath) - - elif path[-1] == "/": - filecopy(f"./file/{file}", path + file) - else: - filecopy(f"./file/{file}", path) - - for fp in fs: - for f in glob.glob(self.path + "/files/" + fp, recursive=True): - if type(fs[fp]) == str: - replace_file(path=f, file=fs[fp]) - elif type(fs[fp]) == dict: - for nf in fs[fp]: - if type(fs[fp][nf]) == str: - replace_file(path=f, subpath=nf, file=fs[fp][nf]) - elif type(fs[fp][nf]) == list: - for ffp in fs[fp][nf]: replace_file(path=f, subpath=nf, file=ffp) - - for file in extracted_file: - gui.progress(statut=gui.translate("Recompilating", "\n", get_nodir(file)), add=1) - subprocess.run(["./tools/szs/wszst", "CREATE", get_nodir(file) + ".d", "-d", get_nodir(file), - "--overwrite"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(file), - check=True, stdout=subprocess.PIPE) - if os.path.exists(file + ".d"): shutil.rmtree(file + ".d") - - gui.progress(statut=gui.translate("Patch main.dol"), add=1) - subprocess.run(["./tools/szs/wstrt", "patch", get_nodir(self.path) + "/sys/main.dol", "--clean-dol", - "--add-lecode"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path), - check=True, stdout=subprocess.PIPE) - - gui.progress(statut=gui.translate("Patch lecode.bin"), add=1) - - shutil.copytree("./file/Track/", self.path+"/files/Race/Course/", dirs_exist_ok=True) - if not(os.path.exists(self.path+"/tmp/")): os.makedirs(self.path+"/tmp/") - filecopy("./file/CTFILE.txt", self.path+"/tmp/CTFILE.txt") - filecopy("./file/lpar-default.txt", self.path + "/tmp/lpar-default.txt") - filecopy(f"./file/lecode-{self.region}.bin", self.path + f"/tmp/lecode-{self.region}.bin") - - subprocess.run( - ["./tools/szs/wlect", "patch", f"./tmp/lecode-{self.region}.bin", "-od", - f"./files/rel/lecode-{self.region}.bin", "--track-dir", "./files/Race/Course/", - "--move-tracks", "./files/Race/Course/", "--le-define", "./tmp/CTFILE.txt", "--lpar", - "./tmp/lpar-default.txt", "--overwrite"], - creationflags=CREATE_NO_WINDOW, cwd=self.path, check=True, stdout=subprocess.PIPE) - - shutil.rmtree(self.path + "/tmp/") - - output_format = gui.stringvar_game_format.get() - gui.progress(statut=gui.translate("Converting to", " ", output_format), add=1) - - if output_format in ["ISO", "WBFS", "CISO"]: - path_game_format: str = os.path.realpath(self.path + "/../MKWFaraphel." + output_format.lower()) - subprocess.run(["./tools/wit/wit", "COPY", get_nodir(self.path), "--DEST", - get_nodir(path_game_format), f"--{output_format.lower()}", "--overwrite"], - creationflags=CREATE_NO_WINDOW, cwd=get_dir(path_game_format), - check=True, stdout=subprocess.PIPE) - shutil.rmtree(self.path) - self.path = path_game_format - - gui.progress(statut=gui.translate("Changing game's ID"), add=1) - subprocess.run(["./tools/wit/wit", "EDIT", get_nodir(self.path), "--id", - f"RMC{self.region_ID}60", "--name", - f"Mario Kart Wii Faraphel {gui.ctconfig.version}", "--modify", "ALL"], - creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path), - check=True, stdout=subprocess.PIPE) - - # messagebox.showinfo(gui.translate("End"), gui.translate("The mod has been installed !")) - - except: gui.log_error() - finally: gui.progress(show=False) - - t = Thread(target=func) - t.setDaemon(True) - t.start() - return t - - def convert_to(self, format: str = "FST"): - """ - :param format: game format (ISO, WBFS, ...) - :return: converted game path - """ - - def patch_file(self, gui): - def func(): - try: - if not (os.path.exists("./file/Track-WU8/")): os.makedirs("./file/Track-WU8/") - with open("./convert_file.json") as f: - fc = json.load(f) - max_step = len(fc["img"]) + len(gui.ctconfig.all_tracks) + 3 + len("EGFIS") - - gui.progress(show=True, indeter=False, statut=gui.translate("Converting files"), max=max_step, step=0) - gui.progress(statut=gui.translate("Configurating LE-CODE"), add=1) - gui.ctconfig.create_ctfile() - - gui.progress(statut=gui.translate("Creating ct_icon.png"), add=1) - ct_icon = gui.ctconfig.get_cticon() - ct_icon.save("./file/ct_icons.tpl.png") - - gui.progress(statut=gui.translate("Creating descriptive images"), add=1) - patch_img_desc() - patch_image(fc, gui) - for file in glob.glob(self.path + "/files/Scene/UI/MenuSingle_?.szs"): patch_bmg(file, gui) - # MenuSingle could be any other file, Common and Menu are all the same in all other files. - self.patch_autoadd() - if patch_track(gui) != 0: return - - gui.button_install_mod.grid(row=2, column=1, columnspan=2, sticky="NEWS") - gui.button_install_mod.config( - text=gui.translate("Install mod", " (v", gui.ctconfig.version, ")")) - - except: gui.log_error() - finally: gui.progress(show=False) - - t = Thread(target=func) - t.setDaemon(True) - t.start() - return t diff --git a/source/Game/__init__.py b/source/Game/__init__.py new file mode 100644 index 0000000..22f3960 --- /dev/null +++ b/source/Game/__init__.py @@ -0,0 +1,24 @@ +from source.definition import * +import os + +from .exception import * + + +class Game: + def __init__(self, path: str, region_ID: str = "P", game_ID: str = "RMCP01"): + if not os.path.exists(path): raise InvalidGamePath() + self.extension = get_extension(path).upper() + self.path = path + self.region = region_id_to_name[region_ID] + self.region_ID = region_ID + self.game_ID = game_ID + + from .convert_to import convert_to + from .extract import extract + from .install_mod import install_mod + from .patch_autoadd import patch_autoadd + from .patch_bmg import patch_bmg + from .patch_file import patch_file + from .patch_image import patch_image + from .patch_img_desc import patch_img_desc + from .patch_track import patch_track diff --git a/source/Game/convert_to.py b/source/Game/convert_to.py new file mode 100644 index 0000000..9e53651 --- /dev/null +++ b/source/Game/convert_to.py @@ -0,0 +1,7 @@ +def convert_to(self, format: str = "FST"): + """ + :param format: game format (ISO, WBFS, ...) + :return: converted game path + """ + +# TODO: code this function \ No newline at end of file diff --git a/source/Game/exception.py b/source/Game/exception.py new file mode 100644 index 0000000..6136281 --- /dev/null +++ b/source/Game/exception.py @@ -0,0 +1,23 @@ +class InvalidGamePath(Exception): + def __init__(self): + super().__init__("This path is not valid !") + + +class InvalidFormat(Exception): + def __init__(self): + super().__init__("This game format is not supported !") + + +class TooMuchDownloadFailed(Exception): + def __init__(self): + super().__init__("Too much download failed !") + + +class TooMuchSha1CheckFailed(Exception): + def __init__(self): + super().__init__("Too much sha1 check failed !") + + +class CantConvertTrack(Exception): + def __init__(self): + super().__init__("Can't convert track, check if download are enabled.") \ No newline at end of file diff --git a/source/Game/extract.py b/source/Game/extract.py new file mode 100644 index 0000000..ec1df11 --- /dev/null +++ b/source/Game/extract.py @@ -0,0 +1,39 @@ +import glob +import os + +from .exception import * +from source.definition import * +from source import wszst + + +def extract(self): + if self.extension == "DOL": + self.path = os.path.realpath(self.path + "/../../") # main.dol is in PATH/sys/, so go back 2 dir upper + + elif self.extension in ["ISO", "WBFS", "CSIO"]: + # Fiding a directory name that doesn't already exist + directory_name, i = "MKWiiFaraphel", 1 + while True: + path_dir = os.path.realpath(self.path + f"/../{directory_name}") + if not (os.path.exists(path_dir)): break + directory_name, i = f"MKWiiFaraphel ({i})", i + 1 + + wszst.extract(self.path, path_dir) + + self.path = path_dir + if os.path.exists(self.path + "/DATA"): self.path += "/DATA" + self.extension = "DOL" + + else: + raise InvalidFormat() + + if glob.glob(self.path + "/files/rel/lecode-???.bin"): # if a LECODE file is already here + raise Warning("ROM Already patched") # warning already patched + + with open(self.path + "/setup.txt") as f: + setup = f.read() + setup = setup[setup.find("!part-id = ") + len("!part-id = "):] + self.game_ID = setup[:setup.find("\n")] + + self.region_ID = self.game_ID[3] + self.region = region_id_to_name[self.region_ID] if self.region_ID in region_id_to_name else self.region \ No newline at end of file diff --git a/source/Game/install_mod.py b/source/Game/install_mod.py new file mode 100644 index 0000000..e02fec6 --- /dev/null +++ b/source/Game/install_mod.py @@ -0,0 +1,136 @@ +from threading import Thread +import subprocess +import shutil +import json +import glob +import os + +from source.definition import * + + +def install_mod(self, gui): + def func(): + try: + with open("./fs.json") as f: + fs = json.load(f) + + # This part is used to estimate the max_step + extracted_file = [] + max_step, step = 1, 0 + + def count_rf(path): + nonlocal max_step + max_step += 1 + if get_extension(path) == "szs": + if not (os.path.realpath(path) in extracted_file): + extracted_file.append(os.path.realpath(path)) + max_step += 1 + + for fp in fs: + for f in glob.glob(self.path + "/files/" + fp, recursive=True): + if type(fs[fp]) == str: + count_rf(path=f) + elif type(fs[fp]) == dict: + for nf in fs[fp]: + if type(fs[fp][nf]) == str: + count_rf(path=f) + elif type(fs[fp][nf]) == list: + for ffp in fs[fp][nf]: count_rf(path=f) + ### + extracted_file = [] + max_step += 4 # PATCH main.dol and PATCH lecode.bin, converting, changing ID + gui.progress(show=True, indeter=False, statut=gui.translate("Installing mod"), max=max_step, step=0) + + def replace_file(path, file, subpath="/"): + gui.progress(statut=gui.translate("Editing", "\n", get_nodir(path)), add=1) + extension = get_extension(path) + + if extension == "szs": + if not (os.path.realpath(path) in extracted_file): + subprocess.run(["./tools/szs/wszst", "EXTRACT", get_nodir(path), "-d", get_nodir(path) + ".d", + "--overwrite"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(path), + check=True, stdout=subprocess.PIPE) + extracted_file.append(os.path.realpath(path)) + + szs_extract_path = path + ".d" + if os.path.exists(szs_extract_path + subpath): + if subpath[-1] == "/": + filecopy(f"./file/{file}", szs_extract_path + subpath + file) + else: + filecopy(f"./file/{file}", szs_extract_path + subpath) + + elif path[-1] == "/": + filecopy(f"./file/{file}", path + file) + else: + filecopy(f"./file/{file}", path) + + for fp in fs: + for f in glob.glob(self.path + "/files/" + fp, recursive=True): + if type(fs[fp]) == str: + replace_file(path=f, file=fs[fp]) + elif type(fs[fp]) == dict: + for nf in fs[fp]: + if type(fs[fp][nf]) == str: + replace_file(path=f, subpath=nf, file=fs[fp][nf]) + elif type(fs[fp][nf]) == list: + for ffp in fs[fp][nf]: replace_file(path=f, subpath=nf, file=ffp) + + for file in extracted_file: + gui.progress(statut=gui.translate("Recompilating", "\n", get_nodir(file)), add=1) + subprocess.run(["./tools/szs/wszst", "CREATE", get_nodir(file) + ".d", "-d", get_nodir(file), + "--overwrite"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(file), + check=True, stdout=subprocess.PIPE) + if os.path.exists(file + ".d"): shutil.rmtree(file + ".d") + + gui.progress(statut=gui.translate("Patch main.dol"), add=1) + subprocess.run(["./tools/szs/wstrt", "patch", get_nodir(self.path) + "/sys/main.dol", "--clean-dol", + "--add-lecode"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path), + check=True, stdout=subprocess.PIPE) + + gui.progress(statut=gui.translate("Patch lecode.bin"), add=1) + + shutil.copytree("./file/Track/", self.path + "/files/Race/Course/", dirs_exist_ok=True) + if not (os.path.exists(self.path + "/tmp/")): os.makedirs(self.path + "/tmp/") + filecopy("./file/CTFILE.txt", self.path + "/tmp/CTFILE.txt") + filecopy("./file/lpar-default.txt", self.path + "/tmp/lpar-default.txt") + filecopy(f"./file/lecode-{self.region}.bin", self.path + f"/tmp/lecode-{self.region}.bin") + + subprocess.run( + ["./tools/szs/wlect", "patch", f"./tmp/lecode-{self.region}.bin", "-od", + f"./files/rel/lecode-{self.region}.bin", "--track-dir", "./files/Race/Course/", + "--move-tracks", "./files/Race/Course/", "--le-define", "./tmp/CTFILE.txt", "--lpar", + "./tmp/lpar-default.txt", "--overwrite"], + creationflags=CREATE_NO_WINDOW, cwd=self.path, check=True, stdout=subprocess.PIPE) + + shutil.rmtree(self.path + "/tmp/") + + output_format = gui.stringvar_game_format.get() + gui.progress(statut=gui.translate("Converting to", " ", output_format), add=1) + + if output_format in ["ISO", "WBFS", "CISO"]: + path_game_format: str = os.path.realpath(self.path + "/../MKWFaraphel." + output_format.lower()) + subprocess.run(["./tools/wit/wit", "COPY", get_nodir(self.path), "--DEST", + get_nodir(path_game_format), f"--{output_format.lower()}", "--overwrite"], + creationflags=CREATE_NO_WINDOW, cwd=get_dir(path_game_format), + check=True, stdout=subprocess.PIPE) + shutil.rmtree(self.path) + self.path = path_game_format + + gui.progress(statut=gui.translate("Changing game's ID"), add=1) + subprocess.run(["./tools/wit/wit", "EDIT", get_nodir(self.path), "--id", + f"RMC{self.region_ID}60", "--name", + f"Mario Kart Wii Faraphel {gui.ctconfig.version}", "--modify", "ALL"], + creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path), + check=True, stdout=subprocess.PIPE) + + # messagebox.showinfo(gui.translate("End"), gui.translate("The mod has been installed !")) + + except: + gui.log_error() + finally: + gui.progress(show=False) + + t = Thread(target=func) + t.setDaemon(True) + t.start() + return t \ No newline at end of file diff --git a/source/Game/patch_autoadd.py b/source/Game/patch_autoadd.py new file mode 100644 index 0000000..d95a5a6 --- /dev/null +++ b/source/Game/patch_autoadd.py @@ -0,0 +1,16 @@ +import subprocess +import shutil +import os + +from source.definition import * + + +def patch_autoadd(self, auto_add_dir: str = "./file/auto-add"): + if os.path.exists(auto_add_dir): shutil.rmtree(auto_add_dir) + if not os.path.exists(self.path + "/tmp/"): os.makedirs(self.path + "/tmp/") + subprocess.run(["./tools/szs/wszst", "AUTOADD", get_nodir(self.path) + "/files/Race/Course/", + "--DEST", get_nodir(self.path) + "/tmp/auto-add/"], + creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path), + check=True, stdout=subprocess.PIPE) + shutil.move(self.path + "/tmp/auto-add/", auto_add_dir) + shutil.rmtree(self.path + "/tmp/") diff --git a/source/Game/patch_bmg.py b/source/Game/patch_bmg.py new file mode 100644 index 0000000..8c8cd37 --- /dev/null +++ b/source/Game/patch_bmg.py @@ -0,0 +1,99 @@ +import subprocess +import shutil +import os + +from source.definition import * + + +def patch_bmg(gamefile: str, gui): # gamefile est le fichier .szs trouvé dans le /files/Scene/UI/ du jeu + NINTENDO_CWF_REPLACE = "Wiimmfi" + MAINMENU_REPLACE = f"MKWFaraphel {gui.ctconfig.version}" + menu_replacement = { + "CWF de Nintendo": NINTENDO_CWF_REPLACE, + "Wi-Fi Nintendo": NINTENDO_CWF_REPLACE, + "CWF Nintendo": NINTENDO_CWF_REPLACE, + "Nintendo WFC": NINTENDO_CWF_REPLACE, + "Wi-Fi": NINTENDO_CWF_REPLACE, + "インターネット": NINTENDO_CWF_REPLACE, + + "Menu principal": MAINMENU_REPLACE, + "Menú principal": MAINMENU_REPLACE, + "Main Menu": MAINMENU_REPLACE, + "トップメニュー": MAINMENU_REPLACE, + + "Mario Kart Wii": MAINMENU_REPLACE, + } + + bmglang = gamefile[-len("E.txt"):-len(".txt")] # Langue du fichier + gui.progress(statut=gui.translate("Patching text", " ", bmglang), add=1) + + subprocess.run(["./tools/szs/wszst", "EXTRACT", get_nodir(gamefile), "-d", get_nodir(gamefile) + ".d", + "--overwrite"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(gamefile)) + + # Menu.bmg + bmgmenu = subprocess.run(["./tools/szs/wbmgt", "CAT", get_nodir(gamefile) + ".d/message/Menu.bmg"], + creationflags=CREATE_NO_WINDOW, cwd=get_dir(gamefile), + check=True, stdout=subprocess.PIPE).stdout.decode() + + # Common.bmg + bmgtracks = subprocess.run(["./tools/szs/wbmgt", "CAT", get_nodir(gamefile) + ".d/message/Common.bmg"], + creationflags=CREATE_NO_WINDOW, cwd=get_dir(gamefile), + check=True, stdout=subprocess.PIPE).stdout.decode() + trackheader = "#--- standard track names" + trackend = "2328" + bmgtracks = bmgtracks[bmgtracks.find(trackheader) + len(trackheader):bmgtracks.find(trackend)] + + with open("./file/ExtraCommon.txt", "w", encoding="utf8") as f: + f.write("#BMG\n\n" + f" 703e\t= \\\\c{{white}}{gui.translate('Random: All tracks', lang=bmglang)}\n" + f" 703f\t= \\\\c{{white}}{gui.translate('Random: Original tracks', lang=bmglang)}\n" + f" 7040\t= \\\\c{{white}}{gui.translate('Random: Custom Tracks', lang=bmglang)}\n" + f" 7041\t= \\\\c{{white}}{gui.translate('Random: New tracks', lang=bmglang)}\n") + + for bmgtrack in bmgtracks.split("\n"): + if "=" in bmgtrack: + + prefix = "" + if "T" in bmgtrack[:bmgtrack.find("=")]: + sTid = bmgtrack.find("T") + Tid = bmgtrack[sTid:sTid + 3] + if Tid[1] in "1234": + prefix = trackname_color["Wii"] + " " # Si la course est original à la wii + Tid = hex(bmgID_track_move[Tid])[2:] + + else: # Arena + sTid = bmgtrack.find("U") + 1 + Tid = bmgtrack[sTid:sTid + 2] + Tid = hex((int(Tid[0]) - 1) * 5 + (int(Tid[1]) - 1) + 0x7020)[2:] + + Tname = bmgtrack[bmgtrack.find("= ") + 2:] + f.write(f" {Tid}\t= {prefix}{Tname}\n") + + if not (os.path.exists("./file/tmp/")): os.makedirs("./file/tmp/") + + filecopy(gamefile + ".d/message/Common.bmg", "./file/tmp/Common.bmg") + bmgcommon = subprocess.run( + ["tools/szs/wctct", "bmg", "--le-code", "--long", "./file/CTFILE.txt", "--patch-bmg", + "OVERWRITE=./file/tmp/Common.bmg", "--patch-bmg", "OVERWRITE=./file/ExtraCommon.txt"], + creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE).stdout.decode() + rbmgcommon = subprocess.run( + ["tools/szs/wctct", "bmg", "--le-code", "--long", "./file/RCTFILE.txt", "--patch-bmg", + "OVERWRITE=./file/tmp/Common.bmg", "--patch-bmg", "OVERWRITE=./file/ExtraCommon.txt"], + creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE).stdout.decode() + + shutil.rmtree(gamefile + ".d") + os.remove("./file/tmp/Common.bmg") + os.remove("./file/ExtraCommon.txt") + + def finalise(file, bmgtext, replacement_list=None): + if replacement_list: + for text, colored_text in replacement_list.items(): bmgtext = bmgtext.replace(text, colored_text) + with open(file, "w", encoding="utf-8") as f: + f.write(bmgtext) + subprocess.run(["./tools/szs/wbmgt", "ENCODE", get_nodir(file), "--overwrite"], + creationflags=CREATE_NO_WINDOW, cwd=get_dir(file)) + os.remove(file) + + finalise(f"./file/Menu_{bmglang}.txt", bmgmenu, menu_replacement) + finalise(f"./file/Common_{bmglang}.txt", bmgcommon) + finalise(f"./file/Common_R{bmglang}.txt", rbmgcommon) \ No newline at end of file diff --git a/source/Game/patch_file.py b/source/Game/patch_file.py new file mode 100644 index 0000000..bf977a3 --- /dev/null +++ b/source/Game/patch_file.py @@ -0,0 +1,48 @@ +from threading import Thread +import glob +import json +import os + +from .patch_img_desc import patch_img_desc +from .patch_image import patch_image +from .patch_track import patch_track +from .patch_bmg import patch_bmg + + +def patch_file(self, gui): + def func(): + try: + if not (os.path.exists("./file/Track-WU8/")): os.makedirs("./file/Track-WU8/") + with open("./convert_file.json") as f: + fc = json.load(f) + max_step = len(fc["img"]) + len(gui.ctconfig.all_tracks) + 3 + len("EGFIS") + + gui.progress(show=True, indeter=False, statut=gui.translate("Converting files"), max=max_step, step=0) + gui.progress(statut=gui.translate("Configurating LE-CODE"), add=1) + gui.ctconfig.create_ctfile() + + gui.progress(statut=gui.translate("Creating ct_icon.png"), add=1) + ct_icon = gui.ctconfig.get_cticon() + ct_icon.save("./file/ct_icons.tpl.png") + + gui.progress(statut=gui.translate("Creating descriptive images"), add=1) + patch_img_desc() + patch_image(fc, gui) + for file in glob.glob(self.path + "/files/Scene/UI/MenuSingle_?.szs"): patch_bmg(file, gui) + # MenuSingle could be any other file, Common and Menu are all the same in all other files. + self.patch_autoadd() + if patch_track(gui) != 0: return + + gui.button_install_mod.grid(row=2, column=1, columnspan=2, sticky="NEWS") + gui.button_install_mod.config( + text=gui.translate("Install mod", " (v", gui.ctconfig.version, ")")) + + except: + gui.log_error() + finally: + gui.progress(show=False) + + t = Thread(target=func) + t.setDaemon(True) + t.start() + return t \ No newline at end of file diff --git a/source/Game/patch_image.py b/source/Game/patch_image.py new file mode 100644 index 0000000..03ab79d --- /dev/null +++ b/source/Game/patch_image.py @@ -0,0 +1,10 @@ +import subprocess + +from source.definition import * + + +def patch_image(fc, gui): + for i, file in enumerate(fc["img"]): + gui.progress(statut=gui.translate("Converting images") + f"\n({i + 1}/{len(fc['img'])}) {file}", add=1) + subprocess.run(["./tools/szs/wimgt", "ENCODE", "./file/" + file, "-x", fc["img"][file], "--overwrite"], + creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE) diff --git a/source/Game/patch_img_desc.py b/source/Game/patch_img_desc.py new file mode 100644 index 0000000..fb1ec35 --- /dev/null +++ b/source/Game/patch_img_desc.py @@ -0,0 +1,25 @@ +from PIL import Image +import glob + +from source.definition import * + + +def patch_img_desc(img_desc_path: str = "./file/img_desc", dest_dir: str = "./file"): + il = Image.open(img_desc_path+"/illustration.png") + il_16_9 = il.resize((832, 456)) + il_4_3 = il.resize((608, 456)) + + for file_lang in glob.glob(img_desc_path+"??.png"): + img_lang = Image.open(file_lang) + img_lang_16_9 = img_lang.resize((832, 456)) + img_lang_4_3 = img_lang.resize((608, 456)) + + new_16_9 = Image.new("RGBA", (832, 456), (0, 0, 0, 255)) + new_16_9.paste(il_16_9, (0, 0), il_16_9) + new_16_9.paste(img_lang_16_9, (0, 0), img_lang_16_9) + new_16_9.save(dest_dir+f"/strapA_16_9_832x456{get_filename(get_nodir(file_lang))}.png") + + new_4_3 = Image.new("RGBA", (608, 456), (0, 0, 0, 255)) + new_4_3.paste(il_4_3, (0, 0), il_4_3) + new_4_3.paste(img_lang_4_3, (0, 0), img_lang_4_3) + new_4_3.save(dest_dir+f"/strapA_608x456{get_filename(get_nodir(file_lang))}.png") \ No newline at end of file diff --git a/source/Game/patch_track.py b/source/Game/patch_track.py new file mode 100644 index 0000000..d1608a6 --- /dev/null +++ b/source/Game/patch_track.py @@ -0,0 +1,123 @@ +import os + +from .exception import * + + +def patch_track(gui): + max_process = gui.intvar_process_track.get() + process_list = {} + error_count, error_max = 0, 3 + + def add_process(track): + nonlocal error_count, error_max, process_list + track_file = track.get_track_name() + total_track = len(gui.ctconfig.all_tracks) + + process_list[track_file] = None # Used for showing track in progress even if there's no process + gui.progress(statut=gui.translate("Converting tracks", f"\n({i + 1}/{total_track})\n", + "\n".join(process_list.keys())), add=1) + + for _track in [track.file_szs, track.file_wu8]: + if os.path.exists(_track): + if os.path.getsize(_track) < 1000: # File under this size are corrupted + os.remove(_track) + + if not gui.boolvar_disable_download.get(): + while True: + download_returncode = track.download_wu8() + if download_returncode == -1: # can't download + error_count += 1 + if error_count > error_max: # Too much track wasn't correctly converted + """messagebox.showerror( + gui.translate("Error"), + gui.translate("Too much tracks had a download issue.")) + return -1""" + raise TooMuchDownloadFailed() + else: + """messagebox.showwarning(gui.translate("Warning"), + gui.translate("Can't download this track !", + f" ({error_count} / {error_max})"))""" + elif download_returncode == 2: + break # if download is disabled, do not check sha1 + + if track.sha1: + if not gui.boolvar_dont_check_track_sha1.get(): + if not track.check_sha1(): # Check si le sha1 du fichier est le bon + error_count += 1 + if error_count > error_max: # Too much track wasn't correctly converted + """messagebox.showerror( + gui.translate("Error"), + gui.translate("Too much tracks had an issue during sha1 check."))""" + raise TooMuchSha1CheckFailed() + continue + + break + + if not ( + os.path.exists(track.file_szs)) or download_returncode == 3: # returncode 3 is track has been updated + if os.path.exists(track.file_wu8): + process_list[track_file] = track.convert_wu8_to_szs() + else: + """messagebox.showerror(gui.translate("Error"), + gui.translate("Can't convert track.\nEnable track download and retry."))""" + raise CantConvertTrack() + elif gui.boolvar_del_track_after_conv.get(): + os.remove(track.file_wu8) + return 0 + + def clean_process(): + nonlocal error_count, error_max, process_list + + for track_file, process in process_list.copy().items(): + if process is not None: + if process.poll() is None: + pass # if the process is still running + else: # process ended + process_list.pop(track_file) + stderr = process.stderr.read() + if b"wszst: ERROR" in stderr: # Error occured + os.remove(track.file_szs) + error_count += 1 + if error_count > error_max: # Too much track wasn't correctly converted + """messagebox.showerror( + gui.translate("Error"), + gui.translate("Too much track had a conversion issue."))""" + raise CantConvertTrack + else: # if the error max hasn't been reach + """messagebox.showwarning( + gui.translate("Warning"), + gui.translate("The track", " ", track.file_wu8, + "do not have been properly converted.", + f" ({error_count} / {error_max})"))""" + else: + if gui.boolvar_del_track_after_conv.get(): os.remove(track.file_wu8) + else: + process_list.pop(track_file) + if not (any(process_list.values())): return 1 # si il n'y a plus de processus + + if len(process_list): + return 1 + else: + return 0 + + for i, track in enumerate(gui.ctconfig.all_tracks): + while True: + if len(process_list) < max_process: + returncode = add_process(track) + if returncode == 0: + break + elif returncode == -1: + return -1 # if error occur, stop function + elif clean_process() == -1: + return -1 + + while True: + returncode = clean_process() + if returncode == 1: + break # End the process if all process ended + elif returncode == 0: + pass + else: + return -1 + + return 0 \ No newline at end of file diff --git a/source/Gui.py b/source/Gui/__init__.py similarity index 63% rename from source/Gui.py rename to source/Gui/__init__.py index fa141ae..46936e3 100644 --- a/source/Gui.py +++ b/source/Gui/__init__.py @@ -1,27 +1,15 @@ -from tkinter import * from tkinter import messagebox, filedialog, ttk from threading import Thread -import subprocess -import traceback -import glob +from tkinter import * import os -from .definition import * -from .CT_Config import * -from .Option import * -from .Game import * +from source.CT_Config import CT_Config +from source.Game.exception import * +from source.Option import Option +from source.Game import Game -with open("./translation.json", encoding="utf-8") as f: - translation_dict = json.load(f) - - -def restart(): - subprocess.Popen([sys.executable] + sys.argv, creationflags=CREATE_NO_WINDOW, cwd=os.getcwd()) - exit() - - -class Gui(): +class Gui: def __init__(self): self.root = Tk() @@ -54,8 +42,8 @@ class Gui(): self.menu_language = Menu(self.menu_bar, tearoff=0) self.menu_bar.add_cascade(label=self.translate("Language"), menu=self.menu_language) - self.menu_language.add_radiobutton(label="Français", variable=self.stringvar_language, value="fr", command=lambda: self.option.edit("language", "fr", restart=True)) - self.menu_language.add_radiobutton(label="English", variable=self.stringvar_language, value="en", command=lambda: self.option.edit("language", "en", restart=True)) + self.menu_language.add_radiobutton(label="Français", variable=self.stringvar_language, value="fr", command=lambda: self.option.edit("language", "fr", need_restart=True)) + self.menu_language.add_radiobutton(label="English", variable=self.stringvar_language, value="en", command=lambda: self.option.edit("language", "en", need_restart=True)) self.menu_format = Menu(self.menu_bar, tearoff=0) self.menu_bar.add_cascade(label=self.translate("Format"), menu=self.menu_format) @@ -90,7 +78,6 @@ class Gui(): self.menu_advanced.add_radiobutton(label=self.translate("4 ", "process"), variable=self.intvar_process_track, value=4, command=lambda: self.option.edit("process_track", 4)) self.menu_advanced.add_radiobutton(label=self.translate("8 ", "process"), variable=self.intvar_process_track, value=8, command=lambda: self.option.edit("process_track", 8)) - self.frame_language = Frame(self.root) self.frame_language.grid(row=1, column=1, sticky="E") @@ -119,7 +106,7 @@ class Gui(): try: self.game = Game(path = entry_game_path.get()) self.progress(show=True, indeter=True, statut=self.translate("Extracting the game...")) - self.game.extract_game() + self.game.extract() self.frame_action.grid(row=3, column=1, sticky="NEWS") except InvalidGamePath: messagebox.showerror(self.translate("Error"), self.translate("The file path in invalid")) @@ -142,7 +129,7 @@ class Gui(): def do_everything(): def func(): use_path().join() - self.game.patch_file(gui).join() + self.game.patch_file(self).join() self.game.install_mod(self).join() if messagebox.askyesno(self.translate("Experimental functionality"), @@ -155,7 +142,6 @@ class Gui(): self.button_do_everything = Button(self.frame_game_path_action, text=self.translate("Do everything"), relief=RIDGE, command=do_everything) self.button_do_everything.grid(row=1, column=2, sticky="NEWS") - self.frame_action = LabelFrame(self.root, text=self.translate("Action")) self.button_prepare_file = Button(self.frame_action, text=self.translate("Prepare files"), relief=RIDGE, command=lambda: self.game.patch_file(self), width=45) @@ -166,120 +152,9 @@ class Gui(): self.progressbar = ttk.Progressbar(self.root) self.progresslabel = Label(self.root) - - def check_update(self): - try: - gitversion = requests.get(VERSION_FILE_URL, allow_redirects=True).json() - with open("./version", "rb") as f: - locversion = json.load(f) - - if ((float(gitversion["version"]) > float(locversion["version"])) or # if github version is newer than - (float(gitversion["version"]) == float(locversion["version"])) and # local version - float(gitversion["subversion"]) > float(locversion["subversion"])): - if messagebox.askyesno( - self.translate("Update available !"), - self.translate("An update is available, do you want to install it ?", - f"\n\nVersion : {locversion['version']}.{locversion['subversion']} -> " - f"{gitversion['version']}.{gitversion['subversion']}\n" - f"Changelog :\n{gitversion['changelog']}")): - - if not (os.path.exists("./Updater/Updater.exe")): - dl = requests.get(gitversion["updater_bin"], allow_redirects=True) - with open("./download.zip", "wb") as file: - print(self.translate("Downloading the Updater...")) - file.write(dl.content) - print(self.translate("end of the download, extracting...")) - - with zipfile.ZipFile("./download.zip") as file: - file.extractall("./Updater/") - print(self.translate("finished extracting")) - - os.remove("./download.zip") - print(self.translate("starting application...")) - os.startfile(os.path.realpath("./Updater/Updater.exe")) - - if ((float(gitversion["version"]) < float(locversion["version"])) or # if local version is newer than - (float(gitversion["version"]) == float(locversion["version"])) and # github version - float(gitversion["subversion"]) < float(locversion["subversion"])): - self.is_dev_version = True - - except requests.ConnectionError: - messagebox.showwarning(self.translate("Warning"), - self.translate("Can't connect to internet. Download will be disabled.")) - self.option.disable_download = True - - except: - self.log_error() - - - def log_error(func): - try: - func() - except Exception: - error = traceback.format_exc() - with open("./error.log", "a") as f: - f.write(f"---\n{error}\n") - messagebox.showerror(self.translate("Error"), self.translate("An error occured", " :", "\n", error, "\n\n")) - - - def translate(self, *texts, lang=None): - if lang is None: - lang = self.stringvar_language.get() - elif lang == "F": - lang = "fr" - elif lang == "G": - lang = "ge" - elif lang == "I": - lang = "it" - elif lang == "S": - lang = "sp" - - if lang in translation_dict: - _lang_trad = translation_dict[lang] - translated_text = "" - for text in texts: - if text in _lang_trad: - translated_text += _lang_trad[text] - else: - translated_text += text - return translated_text - - return "".join(texts) # if no translation language is found - - - def progress(self, show=None, indeter=None, step=None, statut=None, max=None, add=None): - if indeter is True: - self.progressbar.config(mode="indeterminate") - self.progressbar.start(50) - elif indeter is False: - self.progressbar.config(mode="determinate") - self.progressbar.stop() - if show is True: - self.state_button(enable=False) - self.progressbar.grid(row=100, column=1, sticky="NEWS") - self.progresslabel.grid(row=101, column=1, sticky="NEWS") - elif show is False: - self.state_button(enable=True) - self.progressbar.grid_forget() - self.progresslabel.grid_forget() - - if statut: self.progresslabel.config(text=statut) - if step: self.progressbar["value"] = step - if max: - self.progressbar["maximum"] = max - self.progressbar["value"] = 0 - if add: self.progressbar.step(add) - - - def state_button(self, enable=True): - button = [ - self.button_game_extract, - self.button_install_mod, - self.button_prepare_file, - self.button_do_everything - ] - for widget in button: - if enable: - widget.config(state=NORMAL) - else: - widget.config(state=DISABLED) + from .check_update import check_update + from .log_error import log_error + from .progress import progress + from .restart import restart + from .state_button import state_button + from .translate import translate diff --git a/source/Gui/check_update.py b/source/Gui/check_update.py new file mode 100644 index 0000000..2c3cbd6 --- /dev/null +++ b/source/Gui/check_update.py @@ -0,0 +1,52 @@ +from tkinter import messagebox +import requests +import zipfile +import json +import os + +from ..definition import * + + +def check_update(self): + try: + gitversion = requests.get(VERSION_FILE_URL, allow_redirects=True).json() + with open("./version", "rb") as f: + locversion = json.load(f) + + if ((float(gitversion["version"]) > float(locversion["version"])) or # if github version is newer than + (float(gitversion["version"]) == float(locversion["version"])) and # local version + float(gitversion["subversion"]) > float(locversion["subversion"])): + if messagebox.askyesno( + self.translate("Update available !"), + self.translate("An update is available, do you want to install it ?", + f"\n\nVersion : {locversion['version']}.{locversion['subversion']} -> " + f"{gitversion['version']}.{gitversion['subversion']}\n" + f"Changelog :\n{gitversion['changelog']}")): + + if not (os.path.exists("./Updater/Updater.exe")): + dl = requests.get(gitversion["updater_bin"], allow_redirects=True) + with open("./download.zip", "wb") as file: + print(self.translate("Downloading the Updater...")) + file.write(dl.content) + print(self.translate("end of the download, extracting...")) + + with zipfile.ZipFile("./download.zip") as file: + file.extractall("./Updater/") + print(self.translate("finished extracting")) + + os.remove("./download.zip") + print(self.translate("starting application...")) + os.startfile(os.path.realpath("./Updater/Updater.exe")) + + if ((float(gitversion["version"]) < float(locversion["version"])) or # if local version is newer than + (float(gitversion["version"]) == float(locversion["version"])) and # github version + float(gitversion["subversion"]) < float(locversion["subversion"])): + self.is_dev_version = True + + except requests.ConnectionError: + messagebox.showwarning(self.translate("Warning"), + self.translate("Can't connect to internet. Download will be disabled.")) + self.option.disable_download = True + + except: + self.log_error() \ No newline at end of file diff --git a/source/Gui/log_error.py b/source/Gui/log_error.py new file mode 100644 index 0000000..710b466 --- /dev/null +++ b/source/Gui/log_error.py @@ -0,0 +1,9 @@ +from tkinter import messagebox +import traceback + + +def log_error(self): + error = traceback.format_exc() + with open("./error.log", "a") as f: + f.write(f"---\n{error}\n") + messagebox.showerror(self.translate("Error"), self.translate("An error occured", " :", "\n", error, "\n\n")) \ No newline at end of file diff --git a/source/Gui/progress.py b/source/Gui/progress.py new file mode 100644 index 0000000..bf9198f --- /dev/null +++ b/source/Gui/progress.py @@ -0,0 +1,22 @@ +def progress(self, show=None, indeter=None, step=None, statut=None, max=None, add=None): + if indeter is True: + self.progressbar.config(mode="indeterminate") + self.progressbar.start(50) + elif indeter is False: + self.progressbar.config(mode="determinate") + self.progressbar.stop() + if show is True: + self.state_button(enable=False) + self.progressbar.grid(row=100, column=1, sticky="NEWS") + self.progresslabel.grid(row=101, column=1, sticky="NEWS") + elif show is False: + self.state_button(enable=True) + self.progressbar.grid_forget() + self.progresslabel.grid_forget() + + if statut: self.progresslabel.config(text=statut) + if step: self.progressbar["value"] = step + if max: + self.progressbar["maximum"] = max + self.progressbar["value"] = 0 + if add: self.progressbar.step(add) \ No newline at end of file diff --git a/source/Gui/restart.py b/source/Gui/restart.py new file mode 100644 index 0000000..85bbc08 --- /dev/null +++ b/source/Gui/restart.py @@ -0,0 +1,10 @@ +import subprocess +import sys +import os + +from source.definition import * + + +def restart(): + subprocess.Popen([sys.executable] + sys.argv, creationflags=CREATE_NO_WINDOW, cwd=os.getcwd()) + exit() diff --git a/source/Gui/state_button.py b/source/Gui/state_button.py new file mode 100644 index 0000000..c30e725 --- /dev/null +++ b/source/Gui/state_button.py @@ -0,0 +1,15 @@ +from tkinter import * + + +def state_button(self, enable=True): + button = [ + self.button_game_extract, + self.button_install_mod, + self.button_prepare_file, + self.button_do_everything + ] + for widget in button: + if enable: + widget.config(state=NORMAL) + else: + widget.config(state=DISABLED) diff --git a/source/Gui/translate.py b/source/Gui/translate.py new file mode 100644 index 0000000..056bed9 --- /dev/null +++ b/source/Gui/translate.py @@ -0,0 +1,29 @@ +import json + +with open("./translation.json", encoding="utf-8") as f: + translation_dict = json.load(f) + + +def translate(self, *texts, lang=None): + if lang is None: + lang = self.stringvar_language.get() + elif lang == "F": + lang = "fr" + elif lang == "G": + lang = "ge" + elif lang == "I": + lang = "it" + elif lang == "S": + lang = "sp" + + if lang in translation_dict: + _lang_trad = translation_dict[lang] + translated_text = "" + for text in texts: + if text in _lang_trad: + translated_text += _lang_trad[text] + else: + translated_text += text + return translated_text + + return "".join(texts) # if no translation language is found diff --git a/source/Option.py b/source/Option.py deleted file mode 100644 index 1eef262..0000000 --- a/source/Option.py +++ /dev/null @@ -1,34 +0,0 @@ -import json -import os - - -class Option: - def __init__(self): - self.language = "en" - self.format = "FST" - self.disable_download = False - self.del_track_after_conv = False - self.dont_check_for_update = False - self.dont_check_track_sha1 = False - self.process_track = 8 - - def load_from_json(self, option_json: dict): - for key, value in option_json.items(): # load all value in the json as class attribute - setattr(self, key, value) - - def load_from_file(self, option_file: str = "./option.json"): - if os.path.exists(option_file): - with open(option_file, encoding="utf-8") as file: - file_json = json.load(file) - self.load_from_json(file_json) - - def save_to_file(self, option_file: str = "./option.json"): - option_json: dict = self.__dict__ # this return all attribute of the class as a dict - with open(option_file, "w", encoding="utf-8") as file: - json.dump(option_json, file, ensure_ascii=False) - - def edit(self, option, value, need_restart=False, gui=None): - if type(value) in [str, int, bool]: setattr(self, option, value) - else: setattr(self, option, value.get()) - self.save_to_file() - if need_restart: gui.restart() diff --git a/source/Option/__init__.py b/source/Option/__init__.py new file mode 100644 index 0000000..20a99da --- /dev/null +++ b/source/Option/__init__.py @@ -0,0 +1,14 @@ +class Option: + def __init__(self): + self.language = "en" + self.format = "FST" + self.disable_download = False + self.del_track_after_conv = False + self.dont_check_for_update = False + self.dont_check_track_sha1 = False + self.process_track = 8 + + from .edit import edit + from .load_from_file import load_from_file + from .load_from_json import load_from_json + from .save_to_file import save_to_file diff --git a/source/Option/edit.py b/source/Option/edit.py new file mode 100644 index 0000000..bf1c57b --- /dev/null +++ b/source/Option/edit.py @@ -0,0 +1,7 @@ +def edit(self, option, value, need_restart=False, gui=None): + if type(value) in [str, int, bool]: + setattr(self, option, value) + else: + setattr(self, option, value.get()) + self.save_to_file() + if need_restart: gui.restart() \ No newline at end of file diff --git a/source/Option/load_from_file.py b/source/Option/load_from_file.py new file mode 100644 index 0000000..c862b74 --- /dev/null +++ b/source/Option/load_from_file.py @@ -0,0 +1,9 @@ +import json +import os + + +def load_from_file(self, option_file: str = "./option.json"): + if os.path.exists(option_file): + with open(option_file, encoding="utf-8") as file: + file_json = json.load(file) + self.load_from_json(file_json) diff --git a/source/Option/load_from_json.py b/source/Option/load_from_json.py new file mode 100644 index 0000000..abc8ae2 --- /dev/null +++ b/source/Option/load_from_json.py @@ -0,0 +1,3 @@ +def load_from_json(self, option_json: dict): + for key, value in option_json.items(): # load all value in the json as class attribute + setattr(self, key, value) \ No newline at end of file diff --git a/source/Option/save_to_file.py b/source/Option/save_to_file.py new file mode 100644 index 0000000..f3837e2 --- /dev/null +++ b/source/Option/save_to_file.py @@ -0,0 +1,7 @@ +import json + + +def save_to_file(self, option_file: str = "./option.json"): + option_json: dict = self.__dict__ # this return all attribute of the class as a dict + with open(option_file, "w", encoding="utf-8") as file: + json.dump(option_json, file, ensure_ascii=False) diff --git a/source/StateButton.py b/source/StateButton.py deleted file mode 100644 index e69de29..0000000 diff --git a/source/Track.py b/source/Track.py deleted file mode 100644 index 512a106..0000000 --- a/source/Track.py +++ /dev/null @@ -1,120 +0,0 @@ -from .definition import * -import source.wszst -import requests -import os - - -class Track: - def __init__(self, name: str = "_", file_wu8: str = None, file_szs: str = None, prefix: str = None, suffix: str = None, - author="Nintendo", special="T11", music="T11", new=True, sha1: str = None, since_version: str = None, - score: int = 0, warning: int = 0, note: str = "", track_wu8_dir: str = "./file/Track-WU8/", - track_szs_dir: str = "./file/Track/", *args, **kwargs): - - self.name = name # Track name - self.prefix = prefix # Prefix, often used for game or original console like Wii U, DS, ... - self.suffix = suffix # Suffix, often used for variety like Boost, Night, ... - self.author = author # Track author - self.sha1 = sha1 # Track sha1 from wszst SHA1 - self.special = special # Special slot of the track - self.music = music # Music of the track - self.new = new # Is the track new - self.since_version = since_version # Since which version is this track available - self.score = score # Track score between 1 and 3 stars - self.warning = warning # Track bug level (1 = minor, 2 = major) - self.note = note # Note about the track - self.file_wu8 = f"{track_wu8_dir}/{self.get_track_name()}.wu8" - self.file_szs = f"{track_szs_dir}/{self.get_track_name()}.szs" - - def __repr__(self): - return f"{self.get_track_name()} sha1={self.sha1} score={self.score}" - - def get_track_name(self): - prefix = self.prefix + " " if self.prefix else "" - suffix = self.suffix + " " if self.suffix else "" - - name = (prefix + self.name + suffix) - return name - - def load_from_json(self, track_json: dict): - for key, value in track_json.items(): # load all value in the json as class attribute - setattr(self, key, value) - - def get_track_formatted_name(self, highlight_track_from_version: str = None): - """ - :param highlight_track_from_version: if a specific version need to be highlighted. - :return: the name of the track with colored prefix, suffix - """ - hl_prefix = "" - hl_suffix = "" - prefix = "" - suffix = "" - star_text = "" - - if self.score: - if 0 < self.score <= 3: - star_text = "★" * self.score + "☆" * (3 - self.score) - star_text = trackname_color[star_text] + " " - - if self.since_version == highlight_track_from_version: - hl_prefix, hl_suffix = "\\\\c{blue1}", "\\\\c{off}" - - if self.prefix in trackname_color: - prefix = trackname_color[self.prefix] + " " - if self.suffix in trackname_color: - suffix = "(" + trackname_color[self.suffix] + ")" - - name = (star_text + prefix + hl_prefix + self.name + hl_suffix + suffix) - name = name.replace("_", " ") - return name - - def convert_wu8_to_szs(self): - return source.wszst.normalize(src_file=self.file_wu8, use_popen=True) - - def download_wu8(self): - returncode = 0 - - dl = requests.get(get_github_content_root(self) + self.file_wu8, allow_redirects=True, stream=True) - if os.path.exists(self.file_wu8): - if int(dl.headers['Content-Length']) == os.path.getsize(self.file_wu8): return 1 - else: returncode = 3 - - if dl.status_code == 200: # if page is found - with open(self.file_wu8, "wb") as file: - chunk_size = 4096 - for i, chunk in enumerate(dl.iter_content(chunk_size=chunk_size)): - file.write(chunk) - file.flush() - return returncode - else: - print(f"error {dl.status_code} {self.file_wu8}") - return -1 - - def check_sha1(self): - if source.wszst.sha1(self.file_wu8) == self.sha1: return 0 - else: return -1 - - def get_ctfile_track(self, race=False): - """ - :param race: is it a text used for Race_*.szs ? - :return: ctfile definition for the track - """ - ctfile_text = ( - f' T {self.music}; ' - f'{self.special}; ' - f'{"0x01" if self.new else "0x00"}; ' - ) - if not race: - ctfile_text += ( - f'"{self.get_track_name()}"; ' # track path - f'"{self.get_track_formatted_name()}"; ' # track text shown ig - f'"-"\n') # sha1, useless for now. - else: - ctfile_text += ( - f'"-"; ' # track path, not used in Race_*.szs, save a bit of space - f'"{self.get_track_formatted_name()}\\n{self.author}"; ' # only in race show author's name - f'"-"\n' # sha1, useless for now. - ) - - return ctfile_text - -# TODO: code download_wu8 \ No newline at end of file diff --git a/source/Track/__init__.py b/source/Track/__init__.py new file mode 100644 index 0000000..5477d2a --- /dev/null +++ b/source/Track/__init__.py @@ -0,0 +1,29 @@ +class Track: + def __init__(self, name: str = "_", file_wu8: str = None, file_szs: str = None, prefix: str = None, suffix: str = None, + author="Nintendo", special="T11", music="T11", new=True, sha1: str = None, since_version: str = None, + score: int = 0, warning: int = 0, note: str = "", track_wu8_dir: str = "./file/Track-WU8/", + track_szs_dir: str = "./file/Track/", *args, **kwargs): + + self.name = name # Track name + self.prefix = prefix # Prefix, often used for game or original console like Wii U, DS, ... + self.suffix = suffix # Suffix, often used for variety like Boost, Night, ... + self.author = author # Track author + self.sha1 = sha1 # Track sha1 from wszst SHA1 + self.special = special # Special slot of the track + self.music = music # Music of the track + self.new = new # Is the track new + self.since_version = since_version # Since which version is this track available + self.score = score # Track score between 1 and 3 stars + self.warning = warning # Track bug level (1 = minor, 2 = major) + self.note = note # Note about the track + self.file_wu8 = f"{track_wu8_dir}/{self.get_track_name()}.wu8" + self.file_szs = f"{track_szs_dir}/{self.get_track_name()}.szs" + + from .__repr__ import __repr__ + from .check_sha1 import check_sha1 + from .convert_wu8_to_szs import convert_wu8_to_szs + from .download_wu8 import download_wu8 + from .get_ctfile import get_ctfile + from .get_track_formatted_name import get_track_formatted_name + from .get_track_name import get_track_name + from .load_from_json import load_from_json diff --git a/source/Track/__repr__.py b/source/Track/__repr__.py new file mode 100644 index 0000000..5c4097e --- /dev/null +++ b/source/Track/__repr__.py @@ -0,0 +1,2 @@ +def __repr__(self): + return f"{self.get_track_name()} sha1={self.sha1} score={self.score}" diff --git a/source/Track/check_sha1.py b/source/Track/check_sha1.py new file mode 100644 index 0000000..e785d87 --- /dev/null +++ b/source/Track/check_sha1.py @@ -0,0 +1,8 @@ +import source.wszst + + +def check_sha1(self): + if source.wszst.sha1(self.file_wu8) == self.sha1: + return 0 + else: + return -1 diff --git a/source/Track/convert_wu8_to_szs.py b/source/Track/convert_wu8_to_szs.py new file mode 100644 index 0000000..e97ccce --- /dev/null +++ b/source/Track/convert_wu8_to_szs.py @@ -0,0 +1,5 @@ +import source.wszst + + +def convert_wu8_to_szs(self): + return source.wszst.normalize(src_file=self.file_wu8, use_popen=True) diff --git a/source/Track/download_wu8.py b/source/Track/download_wu8.py new file mode 100644 index 0000000..a5d05f3 --- /dev/null +++ b/source/Track/download_wu8.py @@ -0,0 +1,26 @@ +from ..definition import * + +import requests +import os + + +def download_wu8(self): + returncode = 0 + + dl = requests.get(get_github_content_root(self) + self.file_wu8, allow_redirects=True, stream=True) + if os.path.exists(self.file_wu8): + if int(dl.headers['Content-Length']) == os.path.getsize(self.file_wu8): + return 1 + else: + returncode = 3 + + if dl.status_code == 200: # if page is found + with open(self.file_wu8, "wb") as file: + chunk_size = 4096 + for i, chunk in enumerate(dl.iter_content(chunk_size=chunk_size)): + file.write(chunk) + file.flush() + return returncode + else: + print(f"error {dl.status_code} {self.file_wu8}") + return -1 diff --git a/source/Track/get_ctfile.py b/source/Track/get_ctfile.py new file mode 100644 index 0000000..55b7c85 --- /dev/null +++ b/source/Track/get_ctfile.py @@ -0,0 +1,23 @@ +def get_ctfile(self, race=False): + """ + :param race: is it a text used for Race_*.szs ? + :return: ctfile definition for the track + """ + ctfile_text = ( + f' T {self.music}; ' + f'{self.special}; ' + f'{"0x01" if self.new else "0x00"}; ' + ) + if not race: + ctfile_text += ( + f'"{self.get_track_name()}"; ' # track path + f'"{self.get_track_formatted_name()}"; ' # track text shown ig + f'"-"\n') # sha1, useless for now. + else: + ctfile_text += ( + f'"-"; ' # track path, not used in Race_*.szs, save a bit of space + f'"{self.get_track_formatted_name()}\\n{self.author}"; ' # only in race show author's name + f'"-"\n' # sha1, useless for now. + ) + + return ctfile_text diff --git a/source/Track/get_track_formatted_name.py b/source/Track/get_track_formatted_name.py new file mode 100644 index 0000000..7190040 --- /dev/null +++ b/source/Track/get_track_formatted_name.py @@ -0,0 +1,30 @@ +from ..definition import * + + +def get_track_formatted_name(self, highlight_track_from_version: str = None): + """ + :param highlight_track_from_version: if a specific version need to be highlighted. + :return: the name of the track with colored prefix, suffix + """ + hl_prefix = "" + hl_suffix = "" + prefix = "" + suffix = "" + star_text = "" + + if self.score: + if 0 < self.score <= 3: + star_text = "★" * self.score + "☆" * (3 - self.score) + star_text = trackname_color[star_text] + " " + + if self.since_version == highlight_track_from_version: + hl_prefix, hl_suffix = "\\\\c{blue1}", "\\\\c{off}" + + if self.prefix in trackname_color: + prefix = trackname_color[self.prefix] + " " + if self.suffix in trackname_color: + suffix = "(" + trackname_color[self.suffix] + ")" + + name = (star_text + prefix + hl_prefix + self.name + hl_suffix + suffix) + name = name.replace("_", " ") + return name diff --git a/source/Track/get_track_name.py b/source/Track/get_track_name.py new file mode 100644 index 0000000..ca55e29 --- /dev/null +++ b/source/Track/get_track_name.py @@ -0,0 +1,6 @@ +def get_track_name(self): + prefix = self.prefix + " " if self.prefix else "" + suffix = self.suffix + " " if self.suffix else "" + + name = (prefix + self.name + suffix) + return name diff --git a/source/Track/load_from_json.py b/source/Track/load_from_json.py new file mode 100644 index 0000000..05d46c6 --- /dev/null +++ b/source/Track/load_from_json.py @@ -0,0 +1,3 @@ +def load_from_json(self, track_json: dict): + for key, value in track_json.items(): # load all value in the json as class attribute + setattr(self, key, value) diff --git a/source/definition.py b/source/definition.py index 7da875f..e7edc0c 100644 --- a/source/definition.py +++ b/source/definition.py @@ -87,6 +87,12 @@ trackname_color = { "★☆☆!!": "\\\\c{YOR6}★☆☆\\\\c{off}", } +region_id_to_name = { + "J": "JAP", + "P": "PAL", + "K": "KO", + "E": "USA" +} def filecopy(src, dst): with open(src, "rb") as f1: