From 5c88451f6fe8d1ab5f7ac97b7c341eb505cbe911 Mon Sep 17 00:00:00 2001 From: tterrag Date: Thu, 5 Dec 2019 21:50:33 -0500 Subject: [PATCH] Allow classpath locator to discover jars via FMLModType attribute --- .../fml/loading/moddiscovery/ModFile.java | 2 +- .../userdev/ClasspathLocator.java | 40 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java index defe1005e..122c4c75a 100644 --- a/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java @@ -92,7 +92,7 @@ public class ModFile implements IModFile { private List coreMods; private Path accessTransformer; - private static final Attributes.Name TYPE = new Attributes.Name("FMLModType"); + public static final Attributes.Name TYPE = new Attributes.Name("FMLModType"); public ModFile(final Path file, final IModLocator locator) { this.locator = locator; diff --git a/src/userdev/java/net/minecraftforge/userdev/ClasspathLocator.java b/src/userdev/java/net/minecraftforge/userdev/ClasspathLocator.java index 2c7f2e74c..824ab8a3c 100644 --- a/src/userdev/java/net/minecraftforge/userdev/ClasspathLocator.java +++ b/src/userdev/java/net/minecraftforge/userdev/ClasspathLocator.java @@ -25,24 +25,29 @@ import java.io.IOException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Enumeration; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.function.Predicate; import java.util.stream.Collectors; -import net.minecraftforge.forgespi.locating.IModFile; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import com.google.common.base.Predicates; + import net.minecraftforge.fml.loading.LibraryFinder; import net.minecraftforge.fml.loading.moddiscovery.AbstractJarFileLocator; import net.minecraftforge.fml.loading.moddiscovery.ModFile; +import net.minecraftforge.forgespi.locating.IModFile; public class ClasspathLocator extends AbstractJarFileLocator { private static final Logger LOGGER = LogManager.getLogger(); private static final String MODS_TOML = "META-INF/mods.toml"; - private List modCoords; + private static final String MANIFEST = "META-INF/MANIFEST.MF"; + private Set modCoords; @Override public List scanMods() { @@ -60,20 +65,27 @@ public class ClasspathLocator extends AbstractJarFileLocator { @Override public void initArguments(Map arguments) { try { - modCoords = new ArrayList<>(); - final Enumeration resources = ClassLoader.getSystemClassLoader().getResources(MODS_TOML); - while (resources.hasMoreElements()) { - URL url = resources.nextElement(); - Path path = LibraryFinder.findJarPathFor(MODS_TOML, "classpath_mod", url); - if (Files.isDirectory(path)) - continue; - - LOGGER.debug(CORE, "Found classpath mod: {}", path); - this.modCoords.add(path); - } + modCoords = new LinkedHashSet<>(); + locateMods(MODS_TOML, "classpath_mod", Predicates.alwaysTrue()); + locateMods(MANIFEST, "manifest_jar", path -> findManifest(path).map(m -> m.getMainAttributes().getValue(ModFile.TYPE)).isPresent()); } catch (IOException e) { LOGGER.fatal(CORE,"Error trying to find resources", e); throw new RuntimeException("wha?", e); } } + + private void locateMods(String resource, String name, Predicate filter) throws IOException { + final Enumeration modsTomls = ClassLoader.getSystemClassLoader().getResources(resource); + while (modsTomls.hasMoreElements()) { + URL url = modsTomls.nextElement(); + Path path = LibraryFinder.findJarPathFor(resource, name, url); + if (Files.isDirectory(path)) + continue; + + if (filter.test(path)) { + LOGGER.debug(CORE, "Found classpath mod: {}", path); + this.modCoords.add(path); + } + } + } }