2424import net .flintmc .gradle .manifest .dev .DevelopmentStaticFiles ;
2525import net .flintmc .gradle .util .MaybeNull ;
2626import net .flintmc .gradle .util .Util ;
27+ import net .flintmc .gradle .util .resource .ResourceFinder ;
28+ import net .flintmc .gradle .util .resource .ResourceLoader ;
2729import net .flintmc .installer .impl .repository .models .PackageModel ;
2830import net .flintmc .installer .impl .repository .models .install .DownloadFileDataModel ;
2931import net .flintmc .installer .impl .repository .models .install .InstallInstructionModel ;
3032import net .flintmc .installer .impl .repository .models .install .InstallInstructionTypes ;
3133import okhttp3 .OkHttpClient ;
3234import 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 ;
3439
3540import javax .inject .Inject ;
3641import java .io .*;
37- import java .net .* ;
42+ import java .net .URI ;
3843import java .nio .file .Files ;
3944import 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 ;
4149
4250/**
4351 * Task for downloading and installing static files.
@@ -89,7 +97,7 @@ public String getMinecraftVersion() {
8997 */
9098 @ Classpath
9199 public Set <File > getClasspath () {
92- if (classpath == null ) {
100+ if (classpath == null ) {
93101 classpath = potentialClasspath .getRealClasspath (getProject (), minecraftVersion ).getFiles ();
94102 }
95103
@@ -100,47 +108,38 @@ public Set<File> getClasspath() {
100108 * Computes the work this task has to do.
101109 */
102110 private void compute () {
103- if (sources != null && classpath != null ) {
111+ if (sources != null && classpath != null ) {
104112 return ;
105113 }
106114
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" ) ;
110118
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 );
122119 Set <PackageModel > manifests = new HashSet <>();
123120
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 ;
132126 }
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 );
133134 }
134- } catch (IOException | URISyntaxException e ) {
135- throw new FlintGradleException ("Failed to load manifests from classpath" , e );
136135 }
137136
138137 sources = new HashMap <>();
139138 // 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 ()) {
142141 // Filter for DOWNLOAD_FILE instructions
143- if (installInstruction .getType ().equals (InstallInstructionTypes .DOWNLOAD_FILE .toString ())) {
142+ if (installInstruction .getType ().equals (InstallInstructionTypes .DOWNLOAD_FILE .toString ())) {
144143 // Try to retrieve a development environment override
145144 DownloadFileDataModel data = installInstruction .getData ();
146145 File localFile = DevelopmentStaticFiles .getFor (
@@ -149,7 +148,7 @@ private void compute() {
149148 // Compute where the file should be
150149 File target = new File (workingDir , data .getPath ());
151150
152- if (localFile != null ) {
151+ if (localFile != null ) {
153152 // There is an override available
154153 sources .put (target , new LocalSource (localFile ));
155154 } else {
@@ -167,7 +166,7 @@ private void compute() {
167166 @ TaskAction
168167 public void performInstall () {
169168 compute ();
170- for (Map .Entry <File , StaticFileSource > entry : sources .entrySet ()) {
169+ for (Map .Entry <File , StaticFileSource > entry : sources .entrySet ()) {
171170 File target = entry .getKey ();
172171 StaticFileSource source = entry .getValue ();
173172
@@ -218,12 +217,12 @@ public LocalSource(File source) {
218217
219218 @ Override
220219 public void install (File target ) throws IOException {
221- if (!target .getParentFile ().isDirectory () && !target .getParentFile ().mkdirs ()) {
220+ if (!target .getParentFile ().isDirectory () && !target .getParentFile ().mkdirs ()) {
222221 throw new IOException ("Failed to create directory " + target .getParentFile ().getAbsolutePath ());
223222 }
224223
225224 // Simply copy the file
226- try (
225+ try (
227226 FileInputStream in = new FileInputStream (source );
228227 FileOutputStream out = new FileOutputStream (target )
229228 ) {
@@ -256,16 +255,16 @@ private RemoteSource(DownloadFileDataModel model) {
256255 @ Override
257256 public void install (File target ) throws IOException {
258257 String localMD5 = null ;
259- if (target .isFile ()) {
258+ if (target .isFile ()) {
260259 // File exists, check MD5
261260 localMD5 = Util .md5Hex (Files .readAllBytes (target .toPath ()));
262- if (localMD5 .equals (model .getMd5 ())) {
261+ if (localMD5 .equals (model .getMd5 ())) {
263262 // MD5 matches, skip download
264263 return ;
265264 }
266265 }
267266
268- if (httpClient == null ) {
267+ if (httpClient == null ) {
269268 String errorMessage = target .isFile () ?
270269 "the md5 checksums " + localMD5 + " of the static file " + model .getPath () + " (" + target .getAbsolutePath ()
271270 + ") does not match the expected value " + model .getMd5 () :
0 commit comments