From 014e4fe259b496c9d4384228609f662be681f33e Mon Sep 17 00:00:00 2001 From: raphael60650 Date: Sun, 18 Jul 2021 14:46:33 +0200 Subject: [PATCH] fixed error and remerged directory into a single file, it is way easier to read, navigate, and code --- source/CT_Config.py | 179 ++++++++ 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 | 37 ++ source/Cup/__init__.py | 22 - source/Cup/get_ctfile_cup.py | 9 - source/Cup/load_from_json.py | 7 - source/Game.py | 503 +++++++++++++++++++++++ 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/__init__.py => Gui.py} | 136 +++++- 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 | 37 ++ 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/Track.py | 123 ++++++ 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/wszst.py | 5 +- 49 files changed, 1007 insertions(+), 1094 deletions(-) create mode 100644 source/CT_Config.py delete mode 100644 source/CT_Config/__init__.py delete mode 100644 source/CT_Config/add_ordered_cup.py delete mode 100644 source/CT_Config/add_unordered_track.py delete mode 100644 source/CT_Config/create_ctfile.py delete mode 100644 source/CT_Config/get_cticon.py delete mode 100644 source/CT_Config/load_ctconfig_file.py delete mode 100644 source/CT_Config/load_ctconfig_json.py delete mode 100644 source/CT_Config/search_tracks.py create mode 100644 source/Cup.py delete mode 100644 source/Cup/__init__.py delete mode 100644 source/Cup/get_ctfile_cup.py delete mode 100644 source/Cup/load_from_json.py create mode 100644 source/Game.py delete mode 100644 source/Game/__init__.py delete mode 100644 source/Game/convert_to.py delete mode 100644 source/Game/exception.py delete mode 100644 source/Game/extract.py delete mode 100644 source/Game/install_mod.py delete mode 100644 source/Game/patch_autoadd.py delete mode 100644 source/Game/patch_bmg.py delete mode 100644 source/Game/patch_file.py delete mode 100644 source/Game/patch_image.py delete mode 100644 source/Game/patch_img_desc.py delete mode 100644 source/Game/patch_track.py rename source/{Gui/__init__.py => Gui.py} (64%) delete mode 100644 source/Gui/check_update.py delete mode 100644 source/Gui/log_error.py delete mode 100644 source/Gui/progress.py delete mode 100644 source/Gui/restart.py delete mode 100644 source/Gui/state_button.py delete mode 100644 source/Gui/translate.py create mode 100644 source/Option.py delete mode 100644 source/Option/__init__.py delete mode 100644 source/Option/edit.py delete mode 100644 source/Option/load_from_file.py delete mode 100644 source/Option/load_from_json.py delete mode 100644 source/Option/save_to_file.py create mode 100644 source/Track.py delete mode 100644 source/Track/__init__.py delete mode 100644 source/Track/__repr__.py delete mode 100644 source/Track/check_sha1.py delete mode 100644 source/Track/convert_wu8_to_szs.py delete mode 100644 source/Track/download_wu8.py delete mode 100644 source/Track/get_ctfile.py delete mode 100644 source/Track/get_track_formatted_name.py delete mode 100644 source/Track/get_track_name.py delete mode 100644 source/Track/load_from_json.py diff --git a/source/CT_Config.py b/source/CT_Config.py new file mode 100644 index 0000000..b64064e --- /dev/null +++ b/source/CT_Config.py @@ -0,0 +1,179 @@ +from PIL import Image, ImageDraw, ImageFont +import math +import json +import os + +from .Cup import Cup +from .Track import Track + + +def get_cup_icon(i: int, cup_id: str = "_", + font_path: str = "./file/SuperMario256.ttf", cup_icon_dir: str = "./file/cup_icon"): + """ + :param i: cup number + :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" % i, (0, 0, 0), font=font) + draw.text((5 + 2, 80 - 2), "%03i" % i, (0, 0, 0), font=font) + draw.text((5 - 2, 80 + 2), "%03i" % i, (0, 0, 0), font=font) + draw.text((5 + 2, 80 + 2), "%03i" % i, (0, 0, 0), font=font) + + draw.text((5, 80), "%03i" % i, (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 = {version} + + 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 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 = len(self.ordered_cups) + math.ceil(len(self.unordered_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, id) + ct_icon.paste(cup_icon, (0, i * CT_ICON_WIDTH)) + + return ct_icon + + 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 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 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/CT_Config/__init__.py b/source/CT_Config/__init__.py deleted file mode 100644 index 3f87714..0000000 --- a/source/CT_Config/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index 27a08b2..0000000 --- a/source/CT_Config/add_ordered_cup.py +++ /dev/null @@ -1,12 +0,0 @@ -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 deleted file mode 100644 index 5f007b5..0000000 --- a/source/CT_Config/add_unordered_track.py +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index c981a44..0000000 --- a/source/CT_Config/create_ctfile.py +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index 3d16ff9..0000000 --- a/source/CT_Config/get_cticon.py +++ /dev/null @@ -1,55 +0,0 @@ -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 deleted file mode 100644 index 0c57c21..0000000 --- a/source/CT_Config/load_ctconfig_file.py +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index c2649a2..0000000 --- a/source/CT_Config/load_ctconfig_json.py +++ /dev/null @@ -1,32 +0,0 @@ -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 deleted file mode 100644 index 966cbcb..0000000 --- a/source/CT_Config/search_tracks.py +++ /dev/null @@ -1,19 +0,0 @@ -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 new file mode 100644 index 0000000..fe2eb8a --- /dev/null +++ b/source/Cup.py @@ -0,0 +1,37 @@ +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 + + 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(race) + return ctfile_cup + + 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) diff --git a/source/Cup/__init__.py b/source/Cup/__init__.py deleted file mode 100644 index 554e5cc..0000000 --- a/source/Cup/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -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 deleted file mode 100644 index d134804..0000000 --- a/source/Cup/get_ctfile_cup.py +++ /dev/null @@ -1,9 +0,0 @@ -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(race) - return ctfile_cup diff --git a/source/Cup/load_from_json.py b/source/Cup/load_from_json.py deleted file mode 100644 index b0a06cd..0000000 --- a/source/Cup/load_from_json.py +++ /dev/null @@ -1,7 +0,0 @@ -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 new file mode 100644 index 0000000..c8a6345 --- /dev/null +++ b/source/Game.py @@ -0,0 +1,503 @@ +from tkinter import messagebox +from threading import Thread +from PIL import Image +import subprocess +import shutil +import glob +import json +import os + +from .definition import * +from . import wszst + + +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.") + + +class Game: + def __init__(self, path: str, region_ID: str = "P", game_ID: str = "RMCP01", gui=None): + 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 + self.gui = gui + + def convert_to(self, format: str = "FST"): + """ + :param format: game format (ISO, WBFS, ...) + :return: converted game path + """ + if format in ["ISO", "WBFS", "CISO"]: + path_game_format: str = os.path.realpath(self.path + "/../MKWFaraphel." + format.lower()) + subprocess.run(["./tools/wit/wit", "COPY", get_nodir(self.path), "--DEST", + get_nodir(path_game_format), f"--{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 + + self.gui.progress(statut=self.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 {self.gui.ctconfig.version}", "--modify", "ALL"], + creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path), + check=True, stdout=subprocess.PIPE) + + 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 + + def install_mod(self): + 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 + self.gui.progress(show=True, indeter=False, statut=self.gui.translate("Installing mod"), max=max_step, + step=0) + + def replace_file(path, file, subpath="/"): + self.gui.progress(statut=self.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: + self.gui.progress(statut=self.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") + + self.gui.progress(statut=self.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) + + self.gui.progress(statut=self.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 = self.gui.stringvar_game_format.get() + self.gui.progress(statut=self.gui.translate("Converting to", " ", output_format), add=1) + self.convert_to(output_format) + + messagebox.showinfo(self.gui.translate("End"), self.gui.translate("The mod has been installed !")) + + except: + self.gui.log_error() + finally: + self.gui.progress(show=False) + + t = Thread(target=func) + t.setDaemon(True) + t.start() + return t + + 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 patch_bmg(self, gamefile: str): # gamefile est le fichier .szs trouvé dans le /files/Scene/UI/ du jeu + NINTENDO_CWF_REPLACE = "Wiimmfi" + MAINMENU_REPLACE = f"MKWFaraphel {self.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 + self.gui.progress(statut=self.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}}{self.gui.translate('Random: All tracks', lang=bmglang)}\n" + f" 703f\t= \\\\c{{white}}{self.gui.translate('Random: Original tracks', lang=bmglang)}\n" + f" 7040\t= \\\\c{{white}}{self.gui.translate('Random: Custom Tracks', lang=bmglang)}\n" + f" 7041\t= \\\\c{{white}}{self.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_file(self): + 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(self.gui.ctconfig.all_tracks) + 3 + len("EGFIS") + + self.gui.progress(show=True, indeter=False, statut=self.gui.translate("Converting files"), max=max_step, + step=0) + self.gui.progress(statut=self.gui.translate("Configurating LE-CODE"), add=1) + self.gui.ctconfig.create_ctfile() + + self.gui.progress(statut=self.gui.translate("Creating ct_icon.png"), add=1) + ct_icon = self.gui.ctconfig.get_cticon() + ct_icon.save("./file/ct_icons.tpl.png") + + self.gui.progress(statut=self.gui.translate("Creating descriptive images"), add=1) + self.patch_img_desc() + self.patch_image(fc) + for file in glob.glob(self.path + "/files/Scene/UI/MenuSingle_?.szs"): self.patch_bmg(file) + # MenuSingle could be any other file, Common and Menu are all the same in all other files. + self.patch_autoadd() + if self.patch_tracks() != 0: return + + self.gui.button_install_mod.grid(row=2, column=1, columnspan=2, sticky="NEWS") + self.gui.button_install_mod.config( + text=self.gui.translate("Install mod", " (v", self.gui.ctconfig.version, ")")) + + except: + self.gui.log_error() + finally: + self.gui.progress(show=False) + + t = Thread(target=func) + t.setDaemon(True) + t.start() + return t + + def patch_image(self, fc): + for i, file in enumerate(fc["img"]): + self.gui.progress(statut=self.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_img_desc(self, 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_tracks(self): + max_process = self.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(self.gui.ctconfig.all_tracks) + + process_list[track_file] = None # Used for showing track in progress even if there's no process + self.gui.progress(statut=self.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 self.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 self.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 self.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 self.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(self.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/Game/__init__.py b/source/Game/__init__.py deleted file mode 100644 index 22f3960..0000000 --- a/source/Game/__init__.py +++ /dev/null @@ -1,24 +0,0 @@ -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 deleted file mode 100644 index 9e53651..0000000 --- a/source/Game/convert_to.py +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 6136281..0000000 --- a/source/Game/exception.py +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index ec1df11..0000000 --- a/source/Game/extract.py +++ /dev/null @@ -1,39 +0,0 @@ -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 deleted file mode 100644 index e02fec6..0000000 --- a/source/Game/install_mod.py +++ /dev/null @@ -1,136 +0,0 @@ -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 deleted file mode 100644 index d95a5a6..0000000 --- a/source/Game/patch_autoadd.py +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index 8c8cd37..0000000 --- a/source/Game/patch_bmg.py +++ /dev/null @@ -1,99 +0,0 @@ -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 deleted file mode 100644 index bf977a3..0000000 --- a/source/Game/patch_file.py +++ /dev/null @@ -1,48 +0,0 @@ -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 deleted file mode 100644 index 03ab79d..0000000 --- a/source/Game/patch_image.py +++ /dev/null @@ -1,10 +0,0 @@ -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 deleted file mode 100644 index fb1ec35..0000000 --- a/source/Game/patch_img_desc.py +++ /dev/null @@ -1,25 +0,0 @@ -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 deleted file mode 100644 index d1608a6..0000000 --- a/source/Game/patch_track.py +++ /dev/null @@ -1,123 +0,0 @@ -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/__init__.py b/source/Gui.py similarity index 64% rename from source/Gui/__init__.py rename to source/Gui.py index 46936e3..446f010 100644 --- a/source/Gui/__init__.py +++ b/source/Gui.py @@ -1,12 +1,21 @@ -from tkinter import messagebox, filedialog, ttk -from threading import Thread +from tkinter import filedialog, ttk from tkinter import * -import os +import zipfile +import traceback +import requests from source.CT_Config import CT_Config -from source.Game.exception import * from source.Option import Option -from source.Game import Game +from source.Game import * + + +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: @@ -104,7 +113,7 @@ class Gui: def func(): self.frame_action.grid_forget() try: - self.game = Game(path = entry_game_path.get()) + self.game = Game(path=entry_game_path.get(), gui=self) self.progress(show=True, indeter=True, statut=self.translate("Extracting the game...")) self.game.extract() self.frame_action.grid(row=3, column=1, sticky="NEWS") @@ -152,9 +161,112 @@ class Gui: self.progressbar = ttk.Progressbar(self.root) self.progresslabel = Label(self.root) - 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 + 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(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")) + + 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) + + 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/Gui/check_update.py b/source/Gui/check_update.py deleted file mode 100644 index 2c3cbd6..0000000 --- a/source/Gui/check_update.py +++ /dev/null @@ -1,52 +0,0 @@ -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 deleted file mode 100644 index 710b466..0000000 --- a/source/Gui/log_error.py +++ /dev/null @@ -1,9 +0,0 @@ -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 deleted file mode 100644 index bf9198f..0000000 --- a/source/Gui/progress.py +++ /dev/null @@ -1,22 +0,0 @@ -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 deleted file mode 100644 index 85bbc08..0000000 --- a/source/Gui/restart.py +++ /dev/null @@ -1,10 +0,0 @@ -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 deleted file mode 100644 index c30e725..0000000 --- a/source/Gui/state_button.py +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index 056bed9..0000000 --- a/source/Gui/translate.py +++ /dev/null @@ -1,29 +0,0 @@ -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 new file mode 100644 index 0000000..867ddf8 --- /dev/null +++ b/source/Option.py @@ -0,0 +1,37 @@ +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 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() + + 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 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 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/Option/__init__.py b/source/Option/__init__.py deleted file mode 100644 index 20a99da..0000000 --- a/source/Option/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -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 deleted file mode 100644 index bf1c57b..0000000 --- a/source/Option/edit.py +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index c862b74..0000000 --- a/source/Option/load_from_file.py +++ /dev/null @@ -1,9 +0,0 @@ -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 deleted file mode 100644 index abc8ae2..0000000 --- a/source/Option/load_from_json.py +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index f3837e2..0000000 --- a/source/Option/save_to_file.py +++ /dev/null @@ -1,7 +0,0 @@ -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/Track.py b/source/Track.py new file mode 100644 index 0000000..b86d309 --- /dev/null +++ b/source/Track.py @@ -0,0 +1,123 @@ +import requests +import os + +from .definition import * +from . import wszst + + +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 check_sha1(self): + if wszst.sha1(self.file_wu8) == self.sha1: + return 0 + else: + return -1 + + def convert_wu8_to_szs(self): + return 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 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 + + 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 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) diff --git a/source/Track/__init__.py b/source/Track/__init__.py deleted file mode 100644 index 5477d2a..0000000 --- a/source/Track/__init__.py +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index 5c4097e..0000000 --- a/source/Track/__repr__.py +++ /dev/null @@ -1,2 +0,0 @@ -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 deleted file mode 100644 index e785d87..0000000 --- a/source/Track/check_sha1.py +++ /dev/null @@ -1,8 +0,0 @@ -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 deleted file mode 100644 index e97ccce..0000000 --- a/source/Track/convert_wu8_to_szs.py +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index a5d05f3..0000000 --- a/source/Track/download_wu8.py +++ /dev/null @@ -1,26 +0,0 @@ -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 deleted file mode 100644 index 55b7c85..0000000 --- a/source/Track/get_ctfile.py +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index 7190040..0000000 --- a/source/Track/get_track_formatted_name.py +++ /dev/null @@ -1,30 +0,0 @@ -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 deleted file mode 100644 index ca55e29..0000000 --- a/source/Track/get_track_name.py +++ /dev/null @@ -1,6 +0,0 @@ -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 deleted file mode 100644 index 05d46c6..0000000 --- a/source/Track/load_from_json.py +++ /dev/null @@ -1,3 +0,0 @@ -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/wszst.py b/source/wszst.py index 6f47326..249a664 100644 --- a/source/wszst.py +++ b/source/wszst.py @@ -70,4 +70,7 @@ def bmg_decode(): pass def bmg_encode(): pass -def wctct_bmg(): pass \ No newline at end of file +def wctct_bmg(): pass + + +def wit_copy(): pass \ No newline at end of file