mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-19 02:39:13 +00:00
Initial implementation of environments, version bump to 0.3
This commit is contained in:
1 parent
dfb1a24c05
commit
5a6dc56db9
5 files changed
+210
-99
No files matched your search
@@ -24,6 +24,8 @@
|
||||
*/
|
||||
package org.spongepowered.asm.launch;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||
@@ -31,14 +33,12 @@ import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
|
||||
|
||||
public abstract class MixinBootstrap {
|
||||
|
||||
public static final String VERSION = "0.2";
|
||||
|
||||
public static final String VERSION = "0.3";
|
||||
public static final String INIT_KEY = "mixin.initialised";
|
||||
public static final String MIXIN_PACKAGE = "org.spongepowered.asm.mixin.";
|
||||
private static final String ASM_PACKAGE = "org.objectweb.asm.";
|
||||
|
||||
public static final String TRANSFORMER_CLASS = MixinBootstrap.MIXIN_PACKAGE + "transformer.MixinTransformer";
|
||||
|
||||
private static final String MIXIN_PACKAGE = "org.spongepowered.asm.mixin.";
|
||||
private static final String ASM_PACKAGE = "org.objectweb.asm.";
|
||||
private static final String TRANSFORMER_CLASS = MixinBootstrap.MIXIN_PACKAGE + "transformer.MixinTransformer";
|
||||
|
||||
static {
|
||||
Launch.classLoader.addClassLoaderExclusion(MixinBootstrap.ASM_PACKAGE);
|
||||
@@ -60,5 +60,13 @@ public abstract class MixinBootstrap {
|
||||
|
||||
Launch.blackboard.put(MixinBootstrap.INIT_KEY, MixinBootstrap.VERSION);
|
||||
MixinEnvironment.getCurrentEnvironment();
|
||||
|
||||
Launch.classLoader.registerTransformer(MixinBootstrap.TRANSFORMER_CLASS);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> tweakClasses = (List<String>)Launch.blackboard.get("TweakClasses");
|
||||
if (tweakClasses != null) {
|
||||
tweakClasses.add(MixinEnvironment.class.getName() + "$EnvironmentStateTweaker");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,12 +64,6 @@ public class MixinTweaker implements ITweaker {
|
||||
* file names.
|
||||
*/
|
||||
private final ITweaker fmlWrapper;
|
||||
|
||||
/**
|
||||
* If FML init fails the tweaker will look for configs, if it finds any then
|
||||
* it will set this flag to signify that it needs to inject the transformer
|
||||
*/
|
||||
private boolean injectTransformer;
|
||||
|
||||
/**
|
||||
* Hello world
|
||||
@@ -149,7 +143,6 @@ public class MixinTweaker implements ITweaker {
|
||||
|
||||
for (String config : mixinConfigs.split(",")) {
|
||||
if (config.endsWith(".json")) {
|
||||
this.injectTransformer = true;
|
||||
MixinEnvironment.getCurrentEnvironment().addConfiguration(config);
|
||||
}
|
||||
}
|
||||
@@ -164,8 +157,6 @@ public class MixinTweaker implements ITweaker {
|
||||
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
|
||||
if (this.fmlWrapper != null) {
|
||||
this.fmlWrapper.injectIntoClassLoader(Launch.classLoader);
|
||||
} else if (this.injectTransformer) {
|
||||
classLoader.registerTransformer(MixinBootstrap.TRANSFORMER_CLASS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
*/
|
||||
package org.spongepowered.asm.mixin;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -32,8 +33,12 @@ import org.apache.logging.log4j.core.helpers.Booleans;
|
||||
import org.spongepowered.asm.launch.MixinBootstrap;
|
||||
import org.spongepowered.asm.util.PrettyPrinter;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import net.minecraft.launchwrapper.IClassTransformer;
|
||||
import net.minecraft.launchwrapper.ITweaker;
|
||||
import net.minecraft.launchwrapper.Launch;
|
||||
import net.minecraft.launchwrapper.LaunchClassLoader;
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,6 +46,32 @@ import net.minecraft.launchwrapper.Launch;
|
||||
*/
|
||||
public class MixinEnvironment {
|
||||
|
||||
public static class Phase {
|
||||
|
||||
public static final Phase PREINIT = new Phase(0, "PREINIT");
|
||||
|
||||
public static final Phase DEFAULT = new Phase(1, "DEFAULT");
|
||||
|
||||
static final List<Phase> phases = ImmutableList.of(
|
||||
Phase.PREINIT,
|
||||
Phase.DEFAULT
|
||||
);
|
||||
|
||||
final int ordinal;
|
||||
|
||||
final String name;
|
||||
|
||||
private Phase(int ordinal, String name) {
|
||||
this.ordinal = ordinal;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
public static enum Side {
|
||||
/**
|
||||
* The environment was unable to determine current side
|
||||
@@ -185,17 +216,51 @@ public class MixinEnvironment {
|
||||
|| (this.parent != null && this.parent.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static class EnvironmentStateTweaker implements ITweaker {
|
||||
|
||||
@Override
|
||||
public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLaunchTarget() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getLaunchArguments() {
|
||||
MixinEnvironment.gotoPhase(Phase.DEFAULT);
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final String CONFIGS_KEY = "mixin.configs";
|
||||
private static final String TRANSFORMER_KEY = "mixin.transformer";
|
||||
|
||||
private static MixinEnvironment env;
|
||||
private static Phase currentPhase = Phase.PREINIT;
|
||||
|
||||
private Side side;
|
||||
private static MixinEnvironment[] environments = new MixinEnvironment[Phase.phases.size()];
|
||||
|
||||
private static MixinEnvironment currentEnvironment;
|
||||
|
||||
private final Phase phase;
|
||||
|
||||
private final String configsKey;
|
||||
|
||||
private final boolean[] options;
|
||||
|
||||
private MixinEnvironment() {
|
||||
private Side side;
|
||||
|
||||
private MixinEnvironment(Phase phase) {
|
||||
this.phase = phase;
|
||||
this.configsKey = MixinEnvironment.CONFIGS_KEY + "." + this.phase.name.toLowerCase();
|
||||
|
||||
// Sanity check
|
||||
Object version = Launch.blackboard.get(MixinBootstrap.INIT_KEY);
|
||||
if (version == null || !MixinBootstrap.VERSION.equals(version)) {
|
||||
@@ -212,7 +277,7 @@ public class MixinEnvironment {
|
||||
this.options[option.ordinal()] = option.getValue();
|
||||
}
|
||||
|
||||
if (this.getOption(Option.DEBUG_VERBOSE)) {
|
||||
if (this.getOption(Option.DEBUG_VERBOSE) && this.phase == Phase.PREINIT) {
|
||||
PrettyPrinter printer = new PrettyPrinter(32);
|
||||
printer.add("SpongePowered MIXIN (Verbose debugging enabled)").centre().hr();
|
||||
printer.add("%25s : %s", "Code source", this.getClass().getProtectionDomain().getCodeSource().getLocation());
|
||||
@@ -232,10 +297,10 @@ public class MixinEnvironment {
|
||||
*/
|
||||
public List<String> getMixinConfigs() {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> mixinConfigs = (List<String>) Launch.blackboard.get(MixinEnvironment.CONFIGS_KEY);
|
||||
List<String> mixinConfigs = (List<String>) Launch.blackboard.get(this.configsKey);
|
||||
if (mixinConfigs == null) {
|
||||
mixinConfigs = new ArrayList<String>();
|
||||
Launch.blackboard.put(MixinEnvironment.CONFIGS_KEY, mixinConfigs);
|
||||
Launch.blackboard.put(this.configsKey, mixinConfigs);
|
||||
}
|
||||
return mixinConfigs;
|
||||
}
|
||||
@@ -286,14 +351,6 @@ public class MixinEnvironment {
|
||||
return (String)Launch.blackboard.get(MixinBootstrap.INIT_KEY);
|
||||
}
|
||||
|
||||
public static MixinEnvironment getCurrentEnvironment() {
|
||||
if (MixinEnvironment.env == null) {
|
||||
MixinEnvironment.env = new MixinEnvironment();
|
||||
}
|
||||
|
||||
return MixinEnvironment.env;
|
||||
}
|
||||
|
||||
public boolean getOption(Option option) {
|
||||
return this.options[option.ordinal()];
|
||||
}
|
||||
@@ -301,4 +358,34 @@ public class MixinEnvironment {
|
||||
public void setOption(Option option, boolean value) {
|
||||
this.options[option.ordinal()] = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s[%s]", this.getClass().getSimpleName(), this.phase);
|
||||
}
|
||||
|
||||
public static MixinEnvironment getEnvironment(Phase phase) {
|
||||
if (MixinEnvironment.environments[phase.ordinal] == null) {
|
||||
MixinEnvironment.environments[phase.ordinal] = new MixinEnvironment(phase);
|
||||
}
|
||||
|
||||
return MixinEnvironment.environments[phase.ordinal];
|
||||
}
|
||||
|
||||
public static MixinEnvironment getDefaultEnvironment() {
|
||||
return MixinEnvironment.getEnvironment(Phase.DEFAULT);
|
||||
}
|
||||
|
||||
public static MixinEnvironment getCurrentEnvironment() {
|
||||
if (MixinEnvironment.currentEnvironment == null) {
|
||||
MixinEnvironment.currentEnvironment = MixinEnvironment.getEnvironment(MixinEnvironment.currentPhase);
|
||||
}
|
||||
|
||||
return MixinEnvironment.currentEnvironment;
|
||||
}
|
||||
|
||||
static void gotoPhase(Phase phase) {
|
||||
MixinEnvironment.currentPhase = phase;
|
||||
MixinEnvironment.currentEnvironment = MixinEnvironment.getEnvironment(MixinEnvironment.currentPhase);
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,6 @@ import org.spongepowered.asm.mixin.MixinApplyError;
|
||||
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.transformer.meta.MixinMerged;
|
||||
import org.spongepowered.asm.transformers.TreeTransformer;
|
||||
import org.spongepowered.asm.util.Constants;
|
||||
|
||||
@@ -174,23 +173,24 @@ public class MixinTransformer extends TreeTransformer {
|
||||
private final Logger logger = LogManager.getLogger("mixin");
|
||||
|
||||
/**
|
||||
* Mixin configuration bundle
|
||||
* All mixin configuration bundles
|
||||
*/
|
||||
private final List<MixinConfig> configs = new ArrayList<MixinConfig>();
|
||||
|
||||
/**
|
||||
* Uninitialised mixin configuration bundles
|
||||
*/
|
||||
private final List<MixinConfig> pendingConfigs = new ArrayList<MixinConfig>();
|
||||
|
||||
/**
|
||||
* Transformer modules
|
||||
*/
|
||||
private final List<IMixinTransformerModule> modules = new ArrayList<IMixinTransformerModule>();
|
||||
|
||||
/**
|
||||
* True once initialisation is done. All mixin configs needs to be
|
||||
* initialised as late as possible in startup (so that other transformers
|
||||
* have had time to register) but before any game classes are transformed.
|
||||
* To do this we initialise the configs on the first call to
|
||||
* {@link #transform} and this flag keeps track of when we've done this.
|
||||
* Current environment
|
||||
*/
|
||||
private boolean initDone;
|
||||
private MixinEnvironment currentEnvironment;
|
||||
|
||||
/**
|
||||
* Re-entrance detector
|
||||
@@ -218,53 +218,9 @@ public class MixinTransformer extends TreeTransformer {
|
||||
// I am a leaf on the wind
|
||||
environment.setActiveTransformer(this);
|
||||
|
||||
this.addConfigs(environment);
|
||||
this.addModules(environment);
|
||||
|
||||
TreeInfo.setLock(this.lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add configurations from the supplied mixin environment to the configs set
|
||||
*
|
||||
* @param environment Environment to query
|
||||
*/
|
||||
private void addConfigs(MixinEnvironment environment) {
|
||||
List<String> configs = environment.getMixinConfigs();
|
||||
|
||||
if (configs != null) {
|
||||
for (String configFile : configs) {
|
||||
try {
|
||||
MixinConfig config = MixinConfig.create(configFile);
|
||||
if (config != null) {
|
||||
this.configs.add(config);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
this.logger.warn(String.format("Failed to load mixin config: %s", configFile), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Collections.sort(this.configs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up this transformer using options from the supplied environment
|
||||
*
|
||||
* @param environment Environment to query
|
||||
*/
|
||||
private void addModules(MixinEnvironment environment) {
|
||||
// Run CheckClassAdapter on the mixin bytecode if debug option is enabled
|
||||
if (environment.getOption(Option.DEBUG_VERIFY)) {
|
||||
this.modules.add(new MixinTransformerModuleCheckClass());
|
||||
}
|
||||
|
||||
// Run implementation checker if option is enabled
|
||||
if (environment.getOption(Option.CHECK_IMPLEMENTS)) {
|
||||
this.modules.add(new MixinTransformerModuleInterfaceChecker());
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.minecraft.launchwrapper.IClassTransformer
|
||||
* #transform(java.lang.String, java.lang.String, byte[])
|
||||
@@ -277,10 +233,11 @@ public class MixinTransformer extends TreeTransformer {
|
||||
|
||||
boolean locked = this.lock.push().isSet();
|
||||
|
||||
if (!this.initDone && !locked) {
|
||||
this.initDone = true;
|
||||
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
|
||||
|
||||
if (this.currentEnvironment != environment && !locked) {
|
||||
try {
|
||||
this.initConfigs();
|
||||
this.init(environment);
|
||||
} catch (Exception ex) {
|
||||
this.lock.pop();
|
||||
throw new RuntimeException(ex);
|
||||
@@ -319,7 +276,7 @@ public class MixinTransformer extends TreeTransformer {
|
||||
try {
|
||||
basicClass = this.applyMixins(transformedName, basicClass, mixins);
|
||||
} catch (InvalidMixinException th) {
|
||||
if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DUMP_TARGET_ON_FAILURE)) {
|
||||
if (environment.getOption(Option.DUMP_TARGET_ON_FAILURE)) {
|
||||
MixinTransformer.dumpClass(transformedName.replace('.', '/') + ".target", basicClass);
|
||||
}
|
||||
|
||||
@@ -343,11 +300,59 @@ public class MixinTransformer extends TreeTransformer {
|
||||
}
|
||||
}
|
||||
|
||||
private void init(MixinEnvironment environment) {
|
||||
this.addConfigs(environment);
|
||||
this.addModules(environment);
|
||||
this.initConfigs();
|
||||
this.currentEnvironment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add configurations from the supplied mixin environment to the configs set
|
||||
*
|
||||
* @param environment Environment to query
|
||||
*/
|
||||
private void addConfigs(MixinEnvironment environment) {
|
||||
List<String> configs = environment.getMixinConfigs();
|
||||
|
||||
if (configs != null) {
|
||||
for (String configFile : configs) {
|
||||
try {
|
||||
MixinConfig config = MixinConfig.create(configFile);
|
||||
if (config != null) {
|
||||
this.pendingConfigs.add(config);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
this.logger.warn(String.format("Failed to load mixin config: %s", configFile), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Collections.sort(this.pendingConfigs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up this transformer using options from the supplied environment
|
||||
*
|
||||
* @param environment Environment to query
|
||||
*/
|
||||
private void addModules(MixinEnvironment environment) {
|
||||
// Run CheckClassAdapter on the mixin bytecode if debug option is enabled
|
||||
if (environment.getOption(Option.DEBUG_VERIFY)) {
|
||||
this.modules.add(new MixinTransformerModuleCheckClass());
|
||||
}
|
||||
|
||||
// Run implementation checker if option is enabled
|
||||
if (environment.getOption(Option.CHECK_IMPLEMENTS)) {
|
||||
this.modules.add(new MixinTransformerModuleInterfaceChecker());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise mixin configs
|
||||
*/
|
||||
private void initConfigs() {
|
||||
for (MixinConfig config : this.configs) {
|
||||
for (MixinConfig config : this.pendingConfigs) {
|
||||
try {
|
||||
config.initialise();
|
||||
} catch (Exception ex) {
|
||||
@@ -355,14 +360,14 @@ public class MixinTransformer extends TreeTransformer {
|
||||
}
|
||||
}
|
||||
|
||||
for (MixinConfig config : this.configs) {
|
||||
for (MixinConfig config : this.pendingConfigs) {
|
||||
IMixinConfigPlugin plugin = config.getPlugin();
|
||||
if (plugin == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Set<String> otherTargets = new HashSet<String>();
|
||||
for (MixinConfig otherConfig : this.configs) {
|
||||
for (MixinConfig otherConfig : this.pendingConfigs) {
|
||||
if (!otherConfig.equals(config)) {
|
||||
otherTargets.addAll(otherConfig.getTargets());
|
||||
}
|
||||
@@ -371,13 +376,17 @@ public class MixinTransformer extends TreeTransformer {
|
||||
plugin.acceptTargets(config.getTargets(), Collections.unmodifiableSet(otherTargets));
|
||||
}
|
||||
|
||||
for (MixinConfig config : this.configs) {
|
||||
for (MixinConfig config : this.pendingConfigs) {
|
||||
try {
|
||||
config.postInitialise();
|
||||
} catch (Exception ex) {
|
||||
this.logger.error("Error encountered during mixin config postInit setp'" + config.getName() + "': " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
this.configs.addAll(this.pendingConfigs);
|
||||
Collections.sort(this.configs);
|
||||
this.pendingConfigs.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTransformer.ReEntranceState;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
@@ -76,19 +77,16 @@ abstract class TreeInfo {
|
||||
*/
|
||||
private static IClassNameTransformer nameTransformer;
|
||||
|
||||
/**
|
||||
* Current environment
|
||||
*/
|
||||
private static MixinEnvironment currentEnvironment;
|
||||
|
||||
/**
|
||||
* Re-entrance lock
|
||||
*/
|
||||
private static ReEntranceState lock;
|
||||
|
||||
static {
|
||||
for (IClassTransformer transformer : Launch.classLoader.getTransformers()) {
|
||||
if (transformer instanceof IClassNameTransformer) {
|
||||
TreeInfo.nameTransformer = (IClassNameTransformer) transformer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void setLock(ReEntranceState lock) {
|
||||
TreeInfo.lock = lock;
|
||||
}
|
||||
@@ -115,6 +113,11 @@ abstract class TreeInfo {
|
||||
* @return Transformed class bytecode for the specified class
|
||||
*/
|
||||
static byte[] loadClass(String className, boolean runTransformers) throws ClassNotFoundException, IOException {
|
||||
if (MixinEnvironment.getCurrentEnvironment() != TreeInfo.currentEnvironment) {
|
||||
TreeInfo.transformers = null;
|
||||
TreeInfo.nameTransformer = null;
|
||||
}
|
||||
|
||||
String transformedName = className.replace('/', '.');
|
||||
String name = TreeInfo.unmap(transformedName);
|
||||
byte[] classBytes = null;
|
||||
@@ -224,6 +227,18 @@ abstract class TreeInfo {
|
||||
return TreeInfo.transformers;
|
||||
}
|
||||
|
||||
private static IClassNameTransformer getNameTransformer() {
|
||||
if (TreeInfo.nameTransformer == null) {
|
||||
for (IClassTransformer transformer : Launch.classLoader.getTransformers()) {
|
||||
if (transformer instanceof IClassNameTransformer) {
|
||||
TreeInfo.nameTransformer = (IClassNameTransformer) transformer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return TreeInfo.nameTransformer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a class name back to its obfuscated counterpart
|
||||
*
|
||||
@@ -231,8 +246,9 @@ abstract class TreeInfo {
|
||||
* @return obfuscated name for the specified deobfuscated reference
|
||||
*/
|
||||
static String unmap(String className) {
|
||||
if (TreeInfo.nameTransformer != null) {
|
||||
return TreeInfo.nameTransformer.unmapClassName(className);
|
||||
IClassNameTransformer nameTransformer = TreeInfo.getNameTransformer();
|
||||
if (nameTransformer != null) {
|
||||
return nameTransformer.unmapClassName(className);
|
||||
}
|
||||
|
||||
return className;
|
||||
|
||||
Reference in new issue
Block a user