Skip to content

Commit cb8b262

Browse files
committed
Make empty launcher runs when dependency doesn't specify any. Closes #1049
1 parent a709ae4 commit cb8b262

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

src/main/java/net/minecraftforge/gradle/internal/SlimeLauncherEclipseConfiguration.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ abstract class SlimeLauncherEclipseConfiguration extends DefaultTask implements
8080

8181
protected abstract @InputFiles ConfigurableFileCollection getMetadata();
8282

83-
protected abstract @InputFile RegularFileProperty getRunsJson();
83+
protected abstract @InputFile @Optional RegularFileProperty getRunsJson();
8484

8585
protected abstract @Inject ObjectFactory getObjects();
8686

@@ -118,13 +118,16 @@ protected void exec() {
118118

119119
//region Launcher Metadata Inheritance
120120
Map<String, RunConfig> configs = Map.of();
121-
try {
122-
configs = JsonData.fromJson(
123-
this.getRunsJson().getAsFile().get(),
124-
new TypeToken<>() { }
125-
);
126-
} catch (JsonIOException e) {
127-
// continue
121+
var jsons = this.getRunsJson().getAsFile().getOrNull();
122+
if (jsons != null && jsons.exists()) {
123+
try {
124+
configs = JsonData.fromJson(
125+
this.getRunsJson().getAsFile().get(),
126+
new TypeToken<>() { }
127+
);
128+
} catch (JsonIOException e) {
129+
// continue
130+
}
128131
}
129132

130133
var options = ((SlimeLauncherOptionsInternal) this.getOptions().get()).inherit(configs, this.getSourceSetName().get());

src/main/java/net/minecraftforge/gradle/internal/SlimeLauncherExec.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
import org.gradle.api.artifacts.ModuleIdentifier;
1515
import org.gradle.api.attributes.Usage;
1616
import org.gradle.api.file.ConfigurableFileCollection;
17-
import org.gradle.api.file.Directory;
1817
import org.gradle.api.file.DirectoryProperty;
19-
import org.gradle.api.file.DirectoryTree;
2018
import org.gradle.api.file.RegularFileProperty;
2119
import org.gradle.api.provider.MapProperty;
2220
import org.gradle.api.provider.Property;
@@ -41,7 +39,6 @@
4139
import java.io.IOException;
4240
import java.nio.file.Files;
4341
import java.util.ArrayList;
44-
import java.util.Collections;
4542
import java.util.Comparator;
4643
import java.util.HashMap;
4744
import java.util.HashSet;
@@ -160,7 +157,7 @@ static TaskProvider<SlimeLauncherExec> register(Project project, SourceSet sourc
160157

161158
protected abstract @InputFiles ConfigurableFileCollection getMetadata();
162159

163-
protected abstract @InputFile RegularFileProperty getRunsJson();
160+
protected abstract @InputFile @Optional RegularFileProperty getRunsJson();
164161

165162
protected abstract @Input @Optional Property<Boolean> getClient();
166163

@@ -193,13 +190,16 @@ public void exec() {
193190

194191
//region Launcher Metadata Inheritance
195192
Map<String, RunConfig> configs = Map.of();
196-
try {
197-
configs = JsonData.fromJson(
198-
this.getRunsJson().getAsFile().get(),
199-
new TypeToken<>() { }
200-
);
201-
} catch (JsonIOException e) {
202-
// continue
193+
var jsons = this.getRunsJson().getAsFile().getOrNull();
194+
if (jsons != null && jsons.exists()) {
195+
try {
196+
configs = JsonData.fromJson(
197+
this.getRunsJson().getAsFile().get(),
198+
new TypeToken<>() { }
199+
);
200+
} catch (JsonIOException e) {
201+
// continue
202+
}
203203
}
204204

205205
var options = ((SlimeLauncherOptionsInternal) this.getOptions().get()).inherit(configs, this.getSourceSetName().get());

src/main/java/net/minecraftforge/gradle/internal/SlimeLauncherMetadata.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,45 @@
1313
import org.gradle.api.tasks.TaskAction;
1414

1515
import javax.inject.Inject;
16+
import java.io.FileInputStream;
17+
import java.io.FileNotFoundException;
1618
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
1720
import java.nio.file.Files;
1821
import java.nio.file.StandardCopyOption;
22+
import java.util.zip.ZipEntry;
23+
import java.util.zip.ZipInputStream;
1924

2025
abstract class SlimeLauncherMetadata extends DefaultTask implements ForgeGradleTask {
2126
protected abstract @InputFiles ConfigurableFileCollection getMetadata();
2227

2328
protected abstract @OutputFile RegularFileProperty getRunsJson();
2429

25-
protected abstract @Inject ArchiveOperations getArchiveOperations();
26-
2730
@Inject
2831
public SlimeLauncherMetadata() {
2932
this.getRunsJson().convention(this.getDefaultOutputDirectory().map(d -> d.file("runs.json")));
3033
}
3134

3235
@TaskAction
33-
protected void exec() {
34-
this.getArchiveOperations().zipTree(this.getMetadata().getSingleFile())
35-
.matching(it -> it.include("launcher/**"))
36-
.visit(file -> {
37-
try {
38-
if (file.getPath().equals("launcher/runs.json")) {
39-
Files.copy(
40-
file.getFile().toPath(),
41-
this.getRunsJson().getAsFile().get().toPath(),
42-
StandardCopyOption.REPLACE_EXISTING,
43-
StandardCopyOption.COPY_ATTRIBUTES
44-
);
45-
}
46-
} catch (IOException e) {
47-
throw new RuntimeException(e);
36+
protected void exec() throws IOException {
37+
var archive = this.getMetadata().getSingleFile();
38+
var json = this.getRunsJson().getAsFile().get().toPath();
39+
40+
boolean foundRuns = false;
41+
try (var zin = new ZipInputStream(new FileInputStream(archive))) {
42+
for (ZipEntry entry; ((entry = zin.getNextEntry()) != null); ) {
43+
if (!entry.getName().startsWith("launcher/"))
44+
continue;
45+
if (entry.getName().equals("launcher/runs.json")) {
46+
Files.copy(zin, json, StandardCopyOption.REPLACE_EXISTING);
47+
foundRuns = true;
4848
}
49-
});
49+
}
50+
}
51+
52+
// If we don't find a metadata file, write an empty runs
53+
// This happens when using a 'vanilla' minecraft dependency
54+
if (!foundRuns)
55+
Files.writeString(json, "{}", StandardCharsets.UTF_8);
5056
}
5157
}

0 commit comments

Comments
 (0)