From 8398b4003b72b3eefea11d8f71897ddb48f4b0ad Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sat, 13 May 2023 12:37:07 +0200 Subject: impl of set_version import --- src/commands/io.rs | 39 ++++++++++++++++++------- src/commands/modification.rs | 69 +++++++++++++++++++++++++++++++------------- src/commands/update.rs | 2 +- 3 files changed, 79 insertions(+), 31 deletions(-) (limited to 'src/commands') diff --git a/src/commands/io.rs b/src/commands/io.rs index e072f00..2bab132 100644 --- a/src/commands/io.rs +++ b/src/commands/io.rs @@ -4,9 +4,9 @@ use std::io::prelude::*; use crate::{ config::Cfg, - db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids}, + db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_set_version, userlist_get_all_ids, userlist_get_current_version}, error::MLE, - mod_add, IDSelector, List, Modloader, + mod_add, IDSelector, List, Modloader, AddMod, }; #[derive(Debug, Serialize, Deserialize)] @@ -14,10 +14,25 @@ struct Export { lists: Vec, } +#[derive(Debug, Serialize, Deserialize)] +struct ExportVersion { + version: String, + set: bool +} + +impl ExportVersion { + fn from(config: Cfg, list_id: &str, mod_id: &str) -> MLE { + Ok(Self { + version: userlist_get_current_version(config.clone(), list_id, mod_id)?, + set: userlist_get_set_version(config.clone(), list_id, mod_id)? + }) + } +} + #[derive(Debug, Serialize, Deserialize)] struct ExportList { id: String, - mods: String, + versions: Vec, launcher: String, mc_version: String, download_folder: Option, @@ -32,11 +47,15 @@ impl ExportList { dl_folder = Some(list.download_folder) }; - let mods = userlist_get_all_ids(config, list_id)?.join("|"); + let mods = userlist_get_all_ids(config.clone(), &list_id)?; + let mut versions = vec![]; + for m in mods { + versions.push(ExportVersion::from(config.clone(), &list_id, &m)?) + } Ok(Self { id: list.id, - mods, + versions, launcher: list.modloader.to_string(), mc_version: list.mc_version, download_folder: dl_folder, @@ -86,14 +105,14 @@ pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE list.modloader.clone(), String::from(&list.download_folder), )?; - let mods: Vec<&str> = exportlist.mods.split('|').collect(); - let mut mod_ids = vec![]; - for mod_id in mods { - mod_ids.push(IDSelector::ModificationID(String::from(mod_id))); + + let mut ver_ids = vec![]; + for id in exportlist.versions { + ver_ids.push(AddMod { id: IDSelector::VersionID(id.version), set_version: id.set} ); } //TODO impl set_version and good direct download //TODO impl all at once, dafuck ?done? - mod_add(config.clone(), mod_ids, list, direct_download, false).await?; + mod_add(config.clone(), ver_ids, list, direct_download).await?; } Ok(()) } diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 216a06e..ab42468 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,4 +1,4 @@ -use std::io::Write; +use std::{io::Write, collections::HashMap}; use crate::{ config::Cfg, @@ -12,6 +12,12 @@ use crate::{ List, }; +#[derive(Debug, Clone)] +pub struct AddMod { + pub id: IDSelector, + pub set_version: bool +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum IDSelector { ModificationID(String), @@ -26,26 +32,27 @@ pub struct ProjectInfo { pub current_version: Option, pub applicable_versions: Vec, pub download_link: String, + pub set_version: bool, } +//TODO fix setversion to per mod pub async fn mod_add( config: Cfg, - ids: Vec, + mods: Vec, list: List, direct_download: bool, - set_version: bool, ) -> MLE<()> { println!("Add mods to {}", list.id); println!(" └Add mods:"); - let mut mod_ids: Vec = Vec::new(); - let mut ver_ids: Vec = Vec::new(); + let mut mod_ids: Vec<(String, bool)> = Vec::new(); + let mut ver_ids: Vec<(String, bool)> = Vec::new(); //"Sort" project ids from version ids to be able to handle them differently but in a batch - for id in ids { - match id { - IDSelector::ModificationID(pid) => mod_ids.push(pid), - IDSelector::VersionID(vid) => ver_ids.push(vid), + for m in mods { + match m.id { + IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)), + IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)), } } @@ -83,7 +90,7 @@ pub async fn mod_add( ¤t_version_id, project.clone().applicable_versions, &project.download_link, - set_version, + project.set_version, ) { Err(e) => { let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); @@ -128,13 +135,23 @@ pub async fn mod_add( Ok(()) } -async fn get_mod_infos(config: Cfg, mod_ids: Vec, list: List) -> MLE> { +async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE> { + + let mut setmap: HashMap = HashMap::new(); + + let mut ids = vec![]; + + for id in mod_ids { + setmap.insert(id.0.to_string(), id.1); + ids.push(id.0); + } + let mut projectinfo: Vec = Vec::new(); //Get required information from mod_ids - let m_projects = match mod_ids.len() { - 1 => vec![project(&config.apis.modrinth, &mod_ids[0]).await], - 2.. => projects(&config.apis.modrinth, mod_ids).await, + let m_projects = match ids.len() { + 1 => vec![project(&config.apis.modrinth, &ids[0]).await], + 2.. => projects(&config.apis.modrinth, ids).await, _ => panic!("PANIC"), }; for project in m_projects { @@ -180,12 +197,13 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec, list: List) -> MLE, list: List) -> MLE, list: List) -> MLE) -> MLE> { +async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE> { + + let mut setmap: HashMap = HashMap::new(); + + let mut ids = vec![]; + + for id in ver_ids { + setmap.insert(id.0.to_string(), id.1); + ids.push(id.0); + } let mut projectinfo: Vec = Vec::new(); //Get required information from ver_ids - let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_ids).await; + let mut v_versions = get_raw_versions(&config.apis.modrinth, ids).await; let mut v_mod_ids: Vec = Vec::new(); for ver in v_versions.clone() { v_mod_ids.push(ver.project_id); @@ -230,12 +258,13 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec) -> MLE .unwrap() .url; projectinfo.push(ProjectInfo { - mod_id: project.id, + mod_id: String::from(&project.id), slug: project.slug, title: project.title, current_version: Some(version.clone()), applicable_versions: vec![String::from(&version.id)], download_link: file, + set_version: setmap.get(&project.id).unwrap().clone(), }) } Ok(projectinfo) @@ -281,7 +310,7 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> { // Remove mod from main list if not used elsewhere let mut mod_used = false; for id in list_ids { - let mods = match userlist_get_all_ids(config.clone(), id) { + let mods = match userlist_get_all_ids(config.clone(), &id) { Ok(m) => m, Err(err) => { if err.to_string() == "Database: NO_MODS_USERLIST" { diff --git a/src/commands/update.rs b/src/commands/update.rs index 1fe664e..2128090 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -19,7 +19,7 @@ pub async fn update( ) -> MLE<()> { for current_list in liststack { println!("Update mods in {}", current_list.id); - let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; + let mods = userlist_get_all_ids(config.clone(), ¤t_list.id)?; let mut current_versions: Vec<(String, String)> = vec![]; -- cgit v1.2.3