mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-18 10:19:15 +00:00
Tidy up code
This commit is contained in:
1 parent
5a7eaeae79
commit
bc8296da0e
6 files changed
+154
-50
No files matched your search
@@ -30,6 +30,7 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.spongepowered.asm.mixin.transformer.MixinTransformer;
|
||||
import org.spongepowered.asm.mixin.transformer.debug.IHotSwap;
|
||||
|
||||
import java.lang.instrument.ClassDefinition;
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
@@ -38,46 +39,60 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A java agent that re-transforms a mixin's target class if the mixin has been
|
||||
* redefined. This agent enables hot-swapping of mixins.
|
||||
*
|
||||
* This class is a singleton because it needs to conofrm to the IHotSwap interface.
|
||||
* An agent that re-transforms a mixin's target classes if the mixin has been
|
||||
* redefined. Basically this agent enables hot-swapping of mixins.
|
||||
*/
|
||||
public class MixinAgent implements IHotSwap{
|
||||
public class MixinAgent implements IHotSwap {
|
||||
|
||||
private class Transformer implements ClassFileTransformer{
|
||||
/**
|
||||
* Class file transformer that re-transforms mixin's target classes if the
|
||||
* mixin has been redefined.
|
||||
*/
|
||||
private class Transformer implements ClassFileTransformer {
|
||||
|
||||
@Override
|
||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
|
||||
MixinAgent.logger.info("Redefining class "+className);
|
||||
if(classBeingRedefined == null)
|
||||
public byte[] transform(ClassLoader loader,
|
||||
String className,
|
||||
Class<?> classBeingRedefined,
|
||||
ProtectionDomain protectionDomain,
|
||||
byte[] classfileBuffer) throws IllegalClassFormatException {
|
||||
if (classBeingRedefined == null) {
|
||||
return null;
|
||||
byte[] mixinBytecode = MixinAgent.classLoader.getOriginalBytecode(classBeingRedefined);
|
||||
if(mixinBytecode != null){
|
||||
MixinAgent.logger.info("Redefining mixin "+className);
|
||||
}
|
||||
byte[] mixinBytecode = MixinAgent.classLoader.getFakeMixinBytecode(classBeingRedefined);
|
||||
if (mixinBytecode != null) {
|
||||
MixinAgent.logger.info("Redefining mixin " + className);
|
||||
List<String> targets;
|
||||
try {
|
||||
targets = MixinAgent.this.classTransformer.reload(className.replace('/', '.'), classfileBuffer);
|
||||
} catch (Throwable th) {
|
||||
// catch everything as otherwise it is ignored
|
||||
MixinAgent.logger.error("Error while finding targets for mixin "+className, th);
|
||||
MixinAgent.logger.error("Error while finding targets for mixin " + className, th);
|
||||
return mixinBytecode;
|
||||
}
|
||||
for (String target:targets) {
|
||||
MixinAgent.logger.debug("Re-transforming target class "+target);
|
||||
for (String target : targets) {
|
||||
String targetName = target.replace('/', '.');
|
||||
MixinAgent.logger.debug("Re-transforming target class " + target);
|
||||
try {
|
||||
instrumentation.retransformClasses(Launch.classLoader.findClass(target.replace('/', '.')));
|
||||
Class<?> targetClass = Launch.classLoader.findClass(targetName);
|
||||
byte[] targetBytecode = MixinAgent.classLoader.getOriginalTargetBytecode(targetName);
|
||||
if (targetBytecode == null) {
|
||||
MixinAgent.logger.error("Target class " + targetName + " hasn't registered its bytecode");
|
||||
continue;
|
||||
}
|
||||
targetBytecode = MixinAgent.this.classTransformer.transform(null, targetName, targetBytecode);
|
||||
instrumentation.redefineClasses(new ClassDefinition(targetClass, targetBytecode));
|
||||
} catch (Throwable th) {
|
||||
MixinAgent.logger.error("Error while re-transforming target class "+target, th);
|
||||
MixinAgent.logger.error("Error while re-transforming target class " + target, th);
|
||||
}
|
||||
}
|
||||
return mixinBytecode;
|
||||
}
|
||||
try {
|
||||
MixinAgent.logger.info("Redefining class "+className);
|
||||
MixinAgent.logger.info("Redefining class " + className);
|
||||
return MixinAgent.this.classTransformer.transform(null, className, classfileBuffer);
|
||||
} catch (Throwable th) {
|
||||
MixinAgent.logger.error("Error while re-transforming class "+className, th);
|
||||
MixinAgent.logger.error("Error while re-transforming class " + className, th);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -100,11 +115,14 @@ public class MixinAgent implements IHotSwap{
|
||||
*/
|
||||
private static List<MixinAgent> agents = new ArrayList<MixinAgent>();
|
||||
|
||||
/**
|
||||
* MixinTransformer instance to use to transform the mixin's target classes
|
||||
*/
|
||||
private final MixinTransformer classTransformer;
|
||||
|
||||
/**
|
||||
* Constructs an agent from a class transformer in which it will use to
|
||||
* transform the class.
|
||||
* transform mixin's target class.
|
||||
*
|
||||
* @param classTransformer Class transformer that will transform a mixin's
|
||||
* target class
|
||||
@@ -112,8 +130,9 @@ public class MixinAgent implements IHotSwap{
|
||||
public MixinAgent(MixinTransformer classTransformer) {
|
||||
this.classTransformer = classTransformer;
|
||||
MixinAgent.agents.add(this);
|
||||
if (MixinAgent.instrumentation != null)
|
||||
initTransformer();
|
||||
if (MixinAgent.instrumentation != null) {
|
||||
this.initTransformer();
|
||||
}
|
||||
}
|
||||
|
||||
private void initTransformer() {
|
||||
@@ -121,19 +140,25 @@ public class MixinAgent implements IHotSwap{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerMixinClass(String name, byte[] bytecode) {
|
||||
public void registerMixinClass(String name) {
|
||||
MixinAgent.classLoader.addMixinClass(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerTargetClass(String name, byte[] bytecode) {
|
||||
MixinAgent.classLoader.addTargetClass(name, bytecode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the agents
|
||||
* Sets the instrumentation instance so that the mixin agents can redefine
|
||||
* mixins.
|
||||
*
|
||||
* @param instrumentation Instance to use to transform the mixins
|
||||
* @param instrumentation Instance to use to redefine the mixins
|
||||
*/
|
||||
public static void init(Instrumentation instrumentation) {
|
||||
MixinAgent.instrumentation = instrumentation;
|
||||
if(!MixinAgent.instrumentation.isRetransformClassesSupported()) {
|
||||
MixinAgent.logger.error("The instrumentation doesn't support re-transformations");
|
||||
if (!MixinAgent.instrumentation.isRedefineClassesSupported()) {
|
||||
MixinAgent.logger.error("The instrumentation doesn't support re-definition of classes");
|
||||
}
|
||||
for (MixinAgent agent:agents) {
|
||||
agent.initTransformer();
|
||||
@@ -141,7 +166,7 @@ public class MixinAgent implements IHotSwap{
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the agent
|
||||
* Initialize the java agent
|
||||
*
|
||||
* This will be called automatically if the jar is in a -javaagent java
|
||||
* command line argument
|
||||
@@ -149,18 +174,18 @@ public class MixinAgent implements IHotSwap{
|
||||
* @param arg Ignored
|
||||
* @param instrumentation Instance to use to transform the mixins
|
||||
*/
|
||||
public static void premain(String arg, Instrumentation instrumentation){
|
||||
public static void premain(String arg, Instrumentation instrumentation) {
|
||||
init(instrumentation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the agent
|
||||
* Initialize the java agent
|
||||
*
|
||||
* This will be called automatically if the agent is loaded after jvm
|
||||
* This will be called automatically if the java agent is loaded after jvm
|
||||
* startup
|
||||
*
|
||||
* @param arg Ignored
|
||||
* @param instrumentation Instance to use to transform the mixins
|
||||
* @param instrumentation Instance to use to re-define the mixins
|
||||
*/
|
||||
public static void agentmain(String arg, Instrumentation instrumentation) {
|
||||
init(instrumentation);
|
||||
|
||||
@@ -31,38 +31,84 @@ import org.spongepowered.asm.lib.MethodVisitor;
|
||||
import org.spongepowered.asm.lib.Opcodes;
|
||||
import org.spongepowered.asm.lib.Type;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Class loader that is used to load fake mixin classes so that they can be
|
||||
* re-defined.
|
||||
*/
|
||||
class MixinClassLoader extends ClassLoader {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger("mixin.agent");
|
||||
|
||||
private Map<Class<?>, byte[]> bytecodes = new HashMap<Class<?>, byte[]>();
|
||||
/**
|
||||
* Mapping of mixin mixin classes to their fake classes
|
||||
*/
|
||||
private Map<Class<?>, byte[]> mixins = new HashMap<Class<?>, byte[]>();
|
||||
|
||||
void addMixinClass(String name){
|
||||
logger.debug("Mixin class "+name+" added to class loader");
|
||||
/**
|
||||
* Mapping that keep track of bytecode for classes that are targeted by
|
||||
* mixins
|
||||
*/
|
||||
private Map<String, byte[]> targets = new HashMap<String, byte[]>();
|
||||
|
||||
/**
|
||||
* Add a fake mixin class
|
||||
*
|
||||
* @param name Name of the fake class
|
||||
*/
|
||||
void addMixinClass(String name) {
|
||||
logger.debug("Mixin class " + name + " added to class loader");
|
||||
try {
|
||||
byte[] bytes = materialize(name);
|
||||
Class<?> clazz = this.defineClass(name, bytes, 0, bytes.length);
|
||||
// apparently the class needs to be instantiated at least once
|
||||
// to be including in list returned by allClasses() method in jdi api
|
||||
clazz.newInstance();
|
||||
bytecodes.put(clazz, bytes);
|
||||
}catch(Throwable e){
|
||||
this.mixins.put(clazz, bytes);
|
||||
} catch (Throwable e) {
|
||||
logger.catching(e);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] getOriginalBytecode(Class<?> clazz){
|
||||
return this.bytecodes.get(clazz);
|
||||
/**
|
||||
* Registers the bytecode for a class targeted by a mixin
|
||||
*
|
||||
* @param name Name of the target clas
|
||||
* @param bytecode Bytecode of the target class
|
||||
*/
|
||||
void addTargetClass(String name, byte[] bytecode) {
|
||||
this.targets.put(name, bytecode);
|
||||
}
|
||||
|
||||
private byte[] materialize(String name) {
|
||||
/**
|
||||
* Gets the bytecode for a fake mixin class
|
||||
*
|
||||
* @param clazz Mixin class
|
||||
* @return Bytecode of the fake mixin class
|
||||
*/
|
||||
byte[] getFakeMixinBytecode(Class<?> clazz) {
|
||||
return this.mixins.get(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the original bytecode for a target class
|
||||
*
|
||||
* @param name Name of the target class
|
||||
* @return Original bytecode
|
||||
*/
|
||||
byte[] getOriginalTargetBytecode(String name) {
|
||||
return this.targets.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the simplest possible class that is instantiable
|
||||
*
|
||||
* @param name Name of the generated class
|
||||
* @return Bytecode of the generated class
|
||||
*/
|
||||
private static byte[] materialize(String name) {
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, name.replace('.', '/'), null, Type.getInternalName(Object.class), null);
|
||||
|
||||
|
||||
Reference in new issue
Block a user