Splitted wszst.py into a module, removed "utf-8 support for path" with cwd because wiimm's tools already support many non-ascii caracter, this simplify the code.

This commit is contained in:
raphael60650 2021-07-26 22:57:45 +02:00
parent fc3030532b
commit 738e0b101b
14 changed files with 198 additions and 197 deletions

View file

@ -41,7 +41,6 @@ class CT_Config:
"""
add a cup to the config
:param cup: a Cup object to add as an ordered cup
:return: ?
"""
self.ordered_cups.append(cup)
for track in cup.tracks:
@ -52,7 +51,6 @@ class CT_Config:
"""
add a single track to the config
:param track: a Track object to add as an unordered tracks
:return: ?
"""
self.unordered_tracks.append(track)
self.all_version.add(track.since_version)
@ -63,7 +61,6 @@ class CT_Config:
create a ctfile configuration in a directory
:param highlight_version: highlight a specific version in light blue
: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:

View file

@ -75,12 +75,12 @@ class Game:
"""
if format in ["ISO", "WBFS", "CISO"]:
path_game_format: str = os.path.realpath(self.path + "/../MKWFaraphel." + format.lower())
wszst.wit_copy(self.path, path_game_format, format)
wszst.wit.copy(self.path, path_game_format, format)
shutil.rmtree(self.path)
self.path = path_game_format
self.gui.progress(statut=self.gui.translate("Changing game's ID"), add=1)
wszst.edit(self.path, region_ID=self.region_ID, name=f"Mario Kart Wii Faraphel {self.ctconfig.version}")
wszst.wit.edit(self.path, region_ID=self.region_ID, name=f"Mario Kart Wii Faraphel {self.ctconfig.version}")
def extract(self) -> None:
"""
@ -97,7 +97,7 @@ class Game:
if not (os.path.exists(path_dir)): break
directory_name, i = f"MKWiiFaraphel ({i})", i + 1
wszst.wit_extract(self.path, path_dir)
wszst.wit.extract(self.path, path_dir)
self.path = path_dir
if os.path.exists(self.path + "/DATA"): self.path += "/DATA"
@ -166,7 +166,7 @@ class Game:
if extension == "szs":
if not (os.path.realpath(path) in extracted_file):
wszst.szs_extract(path, get_nodir(path))
wszst.szs.extract(path, get_nodir(path))
extracted_file.append(os.path.realpath(path))
szs_extract_path = path + ".d"
@ -194,12 +194,12 @@ class Game:
for file in extracted_file:
self.gui.progress(statut=self.gui.translate("Recompilating", "\n", get_nodir(file)), add=1)
wszst.create(file)
wszst.szs.create(file)
if os.path.exists(file + ".d"):
shutil.rmtree(file + ".d")
self.gui.progress(statut=self.gui.translate("Patch main.dol"), add=1)
wszst.str_patch(self.path)
wszst.wstrt.patch(self.path)
self.gui.progress(statut=self.gui.translate("Patch lecode.bin"), add=1)
@ -209,7 +209,7 @@ class Game:
shutil.copyfile("./file/lpar-default.txt", self.path + "/tmp/lpar-default.txt")
shutil.copyfile(f"./file/lecode-{self.region}.bin", self.path + f"/tmp/lecode-{self.region}.bin")
wszst.lec_patch(
wszst.lec.patch(
self.path,
lecode_file=f"./tmp/lecode-{self.region}.bin",
dest_lecode_file=f"./files/rel/lecode-{self.region}.bin",
@ -235,7 +235,7 @@ class Game:
"""
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/")
wszst.autoadd(self.path, get_nodir(self.path) + "/tmp/auto-add/")
wszst.szs.autoadd(self.path, get_nodir(self.path) + "/tmp/auto-add/")
shutil.move(self.path + "/tmp/auto-add/", auto_add_dir)
shutil.rmtree(self.path + "/tmp/")
@ -265,10 +265,10 @@ class Game:
bmglang = gamefile[-len("E.txt"):-len(".txt")] # Langue du fichier
self.gui.progress(statut=self.gui.translate("Patching text", " ", bmglang), add=1)
wszst.szs_extract(gamefile, get_nodir(gamefile))
wszst.szs.extract(gamefile, get_nodir(gamefile))
bmgmenu = wszst.bmg_cat(gamefile, ".d/message/Menu.bmg") # Menu.bmg
bmgtracks = wszst.bmg_cat(gamefile, ".d/message/Common.bmg") # Common.bmg
bmgmenu = wszst.bmg.cat(gamefile, ".d/message/Menu.bmg") # Menu.bmg
bmgtracks = wszst.bmg.cat(gamefile, ".d/message/Common.bmg") # Common.bmg
trackheader = "#--- standard track names"
trackend = "2328"
@ -306,9 +306,9 @@ class Game:
if not os.path.exists("./file/tmp/"): os.makedirs("./file/tmp/")
shutil.copyfile(gamefile + ".d/message/Common.bmg", "./file/tmp/Common.bmg")
bmgcommon = wszst.ctc_patch_bmg(ctfile="./file/CTFILE.txt",
bmgcommon = wszst.ctc.patch_bmg(ctfile="./file/CTFILE.txt",
bmgs=["./file/tmp/Common.bmg", "./file/ExtraCommon.txt"])
rbmgcommon = wszst.ctc_patch_bmg(ctfile="./file/RCTFILE.txt",
rbmgcommon = wszst.ctc.patch_bmg(ctfile="./file/RCTFILE.txt",
bmgs=["./file/tmp/Common.bmg", "./file/ExtraCommon.txt"])
shutil.rmtree(gamefile + ".d")
@ -320,7 +320,7 @@ class Game:
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)
wszst.bmg_encode(file)
wszst.bmg.encode(file)
os.remove(file)
finalise(f"./file/Menu_{bmglang}.txt", bmgmenu, menu_replacement)
@ -372,7 +372,7 @@ class Game:
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)
wszst.img_encode("./file/" + file, fc["img"][file])
wszst.img.encode("./file/" + file, fc["img"][file])
def patch_img_desc(self, img_desc_path: str = "./file/img_desc", dest_dir: str = "./file") -> None:
"""

View file

@ -14,7 +14,10 @@ with open("./translation.json", encoding="utf-8") as f:
def restart():
subprocess.Popen([sys.executable] + sys.argv, creationflags=CREATE_NO_WINDOW, cwd=os.getcwd())
"""
restart the application
"""
subprocess.Popen([sys.executable] + sys.argv, creationflags=subprocess.CREATE_NO_WINDOW, cwd=os.getcwd())
exit()

View file

@ -61,15 +61,14 @@ class Track:
check if track wu8's sha1 is correct
:return: 0 if yes, -1 if no
"""
ws = wszst.sha1(self.file_wu8)
if wszst.sha1(self.file_wu8) == self.sha1: return 0
if wszst.szs.sha1(self.file_wu8) == self.sha1: return 0
else: return -1
def convert_wu8_to_szs(self) -> None:
"""
convert track to szs
"""
wszst.normalize(src_file=self.file_wu8)
wszst.szs.normalize(src_file=self.file_wu8)
def download_wu8(self, github_content_root: str) -> int:
"""

View file

@ -1,6 +1,5 @@
from threading import Thread
CREATE_NO_WINDOW = 0x08000000
GITHUB_REPOSITORY = "Faraphel/MKWF-Install"
GITHUB_MASTER_BRANCH = f"https://raw.githubusercontent.com/{GITHUB_REPOSITORY}/master/"
GITHUB_DEV_BRANCH = f"https://raw.githubusercontent.com/{GITHUB_REPOSITORY}/dev/"

View file

@ -1,174 +0,0 @@
from .definition import *
import subprocess
def sha1(file, autoadd_path: str = "./file/auto-add/") -> str:
"""
:param autoadd_path: directory where is autoadd directory
:param file: track file to check sha1
:return: track's sha1
"""
return subprocess.run(["./tools/szs/wszst", "SHA1", file, "--autoadd-path", autoadd_path],
check=True, creationflags=CREATE_NO_WINDOW,
stdout=subprocess.PIPE).stdout.decode().split(" ")[0]
def normalize(src_file: str, dest_dir: str = "./file/Track/", dest_name: str = "%N.szs",
output_format: str = "szs", autoadd_path: str = "./file/auto-add/") -> None:
"""
convert a track into an another format
:param src_file: source file
:param dest_dir: destination directory
:param dest_name: destination filename (%N mean same name as src_file)
:param output_format: format of the destination track
:param autoadd_path: path of the auto-add directory
"""
subprocess.run(["./tools/szs/wszst", "NORMALIZE", src_file, "--DEST",
dest_dir+dest_name, "--"+output_format, "--overwrite", "--autoadd-path",
autoadd_path], creationflags=CREATE_NO_WINDOW, stderr=subprocess.PIPE)
def wit_extract(file: str, dest_dir: str) -> None:
"""
extract the game into a directory
:param file: game's file to extract (can be WBFS, ISO, CISO)
:param dest_dir: where to extract the game
"""
subprocess.run(["./tools/wit/wit", "EXTRACT", get_nodir(file), "--DEST", dest_dir],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(file))
def create(file: str) -> None:
"""
convert a directory into a szs file
:param file: create a .szs file from the directory {file}.d
"""
subprocess.run(["./tools/szs/wszst", "CREATE", get_nodir(file) + ".d", "-d", get_nodir(file),
"--overwrite"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(file),
check=True, stdout=subprocess.PIPE)
def str_patch(path: str) -> None:
"""
Patch the main.dol file
:param path: path to the game
"""
subprocess.run(["./tools/szs/wstrt", "patch", get_nodir(path) + "/sys/main.dol", "--clean-dol",
"--add-lecode"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(path),
check=True, stdout=subprocess.PIPE)
def lec_patch(path: str,
lecode_file: str = f"./tmp/lecode-PAL.bin",
dest_lecode_file: str = f"./files/rel/lecode-PAL.bin",
game_track_path: str = "./files/Race/Course/",
move_track_path: str = "./files/Race/Course/",
ctfile_path: str = "./tmp/CTFILE.txt",
lpar_path: str = "./tmp/lpar-default.txt") -> None:
"""
Patch the file with a lecode file (this is the adding track part)
:param path: path to the game file
:param lecode_file: path to the lecode file
:param dest_lecode_file: destination of the lecode file
:param game_track_path: subpath to the track directory
:param move_track_path: where are stored the track to move
:param ctfile_path: where is the ctfile (track and cup definition)
:param lpar_path: where is the lpar_path (game modification like speed, speedometer, ...)
"""
subprocess.run(
["./tools/szs/wlect", "patch", lecode_file, "-od",
dest_lecode_file, "--track-dir", game_track_path,
"--move-tracks", move_track_path, "--le-define", ctfile_path, "--lpar",
lpar_path, "--overwrite"], creationflags=CREATE_NO_WINDOW, cwd=path, check=True, stdout=subprocess.PIPE)
def edit(file: str, region_ID: str = "P", name: str = "Mario Kart Wii") -> None:
"""
Edit game property like region or name
:param file: game's file
:param region_ID: new region_ID
:param name: new name
"""
subprocess.run(["./tools/wit/wit", "EDIT", get_nodir(file), "--id",
f"RMC{region_ID}60", "--name", name, "--modify", "ALL"],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(file),
check=True, stdout=subprocess.PIPE)
def autoadd(file: str, dest_dir: str) -> None:
"""
Create an auto_add directory from a game file
:param file: the game's path
:param dest_dir: directory where to store autoadd file
"""
subprocess.run(["./tools/szs/wszst", "AUTOADD", get_nodir(file) + "/files/Race/Course/", "--DEST", dest_dir],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(file),
check=True, stdout=subprocess.PIPE)
def bmg_encode(file: str) -> None:
"""
Encode a txt file into a bmg file
:param file: txt file to convert
"""
subprocess.run(["./tools/szs/wbmgt", "ENCODE", get_nodir(file), "--overwrite"],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(file))
def bmg_cat(path: str, subfile: str = ".d/message/Common.bmg") -> str:
"""
read a bmg file
:param path: path to a szs file
:param subfile: path to a subdirectory
:return: bmg definition
"""
return subprocess.run(["./tools/szs/wbmgt", "CAT", get_nodir(path) + subfile],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(path),
check=True, stdout=subprocess.PIPE).stdout.decode()
def wit_copy(src_path, dst_path, format: str = "ISO") -> None:
"""
Copy the game into an another format
:param src_path: original game path
:param dst_path: new game path
:param format: format for the new game
"""
subprocess.run(["./tools/wit/wit", "COPY", get_nodir(src_path), "--DEST",
get_nodir(dst_path), f"--{format.lower()}", "--overwrite"],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(dst_path),
check=True, stdout=subprocess.PIPE)
def ctc_patch_bmg(bmgs: list, ctfile: str = "./file/CTFILE.txt"):
"""
Patch a bmg file with a ctfile with OVERWRITE option
:param bmgs: all bmg files
:param ctfile: the ctfile path
:return: combined bmg
"""
bmg_cmd = []
for bmg in bmgs: bmg_cmd.extend(["--patch-bmg", f"OVERWRITE={bmg}"])
return subprocess.run(
["tools/szs/wctct", "bmg", "--le-code", "--long", ctfile, *bmg_cmd],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE).stdout.decode()
def img_encode(src_file: str, format: str) -> None:
"""
Encode an .png image into a new format
:param src_file: .png image
:param format: new image format
"""
subprocess.run(["./tools/szs/wimgt", "ENCODE", src_file, "-x", format, "--overwrite"],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)
def szs_extract(file: str, dest_dir: str) -> None:
"""
Extract an szs in a directory
:param file: .szs file
:param dest_dir: directory where to extract the file
"""
subprocess.run(["./tools/szs/wszst", "EXTRACT", get_nodir(file), "--DEST", dest_dir+".d"],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(file))

5
source/wszst/__init__.py Normal file
View file

@ -0,0 +1,5 @@
from ..definition import *
from subprocess import CREATE_NO_WINDOW
import subprocess
__all__ = ["bmg", "ctc", "img", "lec", "wstrt", "szs", "wit", "subprocess", "CREATE_NO_WINDOW"]

22
source/wszst/bmg.py Normal file
View file

@ -0,0 +1,22 @@
from . import *
def encode(file: str) -> None:
"""
Encode a txt file into a bmg file
:param file: txt file to convert
"""
subprocess.run(["./tools/szs/wbmgt", "ENCODE", file, "--overwrite"],
creationflags=CREATE_NO_WINDOW)
def cat(path: str, subfile: str = ".d/message/Common.bmg") -> str:
"""
read a bmg file
:param path: path to a szs file
:param subfile: path to a subdirectory
:return: bmg definition
"""
return subprocess.run(["./tools/szs/wbmgt", "CAT", path + subfile],
creationflags=CREATE_NO_WINDOW,
check=True, stdout=subprocess.PIPE).stdout.decode()

15
source/wszst/ctc.py Normal file
View file

@ -0,0 +1,15 @@
from . import *
def ctc_patch_bmg(bmgs: list, ctfile: str = "./file/CTFILE.txt"):
"""
Patch a bmg file with a ctfile with OVERWRITE option
:param bmgs: all bmg files
:param ctfile: the ctfile path
:return: combined bmg
"""
bmg_cmd = []
for bmg in bmgs: bmg_cmd.extend(["--patch-bmg", f"OVERWRITE={bmg}"])
return subprocess.run(
["tools/szs/wctct", "bmg", "--le-code", "--long", ctfile, *bmg_cmd],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE).stdout.decode()

11
source/wszst/img.py Normal file
View file

@ -0,0 +1,11 @@
from . import *
def encode(src_file: str, format: str) -> None:
"""
Encode an .png image into a new format
:param src_file: .png image
:param format: new image format
"""
subprocess.run(["./tools/szs/wimgt", "ENCODE", src_file, "-x", format, "--overwrite"],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)

24
source/wszst/lec.py Normal file
View file

@ -0,0 +1,24 @@
from . import *
def patch(path: str,
lecode_file: str = f"./tmp/lecode-PAL.bin",
dest_lecode_file: str = f"./files/rel/lecode-PAL.bin",
game_track_path: str = "./files/Race/Course/",
move_track_path: str = "./files/Race/Course/",
ctfile_path: str = "./tmp/CTFILE.txt",
lpar_path: str = "./tmp/lpar-default.txt") -> None:
"""
Patch the file with a lecode file (this is the adding track part)
:param path: path to the game file
:param lecode_file: path to the lecode file
:param dest_lecode_file: destination of the lecode file
:param game_track_path: subpath to the track directory
:param move_track_path: where are stored the track to move
:param ctfile_path: where is the ctfile (track and cup definition)
:param lpar_path: where is the lpar_path (game modification like speed, speedometer, ...)
"""
subprocess.run(
["./tools/szs/wlect", "patch", lecode_file, "-od", dest_lecode_file, "--track-dir", game_track_path,
"--move-tracks", move_track_path, "--le-define", ctfile_path, "--lpar", lpar_path, "--overwrite"],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)

56
source/wszst/szs.py Normal file
View file

@ -0,0 +1,56 @@
from . import *
def szs_extract(file: str, dest_dir: str) -> None:
"""
Extract an szs in a directory
:param file: .szs file
:param dest_dir: directory where to extract the file
"""
subprocess.run(["./tools/szs/wszst", "EXTRACT", file, "--DEST", dest_dir + ".d"],
creationflags=CREATE_NO_WINDOW)
def sha1(file, autoadd_path: str = "./file/auto-add/") -> str:
"""
:param autoadd_path: directory where is autoadd directory
:param file: track file to check sha1
:return: track's sha1
"""
return subprocess.run(["./tools/szs/wszst", "SHA1", file, "--autoadd-path", autoadd_path],
check=True, creationflags=CREATE_NO_WINDOW,
stdout=subprocess.PIPE).stdout.decode().split(" ")[0]
def normalize(src_file: str, dest_dir: str = "./file/Track/", dest_name: str = "%N.szs",
output_format: str = "szs", autoadd_path: str = "./file/auto-add/") -> None:
"""
convert a track into an another format
:param src_file: source file
:param dest_dir: destination directory
:param dest_name: destination filename (%N mean same name as src_file)
:param output_format: format of the destination track
:param autoadd_path: path of the auto-add directory
"""
subprocess.run(["./tools/szs/wszst", "NORMALIZE", src_file, "--DEST", dest_dir + dest_name, "--" + output_format,
"--overwrite", "--autoadd-path", autoadd_path],
creationflags=CREATE_NO_WINDOW, stderr=subprocess.PIPE)
def create(file: str) -> None:
"""
convert a directory into a szs file
:param file: create a .szs file from the directory {file}.d
"""
subprocess.run(["./tools/szs/wszst", "CREATE", file + ".d", "-d", file, "--overwrite"],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)
def autoadd(file: str, dest_dir: str) -> None:
"""
Create an auto_add directory from a game file
:param file: the game's path
:param dest_dir: directory where to store autoadd file
"""
subprocess.run(["./tools/szs/wszst", "AUTOADD", file + "/files/Race/Course/", "--DEST", dest_dir],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)

34
source/wszst/wit.py Normal file
View file

@ -0,0 +1,34 @@
from . import *
def extract(file: str, dest_dir: str) -> None:
"""
extract the game into a directory
:param file: game's file to extract (can be WBFS, ISO, CISO)
:param dest_dir: where to extract the game
"""
subprocess.run(["./tools/wit/wit", "EXTRACT", file, "--DEST", dest_dir],
creationflags=CREATE_NO_WINDOW)
def edit(file: str, region_ID: str = "P", name: str = "Mario Kart Wii") -> None:
"""
Edit game property like region or name
:param file: game's file
:param region_ID: new region_ID
:param name: new name
"""
subprocess.run(
["./tools/wit/wit", "EDIT", file, "--id", f"RMC{region_ID}60", "--name", name, "--modify", "ALL"],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)
def copy(src_path, dst_path, format: str = "ISO") -> None:
"""
Copy the game into an another format
:param src_path: original game path
:param dst_path: new game path
:param format: format for the new game
"""
subprocess.run(["./tools/wit/wit", "COPY", src_path, "--DEST", dst_path, f"--{format.lower()}", "--overwrite"],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)

10
source/wszst/wstrt.py Normal file
View file

@ -0,0 +1,10 @@
from . import *
def patch(path: str) -> None:
"""
Patch the main.dol file
:param path: path to the game
"""
subprocess.run(["./tools/szs/wstrt", "patch", path + "/sys/main.dol", "--clean-dol", "--add-lecode"],
creationflags=CREATE_NO_WINDOW, check=True, stdout=subprocess.PIPE)