24
24
import net .flintmc .gradle .manifest .dev .DevelopmentStaticFiles ;
25
25
import net .flintmc .gradle .util .MaybeNull ;
26
26
import net .flintmc .gradle .util .Util ;
27
+ import net .flintmc .gradle .util .resource .ResourceFinder ;
28
+ import net .flintmc .gradle .util .resource .ResourceLoader ;
27
29
import net .flintmc .installer .impl .repository .models .PackageModel ;
28
30
import net .flintmc .installer .impl .repository .models .install .DownloadFileDataModel ;
29
31
import net .flintmc .installer .impl .repository .models .install .InstallInstructionModel ;
30
32
import net .flintmc .installer .impl .repository .models .install .InstallInstructionTypes ;
31
33
import okhttp3 .OkHttpClient ;
32
34
import org .gradle .api .DefaultTask ;
33
- import org .gradle .api .tasks .*;
35
+ import org .gradle .api .tasks .Classpath ;
36
+ import org .gradle .api .tasks .Input ;
37
+ import org .gradle .api .tasks .InputFile ;
38
+ import org .gradle .api .tasks .TaskAction ;
34
39
35
40
import javax .inject .Inject ;
36
41
import java .io .*;
37
- import java .net .* ;
42
+ import java .net .URI ;
38
43
import java .nio .file .Files ;
39
44
import java .nio .file .StandardCopyOption ;
40
- import java .util .*;
45
+ import java .util .HashMap ;
46
+ import java .util .HashSet ;
47
+ import java .util .Map ;
48
+ import java .util .Set ;
41
49
42
50
/**
43
51
* Task for downloading and installing static files.
@@ -89,7 +97,7 @@ public String getMinecraftVersion() {
89
97
*/
90
98
@ Classpath
91
99
public Set <File > getClasspath () {
92
- if (classpath == null ) {
100
+ if (classpath == null ) {
93
101
classpath = potentialClasspath .getRealClasspath (getProject (), minecraftVersion ).getFiles ();
94
102
}
95
103
@@ -100,47 +108,38 @@ public Set<File> getClasspath() {
100
108
* Computes the work this task has to do.
101
109
*/
102
110
private void compute () {
103
- if (sources != null && classpath != null ) {
111
+ if (sources != null && classpath != null ) {
104
112
return ;
105
113
}
106
114
107
- // Convert the entire classpath to URLs
108
- Set < File > classpath = getClasspath ();
109
- URL [] classpathAsURLs = new URL [ classpath . size ()] ;
115
+ // Set up a search for the entire classpath
116
+ ResourceLoader loader = new ResourceLoader ( getClasspath () );
117
+ ResourceFinder finder = loader . findAll ( "manifest.json" ) ;
110
118
111
- int i = 0 ;
112
- for (File file : classpath ) {
113
- try {
114
- classpathAsURLs [i ++] = file .toURI ().toURL ();
115
- } catch (MalformedURLException e ) {
116
- throw new FlintGradleException ("Failed to convert file " + file .getAbsolutePath () + " to URL" , e );
117
- }
118
- }
119
-
120
- // Set up the class loader, it will just be used to retrieve resources
121
- ClassLoader loader = new URLClassLoader (classpathAsURLs );
122
119
Set <PackageModel > manifests = new HashSet <>();
123
120
124
- try {
125
- for (URL url : new HashSet <>(Collections .list (loader .getResources ("manifest.json" )))) {
126
- try (InputStream manifestStream = Util .getURLStream (httpClient , url .toURI ())) {
127
- // Read the manifest
128
- manifests .add (
129
- JsonConverter .PACKAGE_MODEL_SERIALIZER .fromString (Util .readAll (manifestStream ), PackageModel .class ));
130
- } catch (IOException e ) {
131
- throw new FlintGradleException ("Failed to read manifest " + url .toExternalForm (), e );
121
+ while (true ) {
122
+ try (InputStream stream = finder .streamNext ()) {
123
+ if (stream == null ) {
124
+ // No further streams found
125
+ break ;
132
126
}
127
+
128
+ // Deserialize the stream into a package model
129
+ manifests .add (JsonConverter .PACKAGE_MODEL_SERIALIZER .fromString (
130
+ Util .readAll (stream ), PackageModel .class
131
+ ));
132
+ } catch (IOException e ) {
133
+ throw new FlintGradleException ("Failed to load manifests from classpath" , e );
133
134
}
134
- } catch (IOException | URISyntaxException e ) {
135
- throw new FlintGradleException ("Failed to load manifests from classpath" , e );
136
135
}
137
136
138
137
sources = new HashMap <>();
139
138
// Index all manifests
140
- for (PackageModel manifest : manifests ) {
141
- for (InstallInstructionModel installInstruction : manifest .getInstallInstructions ()) {
139
+ for (PackageModel manifest : manifests ) {
140
+ for (InstallInstructionModel installInstruction : manifest .getInstallInstructions ()) {
142
141
// Filter for DOWNLOAD_FILE instructions
143
- if (installInstruction .getType ().equals (InstallInstructionTypes .DOWNLOAD_FILE .toString ())) {
142
+ if (installInstruction .getType ().equals (InstallInstructionTypes .DOWNLOAD_FILE .toString ())) {
144
143
// Try to retrieve a development environment override
145
144
DownloadFileDataModel data = installInstruction .getData ();
146
145
File localFile = DevelopmentStaticFiles .getFor (
@@ -149,7 +148,7 @@ private void compute() {
149
148
// Compute where the file should be
150
149
File target = new File (workingDir , data .getPath ());
151
150
152
- if (localFile != null ) {
151
+ if (localFile != null ) {
153
152
// There is an override available
154
153
sources .put (target , new LocalSource (localFile ));
155
154
} else {
@@ -167,7 +166,7 @@ private void compute() {
167
166
@ TaskAction
168
167
public void performInstall () {
169
168
compute ();
170
- for (Map .Entry <File , StaticFileSource > entry : sources .entrySet ()) {
169
+ for (Map .Entry <File , StaticFileSource > entry : sources .entrySet ()) {
171
170
File target = entry .getKey ();
172
171
StaticFileSource source = entry .getValue ();
173
172
@@ -218,12 +217,12 @@ public LocalSource(File source) {
218
217
219
218
@ Override
220
219
public void install (File target ) throws IOException {
221
- if (!target .getParentFile ().isDirectory () && !target .getParentFile ().mkdirs ()) {
220
+ if (!target .getParentFile ().isDirectory () && !target .getParentFile ().mkdirs ()) {
222
221
throw new IOException ("Failed to create directory " + target .getParentFile ().getAbsolutePath ());
223
222
}
224
223
225
224
// Simply copy the file
226
- try (
225
+ try (
227
226
FileInputStream in = new FileInputStream (source );
228
227
FileOutputStream out = new FileOutputStream (target )
229
228
) {
@@ -256,16 +255,16 @@ private RemoteSource(DownloadFileDataModel model) {
256
255
@ Override
257
256
public void install (File target ) throws IOException {
258
257
String localMD5 = null ;
259
- if (target .isFile ()) {
258
+ if (target .isFile ()) {
260
259
// File exists, check MD5
261
260
localMD5 = Util .md5Hex (Files .readAllBytes (target .toPath ()));
262
- if (localMD5 .equals (model .getMd5 ())) {
261
+ if (localMD5 .equals (model .getMd5 ())) {
263
262
// MD5 matches, skip download
264
263
return ;
265
264
}
266
265
}
267
266
268
- if (httpClient == null ) {
267
+ if (httpClient == null ) {
269
268
String errorMessage = target .isFile () ?
270
269
"the md5 checksums " + localMD5 + " of the static file " + model .getPath () + " (" + target .getAbsolutePath ()
271
270
+ ") does not match the expected value " + model .getMd5 () :
0 commit comments