2019-04-24 17:32:35 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
2021-09-14 21:40:47 +00:00
|
|
|
* Copyright (c) 2021 FabricMC
|
2019-04-24 17:32:35 +00:00
|
|
|
*
|
|
|
|
* 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.util;
|
|
|
|
|
2021-09-14 21:40:47 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.Map;
|
2021-09-28 09:38:11 +00:00
|
|
|
import java.util.function.Consumer;
|
2021-09-16 19:35:10 +00:00
|
|
|
import java.util.regex.Pattern;
|
2021-09-14 21:40:47 +00:00
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import org.gradle.api.Project;
|
|
|
|
|
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2021-09-19 07:53:13 +00:00
|
|
|
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
2021-09-16 14:28:06 +00:00
|
|
|
import net.fabricmc.mappingio.tree.MappingTree;
|
2021-09-19 07:53:13 +00:00
|
|
|
import net.fabricmc.mappingio.tree.MemoryMappingTree;
|
2019-04-24 17:32:35 +00:00
|
|
|
import net.fabricmc.tinyremapper.IMappingProvider;
|
2021-09-14 21:40:47 +00:00
|
|
|
import net.fabricmc.tinyremapper.TinyRemapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains shortcuts to create tiny remappers using the mappings accessibly to the project.
|
|
|
|
*/
|
|
|
|
public final class TinyRemapperHelper {
|
|
|
|
private static final Map<String, String> JSR_TO_JETBRAINS = new ImmutableMap.Builder<String, String>()
|
|
|
|
.put("javax/annotation/Nullable", "org/jetbrains/annotations/Nullable")
|
|
|
|
.put("javax/annotation/Nonnull", "org/jetbrains/annotations/NotNull")
|
|
|
|
.put("javax/annotation/concurrent/Immutable", "org/jetbrains/annotations/Unmodifiable")
|
|
|
|
.build();
|
|
|
|
|
2021-09-16 19:35:10 +00:00
|
|
|
/**
|
|
|
|
* Matches the new local variable naming format introduced in 21w37a.
|
|
|
|
*/
|
|
|
|
private static final Pattern MC_LV_PATTERN = Pattern.compile("\\$\\$\\d+");
|
|
|
|
|
2021-09-14 21:40:47 +00:00
|
|
|
private TinyRemapperHelper() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TinyRemapper getTinyRemapper(Project project, String fromM, String toM) throws IOException {
|
2021-09-28 09:38:11 +00:00
|
|
|
return getTinyRemapper(project, fromM, toM, false, (builder) -> { });
|
2021-09-19 07:53:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 09:38:11 +00:00
|
|
|
public static TinyRemapper getTinyRemapper(Project project, String fromM, String toM, boolean fixRecords, Consumer<TinyRemapper.Builder> builderConsumer) throws IOException {
|
2021-09-14 21:40:47 +00:00
|
|
|
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
2021-09-19 07:53:13 +00:00
|
|
|
MemoryMappingTree mappingTree = extension.getMappingsProvider().getMappings();
|
|
|
|
|
|
|
|
if (fixRecords && !mappingTree.getSrcNamespace().equals(fromM)) {
|
|
|
|
throw new IllegalStateException("Mappings src namespace must match remap src namespace");
|
|
|
|
}
|
|
|
|
|
|
|
|
int intermediaryNsId = mappingTree.getNamespaceId(MappingsNamespace.INTERMEDIARY.toString());
|
2019-04-24 17:32:35 +00:00
|
|
|
|
2021-09-28 09:38:11 +00:00
|
|
|
TinyRemapper.Builder builder = TinyRemapper.newRemapper()
|
2021-09-19 07:53:13 +00:00
|
|
|
.withMappings(create(mappingTree, fromM, toM, true))
|
2021-09-14 21:40:47 +00:00
|
|
|
.withMappings(out -> JSR_TO_JETBRAINS.forEach(out::acceptClass))
|
|
|
|
.renameInvalidLocals(true)
|
|
|
|
.rebuildSourceFilenames(true)
|
2021-09-16 19:35:10 +00:00
|
|
|
.invalidLvNamePattern(MC_LV_PATTERN)
|
2021-09-19 07:53:13 +00:00
|
|
|
.extraPreApplyVisitor((cls, next) -> {
|
|
|
|
if (fixRecords && !cls.isRecord() && "java/lang/Record".equals(cls.getSuperName())) {
|
|
|
|
return new RecordComponentFixVisitor(next, mappingTree, intermediaryNsId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next;
|
2021-09-28 09:38:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
builderConsumer.accept(builder);
|
|
|
|
return builder.build();
|
2021-09-14 21:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Path[] getMinecraftDependencies(Project project) {
|
|
|
|
return project.getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES).getFiles()
|
|
|
|
.stream().map(File::toPath).toArray(Path[]::new);
|
|
|
|
}
|
2019-04-24 17:32:35 +00:00
|
|
|
|
2019-11-09 19:00:36 +00:00
|
|
|
private static IMappingProvider.Member memberOf(String className, String memberName, String descriptor) {
|
|
|
|
return new IMappingProvider.Member(className, memberName, descriptor);
|
|
|
|
}
|
2019-04-24 17:32:35 +00:00
|
|
|
|
2021-09-16 14:28:06 +00:00
|
|
|
public static IMappingProvider create(MappingTree mappings, String from, String to, boolean remapLocalVariables) {
|
2019-11-09 19:00:36 +00:00
|
|
|
return (acceptor) -> {
|
2021-09-16 14:28:06 +00:00
|
|
|
for (MappingTree.ClassMapping classDef : mappings.getClasses()) {
|
2019-11-09 19:00:36 +00:00
|
|
|
String className = classDef.getName(from);
|
|
|
|
acceptor.acceptClass(className, classDef.getName(to));
|
|
|
|
|
2021-09-16 14:28:06 +00:00
|
|
|
for (MappingTree.FieldMapping field : classDef.getFields()) {
|
|
|
|
acceptor.acceptField(memberOf(className, field.getName(from), field.getDesc(from)), field.getName(to));
|
2019-11-09 19:00:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 14:28:06 +00:00
|
|
|
for (MappingTree.MethodMapping method : classDef.getMethods()) {
|
|
|
|
IMappingProvider.Member methodIdentifier = memberOf(className, method.getName(from), method.getDesc(from));
|
2019-11-09 19:00:36 +00:00
|
|
|
acceptor.acceptMethod(methodIdentifier, method.getName(to));
|
|
|
|
|
|
|
|
if (remapLocalVariables) {
|
2021-09-16 14:28:06 +00:00
|
|
|
for (MappingTree.MethodArgMapping parameter : method.getArgs()) {
|
|
|
|
String name = parameter.getName(to);
|
|
|
|
|
|
|
|
if (name == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptor.acceptMethodArg(methodIdentifier, parameter.getLvIndex(), name);
|
2019-11-09 19:00:36 +00:00
|
|
|
}
|
2019-04-24 17:32:35 +00:00
|
|
|
|
2021-09-16 14:28:06 +00:00
|
|
|
for (MappingTree.MethodVarMapping localVariable : method.getVars()) {
|
|
|
|
acceptor.acceptMethodVar(methodIdentifier, localVariable.getLvIndex(),
|
|
|
|
localVariable.getStartOpIdx(), localVariable.getLvtRowIndex(),
|
2021-09-14 21:40:47 +00:00
|
|
|
localVariable.getName(to));
|
2019-11-09 19:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 17:32:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|