Try check for preloaded classes

This commit is contained in:
libraryaddict 2020-02-21 13:53:08 +13:00
parent 434954a281
commit cf55470fad
No known key found for this signature in database
GPG key ID: 052E4FBCD257AEA4
4 changed files with 54 additions and 8 deletions

View file

@ -5,7 +5,7 @@
<!-- A good example on why temporary names for project identification shouldn't be used --> <!-- A good example on why temporary names for project identification shouldn't be used -->
<groupId>LibsDisguises</groupId> <groupId>LibsDisguises</groupId>
<artifactId>LibsDisguises</artifactId> <artifactId>LibsDisguises</artifactId>
<version>9.9.7</version> <version>9.9.7-SNAPSHOT</version>
<build> <build>
<defaultGoal>exec:java clean install</defaultGoal> <defaultGoal>exec:java clean install</defaultGoal>

View file

@ -30,13 +30,14 @@ public class LibsDisguises extends JavaPlugin {
@Override @Override
public void onLoad() { public void onLoad() {
instance = this;
if (Bukkit.getServer().getWorlds().isEmpty()) { if (Bukkit.getServer().getWorlds().isEmpty()) {
WatcherSanitizer.init();
return; return;
} }
instance = this;
WatcherSanitizer.init();
reloaded = true; reloaded = true;
getLogger().severe("Lib's Disguises was reloaded! Please do not report any bugs! This plugin can't handle " + getLogger().severe("Lib's Disguises was reloaded! Please do not report any bugs! This plugin can't handle " +
"reloads gracefully!"); "reloads gracefully!");

View file

@ -42,10 +42,8 @@ public class Asm13 implements IAsm {
Field field = loader.getClass().getDeclaredField("classes"); Field field = loader.getClass().getDeclaredField("classes");
field.setAccessible(true); field.setAccessible(true);
Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader); Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader);
Class newClass = Class newClass = (Class<?>) getDefineClassMethod()
.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
(Class<?>) getDefineClassMethod()
.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
map.put(className, newClass); map.put(className, newClass);
return newClass; return newClass;

View file

@ -1,23 +1,70 @@
package me.libraryaddict.disguise.utilities.reflection.asm; package me.libraryaddict.disguise.utilities.reflection.asm;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion; 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.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* Created by libraryaddict on 17/02/2020. * Created by libraryaddict on 17/02/2020.
*/ */
public class WatcherSanitizer { 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<String, Class<?>> classes = (Map<String, Class<?>>) 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<String, Class<?>> lClasses = (Map<String, Class<?>>) 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() { public static void init() {
try {
checkPreLoaded();
}
catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
IAsm asm; IAsm asm;
if (NmsVersion.v1_14.isSupported()) { if (NmsVersion.v1_14.isSupported()) {