From c075697316eb4911915d851e2565688017c59562 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 18 Jan 2020 18:04:46 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=9D=D0=BE=D0=B2=D0=BE=D0=B5=20AP?= =?UTF-8?q?I=20=D0=B2=20JarHelper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gravit/launchserver/asm/NodeUtils.java | 3 +- .../binary/tasks/MainBuildTask.java | 7 +++-- .../launchserver/ASMTransformersTest.java | 9 ++---- .../pro/gravit/utils/helper/JarHelper.java | 28 +++++++++++++++++++ 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/asm/NodeUtils.java b/LaunchServer/src/main/java/pro/gravit/launchserver/asm/NodeUtils.java index 9919cde7..b85a27bd 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/asm/NodeUtils.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/asm/NodeUtils.java @@ -5,6 +5,7 @@ import org.objectweb.asm.Type; import org.objectweb.asm.tree.*; import pro.gravit.utils.helper.IOHelper; +import pro.gravit.utils.helper.JarHelper; import java.io.IOException; import java.io.InputStream; @@ -30,7 +31,7 @@ private NodeUtils() { } public static ClassNode forClass(Class cls, int flags) { - try (InputStream in = cls.getClassLoader().getResourceAsStream(cls.getName().replace('.', '/') + ".class")) { + try (InputStream in = JarHelper.getClassBytesStream(cls)) { ClassNode ret = new ClassNode(); new ClassReader(IOHelper.read(in)).accept(ret, flags); return ret; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/MainBuildTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/MainBuildTask.java index 807e6993..99190a1a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/MainBuildTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/MainBuildTask.java @@ -13,6 +13,7 @@ import pro.gravit.launchserver.binary.BuildContext; import pro.gravit.launchserver.binary.LauncherConfigurator; import pro.gravit.utils.helper.IOHelper; +import pro.gravit.utils.helper.JarHelper; import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.SecurityHelper; @@ -120,10 +121,10 @@ public Path process(Path inputJar) throws IOException { Path outputJar = server.launcherBinary.nextPath("main"); try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputJar))) { ClassNode cn = new ClassNode(); - new ClassReader(IOHelper.getResourceBytes(AutogenConfig.class.getName().replace('.', '/').concat(".class"))).accept(cn, 0); + new ClassReader(JarHelper.getClassBytes(AutogenConfig.class)).accept(cn, 0); LauncherConfigurator launcherConfigurator = new LauncherConfigurator(cn); ClassNode cn1 = new ClassNode(); - new ClassReader(IOHelper.getResourceBytes(SecureAutogenConfig.class.getName().replace('.', '/').concat(".class"))).accept(cn1, 0); + new ClassReader(JarHelper.getClassBytes(SecureAutogenConfig.class)).accept(cn1, 0); ConfigGenerator secureConfigurator = new ConfigGenerator(cn1); BuildContext context = new BuildContext(output, launcherConfigurator, this); server.buildHookManager.hook(context); @@ -205,7 +206,7 @@ public Path process(Path inputJar) throws IOException { } // write additional classes for (Map.Entry ent : server.buildHookManager.getIncludeClass().entrySet()) { - output.putNextEntry(newZipEntry(ent.getKey().replace('.', '/').concat(".class"))); + output.putNextEntry(newZipEntry(JarHelper.getClassFile(ent.getKey()))); output.write(server.buildHookManager.classTransform(ent.getValue(), ent.getKey(), this)); } // map for guard diff --git a/LaunchServer/src/test/java/pro/gravit/launchserver/ASMTransformersTest.java b/LaunchServer/src/test/java/pro/gravit/launchserver/ASMTransformersTest.java index c776109d..f9909fd8 100644 --- a/LaunchServer/src/test/java/pro/gravit/launchserver/ASMTransformersTest.java +++ b/LaunchServer/src/test/java/pro/gravit/launchserver/ASMTransformersTest.java @@ -10,6 +10,7 @@ import pro.gravit.launcher.LauncherInject; import pro.gravit.launchserver.asm.InjectClassAcceptor; import pro.gravit.utils.PublicURLClassLoader; +import pro.gravit.utils.helper.JarHelper; import pro.gravit.utils.helper.LogHelper; import java.io.InputStream; @@ -38,16 +39,10 @@ public static class TestClass public static void prepare() throws Exception { classLoader = new ASMClassLoader(ASMTransformersTest.class.getClassLoader()); } - InputStream getClass(Class clazz) - { - String className = clazz.getName(); - String classAsPath = className.replace('.', '/') + ".class"; - return clazz.getClassLoader().getResourceAsStream(classAsPath); - } @Test void testASM() throws Exception { - ClassReader reader = new ClassReader(getClass(TestClass.class)); + ClassReader reader = new ClassReader(JarHelper.getClassBytes(TestClass.class)); ClassNode node = new ClassNode(); reader.accept(node, ClassReader.SKIP_DEBUG); node.name = "ASMTestClass"; diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/JarHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/JarHelper.java index ece54976..62500774 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/JarHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/JarHelper.java @@ -1,6 +1,7 @@ package pro.gravit.utils.helper; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Path; import java.util.HashMap; import java.util.Map; @@ -74,4 +75,31 @@ public static void jarMap(Class clazz, Map map, boolean overw Path file = IOHelper.getCodeSource(clazz); jarMap(file, map, overwrite); } + + public static String getClassFile(Class clazz) + { + return getClassFile(clazz.getName()); + } + public static String getClassFile(String classname) + { + return classname.replace('.', '/').concat(".class"); + } + public static byte[] getClassBytes(Class clazz) throws IOException + { + return getClassBytes(clazz, clazz.getClassLoader()); + } + public static byte[] getClassBytes(Class clazz, ClassLoader classLoader) throws IOException + { + return IOHelper.read(classLoader.getResourceAsStream(getClassFile(clazz))); + } + public static InputStream getClassBytesStream(Class clazz) throws IOException + { + return getClassBytesStream(clazz, clazz.getClassLoader()); + } + public static InputStream getClassBytesStream(Class clazz, ClassLoader classLoader) throws IOException + { + return classLoader.getResourceAsStream(getClassFile(clazz)); + } + + }