diff --git a/build.gradle b/build.gradle index 0726b8a6..c10f4b6b 100644 --- a/build.gradle +++ b/build.gradle @@ -108,7 +108,7 @@ dependencies { // tinyfile management implementation ('dev.architectury:tiny-remapper:1.1.0') - implementation ('dev.architectury:mappings-layers-core:1.2.7') + implementation ('dev.architectury:mappings-layers-core:1.3.8') implementation ('net.fabricmc:tiny-mappings-parser:0.3.0+build.17') implementation 'net.fabricmc:access-widener:1.0.0' diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingSpecBuilder.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingSpecBuilder.java index 98582f2c..035dcd06 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingSpecBuilder.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingSpecBuilder.java @@ -32,6 +32,7 @@ import org.gradle.api.Action; import org.jetbrains.annotations.Nullable; import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.configuration.providers.mappings.crane.CraneMappingsSpec; import net.fabricmc.loom.configuration.providers.mappings.intermediary.IntermediaryMappingsSpec; import net.fabricmc.loom.configuration.providers.mappings.mojmap.MojangMappingsSpec; import net.fabricmc.loom.configuration.providers.mappings.parchment.ParchmentMappingsSpecBuilder; @@ -62,6 +63,11 @@ public class LayeredMappingSpecBuilder { return this; } + public LayeredMappingSpecBuilder crane(String mavenNotation) { + layers.add(new CraneMappingsSpec(mavenNotation)); + return this; + } + public LayeredMappingSpec build() { List> builtLayers = new LinkedList<>(); // Intermediary is always the base layer diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/crane/CraneMappingLayer.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/crane/CraneMappingLayer.java new file mode 100644 index 00000000..65fa8bd4 --- /dev/null +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/crane/CraneMappingLayer.java @@ -0,0 +1,92 @@ +package net.fabricmc.loom.configuration.providers.mappings.crane; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Collections; + +import dev.architectury.mappingslayers.api.mutable.MutableClassDef; +import dev.architectury.mappingslayers.api.mutable.MutableFieldDef; +import dev.architectury.mappingslayers.api.mutable.MutableMethodDef; +import dev.architectury.mappingslayers.api.mutable.MutableParameterDef; +import dev.architectury.mappingslayers.api.mutable.MutableTinyTree; +import dev.architectury.mappingslayers.api.utils.MappingsUtils; +import org.apache.commons.io.IOUtils; + +import net.fabricmc.loom.configuration.providers.mappings.MappingLayer; +import net.fabricmc.loom.util.FileSystemUtil; +import net.fabricmc.mappingio.MappedElementKind; +import net.fabricmc.mappingio.MappingVisitor; + +public record CraneMappingLayer(File craneJar) implements MappingLayer { + private static final String TINY_FILE_NAME = "crane.tiny"; + + @Override + public void visit(MappingVisitor visitor) throws IOException { + try (FileSystemUtil.FileSystemDelegate fs = FileSystemUtil.getJarFileSystem(craneJar().toPath(), false)) { + try (BufferedReader reader = Files.newBufferedReader(fs.get().getPath(TINY_FILE_NAME), StandardCharsets.UTF_8)) { + // can't use this, it requires 2 namespaces + // Tiny2Reader.read(reader, mappingVisitor); + MutableTinyTree tree = MappingsUtils.deserializeFromString(IOUtils.toString(reader)); + + do { + if (visitor.visitHeader()) { + visitor.visitNamespaces(tree.getMetadata().getNamespaces().get(0), Collections.emptyList()); + } + + if (visitor.visitContent()) { + for (MutableClassDef classDef : tree.getClassesMutable()) { + if (visitor.visitClass(classDef.getName(0))) { + if (!visitor.visitElementContent(MappedElementKind.CLASS)) { + return; + } + + for (MutableFieldDef fieldDef : classDef.getFieldsMutable()) { + if (visitor.visitField(fieldDef.getName(0), fieldDef.getDescriptor(0))) { + if (!visitor.visitElementContent(MappedElementKind.FIELD)) { + return; + } + + if (fieldDef.getComment() != null) { + visitor.visitComment(MappedElementKind.FIELD, fieldDef.getComment()); + } + } + } + + for (MutableMethodDef methodDef : classDef.getMethodsMutable()) { + if (visitor.visitMethod(methodDef.getName(0), methodDef.getDescriptor(0))) { + if (!visitor.visitElementContent(MappedElementKind.METHOD)) { + return; + } + + for (MutableParameterDef parameterDef : methodDef.getParametersMutable()) { + if (visitor.visitMethodArg(parameterDef.getLocalVariableIndex(), parameterDef.getLocalVariableIndex(), parameterDef.getName(0))) { + if (!visitor.visitElementContent(MappedElementKind.METHOD_ARG)) { + return; + } + + if (parameterDef.getComment() != null) { + visitor.visitComment(MappedElementKind.METHOD_ARG, parameterDef.getComment()); + } + } + } + + if (methodDef.getComment() != null) { + visitor.visitComment(MappedElementKind.METHOD, methodDef.getComment()); + } + } + } + + if (classDef.getComment() != null) { + visitor.visitComment(MappedElementKind.FIELD, classDef.getComment()); + } + } + } + } + } while (!visitor.visitEnd()); + } + } + } +} diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/crane/CraneMappingsSpec.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/crane/CraneMappingsSpec.java new file mode 100644 index 00000000..179bcc92 --- /dev/null +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/crane/CraneMappingsSpec.java @@ -0,0 +1,35 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.configuration.providers.mappings.crane; + +import net.fabricmc.loom.configuration.providers.mappings.MappingContext; +import net.fabricmc.loom.configuration.providers.mappings.MappingsSpec; + +public record CraneMappingsSpec(String mavenNotation) implements MappingsSpec { + @Override + public CraneMappingLayer createLayer(MappingContext context) { + return new CraneMappingLayer(context.mavenFile(mavenNotation())); + } +}