From dcafda4b712a67a2164c99fcde4bf609be4f3afc Mon Sep 17 00:00:00 2001 From: NinjaCheetah <58050615+NinjaCheetah@users.noreply.github.com> Date: Sat, 6 Jul 2024 07:46:46 +1000 Subject: [PATCH] Allow extracting WAD/U8 files to an existing empty directory --- modules/u8.py | 8 ++------ modules/wad.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/modules/u8.py b/modules/u8.py index 68f6267..9a8caa0 100644 --- a/modules/u8.py +++ b/modules/u8.py @@ -30,12 +30,8 @@ def handle_u8(args): u8_data = open(input_path, "rb").read() - # Ensure the output directory doesn't already exist, because libWiiPy wants to create a new one to ensure that - # the contents of the U8 archive are extracted correctly. - if output_path.exists(): - print("Error: Specified output directory already exists!") - return - + # 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)) print("U8 archive unpacked!") diff --git a/modules/wad.py b/modules/wad.py index 018efc1..4ed4bd2 100644 --- a/modules/wad.py +++ b/modules/wad.py @@ -1,6 +1,7 @@ # "wad.py" from WiiPy by NinjaCheetah # https://github.com/NinjaCheetah/WiiPy +import os import pathlib import binascii import libWiiPy @@ -89,8 +90,14 @@ def handle_wad(args): elif args.unpack: if not input_path.exists(): raise FileNotFoundError(input_path) - if not output_path.is_dir(): - output_path.mkdir() + # Check if the output path already exists, and if it does, ensure that it is both a directory and empty. + if output_path.exists(): + if output_path.is_dir() and next(os.scandir(output_path), None): + raise ValueError("Output folder is not empty!") + elif output_path.is_file(): + raise ValueError("A file already exists with the provided name!") + else: + os.mkdir(output_path) # Step through each component of a WAD and dump it to a file. with open(args.input, "rb") as wad_file: