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
@@ -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