-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manually download netty-codec-http when missing (fixes #29)
- Loading branch information
1 parent
3d8b7a9
commit 78f717f
Showing
6 changed files
with
92 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
allprojects { | ||
group 'com.azuriom' | ||
version '1.3.1' | ||
version '1.3.2' | ||
} | ||
|
||
subprojects { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
bukkit/src/main/java/com/azuriom/azlink/bukkit/injector/NettyLibraryLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.azuriom.azlink.bukkit.injector; | ||
|
||
import com.azuriom.azlink.common.AzLinkPlugin; | ||
import io.netty.util.Version; | ||
|
||
import java.io.InputStream; | ||
import java.lang.reflect.Field; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Since Spigot 1.19, netty-codec-http is no longer included, so we need to manually load it. | ||
* <p> | ||
* We are not including it in the plugin.yml 'libraries' list, because it's not needed for all Minecraft versions, | ||
* and the Netty version changes between Minecraft versions. | ||
*/ | ||
public class NettyLibraryLoader { | ||
|
||
private static final String MAVEN_CENTRAL = "https://repo1.maven.org/maven2/%s/%s/%s/%s-%s.jar"; | ||
|
||
private final AzLinkPlugin plugin; | ||
private final Path libsFolder; | ||
|
||
public NettyLibraryLoader(AzLinkPlugin plugin) { | ||
this.plugin = plugin; | ||
this.libsFolder = plugin.getPlatform().getDataDirectory().resolve("libs"); | ||
} | ||
|
||
public void loadRequiredLibraries() throws Exception { | ||
try { | ||
Class.forName("io.netty.handler.codec.http.HttpServerCodec"); | ||
// netty-codec-http is already loaded, all good | ||
return; | ||
} catch (ClassNotFoundException e) { | ||
// manually load netty-codec-http below | ||
} | ||
|
||
this.plugin.getLogger().info("Loading netty-codec-http..."); | ||
|
||
loadLibrary("io.netty", "netty-codec-http", identifyNettyVersion()); | ||
|
||
this.plugin.getLogger().info("Loaded netty-codec-http successfully."); | ||
} | ||
|
||
private void loadLibrary(String groupId, String artifactId, String version) throws Exception { | ||
Path jar = this.libsFolder.resolve(artifactId + "-" + version + ".jar"); | ||
|
||
if (!Files.exists(jar)) { | ||
Files.createDirectory(jar.getParent()); | ||
|
||
this.plugin.getLogger().warn("Downloading " + artifactId + " v" + version + "..."); | ||
|
||
URL url = new URL(String.format(MAVEN_CENTRAL, groupId.replace('.', '/'), artifactId, version, artifactId, version)); | ||
|
||
try (InputStream in = url.openStream()) { | ||
Files.copy(in, jar); | ||
} | ||
|
||
this.plugin.getLogger().info("Successfully downloaded " + artifactId + "."); | ||
} | ||
|
||
URL[] urls = {jar.toUri().toURL()}; | ||
ClassLoader pluginClassLoader = plugin.getClass().getClassLoader(); | ||
Field classLoaderField = pluginClassLoader.getClass().getDeclaredField("libraryLoader"); | ||
ClassLoader libraryLoader = new URLClassLoader(urls, pluginClassLoader.getParent()); | ||
|
||
classLoaderField.setAccessible(true); | ||
classLoaderField.set(pluginClassLoader, libraryLoader); | ||
} | ||
|
||
private String identifyNettyVersion() { | ||
Version version = Version.identify().get("netty-codec"); | ||
|
||
return version != null ? version.artifactVersion() : "4.1.97.Final"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters