summaryrefslogtreecommitdiff
path: root/enigma-cli/src/main/java/cuchaz/enigma/command/DecompileCommand.java
blob: cc6286396c84e20448c7578a80c21005baec7457 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cuchaz.enigma.command;

import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.source.DecompilerService;
import cuchaz.enigma.source.Decompilers;

import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.Locale;

public class DecompileCommand extends Command {

	public DecompileCommand() {
		super("decompile");
	}

	@Override
	public String getUsage() {
		return "<decompiler> <in jar> <out folder> [<mappings file>]";
	}

	@Override
	public boolean isValidArgument(int length) {
		return length == 3 || length == 4;
	}

	@Override
	public void run(String... args) throws Exception {
		String decompilerName = getArg(args, 0, "decompiler", true);
		Path fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)).toPath();
		Path fileJarOut = getWritableFolder(getArg(args, 2, "out folder", true)).toPath();
		Path fileMappings = getReadablePath(getArg(args, 3, "mappings file", false));

		DecompilerService decompilerService;

		try {
			Field decompilerField = Decompilers.class.getField(decompilerName.toUpperCase(Locale.ROOT));
			decompilerService = (DecompilerService) decompilerField.get(null);
		} catch (NoSuchFieldException e) {
			System.err.println("Decompiler not found.");
			return;
		}

		EnigmaProject project = openProject(fileJarIn, fileMappings);

		ProgressListener progress = new ConsoleProgressListener();

		EnigmaProject.JarExport jar = project.exportRemappedJar(progress);
		EnigmaProject.SourceExport source = jar.decompile(progress, decompilerService);

		source.write(fileJarOut, progress);
	}
}