Rewrote entire source to be based around argparse

This does change a large amount of the syntax for using the CLI, but I think that's for the better. This new system also allows for having help pages for each sub command, making the tool a lot easier to use. It also allows for having more arguments available for each subcommand, which is especially necessary for the ASH module.
This commit is contained in:
Campbell 2024-06-24 00:41:07 -04:00
parent dcba8672bc
commit 5e1bf6ed4e
Signed by: NinjaCheetah
GPG Key ID: 670C282B3291D63D
5 changed files with 175 additions and 169 deletions

View File

@ -1,23 +1,33 @@
# "ash.py" from WiiPy by NinjaCheetah # "ash.py" from WiiPy by NinjaCheetah
# https://github.com/NinjaCheetah/WiiPy # https://github.com/NinjaCheetah/WiiPy
import os import pathlib
import libWiiPy import libWiiPy
def decompress_ash(in_file: str, out_file: str = None): def handle_ash(args):
if not os.path.isfile(in_file): input_path = pathlib.Path(args.input)
raise FileNotFoundError(in_file) output_path = pathlib.Path(args.output)
ash_file = open(in_file, "rb") if args.compress:
ash_data = ash_file.read() print("Compression is not implemented yet.")
ash_file.close()
ash_decompressed = libWiiPy.archive.decompress_ash(ash_data) elif args.decompress:
sym_tree_bits = args.sym_bits
dist_tree_bits = args.dist_bits
if out_file is None: if not input_path.exists():
out_file = in_file + ".arc" raise FileNotFoundError(input_path)
ash_out = open(out_file, "wb") ash_file = open(input_path, "rb")
ash_out.write(ash_decompressed) ash_data = ash_file.read()
ash_out.close() ash_file.close()
ash_decompressed = libWiiPy.archive.decompress_ash(ash_data, sym_tree_bits=sym_tree_bits,
dist_tree_bits=dist_tree_bits)
ash_out = open(output_path, "wb")
ash_out.write(ash_decompressed)
ash_out.close()
print("ASH file decompressed!")

View File

@ -4,19 +4,21 @@
import libWiiPy import libWiiPy
def download_title(title_id: str, title_version_input: str = None): def handle_nus(args):
title_version = None title_version = None
if title_version_input is not None: if args.version is not None:
try: try:
title_version = int(title_version_input) title_version = int(args.version)
except ValueError: except ValueError:
print("Enter a valid integer for the Title Version.") print("Enter a valid integer for the Title Version.")
return return
title = libWiiPy.title.download_title(title_id, title_version) title = libWiiPy.title.download_title(args.tid, title_version)
file_name = title_id + "-v" + str(title.tmd.title_version) + ".wad" file_name = args.tid + "-v" + str(title.tmd.title_version) + ".wad"
wad_file = open(file_name, "wb") wad_file = open(file_name, "wb")
wad_file.write(title.dump_wad()) wad_file.write(title.dump_wad())
wad_file.close() wad_file.close()
print("Downloaded title with Title ID \"" + args.tid + "\"!")

View File

@ -1,29 +1,37 @@
# "u8.py" from WiiPy by NinjaCheetah # "u8.py" from WiiPy by NinjaCheetah
# https://github.com/NinjaCheetah/WiiPy # https://github.com/NinjaCheetah/WiiPy
import os import pathlib
import libWiiPy import libWiiPy
def extract_u8_to_folder(in_file: str, out_folder: str): def handle_u8(args):
if not os.path.isfile(in_file): input_path = pathlib.Path(args.input)
raise FileNotFoundError(in_file) output_path = pathlib.Path(args.output)
u8_data = open(in_file, "rb").read() if args.pack:
try:
u8_data = libWiiPy.archive.pack_u8(input_path)
except ValueError:
print("Error: Specified input file/folder does not exist!")
return
try: out_file = open(output_path, "wb")
libWiiPy.archive.extract_u8(u8_data, out_folder) out_file.write(u8_data)
except ValueError: out_file.close()
print("Specified output folder already exists!")
print("U8 archive packed!")
def pack_u8_from_folder(in_folder: str, out_file: str): elif args.unpack:
try: if not input_path.exists():
u8_data = libWiiPy.archive.pack_u8(in_folder) raise FileNotFoundError(args.input)
except ValueError:
print("Specified input file/folder does not exist!")
return
out_file = open(out_file, "wb") u8_data = open(input_path, "rb").read()
out_file.write(u8_data)
out_file.close() if output_path.exists():
print("Error: Specified output directory already exists!")
return
libWiiPy.archive.extract_u8(u8_data, str(output_path))
print("U8 archive unpacked!")

View File

@ -1,108 +1,100 @@
# "wad.py" from WiiPy by NinjaCheetah # "wad.py" from WiiPy by NinjaCheetah
# https://github.com/NinjaCheetah/WiiPy # https://github.com/NinjaCheetah/WiiPy
import os
import pathlib import pathlib
import binascii import binascii
import libWiiPy import libWiiPy
def extract_wad_to_folder(in_file: str, out_folder: str): def handle_wad(args):
if not os.path.isfile(in_file): input_path = pathlib.Path(args.input)
raise FileNotFoundError(in_file) output_path = pathlib.Path(args.output)
out_folder = pathlib.Path(out_folder)
if not out_folder.is_dir():
out_folder.mkdir()
with open(in_file, "rb") as wad_file: if args.pack:
wad = libWiiPy.title.WAD() if not input_path.exists():
wad.load(wad_file.read()) raise FileNotFoundError(input_path)
if not input_path.is_dir():
raise NotADirectoryError(input_path)
tmd = libWiiPy.title.TMD() tmd_file = list(input_path.glob("*.tmd"))[0]
tmd.load(wad.get_tmd_data()) if not tmd_file.exists():
ticket = libWiiPy.title.Ticket() raise FileNotFoundError("Cannot find a TMD! Exiting...")
ticket.load(wad.get_ticket_data())
content_region = libWiiPy.title.ContentRegion()
content_region.load(wad.get_content_data(), tmd.content_records)
title_key = ticket.get_title_key() ticket_file = list(input_path.glob("*.tik"))[0]
if not ticket_file.exists():
raise FileNotFoundError("Cannot find a Ticket! Exiting...")
cert_name = tmd.title_id + ".cert" cert_file = list(input_path.glob("*.cert"))[0]
cert_out = open(os.path.join(out_folder, cert_name), "wb") if not cert_file.exists():
cert_out.write(wad.get_cert_data()) raise FileNotFoundError("Cannot find a cert! Exiting...")
cert_out.close()
tmd_name = tmd.title_id + ".tmd" content_files = list(input_path.glob("*.app"))
tmd_out = open(os.path.join(out_folder, tmd_name), "wb") if not content_files:
tmd_out.write(wad.get_tmd_data()) raise FileNotFoundError("Cannot find any contents! Exiting...")
tmd_out.close()
ticket_name = tmd.title_id + ".tik" with open(output_path, "wb") as output_path:
ticket_out = open(os.path.join(out_folder, ticket_name), "wb") title = libWiiPy.title.Title()
ticket_out.write(wad.get_ticket_data())
ticket_out.close()
meta_name = tmd.title_id + ".footer" title.load_tmd(open(tmd_file, "rb").read())
meta_out = open(os.path.join(out_folder, meta_name), "wb") title.load_ticket(open(ticket_file, "rb").read())
meta_out.write(wad.get_meta_data()) title.wad.set_cert_data(open(cert_file, "rb").read())
meta_out.close() footer_file = list(input_path.glob("*.footer"))[0]
if footer_file.exists():
title.wad.set_meta_data(open(footer_file, "rb").read())
title.load_content_records()
for content_file in range(0, tmd.num_contents): title_key = title.ticket.get_title_key()
content_file_name = "000000"
content_file_name += str(binascii.hexlify(content_file.to_bytes()).decode()) + ".app"
content_out = open(os.path.join(out_folder, content_file_name), "wb")
content_out.write(content_region.get_content_by_index(content_file, title_key))
content_out.close()
content_list = list(input_path.glob("*.app"))
for index in range(len(title.content.content_records)):
for content in range(len(content_list)):
dec_content = open(content_list[content], "rb").read()
try:
# Attempt to load the content into the correct index.
title.content.load_content(dec_content, index, title_key)
break
except ValueError:
# Wasn't the right content, so try again.
pass
def pack_wad_from_folder(in_folder, out_file): output_path.write(title.dump_wad())
if not os.path.exists(in_folder):
raise FileNotFoundError(in_folder)
if not os.path.isdir(in_folder):
raise NotADirectoryError(in_folder)
out_file = pathlib.Path(out_file) print("WAD file packed!")
in_folder = pathlib.Path(in_folder)
tmd_file = list(in_folder.glob("*.tmd"))[0] elif args.unpack:
if not os.path.exists(tmd_file): if not input_path.exists():
raise FileNotFoundError("Cannot find a TMD! Exiting...") raise FileNotFoundError(input_path)
if not output_path.is_dir():
output_path.mkdir()
ticket_file = list(in_folder.glob("*.tik"))[0] with open(args.input, "rb") as wad_file:
if not os.path.exists(ticket_file): title = libWiiPy.title.Title()
raise FileNotFoundError("Cannot find a Ticket! Exiting...") title.load_wad(wad_file.read())
cert_file = list(in_folder.glob("*.cert"))[0] cert_name = title.tmd.title_id + ".cert"
if not os.path.exists(cert_file): cert_out = open(output_path.joinpath(cert_name), "wb")
raise FileNotFoundError("Cannot find a cert! Exiting...") cert_out.write(title.wad.get_cert_data())
cert_out.close()
content_files = list(in_folder.glob("*.app")) tmd_name = title.tmd.title_id + ".tmd"
if not content_files: tmd_out = open(output_path.joinpath(tmd_name), "wb")
raise FileNotFoundError("Cannot find any contents! Exiting...") tmd_out.write(title.wad.get_tmd_data())
tmd_out.close()
with open(out_file, "wb") as out_file: ticket_name = title.tmd.title_id + ".tik"
title = libWiiPy.title.Title() ticket_out = open(output_path.joinpath(ticket_name), "wb")
ticket_out.write(title.wad.get_ticket_data())
ticket_out.close()
title.load_tmd(open(tmd_file, "rb").read()) meta_name = title.tmd.title_id + ".footer"
title.load_ticket(open(ticket_file, "rb").read()) meta_out = open(output_path.joinpath(meta_name), "wb")
title.wad.set_cert_data(open(cert_file, "rb").read()) meta_out.write(title.wad.get_meta_data())
footer_file = list(in_folder.glob("*.footer"))[0] meta_out.close()
if os.path.exists(footer_file):
title.wad.set_meta_data(open(footer_file, "rb").read())
title.load_content_records()
title_key = title.ticket.get_title_key() for content_file in range(0, title.tmd.num_contents):
content_file_name = "000000" + str(binascii.hexlify(content_file.to_bytes()).decode()) + ".app"
content_out = open(output_path.joinpath(content_file_name), "wb")
content_out.write(title.get_content_by_index(content_file))
content_out.close()
content_list = list(in_folder.glob("*.app")) print("WAD file unpacked!")
for index in range(len(title.content.content_records)):
for content in range(len(content_list)):
dec_content = open(content_list[content], "rb").read()
try:
# Attempt to load the content into the correct index.
title.content.load_content(dec_content, index, title_key)
break
except ValueError:
# Wasn't the right content, so try again.
pass
out_file.write(title.dump_wad())

View File

@ -1,61 +1,55 @@
# "wiipy.py" from WiiPy by NinjaCheetah # "wiipy.py" from WiiPy by NinjaCheetah
# https://github.com/NinjaCheetah/WiiPy # https://github.com/NinjaCheetah/WiiPy
import sys import argparse
from modules.wad import * from modules.wad import *
from modules.nus import * from modules.nus import *
from modules.u8 import * from modules.u8 import *
from modules.ash import * from modules.ash import *
opts = [opt for opt in sys.argv[1:] if opt.startswith("-")]
args = [arg for arg in sys.argv[1:] if not arg.startswith("-")]
if __name__ == "__main__": if __name__ == "__main__":
if "WAD" in args: parser = argparse.ArgumentParser(description="WiiPy is a simple CLI tool to manage file formats used by the Wii.")
if "-u" in opts: subparsers = parser.add_subparsers(dest="subcommand", required=True)
if len(args) == 3:
extract_wad_to_folder(args[1], args[2]) wad_parser = subparsers.add_parser("wad", help="pack/unpack a WAD file",
print("Success!") description="pack/unpack a WAD file")
sys.exit(0) wad_parser.set_defaults(func=handle_wad)
if "-p" in opts: wad_group = wad_parser.add_mutually_exclusive_group(required=True)
if len(args) == 3: wad_group.add_argument("-p", "--pack", help="pack a directory to a WAD file", action="store_true")
pack_wad_from_folder(args[1], args[2]) wad_group.add_argument("-u", "--unpack", help="unpack a WAD file to a directory", action="store_true")
print("Success!") wad_parser.add_argument("input", metavar="IN", type=str, help="input file")
sys.exit(0) wad_parser.add_argument("output", metavar="OUT", type=str, help="output file")
raise SystemExit(f"Usage: {sys.argv[0]} WAD (-u | -p) <input> <output>")
elif "NUS" in args: nus_parser = subparsers.add_parser("nus", help="download a title from the NUS",
if "-d" in opts: description="download a title from the NUS")
if len(args) == 2: nus_parser.set_defaults(func=handle_nus)
download_title(args[1]) nus_parser.add_argument("tid", metavar="TID", type=str, help="Title ID to download")
print("Success!") nus_parser.add_argument("-v", "--version", metavar="VERSION", type=int,
sys.exit(0) help="Version to download")
elif len(args) == 3:
download_title(args[1], args[2]) u8_parser = subparsers.add_parser("u8", help="pack/unpack a U8 archive",
print("Success!") description="pack/unpack a U8 archive")
sys.exit(0) u8_parser.set_defaults(func=handle_u8)
raise SystemExit(f"Usage: {sys.argv[0]} NUS -d <Title ID> <Title Version (Optional)>") u8_group = u8_parser.add_mutually_exclusive_group(required=True)
elif "U8" in args: u8_group.add_argument("-p", "--pack", help="pack a directory to a U8 archive", action="store_true")
if "-u" in opts: u8_group.add_argument("-u", "--unpack", help="unpack a U8 archive to a directory", action="store_true")
if len(args) == 3: u8_parser.add_argument("input", metavar="IN", type=str, help="input file")
extract_u8_to_folder(args[1], args[2]) u8_parser.add_argument("output", metavar="OUT", type=str, help="output file")
print("Success!")
sys.exit(0) ash_parser = subparsers.add_parser("ash", help="compress/decompress an ASH file",
elif "-p" in opts: description="compress/decompress an ASH file")
if len(args) == 3: ash_parser.set_defaults(func=handle_ash)
pack_u8_from_folder(args[1], args[2]) ash_group = ash_parser.add_mutually_exclusive_group(required=True)
print("Success!") ash_group.add_argument("-c", "--compress", help="compress a file into an ASH file", action="store_true")
sys.exit(0) ash_group.add_argument("-d", "--decompress", help="decompress an ASH file", action="store_true")
raise SystemExit(f"Usage: {sys.argv[0]} U8 (-u | -p) <input> <output>") ash_parser.add_argument("input", metavar="IN", type=str, help="input file")
elif "ASH" in args: ash_parser.add_argument("output", metavar="OUT", type=str, help="output file")
if "-d" in opts: ash_parser.add_argument("--sym-bits", metavar="SYM_BITS", type=int,
if len(args) == 2: help="number of bits in each symbol tree leaf (default: 9)", default=9)
decompress_ash(args[1]) ash_parser.add_argument("--dist-bits", metavar="DIST_BITS", type=int,
print("Success!") help="number of bits in each distance tree leaf (default: 11)", default=11)
sys.exit(0)
elif len(args) == 3: args = parser.parse_args()
decompress_ash(args[1], args[2])
print("Success!") args.func(args)
sys.exit(0)
raise SystemExit(f"Usage: {sys.argv[0]} ASH -d <input> <output (Optional)>")
else:
raise SystemExit(f"Usage: {sys.argv[0]} WAD (-u | -p) <input> <output>")