[FEATURE] Новое API в JarHelper

This commit is contained in:
Gravit 2020-01-18 18:04:46 +07:00
parent 5939ed758f
commit c075697316
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 36 additions and 11 deletions

View file

@ -5,6 +5,7 @@
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JarHelper;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -30,7 +31,7 @@ private NodeUtils() {
} }
public static ClassNode forClass(Class<?> cls, int flags) { 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(); ClassNode ret = new ClassNode();
new ClassReader(IOHelper.read(in)).accept(ret, flags); new ClassReader(IOHelper.read(in)).accept(ret, flags);
return ret; return ret;

View file

@ -13,6 +13,7 @@
import pro.gravit.launchserver.binary.BuildContext; import pro.gravit.launchserver.binary.BuildContext;
import pro.gravit.launchserver.binary.LauncherConfigurator; import pro.gravit.launchserver.binary.LauncherConfigurator;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JarHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
@ -120,10 +121,10 @@ public Path process(Path inputJar) throws IOException {
Path outputJar = server.launcherBinary.nextPath("main"); Path outputJar = server.launcherBinary.nextPath("main");
try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputJar))) { try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(outputJar))) {
ClassNode cn = new ClassNode(); 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); LauncherConfigurator launcherConfigurator = new LauncherConfigurator(cn);
ClassNode cn1 = new ClassNode(); 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); ConfigGenerator secureConfigurator = new ConfigGenerator(cn1);
BuildContext context = new BuildContext(output, launcherConfigurator, this); BuildContext context = new BuildContext(output, launcherConfigurator, this);
server.buildHookManager.hook(context); server.buildHookManager.hook(context);
@ -205,7 +206,7 @@ public Path process(Path inputJar) throws IOException {
} }
// write additional classes // write additional classes
for (Map.Entry<String, byte[]> ent : server.buildHookManager.getIncludeClass().entrySet()) { for (Map.Entry<String, byte[]> 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)); output.write(server.buildHookManager.classTransform(ent.getValue(), ent.getKey(), this));
} }
// map for guard // map for guard

View file

@ -10,6 +10,7 @@
import pro.gravit.launcher.LauncherInject; import pro.gravit.launcher.LauncherInject;
import pro.gravit.launchserver.asm.InjectClassAcceptor; import pro.gravit.launchserver.asm.InjectClassAcceptor;
import pro.gravit.utils.PublicURLClassLoader; import pro.gravit.utils.PublicURLClassLoader;
import pro.gravit.utils.helper.JarHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import java.io.InputStream; import java.io.InputStream;
@ -38,16 +39,10 @@ public static class TestClass
public static void prepare() throws Exception { public static void prepare() throws Exception {
classLoader = new ASMClassLoader(ASMTransformersTest.class.getClassLoader()); 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 @Test
void testASM() throws Exception void testASM() throws Exception
{ {
ClassReader reader = new ClassReader(getClass(TestClass.class)); ClassReader reader = new ClassReader(JarHelper.getClassBytes(TestClass.class));
ClassNode node = new ClassNode(); ClassNode node = new ClassNode();
reader.accept(node, ClassReader.SKIP_DEBUG); reader.accept(node, ClassReader.SKIP_DEBUG);
node.name = "ASMTestClass"; node.name = "ASMTestClass";

View file

@ -1,6 +1,7 @@
package pro.gravit.utils.helper; package pro.gravit.utils.helper;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -74,4 +75,31 @@ public static void jarMap(Class<?> clazz, Map<String, String> map, boolean overw
Path file = IOHelper.getCodeSource(clazz); Path file = IOHelper.getCodeSource(clazz);
jarMap(file, map, overwrite); 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));
}
} }