From 32a58688941385fe5c623b736f6a20b3825a0023 Mon Sep 17 00:00:00 2001 From: objz Date: Sat, 25 Jul 2026 12:44:52 +0200 Subject: [PATCH] fix(content): stabilize indexing progress --- src/cli/content.rs | 1 + src/instance/config_sync.rs | 1 + src/instance/content/manifest.rs | 9 +-- src/instance/content/mods.rs | 4 + src/instance/content/reconcile.rs | 108 +++++++++++++++++++++---- src/instance/content/resource_packs.rs | 1 + src/instance/content/shaders.rs | 1 + src/instance/content/worlds.rs | 1 + src/tui/progress.rs | 31 ++++--- src/tui/widgets/content/discovery.rs | 1 + src/tui/widgets/content/list.rs | 32 ++++++-- 11 files changed, 151 insertions(+), 39 deletions(-) diff --git a/src/cli/content.rs b/src/cli/content.rs index d5f9c42..c7c6d1f 100644 --- a/src/cli/content.rs +++ b/src/cli/content.rs @@ -133,6 +133,7 @@ mod tests { description: String::new(), enabled: true, icon_bytes: None, + provider_icon: false, path: PathBuf::from(file_stem), icon_lines: None, } diff --git a/src/instance/config_sync.rs b/src/instance/config_sync.rs index 5ab4a09..10ec7f3 100644 --- a/src/instance/config_sync.rs +++ b/src/instance/config_sync.rs @@ -221,6 +221,7 @@ fn acquire_lock(profile_dir: &Path) -> Result { .read(true) .write(true) .create(true) + .truncate(false) .open(path)?; file.lock()?; Ok(ConfigSyncLock { file }) diff --git a/src/instance/content/manifest.rs b/src/instance/content/manifest.rs index b823805..d99fd17 100644 --- a/src/instance/content/manifest.rs +++ b/src/instance/content/manifest.rs @@ -36,9 +36,10 @@ pub struct ProviderProject { pub version_id: String, } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(tag = "status", rename_all = "snake_case")] pub enum Resolution { + #[default] Pending, Resolved { project: ProviderProject, @@ -52,12 +53,6 @@ pub enum Resolution { }, } -impl Default for Resolution { - fn default() -> Self { - Self::Pending - } -} - #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct FileFingerprint { pub size: u64, diff --git a/src/instance/content/mods.rs b/src/instance/content/mods.rs index 53e78d5..c9628a6 100644 --- a/src/instance/content/mods.rs +++ b/src/instance/content/mods.rs @@ -34,6 +34,7 @@ pub struct ContentEntry { pub description: String, pub enabled: bool, pub icon_bytes: Option>, + pub provider_icon: bool, pub path: PathBuf, pub icon_lines: Option>>, } @@ -121,6 +122,7 @@ pub fn scan_one_mod(path: &Path, file_stem: &str, enabled: bool) -> ContentEntry description, enabled, icon_bytes, + provider_icon: false, path: path.to_path_buf(), icon_lines, } @@ -722,6 +724,7 @@ mod tests { description: String::new(), enabled: false, icon_bytes: None, + provider_icon: false, path: disabled_path.clone(), icon_lines: None, }; @@ -998,6 +1001,7 @@ description = "A neoforge mod" description: String::new(), enabled: true, icon_bytes: None, + provider_icon: false, path: enabled_path.clone(), icon_lines: None, }; diff --git a/src/instance/content/reconcile.rs b/src/instance/content/reconcile.rs index 2687895..7555116 100644 --- a/src/instance/content/reconcile.rs +++ b/src/instance/content/reconcile.rs @@ -6,8 +6,8 @@ use std::time::{SystemTime, UNIX_EPOCH}; use crate::content_provider::{FingerprintQuery, ProviderRegistry}; use crate::instance::InstanceConfig; use crate::instance::content::manifest::{ - ContentFileRecord, ContentKind, ContentManifest, ProviderProject, Resolution, fingerprint, - fingerprint_metadata, + ContentFileRecord, ContentKind, ContentManifest, FileFingerprint, ProviderProject, Resolution, + fingerprint, fingerprint_metadata, }; use crate::storage::{InstancePaths, MetadataPaths}; use crate::tui::progress::{ProgressTask, ProgressTaskHandle}; @@ -170,7 +170,7 @@ async fn reconcile(job: ReconcileJob, task: &ProgressTask) -> ReconcileResult { }) .await; - let result = match inventory { + match inventory { Ok(Ok((mut inventory, manifest_path))) => { let registry = ProviderRegistry::modrinth(client); task.set_action(format!("Identifying content for {instance_name}")); @@ -193,7 +193,7 @@ async fn reconcile(job: ReconcileJob, task: &ProgressTask) -> ReconcileResult { ®istry, &inventory.manifest, &MetadataPaths::new(&meta_dir), - &task, + task, ) .await; match save_reconciled_manifest( @@ -242,8 +242,7 @@ async fn reconcile(job: ReconcileJob, task: &ProgressTask) -> ReconcileResult { icons: HashMap::new(), error: Some(error.to_string()), }, - }; - result + } } fn save_reconciled_manifest( @@ -329,21 +328,34 @@ fn reconcile_inventory( .and_then(|name| name.to_str()) .is_some_and(|name| name.ends_with(".disabled")); let metadata = std::fs::metadata(&path)?; - let modified_ns = metadata - .modified() - .unwrap_or(SystemTime::UNIX_EPOCH) - .duration_since(UNIX_EPOCH) - .unwrap_or_default() - .as_nanos(); + let is_directory = metadata.is_dir(); + let metadata_fingerprint = if is_directory { + directory_fingerprint_metadata(&path)? + } else { + fingerprint_metadata(&path)? + }; + let modified_ns = metadata_fingerprint.modified_ns; let existing = previous.record(&relative_path); let unchanged = existing.is_some_and(|record| { - record.fingerprint.size == metadata.len() + record.fingerprint.size == metadata_fingerprint.size && record.fingerprint.modified_ns == modified_ns }); - let oversized = max_fingerprint_size_mib > 0 + let oversized = !is_directory + && max_fingerprint_size_mib > 0 && metadata.len() > max_fingerprint_size_mib.saturating_mul(1024 * 1024); let mut record = if unchanged { existing.cloned().unwrap() + } else if is_directory { + ContentFileRecord { + relative_path: relative_path.clone(), + kind, + enabled, + fingerprint: metadata_fingerprint, + resolution: Resolution::Unmatched { + checked_at: now, + providers: Vec::new(), + }, + } } else if oversized { tracing::debug!( "Skipping automatic provider matching for {} ({} bytes exceeds {} MiB limit)", @@ -379,7 +391,8 @@ fn reconcile_inventory( providers: Vec::new(), }; } - let should_query = !oversized + let should_query = !is_directory + && !oversized && match &record.resolution { Resolution::Pending => true, Resolution::Unmatched { checked_at, .. } => { @@ -635,7 +648,7 @@ fn content_files(minecraft_dir: &Path) -> std::io::Result std::io::Result bool { +fn supported_content_path(kind: ContentKind, path: &Path) -> bool { + if path.is_dir() { + return matches!(kind, ContentKind::ResourcePack | ContentKind::Shader); + } + if !path.is_file() { + return false; + } let Some(name) = path.file_name().and_then(|name| name.to_str()) else { return false; }; @@ -656,6 +675,37 @@ fn supported_file(kind: ContentKind, path: &Path) -> bool { } } +fn directory_fingerprint_metadata(path: &Path) -> Result { + fn accumulate(path: &Path, size: &mut u64, modified_ns: &mut u128) -> std::io::Result<()> { + for entry in std::fs::read_dir(path)? { + let entry = entry?; + let metadata = entry.metadata()?; + let modified = metadata + .modified() + .unwrap_or(SystemTime::UNIX_EPOCH) + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_nanos(); + *modified_ns = (*modified_ns).max(modified); + if metadata.is_dir() { + accumulate(&entry.path(), size, modified_ns)?; + } else if metadata.is_file() { + *size = size.saturating_add(metadata.len()); + } + } + Ok(()) + } + + let mut size = 0; + let mut modified_ns = 0; + accumulate(path, &mut size, &mut modified_ns)?; + Ok(FileFingerprint { + size, + modified_ns, + hashes: Default::default(), + }) +} + #[cfg(test)] mod tests { use super::*; @@ -751,4 +801,28 @@ mod tests { assert!(second.queries.is_empty()); assert_eq!(second.manifest.files[0].fingerprint, fingerprint); } + + #[test] + fn directory_packs_are_indexed_without_provider_queries() { + let temp = tempfile::tempdir().unwrap(); + let minecraft = temp.path().join("minecraft"); + let pack = minecraft.join("resourcepacks/example"); + std::fs::create_dir_all(&pack).unwrap(); + std::fs::write(pack.join("pack.mcmeta"), b"{}").unwrap(); + + let inventory = reconcile_inventory( + &temp.path().join("manifest.json"), + &minecraft, + 24, + 512, + &NoopProgress, + ) + .unwrap(); + + assert!(inventory.queries.is_empty()); + assert_eq!( + inventory.manifest.files[0].relative_path, + PathBuf::from("resourcepacks/example") + ); + } } diff --git a/src/instance/content/resource_packs.rs b/src/instance/content/resource_packs.rs index b7498b6..db890cf 100644 --- a/src/instance/content/resource_packs.rs +++ b/src/instance/content/resource_packs.rs @@ -73,6 +73,7 @@ pub fn scan_one_resource_pack(path: &Path, file_stem: &str, enabled: bool) -> Co description, enabled, icon_bytes, + provider_icon: false, path: path.to_path_buf(), icon_lines, } diff --git a/src/instance/content/shaders.rs b/src/instance/content/shaders.rs index 2c9d935..a8d9c18 100644 --- a/src/instance/content/shaders.rs +++ b/src/instance/content/shaders.rs @@ -30,6 +30,7 @@ pub fn scan_one_shader(path: &Path, file_stem: &str, enabled: bool) -> ContentEn description, enabled, icon_bytes, + provider_icon: false, path: path.to_path_buf(), icon_lines, } diff --git a/src/instance/content/worlds.rs b/src/instance/content/worlds.rs index ebf8bf5..4e169f4 100644 --- a/src/instance/content/worlds.rs +++ b/src/instance/content/worlds.rs @@ -26,6 +26,7 @@ pub fn scan_one_world(path: &Path, file_stem: &str, enabled: bool) -> ContentEnt description, enabled, icon_bytes, + provider_icon: false, path: path.to_path_buf(), icon_lines, } diff --git a/src/tui/progress.rs b/src/tui/progress.rs index 2af3a99..c0ac663 100644 --- a/src/tui/progress.rs +++ b/src/tui/progress.rs @@ -199,11 +199,8 @@ pub fn set_sub_action(text: impl Into) { pub fn clear() { match PROGRESS.lock() { Ok(mut state) => { - state.current_action = None; - state.progress = None; - state.sub_action = None; - state.tasks.clear(); state.legacy_active = false; + refresh_visible(&mut state); crate::tui::request_redraw(); } Err(e) => { @@ -212,6 +209,12 @@ pub fn clear() { } } +pub fn is_active() -> bool { + PROGRESS + .lock() + .is_ok_and(|state| state.current_action.is_some()) +} + #[cfg(test)] mod tests { use super::*; @@ -256,10 +259,20 @@ mod tests { assert!(!is_active()); clear(); } -} -pub fn is_active() -> bool { - PROGRESS - .lock() - .is_ok_and(|state| state.current_action.is_some()) + #[test] + fn clearing_legacy_progress_keeps_owned_tasks() { + let _guard = TEST_LOCK.lock().unwrap(); + clear(); + let task = ProgressTask::start("indexing"); + set_action("legacy download"); + clear(); + + let state = PROGRESS.lock().unwrap(); + assert_eq!(state.current_action.as_deref(), Some("indexing")); + drop(state); + + task.finish(); + clear(); + } } diff --git a/src/tui/widgets/content/discovery.rs b/src/tui/widgets/content/discovery.rs index 76b0951..10df398 100644 --- a/src/tui/widgets/content/discovery.rs +++ b/src/tui/widgets/content/discovery.rs @@ -600,6 +600,7 @@ pub(crate) fn project_entry( description: project.description, enabled: true, icon_bytes: project.icon_bytes, + provider_icon: true, path: PathBuf::from(project.id), icon_lines: Some(crate::instance::content::mods::fallback_icon()), } diff --git a/src/tui/widgets/content/list.rs b/src/tui/widgets/content/list.rs index 1852092..addd6b4 100644 --- a/src/tui/widgets/content/list.rs +++ b/src/tui/widgets/content/list.rs @@ -199,6 +199,11 @@ impl ContentListState { .filter(|record| record.kind == kind) else { if entry.provider_project.take().is_some() { + if entry.provider_icon { + entry.icon_bytes = None; + entry.icon_lines = Some(crate::instance::content::mods::fallback_icon()); + entry.provider_icon = false; + } changed = true; } continue; @@ -208,6 +213,11 @@ impl ContentListState { _ => None, }; if entry.provider_project != project { + if entry.provider_icon { + entry.icon_bytes = None; + entry.icon_lines = Some(crate::instance::content::mods::fallback_icon()); + entry.provider_icon = false; + } entry.provider_project = project.clone(); changed = true; } @@ -219,6 +229,7 @@ impl ContentListState { { entry.icon_bytes = Some(bytes.clone()); entry.icon_lines = Some(lines); + entry.provider_icon = true; changed = true; } } @@ -408,7 +419,7 @@ impl ContentListState { .iter_mut() .find(|existing| existing.file_stem == entry.file_stem) { - if entry.icon_bytes.is_none() { + if entry.icon_bytes.is_none() && !existing.provider_icon { entry.icon_bytes = existing.icon_bytes.take(); entry.icon_lines = existing.icon_lines.take(); } @@ -443,6 +454,7 @@ impl ContentListState { .find(|entry| entry.file_stem == file_stem && entry.path == path) { entry.icon_bytes = Some(bytes); + entry.provider_icon = true; self.requested_images.remove(&file_stem); self.images_dirty = true; } @@ -1570,7 +1582,11 @@ fn available_description_width( has_icon: bool, ) -> usize { let selector_and_scrollbar = 2; - let icon_and_gap = has_icon.then_some(rendered_icon_columns + 1).unwrap_or(0); + let icon_and_gap = if has_icon { + rendered_icon_columns + 1 + } else { + 0 + }; row_width.saturating_sub(selector_and_scrollbar + icon_and_gap) } @@ -1592,9 +1608,11 @@ fn right_aligned_footer_spans( footer_label: &str, footer_style: Style, ) -> Vec> { - let description_width = has_description - .then(|| Span::raw(description).width()) - .unwrap_or(0); + let description_width = if has_description { + Span::raw(description).width() + } else { + 0 + }; let footer_width = Span::raw(footer_label).width(); let padding = available_width.saturating_sub(description_width + footer_width); vec![ @@ -1734,7 +1752,8 @@ fn diff_event_paths( } else { let removed_stems = known_map .iter() - .filter_map(|(stem, (known_path, _))| (known_path == path).then(|| stem.clone())) + .filter(|(_, (known_path, _))| known_path == path) + .map(|(stem, _)| stem.clone()) .collect::>(); for stem in removed_stems { known_map.remove(&stem); @@ -1863,6 +1882,7 @@ mod tests { description: String::new(), enabled: true, icon_bytes: None, + provider_icon: false, path: PathBuf::from(name.to_lowercase()), icon_lines: None, }