From 2733b70e18296fc616b1e5673dfb21e42a6e18b0 Mon Sep 17 00:00:00 2001 From: NinjaCheetah <58050615+NinjaCheetah@users.noreply.github.com> Date: Sun, 10 Nov 2024 21:51:46 -0500 Subject: [PATCH] Cleaned up warnings related to error handling changes --- commands/archive/u8.py | 11 +++-------- commands/nand/setting.py | 8 ++++---- commands/title/fakesign.py | 12 ++++++------ commands/title/iospatcher.py | 17 +++++++---------- commands/title/nus.py | 7 +++++-- commands/title/wad.py | 10 ++++------ 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/commands/archive/u8.py b/commands/archive/u8.py index 908d27b..5fe4a29 100644 --- a/commands/archive/u8.py +++ b/commands/archive/u8.py @@ -10,14 +10,12 @@ def handle_u8_pack(args): input_path = pathlib.Path(args.input) output_path = pathlib.Path(args.output) + u8_data = None try: u8_data = libWiiPy.archive.pack_u8(input_path) except ValueError: fatal_error(f"The specified input file/folder \"{input_path}\" does not exist!") - - out_file = open(output_path, "wb") - out_file.write(u8_data) - out_file.close() + output_path.write_bytes(u8_data) print("U8 archive packed!") @@ -28,11 +26,8 @@ def handle_u8_unpack(args): if not input_path.exists(): fatal_error(f"The specified input file \"{input_path}\" does not exist!") - - u8_data = open(input_path, "rb").read() - # Output path is deliberately not checked in any way because libWiiPy already has those checks, and it's easier # and cleaner to only have one component doing all the checks. - libWiiPy.archive.extract_u8(u8_data, str(output_path)) + libWiiPy.archive.extract_u8(input_path.read_bytes(), str(output_path)) print("U8 archive unpacked!") diff --git a/commands/nand/setting.py b/commands/nand/setting.py index b1a8477..3912cce 100644 --- a/commands/nand/setting.py +++ b/commands/nand/setting.py @@ -18,9 +18,9 @@ def handle_setting_decrypt(args): # Load and decrypt the provided file. setting = libWiiPy.nand.SettingTxt() - setting.load(open(input_path, "rb").read()) + setting.load(input_path.read_bytes()) # Write out the decrypted data. - open(output_path, "w").write(setting.dump_decrypted()) + output_path.write_text(setting.dump_decrypted()) print("Successfully decrypted setting.txt!") @@ -36,9 +36,9 @@ def handle_setting_encrypt(args): # Load and encrypt the provided file. setting = libWiiPy.nand.SettingTxt() - setting.load_decrypted(open(input_path, "r").read()) + setting.load_decrypted(input_path.read_text()) # Write out the encrypted data. - open(output_path, "wb").write(setting.dump()) + output_path.write_bytes(setting.dump()) print("Successfully encrypted setting.txt!") diff --git a/commands/title/fakesign.py b/commands/title/fakesign.py index 608d496..6e8e995 100644 --- a/commands/title/fakesign.py +++ b/commands/title/fakesign.py @@ -18,21 +18,21 @@ def handle_fakesign(args): if input_path.suffix.lower() == ".tmd": tmd = libWiiPy.title.TMD() - tmd.load(open(input_path, "rb").read()) + tmd.load(input_path.read_bytes()) tmd.fakesign() - open(output_path, "wb").write(tmd.dump()) + output_path.write_bytes(tmd.dump()) print("TMD fakesigned successfully!") elif input_path.suffix.lower() == ".tik": tik = libWiiPy.title.Ticket() - tik.load(open(input_path, "rb").read()) + tik.load(input_path.read_bytes()) tik.fakesign() - open(output_path, "wb").write(tik.dump()) + output_path.write_bytes(tik.dump()) print("Ticket fakesigned successfully!") elif input_path.suffix.lower() == ".wad": title = libWiiPy.title.Title() - title.load_wad(open(input_path, "rb").read()) + title.load_wad(input_path.read_bytes()) title.fakesign() - open(output_path, "wb").write(title.dump_wad()) + output_path.write_bytes(title.dump_wad()) print("WAD fakesigned successfully!") else: fatal_error("The provided file does not appear to be a TMD, Ticket, or WAD and cannot be fakesigned!") diff --git a/commands/title/iospatcher.py b/commands/title/iospatcher.py index 2bff729..da85992 100644 --- a/commands/title/iospatcher.py +++ b/commands/title/iospatcher.py @@ -60,11 +60,16 @@ def _patch_drive_inquiry(ios_patcher: libWiiPy.title.IOSPatcher) -> int: def handle_iospatch(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(): fatal_error(f"The specified IOS file \"{input_path}\" does not exist!") title = libWiiPy.title.Title() - title.load_wad(open(input_path, "rb").read()) + title.load_wad(input_path.read_bytes()) tid = title.tmd.title_id if tid[:8] != "00000001" or tid[8:] == "00000001" or tid[8:] == "00000002": @@ -117,14 +122,6 @@ def handle_iospatch(args): ios_patcher.title.content.content_records[ios_patcher.dip_module_index].content_type = 1 ios_patcher.title.fakesign() # Signature is broken anyway, so fakesign for maximum installation openings - if args.output is not None: - output_path = pathlib.Path(args.output) - output_file = open(output_path, "wb") - output_file.write(ios_patcher.title.dump_wad()) - output_file.close() - else: - output_file = open(input_path, "wb") - output_file.write(ios_patcher.title.dump_wad()) - output_file.close() + output_path.write_bytes(ios_patcher.title.dump_wad()) print("IOS successfully patched!") diff --git a/commands/title/nus.py b/commands/title/nus.py index e9c3d25..30914ca 100644 --- a/commands/title/nus.py +++ b/commands/title/nus.py @@ -133,6 +133,7 @@ def handle_nus_content(args): # Only accepting the 000000xx format because it's the one that would be most commonly known, rather than using the # actual integer that the hex Content ID translates to. + content_id = None try: content_id = int.from_bytes(binascii.unhexlify(cid)) except binascii.Error: @@ -151,6 +152,7 @@ def handle_nus_content(args): # Try to download the content, and catch the ValueError libWiiPy will throw if it can't be found. print("Downloading content with Content ID " + cid + "...") + content_data = None try: content_data = libWiiPy.title.download_content(tid, content_id) except ValueError: @@ -161,6 +163,7 @@ def handle_nus_content(args): tmd = libWiiPy.title.TMD() tmd.load(libWiiPy.title.download_tmd(tid, version)) # Try to get a Ticket for the title, if a common one is available. + ticket = None try: ticket = libWiiPy.title.Ticket() ticket.load(libWiiPy.title.download_ticket(tid, wiiu_endpoint=True)) @@ -200,13 +203,12 @@ def handle_nus_tmd(args): tid = args.tid # Check if --version was passed, because it'll be None if it wasn't. + version = None if args.version is not None: try: version = int(args.version) except ValueError: fatal_error("The specified TMD version must be a valid integer!") - else: - version = None # Use the supplied output path if one was specified, otherwise generate one using the Title ID. If a version has # been specified, append the version to the end of the path as well. @@ -220,6 +222,7 @@ def handle_nus_tmd(args): # Try to download the TMD, and catch the ValueError libWiiPy will throw if it can't be found. print(f"Downloading TMD for title {tid}...") + tmd_data = None try: tmd_data = libWiiPy.title.download_tmd(tid, version) except ValueError: diff --git a/commands/title/wad.py b/commands/title/wad.py index 0baca27..ab50ece 100644 --- a/commands/title/wad.py +++ b/commands/title/wad.py @@ -45,6 +45,7 @@ def handle_wad_add(args): print(f"Using randomly assigned Content ID \"{target_cid:08X}\" since none were provided.") # Get the type of the new content. + target_type = libWiiPy.title.ContentType.NORMAL if args.type is not None: match str.lower(args.type): case "normal": @@ -55,8 +56,6 @@ def handle_wad_add(args): target_type = libWiiPy.title.ContentType.DLC case _: fatal_error(f"The provided content type \"{args.type}\" is not valid!") - else: - target_type = libWiiPy.title.ContentType.NORMAL # Call add_content to add our new content with the set parameters. title.add_content(content_data, target_cid, target_type) @@ -70,6 +69,7 @@ def handle_wad_add(args): def handle_wad_convert(args): input_path = pathlib.Path(args.input) + target = None if args.dev: target = "development" elif args.retail: @@ -79,6 +79,7 @@ def handle_wad_convert(args): else: fatal_error("No valid encryption target was specified!") + output_path = pathlib.Path(args.output) if args.output is None: match target: case "development": @@ -89,8 +90,6 @@ def handle_wad_convert(args): output_path = pathlib.Path(input_path.stem + "_vWii" + input_path.suffix) case _: fatal_error("No valid encryption target was specified!") - else: - output_path = pathlib.Path(args.output) if not input_path.exists(): fatal_error(f"The specified WAD file \"{input_path}\" does not exist!") @@ -276,6 +275,7 @@ def handle_wad_set(args): content_data = content_path.read_bytes() # Get the new type of the content, if one was specified. + target_type = None if args.type is not None: match str.lower(args.type): case "normal": @@ -286,8 +286,6 @@ def handle_wad_set(args): target_type = libWiiPy.title.ContentType.DLC case _: fatal_error(f"The provided content type \"{args.type}\" is not valid!\"") - else: - target_type = None if args.index is not None: # If we're replacing based on the index, then make sure the specified index exists.