Skip to content

Commit 5986d0f

Browse files
Support writing MCP mappings
Rename ContentList to InputCollection Refactor MappingGenerator and introduce OutputCollection
1 parent 399831e commit 5986d0f

31 files changed

Lines changed: 420 additions & 251 deletions

extensions/parchment/src/main/java/cn/maxpixel/mcdecompiler/mapping/parchment/ParchmentMappingGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import cn.maxpixel.mcdecompiler.mapping.component.LocalVariableTable;
2727
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormat;
2828
import cn.maxpixel.mcdecompiler.mapping.generator.MappingGenerator;
29-
import cn.maxpixel.mcdecompiler.mapping.remapper.ClassifiedMappingRemapper;
29+
import cn.maxpixel.mcdecompiler.mapping.remapper.MappingRemapper;
3030
import cn.maxpixel.mcdecompiler.utils.Utils;
3131
import com.google.gson.stream.JsonWriter;
3232
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@@ -43,7 +43,7 @@ public enum ParchmentMappingGenerator implements MappingGenerator.Classified<Pai
4343
INSTANCE;
4444

4545
@Override
46-
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, @Nullable ClassifiedMappingRemapper remapper) {
46+
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, @Nullable MappingRemapper remapper) {
4747
StringListWriter slw = new StringListWriter();// No specialized version of returning String because it's not worth it
4848
try (JsonWriter writer = new JsonWriter(slw)) {
4949
writer.setIndent(" ");// 2 spaces

extensions/parchment/src/main/java/cn/maxpixel/mcdecompiler/mapping/parchment/ParchmentMappingProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import cn.maxpixel.mcdecompiler.mapping.component.LocalVariableTable;
2727
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormat;
2828
import cn.maxpixel.mcdecompiler.mapping.processor.MappingProcessor;
29-
import cn.maxpixel.mcdecompiler.mapping.util.ContentList;
29+
import cn.maxpixel.mcdecompiler.mapping.util.InputCollection;
3030
import cn.maxpixel.mcdecompiler.utils.Utils;
3131
import com.google.gson.stream.JsonReader;
3232
import com.google.gson.stream.JsonToken;
@@ -45,7 +45,7 @@ public MappingFormat<PairedMapping, ClassifiedMapping<PairedMapping>> getFormat(
4545
}
4646

4747
@Override
48-
public ClassifiedMapping<PairedMapping> process(ContentList contents) {
48+
public ClassifiedMapping<PairedMapping> process(InputCollection contents) {
4949
ClassifiedMapping<PairedMapping> mappings = new ClassifiedMapping<>();
5050
for (var content : contents) {
5151
try (JsonReader reader = new JsonReader(content.reader())) {

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/component/Documented.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* so {@code \n} and {@code \r} are not allowed. An empty line must be represented by {@code ""}.
3333
* {@code null} elements are prohibited
3434
*/
35-
public class Documented implements Component {
35+
public class Documented implements Component {// TODO: Make doc more standardized
3636
/**
3737
* The contents
3838
*/

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/format/MappingFormat.java

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@
2424
import cn.maxpixel.mcdecompiler.mapping.collection.UniqueMapping;
2525
import cn.maxpixel.mcdecompiler.mapping.generator.MappingGenerator;
2626
import cn.maxpixel.mcdecompiler.mapping.processor.MappingProcessor;
27-
import cn.maxpixel.mcdecompiler.mapping.util.ContentList;
27+
import cn.maxpixel.mcdecompiler.mapping.util.InputCollection;
28+
import cn.maxpixel.mcdecompiler.mapping.util.OutputCollection;
2829
import cn.maxpixel.mcdecompiler.utils.Utils;
29-
import it.unimi.dsi.fastutil.objects.ObjectList;
3030
import org.jetbrains.annotations.NotNull;
3131

3232
import java.io.*;
33-
import java.nio.ByteBuffer;
34-
import java.nio.channels.WritableByteChannel;
3533
import java.nio.charset.StandardCharsets;
3634

3735
/**
@@ -65,7 +63,7 @@ public interface MappingFormat<M extends Mapping, C extends MappingCollection<M>
6563

6664
default @NotNull C read(@NotNull Reader reader) {
6765
try {
68-
return getProcessor().process(new ContentList.ContentStream(reader));
66+
return getProcessor().process(new InputCollection.Entry(reader));
6967
} catch (IOException e) {
7068
throw Utils.wrapInRuntime(e);
7169
}
@@ -75,7 +73,7 @@ public interface MappingFormat<M extends Mapping, C extends MappingCollection<M>
7573
return read(new InputStreamReader(is, StandardCharsets.UTF_8));
7674
}
7775

78-
default @NotNull C read(@NotNull ContentList contents) {
76+
default @NotNull C read(@NotNull InputCollection contents) {
7977
try {
8078
return getProcessor().process(contents);
8179
} catch (IOException e) {
@@ -84,45 +82,15 @@ public interface MappingFormat<M extends Mapping, C extends MappingCollection<M>
8482
}
8583

8684
default void write(@NotNull C collection, @NotNull OutputStream os) throws IOException {
87-
if (getGenerator().requireMultiFiles()) throw new UnsupportedOperationException("Use write(OutputStream...) instead.");
88-
os.write(String.join("\n", getGenerator().generate(collection)).getBytes(StandardCharsets.UTF_8));
85+
write(collection, new OutputStreamWriter(os, StandardCharsets.UTF_8));
8986
}
9087

9188
default void write(@NotNull C collection, @NotNull Writer writer) throws IOException {
92-
if (getGenerator().requireMultiFiles()) throw new UnsupportedOperationException("Use write(Writer...) instead.");
93-
writer.write(String.join("\n", getGenerator().generate(collection)));
89+
getGenerator().generateAndWrite(collection, OutputCollection.ofUnnamed(writer), null);
9490
}
9591

96-
default void write(@NotNull C collection, @NotNull WritableByteChannel ch) throws IOException {
97-
if (getGenerator().requireMultiFiles()) throw new UnsupportedOperationException("Use write(WritableByteChannel...) instead.");
98-
ch.write(ByteBuffer.wrap(String.join("\n", getGenerator().generate(collection)).getBytes(StandardCharsets.UTF_8)));
99-
}
100-
101-
default void write(@NotNull C collection, @NotNull OutputStream... os) throws IOException {
102-
if (!getGenerator().requireMultiFiles()) throw new UnsupportedOperationException("Use write(OutputStream) instead.");
103-
ObjectList<String>[] output = getGenerator().generateMulti(collection);
104-
if (output.length != os.length) throw new UnsupportedOperationException();
105-
for (int i = 0; i < output.length; i++) {
106-
os[i].write(String.join("\n", output[i]).getBytes(StandardCharsets.UTF_8));
107-
}
108-
}
109-
110-
default void write(@NotNull C collection, @NotNull Writer... writer) throws IOException {
111-
if (!getGenerator().requireMultiFiles()) throw new UnsupportedOperationException("Use write(Writer) instead.");
112-
ObjectList<String>[] output = getGenerator().generateMulti(collection);
113-
if (output.length != writer.length) throw new UnsupportedOperationException();
114-
for (int i = 0; i < output.length; i++) {
115-
writer[i].write(String.join("\n", output[i]));
116-
}
117-
}
118-
119-
default void write(@NotNull C collection, @NotNull WritableByteChannel... ch) throws IOException {
120-
if (!getGenerator().requireMultiFiles()) throw new UnsupportedOperationException("Use write(WritableByteChannel) instead.");
121-
ObjectList<String>[] output = getGenerator().generateMulti(collection);
122-
if (output.length != ch.length) throw new UnsupportedOperationException();
123-
for (int i = 0; i < output.length; i++) {
124-
ch[i].write(ByteBuffer.wrap(String.join("\n", output[i]).getBytes(StandardCharsets.UTF_8)));
125-
}
92+
default void write(@NotNull C collection, @NotNull OutputCollection out) throws IOException {
93+
getGenerator().generateAndWrite(collection, out, null);
12694
}
12795

12896
interface Classified<M extends Mapping> extends MappingFormat<M, ClassifiedMapping<M>> {

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/CsrgMappingGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import cn.maxpixel.mcdecompiler.mapping.collection.ClassifiedMapping;
2323
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormat;
2424
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormats;
25-
import cn.maxpixel.mcdecompiler.mapping.remapper.ClassifiedMappingRemapper;
25+
import cn.maxpixel.mcdecompiler.mapping.remapper.MappingRemapper;
2626
import cn.maxpixel.mcdecompiler.mapping.util.MappingUtils;
2727
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
2828
import it.unimi.dsi.fastutil.objects.ObjectList;
@@ -36,7 +36,7 @@ public MappingFormat<PairedMapping, ClassifiedMapping<PairedMapping>> getFormat(
3636
}
3737

3838
@Override
39-
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, ClassifiedMappingRemapper remapper) {
39+
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, MappingRemapper remapper) {
4040
ObjectArrayList<String> lines = new ObjectArrayList<>();
4141
if (mappings.classes.isEmpty() && mappings.packages.isEmpty()) return lines;
4242
mappings.classes.parallelStream().forEach(cls -> {

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/MCPMappingGenerator.java

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,73 @@
2020

2121
import cn.maxpixel.mcdecompiler.mapping.PairedMapping;
2222
import cn.maxpixel.mcdecompiler.mapping.collection.UniqueMapping;
23+
import cn.maxpixel.mcdecompiler.mapping.component.Documented;
24+
import cn.maxpixel.mcdecompiler.mapping.component.SideSpecific;
2325
import cn.maxpixel.mcdecompiler.mapping.format.MCPMappingFormat;
2426
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormat;
27+
import cn.maxpixel.mcdecompiler.mapping.remapper.MappingRemapper;
28+
import cn.maxpixel.mcdecompiler.mapping.util.OutputCollection;
29+
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
2530
import it.unimi.dsi.fastutil.objects.ObjectList;
31+
import org.jetbrains.annotations.Nullable;
32+
33+
import java.io.IOException;
34+
import java.io.Writer;
2635

2736
public enum MCPMappingGenerator implements MappingGenerator.Unique<PairedMapping> {
2837
INSTANCE;
38+
private static final String MEMBER_HEADER = "searge,name,side,desc\n";
39+
private static final String PARAM_HEADER = "param,name,side\n";
2940

3041
@Override
3142
public MappingFormat<PairedMapping, UniqueMapping<PairedMapping>> getFormat() {
3243
return MCPMappingFormat.INSTANCE;
3344
}
3445

3546
@Override
36-
public ObjectList<String> generate(UniqueMapping<PairedMapping> mappings) {
37-
return null;
47+
public ObjectList<String> generate(UniqueMapping<PairedMapping> mappings, @Nullable MappingRemapper remapper) {
48+
throw new IllegalStateException("Call generateAndWrite");
3849
}
3950

4051
@Override
41-
public boolean requireMultiFiles() {
42-
return true;
52+
public void generateAndWrite(UniqueMapping<PairedMapping> mappings, OutputCollection out, @Nullable MappingRemapper remapper) throws IOException {
53+
try (var mw = out.getOutput(MCPMappingFormat.METHODS_CSV)) {
54+
if (mw != null) {
55+
mw.write(MEMBER_HEADER);
56+
writeMember(mw, mappings.methods);
57+
}
58+
}
59+
try (var fw = out.getOutput(MCPMappingFormat.FIELDS_CSV)) {
60+
if (fw != null) {
61+
fw.write(MEMBER_HEADER);
62+
writeMember(fw, mappings.fields);
63+
}
64+
}
65+
try (var pw = out.getOutput(MCPMappingFormat.PARAMS_CSV)) {
66+
if (pw != null) {
67+
pw.write(PARAM_HEADER);
68+
for (PairedMapping member : mappings.params) {
69+
pw.append(member.unmappedName).append(',')
70+
.append(member.mappedName).append(',')
71+
.append(String.valueOf(member.getComponentOptional(SideSpecific.class)
72+
.orElse(SideSpecific.BOTH).ordinal())).append('\n');
73+
}
74+
}
75+
}
4376
}
4477

45-
@Override
46-
public ObjectList<String>[] generateMulti(UniqueMapping<PairedMapping> mappings) {
47-
return Unique.super.generateMulti(mappings);
78+
private void writeMember(Writer w, ObjectArrayList<PairedMapping> list) throws IOException {
79+
for (PairedMapping member : list) {
80+
String comment = member.getComponentOptional(Documented.class).map(d ->
81+
d.contents.isEmpty() ? null : d.contents.get(0)).orElse("");
82+
if (comment.contains(",") || comment.contains("\"")) {
83+
comment = '"' + comment.replace("\"", "\"\"") + '"';
84+
}
85+
w.append(member.unmappedName).append(',')
86+
.append(member.mappedName).append(',')
87+
.append(String.valueOf(member.getComponentOptional(SideSpecific.class)
88+
.orElse(SideSpecific.BOTH).ordinal())).append(',')
89+
.append(comment).append('\n');
90+
}
4891
}
4992
}

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/MappingGenerator.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
import cn.maxpixel.mcdecompiler.mapping.collection.MappingCollection;
2424
import cn.maxpixel.mcdecompiler.mapping.collection.UniqueMapping;
2525
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormat;
26-
import cn.maxpixel.mcdecompiler.mapping.remapper.ClassifiedMappingRemapper;
26+
import cn.maxpixel.mcdecompiler.mapping.remapper.MappingRemapper;
27+
import cn.maxpixel.mcdecompiler.mapping.util.OutputCollection;
2728
import it.unimi.dsi.fastutil.objects.ObjectList;
29+
import org.jetbrains.annotations.ApiStatus;
2830
import org.jetbrains.annotations.Nullable;
2931

32+
import java.io.IOException;
33+
3034
/**
3135
* A generator which generates mappings to strings.<br>
3236
*
@@ -37,36 +41,22 @@
3741
public interface MappingGenerator<T extends Mapping, C extends MappingCollection<T>> {
3842
MappingFormat<T, C> getFormat();
3943

40-
ObjectList<String> generate(C mappings);
41-
42-
// Just in case that maybe some types of mapping need to generate multiple files
43-
44-
default boolean requireMultiFiles() {
45-
return false;
44+
default ObjectList<String> generate(C mappings) {
45+
return generate(mappings, null);
4646
}
4747

48-
default ObjectList<String>[] generateMulti(C mappings) {
49-
throw new UnsupportedOperationException();
48+
@ApiStatus.OverrideOnly
49+
ObjectList<String> generate(C mappings, @Nullable MappingRemapper remapper);
50+
51+
default void generateAndWrite(C mappings, OutputCollection out, @Nullable MappingRemapper remapper) throws IOException {
52+
try (var unnamed = out.getUnnamedOutput()) {
53+
unnamed.write(String.join("\n", generate(mappings, remapper)));
54+
}
5055
}
5156

5257
interface Unique<T extends Mapping> extends MappingGenerator<T, UniqueMapping<T>> {
5358
}
5459

5560
interface Classified<T extends Mapping> extends MappingGenerator<T, ClassifiedMapping<T>> {
56-
@Override
57-
default ObjectList<String> generate(ClassifiedMapping<T> mappings) {
58-
return generate(mappings, null);
59-
}
60-
61-
ObjectList<String> generate(ClassifiedMapping<T> mappings, @Nullable ClassifiedMappingRemapper remapper);
62-
63-
@Override
64-
default ObjectList<String>[] generateMulti(ClassifiedMapping<T> mappings) {
65-
return generateMulti(mappings, null);
66-
}
67-
68-
default ObjectList<String>[] generateMulti(ClassifiedMapping<T> mappings, @Nullable ClassifiedMappingRemapper remapper) {
69-
throw new UnsupportedOperationException();
70-
}
7161
}
7262
}

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/PdmeMappingGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import cn.maxpixel.mcdecompiler.mapping.component.LocalVariableTable;
2626
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormat;
2727
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormats;
28-
import cn.maxpixel.mcdecompiler.mapping.remapper.ClassifiedMappingRemapper;
28+
import cn.maxpixel.mcdecompiler.mapping.remapper.MappingRemapper;
2929
import cn.maxpixel.mcdecompiler.mapping.trait.AccessTransformationTrait;
3030
import cn.maxpixel.mcdecompiler.mapping.trait.InheritanceTrait;
3131
import cn.maxpixel.mcdecompiler.mapping.util.MappingUtils;
@@ -49,7 +49,7 @@ public MappingFormat<PairedMapping, ClassifiedMapping<PairedMapping>> getFormat(
4949
}
5050

5151
@Override
52-
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, ClassifiedMappingRemapper remapper) {
52+
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, MappingRemapper remapper) {
5353
ObjectArrayList<String> lines = new ObjectArrayList<>();
5454
lines.add("tipo¶original¶nuevo¶def¶pos¶desc");
5555
if (mappings.classes.isEmpty()) return lines;

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/ProguardMappingGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import cn.maxpixel.mcdecompiler.mapping.component.LineNumber;
2626
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormat;
2727
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormats;
28-
import cn.maxpixel.mcdecompiler.mapping.remapper.ClassifiedMappingRemapper;
28+
import cn.maxpixel.mcdecompiler.mapping.remapper.MappingRemapper;
2929
import cn.maxpixel.mcdecompiler.mapping.util.MappingUtils;
3030
import cn.maxpixel.mcdecompiler.mapping.util.NamingUtil;
3131
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@@ -40,7 +40,7 @@ public MappingFormat<PairedMapping, ClassifiedMapping<PairedMapping>> getFormat(
4040
}
4141

4242
@Override
43-
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, ClassifiedMappingRemapper remapper) {
43+
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, MappingRemapper remapper) {
4444
ObjectArrayList<String> lines = new ObjectArrayList<>();
4545
if (mappings.classes.isEmpty()) return lines;
4646
for (ClassMapping<PairedMapping> cls : mappings.classes) {

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/SrgMappingGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import cn.maxpixel.mcdecompiler.mapping.component.Descriptor;
2424
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormat;
2525
import cn.maxpixel.mcdecompiler.mapping.format.MappingFormats;
26-
import cn.maxpixel.mcdecompiler.mapping.remapper.ClassifiedMappingRemapper;
26+
import cn.maxpixel.mcdecompiler.mapping.remapper.MappingRemapper;
2727
import cn.maxpixel.mcdecompiler.mapping.util.MappingUtils;
2828
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
2929
import it.unimi.dsi.fastutil.objects.ObjectList;
@@ -37,7 +37,7 @@ public MappingFormat<PairedMapping, ClassifiedMapping<PairedMapping>> getFormat(
3737
}
3838

3939
@Override
40-
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, ClassifiedMappingRemapper remapper) {
40+
public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, MappingRemapper remapper) {
4141
ObjectArrayList<String> lines = new ObjectArrayList<>();
4242
if (mappings.classes.isEmpty() && mappings.packages.isEmpty()) return lines;
4343
mappings.classes.parallelStream().forEach(cls -> {

0 commit comments

Comments
 (0)