From 31635a80150ec557c149d04ad57fcd9abdf54f9a Mon Sep 17 00:00:00 2001 From: NinjaCheetah <58050615+NinjaCheetah@users.noreply.github.com> Date: Wed, 16 Oct 2024 11:21:16 -0400 Subject: [PATCH] Improve syntax for ASH command to match other new commands --- modules/archive/ash.py | 46 ++++++++++++++++++------------------------ wiipy.py | 35 +++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/modules/archive/ash.py b/modules/archive/ash.py index 4ed4b0a..b1661d1 100644 --- a/modules/archive/ash.py +++ b/modules/archive/ash.py @@ -5,34 +5,28 @@ import pathlib import libWiiPy -def handle_ash(args): +def handle_ash_compress(args): + print("Compression is not implemented yet.") + + +def handle_ash_decompress(args): input_path = pathlib.Path(args.input) - output_path = pathlib.Path(args.output) + if args.output is not None: + output_path = pathlib.Path(args.output) + else: + output_path = pathlib.Path(input_path.name + ".arc") - # Code for if --compress was passed. - # ASH compression has not been implemented in libWiiPy yet, but it'll be filled in here when it has. - if args.compress: - print("Compression is not implemented yet.") + # These default to 9 and 11, respectively, so we can always read them. + sym_tree_bits = args.sym_bits + dist_tree_bits = args.dist_bits - # Code for if --decompress was passed. - elif args.decompress: - # These default to 9 and 11, respectively, so we can always read them. - sym_tree_bits = args.sym_bits - dist_tree_bits = args.dist_bits + if not input_path.exists(): + raise FileNotFoundError(input_path) - if not input_path.exists(): - raise FileNotFoundError(input_path) + ash_data = input_path.read_bytes() + # Decompress ASH file using the provided symbol/distance tree widths. + ash_decompressed = libWiiPy.archive.decompress_ash(ash_data, sym_tree_bits=sym_tree_bits, + dist_tree_bits=dist_tree_bits) + output_path.write_bytes(ash_decompressed) - ash_file = open(input_path, "rb") - ash_data = ash_file.read() - ash_file.close() - - # Decompress ASH file using the provided symbol/distance tree widths. - 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!") + print("ASH file decompressed!") diff --git a/wiipy.py b/wiipy.py index 5702278..af31c4d 100644 --- a/wiipy.py +++ b/wiipy.py @@ -15,27 +15,44 @@ from modules.title.iospatcher import * from modules.title.nus import * from modules.title.wad import * +wiipy_ver = "1.4.0" + if __name__ == "__main__": # Main argument parser. parser = argparse.ArgumentParser( description="A simple command line tool to manage file formats used by the Wii.") parser.add_argument("--version", action="version", - version=f"WiiPy v1.4.0, based on libWiiPy v{version('libWiiPy')} (from branch \'main\')") + version=f"WiiPy v{wiipy_ver}, based on libWiiPy v{version('libWiiPy')} (from branch \'main\')") subparsers = parser.add_subparsers(title="subcommands", dest="subcommand", required=True) # Argument parser for the ASH subcommand. ash_parser = subparsers.add_parser("ash", help="compress/decompress an ASH file", description="compress/decompress an ASH file") - ash_parser.set_defaults(func=handle_ash) - ash_group = ash_parser.add_mutually_exclusive_group(required=True) - ash_group.add_argument("-c", "--compress", help="compress a file into an ASH file", action="store_true") - ash_group.add_argument("-d", "--decompress", help="decompress an ASH file", action="store_true") - ash_parser.add_argument("input", metavar="IN", type=str, help="input file") - ash_parser.add_argument("output", metavar="OUT", type=str, help="output file") - ash_parser.add_argument("--sym-bits", metavar="SYM_BITS", type=int, + ash_subparsers = ash_parser.add_subparsers(title="emunand", dest="emunand", required=True) + # ASH compress parser. + ash_compress_parser = ash_subparsers.add_parser("compress", help="compress a file into an ASH file", + description="compress a file into an ASH file; by default, this " + "will output to .ash") + ash_compress_parser.set_defaults(func=handle_ash_compress) + ash_compress_parser.add_argument("input", metavar="IN", type=str, help="file to compress") + ash_compress_parser.add_argument("--sym-bits", metavar="SYM_BITS", type=int, help="number of bits in each symbol tree leaf (default: 9)", default=9) - ash_parser.add_argument("--dist-bits", metavar="DIST_BITS", type=int, + ash_compress_parser.add_argument("--dist-bits", metavar="DIST_BITS", type=int, help="number of bits in each distance tree leaf (default: 11)", default=11) + ash_compress_parser.add_argument("-o", "--output", metavar="OUT", type=str, + help="file to output the ASH file to (optional)") + # ASH decompress parser. + ash_decompress_parser = ash_subparsers.add_parser("decompress", help="decompress an ASH file", + description="decompress an ASH file; by default, this will " + "output to .arc") + ash_decompress_parser.set_defaults(func=handle_ash_decompress) + ash_decompress_parser.add_argument("input", metavar="IN", type=str, help="ASH file to decompress") + ash_decompress_parser.add_argument("--sym-bits", metavar="SYM_BITS", type=int, + help="number of bits in each symbol tree leaf (default: 9)", default=9) + ash_decompress_parser.add_argument("--dist-bits", metavar="DIST_BITS", type=int, + help="number of bits in each distance tree leaf (default: 11)", default=11) + ash_decompress_parser.add_argument("-o", "--output", metavar="OUT", type=str, + help="file to output the ASH file to (optional)") # Argument parser for the cIOS command cios_parser = subparsers.add_parser("cios", help="build a cIOS from a base IOS and provided map",