Skip to content

Commit 07001e2

Browse files
committed
Add generic variant info to non-mcp net.minecraft artifacts
1 parent 1d3071b commit 07001e2

5 files changed

Lines changed: 124 additions & 96 deletions

File tree

src/main/java/net/minecraftforge/mcmaven/impl/repo/Repo.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,41 @@ protected GradleModule.Variant[] classVariants(Mappings mappings, MCPSide side)
126126
}
127127

128128
// Classes needs a variant for each OS type so that we can have different natives
129-
protected GradleModule.Variant[] classVariants(Mappings mappings, MCPSide side, Collection<Artifact> extraDeps, Collection<Artifact> extraCompileDeps, Collection<Artifact> extraRuntimeDeps) {
129+
protected GradleModule.Variant[] classVariants(
130+
Mappings mappings,
131+
MCPSide side,
132+
Collection<Artifact> extraDeps,
133+
Collection<Artifact> extraCompileDeps,
134+
Collection<Artifact> extraRuntimeDeps
135+
) {
136+
var deps = new ArrayList<Artifact>();
137+
deps.addAll(side.getMCLibraries());
138+
deps.addAll(side.getMCPConfigLibraries());
139+
deps.addAll(extraDeps);
140+
141+
// ExtraRuntimeDeps is never used.... Not sure where it was meant to go
142+
143+
return classVariants(
144+
mappings,
145+
side.getMCP().getMinecraftTasks().versionJson,
146+
deps,
147+
extraCompileDeps
148+
);
149+
}
150+
151+
protected GradleModule.Variant[] classVariants(
152+
Mappings mappings,
153+
Task jsonTask,
154+
Collection<Artifact> deps,
155+
Collection<Artifact> extraCompileDeps
156+
) {
130157
var all = new ArrayList<Artifact>();
131158
var natives = new HashMap<GradleAttributes.OperatingSystemFamily, List<Artifact>>();
132159

133-
for (var artifact : side.getMCLibraries()) {
160+
for (var artifact : deps) {
161+
if (artifact == null)
162+
continue;
163+
134164
var osVariants = EnumSet.noneOf(GradleAttributes.OperatingSystemFamily.class);
135165
for (var os : artifact.getOs()) {
136166
var variant = GradleAttributes.OperatingSystemFamily.from(os);
@@ -147,17 +177,8 @@ protected GradleModule.Variant[] classVariants(Mappings mappings, MCPSide side,
147177
}
148178
}
149179

150-
all.addAll(side.getMCPConfigLibraries());
151-
152-
for (var extra : extraDeps) {
153-
if (extra != null)
154-
all.add(extra);
155-
}
156-
157-
var java = Util.replace(
158-
JsonData.minecraftVersion(side.getMCP().getMinecraftTasks().versionJson.execute()),
159-
v -> v.javaVersion != null ? v.javaVersion.majorVersion : null
160-
);
180+
var json = JsonData.minecraftVersion(jsonTask.execute());
181+
var java = json.javaVersion != null ? json.javaVersion.majorVersion : null;
161182

162183
Consumer<GradleModule.Variant> common = v -> {
163184
v.attribute("org.gradle.status", "release")

src/main/java/net/minecraftforge/mcmaven/impl/repo/mcpconfig/MCPConfigRepo.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
import net.minecraftforge.mcmaven.impl.tasks.RecompileTask;
1111
import net.minecraftforge.mcmaven.impl.tasks.RenameTask;
1212
import net.minecraftforge.mcmaven.impl.cache.Cache;
13+
import net.minecraftforge.mcmaven.impl.data.GradleModule;
1314
import net.minecraftforge.mcmaven.impl.mappings.Mappings;
1415
import net.minecraftforge.mcmaven.impl.util.Artifact;
1516
import net.minecraftforge.mcmaven.impl.util.ComparableVersion;
1617
import net.minecraftforge.mcmaven.impl.util.Constants;
1718
import net.minecraftforge.mcmaven.impl.util.POMBuilder;
1819
import net.minecraftforge.mcmaven.impl.util.Task;
1920
import net.minecraftforge.mcmaven.impl.util.Util;
21+
import net.minecraftforge.util.data.json.JsonData;
2022
import net.minecraftforge.util.download.DownloadUtils;
2123
import net.minecraftforge.util.file.FileUtils;
2224

@@ -34,6 +36,7 @@
3436
import java.util.List;
3537
import java.util.Map;
3638
import java.util.function.Supplier;
39+
import java.util.jar.JarFile;
3740

3841
/*
3942
* Provides the following artifacts:
@@ -217,11 +220,11 @@ public List<PendingArtifact> processWithoutMcp(Artifact artifact, Mappings mappi
217220
if (!isObfuscated(artifact.getVersion())) {
218221
if ("client".equals(artifact.getName())) {
219222
var pom = pending("Maven POM", tasks.clientPom(), artifact.withExtension("pom"), false);
220-
var jar = pending("Official Jar", tasks.versionFile(MCFile.CLIENT_JAR), artifact, false);
223+
var jar = pending("Official Jar", tasks.versionFile(MCFile.CLIENT_JAR), artifact, false, () -> classVariantsClient(mappings, tasks));
221224
return List.of(metadata, pom, jar);
222225
} else if ("server".equals(artifact.getName())) {
223226
var pom = pending("Maven POM", tasks.serverPom(), artifact.withExtension("pom"), false);
224-
var jar = pending("Official Jar", tasks.extractServer(), artifact, false);
227+
var jar = pending("Official Jar", tasks.extractServer(), artifact, false, () -> classVariantsServer(mappings, tasks));
225228
return List.of(metadata, pom, jar);
226229
}
227230
throw new IllegalArgumentException("MCPConfigRepo does not support artifact: " + artifact);
@@ -241,11 +244,11 @@ public List<PendingArtifact> processWithoutMcp(Artifact artifact, Mappings mappi
241244
return List.of(metadata, mapPom, m2o);
242245
} else if ("client".equals(artifact.getName())) {
243246
var pom = pending("Maven POM", tasks.clientPom(), artifact.withExtension("pom"), false);
244-
var jar = pending("Official Jar", tasks.renameClient(), artifact, false);
247+
var jar = pending("Official Jar", tasks.renameClient(), artifact, false, () -> classVariantsClient(mappings, tasks));
245248
return List.of(metadata, mapPom, m2o, pom, jar);
246249
} else if ("server".equals(artifact.getName())) {
247250
var pom = pending("Maven POM", tasks.serverPom(), artifact.withExtension("pom"), false);
248-
var jar = pending("Official Jar", tasks.renameServer(), artifact, false);
251+
var jar = pending("Official Jar", tasks.renameServer(), artifact, false, () -> classVariantsServer(mappings, tasks));
249252
return List.of(metadata, mapPom, m2o, pom, jar);
250253
} else {
251254
throw new IllegalArgumentException("MCPConfigRepo does not support artifact: " + artifact);
@@ -387,4 +390,29 @@ private static Task pomExtra(File build, String side, String version) {
387390
return output;
388391
});
389392
}
393+
394+
private GradleModule.Variant[] classVariantsClient(Mappings mappings, MinecraftTasks tasks) {
395+
var deps = new ArrayList<Artifact>();
396+
var json = JsonData.minecraftVersion(tasks.versionJson.execute());
397+
for (var lib : json.getLibs())
398+
deps.add(Artifact.from(lib.coord).withOS(lib.os));
399+
return classVariants(mappings, tasks.versionJson, deps, List.of());
400+
}
401+
402+
private GradleModule.Variant[] classVariantsServer(Mappings mappings, MinecraftTasks tasks) {
403+
var deps = new ArrayList<Artifact>();
404+
var serverJar = tasks.versionFile(MCFile.SERVER_JAR).execute();
405+
try (var jar = new JarFile(serverJar)) {
406+
var format = jar.getManifest().getMainAttributes().getValue("Bundler-Format");
407+
if (format != null) {
408+
var list = Util.readBundle(serverJar, jar, "libraries");
409+
for (var lib : list)
410+
deps.add(Artifact.from(lib.name()));
411+
}
412+
// TODO: [Mavenizer] Currently non-bundled server jars are not supported for non-mcpconfig setups
413+
} catch (IOException e) {
414+
return Util.sneak(e);
415+
}
416+
return classVariants(mappings, tasks.versionJson, deps, List.of());
417+
}
390418
}

src/main/java/net/minecraftforge/mcmaven/impl/repo/mcpconfig/MCPTaskFactory.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
*/
55
package net.minecraftforge.mcmaven.impl.repo.mcpconfig;
66

7-
import java.io.BufferedReader;
87
import java.io.File;
98
import java.io.FileInputStream;
109
import java.io.FileOutputStream;
1110
import java.io.IOException;
12-
import java.io.InputStreamReader;
1311
import java.io.Serializable;
1412
import java.nio.charset.StandardCharsets;
1513
import java.nio.file.Files;
@@ -713,33 +711,17 @@ private File listLibrariesBundle(Task bundleTask, File libraries, File output) {
713711
var bundle = bundleTask.execute();
714712

715713
try (var jar = new JarFile(bundle)) {
716-
var format = jar.getManifest().getMainAttributes().getValue("Bundler-Format");
717-
if (format == null)
718-
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing format entry from manifest");
719-
720-
if (!"1.0".equals(format))
721-
throw new RuntimeException("Invalid bundle: `" + bundle + "` - Unsupported format " + format);
722-
723-
var entry = jar.getEntry("META-INF/libraries.list");
724-
if (entry == null)
725-
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing META-INF/libraries.list");
714+
var list = Util.readBundle(bundle, jar, "libraries");
726715

727716
record LibLine(String hash, Artifact artifact, String path) implements Comparable<LibLine> {
728717
@Override
729718
public int compareTo(LibLine o) {
730719
return Util.compare(this.artifact, o.artifact);
731720
}
732721
}
733-
var libs = new TreeSet<LibLine>();
734-
735-
var reader = new BufferedReader(new InputStreamReader(jar.getInputStream(entry)));
736-
String line;
737-
while ((line = reader.readLine()) != null) {
738-
var pts = line.split("\t");
739-
if (pts.length < 3)
740-
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Invalid line: " + line);
741-
libs.add(new LibLine(pts[0], Artifact.from(pts[1]), pts[2]));
742-
}
722+
var libs = new ArrayList<LibLine>(list.size());
723+
for (var lib : list)
724+
libs.add(new LibLine(lib.hash(), Artifact.from(lib.name()), lib.path()));
743725

744726
var cache = Util.cache(output)
745727
.add(bundle);
@@ -762,9 +744,9 @@ public int compareTo(LibLine o) {
762744

763745
downloadedLibs.add(new Lib(artifact, target));
764746
if (!target.exists()) {
765-
entry = jar.getEntry("META-INF/libraries/" + lib.path());
747+
var entry = jar.getEntry("META-INF/libraries/" + lib.path());
766748
if (entry == null)
767-
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing META-INF/libraries/" + lib);
749+
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing META-INF/libraries/" + lib.path());
768750

769751
FileUtils.ensureParent(target);
770752

src/main/java/net/minecraftforge/mcmaven/impl/repo/mcpconfig/MinecraftTasks.java

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
*/
55
package net.minecraftforge.mcmaven.impl.repo.mcpconfig;
66

7-
import java.io.BufferedReader;
87
import java.io.File;
98
import java.io.FileOutputStream;
109
import java.io.IOException;
11-
import java.io.InputStreamReader;
1210
import java.nio.charset.StandardCharsets;
1311
import java.nio.file.Files;
1412
import java.nio.file.StandardCopyOption;
15-
import java.util.ArrayList;
1613
import java.util.Collections;
1714
import java.util.HashMap;
1815
import java.util.List;
@@ -310,7 +307,7 @@ private File serverPomImpl() {
310307
if (Mavenizer.checkCache(output, cache))
311308
return output;
312309

313-
var libs = listBundleArtifacts(jarFile);
310+
var libs = Util.listBundleArtifacts(jarFile);
314311
var builder = new POMBuilder("net.minecraft", "server", version)
315312
.preferGradleModule()
316313
.dependencies(deps -> libs.forEach(deps::add));
@@ -338,7 +335,6 @@ public Task extractServer() {
338335
return this.extractServer;
339336
}
340337

341-
private record BundleEntry(String sha, String version, String path) {}
342338
private File extractServerImpl() {
343339
var output = new File(this.cacheRoot, "server-extracted.jar");
344340
var bundle = versionFile(MCFile.SERVER_JAR).execute().getAbsoluteFile();
@@ -349,33 +345,14 @@ private File extractServerImpl() {
349345
return output;
350346

351347
try (var jar = new JarFile(bundle)) {
352-
var format = jar.getManifest().getMainAttributes().getValue("Bundler-Format");
353-
if (format == null) // This is not a bundled jar file, but we expected it to be, so throw an exception
354-
throw new RuntimeException("Server jar missing Bundler-Format: " + bundle);
355-
356-
if (!"1.0".equals(format))
357-
throw new RuntimeException("Invalid bundle: `" + bundle + "` - Unsupported format " + format);
358-
359-
var entry = jar.getEntry("META-INF/versions.list");
360-
if (entry == null)
361-
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing META-INF/versions.list");
362-
363-
var entries = new ArrayList<BundleEntry>();
364-
var reader = new BufferedReader(new InputStreamReader(jar.getInputStream(entry)));
365-
String line;
366-
while ((line = reader.readLine()) != null) {
367-
var pts = line.split("\t");
368-
if (pts.length < 3)
369-
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Invalid line: " + line);
370-
entries.add(new BundleEntry(pts[0], pts[1], pts[2]));
371-
}
348+
var entries = Util.readBundle(bundle, jar, "versions");
372349

373350
if (entries.size() != 1)
374351
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Expected 1 entry in versions.list found " + entries.size());
375352

376353
Files.createDirectories(output.getParentFile().toPath());
377354

378-
var path = "META-INF/versions/" + entries.getFirst().path;
355+
var path = "META-INF/versions/" + entries.getFirst().path();
379356
var jarEntry = jar.getEntry(path);
380357
if (jarEntry == null)
381358
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing " + path);
@@ -388,34 +365,5 @@ private File extractServerImpl() {
388365
} catch (IOException e) {
389366
return Util.sneak(e);
390367
}
391-
392-
}
393-
394-
private static List<Artifact> listBundleArtifacts(File bundle) {
395-
try (var jar = new JarFile(bundle)) {
396-
var format = jar.getManifest().getMainAttributes().getValue("Bundler-Format");
397-
if (format == null) // If this is not a bundle then it's the old 'fatjar' and already contains all deps
398-
return List.of();
399-
400-
if (!"1.0".equals(format))
401-
throw new RuntimeException("Invalid bundle: `" + bundle + "` - Unsupported format " + format);
402-
403-
var entry = jar.getEntry("META-INF/libraries.list");
404-
if (entry == null)
405-
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing META-INF/libraries.list");
406-
407-
var ret = new ArrayList<Artifact>();
408-
var reader = new BufferedReader(new InputStreamReader(jar.getInputStream(entry)));
409-
String line;
410-
while ((line = reader.readLine()) != null) {
411-
var pts = line.split("\t");
412-
if (pts.length < 3)
413-
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Invalid line: " + line);
414-
ret.add(Artifact.from(pts[1]));
415-
}
416-
return ret;
417-
} catch (IOException e) {
418-
return Util.sneak(e);
419-
}
420368
}
421369
}

src/main/java/net/minecraftforge/mcmaven/impl/util/Util.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
*/
55
package net.minecraftforge.mcmaven.impl.util;
66

7+
import java.io.BufferedReader;
78
import java.io.File;
89
import java.io.IOException;
10+
import java.io.InputStreamReader;
911
import java.util.ArrayList;
12+
import java.util.List;
1013
import java.util.TimeZone;
1114
import java.util.concurrent.Callable;
1215
import java.util.function.Consumer;
1316
import java.util.function.Function;
1417
import java.util.function.Supplier;
18+
import java.util.jar.JarFile;
1519
import java.util.stream.Stream;
1620
import java.util.zip.ZipEntry;
1721

@@ -153,6 +157,51 @@ public static boolean attemptCleanupDirectory(File path) {
153157
return success;
154158
}
155159

160+
public record BundleEntry(String hash, String name, String path) implements Comparable<BundleEntry> {
161+
@Override
162+
public int compareTo(BundleEntry o) {
163+
return compare(this.name, o.name);
164+
}
165+
}
166+
public static List<BundleEntry> readBundle(File bundle, JarFile jar, String list) throws IOException {
167+
var format = jar.getManifest().getMainAttributes().getValue("Bundler-Format");
168+
if (format == null)
169+
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing format entry from manifest");
170+
171+
if (!"1.0".equals(format))
172+
throw new RuntimeException("Invalid bundle: `" + bundle + "` - Unsupported format " + format);
173+
174+
var path = "META-INF/" + list + ".list";
175+
var entry = jar.getEntry(path);
176+
if (entry == null)
177+
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Missing " + path);
178+
179+
var libs = new ArrayList<BundleEntry>();
180+
var reader = new BufferedReader(new InputStreamReader(jar.getInputStream(entry)));
181+
String line;
182+
while ((line = reader.readLine()) != null) {
183+
var pts = line.split("\t");
184+
if (pts.length < 3)
185+
throw new IllegalStateException("Invalid bundle: `" + bundle + "` - Invalid line: " + line);
186+
libs.add(new BundleEntry(pts[0], pts[1], pts[2]));
187+
}
188+
189+
return libs;
190+
}
191+
192+
public static List<Artifact> listBundleArtifacts(File bundle) {
193+
try (var jar = new JarFile(bundle)) {
194+
var list = Util.readBundle(bundle, jar, "libraries");
195+
var ret = new ArrayList<Artifact>(list.size());
196+
for (var lib : list)
197+
ret.add(Artifact.from(lib.name()));
198+
return ret;
199+
} catch (IOException e) {
200+
return Util.sneak(e);
201+
}
202+
}
203+
204+
156205
public static HashStore cache(File file) {
157206
return HashStore.fromFile(file)
158207
.invalidate(Mavenizer.ignoreCache())

0 commit comments

Comments
 (0)