Add an option (enabled by default) to map synthetic field and method names from the official mojang mappings. (#538)

This commit is contained in:
modmuss50
2021-11-20 21:46:33 +00:00
committed by GitHub
parent 9c2b1e8d6d
commit babbc55586
11 changed files with 249 additions and 18 deletions

View File

@@ -24,7 +24,10 @@
package net.fabricmc.loom.api.mappings.layered.spec;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
import org.gradle.api.Action;
import org.gradle.util.ConfigureUtil;
import org.jetbrains.annotations.ApiStatus;
/**
@@ -38,14 +41,34 @@ public interface LayeredMappingSpecBuilder {
LayeredMappingSpecBuilder addLayer(MappingsSpec<?> mappingSpec);
/**
* Add a layer that uses the official mappings provided by Mojang.
* Add a layer that uses the official mappings provided by Mojang with the default options.
*/
LayeredMappingSpecBuilder officialMojangMappings();
default LayeredMappingSpecBuilder officialMojangMappings() {
return officialMojangMappings(builder -> { });
}
/**
* Configure and add a layer that uses the official mappings provided by Mojang.
*/
@SuppressWarnings("rawtypes")
default LayeredMappingSpecBuilder officialMojangMappings(@DelegatesTo(value = MojangMappingsSpecBuilder.class, strategy = Closure.DELEGATE_FIRST) Closure closure) {
return officialMojangMappings(mojangMappingsSpecBuilder -> ConfigureUtil.configure(closure, mojangMappingsSpecBuilder));
}
/**
* Configure and add a layer that uses the official mappings provided by Mojang.
*/
LayeredMappingSpecBuilder officialMojangMappings(Action<MojangMappingsSpecBuilder> action);
default LayeredMappingSpecBuilder parchment(Object object) {
return parchment(object, parchmentMappingsSpecBuilder -> parchmentMappingsSpecBuilder.setRemovePrefix(true));
}
@SuppressWarnings("rawtypes")
default LayeredMappingSpecBuilder parchment(Object object, @DelegatesTo(value = ParchmentMappingsSpecBuilder.class, strategy = Closure.DELEGATE_FIRST) Closure closure) {
return parchment(object, parchmentMappingsSpecBuilder -> ConfigureUtil.configure(closure, parchmentMappingsSpecBuilder));
}
LayeredMappingSpecBuilder parchment(Object object, Action<ParchmentMappingsSpecBuilder> action);
/**

View File

@@ -0,0 +1,36 @@
/*
* 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.api.mappings.layered.spec;
public interface MojangMappingsSpecBuilder {
/**
* When enabled synthetic fields and methods will be mapped to name specified in the official mojang mappings.
*
* <p>When disabled synthetic fields and methods will not be mapped leaving them with their intermediary name.
*/
MojangMappingsSpecBuilder setNameSyntheticMembers(boolean value);
boolean getNameSyntheticMembers();
}

View File

@@ -33,10 +33,11 @@ import org.gradle.api.Action;
import net.fabricmc.loom.api.mappings.layered.spec.FileSpec;
import net.fabricmc.loom.api.mappings.layered.spec.LayeredMappingSpecBuilder;
import net.fabricmc.loom.api.mappings.layered.spec.MappingsSpec;
import net.fabricmc.loom.api.mappings.layered.spec.MojangMappingsSpecBuilder;
import net.fabricmc.loom.api.mappings.layered.spec.ParchmentMappingsSpecBuilder;
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.mojmap.MojangMappingsSpecBuilderImpl;
import net.fabricmc.loom.configuration.providers.mappings.parchment.ParchmentMappingsSpecBuilderImpl;
public class LayeredMappingSpecBuilderImpl implements LayeredMappingSpecBuilder {
@@ -49,8 +50,10 @@ public class LayeredMappingSpecBuilderImpl implements LayeredMappingSpecBuilder
}
@Override
public LayeredMappingSpecBuilder officialMojangMappings() {
return addLayer(new MojangMappingsSpec());
public LayeredMappingSpecBuilder officialMojangMappings(Action<MojangMappingsSpecBuilder> action) {
MojangMappingsSpecBuilderImpl builder = MojangMappingsSpecBuilderImpl.builder();
action.execute(builder);
return addLayer(builder.build());
}
@Override

View File

@@ -31,13 +31,15 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Pattern;
import org.gradle.api.logging.Logger;
import net.fabricmc.loom.api.mappings.layered.MappingLayer;
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftVersionMeta;
import net.fabricmc.loom.configuration.providers.mappings.intermediary.IntermediaryMappingLayer;
import net.fabricmc.loom.configuration.providers.mappings.utils.DstNameFilterMappingVisitor;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftVersionMeta;
import net.fabricmc.loom.util.HashedDownloadUtil;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
@@ -45,8 +47,10 @@ import net.fabricmc.mappingio.format.ProGuardReader;
public record MojangMappingLayer(MinecraftVersionMeta.Download clientDownload,
MinecraftVersionMeta.Download serverDownload,
Path workingDir,
Path workingDir, boolean nameSyntheticMembers,
Logger logger) implements MappingLayer {
private static final Pattern SYNTHETIC_NAME_PATTERN = Pattern.compile("^(access|this|val\\$this|lambda\\$.*)\\$[0-9]+$");
@Override
public void visit(MappingVisitor mappingVisitor) throws IOException {
Path clientMappings = workingDir().resolve("client.txt");
@@ -56,8 +60,11 @@ public record MojangMappingLayer(MinecraftVersionMeta.Download clientDownload,
printMappingsLicense(clientMappings);
// Filter out field names matching the pattern
DstNameFilterMappingVisitor nameFilter = new DstNameFilterMappingVisitor(mappingVisitor, SYNTHETIC_NAME_PATTERN);
// Make official the source namespace
MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch(mappingVisitor, MappingsNamespace.OFFICIAL.toString());
MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch(nameSyntheticMembers() ? mappingVisitor : nameFilter, MappingsNamespace.OFFICIAL.toString());
try (BufferedReader clientBufferedReader = Files.newBufferedReader(clientMappings, StandardCharsets.UTF_8);
BufferedReader serverBufferedReader = Files.newBufferedReader(serverMappings, StandardCharsets.UTF_8)) {

View File

@@ -28,7 +28,7 @@ import net.fabricmc.loom.api.mappings.layered.MappingContext;
import net.fabricmc.loom.api.mappings.layered.spec.MappingsSpec;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftVersionMeta;
public record MojangMappingsSpec() implements MappingsSpec<MojangMappingLayer> {
public record MojangMappingsSpec(boolean nameSyntheticMembers) implements MappingsSpec<MojangMappingLayer> {
// Keys in dependency manifest
private static final String MANIFEST_CLIENT_MAPPINGS = "client_mappings";
private static final String MANIFEST_SERVER_MAPPINGS = "server_mappings";
@@ -45,6 +45,7 @@ public record MojangMappingsSpec() implements MappingsSpec<MojangMappingLayer> {
versionInfo.download(MANIFEST_CLIENT_MAPPINGS),
versionInfo.download(MANIFEST_SERVER_MAPPINGS),
context.workingDirectory("mojang"),
nameSyntheticMembers(),
context.getLogger()
);
}

View File

@@ -0,0 +1,54 @@
/*
* 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.mojmap;
import net.fabricmc.loom.api.mappings.layered.spec.MojangMappingsSpecBuilder;
public class MojangMappingsSpecBuilderImpl implements MojangMappingsSpecBuilder {
// TODO 0.11 loom change default to false
private boolean nameSyntheticMembers = true;
private MojangMappingsSpecBuilderImpl() {
}
public static MojangMappingsSpecBuilderImpl builder() {
return new MojangMappingsSpecBuilderImpl();
}
@Override
public MojangMappingsSpecBuilder setNameSyntheticMembers(boolean value) {
nameSyntheticMembers = value;
return this;
}
@Override
public boolean getNameSyntheticMembers() {
return nameSyntheticMembers;
}
public MojangMappingsSpec build() {
return new MojangMappingsSpec(nameSyntheticMembers);
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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.utils;
import java.io.IOException;
import java.util.regex.Pattern;
import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.adapter.ForwardingMappingVisitor;
/**
* Filters out method and field names based on the provided regex pattern.
*/
public class DstNameFilterMappingVisitor extends ForwardingMappingVisitor {
private final Pattern pattern;
public DstNameFilterMappingVisitor(MappingVisitor next, Pattern pattern) {
super(next);
this.pattern = pattern;
}
@Override
public void visitDstName(MappedElementKind targetKind, int namespace, String name) throws IOException {
if ((targetKind == MappedElementKind.FIELD || targetKind == MappedElementKind.METHOD) && pattern.matcher(name).matches()) {
return;
}
super.visitDstName(targetKind, namespace, name);
}
}