From 0c5243a8d137b817a92f2fb25427631d9381ef85 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Sat, 24 Feb 2024 00:14:05 +0000 Subject: [PATCH] Respond to PR feedback - Throws if the SERVICE layer was not built instead of using a default layer - Extracts the discoverer service type constants to a new shared class and adds the ImmediateWindowProvider to this list. --- .../ClasspathTransformerDiscoverer.java | 5 ++-- .../fml/loading/ImmediateWindowHandler.java | 2 +- .../loading/ModDirTransformerDiscoverer.java | 8 +----- .../TransformerDiscovererConstants.java | 26 +++++++++++++++++++ 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 loader/src/main/java/net/neoforged/fml/loading/TransformerDiscovererConstants.java diff --git a/loader/src/main/java/net/neoforged/fml/loading/ClasspathTransformerDiscoverer.java b/loader/src/main/java/net/neoforged/fml/loading/ClasspathTransformerDiscoverer.java index 3d509748e..501959687 100644 --- a/loader/src/main/java/net/neoforged/fml/loading/ClasspathTransformerDiscoverer.java +++ b/loader/src/main/java/net/neoforged/fml/loading/ClasspathTransformerDiscoverer.java @@ -41,8 +41,9 @@ public static List allExcluded() { private void scan(final Path gameDirectory) { try { - locateTransformers("META-INF/services/cpw.mods.modlauncher.api.ITransformationService"); - locateTransformers("META-INF/services/net.neoforged.neoforgespi.locating.IModLocator"); + for (final String serviceClass : TransformerDiscovererConstants.SERVICES) { + locateTransformers("META-INF/services/" + serviceClass); + } } catch (IOException e) { LogManager.getLogger().error("Error during discovery of transform services from the classpath", e); } diff --git a/loader/src/main/java/net/neoforged/fml/loading/ImmediateWindowHandler.java b/loader/src/main/java/net/neoforged/fml/loading/ImmediateWindowHandler.java index a806f121e..75698b5c1 100644 --- a/loader/src/main/java/net/neoforged/fml/loading/ImmediateWindowHandler.java +++ b/loader/src/main/java/net/neoforged/fml/loading/ImmediateWindowHandler.java @@ -36,7 +36,7 @@ public static void load(final String launchTarget, final String[] arguments) { LOGGER.info("Loading ImmediateWindowProvider {}", providername); final var layer = Launcher.INSTANCE.findLayerManager() .flatMap(manager -> manager.getLayer(Layer.SERVICE)) - .orElse(ImmediateWindowHandler.class.getModule().getLayer()); // This will likely be the BOOT layer, but is a sensible fallback. + .orElseThrow(() -> new IllegalStateException("Couldn't find SERVICE layer to load immediate window handler")); final var maybeProvider = ServiceLoader.load(layer, ImmediateWindowProvider.class) .stream() .map(ServiceLoader.Provider::get) diff --git a/loader/src/main/java/net/neoforged/fml/loading/ModDirTransformerDiscoverer.java b/loader/src/main/java/net/neoforged/fml/loading/ModDirTransformerDiscoverer.java index 0609959d9..1d45de629 100644 --- a/loader/src/main/java/net/neoforged/fml/loading/ModDirTransformerDiscoverer.java +++ b/loader/src/main/java/net/neoforged/fml/loading/ModDirTransformerDiscoverer.java @@ -22,15 +22,9 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; -import java.util.Set; public class ModDirTransformerDiscoverer implements ITransformerDiscoveryService { private static final Logger LOGGER = LogUtils.getLogger(); - private static final Set SERVICES = Set.of( - "cpw.mods.modlauncher.api.ITransformationService", - "net.neoforged.neoforgespi.locating.IModLocator", - "net.neoforged.neoforgespi.locating.IDependencyLocator" - ); private UncheckedIOException alreadyFailed; @Override @@ -102,6 +96,6 @@ private static boolean shouldLoadInServiceLayer(Path path) { JarMetadata metadata = JarMetadata.from(new JarContentsBuilder().paths(path).build()); return metadata.providers().stream() .map(SecureJar.Provider::serviceName) - .anyMatch(SERVICES::contains); + .anyMatch(TransformerDiscovererConstants.SERVICES::contains); } } diff --git a/loader/src/main/java/net/neoforged/fml/loading/TransformerDiscovererConstants.java b/loader/src/main/java/net/neoforged/fml/loading/TransformerDiscovererConstants.java new file mode 100644 index 000000000..776d2ff99 --- /dev/null +++ b/loader/src/main/java/net/neoforged/fml/loading/TransformerDiscovererConstants.java @@ -0,0 +1,26 @@ +package net.neoforged.fml.loading; + + +import cpw.mods.modlauncher.api.IModuleLayerManager.Layer; +import cpw.mods.modlauncher.serviceapi.ITransformerDiscoveryService; + +import java.util.Set; + +/** + * Defines a class containing constants which implementations of {@link ITransformerDiscoveryService} + * may use. + */ +public class TransformerDiscovererConstants { + private TransformerDiscovererConstants() { } + + /** + * Defines the set of FML service types which should be loaded on the + * {@link Layer#SERVICE} module layer. + */ + public static final Set SERVICES = Set.of( + "cpw.mods.modlauncher.api.ITransformationService", + "net.neoforged.neoforgespi.locating.IModLocator", + "net.neoforged.neoforgespi.locating.IDependencyLocator", + "net.neoforged.fml.loading.ImmediateWindowProvider" + ); +}