aboutsummaryrefslogtreecommitdiffstats
path: root/src/minecraft/downloads.rs
diff options
context:
space:
mode:
authorFilip Wandzio <contact@philw.dev>2026-01-22 23:14:08 +0100
committerFilip Wandzio <contact@philw.dev>2026-01-22 23:14:08 +0100
commit72ddd7b7704f2087a52c9c0552446682918c513b (patch)
treee5134f215ea82c1fc8eda17b34e426a7b1dfafc6 /src/minecraft/downloads.rs
downloaddml-72ddd7b7704f2087a52c9c0552446682918c513b.tar.gz
dml-72ddd7b7704f2087a52c9c0552446682918c513b.zip
Implement basic game files download logic
Implement core clap arguments Respect XDG_BASE_DIR Currently library extraction is broken because it assumes every instace has it's own library folder. This should be refactored so instances share libraries Signed-off-by: Filip Wandzio <contact@philw.dev>
Diffstat (limited to 'src/minecraft/downloads.rs')
-rw-r--r--src/minecraft/downloads.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/minecraft/downloads.rs b/src/minecraft/downloads.rs
new file mode 100644
index 0000000..5be5a05
--- /dev/null
+++ b/src/minecraft/downloads.rs
@@ -0,0 +1,66 @@
1use log::{debug, info};
2use tokio::{fs, io::AsyncWriteExt};
3
4use crate::{
5 config::Config,
6 errors::McError,
7 minecraft::manifests::{Library, Version},
8 platform::paths,
9};
10
11/// Download everything required to launch:
12/// - client jar
13/// - libraries
14pub async fn download_all(config: &Config, version: &Version) -> Result<(), McError> {
15 download_client(config, version).await?;
16 download_libraries(config, &version.libraries).await?;
17 Ok(())
18}
19
20async fn download_client(config: &Config, version: &Version) -> Result<(), McError> {
21 let jar_path = paths::client_jar(config, &version.id)?;
22
23 if jar_path.exists() {
24 debug!("Client jar already exists");
25 return Ok(());
26 }
27
28 info!("Downloading client {}", version.id);
29
30 download_file(&version.downloads.client.url, &jar_path).await
31}
32
33async fn download_libraries(config: &Config, libraries: &[Library]) -> Result<(), McError> {
34 for lib in libraries {
35 let Some(artifact) = &lib.downloads.artifact else {
36 continue;
37 };
38
39 let lib_path = paths::library_file(config, &artifact.path)?;
40
41 if lib_path.exists() {
42 continue;
43 }
44
45 info!("Downloading library {}", artifact.path);
46 download_file(&artifact.url, &lib_path).await?;
47 }
48
49 Ok(())
50}
51
52/* ---------------- helper ---------------- */
53
54async fn download_file(url: &str, path: &std::path::Path) -> Result<(), McError> {
55 if let Some(parent) = path.parent() {
56 fs::create_dir_all(parent).await?;
57 }
58
59 let response = reqwest::get(url).await?;
60 let bytes = response.bytes().await?;
61
62 let mut file = fs::File::create(path).await?;
63 file.write_all(&bytes).await?;
64
65 Ok(())
66}