Merge mapper fixes and async decompiler

This commit is contained in:
Mumfrey committed 2016-09-16 13:53:48 +01:00
1 parent 6ed36dff60
commit 451f24a6da
12 files changed
+133 -127

No files matched your search

@@ -62,7 +62,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.5.13";
public static final String VERSION = "0.5.12";
// Consts
private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
@@ -276,6 +276,20 @@ public final class MixinEnvironment implements ITokenProvider {
}
},
/**
* Run fernflower in a separate thread. In general this will allow
* export to impact startup time much less (decompiling normally adds
* about 20% to load times) with the trade-off that crashes may lead to
* undecompiled exports.
*/
DEBUG_EXPORT_DECOMPILE_THREADED(Option.DEBUG_EXPORT_DECOMPILE, "async") {
@Override
boolean getBooleanValue() {
// Allow a local FALSE to override a parent TRUE
return Booleans.parseBoolean(System.getProperty(this.property), super.getBooleanValue());
}
},
/**
* Run the CheckClassAdapter on all classes after mixins are applied,
* also enables stricter checks on mixins for use at dev-time, promotes
@@ -321,13 +335,13 @@ public final class MixinEnvironment implements ITokenProvider {
/**
* Disable the injector handler remapper
*/
DEBUG_DISABLE_HANDLER_REMAP(Option.DEBUG_ALL, "disableHandlerRename") {
@Override
boolean getBooleanValue() {
// Allow a local FALSE to override a parent TRUE
return Booleans.parseBoolean(System.getProperty(this.property), super.getBooleanValue());
}
},
// DEBUG_DISABLE_HANDLER_REMAP(Option.DEBUG_ALL, "disableHandlerRename") {
// @Override
// boolean getBooleanValue() {
// // Allow a local FALSE to override a parent TRUE
// return Booleans.parseBoolean(System.getProperty(this.property), super.getBooleanValue());
// }
// },
/**
* Dumps the bytecode for the target class to disk when mixin
@@ -40,6 +40,7 @@ import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.InjectionNodes.InjectionNode;
import org.spongepowered.asm.mixin.injection.InjectionPoint;
@@ -414,7 +415,7 @@ public abstract class InjectionInfo {
}
public static InjectionInfo parse(MixinTargetContext mixin, MethodNode method) {
AnnotationNode annotation = InjectionInfo.getInjectorAnnotation(mixin, method);
AnnotationNode annotation = InjectionInfo.getInjectorAnnotation(mixin.getMixin(), method);
if (annotation == null) {
return null;
@@ -436,7 +437,7 @@ public abstract class InjectionInfo {
}
@SuppressWarnings("unchecked")
public static AnnotationNode getInjectorAnnotation(MixinTargetContext mixin, MethodNode method) {
public static AnnotationNode getInjectorAnnotation(IMixinInfo mixin, MethodNode method) {
AnnotationNode annotation = null;
try {
annotation = ASMHelper.getSingleVisibleAnnotation(method,
@@ -308,8 +308,13 @@ public class ClassInfo extends TreeInfo {
return this.modifiers;
}
public void renameTo(String name) {
/**
* @param name
* @return the passed-in argument, for fluency
*/
public String renameTo(String name) {
this.currentName = name;
return name;
}
public boolean equals(String name, String desc) {
@@ -557,7 +562,9 @@ public class ClassInfo extends TreeInfo {
* Mixin info if this class is a mixin itself
*/
private final MixinInfo mixin;
private final MethodMapper methodMapper;
/**
* True if this is a mixin rather than a class
*/
@@ -610,6 +617,7 @@ public class ClassInfo extends TreeInfo {
this.access = Opcodes.ACC_PUBLIC;
this.isMixin = false;
this.mixin = null;
this.methodMapper = null;
}
/**
@@ -654,6 +662,7 @@ public class ClassInfo extends TreeInfo {
this.isProbablyStatic = isProbablyStatic;
this.outerName = outerName;
this.methodMapper = new MethodMapper(MixinEnvironment.getCurrentEnvironment(), this);
}
void addInterface(String iface) {
@@ -747,6 +756,10 @@ public class ClassInfo extends TreeInfo {
public String toString() {
return this.name;
}
public MethodMapper getMethodMapper() {
return this.methodMapper;
}
public int getAccess() {
return this.access;
@@ -840,10 +853,10 @@ public class ClassInfo extends TreeInfo {
public Set<Method> getInterfaceMethods(boolean includeMixins) {
Set<Method> methods = new HashSet<Method>();
ClassInfo superClass = this.addMethodsRecursive(methods, includeMixins);
ClassInfo supClass = this.addMethodsRecursive(methods, includeMixins);
if (!this.isInterface) {
while (superClass != null && superClass != ClassInfo.OBJECT) {
superClass = superClass.addMethodsRecursive(methods, includeMixins);
while (supClass != null && supClass != ClassInfo.OBJECT) {
supClass = supClass.addMethodsRecursive(methods, includeMixins);
}
}
@@ -1062,13 +1075,13 @@ public class ClassInfo extends TreeInfo {
return false;
}
ClassInfo superClass = this.getSuperClass();
ClassInfo supClass = this.getSuperClass();
while (superClass != null && superClass != ClassInfo.OBJECT) {
if (superClass.isMixin) {
while (supClass != null && supClass != ClassInfo.OBJECT) {
if (supClass.isMixin) {
return true;
}
superClass = superClass.getSuperClass();
supClass = supClass.getSuperClass();
}
return false;
@@ -1087,13 +1100,13 @@ public class ClassInfo extends TreeInfo {
return false;
}
ClassInfo superClass = this.getSuperClass();
ClassInfo supClass = this.getSuperClass();
while (superClass != null && superClass != ClassInfo.OBJECT) {
if (superClass.mixins.size() > 0) {
while (supClass != null && supClass != ClassInfo.OBJECT) {
if (supClass.mixins.size() > 0) {
return true;
}
superClass = superClass.getSuperClass();
supClass = supClass.getSuperClass();
}
return false;
@@ -307,10 +307,10 @@ class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
private MixinEnvironment parseSelector(String target, MixinEnvironment fallbackEnvironment) {
if (target != null) {
String[] selectors = target.split("[&\\| ]");
for (String selector : selectors) {
selector = selector.trim();
for (String sel : selectors) {
sel = sel.trim();
Pattern environmentSelector = Pattern.compile("^@env(?:ironment)?\\(([A-Z]+)\\)$");
Matcher environmentSelectorMatcher = environmentSelector.matcher(selector);
Matcher environmentSelectorMatcher = environmentSelector.matcher(sel);
if (environmentSelectorMatcher.matches()) {
// only parse first env selector
return MixinEnvironment.getEnvironment(Phase.forName(environmentSelectorMatcher.group(1)));
@@ -55,6 +55,8 @@ import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.extensibility.IMixinConfig;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.mixin.injection.Surrogate;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException;
import org.spongepowered.asm.mixin.transformer.throwables.MixinReloadException;
import org.spongepowered.asm.mixin.transformer.throwables.MixinTargetAlreadyLoadedException;
@@ -93,6 +95,18 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
return this.originalName;
}
public boolean isInjector() {
return (this.getInjectorAnnotation() != null || this.isSurrogate());
}
public boolean isSurrogate() {
return ASMHelper.getVisibleAnnotation(this, Surrogate.class) != null;
}
public AnnotationNode getInjectorAnnotation() {
return InjectionInfo.getInjectorAnnotation(MixinInfo.this, this);
}
}
/**
@@ -199,7 +213,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
return this.mixinBytes;
}
ClassNode getClassNode() {
MixinClassNode getClassNode() {
return this.classNode;
}
@@ -243,6 +257,11 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
* @param targetClasses Mixin's target classes
*/
void validate(SubType type, List<ClassInfo> targetClasses) {
MixinPreProcessorStandard preProcessor = type.createPreProcessor(this.getClassNode()).prepare();
for (ClassInfo target : targetClasses) {
preProcessor.conform(target);
}
type.validate(this, targetClasses);
this.detachedSuper = type.isDetachedSuper();
@@ -697,7 +716,7 @@ class MixinInfo extends TreeInfo implements Comparable<MixinInfo>, IMixinInfo {
* @param suppressPlugin
* @return target class list read from classNode
*/
protected List<ClassInfo> readTargetClasses(ClassNode classNode, boolean suppressPlugin) {
protected List<ClassInfo> readTargetClasses(MixinClassNode classNode, boolean suppressPlugin) {
if (classNode == null) {
return Collections.<ClassInfo>emptyList();
}
@@ -40,8 +40,6 @@ import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.Surrogate;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.MixinInfo.MixinClassNode;
@@ -105,17 +103,19 @@ class MixinPreProcessorStandard {
* @return Prepared classnode
*/
MixinPreProcessorStandard prepare() {
if (!this.prepared) {
this.prepared = true;
for (MixinMethodNode mixinMethod : this.classNode.mixinMethods) {
Method method = this.mixin.getClassInfo().findMethod(mixinMethod);
this.prepareMethod(mixinMethod, method);
}
for (FieldNode mixinField : this.classNode.fields) {
this.prepareField(mixinField);
}
if (this.prepared) {
return this;
}
this.prepared = true;
for (MixinMethodNode mixinMethod : this.classNode.mixinMethods) {
Method method = this.mixin.getClassInfo().findMethod(mixinMethod);
this.prepareMethod(mixinMethod, method);
}
for (FieldNode mixinField : this.classNode.fields) {
this.prepareField(mixinField);
}
return this;
@@ -136,8 +136,7 @@ class MixinPreProcessorStandard {
if (mixinMethod.name.startsWith(prefix)) {
ASMHelper.setVisibleAnnotation(mixinMethod, MixinRenamed.class, "originalName", mixinMethod.name);
String newName = mixinMethod.name.substring(prefix.length());
method.renameTo(newName);
mixinMethod.name = newName;
mixinMethod.name = method.renameTo(newName);
}
}
@@ -152,9 +151,29 @@ class MixinPreProcessorStandard {
protected void prepareField(FieldNode mixinField) {
// stub
}
MixinPreProcessorStandard conform(TargetClassContext target) {
return this.conform(target.getClassInfo());
}
MixinPreProcessorStandard conform(ClassInfo target) {
for (MixinMethodNode mixinMethod : this.classNode.mixinMethods) {
if (mixinMethod.isInjector()) {
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL);
this.conformInjector(target, mixinMethod, method);
}
}
return this;
}
private void conformInjector(ClassInfo targetClass, MixinMethodNode mixinMethod, Method method) {
MethodMapper methodMapper = targetClass.getMethodMapper();
methodMapper.remapHandlerMethod(this.mixin, mixinMethod, method);
}
MixinTargetContext createContextFor(TargetClassContext target) {
MixinTargetContext context = new MixinTargetContext(this.mixin, this.classNode, target);
this.conform(target);
this.attach(context);
return context;
}
@@ -164,7 +183,7 @@ class MixinPreProcessorStandard {
*
* @param context
*/
void attach(MixinTargetContext context) {
MixinPreProcessorStandard attach(MixinTargetContext context) {
if (this.attached) {
throw new IllegalStateException("Preprocessor was already attached");
}
@@ -177,6 +196,7 @@ class MixinPreProcessorStandard {
// Apply transformations to the mixin bytecode
this.transform(context);
return this;
}
protected void attachMethods(MixinTargetContext context) {
@@ -188,7 +208,7 @@ class MixinPreProcessorStandard {
continue;
}
if (this.processInjectorMethod(context, mixinMethod)) {
if (mixinMethod.isInjector()) {
continue;
}
@@ -215,26 +235,6 @@ class MixinPreProcessorStandard {
return true;
}
protected boolean processInjectorMethod(MixinTargetContext context, MixinMethodNode mixinMethod) {
AnnotationNode annotation = InjectionInfo.getInjectorAnnotation(context, mixinMethod);
boolean surrogate = ASMHelper.getVisibleAnnotation(mixinMethod, Surrogate.class) != null;
if (annotation == null && !surrogate) {
return false;
}
String handlerName = context.getHandlerName(annotation, mixinMethod, surrogate);
Method method = this.mixin.getClassInfo().findMethod(mixinMethod, ClassInfo.INCLUDE_ALL);
if (method.isUnique()) {
MixinPreProcessorStandard.logger.warn("Redundant @Unique on injector method {} in {}. Injectors are implicitly unique", method, context);
}
method.renameTo(handlerName);
mixinMethod.name = handlerName;
return true;
}
protected boolean processShadowMethod(MixinTargetContext context, MixinMethodNode mixinMethod) {
return this.processSpecialMethod(context, mixinMethod, Shadow.class, false);
}
@@ -271,8 +271,7 @@ class MixinPreProcessorStandard {
throw new InvalidMixinException(this.mixin, "@" + ASMHelper.getSimpleName(type) + " method " + mixinMethod.name
+ " was not located in the target class");
}
mixinMethod.name = target.name;
method.renameTo(target.name);
mixinMethod.name = method.renameTo(target.name);
}
if (Constants.CTOR.equals(target.name)) {
@@ -284,8 +283,7 @@ class MixinPreProcessorStandard {
throw new InvalidMixinException(this.mixin, "Non-private method cannot be aliased. Found " + target.name);
}
mixinMethod.name = target.name;
method.renameTo(target.name);
mixinMethod.name = method.renameTo(target.name);
}
return true;
@@ -306,8 +304,7 @@ class MixinPreProcessorStandard {
String uniqueName = context.getUniqueName(mixinMethod);
MixinPreProcessorStandard.logger.log(this.mixin.getLoggingLevel(), "Renaming @Unique method {}{} to {} in {}",
mixinMethod.name, mixinMethod.desc, uniqueName, this.mixin);
mixinMethod.name = uniqueName;
method.renameTo(uniqueName);
mixinMethod.name = method.renameTo(uniqueName);
return false;
}
@@ -330,8 +327,7 @@ class MixinPreProcessorStandard {
Method parentMethod = this.mixin.getClassInfo().findMethodInHierarchy(mixinMethod, false);
if (parentMethod != null && parentMethod.isRenamed()) {
mixinMethod.name = parentMethod.getName();
method.renameTo(parentMethod.getName());
mixinMethod.name = method.renameTo(parentMethod.getName());
}
}
@@ -363,8 +359,7 @@ class MixinPreProcessorStandard {
// If this field is a shadow field but is NOT found in the target class, that's bad, mmkay
throw new InvalidMixinException(this.mixin, "Shadow field " + mixinField.name + " was not located in the target class");
}
mixinField.name = target.name;
field.renameTo(target.name);
mixinField.name = field.renameTo(target.name);
}
if (field.isUnique()) {
@@ -372,8 +367,7 @@ class MixinPreProcessorStandard {
String uniqueName = context.getUniqueName(mixinField);
MixinPreProcessorStandard.logger.log(this.mixin.getLoggingLevel(), "Renaming @Unique field {}{} to {} in {}",
mixinField.name, mixinField.desc, uniqueName, this.mixin);
mixinField.name = uniqueName;
field.renameTo(uniqueName);
mixinField.name = field.renameTo(uniqueName);
continue;
}
@@ -399,8 +393,7 @@ class MixinPreProcessorStandard {
throw new InvalidMixinException(this.mixin, "Non-private field cannot be aliased. Found " + target.name);
}
mixinField.name = target.name;
field.renameTo(target.name);
mixinField.name = field.renameTo(target.name);
}
// Shadow fields get stripped from the mixin class
@@ -971,10 +971,6 @@ public class MixinTargetContext implements IReferenceMapperContext {
this.mixin.postApply(transformedName, targetClass);
}
public String getHandlerName(AnnotationNode annotation, MethodNode method, boolean surrogate) {
return this.targetClass.getHandlerName(annotation, method, surrogate);
}
public String getUniqueName(MethodNode method) {
return this.targetClass.getUniqueName(method);
}
@@ -390,18 +390,21 @@ public class MixinTransformer extends TreeTransformer {
}
private IDecompiler initDecompiler(File outputPath) {
if (!MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_EXPORT_DECOMPILE)) {
MixinEnvironment env = MixinEnvironment.getCurrentEnvironment();
if (!env.getOption(Option.DEBUG_EXPORT_DECOMPILE)) {
return null;
}
try {
this.logger.info("Attempting to load Fernflower decompiler");
boolean as = env.getOption(Option.DEBUG_EXPORT_DECOMPILE_THREADED);
this.logger.info("Attempting to load Fernflower decompiler{}", as ? " (Threaded mode)" : "");
@SuppressWarnings("unchecked")
Class<? extends IDecompiler> clazz =
(Class<? extends IDecompiler>)Class.forName("org.spongepowered.asm.mixin.transformer.debug.RuntimeDecompiler");
(Class<? extends IDecompiler>)Class.forName("org.spongepowered.asm.mixin.transformer.debug.RuntimeDecompiler" + (as ? "Async" : ""));
Constructor<? extends IDecompiler> ctor = clazz.getDeclaredConstructor(File.class);
IDecompiler decompiler = ctor.newInstance(outputPath);
this.logger.info("Fernflower decompiler was successfully initialised, exported classes will be decompiled");
this.logger.info("Fernflower decompiler was successfully initialised, exported classes will be decompiled{}",
as ? " in a separate thread" : "");
return decompiler;
} catch (Throwable th) {
this.logger.info("Fernflower could not be loaded, exported classes will not be decompiled. {}: {}",
@@ -37,7 +37,6 @@ import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.util.ASMHelper;
@@ -45,13 +44,6 @@ import org.spongepowered.asm.util.ASMHelper;
* Struct for containing target class information during mixin application
*/
class TargetClassContext {
/**
* Mutable integer
*/
static class Counter {
public int value;
}
/**
* Transformer session ID
@@ -77,22 +69,12 @@ class TargetClassContext {
* Mixins to apply
*/
private final SortedSet<MixinInfo> mixins;
/**
* Injector Method descriptor to ID map
*/
private final Map<String, Counter> injectorMethodIndices = new HashMap<String, Counter>();
/**
* Information about methods in the target class, used to keep track of
* transformations we apply
*/
private final Map<String, Target> targetMethods = new HashMap<String, Target>();
/**
* Do not remap injector handler methods (debug option)
*/
private final boolean disableHandlerRemap;
/**
* Unique method and field indices
@@ -116,7 +98,6 @@ class TargetClassContext {
this.classNode = classNode;
this.classInfo = ClassInfo.fromClassNode(classNode);
this.mixins = mixins;
this.disableHandlerRemap = MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_DISABLE_HANDLER_REMAP);
}
@Override
@@ -244,29 +225,15 @@ class TargetClassContext {
}
return target;
}
public String getHandlerName(AnnotationNode annotation, MethodNode method, boolean surrogate) {
if (this.disableHandlerRemap) {
return method.name;
}
String descriptor = String.format("%s%s", method.name, method.desc);
Counter id = this.injectorMethodIndices.get(descriptor);
if (id == null) {
id = new Counter();
this.injectorMethodIndices.put(descriptor, id);
} else if (!surrogate) {
id.value++;
}
return String.format("%s$%s$%d", InjectionInfo.getInjectorPrefix(annotation), method.name, id.value);
}
public String getUniqueName(MethodNode method) {
return String.format("md%s$%s$%d", this.sessionId.substring(30), method.name, this.nextUniqueMethodIndex++);
String uniqueIndex = Integer.toHexString(this.nextUniqueMethodIndex++);
return String.format("md%s$%s$%s", this.sessionId.substring(30), method.name, uniqueIndex);
}
public String getUniqueName(FieldNode field) {
return String.format("fd%s$%s$%d", this.sessionId.substring(30), field.name, this.nextUniqueFieldIndex++);
String uniqueIndex = Integer.toHexString(this.nextUniqueFieldIndex++);
return String.format("fd%s$%s$%s", this.sessionId.substring(30), field.name, uniqueIndex);
}
/**