added support for old forge versions (pre-1.13)
old forge installers don't have --installClient, so we extract the universal jar and library info directly from install_profile.json inside the installer jar. also tries multiple maven URL formats since forge naming is inconsistent across versions.
This commit is contained in:
1 parent
5f3f7890aa
commit
a29c90e86b
2 files changed
+223
-36
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
// forge installation. unlike fabric/quilt, forge ships a java-based installer
|
// forge installation. modern forge runs a java installer, old forge (pre-1.13)
|
||||||
// that has to be downloaded and executed. yes, a jvm is needed just to install
|
// can't run headless so we extract the profile and libraries from the jar
|
||||||
// the thing that runs on a jvm. the installer jar gets cleaned up afterward.
|
// directly. the installer jar gets cleaned up either way.
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
@@ -43,27 +43,43 @@ impl ModLoaderInstaller for ForgeInstaller {
|
|||||||
forge_api::download_forge_installer(client, game_version, loader_version, &installer_jar)
|
forge_api::download_forge_installer(client, game_version, loader_version, &installer_jar)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// use configured java or try to find one on PATH
|
let profile_filename = format!("forge-{game_version}-{loader_version}.json");
|
||||||
let java_path = crate::config::SETTINGS
|
|
||||||
.paths
|
if forge_api::has_legacy_install_profile(&installer_jar) {
|
||||||
.effective_java_path()
|
// old forge: no --installClient support, extract directly from jar
|
||||||
.map(str::to_owned)
|
if let Err(e) = forge_api::install_forge_from_profile(
|
||||||
.unwrap_or_else(crate::net::detect_java_path);
|
client,
|
||||||
if let Err(e) =
|
&installer_jar,
|
||||||
forge_api::run_forge_installer(&installer_jar, instance_dir, &java_path).await
|
meta_dir,
|
||||||
{
|
&profile_filename,
|
||||||
// still clean up even if installation failed
|
)
|
||||||
let _ = tokio::fs::remove_file(&installer_jar).await;
|
.await
|
||||||
return Err(e);
|
{
|
||||||
|
let _ = tokio::fs::remove_file(&installer_jar).await;
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// modern forge: run the java installer
|
||||||
|
let java_path = crate::config::SETTINGS
|
||||||
|
.paths
|
||||||
|
.effective_java_path()
|
||||||
|
.map(str::to_owned)
|
||||||
|
.unwrap_or_else(crate::net::detect_java_path);
|
||||||
|
if let Err(e) =
|
||||||
|
forge_api::run_forge_installer(&installer_jar, instance_dir, &java_path).await
|
||||||
|
{
|
||||||
|
let _ = tokio::fs::remove_file(&installer_jar).await;
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// extract the profile from what the installer just wrote to disk
|
||||||
|
save_forge_profile(instance_dir, meta_dir, game_version, loader_version)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = tokio::fs::remove_file(&installer_jar).await {
|
if let Err(e) = tokio::fs::remove_file(&installer_jar).await {
|
||||||
tracing::warn!("Failed to remove Forge installer JAR: {}", e);
|
tracing::warn!("Failed to remove Forge installer JAR: {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract the profile from what the installer just wrote to disk
|
|
||||||
save_forge_profile(instance_dir, meta_dir, game_version, loader_version)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+189
-18
@@ -1,6 +1,6 @@
|
|||||||
// forge mod loader: version discovery via promotions API, then download
|
// forge mod loader: version discovery via promotions API, download and
|
||||||
// and run the installer jar. forge doesn't have a nice metadata API like
|
// installation. modern forge runs a java installer, old forge (pre-1.13ish)
|
||||||
// fabric/quilt, so it shells out to java to run their installer.
|
// doesn't support headless install so we extract directly from the jar.
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@@ -9,7 +9,7 @@ use serde::Deserialize;
|
|||||||
|
|
||||||
use crate::instance::loader::GameVersion;
|
use crate::instance::loader::GameVersion;
|
||||||
use crate::net::{HttpClient, NetError, download_file};
|
use crate::net::{HttpClient, NetError, download_file};
|
||||||
use crate::tui::progress::set_action;
|
use crate::tui::progress::{set_action, set_sub_action};
|
||||||
|
|
||||||
const FORGE_PROMOTIONS_URL: &str =
|
const FORGE_PROMOTIONS_URL: &str =
|
||||||
"https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json";
|
"https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json";
|
||||||
@@ -64,28 +64,48 @@ pub async fn fetch_forge_game_versions(client: &HttpClient) -> Result<Vec<GameVe
|
|||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// forge has used at least three different maven naming conventions over the
|
||||||
|
// years with no clear cutoff. we just try each one until something works.
|
||||||
pub async fn download_forge_installer(
|
pub async fn download_forge_installer(
|
||||||
client: &HttpClient,
|
client: &HttpClient,
|
||||||
game_version: &str,
|
game_version: &str,
|
||||||
forge_version: &str,
|
forge_version: &str,
|
||||||
dest: &Path,
|
dest: &Path,
|
||||||
) -> Result<(), NetError> {
|
) -> Result<(), NetError> {
|
||||||
let url = format!(
|
let mc_no_dots: String = game_version.chars().filter(|c| *c != '.').collect();
|
||||||
"{}/{game_version}-{forge_version}/forge-{game_version}-{forge_version}-installer.jar",
|
|
||||||
FORGE_MAVEN_BASE,
|
let slugs = [
|
||||||
game_version = game_version,
|
format!("{game_version}-{forge_version}"),
|
||||||
forge_version = forge_version,
|
format!("{game_version}-{forge_version}-{game_version}"),
|
||||||
);
|
format!("{game_version}-{forge_version}-mc{mc_no_dots}"),
|
||||||
|
];
|
||||||
|
|
||||||
set_action(format!(
|
set_action(format!(
|
||||||
"Downloading Forge {}-{}...",
|
"Downloading Forge {}-{}...",
|
||||||
game_version, forge_version
|
game_version, forge_version
|
||||||
));
|
));
|
||||||
|
|
||||||
download_file(client, &url, dest, |downloaded, total| {
|
let mut last_err = None;
|
||||||
crate::tui::progress::set_progress(downloaded, total);
|
for slug in &slugs {
|
||||||
})
|
let url = format!(
|
||||||
.await
|
"{}/{slug}/forge-{slug}-installer.jar",
|
||||||
|
FORGE_MAVEN_BASE,
|
||||||
|
);
|
||||||
|
match download_file(client, &url, dest, |downloaded, total| {
|
||||||
|
crate::tui::progress::set_progress(downloaded, total);
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(()) => return Ok(()),
|
||||||
|
Err(e) => last_err = Some(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(last_err.unwrap_or_else(|| {
|
||||||
|
NetError::Parse(format!(
|
||||||
|
"No Forge installer found for {game_version}-{forge_version}"
|
||||||
|
))
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_forge_installer(
|
pub async fn run_forge_installer(
|
||||||
@@ -112,15 +132,166 @@ pub async fn run_forge_installer(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
return Err(NetError::InstallerFailed(format!(
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
"Forge installer exited with {:?}",
|
let detail = if stderr.trim().is_empty() {
|
||||||
output.status.code()
|
format!("exit code {:?}", output.status.code())
|
||||||
)));
|
} else {
|
||||||
|
stderr.lines().last().unwrap_or("unknown error").to_string()
|
||||||
|
};
|
||||||
|
return Err(NetError::InstallerFailed(detail));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// old forge installers have an install_profile.json with a "versionInfo" key
|
||||||
|
// containing everything needed. modern ones don't have this structure.
|
||||||
|
pub(crate) fn has_legacy_install_profile(installer_path: &Path) -> bool {
|
||||||
|
let file = match std::fs::File::open(installer_path) {
|
||||||
|
Ok(f) => f,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
let mut archive = match zip::ZipArchive::new(file) {
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
let entry = match archive.by_name("install_profile.json") {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
let value: serde_json::Value = match serde_json::from_reader(entry) {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(_) => return false,
|
||||||
|
};
|
||||||
|
value.get("versionInfo").is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
// handles old-style forge installation by extracting the universal jar and
|
||||||
|
// library info directly from the installer, bypassing the GUI-only installer
|
||||||
|
pub(crate) async fn install_forge_from_profile(
|
||||||
|
client: &HttpClient,
|
||||||
|
installer_path: &Path,
|
||||||
|
meta_dir: &Path,
|
||||||
|
profile_filename: &str,
|
||||||
|
) -> Result<(), NetError> {
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
set_action("Installing legacy Forge from profile...");
|
||||||
|
|
||||||
|
let file = std::fs::File::open(installer_path)?;
|
||||||
|
let mut archive = zip::ZipArchive::new(file).map_err(|e| {
|
||||||
|
NetError::Parse(format!("Failed to open installer as ZIP: {e}"))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let profile_data: serde_json::Value = {
|
||||||
|
let entry = archive.by_name("install_profile.json").map_err(|e| {
|
||||||
|
NetError::Parse(format!("install_profile.json not found in installer: {e}"))
|
||||||
|
})?;
|
||||||
|
serde_json::from_reader(entry).map_err(|e| {
|
||||||
|
NetError::Parse(format!("Failed to parse install_profile.json: {e}"))
|
||||||
|
})?
|
||||||
|
};
|
||||||
|
|
||||||
|
let version_info = profile_data.get("versionInfo").ok_or_else(|| {
|
||||||
|
NetError::Parse("install_profile.json missing versionInfo".into())
|
||||||
|
})?;
|
||||||
|
let install_info = profile_data.get("install").ok_or_else(|| {
|
||||||
|
NetError::Parse("install_profile.json missing install section".into())
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let main_class = version_info
|
||||||
|
.get("mainClass")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.ok_or_else(|| NetError::Parse("missing versionInfo.mainClass".into()))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let libraries = version_info
|
||||||
|
.get("libraries")
|
||||||
|
.and_then(|v| v.as_array())
|
||||||
|
.ok_or_else(|| NetError::Parse("missing versionInfo.libraries".into()))?;
|
||||||
|
|
||||||
|
let file_path = install_info
|
||||||
|
.get("filePath")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.ok_or_else(|| NetError::Parse("missing install.filePath".into()))?;
|
||||||
|
|
||||||
|
let install_path_coord = install_info
|
||||||
|
.get("path")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.ok_or_else(|| NetError::Parse("missing install.path".into()))?;
|
||||||
|
|
||||||
|
// extract the universal jar to the correct maven location
|
||||||
|
let universal_maven_path = crate::net::maven_coord_to_path(install_path_coord)
|
||||||
|
.ok_or_else(|| {
|
||||||
|
NetError::Parse(format!("Invalid maven coord in install.path: {install_path_coord}"))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
set_sub_action("Extracting universal JAR...");
|
||||||
|
let universal_dest = meta_dir.join("libraries").join(&universal_maven_path);
|
||||||
|
if let Some(parent) = universal_dest.parent() {
|
||||||
|
std::fs::create_dir_all(parent)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut entry = archive.by_name(file_path).map_err(|e| {
|
||||||
|
NetError::Parse(format!("Universal JAR '{file_path}' not found in installer: {e}"))
|
||||||
|
})?;
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
entry.read_to_end(&mut buf)?;
|
||||||
|
std::fs::write(&universal_dest, &buf)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// download libraries that have a url field (forge-hosted ones).
|
||||||
|
// libs without url are from mojang and already handled elsewhere.
|
||||||
|
let libraries_dir = meta_dir.join("libraries");
|
||||||
|
for lib in libraries {
|
||||||
|
let name = lib.get("name").and_then(|v| v.as_str()).unwrap_or_default();
|
||||||
|
let url = match lib.get("url").and_then(|v| v.as_str()) {
|
||||||
|
Some(u) => u,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
|
||||||
|
let maven_path = match crate::net::maven_coord_to_path(name) {
|
||||||
|
Some(p) => p,
|
||||||
|
None => {
|
||||||
|
return Err(NetError::Parse(format!(
|
||||||
|
"Invalid Maven coordinate: {name}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let dest = libraries_dir.join(&maven_path);
|
||||||
|
if dest.exists() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let base_url = url.trim_end_matches('/');
|
||||||
|
let download_url = format!("{base_url}/{maven_path}");
|
||||||
|
|
||||||
|
set_sub_action(name);
|
||||||
|
download_file(client, &download_url, &dest, |_, _| {}).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
set_action("Saving Forge profile...");
|
||||||
|
let lib_entries: Vec<serde_json::Value> = libraries
|
||||||
|
.iter()
|
||||||
|
.filter_map(|l| {
|
||||||
|
l.get("name")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.map(|n| serde_json::json!({"name": n}))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let profile_json = serde_json::json!({
|
||||||
|
"mainClass": main_class,
|
||||||
|
"libraries": lib_entries,
|
||||||
|
});
|
||||||
|
|
||||||
|
crate::instance::loader::save_profile_json(meta_dir, profile_filename, &profile_json)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
Reference in new issue
Block a user