diff --git a/src/main/java/net/fabricmc/mappingio/MappingWriter.java b/src/main/java/net/fabricmc/mappingio/MappingWriter.java index 736f1a2f..923e1d7e 100644 --- a/src/main/java/net/fabricmc/mappingio/MappingWriter.java +++ b/src/main/java/net/fabricmc/mappingio/MappingWriter.java @@ -26,6 +26,7 @@ import net.fabricmc.mappingio.format.enigma.EnigmaDirWriter; import net.fabricmc.mappingio.format.enigma.EnigmaFileWriter; import net.fabricmc.mappingio.format.proguard.ProGuardFileWriter; +import net.fabricmc.mappingio.format.simple.RecafSimpleFileWriter; import net.fabricmc.mappingio.format.tiny.Tiny1FileWriter; import net.fabricmc.mappingio.format.tiny.Tiny2FileWriter; @@ -49,6 +50,7 @@ static MappingWriter create(Writer writer, MappingFormat format) throws IOExcept case TINY_2_FILE: return new Tiny2FileWriter(writer, false); case ENIGMA_FILE: return new EnigmaFileWriter(writer); case PROGUARD_FILE: return new ProGuardFileWriter(writer); + case RECAF_SIMPLE_FILE: return new RecafSimpleFileWriter(writer); default: throw new UnsupportedOperationException("format "+format+" is not implemented"); } } diff --git a/src/main/java/net/fabricmc/mappingio/format/simple/RecafSimpleFileWriter.java b/src/main/java/net/fabricmc/mappingio/format/simple/RecafSimpleFileWriter.java new file mode 100644 index 00000000..8fc83ad2 --- /dev/null +++ b/src/main/java/net/fabricmc/mappingio/format/simple/RecafSimpleFileWriter.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2023 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.mappingio.format.simple; + +import java.io.IOException; +import java.io.Writer; +import java.util.EnumSet; +import java.util.List; +import java.util.Set; + +import net.fabricmc.mappingio.MappedElementKind; +import net.fabricmc.mappingio.MappingFlag; +import net.fabricmc.mappingio.MappingWriter; + +public final class RecafSimpleFileWriter implements MappingWriter { + public RecafSimpleFileWriter(Writer writer) { + this.writer = writer; + } + + @Override + public void close() throws IOException { + writer.close(); + } + + @Override + public Set getFlags() { + return flags; + } + + @Override + public void visitNamespaces(String srcNamespace, List dstNamespaces) throws IOException { + } + + @Override + public boolean visitClass(String srcName) throws IOException { + classSrcName = srcName; + + return true; + } + + @Override + public boolean visitField(String srcName, String srcDesc) throws IOException { + memberSrcName = srcName; + memberSrcDesc = srcDesc; + + return true; + } + + @Override + public boolean visitMethod(String srcName, String srcDesc) throws IOException { + memberSrcName = srcName; + memberSrcDesc = srcDesc; + + return true; + } + + @Override + public boolean visitMethodArg(int argPosition, int lvIndex, String srcName) throws IOException { + return false; // not supported, skip + } + + @Override + public boolean visitMethodVar(int lvtRowIndex, int lvIndex, int startOpIdx, int endOpIdx, String srcName) throws IOException { + return false; // not supported, skip + } + + @Override + public void visitDstName(MappedElementKind targetKind, int namespace, String name) { + if (namespace != 0) return; + dstName = name; + } + + @Override + public boolean visitElementContent(MappedElementKind targetKind) throws IOException { + if (dstName == null) return true; + write(classSrcName); + + if (targetKind != MappedElementKind.CLASS) { + if (memberSrcName == null) throw new IllegalArgumentException("member source name cannot be null!"); + writer.write('.'); + write(memberSrcName); + + if (memberSrcDesc != null) { + if (targetKind == MappedElementKind.FIELD) writeSpace(); + write(memberSrcDesc); + } + } + + writeSpace(); + write(dstName); + writeLn(); + dstName = null; + + return targetKind == MappedElementKind.CLASS; // only members are supported, skip anything but class contents + } + + @Override + public void visitComment(MappedElementKind targetKind, String comment) throws IOException { + // not supported, skip + } + + private void write(String str) throws IOException { + writer.write(str); + } + + private void writeLn() throws IOException { + writer.write('\n'); + } + + private void writeSpace() throws IOException { + writer.write(' '); + } + + private static final Set flags = EnumSet.of(MappingFlag.NEEDS_SRC_FIELD_DESC, MappingFlag.NEEDS_SRC_METHOD_DESC); + + private final Writer writer; + private String classSrcName; + private String memberSrcName; + private String memberSrcDesc; + private String dstName; +} diff --git a/src/test/java/net/fabricmc/mappingio/write/WriteTest.java b/src/test/java/net/fabricmc/mappingio/write/WriteTest.java index 2d015650..9e8287de 100644 --- a/src/test/java/net/fabricmc/mappingio/write/WriteTest.java +++ b/src/test/java/net/fabricmc/mappingio/write/WriteTest.java @@ -58,6 +58,11 @@ public void tinyV2File() throws Exception { write(MappingFormat.TINY_2_FILE); } + @Test + public void recafSimpleFile() throws Exception { + write(MappingFormat.RECAF_SIMPLE_FILE); + } + private void write(MappingFormat format) throws Exception { TestHelper.writeToDir(tree, format, dir); TestHelper.writeToDir(treeWithHoles, format, dir);