From 3c9067998692b9bfa1a46f3367abae73b710bdf6 Mon Sep 17 00:00:00 2001 From: Not-A-Normal-Robot Date: Thu, 31 Oct 2024 17:43:25 +0700 Subject: [PATCH] Make cold clear download more modifiable --- src/cold_clear.rs | 10 ++++++---- src/dirs.rs | 50 ++++++++++++++++++++++++----------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/cold_clear.rs b/src/cold_clear.rs index 1d7ebd5..0f37bd8 100644 --- a/src/cold_clear.rs +++ b/src/cold_clear.rs @@ -51,7 +51,7 @@ fn format_time(secs: i32) -> String { } } -pub fn download_cold_clear() -> Result<(), reqwest::Error> { +pub fn download_cold_clear(version: &str) -> Result<(), reqwest::Error> { let (tx, rx) = mpsc::channel::(); let window = ColdClearWaitWindow::new() @@ -60,14 +60,16 @@ pub fn download_cold_clear() -> Result<(), reqwest::Error> { window.on_format_bytes(format_bytes); let window_weak = window.as_weak(); + let version = version.to_owned(); thread::spawn(move || { let rt = Runtime::new() .expect("Failed to create Tokio runtime"); - rt.block_on(async move { - let url = paths::COLD_CLEAR_DOWNLOAD_URL; - let save_path = paths::get_cold_clear_download_path(); + let version = version.as_str(); + let url = paths::get_cold_clear_download_url(version); + let save_path = paths::get_cold_clear_download_path(version); + rt.block_on(async move { let begin_time = Instant::now(); let client = reqwest::Client::new(); diff --git a/src/dirs.rs b/src/dirs.rs index b429b7e..10fbc72 100644 --- a/src/dirs.rs +++ b/src/dirs.rs @@ -152,33 +152,35 @@ pub mod paths { return get_conf_dir_path().join("config.json"); } - pub fn get_cold_clear_download_path() -> PathBuf { - return get_conf_dir_path().join("cold_clear.zip"); + pub fn get_cold_clear_download_path(version: &str) -> PathBuf { + return get_conf_dir_path() + .join("cold_clear") + .join(version.to_string() + ".zip"); } - pub const COLD_CLEAR_DOWNLOAD_URL: &str = - if cfg!(target_os = "windows") { - "https://github.com/26F-Studio/cold_clear_ai_love2d_wrapper/releases/download/11.4.2/Windows.zip" - } else if cfg!(target_os = "macos") { - "https://github.com/26F-Studio/cold_clear_ai_love2d_wrapper/releases/download/11.4.2/macOS.zip" - } else if cfg!(target_os = "linux") { - "https://github.com/26F-Studio/cold_clear_ai_love2d_wrapper/releases/download/11.4.2/Linux.zip" - } else if cfg!(target_os = "android") { - "https://github.com/26F-Studio/cold_clear_ai_love2d_wrapper/releases/download/11.4.2/Android.zip" - } else if cfg!(target_os = "ios") { - "https://github.com/26F-Studio/cold_clear_ai_love2d_wrapper/releases/download/11.4.2/iOS.zip" - } else { - #[cfg(not(any( - target_os = "windows", - target_os = "macos", - target_os = "linux", - target_os = "android", - target_os = "ios" - )))] - compile_error!("Unsupported operating system: {}", std::env::consts::OS); - - unreachable!(); + pub fn get_cold_clear_download_url(version: &str) -> String { + let file_name = match std::env::consts::OS { + "windows" => "Windows.zip", + "macos" => "macOS.zip", + "linux" => "Linux.zip", + "android" => "Android.zip", + "ios" => "iOS.zip", + _ => unreachable!() }; + + #[cfg(not(any( + target_os = "windows", + target_os = "macos", + target_os = "linux", + target_os = "android", + target_os = "ios" + )))] + compile_error!("Unsupported operating system: {}", std::env::consts::OS); + + return format!( + "https://github.com/26F-Studio/cold_clear_ai_love2d_wrapper/releases/download/{version}/{file_name}" + ); + } #[cfg(not(any( target_os = "windows",