From fc59510c0a7b7fbd1e1cbeb0b9c88ba6617d04a4 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:14:07 -0700 Subject: [PATCH] Update for NeoForge binpatcher fork (#250) --- .../forge/MinecraftPatchedProvider.java | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java index e2b684bdd..28ec5ac6d 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java @@ -56,6 +56,7 @@ import dev.architectury.loom.util.MappingOption; import dev.architectury.loom.util.TempFiles; import org.gradle.api.Project; +import org.gradle.api.file.FileCollection; import org.gradle.api.logging.LogLevel; import org.gradle.api.logging.Logger; import org.objectweb.asm.ClassReader; @@ -464,8 +465,9 @@ private void patchJars() throws Exception { private void patchJars(Path clean, Path output, Path patches) { ForgeToolExecutor.exec(project, spec -> { UserdevConfig.BinaryPatcherConfig config = getExtension().getForgeUserdevProvider().getConfig().binpatcher(); - spec.classpath(DependencyDownloader.download(project, config.dependency())); - spec.getMainClass().set("net.minecraftforge.binarypatcher.ConsoleTool"); + final FileCollection download = DependencyDownloader.download(project, config.dependency()); + spec.classpath(download); + spec.getMainClass().set(getMainClass(download)); for (String arg : config.args()) { String actual = switch (arg) { @@ -479,6 +481,45 @@ private void patchJars(Path clean, Path output, Path patches) { }); } + private static String getMainClass(final Iterable files) { + String mainClass = null; + IOException ex = null; + + for (File file : files) { + if (file.getName().endsWith(".jar")) { + try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(file.toPath())) { + final Path mfPath = fs.getPath("META-INF/MANIFEST.MF"); + + if (Files.exists(mfPath)) { + try (InputStream in = Files.newInputStream(mfPath)) { + mainClass = new Manifest(in).getMainAttributes().getValue("Main-Class"); + } + } + } catch (final IOException e) { + if (ex == null) { + ex = e; + } else { + ex.addSuppressed(e); + } + } + + if (mainClass != null) { + break; + } + } + } + + if (mainClass == null) { + if (ex != null) { + throw new UncheckedIOException(ex); + } else { + throw new RuntimeException("Failed to find main class"); + } + } + + return mainClass; + } + private void walkFileSystems(Path source, Path target, Predicate filter, Function> toWalk, FsPathConsumer action) throws IOException { try (FileSystemUtil.Delegate sourceFs = FileSystemUtil.getJarFileSystem(source, false);