Allow extracting WAD/U8 files to an existing empty directory

This commit is contained in:
Campbell 2024-07-06 07:46:46 +10:00
parent c7e78476c0
commit dcafda4b71
Signed by: NinjaCheetah
GPG Key ID: 670C282B3291D63D
2 changed files with 11 additions and 8 deletions

View File

@ -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!")

View File

@ -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: