Event handling (#75)

* working on amcros

* fleshed out draft

* added feature support

* finished loading

* Fixed issue with multiple data types in macro

* Working, and added more loading uses

* added window scopes

* clippy, fmt

* working other variants

* fmt; clippy

* prettier

* refactored emissions to use increment

* fixed deadlock

* doc changes

* clippy, prettier

* uuid change

* restructured events to util

* loading restructure

* merge fixes

* comments mistake

* better cfg tauri feature structuring

* added extra fields to some loading enum variants

* removed Option<>

* added pack + version labels

* doc change
This commit is contained in:
Wyatt Verchere
2023-04-16 10:12:37 -07:00
committed by GitHub
parent f8173d3b78
commit b120b5cfa8
22 changed files with 3519 additions and 102 deletions

View File

@@ -1,6 +1,11 @@
//! Downloader for Minecraft data
use crate::{
event::{
emit::{emit_loading, init_loading, loading_try_for_each_concurrent},
LoadingBarId, LoadingBarType,
},
process::Profile,
state::State,
util::{fetch::*, platform::OsExt},
};
@@ -19,14 +24,25 @@ use tokio::{fs, sync::OnceCell};
pub async fn download_minecraft(
st: &State,
version: &GameVersionInfo,
profile: &Profile,
) -> crate::Result<()> {
log::info!("Downloading Minecraft version {}", version.id);
let assets_index = download_assets_index(st, version).await?;
let loading_bar = init_loading(
LoadingBarType::MinecraftDownload {
// If we are downloading minecraft for a profile, provide its name and uuid
profile_name: profile.metadata.name.clone(),
profile_uuid: profile.uuid,
},
100.0,
"Downloading Minecraft...",
)
.await?;
tokio::try_join! {
download_client(st, version),
download_assets(st, version.assets == "legacy", &assets_index),
download_libraries(st, version.libraries.as_slice(), &version.id)
download_client(st, version, Some(&loading_bar)),
download_assets(st, version.assets == "legacy", &assets_index, Some(&loading_bar)),
download_libraries(st, version.libraries.as_slice(), &version.id, Some(&loading_bar))
}?;
log::info!("Done downloading Minecraft!");
@@ -74,6 +90,7 @@ pub async fn download_version_info(
pub async fn download_client(
st: &State,
version_info: &GameVersionInfo,
loading_bar: Option<&LoadingBarId>,
) -> crate::Result<()> {
let version = &version_info.id;
log::debug!("Locating client for version {version}");
@@ -101,6 +118,9 @@ pub async fn download_client(
write(&path, &bytes, &st.io_semaphore).await?;
log::info!("Fetched client version {version}");
}
if let Some(loading_bar) = loading_bar {
emit_loading(loading_bar, 20.0, None).await?;
}
log::debug!("Client loaded for version {version}!");
Ok(())
@@ -138,11 +158,20 @@ pub async fn download_assets(
st: &State,
with_legacy: bool,
index: &AssetsIndex,
loading_bar: Option<&LoadingBarId>,
) -> crate::Result<()> {
log::debug!("Loading assets");
stream::iter(index.objects.iter())
.map(Ok::<(&String, &Asset), crate::Error>)
.try_for_each_concurrent(None, |(name, asset)| async move {
let num_futs = index.objects.len();
let assets = stream::iter(index.objects.iter())
.map(Ok::<(&String, &Asset), crate::Error>);
loading_try_for_each_concurrent(assets,
None,
loading_bar,
50.0,
num_futs,
None,
|(name, asset)| async move {
let hash = &asset.hash;
let resource_path = st.directories.object_dir(hash);
let url = format!(
@@ -190,6 +219,7 @@ pub async fn download_libraries(
st: &State,
libraries: &[Library],
version: &str,
loading_bar: Option<&LoadingBarId>,
) -> crate::Result<()> {
log::debug!("Loading libraries");
@@ -197,10 +227,10 @@ pub async fn download_libraries(
fs::create_dir_all(st.directories.libraries_dir()),
fs::create_dir_all(st.directories.version_natives_dir(version))
}?;
let num_files = libraries.len();
loading_try_for_each_concurrent(
stream::iter(libraries.iter())
.map(Ok::<&Library, crate::Error>)
.try_for_each_concurrent(None, |library| async move {
.map(Ok::<&Library, crate::Error>), None, loading_bar,50.0,num_files, None,|library| async move {
if let Some(rules) = &library.rules {
if !rules.iter().all(super::parse_rule) {
return Ok(());

View File

@@ -2,6 +2,7 @@
use crate::{process, state as st};
use daedalus as d;
use dunce::canonicalize;
use st::Profile;
use std::{path::Path, process::Stdio};
use tokio::process::{Child, Command};
@@ -57,6 +58,7 @@ pub async fn launch_minecraft(
memory: &st::MemorySettings,
resolution: &st::WindowSize,
credentials: &auth::Credentials,
profile: &Profile, // optional ref to Profile for event tracking
) -> crate::Result<Child> {
let state = st::State::get().await?;
let instance_path = &canonicalize(instance_path)?;
@@ -88,7 +90,7 @@ pub async fn launch_minecraft(
.version_dir(&version_jar)
.join(format!("{version_jar}.jar"));
download::download_minecraft(&state, &version_info).await?;
download::download_minecraft(&state, &version_info, profile).await?;
st::State::sync().await?;
if let Some(processors) = &version_info.processors {