linux-mod-manager/lmm/mods/bg3.py

64 lines
1.9 KiB
Python

import json
from pathlib import Path
import zipfile
from lmm.const import LMM_GAMES_PATH
from lmm.games.bg3 import GAME_NAME
def install_mod(mod: Path):
match mod.suffix:
case ".zip":
install_zip_mod(mod)
case _:
print("Unknown mod")
def install_zip_mod(mod: Path):
# Inspect the archive to see if it's a PAK mod
with zipfile.ZipFile(mod, "r") as f:
paks = []
info_json = None
for member in f.infolist():
if member.filename.endswith(".pak"):
paks.append(member)
elif member.filename == "info.json":
info_json = member
if not paks:
print("Automatic installation of non-PAK mods not supported yet")
pass
# This mod is a PAK only mod
if info_json is not None:
with f.open("info.json", "r") as info:
data = json.load(info)
name = data["Mods"][0]["Name"].replace(" ", "")
elif len(paks) == 1:
print("Guessing name from PAK file")
name = paks[0].filename.replace(".pak", "").replace(" ", "")
else:
print("Cannot deduce mod name!")
try:
name = input("Mod name: ")
except InterruptedError:
print("Not installing mod")
return
except EOFError:
print("Not installing mod")
return
# Check if we can install the mod
installed_mod_path = LMM_GAMES_PATH / GAME_NAME / name
if installed_mod_path.exists():
print(f"Mod already exists: {name}")
return
# Install the mod
installed_mod_path.mkdir(parents=True)
for pak in paks:
print(f"Extracting {pak.filename}...")
f.extract(pak, installed_mod_path)
print(f"Installed mod as {name}")