Skip to content

Commit

Permalink
Use file extension for CSRG format detection (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas authored Mar 7, 2024
1 parent 1046304 commit 516a029
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added Recaf Simple reader and writer
- Added `OuterClassNameInheritingVisitor`
- Added `MappingFormat#hasWriter` boolean
- Added CSRG detection via the path-based API

## [0.5.1] - 2023-11-30
- Improved documentation
Expand Down
44 changes: 27 additions & 17 deletions src/main/java/net/fabricmc/mappingio/MappingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,23 @@ private MappingReader() {
public static MappingFormat detectFormat(Path file) throws IOException {
if (Files.isDirectory(file)) {
return MappingFormat.ENIGMA_DIR;
} else {
try (Reader reader = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8)) {
return detectFormat(reader);
}
}

try (Reader reader = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8)) {
String fileName = file.getFileName().toString();
int dotIdx = fileName.lastIndexOf('.');
String fileExt = dotIdx >= 0 ? fileName.substring(dotIdx + 1) : null;

return detectFormat(reader, fileExt);
}
}

@Nullable
public static MappingFormat detectFormat(Reader reader) throws IOException {
return detectFormat(reader, null);
}

private static MappingFormat detectFormat(Reader reader, @Nullable String fileExt) throws IOException {
char[] buffer = new char[DETECT_HEADER_LEN];
int pos = 0;
int len;
Expand Down Expand Up @@ -87,7 +95,7 @@ public static MappingFormat detectFormat(Reader reader) throws IOException {
case "CL:":
case "FD:":
case "MD:":
return detectSrgOrXsrg(br);
return detectSrgOrXsrg(br, fileExt);
case "CL ":
case "FD ":
case "MD ":
Expand All @@ -109,31 +117,33 @@ public static MappingFormat detectFormat(Reader reader) throws IOException {
return MappingFormat.TSRG_FILE;
}

// TODO: CSRG, Recaf Simple
if (fileExt != null) {
if (fileExt.equals(MappingFormat.CSRG_FILE.fileExt)) return MappingFormat.CSRG_FILE;
}

// TODO: Recaf Simple

return null; // unknown format or corrupted
return null; // format unknown, not easily detectable or corrupted
}

private static MappingFormat detectSrgOrXsrg(BufferedReader reader) throws IOException {
private static MappingFormat detectSrgOrXsrg(BufferedReader reader, @Nullable String fileExt) throws IOException {
String line;

while ((line = reader.readLine()) != null) {
if (line.startsWith("FD:")) {
String[] parts = line.split(" ");

if (parts.length >= 5) {
if (isEmptyOrStartsWithHash(parts[3]) || isEmptyOrStartsWithHash(parts[4])) {
continue;
}

return MappingFormat.XSRG_FILE;
} else {
break;
if (parts.length < 5
|| isEmptyOrStartsWithHash(parts[3])
|| isEmptyOrStartsWithHash(parts[4])) {
return MappingFormat.SRG_FILE;
}

return MappingFormat.XSRG_FILE;
}
}

return MappingFormat.SRG_FILE;
return MappingFormat.XSRG_FILE.fileExt.equals(fileExt) ? MappingFormat.XSRG_FILE : MappingFormat.SRG_FILE;
}

private static boolean isEmptyOrStartsWithHash(String string) {
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/net/fabricmc/mappingio/read/DetectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void srgFile() throws Exception {
}

@Test
public void xrgFile() throws Exception {
public void xsrgFile() throws Exception {
MappingFormat format = MappingFormat.XSRG_FILE;
check(format);
}
Expand All @@ -81,7 +81,7 @@ public void jamFile() throws Exception {
@Test
public void csrgFile() throws Exception {
MappingFormat format = MappingFormat.CSRG_FILE;
assertThrows(AssertionFailedError.class, () -> check(format));
check(format);
}

@Test
Expand Down Expand Up @@ -119,6 +119,7 @@ private void check(MappingFormat format) throws Exception {
assertEquals(format, MappingReader.detectFormat(path));

if (!format.hasSingleFile()) return;
if (format == MappingFormat.CSRG_FILE) return;

try (Reader reader = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8)) {
assertEquals(format, MappingReader.detectFormat(reader));
Expand Down

0 comments on commit 516a029

Please sign in to comment.