Search in sources :

Example 6 with PluginMetadata

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

the class MetadataContainer method load.

public static MetadataContainer load(String path) {
    List<PluginMetadata> meta;
    path = path + '/' + McModInfo.STANDARD_FILENAME;
    try (InputStream in = MetadataContainer.class.getResourceAsStream(path)) {
        if (in == null) {
            if (VanillaLaunch.ENVIRONMENT != DEVELOPMENT) {
                throw new LaunchException("Unable to find metadata file at " + path);
            }
            return new MetadataContainer(ImmutableMap.of());
        }
        meta = McModInfo.DEFAULT.read(in);
    } catch (IOException e) {
        throw new LaunchException("Failed to load metadata", e);
    }
    ImmutableMap.Builder<String, PluginMetadata> builder = ImmutableMap.builder();
    for (PluginMetadata m : meta) {
        builder.put(m.getId(), m);
    }
    return new MetadataContainer(builder.build());
}
Also used : LaunchException(org.spongepowered.server.launch.LaunchException) InputStream(java.io.InputStream) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with PluginMetadata

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

the class MetadataContainer method get.

public PluginMetadata get(String id, String name) {
    PluginMetadata meta = this.metadata.get(id);
    if (meta == null) {
        if (VanillaLaunch.ENVIRONMENT != DEVELOPMENT) {
            throw new RuntimeException("Unable to find metadata for " + id);
        }
        meta = new PluginMetadata(id);
        meta.setName(name);
    }
    return meta;
}
Also used : PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata)

Example 8 with PluginMetadata

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

the class MinecraftPluginContainer method create.

private static PluginContainer create() {
    MetadataContainer metadata = MetadataContainer.load("/net/minecraft");
    PluginMetadata meta = metadata.get(SpongeImpl.GAME_ID, SpongeImpl.GAME_NAME);
    meta.setVersion(SpongeImpl.MINECRAFT_VERSION.getName());
    return new MetaPluginContainer(meta, PluginSource.find(MinecraftServer.class));
}
Also used : PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 9 with PluginMetadata

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

the class VanillaPluginManager method loadPlugin.

private void loadPlugin(PluginCandidate candidate) {
    final String id = candidate.getId();
    candidate.getSource().addToClasspath();
    final PluginMetadata metadata = candidate.getMetadata();
    final String name = firstNonNull(metadata.getName(), id);
    final String version = firstNonNull(metadata.getVersion(), "unknown");
    try {
        Class<?> pluginClass = Class.forName(candidate.getPluginClass());
        Optional<Path> source = candidate.getSource().getPath();
        if (!source.isPresent()) {
            source = PluginSource.find(pluginClass);
        }
        PluginContainer container = new VanillaPluginContainer(this.rootInjector, pluginClass, metadata, source);
        registerPlugin(container);
        Sponge.getEventManager().registerListeners(container, container.getInstance().get());
        SpongeImpl.getLogger().info("Loaded plugin: {} {} (from {})", name, version, candidate.getSource());
    } catch (Throwable e) {
        SpongeImpl.getLogger().error("Failed to load plugin: {} {} (from {})", name, version, candidate.getSource(), e);
    }
}
Also used : Path(java.nio.file.Path) PluginContainer(org.spongepowered.api.plugin.PluginContainer) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata)

Example 10 with PluginMetadata

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

the class PluginScanner method scanJar.

void scanJar(Path path, boolean classpath) {
    logger.trace("Scanning {} for plugins", path);
    Set<String> annotationProcessors = Collections.emptySet();
    List<PluginCandidate> candidates = new ArrayList<>();
    List<PluginMetadata> metadata = null;
    Set<String> mixinConfigs = null;
    Set<String> mixinTokenProviders = null;
    PluginSource source = new PluginSource(path);
    // 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) {
            Attributes attributes = manifest.getMainAttributes();
            annotationProcessors = PluginAccessTransformers.find(attributes);
            mixinConfigs = PluginTweakers.findMixinConfigs(path, attributes);
            if (mixinConfigs != null) {
                mixinTokenProviders = PluginTweakers.findTokenProviders(attributes);
            }
        } else if (!classpath) {
            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;
                    }
                } else if (annotationProcessors.remove(name)) {
                    try {
                        AccessTransformers.register(jar);
                    } catch (IOException e) {
                        logger.warn("Failed to read access transformer from: {}!{}", path, name, e);
                    }
                }
                continue;
            }
            PluginCandidate candidate = scanClassFile(jar, source);
            if (candidate != null) {
                candidates.add(candidate);
            }
        } while ((entry = jar.getNextEntry()) != null);
    } catch (IOException e) {
        logger.error("Failed to scan plugin JAR: {}", path, e);
        return;
    }
    // There are some annotation processors left we haven't read yet
    if (!annotationProcessors.isEmpty()) {
        logger.warn("Found non-existent access transformers in plugin manifest of {}: {}", path, annotationProcessors);
    }
    if (mixinConfigs != null) {
        logger.warn("Plugin from {} uses Mixins to modify the Minecraft Server. If something breaks, remove it before reporting the " + "problem to Sponge!", source);
        // If the plugin wants to apply Mixins we need to add it to the classpath earlier
        source.addToClasspath();
        for (String config : mixinConfigs) {
            logger.debug("Registering Mixin config '{}' from {}", config, source);
            PluginTweakers.registerConfig(config);
        }
        if (mixinTokenProviders != null) {
            for (String provider : mixinTokenProviders) {
                logger.debug("Registering Mixin token provider '{}' from {}", provider, source);
                PluginTweakers.registerTokenProvider(provider);
            }
        }
    }
    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) {
            if (metadata == null) {
                logger.warn("{} is missing a valid " + METADATA_FILE + " 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);
            }
        }
    } else if (!classpath && mixinConfigs == null) {
        logger.error("No valid plugins found in {}. Is the file actually a plugin JAR? Please keep in mind Forge mods can be only loaded on " + "SpongeForge servers, SpongeVanilla supports only Sponge plugins.", path);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) BufferedInputStream(java.io.BufferedInputStream) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata)

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