Fix merge conflicts

Signed-off-by: shedaniel <daniel@shedaniel.me>
This commit is contained in:
shedaniel
2021-09-18 14:00:14 +08:00
parent 3aa0e5f731
commit 7f54808315
25 changed files with 371 additions and 408 deletions

View File

@@ -43,6 +43,7 @@ public enum MappingsNamespace {
* @see <a href="https://github.com/FabricMC/intermediary/">github.com/FabricMC/intermediary/</a>
*/
INTERMEDIARY,
SRG,
/**
* Named mappings are the developer friendly names used to develop mods against.

View File

@@ -47,4 +47,6 @@ public interface LayeredMappingSpecBuilder {
}
LayeredMappingSpecBuilder parchment(Object object, Action<ParchmentMappingsSpecBuilder> action);
LayeredMappingSpecBuilder crane(Object object);
}

View File

@@ -31,6 +31,7 @@ import java.util.List;
import java.util.Set;
import com.google.common.base.Preconditions;
import dev.architectury.tinyremapper.TinyRemapper;
import org.gradle.api.Project;
import net.fabricmc.accesswidener.AccessWidener;
@@ -44,7 +45,6 @@ import net.fabricmc.loom.configuration.RemappedConfigurationEntry;
import net.fabricmc.loom.configuration.processors.JarProcessor;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.TinyRemapperHelper;
import net.fabricmc.tinyremapper.TinyRemapper;
/**
* Applies transitive access wideners that are inherited from mod and api dependencies.

View File

@@ -67,7 +67,7 @@ import net.fabricmc.loom.util.LoggerFilter;
import net.fabricmc.loom.util.TinyRemapperHelper;
import net.fabricmc.loom.util.srg.AtRemapper;
import net.fabricmc.loom.util.srg.CoreModClassRemapper;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
public class ModProcessor {
public static void processMods(Project project, List<ModDependencyInfo> processList) throws IOException {
@@ -148,7 +148,7 @@ public class ModProcessor {
Stopwatch stopwatch = Stopwatch.createStarted();
project.getLogger().lifecycle(":remapping " + remapList.size() + " mods (TinyRemapper, " + fromM + " -> " + toM + ")");
TinyTree mappings = extension.isForge() ? mappingsProvider.getMappingsWithSrg() : mappingsProvider.getMappings();
MemoryMappingTree mappings = extension.isForge() ? mappingsProvider.getMappingsWithSrg() : mappingsProvider.getMappings();
LoggerFilter.replaceSystemOut();
TinyRemapper remapper = TinyRemapper.newRemapper()
.logger(project.getLogger()::lifecycle)

View File

@@ -27,6 +27,7 @@ package net.fabricmc.loom.configuration.providers.forge;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -44,11 +45,6 @@ import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import dev.architectury.mappingslayers.api.mutable.MappingsEntry;
import dev.architectury.mappingslayers.api.mutable.MutableClassDef;
import dev.architectury.mappingslayers.api.mutable.MutableFieldDef;
import dev.architectury.mappingslayers.api.mutable.MutableTinyTree;
import dev.architectury.mappingslayers.api.utils.MappingsUtils;
import dev.architectury.refmapremapper.utils.DescriptorRemapper;
import org.gradle.api.Project;
import org.objectweb.asm.ClassReader;
@@ -58,14 +54,16 @@ import org.objectweb.asm.Opcodes;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.LoomGradlePlugin;
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.configuration.providers.mappings.MappingsProviderImpl;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.ThreadingUtils;
import net.fabricmc.loom.util.srg.SrgMerger;
import net.fabricmc.mapping.tree.ClassDef;
import net.fabricmc.mapping.tree.FieldDef;
import net.fabricmc.mapping.tree.TinyMappingFactory;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.format.Tiny2Writer;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTreeView;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
public class FieldMigratedMappingsProvider extends MappingsProviderImpl {
private List<Map.Entry<FieldMember, String>> migratedFields = new ArrayList<>();
@@ -148,27 +146,30 @@ public class FieldMigratedMappingsProvider extends MappingsProviderImpl {
fieldDescriptorMap.put(entry.getKey().owner, entry.getKey().field, entry.getValue());
}
MutableTinyTree mappings;
MemoryMappingTree mappings = new MemoryMappingTree();
try (BufferedReader reader = Files.newBufferedReader(rawTinyMappings)) {
mappings = MappingsUtils.copyAsMutable(TinyMappingFactory.loadWithDetection(reader));
MappingReader.read(reader, mappings);
for (MutableClassDef classDef : mappings.getClassesMutable()) {
Map<String, String> row = fieldDescriptorMap.row(classDef.getIntermediary());
for (MappingTree.ClassMapping classDef : new ArrayList<>(mappings.getClasses())) {
Map<String, String> row = fieldDescriptorMap.row(classDef.getName(MappingsNamespace.INTERMEDIARY.toString()));
if (!row.isEmpty()) {
for (MutableFieldDef fieldDef : classDef.getFieldsMutable()) {
String newDescriptor = row.get(fieldDef.getIntermediary());
for (MappingTree.FieldMapping fieldDef : new ArrayList<>(classDef.getFields())) {
String newDescriptor = row.get(fieldDef.getName(MappingsNamespace.INTERMEDIARY.toString()));
if (newDescriptor != null) {
fieldDef.setDescriptor(MappingsEntry.NS_INTERMEDIARY, newDescriptor);
fieldDef.setSrcDesc(mappings.mapDesc(newDescriptor, mappings.getNamespaceId(MappingsNamespace.INTERMEDIARY.toString()), MappingTreeView.SRC_NAMESPACE_ID));
}
}
}
}
}
Files.writeString(tinyMappings, MappingsUtils.serializeToString(mappings), StandardOpenOption.CREATE);
StringWriter stringWriter = new StringWriter();
Tiny2Writer tiny2Writer = new Tiny2Writer(stringWriter, false);
mappings.accept(tiny2Writer);
Files.writeString(tinyMappings, stringWriter.toString(), StandardOpenOption.CREATE);
}
}
@@ -217,27 +218,28 @@ public class FieldMigratedMappingsProvider extends MappingsProviderImpl {
Map<FieldMember, String> migratedFields = new HashMap<>();
try (BufferedReader reader = Files.newBufferedReader(rawTinyMappingsWithSrg)) {
TinyTree mappings = TinyMappingFactory.loadWithDetection(reader);
MemoryMappingTree mappings = new MemoryMappingTree();
MappingReader.read(reader, mappings);
Map<String, String> srgToIntermediary = new HashMap<>();
for (ClassDef aClass : mappings.getClasses()) {
for (MappingTree.ClassMapping aClass : mappings.getClasses()) {
srgToIntermediary.put(aClass.getName("srg"), aClass.getName("intermediary"));
}
for (ClassDef classDef : mappings.getClasses()) {
for (MappingTree.ClassMapping classDef : mappings.getClasses()) {
String ownerSrg = classDef.getName("srg");
String ownerIntermediary = classDef.getName("intermediary");
for (FieldDef fieldDef : classDef.getFields()) {
for (MappingTree.FieldMapping fieldDef : classDef.getFields()) {
String fieldSrg = fieldDef.getName("srg");
String descriptorSrg = fieldDef.getDescriptor("srg");
String descriptorSrg = fieldDef.getDesc("srg");
FieldMember member = new FieldMember(ownerSrg, fieldSrg);
String newDescriptor = fieldDescriptorMap.get(member);
if (newDescriptor != null && !newDescriptor.equals(descriptorSrg)) {
String fieldIntermediary = fieldDef.getName("intermediary");
String descriptorIntermediary = fieldDef.getDescriptor("intermediary");
String descriptorIntermediary = fieldDef.getDesc("intermediary");
String newDescriptorRemapped = DescriptorRemapper.remapDescriptor(newDescriptor,
clazz -> srgToIntermediary.getOrDefault(clazz, clazz));
migratedFields.put(new FieldMember(ownerIntermediary, fieldIntermediary), newDescriptorRemapped);

View File

@@ -32,6 +32,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
@@ -65,10 +66,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteSource;
import de.oceanlabs.mcp.mcinjector.adaptors.ParameterAnnotationFixer;
import dev.architectury.mappingslayers.api.mutable.MutableClassDef;
import dev.architectury.mappingslayers.api.mutable.MutableMethodDef;
import dev.architectury.mappingslayers.api.mutable.MutableTinyTree;
import dev.architectury.mappingslayers.api.utils.MappingsUtils;
import dev.architectury.tinyremapper.InputTag;
import dev.architectury.tinyremapper.OutputConsumerPath;
import dev.architectury.tinyremapper.TinyRemapper;
@@ -92,25 +89,27 @@ import org.objectweb.asm.tree.ClassNode;
import org.zeroturnaround.zip.ZipUtil;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.configuration.DependencyProvider;
import net.fabricmc.loom.configuration.providers.MinecraftProviderImpl;
import net.fabricmc.loom.configuration.providers.mappings.GradleMappingContext;
import net.fabricmc.loom.configuration.providers.mappings.MappingNamespace;
import net.fabricmc.loom.configuration.providers.mappings.mojmap.MojangMappingLayer;
import net.fabricmc.loom.configuration.providers.mappings.mojmap.MojangMappingsSpec;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftMappedProvider;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.DependencyDownloader;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.MappingsProviderVerbose;
import net.fabricmc.loom.util.ThreadingUtils;
import net.fabricmc.loom.util.TinyRemapperMappingsHelper;
import net.fabricmc.loom.util.TinyRemapperHelper;
import net.fabricmc.loom.util.function.FsPathConsumer;
import net.fabricmc.loom.util.srg.InnerClassRemapper;
import net.fabricmc.loom.util.srg.SpecialSourceExecutor;
import net.fabricmc.loom.util.srg.Tsrg2Utils;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.loom.util.srg.Tsrg2Writer;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
public class MinecraftPatchedProvider extends DependencyProvider {
private static final String LOOM_PATCH_VERSION_KEY = "Loom-Patch-Version";
@@ -319,12 +318,12 @@ public class MinecraftPatchedProvider extends DependencyProvider {
}
private TinyRemapper buildRemapper(Path input) throws IOException {
Path[] libraries = MinecraftMappedProvider.getRemapClasspath(getProject());
TinyTree mappingsWithSrg = getExtension().getMappingsProvider().getMappingsWithSrg();
Path[] libraries = TinyRemapperHelper.getMinecraftDependencies(getProject());
MemoryMappingTree mappingsWithSrg = getExtension().getMappingsProvider().getMappingsWithSrg();
TinyRemapper remapper = TinyRemapper.newRemapper()
.logger(getProject().getLogger()::lifecycle)
.logUnknownInvokeDynamic(false)
.withMappings(TinyRemapperMappingsHelper.create(mappingsWithSrg, "srg", "official", true))
.withMappings(TinyRemapperHelper.create(mappingsWithSrg, "srg", "official", true))
.withMappings(InnerClassRemapper.of(InnerClassRemapper.readClassNames(input), mappingsWithSrg, "srg", "official"))
.renameInvalidLocals(true)
.rebuildSourceFilenames(true)
@@ -377,14 +376,14 @@ public class MinecraftPatchedProvider extends DependencyProvider {
GradleMappingContext context = new GradleMappingContext(project, "tmp-mojmap");
try {
FileUtils.deleteDirectory(context.workingDirectory("/"));
FileUtils.deleteDirectory(context.workingDirectory("/").toFile());
MojangMappingLayer layer = new MojangMappingsSpec(() -> true).createLayer(context);
layer.visit(visitor);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
try {
FileUtils.deleteDirectory(context.workingDirectory("/"));
FileUtils.deleteDirectory(context.workingDirectory("/").toFile());
} catch (IOException e) {
e.printStackTrace();
}
@@ -397,7 +396,7 @@ public class MinecraftPatchedProvider extends DependencyProvider {
if (Files.notExists(path)) {
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
Tsrg2Utils.writeTsrg(visitor -> visitMojmap(visitor, project),
MappingNamespace.NAMED.stringValue(), false, writer);
MappingsNamespace.NAMED.toString(), false, writer);
}
}
@@ -409,7 +408,9 @@ public class MinecraftPatchedProvider extends DependencyProvider {
if (Files.notExists(path)) {
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
Tsrg2Utils.writeTsrg2(visitor -> visitMojmap(visitor, project), writer);
MemoryMappingTree tree = new MemoryMappingTree();
visitMojmap(tree, project);
writer.write(Tsrg2Writer.serialize(tree));
}
}
@@ -432,15 +433,16 @@ public class MinecraftPatchedProvider extends DependencyProvider {
out.toAbsolutePath().toString()
});
MutableTinyTree mappings = MappingsUtils.deserializeFromTsrg2(FileUtils.readFileToString(out.toFile(), StandardCharsets.UTF_8));
MemoryMappingTree tree = new MemoryMappingTree();
MappingReader.read(new StringReader(FileUtils.readFileToString(out.toFile(), StandardCharsets.UTF_8)), tree);
for (MutableClassDef classDef : mappings.getClassesMutable()) {
for (MutableMethodDef methodDef : classDef.getMethodsMutable()) {
methodDef.getParametersMutable().clear();
for (MappingTree.ClassMapping classDef : tree.getClasses()) {
for (MappingTree.MethodMapping methodDef : classDef.getMethods()) {
methodDef.getArgs().clear();
}
}
Files.writeString(outTrimmed, MappingsUtils.serializeToTsrg2(mappings), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
Files.writeString(outTrimmed, Tsrg2Writer.serialize(tree), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
mergedMojangTsrg2Files = new Path[]{out, outTrimmed};
}

View File

@@ -46,26 +46,20 @@ public class LayeredMappingSpecBuilderImpl implements LayeredMappingSpecBuilder
@Nullable
private final LoomGradleExtensionAPI extension;
public LayeredMappingSpecBuilder(@Nullable LoomGradleExtensionAPI extension) {
public LayeredMappingSpecBuilderImpl(@Nullable LoomGradleExtensionAPI extension) {
this.extension = extension;
}
@Override
public LayeredMappingSpecBuilder addLayer(MappingsSpec<?> mappingSpec) {
layers.add(mappingSpec);
public LayeredMappingSpecBuilder officialMojangMappings() {
layers.add(new MojangMappingsSpec(() -> extension != null && extension.isSilentMojangMappingsLicenseEnabled()));
return this;
}
public LayeredMappingSpecBuilder parchment(String mavenNotation) {
parchment(mavenNotation, parchmentMappingsSpecBuilder -> parchmentMappingsSpecBuilder.setRemovePrefix(true));
return this;
}
@Override
public LayeredMappingSpecBuilder officialMojangMappings() {
return addLayer(new MojangMappingsSpec());
layers.add(new MojangMappingsSpec(() -> extension != null && extension.isSilentMojangMappingsLicenseEnabled()));
return this;
}
@Override
@@ -75,8 +69,9 @@ public class LayeredMappingSpecBuilderImpl implements LayeredMappingSpecBuilder
return addLayer(builder.build());
}
public LayeredMappingSpecBuilder crane(String mavenNotation) {
layers.add(new CraneMappingsSpec(mavenNotation));
@Override
public LayeredMappingSpecBuilder crane(Object object) {
layers.add(new CraneMappingsSpec(FileSpec.create(object)));
return this;
}

View File

@@ -71,7 +71,6 @@ import net.fabricmc.loom.util.DownloadUtil;
import net.fabricmc.loom.util.srg.MCPReader;
import net.fabricmc.loom.util.srg.SrgMerger;
import net.fabricmc.loom.util.srg.SrgNamedWriter;
import net.fabricmc.mapping.reader.v2.TinyMetadata;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.adapter.MappingNsCompleter;
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
@@ -107,6 +106,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
private boolean hasUnpickDefinitions;
private UnpickMetadata unpickMetadata;
private MemoryMappingTree mappingTree;
private MemoryMappingTree mappingTreeWithSrg;
public MappingsProviderImpl(Project project) {
super(project);
@@ -116,12 +116,8 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
return Objects.requireNonNull(mappingTree, "Cannot get mappings before they have been read");
}
public TinyTree getMappingsWithSrg() throws IOException {
if (getExtension().shouldGenerateSrgTiny()) {
return MappingsCache.INSTANCE.get(tinyMappingsWithSrg);
}
throw new UnsupportedOperationException("Not running with Forge support / Tiny srg support.");
public MemoryMappingTree getMappingsWithSrg() throws IOException {
return Objects.requireNonNull(mappingTreeWithSrg, "Cannot get mappings before they have been read");
}
@Override
@@ -152,7 +148,7 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
patchedProvider.provide(dependency, postPopulationScheduler);
}
mappingTree = readMappings();
mappingTree = readMappings(tinyMappings);
manipulateMappings(mappingsJar.toPath());
if (getExtension().shouldGenerateSrgTiny()) {
@@ -162,6 +158,8 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
}
}
mappingTreeWithSrg = readMappings(tinyMappingsWithSrg);
if (Files.notExists(tinyMappingsJar) || isRefreshDeps()) {
ZipUtil.pack(new ZipEntrySource[] {new FileSource("mappings/mappings.tiny", tinyMappings.toFile())}, tinyMappingsJar.toFile());
}
@@ -318,9 +316,9 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
}
}
private MemoryMappingTree readMappings() throws IOException {
private static MemoryMappingTree readMappings(Path file) throws IOException {
MemoryMappingTree mappingTree = new MemoryMappingTree();
MappingReader.read(tinyMappings, mappingTree);
MappingReader.read(file, mappingTree);
return mappingTree;
}
@@ -362,9 +360,8 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
private static boolean areMappingsMergedV2(Path path) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(path)) {
TinyMetadata metadata = TinyV2Factory.readMetadata(reader);
return metadata.getNamespaces().containsAll(Arrays.asList("named", "intermediary", "official"));
} catch (IllegalArgumentException | NoSuchFileException e) {
return MappingReader.detectFormat(reader) == MappingFormat.TINY_2 && MappingReader.getNamespaces(reader).containsAll(Arrays.asList("named", "intermediary", "official"));
} catch (NoSuchFileException e) {
return false;
}
}
@@ -374,6 +371,8 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
try (BufferedReader reader = Files.newBufferedReader(fs.getPath("mappings", "mappings.tiny"))) {
return MappingReader.detectFormat(reader) == MappingFormat.TINY_2;
}
} catch (NoSuchFileException e) {
return false;
}
}
@@ -466,10 +465,10 @@ public class MappingsProviderImpl extends DependencyProvider implements Mappings
runCommand(command, intermediaryMappings.toAbsolutePath().toString(),
yarnMappings.toAbsolutePath().toString(),
newMergedMappings.toAbsolutePath().toString(),
MappingsNamespace.INTERMEDIARY.toString(), MappingsNamespace.OFFICIAL.toString());
MappingsNamespace.INTERMEDIARY.toString(), MappingsNamespace.OFFICIAL.toString());
} catch (Exception e) {
throw new RuntimeException("Could not merge mappings from " + intermediaryMappings.toString()
+ " with mappings from " + yarnMappings, e);
+ " with mappings from " + yarnMappings, e);
}
}

View File

@@ -25,91 +25,24 @@
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 java.nio.file.Path;
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.api.mappings.layered.MappingLayer;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.format.Tiny2Reader;
public record CraneMappingLayer(File craneJar) implements MappingLayer {
public record CraneMappingLayer(Path 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 (FileSystemUtil.FileSystemDelegate fs = FileSystemUtil.getJarFileSystem(craneJar(), 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());
Tiny2Reader.read(reader, visitor);
}
}
}

View File

@@ -24,12 +24,13 @@
package net.fabricmc.loom.configuration.providers.mappings.crane;
import net.fabricmc.loom.configuration.providers.mappings.MappingContext;
import net.fabricmc.loom.configuration.providers.mappings.MappingsSpec;
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;
public record CraneMappingsSpec(String mavenNotation) implements MappingsSpec<CraneMappingLayer> {
public record CraneMappingsSpec(FileSpec fileSpec) implements MappingsSpec<CraneMappingLayer> {
@Override
public CraneMappingLayer createLayer(MappingContext context) {
return new CraneMappingLayer(context.mavenFile(mavenNotation()));
return new CraneMappingLayer(fileSpec().get(context));
}
}

View File

@@ -32,12 +32,10 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import dev.architectury.tinyremapper.IMappingProvider;
import dev.architectury.tinyremapper.InputTag;
import dev.architectury.tinyremapper.NonClassCopyMode;
@@ -61,15 +59,9 @@ import net.fabricmc.loom.util.TinyRemapperHelper;
import net.fabricmc.loom.util.srg.AtRemapper;
import net.fabricmc.loom.util.srg.CoreModClassRemapper;
import net.fabricmc.loom.util.srg.InnerClassRemapper;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
public class MinecraftMappedProvider extends DependencyProvider {
public 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();
private File inputJar;
private File inputForgeJar;
private File minecraftMappedJar;
@@ -177,14 +169,6 @@ public class MinecraftMappedProvider extends DependencyProvider {
}
}
private TinyRemapper buildRemapper() throws IOException {
Path[] libraries = getRemapClasspath(getProject());
TinyRemapper remapper = getTinyRemapper();
remapper.readClassPath(libraries);
remapper.prepareClasses();
return remapper;
}
private byte[][] inputBytes(Path input) throws IOException {
List<byte[]> inputByteList = new ArrayList<>();
@@ -244,7 +228,7 @@ 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;
TinyRemapper remapper = remapperArray[0] = buildRemapper();
TinyRemapper remapper = remapperArray[0] = TinyRemapperHelper.getTinyRemapper(getProject());
assetsOut(input, vanillaAssets);
@@ -295,60 +279,34 @@ public class MinecraftMappedProvider extends DependencyProvider {
OutputRemappingHandler.remap(remapper, forge.assets, outputForge, null, forgeTag);
}
// TODO TinyRemapperHelper
getProject().getLogger().lifecycle(":remapped minecraft (TinyRemapper, " + fromM + " -> " + toM + ") in " + stopwatch);
remapper.removeInput();
if (getExtension().isForge() && !"srg".equals(toM)) {
getProject().getLogger().info(":running minecraft finalising tasks");
TinyTree yarnWithSrg = getExtension().getMappingsProvider().getMappingsWithSrg();
MemoryMappingTree yarnWithSrg = getExtension().getMappingsProvider().getMappingsWithSrg();
AtRemapper.remap(getProject().getLogger(), output, yarnWithSrg);
CoreModClassRemapper.remapJar(output, yarnWithSrg, getProject().getLogger());
}
}
}
public TinyRemapper getTinyRemapper() throws IOException {
TinyRemapper.Builder builder = TinyRemapper.newRemapper()
.renameInvalidLocals(true)
.logUnknownInvokeDynamic(false)
.ignoreConflicts(getExtension().isForge())
.cacheMappings(true)
.threads(Runtime.getRuntime().availableProcessors())
.logger(getProject().getLogger()::lifecycle)
.rebuildSourceFilenames(true);
if (getExtension().isForge()) {
/* FORGE: Required for classes like aej$OptionalNamedTag (1.16.4) which are added by Forge patches.
* They won't get remapped to their proper packages, so IllegalAccessErrors will happen without ._.
*/
builder.fixPackageAccess(true);
}
return builder.build();
}
public Set<IMappingProvider> getMappings(@Nullable Set<String> fromClassNames, String fromM, String toM) throws IOException {
Set<IMappingProvider> providers = new HashSet<>();
providers.add(TinyRemapperMappingsHelper.create(getExtension().isForge() ? getExtension().getMappingsProvider().getMappingsWithSrg() : getExtension().getMappingsProvider().getMappings(), fromM, toM, true));
providers.add(TinyRemapperHelper.create(getExtension().isForge() ? getExtension().getMappingsProvider().getMappingsWithSrg() : getExtension().getMappingsProvider().getMappings(), fromM, toM, true));
if (getExtension().isForge()) {
if (fromClassNames != null) {
providers.add(InnerClassRemapper.of(fromClassNames, getExtension().getMappingsProvider().getMappingsWithSrg(), fromM, toM));
}
} else {
providers.add(out -> JSR_TO_JETBRAINS.forEach(out::acceptClass));
providers.add(out -> TinyRemapperHelper.JSR_TO_JETBRAINS.forEach(out::acceptClass));
}
return providers;
}
public static Path[] getRemapClasspath(Project project) {
return project.getConfigurations().getByName(Constants.Configurations.MINECRAFT_DEPENDENCIES).getFiles()
.stream().map(File::toPath).toArray(Path[]::new);
}
protected void addDependencies(DependencyInfo dependency, Consumer<Runnable> postPopulationScheduler) {
getProject().getDependencies().add(Constants.Configurations.MINECRAFT_NAMED,
getProject().getDependencies().module("net.minecraft:" + minecraftProvider.getJarPrefix() + "minecraft-mapped:" + getMinecraftProvider().minecraftVersion() + "/" + getExtension().getMappingsProvider().mappingsIdentifier()));

View File

@@ -48,13 +48,13 @@ import org.zeroturnaround.zip.ZipUtil;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.build.ModCompileRemapper;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftMappedProvider;
import net.fabricmc.loom.task.GenerateSourcesTask;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.DeletingFileVisitor;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.SourceRemapper;
import net.fabricmc.loom.util.ThreadingUtils;
import net.fabricmc.loom.util.TinyRemapperHelper;
import net.fabricmc.lorenztiny.TinyMappingsReader;
public class ForgeSourcesRemapper {
@@ -171,7 +171,7 @@ public class ForgeSourcesRemapper {
MappingSet mappings = new TinyMappingsReader(extension.getMappingsProvider().getMappingsWithSrg(), "srg", "named").read();
for (Map.Entry<String, String> entry : MinecraftMappedProvider.JSR_TO_JETBRAINS.entrySet()) {
for (Map.Entry<String, String> entry : TinyRemapperHelper.JSR_TO_JETBRAINS.entrySet()) {
mappings.getOrCreateClassMapping(entry.getKey()).setDeobfuscatedName(entry.getValue());
}

View File

@@ -88,7 +88,6 @@ import org.zeroturnaround.zip.transform.StreamZipEntryTransformer;
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.build.JarRemapper;
import net.fabricmc.loom.build.MixinRefmapHelper;
import net.fabricmc.loom.build.nesting.EmptyNestedJarProvider;
@@ -109,10 +108,7 @@ import net.fabricmc.loom.util.TinyRemapperHelper;
import net.fabricmc.loom.util.ZipReprocessorUtil;
import net.fabricmc.loom.util.aw2at.Aw2At;
import net.fabricmc.lorenztiny.TinyMappingsReader;
import net.fabricmc.mapping.tree.ClassDef;
import net.fabricmc.mapping.tree.FieldDef;
import net.fabricmc.mapping.tree.MethodDef;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.stitch.util.Pair;
public class RemapJarTask extends Jar {
@@ -169,7 +165,7 @@ public class RemapJarTask extends Jar {
}
private ReferenceRemapper createReferenceRemapper(LoomGradleExtension extension, String from, String to) throws IOException {
TinyTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
MappingTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
return new SimpleReferenceRemapper(new SimpleReferenceRemapper.Remapper() {
@Override
@@ -186,13 +182,13 @@ public class RemapJarTask extends Jar {
@Nullable
public String mapMethod(@Nullable String className, String methodName, String methodDescriptor) {
if (className != null) {
Optional<ClassDef> classDef = mappings.getClasses().stream()
Optional<MappingTree.ClassMapping> classDef = (Optional<MappingTree.ClassMapping>) mappings.getClasses().stream()
.filter(c -> Objects.equals(c.getName(from), className))
.findFirst();
if (classDef.isPresent()) {
for (MethodDef methodDef : classDef.get().getMethods()) {
if (Objects.equals(methodDef.getName(from), methodName) && Objects.equals(methodDef.getDescriptor(from), methodDescriptor)) {
for (MappingTree.MethodMapping methodDef : classDef.get().getMethods()) {
if (Objects.equals(methodDef.getName(from), methodName) && Objects.equals(methodDef.getDesc(from), methodDescriptor)) {
return methodDef.getName(to);
}
}
@@ -201,7 +197,7 @@ public class RemapJarTask extends Jar {
return mappings.getClasses().stream()
.flatMap(classDef -> classDef.getMethods().stream())
.filter(methodDef -> Objects.equals(methodDef.getName(from), methodName) && Objects.equals(methodDef.getDescriptor(from), methodDescriptor))
.filter(methodDef -> Objects.equals(methodDef.getName(from), methodName) && Objects.equals(methodDef.getDesc(from), methodDescriptor))
.findFirst()
.map(methodDef -> methodDef.getName(to))
.orElse(null);
@@ -211,13 +207,13 @@ public class RemapJarTask extends Jar {
@Nullable
public String mapField(@Nullable String className, String fieldName, String fieldDescriptor) {
if (className != null) {
Optional<ClassDef> classDef = mappings.getClasses().stream()
Optional<MappingTree.ClassMapping> classDef = (Optional<MappingTree.ClassMapping>) mappings.getClasses().stream()
.filter(c -> Objects.equals(c.getName(from), className))
.findFirst();
if (classDef.isPresent()) {
for (FieldDef fieldDef : classDef.get().getFields()) {
if (Objects.equals(fieldDef.getName(from), fieldName) && Objects.equals(fieldDef.getDescriptor(from), fieldDescriptor)) {
for (MappingTree.FieldMapping fieldDef : classDef.get().getFields()) {
if (Objects.equals(fieldDef.getName(from), fieldName) && Objects.equals(fieldDef.getDesc(from), fieldDescriptor)) {
return fieldDef.getName(to);
}
}
@@ -226,7 +222,7 @@ public class RemapJarTask extends Jar {
return mappings.getClasses().stream()
.flatMap(classDef -> classDef.getFields().stream())
.filter(fieldDef -> Objects.equals(fieldDef.getName(from), fieldName) && Objects.equals(fieldDef.getDescriptor(from), fieldDescriptor))
.filter(fieldDef -> Objects.equals(fieldDef.getName(from), fieldName) && Objects.equals(fieldDef.getDesc(from), fieldDescriptor))
.findFirst()
.map(fieldDef -> fieldDef.getName(to))
.orElse(null);
@@ -407,7 +403,7 @@ public class RemapJarTask extends Jar {
}
private IMappingProvider remapToSrg(LoomGradleExtension extension, IMappingProvider parent, String from, String to) throws IOException {
TinyTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
MappingTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
return sink -> {
parent.load(new IMappingProvider.MappingAcceptor() {
@@ -507,7 +503,7 @@ public class RemapJarTask extends Jar {
}
LoomGradleExtension extension = LoomGradleExtension.get(getProject());
TinyTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
MappingTree mappings = extension.shouldGenerateSrgTiny() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings();
try (TinyMappingsReader reader = new TinyMappingsReader(mappings, fromM.get(), toM.get())) {
MappingSet mappingSet = reader.read();

View File

@@ -25,20 +25,23 @@
package net.fabricmc.loom.util;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import dev.architectury.mappingslayers.api.mutable.MutableTinyMetadata;
import dev.architectury.mappingslayers.api.mutable.MutableTinyTree;
import dev.architectury.mappingslayers.api.utils.MappingsUtils;
import dev.architectury.tinyremapper.IMappingProvider;
import dev.architectury.tinyremapper.TinyRemapper;
import net.fabricmc.mappingio.adapter.RegularAsFlatMappingVisitor;
import net.fabricmc.mappingio.format.Tiny2Writer;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
public class MappingsProviderVerbose {
public static void saveFile(TinyRemapper providers) throws IOException {
try {
@@ -52,26 +55,38 @@ public class MappingsProviderVerbose {
}
public static void saveFile(Iterable<IMappingProvider> providers) throws IOException {
MutableTinyTree tree = MappingsUtils.create(MutableTinyMetadata.create(2, 0, Arrays.asList("from", "to"), new HashMap<>()));
MemoryMappingTree tree = new MemoryMappingTree();
tree.setSrcNamespace("from");
tree.setDstNamespaces(new ArrayList<>(Collections.singletonList("to")));
RegularAsFlatMappingVisitor flatVisitor = new RegularAsFlatMappingVisitor(tree);
for (IMappingProvider provider : providers) {
provider.load(new IMappingProvider.MappingAcceptor() {
@Override
public void acceptClass(String from, String to) {
tree.getOrCreateClass(from).setName(1, to);
try {
flatVisitor.visitClass(from, to);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public void acceptMethod(IMappingProvider.Member from, String to) {
tree.getOrCreateClass(from.owner).getOrCreateMethod(from.name, from.desc)
.setName(1, to);
try {
flatVisitor.visitMethod(from.owner, from.name, from.desc, to);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public void acceptMethodArg(IMappingProvider.Member from, int lvIndex, String to) {
tree.getOrCreateClass(from.owner).getOrCreateMethod(from.name, from.desc)
.getOrCreateParameter(lvIndex, "")
.setName(1, to);
try {
flatVisitor.visitMethodArg(from.owner, from.name, from.desc, lvIndex, lvIndex, "", to);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
@@ -81,14 +96,20 @@ public class MappingsProviderVerbose {
@Override
public void acceptField(IMappingProvider.Member from, String to) {
tree.getOrCreateClass(from.owner).getOrCreateField(from.name, from.desc)
.setName(1, to);
try {
flatVisitor.visitField(from.owner, from.name, from.desc, to);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
});
}
Path check = Files.createTempFile("CHECK", null);
Files.writeString(check, MappingsUtils.serializeToString(tree), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
StringWriter stringWriter = new StringWriter();
Tiny2Writer tiny2Writer = new Tiny2Writer(stringWriter, false);
tree.accept(tiny2Writer);
Files.writeString(check, stringWriter.toString(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
System.out.println("Saved debug check mappings to " + check);
}
}

View File

@@ -30,18 +30,19 @@ import java.nio.file.Path;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import dev.architectury.tinyremapper.IMappingProvider;
import dev.architectury.tinyremapper.TinyRemapper;
import org.gradle.api.Project;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.tinyremapper.IMappingProvider;
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>()
public 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")
@@ -53,12 +54,41 @@ public final class TinyRemapperHelper {
public static TinyRemapper getTinyRemapper(Project project, String fromM, String toM) throws IOException {
LoomGradleExtension extension = LoomGradleExtension.get(project);
return TinyRemapper.newRemapper()
.withMappings(create(extension.getMappingsProvider().getMappings(), fromM, toM, true))
.withMappings(out -> JSR_TO_JETBRAINS.forEach(out::acceptClass))
TinyRemapper remapper = _getTinyRemapper(project);
remapper.replaceMappings(ImmutableSet.of(
TinyRemapperHelper.create(extension.isForge() ? extension.getMappingsProvider().getMappingsWithSrg() : extension.getMappingsProvider().getMappings(), fromM, toM, true),
out -> TinyRemapperHelper.JSR_TO_JETBRAINS.forEach(out::acceptClass)
));
return remapper;
}
public static TinyRemapper _getTinyRemapper(Project project) throws IOException {
LoomGradleExtension extension = LoomGradleExtension.get(project);
TinyRemapper.Builder builder = TinyRemapper.newRemapper()
.renameInvalidLocals(true)
.rebuildSourceFilenames(true)
.build();
.logUnknownInvokeDynamic(false)
.ignoreConflicts(extension.isForge())
.cacheMappings(true)
.threads(Runtime.getRuntime().availableProcessors())
.logger(project.getLogger()::lifecycle)
.rebuildSourceFilenames(true);
if (extension.isForge()) {
/* FORGE: Required for classes like aej$OptionalNamedTag (1.16.4) which are added by Forge patches.
* They won't get remapped to their proper packages, so IllegalAccessErrors will happen without ._.
*/
builder.fixPackageAccess(true);
}
return builder.build();
}
public static TinyRemapper getTinyRemapper(Project project) throws IOException {
TinyRemapper remapper = _getTinyRemapper(project);
remapper.readClassPath(getMinecraftDependencies(project));
remapper.prepareClasses();
return remapper;
}
public static Path[] getMinecraftDependencies(Project project) {

View File

@@ -39,6 +39,7 @@ import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import net.fabricmc.accesswidener.AccessWidenerReader;
import net.fabricmc.accesswidener.AccessWidenerVisitor;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.task.RemapJarTask;
@@ -82,19 +83,19 @@ public final class Aw2At {
public static AccessTransformSet toAccessTransformSet(BufferedReader reader) throws IOException {
AccessTransformSet atSet = AccessTransformSet.create();
new AccessWidenerReader(new AccessWidenerReader.Visitor() {
new AccessWidenerReader(new AccessWidenerVisitor() {
@Override
public void visitClass(String name, AccessWidenerReader.AccessType access) {
public void visitClass(String name, AccessWidenerReader.AccessType access, boolean transitive) {
atSet.getOrCreateClass(name).merge(toAt(access));
}
@Override
public void visitMethod(String owner, String name, String descriptor, AccessWidenerReader.AccessType access) {
public void visitMethod(String owner, String name, String descriptor, AccessWidenerReader.AccessType access, boolean transitive) {
atSet.getOrCreateClass(owner).mergeMethod(MethodSignature.of(name, descriptor), toAt(access));
}
@Override
public void visitField(String owner, String name, String descriptor, AccessWidenerReader.AccessType access) {
public void visitField(String owner, String name, String descriptor, AccessWidenerReader.AccessType access, boolean transitive) {
atSet.getOrCreateClass(owner).mergeField(name, toAt(access));
}
}).read(reader);

View File

@@ -42,7 +42,7 @@ import org.gradle.api.logging.Logger;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.function.CollectionUtil;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.tree.MappingTree;
/**
* Remaps AT classes from SRG to Yarn.
@@ -50,7 +50,7 @@ import net.fabricmc.mapping.tree.TinyTree;
* @author Juuz
*/
public final class AtRemapper {
public static void remap(Logger logger, Path jar, TinyTree mappings) throws IOException {
public static void remap(Logger logger, Path jar, MappingTree mappings) throws IOException {
try (FileSystem fs = FileSystems.newFileSystem(URI.create("jar:" + jar.toUri()), ImmutableMap.of("create", false))) {
Path atPath = fs.getPath(Constants.Forge.ACCESS_TRANSFORMER_PATH);

View File

@@ -47,7 +47,7 @@ import com.google.gson.JsonObject;
import org.gradle.api.logging.Logger;
import net.fabricmc.loom.util.function.CollectionUtil;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.tree.MappingTree;
/**
* Remaps coremod class names from SRG to Yarn.
@@ -57,7 +57,7 @@ import net.fabricmc.mapping.tree.TinyTree;
public final class CoreModClassRemapper {
private static final Pattern CLASS_NAME_PATTERN = Pattern.compile("^(.*')((?:com\\.mojang\\.|net\\.minecraft\\.)[A-Za-z0-9.-_$]+)('.*)$");
public static void remapJar(Path jar, TinyTree mappings, Logger logger) throws IOException {
public static void remapJar(Path jar, MappingTree mappings, Logger logger) throws IOException {
try (FileSystem fs = FileSystems.newFileSystem(URI.create("jar:" + jar.toUri()), ImmutableMap.of("create", false))) {
Path coremodsJsonPath = fs.getPath("META-INF", "coremods.json");
@@ -86,7 +86,7 @@ public final class CoreModClassRemapper {
}
}
public static void remap(Path js, TinyTree mappings) throws IOException {
public static void remap(Path js, MappingTree mappings) throws IOException {
List<String> lines = Files.readAllLines(js);
List<String> output = new ArrayList<>(lines);

View File

@@ -39,11 +39,10 @@ import dev.architectury.tinyremapper.IMappingProvider;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.FileSystemUtil.FileSystemDelegate;
import net.fabricmc.mapping.tree.ClassDef;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.tree.MappingTree;
public class InnerClassRemapper {
public static IMappingProvider of(Set<String> fromClassNames, TinyTree mappingsWithSrg, String from, String to) throws IOException {
public static IMappingProvider of(Set<String> fromClassNames, MappingTree mappingsWithSrg, String from, String to) throws IOException {
return sink -> {
remapInnerClass(fromClassNames, mappingsWithSrg, from, to, sink::acceptClass);
};
@@ -72,10 +71,10 @@ public class InnerClassRemapper {
return set;
}
private static void remapInnerClass(Set<String> classNames, TinyTree mappingsWithSrg, String from, String to, BiConsumer<String, String> action) {
private static void remapInnerClass(Set<String> classNames, MappingTree mappingsWithSrg, String from, String to, BiConsumer<String, String> action) {
BiMap<String, String> availableClasses = HashBiMap.create(mappingsWithSrg.getClasses().stream()
.collect(Collectors.groupingBy(classDef -> classDef.getName(from),
Collectors.<ClassDef, String>reducing(
Collectors.<MappingTree.ClassMapping, String>reducing(
null,
classDef -> classDef.getName(to),
(first, last) -> last

View File

@@ -42,11 +42,6 @@ import java.util.regex.Pattern;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
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.MutableTinyTree;
import dev.architectury.mappingslayers.api.utils.MappingsUtils;
import org.apache.commons.io.IOUtils;
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.io.srg.tsrg.TSrgReader;
@@ -57,6 +52,9 @@ import org.cadixdev.lorenz.model.MethodMapping;
import org.cadixdev.lorenz.model.TopLevelClassMapping;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.stitch.commands.tinyv2.TinyClass;
import net.fabricmc.stitch.commands.tinyv2.TinyField;
import net.fabricmc.stitch.commands.tinyv2.TinyFile;
@@ -204,21 +202,22 @@ public class MCPReader {
return tokens;
}
private void readTsrg2(Map<MemberToken, String> tokens, String content) {
MutableTinyTree tree = MappingsUtils.deserializeFromTsrg2(content);
int obfIndex = tree.getMetadata().index("obf");
int srgIndex = tree.getMetadata().index("srg");
private void readTsrg2(Map<MemberToken, String> tokens, String content) throws IOException {
MemoryMappingTree tree = new MemoryMappingTree();
MappingReader.read(new StringReader(content), tree);
int obfIndex = tree.getNamespaceId("obf");
int srgIndex = tree.getNamespaceId("srg");
for (MutableClassDef classDef : tree.getClassesMutable()) {
for (MappingTree.ClassMapping classDef : tree.getClasses()) {
MemberToken ofClass = MemberToken.ofClass(classDef.getName(obfIndex));
tokens.put(ofClass, classDef.getName(srgIndex));
for (MutableFieldDef fieldDef : classDef.getFieldsMutable()) {
for (MappingTree.FieldMapping fieldDef : classDef.getFields()) {
tokens.put(MemberToken.ofField(ofClass, fieldDef.getName(obfIndex)), fieldDef.getName(srgIndex));
}
for (MutableMethodDef methodDef : classDef.getMethodsMutable()) {
tokens.put(MemberToken.ofMethod(ofClass, methodDef.getName(obfIndex), methodDef.getDescriptor(obfIndex)),
for (MappingTree.MethodMapping methodDef : classDef.getMethods()) {
tokens.put(MemberToken.ofMethod(ofClass, methodDef.getName(obfIndex), methodDef.getDesc(obfIndex)),
methodDef.getName(srgIndex));
}
}

View File

@@ -29,25 +29,34 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import org.apache.commons.io.IOUtils;
import com.google.common.base.Stopwatch;
import org.apache.commons.io.output.NullOutputStream;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.configuration.ShowStacktrace;
import org.zeroturnaround.zip.ZipUtil;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.ThreadingUtils;
public class SpecialSourceExecutor {
private static String trimLeadingSlash(String string) {
if (string.startsWith(File.separator)) {
return string.substring(File.separator.length());
} else if (string.startsWith("/")) {
return string.substring(1);
}
return string;
}
public static Path produceSrgJar(boolean specialSource, Project project, String side, FileCollection classpath, Set<File> mcLibs, Path officialJar, Path mappings)
throws Exception {
Set<String> filter = Files.readAllLines(mappings, StandardCharsets.UTF_8).stream()
@@ -58,14 +67,35 @@ public class SpecialSourceExecutor {
Path stripped = extension.getFiles().getProjectBuildCache().toPath().resolve(officialJar.getFileName().toString().substring(0, officialJar.getFileName().toString().length() - 4) + "-filtered.jar");
Files.deleteIfExists(stripped);
try (JarOutputStream output = new JarOutputStream(Files.newOutputStream(stripped))) {
ZipUtil.iterate(officialJar.toFile(), (in, zipEntry) -> {
if (filter.contains(zipEntry.getName())) {
output.putNextEntry((ZipEntry) zipEntry.clone());
IOUtils.write(IOUtils.toByteArray(in), output);
output.closeEntry();
Stopwatch stopwatch = Stopwatch.createStarted();
try (FileSystemUtil.FileSystemDelegate output = FileSystemUtil.getJarFileSystem(stripped, true)) {
try (FileSystemUtil.FileSystemDelegate fs = FileSystemUtil.getJarFileSystem(officialJar, false)) {
ThreadingUtils.TaskCompleter completer = ThreadingUtils.taskCompleter();
for (Path path : (Iterable<? extends Path>) Files.walk(fs.get().getPath("/"))::iterator) {
String trimLeadingSlash = trimLeadingSlash(path.toString());
if (!trimLeadingSlash.endsWith(".class")) continue;
boolean has = filter.contains(trimLeadingSlash);
String s = trimLeadingSlash;
while (s.contains("$") && !has) {
s = s.substring(0, s.lastIndexOf("$")) + ".class";
has = filter.contains(s);
}
if (!has) continue;
Path to = output.get().getPath(trimLeadingSlash);
Path parent = to.getParent();
if (parent != null) Files.createDirectories(parent);
completer.add(() -> {
Files.copy(path, to, StandardCopyOption.COPY_ATTRIBUTES);
});
}
});
completer.complete();
}
} finally {
project.getLogger().info("Copied class files in " + stopwatch.stop());
}
Path output = extension.getFiles().getProjectBuildCache().toPath().resolve(officialJar.getFileName().toString().substring(0, officialJar.getFileName().toString().length() - 4) + "-srg-output.jar");
@@ -93,7 +123,7 @@ public class SpecialSourceExecutor {
// if running with INFO or DEBUG logging
if (project.getGradle().getStartParameter().getShowStacktrace() != ShowStacktrace.INTERNAL_EXCEPTIONS
|| project.getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) < 0) {
|| project.getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) < 0) {
spec.setStandardOutput(System.out);
spec.setErrorOutput(System.err);
} else {
@@ -131,7 +161,7 @@ public class SpecialSourceExecutor {
// if running with INFO or DEBUG logging
if (project.getGradle().getStartParameter().getShowStacktrace() != ShowStacktrace.INTERNAL_EXCEPTIONS
|| project.getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) < 0) {
|| project.getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) < 0) {
spec.setStandardOutput(System.out);
spec.setErrorOutput(System.err);
} else {

View File

@@ -37,14 +37,10 @@ import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.base.MoreObjects;
import dev.architectury.mappingslayers.api.mutable.MutableClassDef;
import dev.architectury.mappingslayers.api.mutable.MutableDescriptored;
import dev.architectury.mappingslayers.api.mutable.MutableFieldDef;
import dev.architectury.mappingslayers.api.mutable.MutableMethodDef;
import dev.architectury.mappingslayers.api.mutable.MutableTinyTree;
import dev.architectury.mappingslayers.api.utils.MappingsUtils;
import org.apache.commons.io.IOUtils;
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.io.srg.tsrg.TSrgReader;
@@ -56,12 +52,11 @@ import org.cadixdev.lorenz.model.TopLevelClassMapping;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.loom.util.function.CollectionUtil;
import net.fabricmc.mapping.tree.ClassDef;
import net.fabricmc.mapping.tree.FieldDef;
import net.fabricmc.mapping.tree.MethodDef;
import net.fabricmc.mapping.tree.TinyMappingFactory;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.format.TsrgReader;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTreeView;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.stitch.commands.tinyv2.TinyClass;
import net.fabricmc.stitch.commands.tinyv2.TinyField;
import net.fabricmc.stitch.commands.tinyv2.TinyFile;
@@ -87,15 +82,15 @@ public final class SrgMerger {
* or if an element mentioned in the SRG file does not have tiny mappings
*/
public static void mergeSrg(Supplier<Path> mojmap, Path srg, Path tiny, Path out, boolean lenient) throws IOException, MappingException {
Map<String, List<MutableDescriptored>> addRegardlessSrgs = new HashMap<>();
Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs = new HashMap<>();
MappingSet arr = readSrg(srg, mojmap, addRegardlessSrgs);
TinyTree foss;
MemoryMappingTree foss = new MemoryMappingTree();
try (BufferedReader reader = Files.newBufferedReader(tiny)) {
foss = TinyMappingFactory.loadWithDetection(reader);
MappingReader.read(reader, foss);
}
List<String> namespaces = new ArrayList<>(foss.getMetadata().getNamespaces());
List<String> namespaces = Stream.concat(Stream.of(foss.getSrcNamespace()), foss.getDstNamespaces().stream()).collect(Collectors.toList());
namespaces.add(1, "srg");
if (!"official".equals(namespaces.get(0))) {
@@ -114,7 +109,8 @@ public final class SrgMerger {
TinyV2Writer.write(file, out);
}
private static MappingSet readSrg(Path srg, Supplier<Path> mojmap, Map<String, List<MutableDescriptored>> addRegardlessSrgs) throws IOException {
private static MappingSet readSrg(Path srg, Supplier<Path> mojmap, Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs)
throws IOException {
try (BufferedReader reader = Files.newBufferedReader(srg)) {
String content = IOUtils.toString(reader);
@@ -128,26 +124,27 @@ public final class SrgMerger {
}
}
private static MappingSet readTsrg2(String content, Supplier<Path> mojmap, Map<String, List<MutableDescriptored>> addRegardlessSrgs) throws IOException {
private static MappingSet readTsrg2(String content, Supplier<Path> mojmap, Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs)
throws IOException {
MappingSet set;
try (Tsrg2Utils.MappingsIO2LorenzWriter lorenzWriter = new Tsrg2Utils.MappingsIO2LorenzWriter(0, false)) {
TsrgReader.read(new StringReader(content), lorenzWriter);
set = lorenzWriter.read();
MutableTinyTree mojmapTree = readTsrg2ToTinyTree(mojmap.get());
MemoryMappingTree mojmapTree = readTsrg2ToTinyTree(mojmap.get());
for (MutableClassDef classDef : mojmapTree.getClassesMutable()) {
for (MutableMethodDef methodDef : classDef.getMethodsMutable()) {
String name = methodDef.getName(0);
for (MappingTree.ClassMapping classDef : mojmapTree.getClasses()) {
for (MappingTree.MethodMapping methodDef : classDef.getMethods()) {
String name = methodDef.getSrcName();
if (name.indexOf('<') != 0 && name.equals(methodDef.getName(1))) {
addRegardlessSrgs.computeIfAbsent(classDef.getName(0), $ -> new ArrayList<>()).add(methodDef);
if (name.indexOf('<') != 0 && name.equals(methodDef.getDstName(0))) {
addRegardlessSrgs.computeIfAbsent(classDef.getSrcName(), $ -> new ArrayList<>()).add(methodDef);
}
}
for (MutableFieldDef fieldDef : classDef.getFieldsMutable()) {
if (fieldDef.getName(0).equals(fieldDef.getName(1))) {
addRegardlessSrgs.computeIfAbsent(classDef.getName(0), $ -> new ArrayList<>()).add(fieldDef);
for (MappingTree.FieldMapping fieldDef : classDef.getFields()) {
if (fieldDef.getSrcName().equals(fieldDef.getDstName(0))) {
addRegardlessSrgs.computeIfAbsent(classDef.getSrcName(), $ -> new ArrayList<>()).add(fieldDef);
}
}
}
@@ -156,20 +153,20 @@ public final class SrgMerger {
return set;
}
private static MutableTinyTree readTsrg2ToTinyTree(Path path) throws IOException {
MutableTinyTree tree;
private static MemoryMappingTree readTsrg2ToTinyTree(Path path) throws IOException {
MemoryMappingTree tree = new MemoryMappingTree();
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
tree = MappingsUtils.deserializeFromTsrg2(IOUtils.toString(reader));
MappingReader.read(reader, tree);
}
return tree;
}
private static void classToTiny(Map<String, List<MutableDescriptored>> addRegardlessSrgs, TinyTree foss, List<String> namespaces, ClassMapping<?, ?> klass, Consumer<TinyClass> classConsumer, boolean lenient) {
private static void classToTiny(Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs, MappingTree foss, List<String> namespaces, ClassMapping<?, ?> klass, Consumer<TinyClass> classConsumer, boolean lenient) {
String obf = klass.getFullObfuscatedName();
String srg = klass.getFullDeobfuscatedName();
ClassDef classDef = foss.getDefaultNamespaceClassMap().get(obf);
MappingTree.ClassMapping classDef = foss.getClass(obf);
if (classDef == null) {
if (lenient) {
@@ -188,9 +185,9 @@ public final class SrgMerger {
List<TinyField> fields = new ArrayList<>();
for (MethodMapping method : klass.getMethodMappings()) {
MethodDef def = CollectionUtil.find(
MappingTree.MethodMapping def = CollectionUtil.find(
classDef.getMethods(),
m -> m.getName("official").equals(method.getObfuscatedName()) && m.getDescriptor("official").equals(method.getObfuscatedDescriptor())
m -> m.getName("official").equals(method.getObfuscatedName()) && m.getDesc("official").equals(method.getObfuscatedDescriptor())
).orElse(null);
if (def == null) {
@@ -209,7 +206,7 @@ public final class SrgMerger {
);
methods.add(new TinyMethod(
def.getDescriptor("official"), methodNames,
def.getDesc("official"), methodNames,
/* parameters */ Collections.emptyList(),
/* locals */ Collections.emptyList(),
/* comments */ Collections.emptyList()
@@ -217,7 +214,7 @@ public final class SrgMerger {
}
for (FieldMapping field : klass.getFieldMappings()) {
FieldDef def = CollectionUtil.find(
MappingTree.FieldMapping def = CollectionUtil.find(
classDef.getFields(),
f -> f.getName("official").equals(field.getObfuscatedName())
).orElse(nullOrThrow(lenient, () -> new MappingException("Missing field: " + field.getFullObfuscatedName() + " (srg: " + field.getFullDeobfuscatedName() + ")")));
@@ -229,7 +226,7 @@ public final class SrgMerger {
namespace -> "srg".equals(namespace) ? field.getDeobfuscatedName() : def.getName(namespace)
);
fields.add(new TinyField(def.getDescriptor("official"), fieldNames, Collections.emptyList()));
fields.add(new TinyField(def.getDesc("official"), fieldNames, Collections.emptyList()));
}
TinyClass tinyClass = new TinyClass(classNames, methods, fields, Collections.emptyList());
@@ -240,13 +237,13 @@ public final class SrgMerger {
}
}
private static boolean tryMatchRegardlessSrgs(Map<String, List<MutableDescriptored>> addRegardlessSrgs, List<String> namespaces, String obf,
private static boolean tryMatchRegardlessSrgs(Map<String, List<MappingTreeView.MemberMappingView>> addRegardlessSrgs, List<String> namespaces, String obf,
List<TinyMethod> methods, MethodMapping method) {
List<MutableDescriptored> mutableDescriptoredList = addRegardlessSrgs.get(obf);
List<MappingTreeView.MemberMappingView> mutableDescriptoredList = addRegardlessSrgs.get(obf);
if (!method.getDeobfuscatedName().equals(method.getObfuscatedName())) {
for (MutableDescriptored descriptored : MoreObjects.firstNonNull(mutableDescriptoredList, Collections.<MutableDescriptored>emptyList())) {
if (descriptored.isMethod() && descriptored.getName(0).equals(method.getObfuscatedName()) && descriptored.getDescriptor(0).equals(method.getObfuscatedDescriptor())) {
for (MappingTreeView.MemberMappingView descriptored : MoreObjects.firstNonNull(mutableDescriptoredList, Collections.<MappingTreeView.MemberMappingView>emptyList())) {
if (descriptored instanceof MappingTreeView.MethodMappingView && descriptored.getSrcName().equals(method.getObfuscatedName()) && descriptored.getSrcDesc().equals(method.getObfuscatedDescriptor())) {
List<String> methodNames = CollectionUtil.map(
namespaces,
namespace -> "srg".equals(namespace) ? method.getDeobfuscatedName() : method.getObfuscatedName()

View File

@@ -32,10 +32,10 @@ import org.cadixdev.lorenz.io.srg.SrgWriter;
import org.gradle.api.logging.Logger;
import net.fabricmc.lorenztiny.TinyMappingsReader;
import net.fabricmc.mapping.tree.TinyTree;
import net.fabricmc.mappingio.tree.MappingTree;
public class SrgNamedWriter {
public static void writeTo(Logger logger, Path srgFile, TinyTree mappings, String from, String to) throws IOException {
public static void writeTo(Logger logger, Path srgFile, MappingTree mappings, String from, String to) throws IOException {
Files.deleteIfExists(srgFile);
try (SrgWriter writer = new SrgWriter(Files.newBufferedWriter(srgFile))) {

View File

@@ -25,22 +25,11 @@
package net.fabricmc.loom.util.srg;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
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.MutableTinyMetadata;
import dev.architectury.mappingslayers.api.mutable.MutableTinyTree;
import dev.architectury.mappingslayers.api.utils.MappingsUtils;
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.io.srg.tsrg.TSrgWriter;
import org.cadixdev.lorenz.model.ClassMapping;
@@ -49,21 +38,10 @@ import org.cadixdev.lorenz.model.MethodMapping;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.MappingWriter;
import net.fabricmc.mappingio.adapter.ForwardingMappingVisitor;
import net.fabricmc.mappingio.format.TsrgReader;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
public class Tsrg2Utils {
public static void convert(Reader reader, Writer writer) throws IOException {
writeTsrg(visitor -> {
try {
TsrgReader.read(reader, visitor);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}, "srg", false, writer);
}
public static void writeTsrg(Consumer<MappingVisitor> visitorConsumer, String dstNamespace, boolean applyParameterMappings, Writer writer)
throws IOException {
MappingSet set;
@@ -78,18 +56,6 @@ public class Tsrg2Utils {
}
}
public static void writeTsrg2(Consumer<MappingVisitor> visitorConsumer, Writer writer)
throws IOException {
MutableTinyTree tree;
try (MappingsIO2MappingsUtils w = new MappingsIO2MappingsUtils()) {
visitorConsumer.accept(w);
tree = w.read();
}
writer.write(MappingsUtils.serializeToTsrg2(tree));
}
// TODO Move this elsewhere
public abstract static class MappingsIO2Others extends ForwardingMappingVisitor implements MappingWriter {
public MappingsIO2Others() {
@@ -175,59 +141,4 @@ public class Tsrg2Utils {
return mappings;
}
}
public static class MappingsIO2MappingsUtils extends MappingsIO2Others {
public MutableTinyTree read() {
MappingTree tree = tree();
int dstNamesSize = tree.getDstNamespaces().size();
List<String> namespaces = new ArrayList<>();
namespaces.add(tree.getSrcNamespace());
namespaces.addAll(tree.getDstNamespaces());
Map<String, String> properties = new HashMap<>();
for (Map.Entry<String, String> entry : tree.getMetadata()) {
properties.put(entry.getKey(), entry.getValue());
}
MutableTinyTree out = MappingsUtils.create(MutableTinyMetadata.create(2, 0, namespaces, properties));
for (MappingTree.ClassMapping aClass : tree.getClasses()) {
MutableClassDef classDef = out.getOrCreateClass(aClass.getSrcName());
classDef.setComment(aClass.getComment());
for (int i = 0; i < dstNamesSize; i++) {
classDef.setName(i + 1, aClass.getDstName(i));
}
for (MappingTree.MethodMapping aMethod : aClass.getMethods()) {
MutableMethodDef methodDef = classDef.getOrCreateMethod(aMethod.getSrcName(), aMethod.getSrcDesc());
methodDef.setComment(aMethod.getComment());
for (int i = 0; i < dstNamesSize; i++) {
methodDef.setName(i + 1, aMethod.getDstName(i));
}
for (MappingTree.MethodArgMapping aMethodArg : aMethod.getArgs()) {
MutableParameterDef parameterDef = methodDef.getOrCreateParameter(aMethodArg.getLvIndex(), aMethodArg.getSrcName());
parameterDef.setComment(aMethodArg.getComment());
for (int i = 0; i < dstNamesSize; i++) {
parameterDef.setName(i + 1, aMethodArg.getDstName(i));
}
}
}
for (MappingTree.FieldMapping aField : aClass.getFields()) {
MutableFieldDef fieldDef = classDef.getOrCreateField(aField.getSrcName(), aField.getSrcDesc());
fieldDef.setComment(aField.getComment());
for (int i = 0; i < dstNamesSize; i++) {
fieldDef.setName(i + 1, aField.getDstName(i));
}
}
}
return out;
}
}
}

View File

@@ -0,0 +1,86 @@
package net.fabricmc.loom.util.srg;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTreeView;
public class Tsrg2Writer {
public Tsrg2Writer() {
}
public static String serialize(MappingTree tree) {
List<String> namespaces = Stream.concat(Stream.of(tree.getSrcNamespace()), tree.getDstNamespaces().stream()).collect(Collectors.toList());
StringBuilder builder = new StringBuilder();
writeHeader(namespaces, builder);
for (MappingTree.ClassMapping classMapping : tree.getClasses()) {
writeClass(namespaces, classMapping, builder);
}
return builder.toString();
}
private static void writeClass(List<String> namespaces, MappingTree.ClassMapping def, StringBuilder builder) {
writeMapped(false, namespaces, def, builder);
for (MappingTree.MethodMapping method : def.getMethods()) {
writeMethod(namespaces, method, builder);
}
for (MappingTree.FieldMapping field : def.getFields()) {
writeMapped(true, namespaces, field, builder);
}
}
private static void writeMethod(List<String> namespaces, MappingTree.MethodMapping def, StringBuilder builder) {
writeMapped(true, namespaces, def, builder);
for (MappingTree.MethodArgMapping arg : def.getArgs()) {
builder.append("\t\t").append(arg.getLvIndex());
writeMapped(true, namespaces, arg, builder);
}
}
private static void writeField(List<String> namespaces, MappingTree.FieldMapping def, StringBuilder builder) {
writeMapped(true, namespaces, def, builder);
}
private static void writeMapped(boolean needFirst, List<String> namespaces, MappingTreeView.ElementMappingView mapped, StringBuilder builder) {
String[] names = namespaces.stream().map(mapped::getName).toArray(String[]::new);
for (int i = 0; i < names.length; ++i) {
String name = names[i];
if (i == 0) {
if (needFirst) {
builder.append('\t');
}
} else {
builder.append(' ');
}
builder.append(name);
if (i == 0 && mapped instanceof MappingTreeView.MemberMappingView) {
String descriptor = ((MappingTreeView.MemberMappingView) mapped).getSrcDesc();
if (descriptor != null && !descriptor.isEmpty()) {
builder.append(' ');
builder.append(descriptor);
}
}
}
builder.append('\n');
}
private static void writeHeader(List<String> namespaces, StringBuilder builder) {
builder.append("tsrg2");
for (String namespace : namespaces) {
builder.append(' ');
builder.append(namespace);
}
builder.append('\n');
}
}