diff options
author | Luck <git@lucko.me> | 2022-02-12 00:32:56 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2022-02-12 00:32:56 +0000 |
commit | 8bfad57b70746f444c9fe74e6be3d078ae4da247 (patch) | |
tree | 6e0e39a70a258221ff48bc1795185f1fc0b561de /spark-fabric/src/main/java/me/lucko/spark/fabric | |
parent | f54a31e9d4756c12836d5e42c31d3d100546533d (diff) | |
download | spark-8bfad57b70746f444c9fe74e6be3d078ae4da247.tar.gz spark-8bfad57b70746f444c9fe74e6be3d078ae4da247.tar.bz2 spark-8bfad57b70746f444c9fe74e6be3d078ae4da247.zip |
Improve Sponge and Fabric class source lookups
Diffstat (limited to 'spark-fabric/src/main/java/me/lucko/spark/fabric')
-rw-r--r-- | spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java index fa59079..7030680 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java @@ -20,25 +20,47 @@ package me.lucko.spark.fabric; +import com.google.common.collect.ImmutableMap; + import me.lucko.spark.common.util.ClassSourceLookup; import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.ModContainer; import java.nio.file.Path; +import java.util.Collection; +import java.util.Map; public class FabricClassSourceLookup extends ClassSourceLookup.ByCodeSource { private final Path modsDirectory; + private final Map<Path, String> pathToModMap; public FabricClassSourceLookup() { - this.modsDirectory = FabricLoader.getInstance().getGameDir().resolve("mods").toAbsolutePath().normalize(); + FabricLoader loader = FabricLoader.getInstance(); + this.modsDirectory = loader.getGameDir().resolve("mods").toAbsolutePath().normalize(); + this.pathToModMap = constructPathToModIdMap(loader.getAllMods()); } @Override public String identifyFile(Path path) { + String id = this.pathToModMap.get(path); + if (id != null) { + return id; + } + if (!path.startsWith(this.modsDirectory)) { return null; } return super.identifyFileName(this.modsDirectory.relativize(path).toString()); } + + private static Map<Path, String> constructPathToModIdMap(Collection<ModContainer> mods) { + ImmutableMap.Builder<Path, String> builder = ImmutableMap.builder(); + for (ModContainer mod : mods) { + Path path = mod.getRootPath().toAbsolutePath().normalize(); + builder.put(path, mod.getMetadata().getId()); + } + return builder.build(); + } } |