summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/io.rs39
-rw-r--r--src/commands/modification.rs69
-rw-r--r--src/commands/update.rs2
3 files changed, 79 insertions, 31 deletions
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::*;
4 4
5use crate::{ 5use crate::{
6 config::Cfg, 6 config::Cfg,
7 db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_all_ids}, 7 db::{lists_get, lists_get_all_ids, lists_insert, userlist_get_set_version, userlist_get_all_ids, userlist_get_current_version},
8 error::MLE, 8 error::MLE,
9 mod_add, IDSelector, List, Modloader, 9 mod_add, IDSelector, List, Modloader, AddMod,
10}; 10};
11 11
12#[derive(Debug, Serialize, Deserialize)] 12#[derive(Debug, Serialize, Deserialize)]
@@ -15,9 +15,24 @@ struct Export {
15} 15}
16 16
17#[derive(Debug, Serialize, Deserialize)] 17#[derive(Debug, Serialize, Deserialize)]
18struct ExportVersion {
19 version: String,
20 set: bool
21}
22
23impl ExportVersion {
24 fn from(config: Cfg, list_id: &str, mod_id: &str) -> MLE<Self> {
25 Ok(Self {
26 version: userlist_get_current_version(config.clone(), list_id, mod_id)?,
27 set: userlist_get_set_version(config.clone(), list_id, mod_id)?
28 })
29 }
30}
31
32#[derive(Debug, Serialize, Deserialize)]
18struct ExportList { 33struct ExportList {
19 id: String, 34 id: String,
20 mods: String, 35 versions: Vec<ExportVersion>,
21 launcher: String, 36 launcher: String,
22 mc_version: String, 37 mc_version: String,
23 download_folder: Option<String>, 38 download_folder: Option<String>,
@@ -32,11 +47,15 @@ impl ExportList {
32 dl_folder = Some(list.download_folder) 47 dl_folder = Some(list.download_folder)
33 }; 48 };
34 49
35 let mods = userlist_get_all_ids(config, list_id)?.join("|"); 50 let mods = userlist_get_all_ids(config.clone(), &list_id)?;
51 let mut versions = vec![];
52 for m in mods {
53 versions.push(ExportVersion::from(config.clone(), &list_id, &m)?)
54 }
36 55
37 Ok(Self { 56 Ok(Self {
38 id: list.id, 57 id: list.id,
39 mods, 58 versions,
40 launcher: list.modloader.to_string(), 59 launcher: list.modloader.to_string(),
41 mc_version: list.mc_version, 60 mc_version: list.mc_version,
42 download_folder: dl_folder, 61 download_folder: dl_folder,
@@ -86,14 +105,14 @@ pub async fn import(config: Cfg, file_str: String, direct_download: bool) -> MLE
86 list.modloader.clone(), 105 list.modloader.clone(),
87 String::from(&list.download_folder), 106 String::from(&list.download_folder),
88 )?; 107 )?;
89 let mods: Vec<&str> = exportlist.mods.split('|').collect(); 108
90 let mut mod_ids = vec![]; 109 let mut ver_ids = vec![];
91 for mod_id in mods { 110 for id in exportlist.versions {
92 mod_ids.push(IDSelector::ModificationID(String::from(mod_id))); 111 ver_ids.push(AddMod { id: IDSelector::VersionID(id.version), set_version: id.set} );
93 } 112 }
94 //TODO impl set_version and good direct download 113 //TODO impl set_version and good direct download
95 //TODO impl all at once, dafuck ?done? 114 //TODO impl all at once, dafuck ?done?
96 mod_add(config.clone(), mod_ids, list, direct_download, false).await?; 115 mod_add(config.clone(), ver_ids, list, direct_download).await?;
97 } 116 }
98 Ok(()) 117 Ok(())
99} 118}
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 @@
1use std::io::Write; 1use std::{io::Write, collections::HashMap};
2 2
3use crate::{ 3use crate::{
4 config::Cfg, 4 config::Cfg,
@@ -12,6 +12,12 @@ use crate::{
12 List, 12 List,
13}; 13};
14 14
15#[derive(Debug, Clone)]
16pub struct AddMod {
17 pub id: IDSelector,
18 pub set_version: bool
19}
20
15#[derive(Debug, Clone, PartialEq, Eq)] 21#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum IDSelector { 22pub enum IDSelector {
17 ModificationID(String), 23 ModificationID(String),
@@ -26,26 +32,27 @@ pub struct ProjectInfo {
26 pub current_version: Option<Version>, 32 pub current_version: Option<Version>,
27 pub applicable_versions: Vec<String>, 33 pub applicable_versions: Vec<String>,
28 pub download_link: String, 34 pub download_link: String,
35 pub set_version: bool,
29} 36}
30 37
38//TODO fix setversion to per mod
31pub async fn mod_add( 39pub async fn mod_add(
32 config: Cfg, 40 config: Cfg,
33 ids: Vec<IDSelector>, 41 mods: Vec<AddMod>,
34 list: List, 42 list: List,
35 direct_download: bool, 43 direct_download: bool,
36 set_version: bool,
37) -> MLE<()> { 44) -> MLE<()> {
38 println!("Add mods to {}", list.id); 45 println!("Add mods to {}", list.id);
39 println!(" └Add mods:"); 46 println!(" └Add mods:");
40 47
41 let mut mod_ids: Vec<String> = Vec::new(); 48 let mut mod_ids: Vec<(String, bool)> = Vec::new();
42 let mut ver_ids: Vec<String> = Vec::new(); 49 let mut ver_ids: Vec<(String, bool)> = Vec::new();
43 50
44 //"Sort" project ids from version ids to be able to handle them differently but in a batch 51 //"Sort" project ids from version ids to be able to handle them differently but in a batch
45 for id in ids { 52 for m in mods {
46 match id { 53 match m.id {
47 IDSelector::ModificationID(pid) => mod_ids.push(pid), 54 IDSelector::ModificationID(pid) => mod_ids.push((pid, m.set_version)),
48 IDSelector::VersionID(vid) => ver_ids.push(vid), 55 IDSelector::VersionID(vid) => ver_ids.push((vid, m.set_version)),
49 } 56 }
50 } 57 }
51 58
@@ -83,7 +90,7 @@ pub async fn mod_add(
83 &current_version_id, 90 &current_version_id,
84 project.clone().applicable_versions, 91 project.clone().applicable_versions,
85 &project.download_link, 92 &project.download_link,
86 set_version, 93 project.set_version,
87 ) { 94 ) {
88 Err(e) => { 95 Err(e) => {
89 let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); 96 let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id);
@@ -128,13 +135,23 @@ pub async fn mod_add(
128 Ok(()) 135 Ok(())
129} 136}
130 137
131async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec<ProjectInfo>> { 138async fn get_mod_infos(config: Cfg, mod_ids: Vec<(String, bool)>, list: List) -> MLE<Vec<ProjectInfo>> {
139
140 let mut setmap: HashMap<String, bool> = HashMap::new();
141
142 let mut ids = vec![];
143
144 for id in mod_ids {
145 setmap.insert(id.0.to_string(), id.1);
146 ids.push(id.0);
147 }
148
132 let mut projectinfo: Vec<ProjectInfo> = Vec::new(); 149 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
133 150
134 //Get required information from mod_ids 151 //Get required information from mod_ids
135 let m_projects = match mod_ids.len() { 152 let m_projects = match ids.len() {
136 1 => vec![project(&config.apis.modrinth, &mod_ids[0]).await], 153 1 => vec![project(&config.apis.modrinth, &ids[0]).await],
137 2.. => projects(&config.apis.modrinth, mod_ids).await, 154 2.. => projects(&config.apis.modrinth, ids).await,
138 _ => panic!("PANIC"), 155 _ => panic!("PANIC"),
139 }; 156 };
140 for project in m_projects { 157 for project in m_projects {
@@ -180,12 +197,13 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec
180 } 197 }
181 198
182 projectinfo.push(ProjectInfo { 199 projectinfo.push(ProjectInfo {
183 mod_id: project.id, 200 mod_id: String::from(&project.id),
184 slug: project.slug, 201 slug: project.slug,
185 title: project.title, 202 title: project.title,
186 current_version, 203 current_version,
187 applicable_versions: available_versions_vec, 204 applicable_versions: available_versions_vec,
188 download_link: file, 205 download_link: file,
206 set_version: setmap.get(&project.id).unwrap().clone(),
189 }) 207 })
190 } else { 208 } else {
191 println!("\t └There's currently no mod version for your specified target"); 209 println!("\t └There's currently no mod version for your specified target");
@@ -193,12 +211,13 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec
193 file = String::from("NONE"); 211 file = String::from("NONE");
194 available_versions_vec.push(String::from("NONE")); 212 available_versions_vec.push(String::from("NONE"));
195 projectinfo.push(ProjectInfo { 213 projectinfo.push(ProjectInfo {
196 mod_id: project.id, 214 mod_id: String::from(&project.id),
197 slug: project.slug, 215 slug: project.slug,
198 title: project.title, 216 title: project.title,
199 current_version, 217 current_version,
200 applicable_versions: available_versions_vec, 218 applicable_versions: available_versions_vec,
201 download_link: file, 219 download_link: file,
220 set_version: setmap.get(&project.id).unwrap().clone(),
202 }) 221 })
203 } 222 }
204 } 223 }
@@ -206,11 +225,20 @@ async fn get_mod_infos(config: Cfg, mod_ids: Vec<String>, list: List) -> MLE<Vec
206 Ok(projectinfo) 225 Ok(projectinfo)
207} 226}
208 227
209async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>> { 228async fn get_ver_info(config: Cfg, ver_ids: Vec<(String, bool)>) -> MLE<Vec<ProjectInfo>> {
229
230 let mut setmap: HashMap<String, bool> = HashMap::new();
231
232 let mut ids = vec![];
233
234 for id in ver_ids {
235 setmap.insert(id.0.to_string(), id.1);
236 ids.push(id.0);
237 }
210 let mut projectinfo: Vec<ProjectInfo> = Vec::new(); 238 let mut projectinfo: Vec<ProjectInfo> = Vec::new();
211 239
212 //Get required information from ver_ids 240 //Get required information from ver_ids
213 let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_ids).await; 241 let mut v_versions = get_raw_versions(&config.apis.modrinth, ids).await;
214 let mut v_mod_ids: Vec<String> = Vec::new(); 242 let mut v_mod_ids: Vec<String> = Vec::new();
215 for ver in v_versions.clone() { 243 for ver in v_versions.clone() {
216 v_mod_ids.push(ver.project_id); 244 v_mod_ids.push(ver.project_id);
@@ -230,12 +258,13 @@ async fn get_ver_info(config: Cfg, ver_ids: Vec<String>) -> MLE<Vec<ProjectInfo>
230 .unwrap() 258 .unwrap()
231 .url; 259 .url;
232 projectinfo.push(ProjectInfo { 260 projectinfo.push(ProjectInfo {
233 mod_id: project.id, 261 mod_id: String::from(&project.id),
234 slug: project.slug, 262 slug: project.slug,
235 title: project.title, 263 title: project.title,
236 current_version: Some(version.clone()), 264 current_version: Some(version.clone()),
237 applicable_versions: vec![String::from(&version.id)], 265 applicable_versions: vec![String::from(&version.id)],
238 download_link: file, 266 download_link: file,
267 set_version: setmap.get(&project.id).unwrap().clone(),
239 }) 268 })
240 } 269 }
241 Ok(projectinfo) 270 Ok(projectinfo)
@@ -281,7 +310,7 @@ pub fn mod_remove(config: Cfg, id: &str, list: List) -> MLE<()> {
281 // Remove mod from main list if not used elsewhere 310 // Remove mod from main list if not used elsewhere
282 let mut mod_used = false; 311 let mut mod_used = false;
283 for id in list_ids { 312 for id in list_ids {
284 let mods = match userlist_get_all_ids(config.clone(), id) { 313 let mods = match userlist_get_all_ids(config.clone(), &id) {
285 Ok(m) => m, 314 Ok(m) => m,
286 Err(err) => { 315 Err(err) => {
287 if err.to_string() == "Database: NO_MODS_USERLIST" { 316 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(
19) -> MLE<()> { 19) -> MLE<()> {
20 for current_list in liststack { 20 for current_list in liststack {
21 println!("Update mods in {}", current_list.id); 21 println!("Update mods in {}", current_list.id);
22 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; 22 let mods = userlist_get_all_ids(config.clone(), &current_list.id)?;
23 23
24 let mut current_versions: Vec<(String, String)> = vec![]; 24 let mut current_versions: Vec<(String, String)> = vec![];
25 25