Downloading

This commit is contained in:
Michael Wain 2025-03-14 04:08:03 +03:00
parent fa10392184
commit 859a3902aa
6 changed files with 1022 additions and 836 deletions

1800
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,10 +7,10 @@ edition = "2024"
winit = "0.30.9"
wry = "0.50.4"
tokio = { version = "1", features = ["full"] }
futures = "0.3.31"
dirs = "6.0.0"
rand = "0.9.0"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0"
toml = "0.8.20"
reqwest = { version = "0.12.14", features = ["json"] }
manic = "0.8.1"
surf = { version = "2.3.2", features = ["hyper-client"] }

View File

@ -1,7 +1,7 @@
use core::str;
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::mpsc::{Sender, UnboundedReceiver, UnboundedSender};
use crate::{config::LauncherConfig, minecraft::versions::VersionConfig, util};
#[derive(Default)]

View File

@ -111,21 +111,17 @@ pub mod versions {
}
pub async fn fetch_versions_list() -> Result<VersionManifest, Box<dyn Error + Send + Sync>> {
let resp: VersionManifest = reqwest::get("https://piston-meta.mojang.com/mc/game/version_manifest_v2.json")
.await?
.json()
.await?;
Ok(resp)
let mut r = surf::get("https://piston-meta.mojang.com/mc/game/version_manifest_v2.json").await?;
let resp = r.body_bytes().await.unwrap();
let m = serde_json::from_slice(&resp)?;
Ok(m)
}
pub async fn fetch_version_object(version: &Version) -> Result<VersionConfig, Box<dyn Error + Send + Sync>> {
let resp: String = reqwest::get(&version.url)
.await?
.text()
.await?;
let resp: VersionConfig = serde_json::from_str(&resp)?;
let url = version.url.clone();
let mut r = surf::get(url).await?;
let resp = r.body_bytes().await.unwrap();
let resp: VersionConfig = serde_json::from_slice(&resp)?;
Ok(resp)
}
}

View File

@ -1,8 +1,8 @@
use std::sync::{Arc, Mutex};
use futures::AsyncReadExt;
use rand::{distr::Alphanumeric, Rng};
use reqwest::{Client, Response};
use tokio::{fs::File, io::AsyncWriteExt};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::mpsc::{Sender, UnboundedReceiver, UnboundedSender};
use tokio::sync::{mpsc};
use crate::launcher::Launcher;
@ -19,20 +19,24 @@ pub fn download_file(url: &str, file_path: &str, size: u64, sender: UnboundedSen
let file_path = file_path.to_string();
let status = status.to_string();
tokio::spawn( async move {
let client = Client::new();
let mut response: Response = client.get(url).send().await.unwrap();
let mut res = surf::get(url).await.unwrap();
let total_size = res.len().unwrap_or(0); // Total size in bytes (if available)
let mut downloaded = 0;
let mut buf = vec![0; 8192]; // Buffer for reading chunks
let mut file = File::create(file_path).await.unwrap();
if response.status().is_success() {
let mut file = File::create(file_path).await.unwrap();
while let Some(chunk) = response.chunk().await.unwrap() {
if let Err(e) = sender.send((chunk.len(), status.clone()) ) {
println!("SendError: {}", e);
}
let _ = file.write(&chunk).await;
let mut r= res.take_body().into_reader();
while let Ok(n) = r.read(&mut buf).await {
if n == 0 {
break;
}
} else {
println!("Failed to download file: {}", response.status());
downloaded += n;
file.write(&buf[..n]).await;
}
sender.send((downloaded, status.clone()));
});
Ok(())
}

View File

@ -458,7 +458,7 @@
setInterval(function() {
$.get("check_download_status", processParams);
}, 400);
}, 100);
});
</script>
</body>