Skip to content

Commit

Permalink
Add support for locating mod sources with services from the classpath. (
Browse files Browse the repository at this point in the history
#93)

Co-authored-by: Technici4n <[email protected]>
  • Loading branch information
marchermans and Technici4n authored Mar 25, 2024
1 parent 76df451 commit 072d112
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import cpw.mods.modlauncher.api.NamedPath;
import cpw.mods.modlauncher.serviceapi.ITransformerDiscoveryService;
import net.neoforged.fml.loading.targets.CommonLaunchHandler;
import org.apache.logging.log4j.LogManager;

import java.io.File;
Expand All @@ -16,6 +17,8 @@
import java.nio.file.Path;
import java.util.*;

import static net.neoforged.fml.loading.TransformerDiscovererConstants.shouldLoadInServiceLayer;

public class ClasspathTransformerDiscoverer implements ITransformerDiscoveryService {

private final List<Path> legacyClasspath = Arrays.stream(System.getProperty("legacyClassPath", "").split(File.pathSeparator)).map(Path::of).toList();
Expand Down Expand Up @@ -44,6 +47,8 @@ private void scan(final Path gameDirectory) {
for (final String serviceClass : TransformerDiscovererConstants.SERVICES) {
locateTransformers("META-INF/services/" + serviceClass);
}

scanModClasses();
} catch (IOException e) {
LogManager.getLogger().error("Error during discovery of transform services from the classpath", e);
}
Expand All @@ -59,4 +64,14 @@ private void locateTransformers(String resource) throws IOException {
found.add(new NamedPath(path.toUri().toString(), path));
}
}

private void scanModClasses() {
final Map<String, List<Path>> modClassPaths = CommonLaunchHandler.getModClasses();
modClassPaths.forEach((modid, paths) -> {
if (shouldLoadInServiceLayer(paths.toArray(Path[]::new))) {
found.add(new NamedPath(modid, paths.toArray(Path[]::new)));
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
package net.neoforged.fml.loading;

import com.mojang.logging.LogUtils;
import cpw.mods.jarhandling.JarContentsBuilder;
import cpw.mods.jarhandling.JarMetadata;
import cpw.mods.jarhandling.SecureJar;
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
import cpw.mods.modlauncher.api.NamedPath;
import cpw.mods.modlauncher.serviceapi.ITransformerDiscoveryService;
Expand Down Expand Up @@ -93,9 +90,6 @@ private static boolean shouldLoadInServiceLayer(Path path) {
if (!path.toString().endsWith(".jar")) return false;
if (LamdbaExceptionUtils.uncheck(() -> Files.size(path)) == 0) return false;

JarMetadata metadata = JarMetadata.from(new JarContentsBuilder().paths(path).build());
return metadata.providers().stream()
.map(SecureJar.Provider::serviceName)
.anyMatch(TransformerDiscovererConstants.SERVICES::contains);
return TransformerDiscovererConstants.shouldLoadInServiceLayer(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

package net.neoforged.fml.loading;

import cpw.mods.jarhandling.JarContentsBuilder;
import cpw.mods.jarhandling.JarMetadata;
import cpw.mods.jarhandling.SecureJar;
import cpw.mods.modlauncher.api.IModuleLayerManager.Layer;
import cpw.mods.modlauncher.serviceapi.ITransformerDiscoveryService;

import java.nio.file.Path;
import java.util.Set;

/**
* Defines a class containing constants which implementations of {@link ITransformerDiscoveryService}
* may use.
*/
public class TransformerDiscovererConstants {
public final class TransformerDiscovererConstants {
private TransformerDiscovererConstants() { }

/**
Expand All @@ -29,4 +33,11 @@ private TransformerDiscovererConstants() { }
"net.neoforged.fml.loading.ImmediateWindowProvider", // FIXME: remove this when removing the legacy ImmediateWindowProvider
"net.neoforged.neoforgespi.earlywindow.ImmediateWindowProvider"
);

public 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.mojang.logging.LogUtils;
import cpw.mods.jarhandling.JarContentsBuilder;
import cpw.mods.jarhandling.SecureJar;
import net.neoforged.fml.loading.ClasspathTransformerDiscoverer;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.fml.loading.LogMarkers;
import net.neoforged.neoforgespi.language.IModFileInfo;
Expand Down Expand Up @@ -47,7 +48,9 @@ public List<IModLocator.ModFileOrException> scanMods() {
.map(SecureJar::from)
.map(sj -> new ModFile(sj, this, ModFileParser::modsTomlParser))
.collect(Collectors.<IModFile>toList());
var otherModsExcluded = ClasspathTransformerDiscoverer.allExcluded();
var othermods = baseMC.otherModPaths().stream()
.filter(p -> p.stream().noneMatch(otherModsExcluded::contains)) //We cannot load MOD_CLASSES from the classpath if they are loaded on the SERVICE layer.
.map(p -> createMod(p.toArray(Path[]::new)))
.filter(Objects::nonNull);
artifacts.add(mcjar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected final List<Path> getFmlPaths(String[] classpath) {
.toList();
}

protected final Map<String, List<Path>> getModClasses() {
public static Map<String, List<Path>> getModClasses() {
final String modClasses = Optional.ofNullable(System.getenv("MOD_CLASSES")).orElse("");
LOGGER.debug(LogMarkers.CORE, "Got mod coordinates {} from env", modClasses);

Expand Down

0 comments on commit 072d112

Please sign in to comment.