feat: add download clients for Mojang API and mod loaders

This commit is contained in:
objz committed 2025-07-19 15:14:07 +02:00
1 parent b10c4c4973
commit 252fe8e44a
6 files changed
+1284

No files matched your search

+172
View File
@@ -0,0 +1,172 @@
use std::collections::HashMap;
use std::path::Path;
use serde::Deserialize;
use crate::net::{download_file, HttpClient, NetError};
use crate::tui::progress::set_action;
const FORGE_PROMOTIONS_URL: &str =
"https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json";
const FORGE_MAVEN_BASE: &str = "https://maven.minecraftforge.net/net/minecraftforge/forge";
#[derive(Debug, Deserialize)]
struct ForgePromotions {
promos: HashMap<String, String>,
}
/// Fetch available Forge loader versions for a given Minecraft game version.
///
/// Queries the Forge promotions JSON and returns unique Forge version strings
/// matching the requested game version (e.g. `"47.2.20"` for game version `"1.20.1"`).
pub async fn fetch_forge_versions(
client: &HttpClient,
game_version: &str,
) -> Result<Vec<String>, 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 promotions: ForgePromotions = match response.json().await {
Ok(p) => p,
Err(e) => {
tracing::error!("Failed to parse Forge promotions JSON: {}", e);
return Err(NetError::Http(e));
}
};
let prefix = format!("{}-", game_version);
let mut versions: Vec<String> = promotions
.promos
.iter()
.filter(|(key, _)| key.starts_with(&prefix))
.map(|(_, value)| value.clone())
.collect();
versions.sort();
versions.dedup();
Ok(versions)
}
/// 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.
pub async fn download_forge_installer(
client: &HttpClient,
game_version: &str,
forge_version: &str,
dest: &Path,
) -> Result<(), NetError> {
let url = format!(
"{}/{game_version}-{forge_version}/forge-{game_version}-{forge_version}-installer.jar",
FORGE_MAVEN_BASE,
game_version = game_version,
forge_version = forge_version,
);
set_action(format!(
"Downloading Forge {}-{}...",
game_version, forge_version
));
match download_file(client, &url, dest, |downloaded, total| {
crate::tui::progress::set_progress(downloaded, total);
})
.await
{
Ok(()) => Ok(()),
Err(e) => {
tracing::error!("Failed to download Forge installer from {}: {}", url, e);
Err(e)
}
}
}
/// Run the Forge installer JAR to install Forge into the instance directory.
///
/// Requires a Java runtime. The installer is invoked with `--installClient` pointing
/// at the instance's `.minecraft` subdirectory.
pub async fn run_forge_installer(
installer_path: &Path,
instance_dir: &Path,
java_path: &str,
) -> Result<(), NetError> {
use tokio::process::Command;
set_action("Running Forge installer...");
let output = match Command::new(java_path)
.arg("-jar")
.arg(installer_path)
.arg("--installClient")
.arg(instance_dir.join(".minecraft"))
.output()
.await
{
Ok(o) => o,
Err(e) => {
tracing::error!("Failed to launch Forge installer: {}", e);
return Err(NetError::Io(e));
}
};
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
tracing::error!(
"Forge installer failed (exit {:?}): {} {}",
output.status.code(),
stderr,
stdout
);
return Err(NetError::Parse(format!(
"Forge installer exited with {:?}",
output.status.code()
)));
}
Ok(())
}
/// Detect the path to a Java runtime.
///
/// Checks `JAVA_HOME` first, then falls back to `"java"` assuming it is on `PATH`.
pub fn detect_java_path() -> String {
if let Ok(java_home) = std::env::var("JAVA_HOME") {
let path = std::path::Path::new(&java_home).join("bin").join("java");
if path.exists() {
return path.to_string_lossy().to_string();
}
}
"java".to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::net::HttpClient;
#[tokio::test]
async fn test_fetch_versions() {
let client = HttpClient::new();
match fetch_forge_versions(&client, "1.20.1").await {
Ok(versions) => {
assert!(!versions.is_empty(), "Should have Forge versions for 1.20.1");
}
Err(e) => assert!(false, "fetch_forge_versions failed: {}", e),
}
}
}