From cf55470fad38b5b8cc4cc5a1249ed05a2b6df1fc Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 21 Feb 2020 13:53:08 +1300 Subject: [PATCH] Try check for preloaded classes --- pom.xml | 2 +- .../libraryaddict/disguise/LibsDisguises.java | 7 +-- .../utilities/reflection/asm/Asm13.java | 6 +-- .../reflection/asm/WatcherSanitizer.java | 47 +++++++++++++++++++ 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 1ec61068..b9300acc 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ LibsDisguises LibsDisguises - 9.9.7 + 9.9.7-SNAPSHOT exec:java clean install diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 0b581c15..2b4a29df 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -30,13 +30,14 @@ public class LibsDisguises extends JavaPlugin { @Override public void onLoad() { - instance = this; - if (Bukkit.getServer().getWorlds().isEmpty()) { - WatcherSanitizer.init(); return; } + instance = this; + + WatcherSanitizer.init(); + reloaded = true; getLogger().severe("Lib's Disguises was reloaded! Please do not report any bugs! This plugin can't handle " + "reloads gracefully!"); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/Asm13.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/Asm13.java index beeb8e35..cda5f778 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/Asm13.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/Asm13.java @@ -42,10 +42,8 @@ public class Asm13 implements IAsm { Field field = loader.getClass().getDeclaredField("classes"); field.setAccessible(true); Map> map = (Map>) field.get(loader); - Class newClass = - - (Class) getDefineClassMethod() - .invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length); + Class newClass = (Class) getDefineClassMethod() + .invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length); map.put(className, newClass); return newClass; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java index 4d76d569..16d48490 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/asm/WatcherSanitizer.java @@ -1,23 +1,70 @@ package me.libraryaddict.disguise.utilities.reflection.asm; import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.reflection.NmsVersion; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.java.JavaPluginLoader; +import sun.plugin.security.PluginClassLoader; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; /** * Created by libraryaddict on 17/02/2020. */ public class WatcherSanitizer { + public static void checkPreLoaded() throws NoSuchFieldException, IllegalAccessException { + JavaPluginLoader javaLoader = (JavaPluginLoader) LibsDisguises.getInstance().getPluginLoader(); + + Field cM = JavaPluginLoader.class.getDeclaredField("classes"); + cM.setAccessible(true); + Map> classes = (Map>) cM.get(javaLoader); + Field lM = JavaPluginLoader.class.getDeclaredField("loaders"); + lM.setAccessible(true); + List loaders = (List) lM.get(javaLoader); + + Field lF = WatcherSanitizer.class.getClassLoader().getClass().getDeclaredField("classes"); + lF.setAccessible(true); + Field dF = WatcherSanitizer.class.getClassLoader().getClass().getDeclaredField("description"); + dF.setAccessible(true); + + for (Object loader : loaders) { + Map> lClasses = (Map>) lF.get(loader); + + for (Class c : lClasses.values()) { + if (!c.getName().startsWith("me.libraryaddict.disguise.disguisetypes.watchers.") && + !c.getName().equals("me.libraryaddict.disguise.disguisetypes.FlagWatcher")) { + continue; + } + + PluginDescriptionFile desc = (PluginDescriptionFile) dF.get(loader); + + DisguiseUtilities.getLogger().severe(desc.getFullName() + + " has been a naughty plugin, they're declaring access to the disguise watchers before Lib's " + + "Disguises can properly load them! They should add 'LibsDisguises' to the 'depend' section of" + + " their plugin.yml!"); + break; + } + } + } public static void init() { + try { + checkPreLoaded(); + } + catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + IAsm asm; if (NmsVersion.v1_14.isSupported()) {