Search in sources :

Example 1 with DefaultArtifactVersion

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

the class PluginCandidate method verifyVersionRange.

private boolean verifyVersionRange(String id, @Nullable String expectedRange, @Nullable String version) {
    if (expectedRange == null) {
        return true;
    }
    // Don't check version again if it already failed
    if (expectedRange.equals(this.missingRequirements.get(id))) {
        return false;
    }
    // Don't check version again if it was already checked
    if (expectedRange.equals(this.versions.get(id))) {
        return true;
    }
    if (version != null) {
        try {
            VersionRange range = VersionRange.createFromVersionSpec(expectedRange);
            DefaultArtifactVersion installedVersion = new DefaultArtifactVersion(version);
            if (range.containsVersion(installedVersion)) {
                String currentRange = this.versions.get(id);
                if (currentRange != null) {
                    // This should almost never happen because it means the plugin is
                    // depending on two different versions of another plugin
                    // We need to merge the ranges
                    VersionRange otherRange;
                    try {
                        otherRange = VersionRange.createFromVersionSpec(currentRange);
                    } catch (InvalidVersionSpecificationException e) {
                        // Should never happen because we already parsed it once
                        throw new AssertionError(e);
                    }
                    expectedRange = otherRange.restrict(range).toString();
                }
                this.versions.put(id, expectedRange);
                if (range.getRecommendedVersion() instanceof DefaultArtifactVersion) {
                    BigInteger majorExpected = ((DefaultArtifactVersion) range.getRecommendedVersion()).getVersion().getFirstInteger();
                    if (majorExpected != null) {
                        BigInteger majorInstalled = installedVersion.getVersion().getFirstInteger();
                        // or if the installed version is lower than the recommended version
                        if (majorInstalled != null && (!majorExpected.equals(majorInstalled) || installedVersion.compareTo(range.getRecommendedVersion()) < 0)) {
                            VanillaLaunch.getLogger().warn("Plugin {} from {} was designed for {} {}. It may not work properly.", this.id, this.source, id, range.getRecommendedVersion());
                        }
                    }
                }
                return true;
            }
        } catch (InvalidVersionSpecificationException e) {
            VanillaLaunch.getLogger().error("Failed to parse version range {} for dependency {} of plugin {} from {}: {}", version, id, this.id, this.source, e.getMessage());
            this.invalid = true;
        }
    } else {
        if (this.dependenciesWithUnknownVersion.add(id)) {
            VanillaLaunch.getLogger().warn("Cannot check version of dependency {} for plugin {} from {}: Version of dependency unknown", id, this.id, this.source);
        }
        return true;
    }
    return false;
}
Also used : InvalidVersionSpecificationException(org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException) DefaultArtifactVersion(org.spongepowered.plugin.meta.version.DefaultArtifactVersion) BigInteger(java.math.BigInteger) VersionRange(org.spongepowered.plugin.meta.version.VersionRange)

Example 2 with DefaultArtifactVersion

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

the class PluginCandidate method verifyVersionRange.

private boolean verifyVersionRange(String id, @Nullable String expectedRange, @Nullable String version) {
    if (expectedRange == null) {
        return true;
    }
    // Don't check version again if it already failed
    if (expectedRange.equals(this.missingRequirements.get(id))) {
        return false;
    }
    // Don't check version again if it was already checked
    if (expectedRange.equals(this.versions.get(id))) {
        return true;
    }
    if (version != null) {
        try {
            final VersionRange range = VersionRange.createFromVersionSpec(expectedRange);
            final DefaultArtifactVersion installedVersion = new DefaultArtifactVersion(version);
            if (range.containsVersion(installedVersion)) {
                final String currentRange = this.versions.get(id);
                if (currentRange != null) {
                    // This should almost never happen because it means the plugin is
                    // depending on two different versions of another plugin
                    // We need to merge the ranges
                    final VersionRange otherRange;
                    try {
                        otherRange = VersionRange.createFromVersionSpec(currentRange);
                    } catch (InvalidVersionSpecificationException e) {
                        // Should never happen because we already parsed it once
                        throw new AssertionError(e);
                    }
                    expectedRange = otherRange.restrict(range).toString();
                }
                this.versions.put(id, expectedRange);
                if (range.getRecommendedVersion() instanceof DefaultArtifactVersion) {
                    final BigInteger majorExpected = ((DefaultArtifactVersion) range.getRecommendedVersion()).getVersion().getFirstInteger();
                    if (majorExpected != null) {
                        final BigInteger majorInstalled = installedVersion.getVersion().getFirstInteger();
                        // or if the installed version is lower than the recommended version
                        if (majorInstalled != null && (!majorExpected.equals(majorInstalled) || installedVersion.compareTo(range.getRecommendedVersion()) < 0)) {
                            Lantern.getLogger().warn("Plugin {} from {} was designed for {} {}. It may not work properly.", this.id, this.source.map(Path::toString).orElse("unknown"), id, range.getRecommendedVersion());
                        }
                    }
                }
                return true;
            }
        } catch (InvalidVersionSpecificationException e) {
            Lantern.getLogger().error("Failed to parse version range {} for dependency {} of plugin {} from {}: {}", version, id, this.id, getDisplaySource(), e.getMessage());
            this.invalid = true;
        }
    } else {
        if (this.dependenciesWithUnknownVersion.add(id)) {
            Lantern.getLogger().warn("Cannot check version of dependency {} for plugin {} from {}: Unknown dependency version.", id, this.id, this.source.map(Path::toString).orElse("unknown"));
        }
        return true;
    }
    return false;
}
Also used : Path(java.nio.file.Path) InvalidVersionSpecificationException(org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException) DefaultArtifactVersion(org.spongepowered.plugin.meta.version.DefaultArtifactVersion) BigInteger(java.math.BigInteger) VersionRange(org.spongepowered.plugin.meta.version.VersionRange)

Aggregations

BigInteger (java.math.BigInteger)2 DefaultArtifactVersion (org.spongepowered.plugin.meta.version.DefaultArtifactVersion)2 InvalidVersionSpecificationException (org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException)2 VersionRange (org.spongepowered.plugin.meta.version.VersionRange)2 Path (java.nio.file.Path)1