Skip to content

Commit ae8840b

Browse files
authored
Apply SJH updates and remove deprecated members (#38)
1 parent a952595 commit ae8840b

13 files changed

+92
-119
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ changelog {
99
disableAutomaticPublicationRegistration()
1010
}
1111

12+
logger.lifecycle("FML version {}", gradleutils.getTagOffsetVersion())
13+
1214
allprojects {
1315
apply plugin: 'java-library'
1416
apply plugin: 'jacoco'

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ accesstransformers_version=9.0.0
44
coremods_version=6.0.0
55
eventbus_version=7.0.16
66
modlauncher_version=10.0.9
7-
securejarhandler_version=2.1.10
7+
securejarhandler_version=2.1.23
88
bootstraplauncher_version=1.1.2
99
asm_version=9.5
1010
mixin_version=0.8.5

loader/src/main/java/net/neoforged/fml/loading/ModDirTransformerDiscoverer.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package net.neoforged.fml.loading;
77

88
import com.mojang.logging.LogUtils;
9+
import cpw.mods.jarhandling.JarContentsBuilder;
10+
import cpw.mods.jarhandling.JarMetadata;
911
import cpw.mods.jarhandling.SecureJar;
1012
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
1113
import cpw.mods.modlauncher.api.NamedPath;
@@ -14,7 +16,6 @@
1416

1517
import java.io.IOException;
1618
import java.io.UncheckedIOException;
17-
import java.lang.module.ModuleDescriptor;
1819
import java.nio.file.AccessDeniedException;
1920
import java.nio.file.FileAlreadyExistsException;
2021
import java.nio.file.Files;
@@ -81,22 +82,26 @@ private static void scan(final Path gameDirectory) {
8182
// Skip if the mods dir doesn't exist yet.
8283
return;
8384
}
84-
try (var walk = Files.walk(modsDir, 1)){
85-
walk.forEach(ModDirTransformerDiscoverer::visitFile);
85+
try (var walk = Files.walk(modsDir, 1)) {
86+
// Collect to list first, and then parallel stream it.
87+
// Before JDK 19, Files.walk streams are not parallelized efficiently for small numbers of elements.
88+
// See https://bugs.openjdk.org/browse/JDK-8280915.
89+
walk.toList().parallelStream()
90+
.filter(ModDirTransformerDiscoverer::shouldLoadInServiceLayer)
91+
.forEachOrdered(p -> found.add(new NamedPath(p.getFileName().toString(), p)));
8692
} catch (IOException | IllegalStateException ioe) {
8793
LOGGER.error("Error during early discovery", ioe);
8894
}
8995
}
9096

91-
private static void visitFile(Path path) {
92-
if (!Files.isRegularFile(path)) return;
93-
if (!path.toString().endsWith(".jar")) return;
94-
if (LamdbaExceptionUtils.uncheck(() -> Files.size(path)) == 0) return;
97+
private static boolean shouldLoadInServiceLayer(Path path) {
98+
if (!Files.isRegularFile(path)) return false;
99+
if (!path.toString().endsWith(".jar")) return false;
100+
if (LamdbaExceptionUtils.uncheck(() -> Files.size(path)) == 0) return false;
95101

96-
SecureJar jar = SecureJar.from(path);
97-
jar.moduleDataProvider().descriptor().provides().stream()
98-
.map(ModuleDescriptor.Provides::service)
99-
.filter(SERVICES::contains)
100-
.forEach(s -> found.add(new NamedPath(s, path)));
102+
JarMetadata metadata = JarMetadata.from(new JarContentsBuilder().paths(path).build());
103+
return metadata.providers().stream()
104+
.map(SecureJar.Provider::serviceName)
105+
.anyMatch(SERVICES::contains);
101106
}
102107
}

loader/src/main/java/net/neoforged/fml/loading/moddiscovery/AbstractModProvider.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package net.neoforged.fml.loading.moddiscovery;
77

88
import com.mojang.logging.LogUtils;
9-
import cpw.mods.jarhandling.JarMetadata;
9+
import cpw.mods.jarhandling.JarContentsBuilder;
1010
import cpw.mods.jarhandling.SecureJar;
1111
import net.neoforged.fml.loading.LogMarkers;
1212
import net.neoforged.neoforgespi.language.IConfigurable;
@@ -25,7 +25,6 @@
2525
import java.util.Map;
2626
import java.util.Optional;
2727
import java.util.function.Function;
28-
import java.util.jar.Manifest;
2928

3029
public abstract class AbstractModProvider implements IModProvider
3130
{
@@ -34,30 +33,27 @@ public abstract class AbstractModProvider implements IModProvider
3433
protected static final String MANIFEST = "META-INF/MANIFEST.MF";
3534

3635
protected IModLocator.ModFileOrException createMod(Path... path) {
37-
var mjm = new ModJarMetadata();
38-
var sj = SecureJar.from(
39-
Manifest::new,
40-
jar -> jar.moduleDataProvider().findFile(MODS_TOML).isPresent() ? mjm : JarMetadata.from(jar, path),
41-
null,
42-
path
43-
);
36+
var jarContents = new JarContentsBuilder()
37+
.paths(path)
38+
.build();
4439

4540
IModFile mod;
46-
var type = sj.moduleDataProvider().getManifest().getMainAttributes().getValue(ModFile.TYPE);
41+
var type = jarContents.getManifest().getMainAttributes().getValue(ModFile.TYPE);
4742
if (type == null) {
4843
type = getDefaultJarModType();
4944
}
50-
if (sj.moduleDataProvider().findFile(MODS_TOML).isPresent()) {
45+
if (jarContents.findFile(MODS_TOML).isPresent()) {
5146
LOGGER.debug(LogMarkers.SCAN, "Found {} mod of type {}: {}", MODS_TOML, type, path);
52-
mod = new ModFile(sj, this, ModFileParser::modsTomlParser);
47+
var mjm = new ModJarMetadata(jarContents);
48+
mod = new ModFile(SecureJar.from(jarContents, mjm), this, ModFileParser::modsTomlParser);
49+
mjm.setModFile(mod);
5350
} else if (type != null) {
5451
LOGGER.debug(LogMarkers.SCAN, "Found {} mod of type {}: {}", MANIFEST, type, path);
55-
mod = new ModFile(sj, this, this::manifestParser, type);
52+
mod = new ModFile(SecureJar.from(jarContents), this, this::manifestParser, type);
5653
} else {
5754
return new IModLocator.ModFileOrException(null, new ModFileLoadingException("Invalid mod file found "+ Arrays.toString(path)));
5855
}
5956

60-
mjm.setModFile(mod);
6157
return new IModLocator.ModFileOrException(mod, null);
6258
}
6359

loader/src/main/java/net/neoforged/fml/loading/moddiscovery/ExplodedDirectoryLocator.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package net.neoforged.fml.loading.moddiscovery;
77

88
import com.mojang.logging.LogUtils;
9+
import cpw.mods.jarhandling.JarContentsBuilder;
10+
import cpw.mods.jarhandling.SecureJar;
911
import net.neoforged.fml.loading.LogMarkers;
1012
import net.neoforged.neoforgespi.locating.IModFile;
1113
import net.neoforged.neoforgespi.locating.IModLocator;
@@ -31,12 +33,17 @@ public record ExplodedMod(String modid, List<Path> paths) {}
3133

3234
@Override
3335
public List<IModLocator.ModFileOrException> scanMods() {
34-
explodedMods.forEach(explodedMod ->
35-
ModJarMetadata.buildFile(this,
36-
jar->jar.moduleDataProvider().findFile("/META-INF/mods.toml").isPresent(),
37-
null,
38-
explodedMod.paths().toArray(Path[]::new))
39-
.ifPresentOrElse(f->mods.put(explodedMod, f), () -> LOGGER.warn(LogMarkers.LOADING, "Failed to find exploded resource mods.toml in directory {}", explodedMod.paths().get(0).toString())));
36+
explodedMods.forEach(explodedMod -> {
37+
var jarContents = new JarContentsBuilder().paths(explodedMod.paths().toArray(Path[]::new)).build();
38+
if (jarContents.findFile(AbstractModProvider.MODS_TOML).isPresent()) {
39+
var mjm = new ModJarMetadata(jarContents);
40+
var mf = new ModFile(SecureJar.from(jarContents, mjm), this, ModFileParser::modsTomlParser);
41+
mjm.setModFile(mf);
42+
mods.put(explodedMod, mf);
43+
} else {
44+
LOGGER.warn(LogMarkers.LOADING, "Failed to find exploded resource mods.toml in directory {}", explodedMod.paths().get(0).toString());
45+
}
46+
});
4047
return mods.values().stream().map(mf->new IModLocator.ModFileOrException(mf, null)).toList();
4148
}
4249

loader/src/main/java/net/neoforged/fml/loading/moddiscovery/MinecraftLocator.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.electronwill.nightconfig.core.Config;
99
import com.mojang.logging.LogUtils;
10+
import cpw.mods.jarhandling.JarContentsBuilder;
1011
import cpw.mods.jarhandling.SecureJar;
1112
import net.neoforged.fml.loading.FMLLoader;
1213
import net.neoforged.fml.loading.LogMarkers;
@@ -34,7 +35,14 @@ public class MinecraftLocator extends AbstractModProvider implements IModLocator
3435
public List<IModLocator.ModFileOrException> scanMods() {
3536
final var launchHandler = FMLLoader.getLaunchHandler();
3637
var baseMC = launchHandler.getMinecraftPaths();
37-
var mcjar = ModJarMetadata.buildFile(j->ModFileFactory.FACTORY.build(j, this, this::buildMinecraftTOML), j->true, baseMC.minecraftFilter(), baseMC.minecraftPaths().toArray(Path[]::new)).orElseThrow();
38+
var mcJarContents = new JarContentsBuilder()
39+
.paths(baseMC.minecraftPaths().toArray(Path[]::new))
40+
.pathFilter(baseMC.minecraftFilter())
41+
.build();
42+
var mcJarMetadata = new ModJarMetadata(mcJarContents);
43+
var mcSecureJar = SecureJar.from(mcJarContents, mcJarMetadata);
44+
var mcjar = ModFileFactory.FACTORY.build(mcSecureJar, this, this::buildMinecraftTOML);
45+
mcJarMetadata.setModFile(mcjar);
3846
var artifacts = baseMC.otherArtifacts().stream()
3947
.map(SecureJar::from)
4048
.map(sj -> new ModFile(sj, this, ModFileParser::modsTomlParser))

loader/src/main/java/net/neoforged/fml/loading/moddiscovery/ModFile.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,8 @@
3535
import java.util.jar.Manifest;
3636

3737
public class ModFile implements IModFile {
38-
// Mods either must have a mods.toml or a manifest. We can no longer just put any jar on the classpath.
39-
@Deprecated(forRemoval = true, since = "1.18")
40-
public static final Manifest DEFAULTMANIFEST;
4138
private static final Logger LOGGER = LogUtils.getLogger();
4239

43-
static {
44-
DEFAULTMANIFEST = new Manifest();
45-
DEFAULTMANIFEST.getMainAttributes().putValue("FMLModType", "MOD");
46-
}
47-
4840
private final String jarVersion;
4941
private final ModFileFactory.ModFileInfoParser parser;
5042
private Map<String, Object> fileProperties;

loader/src/main/java/net/neoforged/fml/loading/moddiscovery/ModJarMetadata.java

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,20 @@
55

66
package net.neoforged.fml.loading.moddiscovery;
77

8+
import cpw.mods.jarhandling.JarContents;
89
import cpw.mods.jarhandling.JarMetadata;
9-
import cpw.mods.jarhandling.SecureJar;
10+
import cpw.mods.jarhandling.LazyJarMetadata;
1011
import net.neoforged.neoforgespi.locating.IModFile;
11-
import net.neoforged.neoforgespi.locating.IModLocator;
1212

1313
import java.lang.module.ModuleDescriptor;
14-
import java.nio.file.Path;
1514
import java.util.Objects;
16-
import java.util.Optional;
17-
import java.util.function.BiPredicate;
18-
import java.util.function.Function;
19-
import java.util.function.Predicate;
2015

21-
public final class ModJarMetadata implements JarMetadata {
16+
public final class ModJarMetadata extends LazyJarMetadata implements JarMetadata {
17+
private final JarContents jarContents;
2218
private IModFile modFile;
23-
private ModuleDescriptor descriptor;
2419

25-
// TODO: Remove helper functions to cleanup api
26-
@Deprecated(forRemoval = true, since="1.18")
27-
static Optional<IModFile> buildFile(IModLocator locator, Predicate<SecureJar> jarTest, BiPredicate<String, String> filter, Path... files) {
28-
return buildFile(j->new ModFile(j, locator, ModFileParser::modsTomlParser), jarTest, filter, files);
29-
}
30-
31-
// TODO: Remove helper functions to cleanup api
32-
@Deprecated(forRemoval = true, since="1.18")
33-
static IModFile buildFile(IModLocator locator, Path... files) {
34-
return buildFile(locator, j->true, null, files).orElseThrow(()->new IllegalArgumentException("Failed to find valid JAR file"));
35-
}
36-
37-
// TODO: Remove helper functions to cleanup api
38-
@Deprecated(forRemoval = true, since="1.18")
39-
static Optional<IModFile> buildFile(Function<SecureJar, IModFile> mfConstructor, Predicate<SecureJar> jarTest, BiPredicate<String, String> filter, Path... files) {
40-
var mjm = new ModJarMetadata();
41-
var sj = SecureJar.from(()->ModFile.DEFAULTMANIFEST, j->mjm, filter, files);
42-
if (jarTest.test(sj)) {
43-
var mf = mfConstructor.apply(sj);
44-
mjm.setModFile(mf);
45-
return Optional.of(mf);
46-
} else {
47-
return Optional.empty();
48-
}
49-
}
50-
51-
ModJarMetadata() {
20+
ModJarMetadata(JarContents jarContents) {
21+
this.jarContents = jarContents;
5222
}
5323

5424
public void setModFile(IModFile file) {
@@ -66,17 +36,15 @@ public String version() {
6636
}
6737

6838
@Override
69-
public ModuleDescriptor descriptor() {
70-
if (descriptor != null) return descriptor;
39+
public ModuleDescriptor computeDescriptor() {
7140
var bld = ModuleDescriptor.newAutomaticModule(name())
7241
.version(version())
73-
.packages(modFile.getSecureJar().getPackages());
74-
modFile.getSecureJar().getProviders().stream()
42+
.packages(jarContents.getPackagesExcluding("assets", "data"));
43+
jarContents.getMetaInfServices().stream()
7544
.filter(p -> !p.providers().isEmpty())
7645
.forEach(p -> bld.provides(p.serviceName(), p.providers()));
7746
modFile.getModFileInfo().usesServices().forEach(bld::uses);
78-
descriptor = bld.build();
79-
return descriptor;
47+
return bld.build();
8048
}
8149

8250
public IModFile modFile() {

loader/src/main/java/net/neoforged/fml/loading/targets/CommonDevLaunchHandler.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
package net.neoforged.fml.loading.targets;
77

8+
import cpw.mods.jarhandling.JarContentsBuilder;
89
import cpw.mods.jarhandling.SecureJar;
10+
import cpw.mods.niofs.union.UnionPathFilter;
911
import net.neoforged.fml.loading.FileUtils;
1012

1113
import java.nio.file.Path;
1214
import java.nio.file.Paths;
1315
import java.util.Arrays;
1416
import java.util.List;
1517
import java.util.Optional;
16-
import java.util.function.BiPredicate;
1718
import java.util.regex.Matcher;
1819
import java.util.regex.Pattern;
1920
import java.util.stream.Stream;
@@ -88,12 +89,12 @@ protected static Path findJarOnClasspath(String[] classpath, String match) {
8889
.orElseThrow(() -> new IllegalStateException("Could not find " + match + " in classpath"));
8990
}
9091

91-
protected BiPredicate<String, String> getMcFilter(Path extra, List<Path> minecraft, Stream.Builder<List<Path>> mods) {
92+
protected UnionPathFilter getMcFilter(Path extra, List<Path> minecraft, Stream.Builder<List<Path>> mods) {
9293
final var packages = getExcludedPrefixes();
9394
final var extraPath = extra.toString().replace('\\', '/');
9495

9596
// We serve everything, except for things in the forge packages.
96-
BiPredicate<String, String> mcFilter = (path, base) -> {
97+
UnionPathFilter mcFilter = (path, base) -> {
9798
if (base.equals(extraPath) ||
9899
path.endsWith("/")) return true;
99100
for (var pkg : packages)
@@ -102,12 +103,15 @@ protected BiPredicate<String, String> getMcFilter(Path extra, List<Path> minecra
102103
};
103104

104105
// We need to separate out our resources/code so that we can show up as a different data pack.
105-
var modJar = SecureJar.from((path, base) -> {
106-
if (!path.endsWith(".class")) return true;
107-
for (var pkg : packages)
108-
if (path.startsWith(pkg)) return true;
109-
return false;
110-
}, minecraft.stream().distinct().toArray(Path[]::new));
106+
var modJar = SecureJar.from(new JarContentsBuilder()
107+
.pathFilter((path, base) -> {
108+
if (!path.endsWith(".class")) return true;
109+
for (var pkg : packages)
110+
if (path.startsWith(pkg)) return true;
111+
return false;
112+
})
113+
.paths(minecraft.stream().distinct().toArray(Path[]::new))
114+
.build());
111115
//modJar.getPackages().stream().sorted().forEach(System.out::println);
112116
mods.add(List.of(modJar.getRootPath()));
113117

loader/src/main/java/net/neoforged/fml/loading/targets/CommonLaunchHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import cpw.mods.modlauncher.api.ILaunchHandlerService;
1010
import cpw.mods.modlauncher.api.ITransformingClassLoaderBuilder;
1111
import cpw.mods.modlauncher.api.ServiceRunner;
12+
import cpw.mods.niofs.union.UnionPathFilter;
1213
import net.neoforged.fml.loading.FMLLoader;
1314
import net.neoforged.fml.loading.FileUtils;
1415
import net.neoforged.fml.loading.LogMarkers;
@@ -29,11 +30,10 @@
2930
import java.util.Map;
3031
import java.util.Objects;
3132
import java.util.Optional;
32-
import java.util.function.BiPredicate;
3333
import java.util.stream.Collectors;
3434

3535
public abstract class CommonLaunchHandler implements ILaunchHandlerService {
36-
public record LocatedPaths(List<Path> minecraftPaths, BiPredicate<String, String> minecraftFilter, List<List<Path>> otherModPaths, List<Path> otherArtifacts) {}
36+
public record LocatedPaths(List<Path> minecraftPaths, UnionPathFilter minecraftFilter, List<List<Path>> otherModPaths, List<Path> otherArtifacts) {}
3737

3838
protected static final Logger LOGGER = LogUtils.getLogger();
3939

0 commit comments

Comments
 (0)