diff --git a/src/commands/create.rs b/src/commands/create.rs index bd27aea..b7dc5d9 100644 --- a/src/commands/create.rs +++ b/src/commands/create.rs @@ -1,4 +1,4 @@ -use std::error::Error; +use std::{collections::HashMap, error::Error}; use dialoguer::{FuzzySelect, Select, theme::ColorfulTheme}; use futures_util::StreamExt; @@ -6,7 +6,7 @@ use tokio::{fs::File, io::AsyncWriteExt}; use crate::{ models::{ - config::{Config, Mods, Server}, + config::{Config, Server}, fabric::FabricVersions, vanilla::{VanillaVersionDetail, VanillaVersionType, VanillaVersions}, }, @@ -188,7 +188,7 @@ impl LoaderDownloader for VanillaDownloader { loader: Self::name(), loader_version: None, }, - mods: Mods {}, + mods: HashMap::new(), }; Ok(config) @@ -302,7 +302,7 @@ impl LoaderDownloader for FabricDownloader { loader: Self::name(), loader_version: create.loader_version.clone(), }, - mods: Mods {}, + mods: HashMap::new(), }; Ok(config) diff --git a/src/commands/install.rs b/src/commands/install.rs index 45b7579..caa1b74 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -27,7 +27,7 @@ impl Install { if file_res.is_err() { return Err("current directory is not a mcsm project".into()); } - let config: Config = toml::from_str(&file_res.unwrap())?; + let mut config: Config = toml::from_str(&file_res.unwrap())?; if config.server.loader == "Vanilla" { return Err("cannot search for mods on Vanilla server".into()); @@ -47,7 +47,7 @@ impl Install { game_versions: format!(r#"["{}"]"#, config.server.version), }; - let install_result: Vec = with_spinner( + let res: Vec = with_spinner( format!("fetching {}", mod_name).as_str(), format!("fetched {}", mod_name).as_str(), || async { @@ -62,7 +62,9 @@ impl Install { ) .await?; - let file = install_result.first().unwrap().files.first().unwrap(); + let install_result = res.first().unwrap(); + let file = &install_result.files.first().unwrap(); + let version_number = &install_result.version_number; with_progressbar( format!("downloading {}", mod_name).as_str(), format!("downloaded {}", mod_name).as_str(), @@ -88,7 +90,7 @@ impl Install { let verified = verify_sha512(&data, &file.hashes.sha512); if !verified { - tokio::fs::remove_file("server.jar").await?; + tokio::fs::remove_file(&file.filename).await?; return Err("sha1 verifaction failed for downloaded file".into()); } @@ -96,6 +98,8 @@ impl Install { }) .await?; println!(); + + config.mods.insert(mod_name.clone(), version_number.clone()); } println!( @@ -103,6 +107,13 @@ impl Install { dialoguer::console::style("✔").green() ); + with_spinner("saving to config", "saved to config", || async { + std::env::set_current_dir("..")?; + let config_str = toml::to_string(&config)?; + tokio::fs::write("mcsm.toml", config_str).await?; + Ok(()) + }).await?; + Ok(()) } } diff --git a/src/models/config.rs b/src/models/config.rs index 3505350..74852a6 100644 --- a/src/models/config.rs +++ b/src/models/config.rs @@ -1,9 +1,11 @@ +use std::collections::HashMap; + use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize)] pub struct Config { pub server: Server, - pub mods: Mods, + pub mods: HashMap, } #[derive(Deserialize, Serialize)] @@ -12,6 +14,3 @@ pub struct Server { pub loader: String, pub loader_version: Option, } - -#[derive(Deserialize, Serialize)] -pub struct Mods {}