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
use std::fs;
use log::{debug, trace};
use crate::{error::ThermiteError, LocalIndex, Mod, ModVersion};
use super::{actions, Ctx};
pub async fn update(
ctx: &mut Ctx,
outdated: &[ModVersion],
target: &mut LocalIndex,
) -> Result<(), ThermiteError> {
let mut downloaded = vec![];
for base in outdated {
let name = &base.name;
let url = &base.url;
let path = ctx
.dirs
.cache_dir()
.join(format!("{}_{}.zip", name, base.version));
match actions::download_file(url, path).await {
Ok(f) => downloaded.push(f),
Err(e) => eprintln!("{}", e),
}
}
for f in downloaded.into_iter() {
let mut pkg = actions::install_mod(&f, target.path().as_ref()).unwrap();
ctx.cache.clean(&pkg.package_name, &pkg.version)?;
let dir = target.parent_dir();
target.mods.entry(pkg.package_name).and_modify(|inst| {
inst.version = pkg.version;
inst.mods
.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
pkg.mods
.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
for (curr, new) in inst.mods.iter().zip(pkg.mods.iter()) {
trace!("current mod: {:#?} | new mod: {:#?}", curr, new);
if curr.disabled() {
fs::remove_dir_all(dir.join(&curr.path)).unwrap();
debug!(
"Moving mod from {} to {}",
new.path.display(),
curr.path.display()
);
fs::rename(dir.join(&new.path), dir.join(&curr.path)).unwrap_or_else(|e| {
debug!("Unable to move sub-mod to old path");
debug!("{}", e);
});
}
}
debug!("Updated {}", inst.package_name);
});
}
Ok(())
}
pub async fn get_outdated(index: &[Mod], target: &LocalIndex) -> Vec<ModVersion> {
index
.iter()
.filter_map(|e| {
if target
.mods
.iter()
.any(|(n, i)| n.trim() == e.name.trim() && i.version.trim() != e.latest.trim())
{
e.versions.get(&e.latest)
} else {
None
}
})
.cloned()
.collect()
}