Skip to content

Commit

Permalink
Fix importing mappings without field source descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas committed Nov 16, 2023
1 parent 2964590 commit 423f5cf
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 17 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ subprojects {
dependencies {
implementation 'com.google.guava:guava:30.1.1-jre'
implementation 'com.google.code.gson:gson:2.8.7'

implementation 'net.fabricmc:mapping-io:0.5.0'

compileOnly 'org.jetbrains:annotations:24.0.1'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.hamcrest:hamcrest:2.2'
Expand Down
4 changes: 2 additions & 2 deletions enigma-cli/src/main/java/cuchaz/enigma/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ protected static EnigmaProject openProject(Path fileJarIn, Path fileMappings) th
protected static EntryTree<EntryMapping> readMappings(Path path, ProgressListener progress, MappingSaveParameters saveParameters) throws IOException, MappingParseException {
// Legacy
if ("zip".equalsIgnoreCase(MoreFiles.getFileExtension(path))) {
return MappingFormat.ENIGMA_ZIP.read(path, progress, saveParameters);
return MappingFormat.ENIGMA_ZIP.read(path, progress, saveParameters, null);
}

net.fabricmc.mappingio.format.MappingFormat format = MappingReader.detectFormat(path);
if (format == null) throw new IllegalArgumentException("Unknown mapping format!");

VisitableMappingTree tree = new MemoryMappingTree();
MappingReader.read(path, format, tree);
return MappingIoConverter.fromMappingIo(tree, progress);
return MappingIoConverter.fromMappingIo(tree, progress, null);
}

protected static File getWritableFile(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ private MappingCommandsUtil() {

public static EntryTree<EntryMapping> read(String type, Path path, MappingSaveParameters saveParameters) throws MappingParseException, IOException {
if (type.equals("enigma")) {
return (Files.isDirectory(path) ? MappingFormat.ENIGMA_DIRECTORY : MappingFormat.ENIGMA_ZIP).read(path, ProgressListener.none(), saveParameters);
return (Files.isDirectory(path) ? MappingFormat.ENIGMA_DIRECTORY : MappingFormat.ENIGMA_ZIP).read(path, ProgressListener.none(), saveParameters, null);
}

if (type.equals("tiny")) {
return MappingFormat.TINY_FILE.read(path, ProgressListener.none(), saveParameters);
return MappingFormat.TINY_FILE.read(path, ProgressListener.none(), saveParameters, null);
}

MappingFormat format = null;
Expand All @@ -41,7 +41,7 @@ public static EntryTree<EntryMapping> read(String type, Path path, MappingSavePa
}

if (format != null) {
return format.read(path, ProgressListener.none(), saveParameters);
return format.read(path, ProgressListener.none(), saveParameters, null);
}

throw new IllegalArgumentException("no reader for " + type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static void main(String[] args) {
mappingFormat = MappingFormat.ENIGMA_FILE;
}

mappings = EntryRemapper.mapped(project.getJarIndex(), mappingFormat.read(mappingsFile, ProgressListener.none(), profile.getMappingSaveParameters()));
mappings = EntryRemapper.mapped(project.getJarIndex(), mappingFormat.read(mappingsFile, ProgressListener.none(), profile.getMappingSaveParameters(), project.getJarIndex()));
}

PrintWriter log = new PrintWriter(Files.newBufferedWriter(logFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public CompletableFuture<Void> openMappings(MappingFormat format, Path path) {
return ProgressDialog.runOffThread(gui.getFrame(), progress -> {
try {
MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
project.setMappings(format.read(path, progress, saveParameters));
project.setMappings(format.read(path, progress, saveParameters, project.getJarIndex()));

loadedMappingFormat = format;
loadedMappingPath = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.fabricmc.mappingio.tree.VisitableMappingTree;

import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.MappingDelta;
import cuchaz.enigma.translation.mapping.serde.enigma.EnigmaMappingsReader;
Expand Down Expand Up @@ -82,7 +83,12 @@ public void write(EntryTree<EntryMapping> mappings, MappingDelta<EntryMapping> d
}
}

@Deprecated
public EntryTree<EntryMapping> read(Path path, ProgressListener progressListener, MappingSaveParameters saveParameters) throws IOException, MappingParseException {
return read(path, progressListener, saveParameters, null);
}

public EntryTree<EntryMapping> read(Path path, ProgressListener progressListener, MappingSaveParameters saveParameters, JarIndex index) throws IOException, MappingParseException {
if (!useMappingIo()) {
if (reader == null) {
throw new IllegalStateException(name() + " does not support reading");
Expand All @@ -103,7 +109,7 @@ public EntryTree<EntryMapping> read(Path path, ProgressListener progressListener

VisitableMappingTree mappingTree = new MemoryMappingTree();
MappingReader.read(path, mappingIoCounterpart, mappingTree);
return MappingIoConverter.fromMappingIo(mappingTree, progressListener);
return MappingIoConverter.fromMappingIo(mappingTree, progressListener, index);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.util.List;
import java.util.stream.StreamSupport;

import javax.annotation.Nullable;

import org.jetbrains.annotations.ApiStatus;
import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.mappingio.tree.VisitableMappingTree;
Expand All @@ -17,6 +20,7 @@
import net.fabricmc.mappingio.tree.MappingTree.MethodVarMapping;

import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.translation.mapping.EntryMap;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
Expand All @@ -31,6 +35,7 @@
import cuchaz.enigma.translation.representation.entry.MethodEntry;
import cuchaz.enigma.utils.I18n;

@ApiStatus.Internal
public class MappingIoConverter {
public static VisitableMappingTree toMappingIo(EntryTree<EntryMapping> mappings, ProgressListener progress) {
return toMappingIo(mappings, progress, "intermediary", "named");
Expand Down Expand Up @@ -174,20 +179,20 @@ private static void writeMethodVar(EntryTreeNode<EntryMapping> varNode, Visitabl
mappingTree.visitComment(MappedElementKind.METHOD_VAR, varMapping.javadoc());
}

public static EntryTree<EntryMapping> fromMappingIo(VisitableMappingTree mappingTree, ProgressListener progress) {
public static EntryTree<EntryMapping> fromMappingIo(VisitableMappingTree mappingTree, ProgressListener progress, @Nullable JarIndex index) {
EntryTree<EntryMapping> dstMappingTree = new HashEntryTree<>();
progress.init(mappingTree.getClasses().size(), I18n.translate("progress.mappings.converting.from_mappingio"));
int steps = 0;

for (ClassMapping classMapping : mappingTree.getClasses()) {
progress.step(steps++, classMapping.getDstName(0) != null ? classMapping.getDstName(0) : classMapping.getSrcName());
readClass(classMapping, dstMappingTree);
readClass(classMapping, dstMappingTree, index);
}

return dstMappingTree;
}

private static void readClass(ClassMapping classMapping, EntryTree<EntryMapping> mappingTree) {
private static void readClass(ClassMapping classMapping, EntryTree<EntryMapping> mappingTree, JarIndex index) {
ClassEntry currentClass = new ClassEntry(classMapping.getSrcName());
String dstName = classMapping.getDstName(0);

Expand All @@ -198,17 +203,48 @@ private static void readClass(ClassMapping classMapping, EntryTree<EntryMapping>
mappingTree.insert(currentClass, new EntryMapping(dstName, classMapping.getComment()));

for (FieldMapping fieldMapping : classMapping.getFields()) {
readField(fieldMapping, currentClass, mappingTree);
readField(fieldMapping, currentClass, mappingTree, index);
}

for (MethodMapping methodMapping : classMapping.getMethods()) {
readMethod(methodMapping, currentClass, mappingTree);
}
}

private static void readField(FieldMapping fieldMapping, ClassEntry parent, EntryTree<EntryMapping> mappingTree) {
mappingTree.insert(new FieldEntry(parent, fieldMapping.getSrcName(), new TypeDescriptor(fieldMapping.getSrcDesc())),
new EntryMapping(fieldMapping.getDstName(0), fieldMapping.getComment()));
private static void readField(FieldMapping fieldMapping, ClassEntry parent, EntryTree<EntryMapping> mappingTree, JarIndex index) {
String srcDesc = fieldMapping.getSrcDesc();
FieldEntry[] fieldEntries;

if (srcDesc != null) {
fieldEntries = new FieldEntry[] { new FieldEntry(parent, fieldMapping.getSrcName(), new TypeDescriptor(fieldMapping.getSrcDesc())) };
} else {
if (index == null) return; // Enigma requires source descriptors, and without an index we can't look them up

fieldEntries = index.getChildrenByClass().get(parent).stream()
.filter(entry -> entry instanceof FieldEntry)
.filter(entry -> entry.getName().equals(fieldMapping.getSrcName()))
.toArray(FieldEntry[]::new);

if (fieldEntries.length == 0) { // slow path for synthetics
fieldEntries = index.getEntryIndex().getFields().stream()
.filter(entry -> entry.getParent().getFullName().equals(parent.getFullName()))
.filter(entry -> {
if (entry.getName().equals(fieldMapping.getSrcName())) {
return true;
} else {
System.out.println("Entry name: " + entry.getName() + ", mapping name: " + fieldMapping.getSrcName());
return false;
}
})
.toArray(FieldEntry[]::new);
}

if (fieldEntries.length == 0) return; // No target found, invalid mapping
}

for (FieldEntry fieldEntry : fieldEntries) {
mappingTree.insert(fieldEntry, new EntryMapping(fieldMapping.getDstName(0), fieldMapping.getComment()));
}
}

private static void readMethod(MethodMapping methodMapping, ClassEntry parent, EntryTree<EntryMapping> mappingTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void testReadWriteCycle(MappingFormat mappingFormat, boolean supportsJav
mappingFormat.write(testMappings, tempFile.toPath(), ProgressListener.none(), parameters);
Assert.assertTrue("Written file not created", tempFile.exists());

EntryTree<EntryMapping> loadedMappings = mappingFormat.read(tempFile.toPath(), ProgressListener.none(), parameters);
EntryTree<EntryMapping> loadedMappings = mappingFormat.read(tempFile.toPath(), ProgressListener.none(), parameters, null);

Assert.assertTrue("Loaded mappings don't contain testClazz", loadedMappings.contains(testClazz.a));
Assert.assertTrue("Loaded mappings don't contain testField1", loadedMappings.contains(testField1.a));
Expand Down

0 comments on commit 423f5cf

Please sign in to comment.