Skip to content

Commit

Permalink
Various final backend fixes (#117)
Browse files Browse the repository at this point in the history
* Various final backend fixes

* Add FS watching

* run lint

* Autodetect installed jars
  • Loading branch information
Geometrically authored May 16, 2023
1 parent 5cb54b4 commit 3fa0e99
Show file tree
Hide file tree
Showing 26 changed files with 941 additions and 529 deletions.
378 changes: 327 additions & 51 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions theseus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,40 @@ sha1 = { version = "0.6.1", features = ["std"]}
sha2 = "0.9.9"
url = "2.2"
uuid = { version = "1.1", features = ["serde", "v4"] }
zip = "0.5"
zip = "0.6.5"
async_zip = { version = "0.0.13", features = ["full"] }
tempfile = "3.5.0"

chrono = { version = "0.4.19", features = ["serde"] }
daedalus = { version = "0.1.20" }
dirs = "4.0"
daedalus = { version = "0.1.21" }
dirs = "5.0.1"

regex = "1.5"
sys-info = "0.9.0"
thiserror = "1.0"
tracing = "0.1.37"
tracing-subscriber = "0.2"
tracing-error = "0.1"
tracing-subscriber = "0.3.17"
tracing-error = "0.2.0"

paste = { version = "1.0"}

tauri = { version = "1.2", optional = true}
indicatif = { version = "0.17.3", optional = true }

async-tungstenite = { version = "0.20.0", features = ["tokio-runtime", "tokio-native-tls"] }
async-tungstenite = { version = "0.22.1", features = ["tokio-runtime", "tokio-native-tls"] }
futures = "0.3"
reqwest = { version = "0.11", features = ["json", "stream"] }
tokio = { version = "1", features = ["full"] }
tokio-stream = { version = "0.1", features = ["fs"] }

notify = { version = "5.1.0", default-features = false }
notify-debouncer-mini = { version = "0.2.1", default-features = false }

lazy_static = "1.4.0"
dunce = "1.0.3"

[target.'cfg(windows)'.dependencies]
winreg = "0.11.0"
winreg = "0.50.0"

[features]
tauri = ["dep:tauri"]
Expand Down
Binary file added theseus/library/JavaInfo.class
Binary file not shown.
22 changes: 22 additions & 0 deletions theseus/library/JavaInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public final class JavaInfo {
private static final String[] CHECKED_PROPERTIES = new String[] {
"os.arch",
"java.version"
};

public static void main(String[] args) {
int returnCode = 0;

for (String key : CHECKED_PROPERTIES) {
String property = System.getProperty(key);

if (property != null) {
System.out.println(key + "=" + property);
} else {
returnCode = 1;
}
}

System.exit(returnCode);
}
}
98 changes: 97 additions & 1 deletion theseus/src/api/jre.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
//! Authentication flow interface
use reqwest::Method;
use serde::Deserialize;
use std::path::PathBuf;

use crate::event::emit::{emit_loading, init_loading};
use crate::util::fetch::{fetch_advanced, fetch_json};
use crate::{
launcher::download,
prelude::Profile,
state::JavaGlobals,
util::jre::{self, extract_java_majorminor_version, JavaVersion},
State,
LoadingBarType, State,
};

pub const JAVA_8_KEY: &str = "JAVA_8";
Expand Down Expand Up @@ -133,6 +137,87 @@ pub async fn find_java17_jres() -> crate::Result<Vec<JavaVersion>> {
.collect())
}

pub async fn auto_install_java(java_version: u32) -> crate::Result<PathBuf> {
let state = State::get().await?;

let loading_bar = init_loading(
LoadingBarType::JavaDownload {
version: java_version,
},
100.0,
"Downloading java version",
)
.await?;

#[derive(Deserialize)]
struct Package {
pub download_url: String,
pub name: PathBuf,
}

emit_loading(&loading_bar, 0.0, Some("Fetching java version")).await?;
let packages = fetch_json::<Vec<Package>>(
Method::GET,
&format!(
"https://api.azul.com/metadata/v1/zulu/packages?arch={}&java_version={}&os={}&archive_type=zip&javafx_bundled=false&java_package_type=jre&page_size=1",
std::env::consts::ARCH, java_version, std::env::consts::OS
),
None,
None,
&state.fetch_semaphore,
).await?;
emit_loading(&loading_bar, 10.0, Some("Downloading java version")).await?;

if let Some(download) = packages.first() {
let file = fetch_advanced(
Method::GET,
&download.download_url,
None,
None,
None,
Some((&loading_bar, 80.0)),
&state.fetch_semaphore,
)
.await?;

let path = state.directories.java_versions_dir();

if path.exists() {
tokio::fs::remove_dir_all(&path).await?;
}

let mut archive = zip::ZipArchive::new(std::io::Cursor::new(file))
.map_err(|_| {
crate::Error::from(crate::ErrorKind::InputError(
"Failed to read java zip".to_string(),
))
})?;

emit_loading(&loading_bar, 0.0, Some("Extracting java")).await?;
archive.extract(&path).map_err(|_| {
crate::Error::from(crate::ErrorKind::InputError(
"Failed to extract java zip".to_string(),
))
})?;
emit_loading(&loading_bar, 100.0, Some("Done extracting java")).await?;
Ok(path
.join(
download
.name
.file_stem()
.unwrap_or_default()
.to_string_lossy()
.to_string(),
)
.join(format!("zulu-{}.jre/Contents/Home/bin/java", java_version)))
} else {
Err(crate::ErrorKind::LauncherError(format!(
"No Java Version found for Java version {}, OS {}, and Architecture {}",
java_version, std::env::consts::OS, std::env::consts::ARCH,
)).into())
}
}

// Get all JREs that exist on the system
pub async fn get_all_jre() -> crate::Result<Vec<JavaVersion>> {
Ok(jre::get_all_jre().await?)
Expand All @@ -148,3 +233,14 @@ pub async fn validate_globals() -> crate::Result<bool> {
pub async fn check_jre(path: PathBuf) -> crate::Result<Option<JavaVersion>> {
Ok(jre::check_java_at_filepath(&path).await)
}

// Gets maximum memory in KiB.
pub async fn get_max_memory() -> crate::Result<u64> {
Ok(sys_info::mem_info()
.map_err(|_| {
crate::Error::from(crate::ErrorKind::LauncherError(
"Unable to get computer memory".to_string(),
))
})?
.total)
}
4 changes: 1 addition & 3 deletions theseus/src/api/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use crate::event::emit::{
};
use crate::event::{LoadingBarId, LoadingBarType};
use crate::state::{
LinkedData, ModrinthProject, ModrinthVersion, Profile, ProfileInstallStage,
SideType,
LinkedData, ModrinthProject, ModrinthVersion, ProfileInstallStage, SideType,
};
use crate::util::fetch::{
fetch, fetch_advanced, fetch_json, fetch_mirrors, write, write_cached_icon,
Expand Down Expand Up @@ -478,7 +477,6 @@ async fn install_pack(
if let Some(profile_val) =
crate::api::profile::get(&profile, None).await?
{
Profile::sync_projects_task(profile.clone());
crate::launcher::install_minecraft(
&profile_val,
Some(loading_bar),
Expand Down
Loading

0 comments on commit 3fa0e99

Please sign in to comment.