diff --git a/src/main/java/vfyjxf/bettercrashes/utils/ModIdentifier.java b/src/main/java/vfyjxf/bettercrashes/utils/ModIdentifier.java index b93ab6e..52ca2f5 100644 --- a/src/main/java/vfyjxf/bettercrashes/utils/ModIdentifier.java +++ b/src/main/java/vfyjxf/bettercrashes/utils/ModIdentifier.java @@ -56,19 +56,15 @@ public final class ModIdentifier { return mods; } - public static Set identifyFromClass(String className) { - return identifyFromClass(className, makeModMap()); - } - private static Set identifyFromClass(String className, Map> modMap) { // Skip identification for Mixin, one's mod copy of the library is shared with all other mods if (className.startsWith("org.spongepowered.asm.mixin.")) return Collections.emptySet(); // Get the URL of the class - final String untrasformedName = untransformName(Launch.classLoader, className); - URL url = Launch.classLoader.getResource(untrasformedName.replace('.', '/') + ".class"); + final String untransformedName = untransformName(Launch.classLoader, className); + URL url = Launch.classLoader.getResource(untransformedName.replace('.', '/') + ".class"); if (url == null) { - log.warn("Failed to identify " + className + " (untransformed name: " + untrasformedName + ")"); + log.warn("Failed to identify " + className + " (untransformed name: " + untransformedName + ")"); return Collections.emptySet(); } @@ -88,10 +84,21 @@ public final class ModIdentifier { return Collections.emptySet(); } + private static Map> cachedModMap = null; + private static Map> makeModMap() { + if (cachedModMap != null) return cachedModMap; Map> modMap = new HashMap<>(); - for (ModContainer mod : Loader.instance().getModList()) { - Set currentMods = modMap.getOrDefault(mod.getSource(), new HashSet<>()); + Map indexedModList = Loader.instance().getIndexedModList(); + + ModContainer mc = Loader.instance().getMinecraftModContainer(); // Ignore minecraft jar (minecraft) + ModContainer fml = Loader.instance().getIndexedModList().get("FML"); // Ignore forge jar (FML,forge) + + for (ModContainer mod : indexedModList.values()) { + // Workaround for https://github.com/MinecraftForge/MinecraftForge/issues/4919 + if (mod.equals(fml) || mod.equals(mc)) continue; + + Set currentMods = new HashSet<>(); currentMods.add(mod); try { modMap.put(mod.getSource().getCanonicalFile(), currentMods); @@ -99,22 +106,18 @@ public final class ModIdentifier { throw new RuntimeException(e); } } - - try { - modMap.remove(Loader.instance().getMinecraftModContainer().getSource()); // Ignore minecraft jar (minecraft) - modMap.remove(Loader.instance().getIndexedModList().get("FML").getSource()); // Ignore forge jar (FML, - // forge) - } catch (NullPointerException ignored) { - // Workaround for https://github.com/MinecraftForge/MinecraftForge/issues/4919 - } - - return modMap; + cachedModMap = Collections.unmodifiableMap(modMap); + return cachedModMap; } + private static Method untransformNameMethod = null; + private static String untransformName(LaunchClassLoader launchClassLoader, String className) { try { - Method untransformNameMethod = LaunchClassLoader.class.getDeclaredMethod("untransformName", String.class); - untransformNameMethod.setAccessible(true); + if (untransformNameMethod == null) { + untransformNameMethod = LaunchClassLoader.class.getDeclaredMethod("untransformName", String.class); + untransformNameMethod.setAccessible(true); + } return (String) untransformNameMethod.invoke(launchClassLoader, className); } catch (ReflectiveOperationException e) { throw new RuntimeException(e);