Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MinecraftForge
GitHub Repository: MinecraftForge/MinecraftForge
Path: blob/1.21.x/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/InvalidModIdentifier.java
7412 views
1
/*
2
* Copyright (c) Forge Development LLC and contributors
3
* SPDX-License-Identifier: LGPL-2.1-only
4
*/
5
6
package net.minecraftforge.fml.loading.moddiscovery;
7
8
import cpw.mods.modlauncher.api.LamdbaExceptionUtils.Supplier_WithExceptions;
9
import net.minecraftforge.fml.loading.StringUtils;
10
11
import java.nio.file.Path;
12
import java.util.Optional;
13
import java.util.function.BiPredicate;
14
import java.util.stream.Stream;
15
import java.util.zip.ZipFile;
16
17
import static cpw.mods.modlauncher.api.LamdbaExceptionUtils.*;
18
19
// TODO: [FML][Loader] By the time this is reached, most stuff this checks for is already filtered out - needs fixing.
20
public enum InvalidModIdentifier {
21
22
OLDFORGE(filePresent("mcmod.info")),
23
FABRIC(filePresent("fabric.mod.json")),
24
LITELOADER(filePresent("litemod.json")),
25
OPTIFINE(filePresent("optifine/Installer.class")),
26
BUKKIT(filePresent("plugin.yml")),
27
INVALIDZIP((f,zf) -> zf.isEmpty()); // note: only this one INVALIDZIP check is ran until the todo on this class is fixed
28
29
private final BiPredicate<Path, Optional<ZipFile>> ident;
30
31
InvalidModIdentifier(BiPredicate<Path, Optional<ZipFile>> identifier)
32
{
33
this.ident = identifier;
34
}
35
36
private String getReason()
37
{
38
return "fml.modloading.brokenfile." + StringUtils.toLowerCase(name());
39
}
40
41
public static Optional<String> identifyJarProblem(Path path)
42
{
43
Optional<ZipFile> zfo = optionalFromException(() -> new ZipFile(path.toFile()));
44
Optional<String> result = Stream.of(INVALIDZIP).
45
filter(i -> i.ident.test(path, zfo)).
46
map(InvalidModIdentifier::getReason).
47
findAny();
48
zfo.ifPresent(rethrowConsumer(ZipFile::close));
49
return result;
50
}
51
52
private static BiPredicate<Path, Optional<ZipFile>> filePresent(String filename)
53
{
54
return (f, zfo) -> zfo.map(zf -> zf.getEntry(filename) != null).orElse(false);
55
}
56
57
private static <T> Optional<T> optionalFromException(Supplier_WithExceptions<T, ? extends Exception> supp)
58
{
59
try
60
{
61
return Optional.of(supp.get());
62
}
63
catch (Exception e)
64
{
65
return Optional.empty();
66
}
67
}
68
69
}
70
71