mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-28 00:55:37 +00:00
Apply injectors in discrete phases, ensure Redirects always go last(ish)
This commit is contained in:
1 parent
08168676c7
commit
a2751aa134
11 files changed
+196
-33
No files matched your search
@@ -1086,7 +1086,7 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
/**
|
||||
* Support for the use of injector annotations in interface mixins
|
||||
*/
|
||||
INJECTORS_IN_INTERFACE_MIXINS(false) {
|
||||
INJECTORS_IN_INTERFACE_MIXINS {
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
@@ -1109,6 +1109,10 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
*/
|
||||
private boolean enabled;
|
||||
|
||||
private Feature() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
private Feature(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInjector;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
import org.spongepowered.asm.mixin.injection.code.Injector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.AnnotationType;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.InjectorOrder;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
import org.spongepowered.asm.util.Annotations;
|
||||
|
||||
@@ -40,6 +41,7 @@ import com.google.common.base.Strings;
|
||||
* Information about a callback to inject, usually specified by {@link Inject}
|
||||
*/
|
||||
@AnnotationType(Inject.class)
|
||||
@InjectorOrder(InjectorOrder.BUILTIN_CALLBACKS)
|
||||
public class CallbackInjectionInfo extends InjectionInfo {
|
||||
|
||||
protected CallbackInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) {
|
||||
|
||||
@@ -108,12 +108,110 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
|
||||
@java.lang.annotation.Target(ElementType.TYPE)
|
||||
public @interface HandlerPrefix {
|
||||
|
||||
/**
|
||||
* Default conform prefix for handler methods
|
||||
*/
|
||||
public static final String DEFAULT = "handler";
|
||||
|
||||
/**
|
||||
* String prefix for conforming handler methods
|
||||
*/
|
||||
public String value();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Decoration for subclasses which specifies the order (phase) in which the
|
||||
* injector should be applied relative to other injectors. Built-in
|
||||
* injectors all have predefined orders which allows custom injectors to
|
||||
* specify their own order either as an explicit priority (eg. LATE) or
|
||||
* relative to a known injector (eg. InjectorOrder.BUILTIN_REDIRECT - 100).
|
||||
*
|
||||
* <p>Built-in injectors are grouped into three separate phases rather than
|
||||
* split into individual phases, in order to retain the existing behaviour
|
||||
* of mixin priority. Injectors in the same order are sorted by mixin
|
||||
* priority and declaration order within the mixin as always.</p>
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@java.lang.annotation.Target(ElementType.TYPE)
|
||||
public @interface InjectorOrder {
|
||||
|
||||
/**
|
||||
* The lowest possible order, avoid using this unlesss you are insane
|
||||
*/
|
||||
public static final int FIRST = Integer.MIN_VALUE;
|
||||
|
||||
/**
|
||||
* A very early injector, will run before injectors such as ModifyArgs
|
||||
* and ModifyVariable, as well as before EARLY injectors
|
||||
*/
|
||||
public static final int VERY_EARLY = 100;
|
||||
|
||||
/**
|
||||
* An early injector, will run before injectors suchs as ModifyArgs
|
||||
* and ModifyVariable
|
||||
*/
|
||||
public static final int EARLY = 250;
|
||||
|
||||
/**
|
||||
* Built-in order for ModifyArg injectors
|
||||
*/
|
||||
public static final int BUILTIN_MODIFYARG = 500;
|
||||
|
||||
/**
|
||||
* Built-in order for ModifyArgs injectors
|
||||
*/
|
||||
public static final int BUILTIN_MODIFYARGS = 500;
|
||||
|
||||
/**
|
||||
* Built-in order for ModifyVariable injectors
|
||||
*/
|
||||
public static final int BUILTIN_MODIFYVARIABLE = 500;
|
||||
|
||||
/**
|
||||
* Default order
|
||||
*/
|
||||
public static final int DEFAULT = 1000;
|
||||
|
||||
/**
|
||||
* Built-in order for Inject injectors
|
||||
*/
|
||||
public static final int BUILTIN_CALLBACKS = 1000;
|
||||
|
||||
/**
|
||||
* Late injector, runs after most injectors except VERY_LATE and
|
||||
* Redirect injectors
|
||||
*/
|
||||
public static final int LATE = 2000;
|
||||
|
||||
/**
|
||||
* Built-in order for ModifyConstant injectors
|
||||
*/
|
||||
public static final int BUILTIN_MODIFYCONSTANT = 5000;
|
||||
|
||||
/**
|
||||
* Built-in order for Redirect injectors
|
||||
*/
|
||||
public static final int BUILTIN_REDIRECT = 5000;
|
||||
|
||||
/**
|
||||
* Very late injector, runs after nearly all injectors including
|
||||
* Redirect injectors
|
||||
*/
|
||||
public static final int VERY_LATE = 10000;
|
||||
|
||||
/**
|
||||
* The highest possible order, using this causes the universe to
|
||||
* implode, bringing about the end of days.
|
||||
*/
|
||||
public static final int LAST = Integer.MAX_VALUE;
|
||||
|
||||
/**
|
||||
* String prefix for conforming handler methods
|
||||
*/
|
||||
public int value() default InjectorOrder.DEFAULT;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* An injector registration entry
|
||||
@@ -137,7 +235,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
|
||||
this.annotationDesc = Type.getDescriptor(annotationType);
|
||||
|
||||
HandlerPrefix handlerPrefix = type.<HandlerPrefix>getAnnotation(HandlerPrefix.class);
|
||||
this.prefix = handlerPrefix != null ? handlerPrefix.value() : InjectionInfo.DEFAULT_PREFIX;
|
||||
this.prefix = handlerPrefix != null ? handlerPrefix.value() : HandlerPrefix.DEFAULT;
|
||||
}
|
||||
|
||||
InjectionInfo create(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) {
|
||||
@@ -156,11 +254,6 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default conform prefix for handler methods
|
||||
*/
|
||||
public static final String DEFAULT_PREFIX = "handler";
|
||||
|
||||
/**
|
||||
* Registry of subclasses
|
||||
*/
|
||||
@@ -387,6 +480,14 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
|
||||
return this.targets.size() > 0 && this.injectionPoints.size() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the application order for this injector type
|
||||
*/
|
||||
public int getOrder() {
|
||||
InjectorOrder injectorOrder = this.getClass().<InjectorOrder>getAnnotation(InjectorOrder.class);
|
||||
return injectorOrder != null ? injectorOrder.value() : InjectorOrder.DEFAULT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover injection points
|
||||
*/
|
||||
@@ -624,7 +725,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
|
||||
*/
|
||||
public static String getInjectorPrefix(AnnotationNode annotation) {
|
||||
if (annotation == null) {
|
||||
return InjectionInfo.DEFAULT_PREFIX;
|
||||
return HandlerPrefix.DEFAULT;
|
||||
}
|
||||
|
||||
for (InjectorEntry injector : InjectionInfo.registry.values()) {
|
||||
@@ -633,7 +734,7 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
|
||||
}
|
||||
}
|
||||
|
||||
return InjectionInfo.DEFAULT_PREFIX;
|
||||
return HandlerPrefix.DEFAULT;
|
||||
}
|
||||
|
||||
static String describeInjector(IMixinContext mixin, AnnotationNode annotation, MethodNode method) {
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.spongepowered.asm.mixin.injection.code.Injector;
|
||||
import org.spongepowered.asm.mixin.injection.invoke.ModifyArgInjector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.AnnotationType;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.HandlerPrefix;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.InjectorOrder;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
import org.spongepowered.asm.util.Annotations;
|
||||
|
||||
@@ -39,6 +40,7 @@ import org.spongepowered.asm.util.Annotations;
|
||||
*/
|
||||
@AnnotationType(ModifyArg.class)
|
||||
@HandlerPrefix("modify")
|
||||
@InjectorOrder(InjectorOrder.BUILTIN_MODIFYARG)
|
||||
public class ModifyArgInjectionInfo extends InjectionInfo {
|
||||
|
||||
public ModifyArgInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) {
|
||||
|
||||
+2
@@ -31,6 +31,7 @@ import org.spongepowered.asm.mixin.injection.code.Injector;
|
||||
import org.spongepowered.asm.mixin.injection.invoke.ModifyArgsInjector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.AnnotationType;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.HandlerPrefix;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.InjectorOrder;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
|
||||
/**
|
||||
@@ -38,6 +39,7 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
*/
|
||||
@AnnotationType(ModifyArgs.class)
|
||||
@HandlerPrefix("args")
|
||||
@InjectorOrder(InjectorOrder.BUILTIN_MODIFYARG)
|
||||
public class ModifyArgsInjectionInfo extends InjectionInfo {
|
||||
|
||||
public ModifyArgsInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) {
|
||||
|
||||
+2
@@ -36,6 +36,7 @@ import org.spongepowered.asm.mixin.injection.invoke.ModifyConstantInjector;
|
||||
import org.spongepowered.asm.mixin.injection.points.BeforeConstant;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.AnnotationType;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.HandlerPrefix;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.InjectorOrder;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
@@ -45,6 +46,7 @@ import com.google.common.base.Strings;
|
||||
*/
|
||||
@AnnotationType(ModifyConstant.class)
|
||||
@HandlerPrefix("constant")
|
||||
@InjectorOrder(InjectorOrder.BUILTIN_MODIFYCONSTANT)
|
||||
public class ModifyConstantInjectionInfo extends InjectionInfo {
|
||||
|
||||
private static final String CONSTANT_ANNOTATION_CLASS = Constant.class.getName().replace('.', '/');
|
||||
|
||||
+2
@@ -32,6 +32,7 @@ import org.spongepowered.asm.mixin.injection.modify.LocalVariableDiscriminator;
|
||||
import org.spongepowered.asm.mixin.injection.modify.ModifyVariableInjector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.AnnotationType;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.HandlerPrefix;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.InjectorOrder;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
|
||||
/**
|
||||
@@ -39,6 +40,7 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
*/
|
||||
@AnnotationType(ModifyVariable.class)
|
||||
@HandlerPrefix("localvar")
|
||||
@InjectorOrder(InjectorOrder.BUILTIN_MODIFYVARIABLE)
|
||||
public class ModifyVariableInjectionInfo extends InjectionInfo {
|
||||
|
||||
public ModifyVariableInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) {
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.spongepowered.asm.mixin.injection.code.Injector;
|
||||
import org.spongepowered.asm.mixin.injection.invoke.RedirectInjector;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.AnnotationType;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.HandlerPrefix;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo.InjectorOrder;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
|
||||
/**
|
||||
@@ -38,6 +39,7 @@ import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
|
||||
*/
|
||||
@AnnotationType(Redirect.class)
|
||||
@HandlerPrefix("redirect")
|
||||
@InjectorOrder(InjectorOrder.BUILTIN_REDIRECT)
|
||||
public class RedirectInjectionInfo extends InjectionInfo {
|
||||
|
||||
public RedirectInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) {
|
||||
|
||||
@@ -118,12 +118,12 @@ class MixinApplicatorInterface extends MixinApplicatorStandard {
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
|
||||
* #applyInjections(
|
||||
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
|
||||
* org.spongepowered.asm.mixin.transformer.MixinTargetContext, int)
|
||||
*/
|
||||
@Override
|
||||
protected void applyInjections(MixinTargetContext mixin) {
|
||||
protected void applyInjections(MixinTargetContext mixin, int injectorOrder) {
|
||||
if (Feature.INJECTORS_IN_INTERFACE_MIXINS.isEnabled()) {
|
||||
super.applyInjections(mixin);
|
||||
super.applyInjections(mixin, injectorOrder);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
+41
-15
@@ -70,6 +70,7 @@ import org.spongepowered.asm.util.throwables.ConstraintViolationException;
|
||||
import org.spongepowered.asm.util.throwables.InvalidConstraintException;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
/**
|
||||
* Applies mixins to a target class
|
||||
@@ -104,12 +105,22 @@ class MixinApplicatorStandard {
|
||||
*/
|
||||
PREINJECT,
|
||||
|
||||
/**
|
||||
* Apply accessors and invokers
|
||||
*/
|
||||
ACCESSOR,
|
||||
|
||||
/**
|
||||
* Apply injectors from previous pass
|
||||
*/
|
||||
INJECT
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Order collection to use for all passes except ApplicatorPass.INJECT
|
||||
*/
|
||||
protected static final Set<Integer> ORDERS_NONE = ImmutableSet.<Integer>of(Integer.valueOf(0));
|
||||
|
||||
/**
|
||||
* Log more things
|
||||
@@ -211,17 +222,28 @@ class MixinApplicatorStandard {
|
||||
activity.next("%s Applicator Phase", pass);
|
||||
Section timer = this.profiler.begin("pass", pass.name().toLowerCase(Locale.ROOT));
|
||||
IActivity applyActivity = this.activities.begin("Mixin");
|
||||
for (Iterator<MixinTargetContext> iter = mixinContexts.iterator(); iter.hasNext();) {
|
||||
current = iter.next();
|
||||
applyActivity.next(current.toString());
|
||||
try {
|
||||
this.applyMixin(current, pass);
|
||||
} catch (InvalidMixinException ex) {
|
||||
if (current.isRequired()) {
|
||||
throw ex;
|
||||
|
||||
Set<Integer> orders = MixinApplicatorStandard.ORDERS_NONE;
|
||||
if (pass == ApplicatorPass.INJECT) {
|
||||
orders = new TreeSet<Integer>();
|
||||
for (MixinTargetContext context : mixinContexts) {
|
||||
context.getInjectorOrders(orders);
|
||||
}
|
||||
}
|
||||
|
||||
for (Integer injectorOrder : orders) {
|
||||
for (Iterator<MixinTargetContext> iter = mixinContexts.iterator(); iter.hasNext();) {
|
||||
current = iter.next();
|
||||
applyActivity.next(current.toString());
|
||||
try {
|
||||
this.applyMixin(current, pass, injectorOrder.intValue());
|
||||
} catch (InvalidMixinException ex) {
|
||||
if (current.isRequired()) {
|
||||
throw ex;
|
||||
}
|
||||
this.context.addSuppressed(ex);
|
||||
iter.remove(); // Do not process this mixin further
|
||||
}
|
||||
this.context.addSuppressed(ex);
|
||||
iter.remove(); // Do not process this mixin further
|
||||
}
|
||||
}
|
||||
applyActivity.end();
|
||||
@@ -261,7 +283,7 @@ class MixinApplicatorStandard {
|
||||
*
|
||||
* @param mixin Mixin to apply
|
||||
*/
|
||||
protected final void applyMixin(MixinTargetContext mixin, ApplicatorPass pass) {
|
||||
protected final void applyMixin(MixinTargetContext mixin, ApplicatorPass pass, int injectorOrder) {
|
||||
IActivity activity = this.activities.begin("Apply");
|
||||
switch (pass) {
|
||||
case MAIN:
|
||||
@@ -286,11 +308,14 @@ class MixinApplicatorStandard {
|
||||
this.prepareInjections(mixin);
|
||||
break;
|
||||
|
||||
case INJECT:
|
||||
case ACCESSOR:
|
||||
activity.next("Apply Accessors");
|
||||
this.applyAccessors(mixin);
|
||||
break;
|
||||
|
||||
case INJECT:
|
||||
activity.next("Apply Injections");
|
||||
this.applyInjections(mixin);
|
||||
this.applyInjections(mixin, injectorOrder);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -697,9 +722,10 @@ class MixinApplicatorStandard {
|
||||
* Apply all injectors discovered in the previous pass
|
||||
*
|
||||
* @param mixin Mixin being applied
|
||||
* @param injectorOrder injector order for this pass
|
||||
*/
|
||||
protected void applyInjections(MixinTargetContext mixin) {
|
||||
mixin.applyInjections();
|
||||
protected void applyInjections(MixinTargetContext mixin, int injectorOrder) {
|
||||
mixin.applyInjections(injectorOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1401,35 +1401,55 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply injectors discovered in the {@link #prepareInjections()} pass
|
||||
* Get all injector order values for injectors in this mixin
|
||||
*
|
||||
* @param orders Orders Set to add this mixin's orders to
|
||||
*/
|
||||
void applyInjections() {
|
||||
void getInjectorOrders(Set<Integer> orders) {
|
||||
for (InjectionInfo injectInfo : this.injectors) {
|
||||
orders.add(injectInfo.getOrder());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply injectors discovered in the {@link #prepareInjections()} pass
|
||||
*
|
||||
* @param injectorOrder injector order for this pass
|
||||
*/
|
||||
void applyInjections(int injectorOrder) {
|
||||
this.activities.clear();
|
||||
|
||||
List<InjectionInfo> injectors = new ArrayList<InjectionInfo>();
|
||||
for (InjectionInfo injectInfo : this.injectors) {
|
||||
if (injectInfo.getOrder() == injectorOrder) {
|
||||
injectors.add(injectInfo);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
IActivity applyActivity = this.activities.begin("PreInject");
|
||||
IActivity preInjectActivity = this.activities.begin("?");
|
||||
for (InjectionInfo injectInfo : this.injectors) {
|
||||
for (InjectionInfo injectInfo : injectors) {
|
||||
preInjectActivity.next(injectInfo.toString());
|
||||
injectInfo.preInject();
|
||||
}
|
||||
|
||||
applyActivity.next("Inject");
|
||||
IActivity injectActivity = this.activities.begin("?");
|
||||
for (InjectionInfo injectInfo : this.injectors) {
|
||||
for (InjectionInfo injectInfo : injectors) {
|
||||
injectActivity.next(injectInfo.toString());
|
||||
injectInfo.inject();
|
||||
}
|
||||
|
||||
applyActivity.next("PostInject");
|
||||
IActivity postInjectActivity = this.activities.begin("?");
|
||||
for (InjectionInfo injectInfo : this.injectors) {
|
||||
for (InjectionInfo injectInfo : injectors) {
|
||||
postInjectActivity.next(injectInfo.toString());
|
||||
injectInfo.postInject();
|
||||
}
|
||||
|
||||
applyActivity.end();
|
||||
this.injectors.clear();
|
||||
this.injectors.removeAll(injectors);
|
||||
} catch (InvalidMixinException ex) {
|
||||
ex.prepend(this.activities);
|
||||
throw ex;
|
||||
|
||||
Reference in new issue
Block a user