mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-20 12:19:52 +00:00
Alternative strategy for injecting mixin field initialisers, closes #138
This commit is contained in:
1 parent
065b0281f1
commit
c5e97a08d6
4 files changed
+135
-35
No files matched your search
@@ -62,7 +62,7 @@ public abstract class MixinBootstrap {
|
||||
/**
|
||||
* Subsystem version
|
||||
*/
|
||||
public static final String VERSION = "0.5.11";
|
||||
public static final String VERSION = "0.5.13";
|
||||
|
||||
// Consts
|
||||
private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
|
||||
|
||||
@@ -271,6 +271,7 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
DEBUG_EXPORT_DECOMPILE(Option.DEBUG_EXPORT, "decompile") {
|
||||
@Override
|
||||
boolean getBooleanValue() {
|
||||
// Allow a local FALSE to override a parent TRUE
|
||||
return Booleans.parseBoolean(System.getProperty(this.property), super.getBooleanValue());
|
||||
}
|
||||
},
|
||||
@@ -300,7 +301,8 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
DEBUG_STRICT(Option.DEBUG_ALL, "strict") {
|
||||
@Override
|
||||
boolean getBooleanValue() {
|
||||
return Booleans.parseBoolean(System.getProperty(this.property), false);
|
||||
// no inheritance
|
||||
return this.getLocalBooleanValue();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -322,6 +324,7 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
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());
|
||||
}
|
||||
},
|
||||
@@ -376,7 +379,13 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
/**
|
||||
* Default compatibility level to operate at
|
||||
*/
|
||||
DEFAULT_COMPATIBILITY_LEVEL(Option.ENVIRONMENT, "compatLevel");
|
||||
DEFAULT_COMPATIBILITY_LEVEL(Option.ENVIRONMENT, "compatLevel"),
|
||||
|
||||
/**
|
||||
* Behaviour for initialiser injections, current supported options are
|
||||
* "default" and "safe"
|
||||
*/
|
||||
INITIALISER_INJECTION_MODE("initialiserInjectionMode", "default");
|
||||
|
||||
/**
|
||||
* Prefix for mixin options
|
||||
@@ -394,6 +403,11 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
*/
|
||||
final String property;
|
||||
|
||||
/**
|
||||
* Default value for string properties
|
||||
*/
|
||||
final String defaultStringValue;
|
||||
|
||||
/**
|
||||
* Whether this property is boolean or not
|
||||
*/
|
||||
@@ -411,14 +425,27 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
private Option(String property, boolean flag) {
|
||||
this(null, property, flag);
|
||||
}
|
||||
|
||||
private Option(String property, String defaultStringValue) {
|
||||
this(null, property, false, defaultStringValue);
|
||||
}
|
||||
|
||||
private Option(Option parent, String property) {
|
||||
this(parent, property, true);
|
||||
}
|
||||
|
||||
private Option(Option parent, String property, boolean flag) {
|
||||
this(parent, property, flag, null);
|
||||
}
|
||||
|
||||
private Option(Option parent, String property, String defaultStringValue) {
|
||||
this(parent, property, false, defaultStringValue);
|
||||
}
|
||||
|
||||
private Option(Option parent, String property, boolean flag, String defaultStringValue) {
|
||||
this.parent = parent;
|
||||
this.property = (parent != null ? parent.property : Option.PREFIX) + "." + property;
|
||||
this.defaultStringValue = defaultStringValue;
|
||||
this.flag = flag;
|
||||
int depth = 0;
|
||||
for (; parent != null; depth++) {
|
||||
@@ -440,20 +467,27 @@ public final class MixinEnvironment implements ITokenProvider {
|
||||
return this.flag ? String.valueOf(this.getBooleanValue()) : this.getStringValue();
|
||||
}
|
||||
|
||||
boolean getBooleanValue() {
|
||||
return Booleans.parseBoolean(System.getProperty(this.property), false)
|
||||
|| (this.parent != null && this.parent.getBooleanValue());
|
||||
protected boolean getLocalBooleanValue() {
|
||||
return Booleans.parseBoolean(System.getProperty(this.property), false);
|
||||
}
|
||||
|
||||
protected boolean getInheritedBooleanValue() {
|
||||
return this.parent != null && this.parent.getBooleanValue();
|
||||
}
|
||||
|
||||
boolean getBooleanValue() {
|
||||
return this.getLocalBooleanValue() || this.getInheritedBooleanValue();
|
||||
}
|
||||
|
||||
String getStringValue() {
|
||||
return (this.parent == null || this.parent.getBooleanValue()) ? System.getProperty(this.property) : null;
|
||||
return (this.parent == null || this.parent.getBooleanValue()) ? System.getProperty(this.property) : this.defaultStringValue;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<E extends Enum<E>> E getEnumValue(E defaultValue) {
|
||||
String value = System.getProperty(this.property, defaultValue.name());
|
||||
try {
|
||||
return (E) Enum.valueOf(defaultValue.getClass(), value);
|
||||
return (E)Enum.valueOf(defaultValue.getClass(), value.toUpperCase());
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
+92
-26
@@ -28,10 +28,12 @@ import java.lang.annotation.Annotation;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -39,15 +41,7 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.spongepowered.asm.lib.Label;
|
||||
import org.spongepowered.asm.lib.Opcodes;
|
||||
import org.spongepowered.asm.lib.Type;
|
||||
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.AnnotationNode;
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
import org.spongepowered.asm.lib.tree.FieldNode;
|
||||
import org.spongepowered.asm.lib.tree.JumpInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.LabelNode;
|
||||
import org.spongepowered.asm.lib.tree.LineNumberNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodNode;
|
||||
import org.spongepowered.asm.lib.tree.*;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Intrinsic;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||
@@ -108,6 +102,23 @@ public class MixinApplicatorStandard {
|
||||
INJECT
|
||||
}
|
||||
|
||||
/**
|
||||
* Strategy for injecting initialiser insns
|
||||
*/
|
||||
enum InitialiserInjectionMode {
|
||||
/**
|
||||
* Default mode, attempts to place initialisers after all other
|
||||
* competing initialisers in the target ctor
|
||||
*/
|
||||
DEFAULT,
|
||||
|
||||
/**
|
||||
* Safe mode, only injects initialiser directly after the super-ctor
|
||||
* invocation
|
||||
*/
|
||||
SAFE
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal struct for representing a range
|
||||
*/
|
||||
@@ -600,7 +611,7 @@ public class MixinApplicatorStandard {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds a suitable ctor for reading the instance initialiser bytecode
|
||||
*
|
||||
@@ -753,25 +764,80 @@ public class MixinApplicatorStandard {
|
||||
protected final void injectInitialiser(MixinTargetContext mixin, MethodNode ctor, Deque<AbstractInsnNode> initialiser) {
|
||||
Map<LabelNode, LabelNode> labels = ASMHelper.cloneLabels(ctor.instructions);
|
||||
|
||||
for (Iterator<AbstractInsnNode> iter = ctor.instructions.iterator(0); iter.hasNext();) {
|
||||
AbstractInsnNode insn = iter.next();
|
||||
if (insn.getOpcode() == Opcodes.INVOKESPECIAL && Constants.CTOR.equals(((MethodInsnNode)insn).name)) {
|
||||
for (AbstractInsnNode node : initialiser) {
|
||||
if (node instanceof LabelNode) {
|
||||
continue;
|
||||
}
|
||||
if (node instanceof JumpInsnNode) {
|
||||
throw new InvalidMixinException(mixin, "Unsupported JUMP opcode in initialiser in " + mixin);
|
||||
}
|
||||
AbstractInsnNode imACloneNow = node.clone(labels);
|
||||
ctor.instructions.insert(insn, imACloneNow);
|
||||
insn = imACloneNow;
|
||||
}
|
||||
return;
|
||||
AbstractInsnNode insn = this.findInitialiserInjectionPoint(mixin, ctor, initialiser);
|
||||
if (insn == null) {
|
||||
this.logger.warn("Failed to locate initialiser injection point in <init>{}, initialiser was not mixed in.", ctor.desc);
|
||||
return;
|
||||
}
|
||||
|
||||
for (AbstractInsnNode node : initialiser) {
|
||||
if (node instanceof LabelNode) {
|
||||
continue;
|
||||
}
|
||||
if (node instanceof JumpInsnNode) {
|
||||
throw new InvalidMixinException(mixin, "Unsupported JUMP opcode in initialiser in " + mixin);
|
||||
}
|
||||
AbstractInsnNode imACloneNow = node.clone(labels);
|
||||
ctor.instructions.insert(insn, imACloneNow);
|
||||
insn = imACloneNow;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the injection point for injected initialiser insns in the target
|
||||
* ctor
|
||||
*
|
||||
* @param mixin target context for mixin being applied
|
||||
* @param ctor target ctor
|
||||
* @param initialiser source initialiser insns
|
||||
* @return target node
|
||||
*/
|
||||
protected AbstractInsnNode findInitialiserInjectionPoint(MixinTargetContext mixin, MethodNode ctor, Deque<AbstractInsnNode> initialiser) {
|
||||
Set<String> initialisedFields = new HashSet<String>();
|
||||
for (AbstractInsnNode initialiserInsn : initialiser) {
|
||||
if (initialiserInsn.getOpcode() == Opcodes.PUTFIELD) {
|
||||
initialisedFields.add(MixinApplicatorStandard.fieldKey((FieldInsnNode)initialiserInsn));
|
||||
}
|
||||
}
|
||||
|
||||
InitialiserInjectionMode mode = this.getInitialiserInjectionMode(mixin.getEnvironment());
|
||||
String targetName = mixin.getTargetClassInfo().getName();
|
||||
String targetSuperName = mixin.getTargetClassInfo().getSuperName();
|
||||
AbstractInsnNode targetInsn = null;
|
||||
|
||||
for (Iterator<AbstractInsnNode> iter = ctor.instructions.iterator(); iter.hasNext();) {
|
||||
AbstractInsnNode insn = iter.next();
|
||||
if (insn.getOpcode() == Opcodes.INVOKESPECIAL && Constants.CTOR.equals(((MethodInsnNode)insn).name)) {
|
||||
String owner = ((MethodInsnNode)insn).owner;
|
||||
if (owner.equals(targetName) || owner.equals(targetSuperName)) {
|
||||
targetInsn = insn;
|
||||
if (mode == InitialiserInjectionMode.SAFE) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (insn.getOpcode() == Opcodes.PUTFIELD && mode == InitialiserInjectionMode.DEFAULT) {
|
||||
String key = MixinApplicatorStandard.fieldKey((FieldInsnNode)insn);
|
||||
if (initialisedFields.contains(key)) {
|
||||
targetInsn = insn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.warn("Failed to locate super-invoke whilst injecting initialiser, initialiser was not mixed in!");
|
||||
return targetInsn;
|
||||
}
|
||||
|
||||
private InitialiserInjectionMode getInitialiserInjectionMode(MixinEnvironment environment) {
|
||||
String strMode = environment.getOptionValue(Option.INITIALISER_INJECTION_MODE);
|
||||
try {
|
||||
return InitialiserInjectionMode.valueOf(strMode.toUpperCase());
|
||||
} catch (Exception ex) {
|
||||
this.logger.warn("Could not parse unexpected value \"{}\" for mixin.initialiserInjectionMode, reverting to DEFAULT", strMode);
|
||||
return InitialiserInjectionMode.DEFAULT;
|
||||
}
|
||||
}
|
||||
|
||||
private static String fieldKey(FieldInsnNode fieldNode) {
|
||||
return String.format("%s:%s", fieldNode.desc, fieldNode.name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user