From 52213d7c553f493b9ad1d66e60e0f6ad3af87c1f Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Mon, 17 Dec 2018 19:01:41 +0300 Subject: [PATCH 1/2] Applied gradle fix. --- LaunchServer/build.gradle | 11 +++-- Launcher/build.gradle | 3 +- libLauncher/build.gradle | 1 - .../ru/gravit/launcher/LauncherAgent.java | 21 -------- .../launcher/NeedGarbageCollection.java | 1 - .../SystemClassLoaderTransformer.java | 49 ------------------- 6 files changed, 7 insertions(+), 79 deletions(-) delete mode 100644 libLauncher/src/main/java/ru/gravit/launcher/transformers/SystemClassLoaderTransformer.java diff --git a/LaunchServer/build.gradle b/LaunchServer/build.gradle index 9d6df28f..1e52008a 100644 --- a/LaunchServer/build.gradle +++ b/LaunchServer/build.gradle @@ -17,13 +17,14 @@ bundleOnly bundle hikari - bundle.extendsFrom bundleOnly - compileOnly.extendsFrom bundle, hikari + pack + bundleOnly.extendsFrom bundle + compile.extendsFrom bundle, hikari, pack } jar { dependsOn parent.childProjects.Launcher.tasks.build, parent.childProjects.Launcher.tasks.genRuntimeJS, parent.childProjects.Launcher.tasks.jar - from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } + from { configurations.pack.collect { it.isDirectory() ? it : zipTree(it) } } from(parent.childProjects.Launcher.tasks.jar.archivePath, parent.childProjects.Launcher.tasks.genRuntimeJS.archivePath) manifest.attributes("Main-Class": mainClassName, "Premain-Class": mainAgentName, @@ -34,7 +35,7 @@ } dependencies { - compile project(':libLauncher') // pack + pack project(':libLauncher') // pack bundle 'org.ow2.asm:asm-commons:7.0' bundle 'org.ow2.asm:asm-util:7.0' bundle 'mysql:mysql-connector-java:8.0.12' @@ -70,7 +71,7 @@ task hikari(type: Copy) { task dumpLibs(type: Copy) { dependsOn tasks.hikari into "$buildDir/libs/libraries" - from configurations.bundle + from configurations.bundleOnly } build.dependsOn tasks.dumpLibs diff --git a/Launcher/build.gradle b/Launcher/build.gradle index 7521000f..b41df9a2 100644 --- a/Launcher/build.gradle +++ b/Launcher/build.gradle @@ -22,8 +22,7 @@ dependencies { compile project(':LauncherAPI') - compile 'org.javassist:javassist:3.23.1-GA' - compile group: 'com.github.oshi', name: 'oshi-core', version: '3.11.0' + compile 'com.github.oshi:oshi-core:3.11.0' } task genRuntimeJS(type: Zip) { diff --git a/libLauncher/build.gradle b/libLauncher/build.gradle index 41b686dd..2d77906e 100644 --- a/libLauncher/build.gradle +++ b/libLauncher/build.gradle @@ -3,6 +3,5 @@ dependencies { compileOnly 'org.fusesource.jansi:jansi:1.17.1' - compileOnly 'org.javassist:javassist:3.23.1-GA' compile 'com.google.code.gson:gson:2.8.5' } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/LauncherAgent.java b/libLauncher/src/main/java/ru/gravit/launcher/LauncherAgent.java index ba4b128b..6ee80895 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/LauncherAgent.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/LauncherAgent.java @@ -2,17 +2,12 @@ import java.io.IOException; import java.lang.instrument.Instrumentation; -import java.lang.instrument.UnmodifiableClassException; -import java.util.ArrayList; import java.util.jar.JarFile; -import javassist.bytecode.ClassFile; -import ru.gravit.launcher.transformers.SystemClassLoaderTransformer; import ru.gravit.utils.helper.LogHelper; @LauncherAPI public class LauncherAgent { - private static final boolean enabled = false; private static boolean isAgentStarted = false; public static Instrumentation inst; @@ -33,22 +28,6 @@ public static void premain(String agentArgument, Instrumentation instrumentation System.out.println("Launcher Agent"); inst = instrumentation; isAgentStarted = true; - - if (ClassFile.MAJOR_VERSION > ClassFile.JAVA_8 || enabled) { - inst.addTransformer(new SystemClassLoaderTransformer()); - Class[] classes = inst.getAllLoadedClasses(); // Получаем список уже загруженных классов, которые могут быть изменены. Классы, которые ещё не загружены, будут изменены при загрузке - ArrayList> classList = new ArrayList<>(); - for (Class classe : classes) - if (inst.isModifiableClass(classe)) - classList.add(classe); - // Reload classes, if possible. - Class[] workaround = new Class[classList.size()]; - try { - inst.retransformClasses(classList.toArray(workaround)); // Запускаем процесс трансформации - } catch (UnmodifiableClassException e) { - System.err.println("MainClass was unable to retransform early loaded classes: " + e); - } - } } public static boolean isStarted() { diff --git a/libLauncher/src/main/java/ru/gravit/launcher/NeedGarbageCollection.java b/libLauncher/src/main/java/ru/gravit/launcher/NeedGarbageCollection.java index a2b5f567..04c69b14 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/NeedGarbageCollection.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/NeedGarbageCollection.java @@ -1,6 +1,5 @@ package ru.gravit.launcher; - @FunctionalInterface public interface NeedGarbageCollection { void garbageCollection(); diff --git a/libLauncher/src/main/java/ru/gravit/launcher/transformers/SystemClassLoaderTransformer.java b/libLauncher/src/main/java/ru/gravit/launcher/transformers/SystemClassLoaderTransformer.java deleted file mode 100644 index ec76f96e..00000000 --- a/libLauncher/src/main/java/ru/gravit/launcher/transformers/SystemClassLoaderTransformer.java +++ /dev/null @@ -1,49 +0,0 @@ -package ru.gravit.launcher.transformers; - -import java.io.ByteArrayInputStream; -import java.lang.instrument.ClassFileTransformer; -import java.security.ProtectionDomain; - -import javassist.ClassPool; -import javassist.CodeConverter; -import javassist.CtClass; -import javassist.CtConstructor; -import javassist.CtMethod; -import javassist.LoaderClassPath; -import ru.gravit.utils.PublicURLClassLoader; - -public class SystemClassLoaderTransformer implements ClassFileTransformer { - @Override - public byte[] transform(ClassLoader classLoader, String classname, Class aClass, ProtectionDomain protectionDomain, byte[] bytes) { - if (classname.startsWith("ru/gravit/launcher/")) return bytes; - if (classname.startsWith("java/")) return bytes; - if (classname.startsWith("sun/")) return bytes; - if (classname.startsWith("com/sun/")) return bytes; - if (classname.startsWith("javax/")) return bytes; - if (classname.startsWith("jdk/")) return bytes; - try { - ClassPool pool = ClassPool.getDefault(); - pool.appendClassPath(new LoaderClassPath(PublicURLClassLoader.systemclassloader)); - pool.appendClassPath(new LoaderClassPath(classLoader)); - CtClass s1 = pool.get("java.lang.ClassLoader"); - CtMethod m11 = s1.getDeclaredMethod("getSystemClassLoader"); // Находим метод, который нам нужно заменить - CtClass s2 = pool.get(PublicURLClassLoader.class.getName()); - CtMethod m21 = s2.getDeclaredMethod("getSystemClassLoader"); // Находим метод, на который мы будем заменять - CodeConverter cc = new CodeConverter(); - cc.redirectMethodCall(m11, m21); // Указываем что на что нам нужно заменить - - CtClass cl = pool.makeClass(new ByteArrayInputStream(bytes), false); // Загружаем класс, переданный для трансформации - if (cl.isFrozen()) return null; - CtConstructor[] constructors = cl.getConstructors(); // Находим все конструкторы класса - for (CtConstructor constructor : constructors) - constructor.instrument(cc); // Заменяем вызовы - CtMethod[] methods = cl.getDeclaredMethods(); // Находим все методы класса - for (CtMethod method : methods) - method.instrument(cc); // Заменяем вызовы - return cl.toBytecode(); - } catch (Exception ignored) { - - } - return bytes; - } -} From 1aba7624a1cf002c0cbb4a19ef59fe8f986028e0 Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Mon, 17 Dec 2018 19:22:04 +0300 Subject: [PATCH 2/2] Commands fixes. --- .../command/hash/DownloadClientCommand.java | 1 + .../command/modules/LoadModuleCommand.java | 2 +- .../launcher/modules/SimpleModuleManager.java | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java index ec7376aa..2ddd708e 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/DownloadClientCommand.java @@ -59,6 +59,7 @@ public void invoke(String... args) throws IOException, CommandException { } client.setTitle(dirName); client.block.getEntry("dir", StringConfigEntry.class).setValue(dirName); + client.block.getEntry("title", StringConfigEntry.class).setValue(dirName); try (BufferedWriter writer = IOHelper.newWriter(IOHelper.resolveIncremental(server.profilesDir, dirName, "cfg"))) { TextConfigWriter.write(client.block, writer, true); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/modules/LoadModuleCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/modules/LoadModuleCommand.java index e4f83eeb..1ab8a23a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/modules/LoadModuleCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/modules/LoadModuleCommand.java @@ -25,6 +25,6 @@ public String getUsageDescription() { public void invoke(String... args) throws Exception { verifyArgs(args, 1); URI uri = Paths.get(args[0]).toUri(); - server.modulesManager.loadModule(uri.toURL(), false); + server.modulesManager.loadModuleFull(uri.toURL()); } } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/modules/SimpleModuleManager.java b/libLauncher/src/main/java/ru/gravit/launcher/modules/SimpleModuleManager.java index a66faa36..afbef9c2 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/modules/SimpleModuleManager.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/modules/SimpleModuleManager.java @@ -105,6 +105,21 @@ public void loadModule(URL jarpath, boolean preload) throws ClassNotFoundExcepti f.close(); } + public void loadModuleFull(URL jarpath) throws ClassNotFoundException, IllegalAccessException, InstantiationException, URISyntaxException, IOException { + JarFile f = new JarFile(Paths.get(jarpath.toURI()).toString()); + Manifest m = f.getManifest(); + String mainclass = m.getMainAttributes().getValue("Main-Class"); + classloader.addURL(jarpath); + Class moduleclass = Class.forName(mainclass, true, classloader); + Module module = (Module) moduleclass.newInstance(); + modules.add(module); + module.preInit(context); + module.init(context); + module.postInit(context); + LogHelper.info("Module %s version: %s loaded", module.getName(), module.getVersion()); + f.close(); + } + @Override public void loadModule(URL jarpath, String classname, boolean preload) throws ClassNotFoundException, IllegalAccessException, InstantiationException {