Skip to content

Commit

Permalink
Make Enigma writer always output destination names if visited explici…
Browse files Browse the repository at this point in the history
…tly (#125)
  • Loading branch information
NebelNidas authored Jan 16, 2025
1 parent 9913123 commit e8acce3
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 79 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
- Made `OuterClassNamePropagator` configurable
- Made Enigma writer always output destination names if visited explicitly, establishing consistency across all writers
- Added a simplified `MappingNsCompleter` constructor for completing all destination names with the source names

## [0.7.1] - 2025-01-07
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.ArrayList;
import java.util.List;

import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.format.MappingFormat;

/**
Expand Down Expand Up @@ -80,75 +79,65 @@ public void close() throws IOException {
}

@Override
public boolean visitElementContent(MappedElementKind targetKind) throws IOException {
if (targetKind == MappedElementKind.CLASS) {
String name = dstName != null ? dstName : srcClassName;

if (currentClass == null
|| !name.startsWith(currentClass)
|| name.length() > currentClass.length() && name.charAt(currentClass.length()) != '$') {
int pos = getNextOuterEnd(name, 0);
if (pos >= 0) name = name.substring(0, pos);

// currentClass is not an outer class of srcName (or the same)
Path file = dir.resolve(name + "." + MappingFormat.ENIGMA_FILE.fileExt).normalize();
if (!file.startsWith(dir)) throw new RuntimeException("invalid name: " + name);

if (writer != null) {
writer.close();
}
void visitClassContent() throws IOException {
String name = dstName != null ? dstName : srcClassName;

if (currentClass == null
|| !name.startsWith(currentClass)
|| name.length() > currentClass.length() && name.charAt(currentClass.length()) != '$') {
int pos = getNextOuterEnd(name, 0);
if (pos >= 0) name = name.substring(0, pos);

currentClass = name;
// currentClass is not an outer class of srcName (or the same)
Path file = dir.resolve(name + "." + MappingFormat.ENIGMA_FILE.fileExt).normalize();
if (!file.startsWith(dir)) throw new RuntimeException("invalid name: " + name);

if (Files.exists(file)) {
// initialize writtenClass with last CLASS entry
if (writer != null) {
writer.close();
}

List<String> writtenClassParts = new ArrayList<>();
currentClass = name;

try (BufferedReader reader = Files.newBufferedReader(file)) {
String line;
if (Files.exists(file)) {
// initialize writtenClass with last CLASS entry

while ((line = reader.readLine()) != null) {
int offset = 0;
List<String> writtenClassParts = new ArrayList<>();

while (offset < line.length() && line.charAt(offset) == '\t') {
offset++;
}
try (BufferedReader reader = Files.newBufferedReader(file)) {
String line;

if (line.startsWith("CLASS ", offset)) {
int start = offset + 6;
int end = line.indexOf(' ', start);
if (end < 0) end = line.length();
String part = line.substring(start, end);
while ((line = reader.readLine()) != null) {
int offset = 0;

while (writtenClassParts.size() > offset) {
writtenClassParts.remove(writtenClassParts.size() - 1);
}
while (offset < line.length() && line.charAt(offset) == '\t') {
offset++;
}

if (line.startsWith("CLASS ", offset)) {
int start = offset + 6;
int end = line.indexOf(' ', start);
if (end < 0) end = line.length();
String part = line.substring(start, end);

writtenClassParts.add(part);
while (writtenClassParts.size() > offset) {
writtenClassParts.remove(writtenClassParts.size() - 1);
}

writtenClassParts.add(part);
}
}

lastWrittenClass = String.join("$", writtenClassParts);
} else {
lastWrittenClass = "";
Files.createDirectories(file.getParent());
}

writer = Files.newBufferedWriter(file, StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
lastWrittenClass = String.join("$", writtenClassParts);
} else {
lastWrittenClass = "";
Files.createDirectories(file.getParent());
}

writeMismatchedOrMissingClasses();
} else if (targetKind == MappedElementKind.FIELD || targetKind == MappedElementKind.METHOD) {
writer.write(' ');
writer.write(desc);
writer.write('\n');
} else {
writer.write('\n');
writer = Files.newBufferedWriter(file, StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
}

return true;
writeMismatchedOrMissingClasses();
}

private final Path dir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.Writer;

import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.format.MappingFormat;

/**
Expand All @@ -31,17 +30,7 @@ public EnigmaFileWriter(Writer writer) throws IOException {
}

@Override
public boolean visitElementContent(MappedElementKind targetKind) throws IOException {
if (targetKind == MappedElementKind.CLASS) {
writeMismatchedOrMissingClasses();
} else if (targetKind == MappedElementKind.FIELD || targetKind == MappedElementKind.METHOD) {
writer.write(' ');
writer.write(desc);
writer.write('\n');
} else {
writer.write('\n');
}

return true;
void visitClassContent() throws IOException {
writeMismatchedOrMissingClasses();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,21 @@ public void visitDstName(MappedElementKind targetKind, int namespace, String nam
}

@Override
public abstract boolean visitElementContent(MappedElementKind targetKind) throws IOException;
public final boolean visitElementContent(MappedElementKind targetKind) throws IOException {
if (targetKind == MappedElementKind.CLASS) {
visitClassContent();
} else if (targetKind == MappedElementKind.FIELD || targetKind == MappedElementKind.METHOD) {
writer.write(' ');
writer.write(desc);
writer.write('\n');
} else {
writer.write('\n');
}

return true;
}

abstract void visitClassContent() throws IOException;

protected static int getNextOuterEnd(String name, int startPos) {
int pos;
Expand Down Expand Up @@ -185,7 +199,8 @@ protected void writeMismatchedOrMissingClasses() throws IOException {
if (dstEnd < 0) dstEnd = dstName.length();
int dstLen = dstEnd - dstStart;

if (dstLen != srcLen || !srcClassName.regionMatches(srcStart, dstName, dstStart, srcLen)) { // src != dst
if (dstLen != srcLen || !srcClassName.regionMatches(srcStart, dstName, dstStart, srcLen) // src != dst
|| dstEnd == dstName.length()) { // always write innermost destination name
writer.write(' ');
writer.write(dstName, dstStart, dstLen);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CLASS class_1 class1Ns0Rename
FIELD field_1 field_1 Lclass_1;
CLASS class_2
CLASS class_2 class_2
FIELD field_2 field_2 Lclass_1$class_2;
CLASS class_3
CLASS class_3 class_3
FIELD field_2 field_2 Lclass_1$class_2$class_3;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CLASS class_1 class1Ns0Rename
FIELD field_1 field_1 Lclass_1;
CLASS class_2
CLASS class_2 class_2
FIELD field_2 field_2 Lclass_1$class_2;
CLASS class_3
CLASS class_3 class_3
FIELD field_2 field_2 Lclass_1$class_2$class_3;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CLASS class_1 class1Ns0Rename
FIELD field_1 field_1 Lclass_1;
CLASS class_2
CLASS class_2 class_2
FIELD field_2 field_2 Lclass_1$class_2;
CLASS class_3
CLASS class_3 class_3
FIELD field_2 field_2 Lclass_1$class_2$class_3;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CLASS class_1 class1Ns0Rename
FIELD field_1 field_1 Lclass_1;
CLASS class_2
CLASS class_2 class_2
FIELD field_2 field_2 Lclass_1$class_2;
CLASS class_3
CLASS class_3 class_3
FIELD field_2 field_2 Lclass_1$class_2$class_3;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CLASS class_1
CLASS class_2
CLASS class_2 class_2
FIELD field_2 field_2 Lclass_1$class_2;
CLASS class_3
CLASS class_3 class_3
FIELD field_2 field_2 Lclass_1$class_2$class_3;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CLASS class_1 class1Ns0Rename
FIELD field_1 field_1 Lclass_1;
CLASS class_2
CLASS class_2 class_2
FIELD field_2 field_2 Lclass_1$class_2;
CLASS class_3
CLASS class_3 class_3
FIELD field_2 field_2 Lclass_1$class_2$class_3;

0 comments on commit e8acce3

Please sign in to comment.