From ff23a11e632812b685f594324e6004c6da81cd4d Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 5 Feb 2023 09:23:29 +0100 Subject: Fixed update shit not correctly updating --- src/commands/update.rs | 90 ++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 50 deletions(-) (limited to 'src/commands/update.rs') diff --git a/src/commands/update.rs b/src/commands/update.rs index d400a24..bc5b316 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,4 +1,4 @@ -use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}}; +use crate::{config::Cfg, modrinth::{versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_get_title, userlist_get_set_version}, List, input::Input, files::{delete_version, download_versions, disable_version, clean_list_dir}, error::{MLE, MLError, ErrorType}}; pub async fn update(config: Cfg, input: Input) -> MLE<()> { let mut liststack: Vec = vec![]; @@ -9,7 +9,7 @@ pub async fn update(config: Cfg, input: Input) -> MLE<()> { } } else { let current = get_current_list(config.clone())?; - println!("Check for updates of mods in list {}", current.id); + println!("Update list {}:", current.id); liststack.push(current) } cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await @@ -20,59 +20,41 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; let mut current_versions: Vec<(String, String)> = vec![]; + + println!(" └Update mods:"); + let mut updatestack: Vec = vec![]; + + for id in mods { + let title = mods_get_title(config.clone(), &id)?; + println!("\t└{}", title); - let mut versions = mods_get_versions(config.clone(), mods.clone())?; - versions.sort_by_key(|ver| ver.mod_id.clone()); - - let mut projects = projects(String::from(&config.apis.modrinth), mods).await; - projects.sort_by_key(|pro| pro.id.clone()); + if userlist_get_set_version(config.clone(), ¤t_list.id, &id)? { + println!("\t └Set version, skipping update"); + continue; + } - println!("Comparing mod versions:"); - let mut updatestack: Vec = vec![]; - for (index, project) in projects.into_iter().enumerate() { - //Get versions for project and check if they match up - let current_version = &versions[index]; - let p_id = String::from(&project.id); - let v_id = ¤t_version.mod_id; - if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) }; - - println!("\t({}) Check for update", project.title); - //Getting current installed version for disable or delete - let disable_version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&project.id))?; - - let version_db_string = project.versions.join("|"); + let disable_version = userlist_get_current_version(config.clone(), String::from(¤t_list.id), String::from(&id))?; - //Adding to stack if not the same versions in the list OR if clean == true - if clean || (version_db_string != current_version.versions) { - updatestack.push(match specific_update(config.clone(), clean, current_list.clone(), project.clone()).await { + updatestack.push( + match specific_update(config.clone(), clean, current_list.clone(), String::from(&id)).await { Ok(ver) => { - current_versions.push((disable_version, p_id)); + current_versions.push((disable_version, id)); ver }, Err(e) => { - //Catch no update available if e.to_string() == "Mod: NO_UPDATE_AVAILABLE" { - mods_change_versions(config.clone(), version_db_string, project.id)?; - println!("\t └No new version found for the specified minecraft version"); + println!("\t └No new version found for the specified minecraft version"); } else { return Err(e); }; continue; - }, - }); - } else { - println!("\t └No new version found"); - }; + } + } + ) }; - - //Linebreak readability - println!(""); - if clean { clean_list_dir(¤t_list)? }; - - //Linebreak readability - println!(""); + if clean { clean_list_dir(¤t_list)?; }; if direct_download && !updatestack.is_empty() { download_versions(current_list.clone(), config.clone(), updatestack).await?; @@ -81,10 +63,10 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d if !clean { for ver in current_versions { if delete_old { - println!("Deleting version {} for mod {}", ver.0, ver.1); + println!("\t └Delete version {}", ver.0); delete_version(current_list.clone(), ver.0)?; } else if ver.0 != "NONE" { - println!("Disabling version {} for mod {}", ver.0, ver.1); + println!("\t └Disable version {}", ver.0); disable_version(config.clone(), current_list.clone(), ver.0, ver.1)?; }; } @@ -95,8 +77,8 @@ pub async fn cmd_update(config: Cfg, liststack: Vec, clean: bool, direct_d Ok(()) } -async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) -> MLE { - let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; +async fn specific_update(config: Cfg, clean: bool, list: List, id: String) -> MLE { + let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&id), list.clone()).await; let mut versions: Vec = vec![]; @@ -108,12 +90,20 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) versions.push(String::from("NONE")); } - let mut current: Vec = vec![]; - if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { - //get new versions - println!("\t └Get versions for specified minecraft versions"); + //TODO Split clean and no match + if clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&id))?) { + let current_str = extract_current_version(applicable_versions.clone())?; + + if clean { + println!("\t └Add version to downloadstack"); + } else { + println!("\t └Get versions for specified minecraft versions"); + println!("\t └New current version: {}", current_str); + }; + + //get new versions let current_ver = match applicable_versions.into_iter().find(|ver| ver.id == current_str).ok_or("!no current version in applicable_versions") { Ok(v) => Ok(v), Err(e) => Err(MLError::new(ErrorType::Other, e)), @@ -124,7 +114,7 @@ async fn specific_update(config: Cfg, clean: bool, list: List, project: Project) Ok(p) => Ok(p), Err(e) => Err(MLError::new(ErrorType::Other, e)), }?.url; - userlist_change_versions(config, list.id, current_str, versions.join("|"), link, project.id)?; + userlist_change_versions(config, list.id, current_str, versions.join("|"), link, id)?; } if current.is_empty() { return Err(MLError::new(ErrorType::ModError, "NO_UPDATE_AVAILABLE")) }; @@ -139,7 +129,7 @@ async fn download_updates_test() { use crate::{modrinth::{Version, VersionFile, Hash, VersionType}, Modloader, List}; let config = Cfg::init("modlist.toml").unwrap(); - let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Forge, download_folder: String::from("./dl") }; + let current_list = List { id: String::from("..."), mc_version: String::from("..."), modloader: Modloader::Fabric, download_folder: String::from("./dev/tests/dl") }; let versions = vec![Version { id: "dEqtGnT9".to_string(), -- cgit v1.2.3