mirror of
https://github.com/modrinth/code.git
synced 2026-09-04 22:10:15 +00:00
fix: better progress indicator
This commit is contained in:
@@ -345,7 +345,7 @@ function buildDownloadItems(): PopupNotificationProgressItem[] {
|
|||||||
iconUrl: currentLoadingBarIconUrls.value[getLoadingBarKey(bar)] ?? null,
|
iconUrl: currentLoadingBarIconUrls.value[getLoadingBarKey(bar)] ?? null,
|
||||||
progress: getLoadingProgress(bar),
|
progress: getLoadingProgress(bar),
|
||||||
waiting: !bar.total || bar.total <= 0,
|
waiting: !bar.total || bar.total <= 0,
|
||||||
progressType: 'percentage',
|
progressType: bar.bar_type?.type === 'pack_import' ? 'bytes' : 'percentage',
|
||||||
progressCurrent: bar.current,
|
progressCurrent: bar.current,
|
||||||
progressTotal: bar.total,
|
progressTotal: bar.total,
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use crate::state::{
|
|||||||
};
|
};
|
||||||
use crate::util::fetch::{
|
use crate::util::fetch::{
|
||||||
DownloadMeta, DownloadReason, FetchProgressFn, fetch,
|
DownloadMeta, DownloadReason, FetchProgressFn, fetch,
|
||||||
fetch_advanced_with_progress, sha1_file_async,
|
fetch_advanced_with_progress, sha1_file_async_with_progress,
|
||||||
};
|
};
|
||||||
use path_util::SafeRelativeUtf8UnixPathBuf;
|
use path_util::SafeRelativeUtf8UnixPathBuf;
|
||||||
use reqwest::Method;
|
use reqwest::Method;
|
||||||
@@ -196,28 +196,59 @@ pub async fn get_instance_from_pack(
|
|||||||
}),
|
}),
|
||||||
CreatePackLocation::FromFile { path } => {
|
CreatePackLocation::FromFile { path } => {
|
||||||
let mut instance = get_local_pack_instance(&path);
|
let mut instance = get_local_pack_instance(&path);
|
||||||
|
let file_size = tokio::fs::metadata(&path).await?.len();
|
||||||
|
let hashes_archive = file_size <= MAX_LOCAL_FILE_HASH_LOOKUP_SIZE;
|
||||||
|
let archive_hashing_bytes =
|
||||||
|
if hashes_archive { file_size } else { 0 };
|
||||||
|
let pack_file = CreatePackFile::Path(path.clone());
|
||||||
|
let external_file_hashing_bytes =
|
||||||
|
super::install_mrpack::get_external_file_hashing_size_from_mrpack(
|
||||||
|
&pack_file,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let inspection_total_bytes = archive_hashing_bytes
|
||||||
|
.saturating_add(external_file_hashing_bytes)
|
||||||
|
.max(1);
|
||||||
let inspection = init_loading(
|
let inspection = init_loading(
|
||||||
LoadingBarType::PackImport {
|
LoadingBarType::PackImport {
|
||||||
pack_name: instance.name.clone(),
|
pack_name: instance.name.clone(),
|
||||||
},
|
},
|
||||||
100.0,
|
inspection_total_bytes as f64,
|
||||||
"Inspecting modpack",
|
"Inspecting modpack",
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
if let Some(inspection) = &inspection {
|
let min_delta = (inspection_total_bytes / 200).max(256 * 1024);
|
||||||
let _ = emit_loading(
|
let mut reported_bytes = 0_u64;
|
||||||
inspection,
|
let mut report_progress =
|
||||||
1.0,
|
|current: u64, offset: u64, message: &str| {
|
||||||
Some("Reading local modpack"),
|
let target = offset
|
||||||
);
|
.saturating_add(current)
|
||||||
}
|
.min(inspection_total_bytes);
|
||||||
|
let increment = target.saturating_sub(reported_bytes);
|
||||||
|
if target < inspection_total_bytes && increment < min_delta
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let is_known_file = if tokio::fs::metadata(&path).await?.len()
|
if let Some(inspection) = &inspection {
|
||||||
<= MAX_LOCAL_FILE_HASH_LOOKUP_SIZE
|
let _ = emit_loading(
|
||||||
{
|
inspection,
|
||||||
|
increment as f64,
|
||||||
|
Some(message),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
reported_bytes = target;
|
||||||
|
};
|
||||||
|
|
||||||
|
let is_known_file = if hashes_archive {
|
||||||
let state = State::get().await?;
|
let state = State::get().await?;
|
||||||
let (_, hash) = sha1_file_async(&path).await?;
|
let (_, hash) =
|
||||||
|
sha1_file_async_with_progress(&path, |current, _| {
|
||||||
|
report_progress(current, 0, "Hashing local modpack");
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
match CachedEntry::get_file_many(
|
match CachedEntry::get_file_many(
|
||||||
&[&hash],
|
&[&hash],
|
||||||
Some(CacheBehaviour::StaleWhileRevalidateSkipOffline),
|
Some(CacheBehaviour::StaleWhileRevalidateSkipOffline),
|
||||||
@@ -239,19 +270,25 @@ pub async fn get_instance_from_pack(
|
|||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
if let Some(inspection) = &inspection {
|
|
||||||
let _ = emit_loading(
|
|
||||||
inspection,
|
|
||||||
39.0,
|
|
||||||
Some("Inspecting modpack files"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let external_files_in_modpack =
|
let external_files_in_modpack =
|
||||||
super::install_mrpack::get_external_files_from_mrpack(
|
super::install_mrpack::get_external_files_from_mrpack(
|
||||||
&CreatePackFile::Path(path),
|
&pack_file,
|
||||||
|
|current, _| {
|
||||||
|
report_progress(
|
||||||
|
current,
|
||||||
|
archive_hashing_bytes,
|
||||||
|
"Inspecting modpack files",
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
report_progress(
|
||||||
|
inspection_total_bytes,
|
||||||
|
0,
|
||||||
|
"Finished inspecting modpack",
|
||||||
|
);
|
||||||
|
|
||||||
instance.unknown_file = !is_known_file;
|
instance.unknown_file = !is_known_file;
|
||||||
instance.external_files_in_modpack = external_files_in_modpack;
|
instance.external_files_in_modpack = external_files_in_modpack;
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ use tokio::sync::Mutex;
|
|||||||
type ExtractProgressFn<'a> = dyn FnMut(u64) -> Pin<Box<dyn Future<Output = crate::Result<()>> + Send + 'a>>
|
type ExtractProgressFn<'a> = dyn FnMut(u64) -> Pin<Box<dyn Future<Output = crate::Result<()>> + Send + 'a>>
|
||||||
+ Send
|
+ Send
|
||||||
+ 'a;
|
+ 'a;
|
||||||
|
type HashProgressFn<'a> = dyn FnMut(u64) -> crate::Result<()> + Send + 'a;
|
||||||
const MODPACK_CONTENT_DOWNLOAD_CONCURRENCY: usize = 4;
|
const MODPACK_CONTENT_DOWNLOAD_CONCURRENCY: usize = 4;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -166,13 +167,16 @@ impl MrpackZipReader {
|
|||||||
async fn hash_entry(
|
async fn hash_entry(
|
||||||
&mut self,
|
&mut self,
|
||||||
index: usize,
|
index: usize,
|
||||||
|
progress: Option<&mut HashProgressFn<'_>>,
|
||||||
) -> crate::Result<(u64, String)> {
|
) -> crate::Result<(u64, String)> {
|
||||||
match self {
|
match self {
|
||||||
Self::Memory(reader) => {
|
Self::Memory(reader) => {
|
||||||
hash_zip_entry(reader.reader_with_entry(index).await?).await
|
hash_zip_entry(reader.reader_with_entry(index).await?, progress)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
Self::File(reader) => {
|
Self::File(reader) => {
|
||||||
hash_zip_entry(reader.reader_with_entry(index).await?).await
|
hash_zip_entry(reader.reader_with_entry(index).await?, progress)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -209,6 +213,7 @@ impl MrpackZipReader {
|
|||||||
|
|
||||||
async fn hash_zip_entry<R>(
|
async fn hash_zip_entry<R>(
|
||||||
mut reader: ZipEntryReader<'_, R, WithEntry<'_>>,
|
mut reader: ZipEntryReader<'_, R, WithEntry<'_>>,
|
||||||
|
mut progress: Option<&mut HashProgressFn<'_>>,
|
||||||
) -> crate::Result<(u64, String)>
|
) -> crate::Result<(u64, String)>
|
||||||
where
|
where
|
||||||
R: futures_lite::io::AsyncBufRead + Unpin,
|
R: futures_lite::io::AsyncBufRead + Unpin,
|
||||||
@@ -228,6 +233,9 @@ where
|
|||||||
|
|
||||||
hasher.update(&buffer[..bytes_read]);
|
hasher.update(&buffer[..bytes_read]);
|
||||||
size += bytes_read as u64;
|
size += bytes_read as u64;
|
||||||
|
if let Some(progress) = progress.as_mut() {
|
||||||
|
progress(bytes_read as u64)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if reader.compute_hash() != expected_crc32 {
|
if reader.compute_hash() != expected_crc32 {
|
||||||
@@ -239,6 +247,7 @@ where
|
|||||||
|
|
||||||
pub(crate) async fn get_external_files_from_mrpack(
|
pub(crate) async fn get_external_files_from_mrpack(
|
||||||
file: &CreatePackFile,
|
file: &CreatePackFile,
|
||||||
|
mut progress: impl FnMut(u64, u64) -> crate::Result<()> + Send,
|
||||||
) -> crate::Result<Vec<String>> {
|
) -> crate::Result<Vec<String>> {
|
||||||
let mut zip_reader = MrpackZipReader::new(file).await?;
|
let mut zip_reader = MrpackZipReader::new(file).await?;
|
||||||
let Some(manifest_idx) =
|
let Some(manifest_idx) =
|
||||||
@@ -271,21 +280,35 @@ pub(crate) async fn get_external_files_from_mrpack(
|
|||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(index, entry)| {
|
.filter_map(|(index, entry)| {
|
||||||
let path = entry.filename().as_str().ok()?;
|
let path = entry.filename().as_str().ok()?;
|
||||||
let relative_path = path
|
let relative_path = external_override_relative_path(path)?;
|
||||||
.strip_prefix("overrides/")
|
|
||||||
.or_else(|| path.strip_prefix("client-overrides/"))?;
|
|
||||||
if path.ends_with('/')
|
|
||||||
|| ProjectType::get_from_parent_folder(relative_path).is_none()
|
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let file_name = relative_path.rsplit('/').next()?.to_string();
|
let file_name = relative_path.rsplit('/').next()?.to_string();
|
||||||
Some((index, file_name))
|
Some((index, file_name, entry.uncompressed_size()))
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
for (index, file_name) in override_entries {
|
let total_bytes = override_entries
|
||||||
let (_, hash) = zip_reader.hash_entry(index).await?;
|
.iter()
|
||||||
|
.map(|(_, _, size)| size)
|
||||||
|
.sum::<u64>();
|
||||||
|
let min_delta = (total_bytes / 200).max(256 * 1024);
|
||||||
|
let mut current_bytes = 0_u64;
|
||||||
|
let mut last_reported_bytes = 0_u64;
|
||||||
|
let mut report_progress = |bytes_read: u64| {
|
||||||
|
current_bytes = current_bytes.saturating_add(bytes_read);
|
||||||
|
if current_bytes >= total_bytes
|
||||||
|
|| current_bytes.saturating_sub(last_reported_bytes) < min_delta
|
||||||
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
last_reported_bytes = current_bytes;
|
||||||
|
progress(current_bytes.min(total_bytes), total_bytes)
|
||||||
|
};
|
||||||
|
|
||||||
|
for (index, file_name, _) in override_entries {
|
||||||
|
let (_, hash) = zip_reader
|
||||||
|
.hash_entry(index, Some(&mut report_progress))
|
||||||
|
.await?;
|
||||||
candidates.push((file_name, hash));
|
candidates.push((file_name, hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,6 +350,31 @@ pub(crate) async fn get_external_files_from_mrpack(
|
|||||||
Ok(external_files)
|
Ok(external_files)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn get_external_file_hashing_size_from_mrpack(
|
||||||
|
file: &CreatePackFile,
|
||||||
|
) -> crate::Result<u64> {
|
||||||
|
let zip_reader = MrpackZipReader::new(file).await?;
|
||||||
|
Ok(zip_reader
|
||||||
|
.file()
|
||||||
|
.entries()
|
||||||
|
.iter()
|
||||||
|
.filter_map(|entry| {
|
||||||
|
let path = entry.filename().as_str().ok()?;
|
||||||
|
external_override_relative_path(path)
|
||||||
|
.map(|_| entry.uncompressed_size())
|
||||||
|
})
|
||||||
|
.sum())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn external_override_relative_path(path: &str) -> Option<&str> {
|
||||||
|
let relative_path = path
|
||||||
|
.strip_prefix("overrides/")
|
||||||
|
.or_else(|| path.strip_prefix("client-overrides/"))?;
|
||||||
|
(!path.ends_with('/')
|
||||||
|
&& ProjectType::get_from_parent_folder(relative_path).is_some())
|
||||||
|
.then_some(relative_path)
|
||||||
|
}
|
||||||
|
|
||||||
async fn extract_zip_entry<R>(
|
async fn extract_zip_entry<R>(
|
||||||
mut reader: ZipEntryReader<'_, R, WithEntry<'_>>,
|
mut reader: ZipEntryReader<'_, R, WithEntry<'_>>,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
@@ -527,7 +575,7 @@ pub(crate) async fn install_zipped_mrpack_files_with_reporter(
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for index in override_entries {
|
for index in override_entries {
|
||||||
let (_, hash) = zip_reader.hash_entry(index).await?;
|
let (_, hash) = zip_reader.hash_entry(index, None).await?;
|
||||||
file_hashes.push(hash);
|
file_hashes.push(hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ pub async fn init_loading_unsafe(
|
|||||||
message: title.to_string(),
|
message: title.to_string(),
|
||||||
total,
|
total,
|
||||||
current: 0.0,
|
current: 0.0,
|
||||||
last_sent: 0.0,
|
last_sent: -1.0,
|
||||||
bar_type,
|
bar_type,
|
||||||
#[cfg(feature = "cli")]
|
#[cfg(feature = "cli")]
|
||||||
cli_progress_bar: {
|
cli_progress_bar: {
|
||||||
|
|||||||
@@ -954,12 +954,24 @@ pub async fn sha1_async(bytes: Bytes) -> crate::Result<String> {
|
|||||||
|
|
||||||
pub async fn sha1_file_async(
|
pub async fn sha1_file_async(
|
||||||
path: impl AsRef<Path>,
|
path: impl AsRef<Path>,
|
||||||
|
) -> crate::Result<(u64, String)> {
|
||||||
|
sha1_file_async_with_progress(path, |_, _| Ok(())).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn sha1_file_async_with_progress(
|
||||||
|
path: impl AsRef<Path>,
|
||||||
|
mut progress: impl FnMut(u64, u64) -> crate::Result<()>,
|
||||||
) -> crate::Result<(u64, String)> {
|
) -> crate::Result<(u64, String)> {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
// Local files can be multi-gigabyte .mrpacks, so hash them without materializing bytes.
|
// Local files can be multi-gigabyte .mrpacks, so hash them without materializing bytes.
|
||||||
let mut file = File::open(path)
|
let mut file = File::open(path)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| IOError::with_path(e, path))?;
|
.map_err(|e| IOError::with_path(e, path))?;
|
||||||
|
let total = file
|
||||||
|
.metadata()
|
||||||
|
.await
|
||||||
|
.map_err(|e| IOError::with_path(e, path))?
|
||||||
|
.len();
|
||||||
let mut hasher = sha1_smol::Sha1::new();
|
let mut hasher = sha1_smol::Sha1::new();
|
||||||
let mut size = 0;
|
let mut size = 0;
|
||||||
let mut buffer = vec![0; 262144];
|
let mut buffer = vec![0; 262144];
|
||||||
@@ -975,6 +987,7 @@ pub async fn sha1_file_async(
|
|||||||
|
|
||||||
hasher.update(&buffer[..bytes_read]);
|
hasher.update(&buffer[..bytes_read]);
|
||||||
size += bytes_read as u64;
|
size += bytes_read as u64;
|
||||||
|
progress(size, total)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((size, hasher.digest().to_string()))
|
Ok((size, hasher.digest().to_string()))
|
||||||
|
|||||||
Reference in New Issue
Block a user