balatro_mod_manager 0.2.0

a library for downloading Balatro mods and interacting with the `balatro_mod_index` crate.
Documentation
#![allow(clippy::missing_errors_doc)]

pub const CRATE_NAME: &str = "libmanager";

use std::collections::HashMap;
use std::fs::File;
use std::io::{self, Write};
use std::path::PathBuf;

use balatro_mod_index::mods::{Mod, ModId, ModIndex};
use zip::{ZipArchive, read::root_dir_common_filter};

pub type ModEntry<'index> = (ModId, Mod<'index>);
#[derive(Clone, Debug, Default)]
pub struct ModManager<'index> {
    pub index: ModIndex<'index>,
    pub installed_mods: HashMap<ModId, String>,
    pub expected_mods: HashMap<ModId, (bool, String)>,
}
impl ModManager<'_> {
    pub fn detect_installed_mods(&mut self) -> Result<(), String> {
        self.installed_mods = detect_installed_mods()?
            .into_iter()
            .map(|(id, (_, version))| (id, version))
            .collect();
        Ok(())
    }
    pub fn read_expected_mods(&mut self) -> Result<(), String> {
        self.expected_mods = read_expected_mods()?;
        Ok(())
    }
    pub fn write_expected_mods(&self) -> Result<(), String> {
        let p = mods_dir()?.join(format!(".{}", crate::CRATE_NAME));
        let mut expectfile = File::create(p).map_err(|e| e.to_string())?;
        self.expected_mods
            .iter()
            .try_for_each(|(id, (enabled, version))| {
                writeln!(
                    expectfile,
                    "{}/{id}/{version}",
                    if *enabled { "" } else { "-" }
                )
                .map_err(|e| e.to_string())
            })
            .map_err(|e| format!("failed to write expectfile: {e}"))
    }
    pub fn repopulate_expected_mods(&mut self) -> Result<(), String> {
        self.expected_mods = detect_installed_mods()?;
        self.write_expected_mods()
    }

    pub fn uninstall_mod(&mut self, (id, m): &ModEntry) -> Result<(), String> {
        _ = self.installed_mods.get(id).ok_or("mod not installed")?;
        let mod_dir = mod_path(m)?;
        if !mod_dir.exists() {
            return Ok(());
        }
        std::fs::remove_dir_all(mod_dir).map_err(|e| e.to_string())?;
        self.installed_mods.remove(id);
        self.expected_mods.remove(id);
        self.write_expected_mods()
    }
    pub async fn install_mod(
        &mut self,
        client: &reqwest::Client,
        entry: &ModEntry<'_>,
    ) -> Result<(), String> {
        install_mod(self, client, entry, false).await
    }
    pub async fn reinstall_mod(
        &mut self,
        client: &reqwest::Client,
        entry: &ModEntry<'_>,
    ) -> Result<(), String> {
        install_mod(self, client, entry, true).await
    }
    pub fn enable_mod(&mut self, (id, m): &ModEntry<'_>) -> Result<(), String> {
        let version = self.installed_mods.get(id).ok_or("mod not installed")?;
        let disablefile = mod_path(m)?.join(".lovelyignore");
        if !disablefile.exists() {
            return Ok(());
        }
        std::fs::remove_file(disablefile).map_err(|e| e.to_string())?;
        self.expected_mods
            .insert(id.clone(), (true, version.clone()));
        self.write_expected_mods()
    }
    pub fn disable_mod(&mut self, (id, m): &ModEntry<'_>) -> Result<(), String> {
        let version = self.installed_mods.get(id).ok_or("mod not installed")?;
        File::create(mod_path(m)?.join(".lovelyignore")).map_err(|e| e.to_string())?;
        self.expected_mods
            .insert(id.clone(), (false, version.clone()));
        self.write_expected_mods()
    }
}

async fn install_mod(
    manager: &mut ModManager<'_>,
    client: &reqwest::Client,
    entry @ (id, m): &ModEntry<'_>,
    reinstall: bool,
) -> Result<(), String> {
    let outdir = mod_path(m)?;

    let data = client
        .get(&m.meta.download_url)
        .send()
        .await
        .map_err(|e| e.to_string())?
        .bytes()
        .await
        .map_err(|e| e.to_string())?;

    let mut zip = ZipArchive::new(io::Cursor::new(data)).map_err(|e| e.to_string())?;
    log::debug!(
        "downloaded zip file {}, will install it to {}",
        m.meta.download_url,
        outdir.display()
    );

    if outdir.exists() {
        if !reinstall {
            return Err(format!("mod `{}` is already installed", id.0));
        }
        std::fs::remove_dir_all(&outdir).map_err(|e| e.to_string())?;
    }
    zip.extract_unwrapped_root_dir(&outdir, root_dir_common_filter)
        .map_err(|e| e.to_string())?;

    let statefile = &mut File::create(outdir.join(format!(".{}", crate::CRATE_NAME)))
        .map_err(|e| e.to_string())?;
    write_state(entry, statefile).map_err(|e| e.to_string())?;

    manager
        .installed_mods
        .insert(id.clone(), m.meta.version.clone());
    manager
        .expected_mods
        .insert(id.clone(), (true, m.meta.version.clone()));
    manager.write_expected_mods()?;
    Ok(())
}

fn mod_path(m: &Mod<'_>) -> Result<PathBuf, String> {
    let basename = m
        .meta
        .folder_name
        .clone()
        .unwrap_or_else(|| m.meta.title.chars().filter(char::is_ascii).collect());
    Ok(mods_dir()?.join(&basename))
}

fn detect_installed_mods() -> Result<HashMap<ModId, (bool, String)>, String> {
    let mods_dir = mods_dir()?;

    if !mods_dir.exists() {
        return Err("Mods directory does not exist".to_string());
    }

    let mut mods = HashMap::new();
    for entry in std::fs::read_dir(&mods_dir).map_err(|e| e.to_string())? {
        let path = entry.map_err(|e| e.to_string())?.path();
        if !path.is_dir() {
            continue;
        }

        let statefile = path.join(format!(".{}", crate::CRATE_NAME));
        if !statefile.exists() {
            continue;
        }
        let (id, version) =
            parse_state(&std::fs::read_to_string(statefile).map_err(|e| e.to_string())?)?;
        let enabled = !path.join(".lovelyignore").exists();

        mods.insert(id.clone(), (enabled, version.clone()));
    }

    Ok(mods)
}

fn mods_dir() -> Result<PathBuf, String> {
    let mods_dir = dirs::config_dir()
        .ok_or("couldn't find config directory, your env is so cooked")?
        .join("Balatro")
        .join("Mods");

    if !mods_dir.exists() {
        std::fs::create_dir_all(&mods_dir).map_err(|e| e.to_string())?;
    }

    Ok(mods_dir)
}

fn write_state<T>((id, m): &ModEntry<'_>, statefile: &mut T) -> Result<(), String>
where
    T: Write,
{
    write!(statefile, "{id}/{}", m.meta.version).map_err(|e| e.to_string())
}
fn parse_state(state: &str) -> Result<(ModId, String), String> {
    state
        .lines()
        .find(|l| !l.trim().is_empty())
        .ok_or("statefile can't be empty")?
        .split_once('/')
        .map_or(Err("invalid statefile format".into()), |(id, version)| {
            if id.is_empty() {
                return Err("id can't be empty".to_string());
            }
            if version.is_empty() {
                return Err("version can't be empty".to_string());
            }
            Ok((ModId(id.into()), version.to_string()))
        })
}

fn read_expected_mods() -> Result<HashMap<ModId, (bool, String)>, String> {
    let p = mods_dir()?.join(format!(".{}", crate::CRATE_NAME));
    if !p.exists() {
        return Ok(HashMap::new());
    }
    Ok(std::fs::read_to_string(p)
        .map_err(|e| e.to_string())?
        .lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|l| {
            if let [s, id, version] = l.split('/').collect::<Vec<_>>()[..] {
                Some((ModId(id.into()), (s.is_empty(), version.to_string())))
            } else {
                log::warn!("invalid modspec format in `{l}`, ignored it");
                None
            }
        })
        .collect::<HashMap<_, _>>())
}