11use std:: collections:: { HashMap , HashSet } ;
22
33use anyhow:: { Context , Result , anyhow, ensure} ;
4- use futures:: { StreamExt , TryStreamExt , stream} ;
4+ use futures:: { StreamExt , stream} ;
55use indexmap:: IndexMap ;
66use tracing:: { debug, info, info_span, warn} ;
77use tracing_anyhow:: FutureContext ;
@@ -13,7 +13,7 @@ use crate::{
1313 FabriclikeLoader , FabriclikeStoredArtifact , GAME_VERSION_PLACEHOLDER ,
1414 } ,
1515 util:: {
16- MavenCoordinate , ResponseExt , Sha1 , Sha256 , TaskContext ,
16+ MavenCoordinate , RequestExt , ResponseExt , Sha1 , Sha256 , TaskContext ,
1717 from_json_slice,
1818 } ,
1919} ;
@@ -147,6 +147,10 @@ pub async fn extract_catalogs(
147147 . with_context ( || {
148148 format ! ( "normalizing profile {} from {source}" , version. id)
149149 } ) ?;
150+ let profile_dependencies = dependencies
151+ . iter ( )
152+ . map ( |artifact| artifact. coordinate . clone ( ) )
153+ . collect ( ) ;
150154 for mut artifact in dependencies {
151155 artifact. source =
152156 Some ( format ! ( "{source} (profile SHA256 {sha256})" ) ) ;
@@ -162,14 +166,13 @@ pub async fn extract_catalogs(
162166 source_sha256 : sha256,
163167 template : template. to_owned ( ) ,
164168 metadata : profile,
169+ dependencies : profile_dependencies,
165170 } ) ;
166171 }
167172 drop ( blobs) ;
168173 validate_groups ( & games, & groups) ?;
169174 drop ( conn) ;
170175
171- // store dependencies before committing profiles which reference them;
172- // a failed download leaves the previous extracted dataset intact
173176 let num_total = artifacts. len ( ) ;
174177 let mut num_done = 0usize ;
175178 info ! ( num_artifacts = num_total, "downloading loader dependencies" ) ;
@@ -181,23 +184,36 @@ pub async fn extract_catalogs(
181184 || info_span ! ( "downloading Maven artifact" , %coordinate) ,
182185 )
183186 . await
187+ . inspect_err ( |error| {
188+ warn ! ( %coordinate, error = ?error, "skipping loader dependency after download failure" ) ;
189+ } )
190+ . ok ( )
184191 . map ( |artifact| ( index, artifact) )
185192 } )
186193 . buffer_unordered ( app. concurrency . download . get ( ) )
187- . inspect_ok ( |_| {
194+ . inspect ( |_| {
188195 num_done += 1 ;
189196 if num_done. is_multiple_of ( 100 ) || num_done == num_total {
190- info ! ( "downloaded {num_done}/{num_total} dependencies" ) ;
197+ info ! ( "processed {num_done}/{num_total} dependencies" ) ;
191198 }
192199 } )
193- . try_collect :: < Vec < _ > > ( )
194- . context ( info_span ! ( "downloading loader dependencies" ) )
195- . await ? ;
200+ . filter_map ( futures :: future :: ready )
201+ . collect :: < Vec < _ > > ( )
202+ . await ;
196203 artifacts. sort_unstable_by_key ( |( index, _) | * index) ;
197204 let artifacts = artifacts
198205 . into_iter ( )
199206 . map ( |( _, artifact) | artifact)
200207 . collect :: < Vec < _ > > ( ) ;
208+ let available_artifacts = artifacts
209+ . iter ( )
210+ . map ( |stored| stored. artifact . coordinate . to_maven_path ( ) )
211+ . collect :: < HashSet < _ > > ( ) ;
212+ retain_complete_profiles ( & mut profiles, & available_artifacts) ;
213+ ensure ! (
214+ !profiles. is_empty( ) ,
215+ "no complete {loader:?} profiles; keeping previous extracted dataset"
216+ ) ;
201217
202218 let mut conn = app
203219 . db
@@ -299,14 +315,14 @@ async fn download_artifact(
299315 let mut checksum_url = url. clone ( ) ;
300316 checksum_url. set_path ( & format ! ( "{}.sha256" , url. path( ) ) ) ;
301317
302- let response = app. http . get ( checksum_url. clone ( ) ) . send ( )
318+ let response = app. http . get ( checksum_url. clone ( ) ) . send_retry ( )
303319 . context ( info_span ! ( "fetching Maven artifact checksum" , url = %checksum_url, coordinate = %artifact. coordinate) )
304320 . await ?;
305321 let use_sha1 = response. status ( ) == reqwest:: StatusCode :: NOT_FOUND ;
306322 let response = if use_sha1 {
307323 debug ! ( %url, coordinate = %artifact. coordinate, "Maven artifact SHA256 checksum unavailable; using SHA1" ) ;
308324 checksum_url. set_path ( & format ! ( "{}.sha1" , url. path( ) ) ) ;
309- app. http . get ( checksum_url. clone ( ) ) . send ( )
325+ app. http . get ( checksum_url. clone ( ) ) . send_retry ( )
310326 . context ( info_span ! ( "fetching Maven artifact SHA1 checksum" , url = %checksum_url, coordinate = %artifact. coordinate) )
311327 . await ?
312328 } else {
@@ -374,7 +390,7 @@ async fn download_artifact(
374390 let bytes = async {
375391 app. http
376392 . get ( url. clone ( ) )
377- . send ( )
393+ . send_retry ( )
378394 . await ?
379395 . error_for_status_ext ( )
380396 . await ?
@@ -573,6 +589,33 @@ struct SelectedProfile {
573589 source_sha256 : Sha256 ,
574590 template : String ,
575591 metadata : ProfileMetadata ,
592+ dependencies : Vec < MavenCoordinate > ,
593+ }
594+
595+ fn retain_complete_profiles (
596+ profiles : & mut Vec < SelectedProfile > ,
597+ available_artifacts : & HashSet < String > ,
598+ ) {
599+ profiles. retain ( |profile| {
600+ let missing = profile
601+ . dependencies
602+ . iter ( )
603+ . filter ( |coordinate| {
604+ !available_artifacts. contains ( & coordinate. to_maven_path ( ) )
605+ } )
606+ . map ( ToString :: to_string)
607+ . collect :: < Vec < _ > > ( ) ;
608+ if missing. is_empty ( ) {
609+ return true ;
610+ }
611+ warn ! (
612+ loader_version = %profile. loader_version,
613+ group = %profile. version_group,
614+ missing_dependencies = ?missing,
615+ "skipping loader profile with missing dependencies"
616+ ) ;
617+ false
618+ } ) ;
576619}
577620
578621fn profile_source (
@@ -618,6 +661,119 @@ mod tests {
618661 use super :: * ;
619662 use serde_json:: { Value , json} ;
620663
664+ fn selected_profile (
665+ version : & str ,
666+ libraries : & [ & str ] ,
667+ games : & [ String ] ,
668+ ) -> SelectedProfile {
669+ let profile = serde_json:: from_value ( json ! ( {
670+ "id" : "loader-GAME" ,
671+ "inheritsFrom" : "GAME" ,
672+ "releaseTime" : "2026-01-01T00:00:00Z" ,
673+ "time" : "2026-01-01T00:00:00Z" ,
674+ "type" : "release" ,
675+ "libraries" : libraries. iter( ) . map( |name| json!( { "name" : name} ) ) . collect:: <Vec <_>>( )
676+ } ) ) . unwrap ( ) ;
677+ let ( metadata, artifacts) = normalize_profile (
678+ profile,
679+ "GAME" ,
680+ games,
681+ & "https://maven.example.com/" . parse ( ) . unwrap ( ) ,
682+ )
683+ . unwrap ( ) ;
684+ SelectedProfile {
685+ loader_version : version. into ( ) ,
686+ version_group : String :: new ( ) ,
687+ stable : true ,
688+ source_url : "https://meta.example.com/profile.json" . into ( ) ,
689+ source_sha256 : Sha256 :: from_digest ( b"profile" ) ,
690+ template : "GAME" . into ( ) ,
691+ metadata,
692+ dependencies : artifacts
693+ . into_iter ( )
694+ . map ( |artifact| artifact. coordinate )
695+ . collect ( ) ,
696+ }
697+ }
698+
699+ fn artifact_paths ( names : & [ & str ] ) -> HashSet < String > {
700+ names
701+ . iter ( )
702+ . map ( |name| {
703+ name. parse :: < MavenCoordinate > ( ) . unwrap ( ) . to_maven_path ( )
704+ } )
705+ . collect ( )
706+ }
707+
708+ #[ test]
709+ fn retains_complete_profiles_in_original_order ( ) {
710+ let mut profiles = vec ! [
711+ selected_profile( "2" , & [ "loader:main:2" ] , & [ ] ) ,
712+ selected_profile( "1" , & [ "loader:main:1" ] , & [ ] ) ,
713+ ] ;
714+ retain_complete_profiles (
715+ & mut profiles,
716+ & artifact_paths ( & [ "loader:main:1@jar" , "loader:main:2" ] ) ,
717+ ) ;
718+ assert_eq ! (
719+ profiles
720+ . iter( )
721+ . map( |profile| profile. loader_version. as_str( ) )
722+ . collect:: <Vec <_>>( ) ,
723+ vec![ "2" , "1" ]
724+ ) ;
725+ }
726+
727+ #[ test]
728+ fn excludes_only_profiles_with_missing_dependencies ( ) {
729+ let mut profiles = vec ! [
730+ selected_profile( "1" , & [ "loader:main:1" ] , & [ ] ) ,
731+ selected_profile( "2" , & [ "loader:main:2" , "org.ow2.asm:asm:9" ] , & [ ] ) ,
732+ ] ;
733+ retain_complete_profiles (
734+ & mut profiles,
735+ & artifact_paths ( & [ "loader:main:1" , "loader:main:2" ] ) ,
736+ ) ;
737+ assert_eq ! ( profiles. len( ) , 1 ) ;
738+ assert_eq ! ( profiles[ 0 ] . loader_version, "1" ) ;
739+ }
740+
741+ #[ test]
742+ fn checks_concrete_mappings_for_every_game_before_importing ( ) {
743+ let games = vec ! [ "1.21" . into( ) , "1.22" . into( ) ] ;
744+ for mapping in [ "net.fabricmc:intermediary" , "org.quiltmc:hashed" ] {
745+ let library = format ! ( "{mapping}:GAME:v2@jar" ) ;
746+ let first = format ! ( "{mapping}:1.21:v2" ) ;
747+ let second = format ! ( "{mapping}:1.22:v2" ) ;
748+ let mut incomplete =
749+ vec ! [ selected_profile( "1" , & [ & library] , & games) ] ;
750+ retain_complete_profiles (
751+ & mut incomplete,
752+ & artifact_paths ( & [ & first] ) ,
753+ ) ;
754+ assert ! ( incomplete. is_empty( ) ) ;
755+ let mut complete = vec ! [ selected_profile( "1" , & [ & library] , & games) ] ;
756+ retain_complete_profiles (
757+ & mut complete,
758+ & artifact_paths ( & [ & first, & second] ) ,
759+ ) ;
760+ assert_eq ! ( complete. len( ) , 1 ) ;
761+ }
762+ }
763+
764+ #[ test]
765+ fn missing_shared_dependency_excludes_all_affected_profiles ( ) {
766+ let mut profiles = vec ! [
767+ selected_profile( "1" , & [ "loader:main:1" , "org.ow2.asm:asm:9" ] , & [ ] ) ,
768+ selected_profile( "2" , & [ "loader:main:2" , "org.ow2.asm:asm:9" ] , & [ ] ) ,
769+ ] ;
770+ retain_complete_profiles (
771+ & mut profiles,
772+ & artifact_paths ( & [ "loader:main:1" , "loader:main:2" ] ) ,
773+ ) ;
774+ assert ! ( profiles. is_empty( ) ) ;
775+ }
776+
621777 fn group ( id : Option < & str > , template : & str , games : & [ & str ] ) -> ProfileGroup {
622778 ProfileGroup {
623779 id : id. map ( str:: to_owned) ,
0 commit comments