Search in sources :

Example 1 with PluginMetadata

use of org.spongepowered.plugin.metadata.PluginMetadata 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 PluginMetadata

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

the class CrashReportMixin_Vanilla method vanilla$addPluginsToEnvironment.

@Inject(method = "initDetails", at = @At("RETURN"))
private void vanilla$addPluginsToEnvironment(final CallbackInfo ci) {
    this.systemDetails.setDetail("Plugins", () -> {
        final StringBuilder result = new StringBuilder(64);
        for (final PluginContainer container : Sponge.pluginManager().plugins()) {
            final PluginMetadata metadata = container.metadata();
            final String name = metadata.name().orElse(metadata.id());
            result.append("\n\t\t").append(name).append(" (").append(metadata.id()).append(") ").append(metadata.version());
        }
        return result.toString();
    });
}
Also used : PluginContainer(org.spongepowered.plugin.PluginContainer) PluginMetadata(org.spongepowered.plugin.metadata.PluginMetadata) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with PluginMetadata

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

the class PluginFileConfigurable method getConfigList.

@Override
public List<? extends IConfigurable> getConfigList(final String... key) {
    if (key.length < 1) {
        return Collections.emptyList();
    }
    final String query = key[0];
    if (key.length != this.requiredConfigElements(query)) {
        return Collections.emptyList();
    }
    if ("mods".equals(query)) {
        final Set<PluginMetadata> metadataById = this.container.metadata();
        if (metadataById.isEmpty()) {
            return Collections.emptyList();
        }
        final List<IConfigurable> metadataConfigurables = new ArrayList<>();
        metadataById.forEach((metadata) -> metadataConfigurables.add(new PluginMetadataConfigurable(metadata)));
        return metadataConfigurables;
    }
    if (key.length != 2) {
        return Collections.emptyList();
    }
    final String plugin = key[1];
    final PluginMetadata metadata = this.container.metadata(plugin).orElse(null);
    if (metadata == null) {
        return Collections.emptyList();
    }
    if ("dependencies".equals(query)) {
        // TODO Should we inject a dependency on SpongeForge?
        final Set<PluginDependency> dependencies = metadata.dependencies();
        if (dependencies.isEmpty()) {
            return Collections.emptyList();
        }
        final List<IConfigurable> depConfigurables = new ArrayList<>();
        for (final PluginDependency dependency : dependencies) {
            depConfigurables.add(new PluginDependencyConfigurable(metadata, dependency));
        }
        return depConfigurables;
    }
    return Collections.emptyList();
}
Also used : PluginDependency(org.spongepowered.plugin.metadata.model.PluginDependency) ArrayList(java.util.ArrayList) PluginMetadata(org.spongepowered.plugin.metadata.PluginMetadata) IConfigurable(net.minecraftforge.forgespi.language.IConfigurable)

Example 4 with PluginMetadata

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

the class PluginFileConfigurable method getConfigElement.

@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getConfigElement(final String... key) {
    if (key.length < 1) {
        return Optional.empty();
    }
    final String query = key[0];
    if (key.length != this.requiredConfigElements(query)) {
        return Optional.empty();
    }
    if ("modLoader".equals(query)) {
        return (Optional<T>) Optional.of(this.container.loader().name());
    }
    if ("loaderVersion".equals(query)) {
        return (Optional<T>) Optional.of(this.container.loader().version().toString());
    }
    if ("license".equals(query)) {
        return (Optional<T>) Optional.of(this.container.license());
    }
    if (key.length == 2) {
        final String plugin = key[1];
        final PluginMetadata metadata = this.container.metadata(plugin).orElse(null);
        if (metadata == null) {
            return Optional.empty();
        }
        if ("modproperties".equals(query)) {
            return (Optional<T>) Optional.of(metadata.properties());
        }
    }
    return Optional.empty();
}
Also used : Optional(java.util.Optional) PluginMetadata(org.spongepowered.plugin.metadata.PluginMetadata)

Example 5 with PluginMetadata

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

the class SpongeCommand method versionExecutor.

@NonNull
private CommandResult versionExecutor(final CommandContext context) {
    if (this.versionText == null) {
        final PluginContainer platformPlugin = Launch.instance().platformPlugin();
        final TextComponent.Builder builder = Component.text().append(Component.text(platformPlugin.metadata().name().get(), Style.style(NamedTextColor.YELLOW, TextDecoration.BOLD)));
        final Component colon = Component.text(": ", NamedTextColor.GRAY);
        for (final PluginContainer container : Launch.instance().launcherPlugins()) {
            final PluginMetadata metadata = container.metadata();
            builder.append(Component.newline(), SpongeCommand.INDENT_COMPONENT, Component.text(metadata.name().orElseGet(metadata::id), NamedTextColor.GRAY), colon, Component.text(container.metadata().version().toString()));
        }
        final String arch = System.getProperty("sun.arch.data.model");
        final String javaArch = arch != null ? arch + "-bit" : "UNKNOWN";
        final String javaVendor = System.getProperty("java.vendor");
        final String javaVersion = System.getProperty("java.version");
        final String osName = System.getProperty("os.name");
        final String osVersion = System.getProperty("os.version");
        final String osArch = System.getProperty("os.arch");
        builder.append(Component.newline(), SpongeCommand.INDENT_COMPONENT, Component.text("JVM", NamedTextColor.GRAY), colon, Component.text(javaVersion + "/" + javaArch + " (" + javaVendor + ")"), Component.newline(), SpongeCommand.INDENT_COMPONENT, Component.text("OS", NamedTextColor.GRAY), colon, Component.text(osName + "/" + osVersion + " (" + osArch + ")"));
        this.versionText = builder.build();
    }
    context.sendMessage(Identity.nil(), this.versionText);
    return CommandResult.success();
}
Also used : TextComponent(net.kyori.adventure.text.TextComponent) PluginContainer(org.spongepowered.plugin.PluginContainer) PluginMetadata(org.spongepowered.plugin.metadata.PluginMetadata) TextComponent(net.kyori.adventure.text.TextComponent) Component(net.kyori.adventure.text.Component) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

PluginMetadata (org.spongepowered.plugin.metadata.PluginMetadata)6 PluginContainer (org.spongepowered.plugin.PluginContainer)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 Component (net.kyori.adventure.text.Component)2 TextComponent (net.kyori.adventure.text.TextComponent)2 NonNull (org.checkerframework.checker.nullness.qual.NonNull)2 Timings (co.aikar.timings.Timings)1 SpongeTimingsFactory (co.aikar.timings.sponge.SpongeTimingsFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ManagementFactory (java.lang.management.ManagementFactory)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 DecimalFormat (java.text.DecimalFormat)1 MessageFormat (java.text.MessageFormat)1