Skip to content

Commit

Permalink
Support loading plugins from classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed May 27, 2024
1 parent 42d4288 commit 37887a0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
18 changes: 18 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ plugins {
`java-library`
id("velocity-checkstyle") apply false
id("velocity-spotless") apply false
alias(libs.plugins.ideaExt)
}

subprojects {
apply<JavaLibraryPlugin>()

apply(plugin = "velocity-checkstyle")
apply(plugin = "velocity-spotless")
apply(plugin = "org.jetbrains.gradle.plugin.idea-ext")

java {
toolchain {
Expand All @@ -28,4 +30,20 @@ subprojects {
}
}
}

idea {
if (project != null) {
(project as ExtensionAware).extensions["settings"].run {
(this as ExtensionAware).extensions.getByType(org.jetbrains.gradle.ext.ActionDelegationConfig::class).run {
delegateBuildRunToGradle = false
testRunner = org.jetbrains.gradle.ext.ActionDelegationConfig.TestRunner.PLATFORM
}
extensions.getByType(org.jetbrains.gradle.ext.IdeaCompilerConfiguration::class).run {
addNotNullAssertions = false
useReleaseOption = JavaVersion.current().isJava10Compatible
parallelCompilation = true
}
}
}
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ configurate4 = "4.1.2"
flare = "2.0.1"
log4j = "2.22.1"
netty = "4.1.106.Final"
ideaExt = "1.1.8"

[plugins]
indra-publishing = "net.kyori.indra.publishing:2.0.6"
shadow = "io.github.goooler.shadow:8.1.5"
spotless = "com.diffplug.spotless:6.25.0"
ideaExt = { id = "org.jetbrains.gradle.plugin.idea-ext", version.ref = "ideaExt" }

[libraries]
adventure-bom = "net.kyori:adventure-bom:4.16.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@
import com.velocitypowered.proxy.plugin.util.PluginDependencyUtils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -89,6 +93,26 @@ public void loadPlugins(Path directory) throws IOException {
List<PluginDescription> found = new ArrayList<>();
JavaPluginLoader loader = new JavaPluginLoader(server, directory);

Enumeration<URL> resources = ClassLoader.getSystemClassLoader().getResources("velocity-plugin.json");
while (resources.hasMoreElements()) {
URL url = resources.nextElement();

URI uri;
try {
uri = url.toURI();
} catch (URISyntaxException e) {
logger.error("Malformed URI {}", url, e);
continue;
}

Path path = Path.of(uri);
try {
found.add(loader.loadCandidate(path.getParent()));
} catch (Throwable e) {
logger.error("Unable to load plugin {}", path, e);
}
}

try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory,
p -> p.toFile().isFile() && p.toString().endsWith(".jar"))) {
for (Path path : stream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.velocitypowered.proxy.plugin.loader.VelocityPluginContainer;
import com.velocitypowered.proxy.plugin.loader.VelocityPluginDescription;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
Expand Down Expand Up @@ -141,32 +143,47 @@ public void createPlugin(PluginContainer container, Module... modules) {
private Optional<SerializedPluginDescription> getSerializedPluginInfo(Path source)
throws Exception {
boolean foundBungeeBukkitPluginFile = false;
try (JarInputStream in = new JarInputStream(
new BufferedInputStream(Files.newInputStream(source)))) {
JarEntry entry;
while ((entry = in.getNextJarEntry()) != null) {
if (entry.getName().equals("velocity-plugin.json")) {
try (Reader pluginInfoReader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
return Optional.of(VelocityServer.GENERAL_GSON.fromJson(pluginInfoReader,
SerializedPluginDescription.class));
if (source.getFileName().endsWith(".jar")) {
try (JarInputStream in = new JarInputStream(
new BufferedInputStream(Files.newInputStream(source)))) {
JarEntry entry;
while ((entry = in.getNextJarEntry()) != null) {
if (entry.getName().equals("velocity-plugin.json")) {
return Optional.of(readPluginInfo(in));
}

if (entry.getName().equals("plugin.yml") || entry.getName().equals("bungee.yml")) {
foundBungeeBukkitPluginFile = true;
}
}

if (entry.getName().equals("plugin.yml") || entry.getName().equals("bungee.yml")) {
foundBungeeBukkitPluginFile = true;
if (foundBungeeBukkitPluginFile) {
throw new InvalidPluginException("The plugin file " + source.getFileName() + " appears to "
+ "be a Bukkit or BungeeCord plugin. Velocity does not support Bukkit or BungeeCord "
+ "plugins.");
}
}

if (foundBungeeBukkitPluginFile) {
throw new InvalidPluginException("The plugin file " + source.getFileName() + " appears to "
+ "be a Bukkit or BungeeCord plugin. Velocity does not support Bukkit or BungeeCord "
+ "plugins.");
return Optional.empty();
}
} else {
Path velocityPluginPath = source.resolve("velocity-plugin.json");
if (Files.exists(velocityPluginPath)) {
try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(velocityPluginPath))) {
return Optional.of(readPluginInfo(in));
}
}

return Optional.empty();
}
}

private SerializedPluginDescription readPluginInfo(InputStream in) throws IOException {
try (Reader pluginInfoReader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
return VelocityServer.GENERAL_GSON.fromJson(pluginInfoReader,
SerializedPluginDescription.class);
}
}

private VelocityPluginDescription createCandidateDescription(
SerializedPluginDescription description,
Path source) {
Expand Down

0 comments on commit 37887a0

Please sign in to comment.