Search in sources :

Example 11 with PluginMetadata

use of org.spongepowered.plugin.meta.PluginMetadata in project SpongeVanilla by SpongePowered.

the class PluginScanner method scanClassFile.

private PluginCandidate scanClassFile(InputStream in, PluginSource source) throws IOException {
    ClassReader reader = new ClassReader(in);
    PluginClassVisitor visitor = new PluginClassVisitor();
    try {
        reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        PluginMetadata metadata = visitor.getMetadata();
        if (metadata == null) {
            // Not a plugin class
            return null;
        }
        return new PluginCandidate(visitor.getClassName().replace('/', '.'), source, metadata);
    } catch (InvalidPluginException e) {
        logger.error("Skipping invalid plugin {} from {}", visitor.getClassName(), source, e);
    }
    return null;
}
Also used : ClassReader(org.objectweb.asm.ClassReader) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) PluginClassVisitor(org.spongepowered.server.launch.plugin.asm.PluginClassVisitor)

Example 12 with PluginMetadata

use of org.spongepowered.plugin.meta.PluginMetadata in project SpongeAPI by SpongePowered.

the class PluginProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        if (!roundEnv.errorRaised()) {
            finish();
        }
        return false;
    }
    if (!ProcessorUtils.contains(annotations, Plugin.class)) {
        return false;
    }
    for (Element e : roundEnv.getElementsAnnotatedWith(Plugin.class)) {
        if (e.getKind() != ElementKind.CLASS) {
            getMessager().printMessage(ERROR, "Invalid element of type " + e.getKind() + " annotated with @Plugin", e);
            continue;
        }
        final TypeElement pluginElement = (TypeElement) e;
        AnnotationWrapper<Plugin> annotation = AnnotationWrapper.of(pluginElement, Plugin.class);
        final String id = annotation.get().id();
        if (id.isEmpty()) {
            getMessager().printMessage(ERROR, "Plugin ID cannot be empty", pluginElement, annotation.getMirror(), annotation.getValue("id"));
            continue;
        }
        PluginMetadata meta = this.meta.remove(id);
        if (meta == null) {
            meta = new PluginMetadata(id);
        }
        PluginElement plugin = new PluginElement(pluginElement, annotation, meta);
        // Check for conflicting plugin IDs
        if (this.duplicates.contains(id) || this.plugins.containsKey(id)) {
            PluginElement otherPlugin = this.plugins.remove(id);
            if (otherPlugin != null) {
                reportDuplicatePlugin(id, otherPlugin);
                this.duplicates.add(id);
            }
            reportDuplicatePlugin(id, plugin);
            continue;
        }
        this.plugins.put(id, plugin);
        plugin.apply(getMessager());
    }
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) Plugin(org.spongepowered.api.plugin.Plugin)

Example 13 with PluginMetadata

use of org.spongepowered.plugin.meta.PluginMetadata in project LanternServer by LanternPowered.

the class InternalPluginContainer method readPluginInfo.

private static PluginMetadata readPluginInfo(String id, @Nullable String version) {
    try {
        final URL url = LanternClassLoader.get().getResource("data/" + id + "/plugin.info");
        checkNotNull(url, "Missing plugin.info file for internal plugin %s", id);
        final PluginMetadata metadata = InfoPluginContainer.readPluginInfo(id, url);
        // Allow the version to be overwritten
        if (version != null) {
            metadata.setVersion(version);
        }
        return metadata;
    } catch (IOException e) {
        throw new RuntimeException("Failed to read plugin.info files for the internal plugins.");
    }
}
Also used : PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) IOException(java.io.IOException) URL(java.net.URL)

Example 14 with PluginMetadata

use of org.spongepowered.plugin.meta.PluginMetadata in project LanternServer by LanternPowered.

the class AbstractAssetRepository method registerAsset.

Asset registerAsset(Object plugin, String id, URL url, Path file) {
    final Optional<Asset> optAsset = this.loadedAssets.get(id);
    if (optAsset != null) {
        return optAsset.get();
    }
    PluginContainer pluginContainer;
    if (plugin instanceof String) {
        final String pluginId = (String) plugin;
        // Attempt to find a plugin container that is assigned to the id,
        // if not, create a plugin container that represents the plugin
        // with the id.
        pluginContainer = this.pluginManager.getPlugin(pluginId).orElse(null);
        if (pluginContainer == null) {
            // don't register a plugin in this case, just return a asset with a dummy one.
            if (INFO_FILE_PATTERN.matcher(id).matches()) {
                return new LanternAsset(new SimplePluginContainer(pluginId), id, url, file);
            }
            // Attempt to get plugin info from the repository, and use
            // that to define the plugin container
            final URL infoUrl = getAssetURL(Paths.get(DEFAULT_ASSET_DIR).resolve(pluginId).resolve("plugin.info"));
            if (infoUrl != null) {
                try {
                    final PluginMetadata pluginMetadata = InfoPluginContainer.readPluginInfo(pluginId, infoUrl);
                    // Construct a plugin container
                    pluginContainer = new InfoPluginContainer(pluginId, pluginMetadata);
                } catch (IOException e) {
                    Lantern.getLogger().error("Failed to read plugin.info");
                }
            }
            if (pluginContainer == null) {
                // Generate a simple plugin container
                pluginContainer = new SimplePluginContainer(pluginId);
            }
            // Register the plugin container
            this.pluginManager.registerPlugin(pluginContainer);
            Lantern.getLogger().info("Registered data pack plugin: {} {}", pluginContainer.getName(), pluginContainer.getVersion().orElse("unknown"));
        }
    } else {
        // Search for the plugin container based on the instance
        pluginContainer = this.pluginManager.fromInstance(plugin).get();
    }
    checkNotNull(pluginContainer);
    final LanternAsset asset = new LanternAsset(pluginContainer, id, url, file);
    this.loadedAssets.put(id, Optional.of(asset));
    return asset;
}
Also used : SimplePluginContainer(org.lanternpowered.server.plugin.SimplePluginContainer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) InfoPluginContainer(org.lanternpowered.server.plugin.InfoPluginContainer) SimplePluginContainer(org.lanternpowered.server.plugin.SimplePluginContainer) Asset(org.lanternpowered.api.asset.Asset) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) IOException(java.io.IOException) URL(java.net.URL) InfoPluginContainer(org.lanternpowered.server.plugin.InfoPluginContainer)

Aggregations

PluginMetadata (org.spongepowered.plugin.meta.PluginMetadata)14 IOException (java.io.IOException)7 PluginContainer (org.spongepowered.api.plugin.PluginContainer)3 BufferedInputStream (java.io.BufferedInputStream)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 JarFile (java.util.jar.JarFile)2 JarInputStream (java.util.jar.JarInputStream)2 Manifest (java.util.jar.Manifest)2 ZipEntry (java.util.zip.ZipEntry)2 ClassReader (org.objectweb.asm.ClassReader)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BufferedWriter (java.io.BufferedWriter)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Attributes (java.util.jar.Attributes)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1