Skip to content

Commit

Permalink
Catch some loading errors
Browse files Browse the repository at this point in the history
- catch invalid mod files preventing system mods from being found
- catch and log early loading errors
  • Loading branch information
Matyrobbrt committed Jan 13, 2024
1 parent 3ce905f commit ea5dc3c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
15 changes: 12 additions & 3 deletions loader/src/main/java/net/neoforged/fml/loading/ModSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ private ModSorter(final List<ModFile> modFiles)
this.uniqueModListBuilder = new UniqueModListBuilder(modFiles);
}

public static LoadingModList sort(List<ModFile> mods, final List<EarlyLoadingException.ExceptionData> errors)
public static LoadingModList sort(List<ModFile> mods, final List<EarlyLoadingException.ExceptionData> 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
Expand All @@ -60,7 +65,7 @@ public static LoadingModList sort(List<ModFile> mods, final List<EarlyLoadingExc

// if we miss a dependency or detect an incompatibility, we abort now
if (!resolutionResult.versionResolution.isEmpty() || !resolutionResult.incompatibilities.isEmpty()) {
list = LoadingModList.of(ms.systemMods, ms.systemMods.stream().map(mf->(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;
Expand All @@ -83,6 +88,10 @@ public static LoadingModList sort(List<ModFile> mods, final List<EarlyLoadingExc
return list;
}

public LoadingModList buildSystemMods(EarlyLoadingException exception) {
return LoadingModList.of(systemMods, systemMods.stream().map(mf -> (ModInfo) mf.getModInfos().get(0)).toList(), exception);
}

@SuppressWarnings("UnstableApiUsage")
private void sort()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down

0 comments on commit ea5dc3c

Please sign in to comment.