Search in sources :

Example 1 with PluginVersion

use of io.gomint.proxprox.api.plugin.PluginVersion in project ProxProx by GoMint.

the class PluginAutoDetector method checkPlugin.

/**
 * Check if the jar given is useable as plugin
 *
 * @param jarFile The jar file which should be checked
 * @return a loaded plugin meta or null when not usable as plugin
 */
public PluginMeta checkPlugin(JarFile jarFile) {
    Enumeration<JarEntry> jarEntries = jarFile.entries();
    if (jarEntries == null) {
        logger.warn("Could not load Plugin. File " + jarFile + " is empty");
        return null;
    }
    try {
        while (jarEntries.hasMoreElements()) {
            JarEntry jarEntry = jarEntries.nextElement();
            if (jarEntry != null && jarEntry.getName().endsWith(".class")) {
                ClassFile classFile = new ClassFile(new DataInputStream(jarFile.getInputStream(jarEntry)));
                if (classFile.getSuperclass().equals("io.gomint.proxprox.api.plugin.Plugin")) {
                    PluginMeta pluginDescription = new PluginMeta();
                    pluginDescription.setName(classFile.getName().substring(classFile.getName().lastIndexOf('.') + 1));
                    AnnotationsAttribute visible = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag);
                    for (Annotation annotation : visible.getAnnotations()) {
                        switch(annotation.getTypeName()) {
                            case "io.gomint.proxprox.api.plugin.annotation.Description":
                                pluginDescription.setDescription(((StringMemberValue) annotation.getMemberValue("value")).getValue());
                                break;
                            case "io.gomint.proxprox.api.plugin.annotation.Version":
                                int major = ((IntegerMemberValue) annotation.getMemberValue("major")).getValue();
                                int minor = ((IntegerMemberValue) annotation.getMemberValue("minor")).getValue();
                                pluginDescription.setVersion(new PluginVersion(major, minor));
                                break;
                            case "io.gomint.proxprox.api.plugin.annotation.Depends":
                                MemberValue[] dependsValues = ((ArrayMemberValue) annotation.getMemberValue("value")).getValue();
                                HashSet<String> dependsStringValues = new HashSet<>();
                                for (MemberValue value : dependsValues) {
                                    dependsStringValues.add(((StringMemberValue) value).getValue());
                                }
                                pluginDescription.setDepends(dependsStringValues);
                                break;
                            case "io.gomint.proxprox.api.plugin.annotation.Name":
                                pluginDescription.setName(((StringMemberValue) annotation.getMemberValue("value")).getValue());
                                break;
                            default:
                                break;
                        }
                    }
                    pluginDescription.setMainClass(classFile.getName());
                    return pluginDescription;
                }
            }
        }
        return null;
    } catch (IOException e) {
        logger.warn("Could not load Plugin. File " + jarFile + " is corrupted", e);
        return null;
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) PluginMeta(io.gomint.proxprox.api.plugin.PluginMeta) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) DataInputStream(java.io.DataInputStream) PluginVersion(io.gomint.proxprox.api.plugin.PluginVersion) HashSet(java.util.HashSet)

Aggregations

PluginMeta (io.gomint.proxprox.api.plugin.PluginMeta)1 PluginVersion (io.gomint.proxprox.api.plugin.PluginVersion)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 JarEntry (java.util.jar.JarEntry)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 ClassFile (javassist.bytecode.ClassFile)1