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
|
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use crate::apis::modrinth::get_raw_versions;
use crate::{config::Cfg, List};
use crate::{
db::userlist_get_all_current_versions_with_mods,
error::{EType, MLErr, MLE},
files::{
clean_list_dir, delete_version, disable_version, download_versions,
get_downloaded_versions,
},
};
use crate::{PROGRESS_CHARS, STYLE_BAR_POS};
/// # Errors
pub async fn download(
config: &Cfg,
liststack: Vec<List>,
clean: bool,
delete_old: bool,
) -> MLE<()> {
let mp = MultiProgress::new();
let download_p = mp.add(ProgressBar::new(
liststack
.len()
.try_into()
.map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?,
));
download_p.set_style(
ProgressStyle::with_template(STYLE_BAR_POS)
.map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?
.progress_chars(PROGRESS_CHARS),
);
for current_list in liststack {
download_list(config, mp.clone(), download_p.clone(), current_list, clean, delete_old).await?;
}
download_p.finish_with_message("Downloaded all lists");
Ok(())
}
async fn download_list(
config: &Cfg,
mp: MultiProgress,
download_p: ProgressBar,
current_list: List,
clean: bool,
delete_old: bool,
) -> MLE<()> {
download_p.set_message(format!("Download in {}", current_list.id));
let downloaded_versions = get_downloaded_versions(¤t_list)?;
let current_version_ids = match userlist_get_all_current_versions_with_mods(
config,
¤t_list.id,
) {
Ok(i) => Ok(i),
Err(e) => Err(MLErr::new(EType::DBError, e.to_string().as_str())),
}?;
let mut to_download: Vec<String> = vec![];
//(mod_id, version_id)
let mut to_disable: Vec<(String, String)> = vec![];
for version in current_version_ids {
let mod_id = version.0;
let current_version = version.1;
let current_download = downloaded_versions.get(&mod_id);
if current_download.is_none() || clean {
to_download.push(current_version);
} else {
let downloaded_version =
current_download.ok_or(MLErr::new(EType::Other, "IDK, WTF"))?;
if ¤t_version != downloaded_version {
to_disable
.push((mod_id.clone(), String::from(downloaded_version)));
to_download.push(current_version);
}
}
}
if clean {
clean_list_dir(¤t_list)?;
};
if to_download.is_empty() {
download_p.println(format!(
"There are no new versions to download for {}",
current_list.id
));
} else {
download_versions(
current_list.clone(),
config.clone(),
get_raw_versions(&config.apis.modrinth, to_download).await?,
&mp,
&download_p,
)
.await?;
}
if !to_disable.is_empty() {
let d_p = mp.insert_before(
&download_p,
ProgressBar::new(
to_disable
.len()
.try_into()
.map_err(|_| MLErr::new(EType::Other, "ListStackLen"))?,
),
);
d_p.set_style(
ProgressStyle::with_template(STYLE_BAR_POS)
.map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?
.progress_chars(PROGRESS_CHARS),
);
for ver in to_disable {
if delete_old {
d_p.set_message(format!("Delete version {}", ver.1));
d_p.inc(1);
delete_version(¤t_list, &ver.1)?;
} else {
d_p.set_message(format!("Disable version {}", ver.1));
d_p.inc(1);
disable_version(config, ¤t_list, ver.1, ver.0)?;
};
}
let del_msg = if delete_old {
"Deleted all old versions"
} else {
"Disabled all old versions"
};
d_p.finish_with_message(del_msg);
}
download_p.inc(1);
Ok(())
}
|