From 36576e239bb12e3638b0d5a9d2a11a668be4f36e Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Wed, 13 Jan 2016 01:09:12 +0000 Subject: [PATCH] Add support for injector grouping and config-wide Inject::require value --- gradle.properties | 2 +- .../asm/launch/MixinBootstrap.java | 2 +- .../asm/mixin/injection/Group.java | 58 +++++ .../InjectionValidationException.java | 42 ++++ .../mixin/injection/InjectorGroupInfo.java | 224 ++++++++++++++++++ .../mixin/injection/struct/InjectionInfo.java | 34 ++- .../asm/mixin/transformer/MixinConfig.java | 39 +++ .../mixin/transformer/MixinTargetContext.java | 49 +++- 8 files changed, 441 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/injection/Group.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/injection/InjectionValidationException.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/injection/InjectorGroupInfo.java diff --git a/gradle.properties b/gradle.properties index 3ffa47fd..756d88e0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.4.10 +buildVersion=0.4.11 buildType=SNAPSHOT diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index fb5fa891..08eb8c0a 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -60,7 +60,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.4.10"; + public static final String VERSION = "0.4.11"; /** * Blackboard key where the subsystem version will be stored to indicate diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Group.java b/src/main/java/org/spongepowered/asm/mixin/injection/Group.java new file mode 100644 index 00000000..507cd8fc --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Group.java @@ -0,0 +1,58 @@ +/* + * 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.mixin.injection; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation can be used on any injector callback to define a value for + * total required injections across multiple callbacks. Much like + * {@link Inject#require()} it can be used to treat an injection failure as a + * critical error, however the require value specified applies to all + * injectors in the group. This can be used, for example, for 'surrogate' + * injectors where multiple injectors exist but only one is expected to succeed. + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.CLASS) +public @interface Group { + + /** + * The name of the injector group, any string + * + * @return group name + */ + public String name() default ""; + + /** + * Require value for this injector group + * + * @return require value + */ + public int require() default -1; + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionValidationException.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionValidationException.java new file mode 100644 index 00000000..19a61b3e --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionValidationException.java @@ -0,0 +1,42 @@ +/* + * 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.mixin.injection; + +public class InjectionValidationException extends Exception { + + private static final long serialVersionUID = 1L; + + private final InjectorGroupInfo group; + + public InjectionValidationException(InjectorGroupInfo group, String message) { + super(message); + this.group = group; + } + + public InjectorGroupInfo getGroup() { + return this.group; + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectorGroupInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectorGroupInfo.java new file mode 100644 index 00000000..902017c1 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectorGroupInfo.java @@ -0,0 +1,224 @@ +/* + * 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.mixin.injection; + +import org.apache.logging.log4j.LogManager; +import org.spongepowered.asm.lib.tree.AnnotationNode; +import org.spongepowered.asm.lib.tree.MethodNode; +import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; +import org.spongepowered.asm.util.ASMHelper; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +/** + * Information store for injector groups + */ +public class InjectorGroupInfo { + + /** + * Storage for injector groups + */ + public static final class Map extends HashMap { + + private static final long serialVersionUID = 1L; + + private static final InjectorGroupInfo NO_GROUP = new InjectorGroupInfo("NONE"); + + @Override + public InjectorGroupInfo get(Object key) { + return this.forName(key.toString()); + } + + /** + * Get group for the specified name, creates the group in this map if + * it does not already exist + * + * @param name Name of group to fetch + * @return Existing group or new group if none was previously declared + */ + public InjectorGroupInfo forName(String name) { + InjectorGroupInfo value = super.get(name); + if (value == null) { + value = new InjectorGroupInfo(name); + this.put(name, value); + } + return value; + } + + /** + * Parse a group from the specified method, use the default group name + * if no group name is specified on the annotation + * + * @param method (Possibly) annotated method + * @param defaultGroup Default group name to use + * @return Group or NO_GROUP if no group + */ + public InjectorGroupInfo parseGroup(MethodNode method, String defaultGroup) { + return this.parseGroup(ASMHelper.getInvisibleAnnotation(method, Group.class), defaultGroup); + } + + /** + * Parse a group from the specified annotation, use the default group + * name if no group name is specified on the annotation + * + * @param method Annotation or null + * @param defaultGroup Default group name to use + * @return Group or NO_GROUP if no group + */ + public InjectorGroupInfo parseGroup(AnnotationNode annotation, String defaultGroup) { + if (annotation == null) { + return InjectorGroupInfo.Map.NO_GROUP; + } + + String name = ASMHelper.getAnnotationValue(annotation, "name"); + if (name == null || name.isEmpty()) { + name = defaultGroup; + } + InjectorGroupInfo groupInfo = this.forName(name); + + Integer require = ASMHelper.getAnnotationValue(annotation, "require"); + if (require != null) { + if (require.intValue() != -1) { + groupInfo.setRequired(require.intValue()); + } + } + + return groupInfo; + } + + /** + * Validate all groups in this collection + * + * @throws InjectionValidationException if validation fails + */ + public void validateAll() throws InjectionValidationException { + for (InjectorGroupInfo group : this.values()) { + group.validate(); + } + } + + } + + /** + * Group name + */ + private final String name; + + /** + * Members of this group + */ + private final List members = new ArrayList(); + + /** + * Number of callbacks we require injected across this group + */ + private int requiredCallbackCount = -1; + + public InjectorGroupInfo(String name) { + this.name = name; + } + + @Override + public String toString() { + return String.format("@Group(name=%s, require=%d)", this.getName(), this.getRequired()); + } + + public String getName() { + return this.name; + } + + public int getRequired() { + return Math.max(this.requiredCallbackCount, 1); + } + + /** + * Get all members of this group as a read-only collection + * + * @return read-only view of group members + */ + public Collection getMembers() { + return Collections.unmodifiableCollection(this.members); + } + + /** + * Set the required value for this group. Since this is normally done on the + * first {@link Group} annotation it is considered a warning-level event if + * a later annotation sets a different value. The highest value specified on + * all annotations is always used + * + * @param required new value for required + */ + public void setRequired(int required) { + if (required < 1) { + throw new IllegalArgumentException("Cannot set zero or negative value for injector group required count. Attempted to set required=" + + required + " on " + this + ""); + } + if (this.requiredCallbackCount > 0 && this.requiredCallbackCount != required) { + LogManager.getLogger("mixin").warn("Conflicting require value '{}' on @Group({}), previously specified {}", required, this.name, + this.requiredCallbackCount); + } + this.requiredCallbackCount = Math.max(this.requiredCallbackCount, required); + } + + /** + * Add a new member to this group + * + * @param member injector to add + * @return fluent interface + */ + public InjectorGroupInfo add(InjectionInfo member) { + this.members.add(member); + return this; + } + + /** + * Validate all members in this group + * + * @return fluent interface + * @throws InjectionValidationException if validation fails + */ + public InjectorGroupInfo validate() throws InjectionValidationException { + if (this.members.size() == 0) { + // I have no idea how we got here, but it's not an error :/ + return this; + } + + int total = 0; + for (InjectionInfo member : this.members) { + total += member.getInjectedCallbackCount(); + } + + int required = this.getRequired(); + if (total < required) { + throw new InjectionValidationException(this, String.format("expected %d invocation(s) but only %d succeeded", required, total)); + } + + return this; + } +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java index cb78700c..3297e6a0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java @@ -39,6 +39,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.InjectionError; import org.spongepowered.asm.mixin.injection.InjectionPoint; +import org.spongepowered.asm.mixin.injection.InjectorGroupInfo; import org.spongepowered.asm.mixin.injection.InvalidInjectionException; import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.Redirect; @@ -48,7 +49,6 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext; import org.spongepowered.asm.mixin.transformer.meta.MixinMerged; import org.spongepowered.asm.util.ASMHelper; - /** * Contructs information about an injection from an {@link Inject} annotation * and allows the injection to be processed. @@ -57,6 +57,8 @@ public abstract class InjectionInfo { protected final AnnotationNode annotation; + protected final String annotationType; + /** * Class */ @@ -71,8 +73,7 @@ public abstract class InjectionInfo { * Mixin data */ protected final MixinTargetContext mixin; - - + /** * Annotated method is static */ @@ -94,6 +95,11 @@ public abstract class InjectionInfo { */ protected Injector injector; + /** + * Injection group + */ + protected InjectorGroupInfo group; + /** * Methods injected by injectors */ @@ -122,11 +128,13 @@ public abstract class InjectionInfo { * @param annotation Annotation to parse */ protected InjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) { - this.annotation = annotation; - this.method = method; this.mixin = mixin; + this.method = method; + this.annotation = annotation; + this.annotationType = annotation.desc.substring(annotation.desc.lastIndexOf('/') + 1).replace(";", ""); this.classNode = mixin.getTargetClass(); this.isStatic = ASMHelper.methodIsStatic(method); + this.requiredCallbackCount = mixin.getDefaultRequiredInjections(); this.readAnnotation(); } @@ -177,6 +185,8 @@ public abstract class InjectionInfo { } } + this.group = this.mixin.getInjectorGroups().parseGroup(this.method, this.mixin.getDefaultInjectorGroup()).add(this); + Integer expect = ASMHelper.getAnnotationValue(this.annotation, "expect"); if (expect != null) { this.expectedCallbackCount = expect.intValue(); @@ -232,6 +242,11 @@ public abstract class InjectionInfo { } } + @Override + public String toString() { + return String.format("%s->@%s::%s%s", this.mixin.toString(), this.annotationType, this.method.name, this.method.desc); + } + /** * Get the mixin target context for this injection * @@ -276,6 +291,15 @@ public abstract class InjectionInfo { public Collection getTargets() { return this.targets; } + + /** + * Get the injected callback count + * + * @return the injected callback count + */ + public int getInjectedCallbackCount() { + return this.injectedCallbackCount; + } /** * Inject a method into the target class 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 9f0dcda8..ce55883f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -57,6 +57,19 @@ import net.minecraft.launchwrapper.Launch; */ class MixinConfig implements Comparable, IMixinConfig { + /** + * Wrapper for injection options + */ + static class InjectorOptions { + + @SerializedName("defaultRequire") + int defaultRequireValue = 0; + + @SerializedName("defaultGroup") + String defaultGroup = "default"; + + } + /** * Global order of mixin configs, used to determine ordering between configs * with equivalent priority @@ -189,6 +202,12 @@ class MixinConfig implements Comparable, IMixinConfig { @SerializedName("plugin") private String pluginClassName; + /** + * Injector options + */ + @SerializedName("injectors") + private InjectorOptions injectorOptions = new InjectorOptions(); + /** * Config plugin, if supplied */ @@ -420,6 +439,26 @@ class MixinConfig implements Comparable, IMixinConfig { public int getDefaultMixinPriority() { return this.mixinPriority; } + + /** + * Get the defined value for the {@link Inject#require} parameter on + * injectors defined in mixins in this configuration. + * + * @return default require value + */ + public int getDefaultRequiredInjections() { + return this.injectorOptions.defaultRequireValue; + } + + /** + * Get the defined injector group for injectors + * + * @return default group name + */ + public String getDefaultInjectorGroup() { + String defaultGroup = this.injectorOptions.defaultGroup; + return defaultGroup != null && !defaultGroup.isEmpty() ? defaultGroup : "default"; + } /** * Get the list of mixin classes we will be applying diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index eff6a03e..950e6c56 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -39,13 +39,16 @@ import org.spongepowered.asm.lib.tree.*; import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel; import org.spongepowered.asm.mixin.SoftOverride; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.InjectionError; +import org.spongepowered.asm.mixin.injection.InjectionValidationException; +import org.spongepowered.asm.mixin.injection.InjectorGroupInfo; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.transformer.ClassInfo.Traversal; import org.spongepowered.asm.util.ASMHelper; import org.spongepowered.asm.util.Constants; - /** * This object keeps track of data for applying a mixin to a specific target * class during a mixin application. This is a single-use object which @@ -91,7 +94,12 @@ public class MixinTargetContext implements IReferenceMapperContext { * transformations we apply */ private final Map targetMethods = new HashMap(); - + + /** + * Injector groups + */ + protected final InjectorGroupInfo.Map injectorGroups = new InjectorGroupInfo.Map(); + /** * True if this mixin inherits from a mixin at any point in its hierarchy */ @@ -208,6 +216,34 @@ public class MixinTargetContext implements IReferenceMapperContext { public int getMinRequiredClassVersion() { return this.minRequiredClassVersion; } + + /** + * Get the defined value for the {@link Inject#require} parameter on + * injectors defined in mixins in this configuration. + * + * @return default require value + */ + public int getDefaultRequiredInjections() { + return this.mixin.getParent().getDefaultRequiredInjections(); + } + + /** + * Get the defined injector group for injectors + * + * @return default group name + */ + public String getDefaultInjectorGroup() { + return this.mixin.getParent().getDefaultInjectorGroup(); + } + + /** + * Get the injector groups for this target + * + * @return + */ + public InjectorGroupInfo.Map getInjectorGroups() { + return this.injectorGroups; + } /** * Find the corresponding class type for the supplied mixin class in this @@ -769,6 +805,15 @@ public class MixinTargetContext implements IReferenceMapperContext { * @param targetClass Target class */ public void postApply(String transformedName, ClassNode targetClass) { + try { + this.injectorGroups.validateAll(); + } catch (InjectionValidationException ex) { + InjectorGroupInfo group = ex.getGroup(); + throw new InjectionError( + String.format("Critical injection failure: Callback group %s in %s failed injection check: %s", + group, this.mixin, ex.getMessage())); + } + this.mixin.postApply(transformedName, targetClass); }