Skip to content

Commit

Permalink
Allow multiple copies of a class on the CP and pick the JAR containin…
Browse files Browse the repository at this point in the history
…g the first.
  • Loading branch information
shartte committed Jun 24, 2024
1 parent 74cb237 commit 5e864b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
18 changes: 15 additions & 3 deletions loader/src/main/java/net/neoforged/fml/util/DevEnvUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import net.neoforged.fml.ModLoadingException;
import net.neoforged.fml.ModLoadingIssue;
import org.jetbrains.annotations.ApiStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ApiStatus.Internal
public final class DevEnvUtils {
private static final Logger LOG = LoggerFactory.getLogger(DevEnvUtils.class);

private DevEnvUtils() {}

public static List<Path> findFileSystemRootsOfFileOnClasspath(String relativePath) {
Expand All @@ -37,7 +43,8 @@ public static List<Path> findFileSystemRootsOfFileOnClasspath(String relativePat
throw new IllegalArgumentException("Failed to enumerate classpath locations of " + relativePath);
}

List<Path> result = new ArrayList<>();
// Remove duplicates, but maintain order!
Set<Path> result = new LinkedHashSet<>();
while (resourceIt.hasNext()) {
var resourceUrl = resourceIt.next();

Expand Down Expand Up @@ -66,7 +73,7 @@ public static List<Path> findFileSystemRootsOfFileOnClasspath(String relativePat
}
}

return result;
return new ArrayList<>(result);
}

public static Path findFileSystemRootOfFileOnClasspath(String relativePath) {
Expand All @@ -75,7 +82,12 @@ public static Path findFileSystemRootOfFileOnClasspath(String relativePath) {
if (paths.isEmpty()) {
throw new ModLoadingException(ModLoadingIssue.error("fml.modloadingissue.failed_to_find_on_classpath", relativePath));
} else if (paths.size() > 1) {
throw new ModLoadingException(ModLoadingIssue.error("fml.modloadingissue.multiple_copies_on_classpath", relativePath, paths));
// Warn for anything past the first. It's impossible to know if the user actually intended this setup,
// but to allow launching in uncommon configurations, we'll allow it.
for (int i = 1; i < paths.size(); i++) {
var path = paths.get(i);
LOG.warn("Found multiple copies of {} on classpath. Ignoring copy from {}, using {} instead.", relativePath, path, paths.getFirst());
}
}

return paths.getFirst();
Expand Down
1 change: 0 additions & 1 deletion loader/src/main/resources/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"fml.modloadingissue.maven_coordinate_not_found": "No mod with the Maven coordinate {0} could be found in {1}",
"fml.modloadingissue.failed_to_list_folder_content": "Failed to list content of folder {0}",
"fml.modloadingissue.failed_to_find_on_classpath": "Failed to find {0} on the classpath",
"fml.modloadingissue.multiple_copies_on_classpath": "Found multiple copies of {0} on the classpath: {1}",
"fml.messages.artifactversion.ornotinstalled": "{0,ornull,fml.messages.artifactversion.notinstalled}",
"fml.messages.artifactversion": "{0,ornull,fml.messages.artifactversion.none}",
"fml.messages.artifactversion.none": "none",
Expand Down

0 comments on commit 5e864b1

Please sign in to comment.