diff --git a/modules/title/wad.py b/modules/title/wad.py index 26c16e0..99feb71 100644 --- a/modules/title/wad.py +++ b/modules/title/wad.py @@ -3,6 +3,7 @@ import os import pathlib +from multiprocessing.managers import Value from random import randint import libWiiPy @@ -156,6 +157,52 @@ def handle_wad_pack(args): print("WAD file packed!") + +def handle_wad_remove(args): + input_path = pathlib.Path(args.input) + if args.output is not None: + output_path = pathlib.Path(args.output) + else: + output_path = pathlib.Path(args.input) + + if not input_path.exists(): + raise FileNotFoundError(input_path) + + wad_file = open(input_path, 'rb') + title = libWiiPy.title.Title() + title.load_wad(wad_file.read()) + wad_file.close() + + if args.index is not None: + # List indices in the title, and ensure that the target content index exists. + valid_indices = [] + for record in title.content.content_records: + valid_indices.append(record.index) + if args.index not in valid_indices: + raise ValueError("The provided content index could not be found in this title!") + title.content.remove_content_by_index(args.index) + out_file = open(output_path, 'wb') + out_file.write(title.dump_wad()) + out_file.close() + print(f"Removed content at content index {args.index}!") + + elif args.cid is not None: + if len(args.cid) != 8: + raise ValueError("The provided Content ID is invalid!") + target_cid = int(args.cid, 16) + # List Contents IDs in the title, and ensure that the target Content ID exists. + valid_ids = [] + for record in title.content.content_records: + valid_ids.append(record.content_id) + if target_cid not in valid_ids: + raise ValueError("The provided Content ID could not be found in this title!") + title.content.remove_content_by_cid(target_cid) + out_file = open(output_path, 'wb') + out_file.write(title.dump_wad()) + out_file.close() + print(f"Removed content with Content ID \"{target_cid:08X}\"!") + + def handle_wad_unpack(args): input_path = pathlib.Path(args.input) output_path = pathlib.Path(args.output) diff --git a/wiipy.py b/wiipy.py index 4433b8d..3c42259 100644 --- a/wiipy.py +++ b/wiipy.py @@ -152,13 +152,13 @@ if __name__ == "__main__": wad_add_parser.add_argument("input", metavar="IN", type=str, help="WAD file to add to") wad_add_parser.add_argument("content", metavar="CONTENT", type=str, help="decrypted content to add") wad_add_parser.add_argument("-c", "--cid", metavar="CID", type=str, - help="content ID to assign the new content (optional, will be randomly assigned if " + help="Content ID to assign the new content (optional, will be randomly assigned if " "not specified)") wad_add_parser.add_argument("-t", "--type", metavar="TYPE", type=str, help="the type of the new content, can be \"Normal\", \"Shared\", or \"DLC\" " "(optional, will default to \"Normal\" if not specified)") wad_add_parser.add_argument("-o", "--output", metavar="OUT", type=str, - help="file to output the new WAD to (optional)") + help="file to output the updated WAD to (optional)") # Pack WAD subcommand. wad_pack_parser = wad_subparsers.add_parser("pack", help="pack a directory to a WAD file", description="pack a directory to a WAD file") @@ -167,6 +167,20 @@ if __name__ == "__main__": wad_pack_parser.add_argument("output", metavar="OUT", type=str, help="WAD file to pack") wad_pack_parser.add_argument("-f", "--fakesign", help="fakesign the TMD and Ticket (trucha bug)", action="store_true") + # Remove WAD subcommand. + wad_remove_parser = wad_subparsers.add_parser("remove", help="remove content from a WAD file", + description="remove content from a WAD file, either by its CID or" + "by its index; by default, this will overwrite the input " + "file unless an output is specified") + wad_remove_parser.set_defaults(func=handle_wad_remove) + wad_remove_parser.add_argument("input", metavar="IN", type=str, help="WAD file to remove content from") + wad_remove_targets = wad_remove_parser.add_mutually_exclusive_group(required=True) + wad_remove_targets.add_argument("-i", "--index", metavar="INDEX", type=int, + help="index of the content to remove") + wad_remove_targets.add_argument("-c", "--cid", metavar="CID", type=str, + help="Content ID of the content to remove") + wad_remove_parser.add_argument("-o", "--output", metavar="OUT", type=str, + help="file to output the updated WAD to (optional)") # Unpack WAD subcommand. wad_unpack_parser = wad_subparsers.add_parser("unpack", help="unpack a WAD file to a directory", description="unpack a WAD file to a directory")