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

Make "checkmappings" command support all readable formats #465

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
Expand Down Expand Up @@ -43,10 +42,9 @@ public void run(String... args) throws Exception {

System.out.println("Reading mappings...");

MappingFormat format = chooseEnigmaFormat(fileMappings);
MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();

EntryTree<EntryMapping> mappings = format.read(fileMappings, ProgressListener.none(), saveParameters);
EntryTree<EntryMapping> mappings = readMappings(fileMappings, ProgressListener.none(), saveParameters);
project.setMappings(mappings);

JarIndex idx = project.getJarIndex();
Expand Down
30 changes: 23 additions & 7 deletions enigma-cli/src/main/java/cuchaz/enigma/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.google.common.io.MoreFiles;

Expand Down Expand Up @@ -41,22 +43,36 @@ protected static EnigmaProject openProject(Path fileJarIn, Path fileMappings) th
System.out.println("Reading mappings...");

MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
EntryTree<EntryMapping> mappings = chooseEnigmaFormat(fileMappings).read(fileMappings, progress, saveParameters);
EntryTree<EntryMapping> mappings = readMappings(fileMappings, progress, saveParameters);

project.setMappings(mappings);
}

return project;
}

protected static MappingFormat chooseEnigmaFormat(Path path) {
if (Files.isDirectory(path)) {
return MappingFormat.ENIGMA_DIRECTORY;
} else if ("zip".equalsIgnoreCase(MoreFiles.getFileExtension(path))) {
return MappingFormat.ENIGMA_ZIP;
protected static EntryTree<EntryMapping> readMappings(Path path, ProgressListener progress, MappingSaveParameters saveParameters) throws Exception {
List<Exception> suppressed = new ArrayList<>();

if ("zip".equalsIgnoreCase(MoreFiles.getFileExtension(path))) {
return MappingFormat.ENIGMA_ZIP.read(path, progress, saveParameters);
} else {
return MappingFormat.ENIGMA_FILE;
for (MappingFormat format : MappingFormat.getReadableFormats()) {
try {
return format.read(path, progress, saveParameters);
} catch (Exception e) {
suppressed.add(e);
}
Comment on lines +61 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awful, it should detect the format before trying to read it like this.

Mapping-io has a thing to detect mapping formats, either use something similar or come back to this change once moved over to mapping-io.

For now I see little benefit in this PR.

Copy link
Contributor Author

@NebelNidas NebelNidas Sep 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely a bit hacky, but since we'll be switching to mapping-io soon anyway, I don't see a point in investing the time to write a proper mapping format detector.

For now I see little benefit in this PR

Users can check other mapping formats for correctness, I think it's worth it ;)

}
}

RuntimeException exception = new RuntimeException("Unable to parse mappings!");

for (Exception suppressedException : suppressed) {
exception.addSuppressed(suppressedException);
}

throw exception;
}

protected static File getWritableFile(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -66,4 +68,18 @@ public MappingsWriter getWriter() {
public MappingsReader getReader() {
return reader;
}

public static List<MappingFormat> getWritableFormats() {
return Arrays.asList(values())
.stream()
.filter(format -> format.getWriter() != null)
.toList();
}

public static List<MappingFormat> getReadableFormats() {
return Arrays.asList(values())
.stream()
.filter(format -> format.getReader() != null)
.toList();
}
}