Skip to content

Commit 41e3334

Browse files
committed
Add new Enigma IO which only sorts inner classes lexicographically
Enigma 2.4.0 changed its writing system to use the mapping-io library. The library differs in how it writes inner classes. Previously, inner class sorting worked by first sorting based on enclosing classes first, then by length and then lexicographically. Now, inner class sorting is solely lexicographically, without regard to length. To allow for compatibility with Enigma versions that do not yet use mapping-io, the existing Enigma IO retains its behavior, and new Enigma IO instances, `ENIGMA_EXPLODED_LENGTH_SORT` and `ENIGMA_EXPLODED_LEXICOGRAPHIC_SORT`, are introduced which express the original behavior and the new behavior respectively. `ENIGMA_EXPLODED_LEXICOGRAPHIC_SORT` can be specified for the data directories using the `compass` extension as usual.
1 parent 8cfdf6f commit 41e3334

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/main/java/org/parchmentmc/compass/storage/io/MappingIOFormat.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
public enum MappingIOFormat implements MappingDataIO {
1010
MDC_SINGLE(true, SingleFileDataIO.INSTANCE),
1111
MDC_EXPLODED(false, ExplodedDataIO.INSTANCE),
12-
ENIGMA_EXPLODED(false, EnigmaFormattedExplodedIO.INSTANCE);
12+
ENIGMA_EXPLODED(false, EnigmaFormattedExplodedIO.LENGTH_SORT_INSTANCE),
13+
ENIGMA_EXPLODED_LENGTH_SORT(false, EnigmaFormattedExplodedIO.LENGTH_SORT_INSTANCE),
14+
ENIGMA_EXPLODED_LEXICOGRAPHIC_SORT(false, EnigmaFormattedExplodedIO.LEXICOGRAPHIC_SORT_INSTANCE);
1315

1416
private final boolean fileBased;
1517
private final MappingDataIO dataIO;

src/main/java/org/parchmentmc/compass/storage/io/enigma/EnigmaFormattedExplodedIO.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.attribute.BasicFileAttributes;
2323
import java.util.*;
2424
import java.util.function.Function;
25+
import java.util.function.Supplier;
2526
import java.util.stream.Collectors;
2627
import java.util.stream.Stream;
2728

@@ -30,8 +31,10 @@
3031
import static org.parchmentmc.feather.mapping.MappingDataContainer.ClassData;
3132

3233
public class EnigmaFormattedExplodedIO implements MappingDataIO {
33-
public static final EnigmaFormattedExplodedIO INSTANCE = new EnigmaFormattedExplodedIO(JSONUtil.MOSHI,
34-
" ", "mapping");
34+
public static final EnigmaFormattedExplodedIO LENGTH_SORT_INSTANCE = new EnigmaFormattedExplodedIO(JSONUtil.MOSHI,
35+
" ", "mapping", true);
36+
public static final EnigmaFormattedExplodedIO LEXICOGRAPHIC_SORT_INSTANCE = new EnigmaFormattedExplodedIO(JSONUtil.MOSHI,
37+
" ", "mapping", false);
3538

3639
static final CharMatcher DOLLAR_SIGN = CharMatcher.is('$');
3740
static final String DOLLAR_SIGN_REGEX = "\\$";
@@ -54,11 +57,15 @@ public class EnigmaFormattedExplodedIO implements MappingDataIO {
5457
private final Moshi moshi;
5558
private final String jsonIndent;
5659
private final String extension;
60+
// Whether to manually sort inner classes by inner class name length then lexicographically
61+
// If false, then sorting is lexicographically by the entire inner class FQN
62+
private final boolean lengthSort;
5763

58-
public EnigmaFormattedExplodedIO(Moshi moshi, String jsonIndent, String extension) {
64+
public EnigmaFormattedExplodedIO(Moshi moshi, String jsonIndent, String extension, boolean lengthSort) {
5965
this.moshi = moshi;
6066
this.jsonIndent = jsonIndent;
6167
this.extension = extension;
68+
this.lengthSort = lengthSort;
6269
}
6370

6471
@Override
@@ -79,11 +86,14 @@ public void write(VersionedMappingDataContainer data, Path base) throws IOExcept
7986
}
8087

8188
// Group classes by their outermost classes (via `$` matching)
89+
final Supplier<Set<String>> setCreator = lengthSort
90+
? () -> new TreeSet<>(EnigmaFormattedExplodedIO::compareClassNames)
91+
: TreeSet::new;
8292
final Map<String, Set<String>> outerClassesToClasses = data.getClasses().stream()
8393
.map(ClassData::getName)
8494
.sorted()
8595
.collect(Collectors.groupingBy(EnigmaWriter::stripToOuter,
86-
Collectors.toCollection(() -> new TreeSet<>(EnigmaFormattedExplodedIO::compareClassNames))));
96+
Collectors.toCollection(setCreator)));
8797

8898
Set<String> visited = new HashSet<>();
8999

0 commit comments

Comments
 (0)