Path: blob/1.21.x/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/InvalidModIdentifier.java
7412 views
/*1* Copyright (c) Forge Development LLC and contributors2* SPDX-License-Identifier: LGPL-2.1-only3*/45package net.minecraftforge.fml.loading.moddiscovery;67import cpw.mods.modlauncher.api.LamdbaExceptionUtils.Supplier_WithExceptions;8import net.minecraftforge.fml.loading.StringUtils;910import java.nio.file.Path;11import java.util.Optional;12import java.util.function.BiPredicate;13import java.util.stream.Stream;14import java.util.zip.ZipFile;1516import static cpw.mods.modlauncher.api.LamdbaExceptionUtils.*;1718// TODO: [FML][Loader] By the time this is reached, most stuff this checks for is already filtered out - needs fixing.19public enum InvalidModIdentifier {2021OLDFORGE(filePresent("mcmod.info")),22FABRIC(filePresent("fabric.mod.json")),23LITELOADER(filePresent("litemod.json")),24OPTIFINE(filePresent("optifine/Installer.class")),25BUKKIT(filePresent("plugin.yml")),26INVALIDZIP((f,zf) -> zf.isEmpty()); // note: only this one INVALIDZIP check is ran until the todo on this class is fixed2728private final BiPredicate<Path, Optional<ZipFile>> ident;2930InvalidModIdentifier(BiPredicate<Path, Optional<ZipFile>> identifier)31{32this.ident = identifier;33}3435private String getReason()36{37return "fml.modloading.brokenfile." + StringUtils.toLowerCase(name());38}3940public static Optional<String> identifyJarProblem(Path path)41{42Optional<ZipFile> zfo = optionalFromException(() -> new ZipFile(path.toFile()));43Optional<String> result = Stream.of(INVALIDZIP).44filter(i -> i.ident.test(path, zfo)).45map(InvalidModIdentifier::getReason).46findAny();47zfo.ifPresent(rethrowConsumer(ZipFile::close));48return result;49}5051private static BiPredicate<Path, Optional<ZipFile>> filePresent(String filename)52{53return (f, zfo) -> zfo.map(zf -> zf.getEntry(filename) != null).orElse(false);54}5556private static <T> Optional<T> optionalFromException(Supplier_WithExceptions<T, ? extends Exception> supp)57{58try59{60return Optional.of(supp.get());61}62catch (Exception e)63{64return Optional.empty();65}66}6768}697071