Skip to content
Merged
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
@@ -1,84 +1,3 @@
package org.quiltmc.enigma.command;

public enum Argument {
INPUT_JAR("<input-jar>",
"""
A path to the .jar file to use in executing the command."""
),
INPUT_MAPPINGS("<input-mappings>",
"""
A path to the file or folder to read mappings from."""
),
LEFT_MAPPINGS("<left-mappings>",
"""
A path to the left file or folder to read mappings from, used in commands which take two mapping inputs."""
),
RIGHT_MAPPINGS("<right-mappings>",
"""
A path to the right file or folder to read mappings from, used in commands which take two mapping inputs."""
),
MAPPING_OUTPUT("<mapping-output>",
"""
A path to the file or folder to write mappings to. Will be created if missing."""
),
KEEP_MODE("<keep-mode>",
"""
Which mappings should overwrite the others when composing conflicting mappings. Allowed values are "left", "right", and "both"."""
),
DECOMPILER("<decompiler>",
"""
The decompiler to use when producing output. Allowed values are (case-insensitive):
- VINEFLOWER
- CFR
- PROCYON
- BYTECODE"""
),
OUTPUT_FOLDER("<output-folder>",
"""
A path to the file or folder to write output to."""
),
OUTPUT_JAR("<output-jar>",
"""
A path to the .jar file to write output to. Will be created if missing."""
),
FILL_ALL("<fill-all>",
"""
Whether to fill all possible mappings. Allowed values are "true" and "false"."""
),
ENIGMA_PROFILE("<enigma-profile>",
"""
A path to an Enigma profile JSON file, used to apply things like plugins."""
),
OBFUSCATED_NAMESPACE("<obfuscated-namespace>",
"""
The namespace to use for obfuscated names when writing mappings. Only used in certain mapping formats."""
),
DEOBFUSCATED_NAMESPACE("<deobfuscated-namespace>",
"""
The namespace to use for deobfuscated names when writing mappings. Only used in certain mapping formats."""
);

private final String displayForm;
private final String explanation;

Argument(String displayForm, String explanation) {
this.displayForm = displayForm;
this.explanation = explanation;
}

public String getDisplayForm() {
return this.displayForm;
}

public String getExplanation() {
return this.explanation;
}

public ComposedArgument required() {
return new ComposedArgument(this, false);
}

public ComposedArgument optional() {
return new ComposedArgument(this, true);
}
}
record Argument(String displayForm, String explanation) { }
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import java.util.Set;
import java.util.stream.Collectors;

public class CheckMappingsCommand extends Command {
public CheckMappingsCommand() {
super(Argument.INPUT_JAR.required(),
Argument.INPUT_MAPPINGS.required()
);
public final class CheckMappingsCommand extends Command {
public static final CheckMappingsCommand INSTANCE = new CheckMappingsCommand();

private CheckMappingsCommand() {
super(CommonArguments.INPUT_JAR, CommonArguments.INPUT_MAPPINGS);
}

@Override
Expand Down
61 changes: 31 additions & 30 deletions enigma-cli/src/main/java/org/quiltmc/enigma/command/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.command;

import com.google.common.collect.ImmutableList;
import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.EnigmaProfile;
import org.quiltmc.enigma.api.EnigmaProject;
Expand All @@ -25,32 +26,28 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;

public abstract class Command {
protected final List<Argument> requiredArguments = new ArrayList<>();
protected final List<Argument> optionalArguments = new ArrayList<>();
protected final List<ComposedArgument> allArguments;
public abstract sealed class Command permits
CheckMappingsCommand, ComposeMappingsCommand, ConvertMappingsCommand, DecompileCommand, DeobfuscateCommand,
DropInvalidMappingsCommand, FillClassMappingsCommand, HelpCommand, InsertProposedMappingsCommand,
InvertMappingsCommand, MapSpecializedMethodsCommand, PrintStatsCommand {
final ImmutableList<Argument> requiredArguments;
final ImmutableList<Argument> optionalArguments;

protected Command(ComposedArgument... arguments) {
this.allArguments = new ArrayList<>();
final int totalArgumentCount;

for (ComposedArgument argument : arguments) {
if (argument.optional()) {
this.optionalArguments.add(argument.argument());
this.allArguments.add(argument);
} else {
this.requiredArguments.add(argument.argument());
this.allArguments.add(argument);
Command(Argument... requiredArguments) {
this(ImmutableList.copyOf(requiredArguments), ImmutableList.of());
}

if (!this.optionalArguments.isEmpty()) {
throw new IllegalArgumentException("optional arguments should be grouped at the end of command arguments! (declaring arg " + argument + ")");
}
}
}
Command(Collection<Argument> requiredArguments, Collection<Argument> optionalArguments) {
this.requiredArguments = ImmutableList.copyOf(requiredArguments);
this.optionalArguments = ImmutableList.copyOf(optionalArguments);
this.totalArgumentCount = this.requiredArguments.size() + this.optionalArguments.size();
}

public String getUsage() {
Expand All @@ -68,7 +65,7 @@ public String getUsage() {

private static void appendArguments(StringBuilder builder, List<Argument> arguments) {
for (int i = 0; i < arguments.size(); i++) {
builder.append(arguments.get(i).getDisplayForm());
builder.append(arguments.get(i).displayForm());
if (i < arguments.size() - 1) {
builder.append(" ");
}
Expand All @@ -81,8 +78,7 @@ private static void appendArguments(StringBuilder builder, List<Argument> argume
* @return {@code true} if the argument count is valid, {@code false} otherwise
*/
public boolean checkArgumentCount(int length) {
// valid if length is equal to the amount of required arguments or between required argument count and total argument count
return length == this.requiredArguments.size() || length > this.requiredArguments.size() && length <= this.allArguments.size();
return length >= this.requiredArguments.size() && length <= this.totalArgumentCount;
}

/**
Expand Down Expand Up @@ -125,10 +121,15 @@ public static Enigma createEnigma() {
* @return the argument, as a string
*/
protected String getArg(String[] args, int index) {
if (index < this.allArguments.size() && index >= 0) {
return getArg(args, index, this.allArguments.get(index));
if (index < this.totalArgumentCount && index >= 0) {
final int requiredCount = this.requiredArguments.size();
if (index < requiredCount) {
return getArg(args, index, this.requiredArguments.get(index), false);
} else {
return getArg(args, index, this.optionalArguments.get(index - requiredCount), true);
}
} else {
throw new RuntimeException("arg index is outside of range of possible arguments! (index: " + index + ", allowed arg count: " + this.allArguments.size() + ")");
throw new RuntimeException("arg index is outside of range of possible arguments! (index: " + index + ", allowed arg count: " + this.totalArgumentCount + ")");
}
}

Expand Down Expand Up @@ -244,16 +245,16 @@ protected static Path getWritablePath(String path) {
return dir;
}

private static String getArg(String[] args, int i, ComposedArgument argument) {
if (i >= args.length) {
if (!argument.optional()) {
throw new IllegalArgumentException(argument.argument().getDisplayForm() + " is required");
private static String getArg(String[] args, int index, Argument argument, boolean optional) {
if (index >= args.length) {
if (!optional) {
throw new IllegalArgumentException(argument.displayForm() + " is required");
} else {
return null;
}
}

return args[i];
return args[index];
}

protected static boolean shouldDebug(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import java.nio.file.Path;

public class CommandsUtil {
private CommandsUtil() {
throw new UnsupportedOperationException();
}

public static ReadWriteService getReadWriteService(Enigma enigma, Path file) {
var service = enigma.getReadWriteService(file);
if (service.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.quiltmc.enigma.command;

final class CommonArguments {
private CommonArguments() {
throw new UnsupportedOperationException();
}

static final Argument INPUT_JAR = new Argument("<input-jar>",
"""
A path to the .jar file to use in executing the command."""
);
static final Argument INPUT_MAPPINGS = new Argument("<input-mappings>",
"""
A path to the file or folder to read mappings from."""
);
static final Argument MAPPING_OUTPUT = new Argument("<mapping-output>",
"""
A path to the file or folder to write mappings to. Will be created if missing."""
);
static final Argument OUTPUT_JAR = new Argument("<output-jar>",
"""
A path to the .jar file to write output to. Will be created if missing."""
);
static final Argument ENIGMA_PROFILE = new Argument("<enigma-profile>",
"""
A path to an Enigma profile JSON file, used to apply things like plugins."""
);
static final Argument OBFUSCATED_NAMESPACE = new Argument("<obfuscated-namespace>",
"""
The namespace to use for obfuscated names when writing mappings. Only used in certain mapping formats."""
);
static final Argument DEOBFUSCATED_NAMESPACE = new Argument("<deobfuscated-namespace>",
"""
The namespace to use for deobfuscated names when writing mappings. Only used in certain mapping formats."""
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@
import java.io.IOException;
import java.nio.file.Path;

public class ComposeMappingsCommand extends Command {
public ComposeMappingsCommand() {
super(Argument.LEFT_MAPPINGS.required(),
Argument.RIGHT_MAPPINGS.required(),
Argument.MAPPING_OUTPUT.required(),
Argument.KEEP_MODE.required()
);
public final class ComposeMappingsCommand extends Command {
private static final Argument LEFT_MAPPINGS = new Argument("<left-mappings>",
"""
A path to the left file or folder to read mappings from, used in commands which take two mapping inputs."""
);
private static final Argument RIGHT_MAPPINGS = new Argument("<right-mappings>",
"""
A path to the right file or folder to read mappings from, used in commands which take two mapping inputs."""
);
private static final Argument KEEP_MODE = new Argument("<keep-mode>",
"""
Which mappings should overwrite the others when composing conflicting mappings. Allowed values are "left", "right", and "both"."""
);

public static final ComposeMappingsCommand INSTANCE = new ComposeMappingsCommand();

private ComposeMappingsCommand() {
super(LEFT_MAPPINGS, RIGHT_MAPPINGS, CommonArguments.MAPPING_OUTPUT, KEEP_MODE);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
import java.io.IOException;
import java.nio.file.Path;

public class ConvertMappingsCommand extends Command {
public ConvertMappingsCommand() {
super(Argument.INPUT_MAPPINGS.required(),
Argument.MAPPING_OUTPUT.required(),
Argument.OBFUSCATED_NAMESPACE.required(),
Argument.DEOBFUSCATED_NAMESPACE.required()
public final class ConvertMappingsCommand extends Command {
public static final ConvertMappingsCommand INSTANCE = new ConvertMappingsCommand();

private ConvertMappingsCommand() {
super(
CommonArguments.INPUT_MAPPINGS,
CommonArguments.MAPPING_OUTPUT,
CommonArguments.OBFUSCATED_NAMESPACE,
CommonArguments.DEOBFUSCATED_NAMESPACE
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.command;

import com.google.common.collect.ImmutableList;
import org.quiltmc.enigma.api.EnigmaProject;
import org.quiltmc.enigma.api.ProgressListener;
import org.quiltmc.enigma.api.EnigmaProject.DecompileErrorStrategy;
Expand All @@ -11,12 +12,23 @@
import java.nio.file.Path;
import java.util.Locale;

public class DecompileCommand extends Command {
public DecompileCommand() {
super(Argument.DECOMPILER.required(),
Argument.INPUT_JAR.required(),
Argument.OUTPUT_JAR.required(),
Argument.INPUT_MAPPINGS.optional());
public final class DecompileCommand extends Command {
private static final Argument DECOMPILER = new Argument("<decompiler>",
"""
The decompiler to use when producing output. Allowed values are (case-insensitive):
- VINEFLOWER
- CFR
- PROCYON
- BYTECODE"""
);

public static final DecompileCommand INSTANCE = new DecompileCommand();

private DecompileCommand() {
super(
ImmutableList.of(DECOMPILER, CommonArguments.INPUT_JAR, CommonArguments.OUTPUT_JAR),
ImmutableList.of(CommonArguments.INPUT_MAPPINGS)
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.quiltmc.enigma.command;

import com.google.common.collect.ImmutableList;
import org.quiltmc.enigma.api.EnigmaProject;
import org.quiltmc.enigma.api.ProgressListener;

import java.nio.file.Path;

public class DeobfuscateCommand extends Command {
public DeobfuscateCommand() {
super(Argument.INPUT_JAR.required(),
Argument.OUTPUT_JAR.required(),
Argument.INPUT_MAPPINGS.optional()
public final class DeobfuscateCommand extends Command {
public static final DeobfuscateCommand INSTANCE = new DeobfuscateCommand();

private DeobfuscateCommand() {
super(
ImmutableList.of(CommonArguments.INPUT_JAR, CommonArguments.OUTPUT_JAR),
ImmutableList.of(CommonArguments.INPUT_MAPPINGS)
);
}

Expand Down
Loading