diff options
Diffstat (limited to 'spark-fabric/src')
-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(); + } } |