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

Make TransformingClassLoader usable in FML tests. #127

Merged
merged 1 commit into from
Jun 12, 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
16 changes: 11 additions & 5 deletions src/main/java/cpw/mods/modlauncher/LaunchPluginHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.*;
import cpw.mods.modlauncher.serviceapi.ILaunchPluginService;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static cpw.mods.modlauncher.LogMarkers.*;

Expand All @@ -41,10 +43,14 @@ public class LaunchPluginHandler {
private final Map<String, ILaunchPluginService> plugins;

public LaunchPluginHandler(final ModuleLayerHandler layerHandler) {
this.plugins = ServiceLoaderUtils.streamServiceLoader(()->ServiceLoader.load(layerHandler.getLayer(IModuleLayerManager.Layer.BOOT).orElseThrow(), ILaunchPluginService.class),
e->LOGGER.fatal(MODLAUNCHER, "Encountered serious error loading launch plugin service. Things will not work well", e))
.collect(Collectors.toMap(ILaunchPluginService::name, Function.identity()));
final var modlist = plugins.entrySet().stream().map(e->Map.of(
this(ServiceLoaderUtils.streamServiceLoader(()->ServiceLoader.load(layerHandler.getLayer(IModuleLayerManager.Layer.BOOT).orElseThrow(), ILaunchPluginService.class),
e->LOGGER.fatal(MODLAUNCHER, "Encountered serious error loading launch plugin service. Things will not work well", e)));
}

@VisibleForTesting
public LaunchPluginHandler(Stream<ILaunchPluginService> plugins) {
this.plugins = plugins.collect(Collectors.toMap(ILaunchPluginService::name, Function.identity()));
final var modlist = this.plugins.entrySet().stream().map(e->Map.of(
"name", e.getKey(),
"type", "PLUGINSERVICE",
"file", ServiceLoaderUtils.fileNameFor(e.getValue().getClass())))
Expand All @@ -55,7 +61,7 @@ public LaunchPluginHandler(final ModuleLayerHandler layerHandler) {
throw new RuntimeException("The MODLIST isn't set, huh?");
});
}
LOGGER.debug(MODLAUNCHER,"Found launch plugins: [{}]", ()-> String.join(",", plugins.keySet()));
LOGGER.debug(MODLAUNCHER,"Found launch plugins: [{}]", ()-> String.join(",", this.plugins.keySet()));
}

public Optional<ILaunchPluginService> get(final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import cpw.mods.modlauncher.api.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.VisibleForTesting;

import java.util.*;
import java.util.stream.*;
Expand All @@ -35,7 +36,8 @@ public class TransformationServiceDecorator {
private final ITransformationService service;
private boolean isValid;

TransformationServiceDecorator(ITransformationService service) {
@VisibleForTesting
public TransformationServiceDecorator(ITransformationService service) {
this.service = service;
}

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/cpw/mods/modlauncher/TransformingClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import cpw.mods.cl.ModuleClassLoader;
import cpw.mods.modlauncher.api.*;
import org.jetbrains.annotations.VisibleForTesting;

import java.lang.module.Configuration;
import java.util.*;
Expand All @@ -33,13 +34,19 @@ public class TransformingClassLoader extends ModuleClassLoader {
}
private final ClassTransformer classTransformer;

public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, ModuleLayerHandler moduleLayerHandler) {
public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, IModuleLayerManager moduleLayerHandler) {
super("TRANSFORMER", moduleLayerHandler.getLayer(IModuleLayerManager.Layer.GAME).orElseThrow().configuration(), List.of(moduleLayerHandler.getLayer(IModuleLayerManager.Layer.SERVICE).orElseThrow()));
this.classTransformer = new ClassTransformer(transformStore, pluginHandler, this);
}

TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, final Environment environment, final Configuration configuration, List<ModuleLayer> parentLayers) {
super("TRANSFORMER", configuration, parentLayers);
@VisibleForTesting
public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, final Environment environment, final Configuration configuration, List<ModuleLayer> parentLayers) {
this(transformStore, pluginHandler, environment, configuration, parentLayers, null);
}

@VisibleForTesting
public TransformingClassLoader(TransformStore transformStore, LaunchPluginHandler pluginHandler, final Environment environment, final Configuration configuration, List<ModuleLayer> parentLayers, ClassLoader parentClassLoader) {
super("TRANSFORMER", configuration, parentLayers, parentClassLoader);
TransformerAuditTrail tat = new TransformerAuditTrail();
environment.computePropertyIfAbsent(IEnvironment.Keys.AUDITTRAIL.get(), v->tat);
this.classTransformer = new ClassTransformer(transformStore, pluginHandler, this, tat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public static <T> Stream<T> streamWithErrorHandling(ServiceLoader<T> sl, Consume
}

public static String fileNameFor(Class<?> clazz) {
// Used in test scenarios where services might come from normal CP
if (clazz.getModule().getLayer() == null) {
return clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
}

return clazz.getModule().getLayer().configuration()
.findModule(clazz.getModule().getName())
.flatMap(rm->rm.reference().location())
Expand Down
Loading