diff options
| author | 2026-01-25 04:52:35 +0100 | |
|---|---|---|
| committer | 2026-01-29 02:53:33 +0100 | |
| commit | d7ddf3710f6aff40d0595430f5f49255c89fdca1 (patch) | |
| tree | dad7408e179479393ea01bda57fbf3f0a9346de6 | |
| parent | a17d94168440ce91703069fc6027dc766e0d8998 (diff) | |
| download | DropOut-d7ddf3710f6aff40d0595430f5f49255c89fdca1.tar.gz DropOut-d7ddf3710f6aff40d0595430f5f49255c89fdca1.zip | |
refactor(java): modularize Java detection and management system
- Split monolithic java.rs (1089 lines) into focused modules
- detection: Java installation discovery
- validation: Version validation and requirements checking
- priority: Installation selection priority logic
- provider: Java download provider trait
- providers: Provider implementations (Adoptium)
- persistence: Cache and catalog management
- Add java_path_override field to Instance struct for per-instance Java configuration
- Export JavaInstallation at core module level
This refactoring improves maintainability and prepares for multi-vendor Java provider support.
Reviewed-by: Claude Sonnet 4.5
| -rw-r--r-- | src-tauri/src/core/instance.rs | 3 | ||||
| -rw-r--r-- | src-tauri/src/core/java.rs | 1119 | ||||
| -rw-r--r-- | src-tauri/src/core/java/detection.rs | 184 | ||||
| -rw-r--r-- | src-tauri/src/core/java/mod.rs | 527 | ||||
| -rw-r--r-- | src-tauri/src/core/java/persistence.rs | 82 | ||||
| -rw-r--r-- | src-tauri/src/core/java/priority.rs | 72 | ||||
| -rw-r--r-- | src-tauri/src/core/java/provider.rs | 41 | ||||
| -rw-r--r-- | src-tauri/src/core/java/providers/adoptium.rs | 306 | ||||
| -rw-r--r-- | src-tauri/src/core/java/providers/mod.rs | 3 | ||||
| -rw-r--r-- | src-tauri/src/core/java/validation.rs | 120 | ||||
| -rw-r--r-- | src-tauri/src/core/mod.rs | 2 | ||||
| -rw-r--r-- | src-tauri/src/main.rs | 20 |
12 files changed, 1354 insertions, 1125 deletions
diff --git a/src-tauri/src/core/instance.rs b/src-tauri/src/core/instance.rs index e7746b2..e842ec9 100644 --- a/src-tauri/src/core/instance.rs +++ b/src-tauri/src/core/instance.rs @@ -34,6 +34,7 @@ pub struct Instance { pub jvm_args_override: Option<String>, // JVM参数覆盖(可选) #[serde(default)] pub memory_override: Option<MemoryOverride>, // 内存设置覆盖(可选) + pub java_path_override: Option<String>, // 实例级Java路径覆盖(可选) } /// Memory settings override for an instance @@ -127,6 +128,7 @@ impl InstanceState { mod_loader_version: None, jvm_args_override: None, memory_override: None, + java_path_override: None, }; let mut config = self.instances.lock().unwrap(); @@ -283,6 +285,7 @@ impl InstanceState { last_played: None, jvm_args_override: source_instance.jvm_args_override.clone(), memory_override: source_instance.memory_override.clone(), + java_path_override: source_instance.java_path_override.clone(), }; self.update_instance(new_instance.clone())?; diff --git a/src-tauri/src/core/java.rs b/src-tauri/src/core/java.rs deleted file mode 100644 index 459ab15..0000000 --- a/src-tauri/src/core/java.rs +++ /dev/null @@ -1,1119 +0,0 @@ -use serde::{Deserialize, Serialize}; -#[cfg(target_os = "windows")] -use std::os::windows::process::CommandExt; -use std::path::PathBuf; -use std::process::Command; -use tauri::AppHandle; -use tauri::Emitter; -use tauri::Manager; -use ts_rs::TS; - -use crate::core::downloader::{self, DownloadQueue, JavaDownloadProgress, 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 - -/// Helper to strip UNC prefix on Windows (\\?\) -fn strip_unc_prefix(path: PathBuf) -> PathBuf { - #[cfg(target_os = "windows")] - { - let s = path.to_string_lossy().to_string(); - if s.starts_with(r"\\?\") { - return PathBuf::from(&s[4..]); - } - } - path -} - -#[derive(Debug, Clone, Serialize, Deserialize, TS)] -#[serde(rename_all = "camelCase")] -#[ts(export, export_to = "../../packages/ui-new/src/types/bindings/java.ts")] -pub struct JavaInstallation { - pub path: String, - pub version: String, - pub is_64bit: bool, -} - -/// Java image type: JRE or JDK -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)] -#[ts( - export, - rename_all = "lowercase", - export_to = "../../packages/ui-new/src/types/bindings/java.ts" -)] -#[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, TS)] -#[serde(rename_all = "camelCase")] -#[ts(export, export_to = "../../packages/ui-new/src/types/bindings/java.ts")] -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, Default, TS)] -#[serde(rename_all = "camelCase")] -#[ts(export, export_to = "../../packages/ui-new/src/types/bindings/java.ts")] -pub struct JavaCatalog { - pub releases: Vec<JavaReleaseInfo>, - pub available_major_versions: Vec<u32>, - pub lts_versions: Vec<u32>, - pub cached_at: u64, -} - -/// 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)] -#[allow(dead_code)] -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)] -#[allow(dead_code)] -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)] -#[allow(dead_code)] -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, TS)] -#[serde(rename_all = "camelCase")] -#[ts(export, export_to = "../../packages/ui-new/src/types/bindings/java.ts")] -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 -#[allow(dead_code)] -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() - )); - } - - // Resolve symlinks and strip UNC prefix to ensure clean path - let java_bin = std::fs::canonicalize(&java_bin).map_err(|e| e.to_string())?; - let java_bin = strip_unc_prefix(java_bin); - - // 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(); - let candidates = get_java_candidates(); - - for candidate in candidates { - if let Some(java) = check_java_installation(&candidate) { - // Avoid duplicates - if !installations - .iter() - .any(|j: &JavaInstallation| j.path == java.path) - { - installations.push(java); - } - } - } - - // Sort by version (newer first) - 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 -} - -/// TODO: Refactor to use a more robust method -/// Get list of candidate Java paths to check -fn get_java_candidates() -> Vec<PathBuf> { - let mut candidates = Vec::new(); - - // Check PATH first - let mut cmd = Command::new(if cfg!(windows) { "where" } else { "which" }); - cmd.arg("java"); - #[cfg(target_os = "windows")] - // hide console window - cmd.creation_flags(0x08000000); - - if let Ok(output) = cmd.output() { - if output.status.success() { - let paths = String::from_utf8_lossy(&output.stdout); - for line in paths.lines() { - let path = PathBuf::from(line.trim()); - if path.exists() { - // Resolve symlinks (important for Windows javapath wrapper) - let resolved = std::fs::canonicalize(&path).unwrap_or(path); - // Strip UNC prefix if present to keep paths clean - let final_path = strip_unc_prefix(resolved); - candidates.push(final_path); - } - } - } - } - - #[cfg(target_os = "linux")] - { - // Common Linux Java paths - let linux_paths = [ - "/usr/lib/jvm", - "/usr/java", - "/opt/java", - "/opt/jdk", - "/opt/openjdk", - ]; - - for base in &linux_paths { - if let Ok(entries) = std::fs::read_dir(base) { - for entry in entries.flatten() { - let java_path = entry.path().join("bin/java"); - if java_path.exists() { - candidates.push(java_path); - } - } - } - } - - // Flatpak / Snap locations - let home = std::env::var("HOME").unwrap_or_default(); - let snap_java = PathBuf::from(&home).join(".sdkman/candidates/java"); - if snap_java.exists() { - if let Ok(entries) = std::fs::read_dir(&snap_java) { - for entry in entries.flatten() { - let java_path = entry.path().join("bin/java"); - if java_path.exists() { - candidates.push(java_path); - } - } - } - } - } - - #[cfg(target_os = "macos")] - { - // macOS Java paths - let mac_paths = [ - "/Library/Java/JavaVirtualMachines", - "/System/Library/Java/JavaVirtualMachines", - "/usr/local/opt/openjdk/bin/java", - "/opt/homebrew/opt/openjdk/bin/java", - ]; - - for path in &mac_paths { - let p = PathBuf::from(path); - if p.is_dir() { - if let Ok(entries) = std::fs::read_dir(&p) { - for entry in entries.flatten() { - let java_path = entry.path().join("Contents/Home/bin/java"); - if java_path.exists() { - candidates.push(java_path); - } - } - } - } else if p.exists() { - candidates.push(p); - } - } - - // Homebrew ARM64 - let homebrew_arm = PathBuf::from("/opt/homebrew/Cellar/openjdk"); - if homebrew_arm.exists() { - if let Ok(entries) = std::fs::read_dir(&homebrew_arm) { - for entry in entries.flatten() { - let java_path = entry - .path() - .join("libexec/openjdk.jdk/Contents/Home/bin/java"); - if java_path.exists() { - candidates.push(java_path); - } - } - } - } - } - - #[cfg(target_os = "windows")] - { - // Windows Java paths - let program_files = - std::env::var("ProgramFiles").unwrap_or_else(|_| "C:\\Program Files".to_string()); - let program_files_x86 = std::env::var("ProgramFiles(x86)") - .unwrap_or_else(|_| "C:\\Program Files (x86)".to_string()); - let local_app_data = std::env::var("LOCALAPPDATA").unwrap_or_default(); - - let win_paths = [ - format!("{}\\Java", program_files), - format!("{}\\Java", program_files_x86), - format!("{}\\Eclipse Adoptium", program_files), - format!("{}\\AdoptOpenJDK", program_files), - format!("{}\\Microsoft\\jdk", program_files), - format!("{}\\Zulu", program_files), - format!("{}\\Amazon Corretto", program_files), - format!("{}\\BellSoft\\LibericaJDK", program_files), - format!("{}\\Programs\\Eclipse Adoptium", local_app_data), - ]; - - for base in &win_paths { - let base_path = PathBuf::from(base); - if base_path.exists() { - if let Ok(entries) = std::fs::read_dir(&base_path) { - for entry in entries.flatten() { - let java_path = entry.path().join("bin\\java.exe"); - if java_path.exists() { - candidates.push(java_path); - } - } - } - } - } - - // Also check JAVA_HOME - if let Ok(java_home) = std::env::var("JAVA_HOME") { - let java_path = PathBuf::from(&java_home).join("bin\\java.exe"); - if java_path.exists() { - candidates.push(java_path); - } - } - } - - // JAVA_HOME environment variable (cross-platform) - if let Ok(java_home) = std::env::var("JAVA_HOME") { - let bin_name = if cfg!(windows) { "java.exe" } else { "java" }; - let java_path = PathBuf::from(&java_home).join("bin").join(bin_name); - if java_path.exists() { - candidates.push(java_path); - } - } - - candidates -} - -/// Check a specific Java installation and get its version info -fn check_java_installation(path: &PathBuf) -> Option<JavaInstallation> { - let mut cmd = Command::new(path); - cmd.arg("-version"); - #[cfg(target_os = "windows")] - cmd.creation_flags(0x08000000); - - let output = cmd.output().ok()?; - - // Java outputs version info to stderr - let version_output = String::from_utf8_lossy(&output.stderr); - - // Parse version string (e.g., "openjdk version \"17.0.1\"" or "java version \"1.8.0_301\"") - let version = parse_version_string(&version_output)?; - let is_64bit = version_output.contains("64-Bit"); - - Some(JavaInstallation { - path: path.to_string_lossy().to_string(), - version, - is_64bit, - }) -} - -/// Parse version string from java -version output -fn parse_version_string(output: &str) -> Option<String> { - for line in output.lines() { - if line.contains("version") { - // Find the quoted version string - if let Some(start) = line.find('"') { - if let Some(end) = line[start + 1..].find('"') { - return Some(line[start + 1..start + 1 + end].to_string()); - } - } - } - } - None -} - -/// Parse version for comparison (returns major version number) -fn parse_java_version(version: &str) -> u32 { - // Handle various formats: - // - Old format: 1.8.0_xxx (Java 8 with update) - // - New format: 17.0.1, 11.0.5+10 (Java 11+) - // - Format with build: 21.0.3+13-Ubuntu-0ubuntu0.24.04.1 - // - Format with underscores: 1.8.0_411 - - // First, strip build metadata (everything after '+') - let version_only = version.split('+').next().unwrap_or(version); - - // Remove trailing junk (like "-Ubuntu-0ubuntu0.24.04.1") - let version_only = version_only.split('-').next().unwrap_or(version_only); - - // Replace underscores with dots (1.8.0_411 -> 1.8.0.411) - let normalized = version_only.replace('_', "."); - - // Split by dots - let parts: Vec<&str> = normalized.split('.').collect(); - - if let Some(first) = parts.first() { - if *first == "1" { - // Old format: 1.8.0 -> major is 8 - parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0) - } else { - // New format: 17.0.1 -> major is 17 - first.parse().unwrap_or(0) - } - } else { - 0 - } -} - -/// Get the best Java for a specific Minecraft version -pub fn get_recommended_java(required_major_version: Option<u64>) -> Option<JavaInstallation> { - let installations = detect_java_installations(); - - if let Some(required) = required_major_version { - // Find exact match or higher - installations.into_iter().find(|java| { - let major = parse_java_version(&java.version); - major >= required as u32 - }) - } else { - // Return newest - installations.into_iter().next() - } -} - -/// Get compatible Java for a specific Minecraft version with upper bound -/// For older Minecraft versions (1.13.x and below), we need Java 8 specifically -/// as newer Java versions have compatibility issues with old Forge versions -pub fn get_compatible_java( - app_handle: &AppHandle, - required_major_version: Option<u64>, - max_major_version: Option<u32>, -) -> Option<JavaInstallation> { - let installations = detect_all_java_installations(app_handle); - - if let Some(max_version) = max_major_version { - // Find Java version within the acceptable range - installations.into_iter().find(|java| { - let major = parse_java_version(&java.version); - let meets_min = if let Some(required) = required_major_version { - major >= required as u32 - } else { - true - }; - meets_min && major <= max_version - }) - } else if let Some(required) = required_major_version { - // Find exact match or higher (no upper bound) - installations.into_iter().find(|java| { - let major = parse_java_version(&java.version); - major >= required as u32 - }) - } else { - // Return newest - installations.into_iter().next() - } -} - -/// Check if a Java installation is compatible with the required version range -pub fn is_java_compatible( - java_path: &str, - required_major_version: Option<u64>, - max_major_version: Option<u32>, -) -> bool { - let java_path_buf = PathBuf::from(java_path); - if let Some(java) = check_java_installation(&java_path_buf) { - let major = parse_java_version(&java.version); - let meets_min = if let Some(required) = required_major_version { - major >= required as u32 - } else { - true - }; - let meets_max = if let Some(max_version) = max_major_version { - major <= max_version - } else { - true - }; - meets_min && meets_max - } else { - false - } -} - -/// 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() { - let resolved = std::fs::canonicalize(&direct_bin).unwrap_or(direct_bin); - return Some(strip_unc_prefix(resolved)); - } - - // 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() { - let resolved = std::fs::canonicalize(&nested_bin).unwrap_or(nested_bin); - return Some(strip_unc_prefix(resolved)); - } - - // 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 -#[allow(dead_code)] -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/java/detection.rs b/src-tauri/src/core/java/detection.rs new file mode 100644 index 0000000..263580f --- /dev/null +++ b/src-tauri/src/core/java/detection.rs @@ -0,0 +1,184 @@ +use std::path::PathBuf; +use std::process::Command; + +#[cfg(target_os = "windows")] +use std::os::windows::process::CommandExt; + +pub fn strip_unc_prefix(path: PathBuf) -> PathBuf { + #[cfg(target_os = "windows")] + { + // Remove the UNC prefix (\\?\) from Windows paths + let s = path.to_string_lossy().to_string(); + if s.starts_with(r"\\?\") { + return PathBuf::from(&s[4..]); + } + } + path +} + +pub fn find_sdkman_java() -> Option<PathBuf> { + let home = std::env::var("HOME").ok()?; + let sdkman_path = PathBuf::from(&home).join(".sdkman/candidates/java/current/bin/java"); + if sdkman_path.exists() { + Some(sdkman_path) + } else { + None + } +} + +fn run_which_command_with_timeout() -> Option<String> { + let mut cmd = Command::new(if cfg!(windows) { "where" } else { "which" }); + cmd.arg("java"); + #[cfg(target_os = "windows")] + // Hide the console window on Windows + cmd.creation_flags(0x08000000); + + match cmd.output() { + Ok(output) => { + if output.status.success() { + Some(String::from_utf8_lossy(&output.stdout).to_string()) + } else { + None + } + } + Err(_) => None, + } +} + +pub fn get_java_candidates() -> Vec<PathBuf> { + let mut candidates = Vec::new(); + + // Only attempt 'which' or 'where' if is not Windows + // CAUTION: linux 'which' may return symlinks, so we need to canonicalize later + if let Some(paths_str) = run_which_command_with_timeout() { + for line in paths_str.lines() { + let path = PathBuf::from(line.trim()); + if path.exists() { + let resolved = std::fs::canonicalize(&path).unwrap_or(path); + let final_path = strip_unc_prefix(resolved); + candidates.push(final_path); + } + } + } + + #[cfg(target_os = "linux")] + { + let linux_paths = [ + "/usr/lib/jvm", + "/usr/java", + "/opt/java", + "/opt/jdk", + "/opt/openjdk", + ]; + + for base in &linux_paths { + if let Ok(entries) = std::fs::read_dir(base) { + for entry in entries.flatten() { + let java_path = entry.path().join("bin/java"); + if java_path.exists() { + candidates.push(java_path); + } + } + } + } + + let home = std::env::var("HOME").unwrap_or_default(); + // Check common SDKMAN! java candidates + if let Some(sdkman_java) = find_sdkman_java() { + candidates.push(sdkman_java); + } + } + + #[cfg(target_os = "macos")] + { + let mac_paths = [ + "/Library/Java/JavaVirtualMachines", + "/System/Library/Java/JavaVirtualMachines", + "/usr/local/opt/openjdk/bin/java", + "/opt/homebrew/opt/openjdk/bin/java", + ]; + + for path in &mac_paths { + let p = PathBuf::from(path); + if p.is_dir() { + if let Ok(entries) = std::fs::read_dir(&p) { + for entry in entries.flatten() { + let java_path = entry.path().join("Contents/Home/bin/java"); + if java_path.exists() { + candidates.push(java_path); + } + } + } + } else if p.exists() { + candidates.push(p); + } + } + + // Check common Homebrew java candidates for aarch64 macs + let homebrew_arm = PathBuf::from("/opt/homebrew/Cellar/openjdk"); + if homebrew_arm.exists() { + if let Ok(entries) = std::fs::read_dir(&homebrew_arm) { + for entry in entries.flatten() { + let java_path = entry + .path() + .join("libexec/openjdk.jdk/Contents/Home/bin/java"); + if java_path.exists() { + candidates.push(java_path); + } + } + } + } + + // Check common SDKMAN! java candidates + if let Some(sdkman_java) = find_sdkman_java() { + candidates.push(sdkman_java); + } + } + + #[cfg(target_os = "windows")] + { + let program_files = + std::env::var("ProgramFiles").unwrap_or_else(|_| "C:\\Program Files".to_string()); + let program_files_x86 = std::env::var("ProgramFiles(x86)") + .unwrap_or_else(|_| "C:\\Program Files (x86)".to_string()); + let local_app_data = std::env::var("LOCALAPPDATA").unwrap_or_default(); + + // Common installation paths for various JDK distributions + let mut win_paths = vec![]; + for base in &[&program_files, &program_files_x86, &local_app_data] { + win_paths.push(format!("{}\\Java", base)); + win_paths.push(format!("{}\\Eclipse Adoptium", base)); + win_paths.push(format!("{}\\AdoptOpenJDK", base)); + win_paths.push(format!("{}\\Microsoft\\jdk", base)); + win_paths.push(format!("{}\\Zulu", base)); + win_paths.push(format!("{}\\Amazon Corretto", base)); + win_paths.push(format!("{}\\BellSoft\\LibericaJDK", base)); + win_paths.push(format!("{}\\Programs\\Eclipse Adoptium", base)); + } + + for base in &win_paths { + let base_path = PathBuf::from(base); + if base_path.exists() { + if let Ok(entries) = std::fs::read_dir(&base_path) { + for entry in entries.flatten() { + let java_path = entry.path().join("bin\\java.exe"); + if java_path.exists() { + candidates.push(java_path); + } + } + } + } + } + } + + // Check JAVA_HOME java candidate + if let Ok(java_home) = std::env::var("JAVA_HOME") { + let bin_name = if cfg!(windows) { "java.exe" } else { "java" }; + let java_path = PathBuf::from(&java_home).join("bin").join(bin_name); + if java_path.exists() { + candidates.push(java_path); + } + } + + candidates +} diff --git a/src-tauri/src/core/java/mod.rs b/src-tauri/src/core/java/mod.rs new file mode 100644 index 0000000..fd82390 --- /dev/null +++ b/src-tauri/src/core/java/mod.rs @@ -0,0 +1,527 @@ +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use tauri::{AppHandle, Emitter, Manager}; + +pub mod detection; +pub mod persistence; +pub mod priority; +pub mod validation; +pub mod provider; +pub mod providers; + +use crate::core::downloader::{DownloadQueue, JavaDownloadProgress, PendingJavaDownload}; +use crate::utils::zip; +use provider::JavaProvider; +use providers::AdoptiumProvider; + +const CACHE_DURATION_SECS: u64 = 24 * 60 * 60; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct JavaInstallation { + pub path: String, + pub version: String, + pub arch: String, + pub vendor: String, + pub source: String, + pub is_64bit: bool, +} + +#[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"), + } + } +} + +#[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, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct JavaCatalog { + pub releases: Vec<JavaReleaseInfo>, + pub available_major_versions: Vec<u32>, + pub lts_versions: Vec<u32>, + pub cached_at: u64, +} + +#[derive(Debug, Clone, Serialize)] +pub struct JavaDownloadInfo { + pub version: String, // e.g., "17.0.2+8" + pub release_name: String, // e.g., "jdk-17.0.2+8" + pub download_url: String, // Direct download URL + pub file_name: String, // e.g., "OpenJDK17U-jre_x64_linux_hotspot_17.0.2_8.tar.gz" + pub file_size: u64, // in bytes + pub checksum: Option<String>, // SHA256 checksum + pub image_type: String, // "jre" or "jdk" +} + + + +pub fn get_java_install_dir(app_handle: &AppHandle) -> PathBuf { + app_handle.path().app_data_dir().unwrap().join("java") +} + +fn get_catalog_cache_path(app_handle: &AppHandle) -> PathBuf { + app_handle + .path() + .app_data_dir() + .unwrap() + .join("java_catalog_cache.json") +} + +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; + } + + // Read cache file + let content = std::fs::read_to_string(&cache_path).ok()?; + let catalog: JavaCatalog = serde_json::from_str(&content).ok()?; + + // Get current time in seconds since UNIX_EPOCH + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + + // Check if cache is still valid + if now - catalog.cached_at < CACHE_DURATION_SECS { + Some(catalog) + } else { + None + } +} + +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(()) +} + +#[allow(dead_code)] +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(()) +} + +pub async fn fetch_java_catalog( + app_handle: &AppHandle, + force_refresh: bool, +) -> Result<JavaCatalog, String> { + let provider = AdoptiumProvider::new(); + provider.fetch_catalog(app_handle, force_refresh).await +} + +pub async fn fetch_java_release( + major_version: u32, + image_type: ImageType, +) -> Result<JavaDownloadInfo, String> { + let provider = AdoptiumProvider::new(); + provider.fetch_release(major_version, image_type).await +} + +pub async fn fetch_available_versions() -> Result<Vec<u32>, String> { + let provider = AdoptiumProvider::new(); + provider.available_versions().await +} + +pub async fn download_and_install_java( + app_handle: &AppHandle, + major_version: u32, + image_type: ImageType, + custom_path: Option<PathBuf>, +) -> Result<JavaInstallation, String> { + let provider = AdoptiumProvider::new(); + let info = provider.fetch_release(major_version, image_type).await?; + let file_name = info.file_name.clone(); + + let install_base = custom_path.unwrap_or_else(|| get_java_install_dir(app_handle)); + let version_dir = install_base.join(format!("{}-{}-{}", provider.install_prefix(), major_version, image_type)); + + std::fs::create_dir_all(&install_base) + .map_err(|e| format!("Failed to create installation directory: {}", e))?; + + 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)?; + + let archive_path = install_base.join(&info.file_name); + + 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))?; + !crate::core::downloader::verify_checksum(&data, Some(expected_checksum), None) + } else { + false + } + } else { + true + }; + + if need_download { + crate::core::downloader::download_with_resume( + app_handle, + &info.download_url, + &archive_path, + info.checksum.as_deref(), + info.file_size, + ) + .await?; + } + + 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, + }, + ); + + 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_top_level_dir(&version_dir)? + } else { + return Err(format!("Unsupported archive format: {}", info.file_name)); + }; + + let _ = std::fs::remove_file(&archive_path); + + 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() + )); + } + + let java_bin = std::fs::canonicalize(&java_bin).map_err(|e| e.to_string())?; + let java_bin = validation::strip_unc_prefix(java_bin); + + let installation = validation::check_java_installation(&java_bin) + .await + .ok_or_else(|| "Failed to verify Java installation".to_string())?; + + queue.remove(major_version, &image_type.to_string()); + queue.save(app_handle)?; + + 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) +} + +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 { + Ok(String::new()) + } +} + +pub async fn detect_java_installations() -> Vec<JavaInstallation> { + let mut installations = Vec::new(); + let candidates = detection::get_java_candidates(); + + for candidate in candidates { + if let Some(java) = validation::check_java_installation(&candidate).await { + if !installations + .iter() + .any(|j: &JavaInstallation| j.path == java.path) + { + installations.push(java); + } + } + } + + installations.sort_by(|a, b| { + let v_a = validation::parse_java_version(&a.version); + let v_b = validation::parse_java_version(&b.version); + v_b.cmp(&v_a) + }); + + installations +} + +pub async fn get_recommended_java(required_major_version: Option<u64>) -> Option<JavaInstallation> { + let installations = detect_java_installations().await; + + if let Some(required) = required_major_version { + installations.into_iter().find(|java| { + let major = validation::parse_java_version(&java.version); + major >= required as u32 + }) + } else { + installations.into_iter().next() + } +} + +pub async fn get_compatible_java( + app_handle: &AppHandle, + required_major_version: Option<u64>, + max_major_version: Option<u32>, +) -> Option<JavaInstallation> { + let installations = detect_all_java_installations(app_handle).await; + + if let Some(max_version) = max_major_version { + installations.into_iter().find(|java| { + let major = validation::parse_java_version(&java.version); + let meets_min = if let Some(required) = required_major_version { + major >= required as u32 + } else { + true + }; + meets_min && major <= max_version + }) + } else if let Some(required) = required_major_version { + installations.into_iter().find(|java| { + let major = validation::parse_java_version(&java.version); + major >= required as u32 + }) + } else { + installations.into_iter().next() + } +} + +pub async fn is_java_compatible( + java_path: &str, + required_major_version: Option<u64>, + max_major_version: Option<u32>, +) -> bool { + let java_path_buf = PathBuf::from(java_path); + if let Some(java) = validation::check_java_installation(&java_path_buf).await { + let major = validation::parse_java_version(&java.version); + let meets_min = if let Some(required) = required_major_version { + major >= required as u32 + } else { + true + }; + let meets_max = if let Some(max_version) = max_major_version { + major <= max_version + } else { + true + }; + meets_min && meets_max + } else { + false + } +} + +pub async fn detect_all_java_installations(app_handle: &AppHandle) -> Vec<JavaInstallation> { + let mut installations = detect_java_installations().await; + + 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() { + let java_bin = find_java_executable(&path); + if let Some(java_path) = java_bin { + if let Some(java) = validation::check_java_installation(&java_path).await { + if !installations.iter().any(|j| j.path == java.path) { + installations.push(java); + } + } + } + } + } + } + } + + installations.sort_by(|a, b| { + let v_a = validation::parse_java_version(&a.version); + let v_b = validation::parse_java_version(&b.version); + v_b.cmp(&v_a) + }); + + installations +} + +fn find_java_executable(dir: &PathBuf) -> Option<PathBuf> { + let bin_name = if cfg!(windows) { "java.exe" } else { "java" }; + + let direct_bin = dir.join("bin").join(bin_name); + if direct_bin.exists() { + let resolved = std::fs::canonicalize(&direct_bin).unwrap_or(direct_bin); + return Some(validation::strip_unc_prefix(resolved)); + } + + #[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); + } + } + + if let Ok(entries) = std::fs::read_dir(dir) { + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() { + let nested_bin = path.join("bin").join(bin_name); + if nested_bin.exists() { + let resolved = std::fs::canonicalize(&nested_bin).unwrap_or(nested_bin); + return Some(validation::strip_unc_prefix(resolved)); + } + + #[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 +} + +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 + }; + + 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) +} + +pub fn cancel_current_download() { + crate::core::downloader::cancel_java_download(); +} + +pub fn get_pending_downloads(app_handle: &AppHandle) -> Vec<PendingJavaDownload> { + let queue = DownloadQueue::load(app_handle); + queue.pending_downloads +} + +#[allow(dead_code)] +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/java/persistence.rs b/src-tauri/src/core/java/persistence.rs new file mode 100644 index 0000000..0932f2e --- /dev/null +++ b/src-tauri/src/core/java/persistence.rs @@ -0,0 +1,82 @@ +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use tauri::{AppHandle, Manager}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct JavaConfig { + pub user_defined_paths: Vec<String>, + pub preferred_java_path: Option<String>, + pub last_detection_time: u64, +} + +impl Default for JavaConfig { + fn default() -> Self { + Self { + user_defined_paths: Vec::new(), + preferred_java_path: None, + last_detection_time: 0, + } + } +} + +fn get_java_config_path(app_handle: &AppHandle) -> PathBuf { + app_handle + .path() + .app_data_dir() + .unwrap() + .join("java_config.json") +} + +pub fn load_java_config(app_handle: &AppHandle) -> JavaConfig { + let config_path = get_java_config_path(app_handle); + if !config_path.exists() { + return JavaConfig::default(); + } + + match std::fs::read_to_string(&config_path) { + Ok(content) => serde_json::from_str(&content).unwrap_or_default(), + Err(_) => JavaConfig::default(), + } +} + +pub fn save_java_config(app_handle: &AppHandle, config: &JavaConfig) -> Result<(), String> { + let config_path = get_java_config_path(app_handle); + let content = serde_json::to_string_pretty(config).map_err(|e| e.to_string())?; + std::fs::create_dir_all(config_path.parent().unwrap()).map_err(|e| e.to_string())?; + std::fs::write(&config_path, content).map_err(|e| e.to_string())?; + Ok(()) +} + +pub fn add_user_defined_path(app_handle: &AppHandle, path: String) -> Result<(), String> { + let mut config = load_java_config(app_handle); + if !config.user_defined_paths.contains(&path) { + config.user_defined_paths.push(path); + } + save_java_config(app_handle, &config) +} + +pub fn remove_user_defined_path(app_handle: &AppHandle, path: &str) -> Result<(), String> { + let mut config = load_java_config(app_handle); + config.user_defined_paths.retain(|p| p != path); + save_java_config(app_handle, &config) +} + +pub fn set_preferred_java_path(app_handle: &AppHandle, path: Option<String>) -> Result<(), String> { + let mut config = load_java_config(app_handle); + config.preferred_java_path = path; + save_java_config(app_handle, &config) +} + +pub fn get_preferred_java_path(app_handle: &AppHandle) -> Option<String> { + let config = load_java_config(app_handle); + config.preferred_java_path +} + +pub fn update_last_detection_time(app_handle: &AppHandle) -> Result<(), String> { + let mut config = load_java_config(app_handle); + config.last_detection_time = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + save_java_config(app_handle, &config) +} diff --git a/src-tauri/src/core/java/priority.rs b/src-tauri/src/core/java/priority.rs new file mode 100644 index 0000000..cf39fdd --- /dev/null +++ b/src-tauri/src/core/java/priority.rs @@ -0,0 +1,72 @@ +use tauri::AppHandle; + +use super::JavaInstallation; +use crate::core::java::persistence; +use crate::core::java::validation; + +pub async fn resolve_java_for_launch( + app_handle: &AppHandle, + instance_java_override: Option<&str>, + global_java_path: Option<&str>, + required_major_version: Option<u64>, + max_major_version: Option<u32>, +) -> Option<JavaInstallation> { + if let Some(override_path) = instance_java_override { + if !override_path.is_empty() { + let path_buf = std::path::PathBuf::from(override_path); + if let Some(java) = validation::check_java_installation(&path_buf).await { + if is_version_compatible(&java, required_major_version, max_major_version) { + return Some(java); + } + } + } + } + + if let Some(global_path) = global_java_path { + if !global_path.is_empty() { + let path_buf = std::path::PathBuf::from(global_path); + if let Some(java) = validation::check_java_installation(&path_buf).await { + if is_version_compatible(&java, required_major_version, max_major_version) { + return Some(java); + } + } + } + } + + let preferred = persistence::get_preferred_java_path(app_handle); + if let Some(pref_path) = preferred { + let path_buf = std::path::PathBuf::from(&pref_path); + if let Some(java) = validation::check_java_installation(&path_buf).await { + if is_version_compatible(&java, required_major_version, max_major_version) { + return Some(java); + } + } + } + + let installations = super::detect_all_java_installations(app_handle).await; + installations + .into_iter() + .find(|java| is_version_compatible(java, required_major_version, max_major_version)) +} + +fn is_version_compatible( + java: &JavaInstallation, + required_major_version: Option<u64>, + max_major_version: Option<u32>, +) -> bool { + let major = validation::parse_java_version(&java.version); + + let meets_min = if let Some(required) = required_major_version { + major >= required as u32 + } else { + true + }; + + let meets_max = if let Some(max_version) = max_major_version { + major <= max_version + } else { + true + }; + + meets_min && meets_max +} diff --git a/src-tauri/src/core/java/provider.rs b/src-tauri/src/core/java/provider.rs new file mode 100644 index 0000000..0f9d78a --- /dev/null +++ b/src-tauri/src/core/java/provider.rs @@ -0,0 +1,41 @@ +use crate::core::java::{ImageType, JavaCatalog, JavaDownloadInfo}; +use tauri::AppHandle; + +/// Trait for Java download providers (Adoptium, Temurin, Corretto, etc.) +/// +/// Implements SOLID principles: +/// - Single Responsibility: Each provider handles one download source +/// - Open/Closed: New providers can be added without modifying existing code +/// - Liskov Substitution: All providers are interchangeable +/// - Interface Segregation: Minimal required methods +/// - Dependency Inversion: Code depends on trait, not concrete implementations +pub trait JavaProvider: Send + Sync { + /// Fetch the Java catalog (all available versions for this provider) + async fn fetch_catalog( + &self, + app_handle: &AppHandle, + force_refresh: bool, + ) -> Result<JavaCatalog, String>; + + /// Fetch a specific Java release + async fn fetch_release( + &self, + major_version: u32, + image_type: ImageType, + ) -> Result<JavaDownloadInfo, String>; + + /// Get list of available major versions + async fn available_versions(&self) -> Result<Vec<u32>, String>; + + /// Get provider name (e.g., "adoptium", "corretto") + fn provider_name(&self) -> &'static str; + + /// Get OS name for this provider's API + fn os_name(&self) -> &'static str; + + /// Get architecture name for this provider's API + fn arch_name(&self) -> &'static str; + + /// Get installation directory prefix (e.g., "temurin", "corretto") + fn install_prefix(&self) -> &'static str; +} diff --git a/src-tauri/src/core/java/providers/adoptium.rs b/src-tauri/src/core/java/providers/adoptium.rs new file mode 100644 index 0000000..dfd4c0e --- /dev/null +++ b/src-tauri/src/core/java/providers/adoptium.rs @@ -0,0 +1,306 @@ +use crate::core::java::provider::JavaProvider; +use crate::core::java::{ImageType, JavaCatalog, JavaDownloadInfo, JavaReleaseInfo}; +use serde::Deserialize; +use tauri::AppHandle; + +const ADOPTIUM_API_BASE: &str = "https://api.adoptium.net/v3"; + +#[derive(Debug, Clone, Deserialize)] +pub struct AdoptiumAsset { + pub binary: AdoptiumBinary, + pub release_name: String, + pub version: AdoptiumVersionData, +} + +#[derive(Debug, Clone, Deserialize)] +#[allow(dead_code)] +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>, +} + +#[derive(Debug, Clone, Deserialize)] +#[allow(dead_code)] +pub struct AdoptiumVersionData { + pub major: u32, + pub minor: u32, + pub security: u32, + pub semver: String, + pub openjdk_version: String, +} + +#[derive(Debug, Clone, Deserialize)] +#[allow(dead_code)] +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>, +} + +pub struct AdoptiumProvider; + +impl AdoptiumProvider { + pub fn new() -> Self { + Self + } +} + +impl Default for AdoptiumProvider { + fn default() -> Self { + Self::new() + } +} + +impl JavaProvider for AdoptiumProvider { + async fn fetch_catalog( + &self, + app_handle: &AppHandle, + force_refresh: bool, + ) -> Result<JavaCatalog, String> { + if !force_refresh { + if let Some(cached) = super::super::load_cached_catalog(app_handle) { + return Ok(cached); + } + } + + let os = self.os_name(); + let arch = self.arch_name(); + let client = reqwest::Client::new(); + + 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(); + + 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(); + let release_info = 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(), + }; + releases.push(release_info); + } + } + } else { + let release_info = 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(), + }; + releases.push(release_info); + } + } + Err(_) => { + 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, + }; + + let _ = super::super::save_catalog_cache(app_handle, &catalog); + + Ok(catalog) + } + + async fn fetch_release( + &self, + major_version: u32, + image_type: ImageType, + ) -> Result<JavaDownloadInfo, String> { + let os = self.os_name(); + let arch = self.arch_name(); + + 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, + }) + } + + async fn available_versions(&self) -> 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))?; + + let releases: AvailableReleases = response + .json() + .await + .map_err(|e| format!("Failed to parse response: {}", e))?; + + Ok(releases.available_releases) + } + + fn provider_name(&self) -> &'static str { + "adoptium" + } + + fn os_name(&self) -> &'static str { + #[cfg(target_os = "linux")] + { + 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" + } + } + + fn arch_name(&self) -> &'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" + } + } + + fn install_prefix(&self) -> &'static str { + "temurin" + } +} diff --git a/src-tauri/src/core/java/providers/mod.rs b/src-tauri/src/core/java/providers/mod.rs new file mode 100644 index 0000000..16eb5c7 --- /dev/null +++ b/src-tauri/src/core/java/providers/mod.rs @@ -0,0 +1,3 @@ +pub mod adoptium; + +pub use adoptium::AdoptiumProvider; diff --git a/src-tauri/src/core/java/validation.rs b/src-tauri/src/core/java/validation.rs new file mode 100644 index 0000000..ec7745d --- /dev/null +++ b/src-tauri/src/core/java/validation.rs @@ -0,0 +1,120 @@ +use std::path::PathBuf; +use std::process::Command; +use std::time::Duration; + +#[cfg(target_os = "windows")] +use std::os::windows::process::CommandExt; + +use super::JavaInstallation; + +const JAVA_CHECK_TIMEOUT: Duration = Duration::from_secs(5); + +pub fn strip_unc_prefix(path: PathBuf) -> PathBuf { + #[cfg(target_os = "windows")] + { + let s = path.to_string_lossy().to_string(); + if s.starts_with(r"\\?\") { + return PathBuf::from(&s[4..]); + } + } + path +} + +pub async fn check_java_installation(path: &PathBuf) -> Option<JavaInstallation> { + let path = path.clone(); + tokio::task::spawn_blocking(move || { + check_java_installation_blocking(&path) + }) + .await + .ok()? +} + +fn check_java_installation_blocking(path: &PathBuf) -> Option<JavaInstallation> { + let mut cmd = Command::new(path); + cmd.arg("-version"); + #[cfg(target_os = "windows")] + cmd.creation_flags(0x08000000); + + let output = cmd.output().ok()?; + + let version_output = String::from_utf8_lossy(&output.stderr); + + let version = parse_version_string(&version_output)?; + let arch = extract_architecture(&version_output); + let vendor = extract_vendor(&version_output); + let is_64bit = version_output.contains("64-Bit"); + + Some(JavaInstallation { + path: path.to_string_lossy().to_string(), + version, + arch, + vendor, + source: "system".to_string(), + is_64bit, + }) +} + +pub fn parse_version_string(output: &str) -> Option<String> { + for line in output.lines() { + if line.contains("version") { + if let Some(start) = line.find('"') { + if let Some(end) = line[start + 1..].find('"') { + return Some(line[start + 1..start + 1 + end].to_string()); + } + } + } + } + None +} + +pub fn parse_java_version(version: &str) -> u32 { + let parts: Vec<&str> = version.split('.').collect(); + if let Some(first) = parts.first() { + // Handle both legacy (1.x) and modern (x) versioning + if *first == "1" { + // Legacy versioning + parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0) + } else { + // Modern versioning + first.parse().unwrap_or(0) + } + } else { + 0 + } +} + +pub fn extract_architecture(version_output: &str) -> String { + if version_output.contains("64-Bit") { + "x64".to_string() + } else if version_output.contains("32-Bit") { + "x86".to_string() + } else if version_output.contains("aarch64") || version_output.contains("ARM64") { + "aarch64".to_string() + } else { + "x64".to_string() + } +} + +pub fn extract_vendor(version_output: &str) -> String { + let lower = version_output.to_lowercase(); + + if lower.contains("temurin") || lower.contains("adoptium") { + "Eclipse Adoptium".to_string() + } else if lower.contains("openjdk") { + "OpenJDK".to_string() + } else if lower.contains("oracle") { + "Oracle".to_string() + } else if lower.contains("microsoft") { + "Microsoft".to_string() + } else if lower.contains("zulu") { + "Azul Zulu".to_string() + } else if lower.contains("corretto") { + "Amazon Corretto".to_string() + } else if lower.contains("liberica") { + "BellSoft Liberica".to_string() + } else if lower.contains("graalvm") { + "GraalVM".to_string() + } else { + "Unknown".to_string() + } +} diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index dcbd47a..12dff7c 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -12,3 +12,5 @@ pub mod manifest; pub mod maven; pub mod rules; pub mod version_merge; + +pub use java::JavaInstallation; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 45fa77b..5fa46b8 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -204,7 +204,7 @@ async fn start_game( let mut java_path_to_use = config.java_path.clone(); if !java_path_to_use.is_empty() && java_path_to_use != "java" { let is_compatible = - core::java::is_java_compatible(&java_path_to_use, required_java_major, max_java_major); + core::java::is_java_compatible(&java_path_to_use, required_java_major, max_java_major).await; if !is_compatible { emit_log!( @@ -216,7 +216,7 @@ async fn start_game( // Try to find a compatible Java version if let Some(compatible_java) = - core::java::get_compatible_java(app_handle, required_java_major, max_java_major) + core::java::get_compatible_java(app_handle, required_java_major, max_java_major).await { emit_log!( window, @@ -252,7 +252,7 @@ async fn start_game( } else { // No Java configured, try to find a compatible one if let Some(compatible_java) = - core::java::get_compatible_java(app_handle, required_java_major, max_java_major) + core::java::get_compatible_java(app_handle, required_java_major, max_java_major).await { emit_log!( window, @@ -1556,10 +1556,18 @@ async fn refresh_account( /// Detect Java installations on the system #[tauri::command] +async fn detect_all_java_installations( + app_handle: tauri::AppHandle, +) -> Result<Vec<core::java::JavaInstallation>, String> { + Ok(core::java::detect_all_java_installations(&app_handle).await) +} + +/// Alias for detect_all_java_installations (for backward compatibility) +#[tauri::command] async fn detect_java( app_handle: tauri::AppHandle, ) -> Result<Vec<core::java::JavaInstallation>, String> { - Ok(core::java::detect_all_java_installations(&app_handle)) + Ok(core::java::detect_all_java_installations(&app_handle).await) } /// Get recommended Java for a specific Minecraft version @@ -1567,7 +1575,7 @@ async fn detect_java( async fn get_recommended_java( required_major_version: Option<u64>, ) -> Result<Option<core::java::JavaInstallation>, String> { - Ok(core::java::get_recommended_java(required_major_version)) + Ok(core::java::get_recommended_java(required_major_version).await) } /// Get Adoptium Java download info @@ -2065,7 +2073,7 @@ async fn install_forge( config.java_path.clone() } else { // Try to find a suitable Java installation - let javas = core::java::detect_all_java_installations(app_handle); + let javas = core::java::detect_all_java_installations(app_handle).await; if let Some(java) = javas.first() { java.path.clone() } else { |