fix: sort provider version lists newest first
Preserve Minecraft release and snapshot chronology, compare loader versions numerically with provider-specific prerelease/build handling, and order Modrinth and CurseForge versions by publication date, including cached lists. Add regression coverage and live API ordering checks. Fixes #45
This commit is contained in:
1 parent
874c38d7f8
commit
25a4b90f1f
23 files changed
+664
-95
No files matched your search
@@ -892,6 +892,11 @@ pub struct InstallCompletion {
|
||||
pub orphaned_dependencies: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
pub struct ProjectVersions {
|
||||
pub versions: Vec<VersionInfo>,
|
||||
pub minecraft_versions: Vec<String>,
|
||||
}
|
||||
|
||||
pub enum DiscoveryActionResult {
|
||||
ProjectPage {
|
||||
request_id: u64,
|
||||
@@ -907,14 +912,14 @@ pub enum DiscoveryActionResult {
|
||||
Versions {
|
||||
request_id: u64,
|
||||
project_id: String,
|
||||
result: Result<Vec<VersionInfo>, String>,
|
||||
result: Result<ProjectVersions, String>,
|
||||
},
|
||||
/// The filtered fetch found nothing, so the task retried unfiltered.
|
||||
/// Drain opens the Minecraft version picker instead of a dead end.
|
||||
VersionsUnfiltered {
|
||||
request_id: u64,
|
||||
project_id: String,
|
||||
result: Result<Vec<VersionInfo>, String>,
|
||||
result: Result<ProjectVersions, String>,
|
||||
},
|
||||
Dependencies {
|
||||
request_id: u64,
|
||||
@@ -2564,9 +2569,9 @@ impl DiscoveryState {
|
||||
};
|
||||
popup.loading = false;
|
||||
match result {
|
||||
Ok(versions) => {
|
||||
popup.minecraft_versions = minecraft_versions(&versions);
|
||||
popup.versions = versions;
|
||||
Ok(result) => {
|
||||
popup.minecraft_versions = result.minecraft_versions;
|
||||
popup.versions = result.versions;
|
||||
popup.selected = 0;
|
||||
popup.error = None;
|
||||
}
|
||||
@@ -2585,11 +2590,11 @@ impl DiscoveryState {
|
||||
};
|
||||
popup.loading = false;
|
||||
match result {
|
||||
Ok(versions) if !versions.is_empty() => {
|
||||
Ok(result) if !result.versions.is_empty() => {
|
||||
popup.all_game_versions = true;
|
||||
popup.selecting_minecraft_version = true;
|
||||
popup.minecraft_versions = minecraft_versions(&versions);
|
||||
popup.versions = versions;
|
||||
popup.minecraft_versions = result.minecraft_versions;
|
||||
popup.versions = result.versions;
|
||||
popup.selected = 0;
|
||||
popup.error = None;
|
||||
}
|
||||
@@ -3039,9 +3044,60 @@ fn minecraft_versions(versions: &[VersionInfo]) -> Vec<String> {
|
||||
.filter(|game_version| seen.insert((*game_version).clone()))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
if numeric_game_versions(&minecraft_versions) {
|
||||
minecraft_versions.sort_by(|a, b| crate::net::versions::compare_versions(b, a));
|
||||
}
|
||||
minecraft_versions
|
||||
.sort_by(|a, b| crate::tui::widgets::popups::compare_game_versions(b.as_str(), a.as_str()));
|
||||
minecraft_versions
|
||||
}
|
||||
|
||||
fn numeric_game_versions(versions: &[String]) -> bool {
|
||||
versions.iter().all(|version| {
|
||||
version
|
||||
.split('.')
|
||||
.all(|part| !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit()))
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn prepare_project_versions(
|
||||
result: Result<Vec<VersionInfo>, String>,
|
||||
choose_minecraft: bool,
|
||||
) -> Result<ProjectVersions, String> {
|
||||
let mut versions = result?;
|
||||
crate::instance::content::provider::sort_versions_newest_first(&mut versions);
|
||||
let mut minecraft_versions = if choose_minecraft {
|
||||
minecraft_versions(&versions)
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
// Numeric releases can be compared directly. Weekly snapshots, prereleases
|
||||
// and special releases require the official chronology, not a string sort.
|
||||
if !numeric_game_versions(&minecraft_versions) {
|
||||
let manifest = crate::net::mojang::fetch_version_manifest(&crate::net::HttpClient::new())
|
||||
.await
|
||||
.map_err(|error| format!("Failed to load Minecraft version order: {error}"))?;
|
||||
minecraft_versions = minecraft_versions_from_manifest(&minecraft_versions, &manifest);
|
||||
}
|
||||
Ok(ProjectVersions {
|
||||
versions,
|
||||
minecraft_versions,
|
||||
})
|
||||
}
|
||||
|
||||
fn minecraft_versions_from_manifest(
|
||||
supported: &[String],
|
||||
manifest: &crate::net::mojang::VersionManifest,
|
||||
) -> Vec<String> {
|
||||
let order = manifest
|
||||
.versions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, version)| (version.id.as_str(), index))
|
||||
.collect::<std::collections::HashMap<_, _>>();
|
||||
let mut versions = supported.to_vec();
|
||||
// Keep provider-only IDs (e.g. experimental snapshots) in their original
|
||||
// order after catalogued versions instead of silently dropping choices.
|
||||
versions.sort_by_key(|version| order.get(version.as_str()).copied().unwrap_or(usize::MAX));
|
||||
versions
|
||||
}
|
||||
|
||||
pub(crate) fn page_key_direction(key_event: &KeyEvent) -> Option<bool> {
|
||||
|
||||
@@ -669,6 +669,8 @@ fn spawn_versions(request: crate::tui::widgets::content::discovery::VersionsRequ
|
||||
_ => crate::net::modrinth::fetch_versions(&client, &request.project_id).await,
|
||||
}
|
||||
.map_err(|error| error.to_string());
|
||||
let result =
|
||||
crate::tui::widgets::content::discovery::prepare_project_versions(result, true).await;
|
||||
crate::tui::widgets::content::DiscoveryState::push_action_result(
|
||||
&request.pending,
|
||||
crate::tui::widgets::content::discovery::DiscoveryActionResult::Versions {
|
||||
|
||||
@@ -20,24 +20,6 @@ use ratatui::{layout::Rect, text::Span};
|
||||
|
||||
use crate::config::settings::ShortcutHintScope;
|
||||
|
||||
pub(crate) fn compare_game_versions(a: &str, b: &str) -> std::cmp::Ordering {
|
||||
let parse_parts = |version: &str| {
|
||||
version
|
||||
.split('.')
|
||||
.map(|part| part.parse::<u64>().unwrap_or(0))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let a_parts = parse_parts(a);
|
||||
let b_parts = parse_parts(b);
|
||||
for (a, b) in a_parts.iter().zip(&b_parts) {
|
||||
match a.cmp(b) {
|
||||
std::cmp::Ordering::Equal => {}
|
||||
ordering => return ordering,
|
||||
}
|
||||
}
|
||||
a_parts.len().cmp(&b_parts.len())
|
||||
}
|
||||
|
||||
pub fn word_wrap_size(text: &str, max_inner_width: usize) -> (usize, usize) {
|
||||
if text.is_empty() || max_inner_width == 0 {
|
||||
return (0, 1);
|
||||
|
||||
@@ -8,12 +8,12 @@ use crate::instance::{
|
||||
|
||||
pub async fn game_versions(loader: ModLoader) -> Result<Vec<GameVersion>, String> {
|
||||
let client = crate::net::HttpClient::new();
|
||||
let mut versions = get_installer(loader)
|
||||
// Mojang, Fabric and Quilt supply chronological lists, including snapshots
|
||||
// whose IDs cannot be compared as numeric Minecraft versions.
|
||||
get_installer(loader)
|
||||
.get_game_versions(&client)
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
versions.sort_by(|a, b| super::compare_game_versions(&b.id, &a.id));
|
||||
Ok(versions)
|
||||
.map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
pub async fn loader_versions(loader: ModLoader, game_version: &str) -> Result<Vec<String>, String> {
|
||||
|
||||
Reference in new issue
Block a user