diff options
| author | 2026-01-13 15:56:20 +0800 | |
|---|---|---|
| committer | 2026-01-13 15:56:20 +0800 | |
| commit | 66f7825ed9638606665b9e61c6f8132de013da14 (patch) | |
| tree | 764c30309a1c5a77f3b0d92d5131b1b50ae50402 /src-tauri/src/main.rs | |
| parent | 6fbb5f19cab02f3a0f18cdeda3da02e717b69cd6 (diff) | |
| download | DropOut-66f7825ed9638606665b9e61c6f8132de013da14.tar.gz DropOut-66f7825ed9638606665b9e61c6f8132de013da14.zip | |
feat: implement download functionality with progress monitoring and version management
Diffstat (limited to 'src-tauri/src/main.rs')
| -rw-r--r-- | src-tauri/src/main.rs | 64 |
1 files changed, 46 insertions, 18 deletions
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 402f58f..6623802 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,28 +1,50 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use tauri::State; +use tauri::{State, Window}; // Added Window mod core; mod launcher; #[tauri::command] -async fn start_game() -> Result<String, String> { - println!("Backend received StartGame"); - match core::manifest::fetch_version_manifest().await { - Ok(manifest) => { - let msg = format!( - "Fetched manifest.\nLatest release: {}\nLatest snapshot: {}", - manifest.latest.release, manifest.latest.snapshot - ); - println!("{}", msg); - Ok(msg) - } - Err(e) => { - eprintln!("Error fetching manifest: {}", e); - Err(e.to_string()) - } - } +async fn start_game( + window: Window, + version_id: String +) -> Result<String, String> { + println!("Backend received StartGame for {}", version_id); + + // 1. Fetch manifest to find the version URL + let manifest = core::manifest::fetch_version_manifest().await.map_err(|e| e.to_string())?; + + // Find the version info + let version_info = manifest.versions.iter().find(|v| v.id == version_id) + .ok_or_else(|| format!("Version {} not found in manifest", version_id))?; + + // 2. Fetch specific version JSON (client.jar info) + let version_url = &version_info.url; + let version_details: core::game_version::GameVersion = reqwest::get(version_url) + .await.map_err(|e| e.to_string())? + .json().await.map_err(|e| e.to_string())?; + + // 3. Prepare download task for Client Jar + let client_jar = version_details.downloads.client; + // Where to save? Let's use ./versions/{version_id}/{version_id}.jar + let mut path = std::path::PathBuf::from("versions"); + path.push(&version_id); + path.push(format!("{}.jar", version_id)); + + let task = core::downloader::DownloadTask { + url: client_jar.url, + path, + sha1: Some(client_jar.sha1), + }; + + println!("Starting download of client jar..."); + + // 4. Start Download + core::downloader::download_files(window, vec![task]).await.map_err(|e| e.to_string())?; + + Ok(format!("Download complete for {}", version_id)) } #[tauri::command] @@ -55,11 +77,17 @@ async fn get_active_account( Ok(state.active_account.lock().unwrap().clone()) } +#[tauri::command] +async fn logout(state: State<'_, core::auth::AccountState>) -> Result<(), String> { + *state.active_account.lock().unwrap() = None; + Ok(()) +} + fn main() { tauri::Builder::default() .plugin(tauri_plugin_shell::init()) .manage(core::auth::AccountState::new()) - .invoke_handler(tauri::generate_handler![start_game, get_versions, login_offline, get_active_account]) + .invoke_handler(tauri::generate_handler![start_game, get_versions, login_offline, get_active_account, logout]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } |