From 57d5655f73939422162ddda5180606359688d511 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Sun, 29 Sep 2019 21:34:42 +0100 Subject: [PATCH] Update for ModLauncher 4.0, remove ugly hax, connectors now via manifest --- README.MD | 3 +- build.gradle | 2 +- .../asm/launch/MixinBootstrap.java | 6 - .../{ => platform}/MixinConnectorManager.java | 41 ++- .../platform/MixinPlatformAgentDefault.java | 5 + .../launch/platform/MixinPlatformManager.java | 19 ++ .../org/spongepowered/asm/util/Constants.java | 1 + .../asm/launch/IClassInterceptor.java | 6 +- .../asm/launch/IClassProcessor.java | 6 +- .../asm/launch/MixinLaunchPlugin.java | 159 ++++++--- .../launch/MixinTransformationService.java | 14 +- .../MixinPlatformAgentMinecraftForge.java | 76 +---- .../container/ContainerHandleModLauncher.java | 54 ++++ .../MixinTransformationHandler.java | 12 +- .../asm/service/modlauncher/Internals.java | 305 ------------------ .../modlauncher/MixinServiceModLauncher.java | 65 +--- .../ModLauncherBytecodeProvider.java | 275 ++++++++-------- .../modlauncher/ModLauncherClassProvider.java | 19 +- .../modlauncher/ModLauncherClassTracker.java | 9 +- .../IModLauncherClassBytecodeProvider.java | 34 -- .../ext/IModLauncherClassProvider.java | 34 -- 21 files changed, 413 insertions(+), 732 deletions(-) rename src/main/java/org/spongepowered/asm/launch/{ => platform}/MixinConnectorManager.java (58%) delete mode 100644 src/modlauncher/java/org/spongepowered/asm/service/modlauncher/Internals.java delete mode 100644 src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ext/IModLauncherClassBytecodeProvider.java delete mode 100644 src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ext/IModLauncherClassProvider.java diff --git a/README.MD b/README.MD index 6365dc92..73435e67 100644 --- a/README.MD +++ b/README.MD @@ -13,7 +13,8 @@ The main documentation for **Mixin** can be found in the [Wiki](../../wiki). Additional documentation for individual features and annotations can be found in the extensive [Javadoc](http://jenkins.liteloader.com/job/Mixin/javadoc/). For -additional help use the channel `#spongedev` on the Espernet IRC network. +additional help use the channel `#mixin` on the +[Sponge Discord Server](https://discord.gg/sponge). ### Building Mixin **Mixin** uses the [Gradle](http://gradle.org/) build automation system. To diff --git a/build.gradle b/build.gradle index eb85dd86..3a03e540 100644 --- a/build.gradle +++ b/build.gradle @@ -146,7 +146,7 @@ dependencies { modlauncherCompile configurations.compile modlauncherCompile 'net.sf.jopt-simple:jopt-simple:5.0.4' - modlauncherCompile ('cpw.mods:modlauncher:3.2.0') { + modlauncherCompile ('cpw.mods:modlauncher:4.0.0') { exclude module: 'log4j-core' } diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index dab551b1..cfe56bf1 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -82,11 +82,6 @@ public abstract class MixinBootstrap { * Platform manager instance */ private static MixinPlatformManager platform; - - /** - * Connectors - */ - private static MixinConnectorManager connectors = new MixinConnectorManager(); private MixinBootstrap() {} @@ -193,7 +188,6 @@ public abstract class MixinBootstrap { static void inject() { MixinBootstrap.getPlatform().inject(); - MixinBootstrap.connectors.inject(); } private static boolean isSubsystemRegistered() { diff --git a/src/main/java/org/spongepowered/asm/launch/MixinConnectorManager.java b/src/main/java/org/spongepowered/asm/launch/platform/MixinConnectorManager.java similarity index 58% rename from src/main/java/org/spongepowered/asm/launch/MixinConnectorManager.java rename to src/main/java/org/spongepowered/asm/launch/platform/MixinConnectorManager.java index 552cda96..69635d62 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinConnectorManager.java +++ b/src/main/java/org/spongepowered/asm/launch/platform/MixinConnectorManager.java @@ -22,17 +22,21 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.asm.launch; +package org.spongepowered.asm.launch.platform; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; +import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.mixin.connect.IMixinConnector; +import org.spongepowered.asm.service.IClassProvider; +import org.spongepowered.asm.service.MixinService; /** * Manager for Mixin containers bootstrapping via {@link IMixinConnector} @@ -43,27 +47,50 @@ public class MixinConnectorManager { * Logging to the max */ private static final Logger logger = LogManager.getLogger("mixin"); + + private final Set connectorClasses = new LinkedHashSet(); - private List connectors = new ArrayList(); + private final List connectors = new ArrayList(); MixinConnectorManager() { } + void addConnector(String connectorClass) { + this.connectorClasses.add(connectorClass); + } + void inject() { this.loadConnectors(); this.initConnectors(); } + @SuppressWarnings("unchecked") void loadConnectors() { - ServiceLoader serviceLoader = ServiceLoader.load(IMixinConnector.class); - for (Iterator iterator = serviceLoader.iterator(); iterator.hasNext(); ) { + IClassProvider classProvider = MixinService.getService().getClassProvider(); + + for (String connectorClassName : this.connectorClasses) { + Class connectorClass = null; try { - IMixinConnector connector = iterator.next(); + Class clazz = classProvider.findClass(connectorClassName); + if (!IMixinConnector.class.isAssignableFrom(clazz)) { + MixinConnectorManager.logger.error("Mixin Connector [" + connectorClassName + "] does not implement IMixinConnector"); + continue; + } + connectorClass = (Class)clazz; + } catch (ClassNotFoundException ex) { + MixinConnectorManager.logger.catching(ex); + continue; + } + + try { + IMixinConnector connector = connectorClass.newInstance(); this.connectors.add(connector); - } catch (ServiceConfigurationError e) { - MixinConnectorManager.logger.catching(e); + MixinConnectorManager.logger.info("Successfully loaded Mixin Connector [" + connectorClassName + "]"); + } catch (ReflectiveOperationException ex) { + MixinConnectorManager.logger.warn("Error loading Mixin Connector [" + connectorClassName + "]", ex); } } + this.connectorClasses.clear(); } void initConnectors() { diff --git a/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentDefault.java b/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentDefault.java index 348d26a3..926626cc 100644 --- a/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentDefault.java +++ b/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentDefault.java @@ -53,6 +53,11 @@ public class MixinPlatformAgentDefault extends MixinPlatformAgentAbstract { this.manager.addTokenProvider(provider.trim()); } } + + String connectorClass = this.handle.getAttribute(ManifestAttributes.MIXINCONNECTOR); + if (connectorClass != null) { + this.manager.addConnector(connectorClass.trim()); + } } } diff --git a/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java b/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java index 0d689eff..2bb0634c 100644 --- a/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java +++ b/src/main/java/org/spongepowered/asm/launch/platform/MixinPlatformManager.java @@ -68,6 +68,12 @@ public class MixinPlatformManager { */ private final Map containers = new LinkedHashMap(); + /** + * Connectors + */ + private final MixinConnectorManager connectors = new MixinConnectorManager(); + + /** * Container for this tweaker */ @@ -187,6 +193,8 @@ public class MixinPlatformManager { ex.printStackTrace(); } } + + this.connectors.inject(); } /** @@ -280,5 +288,16 @@ public class MixinPlatformManager { MixinEnvironment.getDefaultEnvironment().registerTokenProviderClass(provider); } + + /** + * Add a mixin connector class for a jar manifest source. Supports only bare + * class names. + * + * @param connectorClass Name of the connector class to load + */ + final void addConnector(String connectorClass) { + this.connectors.addConnector(connectorClass); + + } } diff --git a/src/main/java/org/spongepowered/asm/util/Constants.java b/src/main/java/org/spongepowered/asm/util/Constants.java index 8003e865..8666c51a 100644 --- a/src/main/java/org/spongepowered/asm/util/Constants.java +++ b/src/main/java/org/spongepowered/asm/util/Constants.java @@ -69,6 +69,7 @@ public abstract class Constants { public static final String MAINCLASS = "Main-Class"; public static final String MIXINCONFIGS = "MixinConfigs"; public static final String TOKENPROVIDERS = "MixinTokenProviders"; + public static final String MIXINCONNECTOR = "MixinConnector"; @Deprecated public static final String COMPATIBILITY = "MixinCompatibilityLevel"; diff --git a/src/modlauncher/java/org/spongepowered/asm/launch/IClassInterceptor.java b/src/modlauncher/java/org/spongepowered/asm/launch/IClassInterceptor.java index c1b8e656..e54a679e 100644 --- a/src/modlauncher/java/org/spongepowered/asm/launch/IClassInterceptor.java +++ b/src/modlauncher/java/org/spongepowered/asm/launch/IClassInterceptor.java @@ -27,6 +27,6 @@ package org.spongepowered.asm.launch; /** * Class interceptor. Decorator interface only. Blatant h4x. */ -public interface IClassInterceptor extends IClassProcessor { - -} +//public interface IClassInterceptor extends IClassProcessor { +// +//} diff --git a/src/modlauncher/java/org/spongepowered/asm/launch/IClassProcessor.java b/src/modlauncher/java/org/spongepowered/asm/launch/IClassProcessor.java index 2b64e5b3..5aaf07ac 100644 --- a/src/modlauncher/java/org/spongepowered/asm/launch/IClassProcessor.java +++ b/src/modlauncher/java/org/spongepowered/asm/launch/IClassProcessor.java @@ -45,9 +45,10 @@ public interface IClassProcessor { * @param classType the class to consider * @param isEmpty if the class is empty at present (indicates no backing * file found) + * @param reason the reason for processing * @return the set of Phases the plugin wishes to be called back with */ - public abstract EnumSet handlesClass(Type classType, boolean isEmpty); + public abstract EnumSet handlesClass(Type classType, boolean isEmpty, String reason); /** @@ -56,8 +57,9 @@ public interface IClassProcessor { * @param phase The phase of the supplied class node * @param classNode the classnode to process * @param classType the name of the class + * @param reason the reason for processing * @return true if the class was processed */ - public abstract boolean processClass(Phase phase, ClassNode classNode, Type classType); + public abstract boolean processClass(Phase phase, ClassNode classNode, Type classType, String reason); } diff --git a/src/modlauncher/java/org/spongepowered/asm/launch/MixinLaunchPlugin.java b/src/modlauncher/java/org/spongepowered/asm/launch/MixinLaunchPlugin.java index 11bd46a2..5da42cb2 100644 --- a/src/modlauncher/java/org/spongepowered/asm/launch/MixinLaunchPlugin.java +++ b/src/modlauncher/java/org/spongepowered/asm/launch/MixinLaunchPlugin.java @@ -24,40 +24,51 @@ */ package org.spongepowered.asm.launch; +import java.io.IOException; +import java.net.URL; import java.nio.file.Path; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; +import java.util.Map.Entry; +import org.objectweb.asm.ClassReader; import org.objectweb.asm.Type; import org.objectweb.asm.tree.ClassNode; +import org.spongepowered.asm.launch.platform.CommandLineOptions; import org.spongepowered.asm.service.IClassBytecodeProvider; import org.spongepowered.asm.service.IMixinService; import org.spongepowered.asm.service.MixinService; import org.spongepowered.asm.service.modlauncher.MixinServiceModLauncher; +import com.google.common.io.Resources; + import cpw.mods.modlauncher.api.IEnvironment; import cpw.mods.modlauncher.serviceapi.ILaunchPluginService; /** * Mixin launch plugin */ -public class MixinLaunchPlugin implements ILaunchPluginService { +public class MixinLaunchPlugin implements ILaunchPluginService, IClassBytecodeProvider { /** * Name used for ModLauncher mixin service components */ public static final String NAME = "mixin"; - /** - * Interceptor if present - */ - private IClassInterceptor interceptor; - /** * Class processing components */ private List processors = new ArrayList(); + + /** + * Mixin config names specified on the command line + */ + private List commandLineMixins; + + private ITransformerLoader transformerLoader; + + private MixinServiceModLauncher service; /* (non-Javadoc) * @see cpw.mods.modlauncher.serviceapi.ILaunchPluginService#name() @@ -67,27 +78,31 @@ public class MixinLaunchPlugin implements ILaunchPluginService { return MixinLaunchPlugin.NAME; } - /* (non-Javadoc) - * @see cpw.mods.modlauncher.serviceapi.ILaunchPluginService - * #handlesClass(org.objectweb.asm.Type, boolean) - */ @Override public EnumSet handlesClass(Type classType, boolean isEmpty) { - if (this.interceptor != null) { - // If the interceptor wants to handle this class, it has executive - // power since this entry is almost certainly simulated. If it's - // not a simulated entry then something is probably not right. RIP? - EnumSet interceptorVote = this.interceptor.handlesClass(classType, isEmpty); - if (interceptorVote != null) { - return interceptorVote; - } + throw new IllegalStateException("Outdated ModLauncher"); + } + + @Override + public boolean processClass(Phase phase, ClassNode classNode, Type classType) { + throw new IllegalStateException("Outdated ModLauncher"); + } + + /* (non-Javadoc) + * @see cpw.mods.modlauncher.serviceapi.ILaunchPluginService#handlesClass( + * org.objectweb.asm.Type, boolean, java.lang.String) + */ + @Override + public EnumSet handlesClass(Type classType, boolean isEmpty, final String reason) { + if (MixinLaunchPlugin.NAME.equals(reason)) { + return Phases.NONE; } - + // All processors can nominate phases, we aggregate the results EnumSet phases = EnumSet.noneOf(Phase.class); synchronized (this.processors) { for (IClassProcessor postProcessor : this.processors) { - EnumSet processorVote = postProcessor.handlesClass(classType, isEmpty); + EnumSet processorVote = postProcessor.handlesClass(classType, isEmpty, reason); if (processorVote != null) { phases.addAll(processorVote); } @@ -98,40 +113,53 @@ public class MixinLaunchPlugin implements ILaunchPluginService { } /* (non-Javadoc) - * @see cpw.mods.modlauncher.serviceapi.ILaunchPluginService - * #processClass( + * @see cpw.mods.modlauncher.serviceapi.ILaunchPluginService#processClass( * cpw.mods.modlauncher.serviceapi.ILaunchPluginService.Phase, - * org.objectweb.asm.tree.ClassNode, org.objectweb.asm.Type) + * org.objectweb.asm.tree.ClassNode, org.objectweb.asm.Type, + * java.lang.String) */ @Override - public boolean processClass(Phase phase, ClassNode classNode, Type classType) { - if (this.interceptor != null) { - if (this.interceptor.processClass(phase, classNode, classType)) { - // If the interceptor handles the class, then this is likely a - // simulated entry. Return immediately and don't delegate to - // other processors - return true; - } + public boolean processClass(Phase phase, ClassNode classNode, Type classType, String reason) { + if (MixinLaunchPlugin.NAME.equals(reason)) { + return false; } - + boolean processed = false; synchronized (this.processors) { for (IClassProcessor postProcessor : this.processors) { - processed |= postProcessor.processClass(phase, classNode, classType); + processed |= postProcessor.processClass(phase, classNode, classType, reason); } } return processed; } - - /* (non-Javadoc) - * @see cpw.mods.modlauncher.serviceapi.ILaunchPluginService#addResource( - * java.nio.file.Path, java.lang.String) + + /** + * Initialisation routine, called as a lifecycle event from the + * transformation service */ + void init(IEnvironment environment, List commandLineMixins) { + IMixinService service = MixinService.getService(); + if (!(service instanceof MixinServiceModLauncher)) { + throw new IllegalStateException("Unsupported service type for ModLauncher Mixin Service"); + } + this.service = (MixinServiceModLauncher)service; + synchronized (this.processors) { + this.processors.addAll(this.service.getProcessors()); + } + this.commandLineMixins = commandLineMixins; + this.service.onInit(this); + } + @Override public void addResource(Path resource, String name) { - // This is never called, I'm not sure what it's for + this.service.getPrimaryContainer().addResource(name, resource); + } + + @Override + public void addResources(List> resources) { + this.service.getPrimaryContainer().addResources(resources); } /* (non-Javadoc) @@ -142,24 +170,49 @@ public class MixinLaunchPlugin implements ILaunchPluginService { return null; } - /** - * Initialisation routine, called as a lifecycle event from the - * transformation service - */ - void init(IEnvironment environment, Runnable startupListener) { - IMixinService service = MixinService.getService(); - if (!(service instanceof MixinServiceModLauncher)) { - throw new IllegalStateException("Unsupported service type for ModLauncher Mixin Service"); + @Override + public void initializeLaunch(ITransformerLoader transformerLoader, Path[] specialPaths) { + this.transformerLoader = transformerLoader; + MixinBootstrap.doInit(CommandLineOptions.of(this.commandLineMixins)); + MixinBootstrap.inject(); + this.service.onStartup(); + } + + @Override + public ClassNode getClassNode(String name) throws ClassNotFoundException, IOException { + return this.getClassNode(name, true); + } + + @Override + public ClassNode getClassNode(String name, boolean runTransformers) throws ClassNotFoundException, IOException { + if (!runTransformers) { + throw new IllegalArgumentException("ModLauncher service does not currently support retrieval of untransformed bytecode"); } - IClassBytecodeProvider bytecodeProvider = service.getBytecodeProvider(); - if (bytecodeProvider instanceof IClassInterceptor) { - this.interceptor = (IClassInterceptor)bytecodeProvider; + + byte[] classBytes; + + try { + classBytes = this.transformerLoader.buildTransformedClassNodeFor(name); + } catch (ClassNotFoundException ex) { + URL url = Thread.currentThread().getContextClassLoader().getResource(name + ".class"); + if (url == null) { + throw ex; + } + try { + classBytes = Resources.asByteSource(url).read(); + } catch (IOException ioex) { + throw ex; + } } - MixinServiceModLauncher modLauncherService = (MixinServiceModLauncher)service; - synchronized (this.processors) { - this.processors.addAll(modLauncherService.getProcessors()); + + if (classBytes == null) { + throw new ClassNotFoundException(name.replace('/', '.')); } - modLauncherService.onInit(startupListener); + + ClassNode classNode = new ClassNode(); + ClassReader classReader = new ClassReader(classBytes); + classReader.accept(classNode, ClassReader.EXPAND_FRAMES); + return classNode; } } diff --git a/src/modlauncher/java/org/spongepowered/asm/launch/MixinTransformationService.java b/src/modlauncher/java/org/spongepowered/asm/launch/MixinTransformationService.java index af9d720a..432bb515 100644 --- a/src/modlauncher/java/org/spongepowered/asm/launch/MixinTransformationService.java +++ b/src/modlauncher/java/org/spongepowered/asm/launch/MixinTransformationService.java @@ -24,13 +24,12 @@ */ package org.spongepowered.asm.launch; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.function.BiFunction; -import org.spongepowered.asm.launch.platform.CommandLineOptions; - import com.google.common.collect.ImmutableList; import cpw.mods.modlauncher.api.IEnvironment; @@ -47,7 +46,7 @@ import joptsimple.OptionSpecBuilder; public class MixinTransformationService implements ITransformationService { private ArgumentAcceptingOptionSpec mixinsArgument; - private List commandLineMixins; + private List commandLineMixins = new ArrayList(); private MixinLaunchPlugin plugin; @Override @@ -63,7 +62,7 @@ public class MixinTransformationService implements ITransformationService { @Override public void argumentValues(OptionResult option) { - this.commandLineMixins = option.values(this.mixinsArgument); + this.commandLineMixins.addAll(option.values(this.mixinsArgument)); } @Override @@ -83,14 +82,9 @@ public class MixinTransformationService implements ITransformationService { this.plugin = (MixinLaunchPlugin)launchPlugin; MixinBootstrap.start(); - this.plugin.init(environment, this::onStartup); + this.plugin.init(environment, this.commandLineMixins); } - private void onStartup() { - MixinBootstrap.doInit(CommandLineOptions.of(this.commandLineMixins)); - MixinBootstrap.inject(); - } - @Override public void beginScanning(IEnvironment environment) { } diff --git a/src/modlauncher/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentMinecraftForge.java b/src/modlauncher/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentMinecraftForge.java index 62f37831..82fc449a 100644 --- a/src/modlauncher/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentMinecraftForge.java +++ b/src/modlauncher/java/org/spongepowered/asm/launch/platform/MixinPlatformAgentMinecraftForge.java @@ -24,88 +24,27 @@ */ package org.spongepowered.asm.launch.platform; -import java.lang.reflect.Method; -import java.nio.file.Path; import java.util.Collection; -import java.util.List; import java.util.Locale; -import org.spongepowered.asm.launch.platform.container.ContainerHandleURI; -import org.spongepowered.asm.launch.platform.container.ContainerHandleVirtual; import org.spongepowered.asm.launch.platform.container.IContainerHandle; import org.spongepowered.asm.util.Constants; -import com.google.common.collect.ImmutableSet; - import cpw.mods.modlauncher.Environment; import cpw.mods.modlauncher.Launcher; import cpw.mods.modlauncher.api.IEnvironment; /** - * Platform agent for minecraft forge under ModLauncher + * Platform agent for minecraft forge under ModLauncher, only detects the side */ public class MixinPlatformAgentMinecraftForge extends MixinPlatformAgentAbstract implements IMixinPlatformServiceAgent { - private static final String FML_LOADING_PACKAGE = "net.minecraftforge.fml.loading."; - private static final String FML_DISCOVERY_PACKAGE = MixinPlatformAgentMinecraftForge.FML_LOADING_PACKAGE + "moddiscovery."; - - private static final String LAUNCHER_VERSION_CLASS = MixinPlatformAgentMinecraftForge.FML_LOADING_PACKAGE + "LauncherVersion"; - private static final String LOADING_MOD_LIST_CLASS = MixinPlatformAgentMinecraftForge.FML_LOADING_PACKAGE + "LoadingModList"; - private static final String MOD_FILE_INFO_CLASS = MixinPlatformAgentMinecraftForge.FML_DISCOVERY_PACKAGE + "ModFileInfo"; - private static final String MOD_FILE_CLASS = MixinPlatformAgentMinecraftForge.FML_DISCOVERY_PACKAGE + "ModFile"; - - private static final String GET_MOD_LIST_METHOD = "get"; - private static final String GET_MOD_FILES_METHOD = "getModFiles"; - private static final String GET_MOD_FILE_METHOD = "getFile"; - private static final String GET_FILE_PATH_METHOD = "getFilePath"; - - private ContainerHandleVirtual rootContainer; - + /* (non-Javadoc) + * @see org.spongepowered.asm.launch.platform.IMixinPlatformServiceAgent + * #init() + */ @Override public void init() { - try { - Class.forName(MixinPlatformAgentMinecraftForge.LAUNCHER_VERSION_CLASS); - } catch (Exception ex) { - MixinPlatformAgentAbstract.logger.debug("FML Launcher Version class was not found, assuming FML is not present"); - return; - } - - String activity = "Initialising FML containers"; - try { - // Normally I'd cache these reflection objects, but this should only get called once - activity = "Resolving FML LoadingModList class"; - Class clModList = Class.forName(MixinPlatformAgentMinecraftForge.LOADING_MOD_LIST_CLASS); - activity = "Resolving FML ModFileInfo class"; - Class clModFileInfo = Class.forName(MixinPlatformAgentMinecraftForge.MOD_FILE_INFO_CLASS); - activity = "Resolving FML ModFile class"; - Class clModFile = Class.forName(MixinPlatformAgentMinecraftForge.MOD_FILE_CLASS); - - activity = "Resolving FML LoadingModList::get method"; - Method mdGetModListInstance = clModList.getDeclaredMethod(MixinPlatformAgentMinecraftForge.GET_MOD_LIST_METHOD); - activity = "Resolving FML LoadingModList::getModFiles method"; - Method mdGetModFiles = clModList.getDeclaredMethod(MixinPlatformAgentMinecraftForge.GET_MOD_FILES_METHOD); - activity = "Resolving FML ModFileInfo::getFile method"; - Method mdGetModFile = clModFileInfo.getDeclaredMethod(MixinPlatformAgentMinecraftForge.GET_MOD_FILE_METHOD); - activity = "Resolving FML ModFile::getFilePath method"; - Method mdGetFilePath = clModFile.getDeclaredMethod(MixinPlatformAgentMinecraftForge.GET_FILE_PATH_METHOD); - - activity = "Calling FML LoadingModList::get method to obtain LoadingModList singleton"; - Object loadingModList = mdGetModListInstance.invoke(null); - activity = "Calling FML LoadingModList::getModFiles method to obtain list of mod candidates"; - List modlist = (List)mdGetModFiles.invoke(loadingModList); - for (Object modFileInfo : modlist) { - activity = "Calling ModFileInfo::getFile method to obtain mod file info"; - Object modFile = mdGetModFile.invoke(modFileInfo); - activity = "Calling ModFile::getFilePath method to obtain mod file location"; - Path path = (Path)mdGetFilePath.invoke(modFile); - activity = "Creating ContainerHandleURI instance to contain FML mod: " + path; - this.rootContainer.add(new ContainerHandleURI(path.toUri())); - } - - } catch (Exception ex) { - MixinPlatformAgentAbstract.logger.error("Error reading FML mod list contents during activity: {}", activity); - MixinPlatformAgentAbstract.logger.catching(ex); - } } /* (non-Javadoc) @@ -142,10 +81,7 @@ public class MixinPlatformAgentMinecraftForge extends MixinPlatformAgentAbstract */ @Override public Collection getMixinContainers() { - if (this.rootContainer == null) { - this.rootContainer = new ContainerHandleVirtual("forge"); - } - return ImmutableSet.of(this.rootContainer); + return null; } } diff --git a/src/modlauncher/java/org/spongepowered/asm/launch/platform/container/ContainerHandleModLauncher.java b/src/modlauncher/java/org/spongepowered/asm/launch/platform/container/ContainerHandleModLauncher.java index 49ac4b8b..1d5b0a3f 100644 --- a/src/modlauncher/java/org/spongepowered/asm/launch/platform/container/ContainerHandleModLauncher.java +++ b/src/modlauncher/java/org/spongepowered/asm/launch/platform/container/ContainerHandleModLauncher.java @@ -24,15 +24,69 @@ */ package org.spongepowered.asm.launch.platform.container; +import java.nio.file.Path; +import java.util.List; +import java.util.Map.Entry; + /** * ModLauncher root container */ public class ContainerHandleModLauncher extends ContainerHandleVirtual { + + /** + * Container handle for resources offered by ModLauncher + */ + public class Resource extends ContainerHandleURI { + + private String name; + private Path path; + + public Resource(String name, Path path) { + super(path.toUri()); + this.name = name; + this.path = path; + } + + public String getName() { + return this.name; + } + + public Path getPath() { + return this.path; + } + + @Override + public String toString() { + return String.format("ContainerHandleModLauncher.Resource(%s:%s)", this.name, this.path); + } + + } public ContainerHandleModLauncher(String name) { super(name); } + /** + * Add a resource to to this container + * + * @param name Resource name + * @param path Resource path + */ + public void addResource(String name, Path path) { + this.add(new Resource(name, path)); + } + + /** + * Add a collection of resources to this container + * + * @param resources Resources to add + */ + public void addResources(List> resources) { + for (Entry resource : resources) { + this.addResource(resource.getKey(), resource.getValue()); + } + } + @Override public String toString() { return String.format("ModLauncher Root Container(%x)", this.hashCode()); diff --git a/src/modlauncher/java/org/spongepowered/asm/mixin/transformer/MixinTransformationHandler.java b/src/modlauncher/java/org/spongepowered/asm/mixin/transformer/MixinTransformationHandler.java index c8b1cfde..2c10fd4a 100644 --- a/src/modlauncher/java/org/spongepowered/asm/mixin/transformer/MixinTransformationHandler.java +++ b/src/modlauncher/java/org/spongepowered/asm/mixin/transformer/MixinTransformationHandler.java @@ -60,10 +60,10 @@ public class MixinTransformationHandler implements IClassProcessor { /* (non-Javadoc) * @see org.spongepowered.asm.launch.IClassProcessor#handlesClass( - * org.objectweb.asm.Type, boolean) + * org.objectweb.asm.Type, boolean, java.lang.String) */ @Override - public EnumSet handlesClass(Type classType, boolean isEmpty) { + public EnumSet handlesClass(Type classType, boolean isEmpty, String reason) { if (!isEmpty) { return Phases.AFTER_ONLY; } @@ -77,13 +77,13 @@ public class MixinTransformationHandler implements IClassProcessor { } /* (non-Javadoc) - * @see org.spongepowered.asm.launch.IClassProcessor - * #processClass( + * @see org.spongepowered.asm.launch.IClassProcessor#processClass( * cpw.mods.modlauncher.serviceapi.ILaunchPluginService.Phase, - * org.objectweb.asm.tree.ClassNode, org.objectweb.asm.Type) + * org.objectweb.asm.tree.ClassNode, org.objectweb.asm.Type, + * java.lang.String) */ @Override - public synchronized boolean processClass(Phase phase, ClassNode classNode, Type classType) { + public synchronized boolean processClass(Phase phase, ClassNode classNode, Type classType, String reason) { if (phase == Phase.BEFORE) { return false; } diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/Internals.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/Internals.java deleted file mode 100644 index 7abbcb63..00000000 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/Internals.java +++ /dev/null @@ -1,305 +0,0 @@ -/* - * This file is part of Mixin, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.spongepowered.asm.service.modlauncher; - -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Function; -import java.util.jar.Manifest; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.appender.AbstractAppender; - -import cpw.mods.modlauncher.ClassTransformer; -import cpw.mods.modlauncher.Launcher; -import cpw.mods.modlauncher.TransformingClassLoader; - -/** - * Shim for accessing resources inside modlauncher that Mixin needs to operate. - * Ideally in the long run facilities can be provided directly within - * modlauncher to make this shim unnecessary. - */ -class Internals { - - /** - * Temporary - */ - static class MixinAppender extends AbstractAppender { - - private final List startupListeners = new ArrayList(); - - MixinAppender() { - super("MixinLogWatcherAppender", null, null); - this.start(); - } - - void addListener(Runnable listener) { - synchronized (this.startupListeners) { - this.startupListeners.add(listener); - } - } - - @Override - public void append(LogEvent event) { - if (event.getLevel() != Level.INFO || !event.getMessage().getFormat().startsWith("Launching target '")) { - return; - } - - // transition to DEFAULT - synchronized (this.startupListeners) { - for (Runnable listener : this.startupListeners) { - listener.run(); - } - } - } - - } - - /** - * Logger - */ - private static final Logger logger = LogManager.getLogger("mixin"); - - private static MixinAppender appender; - private static org.apache.logging.log4j.core.Logger launcherLogger; - - private static Internals instance; - - private static final String FD_CLASSLOADER = "classLoader"; - private static final String FD_CLASSTRANSFORMER = "classTransformer"; - - private static final String FD_MANIFESTFINDER = "manifestFinder"; - private static final String FD_CLASSBYTESFINDER = "classBytesFinder"; - - private static final String MD_TRANSFORM = "transform"; - -// private static final String MD_LOADCLASS = "loadClass"; - - private TransformingClassLoader transformingClassLoader; - private ClassTransformer classTransformer; - - private Function manifestFinder; - private Function classBytesFinder; - -// private Method mdLoadClass; - private Method mdTransform; - - TransformingClassLoader getTransformingClassLoader() { - if (this.transformingClassLoader == null) { - this.transformingClassLoader = Internals.getField(Launcher.class, Launcher.INSTANCE, - Internals.FD_CLASSLOADER); - } - return this.transformingClassLoader; - } - - ClassTransformer getClassTransformer() { - if (this.classTransformer == null) { - this.classTransformer = Internals.getField(TransformingClassLoader.class, - this.getTransformingClassLoader(), Internals.FD_CLASSTRANSFORMER); - } - return this.classTransformer; - } - - Function getManifestFinder() { - if (this.manifestFinder == null) { - this.manifestFinder = Internals.>getField(TransformingClassLoader.class, - this.getTransformingClassLoader(), Internals.FD_MANIFESTFINDER); - } - return this.manifestFinder; - } - - Function getClassBytesFinder() { - if (this.classBytesFinder == null) { - this.classBytesFinder = Internals.>getField(TransformingClassLoader.class, - this.getTransformingClassLoader(), Internals.FD_CLASSBYTESFINDER); - } - return this.classBytesFinder; - } - -// /** -// * Invokes 'loadClass' on the Transforming classloader -// * -// * @param name class name to load -// * @param resolve If true then resolve the class -// * @return loaded class -// * @throws ClassNotFoundException propagated -// */ -// public Class findClass(String name, boolean resolve) throws ClassNotFoundException { -// if (this.mdLoadClass == null) { -// this.mdLoadClass = Internals.getMethod(TransformingClassLoader.class, Internals.MD_LOADCLASS); -// } -// if (this.mdLoadClass != null) { -// try { -// return (Class)this.mdLoadClass.invoke(this.getTransformingClassLoader(), name, resolve); -// } catch (ReflectiveOperationException ex) { -// ex.printStackTrace(); -// } -// } -// return Class.forName("name"); -// } - - byte[] simulateClassLoad(String name, boolean runTransformers) throws ClassNotFoundException { - Function classBytesFinder = this.getClassBytesFinder(); - - final String path = name.replace('.', '/').concat(".class"); - final URL classResource = classBytesFinder.apply(path); - byte[] classBytes; - if (classResource != null) { - try (AutoURLConnection urlConnection = new AutoURLConnection(classResource, this.getManifestFinder())) { - final int length = urlConnection.getContentLength(); - final InputStream is = urlConnection.getInputStream(); - classBytes = new byte[length]; - int pos = 0, remain = length, read; - while ((read = is.read(classBytes, pos, remain)) != -1 && remain > 0) { - pos += read; - remain -= read; - } - @SuppressWarnings("unused") - Manifest jarManifest = urlConnection.getJarManifest(); - } catch (IOException e) { - throw new ClassNotFoundException("Failed to find class bytes for " + name, e); - } - } else { - classBytes = new byte[0]; - } - return this.transform(classBytes, name); - } - - /** - * Copied directly from ModLauncher, ideally won't be necessary in the long - * run - */ - static class AutoURLConnection implements AutoCloseable { - private final URLConnection urlConnection; - private final InputStream inputStream; - private final Function manifestFinder; - - AutoURLConnection(URL url, Function manifestFinder) throws IOException { - this.urlConnection = url.openConnection(); - this.inputStream = this.urlConnection.getInputStream(); - this.manifestFinder = manifestFinder; - } - - @Override - public void close() throws IOException { - this.inputStream.close(); - } - - int getContentLength() { - return this.urlConnection.getContentLength(); - } - - InputStream getInputStream() { - return this.inputStream; - } - - Manifest getJarManifest() { - return this.manifestFinder.apply(this.urlConnection); - } - } - - private byte[] transform(byte[] inputClass, String className) { - if (this.mdTransform == null) { - this.mdTransform = Internals.getMethod(ClassTransformer.class, Internals.MD_TRANSFORM, byte[].class, String.class); - } - if (this.mdTransform == null) { - return inputClass; - } - try { - return (byte[])this.mdTransform.invoke(this.getClassTransformer(), inputClass, className); - } catch (InvocationTargetException ex) { - Throwable cause = ex.getCause(); - if (cause instanceof RuntimeException) { - throw (RuntimeException)cause; - } - throw new RuntimeException(cause); - } catch (ReflectiveOperationException ex) { - Internals.logger.catching(ex); - } - return inputClass; - } - - @SuppressWarnings("unchecked") - private static T getField(Class clazz, C instance, String fieldName) { - try { - Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - return (T)field.get(instance); - } catch (Exception ex) { -// ex.printStackTrace(); - } - return null; - } - - private static Method getMethod(Class clazz, String methodName, Class... parameterTypes) { - try { - Method method = clazz.getDeclaredMethod(methodName, parameterTypes); - method.setAccessible(true); - return method; - } catch (Exception ex) { -// ex.printStackTrace(); - } - return null; - } - - void registerStartupListener(Runnable listener) { - if (Internals.appender == null) { - Logger launcherLog = LogManager.getLogger("cpw.mods.modlauncher.LaunchServiceHandler"); - if (!(launcherLog instanceof org.apache.logging.log4j.core.Logger)) { - return; - } - - Internals.appender = new MixinAppender(); - Internals.launcherLogger = (org.apache.logging.log4j.core.Logger)launcherLog; - Internals.launcherLogger.addAppender(Internals.appender); - } - - Internals.appender.addListener(listener); - } - - void destroyLogWatcher() { - if (Internals.launcherLogger != null) { - Internals.launcherLogger.removeAppender(Internals.appender); - } - } - - static synchronized Internals getInstance() { - if (Internals.instance == null) { - Internals.instance = new Internals(); - } - return Internals.instance; - } - -} diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java index 401d6431..403f63ef 100644 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java +++ b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java @@ -26,27 +26,20 @@ package org.spongepowered.asm.service.modlauncher; import java.io.InputStream; import java.util.Collection; -import java.util.Iterator; -import java.util.ServiceConfigurationError; -import java.util.ServiceLoader; import org.spongepowered.asm.launch.IClassProcessor; import org.spongepowered.asm.launch.platform.container.ContainerHandleModLauncher; -import org.spongepowered.asm.launch.platform.container.IContainerHandle; import org.spongepowered.asm.mixin.MixinEnvironment.Phase; import org.spongepowered.asm.mixin.transformer.MixinTransformationHandler; import org.spongepowered.asm.service.IClassBytecodeProvider; import org.spongepowered.asm.service.IClassProvider; import org.spongepowered.asm.service.ITransformer; import org.spongepowered.asm.service.MixinServiceAbstract; -import org.spongepowered.asm.service.modlauncher.ext.IModLauncherClassBytecodeProvider; -import org.spongepowered.asm.service.modlauncher.ext.IModLauncherClassProvider; import org.spongepowered.asm.util.IConsumer; import com.google.common.collect.ImmutableList; import cpw.mods.modlauncher.Launcher; -import cpw.mods.modlauncher.TransformingClassLoader; import cpw.mods.modlauncher.api.ITransformationService; /** @@ -57,17 +50,17 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { /** * Specification version to check for at startup */ - private static final String MODLAUNCHER_SPECIFICATION_VERSION = "1.0"; + private static final String MODLAUNCHER_SPECIFICATION_VERSION = "4.0"; /** * Class provider, either uses hacky internals or provided service */ - private IModLauncherClassProvider classProvider; + private IClassProvider classProvider; /** * Bytecode provider, either uses hacky internals or provided service */ - private IModLauncherClassBytecodeProvider bytecodeProvider; + private IClassBytecodeProvider bytecodeProvider; /** * Container for the mixin pipeline which is called by the launch plugin @@ -88,25 +81,29 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { * Only allow onInit to be called once */ private volatile boolean initialised; + + /** + * Root container + */ + private ContainerHandleModLauncher rootContainer = new ContainerHandleModLauncher(this.getName()); /** * Begin init * - * @param startupListener Lifecyle listener + * @param bytecodeProvider bytecode provider */ - public void onInit(Runnable startupListener) { + public void onInit(IClassBytecodeProvider bytecodeProvider) { if (this.initialised) { throw new IllegalStateException("Already initialised"); } this.initialised = true; - Internals.getInstance().registerStartupListener(startupListener); - Internals.getInstance().registerStartupListener(MixinServiceModLauncher.this::onStartup); + this.bytecodeProvider = bytecodeProvider; } /** * Lifecycle event */ - private void onStartup() { + public void onStartup() { this.phaseConsumer.accept(Phase.DEFAULT); } @@ -149,7 +146,7 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { @Override public IClassProvider getClassProvider() { if (this.classProvider == null) { - this.classProvider = MixinServiceModLauncher.createClassProvider(); + this.classProvider = new ModLauncherClassProvider(); } return this.classProvider; } @@ -160,7 +157,7 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { @Override public IClassBytecodeProvider getBytecodeProvider() { if (this.bytecodeProvider == null) { - this.bytecodeProvider = MixinServiceModLauncher.createBytecodeProvider(); + throw new IllegalStateException("Service initialisation incomplete"); } return this.bytecodeProvider; } @@ -199,8 +196,8 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { * @see org.spongepowered.asm.service.IMixinService#getPrimaryContainer() */ @Override - public IContainerHandle getPrimaryContainer() { - return new ContainerHandleModLauncher(this.getName()); + public ContainerHandleModLauncher getPrimaryContainer() { + return this.rootContainer; } /* (non-Javadoc) @@ -209,12 +206,6 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { */ @Override public InputStream getResourceAsStream(String name) { - TransformingClassLoader tcl = Internals.getInstance().getTransformingClassLoader(); - if (tcl != null) { - return tcl.getResourceAsStream(name); - } - - // Probably not what we want :/ return Thread.currentThread().getContextClassLoader().getResourceAsStream(name); } @@ -282,28 +273,4 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { ); } - private static IModLauncherClassProvider createClassProvider() { - IModLauncherClassProvider service = - MixinServiceModLauncher.loadFirstAvailableService(IModLauncherClassProvider.class); - return service != null ? service : new ModLauncherClassProvider(); - } - - private static IModLauncherClassBytecodeProvider createBytecodeProvider() { - IModLauncherClassBytecodeProvider service = - MixinServiceModLauncher.loadFirstAvailableService(IModLauncherClassBytecodeProvider.class); - return service != null ? service : new ModLauncherBytecodeProvider(); - } - - private static TService loadFirstAvailableService(Class serviceClass) { - Iterator loader = ServiceLoader.load(serviceClass).iterator(); - if (loader.hasNext()) { - try { - return loader.next(); - } catch (ServiceConfigurationError ex) { - ex.printStackTrace(); - } - } - return null; - } - } diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherBytecodeProvider.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherBytecodeProvider.java index 97132532..b033e11f 100644 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherBytecodeProvider.java +++ b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherBytecodeProvider.java @@ -24,136 +24,147 @@ */ package org.spongepowered.asm.service.modlauncher; -import java.io.IOException; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +//import java.io.IOException; +//import java.util.EnumSet; +//import java.util.HashMap; +//import java.util.HashSet; +//import java.util.Map; +//import java.util.Set; +// +//import org.objectweb.asm.Type; +//import org.objectweb.asm.tree.ClassNode; +//import org.spongepowered.asm.launch.IClassInterceptor; +//import org.spongepowered.asm.service.IClassBytecodeProvider; +//import org.spongepowered.asm.service.modlauncher.ext.IModLauncherClassBytecodeProvider; +// +//import cpw.mods.modlauncher.serviceapi.ILaunchPluginService.Phase; -import org.objectweb.asm.Type; -import org.objectweb.asm.tree.ClassNode; -import org.spongepowered.asm.launch.IClassInterceptor; -import org.spongepowered.asm.service.modlauncher.ext.IModLauncherClassBytecodeProvider; - -import cpw.mods.modlauncher.serviceapi.ILaunchPluginService.Phase; - -/** - * Bytecode provider for ModLauncher environment. - */ -class ModLauncherBytecodeProvider implements IModLauncherClassBytecodeProvider, IClassInterceptor { - - /** - * Exception thrown by the interceptor to terminate the transformation - * pipeline once we have obtained a ClassNode in the state we want it. - */ - static class CaptureException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - ClassNode captured; - - CaptureException(ClassNode captured) { - this.captured = captured; - } - - } - - /** - * Filthy hacks - */ - private final Internals internals = Internals.getInstance(); - - /** - * Class names we want to capture - */ - private final Map captures = new HashMap(); - - /** - * Classes which are initially empty - */ - private final Set emptyClasses = new HashSet(); - - ModLauncherBytecodeProvider() { - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IClassBytecodeProvider#getClassNode( - * java.lang.String) - */ - @Override - public synchronized ClassNode getClassNode(String name) throws ClassNotFoundException, IOException { - return this.getClassNode(name, true, true); - } - - @Override - public EnumSet handlesClass(Type classType, boolean isEmpty) { - synchronized (this.captures) { - String internalName = classType.getInternalName(); - Phase capturePhase = this.captures.get(internalName); - if (capturePhase != null) { - if (isEmpty) { - this.emptyClasses.add(internalName); - } - return EnumSet.of(capturePhase); - } - } - return null; - } - - @Override - public boolean processClass(Phase phase, ClassNode classNode, Type classType) { - synchronized (this.captures) { - Phase capturePhase = this.captures.get(classType.getInternalName()); - if (capturePhase != null && phase == capturePhase) { - throw new CaptureException(classNode); - } - } - return false; - } - - /* (non-Javadoc) - * @see org.spongepowered.asm.service.IClassBytecodeProvider#getClassNode( - * java.lang.String, boolean) - */ - @Override - public synchronized ClassNode getClassNode(String name, boolean runTransformers) throws ClassNotFoundException, IOException { - return this.getClassNode(name, runTransformers, false); - } - - private synchronized ClassNode getClassNode(String name, boolean runTransformers, boolean ignoreEmpty) - throws ClassNotFoundException { - - String internalName = name.replace('.', '/'); - - synchronized (this.captures) { - this.emptyClasses.remove(internalName); - this.captures.put(internalName, runTransformers ? Phase.AFTER : Phase.BEFORE); - } - - ClassNode captured = null; - try { - this.internals.simulateClassLoad(name, runTransformers); - } catch (CaptureException ex) { - captured = ex.captured; - synchronized (this.captures) { - if (this.emptyClasses.contains(internalName)) { - if (!ignoreEmpty) { - throw new ClassNotFoundException(name); - } - } - } - } finally { - synchronized (this.captures) { - this.captures.remove(internalName); - } - } - - if (captured == null) { - throw new ClassNotFoundException("Unable to obtain ClassNode for: " + name); - } - - return captured; - } - -} +///** +// * Bytecode provider for ModLauncher environment. +// */ +//class ModLauncherBytecodeProvider implements IClassBytecodeProvider { +// +// @Override +// public ClassNode getClassNode(String name) throws ClassNotFoundException, IOException { +// return this.getClassNode(name); +// } +// +// @Override +// public ClassNode getClassNode(String name, boolean runTransformers) throws ClassNotFoundException, IOException { +// return null; +// } +// +//// /** +//// * Exception thrown by the interceptor to terminate the transformation +//// * pipeline once we have obtained a ClassNode in the state we want it. +//// */ +//// static class CaptureException extends RuntimeException { +//// +//// private static final long serialVersionUID = 1L; +//// +//// ClassNode captured; +//// +//// CaptureException(ClassNode captured) { +//// this.captured = captured; +//// } +//// +//// } +//// +//// /** +//// * Filthy hacks +//// */ +//// private final Internals internals = Internals.getInstance(); +//// +//// /** +//// * Class names we want to capture +//// */ +//// private final Map captures = new HashMap(); +//// +//// /** +//// * Classes which are initially empty +//// */ +//// private final Set emptyClasses = new HashSet(); +//// +//// ModLauncherBytecodeProvider() { +//// } +//// +//// /* (non-Javadoc) +//// * @see org.spongepowered.asm.service.IClassBytecodeProvider#getClassNode( +//// * java.lang.String) +//// */ +//// @Override +//// public synchronized ClassNode getClassNode(String name) throws ClassNotFoundException, IOException { +//// return this.getClassNode(name, true, true); +//// } +//// +//// @Override +//// public EnumSet handlesClass(Type classType, boolean isEmpty) { +//// synchronized (this.captures) { +//// String internalName = classType.getInternalName(); +//// Phase capturePhase = this.captures.get(internalName); +//// if (capturePhase != null) { +//// if (isEmpty) { +//// this.emptyClasses.add(internalName); +//// } +//// return EnumSet.of(capturePhase); +//// } +//// } +//// return null; +//// } +//// +//// @Override +//// public boolean processClass(Phase phase, ClassNode classNode, Type classType) { +//// synchronized (this.captures) { +//// Phase capturePhase = this.captures.get(classType.getInternalName()); +//// if (capturePhase != null && phase == capturePhase) { +//// throw new CaptureException(classNode); +//// } +//// } +//// return false; +//// } +//// +//// /* (non-Javadoc) +//// * @see org.spongepowered.asm.service.IClassBytecodeProvider#getClassNode( +//// * java.lang.String, boolean) +//// */ +//// @Override +//// public synchronized ClassNode getClassNode(String name, boolean runTransformers) throws ClassNotFoundException, IOException { +//// return this.getClassNode(name, runTransformers, false); +//// } +//// +//// private synchronized ClassNode getClassNode(String name, boolean runTransformers, boolean ignoreEmpty) +//// throws ClassNotFoundException { +//// +//// String internalName = name.replace('.', '/'); +//// +//// synchronized (this.captures) { +//// this.emptyClasses.remove(internalName); +//// this.captures.put(internalName, runTransformers ? Phase.AFTER : Phase.BEFORE); +//// } +//// +//// ClassNode captured = null; +//// try { +//// this.internals.simulateClassLoad(name, runTransformers); +//// } catch (CaptureException ex) { +//// captured = ex.captured; +//// synchronized (this.captures) { +//// if (this.emptyClasses.contains(internalName)) { +//// if (!ignoreEmpty) { +//// throw new ClassNotFoundException(name); +//// } +//// } +//// } +//// } finally { +//// synchronized (this.captures) { +//// this.captures.remove(internalName); +//// } +//// } +//// +//// if (captured == null) { +//// throw new ClassNotFoundException("Unable to obtain ClassNode for: " + name); +//// } +//// +//// return captured; +//// } +// +//} diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassProvider.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassProvider.java index ba7429a1..c3c6f346 100644 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassProvider.java +++ b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassProvider.java @@ -26,21 +26,15 @@ package org.spongepowered.asm.service.modlauncher; import java.net.URL; -import org.spongepowered.asm.service.modlauncher.ext.IModLauncherClassProvider; +import org.spongepowered.asm.service.IClassProvider; import cpw.mods.gross.Java9ClassLoaderUtil; import cpw.mods.modlauncher.Launcher; -import cpw.mods.modlauncher.TransformingClassLoader; /** * Class provider for use under ModLauncher */ -class ModLauncherClassProvider implements IModLauncherClassProvider { - - /** - * Here be dragons - */ - private final Internals internals = Internals.getInstance(); +class ModLauncherClassProvider implements IClassProvider { ModLauncherClassProvider() { } @@ -60,7 +54,7 @@ class ModLauncherClassProvider implements IModLauncherClassProvider { */ @Override public Class findClass(String name) throws ClassNotFoundException { - return Class.forName(name, true, this.getClassLoader()); + return Class.forName(name, true, Thread.currentThread().getContextClassLoader()); } /* (non-Javadoc) @@ -69,12 +63,7 @@ class ModLauncherClassProvider implements IModLauncherClassProvider { */ @Override public Class findClass(String name, boolean initialize) throws ClassNotFoundException { - return Class.forName(name, initialize, this.getClassLoader()); - } - - private ClassLoader getClassLoader() { - TransformingClassLoader tcl = this.internals.getTransformingClassLoader(); - return tcl != null ? tcl : Launcher.class.getClassLoader(); + return Class.forName(name, initialize, Thread.currentThread().getContextClassLoader()); } /* (non-Javadoc) diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassTracker.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassTracker.java index 7f5f3e26..e9fdc56b 100644 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassTracker.java +++ b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ModLauncherClassTracker.java @@ -64,10 +64,10 @@ public class ModLauncherClassTracker implements IClassProcessor { /* (non-Javadoc) * @see org.spongepowered.asm.launch.IClassProcessor#handlesClass( - * org.objectweb.asm.Type, boolean) + * org.objectweb.asm.Type, boolean, java.lang.String) */ @Override - public EnumSet handlesClass(Type classType, boolean isEmpty) { + public EnumSet handlesClass(Type classType, boolean isEmpty, String reason) { String name = classType.getClassName(); synchronized (this.invalidClasses) { if (this.invalidClasses.contains(name)) { @@ -85,10 +85,11 @@ public class ModLauncherClassTracker implements IClassProcessor { /* (non-Javadoc) * @see org.spongepowered.asm.launch.IClassProcessor#processClass( * cpw.mods.modlauncher.serviceapi.ILaunchPluginService.Phase, - * org.objectweb.asm.tree.ClassNode, org.objectweb.asm.Type) + * org.objectweb.asm.tree.ClassNode, org.objectweb.asm.Type, + * java.lang.String) */ @Override - public boolean processClass(Phase phase, ClassNode classNode, Type classType) { + public boolean processClass(Phase phase, ClassNode classNode, Type classType, String reason) { return false; } diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ext/IModLauncherClassBytecodeProvider.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ext/IModLauncherClassBytecodeProvider.java deleted file mode 100644 index 3bb3bed5..00000000 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ext/IModLauncherClassBytecodeProvider.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of Mixin, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.spongepowered.asm.service.modlauncher.ext; - -import org.spongepowered.asm.service.IClassBytecodeProvider; - -/** - * Service-loadable interface for modlauncher class bytecode provider - */ -public interface IModLauncherClassBytecodeProvider extends IClassBytecodeProvider { - -} diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ext/IModLauncherClassProvider.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ext/IModLauncherClassProvider.java deleted file mode 100644 index 49a7a365..00000000 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/ext/IModLauncherClassProvider.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of Mixin, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.spongepowered.asm.service.modlauncher.ext; - -import org.spongepowered.asm.service.IClassProvider; - -/** - * Service-loadable interface for modlauncher class provider - */ -public interface IModLauncherClassProvider extends IClassProvider { - -}