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 -->
<groupId>LibsDisguises</groupId>
<artifactId>LibsDisguises</artifactId>
<version>9.9.7</version>
<version>9.9.7-SNAPSHOT</version>
<build>
<defaultGoal>exec:java clean install</defaultGoal>

View File

@ -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!");

View File

@ -42,10 +42,8 @@ public class Asm13 implements IAsm {
Field field = loader.getClass().getDeclaredField("classes");
field.setAccessible(true);
Map<String, Class<?>> map = (Map<String, Class<?>>) 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;

View File

@ -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<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() {
try {
checkPreLoaded();
}
catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
IAsm asm;
if (NmsVersion.v1_14.isSupported()) {