diff options
| author | 2025-10-18 18:46:07 +0300 | |
|---|---|---|
| committer | 2025-10-18 16:46:07 +0100 | |
| commit | 1edc1eb91037443e8d341cbe70e6c57ea42ceb79 (patch) | |
| tree | 99390ab5edac7a6f591270031aaf4d657b914d90 | |
| parent | Annotation editor support (#568) (diff) | |
| download | enigma-fork-1edc1eb91037443e8d341cbe70e6c57ea42ceb79.tar.gz enigma-fork-1edc1eb91037443e8d341cbe70e6c57ea42ceb79.tar.xz enigma-fork-1edc1eb91037443e8d341cbe70e6c57ea42ceb79.zip | |
Track mapping loading progress for the Enigma directory format (#577)
| -rw-r--r-- | enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java | 7 | ||||
| -rw-r--r-- | enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/ProgressTrackingMappingVisitor.java | 73 |
2 files changed, 77 insertions, 3 deletions
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java index b891f43..fb52ac0 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java | |||
| @@ -93,10 +93,11 @@ public enum MappingFormat { | |||
| 93 | loadingMessage = I18n.translate("progress.mappings.loading_directory"); | 93 | loadingMessage = I18n.translate("progress.mappings.loading_directory"); |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | progressListener.init(1, loadingMessage); | ||
| 97 | |||
| 98 | VisitableMappingTree mappingTree = new MemoryMappingTree(); | 96 | VisitableMappingTree mappingTree = new MemoryMappingTree(); |
| 99 | MappingReader.read(path, mappingIoCounterpart, mappingTree); | 97 | ProgressTrackingMappingVisitor.trackLoadingProgress(mappingTree, path, this, progressListener, (visitor, totalWork) -> { |
| 98 | progressListener.init(totalWork, loadingMessage); | ||
| 99 | MappingReader.read(path, mappingIoCounterpart, visitor); | ||
| 100 | }); | ||
| 100 | EntryTree<EntryMapping> mappings = MappingIoConverter.fromMappingIo(mappingTree, progressListener, index); | 101 | EntryTree<EntryMapping> mappings = MappingIoConverter.fromMappingIo(mappingTree, progressListener, index); |
| 101 | 102 | ||
| 102 | return this == PROGUARD ? MappingOperations.invert(mappings) : mappings; | 103 | return this == PROGUARD ? MappingOperations.invert(mappings) : mappings; |
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/ProgressTrackingMappingVisitor.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/ProgressTrackingMappingVisitor.java new file mode 100644 index 0000000..0ee90e4 --- /dev/null +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/ProgressTrackingMappingVisitor.java | |||
| @@ -0,0 +1,73 @@ | |||
| 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 | } | ||