1
1
package com .falsepattern .lib .dependencies ;
2
2
3
3
import 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
+
6
6
import java .io .BufferedOutputStream ;
7
7
import java .io .File ;
8
- import java .io .FileOutputStream ;
9
8
import java .io .IOException ;
10
9
import java .io .InputStream ;
11
10
import java .net .URL ;
14
13
import java .util .HashSet ;
15
14
import java .util .Map ;
16
15
import java .util .Set ;
17
- import javax .net .ssl .HttpsURLConnection ;
18
16
19
- import com .falsepattern .lib .internal .LibraryConfig ;
20
- import lombok .Builder ;
21
- import lombok .NonNull ;
22
- import lombok .val ;
23
- import lombok .var ;
17
+ import lombok .*;
24
18
import net .minecraft .launchwrapper .LaunchClassLoader ;
25
19
26
20
@@ -36,21 +30,18 @@ public static void addMavenRepo(String url) {
36
30
}
37
31
38
32
@ Builder
39
- public static void loadLibrary (String loadingModId ,
40
- String groupId ,
41
- String artifactId ,
33
+ public static void loadLibrary (@ NonNull String loadingModId ,
34
+ @ NonNull String groupId ,
35
+ @ NonNull String artifactId ,
42
36
@ NonNull Version minVersion ,
43
37
Version maxVersion ,
44
38
@ NonNull Version preferredVersion ,
45
39
String regularSuffix ,
46
40
String devSuffix ) {
47
41
val suffix = FalsePatternLib .isDeveloperEnvironment () ? devSuffix : regularSuffix ;
42
+ val artifactLogName = String .format ("%s:%s:%s%s" , groupId , artifactId , preferredVersion , suffix != null ? "-" + suffix : "" );
48
43
FalsePatternLib .getLog ()
49
- .info ("Adding library {}:{}:{}{}, requested by mod {}" ,
50
- groupId ,
51
- artifactId ,
52
- preferredVersion ,
53
- suffix != null ? "-" + suffix : "" ,
44
+ .info ("Adding library {}, requested by mod {}" , artifactLogName ,
54
45
loadingModId );
55
46
var artifact = groupId + ":" + artifactId + ":" + suffix ;
56
47
if (loadedLibraries .containsKey (artifact )) {
@@ -110,19 +101,11 @@ public static void loadLibrary(String loadingModId,
110
101
addToClasspath (file );
111
102
loadedLibraries .put (artifact , preferredVersion );
112
103
FalsePatternLib .getLog ()
113
- .info ("Library {}:{}:{}{} successfully loaded from disk!" ,
114
- groupId ,
115
- artifactId ,
116
- preferredVersion ,
117
- (suffix != null ) ? ":" + suffix : "" );
104
+ .info ("Library {} successfully loaded from disk!" , artifactLogName );
118
105
return ;
119
106
} catch (RuntimeException e ) {
120
107
FalsePatternLib .getLog ()
121
- .warn ("Failed to load library {}:{}:{}{} from file! Redownloading..." ,
122
- groupId ,
123
- artifactId ,
124
- preferredVersion ,
125
- (suffix != null ) ? ":" + suffix : "" );
108
+ .warn ("Failed to load library {} from file! Redownloading..." , artifactLogName );
126
109
if (!file .delete ()) {
127
110
FalsePatternLib .getLog ().fatal ("Failed to delete file {}" , file );
128
111
throw new RuntimeException ("Failed to delete file " + file );
@@ -131,9 +114,9 @@ public static void loadLibrary(String loadingModId,
131
114
}
132
115
if (!LibraryConfig .ENABLE_LIBRARY_DOWNLOADS ) {
133
116
val errorMessage = "Failed to load library " + groupId + ":" + artifactId + ":" + preferredVersion +
134
- ((suffix != null ) ? ":" + suffix : "" ) + ": FalsePatternLib library downloading has " +
135
- "been disabled in the config, and the library is not present on disk! Requested by mod: " +
136
- loadingModId ;
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 ;
137
120
FalsePatternLib .getLog ().fatal (errorMessage );
138
121
throw new IllegalStateException (errorMessage );
139
122
}
@@ -148,39 +131,24 @@ public static void loadLibrary(String loadingModId,
148
131
artifactId ,
149
132
preferredVersion ,
150
133
mavenJarName ));
151
-
152
- val connection = (HttpsURLConnection ) url .openConnection ();
153
- connection .setConnectTimeout (1500 );
154
- connection .setReadTimeout (1500 );
155
- connection .setRequestProperty ("User-Agent" , "FalsePatternLib Downloader" );
156
- if (connection .getResponseCode () != 200 ) {
134
+ String finalRepo = repo ;
135
+ Internet .connect (url , (ex ) -> {
157
136
FalsePatternLib .getLog ()
158
- .info ("Artifact {}:{}:{}{} was not found on repo {}" ,
159
- groupId ,
160
- artifactId ,
161
- preferredVersion ,
162
- (suffix != null ) ? ":" + suffix : "" ,
163
- repo );
164
- connection .disconnect ();
165
- continue ;
166
- }
167
- FalsePatternLib .getLog ()
168
- .info ("Downloading {}:{}:{}{} from {}" ,
169
- groupId ,
170
- artifactId ,
171
- preferredVersion ,
172
- (suffix != null ) ? ":" + suffix : "" ,
173
- repo );
174
- download (connection .getInputStream (), file );
175
- FalsePatternLib .getLog ()
176
- .info ("Downloaded {}:{}:{}{}" ,
177
- groupId ,
178
- artifactId ,
179
- preferredVersion ,
180
- (suffix != null ) ? ":" + suffix : "" );
181
- loadedLibraries .put (artifact , preferredVersion );
182
- loadedLibraryMods .put (artifact , loadingModId );
183
- 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
+ });
184
152
return ;
185
153
} catch (IOException ignored ) {
186
154
}
@@ -202,19 +170,11 @@ private static void addToClasspath(File file) {
202
170
}
203
171
}
204
172
205
- private static void download (InputStream is , File target ) throws IOException {
173
+ @ SneakyThrows
174
+ private static void download (InputStream is , File target ) {
206
175
if (target .exists ()) {
207
176
return ;
208
177
}
209
-
210
- var bytesRead = 0 ;
211
-
212
- val fileOutput = new BufferedOutputStream (Files .newOutputStream (target .toPath ()));
213
- byte [] smallBuffer = new byte [4096 ];
214
- while ((bytesRead = is .read (smallBuffer )) >= 0 ) {
215
- fileOutput .write (smallBuffer , 0 , bytesRead );
216
- }
217
- fileOutput .close ();
218
- is .close ();
178
+ Internet .transferAndClose (is , new BufferedOutputStream (Files .newOutputStream (target .toPath ())));
219
179
}
220
180
}
0 commit comments