From 692caeea8a833b9ba4c5ca87b40f29f10515c8b8 Mon Sep 17 00:00:00 2001 From: Octavia Togami Date: Mon, 10 Feb 2020 20:11:08 -0500 Subject: [PATCH] Read WE version from manifest ourselves (cherry picked from commit 947775b5e54aa9dd89d68ee0b382939796301902) --- buildSrc/src/main/kotlin/PlatformConfig.kt | 20 ++++- worldedit-bukkit/build.gradle.kts | 7 +- .../java/com/sk89q/worldedit/WorldEdit.java | 18 +---- .../sk89q/worldedit/WorldEditManifest.java | 81 +++++++++++++++++++ worldedit-fabric/build.gradle.kts | 21 ++--- worldedit-forge/build.gradle.kts | 6 +- worldedit-sponge/build.gradle.kts | 9 +-- 7 files changed, 111 insertions(+), 51 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/WorldEditManifest.java diff --git a/buildSrc/src/main/kotlin/PlatformConfig.kt b/buildSrc/src/main/kotlin/PlatformConfig.kt index 0c573871c..4175d9362 100644 --- a/buildSrc/src/main/kotlin/PlatformConfig.kt +++ b/buildSrc/src/main/kotlin/PlatformConfig.kt @@ -120,7 +120,19 @@ fun Project.applyShadowConfiguration() { } } -val CLASSPATH = listOf("truezip", "truevfs", "js") - .map { "$it.jar" } - .flatMap { listOf(it, "WorldEdit/$it") } - .joinToString(separator = " ") +private val CLASSPATH = listOf("truezip", "truevfs", "js") + .map { "$it.jar" } + .flatMap { listOf(it, "WorldEdit/$it") } + .joinToString(separator = " ") + +fun Project.addJarManifest(includeClasspath: Boolean = false) { + tasks.named("jar") { + val attributes = mutableMapOf( + "WorldEdit-Version" to project(":worldedit-core").version + ) + if (includeClasspath) { + attributes["Class-Path"] = CLASSPATH + } + manifest.attributes(attributes) + } +} diff --git a/worldedit-bukkit/build.gradle.kts b/worldedit-bukkit/build.gradle.kts index 07fab21be..9bb48edf5 100644 --- a/worldedit-bukkit/build.gradle.kts +++ b/worldedit-bukkit/build.gradle.kts @@ -75,12 +75,7 @@ tasks.named("processResources") { exclude("**/worldedit-adapters.jar") } -tasks.named("jar") { - manifest { - attributes("Class-Path" to CLASSPATH, - "WorldEdit-Version" to project.version) - } -} +addJarManifest(includeClasspath = true) tasks.named("shadowJar") { from(zipTree("src/main/resources/worldedit-adapters.jar").matching { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java index d9fdb2c5e..ac5b9d04b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -750,23 +750,9 @@ public final class WorldEdit { return version; } - Package p = WorldEdit.class.getPackage(); + WorldEditManifest manifest = WorldEditManifest.load(); - if (p == null) { - p = Package.getPackage("com.sk89q.worldedit"); - } - - if (p == null) { - version = "(unknown)"; - } else { - version = p.getImplementationVersion(); - - if (version == null) { - version = "(unknown)"; - } - } - - return version; + return version = manifest.getWorldEditVersion(); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEditManifest.java b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEditManifest.java new file mode 100644 index 000000000..febe7cac4 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEditManifest.java @@ -0,0 +1,81 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.net.JarURLConnection; +import java.net.URL; +import java.util.function.Supplier; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +/** + * Represents WorldEdit info from the MANIFEST.MF file. + */ +public class WorldEditManifest { + + public static final String WORLD_EDIT_VERSION = "WorldEdit-Version"; + + public static WorldEditManifest load() { + Attributes attributes = readAttributes(); + return new WorldEditManifest( + readAttribute(attributes, WORLD_EDIT_VERSION, () -> "(unknown)") + ); + } + + private static @Nullable Attributes readAttributes() { + Class clazz = WorldEditManifest.class; + String className = clazz.getSimpleName() + ".class"; + String classPath = clazz.getResource(className).toString(); + if (!classPath.startsWith("jar")) { + return null; + } + + try { + URL url = new URL(classPath); + JarURLConnection jarConnection = (JarURLConnection) url.openConnection(); + Manifest manifest = jarConnection.getManifest(); + return manifest.getMainAttributes(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private static String readAttribute(@Nullable Attributes attributes, String name, + Supplier defaultAction) { + if (attributes == null) { + return defaultAction.get(); + } + String value = attributes.getValue(name); + return value != null ? value : defaultAction.get(); + } + + private final String worldEditVersion; + + private WorldEditManifest(String worldEditVersion) { + this.worldEditVersion = worldEditVersion; + } + + public String getWorldEditVersion() { + return worldEditVersion; + } +} diff --git a/worldedit-fabric/build.gradle.kts b/worldedit-fabric/build.gradle.kts index fa38ce619..de69b0b00 100644 --- a/worldedit-fabric/build.gradle.kts +++ b/worldedit-fabric/build.gradle.kts @@ -6,9 +6,9 @@ applyShadowConfiguration() apply(plugin = "fabric-loom") -val minecraftVersion = "1.14.4" -val yarnMappings = "1.14.4+build.12" -val loaderVersion = "0.6.2+build.166" +val minecraftVersion = "1.15.2" +val yarnMappings = "1.15.2+build.8:v2" +val loaderVersion = "0.7.6+build.180" configurations.all { resolutionStrategy { @@ -25,10 +25,10 @@ dependencies { "modCompile"("net.fabricmc:fabric-loader:$loaderVersion") listOf( - "net.fabricmc.fabric-api:fabric-api-base:0.1.0+2983bc0442", - "net.fabricmc.fabric-api:fabric-events-interaction-v0:0.1.1+591e97ae42", - "net.fabricmc.fabric-api:fabric-events-lifecycle-v0:0.1.1+591e97ae42", - "net.fabricmc.fabric-api:fabric-networking-v0:0.1.3+591e97ae42" + "net.fabricmc.fabric-api:fabric-api-base:0.1.2+28f8190f42", + "net.fabricmc.fabric-api:fabric-events-interaction-v0:0.2.6+12515ed975", + "net.fabricmc.fabric-api:fabric-events-lifecycle-v0:0.1.2+b7f9825de8", + "net.fabricmc.fabric-api:fabric-networking-v0:0.1.7+12515ed975" ).forEach { "include"(it) "modImplementation"(it) @@ -61,12 +61,7 @@ tasks.named("processResources") { } } -tasks.named("jar") { - manifest { - attributes("Class-Path" to "truezip.jar WorldEdit/truezip.jar js.jar WorldEdit/js.jar", - "WorldEdit-Version" to project.version) - } -} +addJarManifest(includeClasspath = true) tasks.named("shadowJar") { archiveClassifier.set("dist-dev") diff --git a/worldedit-forge/build.gradle.kts b/worldedit-forge/build.gradle.kts index b339aa62b..d060fba6e 100644 --- a/worldedit-forge/build.gradle.kts +++ b/worldedit-forge/build.gradle.kts @@ -77,11 +77,7 @@ tasks.named("processResources") { } } -tasks.named("jar") { - manifest { - attributes("WorldEdit-Version" to project.version) - } -} +addJarManifest(includeClasspath = false) tasks.named("shadowJar") { dependencies { diff --git a/worldedit-sponge/build.gradle.kts b/worldedit-sponge/build.gradle.kts index 1c5fa9453..d80932652 100644 --- a/worldedit-sponge/build.gradle.kts +++ b/worldedit-sponge/build.gradle.kts @@ -25,12 +25,7 @@ sponge { } } -tasks.named("jar") { - manifest { - attributes("Class-Path" to "truezip.jar WorldEdit/truezip.jar js.jar WorldEdit/js.jar", - "WorldEdit-Version" to project.version) - } -} +addJarManifest(includeClasspath = true) tasks.named("shadowJar") { dependencies { @@ -50,4 +45,4 @@ if (project.hasProperty("signing")) { tasks.named("build").configure { dependsOn("signShadowJar") } -} \ No newline at end of file +}