Performance (#89)

* jre async

* mac support

* fixed some settings not being saved to file

* fixed older version of mac random crashing bug

* added specific mac version detection

* linux support for jre changes

* added app storage options

* tauri features change

* dependency fix

* removed debug statement

* restructured to not pass css through rust

* changed to os_info

* rerun cicd
This commit is contained in:
Wyatt Verchere
2023-04-19 11:44:44 -07:00
committed by GitHub
parent d414e07f41
commit 16e015b527
14 changed files with 164 additions and 82 deletions

View File

@@ -15,10 +15,10 @@ pub const JAVA_18PLUS_KEY: &str = "JAVA_18PLUS";
// Autodetect JavaSettings default
// Make a guess for what the default Java global settings should be
pub fn autodetect_java_globals() -> crate::Result<JavaGlobals> {
let mut java_8 = find_java8_jres()?;
let mut java_17 = find_java17_jres()?;
let mut java_18plus = find_java18plus_jres()?;
pub async fn autodetect_java_globals() -> crate::Result<JavaGlobals> {
let mut java_8 = find_java8_jres().await?;
let mut java_17 = find_java17_jres().await?;
let mut java_18plus = find_java18plus_jres().await?;
// Simply select last one found for initial guess
let mut java_globals = JavaGlobals::new();
@@ -76,9 +76,9 @@ pub async fn get_optimal_jre_key(profile: &Profile) -> crate::Result<String> {
}
// Searches for jres on the system that are 1.18 or higher
pub fn find_java18plus_jres() -> crate::Result<Vec<JavaVersion>> {
pub async fn find_java18plus_jres() -> crate::Result<Vec<JavaVersion>> {
let version = extract_java_majorminor_version("1.18")?;
let jres = jre::get_all_jre()?;
let jres = jre::get_all_jre().await?;
// Filter out JREs that are not 1.17 or higher
Ok(jres
.into_iter()
@@ -94,9 +94,9 @@ pub fn find_java18plus_jres() -> crate::Result<Vec<JavaVersion>> {
}
// Searches for jres on the system that are 1.8 exactly
pub fn find_java8_jres() -> crate::Result<Vec<JavaVersion>> {
pub async fn find_java8_jres() -> crate::Result<Vec<JavaVersion>> {
let version = extract_java_majorminor_version("1.8")?;
let jres = jre::get_all_jre()?;
let jres = jre::get_all_jre().await?;
// Filter out JREs that are not 1.8
Ok(jres
@@ -113,9 +113,9 @@ pub fn find_java8_jres() -> crate::Result<Vec<JavaVersion>> {
}
// Searches for jres on the system that are 1.17 exactly
pub fn find_java17_jres() -> crate::Result<Vec<JavaVersion>> {
pub async fn find_java17_jres() -> crate::Result<Vec<JavaVersion>> {
let version = extract_java_majorminor_version("1.17")?;
let jres = jre::get_all_jre()?;
let jres = jre::get_all_jre().await?;
// Filter out JREs that are not 1.8
Ok(jres
@@ -132,17 +132,17 @@ pub fn find_java17_jres() -> crate::Result<Vec<JavaVersion>> {
}
// Get all JREs that exist on the system
pub fn get_all_jre() -> crate::Result<Vec<JavaVersion>> {
Ok(jre::get_all_jre()?)
pub async fn get_all_jre() -> crate::Result<Vec<JavaVersion>> {
Ok(jre::get_all_jre().await?)
}
pub async fn validate_globals() -> crate::Result<bool> {
let state = State::get().await?;
let settings = state.settings.read().await;
Ok(settings.java_globals.is_all_valid())
Ok(settings.java_globals.is_all_valid().await)
}
// Validates JRE at a given at a given path
pub async fn check_jre(path: PathBuf) -> crate::Result<Option<JavaVersion>> {
Ok(jre::check_java_at_filepath(&path))
Ok(jre::check_java_at_filepath(&path).await)
}

View File

@@ -21,5 +21,6 @@ pub async fn set(settings: Settings) -> crate::Result<()> {
// Replaces the settings struct in the RwLock with the passed argument
*state.settings.write().await = settings;
state.reset_semaphore().await; // reset semaphore to new max
State::sync().await?;
Ok(())
}

View File

@@ -26,7 +26,6 @@ impl DirectoryInfo {
"Could not find valid config dir".to_string(),
))?;
dbg!(&config_dir);
fs::create_dir_all(&config_dir).await.map_err(|err| {
crate::ErrorKind::FSError(format!(
"Error creating Theseus config directory: {err}"

View File

@@ -37,11 +37,12 @@ impl JavaGlobals {
// Validates that every path here is a valid Java version and that the version matches the version stored here
// If false, when checked, the user should be prompted to reselect the Java version
pub fn is_all_valid(&self) -> bool {
pub async fn is_all_valid(&self) -> bool {
for (_, java) in self.0.iter() {
let jre = jre::check_java_at_filepath(
PathBuf::from(&java.path).as_path(),
);
)
.await;
if let Some(jre) = jre {
if jre.version != java.version {
return false;

View File

@@ -130,7 +130,7 @@ impl State {
// On launcher initialization, if global java variables are unset, try to find and set them
// (they are required for the game to launch)
if settings.java_globals.count() == 0 {
settings.java_globals = jre::autodetect_java_globals()?;
settings.java_globals = jre::autodetect_java_globals().await?;
}
Ok(Arc::new(Self {

View File

@@ -1,4 +1,5 @@
use dunce::canonicalize;
use futures::prelude::*;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
@@ -6,6 +7,7 @@ use std::env;
use std::path::PathBuf;
use std::process::Command;
use std::{collections::HashSet, path::Path};
use tokio::task::JoinError;
#[cfg(target_os = "windows")]
use winreg::{
@@ -23,12 +25,11 @@ pub struct JavaVersion {
// Returns a Vec of unique JavaVersions from the PATH, Windows Registry Keys and common Java locations
#[cfg(target_os = "windows")]
#[tracing::instrument]
pub fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
// Use HashSet to avoid duplicates
let mut jres = HashSet::new();
pub async fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
let mut jre_paths = HashSet::new();
// Add JRES directly on PATH
jres.extend(get_all_jre_path()?);
jre_paths.extend(get_all_jre_path().await?);
// Hard paths for locations for commonly installed .exes
let java_paths = [r"C:/Program Files/Java", r"C:/Program Files (x86)/Java"];
@@ -36,9 +37,7 @@ pub fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
let Ok(java_subpaths) = std::fs::read_dir(java_path) else {continue };
for java_subpath in java_subpaths {
let path = java_subpath?.path();
if let Some(j) = check_java_at_filepath(&path.join("bin")) {
jres.insert(j);
}
jre_paths.insert(path.join("bin"));
}
}
@@ -53,28 +52,35 @@ pub fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
r"SOFTWARE\\Eclipse Foundation\\JDK", // Eclipse
r"SOFTWARE\\Microsoft\\JDK", // Microsoft
];
for key in key_paths {
if let Ok(jre_key) = RegKey::predef(HKEY_LOCAL_MACHINE)
.open_subkey_with_flags(key, KEY_READ | KEY_WOW64_32KEY)
{
jres.extend(get_all_jre_winregkey(jre_key)?);
jre_paths.extend(get_paths_from_jre_winregkey(jre_key)?);
}
if let Ok(jre_key) = RegKey::predef(HKEY_LOCAL_MACHINE)
.open_subkey_with_flags(key, KEY_READ | KEY_WOW64_64KEY)
{
jres.extend(get_all_jre_winregkey(jre_key)?);
jre_paths.extend(get_paths_from_jre_winregkey(jre_key)?);
}
}
Ok(jres.into_iter().collect())
// Get JRE versions from potential paths concurrently
let j = check_java_at_filepaths(jre_paths)
.await?
.into_iter()
.collect();
Ok(j)
}
// Gets paths rather than search directly as RegKeys should not be passed asynchronously (do not impl Send)
#[cfg(target_os = "windows")]
#[tracing::instrument]
pub fn get_all_jre_winregkey(
pub fn get_paths_from_jre_winregkey(
jre_key: RegKey,
) -> Result<HashSet<JavaVersion>, JREError> {
let mut jres = HashSet::new();
) -> Result<HashSet<PathBuf>, JREError> {
let mut jre_paths = HashSet::new();
for subkey in jre_key.enum_keys() {
let subkey = subkey?;
@@ -87,26 +93,23 @@ pub fn get_all_jre_winregkey(
let path: Result<String, std::io::Error> =
subkey.get_value(subkey_value);
let Ok(path) = path else {continue};
if let Some(j) =
check_java_at_filepath(&PathBuf::from(path).join("bin"))
{
jres.insert(j);
}
jre_paths.insert(PathBuf::from(path).join("bin"));
}
}
Ok(jres)
Ok(jre_paths)
}
// Entrypoint function (Mac)
// Returns a Vec of unique JavaVersions from the PATH, and common Java locations
#[cfg(target_os = "macos")]
#[tracing::instrument]
pub fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
pub async fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
// Use HashSet to avoid duplicates
let mut jres = HashSet::new();
let mut jre_paths = HashSet::new();
// Add JREs directly on PATH
jres.extend(get_all_jre_path()?);
jre_paths.extend(get_all_jre_path().await?);
// Hard paths for locations for commonly installed .exes
let java_paths = [
@@ -115,36 +118,34 @@ pub fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
r"/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands",
];
for path in java_paths {
if let Some(j) =
check_java_at_filepath(&PathBuf::from(path).join("bin"))
{
jres.insert(j);
}
jre_paths.insert(PathBuf::from(path));
}
// Iterate over JavaVirtualMachines/(something)/Contents/Home/bin
let base_path = PathBuf::from("/Library/Java/JavaVirtualMachines/");
if base_path.is_dir() {
for entry in std::fs::read_dir(base_path)? {
let entry = entry?.path().join("Contents/Home/bin");
if let Some(j) = check_java_at_filepath(entry.as_path()) {
jres.insert(j);
}
jre_paths.insert(entry);
}
}
Ok(jres.into_iter().collect())
// Get JRE versions from potential paths concurrently
let j = check_java_at_filepaths(jre_paths)
.await?
.into_iter()
.collect();
Ok(j)
}
// Entrypoint function (Linux)
// Returns a Vec of unique JavaVersions from the PATH, and common Java locations
#[cfg(target_os = "linux")]
#[tracing::instrument]
pub fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
pub async fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
// Use HashSet to avoid duplicates
let mut jres = HashSet::new();
let mut jre_paths = HashSet::new();
// Add JREs directly on PATH
jres.extend(get_all_jre_path()?);
jre_paths.extend(get_all_jre_path().await?);
// Hard paths for locations for commonly installed locations
let java_paths = [
@@ -156,34 +157,23 @@ pub fn get_all_jre() -> Result<Vec<JavaVersion>, JREError> {
r"/opt/jdks",
];
for path in java_paths {
if let Some(j) =
check_java_at_filepath(&PathBuf::from(path).join("jre").join("bin"))
{
jres.insert(j);
}
if let Some(j) =
check_java_at_filepath(&PathBuf::from(path).join("bin"))
{
jres.insert(j);
}
jre_paths.insert(PathBuf::from(path).join("jre").join("bin"));
jre_paths.insert(PathBuf::from(path).join("bin"));
}
Ok(jres.into_iter().collect())
// Get JRE versions from potential paths concurrently
let j = check_java_at_filepaths(jre_paths)
.await?
.into_iter()
.collect();
Ok(j)
}
// Gets all JREs from the PATH env variable
#[tracing::instrument]
fn get_all_jre_path() -> Result<HashSet<JavaVersion>, JREError> {
async fn get_all_jre_path() -> Result<HashSet<PathBuf>, JREError> {
// Iterate over values in PATH variable, where accessible JREs are referenced
let paths = env::var("PATH")?;
let paths = env::split_paths(&paths);
let mut jres = HashSet::new();
for path in paths {
if let Some(j) = check_java_at_filepath(&path) {
jres.insert(j);
}
}
Ok(jres)
Ok(env::split_paths(&paths).collect())
}
#[cfg(target_os = "windows")]
@@ -194,10 +184,28 @@ const JAVA_BIN: &str = "java.exe";
#[allow(dead_code)]
const JAVA_BIN: &str = "java";
// For each example filepath in 'paths', perform check_java_at_filepath, checking each one concurrently
// and returning a JavaVersion for every valid path that points to a java bin
#[tracing::instrument]
pub async fn check_java_at_filepaths(
paths: HashSet<PathBuf>,
) -> Result<HashSet<JavaVersion>, JREError> {
let jres = stream::iter(paths.into_iter())
.map(|p: PathBuf| {
tokio::task::spawn(async move { check_java_at_filepath(&p).await })
})
.buffer_unordered(64)
.collect::<Vec<_>>()
.await;
let jres: Result<Vec<_>, JoinError> = jres.into_iter().collect();
Ok(jres?.into_iter().flatten().collect())
}
// For example filepath 'path', attempt to resolve it and get a Java version at this path
// If no such path exists, or no such valid java at this path exists, returns None
#[tracing::instrument]
pub fn check_java_at_filepath(path: &Path) -> Option<JavaVersion> {
pub async fn check_java_at_filepath(path: &Path) -> Option<JavaVersion> {
// Attempt to canonicalize the potential java filepath
// If it fails, this path does not exist and None is returned (no Java here)
let Ok(path) = canonicalize(path) else { return None };
@@ -289,6 +297,9 @@ pub enum JREError {
#[error("Parsing error: {0}")]
ParseError(#[from] std::num::ParseIntError),
#[error("Join error: {0}")]
JoinError(#[from] JoinError),
#[error("No stored tag for Minecraft Version {0}")]
NoMinecraftVersionFound(String),
}