From 575d2493e8e5747bf65321f7277e52211d73e387 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Tue, 29 Nov 2022 22:59:19 +0100 Subject: fixed mod without matching specific version --- data.db | Bin 24576 -> 24576 bytes src/apis/modrinth.rs | 40 ++++++++++++++++++++++++++-------------- src/commands/modification.rs | 38 ++++++++++++++++++++++++++------------ src/commands/update.rs | 11 ++++++++--- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/data.db b/data.db index 9f6316d..51f2ff4 100644 Binary files a/data.db and b/data.db differ diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 7b322cb..fb1e666 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -111,18 +111,25 @@ pub struct Hash { pub sha1: String, } -async fn get(api: String, path: String) -> Result, Box> { +async fn get(api: String, path: String) -> Result>, Box> { let url = format!(r#"{}{}"#, api, path); let client = Client::builder() .user_agent(format!("fxqnlr/modlistcli/{} (fxqnlr@gmail.com)", env!("CARGO_PKG_VERSION"))) .build()?; - let data = client.get(url) + let res = client.get(url) .send() - .await? - .bytes() - .await? - .to_vec(); + .await?; + + let mut data: Option> = None; + + if res.status() == 200 { + data = Some(res + .bytes() + .await? + .to_vec() + ); + } Ok(data) } @@ -130,9 +137,9 @@ async fn get(api: String, path: String) -> Result, Box Project { println!("!!!PROJECT"); let url = format!("project/{}", name); - let data = get(api, url); + let data = get(api, url).await.unwrap().unwrap(); - serde_json::from_slice(&data.await.unwrap()).unwrap() + serde_json::from_slice(&data).unwrap() } pub async fn projects(api: String, ids: Vec) -> Vec { @@ -142,9 +149,9 @@ pub async fn projects(api: String, ids: Vec) -> Vec { let url = format!(r#"projects?ids=["{}"]"#, all); //println!("{}", url); - let data = get(api, url); + let data = get(api, url).await.unwrap().unwrap(); - serde_json::from_slice(&data.await.unwrap()).unwrap() + serde_json::from_slice(&data).unwrap() } pub async fn versions(api: String, id: String, list: List) -> Vec { @@ -156,9 +163,14 @@ pub async fn versions(api: String, id: String, list: List) -> Vec { let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, list.mc_version); - let data = get(api, url); + let data = get(api, url).await.unwrap(); - serde_json::from_slice(&data.await.unwrap()).unwrap() + dbg!(&data); + + match data { + Some(data) => serde_json::from_slice(&data).unwrap(), + None => Vec::new(), + } } pub async fn get_raw_versions(api: String, versions: Vec) -> Vec { @@ -167,9 +179,9 @@ pub async fn get_raw_versions(api: String, versions: Vec) -> Vec) -> Result> { diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 8e39d11..ac23970 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,6 +1,6 @@ use std::io::{Error, ErrorKind}; -use crate::{modrinth::{project, versions, extract_current_version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list, download_versions}; +use crate::{modrinth::{project, versions, extract_current_version, Version}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, mods_get_all_ids, userlist_get_all_ids}, input::{Input, Subcmd}, get_current_list, download_versions}; pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { @@ -26,27 +26,41 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box let project = project(String::from(&config.apis.modrinth), &args[0]).await; let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; - - let current_id = extract_current_version(available_versions.clone())?; - - let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap(); - let file = current_version.clone().files.into_iter().find(|f| f.primary).unwrap().url; - let mut available_versions_vec: Vec = Vec::new(); - for ver in available_versions { - available_versions_vec.push(ver.id); + let current_version: Option; + let current_version_id: String; + let file: String; + if !available_versions.is_empty() { + let current_id = extract_current_version(available_versions.clone())?; + + current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); + + current_version_id = current_version.clone().unwrap().id; + + file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url; + + for ver in available_versions { + available_versions_vec.push(ver.id); + }; + } else { + println!("There's currently no mod version for your specified target"); + current_version = None; + current_version_id = String::from("NONE"); + file = String::from("NONE"); + available_versions_vec.push(String::from("NONE")); } + //add to current list and mod table match userlist_get_all_ids(config.clone(), current_list.clone().id) { Ok(mods) => { if mods.contains(&project.id) { return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } else { - userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version.id), available_versions_vec, file)?; + userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?; } }, - Err(..) => userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version.id), available_versions_vec, file)?, + Err(..) => userlist_insert(config.clone(), String::from(¤t_list.id), String::from(&project.id), String::from(¤t_version_id), available_versions_vec, file)?, }; match mods_get_all_ids(config.clone()) { @@ -62,7 +76,7 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box }, }; - if !input.disable_download { download_versions(current_list, vec![current_version]).await?; } + if !input.disable_download && current_version.is_some() { download_versions(current_list, vec![current_version.unwrap()]).await?; } Ok(()) } diff --git a/src/commands/update.rs b/src/commands/update.rs index c8f0880..be15cfa 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -86,10 +86,15 @@ async fn specific_update(config: Cfg, input: Input, list: List, project: Project let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; let mut versions: Vec = vec![]; - - for ver in &applicable_versions { - versions.push(String::from(&ver.id)); + + if !applicable_versions.is_empty() { + for ver in &applicable_versions { + versions.push(String::from(&ver.id)); + } + } else { + versions.push(String::from("NONE")); } + let mut current: Vec = vec![]; if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { -- cgit v1.2.3