diff options
| author | 2019-05-15 12:45:41 +0200 | |
|---|---|---|
| committer | 2019-05-15 12:45:41 +0200 | |
| commit | df8def23dd0336d8a5d2369c5d4c0f4331838ef4 (patch) | |
| tree | de7fbdc9ccdf4a20c83103c0e2b0c3129c84430b /src/main/java/cuchaz/enigma/CommandMain.java | |
| parent | Resolve root when navigating to declaration (diff) | |
| download | enigma-fork-df8def23dd0336d8a5d2369c5d4c0f4331838ef4.tar.gz enigma-fork-df8def23dd0336d8a5d2369c5d4c0f4331838ef4.tar.xz enigma-fork-df8def23dd0336d8a5d2369c5d4c0f4331838ef4.zip | |
checkmappings command (#137)
* Use expected map sizes for remapped multimaps
* Index method and field types
* Add package visibility index
* Add checkmappings command and use System.err for error messages
* Use exit codes for errors
* Remove outer class check for package visible only refs
* Throw exception on mapping error instead of exiting
Diffstat (limited to 'src/main/java/cuchaz/enigma/CommandMain.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/CommandMain.java | 62 |
1 files changed, 52 insertions, 10 deletions
diff --git a/src/main/java/cuchaz/enigma/CommandMain.java b/src/main/java/cuchaz/enigma/CommandMain.java index c9f8382..db4fd12 100644 --- a/src/main/java/cuchaz/enigma/CommandMain.java +++ b/src/main/java/cuchaz/enigma/CommandMain.java | |||
| @@ -11,35 +11,47 @@ | |||
| 11 | 11 | ||
| 12 | package cuchaz.enigma; | 12 | package cuchaz.enigma; |
| 13 | 13 | ||
| 14 | import cuchaz.enigma.analysis.index.JarIndex; | ||
| 14 | import cuchaz.enigma.translation.mapping.EntryMapping; | 15 | import cuchaz.enigma.translation.mapping.EntryMapping; |
| 15 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; | 16 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; |
| 16 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | 17 | import cuchaz.enigma.translation.mapping.tree.EntryTree; |
| 18 | import cuchaz.enigma.translation.representation.entry.ClassEntry; | ||
| 17 | 19 | ||
| 18 | import java.io.File; | 20 | import java.io.File; |
| 19 | import java.nio.file.Files; | 21 | import java.nio.file.Files; |
| 20 | import java.nio.file.Path; | 22 | import java.nio.file.Path; |
| 21 | import java.nio.file.Paths; | 23 | import java.nio.file.Paths; |
| 22 | import java.util.Locale; | 24 | import java.util.Locale; |
| 25 | import java.util.Set; | ||
| 23 | import java.util.jar.JarFile; | 26 | import java.util.jar.JarFile; |
| 27 | import java.util.stream.Collectors; | ||
| 24 | 28 | ||
| 25 | public class CommandMain { | 29 | public class CommandMain { |
| 26 | 30 | ||
| 27 | public static void main(String[] args) throws Exception { | 31 | public static void main(String[] args) throws Exception { |
| 28 | try { | 32 | try { |
| 29 | // process the command | 33 | // process the command |
| 30 | String command = getArg(args, 0, "command", true); | 34 | String command = getArg(args, 0, "command", true).toLowerCase(Locale.ROOT); |
| 31 | if (command.equalsIgnoreCase("deobfuscate")) { | 35 | switch (command) { |
| 32 | deobfuscate(args); | 36 | case "deobfuscate": |
| 33 | } else if (command.equalsIgnoreCase("decompile")) { | 37 | deobfuscate(args); |
| 34 | decompile(args); | 38 | break; |
| 35 | } else if (command.equalsIgnoreCase("convertmappings")) { | 39 | case "decompile": |
| 36 | convertMappings(args); | 40 | decompile(args); |
| 37 | } else { | 41 | break; |
| 38 | throw new IllegalArgumentException("Command not recognized: " + command); | 42 | case "convertmappings": |
| 43 | convertMappings(args); | ||
| 44 | break; | ||
| 45 | case "checkmappings": | ||
| 46 | checkMappings(args); | ||
| 47 | break; | ||
| 48 | default: | ||
| 49 | throw new IllegalArgumentException("Command not recognized: " + command); | ||
| 39 | } | 50 | } |
| 40 | } catch (IllegalArgumentException ex) { | 51 | } catch (IllegalArgumentException ex) { |
| 41 | System.out.println(ex.getMessage()); | 52 | System.err.println(ex.getMessage()); |
| 42 | printHelp(); | 53 | printHelp(); |
| 54 | System.exit(1); | ||
| 43 | } | 55 | } |
| 44 | } | 56 | } |
| 45 | 57 | ||
| @@ -51,6 +63,7 @@ public class CommandMain { | |||
| 51 | System.out.println("\t\tdeobfuscate <in jar> <out jar> [<mappings file>]"); | 63 | System.out.println("\t\tdeobfuscate <in jar> <out jar> [<mappings file>]"); |
| 52 | System.out.println("\t\tdecompile <in jar> <out folder> [<mappings file>]"); | 64 | System.out.println("\t\tdecompile <in jar> <out folder> [<mappings file>]"); |
| 53 | System.out.println("\t\tconvertmappings <enigma mappings> <converted mappings> <ENIGMA_FILE|ENIGMA_DIRECTORY|SRG_FILE>"); | 65 | System.out.println("\t\tconvertmappings <enigma mappings> <converted mappings> <ENIGMA_FILE|ENIGMA_DIRECTORY|SRG_FILE>"); |
| 66 | System.out.println("\t\tcheckmappings <in jar> <mappings file>"); | ||
| 54 | } | 67 | } |
| 55 | 68 | ||
| 56 | private static void decompile(String[] args) throws Exception { | 69 | private static void decompile(String[] args) throws Exception { |
| @@ -100,6 +113,35 @@ public class CommandMain { | |||
| 100 | saveFormat.write(mappings, result.toPath(), new ConsoleProgressListener()); | 113 | saveFormat.write(mappings, result.toPath(), new ConsoleProgressListener()); |
| 101 | } | 114 | } |
| 102 | 115 | ||
| 116 | private static void checkMappings(String[] args) throws Exception { | ||
| 117 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); | ||
| 118 | Path fileMappings = getReadablePath(getArg(args, 2, "enigma mapping", true)); | ||
| 119 | |||
| 120 | System.out.println("Reading JAR..."); | ||
| 121 | Deobfuscator deobfuscator = new Deobfuscator(new JarFile(fileJarIn)); | ||
| 122 | System.out.println("Reading mappings..."); | ||
| 123 | |||
| 124 | MappingFormat format = chooseEnigmaFormat(fileMappings); | ||
| 125 | EntryTree<EntryMapping> mappings = format.read(fileMappings, ProgressListener.VOID); | ||
| 126 | deobfuscator.setMappings(mappings); | ||
| 127 | |||
| 128 | JarIndex idx = deobfuscator.getJarIndex(); | ||
| 129 | |||
| 130 | boolean error = false; | ||
| 131 | |||
| 132 | for (Set<ClassEntry> partition : idx.getPackageVisibilityIndex().getPartitions()) { | ||
| 133 | long packages = partition.stream().map(deobfuscator.getMapper()::deobfuscate).map(ClassEntry::getPackageName).distinct().count(); | ||
| 134 | if (packages > 1) { | ||
| 135 | error = true; | ||
| 136 | System.err.println("ERROR: Must be in one package:\n" + partition.stream().map(deobfuscator.getMapper()::deobfuscate).map(ClassEntry::toString).sorted().collect(Collectors.joining("\n"))); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | if (error) { | ||
| 141 | throw new Exception("Access violations detected"); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 103 | private static MappingFormat chooseEnigmaFormat(Path path) { | 145 | private static MappingFormat chooseEnigmaFormat(Path path) { |
| 104 | if (Files.isDirectory(path)) { | 146 | if (Files.isDirectory(path)) { |
| 105 | return MappingFormat.ENIGMA_DIRECTORY; | 147 | return MappingFormat.ENIGMA_DIRECTORY; |