From 42ed5a9b1e40536e8db1abbafd5f58f1c2dfe3fb Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Sat, 29 Dec 2018 21:42:53 +0800 Subject: [PATCH] Apply AuthlibLogInterceptor only if necessary --- .../authlibinjector/transform/AuthlibLogInterceptor.java | 9 +++++++-- .../authlibinjector/transform/ClassTransformer.java | 8 +++++--- .../authlibinjector/transform/LdcTransformUnit.java | 2 +- .../transform/SkinWhitelistTransformUnit.java | 2 +- .../yushi/authlibinjector/transform/TransformUnit.java | 2 +- .../transform/YggdrasilKeyTransformUnit.java | 2 +- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/moe/yushi/authlibinjector/transform/AuthlibLogInterceptor.java b/src/main/java/moe/yushi/authlibinjector/transform/AuthlibLogInterceptor.java index d64dfcd..2c9e3c0 100644 --- a/src/main/java/moe/yushi/authlibinjector/transform/AuthlibLogInterceptor.java +++ b/src/main/java/moe/yushi/authlibinjector/transform/AuthlibLogInterceptor.java @@ -196,18 +196,23 @@ public class AuthlibLogInterceptor implements TransformUnit { } @Override - public Optional transform(String className, ClassVisitor writer, Runnable modifiedCallback) { + public Optional transform(ClassLoader classLoader, String className, ClassVisitor writer, Runnable modifiedCallback) { if (className.startsWith("com.mojang.authlib.")) { + synchronized (interceptedClassloaders) { + if (interceptedClassloaders.contains(classLoader)) { + return Optional.empty(); + } + } return Optional.of(new ClassVisitor(ASM6, writer) { @Override public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions); if ("".equals(name)) { - modifiedCallback.run(); mv.visitLdcInsn(Type.getType("L" + className.replace('.', '/') + ";")); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false); mv.visitMethodInsn(INVOKESTATIC, AuthlibLogInterceptor.class.getName().replace('.', '/'), "onClassLoading", "(Ljava/lang/ClassLoader;)V", false); + modifiedCallback.run(); } return mv; } diff --git a/src/main/java/moe/yushi/authlibinjector/transform/ClassTransformer.java b/src/main/java/moe/yushi/authlibinjector/transform/ClassTransformer.java index 42e4cb6..c1a8e6f 100644 --- a/src/main/java/moe/yushi/authlibinjector/transform/ClassTransformer.java +++ b/src/main/java/moe/yushi/authlibinjector/transform/ClassTransformer.java @@ -34,10 +34,12 @@ public class ClassTransformer implements ClassFileTransformer { private String className; private byte[] classBuffer; private ClassWriter pooledClassWriter; + private ClassLoader classLoader; - public TransformHandle(String className, byte[] classBuffer) { + public TransformHandle(ClassLoader classLoader, String className, byte[] classBuffer) { this.className = className; this.classBuffer = classBuffer; + this.classLoader = classLoader; } public void accept(TransformUnit unit) { @@ -49,7 +51,7 @@ public class ClassTransformer implements ClassFileTransformer { pooledClassWriter = null; } - Optional optionalVisitor = unit.transform(className, writer, () -> currentModified = true); + Optional optionalVisitor = unit.transform(classLoader, className, writer, () -> currentModified = true); if (optionalVisitor.isPresent()) { currentModified = false; ClassReader reader = new ClassReader(classBuffer); @@ -96,7 +98,7 @@ public class ClassTransformer implements ClassFileTransformer { } } - TransformHandle handle = new TransformHandle(className, classfileBuffer); + TransformHandle handle = new TransformHandle(loader, className, classfileBuffer); units.forEach(handle::accept); listeners.forEach(it -> it.onClassLoading(loader, className, handle.getFinalResult(), handle.getAppliedTransformers())); diff --git a/src/main/java/moe/yushi/authlibinjector/transform/LdcTransformUnit.java b/src/main/java/moe/yushi/authlibinjector/transform/LdcTransformUnit.java index f122366..3e6cf1f 100644 --- a/src/main/java/moe/yushi/authlibinjector/transform/LdcTransformUnit.java +++ b/src/main/java/moe/yushi/authlibinjector/transform/LdcTransformUnit.java @@ -9,7 +9,7 @@ import moe.yushi.authlibinjector.util.Logging; public abstract class LdcTransformUnit implements TransformUnit { @Override - public Optional transform(String className, ClassVisitor writer, Runnable modifiedCallback) { + public Optional transform(ClassLoader classLoader, String className, ClassVisitor writer, Runnable modifiedCallback) { return Optional.of(new ClassVisitor(ASM6, writer) { @Override diff --git a/src/main/java/moe/yushi/authlibinjector/transform/SkinWhitelistTransformUnit.java b/src/main/java/moe/yushi/authlibinjector/transform/SkinWhitelistTransformUnit.java index 80822f6..54c610a 100644 --- a/src/main/java/moe/yushi/authlibinjector/transform/SkinWhitelistTransformUnit.java +++ b/src/main/java/moe/yushi/authlibinjector/transform/SkinWhitelistTransformUnit.java @@ -21,7 +21,7 @@ public class SkinWhitelistTransformUnit implements TransformUnit { } @Override - public Optional transform(String className, ClassVisitor writer, Runnable modifiedCallback) { + public Optional transform(ClassLoader classLoader, String className, ClassVisitor writer, Runnable modifiedCallback) { if ("com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService".equals(className)) { return Optional.of(new ClassVisitor(ASM6, writer) { diff --git a/src/main/java/moe/yushi/authlibinjector/transform/TransformUnit.java b/src/main/java/moe/yushi/authlibinjector/transform/TransformUnit.java index f5b0231..c3de08a 100644 --- a/src/main/java/moe/yushi/authlibinjector/transform/TransformUnit.java +++ b/src/main/java/moe/yushi/authlibinjector/transform/TransformUnit.java @@ -5,6 +5,6 @@ import org.objectweb.asm.ClassVisitor; public interface TransformUnit { - Optional transform(String className, ClassVisitor writer, Runnable modifiedCallback); + Optional transform(ClassLoader classLoader, String className, ClassVisitor writer, Runnable modifiedCallback); } diff --git a/src/main/java/moe/yushi/authlibinjector/transform/YggdrasilKeyTransformUnit.java b/src/main/java/moe/yushi/authlibinjector/transform/YggdrasilKeyTransformUnit.java index e5fbe42..f8aebf8 100644 --- a/src/main/java/moe/yushi/authlibinjector/transform/YggdrasilKeyTransformUnit.java +++ b/src/main/java/moe/yushi/authlibinjector/transform/YggdrasilKeyTransformUnit.java @@ -23,7 +23,7 @@ public class YggdrasilKeyTransformUnit implements TransformUnit { } @Override - public Optional transform(String className, ClassVisitor writer, Runnable modifiedCallback) { + public Optional transform(ClassLoader classLoader, String className, ClassVisitor writer, Runnable modifiedCallback) { if ("com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService".equals(className)) { return Optional.of(new ClassVisitor(ASM6, writer) {