feat(modpacks): check discovered pack updates
This commit is contained in:
1 parent
f049904466
commit
695ba5a758
25 files changed
+191
-38
No files matched your search
@@ -115,6 +115,7 @@ impl App {
|
||||
|
||||
let manager = InstanceManager::new(instances_dir, meta_dir);
|
||||
let instances = manager.load_all();
|
||||
instances::spawn_modpack_update_checks(&instances);
|
||||
let instances_state = instances::State::with_instances(instances);
|
||||
|
||||
let mut mods_state = widgets::content::list::ContentListState::default();
|
||||
|
||||
@@ -40,6 +40,7 @@ impl App {
|
||||
// every content type has its own pending queue because they each
|
||||
// get scanned/loaded on separate tokio tasks
|
||||
self.drain_pending_instances();
|
||||
self.instances_state.drain_modpack_updates();
|
||||
self.drain_pending_last_played();
|
||||
let mut local_streamed = false;
|
||||
let mut content_changed = false;
|
||||
@@ -686,6 +687,7 @@ impl App {
|
||||
fn drain_pending_instances(&mut self) {
|
||||
if let Ok(mut pending) = PENDING_INSTANCES.lock() {
|
||||
for config in pending.drain(..) {
|
||||
widgets::instances::spawn_modpack_update_check(&config);
|
||||
self.instances_state.add_instance(config);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +122,7 @@ impl UiHarness {
|
||||
jvm_args: Vec::new(),
|
||||
resolution: None,
|
||||
config_sync_profile: None,
|
||||
modpack_source: None,
|
||||
};
|
||||
std::fs::create_dir_all(self.instance_path(name)).expect("instance directory");
|
||||
self.app
|
||||
|
||||
@@ -42,6 +42,7 @@ fn instance(name: &str, version: &str) -> InstanceConfig {
|
||||
jvm_args: Vec::new(),
|
||||
resolution: None,
|
||||
config_sync_profile: None,
|
||||
modpack_source: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,9 @@ use ratatui::{
|
||||
|
||||
use super::{
|
||||
ContentListState, WatcherEventHandling, available_description_width, description_text_width,
|
||||
diff_directory, diff_event_paths, ellipsize, has_newer_compatible_version,
|
||||
load_provider_metadata, read_dir_stems, right_aligned_footer_spans, square_icon_columns,
|
||||
title_suffix_spans, watcher_event_handling, world_descriptions, world_game_mode_color,
|
||||
diff_directory, diff_event_paths, ellipsize, load_provider_metadata, read_dir_stems,
|
||||
right_aligned_footer_spans, square_icon_columns, title_suffix_spans, watcher_event_handling,
|
||||
world_descriptions, world_game_mode_color,
|
||||
};
|
||||
|
||||
fn entry(name: &str) -> ContentEntry {
|
||||
@@ -695,24 +695,3 @@ async fn provider_metadata_loads_from_cache_without_network() {
|
||||
assert_eq!(description, "Cached description");
|
||||
assert!(!update_available);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_check_requires_the_installed_version_behind_a_newer_result() {
|
||||
let version = |id: &str| crate::net::modrinth::VersionInfo {
|
||||
id: id.to_owned(),
|
||||
project_id: "project".to_owned(),
|
||||
name: id.to_owned(),
|
||||
version_number: id.to_owned(),
|
||||
game_versions: vec!["1.21.1".to_owned()],
|
||||
loaders: vec!["fabric".to_owned()],
|
||||
version_type: crate::net::modrinth::VersionType::Release,
|
||||
dependencies: Vec::new(),
|
||||
date_published: String::new(),
|
||||
files: Vec::new(),
|
||||
};
|
||||
let versions = vec![version("new"), version("installed")];
|
||||
|
||||
assert!(has_newer_compatible_version(&versions, "installed"));
|
||||
assert!(!has_newer_compatible_version(&versions, "new"));
|
||||
assert!(!has_newer_compatible_version(&versions, "missing"));
|
||||
}
|
||||
@@ -46,6 +46,7 @@ fn synthetic_instance(name: &str) -> InstanceConfig {
|
||||
jvm_args: vec![],
|
||||
resolution: None,
|
||||
config_sync_profile: None,
|
||||
modpack_source: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,3 +79,24 @@ fn instances_list_renders_empty() {
|
||||
|
||||
insta::assert_snapshot!(terminal.backend());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn managed_modpack_update_is_badged_in_the_instance_list() {
|
||||
let mut state = State::with_instances(vec![synthetic_instance("Managed Pack")]);
|
||||
state.modpack_updates.insert("Managed Pack".to_owned());
|
||||
let backend = TestBackend::new(40, 5);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
|
||||
terminal
|
||||
.draw(|frame| render(frame, frame.area(), FocusedArea::Instances, &mut state))
|
||||
.unwrap();
|
||||
|
||||
let text = terminal
|
||||
.backend()
|
||||
.buffer()
|
||||
.content()
|
||||
.iter()
|
||||
.map(|cell| cell.symbol())
|
||||
.collect::<String>();
|
||||
assert!(text.contains("Managed Pack Update"));
|
||||
}
|
||||
@@ -154,6 +154,7 @@ fn import_modpack_renders_confirm_step() {
|
||||
override_count: 3,
|
||||
format: PackFormat::Mrpack,
|
||||
archive_path: PathBuf::from("/tmp/synthetic.mrpack"),
|
||||
source: None,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -197,6 +198,7 @@ fn import_modpack_renders_confirm_step_without_loader_version() {
|
||||
override_count: 12,
|
||||
format: PackFormat::Mmc,
|
||||
archive_path: PathBuf::from("/tmp/vanilla.zip"),
|
||||
source: None,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ fn summary() -> ImportSummary {
|
||||
override_count: 1,
|
||||
format: PackFormat::Mrpack,
|
||||
archive_path: PathBuf::from("test.mrpack"),
|
||||
source: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1399,7 +1399,10 @@ async fn load_provider_metadata(
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(versions) => has_newer_compatible_version(&versions, &installed.version_id),
|
||||
Ok(versions) => crate::instance::content::provider::has_newer_compatible_version(
|
||||
&versions,
|
||||
&installed.version_id,
|
||||
),
|
||||
Err(error) => {
|
||||
tracing::debug!("Could not check updates for project '{project_id}': {error}");
|
||||
false
|
||||
@@ -1428,16 +1431,6 @@ async fn load_provider_metadata(
|
||||
Ok((bytes, project.description, update_available))
|
||||
}
|
||||
|
||||
fn has_newer_compatible_version(
|
||||
versions: &[crate::net::modrinth::VersionInfo],
|
||||
installed_version_id: &str,
|
||||
) -> bool {
|
||||
versions
|
||||
.iter()
|
||||
.position(|version| version.id == installed_version_id)
|
||||
.is_some_and(|position| position > 0)
|
||||
}
|
||||
|
||||
pub fn handle_key_no_toggle(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
|
||||
handle_key_inner(key_event, state, false)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ use ratatui::{
|
||||
text::{Line, Span, Text},
|
||||
widgets::{Block, Borders, Scrollbar, ScrollbarOrientation, ScrollbarState},
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use std::sync::{Arc, LazyLock, Mutex};
|
||||
use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView};
|
||||
|
||||
use crate::instance::models::InstanceConfig;
|
||||
@@ -20,6 +22,55 @@ use crate::tui::app::FocusedArea;
|
||||
|
||||
use super::{WidgetKey, search::SearchState, styled_title};
|
||||
|
||||
static PENDING_MODPACK_UPDATES: LazyLock<
|
||||
Mutex<Vec<(String, crate::instance::ProviderProject, bool)>>,
|
||||
> = LazyLock::new(|| Mutex::new(Vec::new()));
|
||||
static MODPACK_UPDATE_SLOTS: LazyLock<Arc<tokio::sync::Semaphore>> =
|
||||
LazyLock::new(|| Arc::new(tokio::sync::Semaphore::new(4)));
|
||||
|
||||
pub fn spawn_modpack_update_checks(instances: &[InstanceConfig]) {
|
||||
for instance in instances {
|
||||
spawn_modpack_update_check(instance);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_modpack_update_check(instance: &InstanceConfig) {
|
||||
let Some(source) = instance.modpack_source.clone() else {
|
||||
return;
|
||||
};
|
||||
let instance = instance.clone();
|
||||
tokio::spawn(async move {
|
||||
let Ok(_permit) = MODPACK_UPDATE_SLOTS.clone().acquire_owned().await else {
|
||||
return;
|
||||
};
|
||||
let registry = crate::instance::content::provider::ProviderRegistry::configured(
|
||||
crate::net::HttpClient::new(),
|
||||
);
|
||||
let Some(provider) = registry.get(&source.provider) else {
|
||||
return;
|
||||
};
|
||||
let Ok(versions) = provider
|
||||
.compatible_versions(
|
||||
&source.project_id,
|
||||
crate::instance::ContentKind::Mod,
|
||||
&instance.game_version,
|
||||
instance.loader,
|
||||
)
|
||||
.await
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let update = crate::instance::content::provider::has_newer_compatible_version(
|
||||
&versions,
|
||||
&source.version_id,
|
||||
);
|
||||
if let Ok(mut pending) = PENDING_MODPACK_UPDATES.lock() {
|
||||
pending.push((instance.name, source, update));
|
||||
crate::feedback::request_redraw();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct State {
|
||||
pub instances: Vec<InstanceConfig>,
|
||||
@@ -29,6 +80,7 @@ pub struct State {
|
||||
pub show_import_popup: bool,
|
||||
pub search: SearchState,
|
||||
pub renaming: Option<String>,
|
||||
pub(crate) modpack_updates: HashSet<String>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
@@ -42,6 +94,7 @@ impl State {
|
||||
show_import_popup: false,
|
||||
search: SearchState::default(),
|
||||
renaming: None,
|
||||
modpack_updates: HashSet::new(),
|
||||
};
|
||||
if count > 0 {
|
||||
s.list_state.selected = Some(0);
|
||||
@@ -122,10 +175,33 @@ impl State {
|
||||
self.instances.retain(|i| i.name != name);
|
||||
let after = self.instances.len();
|
||||
if after < before {
|
||||
self.modpack_updates.remove(name);
|
||||
self.update_scrollbar();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn drain_modpack_updates(&mut self) {
|
||||
let Ok(mut pending) = PENDING_MODPACK_UPDATES.lock() else {
|
||||
return;
|
||||
};
|
||||
for (name, source, available) in pending.drain(..) {
|
||||
if self
|
||||
.instances
|
||||
.iter()
|
||||
.find(|instance| instance.name == name)
|
||||
.and_then(|instance| instance.modpack_source.as_ref())
|
||||
!= Some(&source)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if available {
|
||||
self.modpack_updates.insert(name);
|
||||
} else {
|
||||
self.modpack_updates.remove(&name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_instance(&mut self, instance: InstanceConfig) {
|
||||
self.instances.push(instance);
|
||||
self.list_state.selected = self.filtered_indices().len().checked_sub(1);
|
||||
@@ -275,6 +351,18 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
|
||||
} else {
|
||||
let mut spans = vec![selector.clone()];
|
||||
spans.extend(state.search.highlight_spans(&instance.name, name_style));
|
||||
if state.modpack_updates.contains(&instance.name) {
|
||||
spans.extend([
|
||||
Span::raw(" "),
|
||||
Span::styled(
|
||||
"Update",
|
||||
Style::default()
|
||||
.fg(theme.background())
|
||||
.bg(theme.warning())
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
]);
|
||||
}
|
||||
Line::from(spans)
|
||||
};
|
||||
|
||||
|
||||
@@ -667,7 +667,12 @@ fn start_discovered_download(request: crate::tui::widgets::content::discovery::I
|
||||
let tmp_dir =
|
||||
crate::storage::MetadataPaths::new(crate::config::SETTINGS.paths.resolve_meta_dir())
|
||||
.temporary();
|
||||
let result = async {
|
||||
let result: Result<crate::instance::import::ImportSummary, crate::net::NetError> = async {
|
||||
let source = crate::instance::ProviderProject {
|
||||
provider: request.provider.clone(),
|
||||
project_id: request.project_id.clone(),
|
||||
version_id: request.version.id.clone(),
|
||||
};
|
||||
tokio::fs::create_dir_all(&tmp_dir).await?;
|
||||
let provider = registry.get(&request.provider).ok_or_else(|| {
|
||||
crate::net::NetError::Parse(format!(
|
||||
@@ -682,7 +687,10 @@ fn start_discovered_download(request: crate::tui::widgets::content::discovery::I
|
||||
crate::net::modrinth::DownloadOutcome::Downloaded(path)
|
||||
| crate::net::modrinth::DownloadOutcome::SkippedExisting(path) => path,
|
||||
};
|
||||
crate::instance::import::build_summary(&path).map_err(crate::net::NetError::Parse)
|
||||
let mut summary = crate::instance::import::build_summary(&path)
|
||||
.map_err(crate::net::NetError::Parse)?;
|
||||
summary.source = Some(source);
|
||||
Ok(summary)
|
||||
}
|
||||
.await;
|
||||
match result {
|
||||
|
||||
Reference in new issue
Block a user