11package com .falsepattern .lib ;
22
3+ import com .falsepattern .lib .api .Version ;
34import com .google .common .eventbus .EventBus ;
45import com .google .common .eventbus .Subscribe ;
56import cpw .mods .fml .common .DummyModContainer ;
67import cpw .mods .fml .common .FMLCommonHandler ;
78import cpw .mods .fml .common .LoadController ;
89import cpw .mods .fml .common .ModMetadata ;
910import cpw .mods .fml .common .event .FMLPreInitializationEvent ;
11+ import lombok .NonNull ;
1012import lombok .val ;
1113import lombok .var ;
1214import net .minecraft .launchwrapper .Launch ;
@@ -27,11 +29,13 @@ public class FalsePatternLib extends DummyModContainer {
2729 public static Logger libLog = LogManager .getLogger (ModInfo .MODNAME );
2830 public static final boolean developerEnvironment = (boolean ) Launch .blackboard .get ("fml.deobfuscatedEnvironment" );
2931
30- private static Map <String , String > loadedLibraries = new HashMap <>();
31- private static Set <String > mavenRepositories = new HashSet <>();
32+ private static final Map <String , Version > loadedLibraries = new HashMap <>();
33+ private static final Map <String , String > loadedLibraryMods = new HashMap <>();
34+ private static final Set <String > mavenRepositories = new HashSet <>();
3235
3336 private static boolean modWasDownloaded = false ;
3437
38+ @ SuppressWarnings ("unchecked" )
3539 public FalsePatternLib () {
3640 super (new ModMetadata ());
3741 libLog .info ("FalsePatternLib has been awakened!" );
@@ -68,18 +72,39 @@ public static void addMavenRepo(String url) {
6872 mavenRepositories .add (url );
6973 }
7074
71- public static void loadLibrary (String groupId , String artifactId , String version , String devSuffix , boolean isMod ) {
72- libLog .info ("Adding library {}:{}:{}" , groupId , artifactId , version );
75+ @ SuppressWarnings ("ResultOfMethodCallIgnored" )
76+ public static void loadLibrary (String loadingModId , String groupId , String artifactId , @ NonNull Version minVersion , Version maxVersion , @ NonNull Version preferredVersion , String devSuffix , boolean isMod ) {
77+ libLog .info ("Adding library {}:{}:{}, requested by mod {}" , groupId , artifactId , preferredVersion , loadingModId );
7378 var artifact = groupId + ":" + artifactId ;
7479 if (loadedLibraries .containsKey (artifact )) {
7580 val currentVer = loadedLibraries .get (artifact );
76- if (!version .equals (currentVer )) {
77- libLog .warn ("Tried to load library {}:{}:{}, but version {} was already loaded!" , groupId , artifactId , version , currentVer );
78- return ;
81+ if (currentVer .equals (preferredVersion )) return ;
82+ val rangeString = "(minimum: " + minVersion + (maxVersion == null ? "" : ", maximum: " + maxVersion ) + ")" ;
83+ if (minVersion .compareTo (currentVer ) > 0 || (maxVersion != null && maxVersion .compareTo (currentVer ) < 0 )) {
84+ for (int i = 0 ; i < 16 ; i ++) {
85+ libLog .fatal ("ALERT VVVVVVVVVVVV ALERT" );
86+ }
87+ libLog .fatal ("Library {}:{} already loaded with version {}, " +
88+ "but a version in the range {} was requested! Thing may go horribly wrong! " +
89+ "Requested by mod: {}, previously loaded by mod: {}" ,
90+ groupId , artifactId , currentVer ,
91+ rangeString ,
92+ loadingModId , loadedLibraryMods .get (artifact ));
93+ for (int i = 0 ; i < 16 ; i ++) {
94+ libLog .fatal ("ALERT ^^^^^^^^^^^^ ALERT" );
95+ }
96+ } else {
97+ libLog .info ("Attempted loading of library {}:{} with preferred version {}, " +
98+ "but version {} was already loaded, which matched the range {}. This is not an error. " +
99+ "Requested by mod: {}, previously loaded by mod: {}" ,
100+ groupId , artifactId , preferredVersion ,
101+ currentVer , rangeString ,
102+ loadingModId , loadedLibraryMods .get (artifact ));
79103 }
104+ return ;
80105 }
81106 val modsDir = new File (CoreLoadingPlugin .mcDir , "mods" );
82- val jarName = String .format ("%s-%s%s.jar" , artifactId , version , (developerEnvironment && devSuffix != null ) ? ("-" + devSuffix ) : "" );
107+ val jarName = String .format ("%s%s -%s%s.jar" , isMod ? "" : ( groupId + "-" ), artifactId , preferredVersion , (developerEnvironment && devSuffix != null ) ? ("-" + devSuffix ) : "" );
83108 File file ;
84109 if (isMod ) {
85110 file = new File (modsDir , jarName );
@@ -95,32 +120,33 @@ public static void loadLibrary(String groupId, String artifactId, String version
95120 if (!isMod ) {
96121 addToClasspath (file );
97122 }
98- loadedLibraries .put (artifact , version );
99- libLog .info ("Library {}:{}:{} successfully loaded from disk!" , groupId , artifactId , version );
123+ loadedLibraries .put (artifact , preferredVersion );
124+ libLog .info ("Library {}:{}:{} successfully loaded from disk!" , groupId , artifactId , preferredVersion );
100125 return ;
101126 } catch (RuntimeException e ) {
102- libLog .warn ("Failed to load library {}:{}:{} from file! Redownloading..." , groupId , artifactId , version );
127+ libLog .warn ("Failed to load library {}:{}:{} from file! Redownloading..." , groupId , artifactId , preferredVersion );
103128 file .delete ();
104129 }
105130 }
106131 for (var repo : mavenRepositories ) {
107132 try {
108133 if (!repo .endsWith ("/" )) repo = repo + "/" ;
109- val url = new URL (String .format ("%s%s/%s/%s/%s" , repo , groupId .replace ('.' , '/' ), artifactId , version , jarName ));
134+ val url = new URL (String .format ("%s%s/%s/%s/%s" , repo , groupId .replace ('.' , '/' ), artifactId , preferredVersion , jarName ));
110135
111136 val connection = (HttpsURLConnection ) url .openConnection ();
112137 connection .setConnectTimeout (1500 );
113138 connection .setReadTimeout (1500 );
114139 connection .setRequestProperty ("User-Agent" , "FalsePatternLib Downloader" );
115140 if (connection .getResponseCode () != 200 ) {
116- libLog .info ("Artifact {}:{}:{} was not found on repo {}" , groupId , artifactId , version , repo );
141+ libLog .info ("Artifact {}:{}:{} was not found on repo {}" , groupId , artifactId , preferredVersion , repo );
117142 connection .disconnect ();
118143 continue ;
119144 }
120- libLog .info ("Downloading {}:{}:{} from {}" , groupId , artifactId , version , repo );
145+ libLog .info ("Downloading {}:{}:{} from {}" , groupId , artifactId , preferredVersion , repo );
121146 download (connection .getInputStream (), file );
122- libLog .info ("Downloaded {}:{}:{}" , groupId , artifactId , version );
123- loadedLibraries .put (artifact , version );
147+ libLog .info ("Downloaded {}:{}:{}" , groupId , artifactId , preferredVersion );
148+ loadedLibraries .put (artifact , preferredVersion );
149+ loadedLibraryMods .put (artifact , loadingModId );
124150 if (isMod ) {
125151 if (!modWasDownloaded ) {
126152 modWasDownloaded = true ;
@@ -132,7 +158,9 @@ public static void loadLibrary(String groupId, String artifactId, String version
132158 return ;
133159 } catch (IOException ignored ) {}
134160 }
135- throw new IllegalStateException ("Failed to download library " + groupId + ":" + artifactId + ":" + version + " from any repository!" );
161+ val errorMessage = "Failed to download library " + groupId + ":" + artifactId + ":" + preferredVersion + " from any repository! Requested by mod: " + loadingModId ;
162+ libLog .fatal (errorMessage );
163+ throw new IllegalStateException (errorMessage );
136164 }
137165
138166 private static void addToClasspath (File file ) {
0 commit comments