use anyhow::anyhow; use log::error; use crate::{ nexus::{NXMUrl, download_nxm}, types::{ModConfig, RootConfig}, unpacker::unpack, }; /// Handles a nexus mod url. Downloads, unpacks and adds the mod to the config pub fn handle_nxm(root_config: &mut RootConfig, raw_url: &str) -> anyhow::Result<()> { let Some(dl_location) = root_config.download_location() else { return Err(anyhow!("No download location set")); }; let Some(api_key) = root_config.nexus_api_key() else { return Err(anyhow!("No API key provided")); }; let Some(nxm_url) = NXMUrl::parse_url(raw_url) else { return Err(anyhow!("Failed to parse URL")); }; let (dl_file, mod_info) = download_nxm(api_key, &nxm_url, dl_location)?; let mod_id = format!("{}-{}", mod_info.generate_id(), nxm_url.file); if root_config.game_by_id(&mod_id).is_some() { error!( "Generated mod id already exists. Pleas install downloaded mod manually. Downloaded at {}", &dl_file.to_string_lossy() ); return Err(anyhow!("Mod with generated id already exists")); } let extract_to = root_config.mod_location().join(&mod_id); unpack(dl_file, extract_to)?; let file_id: u64 = nxm_url.file.parse()?; let new_mod = ModConfig::from_mod_info(&mod_id, &mod_id, &mod_info, file_id); root_config.add_mod(&new_mod); Ok(()) }