Skip to content

Commit

Permalink
Locator rewrite initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt authored and Technici4n committed Apr 9, 2024
1 parent 2dfe2ee commit 2a35c91
Show file tree
Hide file tree
Showing 28 changed files with 329 additions and 336 deletions.
8 changes: 2 additions & 6 deletions loader/src/main/java/net/neoforged/fml/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.fml.loading.ImmediateWindowHandler;
import net.neoforged.fml.loading.LoadingModList;
import net.neoforged.fml.loading.moddiscovery.AbstractModProvider;
import net.neoforged.fml.loading.moddiscovery.InvalidModIdentifier;
import net.neoforged.fml.loading.moddiscovery.ModFileInfo;
import net.neoforged.fml.loading.moddiscovery.ModInfo;
import net.neoforged.fml.loading.progress.ProgressMeter;
Expand Down Expand Up @@ -104,9 +102,7 @@ private ModLoader() {
this.loadingExceptions = FMLLoader.getLoadingModList().getErrors().stream()
.flatMap(ModLoadingException::fromEarlyException)
.collect(Collectors.toList());
this.loadingWarnings = FMLLoader.getLoadingModList().getBrokenFiles().stream()
.map(file -> new ModLoadingWarning(null, ModLoadingStage.VALIDATE, InvalidModIdentifier.identifyJarProblem(file.getFilePath()).orElse("fml.modloading.brokenfile"), file.getFileName()))
.collect(Collectors.toList());
this.loadingWarnings = new ArrayList<>();

FMLLoader.getLoadingModList().getWarnings().stream()
.flatMap(ModLoadingWarning::fromEarlyException)
Expand Down Expand Up @@ -282,7 +278,7 @@ private List<ModContainer> buildMods(final IModFile modFile) {

var missingClasses = new ArrayList<>(modIds);
missingClasses.removeAll(containerIds);
LOGGER.fatal(LOADING, "The following classes are missing, but are reported in the {}: {}", AbstractModProvider.MODS_TOML, missingClasses);
LOGGER.fatal(LOADING, "The following classes are missing, but are reported in the {}: {}", JarModsDotTomlModProvider.MODS_TOML, missingClasses);

var missingMods = new ArrayList<>(containerIds);
missingMods.removeAll(modIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ public void initialize(IEnvironment environment) {
LOGGER.debug(CORE, "Loading configuration");
FMLConfig.load();
LOGGER.debug(CORE, "Preparing ModFile");
environment.computePropertyIfAbsent(Environment.Keys.MODFILEFACTORY.get(), k -> ModFile::new);
environment.computePropertyIfAbsent(Environment.Keys.MODFILEFACTORY.get(), k -> new ModFileFactory() {
@Override
public IModFile build(SecureJar jar, IModProvider provider, ModFileInfoParser parser) throws InvalidModFileException {
return new ModFile(jar, provider, parser);
}

@Override
public IModFile build(SecureJar jar, IModProvider provider, ModFileInfoParser parser, IModFile.Type type) throws InvalidModFileException {
return new ModFile(jar, provider, parser, type.name());
}
});
arguments = new HashMap<>();
arguments.put("modLists", modListsArgumentList);
arguments.put("mods", modsArgumentList);
Expand Down
10 changes: 0 additions & 10 deletions loader/src/main/java/net/neoforged/fml/loading/LoadingModList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.neoforged.fml.loading.moddiscovery.ModFile;
import net.neoforged.fml.loading.moddiscovery.ModFileInfo;
import net.neoforged.fml.loading.moddiscovery.ModInfo;
import net.neoforged.neoforgespi.locating.IModFile;

/**
* Master list of all mods <em>in the loading context. This class cannot refer outside the
Expand All @@ -35,7 +34,6 @@ public class LoadingModList {
private final Map<String, ModFileInfo> fileById;
private final List<EarlyLoadingException> preLoadErrors;
private final List<EarlyLoadingException> preLoadWarnings;
private List<IModFile> brokenFiles;

private LoadingModList(final List<ModFile> modFiles, final List<ModInfo> sortedList) {
this.modFiles = modFiles.stream()
Expand Down Expand Up @@ -166,12 +164,4 @@ public List<EarlyLoadingException> getErrors() {
public List<EarlyLoadingException> getWarnings() {
return preLoadWarnings;
}

public void setBrokenFiles(final List<IModFile> brokenFiles) {
this.brokenFiles = brokenFiles;
}

public List<IModFile> getBrokenFiles() {
return this.brokenFiles;
}
}
17 changes: 15 additions & 2 deletions loader/src/main/java/net/neoforged/fml/loading/ModSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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 = LoadingModList.of(ms.systemMods, ms.systemMods.stream().map(mf -> (ModInfo) mf.getModInfos().get(0)).collect(toList()), new EarlyLoadingException("failure to validate mod list", null, listOf(errors, resolutionResult.buildErrorMessages())));
} else {
// Otherwise, lets try and sort the modlist and proceed
EarlyLoadingException earlyLoadingException = null;
Expand All @@ -77,7 +77,11 @@ public static LoadingModList sort(List<ModFile> mods, final List<EarlyLoadingExc
} catch (EarlyLoadingException e) {
earlyLoadingException = e;
}
list = LoadingModList.of(ms.modFiles, ms.sortedList, earlyLoadingException);
if (earlyLoadingException == null) {
list = LoadingModList.of(ms.modFiles, ms.sortedList, errors.isEmpty() ? null : new EarlyLoadingException("early loading exception", null, errors));
} else {
list = LoadingModList.of(ms.modFiles, ms.sortedList, new EarlyLoadingException(earlyLoadingException.getMessage(), earlyLoadingException.getCause(), listOf(earlyLoadingException.getAllData(), errors)));
}
}

// If we have conflicts those are considered warnings
Expand All @@ -90,6 +94,15 @@ public static LoadingModList sort(List<ModFile> mods, final List<EarlyLoadingExc
return list;
}

@SafeVarargs
private static <T> List<T> listOf(List<T>... lists) {
var lst = new ArrayList<T>();
for (List<T> list : lists) {
lst.addAll(list);
}
return lst;
}

@SuppressWarnings("UnstableApiUsage")
private void sort() {
// lambdas are identity based, so sorting them is impossible unless you hold reference to them
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import net.neoforged.neoforgespi.locating.IModLocator;

public abstract class AbstractJarFileModLocator extends AbstractJarFileModProvider implements IModLocator {
@Override
public List<IModLocator.ModFileOrException> scanMods() {
return scanCandidates().map(this::createMod).toList();
}

public abstract Stream<Path> scanCandidates();

public abstract class AbstractJarFileModLocator implements IModLocator {
protected static List<Path> getLegacyClasspath() {
return Arrays.stream(System.getProperty("legacyClassPath", "").split(File.pathSeparator)).map(Path::of).toList();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package net.neoforged.fml.loading.moddiscovery;

import cpw.mods.jarhandling.SecureJar;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
Expand All @@ -25,7 +26,7 @@ public String name() {
public void initArguments(Map<String, ?> arguments) {}

@Override
public Stream<Path> scanCandidates() {
public Stream<SecureJar> scanCandidates() {
String gameLibrariesStr = System.getProperty("fml.gameLayerLibraries");
if (gameLibrariesStr == null || gameLibrariesStr.isBlank())
return Stream.of();
Expand All @@ -38,6 +39,6 @@ public Stream<Path> scanCandidates() {
paths.add(path);
}

return paths.build();
return paths.build().map(SecureJar::from);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package net.neoforged.fml.loading.moddiscovery;

import com.mojang.logging.LogUtils;
import cpw.mods.jarhandling.SecureJar;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -33,18 +34,18 @@ public String name() {
}

@Override
public Stream<Path> scanCandidates() {
public Stream<SecureJar> scanCandidates() {
if (!enabled)
return Stream.of();

try {
var claimed = new ArrayList<>(legacyClasspath);
var paths = Stream.<Path>builder();

findPaths(claimed, MODS_TOML).forEach(paths::add);
findPaths(claimed, MANIFEST).forEach(paths::add);
findPaths(claimed, JarModsDotTomlModProvider.MODS_TOML).forEach(paths::add);
findPaths(claimed, JarModsDotTomlModProvider.MANIFEST).forEach(paths::add);

return paths.build();
return paths.build().map(SecureJar::from);
} catch (IOException e) {
LOGGER.error(LogMarkers.SCAN, "Error trying to find resources", e);
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@
import com.mojang.logging.LogUtils;
import cpw.mods.jarhandling.JarContentsBuilder;
import cpw.mods.jarhandling.SecureJar;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;
import net.neoforged.fml.loading.LogMarkers;
import net.neoforged.neoforgespi.locating.IModFile;
import net.neoforged.neoforgespi.locating.IModLocator;
import org.slf4j.Logger;

Expand All @@ -28,40 +22,20 @@ public class ExplodedDirectoryLocator implements IModLocator {
public record ExplodedMod(String modid, List<Path> paths) {}

private final List<ExplodedMod> explodedMods = new ArrayList<>();
private final Map<ExplodedMod, IModFile> mods = new HashMap<>();

@Override
public List<IModLocator.ModFileOrException> scanMods() {
explodedMods.forEach(explodedMod -> {
public Stream<SecureJar> scanCandidates() {
return explodedMods.stream().map(explodedMod -> {
var jarContents = new JarContentsBuilder().paths(explodedMod.paths().toArray(Path[]::new)).build();
if (jarContents.findFile(AbstractModProvider.MODS_TOML).isPresent()) {
var mjm = new ModJarMetadata(jarContents);
var mf = new ModFile(SecureJar.from(jarContents, mjm), this, ModFileParser::modsTomlParser);
mjm.setModFile(mf);
mods.put(explodedMod, mf);
} else {
LOGGER.warn(LogMarkers.LOADING, "Failed to find exploded resource {} in directory {}", AbstractModProvider.MODS_TOML, explodedMod.paths().get(0).toString());
}
return SecureJar.from(jarContents);
});
return mods.values().stream().map(mf -> new IModLocator.ModFileOrException(mf, null)).toList();
}

@Override
public String name() {
return "exploded directory";
}

@Override
public void scanFile(final IModFile file, final Consumer<Path> pathConsumer) {
LOGGER.debug(LogMarkers.SCAN, "Scanning exploded directory {}", file.getFilePath().toString());
try (Stream<Path> files = Files.find(file.getSecureJar().getRootPath(), Integer.MAX_VALUE, (p, a) -> p.getNameCount() > 0 && p.getFileName().toString().endsWith(".class"))) {
files.forEach(pathConsumer);
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.debug(LogMarkers.SCAN, "Exploded directory scan complete {}", file.getFilePath().toString());
}

@Override
public String toString() {
return "{ExplodedDir locator}";
Expand All @@ -75,9 +49,4 @@ public void initArguments(final Map<String, ?> arguments) {
explodedMods.addAll(explodedTargets);
}
}

@Override
public boolean isValid(final IModFile modFile) {
return true;
}
}
Loading

0 comments on commit 2a35c91

Please sign in to comment.