19
19
20
20
import com .falsepattern .lib .util .FileUtil ;
21
21
import lombok .*;
22
+ import org .apache .logging .log4j .LogManager ;
23
+ import org .apache .logging .log4j .Logger ;
24
+
22
25
import net .minecraft .launchwrapper .LaunchClassLoader ;
23
26
24
27
@@ -33,6 +36,8 @@ public class DependencyLoader {
33
36
private static final Map <String , Version > loadedLibraries = new HashMap <>();
34
37
private static final Map <String , String > loadedLibraryMods = new HashMap <>();
35
38
private static final Set <String > mavenRepositories = new HashSet <>();
39
+
40
+ private static final Logger log = LogManager .getLogger (Tags .MODNAME + " Dependency Downloader" );
36
41
37
42
public static void addMavenRepo (String url ) {
38
43
mavenRepositories .add (url );
@@ -49,9 +54,7 @@ public static void loadLibrary(@NonNull String loadingModId,
49
54
String devSuffix ) {
50
55
val suffix = FalsePatternLib .isDeveloperEnvironment () ? devSuffix : regularSuffix ;
51
56
val artifactLogName = String .format ("%s:%s:%s%s" , groupId , artifactId , preferredVersion , suffix != null ? "-" + suffix : "" );
52
- FalsePatternLib .getLog ()
53
- .info ("Adding library {}, requested by mod {}" , artifactLogName ,
54
- loadingModId );
57
+ log .info ("Adding library {}, requested by mod {}" , artifactLogName , loadingModId );
55
58
var artifact = groupId + ":" + artifactId + ":" + suffix ;
56
59
if (loadedLibraries .containsKey (artifact )) {
57
60
val currentVer = loadedLibraries .get (artifact );
@@ -61,35 +64,33 @@ public static void loadLibrary(@NonNull String loadingModId,
61
64
val rangeString = "(minimum: " + minVersion + (maxVersion == null ? "" : ", maximum: " + maxVersion ) + ")" ;
62
65
if (minVersion .compareTo (currentVer ) > 0 || (maxVersion != null && maxVersion .compareTo (currentVer ) < 0 )) {
63
66
for (int i = 0 ; i < 16 ; i ++) {
64
- FalsePatternLib . getLog () .fatal ("ALERT VVVVVVVVVVVV ALERT" );
67
+ log .fatal ("ALERT VVVVVVVVVVVV ALERT" );
65
68
}
66
- FalsePatternLib .getLog ()
67
- .fatal ("Library {}:{}{} already loaded with version {}, " +
68
- "but a version in the range {} was requested! Thing may go horribly wrong! " +
69
- "Requested by mod: {}, previously loaded by mod: {}" ,
70
- groupId ,
71
- artifactId ,
72
- suffix != null ? ":" + suffix : "" ,
73
- currentVer ,
74
- rangeString ,
75
- loadingModId ,
76
- loadedLibraryMods .get (artifact ));
69
+ log .fatal ("Library {}:{}{} already loaded with version {}, " +
70
+ "but a version in the range {} was requested! Thing may go horribly wrong! " +
71
+ "Requested by mod: {}, previously loaded by mod: {}" ,
72
+ groupId ,
73
+ artifactId ,
74
+ suffix != null ? ":" + suffix : "" ,
75
+ currentVer ,
76
+ rangeString ,
77
+ loadingModId ,
78
+ loadedLibraryMods .get (artifact ));
77
79
for (int i = 0 ; i < 16 ; i ++) {
78
- FalsePatternLib . getLog () .fatal ("ALERT ^^^^^^^^^^^^ ALERT" );
80
+ log .fatal ("ALERT ^^^^^^^^^^^^ ALERT" );
79
81
}
80
82
} else {
81
- FalsePatternLib .getLog ()
82
- .info ("Attempted loading of library {}:{}{} with preferred version {}, " +
83
- "but version {} was already loaded, which matched the range {}. This is not an " +
84
- "error. " + "Requested by mod: {}, previously loaded by mod: {}" ,
85
- groupId ,
86
- artifactId ,
87
- suffix != null ? ":" + suffix : "" ,
88
- preferredVersion ,
89
- currentVer ,
90
- rangeString ,
91
- loadingModId ,
92
- loadedLibraryMods .get (artifact ));
83
+ log .info ("Attempted loading of library {}:{}{} with preferred version {}, " +
84
+ "but version {} was already loaded, which matched the range {}. This is not an " +
85
+ "error. " + "Requested by mod: {}, previously loaded by mod: {}" ,
86
+ groupId ,
87
+ artifactId ,
88
+ suffix != null ? ":" + suffix : "" ,
89
+ preferredVersion ,
90
+ currentVer ,
91
+ rangeString ,
92
+ loadingModId ,
93
+ loadedLibraryMods .get (artifact ));
93
94
}
94
95
return ;
95
96
}
@@ -107,7 +108,7 @@ public static void loadLibrary(@NonNull String loadingModId,
107
108
val libDir = new File (modsDir , "falsepattern" );
108
109
if (!libDir .exists ()) {
109
110
if (!libDir .mkdirs ()) {
110
- FalsePatternLib . getLog () .fatal ("Failed to create directory {}" , libDir );
111
+ log .fatal ("Failed to create directory {}" , libDir );
111
112
throw new RuntimeException ("Failed to create directory " + libDir );
112
113
}
113
114
}
@@ -116,14 +117,12 @@ public static void loadLibrary(@NonNull String loadingModId,
116
117
try {
117
118
addToClasspath (file );
118
119
loadedLibraries .put (artifact , preferredVersion );
119
- FalsePatternLib .getLog ()
120
- .info ("Library {} successfully loaded from disk!" , artifactLogName );
120
+ log .info ("Library {} successfully loaded from disk!" , artifactLogName );
121
121
return ;
122
122
} catch (RuntimeException e ) {
123
- FalsePatternLib .getLog ()
124
- .warn ("Failed to load library {} from file! Redownloading..." , artifactLogName );
123
+ log .warn ("Failed to load library {} from file! Re-downloading..." , artifactLogName );
125
124
if (!file .delete ()) {
126
- FalsePatternLib . getLog () .fatal ("Failed to delete file {}" , file );
125
+ log .fatal ("Failed to delete file {}" , file );
127
126
throw new RuntimeException ("Failed to delete file " + file );
128
127
}
129
128
}
@@ -133,7 +132,7 @@ public static void loadLibrary(@NonNull String loadingModId,
133
132
((suffix != null ) ? ":" + suffix : "" ) + ": " + Tags .MODNAME +
134
133
" library downloading has been disabled in the config, and the library is not present " +
135
134
"on disk! Requested by mod: " + loadingModId ;
136
- FalsePatternLib . getLog () .fatal (errorMessage );
135
+ log .fatal (errorMessage );
137
136
throw new IllegalStateException (errorMessage );
138
137
}
139
138
for (var repo : mavenRepositories ) {
@@ -154,59 +153,52 @@ public static void loadLibrary(@NonNull String loadingModId,
154
153
break ;
155
154
}
156
155
val success = new AtomicBoolean (false );
157
- Internet .connect (new URL (url ), (ex ) -> {
158
- FalsePatternLib .getLog ()
159
- .info ("Artifact {} could not be downloaded from repo {}: {}" , artifactLogName ,
160
- finalRepo , ex .getMessage ());
161
- }, (input ) -> {
162
- FalsePatternLib .getLog ().info ("Downloading {} from {}" , artifactLogName , finalRepo );
156
+ Internet .connect (new URL (url ),
157
+ (ex ) -> log .info ("Artifact {} could not be downloaded from repo {}: {}" , artifactLogName ,
158
+ finalRepo , ex .getMessage ()),
159
+ (input ) -> {
160
+ log .info ("Downloading {} from {}" , artifactLogName , finalRepo );
163
161
download (input , file );
164
- FalsePatternLib . getLog () .info ("Downloaded {}" , artifactLogName );
162
+ log .info ("Downloaded {}" , artifactLogName );
165
163
success .set (true );
166
164
});
167
165
if (success .get ()) {
168
- FalsePatternLib . getLog () .info ("Validating checksum for {}" , artifactLogName );
166
+ log .info ("Validating checksum for {}" , artifactLogName );
169
167
boolean hadChecksum = false ;
170
168
for (val checksumType : CHECKSUM_TYPES ) {
171
169
val checksumURL = url + "." + checksumType ;
172
170
val checksumFile = new File (libDir , jarName + "." + checksumType );
173
- FalsePatternLib . getLog () .info ("Attempting to get {} checksum..." , checksumType );
171
+ log .info ("Attempting to get {} checksum..." , checksumType );
174
172
success .set (false );
175
- Internet .connect (new URL (checksumURL ), (ex ) -> {
176
- FalsePatternLib .getLog ()
177
- .info ("Could not get {} checksum for {}: {}" , checksumType ,
178
- artifactLogName , ex .getMessage ());
179
- }, (input ) -> {
180
- FalsePatternLib .getLog ()
181
- .info ("Downloading {} checksum for {}" , checksumType , artifactLogName );
173
+ Internet .connect (new URL (checksumURL ),
174
+ (ex ) -> log .info ("Could not get {} checksum for {}: {}" , checksumType ,
175
+ artifactLogName , ex .getMessage ()),
176
+ (input ) -> {
177
+ log .info ("Downloading {} checksum for {}" , checksumType , artifactLogName );
182
178
download (input , checksumFile );
183
- FalsePatternLib .getLog ()
184
- .info ("Downloaded {} checksum for {}" , checksumType , artifactLogName );
179
+ log .info ("Downloaded {} checksum for {}" , checksumType , artifactLogName );
185
180
success .set (true );
186
181
});
187
182
if (success .get ()) {
188
183
val fileHash = hash (checksumType , file );
189
184
val referenceHash = new String (Files .readAllBytes (checksumFile .toPath ()));
190
185
if (!fileHash .equals (referenceHash )) {
191
- FalsePatternLib .getLog ()
192
- .error ("Failed {} checksum validation for {}. Retrying download..." ,
193
- checksumType , artifactLogName );
186
+ log .error ("Failed {} checksum validation for {}. Retrying download..." ,
187
+ checksumType , artifactLogName );
194
188
file .delete ();
195
189
retry = true ;
196
190
continue redownload ;
197
191
}
198
- FalsePatternLib .getLog ()
199
- .info ("Successfully validated {} checksum for {}" , checksumType ,
200
- artifactLogName );
192
+ log .info ("Successfully validated {} checksum for {}" , checksumType ,
193
+ artifactLogName );
201
194
hadChecksum = true ;
202
195
break ;
203
196
}
204
197
}
205
198
if (!hadChecksum ) {
206
- FalsePatternLib .getLog ()
207
- .warn ("The library {} had no checksum available on the repository.\n " +
208
- "There's a chance it might have gotten corrupted during download,\n " +
209
- "but we're loading it anyways." , artifactLogName );
199
+ log .warn ("The library {} had no checksum available on the repository.\n " +
200
+ "There's a chance it might have gotten corrupted during download,\n " +
201
+ "but we're loading it anyways." , artifactLogName );
210
202
}
211
203
loadedLibraries .put (artifact , preferredVersion );
212
204
loadedLibraryMods .put (artifact , loadingModId );
@@ -219,7 +211,7 @@ public static void loadLibrary(@NonNull String loadingModId,
219
211
}
220
212
val errorMessage = "Failed to download library " + groupId + ":" + artifactId + ":" + preferredVersion + ((suffix != null ) ? ":" + suffix : "" ) + " from any repository! Requested by mod: " +
221
213
loadingModId ;
222
- FalsePatternLib . getLog () .fatal (errorMessage );
214
+ log .fatal (errorMessage );
223
215
throw new IllegalStateException (errorMessage );
224
216
}
225
217
@@ -264,7 +256,7 @@ private static void addToClasspath(File file) {
264
256
try {
265
257
val cl = (LaunchClassLoader ) DependencyLoader .class .getClassLoader ();
266
258
cl .addURL (file .toURI ().toURL ());
267
- FalsePatternLib . getLog () .info ("Injected file {} into classpath!" , file .getPath ());
259
+ log .info ("Injected file {} into classpath!" , file .getPath ());
268
260
} catch (Exception e ) {
269
261
throw new RuntimeException ("Failed to add library to classpath: " + file .getAbsolutePath (), e );
270
262
}
0 commit comments