Add support for injector grouping and config-wide Inject::require value

This commit is contained in:
Mumfrey committed 2016-01-13 01:09:12 +00:00
1 parent 61ec3187b8
commit 36576e239b
8 files changed
+441 -9

No files matched your search

+1 -1
View File
@@ -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
@@ -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
@@ -0,0 +1,58 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.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.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 <tt>require</tt> 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;
}
@@ -0,0 +1,42 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.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.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;
}
}
@@ -0,0 +1,224 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.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.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<String, InjectorGroupInfo> {
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.<String>getAnnotationValue(annotation, "name");
if (name == null || name.isEmpty()) {
name = defaultGroup;
}
InjectorGroupInfo groupInfo = this.forName(name);
Integer require = ASMHelper.<Integer>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<InjectionInfo> members = new ArrayList<InjectionInfo>();
/**
* 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<InjectionInfo> 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;
}
}
@@ -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.<Integer>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<MethodNode> 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
@@ -57,6 +57,19 @@ import net.minecraft.launchwrapper.Launch;
*/
class MixinConfig implements Comparable<MixinConfig>, 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<MixinConfig>, 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<MixinConfig>, 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
@@ -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 <em>during</em> 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<String, Target> targetMethods = new HashMap<String, Target>();
/**
* 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);
}