Refactor mixin options

This commit is contained in:
Mumfrey committed 2015-04-07 16:55:18 +01:00
1 parent 436334925a
commit b88bbba2fe
3 files changed
+95 -9

No files matched your search

@@ -28,6 +28,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.core.helpers.Booleans;
import org.spongepowered.asm.launch.MixinBootstrap;
import net.minecraft.launchwrapper.IClassTransformer;
@@ -103,6 +104,80 @@ public class MixinEnvironment {
}
}
}
public static enum Option {
/**
* Enable all debugging options
*/
DEBUG_ALL("debug"),
/**
* Enable post-mixin class export. This causes all classes to be written
* to the .mixin.out directory within the runtime directory
* <em>after</em> mixins are applied, for debugging purposes.
*/
DEBUG_EXPORT(Option.DEBUG_ALL, "export"),
/**
* Run the CheckClassAdapter on all classes after mixins are applied
*/
DEBUG_VERIFY(Option.DEBUG_ALL, "verify"),
/**
* Enable verbose mixin logging (elevates all DEBUG level messages to
* INFO level)
*/
DEBUG_VERBOSE(Option.DEBUG_ALL, "verbose"),
/**
* Enable all checks
*/
CHECK_ALL("checks"),
/**
* Checks that all declared interface methods are implemented on a class
* after mixin application.
*/
CHECK_IMPLEMENTS(Option.CHECK_ALL, "interfaces");
/**
* Prefix for mixin options
*/
private static final String PREFIX = "mixin";
/**
* Parent option to this option, if non-null then this option is enabled
* if
*/
private final Option parent;
/**
* Java property name
*/
private final String property;
private Option(String property) {
this(null, property);
}
private Option(Option parent, String property) {
this.parent = parent;
this.property = (parent != null ? parent.property : Option.PREFIX) + "." + property;
}
public Option getParent() {
return this.parent;
}
public String getProperty() {
return this.property;
}
protected boolean getValue() {
return Booleans.parseBoolean(System.getProperty(this.property), false)
|| (this.parent != null && this.parent.getValue());
}
}
private static final String CONFIGS_KEY = "mixin.configs";
private static final String TRANSFORMER_KEY = "mixin.transformer";
@@ -111,6 +186,8 @@ public class MixinEnvironment {
private Side side;
private final boolean[] options;
private MixinEnvironment() {
// Sanity check
Object version = Launch.blackboard.get(MixinBootstrap.INIT_KEY);
@@ -122,6 +199,11 @@ public class MixinEnvironment {
if (this.getClass().getClassLoader() != Launch.class.getClassLoader()) {
throw new RuntimeException("Attempted to init the mixin environment in the wrong classloader");
}
this.options = new boolean[Option.values().length];
for (Option option : Option.values()) {
this.options[option.ordinal()] = option.getValue();
}
}
/**
@@ -192,4 +274,12 @@ public class MixinEnvironment {
return MixinEnvironment.env;
}
public boolean getOption(Option option) {
return this.options[option.ordinal()];
}
public void setOption(Option option, boolean value) {
this.options[option.ordinal()] = value;
}
}
@@ -38,9 +38,9 @@ import net.minecraft.launchwrapper.Launch;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.helpers.Booleans;
import org.spongepowered.asm.launch.MixinInitialisationError;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.asm.util.VersionNumber;
@@ -239,7 +239,7 @@ class MixinConfig implements Comparable<MixinConfig> {
}
this.refMapper = ReferenceMapper.read(this.refMapperConfig);
this.verboseLogging |= Booleans.parseBoolean(System.getProperty("mixin.debug.verbose"), false) | MixinTransformer.DEBUG_ALL;
this.verboseLogging |= MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_VERBOSE);
return true;
}
@@ -43,7 +43,6 @@ import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.helpers.Booleans;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
@@ -60,6 +59,7 @@ import org.objectweb.asm.util.CheckClassAdapter;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.MixinApplyError;
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.extensibility.IMixinConfigPlugin;
@@ -159,10 +159,6 @@ public class MixinTransformer extends TreeTransformer {
Opcodes.SASTORE
};
public static final boolean DEBUG_ALL = Booleans.parseBoolean(System.getProperty("mixin.debug"), false);
private static final boolean DEBUG_EXPORT = Booleans.parseBoolean(System.getProperty("mixin.debug.export"), false) | MixinTransformer.DEBUG_ALL;
private static final boolean DEBUG_VERIFY = Booleans.parseBoolean(System.getProperty("mixin.debug.verify"), false) | MixinTransformer.DEBUG_ALL;
/**
* Log all the things
*/
@@ -406,7 +402,7 @@ public class MixinTransformer extends TreeTransformer {
this.postTransform(transformedName, targetClass, mixins);
// Run CheckClassAdapter on the mixin bytecode if debug option is enabled
if (MixinTransformer.DEBUG_VERIFY) {
if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_VERIFY)) {
targetClass.accept(new CheckClassAdapter(new ClassWriter(ClassWriter.COMPUTE_FRAMES)));
}
@@ -418,7 +414,7 @@ public class MixinTransformer extends TreeTransformer {
byte[] bytes = this.writeClass(targetClass);
// Export transformed class for debugging purposes
if (MixinTransformer.DEBUG_EXPORT) {
if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_EXPORT)) {
try {
FileUtils.writeByteArrayToFile(new File(".mixin.out/" + transformedName.replace('.', '/') + ".class"), bytes);
} catch (IOException ex) {