Search in sources :

Example 1 with PluginMetadata

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

the class PluginProcessor method finish.

private void finish() {
    if (!this.meta.isEmpty()) {
        getMessager().printMessage(WARNING, "The following extra plugin IDs were not found: " + this.meta.keySet());
    }
    List<PluginMetadata> meta = this.plugins.values().stream().map(PluginElement::getMetadata).collect(Collectors.toList());
    meta.addAll(this.meta.values());
    try (BufferedWriter writer = createWriter()) {
        McModInfo.DEFAULT.write(writer, meta);
    } catch (IOException e) {
        throw new PluginProcessException("Failed to write plugin metadata", e);
    }
}
Also used : PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 2 with PluginMetadata

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

the class PluginProcessor method init.

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    String extraFiles = processingEnv.getOptions().get(EXTRA_FILES_OPTION);
    if (extraFiles != null && !extraFiles.isEmpty()) {
        for (String file : FILE_SPLITTER.split(extraFiles)) {
            Path path = Paths.get(file);
            getMessager().printMessage(NOTE, "Reading extra plugin metadata from " + path);
            try {
                for (PluginMetadata meta : McModInfo.DEFAULT.read(path)) {
                    PluginMetadata base = this.meta.putIfAbsent(meta.getId(), meta);
                    if (base != null) {
                        base.accept(meta);
                    }
                }
            } catch (IOException e) {
                throw new PluginProcessException("Failed to read extra plugin metadata from " + path, e);
            }
        }
    }
    String outputFile = processingEnv.getOptions().get(OUTPUT_FILE_OPTION);
    if (outputFile != null && !outputFile.isEmpty()) {
        this.outputPath = Paths.get(outputFile);
    }
}
Also used : Path(java.nio.file.Path) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) IOException(java.io.IOException)

Example 3 with PluginMetadata

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

the class LanternPluginManager method loadPlugin.

private void loadPlugin(PluginCandidate candidate) {
    final String id = candidate.getId();
    final LanternClassLoader classLoader = LanternClassLoader.get();
    if (candidate.getSource().isPresent()) {
        try {
            classLoader.addBaseURL(candidate.getSource().get().toUri().toURL());
        } catch (MalformedURLException e) {
            throw new RuntimeException("Failed to add plugin '" + id + "' from " + candidate.getDisplaySource() + " to classpath", e);
        }
    }
    final PluginMetadata metadata = candidate.getMetadata();
    checkNotNull(metadata, "metadata");
    final String name = firstNonNull(metadata.getName(), id);
    final String version = firstNonNull(metadata.getVersion(), "unknown");
    try {
        final Class<?> pluginClass = Class.forName(candidate.getPluginClass());
        final PluginContainer container = new LanternPluginContainer(id, this.injector, pluginClass, metadata, candidate.getSource().orElse(null));
        registerPlugin(container);
        registerPluginInstance(container);
        this.eventManager.registerListeners(container, container.getInstance().get());
        this.logger.info("Loaded plugin: {} {} (from {})", name, version, candidate.getDisplaySource());
    } catch (Throwable e) {
        this.logger.error("Failed to load plugin: {} {} (from {})", name, version, candidate.getDisplaySource(), e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) LanternClassLoader(org.lanternpowered.launch.LanternClassLoader) PluginContainer(org.spongepowered.api.plugin.PluginContainer) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata)

Example 4 with PluginMetadata

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

the class PluginScanner method scanJar.

private void scanJar(Path path, boolean classpath) {
    logger.trace("Scanning {} for plugins", path);
    final List<PluginCandidate> candidates = new ArrayList<>();
    List<PluginMetadata> metadata = null;
    // Open the zip file so we can scan it for plugins
    try (JarInputStream jar = new JarInputStream(new BufferedInputStream(Files.newInputStream(path)))) {
        ZipEntry entry = jar.getNextEntry();
        if (entry == null) {
            return;
        }
        Manifest manifest = jar.getManifest();
        if (manifest == null) {
            // Try harder to find it anyway - Wtf Java?
            try (JarFile jarFile = new JarFile(path.toFile())) {
                manifest = jarFile.getManifest();
            }
        }
        if (manifest == null && !classpath) {
            // TODO
            logger.warn("Missing JAR manifest in {}", path);
        }
        do {
            if (entry.isDirectory()) {
                continue;
            }
            final String name = entry.getName();
            if (!name.endsWith(CLASS_EXTENSION)) {
                if (name.equals(METADATA_FILE)) {
                    try {
                        metadata = McModInfo.DEFAULT.read(jar);
                    } catch (IOException e) {
                        logger.error("Failed to read plugin metadata from " + METADATA_FILE + " in {}", path, e);
                        return;
                    }
                }
                continue;
            }
            final PluginCandidate candidate = scanClassFile(jar, path);
            if (candidate != null) {
                candidates.add(candidate);
            }
        } while ((entry = jar.getNextEntry()) != null);
    } catch (IOException e) {
        logger.error("Failed to scan plugin JAR: {}", path, e);
        return;
    }
    if (!candidates.isEmpty()) {
        boolean success = false;
        for (PluginCandidate candidate : candidates) {
            success |= addCandidate(candidate);
            // Find matching plugin metadata
            if (metadata != null) {
                boolean found = false;
                for (PluginMetadata meta : metadata) {
                    if (candidate.getId().equals(meta.getId())) {
                        found = true;
                        candidate.setMetadata(meta);
                        break;
                    }
                }
                if (!found) {
                    logger.warn("No matching metadata found for plugin '{}' in " + METADATA_FILE + " from {}", candidate.getId(), path);
                }
            }
        }
        if (!success) {
            logger.warn("{} is missing a valid {} file." + "This is not a problem when testing plugins, however it is recommended to include one in public plugins.\n" + "Please see https://docs.spongepowered.org/master/en/plugin/plugin-meta.html for details.", path, METADATA_FILE);
        }
    } else if (!classpath) {
        logger.error("No valid plugins found in {}. Is the file actually a plugin JAR? Please keep in" + "mind that Lantern can only load Sponge plugins.", path);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) BufferedInputStream(java.io.BufferedInputStream) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata)

Example 5 with PluginMetadata

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

the class PluginScanner method scanClassFile.

private PluginCandidate scanClassFile(InputStream in, @Nullable Path source) throws IOException {
    final ClassReader reader = new ClassReader(in);
    final PluginClassVisitor visitor = new PluginClassVisitor();
    try {
        reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        final 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.lanternpowered.server.plugin.asm.PluginClassVisitor)

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