Skip to content

Commit

Permalink
fix class parsing for enigma format
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Aug 11, 2023
1 parent 3dcd89c commit 0537ff7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import cuchaz.enigma.translation.mapping.EntryMapping;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

public final class RawEntryMapping {
private final String targetName;
private final List<String> javadocs = new ArrayList<>();

public RawEntryMapping(String targetName) {
public RawEntryMapping(@Nullable String targetName) {
this.targetName = targetName != null && !targetName.equals("-") ? targetName : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Deque;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;

public enum EnigmaMappingsReader implements MappingsReader {
FILE {
Expand All @@ -56,10 +57,13 @@ public EntryTree<EntryMapping> read(Path root, ProgressListener progress, Mappin

EntryTree<EntryMapping> mappings = new HashEntryTree<>();

List<Path> files = Files.walk(root)
List<Path> files;
try (Stream<Path> fileStream = Files.walk(root)) {
files = fileStream
.filter(f -> !Files.isDirectory(f))
.filter(f -> f.toString().endsWith(".mapping"))
.toList();
}

progress.init(files.size(), I18n.translate("progress.mappings.enigma_directory.loading"));
int step = 0;
Expand All @@ -85,34 +89,6 @@ public EntryTree<EntryMapping> read(Path zip, ProgressListener progress, Mapping
}
};

/**
* Reads multiple Enigma mapping files.
*
* @param progress the progress listener
* @param paths the Enigma files to read; cannot be empty
* @return the parsed mappings
* @throws MappingParseException if a mapping file cannot be parsed
* @throws IOException if an IO error occurs
* @throws IllegalArgumentException if there are no paths to read
*/
public static EntryTree<EntryMapping> readFiles(ProgressListener progress, Path... paths) throws MappingParseException, IOException {
EntryTree<EntryMapping> mappings = new HashEntryTree<>();

if (paths.length == 0) {
throw new IllegalArgumentException("No paths to read mappings from");
}

progress.init(paths.length, I18n.translate("progress.mappings.enigma_directory.loading"));
int step = 0;

for (Path file : paths) {
progress.step(step++, paths.toString());
readFile(file, mappings);
}

return mappings;
}

private static void readFile(Path path, EntryTree<EntryMapping> mappings) throws IOException, MappingParseException {
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
Deque<MappingPair<?, RawEntryMapping>> mappingStack = new ArrayDeque<>();
Expand Down Expand Up @@ -245,7 +221,12 @@ private static MappingPair<ClassEntry, RawEntryMapping> parseClass(@Nullable Ent
obfuscatedEntry = new ClassEntry(obfuscatedName);
}

String mapping = tokens[2];
String mapping = null;
if (tokens.length == 3) {
mapping = tokens[2];
} else if (tokens.length != 2) {
throw new RuntimeException("invalid class declaration: not enough tokens (" + tokens.length + " found, 2 needed)!");
}

return new MappingPair<>(obfuscatedEntry, new RawEntryMapping(mapping));
}
Expand Down

0 comments on commit 0537ff7

Please sign in to comment.