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

Use CoreMods directly instead of going through SPI abstraction #106

Merged
merged 2 commits into from
Mar 31, 2024
Merged
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
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mergetool_version=2.0.0
accesstransformers_version=10.0.1
coremods_version=6.0.0
coremods_version=7.0.3
eventbus_version=7.0.16
modlauncher_version=10.0.9
modlauncher_version=10.1.10
securejarhandler_version=2.1.23
bootstraplauncher_version=1.1.2
asm_version=9.5
Expand Down
1 change: 0 additions & 1 deletion loader/src/main/java/net/neoforged/fml/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ private ModLoader()
this.stateManager = new ModStateManager();
CrashReportCallables.registerCrashCallable("ModLauncher", FMLLoader::getLauncherInfo);
CrashReportCallables.registerCrashCallable("ModLauncher launch target", FMLLoader::launcherHandlerName);
CrashReportCallables.registerCrashCallable("ModLauncher naming", FMLLoader::getNaming);
CrashReportCallables.registerCrashCallable("ModLauncher services", this::computeModLauncherServiceList);
CrashReportCallables.registerCrashCallable("FML Language Providers", this::computeLanguageList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package net.neoforged.fml.core;

import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
import cpw.mods.modlauncher.api.LambdaExceptionUtils;
import net.neoforged.fml.IModStateTransition;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.ModLoadingStage;
Expand All @@ -21,7 +21,7 @@
record ParallelTransition(ModLoadingStage stage, Class<? extends ParallelDispatchEvent> event) implements IModStateTransition {
@Override
public Supplier<Stream<EventGenerator<?>>> eventFunctionStream() {
return () -> Stream.of(IModStateTransition.EventGenerator.fromFunction(LamdbaExceptionUtils.rethrowFunction((ModContainer mc) -> event.getConstructor(ModContainer.class, ModLoadingStage.class).newInstance(mc, stage))));
return () -> Stream.of(IModStateTransition.EventGenerator.fromFunction(LambdaExceptionUtils.rethrowFunction((ModContainer mc) -> event.getConstructor(ModContainer.class, ModLoadingStage.class).newInstance(mc, stage))));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
public class FMLEnvironment
{
public static final Dist dist = FMLLoader.getDist();
public static final String naming = FMLLoader.getNaming();
public static final boolean production = FMLLoader.isProduction() || System.getProperties().containsKey("production");
public static final boolean secureJarsEnabled = FMLLoader.isSecureJarEnabled();

static void setupInteropEnvironment(IEnvironment environment) {
environment.computePropertyIfAbsent(IEnvironment.Keys.NAMING.get(), v->naming);
environment.computePropertyIfAbsent(Environment.Keys.DIST.get(), v->dist);
}

Expand Down
34 changes: 6 additions & 28 deletions loader/src/main/java/net/neoforged/fml/loading/FMLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cpw.mods.modlauncher.util.ServiceLoaderUtils;
import net.neoforged.accesstransformer.api.AccessTransformerEngine;
import net.neoforged.accesstransformer.ml.AccessTransformerService;
import net.neoforged.coremod.CoreModScriptingEngine;
import net.neoforged.fml.common.asm.RuntimeDistCleaner;
import net.neoforged.fml.loading.mixin.DeferredMixinConfigRegistration;
import net.neoforged.fml.loading.moddiscovery.BackgroundScanHandler;
Expand All @@ -20,7 +21,6 @@
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.loading.targets.CommonLaunchHandler;
import net.neoforged.neoforgespi.Environment;
import net.neoforged.neoforgespi.coremod.ICoreModProvider;
import org.slf4j.Logger;

import java.io.IOException;
Expand All @@ -35,10 +35,9 @@ public class FMLLoader
private static final Logger LOGGER = LogUtils.getLogger();
private static AccessTransformerEngine accessTransformer;
private static ModDiscoverer modDiscoverer;
private static ICoreModProvider coreModProvider;
private static CoreModScriptingEngine coreModEngine;
private static LanguageLoadingProvider languageLoadingProvider;
private static Dist dist;
private static String naming;
private static LoadingModList loadingModList;
private static RuntimeDistCleaner runtimeDistCleaner;
private static Path gamePath;
Expand Down Expand Up @@ -88,20 +87,8 @@ static void onInitialLoad(IEnvironment environment, Set<String> otherServices) t
});
LOGGER.debug(LogMarkers.CORE, "Found Runtime Dist Cleaner");

var coreModProviders = ServiceLoaderUtils.streamWithErrorHandling(ServiceLoader.load(FMLLoader.class.getModule().getLayer(), ICoreModProvider.class), sce -> LOGGER.error(LogMarkers.CORE, "Failed to load a coremod library, expect problems", sce)).toList();

if (coreModProviders.isEmpty()) {
LOGGER.error(LogMarkers.CORE, "Found no coremod provider. Cannot run");
throw new IncompatibleEnvironmentException("No coremod library found");
} else if (coreModProviders.size() > 1) {
LOGGER.error(LogMarkers.CORE, "Found multiple coremod providers : {}. Cannot run", coreModProviders.stream().map(p -> p.getClass().getName()).collect(Collectors.toList()));
throw new IncompatibleEnvironmentException("Multiple coremod libraries found");
}

coreModProvider = coreModProviders.get(0);
final Package coremodPackage = coreModProvider.getClass().getPackage();
LOGGER.debug(LogMarkers.CORE,"FML found CoreMod version : {}", coremodPackage.getImplementationVersion());

coreModEngine = new CoreModScriptingEngine();
LOGGER.debug(LogMarkers.CORE, "FML found CoreMods version : {}", coreModEngine.getClass().getPackage().getImplementationVersion());

LOGGER.debug(LogMarkers.CORE, "Found ForgeSPI package implementation version {}", Environment.class.getPackage().getImplementationVersion());
LOGGER.debug(LogMarkers.CORE, "Found ForgeSPI package specification {}", Environment.class.getPackage().getSpecificationVersion());
Expand Down Expand Up @@ -138,7 +125,6 @@ static void setupLaunchHandler(final IEnvironment environment, final Map<String,
launchHandlerName = launchHandler.get().name();
gamePath = environment.getProperty(IEnvironment.Keys.GAMEDIR.get()).orElse(Paths.get(".").toAbsolutePath());

naming = commonLaunchHandler.getNaming();
dist = commonLaunchHandler.getDist();
production = commonLaunchHandler.isProduction();

Expand Down Expand Up @@ -169,8 +155,8 @@ public static List<ITransformationService.Resource> completeScan(IModuleLayerMan
return List.of(modValidator.getModResources());
}

public static ICoreModProvider getCoreModProvider() {
return coreModProvider;
static CoreModScriptingEngine getCoreModEngine() {
return coreModEngine;
}

public static LanguageLoadingProvider getLanguageLoadingProvider()
Expand Down Expand Up @@ -218,14 +204,6 @@ public static Path getGamePath()
return gamePath;
}

public static String getNaming() {
return naming;
}

public static Optional<BiFunction<INameMappingService.Domain, String, String>> getNameFunction(final String naming) {
return Launcher.INSTANCE.environment().findNameMapping(naming);
}

public static String getLauncherInfo() {
return Launcher.INSTANCE.environment().getProperty(IEnvironment.Keys.MLIMPL_VERSION.get()).orElse("MISSING");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public void argumentValues(OptionResult option)
}

@Override
public @NotNull List<ITransformer> transformers()
public @NotNull List<? extends ITransformer<?>> transformers()
{
LOGGER.debug(CORE, "Loading coremod transformers");
return new ArrayList<>(FMLLoader.getCoreModProvider().getCoreModTransformers());
return FMLLoader.getCoreModEngine().initializeCoreMods();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package net.neoforged.fml.loading;

import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
import cpw.mods.modlauncher.api.LambdaExceptionUtils;
import net.neoforged.fml.loading.mixin.DeferredMixinConfigRegistration;
import net.neoforged.fml.loading.moddiscovery.BackgroundScanHandler;
import net.neoforged.fml.loading.moddiscovery.ModFile;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void addCoreMods()
.map(ModFileInfo::getFile)
.map(ModFile::getCoreMods)
.flatMap(List::stream)
.forEach(FMLLoader.getCoreModProvider()::addCoreMod);
.forEach(FMLLoader.getCoreModEngine()::loadCoreMod);
}

public void addMixinConfigs() {
Expand Down Expand Up @@ -151,7 +151,7 @@ private URL findNextURL() {
final ModFileInfo 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));
return LambdaExceptionUtils.uncheck(()->new URL("modjar://" + next.getMods().get(0).getModId() + "/" + resourceName));
}
}
return null;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<Resource> beginScanning(IEnvironment environment) {
}

@Override
public @NotNull List<ITransformer> transformers() {
public @NotNull List<? extends ITransformer<?>> transformers() {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package net.neoforged.fml.loading;

import com.mojang.logging.LogUtils;
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
import cpw.mods.modlauncher.api.LambdaExceptionUtils;
import cpw.mods.modlauncher.api.NamedPath;
import cpw.mods.modlauncher.serviceapi.ITransformerDiscoveryService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -88,7 +88,7 @@ private static void scan(final Path gameDirectory) {
private static boolean shouldLoadInServiceLayer(Path path) {
if (!Files.isRegularFile(path)) return false;
if (!path.toString().endsWith(".jar")) return false;
if (LamdbaExceptionUtils.uncheck(() -> Files.size(path)) == 0) return false;
if (LambdaExceptionUtils.uncheck(() -> Files.size(path)) == 0) return false;

return TransformerDiscovererConstants.shouldLoadInServiceLayer(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package net.neoforged.fml.loading;

import com.mojang.logging.LogUtils;
import cpw.mods.modlauncher.api.LamdbaExceptionUtils;
import cpw.mods.modlauncher.api.LambdaExceptionUtils;
import org.slf4j.Logger;

import java.io.IOException;
Expand Down Expand Up @@ -84,7 +84,7 @@ public long getContentLengthLong() {
// Used to cache protectiondomains by "top level object" aka the modid
@Override
public URL getURL() {
return LamdbaExceptionUtils.uncheck(()->new URL("modjar://"+modid));
return LambdaExceptionUtils.uncheck(()->new URL("modjar://"+modid));
}

public Optional<Manifest> getManifest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

package net.neoforged.fml.loading.moddiscovery;

import net.neoforged.neoforgespi.coremod.ICoreModFile;
import net.neoforged.coremod.ICoreModScriptSource;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;

public class CoreModFile implements ICoreModFile {
public class CoreModFile implements ICoreModScriptSource {
private final Path internalPath;
private final ModFile file;
private final String name;
Expand All @@ -29,8 +29,8 @@ public Reader readCoreMod() throws IOException {
}

@Override
public Path getPath() {
return this.internalPath;
public String getDebugSource() {
return this.internalPath.toString();
}

@Override
Expand All @@ -45,6 +45,6 @@ public String getOwnerId() {

@Override
public String toString() {
return "{Name: " + name + ", Owner: " + getOwnerId() + " @ " + getPath() + "}";
return "{Name: " + name + ", Owner: " + getOwnerId() + " @ " + getDebugSource() + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package net.neoforged.fml.loading.moddiscovery;

import cpw.mods.modlauncher.api.LamdbaExceptionUtils.Supplier_WithExceptions;
import cpw.mods.modlauncher.api.LambdaExceptionUtils;
import net.neoforged.fml.loading.StringUtils;

import java.nio.file.Path;
Expand All @@ -14,8 +14,6 @@
import java.util.function.BiPredicate;
import java.util.zip.ZipFile;

import static cpw.mods.modlauncher.api.LamdbaExceptionUtils.*;

public enum InvalidModIdentifier {

OLDFORGE(filePresent("mcmod.info")),
Expand All @@ -39,12 +37,12 @@ private String getReason()

public static Optional<String> identifyJarProblem(Path path)
{
Optional<ZipFile> zfo = optionalFromException(() -> new ZipFile(path.toFile()));
Optional<ZipFile> zfo = tryOpenFile(path);
Optional<String> result = Arrays.stream(values()).
filter(i -> i.ident.test(path, zfo)).
map(InvalidModIdentifier::getReason).
findAny();
zfo.ifPresent(rethrowConsumer(ZipFile::close));
zfo.ifPresent(LambdaExceptionUtils.rethrowConsumer(ZipFile::close));
return result;
}

Expand All @@ -53,14 +51,10 @@ private static BiPredicate<Path, Optional<ZipFile>> filePresent(String filename)
return (f, zfo) -> zfo.map(zf -> zf.getEntry(filename) != null).orElse(false);
}

private static <T> Optional<T> optionalFromException(Supplier_WithExceptions<T, ? extends Exception> supp)
{
try
{
return Optional.of(supp.get());
}
catch (Exception e)
{
private static Optional<ZipFile> tryOpenFile(Path path) {
try {
return Optional.of(new ZipFile(path.toFile()));
} catch (Exception ignored) {
return Optional.empty();
}
}
Expand Down
Loading
Loading