diff options
author | shedaniel <daniel@shedaniel.me> | 2021-09-29 01:28:29 +0800 |
---|---|---|
committer | shedaniel <daniel@shedaniel.me> | 2021-09-29 01:28:29 +0800 |
commit | b4fd2e10f5e35881585dab8f84c6548e37899c2f (patch) | |
tree | cc52f963b11e7cd0daa7a0ebf2108cd1f5763f18 /src/main/java | |
parent | 357656ca3e657baa7fd9db175db82659d66869cb (diff) | |
parent | c6f51f1dd282b6d2f26f7cecfc3b0bcfe369b0ca (diff) | |
download | architectury-loom-b4fd2e10f5e35881585dab8f84c6548e37899c2f.tar.gz architectury-loom-b4fd2e10f5e35881585dab8f84c6548e37899c2f.tar.bz2 architectury-loom-b4fd2e10f5e35881585dab8f84c6548e37899c2f.zip |
Fix merge conflicts
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'src/main/java')
11 files changed, 347 insertions, 44 deletions
diff --git a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java index cf0b5424..b9876788 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java +++ b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java @@ -74,7 +74,7 @@ public class LoomGradlePlugin implements BootstrappedPlugin { project.getLogger().lifecycle("You are using an unstable version of Architectury Loom! Please report any issues found!"); } - refreshDeps = project.getGradle().getStartParameter().isRefreshDependencies() || "true".equals(System.getProperty("loom.refresh")); + refreshDeps = project.getGradle().getStartParameter().isRefreshDependencies() || Boolean.getBoolean("loom.refresh"); if (refreshDeps) { project.getLogger().lifecycle("Refresh dependencies is in use, loom will be significantly slower."); diff --git a/src/main/java/net/fabricmc/loom/api/mappings/layered/spec/LayeredMappingSpecBuilder.java b/src/main/java/net/fabricmc/loom/api/mappings/layered/spec/LayeredMappingSpecBuilder.java index a93d97c1..ffd4e9ce 100644 --- a/src/main/java/net/fabricmc/loom/api/mappings/layered/spec/LayeredMappingSpecBuilder.java +++ b/src/main/java/net/fabricmc/loom/api/mappings/layered/spec/LayeredMappingSpecBuilder.java @@ -49,4 +49,10 @@ public interface LayeredMappingSpecBuilder { LayeredMappingSpecBuilder parchment(Object object, Action<ParchmentMappingsSpecBuilder> action); LayeredMappingSpecBuilder crane(Object object); + + /** + * Add a signatureFix layer. Reads the @extras/record_signatures.json" file in a jar file such as yarn. + */ + @ApiStatus.Experimental + LayeredMappingSpecBuilder signatureFix(Object object); } diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingSpecBuilderImpl.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingSpecBuilderImpl.java index ce9172b8..6987ea5b 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingSpecBuilderImpl.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingSpecBuilderImpl.java @@ -37,6 +37,7 @@ import net.fabricmc.loom.api.mappings.layered.spec.MappingsSpec; import net.fabricmc.loom.api.mappings.layered.spec.ParchmentMappingsSpecBuilder; import net.fabricmc.loom.api.LoomGradleExtensionAPI; import net.fabricmc.loom.configuration.providers.mappings.crane.CraneMappingsSpec; +import net.fabricmc.loom.configuration.providers.mappings.extras.signatures.SignatureFixesSpec; 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.ParchmentMappingsSpecBuilderImpl; @@ -75,6 +76,11 @@ public class LayeredMappingSpecBuilderImpl implements LayeredMappingSpecBuilder return this; } + @Override + public LayeredMappingSpecBuilder signatureFix(Object object) { + return addLayer(new SignatureFixesSpec(FileSpec.create(object))); + } + public LayeredMappingSpec build() { List<MappingsSpec<?>> builtLayers = new LinkedList<>(); // Intermediary is always the base layer diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingsDependency.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingsDependency.java index 84c9370d..21f1d2fd 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingsDependency.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingsDependency.java @@ -32,6 +32,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Set; @@ -55,6 +57,7 @@ import org.zeroturnaround.zip.ZipUtil; import net.fabricmc.loom.LoomGradlePlugin; import net.fabricmc.loom.api.mappings.layered.MappingContext; +import net.fabricmc.loom.api.mappings.layered.MappingLayer; import net.fabricmc.loom.api.mappings.layered.MappingsNamespace; import net.fabricmc.mappingio.adapter.MappingDstNsReorder; import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch; @@ -84,29 +87,51 @@ public class LayeredMappingsDependency extends AbstractModuleDependency implemen if (!Files.exists(mappingsFile) || LoomGradlePlugin.refreshDeps) { try { var processor = new LayeredMappingsProcessor(layeredMappingSpec); - MemoryMappingTree mappings = processor.getMappings(mappingContext); + List<MappingLayer> layers = processor.resolveLayers(mappingContext); - try (Writer writer = new StringWriter()) { - Tiny2Writer tiny2Writer = new Tiny2Writer(writer, false); + Files.deleteIfExists(mappingsFile); - MappingDstNsReorder nsReorder = new MappingDstNsReorder(tiny2Writer, Collections.singletonList(MappingsNamespace.NAMED.toString())); - MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch(nsReorder, MappingsNamespace.INTERMEDIARY.toString(), true); - mappings.accept(nsSwitch); - - Files.deleteIfExists(mappingsFile); - - ZipUtil.pack(new ZipEntrySource[] { - new ByteSource("mappings/mappings.tiny", writer.toString().getBytes(StandardCharsets.UTF_8)) - }, mappingsFile.toFile()); - } + writeMapping(processor, layers, mappingsFile); + writeSignatureFixes(processor, layers, mappingsFile); } catch (IOException e) { - throw new RuntimeException("Failed to resolve Mojang mappings", e); + throw new RuntimeException("Failed to resolve layered mappings", e); } } return Collections.singleton(mappingsFile.toFile()); } + private void writeMapping(LayeredMappingsProcessor processor, List<MappingLayer> layers, Path mappingsFile) throws IOException { + MemoryMappingTree mappings = processor.getMappings(layers); + + try (Writer writer = new StringWriter()) { + Tiny2Writer tiny2Writer = new Tiny2Writer(writer, false); + + MappingDstNsReorder nsReorder = new MappingDstNsReorder(tiny2Writer, Collections.singletonList(MappingsNamespace.NAMED.toString())); + MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch(nsReorder, MappingsNamespace.INTERMEDIARY.toString(), true); + mappings.accept(nsSwitch); + + ZipUtil.pack(new ZipEntrySource[] { + new ByteSource("mappings/mappings.tiny", writer.toString().getBytes(StandardCharsets.UTF_8)) + }, mappingsFile.toFile()); + } + } + + private void writeSignatureFixes(LayeredMappingsProcessor processor, List<MappingLayer> layers, Path mappingsFile) throws IOException { + Map<String, String> signatureFixes = processor.getSignatureFixes(layers); + + if (signatureFixes == null) { + return; + } + + byte[] data = LoomGradlePlugin.OBJECT_MAPPER.writeValueAsString(signatureFixes).getBytes(StandardCharsets.UTF_8); + + ZipUtil.addEntry( + mappingsFile.toFile(), + new ByteSource("extras/record_signatures.json", data) + ); + } + @Override public Set<File> resolve(boolean transitive) { return resolve(); diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingsProcessor.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingsProcessor.java index b14902c5..f2edfb2b 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingsProcessor.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/LayeredMappingsProcessor.java @@ -26,12 +26,19 @@ package net.fabricmc.loom.configuration.providers.mappings; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; import java.util.List; +import java.util.Map; + +import org.jetbrains.annotations.Nullable; import net.fabricmc.loom.api.mappings.layered.MappingContext; import net.fabricmc.loom.api.mappings.layered.MappingLayer; import net.fabricmc.loom.api.mappings.layered.MappingsNamespace; import net.fabricmc.loom.api.mappings.layered.spec.MappingsSpec; +import net.fabricmc.loom.configuration.providers.mappings.extras.signatures.SignatureFixesLayer; import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch; import net.fabricmc.mappingio.tree.MemoryMappingTree; @@ -42,9 +49,8 @@ public class LayeredMappingsProcessor { this.layeredMappingSpec = spec; } - public MemoryMappingTree getMappings(MappingContext context) throws IOException { - MemoryMappingTree mappingTree = new MemoryMappingTree(); - + public List<MappingLayer> resolveLayers(MappingContext context) { + List<MappingLayer> layers = new LinkedList<>(); List<Class<? extends MappingLayer>> visitedLayers = new ArrayList<>(); for (MappingsSpec<?> spec : layeredMappingSpec.layers()) { @@ -56,8 +62,17 @@ public class LayeredMappingsProcessor { } } + layers.add(layer); visitedLayers.add(layer.getClass()); + } + + return Collections.unmodifiableList(layers); + } + + public MemoryMappingTree getMappings(List<MappingLayer> layers) throws IOException { + MemoryMappingTree mappingTree = new MemoryMappingTree(); + for (MappingLayer layer : layers) { // We have to rebuild a new tree to work on when a layer doesnt merge into layered boolean rebuild = layer.getSourceNamespace() != MappingsNamespace.NAMED; MemoryMappingTree workingTree; @@ -90,4 +105,21 @@ public class LayeredMappingsProcessor { return mappingTree; } + + @Nullable + public Map<String, String> getSignatureFixes(List<MappingLayer> layers) { + Map<String, String> signatureFixes = new HashMap<>(); + + for (MappingLayer layer : layers) { + if (layer instanceof SignatureFixesLayer signatureFixesLayer) { + signatureFixes.putAll(signatureFixesLayer.getSignatureFixes()); + } + } + + if (signatureFixes.isEmpty()) { + return null; + } + + return Collections.unmodifiableMap(signatureFixes); + } } diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/MappingsProviderImpl.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/MappingsProviderImpl.java index 8adfc28b..add9af99 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/MappingsProviderImpl.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/MappingsProviderImpl.java @@ -27,6 +27,7 @@ package net.fabricmc.loom.configuration.providers.mappings; import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.Reader; import java.io.UncheckedIOException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -41,6 +42,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.Map; import java.util.function.Consumer; import java.util.regex.Pattern; @@ -50,6 +52,7 @@ import com.google.gson.JsonObject; import org.apache.tools.ant.util.StringUtils; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; +import org.jetbrains.annotations.Nullable; import org.zeroturnaround.zip.FileSource; import org.zeroturnaround.zip.ZipEntrySource; import org.zeroturnaround.zip.ZipUtil; @@ -109,6 +112,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings private UnpickMetadata unpickMetadata; private MemoryMappingTree mappingTree; private MemoryMappingTree mappingTreeWithSrg; + private Map<String, String> signatureFixes; public MappingsProviderImpl(Project project) { super(project); @@ -141,7 +145,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings storeMappings(getProject(), minecraftProvider, mappingsJar.toPath(), postPopulationScheduler); } else { try (FileSystem fileSystem = FileSystems.newFileSystem(mappingsJar.toPath(), (ClassLoader) null)) { - extractUnpickDefinitions(fileSystem, unpickDefinitions); + extractExtras(fileSystem); } } @@ -299,7 +303,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings try (FileSystem fileSystem = FileSystems.newFileSystem(yarnJar, (ClassLoader) null)) { extractMappings(fileSystem, baseTinyMappings); - extractUnpickDefinitions(fileSystem, unpickDefinitions); + extractExtras(fileSystem); } if (areMappingsMergedV2(baseTinyMappings)) { @@ -388,7 +392,12 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings Files.copy(jar.getPath("mappings/mappings.tiny"), extractTo, StandardCopyOption.REPLACE_EXISTING); } - private void extractUnpickDefinitions(FileSystem jar, Path extractTo) throws IOException { + private void extractExtras(FileSystem jar) throws IOException { + extractUnpickDefinitions(jar); + extractSignatureFixes(jar); + } + + private void extractUnpickDefinitions(FileSystem jar) throws IOException { Path unpickPath = jar.getPath("extras/definitions.unpick"); Path unpickMetadataPath = jar.getPath("extras/unpick.json"); @@ -396,12 +405,25 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings return; } - Files.copy(unpickPath, extractTo, StandardCopyOption.REPLACE_EXISTING); + Files.copy(unpickPath, unpickDefinitions, StandardCopyOption.REPLACE_EXISTING); unpickMetadata = parseUnpickMetadata(unpickMetadataPath); hasUnpickDefinitions = true; } + private void extractSignatureFixes(FileSystem jar) throws IOException { + Path recordSignaturesJsonPath = jar.getPath("extras/record_signatures.json"); + + if (!Files.exists(recordSignaturesJsonPath)) { + return; + } + + try (Reader reader = Files.newBufferedReader(recordSignaturesJsonPath, StandardCharsets.UTF_8)) { + //noinspection unchecked + signatureFixes = LoomGradlePlugin.OBJECT_MAPPER.readValue(reader, Map.class); + } + } + private UnpickMetadata parseUnpickMetadata(Path input) throws IOException { JsonObject jsonObject = LoomGradlePlugin.GSON.fromJson(Files.readString(input), JsonObject.class); @@ -599,6 +621,11 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings return hasUnpickDefinitions; } + @Nullable + public Map<String, String> getSignatureFixes() { + return signatureFixes; + } + @Override public File intermediaryTinyFile() { try { diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesLayer.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesLayer.java new file mode 100644 index 00000000..845eed60 --- /dev/null +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesLayer.java @@ -0,0 +1,34 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2021 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.extras.signatures; + +import java.util.Map; + +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Experimental +public interface SignatureFixesLayer { + Map<String, String> getSignatureFixes(); +} diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesLayerImpl.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesLayerImpl.java new file mode 100644 index 00000000..5f906384 --- /dev/null +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesLayerImpl.java @@ -0,0 +1,64 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2021 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.extras.signatures; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.file.Path; +import java.util.Map; +import java.util.Objects; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.jetbrains.annotations.ApiStatus; + +import net.fabricmc.loom.LoomGradlePlugin; +import net.fabricmc.loom.api.mappings.layered.MappingLayer; +import net.fabricmc.mappingio.MappingVisitor; + +@ApiStatus.Experimental +public record SignatureFixesLayerImpl(Path mappingsFile) implements MappingLayer, SignatureFixesLayer { + private static final String SIGNATURE_FIXES_PATH = "extras/record_signatures.json"; + + @Override + public void visit(MappingVisitor mappingVisitor) throws IOException { + // Nothing to do here + } + + @Override + public Map<String, String> getSignatureFixes() { + try (var zipFile = new ZipFile(mappingsFile().toFile())) { + ZipEntry zipFileEntry = zipFile.getEntry(SIGNATURE_FIXES_PATH); + Objects.requireNonNull(zipFileEntry, "Could not find %s in file".formatted(SIGNATURE_FIXES_PATH)); + + try (var reader = new InputStreamReader(zipFile.getInputStream(zipFileEntry))) { + //noinspection unchecked + return LoomGradlePlugin.OBJECT_MAPPER.readValue(reader, Map.class); + } + } catch (IOException e) { + throw new RuntimeException("Failed to extract signature fixes", e); + } + } +} diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesSpec.java b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesSpec.java new file mode 100644 index 00000000..1f4f1ae5 --- /dev/null +++ b/src/main/java/net/fabricmc/loom/configuration/providers/mappings/extras/signatures/SignatureFixesSpec.java @@ -0,0 +1,39 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2021 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.extras.signatures; + +import org.jetbrains.annotations.ApiStatus; + +import net.fabricmc.loom.api.mappings.layered.MappingContext; +import net.fabricmc.loom.api.mappings.layered.spec.FileSpec; +import net.fabricmc.loom.api.mappings.layered.spec.MappingsSpec; + +@ApiStatus.Experimental +public record SignatureFixesSpec(FileSpec fileSpec) implements MappingsSpec<SignatureFixesLayerImpl> { + @Override + public SignatureFixesLayerImpl createLayer(MappingContext context) { + return new SignatureFixesLayerImpl(fileSpec.get(context)); + } +} diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/MinecraftMappedProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/MinecraftMappedProvider.java index 5d03b971..49ddccd1 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/MinecraftMappedProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/MinecraftMappedProvider.java @@ -30,9 +30,13 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import com.google.common.base.Stopwatch; @@ -41,10 +45,13 @@ import dev.architectury.tinyremapper.InputTag; import dev.architectury.tinyremapper.NonClassCopyMode; import dev.architectury.tinyremapper.OutputConsumerPath; import dev.architectury.tinyremapper.TinyRemapper; +import dev.architectury.tinyremapper.api.TrClass; import org.apache.commons.lang3.mutable.Mutable; -import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.lang3.tuple.Triple; import org.gradle.api.Project; import org.jetbrains.annotations.Nullable; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.commons.Remapper; import net.fabricmc.loom.api.mappings.layered.MappingsNamespace; import net.fabricmc.loom.configuration.DependencyProvider; @@ -230,8 +237,8 @@ public class MinecraftMappedProvider extends DependencyProvider { Info vanilla = new Info(vanillaAssets, input, outputMapped, outputIntermediary, outputSrg); Info forge = getExtension().isForgeAndNotOfficial() ? new Info(forgeAssets, inputForge, forgeOutputMapped, forgeOutputIntermediary, forgeOutputSrg) : null; - Pair<TinyRemapper, Mutable<MemoryMappingTree>> pair = TinyRemapperHelper.getTinyRemapper(getProject(), true); - TinyRemapper remapper = remapperArray[0] = pair.getKey(); + Triple<TinyRemapper, Mutable<MemoryMappingTree>, List<TinyRemapper.ApplyVisitorProvider>> pair = TinyRemapperHelper.getTinyRemapper(getProject(), true, builder -> { }); + TinyRemapper remapper = remapperArray[0] = pair.getLeft(); assetsOut(input, vanillaAssets); @@ -239,7 +246,7 @@ public class MinecraftMappedProvider extends DependencyProvider { assetsOut(inputForge, forgeAssets); } - remap(remapper, pair.getValue(), vanilla, forge, MappingsNamespace.OFFICIAL.toString()); + remap(remapper, pair.getMiddle(), pair.getRight(), vanilla, forge, MappingsNamespace.OFFICIAL.toString()); } public static class Info { @@ -258,7 +265,7 @@ public class MinecraftMappedProvider extends DependencyProvider { } } - public void remap(TinyRemapper remapper, Mutable<MemoryMappingTree> mappings, Info vanilla, @Nullable Info forge, String fromM) throws IOException { + public void remap(TinyRemapper remapper, Mutable<MemoryMappingTree> mappings, List<TinyRemapper.ApplyVisitorProvider> postApply, Info vanilla, @Nullable Info forge, String fromM) throws IOException { Set<String> classNames = getExtension().isForge() ? InnerClassRemapper.readClassNames(vanilla.input) : null; for (String toM : getExtension().isForge() ? Arrays.asList(MappingsNamespace.INTERMEDIARY.toString(), MappingsNamespace.SRG.toString(), MappingsNamespace.NAMED.toString()) : Arrays.asList(MappingsNamespace.INTERMEDIARY.toString(), MappingsNamespace.NAMED.toString())) { @@ -267,6 +274,7 @@ public class MinecraftMappedProvider extends DependencyProvider { InputTag vanillaTag = remapper.createInputTag(); InputTag forgeTag = remapper.createInputTag(); Stopwatch stopwatch = Stopwatch.createStarted(); + final boolean fixSignatures = getExtension().getMappingsProvider().getSignatureFixes() != null; getProject().getLogger().lifecycle(":remapping minecraft (TinyRemapper, " + fromM + " -> " + toM + ")"); remapper.readInputs(vanillaTag, vanilla.input); @@ -277,6 +285,55 @@ public class MinecraftMappedProvider extends DependencyProvider { remapper.replaceMappings(getMappings(classNames, fromM, toM, mappings)); if (!MappingsNamespace.INTERMEDIARY.toString().equals(toM)) mappings.setValue(null); + postApply.clear(); + + // Bit ugly but whatever, the whole issue is a bit ugly :) + AtomicReference<Map<String, String>> remappedSignatures = new AtomicReference<>(); + if (fixSignatures) { + postApply.add(new TinyRemapper.ApplyVisitorProvider() { + @Override + public ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next) { + return new ClassVisitor(Constants.ASM_VERSION, next) { + @Override + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + Map<String, String> signatureFixes = Objects.requireNonNull(remappedSignatures.get(), "Could not get remapped signatures"); + + if (signature == null) { + signature = signatureFixes.getOrDefault(name, null); + + if (signature != null) { + getProject().getLogger().info("Replaced signature for {} with {}", name, signature); + } + } + + super.visit(version, access, name, signature, superName, interfaces); + } + }; + } + }); + + if (MappingsNamespace.INTERMEDIARY.toString().equals(toM)) { + // No need to remap, as these are already intermediary + remappedSignatures.set(getExtension().getMappingsProvider().getSignatureFixes()); + } else { + // Remap the sig fixes from intermediary to the target namespace + final Map<String, String> remapped = new HashMap<>(); + final TinyRemapper sigTinyRemapper = TinyRemapperHelper.getTinyRemapper(getProject(), fromM, toM); + final Remapper sigAsmRemapper = sigTinyRemapper.getRemapper(); + + // Remap the class names and the signatures using a new tiny remapper instance. + for (Map.Entry<String, String> entry : getExtension().getMappingsProvider().getSignatureFixes().entrySet()) { + remapped.put( + sigAsmRemapper.map(entry.getKey()), + sigAsmRemapper.mapSignature(entry.getValue(), false) + ); + } + + sigTinyRemapper.finish(); + remappedSignatures.set(remapped); + } + } + OutputRemappingHandler.remap(remapper, vanilla.assets, output, null, vanillaTag); if (forge != null) { diff --git a/src/main/java/net/fabricmc/loom/util/TinyRemapperHelper.java b/src/main/java/net/fabricmc/loom/util/TinyRemapperHelper.java index b9af8fb8..45ccb6c7 100644 --- a/src/main/java/net/fabricmc/loom/util/TinyRemapperHelper.java +++ b/src/main/java/net/fabricmc/loom/util/TinyRemapperHelper.java @@ -27,7 +27,10 @@ package net.fabricmc.loom.util; import java.io.File; import java.io.IOException; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import java.util.function.Consumer; import java.util.regex.Pattern; import com.google.common.collect.ImmutableMap; @@ -37,6 +40,7 @@ import dev.architectury.tinyremapper.TinyRemapper; import org.apache.commons.lang3.mutable.Mutable; import org.apache.commons.lang3.mutable.MutableObject; import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.lang3.tuple.Triple; import org.gradle.api.Project; import net.fabricmc.loom.LoomGradleExtension; @@ -63,13 +67,13 @@ public final class TinyRemapperHelper { } public static TinyRemapper getTinyRemapper(Project project, String fromM, String toM) throws IOException { - return getTinyRemapper(project, fromM, toM, false); + return getTinyRemapper(project, fromM, toM, false, (builder) -> { }); } - public static TinyRemapper getTinyRemapper(Project project, String fromM, String toM, boolean fixRecords) throws IOException { + public static TinyRemapper getTinyRemapper(Project project, String fromM, String toM, boolean fixRecords, Consumer<TinyRemapper.Builder> builderConsumer) throws IOException { LoomGradleExtension extension = LoomGradleExtension.get(project); - TinyRemapper remapper = _getTinyRemapper(project, fixRecords).getKey(); + TinyRemapper remapper = _getTinyRemapper(project, fixRecords, builderConsumer).getLeft(); remapper.replaceMappings(ImmutableSet.of( TinyRemapperHelper.create((fromM.equals("srg") || toM.equals("srg")) && extension.isForge() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings(), fromM, toM, true), out -> TinyRemapperHelper.JSR_TO_JETBRAINS.forEach(out::acceptClass) @@ -77,9 +81,10 @@ public final class TinyRemapperHelper { return remapper; } - public static Pair<TinyRemapper, Mutable<MemoryMappingTree>> _getTinyRemapper(Project project, boolean fixRecords) throws IOException { + public static Triple<TinyRemapper, Mutable<MemoryMappingTree>, List<TinyRemapper.ApplyVisitorProvider>> _getTinyRemapper(Project project, boolean fixRecords, Consumer<TinyRemapper.Builder> builderConsumer) throws IOException { LoomGradleExtension extension = LoomGradleExtension.get(project); Mutable<MemoryMappingTree> mappings = new MutableObject<>(); + List<TinyRemapper.ApplyVisitorProvider> postApply = new ArrayList<>(); TinyRemapper.Builder builder = TinyRemapper.newRemapper() .renameInvalidLocals(true) @@ -97,22 +102,30 @@ public final class TinyRemapperHelper { builder.fixPackageAccess(true); } - TinyRemapper remapper = builder.invalidLvNamePattern(MC_LV_PATTERN) - .extraPreApplyVisitor((cls, next) -> { - if (fixRecords && !cls.isRecord() && "java/lang/Record".equals(cls.getSuperName()) && mappings.getValue() != null) { - return new RecordComponentFixVisitor(next, mappings.getValue(), mappings.getValue().getNamespaceId(MappingsNamespace.INTERMEDIARY.toString())); - } + builder.invalidLvNamePattern(MC_LV_PATTERN); + builder.extraPreApplyVisitor((cls, next) -> { + if (fixRecords && !cls.isRecord() && "java/lang/Record".equals(cls.getSuperName()) && mappings.getValue() != null) { + return new RecordComponentFixVisitor(next, mappings.getValue(), mappings.getValue().getNamespaceId(MappingsNamespace.INTERMEDIARY.toString())); + } + + return next; + }); + builder.extraPostApplyVisitor((trClass, classVisitor) -> { + for (TinyRemapper.ApplyVisitorProvider provider : postApply) { + classVisitor = provider.insertApplyVisitor(trClass, classVisitor); + } + + return classVisitor; + }); - return next; - }) - .build(); - return Pair.of(remapper, mappings); + builderConsumer.accept(builder); + return Triple.of(builder.build(), mappings, postApply); } - public static Pair<TinyRemapper, Mutable<MemoryMappingTree>> getTinyRemapper(Project project, boolean fixRecords) throws IOException { - Pair<TinyRemapper, Mutable<MemoryMappingTree>> remapper = _getTinyRemapper(project, fixRecords); - remapper.getKey().readClassPath(getMinecraftDependencies(project)); - remapper.getKey().prepareClasses(); + public static Triple<TinyRemapper, Mutable<MemoryMappingTree>, List<TinyRemapper.ApplyVisitorProvider>> getTinyRemapper(Project project, boolean fixRecords, Consumer<TinyRemapper.Builder> builderConsumer) throws IOException { + Triple<TinyRemapper, Mutable<MemoryMappingTree>, List<TinyRemapper.ApplyVisitorProvider>> remapper = _getTinyRemapper(project, fixRecords, builderConsumer); + remapper.getLeft().readClassPath(getMinecraftDependencies(project)); + remapper.getLeft().prepareClasses(); return remapper; } |