Search in sources :

Example 1 with VanillaDummyPluginContainer

use of org.spongepowered.vanilla.launch.plugin.VanillaDummyPluginContainer 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)

Aggregations

IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 PluginMetadata (org.spongepowered.plugin.metadata.PluginMetadata)1 MetadataContainer (org.spongepowered.plugin.metadata.builtin.MetadataContainer)1 VanillaDummyPluginContainer (org.spongepowered.vanilla.launch.plugin.VanillaDummyPluginContainer)1