|
| 1 | +package cuchaz.enigma.translation.mapping.serde; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.FileVisitResult; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.SimpleFileVisitor; |
| 8 | +import java.nio.file.attribute.BasicFileAttributes; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import net.fabricmc.mappingio.MappedElementKind; |
| 13 | +import net.fabricmc.mappingio.MappingVisitor; |
| 14 | +import net.fabricmc.mappingio.adapter.ForwardingMappingVisitor; |
| 15 | + |
| 16 | +import cuchaz.enigma.ProgressListener; |
| 17 | + |
| 18 | +final class ProgressTrackingMappingVisitor extends ForwardingMappingVisitor { |
| 19 | + private final Set<String> classNames; |
| 20 | + private final ProgressListener progressListener; |
| 21 | + private int progress = 0; |
| 22 | + |
| 23 | + ProgressTrackingMappingVisitor(MappingVisitor next, Set<String> classNames, ProgressListener progressListener) { |
| 24 | + super(next); |
| 25 | + this.classNames = classNames; |
| 26 | + this.progressListener = progressListener; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void visitDstName(MappedElementKind targetKind, int namespace, String name) throws IOException { |
| 31 | + if (targetKind == MappedElementKind.CLASS && classNames.contains(name)) { |
| 32 | + progressListener.step(++progress, name); |
| 33 | + } |
| 34 | + |
| 35 | + super.visitDstName(targetKind, namespace, name); |
| 36 | + } |
| 37 | + |
| 38 | + static void trackLoadingProgress(MappingVisitor next, Path path, MappingFormat format, ProgressListener progressListener, VisitorWithProgressConsumer consumer) throws IOException { |
| 39 | + if (format != MappingFormat.ENIGMA_DIRECTORY) { |
| 40 | + consumer.accept(next, 1); |
| 41 | + } |
| 42 | + |
| 43 | + Set<String> classNames = collectClassNames(path); |
| 44 | + consumer.accept(new ProgressTrackingMappingVisitor(next, classNames, progressListener), classNames.size()); |
| 45 | + } |
| 46 | + |
| 47 | + private static Set<String> collectClassNames(Path dir) throws IOException { |
| 48 | + Set<String> names = new HashSet<>(); |
| 49 | + |
| 50 | + Files.walkFileTree(dir, new SimpleFileVisitor<>() { |
| 51 | + @Override |
| 52 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 53 | + // Matches the logic for finding files in mapping-io's EnigmaDirReader. |
| 54 | + String extension = "." + net.fabricmc.mappingio.format.MappingFormat.ENIGMA_FILE.fileExt; |
| 55 | + |
| 56 | + if (file.getFileName().toString().endsWith(extension)) { |
| 57 | + String filePath = dir.relativize(file).toString().replace(dir.getFileSystem().getSeparator(), "/"); |
| 58 | + String className = filePath.substring(0, filePath.length() - extension.length()); |
| 59 | + names.add(className); |
| 60 | + } |
| 61 | + |
| 62 | + return FileVisitResult.CONTINUE; |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + return names; |
| 67 | + } |
| 68 | + |
| 69 | + @FunctionalInterface |
| 70 | + interface VisitorWithProgressConsumer { |
| 71 | + void accept(MappingVisitor visitor, int totalWork) throws IOException; |
| 72 | + } |
| 73 | +} |
0 commit comments