From bff9715e7fa8ca56b08c15b8d4b7540df14b8ddf Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 29 Dec 2017 15:25:49 +0000 Subject: [PATCH] Implement nasty workaround for Spigot's changes to the PluginClassLoader (#648) --- .../luckperms/bukkit/LPBukkitPlugin.java | 9 ++ .../classloader/FallbackClassLoader.java | 47 ++++++ .../classloader/InjectedClassLoader.java | 146 ++++++++++++++++++ .../bukkit/classloader/LPClassLoader.java | 54 +++++++ .../dependencies/DependencyManager.java | 28 ++-- .../common/plugin/LuckPermsPlugin.java | 10 ++ 6 files changed, 283 insertions(+), 11 deletions(-) create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/FallbackClassLoader.java create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/InjectedClassLoader.java create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/LPClassLoader.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index f21d0afd..25ad28c8 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -31,6 +31,7 @@ import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.LuckPermsApi; import me.lucko.luckperms.api.platform.PlatformType; import me.lucko.luckperms.bukkit.calculators.BukkitCalculatorFactory; +import me.lucko.luckperms.bukkit.classloader.LPClassLoader; import me.lucko.luckperms.bukkit.contexts.BukkitContextManager; import me.lucko.luckperms.bukkit.contexts.WorldCalculator; import me.lucko.luckperms.bukkit.listeners.BukkitConnectionListener; @@ -95,6 +96,7 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.InputStream; +import java.net.URL; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; @@ -129,6 +131,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { private DefaultsProvider defaultsProvider; private ChildPermissionProvider childPermissionProvider; private LocaleManager localeManager; + private LPClassLoader lpClassLoader; private DependencyManager dependencyManager; private CachedStateManager cachedStateManager; private ContextManager contextManager; @@ -153,6 +156,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { senderFactory = new BukkitSenderFactory(this); log = new SenderLogger(this, getConsoleSender()); + lpClassLoader = LPClassLoader.obtainFor(this); dependencyManager = new DependencyManager(this); dependencyManager.loadDependencies(Collections.singleton(Dependency.CAFFEINE)); } @@ -389,6 +393,11 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { getLog().info("Goodbye!"); } + @Override + public void loadUrlIntoClasspath(URL url) { + lpClassLoader.addURL(url); + } + public void tryVaultHook(boolean force) { if (vaultHookManager != null) { return; // already hooked diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/FallbackClassLoader.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/FallbackClassLoader.java new file mode 100644 index 00000000..9c8e17e0 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/FallbackClassLoader.java @@ -0,0 +1,47 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.classloader; + +import lombok.RequiredArgsConstructor; + +import me.lucko.luckperms.common.dependencies.DependencyManager; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; + +import java.net.URL; + +/** + * A dummy implementation of {@link LPClassLoader} which just falls back to the + * reflection method used by other platforms. + */ +@RequiredArgsConstructor +public class FallbackClassLoader implements LPClassLoader { + private final LuckPermsPlugin plugin; + + @Override + public void addURL(URL url) { + DependencyManager.loadUrlIntoClassLoader(url, plugin.getClass().getClassLoader()); + } +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/InjectedClassLoader.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/InjectedClassLoader.java new file mode 100644 index 00000000..a3f0e857 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/InjectedClassLoader.java @@ -0,0 +1,146 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.classloader; + +import lombok.RequiredArgsConstructor; +import lombok.experimental.Delegate; + +import org.bukkit.plugin.java.JavaPlugin; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * A fake classloader instance which sits in-front of a PluginClassLoader, which + * attempts to load classes from it's own sources before allowing the PCL to load + * the class. + * + * This allows us to inject extra URL sources into the plugin's classloader at + * runtime. + */ +public class InjectedClassLoader extends URLClassLoader implements LPClassLoader { + + public static InjectedClassLoader inject(JavaPlugin plugin) throws Exception { + // get the plugin's PluginClassLoader instance + ClassLoader classLoader = plugin.getClass().getClassLoader(); + + // get a ref to the PCL class + Class pclClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader"); + + // extract the 'classes' cache map + Field classesField = pclClass.getDeclaredField("classes"); + classesField.setAccessible(true); + + // obtain the classes instance from the classloader + //noinspection unchecked + Map> old = (Map) classesField.get(classLoader); + + // init a new InjectedClassLoader to read from + InjectedClassLoader newLoader = new InjectedClassLoader(); + + // replace the 'classes' cache map with our own. + classesField.set(classLoader, new DelegatingClassMap(newLoader, old)); + + return newLoader; + } + + static { + try { + Method method = ClassLoader.class.getDeclaredMethod("registerAsParallelCapable"); + if (method != null) { + method.setAccessible(true); + method.invoke(null); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private final Map> cache = new ConcurrentHashMap<>(); + + private InjectedClassLoader() { + super(new URL[0]); + } + + @Override + public void addURL(URL url) { + super.addURL(url); + } + + private Class lookup(String name) { + // try the cache + Class clazz = cache.get(name); + if (clazz != null) { + return clazz; + } + + try { + // attempt to load + clazz = loadClass(name); + + // if successful, add to the cache file + cache.put(name, clazz); + return clazz; + } catch (ClassNotFoundException e) { + return null; + } + } + + /** + * A fake map instance which effectively allows us to override the behaviour + * of #findClass in PluginClassLoader. + */ + @RequiredArgsConstructor + private static final class DelegatingClassMap implements Map> { + private final InjectedClassLoader loader; + + // delegate all other calls to the original map + @Delegate(excludes = Exclude.class) + private final Map> delegate; + + // override the #get call, so we can attempt to load the class ourselves. + @Override + public Class get(Object key) { + String className = ((String) key); + + Class clazz = loader.lookup(className); + if (clazz != null) { + return clazz; + } else { + return delegate.get(className); + } + } + + private interface Exclude { + Class get(Object key); + } + } + +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/LPClassLoader.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/LPClassLoader.java new file mode 100644 index 00000000..ab985e30 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/classloader/LPClassLoader.java @@ -0,0 +1,54 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.classloader; + +import me.lucko.luckperms.bukkit.LPBukkitPlugin; + +import java.net.URL; + +/** + * An interface over a {@link org.bukkit.plugin.java.JavaPlugin}'s PluginClassLoader. + */ +public interface LPClassLoader { + + static LPClassLoader obtainFor(LPBukkitPlugin plugin) { + try { + // try to inject into the classloader + return InjectedClassLoader.inject(plugin); + } catch (Exception e) { + // fallback to a reflection based method + return new FallbackClassLoader(plugin); + } + } + + /** + * Adds a URL to the classloader + * + * @param url the url to add + */ + void addURL(URL url); + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java index 42bca978..965532e0 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java @@ -186,18 +186,12 @@ public class DependencyManager { } } - private void loadJar(File file) { + private void loadJar(File file) { // get the classloader to load into - ClassLoader classLoader = plugin.getClass().getClassLoader(); - - if (classLoader instanceof URLClassLoader) { - try { - ADD_URL_METHOD.invoke(classLoader, file.toURI().toURL()); - } catch (IllegalAccessException | InvocationTargetException | MalformedURLException e) { - throw new RuntimeException("Unable to invoke URLClassLoader#addURL", e); - } - } else { - throw new RuntimeException("Unknown classloader type: " + classLoader.getClass()); + try { + plugin.loadUrlIntoClasspath(file.toURI().toURL()); + } catch (MalformedURLException e) { + throw new RuntimeException(e); } } @@ -210,4 +204,16 @@ public class DependencyManager { } } + public static void loadUrlIntoClassLoader(URL url, ClassLoader classLoader) { + if (classLoader instanceof URLClassLoader) { + try { + ADD_URL_METHOD.invoke(classLoader, url); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException("Unable to invoke URLClassLoader#addURL", e); + } + } else { + throw new RuntimeException("Unknown classloader type: " + classLoader.getClass()); + } + } + } diff --git a/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java index d945cdd1..92db9c37 100644 --- a/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/plugin/LuckPermsPlugin.java @@ -55,6 +55,7 @@ import me.lucko.luckperms.common.verbose.VerboseHandler; import java.io.File; import java.io.InputStream; +import java.net.URL; import java.util.Collections; import java.util.List; import java.util.Map; @@ -154,6 +155,15 @@ public interface LuckPermsPlugin { */ DependencyManager getDependencyManager(); + /** + * Loads a dependency into the plugins classpath + * + * @param url the url to load + */ + default void loadUrlIntoClasspath(URL url) { + DependencyManager.loadUrlIntoClassLoader(url, getClass().getClassLoader()); + } + /** * Gets the context manager. * This object handles context accumulation for all players on the platform.