diff --git a/src/instance/launch/mod.rs b/src/instance/launch/mod.rs index 60cf0b4..c042be8 100644 --- a/src/instance/launch/mod.rs +++ b/src/instance/launch/mod.rs @@ -115,6 +115,17 @@ async fn migrate_legacy_loader_profile_if_needed( config: &InstanceConfig, instance_dir: &Path, ) -> Result, LaunchError> { + // Fabric and Quilt fetch their profiles from a network endpoint at + // install time; there's no installer-written JSON on disk to recover + // from. their upstream profiles also happen to match the "legacy + // stripped" predicate (no inheritsFrom, no arguments), so without this + // early return every Fabric/Quilt launch would incorrectly fail + // migration. resolve() handles their lack of inheritsFrom via the + // implicit fallback in the launch flow. + if matches!(config.loader, ModLoader::Fabric | ModLoader::Quilt) { + return Ok(None); + } + let is_legacy = profile.inherits_from.is_none() && profile.arguments.is_none() && profile.minecraft_arguments.is_none(); @@ -809,4 +820,64 @@ mod tests { && modern.minecraft_arguments.is_none(); assert!(!is_legacy); } + + #[tokio::test] + async fn migrate_legacy_loader_profile_skips_fabric() { + // a fresh upstream Fabric profile happens to match the "legacy" + // shape (no inheritsFrom, no arguments, no minecraftArguments). + // make sure the migration helper recognises this is Fabric and + // returns Ok(None) instead of erroring with "reinstall Fabric". + use crate::launch_profile::model::LaunchProfile; + use chrono::Utc; + use tempfile::TempDir; + + let tmp = TempDir::new().unwrap(); + let instance_dir = tmp.path().join("instance"); + std::fs::create_dir_all(&instance_dir).unwrap(); + let profile_path = tmp.path().join("fabric-1.20.1-0.14.21.json"); + std::fs::write(&profile_path, b"{}").unwrap(); + + let upstream_fabric_shape = LaunchProfile { + id: "fabric-loader-0.14.21-1.20.1".into(), + inherits_from: None, + main_class: Some("net.fabricmc.loader.impl.launch.knot.KnotClient".into()), + libraries: Vec::new(), + arguments: None, + minecraft_arguments: None, + asset_index: None, + assets: None, + java_version: None, + downloads: None, + release_time: None, + time: None, + type_: None, + }; + + let config = InstanceConfig { + name: "test".into(), + game_version: "1.20.1".into(), + loader: ModLoader::Fabric, + loader_version: Some("0.14.21".into()), + created: Utc::now(), + last_played: None, + java_path: None, + memory_max: None, + memory_min: None, + jvm_args: Vec::new(), + resolution: None, + }; + + let result = migrate_legacy_loader_profile_if_needed( + &profile_path, + &upstream_fabric_shape, + &config, + &instance_dir, + ) + .await; + + assert!( + matches!(result, Ok(None)), + "expected Ok(None), got {result:?}" + ); + } } diff --git a/src/instance/loader/fabric.rs b/src/instance/loader/fabric.rs index a442c70..ae658cf 100644 --- a/src/instance/loader/fabric.rs +++ b/src/instance/loader/fabric.rs @@ -41,15 +41,15 @@ impl ModLoaderInstaller for FabricInstaller { _instance_dir: &Path, meta_dir: &Path, ) -> Result<(), NetError> { - let profile = - fabric_api::fetch_fabric_profile(client, game_version, loader_version).await?; + let (profile, raw_bytes) = + fabric_api::fetch_fabric_profile_with_raw(client, game_version, loader_version).await?; fabric_api::download_fabric_libraries(client, &profile, meta_dir).await?; - super::save_profile_json( - meta_dir, - &format!("fabric-{game_version}-{loader_version}.json"), - &profile, - )?; + let profiles_dir = meta_dir.join("loader-profiles"); + std::fs::create_dir_all(&profiles_dir)?; + let profile_path = + profiles_dir.join(format!("fabric-{game_version}-{loader_version}.json")); + std::fs::write(&profile_path, &raw_bytes)?; Ok(()) } diff --git a/src/instance/loader/mod.rs b/src/instance/loader/mod.rs index aa3e5d9..168ca68 100644 --- a/src/instance/loader/mod.rs +++ b/src/instance/loader/mod.rs @@ -253,4 +253,46 @@ mod tests { // resolved at launch time via maven_coord_to_path(name). assert!(profile.libraries[0].downloads.is_none()); } + + #[test] + fn raw_fabric_profile_bytes_parse_as_launch_profile() { + use tempfile::TempDir; + let tmp = TempDir::new().unwrap(); + let meta_dir = tmp.path(); + + // a synthetic upstream fabric profile shape (id, mainClass, + // libraries with name+url, no inheritsFrom, no arguments). + // simulates what the install path will write verbatim. + let upstream_bytes = br#"{ + "id": "fabric-loader-0.14.21-1.20.1", + "mainClass": "net.fabricmc.loader.impl.launch.knot.KnotClient", + "libraries": [ + { "name": "net.fabricmc:fabric-loader:0.14.21", "url": "https://maven.fabricmc.net/" }, + { "name": "net.fabricmc:intermediary:1.20.1", "url": "https://maven.fabricmc.net/" } + ] + }"#; + + let profiles_dir = meta_dir.join("loader-profiles"); + std::fs::create_dir_all(&profiles_dir).unwrap(); + let profile_path = profiles_dir.join("fabric-1.20.1-0.14.21.json"); + std::fs::write(&profile_path, upstream_bytes).unwrap(); + + let saved = std::fs::read(&profile_path).unwrap(); + assert_eq!(saved, upstream_bytes.to_vec()); + + let parsed: crate::launch_profile::model::LaunchProfile = + serde_json::from_slice(&saved).unwrap(); + assert_eq!(parsed.id, "fabric-loader-0.14.21-1.20.1"); + assert_eq!( + parsed.main_class.as_deref(), + Some("net.fabricmc.loader.impl.launch.knot.KnotClient") + ); + assert!(parsed.inherits_from.is_none()); // launch flow's implicit fallback handles it + assert!(parsed.arguments.is_none()); + assert_eq!(parsed.libraries.len(), 2); + assert_eq!( + parsed.libraries[0].url.as_deref(), + Some("https://maven.fabricmc.net/") + ); + } } diff --git a/src/instance/loader/quilt.rs b/src/instance/loader/quilt.rs index 5935f5e..3e208c7 100644 --- a/src/instance/loader/quilt.rs +++ b/src/instance/loader/quilt.rs @@ -41,14 +41,14 @@ impl ModLoaderInstaller for QuiltInstaller { _instance_dir: &Path, meta_dir: &Path, ) -> Result<(), NetError> { - let profile = quilt_api::fetch_quilt_profile(client, game_version, loader_version).await?; + let (profile, raw_bytes) = + quilt_api::fetch_quilt_profile_with_raw(client, game_version, loader_version).await?; quilt_api::download_quilt_libraries(client, &profile, meta_dir).await?; - super::save_profile_json( - meta_dir, - &format!("quilt-{game_version}-{loader_version}.json"), - &profile, - )?; + let profiles_dir = meta_dir.join("loader-profiles"); + std::fs::create_dir_all(&profiles_dir)?; + let profile_path = profiles_dir.join(format!("quilt-{game_version}-{loader_version}.json")); + std::fs::write(&profile_path, &raw_bytes)?; Ok(()) } diff --git a/src/net/fabric.rs b/src/net/fabric.rs index 86bcdc4..3587520 100644 --- a/src/net/fabric.rs +++ b/src/net/fabric.rs @@ -77,6 +77,25 @@ pub async fn fetch_fabric_profile( client.get_json(&url).await } +// like fetch_fabric_profile but also returns the raw response bytes so the +// caller can write the upstream JSON byte-for-byte to disk. used by the +// install path so we don't lose data (e.g. any future arguments field) by +// re-serializing through our narrow FabricProfile struct. +pub async fn fetch_fabric_profile_with_raw( + client: &HttpClient, + game_version: &str, + loader_version: &str, +) -> Result<(FabricProfile, Vec), NetError> { + let url = format!( + "{}/versions/loader/{}/{}/profile/json", + FABRIC_META_BASE, game_version, loader_version + ); + let raw = client.get_bytes(&url).await?; + let parsed: FabricProfile = serde_json::from_slice(&raw) + .map_err(|e| NetError::Parse(format!("Failed to parse Fabric profile: {e}")))?; + Ok((parsed, raw)) +} + // each fabric library entry has a maven coordinate and a base url. // the coordinate gets resolved to a path and combined with the url to download. pub async fn download_fabric_libraries( diff --git a/src/net/quilt.rs b/src/net/quilt.rs index 74560ec..fef5d59 100644 --- a/src/net/quilt.rs +++ b/src/net/quilt.rs @@ -74,6 +74,25 @@ pub async fn fetch_quilt_profile( client.get_json(&url).await } +// like fetch_quilt_profile but also returns the raw response bytes so the +// caller can write the upstream JSON byte-for-byte to disk. used by the +// install path so we don't lose data (e.g. any future arguments field) by +// re-serializing through our narrow QuiltProfile struct. +pub async fn fetch_quilt_profile_with_raw( + client: &HttpClient, + game_version: &str, + loader_version: &str, +) -> Result<(QuiltProfile, Vec), NetError> { + let url = format!( + "{}/versions/loader/{}/{}/profile/json", + QUILT_META_BASE, game_version, loader_version + ); + let raw = client.get_bytes(&url).await?; + let parsed: QuiltProfile = serde_json::from_slice(&raw) + .map_err(|e| NetError::Parse(format!("Failed to parse Quilt profile: {e}")))?; + Ok((parsed, raw)) +} + pub async fn download_quilt_libraries( client: &HttpClient, profile: &QuiltProfile,