Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Enigma Directory detection to require at least one contained .mapping file #126

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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