Update hotswapper agent for 0.8

This commit is contained in:
Mumfrey committed 2019-10-24 16:28:34 +01:00
1 parent 7b37a681ee
commit da74418eca
7 files changed
+79 -46

No files matched your search

@@ -182,8 +182,8 @@ public class MixinAgent implements IHotSwap {
}
@Override
public void registerTargetClass(String name, byte[] bytecode) {
MixinAgent.classLoader.addTargetClass(name, bytecode);
public void registerTargetClass(String name, ClassNode classNode) {
MixinAgent.classLoader.addTargetClass(name, classNode);
}
/**
@@ -228,4 +228,5 @@ public class MixinAgent implements IHotSwap {
public static void agentmain(String arg, Instrumentation instrumentation) {
MixinAgent.init(instrumentation);
}
}
@@ -33,6 +33,7 @@ import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.util.Constants;
@@ -78,10 +79,23 @@ class MixinAgentClassLoader extends ClassLoader {
* Registers the bytecode for a class targeted by a mixin
*
* @param name Name of the target clas
* @param bytecode Bytecode of the target class
* @param classNode ASM tree node of the target class
*/
void addTargetClass(String name, byte[] bytecode) {
this.targets.put(name, bytecode);
void addTargetClass(String name, ClassNode classNode) {
synchronized (this.targets) {
if (this.targets.containsKey(name)) {
return;
}
try {
ClassWriter cw = new ClassWriter(0);
classNode.accept(cw);
this.targets.put(name, cw.toByteArray());
} catch (Exception ex) {
MixinAgentClassLoader.logger.error("Error storing original class bytecode for {} in mixin hotswap agent. {}: {}",
name, ex.getClass().getName(), ex.getMessage());
MixinAgentClassLoader.logger.debug(ex);
}
}
}
/**
@@ -101,7 +115,9 @@ class MixinAgentClassLoader extends ClassLoader {
* @return Original bytecode
*/
byte[] getOriginalTargetBytecode(String name) {
return this.targets.get(name);
synchronized (this.targets) {
return this.targets.get(name);
}
}
/**
@@ -127,4 +143,5 @@ class MixinAgentClassLoader extends ClassLoader {
cw.visitEnd();
return cw.toByteArray();
}
}
@@ -518,10 +518,14 @@ class MixinPreProcessorStandard {
String type = method.isSynthetic() ? "synthetic" : "@Unique";
if (Bytecode.getVisibility(mixinMethod).ordinal() < Visibility.PUBLIC.ordinal()) {
String uniqueName = context.getUniqueName(mixinMethod, false);
MixinPreProcessorStandard.logger.log(this.mixin.getLoggingLevel(), "Renaming {} method {}{} to {} in {}",
type, mixinMethod.name, mixinMethod.desc, uniqueName, this.mixin);
mixinMethod.name = method.conform(uniqueName);
if (method.isConformed()) {
mixinMethod.name = method.getName();
} else {
String uniqueName = context.getUniqueName(mixinMethod, false);
MixinPreProcessorStandard.logger.log(this.mixin.getLoggingLevel(), "Renaming {} method {}{} to {} in {}",
type, mixinMethod.name, mixinMethod.desc, uniqueName, this.mixin);
mixinMethod.name = method.conform(uniqueName);
}
return false;
}
@@ -24,7 +24,6 @@
*/
package org.spongepowered.asm.mixin.transformer;
import java.lang.reflect.Constructor;
import java.text.DecimalFormat;
import java.util.*;
@@ -132,8 +131,6 @@ public class MixinProcessor {
}
}
private static final String MIXIN_AGENT_CLASS = "org.spongepowered.tools.agent.MixinAgent";
/**
* Log all the things
@@ -210,36 +207,16 @@ public class MixinProcessor {
/**
* ctor
*/
MixinProcessor(MixinEnvironment environment, Extensions extensions) {
MixinProcessor(MixinEnvironment environment, Extensions extensions, IHotSwap hotSwapper) {
this.lock = this.service.getReEntranceLock();
this.extensions = extensions;
this.hotSwapper = this.initHotSwapper(environment);
this.hotSwapper = hotSwapper;
this.postProcessor = new MixinPostProcessor(this.sessionId);
this.profiler = MixinEnvironment.getProfiler();
}
private IHotSwap initHotSwapper(MixinEnvironment environment) {
if (!environment.getOption(Option.HOT_SWAP)) {
return null;
}
try {
MixinProcessor.logger.info("Attempting to load Hot-Swap agent");
@SuppressWarnings("unchecked")
Class<? extends IHotSwap> clazz =
(Class<? extends IHotSwap>)Class.forName(MixinProcessor.MIXIN_AGENT_CLASS);
Constructor<? extends IHotSwap> ctor = clazz.getDeclaredConstructor(MixinProcessor.class);
return ctor.newInstance(this);
} catch (Throwable th) {
MixinProcessor.logger.info("Hot-swap agent could not be loaded, hot swapping of mixins won't work. {}: {}",
th.getClass().getSimpleName(), th.getMessage());
}
return null;
}
/**
* Force-load all classes targetted by mixins but not yet applied
*
@@ -345,8 +322,7 @@ public class MixinProcessor {
}
if (this.hotSwapper != null) {
// TODO hotswapper
// this.hotSwapper.registerTargetClass(transformedName, basicClass);
this.hotSwapper.registerTargetClass(name, targetClassNode);
}
try {
@@ -24,13 +24,16 @@
*/
package org.spongepowered.asm.mixin.transformer;
import java.lang.reflect.Constructor;
import java.util.List;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.throwables.MixinException;
import org.spongepowered.asm.mixin.transformer.ext.Extensions;
import org.spongepowered.asm.mixin.transformer.ext.IExtensionRegistry;
import org.spongepowered.asm.mixin.transformer.ext.IHotSwap;
import org.spongepowered.asm.transformers.TreeTransformer;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.asm.ASM;
@@ -40,10 +43,22 @@ import org.spongepowered.asm.util.asm.ASM;
*/
class MixinTransformer extends TreeTransformer implements IMixinTransformer {
private static final String MIXIN_AGENT_CLASS = "org.spongepowered.tools.agent.MixinAgent";
/**
* Synthetic class registry
*/
private final SyntheticClassRegistry syntheticClassRegistry;
/**
* Transformer extensions
*/
private final Extensions extensions;
/**
* Hotswap agent, if available
*/
private final IHotSwap hotSwapper;
/**
* Mixin processor which actually manages application of mixins
@@ -55,11 +70,6 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
*/
private final MixinClassGenerator generator;
/**
* Synthetic class registry
*/
private final SyntheticClassRegistry syntheticClassRegistry;
public MixinTransformer() {
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
@@ -73,13 +83,35 @@ class MixinTransformer extends TreeTransformer implements IMixinTransformer {
this.syntheticClassRegistry = new SyntheticClassRegistry();
this.extensions = new Extensions(this.syntheticClassRegistry);
this.hotSwapper = this.initHotSwapper(environment);
this.processor = new MixinProcessor(environment, this.extensions);
this.processor = new MixinProcessor(environment, this.extensions, this.hotSwapper);
this.generator = new MixinClassGenerator(environment, this.extensions);
DefaultExtensions.create(environment, this.extensions, this.syntheticClassRegistry);
}
private IHotSwap initHotSwapper(MixinEnvironment environment) {
if (!environment.getOption(Option.HOT_SWAP)) {
return null;
}
try {
MixinProcessor.logger.info("Attempting to load Hot-Swap agent");
@SuppressWarnings("unchecked")
Class<? extends IHotSwap> clazz =
(Class<? extends IHotSwap>)Class.forName(MixinTransformer.MIXIN_AGENT_CLASS);
Constructor<? extends IHotSwap> ctor = clazz.getDeclaredConstructor(IMixinTransformer.class);
return ctor.newInstance(this);
} catch (Throwable th) {
MixinProcessor.logger.info("Hot-swap agent could not be loaded, hot swapping of mixins won't work. {}: {}",
th.getClass().getSimpleName(), th.getMessage());
}
return null;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformer
* #getExtensions()
@@ -24,6 +24,8 @@
*/
package org.spongepowered.asm.mixin.transformer.ext;
import org.objectweb.asm.tree.ClassNode;
/**
* Interface to allow the hot-swap agent to be loaded on-demand
*/
@@ -42,11 +44,11 @@ public interface IHotSwap {
* Registers a class targeted by at least one mixin.
*
* <p>This is used to rollback the target class to a state before the
* mixin's were applied.</p>
* mixins were applied.</p>
*
* @param name Name of the class
* @param bytecode Bytecode of the class before mixin's have been applied
* @param classNode ASM tree of the class before mixins have been applied
*/
public abstract void registerTargetClass(String name, byte[] bytecode);
public abstract void registerTargetClass(String name, ClassNode classNode);
}
@@ -108,4 +108,5 @@ public class MixinTransformationHandler implements IClassProcessor {
}
return transformer.transformClass(environment, classType.getClassName(), classNode);
}
}