Search in sources :

Example 1 with MetadataContainer

use of org.spongepowered.plugin.metadata.builtin.MetadataContainer in project SpongeCommon by SpongePowered.

the class VanillaLaunch method createPlatformPlugins.

protected final void createPlatformPlugins() {
    final String metadataFileLocation = this.pluginPlatform.metadataFilePath();
    try {
        // This is a bit nasty, but allows Sponge to detect builtin platform plugins when it's not the first entry on the classpath.
        final URL classUrl = VanillaLaunch.class.getResource("/" + VanillaLaunch.class.getName().replace('.', '/') + ".class");
        MetadataContainer read = null;
        // In production, let's try to ensure we can find our descriptor even if we're not first on the classpath
        if (classUrl.getProtocol().equals("jar")) {
            // Extract the path of the underlying jar file, and parse it as a path to normalize it
            final String[] classUrlSplit = classUrl.getPath().split("!");
            final Path expectedFile = Paths.get(new URL(classUrlSplit[0]).toURI());
            // Then go through every possible resource
            final Enumeration<URL> manifests = VanillaLaunch.class.getClassLoader().getResources("/" + metadataFileLocation);
            while (manifests.hasMoreElements()) {
                final URL next = manifests.nextElement();
                if (!next.getProtocol().equals("jar")) {
                    continue;
                }
                // And stop when the normalized jar in that resource matches the URL of the jar that loaded VanillaLaunch?
                final String[] pathSplit = next.getPath().split("!");
                if (pathSplit.length == 2) {
                    final Path vanillaPath = Paths.get(new URL(pathSplit[0]).toURI());
                    if (vanillaPath.equals(expectedFile)) {
                        try (final Reader reader = new InputStreamReader(next.openStream(), StandardCharsets.UTF_8)) {
                            read = MetadataParser.read(reader);
                        }
                        break;
                    }
                }
            }
        }
        if (read == null) {
            // other measures failed, fall back to directly querying the classpath
            final Path vanillaPath = Paths.get(VanillaLaunch.class.getResource("/" + metadataFileLocation).toURI());
            try (final Reader reader = Files.newBufferedReader(vanillaPath, StandardCharsets.UTF_8)) {
                read = MetadataParser.read(reader);
            }
        }
        if (read == null) {
            throw new RuntimeException("Could not determine location for implementation metadata!");
        }
        for (final PluginMetadata metadata : read.metadata()) {
            this.pluginManager().addPlugin(new VanillaDummyPluginContainer(metadata, this.logger(), this));
        }
    } catch (final IOException | URISyntaxException e) {
        throw new RuntimeException("Could not load metadata information for the implementation! This should be impossible!");
    }
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) VanillaDummyPluginContainer(org.spongepowered.vanilla.launch.plugin.VanillaDummyPluginContainer) MetadataContainer(org.spongepowered.plugin.metadata.builtin.MetadataContainer) PluginMetadata(org.spongepowered.plugin.metadata.PluginMetadata)

Example 2 with MetadataContainer

use of org.spongepowered.plugin.metadata.builtin.MetadataContainer in project SpongeCommon by SpongePowered.

the class ModFileParsers method pluginMetadataParser.

public static IModFileInfo pluginMetadataParser(final String fileName, final IModFile iModFile) {
    final ModFile modFile = (ModFile) iModFile;
    AppLaunch.logger().debug("Considering plugin file candidate {}", modFile.getFilePath());
    final Path metadataFile = modFile.getLocator().findPath(modFile, "META-INF/" + fileName + ".json");
    if (Files.notExists(metadataFile)) {
        AppLaunch.logger().debug("Plugin file '{}' is missing a 'sponge_plugins.json' metadata file in META-INF", modFile);
        return null;
    }
    try {
        final MetadataContainer container;
        try (final Reader reader = Files.newBufferedReader(metadataFile, StandardCharsets.UTF_8)) {
            container = MetadataParser.read(reader);
        }
        final PluginFileConfigurable configurable = new PluginFileConfigurable(container);
        return ModFileParsers.generateModFileMetadata(modFile, configurable);
    } catch (final Exception e) {
        AppLaunch.logger().warn("Could not read metadata for plugin file '{}'", modFile, e);
        return null;
    }
}
Also used : Path(java.nio.file.Path) ModFile(net.minecraftforge.fml.loading.moddiscovery.ModFile) IModFile(net.minecraftforge.forgespi.locating.IModFile) MetadataContainer(org.spongepowered.plugin.metadata.builtin.MetadataContainer) PluginFileConfigurable(org.spongepowered.forge.applaunch.loading.metadata.PluginFileConfigurable) Reader(java.io.Reader)

Aggregations

Reader (java.io.Reader)2 Path (java.nio.file.Path)2 MetadataContainer (org.spongepowered.plugin.metadata.builtin.MetadataContainer)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ModFile (net.minecraftforge.fml.loading.moddiscovery.ModFile)1 IModFile (net.minecraftforge.forgespi.locating.IModFile)1 PluginFileConfigurable (org.spongepowered.forge.applaunch.loading.metadata.PluginFileConfigurable)1 PluginMetadata (org.spongepowered.plugin.metadata.PluginMetadata)1 VanillaDummyPluginContainer (org.spongepowered.vanilla.launch.plugin.VanillaDummyPluginContainer)1