From c81e9faad0f8cef711c66197023e5934eb0a85c4 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Wed, 22 Jan 2025 23:18:58 -0600 Subject: make some changes after testing some mod loaders - fabric - forge - neoforge --- src/launcher/download.rs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'src/launcher/download.rs') diff --git a/src/launcher/download.rs b/src/launcher/download.rs index 846bed1..ec4a59c 100644 --- a/src/launcher/download.rs +++ b/src/launcher/download.rs @@ -1,10 +1,9 @@ use std::error::Error; use std::fmt::{Debug, Display, Formatter}; -use std::io::ErrorKind; use std::path::{Path, PathBuf}; use futures::{stream, StreamExt, TryStream, TryStreamExt}; -use log::{debug, warn}; -use reqwest::{Client, IntoUrl, Method, RequestBuilder}; +use log::debug; +use reqwest::{Client, Method, RequestBuilder}; use sha1_smol::{Digest, Sha1}; use tokio::fs; use tokio::fs::File; @@ -15,13 +14,15 @@ use crate::util::{FileVerifyError, IntegrityError}; pub trait Download: Debug + Display { // return Ok(None) to skip downloading this file - fn get_url(&self) -> impl IntoUrl; - - async fn prepare(&mut self, req: RequestBuilder) -> Result, Box>; + async fn prepare(&mut self, client: &Client) -> Result, Box>; async fn handle_chunk(&mut self, chunk: &[u8]) -> Result<(), Box>; async fn finish(&mut self) -> Result<(), Box>; } +pub trait FileDownload: Download { + fn get_path(&self) -> &Path; +} + pub struct MultiDownloader<'j, T: Download + 'j, I: Iterator> { jobs: I, nconcurrent: usize @@ -110,12 +111,12 @@ impl<'j, T: Download + 'j, I: Iterator> MultiDownloader<'j, T, } } - let Some(rq) = map_err!( - job.prepare(client.request(Method::GET, job.get_url()) - .header(reqwest::header::USER_AGENT, USER_AGENT)).await, Phase::Prepare, job) else { + let Some(rq) = map_err!(job.prepare(client).await, Phase::Prepare, job) else { return Ok(()) }; + let rq = rq.header(reqwest::header::USER_AGENT, USER_AGENT); + let mut data = map_err!(map_err!(rq.send().await, Phase::Send, job).error_for_status(), Phase::Send, job).bytes_stream(); while let Some(bytes) = data.next().await { @@ -187,10 +188,6 @@ impl VerifiedDownload { &self.url } - pub fn get_path(&self) -> &Path { - &self.path - } - pub fn get_expect_size(&self) -> Option { self.expect_size } @@ -210,11 +207,7 @@ impl VerifiedDownload { } impl Download for VerifiedDownload { - fn get_url(&self) -> impl IntoUrl { - &self.url - } - - async fn prepare(&mut self, req: RequestBuilder) -> Result, Box> { + async fn prepare(&mut self, client: &Client) -> Result, Box> { if !util::should_download(&self.path, self.expect_size, self.expect_sha1).await? { return Ok(None) } @@ -222,7 +215,7 @@ impl Download for VerifiedDownload { // potentially racy to close the file and reopen it... :/ self.open_output().await?; - Ok(Some(req)) + Ok(Some(client.request(Method::GET, &self.url))) } async fn handle_chunk(&mut self, chunk: &[u8]) -> Result<(), Box> { @@ -257,6 +250,12 @@ impl Download for VerifiedDownload { } } +impl FileDownload for VerifiedDownload { + fn get_path(&self) -> &Path { + &self.path + } +} + pub async fn verify_files(files: impl Iterator) -> Result<(), FileVerifyError> { stream::iter(files) .map(|dl| Ok(async move { -- cgit v1.2.3-70-g09d2