diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
index 1cc02470..022a37b1 100644
--- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
+++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java
@@ -28,35 +28,83 @@ import java.util.List;
import net.minecraft.launchwrapper.Launch;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.MixinEnvironment;
-import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
+import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
+/**
+ * Bootstaps the mixin subsystem. This class acts as a bridge between the mixin
+ * subsystem and the tweaker or coremod which is boostrapping it. Without this
+ * class, a coremod may cause classload of MixinEnvironment in the
+ * LaunchClassLoader before we have a chance to exclude it. By placing the main
+ * bootstap logic here we avoid the need for consumers to add the classloader
+ * exclusion themselves.
+ *
+ *
In development, where (because of the classloader environment at dev time)
+ * it is safe to let a coremod initialise the mixin subsystem, we can perform
+ * initialisation all in one go using the {@link #init} method and everything is
+ * fine. However in production the tweaker must be used and the situation is a
+ * little more delicate.
+ *
+ * In an ideal world, the mixin tweaker would initialise the environment in
+ * its constructor and that would be the end of the story. However we also need
+ * to register the additional tweaker for environment to detect the transition
+ * from pre-init to default and we cannot do this within the tweaker constructor
+ * witout triggering a ConcurrentModificationException in the tweaker list. To
+ * work around this we register the secondary tweaker from within the mixin
+ * tweaker's acceptOptions method instead.
+ */
public abstract class MixinBootstrap {
+ /**
+ * Subsystem version
+ */
public static final String VERSION = "0.3";
+
+ /**
+ * Blackboard key where the subsystem version will be stored to indicate
+ * successful bootstrap
+ */
public static final String INIT_KEY = "mixin.initialised";
+ // Consts
+ private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
private static final String MIXIN_PACKAGE = "org.spongepowered.asm.mixin.";
private static final String ASM_PACKAGE = "org.objectweb.asm.";
private static final String TRANSFORMER_PROXY_CLASS = MixinBootstrap.MIXIN_PACKAGE + "transformer.MixinTransformer$Proxy";
- private static boolean initialised = false;
+ /**
+ * Log all the things
+ */
+ private static final Logger logger = LogManager.getLogger("mixin");
+ // These are Klass local, with luck this shouldn't be a problem
+ private static boolean initialised = false;
+ private static boolean injectStateTweaker = true;
+
+ // Static initialiser, add classloader exclusions as early as possible
static {
+ // The important ones
Launch.classLoader.addClassLoaderExclusion(MixinBootstrap.ASM_PACKAGE);
-
- @SuppressWarnings("unused")
- IMixinConfigPlugin forceClassLoad = null;
-
Launch.classLoader.addClassLoaderExclusion(MixinBootstrap.MIXIN_PACKAGE);
+
+ // Only needed in dev, in production this would be handled by the tweaker
+ Launch.classLoader.addClassLoaderExclusion(MixinBootstrap.LAUNCH_PACKAGE);
}
private MixinBootstrap() {}
+ /**
+ * Register a new proxy transformer
+ */
public static void addProxy() {
Launch.classLoader.registerTransformer(MixinBootstrap.TRANSFORMER_PROXY_CLASS);
}
+ /**
+ * Initialise the mixin subsystem
+ */
public static void init() {
if (!MixinBootstrap.preInit()) {
return;
@@ -65,6 +113,9 @@ public abstract class MixinBootstrap {
MixinBootstrap.register();
}
+ /**
+ * Phase 1 of mixin initialisation
+ */
static boolean preInit() {
Object registeredVersion = Launch.blackboard.get(MixinBootstrap.INIT_KEY);
if (registeredVersion != null) {
@@ -80,26 +131,39 @@ public abstract class MixinBootstrap {
if (!MixinBootstrap.initialised) {
MixinBootstrap.initialised = true;
- MixinEnvironment.getCurrentEnvironment();
- MixinBootstrap.addProxy();
if (MixinBootstrap.findInStackTrace(Launch.class.getName(), "launch") > 132) {
-// MixinEnvironment.gotoPhase(Phase.DEFAULT); TODO
+ MixinBootstrap.logger.error("Initialising mixin subsystem after game pre-init phase! Some mixins may be skipped.");
+ MixinEnvironment.init(Phase.DEFAULT);
+ MixinBootstrap.injectStateTweaker = false;
+ } else {
+ MixinEnvironment.init(Phase.PREINIT);
}
+
+ MixinBootstrap.addProxy();
}
return true;
}
+ /**
+ * Phase 2 of mixin initialisation, register the state tweaker
+ */
static void register() {
if (!MixinBootstrap.initialised) {
throw new IllegalStateException("MixinBootstrap.register() called before MixinBootstrap.preInit()");
}
- @SuppressWarnings("unchecked")
- List tweakClasses = (List)Launch.blackboard.get("TweakClasses");
- if (tweakClasses != null) {
- tweakClasses.add(MixinEnvironment.class.getName() + "$EnvironmentStateTweaker");
+ if (MixinBootstrap.injectStateTweaker) {
+ if (MixinBootstrap.findInStackTrace(Launch.class.getName(), "launch") < 4) {
+ MixinBootstrap.logger.warn("MixinBootstrap.register() called during a tweak constructor. Expect CoModificationException in 5.. 4..");
+ }
+
+ @SuppressWarnings("unchecked")
+ List tweakClasses = (List)Launch.blackboard.get("TweakClasses");
+ if (tweakClasses != null) {
+ tweakClasses.add(MixinEnvironment.class.getName() + "$EnvironmentStateTweaker");
+ }
}
}
diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java
index 88ad8f8a..f8a5eab4 100644
--- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java
+++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java
@@ -53,6 +53,11 @@ public class MixinEnvironment {
*/
public static class Phase {
+ /**
+ * Not initialised phase
+ */
+ static final Phase NOT_INITIALISED = new Phase(-1, "NOT_INITIALISED");
+
/**
* "Pre initialisation" phase, everything before the tweak system begins
* to load the game
@@ -273,21 +278,44 @@ public class MixinEnvironment {
}
+ // Blackboard keys
private static final String CONFIGS_KEY = "mixin.configs";
private static final String TRANSFORMER_KEY = "mixin.transformer";
- private static Phase currentPhase = Phase.PREINIT;
+ /**
+ * Current (active) environment phase, set to NOT_INITIALISED until the
+ * phases have been populated
+ */
+ private static Phase currentPhase = Phase.NOT_INITIALISED;
+ /**
+ * Array of all (real) environments, indexed by ordinal
+ */
private static MixinEnvironment[] environments = new MixinEnvironment[Phase.phases.size()];
+ /**
+ * Currently active environment
+ */
private static MixinEnvironment currentEnvironment;
+ /**
+ * The phase for this environment
+ */
private final Phase phase;
+ /**
+ * The blackboard key for this environment's configs
+ */
private final String configsKey;
+ /**
+ * This environment's options
+ */
private final boolean[] options;
+ /**
+ * Detected side
+ */
private Side side;
private MixinEnvironment(Phase phase) {
@@ -323,6 +351,13 @@ public class MixinEnvironment {
}
}
+ /**
+ * Get the phase for this environment
+ */
+ public Phase getPhase() {
+ return this.phase;
+ }
+
/**
* Get mixin configurations from the blackboard
*
@@ -352,14 +387,30 @@ public class MixinEnvironment {
return this;
}
+ /**
+ * Get the active mixin transformer instance (if any)
+ */
public Object getActiveTransformer() {
return Launch.blackboard.get(MixinEnvironment.TRANSFORMER_KEY);
}
+ /**
+ * Set the mixin transformer instance
+ *
+ * @param transformer Mixin Transformer
+ */
public void setActiveTransformer(IClassTransformer transformer) {
- Launch.blackboard.put(MixinEnvironment.TRANSFORMER_KEY, transformer);
+ if (transformer != null) {
+ Launch.blackboard.put(MixinEnvironment.TRANSFORMER_KEY, transformer);
+ }
}
+ /**
+ * Allows a third party to set the side if the side is currently UNKNOWN
+ *
+ * @param side Side to set to
+ * @return fluent interface
+ */
public MixinEnvironment setSide(Side side) {
if (side != null && this.getSide() == Side.UNKNOWN && side != Side.UNKNOWN) {
this.side = side;
@@ -367,6 +418,11 @@ public class MixinEnvironment {
return this;
}
+ /**
+ * Get (and detect if necessary) the current side
+ *
+ * @return current side (or UNKNOWN if could not be determined)
+ */
public Side getSide() {
if (this.side == null) {
for (Side side : Side.values()) {
@@ -380,18 +436,36 @@ public class MixinEnvironment {
return this.side != null ? this.side : Side.UNKNOWN;
}
+ /**
+ * Get the current mixin subsystem version
+ */
public String getVersion() {
return (String)Launch.blackboard.get(MixinBootstrap.INIT_KEY);
}
+ /**
+ * Get the specified option from the current environment
+ *
+ * @param option Option to get
+ * @return Option value
+ */
public boolean getOption(Option option) {
return this.options[option.ordinal()];
}
+ /**
+ * Set the specified option for this environment
+ *
+ * @param option Option to set
+ * @param value New option value
+ */
public void setOption(Option option, boolean value) {
this.options[option.ordinal()] = value;
}
+ /**
+ * Invoke a mixin environment audit process
+ */
public void audit() {
Object activeTransformer = this.getActiveTransformer();
if (activeTransformer instanceof MixinTransformer) {
@@ -400,12 +474,48 @@ public class MixinEnvironment {
}
}
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
@Override
public String toString() {
return String.format("%s[%s]", this.getClass().getSimpleName(), this.phase);
}
+ /**
+ * Get the current phase, triggers initialisation if necessary
+ */
+ private static Phase getCurrentPhase() {
+ if (MixinEnvironment.currentPhase == Phase.NOT_INITIALISED) {
+ MixinEnvironment.init(Phase.PREINIT);
+ }
+
+ return MixinEnvironment.currentPhase;
+ }
+
+ /**
+ * Initialise the mixin environment in the specified phase
+ *
+ * @param phase initial phase
+ */
+ public static void init(Phase phase) {
+ if (MixinEnvironment.currentPhase == Phase.NOT_INITIALISED) {
+ MixinEnvironment.currentPhase = phase;
+ MixinEnvironment.getEnvironment(phase);
+ }
+ }
+
+ /**
+ * Get the mixin environment for the specified phase
+ *
+ * @param phase phase to fetch environment for
+ * @return the environment
+ */
public static MixinEnvironment getEnvironment(Phase phase) {
+ if (phase.ordinal < 0) {
+ throw new IllegalArgumentException("Cannot access the UNINITIALISED environment");
+ }
+
if (MixinEnvironment.environments[phase.ordinal] == null) {
MixinEnvironment.environments[phase.ordinal] = new MixinEnvironment(phase);
}
@@ -413,24 +523,39 @@ public class MixinEnvironment {
return MixinEnvironment.environments[phase.ordinal];
}
+ /**
+ * Gets the default environment
+ */
public static MixinEnvironment getDefaultEnvironment() {
return MixinEnvironment.getEnvironment(Phase.DEFAULT);
}
+ /**
+ * Gets the current environment
+ */
public static MixinEnvironment getCurrentEnvironment() {
if (MixinEnvironment.currentEnvironment == null) {
- MixinEnvironment.currentEnvironment = MixinEnvironment.getEnvironment(MixinEnvironment.currentPhase);
+ MixinEnvironment.currentEnvironment = MixinEnvironment.getEnvironment(MixinEnvironment.getCurrentPhase());
}
return MixinEnvironment.currentEnvironment;
}
-
+
+ /**
+ * Internal callback
+ *
+ * @param phase
+ */
static void gotoPhase(Phase phase) {
- if (phase.ordinal > MixinEnvironment.currentPhase.ordinal) {
+ if (phase == null || phase.ordinal < 0) {
+ throw new IllegalArgumentException("Cannot go to the specified phase, phase is null or invalid");
+ }
+
+ if (phase.ordinal > getCurrentPhase().ordinal) {
MixinBootstrap.addProxy();
}
MixinEnvironment.currentPhase = phase;
- MixinEnvironment.currentEnvironment = MixinEnvironment.getEnvironment(MixinEnvironment.currentPhase);
+ MixinEnvironment.currentEnvironment = MixinEnvironment.getEnvironment(MixinEnvironment.getCurrentPhase());
}
}