Skip to content

Commit

Permalink
Adjust Enigma Dir detection to require at least one contained `.mappi…
Browse files Browse the repository at this point in the history
…ng` file (#126)
  • Loading branch information
NebelNidas authored Jan 18, 2025
1 parent e8acce3 commit 5c7eac2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/net/fabricmc/mappingio/MappingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<MappingFormat> ret = new AtomicReference<>();

Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/detection/non-mapping-dir/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test

0 comments on commit 5c7eac2

Please sign in to comment.