1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use crate::{
config::structs::{ModIdentifier, Profile},
upgrade::mod_downloadable,
};
use reqwest::StatusCode;
type Result<T> = std::result::Result<T, Error>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("The developer of this mod has denied third party applications from downloading it")]
DistributionDenied,
#[error("The project/repository has already been added")]
AlreadyAdded,
#[error("The project/repository does not exist")]
DoesNotExist,
#[error("The project/repository is not compatible")]
Incompatible,
#[error("The project/repository is not a mod")]
NotAMod,
#[error("{}", .0)]
GitHubError(octocrab::Error),
#[error("{}", .0)]
ModrinthError(ferinth::Error),
#[error("{}", .0)]
CurseForgeError(furse::Error),
}
impl From<furse::Error> for Error {
fn from(err: furse::Error) -> Self {
if let furse::Error::ReqwestError(source) = &err {
if Some(StatusCode::NOT_FOUND) == source.status() {
Self::DoesNotExist
} else {
Self::CurseForgeError(err)
}
} else {
Self::CurseForgeError(err)
}
}
}
impl From<ferinth::Error> for Error {
fn from(err: ferinth::Error) -> Self {
if let ferinth::Error::ReqwestError(source) = &err {
if Some(StatusCode::NOT_FOUND) == source.status() {
Self::DoesNotExist
} else {
Self::ModrinthError(err)
}
} else {
Self::ModrinthError(err)
}
}
}
impl From<octocrab::Error> for Error {
fn from(err: octocrab::Error) -> Self {
if let octocrab::Error::Http { source, .. } = &err {
if Some(StatusCode::NOT_FOUND) == source.status() {
Self::DoesNotExist
} else {
Self::GitHubError(err)
}
} else {
Self::GitHubError(err)
}
}
}
pub async fn github(
repo_handler: &octocrab::repos::RepoHandler<'_>,
profile: &Profile,
should_check_game_version: Option<bool>,
should_check_mod_loader: Option<bool>,
) -> Result<(octocrab::models::Repository, octocrab::models::repos::Asset)> {
let repo = repo_handler.get().await?;
let repo_name = (
repo.owner.as_ref().unwrap().login.clone(),
repo.name.clone(),
);
if profile.mods.iter().any(|mod_| {
mod_.name.to_lowercase() == repo.name.to_lowercase()
|| ModIdentifier::GitHubRepository(repo_name.clone()) == mod_.identifier
}) {
return Err(Error::AlreadyAdded);
}
let releases = repo_handler.releases().list().send().await?.items;
let mut contains_jar_asset = false;
'outer: for release in &releases {
for asset in &release.assets {
if asset.name.contains("jar") {
contains_jar_asset = true;
break 'outer;
}
}
}
if contains_jar_asset {
let asset = mod_downloadable::get_latest_compatible_asset(
&releases,
if should_check_game_version == Some(false) {
None
} else {
Some(&profile.game_version)
},
if should_check_mod_loader == Some(false) {
None
} else {
Some(&profile.mod_loader)
},
)
.ok_or(Error::Incompatible)?
.0;
Ok((repo, asset))
} else {
Err(Error::NotAMod)
}
}
pub async fn modrinth(
modrinth: &ferinth::Ferinth,
project: &ferinth::structures::project::Project,
profile: &Profile,
should_check_game_version: Option<bool>,
should_check_mod_loader: Option<bool>,
) -> Result<ferinth::structures::version::Version> {
if profile.mods.iter().any(|mod_| {
mod_.name.to_lowercase() == project.title.to_lowercase()
|| ModIdentifier::ModrinthProject(project.id.clone()) == mod_.identifier
}) {
Err(Error::AlreadyAdded)
} else if project.project_type != ferinth::structures::project::ProjectType::Mod {
Err(Error::NotAMod)
} else {
let version = mod_downloadable::get_latest_compatible_version(
&modrinth.list_versions(&project.id).await?,
if should_check_game_version == Some(false) {
None
} else {
Some(&profile.game_version)
},
if should_check_mod_loader == Some(false) {
None
} else {
Some(&profile.mod_loader)
},
)
.ok_or(Error::Incompatible)?
.1;
Ok(version)
}
}
pub async fn curseforge(
curseforge: &furse::Furse,
project: &furse::structures::mod_structs::Mod,
profile: &Profile,
should_check_game_version: Option<bool>,
should_check_mod_loader: Option<bool>,
) -> Result<furse::structures::file_structs::File> {
if profile.mods.iter().any(|mod_| {
mod_.name.to_lowercase() == project.name.to_lowercase()
|| ModIdentifier::CurseForgeProject(project.id) == mod_.identifier
}) {
Err(Error::AlreadyAdded)
} else if Some(false) == project.allow_mod_distribution {
Err(Error::DistributionDenied)
} else if project.links.website_url.as_str().contains("mc-mods") {
let file = mod_downloadable::get_latest_compatible_file(
curseforge.get_mod_files(project.id).await?,
if should_check_game_version == Some(false) {
None
} else {
Some(&profile.game_version)
},
if should_check_mod_loader == Some(false) {
None
} else {
Some(&profile.mod_loader)
},
)
.ok_or(Error::Incompatible)?
.0;
Ok(file)
} else {
Err(Error::NotAMod)
}
}