From 7965314c6b9c91377024b5113da7c4f557523b32 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sat, 28 Oct 2023 19:35:28 +0200 Subject: [PATCH] print download size --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 73 +++++++++++++++++++++++++++++++++++--------------- src/misc.rs | 11 ++++++++ src/structs.rs | 2 +- 5 files changed, 65 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 852aa49..19fb026 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,7 +30,7 @@ dependencies = [ [[package]] name = "alterware-launcher" -version = "0.5.3" +version = "0.5.4" dependencies = [ "colored", "http_req", diff --git a/Cargo.toml b/Cargo.toml index 8fb724e..a1a70bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "alterware-launcher" -version = "0.5.3" +version = "0.5.4" edition = "2021" build = "res/build.rs" diff --git a/src/main.rs b/src/main.rs index d57759e..e695296 100644 --- a/src/main.rs +++ b/src/main.rs @@ -180,21 +180,35 @@ fn manual_install(games: &[Game]) { std::process::exit(0); } +fn total_download_size(cdn_info: &Vec, remote_dir: &str) -> u64 { + let remote_dir = format!("{}/", remote_dir); + let mut size: u64 = 0; + for file in cdn_info { + if !file.name.starts_with(&remote_dir) || file.name == "iw4/iw4x.dll" { + continue; + } + size += file.size as u64; + } + size +} + fn update_dir( cdn_info: &Vec, remote_dir: &str, dir: &Path, hashes: &mut HashMap, ) { - let remote_dir = format!("{}/", remote_dir); + let remote_dir_pre = format!("{}/", remote_dir); + + let mut files_to_download: Vec = vec![]; for file in cdn_info { - if !file.name.starts_with(&remote_dir) || file.name == "iw4/iw4x.dll" { + if !file.name.starts_with(&remote_dir_pre) || file.name == "iw4/iw4x.dll" { continue; } let sha1_remote = file.hash.to_lowercase(); - let file_name = &file.name.replace(remote_dir.as_str(), ""); + let file_name = &file.name.replace(remote_dir_pre.as_str(), ""); let file_path = dir.join(file_name); if file_path.exists() { let sha1_local = hashes @@ -204,31 +218,46 @@ fn update_dir( .to_string(); if sha1_local != sha1_remote { - println!( - "[{}] {}", - "Updating".bright_yellow(), - file_path.display() - ); - http::download_file(&format!("{}/{}", MASTER, file.name), &file_path); + files_to_download.push(file.clone()); } else { println!("[{}] {}", "Checked".bright_blue(), file_path.display()); } - hashes.insert(file_name.to_owned(), sha1_remote.to_owned()); } else { - println!( - "[{}] {}", - "Downloading".bright_yellow(), - file_path.display() - ); - if let Some(parent) = file_path.parent() { - if !parent.exists() { - fs::create_dir_all(parent).unwrap(); - } - } - http::download_file(&format!("{}/{}", MASTER, file.name), &file_path); - hashes.insert(file_name.to_owned(), sha1_remote.to_owned()); + files_to_download.push(file.clone()); } } + + if files_to_download.is_empty() { + println!( + "[{}] No files to download for {}", + "Info".bright_magenta(), + remote_dir + ); + return; + } + println!( + "[{}] Downloading outdated or missing files for {}, {}", + "Info".bright_magenta(), + remote_dir, + misc::human_readable_bytes(total_download_size(&files_to_download, &remote_dir)) + ); + for file in files_to_download { + let file_name = &file.name.replace(&format!("{}/", remote_dir), "/"); + let file_path = dir.join(file_name); + println!( + "[{}] {} ({})", + "Downloading".bright_yellow(), + file_path.display(), + misc::human_readable_bytes(file.size as u64) + ); + if let Some(parent) = file_path.parent() { + if !parent.exists() { + fs::create_dir_all(parent).unwrap(); + } + } + http::download_file(&format!("{}/{}", MASTER, file.name), &file_path); + hashes.insert(file_name.to_owned(), file.hash.to_lowercase()); + } } fn update(game: &Game, dir: &Path, bonus_content: bool, force: bool) { diff --git a/src/misc.rs b/src/misc.rs index 28af4fc..6d26c9a 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -26,3 +26,14 @@ pub fn fatal_error(error: &str) { stdin(); std::process::exit(1); } + +pub fn human_readable_bytes(bytes: u64) -> String { + let mut bytes = bytes as f64; + let mut i = 0; + let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; + while bytes > 1024.0 { + bytes /= 1024.0; + i += 1; + } + format!("{:.2} {}", bytes, units[i]) +} diff --git a/src/structs.rs b/src/structs.rs index f2f0b97..40b7af0 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,4 +1,4 @@ -#[derive(serde::Deserialize, serde::Serialize)] +#[derive(serde::Deserialize, serde::Serialize, Clone)] pub struct CdnFile { pub name: String, pub size: u32,