Skip to content

Commit

Permalink
Add checks for #28
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoPangxie732 committed Dec 26, 2021
1 parent d0f5da9 commit a6742b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
20 changes: 10 additions & 10 deletions src/main/java/cn/maxpixel/mcdecompiler/MinecraftDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,13 @@ public void deobfuscate() {
public void decompile(Info.DecompilerType decompilerType) {
if(Files.notExists(options.outputJar())) deobfuscate();
LOGGER.info("Decompiling using \"{}\"", decompilerType);
decompile0(Decompilers.get(decompilerType), options.outputJar().toAbsolutePath().normalize(),
options.outputDecompDir().toAbsolutePath().normalize());
decompile0(Decompilers.get(decompilerType), options.outputJar(), options.outputDecompDir());
}

public void decompileCustomized(String customizedDecompilerName) {
if(Files.notExists(options.outputJar())) deobfuscate();
LOGGER.info("Decompiling using customized decompiler \"{}\"", customizedDecompilerName);
decompile0(Decompilers.getCustom(customizedDecompilerName), options.outputJar().toAbsolutePath().normalize(),
options.outputDecompDir().toAbsolutePath().normalize());
decompile0(Decompilers.getCustom(customizedDecompilerName), options.outputJar(), options.outputDecompDir());
}

private void decompile0(IDecompiler decompiler, Path inputJar, Path outputDir) {
Expand Down Expand Up @@ -170,8 +168,8 @@ public static class OptionBuilder {
public OptionBuilder(String version, Info.SideType type) {
this.version = Objects.requireNonNull(version, "version cannot be null!");
this.type = Objects.requireNonNull(type, "type cannot be null!");
this.outputJar = Path.of("output", version + "_" + type + "_deobfuscated.jar");
this.outputDecompDir = Path.of("output", version + "_" + type + "_decompiled");
this.outputJar = Path.of("output", version + "_" + type + "_deobfuscated.jar").toAbsolutePath().normalize();
this.outputDecompDir = Path.of("output", version + "_" + type + "_decompiled").toAbsolutePath().normalize();
}

public OptionBuilder(Path inputJar) {
Expand All @@ -181,8 +179,8 @@ public OptionBuilder(Path inputJar) {
public OptionBuilder(Path inputJar, boolean reverse) {
this.inputJar = inputJar;
this.reverse = reverse;
this.outputJar = Path.of("output", "deobfuscated.jar");
this.outputDecompDir = Path.of("output", "decompiled");
this.outputJar = Path.of("output", "deobfuscated.jar").toAbsolutePath().normalize();
this.outputDecompDir = Path.of("output", "decompiled").toAbsolutePath().normalize();
}

public OptionBuilder libsUsing(String version) {
Expand Down Expand Up @@ -214,12 +212,12 @@ public OptionBuilder withMapping(BufferedReader inputMappings) {
}

public OptionBuilder output(Path outputJar) {
this.outputJar = Objects.requireNonNull(outputJar, "outputJar cannot be null");
this.outputJar = Objects.requireNonNull(outputJar, "outputJar cannot be null").toAbsolutePath().normalize();
return this;
}

public OptionBuilder outputDecomp(Path outputDecompDir) {
this.outputDecompDir = Objects.requireNonNull(outputDecompDir, "outputDecompDir cannot be null");
this.outputDecompDir = Objects.requireNonNull(outputDecompDir, "outputDecompDir cannot be null").toAbsolutePath().normalize();
return this;
}

Expand All @@ -239,6 +237,8 @@ public OptionBuilder regenerateVariableNames() {
}

public Options build() {
if(this.outputJar.getParent().equals(this.outputDecompDir))
throw new IllegalArgumentException("The parent directory of outputJar cannot be the same as outputDecomp");
return new Options() {
@Override
public String version() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import cn.maxpixel.mcdecompiler.util.Utils;
import joptsimple.*;
import joptsimple.util.PathConverter;
import joptsimple.util.PathProperties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -58,14 +59,14 @@ public static void main(String[] args) throws Throwable {
ArgumentAcceptingOptionSpec<String> targetNamespaceO = parser.accepts("targetNamespace", "The target namespace to remap " +
"to if you are using namespaced mappings(Tiny, Tsrgv2)").availableUnless(sideTypeO).withRequiredArg();
ArgumentAcceptingOptionSpec<Path> inputO = parser.acceptsAll(asList("i", "input"), "The input file. With this option, you must " +
"specify --mappingPath option and musn't specify --version or --side option.").availableUnless(sideTypeO).requiredUnless(sideTypeO)
.withRequiredArg().withValuesConvertedBy(new PathConverter());
"specify --mappingPath option and can't specify --version or --side option.").availableUnless(sideTypeO).requiredUnless(sideTypeO)
.withRequiredArg().withValuesConvertedBy(new PathConverter(PathProperties.FILE_EXISTING));
ArgumentAcceptingOptionSpec<String> mappingPathO = parser.acceptsAll(asList("m", "map", "mappingPath"), "Mapping file use to " +
"deobfuscate.").requiredUnless(sideTypeO).withRequiredArg();
ArgumentAcceptingOptionSpec<Path> outputO = parser.acceptsAll(asList("o", "output"), "The remapped file. Includes suffix.")
.withRequiredArg().withValuesConvertedBy(new PathConverter());
ArgumentAcceptingOptionSpec<Path> outputDecompO = parser.accepts("outputDecomp", "The output decompile directory")
ArgumentAcceptingOptionSpec<Path> outputO = parser.acceptsAll(asList("o", "output"), "The remapped file. Including the suffix.")
.withRequiredArg().withValuesConvertedBy(new PathConverter());
ArgumentAcceptingOptionSpec<Path> outputDecompO = parser.accepts("outputDecomp", "The decompiled output directory. Will " +
"be deleted before decompiling if it is exist").withRequiredArg().withValuesConvertedBy(new PathConverter());
ArgumentAcceptingOptionSpec<Info.DecompilerType> decompileO = parser.acceptsAll(asList("d", "decompile"), "Decompile the " +
"deobfuscated jar. Values are \"FERNFLOWER\", \"OFFICIAL_FERNFLOWER\", \"FORGEFLOWER\", \"CFR\" and \"USER_DEFINED\". Do NOT pass " +
"any arg to this option when \"customDecompilerName\" option is specified.").withOptionalArg().ofType(Info.DecompilerType.class)
Expand Down

0 comments on commit a6742b8

Please sign in to comment.