Skip to content

Commit 65ee24c

Browse files
authored
Merge pull request #22 from FlintMC/bugfix-and-annotation-processor-extension
Bugfix and annotation processor extension
2 parents 598970d + a33b325 commit 65ee24c

File tree

6 files changed

+357
-40
lines changed

6 files changed

+357
-40
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ repositories {
6666
mavenCentral()
6767
}
6868

69-
version = System.getenv().getOrDefault("VERSION", "2.9.0")
69+
version = System.getenv().getOrDefault("VERSION", "2.9.1")
7070

7171
dependencies {
7272
implementation(group = "com.fasterxml.jackson.core", name = "jackson-core", version = "2.11.1")

src/main/java/net/flintmc/gradle/java/JarTaskProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ private void configureAnnotationProcessor(Project project, SourceSet sourceSet)
8888
annotationProcessorArgs.put("net.flintmc.package.group", project.getGroup().toString());
8989
annotationProcessorArgs.put("net.flintmc.package.name", project.getName());
9090
annotationProcessorArgs.put("net.flintmc.package.version", project.getVersion().toString());
91+
annotationProcessorArgs.put("net.flintmc.sourceSet", sourceSet.getName());
9192

9293
// Collect the arguments from the map above into the form of "-Akey=value"
9394
task.getOptions().getCompilerArgumentProviders().add(() ->

src/main/java/net/flintmc/gradle/minecraft/InstallStaticFilesTask.java

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,28 @@
2424
import net.flintmc.gradle.manifest.dev.DevelopmentStaticFiles;
2525
import net.flintmc.gradle.util.MaybeNull;
2626
import net.flintmc.gradle.util.Util;
27+
import net.flintmc.gradle.util.resource.ResourceFinder;
28+
import net.flintmc.gradle.util.resource.ResourceLoader;
2729
import net.flintmc.installer.impl.repository.models.PackageModel;
2830
import net.flintmc.installer.impl.repository.models.install.DownloadFileDataModel;
2931
import net.flintmc.installer.impl.repository.models.install.InstallInstructionModel;
3032
import net.flintmc.installer.impl.repository.models.install.InstallInstructionTypes;
3133
import okhttp3.OkHttpClient;
3234
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;
3439

3540
import javax.inject.Inject;
3641
import java.io.*;
37-
import java.net.*;
42+
import java.net.URI;
3843
import java.nio.file.Files;
3944
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;
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() :
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* FlintMC
3+
* Copyright (C) 2020-2021 LabyMedia GmbH and contributors
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 3 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18+
*/
19+
20+
package net.flintmc.gradle.util.resource;
21+
22+
import org.jetbrains.annotations.NotNull;
23+
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.util.Arrays;
27+
import java.util.LinkedHashSet;
28+
import java.util.Set;
29+
30+
/**
31+
* Input stream which when closed delegates the closing to its delegate stream and other closeables.
32+
*/
33+
public class MultiResourceInputStream extends InputStream {
34+
private final Set<AutoCloseable> closeables;
35+
private final InputStream delegate;
36+
37+
/**
38+
* Constructs a new {@link MultiResourceInputStream} a delegate and other closeables.
39+
*
40+
* @param delegate The delegate to delegate stream operations to
41+
* @param closeables The other closeables to close when the delegate has been closed
42+
*/
43+
public MultiResourceInputStream(InputStream delegate, AutoCloseable... closeables) {
44+
this.closeables = new LinkedHashSet<>(Arrays.asList(closeables));
45+
this.delegate = delegate;
46+
}
47+
48+
@Override
49+
public void close() throws IOException {
50+
IOException ex = null;
51+
52+
try {
53+
delegate.close();
54+
} catch(IOException e) {
55+
ex = e;
56+
}
57+
58+
// Try to close as many closeables no matter what
59+
for(AutoCloseable closeable : closeables) {
60+
try {
61+
closeable.close();
62+
} catch(Exception e) {
63+
if(e instanceof IOException && ex == null) {
64+
// First exception, and its an I/O one, use it directly
65+
ex = (IOException) e;
66+
} else {
67+
if(ex == null) {
68+
// First exception, but not an I/O exception, wrap it
69+
ex = new IOException("Exception while closing MultiResourceInputStream", e);
70+
} else {
71+
// Earlier an exception occurred already, suppress this one
72+
ex.addSuppressed(e);
73+
}
74+
}
75+
}
76+
}
77+
78+
if(ex != null) {
79+
// At least on exception was thrown
80+
throw ex;
81+
}
82+
}
83+
84+
@Override
85+
public int read() throws IOException {
86+
return delegate.read();
87+
}
88+
89+
@Override
90+
public int read(@NotNull byte[] b) throws IOException {
91+
return delegate.read(b);
92+
}
93+
94+
@Override
95+
public int read(@NotNull byte[] b, int off, int len) throws IOException {
96+
return delegate.read(b, off, len);
97+
}
98+
99+
@Override
100+
public long skip(long n) throws IOException {
101+
return delegate.skip(n);
102+
}
103+
104+
@Override
105+
public int available() throws IOException {
106+
return delegate.available();
107+
}
108+
109+
@Override
110+
public synchronized void mark(int readlimit) {
111+
delegate.mark(readlimit);
112+
}
113+
114+
@Override
115+
public synchronized void reset() throws IOException {
116+
delegate.reset();
117+
}
118+
119+
@Override
120+
public boolean markSupported() {
121+
return delegate.markSupported();
122+
}
123+
}

0 commit comments

Comments
 (0)