From ddde9204c72dd867f920f07f6483be03dda7cf68 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 28 Nov 2022 22:55:14 +0100 Subject: basically update impl; added "good" download; auto dl on new mod; db to 0.4; etc --- src/commands/download.rs | 17 +++++++++++ src/commands/modification.rs | 25 +++++++++-------- src/commands/setup.rs | 12 ++++++-- src/commands/update.rs | 67 +++++++++++++++++++++++++++++++------------- 4 files changed, 89 insertions(+), 32 deletions(-) (limited to 'src/commands') diff --git a/src/commands/download.rs b/src/commands/download.rs index 13ba0e1..b0efdc2 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,3 +1,4 @@ +use crate::{modrinth::Version, files::download_file}; #[allow(unused_imports)] use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; @@ -32,3 +33,19 @@ async fn download_links(_config: Cfg, _input: Input, _current_list: List, _links Ok(String::new()) } + +pub async fn download_versions(current_list: List, versions: Vec) -> Result> { + + let dl_path = String::from(¤t_list.download_folder); + + for ver in versions { + let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); + let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); + let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; + let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); + download_file(primary_file.url, current_list.clone().download_folder, filename).await?; + } + + Ok(dl_path) +} + diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 519a0cb..8e39d11 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,12 +1,12 @@ 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}; +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}; pub async fn modification(config: Cfg, input: Input) -> Result<(), Box> { - match input.subcommand.ok_or("")? { + match input.subcommand.as_ref().ok_or("")? { Subcmd::Add => { - add(config, input.args.ok_or("")?).await + add(config, input).await }, Subcmd::Remove => { remove(config, input.args.ok_or("")?) @@ -15,23 +15,23 @@ pub async fn modification(config: Cfg, input: Input) -> Result<(), Box) -> Result<(), Box> { - //TODO! DO NOT PANIC IF MOD IS ALREADY IN MODS DB +async fn add(config: Cfg, input: Input) -> Result<(), Box> { + + let args = input.args.ok_or("")?; + if args.is_empty() { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))); }; let current_list = get_current_list(config.clone())?; let project = project(String::from(&config.apis.modrinth), &args[0]).await; - if project.versions.is_empty() { panic!("This should never happen"); }; - 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.files.into_iter().find(|f| f.primary).unwrap().url; + 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 { @@ -43,16 +43,16 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box userlist_insert(config.clone(), current_list.id, String::from(&project.id), current_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()) { Ok(mods) => { if mods.contains(&project.id) { - return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) + //return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_IN_DATABASE"))) } else { mods_insert(config.clone(), String::from(&project.id), String::from(&project.title), project.versions)?; } @@ -61,6 +61,9 @@ async fn add(config: Cfg, args: Vec) -> Result<(), Box Result<(), Box> { Ok(ver) => { match ver.as_str() { "0.2" => to_03(config)?, + "0.3" => to_04(config)?, _ => return Err(Box::new(Error::new(ErrorKind::Other, "UNKNOWN_VERSION"))) } }, @@ -33,7 +34,7 @@ async fn to_02(config: Cfg) -> Result<(), Box> { for list in lists { println!("Updating {}", list); - s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::from("TEXT"))?; + s_insert_column(config.clone(), String::from(&list), String::from("current_download"), String::from("TEXT"), None)?; let full_list = lists_get(config.clone(), String::from(&list))?; @@ -53,6 +54,13 @@ async fn to_02(config: Cfg) -> Result<(), Box> { } fn to_03(config: Cfg) -> Result<(), Box> { - s_insert_column(config.clone(), String::from("lists"), String::from("download_folder"), String::from("TEXT"))?; + s_insert_column(config.clone(), String::from("lists"), String::from("download_folder"), String::from("TEXT"), None)?; s_config_update_version(config, String::from("0.3")) } + +fn to_04(config: Cfg) -> Result<(), Box> { + for list_id in lists_get_all_ids(config.clone())? { + s_insert_column(config.clone(), list_id, String::from("disabled_versions"), String::from("TEXT"), Some(String::from("NONE")))?; + } + s_config_update_version(config, String::from("0.4")) +} diff --git a/src/commands/update.rs b/src/commands/update.rs index 42d19aa..c8f0880 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,9 +1,9 @@ -use std::io::{Error, ErrorKind}; +use std::{io::{Error, ErrorKind}, fs::{rename, remove_file}}; -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}, List, input::Input, download_file}; +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, userlist_add_disabled_versions, mods_change_versions}, List, input::Input, files::get_file_path, download_versions}; pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { - + let mut liststack: Vec = vec![]; if input.all_lists { let list_ids = lists_get_all_ids(config.clone())?; @@ -11,18 +11,22 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box = vec![]; 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()); - + let mut updatestack: Vec = vec![]; for (index, project) in projects.into_iter().enumerate() { //Get versions for project and check if they match up @@ -30,14 +34,26 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box ver, + Ok(ver) => { + current_versions.push((disable_version, p_id)); + ver + }, //TODO handle errors (only continue on "NO_UPDATE_AVAILABLE") - Err(_) => { println!("({}) No new version found for the specified minecraft version", project.title); continue; }, + Err(e) => { + //Updating versions in modlist for no repeating version calls + mods_change_versions(config.clone(), version_db_string, project.id)?; + println!("({}) No new version found for the specified minecraft version({})", project.title, e); + continue; + }, }); } else { println!("({}) No new version found", project.title); @@ -53,14 +69,19 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box Result> { - print!("Checking update for '{}' in {}", project.title, list.id); + println!("Checking update for '{}' in {}", project.title, list.id); let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; @@ -88,17 +109,25 @@ async fn specific_update(config: Cfg, input: Input, list: List, project: Project Ok(current[0].clone()) } -async fn download_updates(current_list: List, versions: Vec) -> Result> { +fn disable_old(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box> { + println!("Disabling version {} for mod {}", versionid, mod_id); + let file = get_file_path(current_list.clone(), String::from(&versionid))?; + let disabled = format!("{}.disabled", file); - let dl_path = String::from(¤t_list.download_folder); + rename(file, disabled)?; - for ver in versions { - let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); - download_file(primary_file.url, current_list.clone().download_folder, primary_file.filename).await?; - } + userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?; + + Ok(()) +} +fn delete_old(current_list: List, versionid: String, mod_id: String) -> Result<(), Box> { + println!("Deleting version {} for mod {}", versionid, mod_id); + let file = get_file_path(current_list, String::from(&versionid))?; + + remove_file(file)?; - Ok(dl_path) + Ok(()) } #[tokio::test] @@ -138,5 +167,5 @@ async fn download_updates_test() { "fabric".to_string() ] }]; - assert!(download_updates(current_list, versions).await.is_ok()) + assert!(download_versions(current_list, versions).await.is_ok()) } -- cgit v1.2.3