From b21df2f9abb9c8d5da0b77f2d3756802f95a1ad2 Mon Sep 17 00:00:00 2001 From: HsiangNianian Date: Wed, 14 Jan 2026 13:35:06 +0800 Subject: feat: display download rate and progress with concurrency support --- src-tauri/src/core/downloader.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'src-tauri/src/core') diff --git a/src-tauri/src/core/downloader.rs b/src-tauri/src/core/downloader.rs index 5f6ec80..5a0605b 100644 --- a/src-tauri/src/core/downloader.rs +++ b/src-tauri/src/core/downloader.rs @@ -1,6 +1,7 @@ use futures::StreamExt; use serde::{Deserialize, Serialize}; use std::path::PathBuf; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; use std::sync::Arc; use tauri::{Emitter, Window}; use tokio::io::AsyncWriteExt; @@ -19,11 +20,20 @@ pub struct ProgressEvent { pub downloaded: u64, pub total: u64, pub status: String, // "Downloading", "Verifying", "Finished", "Error" + pub completed_files: usize, + pub total_files: usize, + pub total_downloaded_bytes: u64, } -pub async fn download_files(window: Window, tasks: Vec) -> Result<(), String> { - let client = reqwest::Client::new(); - let semaphore = Arc::new(Semaphore::new(10)); // Max 10 concurrent downloads +pub async fn download_files(window: Window, tasks: Vec, max_concurrent: usize) -> Result<(), String> { + let client = reqwest::Client::builder() + .pool_max_idle_per_host(max_concurrent) + .build() + .map_err(|e| e.to_string())?; + let semaphore = Arc::new(Semaphore::new(max_concurrent)); + let completed_files = Arc::new(AtomicUsize::new(0)); + let total_downloaded_bytes = Arc::new(AtomicU64::new(0)); + let total_files = tasks.len(); // Notify start (total files) let _ = window.emit("download-start", tasks.len()); @@ -32,6 +42,8 @@ pub async fn download_files(window: Window, tasks: Vec) -> Result< let client = client.clone(); let window = window.clone(); let semaphore = semaphore.clone(); + let completed_files = completed_files.clone(); + let total_downloaded_bytes = total_downloaded_bytes.clone(); async move { let _permit = semaphore.acquire().await.unwrap(); @@ -46,6 +58,9 @@ pub async fn download_files(window: Window, tasks: Vec) -> Result< downloaded: 0, total: 0, status: "Verifying".into(), + completed_files: completed_files.load(Ordering::Relaxed), + total_files, + total_downloaded_bytes: total_downloaded_bytes.load(Ordering::Relaxed), }, ); @@ -57,6 +72,7 @@ pub async fn download_files(window: Window, tasks: Vec) -> Result< let result = hex::encode(hasher.finalize()); if &result == expected_sha1 { // Already valid + let completed = completed_files.fetch_add(1, Ordering::Relaxed) + 1; let _ = window.emit( "download-progress", ProgressEvent { @@ -64,6 +80,9 @@ pub async fn download_files(window: Window, tasks: Vec) -> Result< downloaded: 0, total: 0, status: "Skipped".into(), + completed_files: completed, + total_files, + total_downloaded_bytes: total_downloaded_bytes.load(Ordering::Relaxed), }, ); return Ok(()); @@ -93,6 +112,7 @@ pub async fn download_files(window: Window, tasks: Vec) -> Result< return Err(format!("Write error: {}", e)); } downloaded += chunk.len() as u64; + let total_bytes = total_downloaded_bytes.fetch_add(chunk.len() as u64, Ordering::Relaxed) + chunk.len() as u64; let _ = window.emit( "download-progress", ProgressEvent { @@ -100,6 +120,9 @@ pub async fn download_files(window: Window, tasks: Vec) -> Result< downloaded, total: total_size, status: "Downloading".into(), + completed_files: completed_files.load(Ordering::Relaxed), + total_files, + total_downloaded_bytes: total_bytes, }, ); } @@ -111,6 +134,7 @@ pub async fn download_files(window: Window, tasks: Vec) -> Result< Err(e) => return Err(format!("Request error: {}", e)), } + let completed = completed_files.fetch_add(1, Ordering::Relaxed) + 1; let _ = window.emit( "download-progress", ProgressEvent { @@ -118,6 +142,9 @@ pub async fn download_files(window: Window, tasks: Vec) -> Result< downloaded: 0, total: 0, status: "Finished".into(), + completed_files: completed, + total_files, + total_downloaded_bytes: total_downloaded_bytes.load(Ordering::Relaxed), }, ); -- cgit v1.2.3-70-g09d2 From 791bd84d175d8d454d8a7846dea63d57653ed7e1 Mon Sep 17 00:00:00 2001 From: HsiangNianian Date: Wed, 14 Jan 2026 13:35:29 +0800 Subject: feat: add download_threads to LauncherConfig and update download logging --- src-tauri/src/core/config.rs | 2 ++ src-tauri/src/main.rs | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src-tauri/src/core') diff --git a/src-tauri/src/core/config.rs b/src-tauri/src/core/config.rs index 47c5306..dc72dcb 100644 --- a/src-tauri/src/core/config.rs +++ b/src-tauri/src/core/config.rs @@ -11,6 +11,7 @@ pub struct LauncherConfig { pub java_path: String, pub width: u32, pub height: u32, + pub download_threads: u32, // concurrent download threads } impl Default for LauncherConfig { @@ -21,6 +22,7 @@ impl Default for LauncherConfig { java_path: "java".to_string(), width: 854, height: 480, + download_threads: 32, } } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index d7ae9a4..73310d5 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -262,8 +262,8 @@ async fn start_game( )); // 4. Start Download - emit_log!(window, "Starting downloads...".to_string()); - core::downloader::download_files(window.clone(), download_tasks) + emit_log!(window, format!("Starting downloads with {} concurrent threads...", config.download_threads)); + core::downloader::download_files(window.clone(), download_tasks, config.download_threads as usize) .await .map_err(|e| e.to_string())?; emit_log!(window, "All downloads completed successfully".to_string()); -- cgit v1.2.3-70-g09d2 From df1450d565fda97e6c6dfce825abb682f567759b Mon Sep 17 00:00:00 2001 From: HsiangNianian Date: Wed, 14 Jan 2026 13:44:01 +0800 Subject: fix: update download_threads comment to specify valid range (1-128) --- src-tauri/src/core/config.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src-tauri/src/core') diff --git a/src-tauri/src/core/config.rs b/src-tauri/src/core/config.rs index dc72dcb..d6d594f 100644 --- a/src-tauri/src/core/config.rs +++ b/src-tauri/src/core/config.rs @@ -5,13 +5,14 @@ use std::sync::Mutex; use tauri::{AppHandle, Manager}; #[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(default)] pub struct LauncherConfig { pub min_memory: u32, // in MB pub max_memory: u32, // in MB pub java_path: String, pub width: u32, pub height: u32, - pub download_threads: u32, // concurrent download threads + pub download_threads: u32, // concurrent download threads (1-128) } impl Default for LauncherConfig { -- cgit v1.2.3-70-g09d2 From 188e3a910ce3566742068979d7fc1eb6f454884c Mon Sep 17 00:00:00 2001 From: HsiangNianian Date: Wed, 14 Jan 2026 13:44:06 +0800 Subject: feat: implement global progress tracking for concurrent downloads --- src-tauri/src/core/downloader.rs | 147 +++++++++++++++++++++++---------------- 1 file changed, 88 insertions(+), 59 deletions(-) (limited to 'src-tauri/src/core') diff --git a/src-tauri/src/core/downloader.rs b/src-tauri/src/core/downloader.rs index 5a0605b..139b621 100644 --- a/src-tauri/src/core/downloader.rs +++ b/src-tauri/src/core/downloader.rs @@ -25,15 +25,92 @@ pub struct ProgressEvent { pub total_downloaded_bytes: u64, } +/// Snapshot of global progress state +struct ProgressSnapshot { + completed_files: usize, + total_files: usize, + total_downloaded_bytes: u64, +} + +/// Centralized progress tracking with atomic counters +struct GlobalProgress { + completed_files: AtomicUsize, + total_downloaded_bytes: AtomicU64, + total_files: usize, +} + +impl GlobalProgress { + fn new(total_files: usize) -> Self { + Self { + completed_files: AtomicUsize::new(0), + total_downloaded_bytes: AtomicU64::new(0), + total_files, + } + } + + /// Get current progress snapshot without modification + fn snapshot(&self) -> ProgressSnapshot { + ProgressSnapshot { + completed_files: self.completed_files.load(Ordering::Relaxed), + total_files: self.total_files, + total_downloaded_bytes: self.total_downloaded_bytes.load(Ordering::Relaxed), + } + } + + /// Increment completed files counter and return updated snapshot + fn inc_completed(&self) -> ProgressSnapshot { + let completed = self.completed_files.fetch_add(1, Ordering::Relaxed) + 1; + ProgressSnapshot { + completed_files: completed, + total_files: self.total_files, + total_downloaded_bytes: self.total_downloaded_bytes.load(Ordering::Relaxed), + } + } + + /// Add downloaded bytes and return updated snapshot + fn add_bytes(&self, delta: u64) -> ProgressSnapshot { + let total_bytes = self.total_downloaded_bytes.fetch_add(delta, Ordering::Relaxed) + delta; + ProgressSnapshot { + completed_files: self.completed_files.load(Ordering::Relaxed), + total_files: self.total_files, + total_downloaded_bytes: total_bytes, + } + } +} + +/// Emit a progress event to the frontend +fn emit_progress( + window: &Window, + file_name: &str, + status: &str, + downloaded: u64, + total: u64, + snapshot: &ProgressSnapshot, +) { + let _ = window.emit( + "download-progress", + ProgressEvent { + file: file_name.to_string(), + downloaded, + total, + status: status.into(), + completed_files: snapshot.completed_files, + total_files: snapshot.total_files, + total_downloaded_bytes: snapshot.total_downloaded_bytes, + }, + ); +} + pub async fn download_files(window: Window, tasks: Vec, max_concurrent: usize) -> Result<(), String> { + // Clamp max_concurrent to a valid range (1-128) to prevent edge cases + let max_concurrent = max_concurrent.clamp(1, 128); + let client = reqwest::Client::builder() .pool_max_idle_per_host(max_concurrent) .build() .map_err(|e| e.to_string())?; let semaphore = Arc::new(Semaphore::new(max_concurrent)); - let completed_files = Arc::new(AtomicUsize::new(0)); - let total_downloaded_bytes = Arc::new(AtomicU64::new(0)); - let total_files = tasks.len(); + let progress = Arc::new(GlobalProgress::new(tasks.len())); // Notify start (total files) let _ = window.emit("download-start", tasks.len()); @@ -42,8 +119,7 @@ pub async fn download_files(window: Window, tasks: Vec, max_concur let client = client.clone(); let window = window.clone(); let semaphore = semaphore.clone(); - let completed_files = completed_files.clone(); - let total_downloaded_bytes = total_downloaded_bytes.clone(); + let progress = progress.clone(); async move { let _permit = semaphore.acquire().await.unwrap(); @@ -51,18 +127,7 @@ pub async fn download_files(window: Window, tasks: Vec, max_concur // 1. Check if file exists and verify SHA1 if task.path.exists() { - let _ = window.emit( - "download-progress", - ProgressEvent { - file: file_name.clone(), - downloaded: 0, - total: 0, - status: "Verifying".into(), - completed_files: completed_files.load(Ordering::Relaxed), - total_files, - total_downloaded_bytes: total_downloaded_bytes.load(Ordering::Relaxed), - }, - ); + emit_progress(&window, &file_name, "Verifying", 0, 0, &progress.snapshot()); if let Some(expected_sha1) = &task.sha1 { if let Ok(data) = tokio::fs::read(&task.path).await { @@ -71,20 +136,8 @@ pub async fn download_files(window: Window, tasks: Vec, max_concur hasher.update(&data); let result = hex::encode(hasher.finalize()); if &result == expected_sha1 { - // Already valid - let completed = completed_files.fetch_add(1, Ordering::Relaxed) + 1; - let _ = window.emit( - "download-progress", - ProgressEvent { - file: file_name.clone(), - downloaded: 0, - total: 0, - status: "Skipped".into(), - completed_files: completed, - total_files, - total_downloaded_bytes: total_downloaded_bytes.load(Ordering::Relaxed), - }, - ); + // Already valid, skip download + emit_progress(&window, &file_name, "Skipped", 0, 0, &progress.inc_completed()); return Ok(()); } } @@ -112,19 +165,8 @@ pub async fn download_files(window: Window, tasks: Vec, max_concur return Err(format!("Write error: {}", e)); } downloaded += chunk.len() as u64; - let total_bytes = total_downloaded_bytes.fetch_add(chunk.len() as u64, Ordering::Relaxed) + chunk.len() as u64; - let _ = window.emit( - "download-progress", - ProgressEvent { - file: file_name.clone(), - downloaded, - total: total_size, - status: "Downloading".into(), - completed_files: completed_files.load(Ordering::Relaxed), - total_files, - total_downloaded_bytes: total_bytes, - }, - ); + let snapshot = progress.add_bytes(chunk.len() as u64); + emit_progress(&window, &file_name, "Downloading", downloaded, total_size, &snapshot); } Ok(None) => break, Err(e) => return Err(format!("Download error: {}", e)), @@ -134,27 +176,14 @@ pub async fn download_files(window: Window, tasks: Vec, max_concur Err(e) => return Err(format!("Request error: {}", e)), } - let completed = completed_files.fetch_add(1, Ordering::Relaxed) + 1; - let _ = window.emit( - "download-progress", - ProgressEvent { - file: file_name.clone(), - downloaded: 0, - total: 0, - status: "Finished".into(), - completed_files: completed, - total_files, - total_downloaded_bytes: total_downloaded_bytes.load(Ordering::Relaxed), - }, - ); - + emit_progress(&window, &file_name, "Finished", 0, 0, &progress.inc_completed()); Ok(()) } }); // Buffer unordered to run concurrently tasks_stream - .buffer_unordered(10) + .buffer_unordered(max_concurrent) .collect::>>() .await; -- cgit v1.2.3-70-g09d2 From ee44423edfa83597411d6f499ae61c2aa2d9ad1f Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 14 Jan 2026 14:13:01 +0800 Subject: Update src-tauri/src/core/downloader.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src-tauri/src/core/downloader.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src-tauri/src/core') diff --git a/src-tauri/src/core/downloader.rs b/src-tauri/src/core/downloader.rs index 139b621..d792167 100644 --- a/src-tauri/src/core/downloader.rs +++ b/src-tauri/src/core/downloader.rs @@ -105,10 +105,7 @@ pub async fn download_files(window: Window, tasks: Vec, max_concur // Clamp max_concurrent to a valid range (1-128) to prevent edge cases let max_concurrent = max_concurrent.clamp(1, 128); - let client = reqwest::Client::builder() - .pool_max_idle_per_host(max_concurrent) - .build() - .map_err(|e| e.to_string())?; + let client = reqwest::Client::new(); let semaphore = Arc::new(Semaphore::new(max_concurrent)); let progress = Arc::new(GlobalProgress::new(tasks.len())); -- cgit v1.2.3-70-g09d2 From bf0a493020aa2e0ffea47e0d10105dd525439f23 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Wed, 14 Jan 2026 14:13:14 +0800 Subject: Update src-tauri/src/core/downloader.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src-tauri/src/core/downloader.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src-tauri/src/core') diff --git a/src-tauri/src/core/downloader.rs b/src-tauri/src/core/downloader.rs index d792167..7ff81ad 100644 --- a/src-tauri/src/core/downloader.rs +++ b/src-tauri/src/core/downloader.rs @@ -134,6 +134,13 @@ pub async fn download_files(window: Window, tasks: Vec, max_concur let result = hex::encode(hasher.finalize()); if &result == expected_sha1 { // Already valid, skip download + let skipped_size = tokio::fs::metadata(&task.path) + .await + .map(|m| m.len()) + .unwrap_or(0); + if skipped_size > 0 { + let _ = progress.add_bytes(skipped_size); + } emit_progress(&window, &file_name, "Skipped", 0, 0, &progress.inc_completed()); return Ok(()); } -- cgit v1.2.3-70-g09d2