diff options
| author | 2026-01-15 17:49:26 +0800 | |
|---|---|---|
| committer | 2026-01-15 17:49:26 +0800 | |
| commit | 32a9aceee42a2261b64f9e6effda522639576a5e (patch) | |
| tree | 4cae8d216c3093421addaa0450bc8004c537e373 /src-tauri | |
| parent | ce4b0c2053d5d16f7091d74840d4a502401f1a4e (diff) | |
| parent | 31077dbd39a25eecd24a1dca0f8c9d1879265277 (diff) | |
| download | DropOut-32a9aceee42a2261b64f9e6effda522639576a5e.tar.gz DropOut-32a9aceee42a2261b64f9e6effda522639576a5e.zip | |
Merge pull request #30 from HsiangNianian/main
Diffstat (limited to 'src-tauri')
| -rw-r--r-- | src-tauri/Cargo.toml | 16 | ||||
| -rw-r--r-- | src-tauri/capabilities/default.json | 16 | ||||
| -rwxr-xr-x | src-tauri/scripts/fix-appimage.sh | 32 | ||||
| -rw-r--r-- | src-tauri/src/core/config.rs | 14 | ||||
| -rw-r--r-- | src-tauri/src/core/downloader.rs | 444 | ||||
| -rw-r--r-- | src-tauri/src/core/fabric.rs | 274 | ||||
| -rw-r--r-- | src-tauri/src/core/forge.rs | 336 | ||||
| -rw-r--r-- | src-tauri/src/core/game_version.rs | 47 | ||||
| -rw-r--r-- | src-tauri/src/core/java.rs | 727 | ||||
| -rw-r--r-- | src-tauri/src/core/manifest.rs | 154 | ||||
| -rw-r--r-- | src-tauri/src/core/maven.rs | 263 | ||||
| -rw-r--r-- | src-tauri/src/core/mod.rs | 4 | ||||
| -rw-r--r-- | src-tauri/src/core/rules.rs | 7 | ||||
| -rw-r--r-- | src-tauri/src/core/version_merge.rs | 244 | ||||
| -rw-r--r-- | src-tauri/src/main.rs | 508 | ||||
| -rw-r--r-- | src-tauri/src/utils/zip.rs | 75 | ||||
| -rw-r--r-- | src-tauri/tauri.conf.json | 15 |
17 files changed, 3090 insertions, 86 deletions
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 8d4a612..0eb143c 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dropout" -version = "0.1.14" +version = "0.1.21" edition = "2021" authors = ["HsiangNianian"] description = "The DropOut Minecraft Game Launcher" @@ -8,21 +8,27 @@ license = "MIT" repository = "https://github.com/HsiangNianian/DropOut" [dependencies] -serde = { version = "1.0", features = ["derive"] } -toml = "0.5" +serde = { version = "1.0", features = ["derive"] } +toml = "0.5" log = "0.4" -env_logger = "0.9" +env_logger = "0.9" tokio = { version = "1.49.0", features = ["full"] } -reqwest = { version = "0.13.1", features = ["json", "blocking"] } +reqwest = { version = "0.11", features = ["json", "blocking", "stream", "multipart"] } serde_json = "1.0.149" tauri = { version = "2.9", features = [] } tauri-plugin-shell = "2.3" uuid = { version = "1.10.0", features = ["v3", "v4", "serde"] } futures = "0.3" sha1 = "0.10" +sha2 = "0.10" hex = "0.4" zip = "2.2.2" +flate2 = "1.0" +tar = "0.4" +dirs = "5.0" serde_urlencoded = "0.7.1" +tauri-plugin-dialog = "2.5.0" +tauri-plugin-fs = "2.4.5" [build-dependencies] tauri-build = { version = "2.0", features = [] } diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index 894b905..ea3fd7b 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -12,6 +12,20 @@ "core:app:allow-version", "core:path:default", "core:window:default", - "shell:allow-open" + "shell:allow-open", + "dialog:default", + "fs:default", + { + "identifier": "fs:allow-read", + "allow": [{ "path": "**" }] + }, + { + "identifier": "fs:allow-exists", + "allow": [{ "path": "**" }] + }, + { + "identifier": "fs:allow-write-text-file", + "allow": [{ "path": "**" }] + } ] } diff --git a/src-tauri/scripts/fix-appimage.sh b/src-tauri/scripts/fix-appimage.sh new file mode 100755 index 0000000..6bb375b --- /dev/null +++ b/src-tauri/scripts/fix-appimage.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# fix AppImage for Wayland compatibility +# This script modifies the AppImage bundle created by Tauri to ensure compatibility with Wayland +# It specifically targets the GTK backend settings to avoid forcing X11 + +set -e + +echo "Fixing AppImage for Wayland compatibility..." + +# Tauri sets the APPIMAGE_BUNDLE_PATH environment variable during the build process +APPDIR_PATH="${APPIMAGE_BUNDLE_PATH:-}" + +if [ -z "$APPDIR_PATH" ]; then + echo "No AppImage bundle path found, skipping fix" + exit 0 +fi + +# Check for the presence of the GTK hook file +if [ -d "$APPDIR_PATH/apprun-hooks" ]; then + HOOK_FILE="$APPDIR_PATH/apprun-hooks/linuxdeploy-plugin-gtk.sh" + + if [ -f "$HOOK_FILE" ]; then + echo "Found GTK hook file, patching..." + + # Comment out the line that forces GDK_BACKEND to x11 + sed -i 's/^export GDK_BACKEND=x11.*$/# export GDK_BACKEND=x11 # Disabled for Wayland compatibility/' "$HOOK_FILE" + + echo "Successfully patched $HOOK_FILE" + fi +fi + +echo "AppImage Wayland fix completed!" diff --git a/src-tauri/src/core/config.rs b/src-tauri/src/core/config.rs index d6d594f..43c8145 100644 --- a/src-tauri/src/core/config.rs +++ b/src-tauri/src/core/config.rs @@ -13,6 +13,13 @@ pub struct LauncherConfig { pub width: u32, pub height: u32, pub download_threads: u32, // concurrent download threads (1-128) + pub custom_background_path: Option<String>, + pub enable_gpu_acceleration: bool, + pub enable_visual_effects: bool, + pub active_effect: String, + pub theme: String, + pub log_upload_service: String, // "paste.rs" or "pastebin.com" + pub pastebin_api_key: Option<String>, } impl Default for LauncherConfig { @@ -24,6 +31,13 @@ impl Default for LauncherConfig { width: 854, height: 480, download_threads: 32, + custom_background_path: None, + enable_gpu_acceleration: false, + enable_visual_effects: true, + active_effect: "constellation".to_string(), + theme: "dark".to_string(), + log_upload_service: "paste.rs".to_string(), + pastebin_api_key: None, } } } diff --git a/src-tauri/src/core/downloader.rs b/src-tauri/src/core/downloader.rs index 3add9b7..bf6334f 100644 --- a/src-tauri/src/core/downloader.rs +++ b/src-tauri/src/core/downloader.rs @@ -1,17 +1,415 @@ use futures::StreamExt; use serde::{Deserialize, Serialize}; +use sha1::Digest as Sha1Digest; use std::path::PathBuf; -use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; use std::sync::Arc; -use tauri::{Emitter, Window}; -use tokio::io::AsyncWriteExt; +use tauri::{AppHandle, Emitter, Manager, Window}; +use tokio::io::{AsyncSeekExt, AsyncWriteExt}; use tokio::sync::Semaphore; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct DownloadTask { pub url: String, pub path: PathBuf, + #[serde(default)] pub sha1: Option<String>, + #[serde(default)] + pub sha256: Option<String>, +} + +/// Metadata for resumable downloads stored in .part.meta file +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DownloadMetadata { + pub url: String, + pub file_name: String, + pub total_size: u64, + pub downloaded_bytes: u64, + pub checksum: Option<String>, + pub timestamp: u64, + pub segments: Vec<DownloadSegment>, +} + +/// A download segment for multi-segment parallel downloading +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DownloadSegment { + pub start: u64, + pub end: u64, + pub downloaded: u64, + pub completed: bool, +} + +/// Progress event for Java download +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct JavaDownloadProgress { + pub file_name: String, + pub downloaded_bytes: u64, + pub total_bytes: u64, + pub speed_bytes_per_sec: u64, + pub eta_seconds: u64, + pub status: String, // "Downloading", "Extracting", "Verifying", "Completed", "Paused", "Error" + pub percentage: f32, +} + +/// Pending download task for queue persistence +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PendingJavaDownload { + pub major_version: u32, + pub image_type: String, + pub download_url: String, + pub file_name: String, + pub file_size: u64, + pub checksum: Option<String>, + pub install_path: String, + pub created_at: u64, +} + +/// Download queue for persistence +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct DownloadQueue { + pub pending_downloads: Vec<PendingJavaDownload>, +} + +impl DownloadQueue { + /// Load download queue from file + pub fn load(app_handle: &AppHandle) -> Self { + let queue_path = app_handle + .path() + .app_data_dir() + .unwrap() + .join("download_queue.json"); + if queue_path.exists() { + if let Ok(content) = std::fs::read_to_string(&queue_path) { + if let Ok(queue) = serde_json::from_str(&content) { + return queue; + } + } + } + Self::default() + } + + /// Save download queue to file + pub fn save(&self, app_handle: &AppHandle) -> Result<(), String> { + let queue_path = app_handle + .path() + .app_data_dir() + .unwrap() + .join("download_queue.json"); + let content = serde_json::to_string_pretty(self).map_err(|e| e.to_string())?; + std::fs::write(&queue_path, content).map_err(|e| e.to_string())?; + Ok(()) + } + + /// Add a pending download + pub fn add(&mut self, download: PendingJavaDownload) { + // Remove existing download for same version/type + self.pending_downloads.retain(|d| { + !(d.major_version == download.major_version && d.image_type == download.image_type) + }); + self.pending_downloads.push(download); + } + + /// Remove a completed or cancelled download + pub fn remove(&mut self, major_version: u32, image_type: &str) { + self.pending_downloads.retain(|d| { + !(d.major_version == major_version && d.image_type == image_type) + }); + } +} + +/// Global cancel flag for Java downloads +pub static JAVA_DOWNLOAD_CANCELLED: AtomicBool = AtomicBool::new(false); + +/// Reset the cancel flag +pub fn reset_java_download_cancel() { + JAVA_DOWNLOAD_CANCELLED.store(false, Ordering::SeqCst); +} + +/// Cancel the current Java download +pub fn cancel_java_download() { + JAVA_DOWNLOAD_CANCELLED.store(true, Ordering::SeqCst); +} + +/// Check if download is cancelled +pub fn is_java_download_cancelled() -> bool { + JAVA_DOWNLOAD_CANCELLED.load(Ordering::SeqCst) +} + +/// Determine optimal segment count based on file size +fn get_segment_count(file_size: u64) -> usize { + if file_size < 20 * 1024 * 1024 { + 1 // < 20MB: single segment + } else if file_size < 100 * 1024 * 1024 { + 4 // 20-100MB: 4 segments + } else { + 8 // > 100MB: 8 segments + } +} + +/// Download a large file with resume support and progress events +pub async fn download_with_resume( + app_handle: &AppHandle, + url: &str, + dest_path: &PathBuf, + checksum: Option<&str>, + total_size: u64, +) -> Result<(), String> { + reset_java_download_cancel(); + + let part_path = dest_path.with_extension( + dest_path + .extension() + .map(|e| format!("{}.part", e.to_string_lossy())) + .unwrap_or_else(|| "part".to_string()), + ); + let meta_path = PathBuf::from(format!("{}.meta", part_path.display())); + let file_name = dest_path + .file_name() + .unwrap_or_default() + .to_string_lossy() + .to_string(); + + // Load or create metadata + let mut metadata = if meta_path.exists() { + let content = tokio::fs::read_to_string(&meta_path) + .await + .map_err(|e| e.to_string())?; + serde_json::from_str(&content).unwrap_or_else(|_| create_new_metadata(url, &file_name, total_size, checksum)) + } else { + create_new_metadata(url, &file_name, total_size, checksum) + }; + + // Create parent directory + if let Some(parent) = dest_path.parent() { + tokio::fs::create_dir_all(parent) + .await + .map_err(|e| e.to_string())?; + } + + // Open or create part file + let file = tokio::fs::OpenOptions::new() + .create(true) + .write(true) + .read(true) + .open(&part_path) + .await + .map_err(|e| format!("Failed to open part file: {}", e))?; + + let file = Arc::new(tokio::sync::Mutex::new(file)); + let client = reqwest::Client::new(); + let progress = Arc::new(AtomicU64::new(metadata.downloaded_bytes)); + let start_time = std::time::Instant::now(); + let last_progress_bytes = Arc::new(AtomicU64::new(metadata.downloaded_bytes)); + + // Download segments concurrently + let segment_count = metadata.segments.len(); + let semaphore = Arc::new(Semaphore::new(segment_count.min(8))); + let mut handles = Vec::new(); + + for (idx, segment) in metadata.segments.iter().enumerate() { + if segment.completed { + continue; + } + + let client = client.clone(); + let url = url.to_string(); + let file = file.clone(); + let progress = progress.clone(); + let semaphore = semaphore.clone(); + let segment_start = segment.start + segment.downloaded; + let segment_end = segment.end; + let app_handle = app_handle.clone(); + let file_name = file_name.clone(); + let total_size = total_size; + let last_progress_bytes = last_progress_bytes.clone(); + let start_time = start_time.clone(); + + let handle = tokio::spawn(async move { + let _permit = semaphore.acquire().await.unwrap(); + + if is_java_download_cancelled() { + return Err("Download cancelled".to_string()); + } + + // Send Range request + let range = format!("bytes={}-{}", segment_start, segment_end); + let response = client + .get(&url) + .header("Range", &range) + .send() + .await + .map_err(|e| format!("Request failed: {}", e))?; + + if !response.status().is_success() && response.status() != reqwest::StatusCode::PARTIAL_CONTENT { + return Err(format!("Server returned error: {}", response.status())); + } + + let mut stream = response.bytes_stream(); + let mut current_pos = segment_start; + + while let Some(chunk_result) = stream.next().await { + if is_java_download_cancelled() { + return Err("Download cancelled".to_string()); + } + + let chunk = chunk_result.map_err(|e| format!("Stream error: {}", e))?; + let chunk_len = chunk.len() as u64; + + // Write to file at correct position + { + let mut file_guard = file.lock().await; + file_guard + .seek(std::io::SeekFrom::Start(current_pos)) + .await + .map_err(|e| format!("Seek error: {}", e))?; + file_guard + .write_all(&chunk) + .await + .map_err(|e| format!("Write error: {}", e))?; + } + + current_pos += chunk_len; + let total_downloaded = progress.fetch_add(chunk_len, Ordering::Relaxed) + chunk_len; + + // Emit progress event (throttled) + let last_bytes = last_progress_bytes.load(Ordering::Relaxed); + if total_downloaded - last_bytes > 100 * 1024 || total_downloaded >= total_size { + last_progress_bytes.store(total_downloaded, Ordering::Relaxed); + + let elapsed = start_time.elapsed().as_secs_f64(); + let speed = if elapsed > 0.0 { + (total_downloaded as f64 / elapsed) as u64 + } else { + 0 + }; + let remaining = total_size.saturating_sub(total_downloaded); + let eta = if speed > 0 { remaining / speed } else { 0 }; + let percentage = (total_downloaded as f32 / total_size as f32) * 100.0; + + let _ = app_handle.emit( + "java-download-progress", + JavaDownloadProgress { + file_name: file_name.clone(), + downloaded_bytes: total_downloaded, + total_bytes: total_size, + speed_bytes_per_sec: speed, + eta_seconds: eta, + status: "Downloading".to_string(), + percentage, + }, + ); + } + } + + Ok::<usize, String>(idx) + }); + + handles.push(handle); + } + + // Wait for all segments + let mut all_success = true; + for handle in handles { + match handle.await { + Ok(Ok(idx)) => { + metadata.segments[idx].completed = true; + } + Ok(Err(e)) => { + all_success = false; + if e.contains("cancelled") { + // Save progress for resume + metadata.downloaded_bytes = progress.load(Ordering::Relaxed); + let meta_content = serde_json::to_string_pretty(&metadata).map_err(|e| e.to_string())?; + tokio::fs::write(&meta_path, meta_content).await.ok(); + return Err(e); + } + } + Err(e) => { + all_success = false; + eprintln!("Segment task panicked: {}", e); + } + } + } + + if !all_success { + // Save progress + metadata.downloaded_bytes = progress.load(Ordering::Relaxed); + let meta_content = serde_json::to_string_pretty(&metadata).map_err(|e| e.to_string())?; + tokio::fs::write(&meta_path, meta_content).await.ok(); + return Err("Some segments failed".to_string()); + } + + // Verify checksum if provided + if let Some(expected) = checksum { + let _ = app_handle.emit( + "java-download-progress", + JavaDownloadProgress { + file_name: file_name.clone(), + downloaded_bytes: total_size, + total_bytes: total_size, + speed_bytes_per_sec: 0, + eta_seconds: 0, + status: "Verifying".to_string(), + percentage: 100.0, + }, + ); + + let data = tokio::fs::read(&part_path) + .await + .map_err(|e| format!("Failed to read file for verification: {}", e))?; + + if !verify_checksum(&data, Some(expected), None) { + // Checksum failed, delete files and retry + tokio::fs::remove_file(&part_path).await.ok(); + tokio::fs::remove_file(&meta_path).await.ok(); + return Err("Checksum verification failed".to_string()); + } + } + + // Rename part file to final destination + tokio::fs::rename(&part_path, dest_path) + .await + .map_err(|e| format!("Failed to rename file: {}", e))?; + + // Clean up metadata file + tokio::fs::remove_file(&meta_path).await.ok(); + + Ok(()) +} + +/// Create new download metadata with segments +fn create_new_metadata(url: &str, file_name: &str, total_size: u64, checksum: Option<&str>) -> DownloadMetadata { + let segment_count = get_segment_count(total_size); + let segment_size = total_size / segment_count as u64; + let mut segments = Vec::new(); + + for i in 0..segment_count { + let start = i as u64 * segment_size; + let end = if i == segment_count - 1 { + total_size - 1 + } else { + (i as u64 + 1) * segment_size - 1 + }; + segments.push(DownloadSegment { + start, + end, + downloaded: 0, + completed: false, + }); + } + + DownloadMetadata { + url: url.to_string(), + file_name: file_name.to_string(), + total_size, + downloaded_bytes: 0, + checksum: checksum.map(|s| s.to_string()), + timestamp: std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(), + segments, + } } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -25,6 +423,32 @@ pub struct ProgressEvent { pub total_downloaded_bytes: u64, } +/// calculate SHA256 hash of data +pub fn compute_sha256(data: &[u8]) -> String { + let mut hasher = sha2::Sha256::new(); + hasher.update(data); + hex::encode(hasher.finalize()) +} + +/// calculate SHA1 hash of data +pub fn compute_sha1(data: &[u8]) -> String { + let mut hasher = sha1::Sha1::new(); + hasher.update(data); + hex::encode(hasher.finalize()) +} + +/// verify file checksum, prefer SHA256, fallback to SHA1 +pub fn verify_checksum(data: &[u8], sha256: Option<&str>, sha1: Option<&str>) -> bool { + if let Some(expected) = sha256 { + return compute_sha256(data) == expected; + } + if let Some(expected) = sha1 { + return compute_sha1(data) == expected; + } + // No checksum provided, default to true + true +} + /// Snapshot of global progress state struct ProgressSnapshot { completed_files: usize, @@ -129,17 +553,17 @@ pub async fn download_files( let _permit = semaphore.acquire().await.unwrap(); let file_name = task.path.file_name().unwrap().to_string_lossy().to_string(); - // 1. Check if file exists and verify SHA1 + // 1. Check if file exists and verify checksum if task.path.exists() { emit_progress(&window, &file_name, "Verifying", 0, 0, &progress.snapshot()); - if let Some(expected_sha1) = &task.sha1 { + if task.sha256.is_some() || task.sha1.is_some() { if let Ok(data) = tokio::fs::read(&task.path).await { - let mut hasher = sha1::Sha1::new(); - use sha1::Digest; - hasher.update(&data); - let result = hex::encode(hasher.finalize()); - if &result == expected_sha1 { + if verify_checksum( + &data, + task.sha256.as_deref(), + task.sha1.as_deref(), + ) { // Already valid, skip download let skipped_size = tokio::fs::metadata(&task.path) .await diff --git a/src-tauri/src/core/fabric.rs b/src-tauri/src/core/fabric.rs new file mode 100644 index 0000000..fd38f41 --- /dev/null +++ b/src-tauri/src/core/fabric.rs @@ -0,0 +1,274 @@ +//! Fabric Loader support module. +//! +//! This module provides functionality to: +//! - Fetch available Fabric loader versions from the Fabric Meta API +//! - Generate version JSON files for Fabric-enabled Minecraft versions +//! - Install Fabric loader for a specific Minecraft version + +use serde::{Deserialize, Serialize}; +use std::error::Error; +use std::path::PathBuf; + +const FABRIC_META_URL: &str = "https://meta.fabricmc.net/v2"; + +/// Represents a Fabric loader version from the Meta API. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct FabricLoaderVersion { + pub separator: String, + pub build: i32, + pub maven: String, + pub version: String, + pub stable: bool, +} + +/// Represents a Fabric intermediary mapping version. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct FabricIntermediaryVersion { + pub maven: String, + pub version: String, + pub stable: bool, +} + +/// Represents a combined loader + intermediary version entry. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct FabricLoaderEntry { + pub loader: FabricLoaderVersion, + pub intermediary: FabricIntermediaryVersion, + #[serde(rename = "launcherMeta")] + pub launcher_meta: FabricLauncherMeta, +} + +/// Launcher metadata from Fabric Meta API. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct FabricLauncherMeta { + pub version: i32, + pub libraries: FabricLibraries, + #[serde(rename = "mainClass")] + pub main_class: FabricMainClass, +} + +/// Libraries required by Fabric loader. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct FabricLibraries { + pub client: Vec<FabricLibrary>, + pub common: Vec<FabricLibrary>, + pub server: Vec<FabricLibrary>, +} + +/// A single Fabric library dependency. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct FabricLibrary { + pub name: String, + pub url: Option<String>, +} + +/// Main class configuration for Fabric. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct FabricMainClass { + pub client: String, + pub server: String, +} + +/// Represents a Minecraft version supported by Fabric. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct FabricGameVersion { + pub version: String, + pub stable: bool, +} + +/// Information about an installed Fabric version. +#[derive(Debug, Serialize, Clone)] +pub struct InstalledFabricVersion { + pub id: String, + pub minecraft_version: String, + pub loader_version: String, + pub path: PathBuf, +} + +/// Fetch all Minecraft versions supported by Fabric. +/// +/// # Returns +/// A list of game versions that have Fabric intermediary mappings available. +pub async fn fetch_supported_game_versions( +) -> Result<Vec<FabricGameVersion>, Box<dyn Error + Send + Sync>> { + let url = format!("{}/versions/game", FABRIC_META_URL); + let resp = reqwest::get(&url) + .await? + .json::<Vec<FabricGameVersion>>() + .await?; + Ok(resp) +} + +/// Fetch all available Fabric loader versions. +/// +/// # Returns +/// A list of all Fabric loader versions, ordered by build number (newest first). +pub async fn fetch_loader_versions( +) -> Result<Vec<FabricLoaderVersion>, Box<dyn Error + Send + Sync>> { + let url = format!("{}/versions/loader", FABRIC_META_URL); + let resp = reqwest::get(&url) + .await? + .json::<Vec<FabricLoaderVersion>>() + .await?; + Ok(resp) +} + +/// Fetch Fabric loader versions available for a specific Minecraft version. +/// +/// # Arguments +/// * `game_version` - The Minecraft version (e.g., "1.20.4") +/// +/// # Returns +/// A list of loader entries with full metadata for the specified game version. +pub async fn fetch_loaders_for_game_version( + game_version: &str, +) -> Result<Vec<FabricLoaderEntry>, Box<dyn Error + Send + Sync>> { + let url = format!("{}/versions/loader/{}", FABRIC_META_URL, game_version); + let resp = reqwest::get(&url) + .await? + .json::<Vec<FabricLoaderEntry>>() + .await?; + Ok(resp) +} + +/// Fetch the version JSON profile for a specific Fabric loader + game version combination. +/// +/// # Arguments +/// * `game_version` - The Minecraft version (e.g., "1.20.4") +/// * `loader_version` - The Fabric loader version (e.g., "0.15.6") +/// +/// # Returns +/// The raw version JSON as a `serde_json::Value` that can be saved to the versions directory. +pub async fn fetch_version_profile( + game_version: &str, + loader_version: &str, +) -> Result<serde_json::Value, Box<dyn Error + Send + Sync>> { + let url = format!( + "{}/versions/loader/{}/{}/profile/json", + FABRIC_META_URL, game_version, loader_version + ); + let resp = reqwest::get(&url) + .await? + .json::<serde_json::Value>() + .await?; + Ok(resp) +} + +/// Generate the version ID for a Fabric installation. +/// +/// # Arguments +/// * `game_version` - The Minecraft version +/// * `loader_version` - The Fabric loader version +/// +/// # Returns +/// The version ID string (e.g., "fabric-loader-0.15.6-1.20.4") +pub fn generate_version_id(game_version: &str, loader_version: &str) -> String { + format!("fabric-loader-{}-{}", loader_version, game_version) +} + +/// Install Fabric loader for a specific Minecraft version. +/// +/// This creates the version JSON file in the versions directory. +/// The actual library downloads happen during game launch. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// * `game_version` - The Minecraft version (e.g., "1.20.4") +/// * `loader_version` - The Fabric loader version (e.g., "0.15.6") +/// +/// # Returns +/// Information about the installed version. +pub async fn install_fabric( + game_dir: &PathBuf, + game_version: &str, + loader_version: &str, +) -> Result<InstalledFabricVersion, Box<dyn Error + Send + Sync>> { + // Fetch the version profile from Fabric Meta + let profile = fetch_version_profile(game_version, loader_version).await?; + + // Get the version ID from the profile or generate it + let version_id = profile + .get("id") + .and_then(|v| v.as_str()) + .map(|s| s.to_string()) + .unwrap_or_else(|| generate_version_id(game_version, loader_version)); + + // Create the version directory + let version_dir = game_dir.join("versions").join(&version_id); + tokio::fs::create_dir_all(&version_dir).await?; + + // Write the version JSON + let json_path = version_dir.join(format!("{}.json", version_id)); + let json_content = serde_json::to_string_pretty(&profile)?; + tokio::fs::write(&json_path, json_content).await?; + + Ok(InstalledFabricVersion { + id: version_id, + minecraft_version: game_version.to_string(), + loader_version: loader_version.to_string(), + path: json_path, + }) +} + +/// Check if Fabric is installed for a specific version combination. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// * `game_version` - The Minecraft version +/// * `loader_version` - The Fabric loader version +/// +/// # Returns +/// `true` if the version JSON exists, `false` otherwise. +pub fn is_fabric_installed(game_dir: &PathBuf, game_version: &str, loader_version: &str) -> bool { + let version_id = generate_version_id(game_version, loader_version); + let json_path = game_dir + .join("versions") + .join(&version_id) + .join(format!("{}.json", version_id)); + json_path.exists() +} + +/// List all installed Fabric versions in the game directory. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// +/// # Returns +/// A list of installed Fabric version IDs. +pub async fn list_installed_fabric_versions( + game_dir: &PathBuf, +) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> { + let versions_dir = game_dir.join("versions"); + let mut installed = Vec::new(); + + if !versions_dir.exists() { + return Ok(installed); + } + + let mut entries = tokio::fs::read_dir(&versions_dir).await?; + while let Some(entry) = entries.next_entry().await? { + let name = entry.file_name().to_string_lossy().to_string(); + if name.starts_with("fabric-loader-") { + // Verify the JSON file exists + let json_path = entry.path().join(format!("{}.json", name)); + if json_path.exists() { + installed.push(name); + } + } + } + + Ok(installed) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_generate_version_id() { + assert_eq!( + generate_version_id("1.20.4", "0.15.6"), + "fabric-loader-0.15.6-1.20.4" + ); + } +} diff --git a/src-tauri/src/core/forge.rs b/src-tauri/src/core/forge.rs new file mode 100644 index 0000000..0f17bcc --- /dev/null +++ b/src-tauri/src/core/forge.rs @@ -0,0 +1,336 @@ +//! Forge Loader support module. +//! +//! This module provides functionality to: +//! - Fetch available Forge versions from the Forge promotions API +//! - Install Forge loader for a specific Minecraft version +//! +//! Note: Forge installation is more complex than Fabric, especially for versions 1.13+. +//! This implementation focuses on the basic JSON generation approach. +//! For full Forge 1.13+ support, processor execution would need to be implemented. + +use serde::{Deserialize, Serialize}; +use std::error::Error; +use std::path::PathBuf; + +const FORGE_PROMOTIONS_URL: &str = + "https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json"; +const FORGE_MAVEN_URL: &str = "https://maven.minecraftforge.net/"; + +/// Represents a Forge version entry. +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct ForgeVersion { + pub version: String, + pub minecraft_version: String, + #[serde(default)] + pub recommended: bool, + #[serde(default)] + pub latest: bool, +} + +/// Forge promotions response from the API. +#[derive(Debug, Deserialize)] +struct ForgePromotions { + promos: std::collections::HashMap<String, String>, +} + +/// Information about an installed Forge version. +#[derive(Debug, Serialize, Clone)] +pub struct InstalledForgeVersion { + pub id: String, + pub minecraft_version: String, + pub forge_version: String, + pub path: PathBuf, +} + +/// Fetch all Minecraft versions supported by Forge. +/// +/// # Returns +/// A list of Minecraft version strings that have Forge available. +pub async fn fetch_supported_game_versions() -> Result<Vec<String>, Box<dyn Error + Send + Sync>> { + let promos = fetch_promotions().await?; + + let mut versions: Vec<String> = promos + .promos + .keys() + .filter_map(|key| { + // Keys are like "1.20.4-latest", "1.20.4-recommended" + let parts: Vec<&str> = key.split('-').collect(); + if parts.len() >= 2 { + Some(parts[0].to_string()) + } else { + None + } + }) + .collect(); + + // Deduplicate and sort + versions.sort(); + versions.dedup(); + versions.reverse(); // Newest first + + Ok(versions) +} + +/// Fetch Forge promotions data. +async fn fetch_promotions() -> Result<ForgePromotions, Box<dyn Error + Send + Sync>> { + let resp = reqwest::get(FORGE_PROMOTIONS_URL) + .await? + .json::<ForgePromotions>() + .await?; + Ok(resp) +} + +/// Fetch available Forge versions for a specific Minecraft version. +/// +/// # Arguments +/// * `game_version` - The Minecraft version (e.g., "1.20.4") +/// +/// # Returns +/// A list of Forge versions available for the specified game version. +pub async fn fetch_forge_versions( + game_version: &str, +) -> Result<Vec<ForgeVersion>, Box<dyn Error + Send + Sync>> { + let promos = fetch_promotions().await?; + let mut versions = Vec::new(); + + // Look for both latest and recommended + let latest_key = format!("{}-latest", game_version); + let recommended_key = format!("{}-recommended", game_version); + + if let Some(latest) = promos.promos.get(&latest_key) { + versions.push(ForgeVersion { + version: latest.clone(), + minecraft_version: game_version.to_string(), + recommended: false, + latest: true, + }); + } + + if let Some(recommended) = promos.promos.get(&recommended_key) { + // Don't duplicate if recommended == latest + if !versions.iter().any(|v| v.version == *recommended) { + versions.push(ForgeVersion { + version: recommended.clone(), + minecraft_version: game_version.to_string(), + recommended: true, + latest: false, + }); + } else { + // Mark the existing one as both + if let Some(v) = versions.iter_mut().find(|v| v.version == *recommended) { + v.recommended = true; + } + } + } + + Ok(versions) +} + +/// Generate the version ID for a Forge installation. +/// +/// # Arguments +/// * `game_version` - The Minecraft version +/// * `forge_version` - The Forge version +/// +/// # Returns +/// The version ID string (e.g., "1.20.4-forge-49.0.38") +pub fn generate_version_id(game_version: &str, forge_version: &str) -> String { + format!("{}-forge-{}", game_version, forge_version) +} + +/// Install Forge for a specific Minecraft version. +/// +/// Note: This creates a basic version JSON. For Forge 1.13+, the full installation +/// requires running the Forge installer processors, which is not yet implemented. +/// This basic implementation works for legacy Forge versions (<1.13) and creates +/// the structure needed for modern Forge (libraries will need to be downloaded +/// separately). +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// * `game_version` - The Minecraft version (e.g., "1.20.4") +/// * `forge_version` - The Forge version (e.g., "49.0.38") +/// +/// # Returns +/// Information about the installed version. +pub async fn install_forge( + game_dir: &PathBuf, + game_version: &str, + forge_version: &str, +) -> Result<InstalledForgeVersion, Box<dyn Error + Send + Sync>> { + let version_id = generate_version_id(game_version, forge_version); + + // Create basic version JSON structure + // Note: This is a simplified version. Full Forge installation requires + // downloading the installer and running processors. + let version_json = create_forge_version_json(game_version, forge_version)?; + + // Create the version directory + let version_dir = game_dir.join("versions").join(&version_id); + tokio::fs::create_dir_all(&version_dir).await?; + + // Write the version JSON + let json_path = version_dir.join(format!("{}.json", version_id)); + let json_content = serde_json::to_string_pretty(&version_json)?; + tokio::fs::write(&json_path, json_content).await?; + + Ok(InstalledForgeVersion { + id: version_id, + minecraft_version: game_version.to_string(), + forge_version: forge_version.to_string(), + path: json_path, + }) +} + +/// Create a basic Forge version JSON. +/// +/// This creates a minimal version JSON that inherits from vanilla and adds +/// the Forge libraries. For full functionality with Forge 1.13+, the installer +/// would need to be run to patch the game. +fn create_forge_version_json( + game_version: &str, + forge_version: &str, +) -> Result<serde_json::Value, Box<dyn Error + Send + Sync>> { + let version_id = generate_version_id(game_version, forge_version); + let forge_maven_coord = format!( + "net.minecraftforge:forge:{}-{}", + game_version, forge_version + ); + + // Determine main class based on version + // Forge 1.13+ uses different launchers + let (main_class, libraries) = if is_modern_forge(game_version) { + // Modern Forge (1.13+) uses cpw.mods.bootstraplauncher + ( + "cpw.mods.bootstraplauncher.BootstrapLauncher".to_string(), + vec![ + create_library_entry(&forge_maven_coord, Some(FORGE_MAVEN_URL)), + create_library_entry( + &format!( + "net.minecraftforge:forge:{}-{}:universal", + game_version, forge_version + ), + Some(FORGE_MAVEN_URL), + ), + ], + ) + } else { + // Legacy Forge uses LaunchWrapper + ( + "net.minecraft.launchwrapper.Launch".to_string(), + vec![ + create_library_entry(&forge_maven_coord, Some(FORGE_MAVEN_URL)), + create_library_entry("net.minecraft:launchwrapper:1.12", None), + ], + ) + }; + + let json = serde_json::json!({ + "id": version_id, + "inheritsFrom": game_version, + "type": "release", + "mainClass": main_class, + "libraries": libraries, + "arguments": { + "game": [], + "jvm": [] + } + }); + + Ok(json) +} + +/// Create a library entry for the version JSON. +fn create_library_entry(name: &str, maven_url: Option<&str>) -> serde_json::Value { + let mut entry = serde_json::json!({ + "name": name + }); + + if let Some(url) = maven_url { + entry["url"] = serde_json::Value::String(url.to_string()); + } + + entry +} + +/// Check if the Minecraft version uses modern Forge (1.13+). +fn is_modern_forge(game_version: &str) -> bool { + let parts: Vec<&str> = game_version.split('.').collect(); + if parts.len() >= 2 { + if let (Ok(major), Ok(minor)) = (parts[0].parse::<u32>(), parts[1].parse::<u32>()) { + return major > 1 || (major == 1 && minor >= 13); + } + } + false +} + +/// Check if Forge is installed for a specific version combination. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// * `game_version` - The Minecraft version +/// * `forge_version` - The Forge version +/// +/// # Returns +/// `true` if the version JSON exists, `false` otherwise. +pub fn is_forge_installed(game_dir: &PathBuf, game_version: &str, forge_version: &str) -> bool { + let version_id = generate_version_id(game_version, forge_version); + let json_path = game_dir + .join("versions") + .join(&version_id) + .join(format!("{}.json", version_id)); + json_path.exists() +} + +/// List all installed Forge versions in the game directory. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// +/// # Returns +/// A list of installed Forge version IDs. +pub async fn list_installed_forge_versions( + game_dir: &PathBuf, +) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> { + let versions_dir = game_dir.join("versions"); + let mut installed = Vec::new(); + + if !versions_dir.exists() { + return Ok(installed); + } + + let mut entries = tokio::fs::read_dir(&versions_dir).await?; + while let Some(entry) = entries.next_entry().await? { + let name = entry.file_name().to_string_lossy().to_string(); + if name.contains("-forge-") { + // Verify the JSON file exists + let json_path = entry.path().join(format!("{}.json", name)); + if json_path.exists() { + installed.push(name); + } + } + } + + Ok(installed) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_generate_version_id() { + assert_eq!( + generate_version_id("1.20.4", "49.0.38"), + "1.20.4-forge-49.0.38" + ); + } + + #[test] + fn test_is_modern_forge() { + assert!(!is_modern_forge("1.12.2")); + assert!(is_modern_forge("1.13")); + assert!(is_modern_forge("1.20.4")); + assert!(is_modern_forge("1.21")); + } +} diff --git a/src-tauri/src/core/game_version.rs b/src-tauri/src/core/game_version.rs index 572882f..c62e232 100644 --- a/src-tauri/src/core/game_version.rs +++ b/src-tauri/src/core/game_version.rs @@ -1,11 +1,15 @@ -use serde::Deserialize; +use serde::{Deserialize, Serialize}; -#[derive(Debug, Deserialize)] +/// Represents a Minecraft version JSON, supporting both vanilla and modded (Fabric/Forge) formats. +/// Modded versions use `inheritsFrom` to reference a parent vanilla version. +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct GameVersion { pub id: String, - pub downloads: Downloads, + /// Optional for mod loaders that inherit from vanilla + pub downloads: Option<Downloads>, + /// Optional for mod loaders that inherit from vanilla #[serde(rename = "assetIndex")] - pub asset_index: AssetIndex, + pub asset_index: Option<AssetIndex>, pub libraries: Vec<Library>, #[serde(rename = "mainClass")] pub main_class: String, @@ -14,66 +18,77 @@ pub struct GameVersion { pub arguments: Option<Arguments>, #[serde(rename = "javaVersion")] pub java_version: Option<JavaVersion>, + /// For mod loaders: the vanilla version this inherits from + #[serde(rename = "inheritsFrom")] + pub inherits_from: Option<String>, + /// Fabric/Forge may specify a custom assets version + pub assets: Option<String>, + /// Release type (release, snapshot, old_beta, etc.) + #[serde(rename = "type")] + pub version_type: Option<String>, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct Downloads { pub client: DownloadArtifact, pub server: Option<DownloadArtifact>, } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct DownloadArtifact { - pub sha1: String, - pub size: u64, + pub sha1: Option<String>, + pub size: Option<u64>, pub url: String, pub path: Option<String>, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct AssetIndex { pub id: String, pub sha1: String, pub size: u64, pub url: String, #[serde(rename = "totalSize")] - pub total_size: u64, + pub total_size: Option<u64>, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct Library { pub downloads: Option<LibraryDownloads>, pub name: String, pub rules: Option<Vec<Rule>>, pub natives: Option<serde_json::Value>, + /// Maven repository URL for mod loader libraries + pub url: Option<String>, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct Rule { pub action: String, // "allow" or "disallow" pub os: Option<OsRule>, + pub features: Option<serde_json::Value>, // Feature-based rules (e.g., is_demo_user, has_quick_plays_support) } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct OsRule { pub name: Option<String>, // "linux", "osx", "windows" pub version: Option<String>, // Regex pub arch: Option<String>, // "x86" } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct LibraryDownloads { pub artifact: Option<DownloadArtifact>, pub classifiers: Option<serde_json::Value>, // Complex, simplifying for now } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct Arguments { pub game: Option<serde_json::Value>, pub jvm: Option<serde_json::Value>, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct JavaVersion { pub component: String, #[serde(rename = "majorVersion")] diff --git a/src-tauri/src/core/java.rs b/src-tauri/src/core/java.rs index 9cf3053..8341138 100644 --- a/src-tauri/src/core/java.rs +++ b/src-tauri/src/core/java.rs @@ -1,6 +1,15 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; use std::process::Command; +use tauri::AppHandle; +use tauri::Emitter; +use tauri::Manager; + +use crate::core::downloader::{self, JavaDownloadProgress, DownloadQueue, PendingJavaDownload}; +use crate::utils::zip; + +const ADOPTIUM_API_BASE: &str = "https://api.adoptium.net/v3"; +const CACHE_DURATION_SECS: u64 = 24 * 60 * 60; // 24 hours #[derive(Debug, Clone, Serialize, Deserialize)] pub struct JavaInstallation { @@ -9,6 +18,590 @@ pub struct JavaInstallation { pub is_64bit: bool, } +/// Java image type: JRE or JDK +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ImageType { + Jre, + Jdk, +} + +impl Default for ImageType { + fn default() -> Self { + Self::Jre + } +} + +impl std::fmt::Display for ImageType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Jre => write!(f, "jre"), + Self::Jdk => write!(f, "jdk"), + } + } +} + +/// Java release information for UI display +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct JavaReleaseInfo { + pub major_version: u32, + pub image_type: String, + pub version: String, + pub release_name: String, + pub release_date: Option<String>, + pub file_size: u64, + pub checksum: Option<String>, + pub download_url: String, + pub is_lts: bool, + pub is_available: bool, + pub architecture: String, +} + +/// Java catalog containing all available versions +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct JavaCatalog { + pub releases: Vec<JavaReleaseInfo>, + pub available_major_versions: Vec<u32>, + pub lts_versions: Vec<u32>, + pub cached_at: u64, +} + +impl Default for JavaCatalog { + fn default() -> Self { + Self { + releases: Vec::new(), + available_major_versions: Vec::new(), + lts_versions: Vec::new(), + cached_at: 0, + } + } +} + +/// Adoptium `/v3/assets/latest/{version}/hotspot` API response structures +#[derive(Debug, Clone, Deserialize)] +pub struct AdoptiumAsset { + pub binary: AdoptiumBinary, + pub release_name: String, + pub version: AdoptiumVersionData, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct AdoptiumBinary { + pub os: String, + pub architecture: String, + pub image_type: String, + pub package: AdoptiumPackage, + #[serde(default)] + pub updated_at: Option<String>, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct AdoptiumPackage { + pub name: String, + pub link: String, + pub size: u64, + pub checksum: Option<String>, // SHA256 +} + +#[derive(Debug, Clone, Deserialize)] +pub struct AdoptiumVersionData { + pub major: u32, + pub minor: u32, + pub security: u32, + pub semver: String, + pub openjdk_version: String, +} + +/// Adoptium available releases response +#[derive(Debug, Clone, Deserialize)] +pub struct AvailableReleases { + pub available_releases: Vec<u32>, + pub available_lts_releases: Vec<u32>, + pub most_recent_lts: Option<u32>, + pub most_recent_feature_release: Option<u32>, +} + +/// Java download information from Adoptium +#[derive(Debug, Clone, Serialize)] +pub struct JavaDownloadInfo { + pub version: String, + pub release_name: String, + pub download_url: String, + pub file_name: String, + pub file_size: u64, + pub checksum: Option<String>, + pub image_type: String, +} + +/// Get the Adoptium OS name for the current platform +pub fn get_adoptium_os() -> &'static str { + #[cfg(target_os = "linux")] + { + // Check if Alpine Linux (musl libc) + if std::path::Path::new("/etc/alpine-release").exists() { + return "alpine-linux"; + } + "linux" + } + #[cfg(target_os = "macos")] + { + "mac" + } + #[cfg(target_os = "windows")] + { + "windows" + } + #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] + { + "linux" // fallback + } +} + +/// Get the Adoptium Architecture name for the current architecture +pub fn get_adoptium_arch() -> &'static str { + #[cfg(target_arch = "x86_64")] + { + "x64" + } + #[cfg(target_arch = "aarch64")] + { + "aarch64" + } + #[cfg(target_arch = "x86")] + { + "x86" + } + #[cfg(target_arch = "arm")] + { + "arm" + } + #[cfg(not(any( + target_arch = "x86_64", + target_arch = "aarch64", + target_arch = "x86", + target_arch = "arm" + )))] + { + "x64" // fallback + } +} + +/// Get the default Java installation directory for DropOut +pub fn get_java_install_dir(app_handle: &AppHandle) -> PathBuf { + app_handle.path().app_data_dir().unwrap().join("java") +} + +/// Get the cache file path for Java catalog +fn get_catalog_cache_path(app_handle: &AppHandle) -> PathBuf { + app_handle + .path() + .app_data_dir() + .unwrap() + .join("java_catalog_cache.json") +} + +/// Load cached Java catalog if not expired +pub fn load_cached_catalog(app_handle: &AppHandle) -> Option<JavaCatalog> { + let cache_path = get_catalog_cache_path(app_handle); + if !cache_path.exists() { + return None; + } + + let content = std::fs::read_to_string(&cache_path).ok()?; + let catalog: JavaCatalog = serde_json::from_str(&content).ok()?; + + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + + if now - catalog.cached_at < CACHE_DURATION_SECS { + Some(catalog) + } else { + None + } +} + +/// Save Java catalog to cache +pub fn save_catalog_cache(app_handle: &AppHandle, catalog: &JavaCatalog) -> Result<(), String> { + let cache_path = get_catalog_cache_path(app_handle); + let content = serde_json::to_string_pretty(catalog).map_err(|e| e.to_string())?; + std::fs::write(&cache_path, content).map_err(|e| e.to_string())?; + Ok(()) +} + +/// Clear Java catalog cache +pub fn clear_catalog_cache(app_handle: &AppHandle) -> Result<(), String> { + let cache_path = get_catalog_cache_path(app_handle); + if cache_path.exists() { + std::fs::remove_file(&cache_path).map_err(|e| e.to_string())?; + } + Ok(()) +} + +/// Fetch complete Java catalog from Adoptium API with platform availability check +pub async fn fetch_java_catalog(app_handle: &AppHandle, force_refresh: bool) -> Result<JavaCatalog, String> { + // Check cache first unless force refresh + if !force_refresh { + if let Some(cached) = load_cached_catalog(app_handle) { + return Ok(cached); + } + } + + let os = get_adoptium_os(); + let arch = get_adoptium_arch(); + let client = reqwest::Client::new(); + + // 1. Fetch available releases + let releases_url = format!("{}/info/available_releases", ADOPTIUM_API_BASE); + let available: AvailableReleases = client + .get(&releases_url) + .header("Accept", "application/json") + .send() + .await + .map_err(|e| format!("Failed to fetch available releases: {}", e))? + .json() + .await + .map_err(|e| format!("Failed to parse available releases: {}", e))?; + + let mut releases = Vec::new(); + + // 2. Fetch details for each major version + for major_version in &available.available_releases { + for image_type in &["jre", "jdk"] { + let url = format!( + "{}/assets/latest/{}/hotspot?os={}&architecture={}&image_type={}", + ADOPTIUM_API_BASE, major_version, os, arch, image_type + ); + + match client + .get(&url) + .header("Accept", "application/json") + .send() + .await + { + Ok(response) => { + if response.status().is_success() { + if let Ok(assets) = response.json::<Vec<AdoptiumAsset>>().await { + if let Some(asset) = assets.into_iter().next() { + let release_date = asset.binary.updated_at.clone(); + releases.push(JavaReleaseInfo { + major_version: *major_version, + image_type: image_type.to_string(), + version: asset.version.semver.clone(), + release_name: asset.release_name.clone(), + release_date, + file_size: asset.binary.package.size, + checksum: asset.binary.package.checksum, + download_url: asset.binary.package.link, + is_lts: available.available_lts_releases.contains(major_version), + is_available: true, + architecture: asset.binary.architecture.clone(), + }); + } + } + } else { + // Platform not available for this version/type + releases.push(JavaReleaseInfo { + major_version: *major_version, + image_type: image_type.to_string(), + version: format!("{}.x", major_version), + release_name: format!("jdk-{}", major_version), + release_date: None, + file_size: 0, + checksum: None, + download_url: String::new(), + is_lts: available.available_lts_releases.contains(major_version), + is_available: false, + architecture: arch.to_string(), + }); + } + } + Err(_) => { + // Network error, mark as unavailable + releases.push(JavaReleaseInfo { + major_version: *major_version, + image_type: image_type.to_string(), + version: format!("{}.x", major_version), + release_name: format!("jdk-{}", major_version), + release_date: None, + file_size: 0, + checksum: None, + download_url: String::new(), + is_lts: available.available_lts_releases.contains(major_version), + is_available: false, + architecture: arch.to_string(), + }); + } + } + } + } + + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + + let catalog = JavaCatalog { + releases, + available_major_versions: available.available_releases, + lts_versions: available.available_lts_releases, + cached_at: now, + }; + + // Save to cache + let _ = save_catalog_cache(app_handle, &catalog); + + Ok(catalog) +} + +/// Get Adoptium API download info for a specific Java version and image type +/// +/// # Arguments +/// * `major_version` - Java major version (e.g., 8, 11, 17) +/// * `image_type` - JRE or JDK +/// +/// # Returns +/// * `Ok(JavaDownloadInfo)` - Download information +/// * `Err(String)` - Error message +pub async fn fetch_java_release( + major_version: u32, + image_type: ImageType, +) -> Result<JavaDownloadInfo, String> { + let os = get_adoptium_os(); + let arch = get_adoptium_arch(); + + let url = format!( + "{}/assets/latest/{}/hotspot?os={}&architecture={}&image_type={}", + ADOPTIUM_API_BASE, major_version, os, arch, image_type + ); + + let client = reqwest::Client::new(); + let response = client + .get(&url) + .header("Accept", "application/json") + .send() + .await + .map_err(|e| format!("Network request failed: {}", e))?; + + if !response.status().is_success() { + return Err(format!( + "Adoptium API returned error: {} - The version/platform might be unavailable", + response.status() + )); + } + + let assets: Vec<AdoptiumAsset> = response + .json() + .await + .map_err(|e| format!("Failed to parse API response: {}", e))?; + + let asset = assets + .into_iter() + .next() + .ok_or_else(|| format!("Java {} {} download not found", major_version, image_type))?; + + Ok(JavaDownloadInfo { + version: asset.version.semver.clone(), + release_name: asset.release_name, + download_url: asset.binary.package.link, + file_name: asset.binary.package.name, + file_size: asset.binary.package.size, + checksum: asset.binary.package.checksum, + image_type: asset.binary.image_type, + }) +} + +/// Fetch available Java versions from Adoptium API +pub async fn fetch_available_versions() -> Result<Vec<u32>, String> { + let url = format!("{}/info/available_releases", ADOPTIUM_API_BASE); + + let response = reqwest::get(url) + .await + .map_err(|e| format!("Network request failed: {}", e))?; + + #[derive(Deserialize)] + struct AvailableReleases { + available_releases: Vec<u32>, + } + + let releases: AvailableReleases = response + .json() + .await + .map_err(|e| format!("Failed to parse response: {}", e))?; + + Ok(releases.available_releases) +} + +/// Download and install Java with resume support and progress events +/// +/// # Arguments +/// * `app_handle` - Tauri app handle for accessing app directories +/// * `major_version` - Java major version (e.g., 8, 11, 17) +/// * `image_type` - JRE or JDK +/// * `custom_path` - Optional custom installation path +/// +/// # Returns +/// * `Ok(JavaInstallation)` - Information about the successfully installed Java +pub async fn download_and_install_java( + app_handle: &AppHandle, + major_version: u32, + image_type: ImageType, + custom_path: Option<PathBuf>, +) -> Result<JavaInstallation, String> { + // 1. Fetch download information + let info = fetch_java_release(major_version, image_type).await?; + let file_name = info.file_name.clone(); + + // 2. Prepare installation directory + let install_base = custom_path.unwrap_or_else(|| get_java_install_dir(app_handle)); + let version_dir = install_base.join(format!("temurin-{}-{}", major_version, image_type)); + + std::fs::create_dir_all(&install_base) + .map_err(|e| format!("Failed to create installation directory: {}", e))?; + + // 3. Add to download queue for persistence + let mut queue = DownloadQueue::load(app_handle); + queue.add(PendingJavaDownload { + major_version, + image_type: image_type.to_string(), + download_url: info.download_url.clone(), + file_name: info.file_name.clone(), + file_size: info.file_size, + checksum: info.checksum.clone(), + install_path: install_base.to_string_lossy().to_string(), + created_at: std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(), + }); + queue.save(app_handle)?; + + // 4. Download the archive with resume support + let archive_path = install_base.join(&info.file_name); + + // Check if we need to download + let need_download = if archive_path.exists() { + if let Some(expected_checksum) = &info.checksum { + let data = std::fs::read(&archive_path) + .map_err(|e| format!("Failed to read downloaded file: {}", e))?; + !downloader::verify_checksum(&data, Some(expected_checksum), None) + } else { + false + } + } else { + true + }; + + if need_download { + // Use resumable download + downloader::download_with_resume( + app_handle, + &info.download_url, + &archive_path, + info.checksum.as_deref(), + info.file_size, + ) + .await?; + } + + // 5. Emit extracting status + let _ = app_handle.emit( + "java-download-progress", + JavaDownloadProgress { + file_name: file_name.clone(), + downloaded_bytes: info.file_size, + total_bytes: info.file_size, + speed_bytes_per_sec: 0, + eta_seconds: 0, + status: "Extracting".to_string(), + percentage: 100.0, + }, + ); + + // 6. Extract + // If the target directory exists, remove it first + if version_dir.exists() { + std::fs::remove_dir_all(&version_dir) + .map_err(|e| format!("Failed to remove old version directory: {}", e))?; + } + + std::fs::create_dir_all(&version_dir) + .map_err(|e| format!("Failed to create version directory: {}", e))?; + + let top_level_dir = if info.file_name.ends_with(".tar.gz") || info.file_name.ends_with(".tgz") { + zip::extract_tar_gz(&archive_path, &version_dir)? + } else if info.file_name.ends_with(".zip") { + zip::extract_zip(&archive_path, &version_dir)?; + // Find the top-level directory inside the extracted folder + find_top_level_dir(&version_dir)? + } else { + return Err(format!("Unsupported archive format: {}", info.file_name)); + }; + + // 7. Clean up downloaded archive + let _ = std::fs::remove_file(&archive_path); + + // 8. Locate java executable + // macOS has a different structure: jdk-xxx/Contents/Home/bin/java + // Linux/Windows: jdk-xxx/bin/java + let java_home = version_dir.join(&top_level_dir); + let java_bin = if cfg!(target_os = "macos") { + java_home.join("Contents").join("Home").join("bin").join("java") + } else if cfg!(windows) { + java_home.join("bin").join("java.exe") + } else { + java_home.join("bin").join("java") + }; + + if !java_bin.exists() { + return Err(format!( + "Installation completed but Java executable not found: {}", + java_bin.display() + )); + } + + // 9. Verify installation + let installation = check_java_installation(&java_bin) + .ok_or_else(|| "Failed to verify Java installation".to_string())?; + + // 10. Remove from download queue + queue.remove(major_version, &image_type.to_string()); + queue.save(app_handle)?; + + // 11. Emit completed status + let _ = app_handle.emit( + "java-download-progress", + JavaDownloadProgress { + file_name, + downloaded_bytes: info.file_size, + total_bytes: info.file_size, + speed_bytes_per_sec: 0, + eta_seconds: 0, + status: "Completed".to_string(), + percentage: 100.0, + }, + ); + + Ok(installation) +} + +/// Find the top-level directory inside the extracted folder +fn find_top_level_dir(extract_dir: &PathBuf) -> Result<String, String> { + let entries: Vec<_> = std::fs::read_dir(extract_dir) + .map_err(|e| format!("Failed to read directory: {}", e))? + .filter_map(|e| e.ok()) + .filter(|e| e.path().is_dir()) + .collect(); + + if entries.len() == 1 { + Ok(entries[0].file_name().to_string_lossy().to_string()) + } else { + // No single top-level directory, return empty string + Ok(String::new()) + } +} + /// Detect Java installations on the system pub fn detect_java_installations() -> Vec<JavaInstallation> { let mut installations = Vec::new(); @@ -256,3 +849,137 @@ pub fn get_recommended_java(required_major_version: Option<u64>) -> Option<JavaI installations.into_iter().next() } } + +/// Detect all installed Java versions (including system installations and DropOut downloads) +pub fn detect_all_java_installations(app_handle: &AppHandle) -> Vec<JavaInstallation> { + let mut installations = detect_java_installations(); + + // Add DropOut downloaded Java versions + let dropout_java_dir = get_java_install_dir(app_handle); + if dropout_java_dir.exists() { + if let Ok(entries) = std::fs::read_dir(&dropout_java_dir) { + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() { + // Find the java executable in this directory + let java_bin = find_java_executable(&path); + if let Some(java_path) = java_bin { + if let Some(java) = check_java_installation(&java_path) { + if !installations.iter().any(|j| j.path == java.path) { + installations.push(java); + } + } + } + } + } + } + } + + // Sort by version + installations.sort_by(|a, b| { + let v_a = parse_java_version(&a.version); + let v_b = parse_java_version(&b.version); + v_b.cmp(&v_a) + }); + + installations +} + +//// Find the java executable in a directory using a limited-depth search +fn find_java_executable(dir: &PathBuf) -> Option<PathBuf> { + let bin_name = if cfg!(windows) { "java.exe" } else { "java" }; + + // Directly look in the bin directory + let direct_bin = dir.join("bin").join(bin_name); + if direct_bin.exists() { + return Some(direct_bin); + } + + // macOS: Contents/Home/bin/java + #[cfg(target_os = "macos")] + { + let macos_bin = dir.join("Contents").join("Home").join("bin").join(bin_name); + if macos_bin.exists() { + return Some(macos_bin); + } + } + + // Look in subdirectories (handle nested directories after Adoptium extraction) + if let Ok(entries) = std::fs::read_dir(dir) { + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() { + // Try direct bin path + let nested_bin = path.join("bin").join(bin_name); + if nested_bin.exists() { + return Some(nested_bin); + } + + // macOS: nested/Contents/Home/bin/java + #[cfg(target_os = "macos")] + { + let macos_nested = path.join("Contents").join("Home").join("bin").join(bin_name); + if macos_nested.exists() { + return Some(macos_nested); + } + } + } + } + } + + None +} + +/// Resume pending Java downloads from queue +pub async fn resume_pending_downloads(app_handle: &AppHandle) -> Result<Vec<JavaInstallation>, String> { + let queue = DownloadQueue::load(app_handle); + let mut installed = Vec::new(); + + for pending in queue.pending_downloads.iter() { + let image_type = if pending.image_type == "jdk" { + ImageType::Jdk + } else { + ImageType::Jre + }; + + // Try to resume the download + match download_and_install_java( + app_handle, + pending.major_version, + image_type, + Some(PathBuf::from(&pending.install_path)), + ) + .await + { + Ok(installation) => { + installed.push(installation); + } + Err(e) => { + eprintln!( + "Failed to resume Java {} {} download: {}", + pending.major_version, pending.image_type, e + ); + } + } + } + + Ok(installed) +} + +/// Cancel current Java download +pub fn cancel_current_download() { + downloader::cancel_java_download(); +} + +/// Get pending downloads from queue +pub fn get_pending_downloads(app_handle: &AppHandle) -> Vec<PendingJavaDownload> { + let queue = DownloadQueue::load(app_handle); + queue.pending_downloads +} + +/// Clear a specific pending download +pub fn clear_pending_download(app_handle: &AppHandle, major_version: u32, image_type: &str) -> Result<(), String> { + let mut queue = DownloadQueue::load(app_handle); + queue.remove(major_version, image_type); + queue.save(app_handle) +} diff --git a/src-tauri/src/core/manifest.rs b/src-tauri/src/core/manifest.rs index 11ebc5a..bae87c9 100644 --- a/src-tauri/src/core/manifest.rs +++ b/src-tauri/src/core/manifest.rs @@ -1,5 +1,8 @@ use serde::{Deserialize, Serialize}; use std::error::Error; +use std::path::PathBuf; + +use crate::core::game_version::GameVersion; #[derive(Debug, Deserialize, Serialize)] pub struct VersionManifest { @@ -24,8 +27,157 @@ pub struct Version { pub release_time: String, } -pub async fn fetch_version_manifest() -> Result<VersionManifest, Box<dyn Error>> { +pub async fn fetch_version_manifest() -> Result<VersionManifest, Box<dyn Error + Send + Sync>> { let url = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"; let resp = reqwest::get(url).await?.json::<VersionManifest>().await?; Ok(resp) } + +/// Load a version JSON from the local versions directory. +/// +/// This is used for loading both vanilla and modded versions that have been +/// previously downloaded or installed. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// * `version_id` - The version ID to load +/// +/// # Returns +/// The parsed `GameVersion` if found, or an error if not found. +pub async fn load_local_version( + game_dir: &PathBuf, + version_id: &str, +) -> Result<GameVersion, Box<dyn Error + Send + Sync>> { + let json_path = game_dir + .join("versions") + .join(version_id) + .join(format!("{}.json", version_id)); + + if !json_path.exists() { + return Err(format!("Version {} not found locally", version_id).into()); + } + + let content = tokio::fs::read_to_string(&json_path).await?; + let version: GameVersion = serde_json::from_str(&content)?; + Ok(version) +} + +/// Fetch a version JSON from Mojang's servers. +/// +/// # Arguments +/// * `version_id` - The version ID to fetch +/// +/// # Returns +/// The parsed `GameVersion` from Mojang's API. +pub async fn fetch_vanilla_version( + version_id: &str, +) -> Result<GameVersion, Box<dyn Error + Send + Sync>> { + // First, get the manifest to find the version URL + let manifest = fetch_version_manifest().await?; + + let version_entry = manifest + .versions + .iter() + .find(|v| v.id == version_id) + .ok_or_else(|| format!("Version {} not found in manifest", version_id))?; + + // Fetch the actual version JSON + let resp = reqwest::get(&version_entry.url) + .await? + .json::<GameVersion>() + .await?; + + Ok(resp) +} + +/// Load a version, checking local first, then fetching from remote if needed. +/// +/// For modded versions (those with `inheritsFrom`), this will also resolve +/// the inheritance chain. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// * `version_id` - The version ID to load +/// +/// # Returns +/// A fully resolved `GameVersion` ready for launching. +pub async fn load_version( + game_dir: &PathBuf, + version_id: &str, +) -> Result<GameVersion, Box<dyn Error + Send + Sync>> { + // Try loading from local first + let mut version = match load_local_version(game_dir, version_id).await { + Ok(v) => v, + Err(_) => { + // Not found locally, try fetching from Mojang + fetch_vanilla_version(version_id).await? + } + }; + + // If this version inherits from another, resolve the inheritance iteratively + while let Some(parent_id) = version.inherits_from.clone() { + // Load the parent version + let parent = match load_local_version(game_dir, &parent_id).await { + Ok(v) => v, + Err(_) => fetch_vanilla_version(&parent_id).await?, + }; + + // Merge child into parent + version = crate::core::version_merge::merge_versions(version, parent); + } + + Ok(version) +} + +/// Save a version JSON to the local versions directory. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// * `version` - The version to save +/// +/// # Returns +/// The path where the JSON was saved. +pub async fn save_local_version( + game_dir: &PathBuf, + version: &GameVersion, +) -> Result<PathBuf, Box<dyn Error + Send + Sync>> { + let version_dir = game_dir.join("versions").join(&version.id); + tokio::fs::create_dir_all(&version_dir).await?; + + let json_path = version_dir.join(format!("{}.json", version.id)); + let content = serde_json::to_string_pretty(version)?; + tokio::fs::write(&json_path, content).await?; + + Ok(json_path) +} + +/// List all locally installed versions. +/// +/// # Arguments +/// * `game_dir` - The .minecraft directory path +/// +/// # Returns +/// A list of version IDs found in the versions directory. +pub async fn list_local_versions( + game_dir: &PathBuf, +) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> { + let versions_dir = game_dir.join("versions"); + let mut versions = Vec::new(); + + if !versions_dir.exists() { + return Ok(versions); + } + + let mut entries = tokio::fs::read_dir(&versions_dir).await?; + while let Some(entry) = entries.next_entry().await? { + if entry.file_type().await?.is_dir() { + let name = entry.file_name().to_string_lossy().to_string(); + let json_path = entry.path().join(format!("{}.json", name)); + if json_path.exists() { + versions.push(name); + } + } + } + + Ok(versions) +} diff --git a/src-tauri/src/core/maven.rs b/src-tauri/src/core/maven.rs new file mode 100644 index 0000000..8c89768 --- /dev/null +++ b/src-tauri/src/core/maven.rs @@ -0,0 +1,263 @@ +//! Maven coordinate parsing and URL construction utilities. +//! +//! Mod loaders like Fabric and Forge specify libraries using Maven coordinates +//! (e.g., `net.fabricmc:fabric-loader:0.14.21`) instead of direct download URLs. +//! This module provides utilities to parse these coordinates and construct +//! download URLs for various Maven repositories. + +use std::path::PathBuf; + +/// Known Maven repository URLs for mod loaders +pub const MAVEN_CENTRAL: &str = "https://repo1.maven.org/maven2/"; +pub const FABRIC_MAVEN: &str = "https://maven.fabricmc.net/"; +pub const FORGE_MAVEN: &str = "https://maven.minecraftforge.net/"; +pub const MOJANG_LIBRARIES: &str = "https://libraries.minecraft.net/"; + +/// Represents a parsed Maven coordinate. +/// +/// Maven coordinates follow the format: `group:artifact:version[:classifier][@extension]` +/// Examples: +/// - `net.fabricmc:fabric-loader:0.14.21` +/// - `org.lwjgl:lwjgl:3.3.1:natives-linux` +/// - `com.example:artifact:1.0@zip` +#[derive(Debug, Clone, PartialEq)] +pub struct MavenCoordinate { + pub group: String, + pub artifact: String, + pub version: String, + pub classifier: Option<String>, + pub extension: String, +} + +impl MavenCoordinate { + /// Parse a Maven coordinate string. + /// + /// # Arguments + /// * `coord` - A string in the format `group:artifact:version[:classifier][@extension]` + /// + /// # Returns + /// * `Some(MavenCoordinate)` if parsing succeeds + /// * `None` if the format is invalid + /// + /// # Examples + /// ``` + /// let coord = MavenCoordinate::parse("net.fabricmc:fabric-loader:0.14.21").unwrap(); + /// assert_eq!(coord.group, "net.fabricmc"); + /// assert_eq!(coord.artifact, "fabric-loader"); + /// assert_eq!(coord.version, "0.14.21"); + /// ``` + pub fn parse(coord: &str) -> Option<Self> { + // Handle extension suffix (e.g., @zip) + let (coord_part, extension) = if let Some(at_idx) = coord.rfind('@') { + let ext = &coord[at_idx + 1..]; + let base = &coord[..at_idx]; + (base, ext.to_string()) + } else { + (coord, "jar".to_string()) + }; + + let parts: Vec<&str> = coord_part.split(':').collect(); + + match parts.len() { + 3 => Some(MavenCoordinate { + group: parts[0].to_string(), + artifact: parts[1].to_string(), + version: parts[2].to_string(), + classifier: None, + extension, + }), + 4 => Some(MavenCoordinate { + group: parts[0].to_string(), + artifact: parts[1].to_string(), + version: parts[2].to_string(), + classifier: Some(parts[3].to_string()), + extension, + }), + _ => None, + } + } + + /// Get the relative path for this artifact in a Maven repository. + /// + /// # Returns + /// The path as `group/artifact/version/artifact-version[-classifier].extension` + /// + /// # Examples + /// ``` + /// let coord = MavenCoordinate::parse("net.fabricmc:fabric-loader:0.14.21").unwrap(); + /// assert_eq!(coord.to_path(), "net/fabricmc/fabric-loader/0.14.21/fabric-loader-0.14.21.jar"); + /// ``` + pub fn to_path(&self) -> String { + let group_path = self.group.replace('.', "/"); + let filename = match &self.classifier { + Some(classifier) => { + format!( + "{}-{}-{}.{}", + self.artifact, self.version, classifier, self.extension + ) + } + None => { + format!("{}-{}.{}", self.artifact, self.version, self.extension) + } + }; + + format!( + "{}/{}/{}/{}", + group_path, self.artifact, self.version, filename + ) + } + + /// Get the local file path for storing this artifact. + /// + /// # Arguments + /// * `libraries_dir` - The base libraries directory + /// + /// # Returns + /// The full path where the library should be stored + pub fn to_local_path(&self, libraries_dir: &PathBuf) -> PathBuf { + let rel_path = self.to_path(); + libraries_dir.join(rel_path.replace('/', std::path::MAIN_SEPARATOR_STR)) + } + + /// Construct the full download URL for this artifact. + /// + /// # Arguments + /// * `base_url` - The Maven repository base URL (e.g., `https://maven.fabricmc.net/`) + /// + /// # Returns + /// The full URL to download the artifact + pub fn to_url(&self, base_url: &str) -> String { + let base = base_url.trim_end_matches('/'); + format!("{}/{}", base, self.to_path()) + } +} + +/// Resolve the download URL for a library. +/// +/// This function handles both: +/// 1. Libraries with explicit download URLs (vanilla Minecraft) +/// 2. Libraries with only Maven coordinates (Fabric/Forge) +/// +/// # Arguments +/// * `name` - The Maven coordinate string +/// * `explicit_url` - An explicit download URL if provided in the library JSON +/// * `maven_url` - A custom Maven repository URL from the library JSON +/// +/// # Returns +/// The resolved download URL +pub fn resolve_library_url( + name: &str, + explicit_url: Option<&str>, + maven_url: Option<&str>, +) -> Option<String> { + // If there's an explicit URL, use it + if let Some(url) = explicit_url { + return Some(url.to_string()); + } + + // Parse the Maven coordinate + let coord = MavenCoordinate::parse(name)?; + + // Determine the base Maven URL + let base_url = maven_url.unwrap_or_else(|| { + // Guess the repository based on group + if coord.group.starts_with("net.fabricmc") { + FABRIC_MAVEN + } else if coord.group.starts_with("net.minecraftforge") + || coord.group.starts_with("cpw.mods") + { + FORGE_MAVEN + } else { + MOJANG_LIBRARIES + } + }); + + Some(coord.to_url(base_url)) +} + +/// Get the local storage path for a library. +/// +/// # Arguments +/// * `name` - The Maven coordinate string +/// * `libraries_dir` - The base libraries directory +/// +/// # Returns +/// The path where the library should be stored +pub fn get_library_path(name: &str, libraries_dir: &PathBuf) -> Option<PathBuf> { + let coord = MavenCoordinate::parse(name)?; + Some(coord.to_local_path(libraries_dir)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_simple_coordinate() { + let coord = MavenCoordinate::parse("net.fabricmc:fabric-loader:0.14.21").unwrap(); + assert_eq!(coord.group, "net.fabricmc"); + assert_eq!(coord.artifact, "fabric-loader"); + assert_eq!(coord.version, "0.14.21"); + assert_eq!(coord.classifier, None); + assert_eq!(coord.extension, "jar"); + } + + #[test] + fn test_parse_with_classifier() { + let coord = MavenCoordinate::parse("org.lwjgl:lwjgl:3.3.1:natives-linux").unwrap(); + assert_eq!(coord.group, "org.lwjgl"); + assert_eq!(coord.artifact, "lwjgl"); + assert_eq!(coord.version, "3.3.1"); + assert_eq!(coord.classifier, Some("natives-linux".to_string())); + assert_eq!(coord.extension, "jar"); + } + + #[test] + fn test_parse_with_extension() { + let coord = MavenCoordinate::parse("com.example:artifact:1.0@zip").unwrap(); + assert_eq!(coord.extension, "zip"); + } + + #[test] + fn test_to_path() { + let coord = MavenCoordinate::parse("net.fabricmc:fabric-loader:0.14.21").unwrap(); + assert_eq!( + coord.to_path(), + "net/fabricmc/fabric-loader/0.14.21/fabric-loader-0.14.21.jar" + ); + } + + #[test] + fn test_to_path_with_classifier() { + let coord = MavenCoordinate::parse("org.lwjgl:lwjgl:3.3.1:natives-linux").unwrap(); + assert_eq!( + coord.to_path(), + "org/lwjgl/lwjgl/3.3.1/lwjgl-3.3.1-natives-linux.jar" + ); + } + + #[test] + fn test_to_url() { + let coord = MavenCoordinate::parse("net.fabricmc:fabric-loader:0.14.21").unwrap(); + assert_eq!( + coord.to_url(FABRIC_MAVEN), + "https://maven.fabricmc.net/net/fabricmc/fabric-loader/0.14.21/fabric-loader-0.14.21.jar" + ); + } + + #[test] + fn test_resolve_library_url_explicit() { + let url = resolve_library_url( + "net.fabricmc:fabric-loader:0.14.21", + Some("https://example.com/lib.jar"), + None, + ); + assert_eq!(url, Some("https://example.com/lib.jar".to_string())); + } + + #[test] + fn test_resolve_library_url_fabric() { + let url = resolve_library_url("net.fabricmc:fabric-loader:0.14.21", None, None); + assert!(url.unwrap().starts_with(FABRIC_MAVEN)); + } +} diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index 475a304..3c09a76 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -2,7 +2,11 @@ pub mod account_storage; pub mod auth; pub mod config; pub mod downloader; +pub mod fabric; +pub mod forge; pub mod game_version; pub mod java; pub mod manifest; +pub mod maven; pub mod rules; +pub mod version_merge; diff --git a/src-tauri/src/core/rules.rs b/src-tauri/src/core/rules.rs index 877982a..71abda5 100644 --- a/src-tauri/src/core/rules.rs +++ b/src-tauri/src/core/rules.rs @@ -47,6 +47,13 @@ pub fn is_library_allowed(rules: &Option<Vec<Rule>>) -> bool { } fn rule_matches(rule: &Rule) -> bool { + // Feature-based rules (e.g., is_demo_user, has_quick_plays_support, is_quick_play_singleplayer) + // are not implemented in this launcher, so we return false for any rule that has features. + // This prevents adding arguments like --demo, --quickPlayPath, --quickPlaySingleplayer, etc. + if rule.features.is_some() { + return false; + } + match &rule.os { None => true, // No OS condition means it applies to all Some(os_rule) => { diff --git a/src-tauri/src/core/version_merge.rs b/src-tauri/src/core/version_merge.rs new file mode 100644 index 0000000..fe6b3cd --- /dev/null +++ b/src-tauri/src/core/version_merge.rs @@ -0,0 +1,244 @@ +//! Version merging utilities for mod loaders. +//! +//! Mod loaders like Fabric and Forge create "partial" version JSON files that +//! inherit from vanilla Minecraft versions via the `inheritsFrom` field. +//! This module provides functionality to merge these partial versions with +//! their parent versions to create a complete, launchable version profile. + +use crate::core::game_version::{Arguments, GameVersion}; +use std::error::Error; + +/// Merge a child version (mod loader) with its parent version (vanilla). +/// +/// The merging follows these rules: +/// 1. Child's `mainClass` overrides parent's +/// 2. Child's libraries are prepended to parent's (mod loader classes take priority) +/// 3. Arguments are merged (child's additions come after parent's) +/// 4. Parent provides `downloads`, `assetIndex`, `javaVersion` if child doesn't have them +/// +/// # Arguments +/// * `child` - The mod loader version (e.g., Fabric) +/// * `parent` - The vanilla Minecraft version +/// +/// # Returns +/// A merged `GameVersion` that can be used for launching. +pub fn merge_versions(child: GameVersion, parent: GameVersion) -> GameVersion { + // Libraries: child libraries first (mod loader takes priority in classpath) + let mut merged_libraries = child.libraries; + merged_libraries.extend(parent.libraries); + + // Arguments: merge both game and JVM arguments + let merged_arguments = merge_arguments(child.arguments, parent.arguments); + + GameVersion { + id: child.id, + // Use child's downloads if present, otherwise parent's + downloads: child.downloads.or(parent.downloads), + // Use child's asset_index if present, otherwise parent's + asset_index: child.asset_index.or(parent.asset_index), + libraries: merged_libraries, + // Child's main class always takes priority (this is the mod loader entry point) + main_class: child.main_class, + // Prefer child's minecraft_arguments, fall back to parent's + minecraft_arguments: child.minecraft_arguments.or(parent.minecraft_arguments), + arguments: merged_arguments, + // Use child's java_version if specified, otherwise parent's + java_version: child.java_version.or(parent.java_version), + // Clear inheritsFrom since we've now merged + inherits_from: None, + // Use child's assets field if present, otherwise parent's + assets: child.assets.or(parent.assets), + // Use parent's version type if child doesn't specify + version_type: child.version_type.or(parent.version_type), + } +} + +/// Merge argument objects from child and parent versions. +/// +/// Both game and JVM arguments are merged, with parent arguments coming first +/// and child arguments appended (child can add additional arguments). +fn merge_arguments(child: Option<Arguments>, parent: Option<Arguments>) -> Option<Arguments> { + match (child, parent) { + (None, None) => None, + (Some(c), None) => Some(c), + (None, Some(p)) => Some(p), + (Some(c), Some(p)) => Some(Arguments { + game: merge_json_arrays(p.game, c.game), + jvm: merge_json_arrays(p.jvm, c.jvm), + }), + } +} + +/// Merge two JSON arrays (used for arguments). +/// +/// Parent array comes first, child array is appended. +fn merge_json_arrays( + parent: Option<serde_json::Value>, + child: Option<serde_json::Value>, +) -> Option<serde_json::Value> { + match (parent, child) { + (None, None) => None, + (Some(p), None) => Some(p), + (None, Some(c)) => Some(c), + (Some(p), Some(c)) => { + if let (serde_json::Value::Array(mut p_arr), serde_json::Value::Array(c_arr)) = + (p.clone(), c.clone()) + { + p_arr.extend(c_arr); + Some(serde_json::Value::Array(p_arr)) + } else { + // If they're not arrays, prefer child + Some(c) + } + } + } +} + +/// Check if a version requires inheritance resolution. +/// +/// # Arguments +/// * `version` - The version to check +/// +/// # Returns +/// `true` if the version has an `inheritsFrom` field that needs resolution. +pub fn needs_inheritance_resolution(version: &GameVersion) -> bool { + version.inherits_from.is_some() +} + +/// Recursively resolve version inheritance. +/// +/// This function resolves the entire inheritance chain by loading parent versions +/// and merging them until a version without `inheritsFrom` is found. +/// +/// # Arguments +/// * `version` - The starting version (e.g., a Fabric version) +/// * `version_loader` - A function that loads a version by ID +/// +/// # Returns +/// A fully merged `GameVersion` with all inheritance resolved. +pub async fn resolve_inheritance<F, Fut>( + version: GameVersion, + version_loader: F, +) -> Result<GameVersion, Box<dyn Error + Send + Sync>> +where + F: Fn(String) -> Fut, + Fut: std::future::Future<Output = Result<GameVersion, Box<dyn Error + Send + Sync>>>, +{ + let mut current = version; + + // Keep resolving until we have no more inheritance + while let Some(parent_id) = current.inherits_from.clone() { + let parent = version_loader(parent_id).await?; + current = merge_versions(current, parent); + } + + Ok(current) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::core::game_version::{DownloadArtifact, Downloads, Library}; + + fn create_test_library(name: &str) -> Library { + Library { + name: name.to_string(), + downloads: None, + rules: None, + natives: None, + url: None, + } + } + + #[test] + fn test_merge_libraries_order() { + let child = GameVersion { + id: "fabric-1.20.4".to_string(), + downloads: None, + asset_index: None, + libraries: vec![create_test_library("fabric:loader:1.0")], + main_class: "net.fabricmc.loader.launch.knot.KnotClient".to_string(), + minecraft_arguments: None, + arguments: None, + java_version: None, + inherits_from: Some("1.20.4".to_string()), + assets: None, + version_type: None, + }; + + let parent = GameVersion { + id: "1.20.4".to_string(), + downloads: Some(Downloads { + client: DownloadArtifact { + sha1: Some("abc".to_string()), + size: Some(1000), + url: "https://example.com/client.jar".to_string(), + path: None, + }, + server: None, + }), + asset_index: None, + libraries: vec![create_test_library("net.minecraft:client:1.20.4")], + main_class: "net.minecraft.client.main.Main".to_string(), + minecraft_arguments: None, + arguments: None, + java_version: None, + inherits_from: None, + assets: None, + version_type: Some("release".to_string()), + }; + + let merged = merge_versions(child, parent); + + // Child libraries should come first + assert_eq!(merged.libraries.len(), 2); + assert_eq!(merged.libraries[0].name, "fabric:loader:1.0"); + assert_eq!(merged.libraries[1].name, "net.minecraft:client:1.20.4"); + + // Child main class should override + assert_eq!( + merged.main_class, + "net.fabricmc.loader.launch.knot.KnotClient" + ); + + // Parent downloads should be used + assert!(merged.downloads.is_some()); + + // inheritsFrom should be cleared + assert!(merged.inherits_from.is_none()); + } + + #[test] + fn test_needs_inheritance_resolution() { + let with_inheritance = GameVersion { + id: "test".to_string(), + downloads: None, + asset_index: None, + libraries: vec![], + main_class: "Main".to_string(), + minecraft_arguments: None, + arguments: None, + java_version: None, + inherits_from: Some("1.20.4".to_string()), + assets: None, + version_type: None, + }; + + let without_inheritance = GameVersion { + id: "test".to_string(), + downloads: None, + asset_index: None, + libraries: vec![], + main_class: "Main".to_string(), + minecraft_arguments: None, + arguments: None, + java_version: None, + inherits_from: None, + assets: None, + version_type: None, + }; + + assert!(needs_inheritance_resolution(&with_inheritance)); + assert!(!needs_inheritance_resolution(&without_inheritance)); + } +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index ae74a03..b69912e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -6,6 +6,7 @@ use std::sync::Mutex; use tauri::{Emitter, Manager, State, Window}; // Added Emitter use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::process::Command; +use serde::Serialize; // Added Serialize #[cfg(target_os = "windows")] use std::os::windows::process::CommandExt; @@ -40,6 +41,27 @@ impl MsRefreshTokenState { } } +/// Check if a string contains unresolved placeholders in the form ${...} +/// +/// After the replacement phase, if a string still contains ${...}, it means +/// that placeholder variable was not found in the replacements map and is +/// therefore unresolved. We should skip adding such arguments to avoid +/// passing malformed arguments to the game launcher. +fn has_unresolved_placeholder(s: &str) -> bool { + // Look for the opening sequence + if let Some(start_pos) = s.find("${") { + // Check if there's a closing brace after the opening sequence + if s[start_pos + 2..].find('}').is_some() { + // Found a complete ${...} pattern - this is an unresolved placeholder + return true; + } + // Found ${ but no closing } - also treat as unresolved/malformed + return true; + } + // No ${ found - the string is fully resolved + false +} + #[tauri::command] async fn start_game( window: Window, @@ -93,35 +115,16 @@ async fn start_game( emit_log!(window, format!("Game directory: {:?}", game_dir)); - // 1. Fetch manifest to find the version URL - emit_log!(window, "Fetching version manifest...".to_string()); - let manifest = core::manifest::fetch_version_manifest() - .await - .map_err(|e| e.to_string())?; + // 1. Load version (supports both vanilla and modded versions with inheritance) emit_log!( window, - format!("Found {} versions in manifest", manifest.versions.len()) + format!("Loading version details for {}...", version_id) ); - // 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) - emit_log!( - window, - format!("Fetching version details for {}...", version_id) - ); - 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() + let version_details = core::manifest::load_version(&game_dir, &version_id) .await .map_err(|e| e.to_string())?; + emit_log!( window, format!( @@ -130,20 +133,33 @@ async fn start_game( ) ); - // 3. Prepare download tasks + // Determine the actual minecraft version for client.jar + // (for modded versions, this is the parent vanilla version) + let minecraft_version = version_details + .inherits_from + .clone() + .unwrap_or_else(|| version_id.clone()); + + // 2. Prepare download tasks emit_log!(window, "Preparing download tasks...".to_string()); let mut download_tasks = Vec::new(); // --- Client Jar --- - let client_jar = version_details.downloads.client; + // Get downloads from version_details (may be inherited) + let downloads = version_details + .downloads + .as_ref() + .ok_or("Version has no downloads information")?; + let client_jar = &downloads.client; let mut client_path = game_dir.join("versions"); - client_path.push(&version_id); - client_path.push(format!("{}.jar", version_id)); + client_path.push(&minecraft_version); + client_path.push(format!("{}.jar", minecraft_version)); download_tasks.push(core::downloader::DownloadTask { - url: client_jar.url, + url: client_jar.url.clone(), path: client_path.clone(), - sha1: Some(client_jar.sha1), + sha1: client_jar.sha1.clone(), + sha256: None, }); // --- Libraries --- @@ -153,7 +169,7 @@ async fn start_game( for lib in &version_details.libraries { if core::rules::is_library_allowed(&lib.rules) { - // 1. Standard Library + // 1. Standard Library - check for explicit downloads first if let Some(downloads) = &lib.downloads { if let Some(artifact) = &downloads.artifact { let path_str = artifact @@ -167,7 +183,8 @@ async fn start_game( download_tasks.push(core::downloader::DownloadTask { url: artifact.url.clone(), path: lib_path, - sha1: Some(artifact.sha1.clone()), + sha1: artifact.sha1.clone(), + sha256: None, }); } @@ -200,13 +217,30 @@ async fn start_game( download_tasks.push(core::downloader::DownloadTask { url: native_artifact.url, path: native_path.clone(), - sha1: Some(native_artifact.sha1), + sha1: native_artifact.sha1, + sha256: None, }); native_libs_paths.push(native_path); } } } + } else { + // 3. Library without explicit downloads (mod loader libraries) + // Use Maven coordinate resolution + if let Some(url) = + core::maven::resolve_library_url(&lib.name, None, lib.url.as_deref()) + { + if let Some(lib_path) = core::maven::get_library_path(&lib.name, &libraries_dir) + { + download_tasks.push(core::downloader::DownloadTask { + url, + path: lib_path, + sha1: None, // Maven libraries often don't have SHA1 in the JSON + sha256: None, + }); + } + } } } } @@ -217,8 +251,14 @@ async fn start_game( let objects_dir = assets_dir.join("objects"); let indexes_dir = assets_dir.join("indexes"); + // Get asset index (may be inherited from parent) + let asset_index = version_details + .asset_index + .as_ref() + .ok_or("Version has no asset index information")?; + // Download Asset Index JSON - let asset_index_path = indexes_dir.join(format!("{}.json", version_details.asset_index.id)); + let asset_index_path = indexes_dir.join(format!("{}.json", asset_index.id)); // Check if index exists or download it // Note: We need the content of this file to parse it. @@ -230,11 +270,8 @@ async fn start_game( .await .map_err(|e| e.to_string())? } else { - println!( - "Downloading asset index from {}", - version_details.asset_index.url - ); - let content = reqwest::get(&version_details.asset_index.url) + println!("Downloading asset index from {}", asset_index.url); + let content = reqwest::get(&asset_index.url) .await .map_err(|e| e.to_string())? .text() @@ -254,6 +291,7 @@ async fn start_game( #[derive(serde::Deserialize, Debug)] struct AssetObject { hash: String, + #[allow(dead_code)] size: u64, } @@ -280,6 +318,7 @@ async fn start_game( url, path, sha1: Some(hash), + sha256: None, }); } @@ -394,10 +433,7 @@ async fn start_game( replacements.insert("${version_name}", version_id.clone()); replacements.insert("${game_directory}", game_dir.to_string_lossy().to_string()); replacements.insert("${assets_root}", assets_dir.to_string_lossy().to_string()); - replacements.insert( - "${assets_index_name}", - version_details.asset_index.id.clone(), - ); + replacements.insert("${assets_index_name}", asset_index.id.clone()); replacements.insert("${auth_uuid}", account.uuid()); replacements.insert("${auth_access_token}", account.access_token()); replacements.insert("${user_type}", "mojang".to_string()); @@ -449,7 +485,10 @@ async fn start_game( for (key, replacement) in &replacements { arg = arg.replace(key, replacement); } - args.push(arg); + // Skip arguments with unresolved placeholders + if !has_unresolved_placeholder(&arg) { + args.push(arg); + } } else if let Some(arr) = val.as_array() { for sub in arr { if let Some(s) = sub.as_str() { @@ -457,7 +496,10 @@ async fn start_game( for (key, replacement) in &replacements { arg = arg.replace(key, replacement); } - args.push(arg); + // Skip arguments with unresolved placeholders + if !has_unresolved_placeholder(&arg) { + args.push(arg); + } } } } @@ -785,7 +827,7 @@ async fn refresh_account( .map_err(|e| e.to_string())?; let storage = core::account_storage::AccountStorage::new(app_dir.clone()); - let (stored_account, ms_refresh) = storage + let (_stored_account, ms_refresh) = storage .get_active_account() .ok_or("No active account found")?; @@ -807,8 +849,8 @@ async fn refresh_account( /// Detect Java installations on the system #[tauri::command] -async fn detect_java() -> Result<Vec<core::java::JavaInstallation>, String> { - Ok(core::java::detect_java_installations()) +async fn detect_java(app_handle: tauri::AppHandle) -> Result<Vec<core::java::JavaInstallation>, String> { + Ok(core::java::detect_all_java_installations(&app_handle)) } /// Get recommended Java for a specific Minecraft version @@ -819,8 +861,349 @@ async fn get_recommended_java( Ok(core::java::get_recommended_java(required_major_version)) } +/// Get Adoptium Java download info +#[tauri::command] +async fn fetch_adoptium_java( + major_version: u32, + image_type: String, +) -> Result<core::java::JavaDownloadInfo, String> { + let img_type = match image_type.to_lowercase().as_str() { + "jdk" => core::java::ImageType::Jdk, + _ => core::java::ImageType::Jre, + }; + core::java::fetch_java_release(major_version, img_type).await +} + +/// Download and install Adoptium Java +#[tauri::command] +async fn download_adoptium_java( + app_handle: tauri::AppHandle, + major_version: u32, + image_type: String, + custom_path: Option<String>, +) -> Result<core::java::JavaInstallation, String> { + let img_type = match image_type.to_lowercase().as_str() { + "jdk" => core::java::ImageType::Jdk, + _ => core::java::ImageType::Jre, + }; + let path = custom_path.map(std::path::PathBuf::from); + core::java::download_and_install_java(&app_handle, major_version, img_type, path).await +} + +/// Get available Adoptium Java versions +#[tauri::command] +async fn fetch_available_java_versions() -> Result<Vec<u32>, String> { + core::java::fetch_available_versions().await +} + +/// Fetch Java catalog with platform availability (uses cache) +#[tauri::command] +async fn fetch_java_catalog( + app_handle: tauri::AppHandle, +) -> Result<core::java::JavaCatalog, String> { + core::java::fetch_java_catalog(&app_handle, false).await +} + +/// Refresh Java catalog (bypass cache) +#[tauri::command] +async fn refresh_java_catalog( + app_handle: tauri::AppHandle, +) -> Result<core::java::JavaCatalog, String> { + core::java::fetch_java_catalog(&app_handle, true).await +} + +/// Cancel current Java download +#[tauri::command] +async fn cancel_java_download() -> Result<(), String> { + core::java::cancel_current_download(); + Ok(()) +} + +/// Get pending Java downloads +#[tauri::command] +async fn get_pending_java_downloads( + app_handle: tauri::AppHandle, +) -> Result<Vec<core::downloader::PendingJavaDownload>, String> { + Ok(core::java::get_pending_downloads(&app_handle)) +} + +/// Resume pending Java downloads +#[tauri::command] +async fn resume_java_downloads( + app_handle: tauri::AppHandle, +) -> Result<Vec<core::java::JavaInstallation>, String> { + core::java::resume_pending_downloads(&app_handle).await +} + +/// Get Minecraft versions supported by Fabric +#[tauri::command] +async fn get_fabric_game_versions() -> Result<Vec<core::fabric::FabricGameVersion>, String> { + core::fabric::fetch_supported_game_versions() + .await + .map_err(|e| e.to_string()) +} + +/// Get available Fabric loader versions +#[tauri::command] +async fn get_fabric_loader_versions() -> Result<Vec<core::fabric::FabricLoaderVersion>, String> { + core::fabric::fetch_loader_versions() + .await + .map_err(|e| e.to_string()) +} + +/// Get Fabric loaders available for a specific Minecraft version +#[tauri::command] +async fn get_fabric_loaders_for_version( + game_version: String, +) -> Result<Vec<core::fabric::FabricLoaderEntry>, String> { + core::fabric::fetch_loaders_for_game_version(&game_version) + .await + .map_err(|e| e.to_string()) +} + +/// Install Fabric loader for a specific Minecraft version +#[tauri::command] +async fn install_fabric( + window: Window, + game_version: String, + loader_version: String, +) -> Result<core::fabric::InstalledFabricVersion, String> { + emit_log!( + window, + format!( + "Installing Fabric {} for Minecraft {}...", + loader_version, game_version + ) + ); + + let app_handle = window.app_handle(); + let game_dir = app_handle + .path() + .app_data_dir() + .map_err(|e| format!("Failed to get app data dir: {}", e))?; + + let result = core::fabric::install_fabric(&game_dir, &game_version, &loader_version) + .await + .map_err(|e| e.to_string())?; + + emit_log!( + window, + format!("Fabric installed successfully: {}", result.id) + ); + + Ok(result) +} + +/// List installed Fabric versions +#[tauri::command] +async fn list_installed_fabric_versions(window: Window) -> Result<Vec<String>, String> { + let app_handle = window.app_handle(); + let game_dir = app_handle + .path() + .app_data_dir() + .map_err(|e| format!("Failed to get app data dir: {}", e))?; + + core::fabric::list_installed_fabric_versions(&game_dir) + .await + .map_err(|e| e.to_string()) +} + +/// Check if Fabric is installed for a specific version +#[tauri::command] +async fn is_fabric_installed( + window: Window, + game_version: String, + loader_version: String, +) -> Result<bool, String> { + let app_handle = window.app_handle(); + let game_dir = app_handle + .path() + .app_data_dir() + .map_err(|e| format!("Failed to get app data dir: {}", e))?; + + Ok(core::fabric::is_fabric_installed( + &game_dir, + &game_version, + &loader_version, + )) +} + +/// Get Minecraft versions supported by Forge +#[tauri::command] +async fn get_forge_game_versions() -> Result<Vec<String>, String> { + core::forge::fetch_supported_game_versions() + .await + .map_err(|e| e.to_string()) +} + +/// Get available Forge versions for a specific Minecraft version +#[tauri::command] +async fn get_forge_versions_for_game( + game_version: String, +) -> Result<Vec<core::forge::ForgeVersion>, String> { + core::forge::fetch_forge_versions(&game_version) + .await + .map_err(|e| e.to_string()) +} + +/// Install Forge for a specific Minecraft version +#[tauri::command] +async fn install_forge( + window: Window, + game_version: String, + forge_version: String, +) -> Result<core::forge::InstalledForgeVersion, String> { + emit_log!( + window, + format!( + "Installing Forge {} for Minecraft {}...", + forge_version, game_version + ) + ); + + let app_handle = window.app_handle(); + let game_dir = app_handle + .path() + .app_data_dir() + .map_err(|e| format!("Failed to get app data dir: {}", e))?; + + let result = core::forge::install_forge(&game_dir, &game_version, &forge_version) + .await + .map_err(|e| e.to_string())?; + + emit_log!( + window, + format!("Forge installed successfully: {}", result.id) + ); + + Ok(result) +} + +#[derive(serde::Serialize)] +struct GithubRelease { + tag_name: String, + name: String, + published_at: String, + body: String, + html_url: String, +} + +#[tauri::command] +async fn get_github_releases() -> Result<Vec<GithubRelease>, String> { + let client = reqwest::Client::new(); + let res = client + .get("https://api.github.com/repos/HsiangNianian/DropOut/releases") + .header("User-Agent", "DropOut-Launcher") + .send() + .await + .map_err(|e| e.to_string())?; + + if !res.status().is_success() { + return Err(format!("GitHub API returned status: {}", res.status())); + } + + let releases: Vec<serde_json::Value> = res.json().await.map_err(|e| e.to_string())?; + + let mut result = Vec::new(); + for r in releases { + if let (Some(tag), Some(name), Some(date), Some(body), Some(url)) = ( + r["tag_name"].as_str(), + r["name"].as_str(), + r["published_at"].as_str(), + r["body"].as_str(), + r["html_url"].as_str() + ) { + result.push(GithubRelease { + tag_name: tag.to_string(), + name: name.to_string(), + published_at: date.to_string(), + body: body.to_string(), + html_url: url.to_string(), + }); + } + } + Ok(result) +} + +#[derive(Serialize)] +struct PastebinResponse { + url: String, +} + +#[tauri::command] +async fn upload_to_pastebin( + state: State<'_, core::config::ConfigState>, + content: String, +) -> Result<PastebinResponse, String> { + // Check content length limit + if content.len() > 500 * 1024 { + return Err("Log file too large (max 500KB)".to_string()); + } + + // Extract config values before any async calls to avoid holding MutexGuard across await + let (service, api_key) = { + let config = state.config.lock().unwrap(); + ( + config.log_upload_service.clone(), + config.pastebin_api_key.clone(), + ) + }; + + let client = reqwest::Client::new(); + + match service.as_str() { + "pastebin.com" => { + let api_key = api_key + .ok_or("Pastebin API Key not configured in settings")?; + + let res = client + .post("https://pastebin.com/api/api_post.php") + .form(&[ + ("api_dev_key", api_key.as_str()), + ("api_option", "paste"), + ("api_paste_code", content.as_str()), + ("api_paste_private", "1"), // Unlisted + ("api_paste_name", "DropOut Launcher Log"), + ("api_paste_expire_date", "1W"), + ]) + .send() + .await + .map_err(|e| e.to_string())?; + + if !res.status().is_success() { + return Err(format!("Pastebin upload failed: {}", res.status())); + } + + let url = res.text().await.map_err(|e| e.to_string())?; + if url.starts_with("Bad API Request") { + return Err(format!("Pastebin API error: {}", url)); + } + Ok(PastebinResponse { url }) + } + // Default to paste.rs + _ => { + let res = client + .post("https://paste.rs/") + .body(content) + .send() + .await + .map_err(|e| e.to_string())?; + + if !res.status().is_success() { + return Err(format!("paste.rs upload failed: {}", res.status())); + } + + let url = res.text().await.map_err(|e| e.to_string())?; + let url = url.trim().to_string(); + Ok(PastebinResponse { url }) + } + } +} + fn main() { tauri::Builder::default() + .plugin(tauri_plugin_fs::init()) + .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_shell::init()) .manage(core::auth::AccountState::new()) .manage(MsRefreshTokenState::new()) @@ -846,6 +1229,13 @@ fn main() { println!("[Startup] Loaded saved account"); } + // Check for pending Java downloads and notify frontend + let pending = core::java::get_pending_downloads(&app.app_handle()); + if !pending.is_empty() { + println!("[Startup] Found {} pending Java download(s)", pending.len()); + let _ = app.emit("pending-java-downloads", pending.len()); + } + Ok(()) }) .invoke_handler(tauri::generate_handler![ @@ -859,8 +1249,30 @@ fn main() { start_microsoft_login, complete_microsoft_login, refresh_account, + // Java commands detect_java, - get_recommended_java + get_recommended_java, + fetch_adoptium_java, + download_adoptium_java, + fetch_available_java_versions, + fetch_java_catalog, + refresh_java_catalog, + cancel_java_download, + get_pending_java_downloads, + resume_java_downloads, + // Fabric commands + get_fabric_game_versions, + get_fabric_loader_versions, + get_fabric_loaders_for_version, + install_fabric, + list_installed_fabric_versions, + is_fabric_installed, + // Forge commands + get_forge_game_versions, + get_forge_versions_for_game, + install_forge, + get_github_releases, + upload_to_pastebin ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/src/utils/zip.rs b/src-tauri/src/utils/zip.rs index a03c975..dfe1214 100644 --- a/src-tauri/src/utils/zip.rs +++ b/src-tauri/src/utils/zip.rs @@ -1,5 +1,7 @@ +use flate2::read::GzDecoder; use std::fs; use std::path::Path; +use tar::Archive; pub fn extract_zip(zip_path: &Path, extract_to: &Path) -> Result<(), String> { let file = fs::File::open(zip_path) @@ -38,3 +40,76 @@ pub fn extract_zip(zip_path: &Path, extract_to: &Path) -> Result<(), String> { Ok(()) } + +/// Extract a tar.gz archive +/// +/// Adoptium's tar.gz archives usually contain a top-level directory, such as `jdk-21.0.5+11-jre/`. +/// This function returns the name of that directory to facilitate locating `bin/java` afterwards. +pub fn extract_tar_gz(archive_path: &Path, extract_to: &Path) -> Result<String, String> { + let file = fs::File::open(archive_path) + .map_err(|e| format!("Failed to open tar.gz {}: {}", archive_path.display(), e))?; + + let decoder = GzDecoder::new(file); + let mut archive = Archive::new(decoder); + + // Ensure the target directory exists + fs::create_dir_all(extract_to) + .map_err(|e| format!("Failed to create extract directory: {}", e))?; + + // Track the top-level directory name + let mut top_level_dir: Option<String> = None; + + for entry in archive + .entries() + .map_err(|e| format!("Failed to read tar entries: {}", e))? + { + let mut entry = entry.map_err(|e| format!("Failed to read tar entry: {}", e))?; + let entry_path = entry + .path() + .map_err(|e| format!("Failed to get entry path: {}", e))? + .into_owned(); + + // Extract the top-level directory name (the first path component) + if top_level_dir.is_none() { + if let Some(first_component) = entry_path.components().next() { + let component_str = first_component.as_os_str().to_string_lossy().to_string(); + if !component_str.is_empty() && component_str != "." { + top_level_dir = Some(component_str); + } + } + } + + let outpath = extract_to.join(&entry_path); + + if entry.header().entry_type().is_dir() { + fs::create_dir_all(&outpath) + .map_err(|e| format!("Failed to create directory {}: {}", outpath.display(), e))?; + } else { + // Ensure parent directory exists + if let Some(parent) = outpath.parent() { + if !parent.exists() { + fs::create_dir_all(parent) + .map_err(|e| format!("Failed to create parent dir: {}", e))?; + } + } + + let mut outfile = fs::File::create(&outpath) + .map_err(|e| format!("Failed to create file {}: {}", outpath.display(), e))?; + + std::io::copy(&mut entry, &mut outfile) + .map_err(|e| format!("Failed to extract file: {}", e))?; + + // Set executable permissions on Unix systems + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + if let Ok(mode) = entry.header().mode() { + let permissions = fs::Permissions::from_mode(mode); + let _ = fs::set_permissions(&outpath, permissions); + } + } + } + } + + top_level_dir.ok_or_else(|| "Archive appears to be empty".to_string()) +} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 9aa3b68..ce54ca8 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,6 +1,6 @@ { "productName": "dropout", - "version": "0.1.14", + "version": "0.1.21", "identifier": "com.dropout.launcher", "build": { "beforeDevCommand": "pnpm -C ../ui dev", @@ -14,8 +14,8 @@ "title": "Minecraft DropOut Launcher", "width": 1024, "height": 768, - "minWidth": 800, - "minHeight": 600, + "minWidth": 905, + "minHeight": 575, "resizable": true } ], @@ -33,6 +33,11 @@ "icons/128x128@2x.png", "icons/icon.icns", "icons/icon.ico" - ] + ], + "linux": { + "appimage": { + "bundleMediaFramework": false + } + } } -}
\ No newline at end of file +} |