From ea5dc3ccc7cc56a13a8949e8020a642ab53b74da Mon Sep 17 00:00:00 2001 From: Matyrobbrt Date: Sat, 13 Jan 2024 10:12:04 +0200 Subject: [PATCH] Catch some loading errors - catch invalid mod files preventing system mods from being found - catch and log early loading errors --- .../java/net/neoforged/fml/loading/ModSorter.java | 15 ++++++++++++--- .../loading/moddiscovery/AbstractModProvider.java | 15 +++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/loader/src/main/java/net/neoforged/fml/loading/ModSorter.java b/loader/src/main/java/net/neoforged/fml/loading/ModSorter.java index 2c789b58d..20364e82e 100644 --- a/loader/src/main/java/net/neoforged/fml/loading/ModSorter.java +++ b/loader/src/main/java/net/neoforged/fml/loading/ModSorter.java @@ -43,14 +43,19 @@ private ModSorter(final List modFiles) this.uniqueModListBuilder = new UniqueModListBuilder(modFiles); } - public static LoadingModList sort(List mods, final List errors) + public static LoadingModList sort(List mods, final List discoveryError) { final ModSorter ms = new ModSorter(mods); + // If a discovery error was encountered, abort + if (!discoveryError.isEmpty()) { + return ms.buildSystemMods(new EarlyLoadingException("encountered discovery error", null, discoveryError)); + } + try { ms.buildUniqueList(); } catch (EarlyLoadingException e) { // We cannot build any list with duped mods. We have to abort immediately and report it - return LoadingModList.of(ms.systemMods, ms.systemMods.stream().map(mf->(ModInfo)mf.getModInfos().get(0)).collect(toList()), e); + return ms.buildSystemMods(e); } // try and validate dependencies @@ -60,7 +65,7 @@ public static LoadingModList sort(List mods, final List(ModInfo)mf.getModInfos().get(0)).collect(toList()), new EarlyLoadingException("failure to validate mod list", null, resolutionResult.buildErrorMessages())); + list = ms.buildSystemMods(new EarlyLoadingException("failure to validate mod list", null, resolutionResult.buildErrorMessages())); } else { // Otherwise, lets try and sort the modlist and proceed EarlyLoadingException earlyLoadingException = null; @@ -83,6 +88,10 @@ public static LoadingModList sort(List mods, final List (ModInfo) mf.getModInfos().get(0)).toList(), exception); + } + @SuppressWarnings("UnstableApiUsage") private void sort() { diff --git a/loader/src/main/java/net/neoforged/fml/loading/moddiscovery/AbstractModProvider.java b/loader/src/main/java/net/neoforged/fml/loading/moddiscovery/AbstractModProvider.java index 2553af652..3da5951f8 100644 --- a/loader/src/main/java/net/neoforged/fml/loading/moddiscovery/AbstractModProvider.java +++ b/loader/src/main/java/net/neoforged/fml/loading/moddiscovery/AbstractModProvider.java @@ -25,6 +25,8 @@ import java.util.Map; import java.util.Optional; import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; public abstract class AbstractModProvider implements IModProvider { @@ -43,10 +45,15 @@ protected IModLocator.ModFileOrException createMod(Path... path) { type = getDefaultJarModType(); } if (jarContents.findFile(MODS_TOML).isPresent()) { - LOGGER.debug(LogMarkers.SCAN, "Found {} mod of type {}: {}", MODS_TOML, type, path); - var mjm = new ModJarMetadata(jarContents); - mod = new ModFile(SecureJar.from(jarContents, mjm), this, ModFileParser::modsTomlParser); - mjm.setModFile(mod); + try { + LOGGER.debug(LogMarkers.SCAN, "Found {} mod of type {}: {}", MODS_TOML, type, path); + var mjm = new ModJarMetadata(jarContents); + mod = new ModFile(SecureJar.from(jarContents, mjm), this, ModFileParser::modsTomlParser); + mjm.setModFile(mod); + } catch (InvalidModFileException invalidModException) { + LOGGER.error("Mod at [{}] has an invalid mod file: ", Stream.of(path).map(p -> p.toAbsolutePath().toString()).collect(Collectors.joining(", ")), invalidModException); + return new IModLocator.ModFileOrException(null, invalidModException); + } } else if (type != null) { LOGGER.debug(LogMarkers.SCAN, "Found {} mod of type {}: {}", MANIFEST, type, path); mod = new ModFile(SecureJar.from(jarContents), this, this::manifestParser, type);