Updated ClassTransform usage

This commit is contained in:
RaphiMC 2023-04-23 22:36:56 +02:00
parent bf931dbae8
commit 1bff55024f
5 changed files with 17 additions and 16 deletions

View File

@ -101,9 +101,9 @@ dependencies {
include "org.apache.logging.log4j:log4j-core:2.20.0" include "org.apache.logging.log4j:log4j-core:2.20.0"
include "org.apache.logging.log4j:log4j-slf4j-impl:2.20.0" include "org.apache.logging.log4j:log4j-slf4j-impl:2.20.0"
include "com/mojang:authlib:3.16.29" include "com/mojang:authlib:3.16.29"
include "net.lenni0451.classtransform:mixinstranslator:1.8.4" include "net.lenni0451.classtransform:mixinstranslator:1.9.0-SNAPSHOT"
include "net.lenni0451.classtransform:mixinsdummy:1.8.4" include "net.lenni0451.classtransform:mixinsdummy:1.9.0-SNAPSHOT"
include "net.lenni0451.classtransform:additionalclassprovider:1.8.4" include "net.lenni0451.classtransform:additionalclassprovider:1.9.0-SNAPSHOT"
include "net.lenni0451:Reflect:1.1.0" include "net.lenni0451:Reflect:1.1.0"
include "net.lenni0451:LambdaEvents:2.0.3" include "net.lenni0451:LambdaEvents:2.0.3"
include "net.raphimc.netminecraft:all:2.3.3" include "net.raphimc.netminecraft:all:2.3.3"

View File

@ -73,7 +73,7 @@ public class ViaProxy {
final IClassProvider classProvider = new GuavaClassPathProvider(); final IClassProvider classProvider = new GuavaClassPathProvider();
final TransformerManager transformerManager = new TransformerManager(classProvider); final TransformerManager transformerManager = new TransformerManager(classProvider);
transformerManager.addTransformerPreprocessor(new MixinsTranslator()); transformerManager.addTransformerPreprocessor(new MixinsTranslator());
transformerManager.addBytecodeTransformer(new Java17ToJava8(classProvider).addWhitelistedPackage("com.mojang")); transformerManager.addBytecodeTransformer(new Java17ToJava8(transformerManager).addWhitelistedPackage("com.mojang"));
transformerManager.addTransformer("net.raphimc.viaproxy.injection.transformer.**"); transformerManager.addTransformer("net.raphimc.viaproxy.injection.transformer.**");
transformerManager.addTransformer("net.raphimc.viaproxy.injection.mixins.**"); transformerManager.addTransformer("net.raphimc.viaproxy.injection.mixins.**");
if (instrumentation != null) { if (instrumentation != null) {

View File

@ -17,9 +17,9 @@
*/ */
package net.raphimc.viaproxy.injection; package net.raphimc.viaproxy.injection;
import net.lenni0451.classtransform.TransformerManager;
import net.lenni0451.classtransform.transformer.IBytecodeTransformer; import net.lenni0451.classtransform.transformer.IBytecodeTransformer;
import net.lenni0451.classtransform.utils.ASMUtils; import net.lenni0451.classtransform.utils.ASMUtils;
import net.lenni0451.classtransform.utils.tree.IClassProvider;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
@ -33,12 +33,12 @@ public class Java17ToJava8 implements IBytecodeTransformer {
private static final char STACK_ARG_CONSTANT = '\u0001'; private static final char STACK_ARG_CONSTANT = '\u0001';
private static final char BSM_ARG_CONSTANT = '\u0002'; private static final char BSM_ARG_CONSTANT = '\u0002';
private final IClassProvider classProvider; final TransformerManager transformerManager;
private final int nativeClassVersion; private final int nativeClassVersion;
private final List<String> whitelistedPackages = new ArrayList<>(); private final List<String> whitelistedPackages = new ArrayList<>();
public Java17ToJava8(final IClassProvider classProvider) { public Java17ToJava8(final TransformerManager transformerManager) {
this.classProvider = classProvider; this.transformerManager = transformerManager;
final String classVersion = System.getProperty("java.class.version"); final String classVersion = System.getProperty("java.class.version");
final String[] versions = classVersion.split("\\."); final String[] versions = classVersion.split("\\.");
@ -53,7 +53,7 @@ public class Java17ToJava8 implements IBytecodeTransformer {
} }
@Override @Override
public byte[] transform(String className, byte[] bytecode) { public byte[] transform(String className, byte[] bytecode, boolean calculateStackMapFrames) {
for (String whitelistedPackage : this.whitelistedPackages) { for (String whitelistedPackage : this.whitelistedPackages) {
if (!className.startsWith(whitelistedPackage)) return null; if (!className.startsWith(whitelistedPackage)) return null;
} }
@ -71,7 +71,11 @@ public class Java17ToJava8 implements IBytecodeTransformer {
this.convertMiscMethods(classNode); this.convertMiscMethods(classNode);
this.removeRecords(classNode); this.removeRecords(classNode);
return ASMUtils.toBytes(classNode, this.classProvider); if (calculateStackMapFrames) {
return ASMUtils.toBytes(classNode, this.transformerManager.getClassTree(), this.transformerManager.getClassProvider());
} else {
return ASMUtils.toStacklessBytes(classNode);
}
} }
private void makePublic(final ClassNode classNode) { private void makePublic(final ClassNode classNode) {
@ -195,7 +199,6 @@ public class Java17ToJava8 implements IBytecodeTransformer {
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", "unmodifiableSet", "(Ljava/util/Set;)Ljava/util/Set;")); list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", "unmodifiableSet", "(Ljava/util/Set;)Ljava/util/Set;"));
} }
if (list.size() != 0) { if (list.size() != 0) {
method.instructions.insertBefore(insn, list); method.instructions.insertBefore(insn, list);
method.instructions.remove(insn); method.instructions.remove(insn);

View File

@ -72,8 +72,7 @@ public class PluginManager {
private static void loadAndScanJar(final File file) throws Throwable { private static void loadAndScanJar(final File file) throws Throwable {
URL url = file.toURI().toURL(); URL url = file.toURI().toURL();
URLClassProvider classProvider = new URLClassProvider(ROOT_CLASS_PROVIDER, url); TransformerManager transformerManager = new TransformerManager(new URLClassProvider(ROOT_CLASS_PROVIDER, url));
TransformerManager transformerManager = new TransformerManager(classProvider);
InjectionClassLoader loader = new InjectionClassLoader(transformerManager, PluginManager.class.getClassLoader(), url); InjectionClassLoader loader = new InjectionClassLoader(transformerManager, PluginManager.class.getClassLoader(), url);
InputStream viaproxyYml = loader.getResourceAsStream("viaproxy.yml"); InputStream viaproxyYml = loader.getResourceAsStream("viaproxy.yml");
if (viaproxyYml == null) throw new IllegalStateException("Plugin '" + file.getName() + "' does not have a viaproxy.yml"); if (viaproxyYml == null) throw new IllegalStateException("Plugin '" + file.getName() + "' does not have a viaproxy.yml");
@ -97,7 +96,7 @@ public class PluginManager {
ViaProxyPlugin plugin = (ViaProxyPlugin) instance; ViaProxyPlugin plugin = (ViaProxyPlugin) instance;
PLUGINS.add(plugin); PLUGINS.add(plugin);
plugin.registerTransformers(transformerManager, classProvider); plugin.registerTransformers(transformerManager);
plugin.onEnable(); plugin.onEnable();
Logger.LOGGER.info("Successfully loaded plugin '" + yaml.get("name") + "' by " + yaml.get("author") + " (v" + yaml.get("version") + ")"); Logger.LOGGER.info("Successfully loaded plugin '" + yaml.get("name") + "' by " + yaml.get("author") + " (v" + yaml.get("version") + ")");
} }

View File

@ -18,13 +18,12 @@
package net.raphimc.viaproxy.plugins; package net.raphimc.viaproxy.plugins;
import net.lenni0451.classtransform.TransformerManager; import net.lenni0451.classtransform.TransformerManager;
import net.lenni0451.classtransform.utils.tree.IClassProvider;
public abstract class ViaProxyPlugin { public abstract class ViaProxyPlugin {
public abstract void onEnable(); public abstract void onEnable();
public void registerTransformers(final TransformerManager transformerManager, final IClassProvider classProvider) { public void registerTransformers(final TransformerManager transformerManager) {
} }
} }