From 1bff55024f135f36491f2536b9a22d3a201e6e79 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Sun, 23 Apr 2023 22:36:56 +0200 Subject: [PATCH] Updated ClassTransform usage --- build.gradle | 6 +++--- .../java/net/raphimc/viaproxy/ViaProxy.java | 2 +- .../viaproxy/injection/Java17ToJava8.java | 17 ++++++++++------- .../raphimc/viaproxy/plugins/PluginManager.java | 5 ++--- .../viaproxy/plugins/ViaProxyPlugin.java | 3 +-- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index 0931245..f79f8da 100644 --- a/build.gradle +++ b/build.gradle @@ -101,9 +101,9 @@ dependencies { include "org.apache.logging.log4j:log4j-core:2.20.0" include "org.apache.logging.log4j:log4j-slf4j-impl:2.20.0" include "com/mojang:authlib:3.16.29" - include "net.lenni0451.classtransform:mixinstranslator:1.8.4" - include "net.lenni0451.classtransform:mixinsdummy:1.8.4" - include "net.lenni0451.classtransform:additionalclassprovider:1.8.4" + include "net.lenni0451.classtransform:mixinstranslator:1.9.0-SNAPSHOT" + include "net.lenni0451.classtransform:mixinsdummy:1.9.0-SNAPSHOT" + include "net.lenni0451.classtransform:additionalclassprovider:1.9.0-SNAPSHOT" include "net.lenni0451:Reflect:1.1.0" include "net.lenni0451:LambdaEvents:2.0.3" include "net.raphimc.netminecraft:all:2.3.3" diff --git a/src/main/java/net/raphimc/viaproxy/ViaProxy.java b/src/main/java/net/raphimc/viaproxy/ViaProxy.java index 6cf3d70..11ba555 100644 --- a/src/main/java/net/raphimc/viaproxy/ViaProxy.java +++ b/src/main/java/net/raphimc/viaproxy/ViaProxy.java @@ -73,7 +73,7 @@ public class ViaProxy { final IClassProvider classProvider = new GuavaClassPathProvider(); final TransformerManager transformerManager = new TransformerManager(classProvider); 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.mixins.**"); if (instrumentation != null) { diff --git a/src/main/java/net/raphimc/viaproxy/injection/Java17ToJava8.java b/src/main/java/net/raphimc/viaproxy/injection/Java17ToJava8.java index f59293b..ab1d827 100644 --- a/src/main/java/net/raphimc/viaproxy/injection/Java17ToJava8.java +++ b/src/main/java/net/raphimc/viaproxy/injection/Java17ToJava8.java @@ -17,9 +17,9 @@ */ package net.raphimc.viaproxy.injection; +import net.lenni0451.classtransform.TransformerManager; import net.lenni0451.classtransform.transformer.IBytecodeTransformer; import net.lenni0451.classtransform.utils.ASMUtils; -import net.lenni0451.classtransform.utils.tree.IClassProvider; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; 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 BSM_ARG_CONSTANT = '\u0002'; - private final IClassProvider classProvider; + final TransformerManager transformerManager; private final int nativeClassVersion; private final List whitelistedPackages = new ArrayList<>(); - public Java17ToJava8(final IClassProvider classProvider) { - this.classProvider = classProvider; + public Java17ToJava8(final TransformerManager transformerManager) { + this.transformerManager = transformerManager; final String classVersion = System.getProperty("java.class.version"); final String[] versions = classVersion.split("\\."); @@ -53,7 +53,7 @@ public class Java17ToJava8 implements IBytecodeTransformer { } @Override - public byte[] transform(String className, byte[] bytecode) { + public byte[] transform(String className, byte[] bytecode, boolean calculateStackMapFrames) { for (String whitelistedPackage : this.whitelistedPackages) { if (!className.startsWith(whitelistedPackage)) return null; } @@ -71,7 +71,11 @@ public class Java17ToJava8 implements IBytecodeTransformer { this.convertMiscMethods(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) { @@ -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;")); } - if (list.size() != 0) { method.instructions.insertBefore(insn, list); method.instructions.remove(insn); diff --git a/src/main/java/net/raphimc/viaproxy/plugins/PluginManager.java b/src/main/java/net/raphimc/viaproxy/plugins/PluginManager.java index 571d234..42cab5f 100644 --- a/src/main/java/net/raphimc/viaproxy/plugins/PluginManager.java +++ b/src/main/java/net/raphimc/viaproxy/plugins/PluginManager.java @@ -72,8 +72,7 @@ public class PluginManager { private static void loadAndScanJar(final File file) throws Throwable { URL url = file.toURI().toURL(); - URLClassProvider classProvider = new URLClassProvider(ROOT_CLASS_PROVIDER, url); - TransformerManager transformerManager = new TransformerManager(classProvider); + TransformerManager transformerManager = new TransformerManager(new URLClassProvider(ROOT_CLASS_PROVIDER, url)); InjectionClassLoader loader = new InjectionClassLoader(transformerManager, PluginManager.class.getClassLoader(), url); InputStream viaproxyYml = loader.getResourceAsStream("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; PLUGINS.add(plugin); - plugin.registerTransformers(transformerManager, classProvider); + plugin.registerTransformers(transformerManager); plugin.onEnable(); Logger.LOGGER.info("Successfully loaded plugin '" + yaml.get("name") + "' by " + yaml.get("author") + " (v" + yaml.get("version") + ")"); } diff --git a/src/main/java/net/raphimc/viaproxy/plugins/ViaProxyPlugin.java b/src/main/java/net/raphimc/viaproxy/plugins/ViaProxyPlugin.java index e6b8b1e..96d26c7 100644 --- a/src/main/java/net/raphimc/viaproxy/plugins/ViaProxyPlugin.java +++ b/src/main/java/net/raphimc/viaproxy/plugins/ViaProxyPlugin.java @@ -18,13 +18,12 @@ package net.raphimc.viaproxy.plugins; import net.lenni0451.classtransform.TransformerManager; -import net.lenni0451.classtransform.utils.tree.IClassProvider; public abstract class ViaProxyPlugin { public abstract void onEnable(); - public void registerTransformers(final TransformerManager transformerManager, final IClassProvider classProvider) { + public void registerTransformers(final TransformerManager transformerManager) { } }