diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/FileUtils.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/FileUtils.java index 4273163438..9353bce3c4 100644 --- a/src/fmllauncher/java/net/minecraftforge/fml/loading/FileUtils.java +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/FileUtils.java @@ -56,4 +56,15 @@ public class FileUtils } return dirPath; } + + + public static String fileExtension(final Path path) { + String fileName = path.getFileName().toString(); + int idx = fileName.lastIndexOf('.'); + if (idx > -1) { + return fileName.substring(idx+1); + } else { + return ""; + } + } } diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/MavenDirectoryLocator.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/MavenDirectoryLocator.java index f405f5a1ea..5d7b3b914f 100644 --- a/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/MavenDirectoryLocator.java +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/MavenDirectoryLocator.java @@ -27,6 +27,7 @@ import java.nio.file.Path; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; public class MavenDirectoryLocator extends AbstractJarFileLocator { private List modCoords; @@ -54,7 +55,9 @@ public class MavenDirectoryLocator extends AbstractJarFileLocator { final List mavenRoots = (List) arguments.get("mavenRoots"); final List mavenRootPaths = mavenRoots.stream().map(n -> FMLPaths.GAMEDIR.get().resolve(n)).collect(Collectors.toList()); final List mods = (List) arguments.get("mods"); - List localModCoords = mods.stream().map(MavenCoordinateResolver::get).collect(Collectors.toList()); + final List listedMods = ModListHandler.processModLists((List) arguments.get("modLists"), mavenRootPaths); + + List localModCoords = Stream.concat(mods.stream(),listedMods.stream()).map(MavenCoordinateResolver::get).collect(Collectors.toList()); // find the modCoords path in each supplied maven path, and turn it into a mod file. (skips not found files) this.modCoords = localModCoords.stream().map(mc -> mavenRootPaths.stream().map(root -> root.resolve(mc)).filter(path -> Files.exists(path)).findFirst().orElseThrow(() -> new IllegalArgumentException("Failed to locate requested mod coordinate " + mc))).collect(Collectors.toList()); diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModListHandler.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModListHandler.java new file mode 100644 index 0000000000..5717c82fc4 --- /dev/null +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModListHandler.java @@ -0,0 +1,103 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2019. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.fml.loading.moddiscovery; + +import net.minecraftforge.fml.loading.FMLPaths; +import net.minecraftforge.fml.loading.FileUtils; +import net.minecraftforge.fml.loading.MavenCoordinateResolver; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +import static net.minecraftforge.fml.loading.LogMarkers.CORE; + +public class ModListHandler { + private static final Logger LOGGER = LogManager.getLogger(); + /** + * Reads the modList paths specified, and searches each maven root for mods matching. Returns a list of mods + * found. + * + * @param modListPaths Paths to search for mod file lists + * @param mavenRootPaths Roots to look for mods listed + * @return list of found mod coordinates + */ + public static List processModLists(final List modListPaths, final List mavenRootPaths) { + final List modCoordinates = modListPaths.stream().map(ModListHandler::transformPathToList). + flatMap(Collection::stream). + collect(Collectors.toList()); + + List> localModCoords = modCoordinates.stream().map(mc->Pair.of(MavenCoordinateResolver.get(mc), mc)).collect(Collectors.toList()); + final List> foundCoordinates = localModCoords.stream(). + map(mc -> mavenRootPaths.stream(). + map(root -> Pair.of(root.resolve(mc.getLeft()), mc.getRight())). + filter(path -> Files.exists(path.getLeft())). + findFirst(). + orElseGet(()->{ + LOGGER.warn(CORE, "Failed to find coordinate {}", mc); + return null; + })). + filter(Objects::nonNull). + collect(Collectors.toList()); + + final List found = foundCoordinates.stream().map(Pair::getRight).collect(Collectors.toList()); + LOGGER.debug(CORE, "Found mod coordinates from lists: {}", found); + return found; + } + + private static List transformPathToList(final String path) { + LOGGER.debug(CORE, "Reading mod list {}", path); + Path filePath = FMLPaths.GAMEDIR.get().resolve(path); + if (!Files.exists(filePath)) { + LOGGER.warn(CORE, "Failed to find modlist file at {}", filePath); + return Collections.emptyList(); + } + + String extension = FileUtils.fileExtension(filePath); + if (Objects.equals("list",extension)) { + return readListFile(filePath).stream().filter(s -> !s.isEmpty()).collect(Collectors.toList()); + } else { + LOGGER.warn(CORE, "Failed to read unknown file list type {} for file {}", extension, filePath); + } + return Collections.emptyList(); + } + + /** + * Simple list file, ending in ".list" with one mod coordinate per line + * @param filePath path + * @return list + */ + private static List readListFile(final Path filePath) { + try { + return Files.readAllLines(filePath); + } catch (IOException e) { + LOGGER.warn(CORE, "Failed to read file list {}", filePath, e); + return Collections.emptyList(); + } + } +}