summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2022-11-29 22:59:19 +0100
committerFxQnLr <[email protected]>2022-11-29 22:59:19 +0100
commit575d2493e8e5747bf65321f7277e52211d73e387 (patch)
tree11b20e371316047110ffd36d1fe5bfefd2183c07
parentddde9204c72dd867f920f07f6483be03dda7cf68 (diff)
downloadmodlist-575d2493e8e5747bf65321f7277e52211d73e387.tar
modlist-575d2493e8e5747bf65321f7277e52211d73e387.tar.gz
modlist-575d2493e8e5747bf65321f7277e52211d73e387.zip
fixed mod without matching specific version
-rw-r--r--data.dbbin24576 -> 24576 bytes
-rw-r--r--src/apis/modrinth.rs40
-rw-r--r--src/commands/modification.rs38
-rw-r--r--src/commands/update.rs11
4 files changed, 60 insertions, 29 deletions
diff --git a/data.db b/data.db
index 9f6316d..51f2ff4 100644
--- a/data.db
+++ b/data.db
Binary files 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 {
111 pub sha1: String, 111 pub sha1: String,
112} 112}
113 113
114async fn get(api: String, path: String) -> Result<Vec<u8>, Box<dyn std::error::Error>> { 114async fn get(api: String, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> {
115 let url = format!(r#"{}{}"#, api, path); 115 let url = format!(r#"{}{}"#, api, path);
116 116
117 let client = Client::builder() 117 let client = Client::builder()
118 .user_agent(format!("fxqnlr/modlistcli/{} ([email protected])", env!("CARGO_PKG_VERSION"))) 118 .user_agent(format!("fxqnlr/modlistcli/{} ([email protected])", env!("CARGO_PKG_VERSION")))
119 .build()?; 119 .build()?;
120 let data = client.get(url) 120 let res = client.get(url)
121 .send() 121 .send()
122 .await? 122 .await?;
123 .bytes() 123
124 .await? 124 let mut data: Option<Vec<u8>> = None;
125 .to_vec(); 125
126 if res.status() == 200 {
127 data = Some(res
128 .bytes()
129 .await?
130 .to_vec()
131 );
132 }
126 133
127 Ok(data) 134 Ok(data)
128} 135}
@@ -130,9 +137,9 @@ async fn get(api: String, path: String) -> Result<Vec<u8>, Box<dyn std::error::E
130pub async fn project(api: String, name: &str) -> Project { 137pub async fn project(api: String, name: &str) -> Project {
131 println!("!!!PROJECT"); 138 println!("!!!PROJECT");
132 let url = format!("project/{}", name); 139 let url = format!("project/{}", name);
133 let data = get(api, url); 140 let data = get(api, url).await.unwrap().unwrap();
134 141
135 serde_json::from_slice(&data.await.unwrap()).unwrap() 142 serde_json::from_slice(&data).unwrap()
136} 143}
137 144
138pub async fn projects(api: String, ids: Vec<String>) -> Vec<Project> { 145pub async fn projects(api: String, ids: Vec<String>) -> Vec<Project> {
@@ -142,9 +149,9 @@ pub async fn projects(api: String, ids: Vec<String>) -> Vec<Project> {
142 let url = format!(r#"projects?ids=["{}"]"#, all); 149 let url = format!(r#"projects?ids=["{}"]"#, all);
143 //println!("{}", url); 150 //println!("{}", url);
144 151
145 let data = get(api, url); 152 let data = get(api, url).await.unwrap().unwrap();
146 153
147 serde_json::from_slice(&data.await.unwrap()).unwrap() 154 serde_json::from_slice(&data).unwrap()
148} 155}
149 156
150pub async fn versions(api: String, id: String, list: List) -> Vec<Version> { 157pub async fn versions(api: String, id: String, list: List) -> Vec<Version> {
@@ -156,9 +163,14 @@ pub async fn versions(api: String, id: String, list: List) -> Vec<Version> {
156 163
157 let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, list.mc_version); 164 let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, list.mc_version);
158 165
159 let data = get(api, url); 166 let data = get(api, url).await.unwrap();
160 167
161 serde_json::from_slice(&data.await.unwrap()).unwrap() 168 dbg!(&data);
169
170 match data {
171 Some(data) => serde_json::from_slice(&data).unwrap(),
172 None => Vec::new(),
173 }
162} 174}
163 175
164pub async fn get_raw_versions(api: String, versions: Vec<String>) -> Vec<Version> { 176pub async fn get_raw_versions(api: String, versions: Vec<String>) -> Vec<Version> {
@@ -167,9 +179,9 @@ pub async fn get_raw_versions(api: String, versions: Vec<String>) -> Vec<Version
167 179
168 let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); 180 let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#));
169 181
170 let data = get(api, url).await; 182 let data = get(api, url).await.unwrap().unwrap();
171 183
172 serde_json::from_slice(&data.unwrap()).unwrap() 184 serde_json::from_slice(&data).unwrap()
173} 185}
174 186
175pub fn extract_current_version(versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { 187pub fn extract_current_version(versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> {
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 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use 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}; 3use 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};
4 4
5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5pub async fn modification(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
6 6
@@ -26,27 +26,41 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>
26 let project = project(String::from(&config.apis.modrinth), &args[0]).await; 26 let project = project(String::from(&config.apis.modrinth), &args[0]).await;
27 27
28 let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await; 28 let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), current_list.clone()).await;
29
30 let current_id = extract_current_version(available_versions.clone())?;
31
32 let current_version = available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap();
33 29
34 let file = current_version.clone().files.into_iter().find(|f| f.primary).unwrap().url;
35
36 let mut available_versions_vec: Vec<String> = Vec::new(); 30 let mut available_versions_vec: Vec<String> = Vec::new();
37 for ver in available_versions { 31 let current_version: Option<Version>;
38 available_versions_vec.push(ver.id); 32 let current_version_id: String;
33 let file: String;
34 if !available_versions.is_empty() {
35 let current_id = extract_current_version(available_versions.clone())?;
36
37 current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap());
38
39 current_version_id = current_version.clone().unwrap().id;
40
41 file = current_version.clone().ok_or("VERSION_CORRUPTED")?.files.into_iter().find(|f| f.primary).unwrap().url;
42
43 for ver in available_versions {
44 available_versions_vec.push(ver.id);
45 };
46 } else {
47 println!("There's currently no mod version for your specified target");
48 current_version = None;
49 current_version_id = String::from("NONE");
50 file = String::from("NONE");
51 available_versions_vec.push(String::from("NONE"));
39 } 52 }
53
40 //add to current list and mod table 54 //add to current list and mod table
41 match userlist_get_all_ids(config.clone(), current_list.clone().id) { 55 match userlist_get_all_ids(config.clone(), current_list.clone().id) {
42 Ok(mods) => { 56 Ok(mods) => {
43 if mods.contains(&project.id) { 57 if mods.contains(&project.id) {
44 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); } 58 return Err(Box::new(Error::new(ErrorKind::Other, "MOD_ALREADY_ON_LIST"))); }
45 else { 59 else {
46 userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version.id), available_versions_vec, file)?; 60 userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?;
47 } 61 }
48 }, 62 },
49 Err(..) => userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version.id), available_versions_vec, file)?, 63 Err(..) => userlist_insert(config.clone(), String::from(&current_list.id), String::from(&project.id), String::from(&current_version_id), available_versions_vec, file)?,
50 }; 64 };
51 65
52 match mods_get_all_ids(config.clone()) { 66 match mods_get_all_ids(config.clone()) {
@@ -62,7 +76,7 @@ async fn add(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>
62 }, 76 },
63 }; 77 };
64 78
65 if !input.disable_download { download_versions(current_list, vec![current_version]).await?; } 79 if !input.disable_download && current_version.is_some() { download_versions(current_list, vec![current_version.unwrap()]).await?; }
66 80
67 Ok(()) 81 Ok(())
68} 82}
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
86 let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; 86 let applicable_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await;
87 87
88 let mut versions: Vec<String> = vec![]; 88 let mut versions: Vec<String> = vec![];
89 89
90 for ver in &applicable_versions { 90 if !applicable_versions.is_empty() {
91 versions.push(String::from(&ver.id)); 91 for ver in &applicable_versions {
92 versions.push(String::from(&ver.id));
93 }
94 } else {
95 versions.push(String::from("NONE"));
92 } 96 }
97
93 98
94 let mut current: Vec<Version> = vec![]; 99 let mut current: Vec<Version> = vec![];
95 if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) { 100 if input.clean || (versions.join("|") != userlist_get_applicable_versions(config.clone(), String::from(&list.id), String::from(&project.id))?) {