mirror of
https://github.com/funkemunky/KauriV3.git
synced 2026-07-02 10:28:26 +00:00
New Speed check and lots of new stuff implemented
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package dev.brighten.ac.utils;
|
||||
|
||||
import dev.brighten.ac.Anticheat;
|
||||
import dev.brighten.ac.utils.reflections.Reflections;
|
||||
import dev.brighten.ac.utils.reflections.types.WrappedClass;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
@@ -8,6 +11,7 @@ import org.objectweb.asm.tree.ClassNode;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -18,6 +22,7 @@ import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
@@ -39,6 +44,125 @@ public class ClassScanner {
|
||||
return scanFile(file, urls);
|
||||
}
|
||||
|
||||
public static Set<WrappedClass> getClasses(Class<? extends Annotation> annotationClass,
|
||||
String packageName) {
|
||||
return scanFile(annotationClass).stream().filter(pkg -> pkg.startsWith(packageName))
|
||||
.map(Reflections::getClass).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public static Set<WrappedClass> getClasses(Class<? extends Annotation> annotationClass) {
|
||||
return scanFile(annotationClass).stream().map(Reflections::getClass).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public static Set<String> scanFile(Class<? extends Annotation> annotationClass) {
|
||||
return scanFile(annotationClass, new URL[]{Anticheat.class.getProtectionDomain().getCodeSource().getLocation()});
|
||||
}
|
||||
|
||||
public static Set<String> scanFile(Class<? extends Annotation> annotationClass, URL[] urls) {
|
||||
Set<URI> sources = new HashSet<>();
|
||||
Set<String> plugins = new HashSet<>();
|
||||
|
||||
|
||||
for (URL url : urls) {
|
||||
if (!url.getProtocol().equals("file")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
URI source;
|
||||
try {
|
||||
source = url.toURI();
|
||||
} catch (URISyntaxException e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sources.add(source)) {
|
||||
scanPath(Paths.get(source), annotationClass, plugins);
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
private static void scanPath(Path path, Class<? extends Annotation> annotationClass, Set<String> plugins) {
|
||||
if (Files.exists(path)) {
|
||||
if (Files.isDirectory(path)) {
|
||||
scanDirectory(path, annotationClass, plugins);
|
||||
} else {
|
||||
scanZip(path, annotationClass, plugins);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void scanDirectory(Path dir, Class<? extends Annotation> annotationClass, final Set<String> plugins) {
|
||||
try {
|
||||
Files.walkFileTree(dir, newHashSet(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
|
||||
new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
|
||||
if (CLASS_FILE.matches(path.getFileName())) {
|
||||
try (InputStream in = Files.newInputStream(path)) {
|
||||
String plugin = findClass(in, annotationClass);
|
||||
if (plugin != null) {
|
||||
plugins.add(plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String findClass(InputStream in, Class<? extends Annotation> annotationClass) {
|
||||
try {
|
||||
ClassReader reader = new ClassReader(in);
|
||||
ClassNode classNode = new ClassNode();
|
||||
reader.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
|
||||
String className = classNode.name.replace('/', '.');
|
||||
final String anName = annotationClass.getName().replace(".", "/");
|
||||
if (classNode.visibleAnnotations != null) {
|
||||
for (Object node : classNode.visibleAnnotations) {
|
||||
AnnotationNode annotation = (AnnotationNode) node;
|
||||
if (annotation.desc
|
||||
.equals("L" + anName + ";"))
|
||||
return className;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//Bukkit.getLogger().info("Failed to scan: " + in.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void scanZip(Path path, Class<? extends Annotation> annotationClass, Set<String> plugins) {
|
||||
if (!ARCHIVE.matches(path.getFileName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
try (ZipFile zip = new ZipFile(path.toFile())) {
|
||||
Enumeration<? extends ZipEntry> entries = zip.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try (InputStream in = zip.getInputStream(entry)) {
|
||||
String plugin = findClass(in, annotationClass);
|
||||
if (plugin != null) {
|
||||
plugins.add(plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Set<String> scanFile(String file, Class<?> clazz) {
|
||||
return scanFile(file, new URL[]{clazz.getProtectionDomain().getCodeSource().getLocation()});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user