Fix manifest util for bundler jars

This commit is contained in:
Jason Penilla 2021-11-26 17:03:47 -08:00 committed by MiniDigger | Martin
parent 7abff79737
commit 251b568882

View File

@ -6,22 +6,21 @@ Subject: [PATCH] Add Git information to version command/on startup
diff --git a/src/main/java/io/papermc/paper/util/JarManifests.java b/src/main/java/io/papermc/paper/util/JarManifests.java diff --git a/src/main/java/io/papermc/paper/util/JarManifests.java b/src/main/java/io/papermc/paper/util/JarManifests.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..2b06b17e20826fb95c9ad58101b4102308ea4b29 index 0000000000000000000000000000000000000000..2a49f4198b07b3ecdc320c330c58eab2f15c5c74
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/util/JarManifests.java +++ b/src/main/java/io/papermc/paper/util/JarManifests.java
@@ -0,0 +1,34 @@ @@ -0,0 +1,36 @@
+package io.papermc.paper.util; +package io.papermc.paper.util;
+ +
+import java.io.IOException; +import java.io.IOException;
+import java.io.InputStream; +import java.io.InputStream;
+import java.net.URL;
+import java.util.Collections; +import java.util.Collections;
+import java.util.Map; +import java.util.Map;
+import java.util.WeakHashMap; +import java.util.WeakHashMap;
+import java.util.jar.Manifest; +import java.util.jar.Manifest;
+
+import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+ +
+@ApiStatus.Internal +@ApiStatus.Internal
+public final class JarManifests { +public final class JarManifests {
@ -30,22 +29,25 @@ index 0000000000000000000000000000000000000000..2b06b17e20826fb95c9ad58101b41023
+ +
+ private static final Map<ClassLoader, Manifest> MANIFESTS = Collections.synchronizedMap(new WeakHashMap<>()); + private static final Map<ClassLoader, Manifest> MANIFESTS = Collections.synchronizedMap(new WeakHashMap<>());
+ +
+ public static @NotNull Manifest manifest(final @NotNull ClassLoader loader) { + public static @NotNull Manifest manifest(final @NotNull Class<?> clazz) {
+ return MANIFESTS.computeIfAbsent(loader, classLoader -> { + return MANIFESTS.computeIfAbsent(clazz.getClassLoader(), classLoader -> {
+ final @Nullable InputStream stream = classLoader.getResourceAsStream("META-INF/MANIFEST.MF"); + final String classLocation = "/" + clazz.getName().replace(".", "/") + ".class";
+ if (stream == null) { + final URL resource = clazz.getResource(classLocation);
+ throw new IllegalArgumentException("Provided ClassLoader does not have a manifest file in the correct location!"); + if (resource == null) {
+ throw new IllegalStateException("Could not find class file for loaded class: " + clazz.getName());
+ } + }
+ try (stream) { + final String classFilePath = resource.toString().replace("\\", "/");
+ final String archivePath = classFilePath.substring(0, classFilePath.length() - classLocation.length());
+ try (final InputStream stream = new URL(archivePath + "/META-INF/MANIFEST.MF").openStream()) {
+ return new Manifest(stream); + return new Manifest(stream);
+ } catch (final IOException ex) { + } catch (final IOException ex) {
+ throw new RuntimeException("Failed to read manifest."); + throw new RuntimeException("Failed to locate or read manifest", ex);
+ } + }
+ }); + });
+ } + }
+} +}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 0521781a48d326c0a4a01b920188e9ce00b51ef0..7868a54eec71f7b31d7fabf3288eb2dc62649e92 100644 index 0521781a48d326c0a4a01b920188e9ce00b51ef0..5a3dbcc9d67ba7f22c12e8854a36bb2076d2d81a 100644
--- a/src/main/java/org/bukkit/Bukkit.java --- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java +++ b/src/main/java/org/bukkit/Bukkit.java
@@ -51,6 +51,7 @@ import org.bukkit.util.CachedServerIcon; @@ -51,6 +51,7 @@ import org.bukkit.util.CachedServerIcon;
@ -71,7 +73,7 @@ index 0521781a48d326c0a4a01b920188e9ce00b51ef0..7868a54eec71f7b31d7fabf3288eb2dc
+ */ + */
+ @NotNull + @NotNull
+ public static String getVersionMessage() { + public static String getVersionMessage() {
+ final var manifest = JarManifests.manifest(Bukkit.getServer().getClass().getClassLoader()); + final var manifest = JarManifests.manifest(Bukkit.getServer().getClass());
+ final String gitBranch = manifest.getMainAttributes().getValue("Git-Branch"); + final String gitBranch = manifest.getMainAttributes().getValue("Git-Branch");
+ final String gitCommit = manifest.getMainAttributes().getValue("Git-Commit"); + final String gitCommit = manifest.getMainAttributes().getValue("Git-Commit");
+ String branchMsg = " on " + gitBranch; + String branchMsg = " on " + gitBranch;