Skip to content

Commit

Permalink
Make TransformingClassLoader usable in FML tests. (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte authored Jun 12, 2024
1 parent 4612c65 commit 0f24dec
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
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

0 comments on commit 0f24dec

Please sign in to comment.