From 3f3950d7f4f9208da5247e0211cf3e314c5e5b1c Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Tue, 13 Jan 2015 13:21:55 +0000 Subject: [PATCH] Allow mixin configs to specify a companion plugin class, closes #8 --- .../asm/launch/MixinBootstrap.java | 8 ++ .../extensibility/IMixinConfigPlugin.java | 93 +++++++++++++++++++ .../asm/mixin/extensibility/IMixinInfo.java | 72 ++++++++++++++ .../asm/mixin/transformer/MixinConfig.java | 68 ++++++++++++-- .../asm/mixin/transformer/MixinData.java | 14 +++ .../asm/mixin/transformer/MixinInfo.java | 85 ++++++++++++----- .../mixin/transformer/MixinTransformer.java | 11 ++- 7 files changed, 317 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 9314d6c9..e7fe725f 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -27,6 +27,7 @@ package org.spongepowered.asm.launch; import net.minecraft.launchwrapper.Launch; import org.spongepowered.asm.mixin.MixinEnvironment; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; public abstract class MixinBootstrap { @@ -34,10 +35,17 @@ public abstract class MixinBootstrap { public static final String INIT_KEY = "mixin.initialised"; public static final String MIXIN_PACKAGE = "org.spongepowered.asm.mixin."; + private static final String ASM_PACKAGE = "org.objectweb.asm."; public static final String TRANSFORMER_CLASS = MixinBootstrap.MIXIN_PACKAGE + "transformer.MixinTransformer"; + static { + Launch.classLoader.addClassLoaderExclusion(MixinBootstrap.ASM_PACKAGE); + + @SuppressWarnings("unused") + IMixinConfigPlugin forceClassLoad = null; + Launch.classLoader.addClassLoaderExclusion(MixinBootstrap.MIXIN_PACKAGE); } diff --git a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin.java b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin.java new file mode 100644 index 00000000..9ad10654 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin.java @@ -0,0 +1,93 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * 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.mixin.extensibility; + +import java.util.List; + +import org.objectweb.asm.tree.ClassNode; + +/** + *

A companion plugin for a mixin configuration object. Objects implementing this interface get some limited power of veto over the mixin load + * process as well as an opportunity to apply their own transformations to the target class pre- and post-transform. Since all methods in this class + * are called indirectly from the transformer, the same precautions as for writing class transformers should be taken. Implementors should take care + * to not reference any game classes, and avoid referencing other classes in their own mod except those specificially designed to be available at + * early startup, such as coremod classes or other standalone bootstrap objects.

+ * + *

Instances of plugins are created by specifying the "plugin" key in the mixin config JSON as the fully-qualified class name of a class + * implementing this interface.

+ */ +public interface IMixinConfigPlugin { + + /** + * Called after the plugin is instantiated, do any setup here. + * + * @param mixinPackage The mixin root package from the config + */ + public abstract void onLoad(String mixinPackage); + + /** + * Called only if the "referenceMap" key in the config is not set. This allows the refmap file name to be supplied by the plugin + * programatically if desired. Returning null will revert to the default behaviour of using the default refmap json file. + * + * @return Path to the refmap resource or null to revert to the default + */ + public abstract String getRefMapperConfig(); + + /** + * Called during mixin intialisation, allows this plugin to control whether a specific will be applied to the specified target. Returning false + * will remove the target from the mixin's target set, and if all targets are removed then the mixin will not be applied at all. + * + * @param targetClassName Fully qualified class name of the target class + * @param mixinClassName Fully qualified class name of the mixin + * @return True to allow the mixin to be applied, or false to remove it from target's mixin set + */ + public abstract boolean shouldApplyMixin(String targetClassName, String mixinClassName); + + /** + * After mixins specified in the configuration have been processed, this method is called to allow the plugin to add any additional mixins to + * load. It should return a list of mixin class names or return null if the plugin does not wish to append any mixins of its own. + */ + public abstract List getMixins(); + + /** + * Called immediately before a mixin is applied to a target class, allows any pre-application transformations to be applied + * + * @param targetClassName Transformed name of the target class + * @param targetClass Target class tree + * @param mixinClassName Name of the mixin class + * @param mixinInfo Information about this mixin + */ + public abstract void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo); + + /** + * Called immediately after a mixin is applied to a target class, allows any post-application transformations to be applied + * + * @param targetClassName Transformed name of the target class + * @param targetClass Target class tree + * @param mixinClassName Name of the mixin class + * @param mixinInfo Information about this mixin + */ + public abstract void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo); +} diff --git a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java new file mode 100644 index 00000000..9d33ac0e --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinInfo.java @@ -0,0 +1,72 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * 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.mixin.extensibility; + +import java.util.List; + +import org.objectweb.asm.tree.ClassNode; + +public interface IMixinInfo { + + /** + * Get the simple name of the mixin + */ + public abstract String getName(); + + /** + * Get the name of the mixin class + */ + public abstract String getClassName(); + + /** + * Get the ref (internal name) of the mixin class + */ + public abstract String getClassRef(); + + /** + * Get the class bytecode + */ + public abstract byte[] getClassBytes(); + + /** + * True if the superclass of the mixin is not the direct superclass of one or more targets + */ + public abstract boolean isDetachedSuper(); + + /** + * Get a new tree for the class bytecode + */ + public abstract ClassNode getClassNode(int flags); + + /** + * Get the target classes for this mixin + */ + public abstract List getTargetClasses(); + + /** + * Get the mixin priority + */ + public abstract int getPriority(); +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java index 31813404..556f3b9c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -27,14 +27,17 @@ package org.spongepowered.asm.mixin.transformer; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import net.minecraft.launchwrapper.Launch; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.spongepowered.asm.mixin.MixinEnvironment; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; import com.google.gson.Gson; @@ -44,6 +47,11 @@ import com.google.gson.annotations.SerializedName; * Mixin configuration bundle */ class MixinConfig { + + /** + * Global list of mixin classes, so we can skip any duplicates + */ + private static final Set globalMixinList = new HashSet(); /** * Log even more things @@ -97,6 +105,17 @@ class MixinConfig { */ private transient String name; + /** + * Name of the {@link IMixinConfigPlugin} to hook onto this MixinConfig + */ + @SerializedName("plugin") + private String pluginClassName; + + /** + * Config plugin, if supplied + */ + private transient IMixinConfigPlugin plugin; + /** * Reference mapper for injectors */ @@ -118,6 +137,20 @@ class MixinConfig { private void onLoad(String name) { this.name = name; + if (this.pluginClassName != null) { + try { + Class pluginClass = Class.forName(this.pluginClassName, true, Launch.classLoader); + this.plugin = (IMixinConfigPlugin)pluginClass.newInstance(); + + if (this.plugin != null) { + this.plugin.onLoad(this.mixinPackage); + } + } catch (Throwable th) { + th.printStackTrace(); + this.plugin = null; + } + } + if (!this.mixinPackage.endsWith(".")) { this.mixinPackage += "."; } @@ -125,7 +158,13 @@ class MixinConfig { Launch.classLoader.addClassLoaderExclusion(this.mixinPackage); if (this.refMapperConfig == null) { - this.refMapperConfig = ReferenceMapper.DEFAULT_RESOURCE; + if (this.plugin != null) { + this.refMapperConfig = this.plugin.getRefMapperConfig(); + } + + if (this.refMapperConfig == null) { + this.refMapperConfig = ReferenceMapper.DEFAULT_RESOURCE; + } } this.refMapper = ReferenceMapper.read(this.refMapperConfig); @@ -143,37 +182,46 @@ class MixinConfig { private void initialise() { this.initialised = true; - this.initialiseSide(this.mixinClasses); + this.initialiseMixins(this.mixinClasses, false); switch (MixinEnvironment.getCurrentEnvironment().getSide()) { case CLIENT : - this.initialiseSide(this.mixinClassesClient); + this.initialiseMixins(this.mixinClassesClient, false); break; case SERVER : - this.initialiseSide(this.mixinClassesServer); + this.initialiseMixins(this.mixinClassesServer, false); break; case UNKNOWN : this.logger.warn("Mixin environment was unable to detect the current side, sided mixins will not be applied"); break; } + + if (this.plugin != null) { + List pluginMixins = this.plugin.getMixins(); + this.initialiseMixins(pluginMixins, true); + } } - private void initialiseSide(List mixinClasses) { + private void initialiseMixins(List mixinClasses, boolean suppressPlugin) { if (mixinClasses == null) { return; } for (String mixinClass : mixinClasses) { - if (mixinClass == null) { + if (mixinClass == null || MixinConfig.globalMixinList.contains(mixinClass)) { continue; } + try { - MixinInfo mixin = new MixinInfo(this, mixinClass, true); - for (String targetClass : mixin.getTargetClasses()) { - this.mixinsFor(targetClass).add(mixin); + MixinInfo mixin = new MixinInfo(this, mixinClass, true, this.plugin, suppressPlugin); + if (mixin.getTargetClasses().size() > 0) { + MixinConfig.globalMixinList.add(mixinClass); + for (String targetClass : mixin.getTargetClasses()) { + this.mixinsFor(targetClass).add(mixin); + } } } catch (Exception ex) { - ex.printStackTrace(); + this.logger.error(ex.getMessage(), ex); } } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java index 9518a021..5d7bd60b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinData.java @@ -277,4 +277,18 @@ public class MixinData { } return value; } + + /** + * Called immediately before the mixin is applied to targetClass + */ + public void preApply(String transformedName, ClassNode targetClass) { + this.info.preApply(transformedName, targetClass); + } + + /** + * Called immediately after the mixin is applied to targetClass + */ + public void postApply(String transformedName, ClassNode targetClass) { + this.info.postApply(transformedName, targetClass); + } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index f323b97d..096be2f0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -36,12 +36,14 @@ import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; import org.spongepowered.asm.mixin.InvalidMixinException; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; import org.spongepowered.asm.util.ASMHelper; /** * Runtime information bundle about a mixin */ -class MixinInfo extends TreeInfo implements Comparable { +class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { /** * Global order of mixin infos, used to determine ordering between mixins with equivalent priority @@ -98,25 +100,35 @@ class MixinInfo extends TreeInfo implements Comparable { */ private final transient byte[] mixinBytes; + /** + * Configuration plugin + */ + private final transient IMixinConfigPlugin plugin; + /** * Internal ctor, called by {@link MixinConfig} * * @param parent * @param mixinName * @param runTransformers + * @param plugin + * @param suppressPlugin + * @throws ClassNotFoundException */ - MixinInfo(MixinConfig parent, String mixinName, boolean runTransformers) { + MixinInfo(MixinConfig parent, String mixinName, boolean runTransformers, IMixinConfigPlugin plugin, boolean suppressPlugin) + throws ClassNotFoundException { this.parent = parent; this.name = mixinName; this.className = parent.getMixinPackage() + mixinName; + this.plugin = plugin; // Read the class bytes and transform this.mixinBytes = this.loadMixinClass(this.className, runTransformers); ClassNode classNode = this.getClassNode(0); this.classInfo = ClassInfo.fromClassNode(classNode); - this.targetClasses = this.readTargetClasses(classNode); this.priority = this.readPriority(classNode); + this.targetClasses = this.readTargetClasses(classNode, suppressPlugin); this.detachedSuper = this.validateTargetClasses(classNode); } @@ -124,9 +136,10 @@ class MixinInfo extends TreeInfo implements Comparable { * Read the target class names from the {@link Mixin} annotation * * @param classNode + * @param suppressPlugin * @return */ - private List readTargetClasses(ClassNode classNode) { + private List readTargetClasses(ClassNode classNode, boolean suppressPlugin) { AnnotationNode mixin = ASMHelper.getInvisibleAnnotation(classNode, Mixin.class); if (mixin == null) { throw new InvalidMixinException(String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); @@ -135,7 +148,10 @@ class MixinInfo extends TreeInfo implements Comparable { List targetClasses = ASMHelper.getAnnotationValue(mixin); List targetClassNames = new ArrayList(); for (Type targetClass : targetClasses) { - targetClassNames.add(targetClass.getClassName()); + String targetClassName = targetClass.getClassName(); + if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetClassName, this.className)) { + targetClassNames.add(targetClassName); + } } return targetClassNames; @@ -188,6 +204,7 @@ class MixinInfo extends TreeInfo implements Comparable { /** * Get the simple name of the mixin */ + @Override public String getName() { return this.name; } @@ -195,6 +212,7 @@ class MixinInfo extends TreeInfo implements Comparable { /** * Get the name of the mixin class */ + @Override public String getClassName() { return this.className; } @@ -202,6 +220,7 @@ class MixinInfo extends TreeInfo implements Comparable { /** * Get the ref (internal name) of the mixin class */ + @Override public String getClassRef() { return this.classInfo.getName(); } @@ -209,6 +228,7 @@ class MixinInfo extends TreeInfo implements Comparable { /** * Get the class bytecode */ + @Override public byte[] getClassBytes() { return this.mixinBytes; } @@ -216,6 +236,7 @@ class MixinInfo extends TreeInfo implements Comparable { /** * True if the superclass of the mixin is not the direct superclass of one or more targets */ + @Override public boolean isDetachedSuper() { return this.detachedSuper; } @@ -223,10 +244,27 @@ class MixinInfo extends TreeInfo implements Comparable { /** * Get a new tree for the class bytecode */ + @Override public ClassNode getClassNode(int flags) { return TreeInfo.getClassNode(this.mixinBytes, flags); } + /** + * Get the target classes for this mixin + */ + @Override + public List getTargetClasses() { + return Collections.unmodifiableList(this.targetClasses); + } + + /** + * Get the mixin priority + */ + @Override + public int getPriority() { + return this.priority; + } + /** * Get a new mixin data container for this info * @@ -236,33 +274,20 @@ class MixinInfo extends TreeInfo implements Comparable { public MixinData createData(ClassNode target) { return new MixinData(this, target); } - - /** - * Get the target classes for this mixin - */ - public List getTargetClasses() { - return Collections.unmodifiableList(this.targetClasses); - } - - /** - * Get the mixin priority - */ - public int getPriority() { - return this.priority; - } /** * @param mixinClassName * @param runTransformers * @return + * @throws ClassNotFoundException */ - private byte[] loadMixinClass(String mixinClassName, boolean runTransformers) { + private byte[] loadMixinClass(String mixinClassName, boolean runTransformers) throws ClassNotFoundException { byte[] mixinBytes = null; try { mixinBytes = TreeInfo.loadClass(mixinClassName, runTransformers); } catch (ClassNotFoundException ex) { - throw new InvalidMixinException(String.format("The specified mixin '%s' was not found", mixinClassName)); + throw new ClassNotFoundException(String.format("The specified mixin '%s' was not found", mixinClassName)); } catch (IOException ex) { this.logger.warn("Failed to load mixin %s, the specified mixin will not be applied", mixinClassName); throw new InvalidMixinException("An error was encountered whilst loading the mixin class", ex); @@ -284,4 +309,22 @@ class MixinInfo extends TreeInfo implements Comparable { } return (this.priority - other.priority); } + + /** + * Called immediately before the mixin is applied to targetClass + */ + public void preApply(String transformedName, ClassNode targetClass) { + if (this.plugin != null) { + this.plugin.preApply(transformedName, targetClass, this.className, this); + } + } + + /** + * Called immediately after the mixin is applied to targetClass + */ + public void postApply(String transformedName, ClassNode targetClass) { + if (this.plugin != null) { + this.plugin.postApply(transformedName, targetClass, this.className, this); + } + } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java index 7763ef54..98ae4566 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -208,9 +208,11 @@ public class MixinTransformer extends TreeTransformer { for (StackTraceElement stackElement : Thread.currentThread().getStackTrace()) { if (transformerClasses.contains(stackElement.getClassName())) { this.logger.warn("Re-entrance detected from transformer " + stackElement.getClassName() + ", this will cause serious problems."); - break; + return; } } + + this.logger.warn("Re-entrance detected from unknown source, this will cause serious problems.", new RuntimeException()); } /** @@ -231,7 +233,7 @@ public class MixinTransformer extends TreeTransformer { for (MixinInfo mixin : mixins) { this.logger.info("Mixing {} into {}", mixin.getName(), transformedName); - this.applyMixin(targetClass, mixin.createData(targetClass)); + this.applyMixin(transformedName, targetClass, mixin.createData(targetClass)); } // Extension point @@ -264,17 +266,20 @@ public class MixinTransformer extends TreeTransformer { /** * Apply the mixin described by mixin to the supplied classNode * + * @param transformedName * @param targetClass * @param mixin */ - protected void applyMixin(ClassNode targetClass, MixinData mixin) { + protected void applyMixin(String transformedName, ClassNode targetClass, MixinData mixin) { try { + mixin.preApply(transformedName, targetClass); this.applyMixinInterfaces(targetClass, mixin); this.applyMixinAttributes(targetClass, mixin); this.applyMixinFields(targetClass, mixin); this.applyMixinMethods(targetClass, mixin); this.applyInitialisers(targetClass, mixin); this.applyInjections(targetClass, mixin); + mixin.postApply(transformedName, targetClass); } catch (Exception ex) { throw new InvalidMixinException("Unexpecteded error whilst applying the mixin class", ex); }