oliver 0.1.9

Lightweight CLI mod manager for Baldur's Gate 3 on Linux
Documentation
use crate::Settings;
use anyhow::{bail, Result};
use larian_formats::lspk::{self};
use std::{fs::File, path::PathBuf};

impl Settings {
    pub(crate) fn unpack_helper(
        mod_file_path: PathBuf,
        destination: Option<PathBuf>,
    ) -> Result<()> {
        let file = File::open(&mod_file_path)?;

        let data = match mod_file_path
            .extension()
            .map(|s| s.to_string_lossy())
            .as_deref()
        {
            Some("zip") => lspk::entry_from_zipfile(&file)?.1,
            Some("pak") => lspk::Reader::new(file)?.read()?,
            _other => {
                bail!("expected .pak file");
            }
        };

        let prefix_dir = match destination {
            Some(unpack_dir) => {
                std::fs::create_dir_all(&unpack_dir)?;
                unpack_dir
            }
            None => "./".into(),
        };

        for file in data.files {
            let path = prefix_dir.join(file.path);

            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent)?;
            }

            std::fs::write(path, file.decompressed_bytes)?;
        }

        Ok(())
    }
}