From 5c7eac2d4212f57fa65219873491fc9783f43266 Mon Sep 17 00:00:00 2001 From: Julian Burner <48808497+NebelNidas@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:01:08 +0100 Subject: [PATCH] Adjust Enigma Dir detection to require at least one contained `.mapping` file (#126) --- CHANGELOG.md | 1 + .../net/fabricmc/mappingio/MappingReader.java | 29 +++++++++++++++---- .../test/tests/reading/DetectionTest.java | 24 ++++++++++----- .../detection/non-mapping-dir/text.txt | 1 + 4 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 src/test/resources/detection/non-mapping-dir/text.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b38e6f8..e2524cab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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 +- Adjusted format detection to only return ENIGMA_DIR for non-empty directories with at least one `.mapping` file ## [0.7.1] - 2025-01-07 - Restored the ability to read source-namespace-only mapping files, even if not spec-compliant diff --git a/src/main/java/net/fabricmc/mappingio/MappingReader.java b/src/main/java/net/fabricmc/mappingio/MappingReader.java index 7191f1e4..bc6cbbae 100644 --- a/src/main/java/net/fabricmc/mappingio/MappingReader.java +++ b/src/main/java/net/fabricmc/mappingio/MappingReader.java @@ -21,10 +21,14 @@ import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; +import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import org.jetbrains.annotations.Nullable; @@ -46,13 +50,28 @@ private MappingReader() { } @Nullable - public static MappingFormat detectFormat(Path file) throws IOException { - if (Files.isDirectory(file)) { - return MappingFormat.ENIGMA_DIR; + public static MappingFormat detectFormat(Path path) throws IOException { + if (Files.isDirectory(path)) { + String enigmaExt = "." + MappingFormat.ENIGMA_FILE.fileExt; + AtomicReference ret = new AtomicReference<>(); + + Files.walkFileTree(path, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + if (file.getFileName().toString().endsWith(enigmaExt)) { + ret.set(MappingFormat.ENIGMA_DIR); + return FileVisitResult.TERMINATE; + } + + return FileVisitResult.CONTINUE; + } + }); + + return ret.get(); } - try (Reader reader = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8)) { - String fileName = file.getFileName().toString(); + try (Reader reader = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8)) { + String fileName = path.getFileName().toString(); int dotIdx = fileName.lastIndexOf('.'); String fileExt = dotIdx >= 0 ? fileName.substring(dotIdx + 1) : null; diff --git a/src/test/java/net/fabricmc/mappingio/test/tests/reading/DetectionTest.java b/src/test/java/net/fabricmc/mappingio/test/tests/reading/DetectionTest.java index 0e0d485a..000cb2cb 100644 --- a/src/test/java/net/fabricmc/mappingio/test/tests/reading/DetectionTest.java +++ b/src/test/java/net/fabricmc/mappingio/test/tests/reading/DetectionTest.java @@ -17,6 +17,7 @@ package net.fabricmc.mappingio.test.tests.reading; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.InputStreamReader; @@ -35,21 +36,28 @@ import net.fabricmc.mappingio.test.visitors.NopMappingVisitor; public class DetectionTest { - private static final MappingDir dir = TestMappings.DETECTION; - @Test public void run() throws Exception { - for (MappingFormat format : MappingFormat.values()) { - if (format == MappingFormat.RECAF_SIMPLE_FILE) { - assertThrows(AssertionFailedError.class, () -> check(format)); - } else { - check(format); + for (MappingDir dir : TestMappings.values()) { + for (MappingFormat format : MappingFormat.values()) { + if (format == MappingFormat.RECAF_SIMPLE_FILE) { + assertThrows(AssertionFailedError.class, () -> check(dir, format)); + } else { + check(dir, format); + } } } + + assertNull(MappingReader.detectFormat(TestMappings.DETECTION.path().resolve("non-mapping-dir"))); } - private void check(MappingFormat format) throws Exception { + private void check(MappingDir dir, MappingFormat format) throws Exception { Path path = dir.pathFor(format); + + if (!Files.exists(path)) { + return; + } + assertEquals(format, MappingReader.detectFormat(path)); if (!format.hasSingleFile()) return; diff --git a/src/test/resources/detection/non-mapping-dir/text.txt b/src/test/resources/detection/non-mapping-dir/text.txt new file mode 100644 index 00000000..9daeafb9 --- /dev/null +++ b/src/test/resources/detection/non-mapping-dir/text.txt @@ -0,0 +1 @@ +test