Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hard casts to ModFile and co #67

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions core/src/main/java/net/neoforged/fml/ModList.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,20 @@ public class ModList
private static ModList INSTANCE;
private final List<IModFileInfo> modFiles;
private final List<IModInfo> sortedList;
private final Map<String, ModFileInfo> fileById;
private final Map<String, IModFileInfo> fileById;
private List<ModContainer> mods;
private Map<String, ModContainer> indexedMods;
private List<ModFileScanData> modFileScanData;
private List<ModContainer> sortedContainers;

private ModList(final List<ModFile> modFiles, final List<ModInfo> sortedList)
private ModList(final List<IModFile> modFiles, final List<IModInfo> sortedList)
{
this.modFiles = modFiles.stream().map(ModFile::getModFileInfo).map(ModFileInfo.class::cast).collect(Collectors.toList());
this.modFiles = modFiles.stream().map(IModFile::getModFileInfo).toList();
this.sortedList = sortedList.stream().
map(ModInfo.class::cast).
collect(Collectors.toList());
this.fileById = this.modFiles.stream().map(IModFileInfo::getMods).flatMap(Collection::stream).
map(ModInfo.class::cast).
collect(Collectors.toMap(ModInfo::getModId, ModInfo::getOwningFile));
collect(Collectors.toMap(IModInfo::getModId, IModInfo::getOwningFile));
CrashReportCallables.registerCrashCallable("Mod List", this::crashReport);
}

Expand All @@ -85,7 +84,7 @@ private String crashReport() {
return "\n"+applyForEachModFile(this::fileToLine).collect(Collectors.joining("\n\t\t", "\t\t", ""));
}

public static ModList of(List<ModFile> modFiles, List<ModInfo> sortedList)
public static ModList of(List<IModFile> modFiles, List<IModInfo> sortedList)
{
INSTANCE = new ModList(modFiles, sortedList);
return INSTANCE;
Expand Down
12 changes: 4 additions & 8 deletions core/src/main/java/net/neoforged/fml/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.neoforged.fml.loading.moddiscovery.ModInfo;
import net.neoforged.fml.loading.progress.ProgressMeter;
import net.neoforged.fml.loading.progress.StartupNotificationManager;
import net.neoforged.neoforgespi.language.IModFileInfo;
import net.neoforged.neoforgespi.language.IModInfo;
import net.neoforged.neoforgespi.language.IModLanguageProvider;
import net.neoforged.neoforgespi.locating.ForgeFeature;
Expand Down Expand Up @@ -105,11 +106,6 @@ private ModLoader()
.flatMap(ModLoadingWarning::fromEarlyException)
.forEach(this.loadingWarnings::add);

FMLLoader.getLoadingModList().getModFiles().stream()
.filter(ModFileInfo::missingLicense)
.filter(modFileInfo -> modFileInfo.getMods().stream().noneMatch(thisModInfo -> this.loadingExceptions.stream().map(ModLoadingException::getModInfo).anyMatch(otherInfo -> otherInfo == thisModInfo))) //Ignore files where any other mod already encountered an error
.map(modFileInfo -> new ModLoadingException(null, ModLoadingStage.VALIDATE, "fml.modloading.missinglicense", null, modFileInfo.getFile()))
.forEach(this.loadingExceptions::add);
Comment on lines -108 to -112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not part of the proposed changes why did you add it?

this.stateManager = new ModStateManager();
CrashReportCallables.registerCrashCallable("ModLauncher", FMLLoader::getLauncherInfo);
CrashReportCallables.registerCrashCallable("ModLauncher launch target", FMLLoader::launcherHandlerName);
Expand Down Expand Up @@ -147,7 +143,7 @@ public void gatherAndInitializeMods(final ModWorkManager.DrivenExecutor syncExec
ForgeFeature.registerFeature("openGLVersion", ForgeFeature.VersionFeatureTest.forVersionString(IModInfo.DependencySide.CLIENT, ImmediateWindowHandler.getGLVersion()));
loadingStateValid = true;
FMLLoader.backgroundScanHandler.waitForScanToComplete(periodicTask);
final ModList modList = ModList.of(loadingModList.getModFiles().stream().map(ModFileInfo::getFile).toList(),
final ModList modList = ModList.of(loadingModList.getModFiles().stream().map(IModFileInfo::getFile).toList(),
loadingModList.getMods());
if (!this.loadingExceptions.isEmpty()) {
LOGGER.fatal(CORE, "Error during pre-loading phase", loadingExceptions.get(0));
Expand All @@ -157,7 +153,7 @@ public void gatherAndInitializeMods(final ModWorkManager.DrivenExecutor syncExec
throw new LoadingFailedException(loadingExceptions);
}
List<? extends ForgeFeature.Bound> failedBounds = loadingModList.getMods().stream()
.map(ModInfo::getForgeFeatures)
.map(IModInfo::getForgeFeatures)
.flatMap(Collection::stream)
.filter(bound -> !ForgeFeature.testFeature(FMLEnvironment.dist, bound))
.toList();
Expand All @@ -173,7 +169,7 @@ public void gatherAndInitializeMods(final ModWorkManager.DrivenExecutor syncExec
}

final List<ModContainer> modContainers = loadingModList.getModFiles().stream()
.map(ModFileInfo::getFile)
.map(IModFileInfo::getFile)
.map(this::buildMods)
.<ModContainer>mapMulti(Iterable::forEach)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.neoforged.fml.loading.targets.CommonLaunchHandler;
import net.neoforged.neoforgespi.Environment;
import net.neoforged.neoforgespi.coremod.ICoreModProvider;
import net.neoforged.neoforgespi.locating.IModFile;
import org.slf4j.Logger;

import java.io.IOException;
Expand Down Expand Up @@ -186,7 +187,7 @@ public static CommonLaunchHandler getLaunchHandler() {
return commonLaunchHandler;
}

public static void addAccessTransformer(Path atPath, ModFile modName)
public static void addAccessTransformer(Path atPath, IModFile modName)
{
LOGGER.debug(LogMarkers.SCAN, "Adding Access Transformer in {}", modName.getFilePath());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.neoforged.neoforgespi.language.IModLanguageProvider;
import net.neoforged.fml.loading.moddiscovery.ExplodedDirectoryLocator;
import net.neoforged.fml.loading.moddiscovery.ModFile;
import net.neoforged.neoforgespi.locating.IModFile;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
Expand Down Expand Up @@ -129,7 +130,7 @@ Stream<Path> getLibraries() {
return languagePaths.stream();
}

public IModLanguageProvider findLanguage(ModFile mf, String modLoader, VersionRange modLoaderVersion) {
public IModLanguageProvider findLanguage(IModFile mf, String modLoader, VersionRange modLoaderVersion) {
final String languageFileName = mf.getProvider() instanceof ExplodedDirectoryLocator ? "in-development" : mf.getFileName();
final ModLanguageWrapper mlw = languageProviderMap.get(modLoader);
if (mlw == null) {
Expand Down
45 changes: 23 additions & 22 deletions loader/src/main/java/net/neoforged/fml/loading/LoadingModList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
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.language.IModFileInfo;
import net.neoforged.neoforgespi.language.IModInfo;
import net.neoforged.neoforgespi.locating.IModFile;

import java.net.URL;
Expand All @@ -32,32 +34,31 @@
public class LoadingModList
{
private static LoadingModList INSTANCE;
private final List<ModFileInfo> modFiles;
private final List<ModInfo> sortedList;
private final Map<String, ModFileInfo> fileById;
private final List<IModFileInfo> modFiles;
private final List<IModInfo> sortedList;
private final Map<String, IModFileInfo> 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)
private LoadingModList(final List<IModFile> modFiles, final List<IModInfo> sortedList)
{
this.modFiles = modFiles.stream()
.map(ModFile::getModFileInfo)
.map(IModFile::getModFileInfo)
.map(ModFileInfo.class::cast)
.collect(Collectors.toList());
this.sortedList = sortedList.stream()
.map(ModInfo.class::cast)
.collect(Collectors.toList());
this.fileById = this.modFiles.stream()
.map(ModFileInfo::getMods)
this.fileById = modFiles.stream()
.map(IModFile::getModInfos)
.flatMap(Collection::stream)
.map(ModInfo.class::cast)
.collect(Collectors.toMap(ModInfo::getModId, ModInfo::getOwningFile));
.collect(Collectors.toMap(IModInfo::getModId, IModInfo::getOwningFile));
this.preLoadErrors = new ArrayList<>();
this.preLoadWarnings = new ArrayList<>();
}

public static LoadingModList of(List<ModFile> modFiles, List<ModInfo> sortedList, final EarlyLoadingException earlyLoadingException)
public static LoadingModList of(List<IModFile> modFiles, List<IModInfo> sortedList, final EarlyLoadingException earlyLoadingException)
{
INSTANCE = new LoadingModList(modFiles, sortedList);
if (earlyLoadingException != null)
Expand All @@ -73,43 +74,43 @@ public static LoadingModList get() {
public void addCoreMods()
{
modFiles.stream()
.map(ModFileInfo::getFile)
.map(ModFile::getCoreMods)
.map(IModFileInfo::getFile)
.map(IModFile::getCoreMods)
.flatMap(List::stream)
.forEach(FMLLoader.getCoreModProvider()::addCoreMod);
}

public void addMixinConfigs() {
modFiles.stream()
.map(ModFileInfo::getFile)
.map(ModFile::getMixinConfigs)
.map(IModFileInfo::getFile)
.map(IModFile::getMixinConfigs)
.flatMap(List::stream)
.forEach(DeferredMixinConfigRegistration::addMixinConfig);
}

public void addAccessTransformers()
{
modFiles.stream()
.map(ModFileInfo::getFile)
.map(IModFileInfo::getFile)
.forEach(mod -> mod.getAccessTransformers().forEach(path -> FMLLoader.addAccessTransformer(path, mod)));
}

public void addForScanning(BackgroundScanHandler backgroundScanHandler)
{
backgroundScanHandler.setLoadingModList(this);
modFiles.stream()
.map(ModFileInfo::getFile)
.map(IModFileInfo::getFile)
.forEach(backgroundScanHandler::submitForScanning);
}

public List<ModFileInfo> getModFiles()
public List<IModFileInfo> getModFiles()
{
return modFiles;
}

public Path findResource(final String className)
{
for (ModFileInfo mf : modFiles) {
for (var mf : modFiles) {
final Path resource = mf.getFile().findResource(className);
if (Files.exists(resource)) return resource;
}
Expand All @@ -125,7 +126,7 @@ public Enumeration<URL> findAllURLsForResource(final String resName) {
resourceName = resName;
}
return new Enumeration<URL>() {
private final Iterator<ModFileInfo> modFileIterator = modFiles.iterator();
private final Iterator<IModFileInfo> modFileIterator = modFiles.iterator();
private URL next;
@Override
public boolean hasMoreElements() {
Expand All @@ -147,7 +148,7 @@ public URL nextElement() {

private URL findNextURL() {
while (modFileIterator.hasNext()) {
final ModFileInfo next = modFileIterator.next();
final var next = modFileIterator.next();
final Path resource = next.getFile().findResource(resourceName);
if (Files.exists(resource)) {
return LamdbaExceptionUtils.uncheck(()->new URL("modjar://" + next.getMods().get(0).getModId() + "/" + resourceName));
Expand All @@ -158,12 +159,12 @@ private URL findNextURL() {
};
}

public ModFileInfo getModFileById(String modid)
public IModFileInfo getModFileById(String modid)
{
return this.fileById.get(modid);
}

public List<ModInfo> getMods()
public List<IModInfo> getMods()
{
return this.sortedList;
}
Expand Down
23 changes: 12 additions & 11 deletions loader/src/main/java/net/neoforged/fml/loading/ModSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.neoforged.fml.loading.moddiscovery.ModInfo;
import net.neoforged.fml.loading.toposort.CyclePresentException;
import net.neoforged.fml.loading.toposort.TopologicalSort;
import net.neoforged.neoforgespi.locating.IModFile;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.slf4j.Logger;
Expand All @@ -33,17 +34,17 @@ public class ModSorter
{
private static final Logger LOGGER = LogUtils.getLogger();
private final UniqueModListBuilder uniqueModListBuilder;
private List<ModFile> modFiles;
private List<ModInfo> sortedList;
private List<IModFile> modFiles;
private List<IModInfo> sortedList;
private Map<String, IModInfo> modIdNameLookup;
private List<ModFile> systemMods;
private List<IModFile> systemMods;

private ModSorter(final List<ModFile> modFiles)
private ModSorter(final List<IModFile> modFiles)
{
this.uniqueModListBuilder = new UniqueModListBuilder(modFiles);
}

public static LoadingModList sort(List<ModFile> mods, final List<EarlyLoadingException.ExceptionData> errors)
public static LoadingModList sort(List<IModFile> mods, final List<EarlyLoadingException.ExceptionData> errors)
{
final ModSorter ms = new ModSorter(mods);
try {
Expand Down Expand Up @@ -90,13 +91,13 @@ private void sort()
final MutableGraph<ModFileInfo> graph = GraphBuilder.directed().build();
AtomicInteger counter = new AtomicInteger();
Map<ModFileInfo, Integer> infos = modFiles.stream()
.map(ModFile::getModFileInfo)
.map(IModFile::getModFileInfo)
.filter(ModFileInfo.class::isInstance)
.map(ModFileInfo.class::cast)
.collect(toMap(Function.identity(), e -> counter.incrementAndGet()));
infos.keySet().forEach(graph::addNode);
modFiles.stream()
.map(ModFile::getModInfos)
.map(IModFile::getModInfos)
.<IModInfo>mapMulti(Iterable::forEach)
.map(IModInfo::getDependencies)
.<IModInfo.ModVersion>mapMulti(Iterable::forEach)
Expand Down Expand Up @@ -163,7 +164,7 @@ private void buildUniqueList()
));
}

private void detectSystemMods(final Map<String, List<ModFile>> modFilesByFirstId)
private void detectSystemMods(final Map<String, List<IModFile>> modFilesByFirstId)
{
// Capture system mods (ex. MC, Forge) here, so we can keep them for later
final Set<String> systemMods = new HashSet<>();
Expand All @@ -172,7 +173,7 @@ private void detectSystemMods(final Map<String, List<ModFile>> modFilesByFirstId
// Find mod file from MinecraftLocator to define the system mods
modFiles.stream()
.filter(modFile -> modFile.getProvider().getClass() == MinecraftLocator.class)
.map(ModFile::getSecureJar)
.map(IModFile::getSecureJar)
.map(SecureJar::moduleDataProvider)
.map(SecureJar.ModuleDataProvider::getManifest)
.map(Manifest::getMainAttributes)
Expand Down Expand Up @@ -228,12 +229,12 @@ public List<EarlyLoadingException.ExceptionData> buildErrorMessages() {
private DependencyResolutionResult verifyDependencyVersions()
{
final var modVersions = modFiles.stream()
.map(ModFile::getModInfos)
.map(IModFile::getModInfos)
.<IModInfo>mapMulti(Iterable::forEach)
.collect(toMap(IModInfo::getModId, IModInfo::getVersion));

final var modVersionDependencies = modFiles.stream()
.map(ModFile::getModInfos)
.map(IModFile::getModInfos)
.<IModInfo>mapMulti(Iterable::forEach)
.collect(groupingBy(Function.identity(), flatMapping(e -> e.getDependencies().stream(), toList())));

Expand Down
Loading
Loading