Add support for resolving dependencies that require a version range

This commit is contained in:
Andrew Steinborn
2021-05-15 08:16:17 -04:00
parent 348ea4cc23
commit 2a39ddb03e
24 changed files with 258 additions and 58 deletions

View File

@@ -25,10 +25,14 @@ test {
jar {
manifest {
def buildNumber = System.getenv("BUILD_NUMBER") ?: "unknown"
def buildNumber = System.getenv("BUILD_NUMBER")
def version
if (project.version.endsWith("-SNAPSHOT")) {
version = "${project.version} (git-${project.ext.getCurrentShortRevision()}-b${buildNumber})"
if (buildNumber != null) {
version = "${project.version}+g${project.ext.getCurrentShortRevision()}-b${buildNumber}"
} else {
version = "${project.version}+g${project.ext.getCurrentShortRevision()}"
}
} else {
version = "${project.version}"
}
@@ -86,6 +90,7 @@ dependencies {
implementation 'org.lanternpowered:lmbda:2.0.0-SNAPSHOT'
implementation 'com.github.ben-manes.caffeine:caffeine:2.8.8'
implementation 'com.vdurmont:semver4j:3.1.0'
compileOnly 'com.github.spotbugs:spotbugs-annotations:4.1.2'

View File

@@ -118,7 +118,7 @@ public class Metrics {
() -> server.configuration().isOnlineMode() ? "online" : "offline")
);
metrics.addCustomChart(new SimplePie("velocity_version",
() -> server.version().getVersion()));
() -> server.version().version()));
metrics.addCustomChart(new DrilldownPie("java_version", () -> {
Map<String, Map<String, Integer>> map = new HashMap<>();

View File

@@ -26,6 +26,7 @@ import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.event.lifecycle.ProxyInitializeEventImpl;
import com.velocitypowered.api.event.lifecycle.ProxyReloadEventImpl;
import com.velocitypowered.api.event.lifecycle.ProxyShutdownEventImpl;
import com.velocitypowered.api.network.NetworkEndpoint;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginManager;
@@ -184,6 +185,11 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
return new ProxyVersion(implName, implVendor, implVersion);
}
@Override
public Collection<NetworkEndpoint> endpoints() {
return this.cm.endpoints();
}
@Override
public VelocityCommandManager commandManager() {
return commandManager;
@@ -195,7 +201,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
@EnsuresNonNull({"serverKeyPair", "eventManager", "console", "cm", "configuration"})
void start() {
logger.info("Booting up {} {}...", version().getName(), version().getVersion());
logger.info("Booting up {} {}...", version().name(), version().version());
console.setupStreams();
registerTranslations();

View File

@@ -217,19 +217,19 @@ public class VelocityCommand implements SimpleCommand {
ProxyVersion version = server.version();
Component velocity = Component.text().content(version.getName() + " ")
Component velocity = Component.text().content(version.name() + " ")
.decoration(TextDecoration.BOLD, true)
.color(VELOCITY_COLOR)
.append(Component.text(version.getVersion()).decoration(TextDecoration.BOLD, false))
.append(Component.text(version.version()).decoration(TextDecoration.BOLD, false))
.build();
Component copyright = Component
.translatable("velocity.command.version-copyright",
Component.text(version.getVendor()),
Component.text(version.getName()));
Component.text(version.vendor()),
Component.text(version.name()));
source.sendMessage(velocity);
source.sendMessage(copyright);
if (version.getName().equals("Velocity")) {
if (version.name().equals("Velocity")) {
TextComponent embellishment = Component.text()
.append(Component.text().content("velocitypowered.com")
.color(NamedTextColor.GREEN)
@@ -383,8 +383,8 @@ public class VelocityCommand implements SimpleCommand {
BoundRequestBuilder request =
httpClient.preparePost("https://dump.velocitypowered.com/documents");
request.setHeader("Content-Type", "text/plain");
request.addHeader("User-Agent", server.version().getName() + "/"
+ server.version().getVersion());
request.addHeader("User-Agent", server.version().name() + "/"
+ server.version().version());
request.setBody(
InformationUtils.toHumanReadableString(dump).getBytes(StandardCharsets.UTF_8));

View File

@@ -25,6 +25,7 @@ import com.velocitypowered.api.proxy.connection.InboundConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerPing;
import com.velocitypowered.api.util.ModInfo;
import com.velocitypowered.api.util.ProxyVersion;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.config.PingPassthroughMode;
import com.velocitypowered.proxy.config.VelocityConfiguration;
@@ -75,9 +76,10 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
private ServerPing constructLocalPing(ProtocolVersion version) {
VelocityConfiguration configuration = server.configuration();
ProxyVersion proxyVersion = server.version();
return new ServerPing(
new ServerPing.Version(version.protocol(),
"Velocity " + ProtocolVersion.SUPPORTED_VERSION_STRING),
proxyVersion.name() + " " + ProtocolVersion.SUPPORTED_VERSION_STRING),
new ServerPing.Players(server.countConnectedPlayers(), configuration.getShowMaxPlayers(),
ImmutableList.of()),
configuration.getMotd(),

View File

@@ -24,6 +24,7 @@ import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.lifecycle.network.ListenerBoundEventImpl;
import com.velocitypowered.api.event.lifecycle.network.ListenerClosedEventImpl;
import com.velocitypowered.api.network.ListenerType;
import com.velocitypowered.api.network.NetworkEndpoint;
import com.velocitypowered.natives.util.Natives;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.network.pipeline.GS4QueryHandler;
@@ -39,7 +40,9 @@ import io.netty.channel.epoll.EpollChannelOption;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -87,7 +90,7 @@ public final class ConnectionManager {
this.resolver = new SeparatePoolInetNameResolver(GlobalEventExecutor.INSTANCE);
this.httpClient = asyncHttpClient(config()
.setEventLoopGroup(this.workerGroup)
.setUserAgent(server.version().getName() + "/" + server.version().getVersion())
.setUserAgent(server.version().name() + "/" + server.version().version())
.addRequestFilter(new RequestFilter() {
@Override
public <T> FilterContext<T> filter(FilterContext<T> ctx) {
@@ -205,7 +208,7 @@ public final class ConnectionManager {
// Fire proxy close event to notify plugins of socket close. We block since plugins
// should have a chance to be notified before the server stops accepting connections.
server.eventManager().fire(new ListenerClosedEventImpl(oldBind, endpoint.getType())).join();
server.eventManager().fire(new ListenerClosedEventImpl(oldBind, endpoint.type())).join();
Channel serverChannel = endpoint.getChannel();
@@ -224,7 +227,7 @@ public final class ConnectionManager {
// Fire proxy close event to notify plugins of socket close. We block since plugins
// should have a chance to be notified before the server stops accepting connections.
server.eventManager().fire(new ListenerClosedEventImpl(address, endpoint.getType())).join();
server.eventManager().fire(new ListenerClosedEventImpl(address, endpoint.type())).join();
try {
LOGGER.info("Closing endpoint {}", address);
@@ -253,4 +256,8 @@ public final class ConnectionManager {
public ChannelInitializerHolder<Channel> getBackendChannelInitializer() {
return this.backendChannelInitializer;
}
public Collection<NetworkEndpoint> endpoints() {
return List.copyOf(this.endpoints.values());
}
}

View File

@@ -19,12 +19,14 @@ package com.velocitypowered.proxy.network;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.network.ListenerType;
import com.velocitypowered.api.network.NetworkEndpoint;
import io.netty.channel.Channel;
import java.net.SocketAddress;
/**
* Represents a listener endpoint.
*/
public final class Endpoint {
public final class Endpoint implements NetworkEndpoint {
private final Channel channel;
private final ListenerType type;
@@ -37,7 +39,13 @@ public final class Endpoint {
return channel;
}
public ListenerType getType() {
@Override
public ListenerType type() {
return type;
}
@Override
public SocketAddress address() {
return this.channel.localAddress();
}
}

View File

@@ -153,7 +153,7 @@ public final class PluginMessageUtil {
checkArgument(isMcBrand(message), "message is not a brand plugin message");
String currentBrand = readBrandMessage(message.content());
String rewrittenBrand = String.format("%s (%s)", currentBrand, version.getName());
String rewrittenBrand = String.format("%s (%s)", currentBrand, version.name());
ByteBuf rewrittenBuf = Unpooled.buffer();
if (protocolVersion.gte(ProtocolVersion.MINECRAFT_1_8)) {

View File

@@ -17,6 +17,7 @@
package com.velocitypowered.proxy.plugin;
import com.velocitypowered.api.plugin.PluginDescription;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
@@ -33,8 +34,11 @@ public class PluginClassLoader extends URLClassLoader {
ClassLoader.registerAsParallelCapable();
}
public PluginClassLoader(URL[] urls) {
private final PluginDescription description;
public PluginClassLoader(URL[] urls, PluginDescription description) {
super(urls);
this.description = description;
}
public void addToClassloaders() {
@@ -82,4 +86,9 @@ public class PluginClassLoader extends URLClassLoader {
throw new ClassNotFoundException(name);
}
@Override
public String toString() {
return "plugin " + this.description.name();
}
}

View File

@@ -25,6 +25,9 @@ import com.google.common.base.MoreObjects;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.name.Names;
import com.vdurmont.semver4j.Semver;
import com.vdurmont.semver4j.Semver.SemverType;
import com.vdurmont.semver4j.SemverException;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.event.EventManager;
import com.velocitypowered.api.plugin.PluginContainer;
@@ -36,6 +39,7 @@ import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.plugin.loader.VelocityPluginContainer;
import com.velocitypowered.proxy.plugin.loader.java.JavaPluginLoader;
import com.velocitypowered.proxy.plugin.util.PluginDependencyUtils;
import com.velocitypowered.proxy.plugin.util.ProxyPluginContainer;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.nio.file.DirectoryStream;
@@ -44,6 +48,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
@@ -64,6 +69,9 @@ public class VelocityPluginManager implements PluginManager {
public VelocityPluginManager(VelocityServer server) {
this.server = checkNotNull(server, "server");
// Register ourselves as a plugin
this.registerPlugin(ProxyPluginContainer.VELOCITY);
}
private void registerPlugin(PluginContainer plugin) {
@@ -106,17 +114,48 @@ public class VelocityPluginManager implements PluginManager {
List<PluginDescription> sortedPlugins = PluginDependencyUtils.sortCandidates(found);
Set<String> loadedPluginsById = new HashSet<>();
Map<String, PluginContainer> loadedPluginsById = new HashMap<>(this.plugins);
Map<PluginContainer, Module> pluginContainers = new LinkedHashMap<>();
// Now load the plugins
pluginLoad:
for (PluginDescription candidate : sortedPlugins) {
// Verify dependencies
for (PluginDependency dependency : candidate.dependencies()) {
if (!dependency.optional() && !loadedPluginsById.contains(dependency.id())) {
logger.error("Can't load plugin {} due to missing dependency {}", candidate.id(),
dependency.id());
continue pluginLoad;
final PluginContainer dependencyContainer = loadedPluginsById.get(dependency.id());
if (dependencyContainer == null) {
if (dependency.optional()) {
logger.warn("Plugin {} has an optional dependency {} that is not available",
candidate.id(), dependency.id());
} else {
logger.error("Can't load plugin {} due to missing dependency {}",
candidate.id(), dependency.id());
continue pluginLoad;
}
} else {
String requiredRange = dependency.version();
if (!requiredRange.isEmpty()) {
try {
Semver dependencyCandidateVersion = new Semver(
dependencyContainer.description().version(), SemverType.NPM);
if (!dependencyCandidateVersion.satisfies(requiredRange)) {
if (dependency.optional()) {
logger.error(
"Can't load plugin {} due to incompatible dependency {} {} (you have {})",
candidate.id(), dependency.id(), requiredRange,
dependencyContainer.description().version());
continue pluginLoad;
} else {
logger.warn(
"Plugin {} has an optional dependency on {} {}, but you have {}",
candidate.id(), dependency.id(), requiredRange,
dependencyContainer.description().version());
}
}
} catch (SemverException exception) {
logger.warn("Can't check dependency of {} for the proper version of {},"
+ " assuming they are compatible", candidate.id(), dependency.id());
}
}
}
}
@@ -124,7 +163,7 @@ public class VelocityPluginManager implements PluginManager {
PluginDescription realPlugin = loader.loadPlugin(candidate);
VelocityPluginContainer container = new VelocityPluginContainer(realPlugin);
pluginContainers.put(container, loader.createModule(container));
loadedPluginsById.add(realPlugin.id());
loadedPluginsById.put(candidate.id(), container);
} catch (Exception e) {
logger.error("Can't create module for plugin {}", candidate.id(), e);
}

View File

@@ -34,7 +34,7 @@ public class VelocityPluginDescription implements PluginDescription {
private final String id;
private final @Nullable String name;
private final @Nullable String version;
private final String version;
private final @Nullable String description;
private final @Nullable String url;
private final List<String> authors;
@@ -52,13 +52,13 @@ public class VelocityPluginDescription implements PluginDescription {
* @param dependencies the dependencies for this plugin
* @param source the original source for the plugin
*/
public VelocityPluginDescription(String id, @Nullable String name, @Nullable String version,
public VelocityPluginDescription(String id, @Nullable String name, String version,
@Nullable String description, @Nullable String url,
@Nullable List<String> authors, Collection<PluginDependency> dependencies,
@Nullable Path source) {
this.id = checkNotNull(id, "id");
this.name = Strings.emptyToNull(name);
this.version = Strings.emptyToNull(version);
this.version = checkNotNull(version, "version");
this.description = Strings.emptyToNull(description);
this.url = Strings.emptyToNull(url);
this.authors = authors == null ? ImmutableList.of() : ImmutableList.copyOf(authors);
@@ -77,7 +77,7 @@ public class VelocityPluginDescription implements PluginDescription {
}
@Override
public @Nullable String version() {
public String version() {
return version;
}

View File

@@ -81,7 +81,8 @@ public class JavaPluginLoader implements PluginLoader {
URL pluginJarUrl = jarFilePath.toUri().toURL();
PluginClassLoader loader = AccessController.doPrivileged(
(PrivilegedAction<PluginClassLoader>) () -> new PluginClassLoader(new URL[]{pluginJarUrl}));
(PrivilegedAction<PluginClassLoader>) () -> new PluginClassLoader(new URL[]{pluginJarUrl},
source));
loader.addToClassloaders();
JavaVelocityPluginDescriptionCandidate candidate =
@@ -200,7 +201,7 @@ public class JavaPluginLoader implements PluginLoader {
SerializedPluginDescription.Dependency dependency) {
return new PluginDependency(
dependency.getId(),
null, // TODO Implement version matching in dependency annotation
dependency.getVersion(),
dependency.isOptional()
);
}

View File

@@ -30,7 +30,7 @@ class JavaVelocityPluginDescription extends VelocityPluginDescription {
private final Class<?> mainClass;
JavaVelocityPluginDescription(String id, @Nullable String name, @Nullable String version,
JavaVelocityPluginDescription(String id, @Nullable String name, String version,
@Nullable String description, @Nullable String url,
@Nullable List<String> authors, Collection<PluginDependency> dependencies,
@Nullable Path source, Class<?> mainClass) {

View File

@@ -30,7 +30,7 @@ class JavaVelocityPluginDescriptionCandidate extends VelocityPluginDescription {
private final String mainClass;
JavaVelocityPluginDescriptionCandidate(String id, @Nullable String name, @Nullable String version,
JavaVelocityPluginDescriptionCandidate(String id, @Nullable String name, String version,
@Nullable String description, @Nullable String url,
@Nullable List<String> authors, Collection<PluginDependency> dependencies, Path source,
String mainClass) {

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2018 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.velocitypowered.proxy.plugin.util;
import com.google.common.base.MoreObjects;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.proxy.VelocityServer;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;
public class ProxyPluginContainer implements PluginContainer {
public static final PluginContainer VELOCITY = new ProxyPluginContainer();
private final PluginDescription description = new PluginDescription() {
@Override
public String id() {
return "velocity";
}
@Override
public String name() {
final Package pkg = VelocityServer.class.getPackage();
return MoreObjects.firstNonNull(pkg.getImplementationTitle(), "Velocity");
}
@Override
public String version() {
final Package pkg = VelocityServer.class.getPackage();
return MoreObjects.firstNonNull(pkg.getImplementationVersion(), "<unknown>");
}
@Override
public List<String> authors() {
final Package pkg = VelocityServer.class.getPackage();
final String vendor = MoreObjects.firstNonNull(pkg.getImplementationVendor(),
"Velocity Contributors");
return List.of(vendor);
}
};
@Override
public PluginDescription description() {
return this.description;
}
@Override
public @Nullable Object instance() {
return null;
}
}

View File

@@ -33,11 +33,11 @@ class PluginDependencyUtilsTest {
private static final PluginDescription NO_DEPENDENCY = testDescription("trivial");
private static final PluginDescription NO_DEPENDENCY_2 = testDescription("trivial2");
private static final PluginDescription HAS_DEPENDENCY_1 = testDescription("dependent1",
new PluginDependency("trivial", null, false));
new PluginDependency("trivial", "", false));
private static final PluginDescription HAS_DEPENDENCY_2 = testDescription("dependent2",
new PluginDependency("dependent1", null, false));
new PluginDependency("dependent1", "", false));
private static final PluginDescription HAS_DEPENDENCY_3 = testDescription("dependent3",
new PluginDependency("trivial", null, false));
new PluginDependency("trivial", "", false));
private static final PluginDescription CIRCULAR_DEPENDENCY_1 = testDescription("circle",
new PluginDependency("oval", "", false));