feat: fetch game versions from loader APIs
This commit is contained in:
1 parent
173250c53f
commit
5177f078a9
7 files changed
+320
-13
No files matched your search
@@ -2,6 +2,7 @@ use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::instance::loader::GameVersion;
|
||||
use crate::net::{download_file, HttpClient, NetError};
|
||||
use crate::tui::progress::set_sub_action;
|
||||
|
||||
@@ -20,6 +21,12 @@ pub struct FabricVersion {
|
||||
pub stable: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct FabricGameVersion {
|
||||
pub version: String,
|
||||
pub stable: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FabricProfile {
|
||||
@@ -36,6 +43,41 @@ pub struct FabricLibrary {
|
||||
|
||||
|
||||
|
||||
pub async fn fetch_fabric_game_versions(
|
||||
client: &HttpClient,
|
||||
) -> Result<Vec<GameVersion>, NetError> {
|
||||
let url = format!("{}/versions/game", FABRIC_META_BASE);
|
||||
let response = match client.inner().get(&url).send().await {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
tracing::error!("Fabric game versions GET {} failed: {}", url, e);
|
||||
return Err(NetError::Http(e));
|
||||
}
|
||||
};
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status().as_u16();
|
||||
tracing::error!("HTTP {} for {}", status, url);
|
||||
return Err(NetError::StatusError { status, url });
|
||||
}
|
||||
|
||||
let versions: Vec<FabricGameVersion> = match response.json().await {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to parse Fabric game versions: {}", e);
|
||||
return Err(NetError::Http(e));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(versions
|
||||
.into_iter()
|
||||
.map(|version| GameVersion {
|
||||
id: version.version,
|
||||
stable: version.stable,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn fetch_fabric_versions(
|
||||
client: &HttpClient,
|
||||
game_version: &str,
|
||||
@@ -181,6 +223,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_fetch_game_versions() {
|
||||
let client = HttpClient::new();
|
||||
match fetch_fabric_game_versions(&client).await {
|
||||
Ok(versions) => {
|
||||
assert!(!versions.is_empty(), "Should have Fabric game versions");
|
||||
assert!(versions.iter().any(|version| version.id == "1.20.1"));
|
||||
}
|
||||
Err(e) => assert!(false, "fetch_fabric_game_versions failed: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_maven_coord_to_path() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::instance::loader::GameVersion;
|
||||
use crate::net::{download_file, HttpClient, NetError};
|
||||
use crate::tui::progress::set_action;
|
||||
|
||||
@@ -61,6 +62,50 @@ pub async fn fetch_forge_versions(
|
||||
Ok(versions)
|
||||
}
|
||||
|
||||
pub async fn fetch_forge_game_versions(client: &HttpClient) -> Result<Vec<GameVersion>, NetError> {
|
||||
let response = match client.inner().get(FORGE_PROMOTIONS_URL).send().await {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
tracing::error!("GET {} failed: {}", FORGE_PROMOTIONS_URL, e);
|
||||
return Err(NetError::Http(e));
|
||||
}
|
||||
};
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status().as_u16();
|
||||
tracing::error!("HTTP {} for {}", status, FORGE_PROMOTIONS_URL);
|
||||
return Err(NetError::StatusError {
|
||||
status,
|
||||
url: FORGE_PROMOTIONS_URL.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let promos: ForgePromotions = match response.json().await {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to parse Forge promotions: {}", e);
|
||||
return Err(NetError::Http(e));
|
||||
}
|
||||
};
|
||||
|
||||
let mut game_versions: Vec<String> = promos
|
||||
.promos
|
||||
.keys()
|
||||
.filter_map(|key| key.rsplit_once('-').map(|(version, _)| version.to_string()))
|
||||
.collect();
|
||||
game_versions.sort();
|
||||
game_versions.dedup();
|
||||
game_versions.reverse();
|
||||
|
||||
Ok(game_versions
|
||||
.into_iter()
|
||||
.map(|version| GameVersion {
|
||||
id: version,
|
||||
stable: true,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Download the Forge installer JAR for the given game + forge version combo.
|
||||
///
|
||||
/// The installer JAR is saved to `dest`. Progress is reported via the TUI progress system.
|
||||
@@ -158,4 +203,16 @@ mod tests {
|
||||
Err(e) => assert!(false, "fetch_forge_versions failed: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_fetch_game_versions() {
|
||||
let client = HttpClient::new();
|
||||
match fetch_forge_game_versions(&client).await {
|
||||
Ok(versions) => {
|
||||
assert!(!versions.is_empty(), "Should have Forge game versions");
|
||||
assert!(versions.iter().any(|version| version.id == "1.20.1"));
|
||||
}
|
||||
Err(e) => assert!(false, "fetch_forge_game_versions failed: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::instance::loader::GameVersion;
|
||||
use crate::net::{download_file, HttpClient, NetError};
|
||||
use crate::tui::progress::set_action;
|
||||
|
||||
@@ -84,6 +85,65 @@ pub async fn fetch_neoforge_versions(
|
||||
Ok(versions)
|
||||
}
|
||||
|
||||
pub async fn fetch_neoforge_game_versions(
|
||||
client: &HttpClient,
|
||||
) -> Result<Vec<GameVersion>, NetError> {
|
||||
let response = match client.inner().get(NEOFORGE_API_BASE).send().await {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
tracing::error!("GET {} failed: {}", NEOFORGE_API_BASE, e);
|
||||
return Err(NetError::Http(e));
|
||||
}
|
||||
};
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status().as_u16();
|
||||
tracing::error!("HTTP {} for {}", status, NEOFORGE_API_BASE);
|
||||
return Err(NetError::StatusError {
|
||||
status,
|
||||
url: NEOFORGE_API_BASE.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let maven: NeoForgeMavenVersions = match response.json().await {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to parse NeoForge versions: {}", e);
|
||||
return Err(NetError::Http(e));
|
||||
}
|
||||
};
|
||||
|
||||
let mut game_versions: Vec<String> = Vec::new();
|
||||
for version in &maven.versions {
|
||||
let parts: Vec<&str> = version.split('.').collect();
|
||||
if parts.len() >= 2 {
|
||||
match (parts[0].parse::<u32>(), parts[1].parse::<u32>()) {
|
||||
(Ok(major), Ok(minor)) => {
|
||||
let mc_version = if minor == 0 {
|
||||
format!("1.{}", major)
|
||||
} else {
|
||||
format!("1.{}.{}", major, minor)
|
||||
};
|
||||
|
||||
if !game_versions.contains(&mc_version) {
|
||||
game_versions.push(mc_version);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
game_versions.reverse();
|
||||
|
||||
Ok(game_versions
|
||||
.into_iter()
|
||||
.map(|version| GameVersion {
|
||||
id: version,
|
||||
stable: true,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn download_neoforge_installer(
|
||||
client: &HttpClient,
|
||||
neoforge_version: &str,
|
||||
@@ -175,6 +235,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_fetch_game_versions() {
|
||||
let client = HttpClient::new();
|
||||
match fetch_neoforge_game_versions(&client).await {
|
||||
Ok(versions) => {
|
||||
assert!(!versions.is_empty(), "Should have NeoForge game versions");
|
||||
assert!(versions.iter().any(|version| version.id == "1.21"));
|
||||
}
|
||||
Err(e) => assert!(false, "fetch_neoforge_game_versions failed: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_version_to_neoforge_prefix() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -2,6 +2,7 @@ use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::instance::loader::GameVersion;
|
||||
use crate::net::{download_file, HttpClient, NetError};
|
||||
use crate::tui::progress::set_sub_action;
|
||||
|
||||
@@ -17,6 +18,12 @@ pub struct QuiltVersion {
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct QuiltGameVersion {
|
||||
pub version: String,
|
||||
pub stable: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct QuiltProfile {
|
||||
@@ -33,6 +40,42 @@ pub struct QuiltLibrary {
|
||||
|
||||
|
||||
|
||||
pub async fn fetch_quilt_game_versions(
|
||||
client: &HttpClient,
|
||||
) -> Result<Vec<GameVersion>, NetError> {
|
||||
let url = format!("{}/versions/game", QUILT_META_BASE);
|
||||
|
||||
let response = match client.inner().get(&url).send().await {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
tracing::error!("Quilt game versions GET {} failed: {}", url, e);
|
||||
return Err(NetError::Http(e));
|
||||
}
|
||||
};
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status().as_u16();
|
||||
tracing::error!("HTTP {} for {}", status, url);
|
||||
return Err(NetError::StatusError { status, url });
|
||||
}
|
||||
|
||||
let versions: Vec<QuiltGameVersion> = match response.json().await {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to parse Quilt game versions: {}", e);
|
||||
return Err(NetError::Http(e));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(versions
|
||||
.into_iter()
|
||||
.map(|version| GameVersion {
|
||||
id: version.version,
|
||||
stable: version.stable,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn fetch_quilt_versions(
|
||||
client: &HttpClient,
|
||||
game_version: &str,
|
||||
@@ -171,6 +214,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_fetch_game_versions() {
|
||||
let client = HttpClient::new();
|
||||
match fetch_quilt_game_versions(&client).await {
|
||||
Ok(versions) => {
|
||||
assert!(!versions.is_empty(), "Should have Quilt game versions");
|
||||
assert!(versions.iter().any(|version| version.id == "1.20.1"));
|
||||
}
|
||||
Err(e) => assert!(false, "fetch_quilt_game_versions failed: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_maven_coord_to_path() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user