From f030496659de025f10e5dda79ac65d94ebcd1280 Mon Sep 17 00:00:00 2001 From: Uriel Date: Tue, 24 Jan 2023 15:39:48 -0300 Subject: [PATCH] Better JVM detection (#494) --- Cargo.lock | 6 ++-- gui/src-tauri/Cargo.toml | 8 +++-- gui/src-tauri/src/main.rs | 67 +++++++++++++++++++-------------------- solarxr-protocol | 2 +- 4 files changed, 43 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87825c53b..dc505756a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2560,9 +2560,9 @@ dependencies = [ [[package]] name = "shadow-rs" -version = "0.19.0" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c401e795850edb4e9fdde5940f856364f0fbab573e8dea58f6ee5f85fcf471d" +checksum = "8da90bd2a722461267a2c5d20b2c6de76dba087348e60484fd6c3fbb48b766d6" dependencies = [ "const_format", "git2", @@ -2609,10 +2609,12 @@ dependencies = [ name = "slimevr" version = "0.0.0" dependencies = [ + "cfg-if", "cfg_aliases", "clap", "clap-verbosity-flag", "const_format", + "glob", "log", "open", "pretty_env_logger", diff --git a/gui/src-tauri/Cargo.toml b/gui/src-tauri/Cargo.toml index d5c405d21..acac05140 100644 --- a/gui/src-tauri/Cargo.toml +++ b/gui/src-tauri/Cargo.toml @@ -23,7 +23,7 @@ custom-protocol = ["tauri/custom-protocol"] [build-dependencies] tauri-build = { version = "1.2", features = [] } cfg_aliases = "0.1" -shadow-rs = "0.19" +shadow-rs = "0.20" [dependencies] serde_json = "1" @@ -36,10 +36,12 @@ clap-verbosity-flag = "2" tauri-plugin-window-state = { git = "https://github.com/tauri-apps/tauri-plugin-window-state", rev = "refs/pull/52/head" } rand = "0.8.5" tempfile = "3" -which = "4.3.0" +which = "4.3" +glob = "0.3" open = "3" -shadow-rs = { version = "0.19", default-features = false } +shadow-rs = { version = "0.20", default-features = false } const_format = "0.2.30" +cfg-if = "1" [target.'cfg(windows)'.dependencies] win32job = "1" diff --git a/gui/src-tauri/src/main.rs b/gui/src-tauri/src/main.rs index 3d2a1b3fc..ed9255964 100644 --- a/gui/src-tauri/src/main.rs +++ b/gui/src-tauri/src/main.rs @@ -5,7 +5,7 @@ use std::io::Write; #[cfg(windows)] use std::os::windows::process::CommandExt; use std::panic; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::{Child, Stdio}; use clap::Parser; @@ -15,7 +15,6 @@ use shadow_rs::shadow; use tauri::api::process::Command; use tauri::Manager; use tempfile::Builder; -use which::which_all; #[cfg(windows)] /// For Commands on Windows so they dont create terminals @@ -53,41 +52,25 @@ struct Cli { verbose: clap_verbosity_flag::Verbosity, } -fn is_valid_path(path: &PathBuf) -> bool { +fn is_valid_path(path: &Path) -> bool { path.join("slimevr.jar").exists() } fn get_launch_path(cli: Cli) -> Option { - if let Some(path) = cli.launch_from_path { - if path.exists() && is_valid_path(&path) { - return Some(path); - } - } - - let path = env::current_dir().unwrap(); - if is_valid_path(&path) { - return Some(path); - } - - let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - if is_valid_path(&path) { - return Some(path); - } - - let path = PathBuf::from("/usr/share/slimevr/"); - if is_valid_path(&path) { - return Some(path); - } - - // This is only for AppImage - if let Some(appimage) = env::var_os("APPDIR") { - let path = PathBuf::from(appimage); - if is_valid_path(&path) { - return Some(path); - } - } - - None + let paths = [ + cli.launch_from_path, + // AppImage passes the fakeroot in `APPDIR` env var. + env::var_os("APPDIR").map(|x| PathBuf::from(x)), + env::current_dir().ok(), + Some(PathBuf::from(env!("CARGO_MANIFEST_DIR"))), + // For flatpak container + Some(PathBuf::from("/app/share/slimevr/")), + Some(PathBuf::from("/usr/share/slimevr/")), + ]; + paths + .into_iter() + .filter_map(|x| x) + .find(|x| is_valid_path(x)) } fn spawn_java(java: &OsStr, java_version: &OsStr) -> std::io::Result { @@ -306,7 +289,23 @@ fn valid_java_paths() -> Vec<(OsString, i32)> { // Otherwise check if anything else is a supported version let mut childs = vec![]; - for java in which_all(JAVA_BIN).unwrap() { + cfg_if::cfg_if! { + if #[cfg(target_os = "macos")] { + // TODO: Actually use macOS paths + let libs = which::which_all(JAVA_BIN).unwrap(); + } else if #[cfg(unix)] { + // Linux JVMs are saved on /usr/lib/jvm from what I found out, + // there is usually a default dir and a default-runtime dir also which are linked + // to the current default runtime and the current default JDK (I think it's JDK) + let libs = glob::glob(concatcp!("/usr/lib/jvm/*/bin/", JAVA_BIN)) + .unwrap() + .filter_map(|res| res.ok()); + } else { + let libs = which::which_all(JAVA_BIN).unwrap(); + } + } + + for java in libs { let res = spawn_java(java.as_os_str(), java_version.as_os_str()); match res { diff --git a/solarxr-protocol b/solarxr-protocol index 6d4c79277..35319f428 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit 6d4c7927708d523e5cd36e228c71024d8f5cdd79 +Subproject commit 35319f428687c1cab7d4f8547a9e53c1b444975b