Skip to content

Commit

Permalink
Provide MappingTreeView -> IMappingProvider converter (#128)
Browse files Browse the repository at this point in the history
* Provide `MappingTreeView` -> `IMappingProvider` converter

* Fix code style
  • Loading branch information
NebelNidas authored Mar 25, 2024
1 parent 1c669ac commit fabc0e5
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/main/java/net/fabricmc/tinyremapper/TinyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@

import net.fabricmc.mappingio.FlatMappingVisitor;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.MappingVisitor;
import net.fabricmc.mappingio.adapter.FlatAsRegularMappingVisitor;
import net.fabricmc.mappingio.adapter.MappingDstNsReorder;
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch;
import net.fabricmc.mappingio.tree.MappingTreeView;
import net.fabricmc.tinyremapper.IMappingProvider.MappingAcceptor;
import net.fabricmc.tinyremapper.IMappingProvider.Member;

Expand Down Expand Up @@ -74,15 +76,28 @@ public static IMappingProvider createTinyMappingProvider(final BufferedReader re
};
}

public static IMappingProvider createMappingProvider(MappingTreeView tree, String fromM, String toM) {
return out -> {
try {
tree.accept(createAdapter(fromM, toM, out));
} catch (IOException e) {
throw new RuntimeException(e);
}
};
}

private static void read(BufferedReader reader, String fromNs, String toNs, MappingAcceptor out) throws IOException {
MappingReader.read(reader,
// Ensure fromNs is on source and toNs is on destination side
new MappingSourceNsSwitch(
// Remove all dst namespaces we're not interested in
new MappingDstNsReorder(
new FlatAsRegularMappingVisitor(new MappingAdapter(out)),
toNs),
fromNs));
MappingReader.read(reader, createAdapter(fromNs, toNs, out));
}

private static MappingVisitor createAdapter(String fromNs, String toNs, MappingAcceptor out) throws IOException {
// Ensure fromNs is on source and toNs is on destination side
return new MappingSourceNsSwitch(
// Remove all dst namespaces we're not interested in
new MappingDstNsReorder(
new FlatAsRegularMappingVisitor(new MappingAdapter(out)),
toNs),
fromNs);
}

private static final class MappingAdapter implements FlatMappingVisitor {
Expand Down

0 comments on commit fabc0e5

Please sign in to comment.