Skip to content

Commit

Permalink
Implement loading (#104)
Browse files Browse the repository at this point in the history
* Implement loading

* LoadingBar

* Run linter

* Update App.vue

* Loading bar all the things

* Update SplashScreen.vue

* Update SplashScreen.vue

* Update App.vue

* initial revert

* Update Instance.vue

* revert css

* Fix instance

* More reverting

* Run lint

* Finalize changes

* Revert "Merge branch 'master' into loading"

This reverts commit 3014e76, reversing
changes made to b780e85.

* Fix loading issues

* fix lint

* Revert "Revert "Merge branch 'master' into loading""

This reverts commit 971ef84.

---------

Co-authored-by: Jai A <jaiagr+gpg@pm.me>
  • Loading branch information
CodexAdrian and Geometrically authored May 10, 2023
1 parent 9be0d16 commit 71cf2c5
Show file tree
Hide file tree
Showing 21 changed files with 464 additions and 189 deletions.
32 changes: 16 additions & 16 deletions theseus/src/api/profile_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn profile_create(
)
.into());
}

if ReadDirStream::new(fs::read_dir(&path).await?)
.next()
.await
Expand All @@ -77,33 +77,34 @@ pub async fn profile_create(
} else {
fs::create_dir_all(&path).await?;
}

info!(
"Creating profile at path {}",
&canonicalize(&path)?.display()
);

let loader = modloader;
let loader = if loader != ModLoader::Vanilla {
let version = loader_version.unwrap_or_else(|| "latest".to_string());

let filter = |it: &LoaderVersion| match version.as_str() {
"latest" => true,
"stable" => it.stable,
id => it.id == *id || format!("{}-{}", game_version, id) == it.id,
};

let loader_data = match loader {
ModLoader::Forge => &metadata.forge,
ModLoader::Fabric => &metadata.fabric,
ModLoader::Quilt => &metadata.quilt,
_ => {
return Err(ProfileCreationError::NoManifest(
loader.to_string(),
)
.into())
}
};

let loaders = &loader_data
.game_versions
.iter()
Expand All @@ -120,7 +121,7 @@ pub async fn profile_create(
)
})?
.loaders;

let loader_version = loaders
.iter()
.cloned()
Expand All @@ -139,12 +140,12 @@ pub async fn profile_create(
loader.to_string(),
)
})?;

Some((loader_version, loader))
} else {
None
};

// Fully canonicalize now that its created for storing purposes
let path = canonicalize(&path)?;
let mut profile =
Expand All @@ -164,9 +165,9 @@ pub async fn profile_create(
profile.metadata.loader = loader;
profile.metadata.loader_version = Some(loader_version);
}

profile.metadata.linked_data = linked_data;

// Attempts to find optimal JRE for the profile from the JavaGlobals
// Finds optimal key, and see if key has been set in JavaGlobals
let settings = state.settings.read().await;
Expand All @@ -179,27 +180,26 @@ pub async fn profile_create(
} else {
emit_warning(&format!("Could not detect optimal JRE: {optimal_version_key}, falling back to system default.")).await?;
}

emit_profile(
uuid,
path.clone(),
&profile.metadata.name,
ProfilePayloadType::Created,
)
.await?;

{
let mut profiles = state.profiles.write().await;
profiles.insert(profile.clone()).await?;
}

if !skip_install_profile.unwrap_or(false) {
crate::launcher::install_minecraft(&profile, None).await?;
}
State::sync().await?;

Ok(path)

}).await
}

Expand Down
63 changes: 35 additions & 28 deletions theseus/src/event/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub async fn init_loading(
message: title.to_string(),
total,
current: 0.0,
last_sent: 0.0,
bar_type,
#[cfg(feature = "cli")]
cli_progress_bar: {
Expand Down Expand Up @@ -115,6 +116,7 @@ pub async fn edit_loading(
bar.total = total;
bar.message = title.to_string();
bar.current = 0.0;
bar.last_sent = 0.0;
#[cfg(feature = "cli")]
{
bar.cli_progress_bar.reset(); // indicatif::ProgressBar::new(CLI_PROGRESS_BAR_TOTAL as u64);
Expand Down Expand Up @@ -150,42 +152,47 @@ pub async fn emit_loading(
// Tick up loading bar
loading_bar.current += increment_frac;
let display_frac = loading_bar.current / loading_bar.total;
let display_frac = if display_frac >= 1.0 {
let opt_display_frac = if display_frac >= 1.0 {
None // by convention, when its done, we submit None
// any further updates will be ignored (also sending None)
} else {
Some(display_frac)
};

// Emit event to indicatif progress bar
#[cfg(feature = "cli")]
{
loading_bar.cli_progress_bar.set_message(
message
.map(|x| x.to_string())
.unwrap_or(loading_bar.message.clone()),
);
loading_bar.cli_progress_bar.set_position(
((loading_bar.current / loading_bar.total)
* CLI_PROGRESS_BAR_TOTAL as f64)
.round() as u64,
);
if f64::abs(display_frac - loading_bar.last_sent) > 0.005 {
// Emit event to indicatif progress bar
#[cfg(feature = "cli")]
{
loading_bar.cli_progress_bar.set_message(
message
.map(|x| x.to_string())
.unwrap_or(loading_bar.message.clone()),
);
loading_bar.cli_progress_bar.set_position(
(display_frac * CLI_PROGRESS_BAR_TOTAL as f64).round() as u64,
);
}

// Emit event to tauri
#[cfg(feature = "tauri")]
event_state
.app
.emit_all(
"loading",
LoadingPayload {
fraction: opt_display_frac,
message: message
.unwrap_or(&loading_bar.message)
.to_string(),
event: loading_bar.bar_type.clone(),
loader_uuid: loading_bar.loading_bar_uuid,
},
)
.map_err(EventError::from)?;

loading_bar.last_sent = display_frac;
}

// Emit event to tauri
#[cfg(feature = "tauri")]
event_state
.app
.emit_all(
"loading",
LoadingPayload {
fraction: display_frac,
message: message.unwrap_or(&loading_bar.message).to_string(),
event: loading_bar.bar_type.clone(),
loader_uuid: loading_bar.loading_bar_uuid,
},
)
.map_err(EventError::from)?;
Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions theseus/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub struct LoadingBar {
pub message: String,
pub total: f64,
pub current: f64,
#[serde(skip)]
pub last_sent: f64,
pub bar_type: LoadingBarType,
#[cfg(feature = "cli")]
#[serde(skip)]
Expand Down
63 changes: 30 additions & 33 deletions theseus/src/launcher/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,22 @@ pub async fn download_minecraft(
let assets_index =
download_assets_index(st, version, Some(loading_bar)).await?;

let amount = if version
.processors
.as_ref()
.map(|x| !x.is_empty())
.unwrap_or(false)
{
25.0
} else {
40.0
};

tokio::try_join! {
// Total loading sums to 80
// Total loading sums to 90/60
download_client(st, version, Some(loading_bar)), // 10
download_assets(st, version.assets == "legacy", &assets_index, Some(loading_bar)), // 35
download_libraries(st, version.libraries.as_slice(), &version.id, Some(loading_bar)) // 35
download_assets(st, version.assets == "legacy", &assets_index, Some(loading_bar), amount), // 40
download_libraries(st, version.libraries.as_slice(), &version.id, Some(loading_bar), amount) // 40
}?;

tracing::info!("Done downloading Minecraft!");
Expand Down Expand Up @@ -83,21 +94,7 @@ pub async fn download_version_info(
}?;

if let Some(loading_bar) = loading_bar {
emit_loading(
loading_bar,
if res
.processors
.as_ref()
.map(|x| !x.is_empty())
.unwrap_or(false)
{
5.0
} else {
15.0
},
None,
)
.await?;
emit_loading(loading_bar, 5.0, None).await?;
}

tracing::debug!("Loaded version info for Minecraft {version_id}");
Expand Down Expand Up @@ -140,7 +137,7 @@ pub async fn download_client(
tracing::trace!("Fetched client version {version}");
}
if let Some(loading_bar) = loading_bar {
emit_loading(loading_bar, 10.0, None).await?;
emit_loading(loading_bar, 9.0, None).await?;
}

tracing::debug!("Client loaded for version {version}!");
Expand Down Expand Up @@ -186,17 +183,18 @@ pub async fn download_assets(
with_legacy: bool,
index: &AssetsIndex,
loading_bar: Option<&LoadingBarId>,
loading_amount: f64,
) -> crate::Result<()> {
Box::pin(async move {
tracing::debug!("Loading assets");
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,
35.0,
loading_amount,
num_futs,
None,
|(name, asset)| async move {
Expand All @@ -206,7 +204,7 @@ pub async fn download_assets(
"https://resources.download.minecraft.net/{sub_hash}/{hash}",
sub_hash = &hash[..2]
);

let fetch_cell = OnceCell::<bytes::Bytes>::new();
tokio::try_join! {
async {
Expand All @@ -233,14 +231,12 @@ pub async fn download_assets(
Ok::<_, crate::Error>(())
},
}?;

tracing::trace!("Loaded asset with hash {hash}");
Ok(())
}).await?;

tracing::debug!("Done loading assets!");
Ok(())

}).await
}

Expand All @@ -250,6 +246,7 @@ pub async fn download_libraries(
libraries: &[Library],
version: &str,
loading_bar: Option<&LoadingBarId>,
loading_amount: f64,
) -> crate::Result<()> {
Box::pin(async move {
tracing::debug!("Loading libraries");
Expand All @@ -261,7 +258,7 @@ pub async fn download_libraries(
let num_files = libraries.len();
loading_try_for_each_concurrent(
stream::iter(libraries.iter())
.map(Ok::<&Library, crate::Error>), None, loading_bar,35.0,num_files, None,|library| async move {
.map(Ok::<&Library, crate::Error>), None, loading_bar,loading_amount,num_files, None,|library| async move {
if let Some(rules) = &library.rules {
if !rules.iter().all(super::parse_rule) {
return Ok(());
Expand All @@ -271,7 +268,7 @@ pub async fn download_libraries(
async {
let artifact_path = d::get_path_from_artifact(&library.name)?;
let path = st.directories.libraries_dir().join(&artifact_path);

match library.downloads {
_ if path.exists() => Ok(()),
Some(d::minecraft::LibraryDownloads {
Expand All @@ -292,7 +289,7 @@ pub async fn download_libraries(
.unwrap_or("https://libraries.minecraft.net"),
&artifact_path
].concat();

let bytes = fetch(&url, None, &st.fetch_semaphore).await?;
write(&path, &bytes, &st.io_semaphore).await?;
tracing::trace!("Fetched library {}", &library.name);
Expand All @@ -318,7 +315,7 @@ pub async fn download_libraries(
"${arch}",
crate::util::platform::ARCH_WIDTH,
);

if let Some(native) = classifiers.get(&parsed_key) {
let data = fetch(&native.url, Some(&native.sha1), &st.fetch_semaphore).await?;
let reader = std::io::Cursor::new(&data);
Expand All @@ -332,18 +329,18 @@ pub async fn download_libraries(
}
}
}

Ok(())
}
}?;

tracing::debug!("Loaded library {}", library.name);
Ok(())
}
).await?;

tracing::debug!("Done loading libraries!");
Ok(())

}).await
}
Loading

0 comments on commit 71cf2c5

Please sign in to comment.