11package com .falsepattern .lib .dependencies ;
22
33import com .falsepattern .lib .StableAPI ;
4- import com .falsepattern .lib .internal .CoreLoadingPlugin ;
5- import com . falsepattern . lib . internal . FalsePatternLib ;
4+ import com .falsepattern .lib .internal .* ;
5+
66import java .io .BufferedOutputStream ;
77import java .io .File ;
8- import java .io .FileOutputStream ;
98import java .io .IOException ;
109import java .io .InputStream ;
1110import java .net .URL ;
1413import java .util .HashSet ;
1514import java .util .Map ;
1615import java .util .Set ;
17- import javax .net .ssl .HttpsURLConnection ;
18- import lombok .Builder ;
19- import lombok .NonNull ;
20- import lombok .val ;
21- import lombok .var ;
16+
17+ import lombok .*;
2218import net .minecraft .launchwrapper .LaunchClassLoader ;
2319
2420
@@ -34,21 +30,18 @@ public static void addMavenRepo(String url) {
3430 }
3531
3632 @ Builder
37- public static void loadLibrary (String loadingModId ,
38- String groupId ,
39- String artifactId ,
33+ public static void loadLibrary (@ NonNull String loadingModId ,
34+ @ NonNull String groupId ,
35+ @ NonNull String artifactId ,
4036 @ NonNull Version minVersion ,
4137 Version maxVersion ,
4238 @ NonNull Version preferredVersion ,
4339 String regularSuffix ,
4440 String devSuffix ) {
4541 val suffix = FalsePatternLib .isDeveloperEnvironment () ? devSuffix : regularSuffix ;
42+ val artifactLogName = String .format ("%s:%s:%s%s" , groupId , artifactId , preferredVersion , suffix != null ? "-" + suffix : "" );
4643 FalsePatternLib .getLog ()
47- .info ("Adding library {}:{}:{}{}, requested by mod {}" ,
48- groupId ,
49- artifactId ,
50- preferredVersion ,
51- suffix != null ? "-" + suffix : "" ,
44+ .info ("Adding library {}, requested by mod {}" , artifactLogName ,
5245 loadingModId );
5346 var artifact = groupId + ":" + artifactId + ":" + suffix ;
5447 if (loadedLibraries .containsKey (artifact )) {
@@ -108,25 +101,25 @@ public static void loadLibrary(String loadingModId,
108101 addToClasspath (file );
109102 loadedLibraries .put (artifact , preferredVersion );
110103 FalsePatternLib .getLog ()
111- .info ("Library {}:{}:{}{} successfully loaded from disk!" ,
112- groupId ,
113- artifactId ,
114- preferredVersion ,
115- (suffix != null ) ? ":" + suffix : "" );
104+ .info ("Library {} successfully loaded from disk!" , artifactLogName );
116105 return ;
117106 } catch (RuntimeException e ) {
118107 FalsePatternLib .getLog ()
119- .warn ("Failed to load library {}:{}:{}{} from file! Redownloading..." ,
120- groupId ,
121- artifactId ,
122- preferredVersion ,
123- (suffix != null ) ? ":" + suffix : "" );
108+ .warn ("Failed to load library {} from file! Redownloading..." , artifactLogName );
124109 if (!file .delete ()) {
125110 FalsePatternLib .getLog ().fatal ("Failed to delete file {}" , file );
126111 throw new RuntimeException ("Failed to delete file " + file );
127112 }
128113 }
129114 }
115+ if (!LibraryConfig .ENABLE_LIBRARY_DOWNLOADS ) {
116+ val errorMessage = "Failed to load library " + groupId + ":" + artifactId + ":" + preferredVersion +
117+ ((suffix != null ) ? ":" + suffix : "" ) + ": " + Tags .MODNAME +
118+ " library downloading has been disabled in the config, and the library is not present " +
119+ "on disk! Requested by mod: " + loadingModId ;
120+ FalsePatternLib .getLog ().fatal (errorMessage );
121+ throw new IllegalStateException (errorMessage );
122+ }
130123 for (var repo : mavenRepositories ) {
131124 try {
132125 if (!repo .endsWith ("/" )) {
@@ -138,39 +131,24 @@ public static void loadLibrary(String loadingModId,
138131 artifactId ,
139132 preferredVersion ,
140133 mavenJarName ));
141-
142- val connection = (HttpsURLConnection ) url .openConnection ();
143- connection .setConnectTimeout (1500 );
144- connection .setReadTimeout (1500 );
145- connection .setRequestProperty ("User-Agent" , "FalsePatternLib Downloader" );
146- if (connection .getResponseCode () != 200 ) {
134+ String finalRepo = repo ;
135+ Internet .connect (url , (ex ) -> {
147136 FalsePatternLib .getLog ()
148- .info ("Artifact {}:{}:{}{} was not found on repo {}" ,
149- groupId ,
150- artifactId ,
151- preferredVersion ,
152- (suffix != null ) ? ":" + suffix : "" ,
153- repo );
154- connection .disconnect ();
155- continue ;
156- }
157- FalsePatternLib .getLog ()
158- .info ("Downloading {}:{}:{}{} from {}" ,
159- groupId ,
160- artifactId ,
161- preferredVersion ,
162- (suffix != null ) ? ":" + suffix : "" ,
163- repo );
164- download (connection .getInputStream (), file );
165- FalsePatternLib .getLog ()
166- .info ("Downloaded {}:{}:{}{}" ,
167- groupId ,
168- artifactId ,
169- preferredVersion ,
170- (suffix != null ) ? ":" + suffix : "" );
171- loadedLibraries .put (artifact , preferredVersion );
172- loadedLibraryMods .put (artifact , loadingModId );
173- addToClasspath (file );
137+ .info ("Artifact {} could not be downloaded from repo {}: {}" ,
138+ artifactLogName ,
139+ finalRepo ,
140+ ex .getMessage ());
141+ }, (input ) -> {
142+ FalsePatternLib .getLog ()
143+ .info ("Downloading {} from {}" ,
144+ artifactLogName ,
145+ finalRepo );
146+ download (input , file );
147+ FalsePatternLib .getLog ().info ("Downloaded {}" , artifactLogName );
148+ loadedLibraries .put (artifact , preferredVersion );
149+ loadedLibraryMods .put (artifact , loadingModId );
150+ addToClasspath (file );
151+ });
174152 return ;
175153 } catch (IOException ignored ) {
176154 }
@@ -192,19 +170,11 @@ private static void addToClasspath(File file) {
192170 }
193171 }
194172
195- private static void download (InputStream is , File target ) throws IOException {
173+ @ SneakyThrows
174+ private static void download (InputStream is , File target ) {
196175 if (target .exists ()) {
197176 return ;
198177 }
199-
200- var bytesRead = 0 ;
201-
202- val fileOutput = new BufferedOutputStream (Files .newOutputStream (target .toPath ()));
203- byte [] smallBuffer = new byte [4096 ];
204- while ((bytesRead = is .read (smallBuffer )) >= 0 ) {
205- fileOutput .write (smallBuffer , 0 , bytesRead );
206- }
207- fileOutput .close ();
208- is .close ();
178+ Internet .transferAndClose (is , new BufferedOutputStream (Files .newOutputStream (target .toPath ())));
209179 }
210180}
0 commit comments