1
1
package com .falsepattern .lib ;
2
2
3
+ import com .falsepattern .lib .api .Version ;
3
4
import com .google .common .eventbus .EventBus ;
4
5
import com .google .common .eventbus .Subscribe ;
5
6
import cpw .mods .fml .common .DummyModContainer ;
6
7
import cpw .mods .fml .common .FMLCommonHandler ;
7
8
import cpw .mods .fml .common .LoadController ;
8
9
import cpw .mods .fml .common .ModMetadata ;
9
10
import cpw .mods .fml .common .event .FMLPreInitializationEvent ;
11
+ import lombok .NonNull ;
10
12
import lombok .val ;
11
13
import lombok .var ;
12
14
import net .minecraft .launchwrapper .Launch ;
@@ -27,11 +29,13 @@ public class FalsePatternLib extends DummyModContainer {
27
29
public static Logger libLog = LogManager .getLogger (ModInfo .MODNAME );
28
30
public static final boolean developerEnvironment = (boolean ) Launch .blackboard .get ("fml.deobfuscatedEnvironment" );
29
31
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 <>();
32
35
33
36
private static boolean modWasDownloaded = false ;
34
37
38
+ @ SuppressWarnings ("unchecked" )
35
39
public FalsePatternLib () {
36
40
super (new ModMetadata ());
37
41
libLog .info ("FalsePatternLib has been awakened!" );
@@ -68,18 +72,39 @@ public static void addMavenRepo(String url) {
68
72
mavenRepositories .add (url );
69
73
}
70
74
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 );
73
78
var artifact = groupId + ":" + artifactId ;
74
79
if (loadedLibraries .containsKey (artifact )) {
75
80
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 ));
79
103
}
104
+ return ;
80
105
}
81
106
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 ) : "" );
83
108
File file ;
84
109
if (isMod ) {
85
110
file = new File (modsDir , jarName );
@@ -95,32 +120,33 @@ public static void loadLibrary(String groupId, String artifactId, String version
95
120
if (!isMod ) {
96
121
addToClasspath (file );
97
122
}
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 );
100
125
return ;
101
126
} 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 );
103
128
file .delete ();
104
129
}
105
130
}
106
131
for (var repo : mavenRepositories ) {
107
132
try {
108
133
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 ));
110
135
111
136
val connection = (HttpsURLConnection ) url .openConnection ();
112
137
connection .setConnectTimeout (1500 );
113
138
connection .setReadTimeout (1500 );
114
139
connection .setRequestProperty ("User-Agent" , "FalsePatternLib Downloader" );
115
140
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 );
117
142
connection .disconnect ();
118
143
continue ;
119
144
}
120
- libLog .info ("Downloading {}:{}:{} from {}" , groupId , artifactId , version , repo );
145
+ libLog .info ("Downloading {}:{}:{} from {}" , groupId , artifactId , preferredVersion , repo );
121
146
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 );
124
150
if (isMod ) {
125
151
if (!modWasDownloaded ) {
126
152
modWasDownloaded = true ;
@@ -132,7 +158,9 @@ public static void loadLibrary(String groupId, String artifactId, String version
132
158
return ;
133
159
} catch (IOException ignored ) {}
134
160
}
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 );
136
164
}
137
165
138
166
private static void addToClasspath (File file ) {
0 commit comments