Skip to main content

elemental_core/runtime/providers/
envpath.rs

1use std::env::consts::EXE_SUFFIX;
2use std::path::{Path, PathBuf};
3
4use async_trait::async_trait;
5
6use super::super::provider::RuntimeProvider;
7use crate::runtime::discovery::{find_runtime_homes, is_runtime_home, path_exists};
8
9const MINECRAFT_RUNTIME_SCAN_DEPTH: usize = 4;
10const USER_TOOLCHAIN_SCAN_DEPTH: usize = 3;
11
12#[derive(Default)]
13pub struct EnvPathProvider;
14
15#[async_trait]
16impl RuntimeProvider for EnvPathProvider {
17    async fn list(&self) -> Vec<PathBuf> {
18        let mut javas = Self::find_java_in_path().await;
19        javas.extend(Self::get_user_toolchain_java_distribution().await);
20        javas.extend(Self::get_official_java_distribution_bundles().await);
21        javas
22    }
23
24    fn name(&self) -> &'static str {
25        "Environment"
26    }
27}
28
29impl EnvPathProvider {
30    async fn find_java_in_path() -> Vec<PathBuf> {
31        let Some(path) = std::env::var_os("PATH") else {
32            return Vec::new();
33        };
34
35        let mut javas = Vec::new();
36        for directory in std::env::split_paths(&path) {
37            if directory.as_os_str().is_empty() {
38                continue;
39            }
40            #[cfg(windows)]
41            if Self::is_oracle_javapath_shim(&directory) {
42                continue;
43            }
44            if let Some(runtime_home) = Self::runtime_home_from_path_entry(&directory).await {
45                javas.push(runtime_home);
46            }
47        }
48        javas
49    }
50
51    async fn runtime_home_from_path_entry(directory: &Path) -> Option<PathBuf> {
52        if is_runtime_home(directory).await {
53            return Some(directory.to_path_buf());
54        }
55
56        let executable = directory.join(format!("java{EXE_SUFFIX}"));
57        if !path_exists(&executable).await {
58            return None;
59        }
60
61        let is_bin_directory = directory
62            .file_name()
63            .is_some_and(|name| name.to_string_lossy().eq_ignore_ascii_case("bin"));
64        if !is_bin_directory {
65            return None;
66        }
67
68        directory.parent().map(Path::to_path_buf)
69    }
70
71    async fn get_user_toolchain_java_distribution() -> Vec<PathBuf> {
72        let locations = Self::get_user_toolchain_search_locations();
73        let mut javas = Vec::new();
74        for location in locations {
75            javas.extend(find_runtime_homes(&location, USER_TOOLCHAIN_SCAN_DEPTH).await);
76        }
77        javas
78    }
79
80    fn get_user_toolchain_search_locations() -> Vec<PathBuf> {
81        let Some(home) = dirs::home_dir() else {
82            tracing::warn!("can't determine home directory for Java toolchain search");
83            return Vec::new();
84        };
85
86        let sdkman_root = std::env::var_os("SDKMAN_DIR")
87            .map(PathBuf::from)
88            .unwrap_or_else(|| home.join(".sdkman"));
89        let asdf_root = std::env::var_os("ASDF_DATA_DIR")
90            .map(PathBuf::from)
91            .unwrap_or_else(|| home.join(".asdf"));
92        let gradle_root = std::env::var_os("GRADLE_USER_HOME")
93            .map(PathBuf::from)
94            .unwrap_or_else(|| home.join(".gradle"));
95
96        let mut locations = Vec::new();
97        locations.extend([
98            home.join(".jdks"),
99            sdkman_root.join("candidates").join("java"),
100            asdf_root.join("installs").join("java"),
101            gradle_root.join("jdks"),
102        ]);
103
104        #[cfg(target_os = "macos")]
105        {
106            locations.push(
107                home.join("Library")
108                    .join("Java")
109                    .join("JavaVirtualMachines"),
110            );
111        }
112
113        locations
114    }
115
116    async fn get_official_java_distribution_bundles() -> Vec<PathBuf> {
117        let search_locations = Self::get_bundle_search_locations();
118        let mut javas = Vec::new();
119        for location in search_locations {
120            javas.extend(find_runtime_homes(&location, MINECRAFT_RUNTIME_SCAN_DEPTH).await);
121        }
122        javas
123    }
124
125    fn get_bundle_search_locations() -> Vec<PathBuf> {
126        let mut locations = Vec::new();
127
128        #[cfg(windows)]
129        {
130            if let Some(data_dir) = dirs::data_dir() {
131                locations.push(data_dir.join(".minecraft").join("runtime"));
132            }
133            if let Some(local_data_dir) = dirs::data_local_dir() {
134                locations.push(local_data_dir.join(".minecraft").join("runtime"));
135                locations.push(
136                    local_data_dir
137                        .join("Packages")
138                        .join("Microsoft.4297127D64EC6_8wekyb3d8bbwe")
139                        .join("LocalCache")
140                        .join("Local")
141                        .join("runtime"),
142                );
143            }
144            if let Some(program_files_x86) = std::env::var_os("ProgramFiles(x86)") {
145                locations.push(
146                    PathBuf::from(program_files_x86)
147                        .join("Minecraft Launcher")
148                        .join("runtime"),
149                );
150            }
151        }
152
153        #[cfg(target_os = "linux")]
154        {
155            if let Some(home) = dirs::home_dir() {
156                locations.push(home.join(".minecraft").join("runtime"));
157            }
158        }
159
160        #[cfg(target_os = "macos")]
161        {
162            if let Some(home) = dirs::home_dir() {
163                locations.push(
164                    home.join("Library")
165                        .join("Application Support")
166                        .join("minecraft")
167                        .join("runtime"),
168                );
169            }
170        }
171
172        locations
173    }
174
175    #[cfg(windows)]
176    fn is_oracle_javapath_shim(path: &Path) -> bool {
177        let normalized = path
178            .to_string_lossy()
179            .replace('/', "\\")
180            .to_ascii_lowercase();
181        normalized.contains("\\common files\\oracle\\java\\")
182    }
183}