diff options
author | Luck <git@lucko.me> | 2022-02-12 00:14:02 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2022-02-12 00:14:02 +0000 |
commit | f54a31e9d4756c12836d5e42c31d3d100546533d (patch) | |
tree | 1245aa1f57dc59c3921f2b81dd980134dadfcbe3 | |
parent | 8a39ad426ca4fe1f5ef39fc5b0b538937802a001 (diff) | |
download | spark-f54a31e9d4756c12836d5e42c31d3d100546533d.tar.gz spark-f54a31e9d4756c12836d5e42c31d3d100546533d.tar.bz2 spark-f54a31e9d4756c12836d5e42c31d3d100546533d.zip |
Add class source lookups for Sponge8
3 files changed, 70 insertions, 5 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java b/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java index 42a04f7..bd9ec37 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java @@ -26,6 +26,7 @@ import me.lucko.spark.common.sampler.node.ThreadNode; import org.checkerframework.checker.nullness.qual.Nullable; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; @@ -87,8 +88,22 @@ public interface ClassSourceLookup { */ interface ByUrl extends ClassSourceLookup { - default String identifyUrl(URL url) throws URISyntaxException { - return url.getProtocol().equals("file") ? identifyFile(Paths.get(url.toURI())) : null; + default String identifyUrl(URL url) throws URISyntaxException, MalformedURLException { + Path path = null; + + String protocol = url.getProtocol(); + if (protocol.equals("file")) { + path = Paths.get(url.toURI()); + } else if (protocol.equals("jar")) { + URL innerUrl = new URL(url.getPath()); + path = Paths.get(innerUrl.getPath().split("!")[0]); + } + + if (path != null) { + return identifyFile(path.toAbsolutePath().normalize()); + } + + return null; } default String identifyFile(Path path) { @@ -123,7 +138,7 @@ public interface ClassSourceLookup { */ class ByCodeSource implements ClassSourceLookup, ByUrl { @Override - public @Nullable String identify(Class<?> clazz) throws URISyntaxException { + public @Nullable String identify(Class<?> clazz) throws URISyntaxException, MalformedURLException { ProtectionDomain protectionDomain = clazz.getProtectionDomain(); if (protectionDomain == null) { return null; @@ -148,12 +163,12 @@ public interface ClassSourceLookup { static Visitor createVisitor(ClassSourceLookup lookup) { if (lookup == ClassSourceLookup.NO_OP) { - return NoOpVistitor.INSTANCE; // don't bother! + return NoOpVisitor.INSTANCE; // don't bother! } return new VisitorImpl(lookup); } - enum NoOpVistitor implements Visitor { + enum NoOpVisitor implements Visitor { INSTANCE; @Override diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8ClassSourceLookup.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8ClassSourceLookup.java new file mode 100644 index 0000000..60ebb60 --- /dev/null +++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8ClassSourceLookup.java @@ -0,0 +1,44 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) 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 <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.sponge; + +import me.lucko.spark.common.util.ClassSourceLookup; + +import org.spongepowered.api.Game; + +import java.nio.file.Path; + +public class Sponge8ClassSourceLookup extends ClassSourceLookup.ByCodeSource { + private final Path modsDirectory; + + public Sponge8ClassSourceLookup(Game game) { + this.modsDirectory = game.gameDirectory().resolve("mods").toAbsolutePath().normalize(); + } + + @Override + public String identifyFile(Path path) { + if (!path.startsWith(this.modsDirectory)) { + return null; + } + + return super.identifyFileName(this.modsDirectory.relativize(path).toString()); + } +} diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java index 6eb2674..e867a75 100644 --- a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java +++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java @@ -29,6 +29,7 @@ import me.lucko.spark.common.monitor.ping.PlayerPingProvider; import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.sampler.ThreadDumper; import me.lucko.spark.common.tick.TickHook; +import me.lucko.spark.common.util.ClassSourceLookup; import net.kyori.adventure.text.Component; @@ -148,6 +149,11 @@ public class Sponge8SparkPlugin implements SparkPlugin { } @Override + public ClassSourceLookup createClassSourceLookup() { + return new Sponge8ClassSourceLookup(this.game); + } + + @Override public PlayerPingProvider createPlayerPingProvider() { if (this.game.isServerAvailable()) { return new Sponge8PlayerPingProvider(this.game.server()); |