Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mapping-IO IO module #14

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ guava = '32.0.1-jre'
# IO subproject dependencies
gson = '2.10.1'
moshi = '1.12.0' # Fixed to 1.12.0 because that's the last version written in Java
mappingio = '0.5.1'
jetbrains-annotations = '24.0.1'

# Test engine
junit = '5.9.3'
Expand All @@ -16,6 +18,8 @@ guava = { module = 'com.google.guava:guava', version.ref = 'guava' }

gson = { module = 'com.google.code.gson:gson', version.ref = 'gson' }
moshi = { module = 'com.squareup.moshi:moshi', version.ref = 'moshi' }
mappingio = { module = 'net.fabricmc:mapping-io', version.ref = 'mappingio' }
jetbrains-annotations = { module = 'org.jetbrains:annotations', version.ref = 'jetbrains-annotations' }

junit-api = { module = 'org.junit.jupiter:junit-jupiter-api', version.ref = 'junit' }
junit-engine = { module = 'org.junit.jupiter:junit-jupiter-engine', version.ref = 'junit' }
22 changes: 22 additions & 0 deletions io-mappingio/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
group = 'org.parchmentmc.feather'
archivesBaseName = 'io-mappingio'

dependencies {
api project(':feather')
api libs.mappingio

compileOnly libs.jetbrains.annotations
testCompileOnly libs.jetbrains.annotations

testFixturesApi testFixtures(project(':feather'))
}

publishing {
publications.create("mappingioIO", MavenPublication) {
from components.java
pom {
name = "Feather IO - Mapping-IO"
description = "Additional IO library for serializing various deobfuscation mapping formats using Mapping-IO."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.parchmentmc.feather.io.mappingio;

import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import org.parchmentmc.feather.mapping.MappingDataContainer;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class MdcToMio {
/**
* Constructs a new {@link MappingTree} from a given {@link MappingDataContainer}'s content.
* All data is added to the source namespace.
*
* @param mdc The {@link MappingDataContainer} to read from.
* @param srcNs The source namespace to use for the tree.
* @return The constructed {@link MappingTree}.
* @throws IOException If an I/O error occurs.
*/
public static MappingTree toTree(MappingDataContainer mdc, String srcNs) throws IOException {
MemoryMappingTree tree = new MemoryMappingTree();
accept(mdc, tree, srcNs);
return tree;
}

/**
* Reads mapping data from a given {@link MappingDataContainer} and passes it to a {@link MappingVisitor} (on the source namespace).
*
* @param mdc The {@link MappingDataContainer} to read from.
* @param visitor The {@link MappingVisitor} to pass the data to.
* @param srcNs The source namespace to pass to the visitor.
* @throws IOException If an I/O error occurs.
*/
public static void accept(MappingDataContainer mdc, MappingVisitor visitor, String srcNs) throws IOException {
if (visitor.visitHeader()) {
visitor.visitNamespaces(srcNs, Collections.emptyList());
}

if (visitor.visitContent()) {
for (MappingDataContainer.ClassData cls : mdc.getClasses()) {
if (!visitor.visitClass(cls.getName())) continue;
if (!visitor.visitElementContent(MappedElementKind.CLASS)) continue;

if (!cls.getJavadoc().isEmpty()) {
visitor.visitComment(MappedElementKind.CLASS, joinComment(cls.getJavadoc()));
}

for (MappingDataContainer.FieldData fld : cls.getFields()) {
if (!visitor.visitField(fld.getName(), fld.getDescriptor())) continue;
if (!visitor.visitElementContent(MappedElementKind.FIELD)) continue;

if (!fld.getJavadoc().isEmpty()) {
visitor.visitComment(MappedElementKind.FIELD, joinComment(fld.getJavadoc()));
}
}

for (MappingDataContainer.MethodData mth : cls.getMethods()) {
if (!visitor.visitMethod(mth.getName(), mth.getDescriptor())) continue;
if (!visitor.visitElementContent(MappedElementKind.METHOD)) continue;

if (!mth.getJavadoc().isEmpty()) {
visitor.visitComment(MappedElementKind.FIELD, joinComment(mth.getJavadoc()));
}

for (MappingDataContainer.ParameterData arg : mth.getParameters()) {
if (!visitor.visitMethodArg(-1, arg.getIndex(), arg.getName())) continue;
if (!visitor.visitElementContent(MappedElementKind.METHOD_ARG)) continue;

if (arg.getJavadoc() != null) {
visitor.visitComment(MappedElementKind.METHOD_ARG, arg.getJavadoc());
}
}
}
}
}
}

private static String joinComment(List<String> commentLines) {
return String.join("\n", commentLines);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.parchmentmc.feather.io.mappingio;

import net.fabricmc.mappingio.FlatMappingVisitor;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.adapter.FlatAsRegularMappingVisitor;
import net.fabricmc.mappingio.tree.MappingTreeView;
import org.jetbrains.annotations.Nullable;
import org.parchmentmc.feather.mapping.MappingDataBuilder;
import org.parchmentmc.feather.mapping.MappingDataContainer;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.Path;
import java.util.List;

public class MioToMdc {
public static MappingDataContainer fromPath(Path path) throws IOException {
ConvertingVisitor converter = new ConvertingVisitor();
MappingReader.read(path, new FlatAsRegularMappingVisitor(converter));
return converter.getResult();
}

public static MappingDataContainer fromReader(Reader reader) throws IOException {
ConvertingVisitor converter = new ConvertingVisitor();
MappingReader.read(reader, new FlatAsRegularMappingVisitor(converter));
return converter.getResult();
}

public static MappingDataContainer fromTree(MappingTreeView tree) throws IOException {
ConvertingVisitor converter = new ConvertingVisitor();
tree.accept(new FlatAsRegularMappingVisitor(converter));
return converter.getResult();
}

private static class ConvertingVisitor implements FlatMappingVisitor {
MappingDataBuilder mdcBuilder = new MappingDataBuilder();
MappingDataBuilder.MutableClassData cls;
MappingDataBuilder.MutableFieldData fld;
MappingDataBuilder.MutableMethodData mth;
MappingDataBuilder.MutableParameterData arg;

@Override
public void visitNamespaces(String srcNamespace, List<String> dstNamespaces) throws IOException {
}

@Override
public boolean visitClass(String srcName, String[] dstNames) throws IOException {
cls = mdcBuilder.getOrCreateClass(srcName);

return true;
}

@Override
public void visitClassComment(String srcName, String[] dstNames, String comment) throws IOException {
cls.addJavadoc(splitComment(comment));
}

@Override
public boolean visitField(String srcClsName, String srcName, @Nullable String srcDesc, @Nullable String[] dstClsNames, @Nullable String[] dstNames, @Nullable String[] dstDescs) throws IOException {
fld = cls.getOrCreateField(srcName, srcDesc);

return true;
}

@Override
public void visitFieldComment(String srcClsName, String srcName, @Nullable String srcDesc, @Nullable String[] dstClsNames, @Nullable String[] dstNames, @Nullable String[] dstDescs, String comment) throws IOException {
fld.addJavadoc(splitComment(comment));
}

@Override
public boolean visitMethod(String srcClsName, String srcName, @Nullable String srcDesc, @Nullable String[] dstClsNames, @Nullable String[] dstNames, @Nullable String[] dstDescs) throws IOException {
mth = cls.getOrCreateMethod(srcName, srcDesc);

return true;
}

@Override
public void visitMethodComment(String srcClsName, String srcName, @Nullable String srcDesc, @Nullable String[] dstClsNames, @Nullable String[] dstNames, @Nullable String[] dstDescs, String comment) throws IOException {
mth.addJavadoc(splitComment(comment));
}

@Override
public boolean visitMethodArg(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc, int argPosition, int lvIndex, @Nullable String srcName, @Nullable String[] dstClsNames, @Nullable String[] dstMethodNames, @Nullable String[] dstMethodDescs, String[] dstNames) throws IOException {
if (lvIndex > Byte.MAX_VALUE) throw new IOException("Feather doesn't support lvIndices larger than " + Byte.MAX_VALUE);
arg = mth.getOrCreateParameter((byte) lvIndex);

return true;
}

@Override
public void visitMethodArgComment(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc, int argPosition, int lvIndex, @Nullable String srcName, @Nullable String[] dstClsNames, @Nullable String[] dstMethodNames, @Nullable String[] dstMethodDescs, @Nullable String[] dstNames, String comment) throws IOException {
arg.addJavadoc(splitComment(comment));
}

@Override
public boolean visitMethodVar(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc, int lvtRowIndex, int lvIndex, int startOpIdx, int endOpIdx, @Nullable String srcName, @Nullable String[] dstClsNames, @Nullable String[] dstMethodNames, @Nullable String[] dstMethodDescs, String[] dstNames) throws IOException {
return false;
}

@Override
public void visitMethodVarComment(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc, int lvtRowIndex, int lvIndex, int startOpIdx, int endOpIdx, @Nullable String srcName, @Nullable String[] dstClsNames, @Nullable String[] dstMethodNames, @Nullable String[] dstMethodDescs, @Nullable String[] dstNames, String comment) throws IOException {
}

MappingDataContainer getResult() {
return mdcBuilder;
}
}

private static String[] splitComment(String comment) {
return comment.split("\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.parchmentmc.feather.io.mappingio;

import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingUtil;
import net.fabricmc.mappingio.adapter.FlatAsRegularMappingVisitor;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.mappingio.tree.VisitableMappingTree;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class MappingIOTest {
@Test
public void testMio() throws IOException {
VisitableMappingTree tree = new MemoryMappingTree();
String srcNs = MappingUtil.NS_SOURCE_FALLBACK;

tree.visitNamespaces(srcNs, Collections.emptyList());

tree.visitClass("net/minecraft/class_1");
tree.visitComment(MappedElementKind.CLASS, "class_1 comment");

tree.visitField("field_1", "I");
tree.visitComment(MappedElementKind.FIELD, "field_1 comment");

tree.visitMethod("method_1", "()V");
tree.visitComment(MappedElementKind.METHOD, "method_1 comment");

tree.visitMethodArg(-1, 0, "arg_1");
tree.visitComment(MappedElementKind.METHOD_ARG, "arg_1 comment");

tree.visitEnd();

MappingTree convertedTree = MdcToMio.toTree(MioToMdc.fromTree(tree), srcNs);

convertedTree.accept(new FlatAsRegularMappingVisitor(new SubsetAssertingVisitor(tree, null, null)));
tree.accept(new FlatAsRegularMappingVisitor(new SubsetAssertingVisitor(convertedTree, null, null)));
}
}
Loading