Files
NukageTools/unmake.py
2025-01-01 06:49:02 -06:00

89 lines
2.7 KiB
Python

#!/usr/bin/env python3
import os
import sys
import shutil
from urllib.request import urlopen
from zipfile import ZipFile
MY_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(MY_DIR, "omgifol"))
import omg
def unmake(source, target=".", overwritedirs=True):
print(source)
ipk3 = ZipFile(source)
for entry in ipk3.infolist():
local_path = os.path.join(target, entry.filename).replace("/", os.sep)
if entry.is_dir():
if overwritedirs and os.path.isdir(local_path):
shutil.rmtree(local_path)
print(f">> Removing existing content of directory {local_path}")
os.makedirs(local_path, exist_ok=True)
print(f">> Creating directory {local_path}")
else:
with ipk3.open(entry) as source_file:
with open(local_path, "wb") as target_file:
print(f">> Writing file {local_path}")
shutil.copyfileobj(source_file, target_file)
def unmerge(source, target="maps"):
source_wad = omg.WadIO(source)
target_wad = None
current_map = None
in_maps = False
os.makedirs(target, exist_ok=True)
for i, entry in enumerate(source_wad.entries):
if entry.name == "P_START":
break
if entry.name.startswith("E"):
in_maps = True
if not in_maps:
continue
if current_map is None:
current_map = entry.name
target_path = os.path.join(target, f"{current_map}.wad")
if os.path.exists(target_path):
os.remove(target_path)
target_wad = omg.WadIO(target_path)
if target_wad is not None:
print(f">> Processing entry {entry.name}")
target_wad.insert(entry.name, source_wad.read(i))
if entry.name == "ENDMAP":
print(f">> Writing map {current_map}")
target_wad.save()
current_map = None
target_wad = None
if __name__ == "__main__":
SOURCE = None
OVERWRITE = False
UNMERGE = False
for arg in sys.argv[1:]:
if arg == "-f":
OVERWRITE = True
elif arg == "-u":
UNMERGE = True
else:
SOURCE = arg
if SOURCE.startswith("https://"):
with urlopen(SOURCE) as remote_file:
unmake(remote_file, overwritedirs=OVERWRITE)
else:
with open(SOURCE, "rb") as local_file:
unmake(local_file, overwritedirs=OVERWRITE)
if UNMERGE:
for item in os.listdir("."):
if item.endswith(".wad"):
print(f">> Unmerging maps wad {item}")
unmerge(item)
os.remove(item)