Skip to content

Commit

Permalink
remove tiny v1 and improve commands to use automatic format detection (
Browse files Browse the repository at this point in the history
…#138)

* bye

* fix command descs

* Update enigma-cli/src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java

Co-authored-by: Iota <[email protected]>

---------

Co-authored-by: Iota <[email protected]>
  • Loading branch information
ix0rai and IotaBread authored Aug 17, 2023
1 parent b247c11 commit 48837c8
Show file tree
Hide file tree
Showing 28 changed files with 150 additions and 448 deletions.
8 changes: 3 additions & 5 deletions enigma-cli/src/main/java/cuchaz/enigma/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.MappingDelta;
import cuchaz.enigma.translation.mapping.serde.MappingParseException;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.tree.DeltaTrackingTree;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
Expand Down Expand Up @@ -89,18 +88,17 @@ public static EnigmaProject openProject(Path fileJarIn, Path fileMappings, Enigm
if (fileMappings != null) {
Logger.info("Reading mappings...");

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

project.setMappings(mappings);
}

return project;
}

protected static EntryTree<EntryMapping> readMappings(Path path, ProgressListener progress, MappingSaveParameters saveParameters) throws MappingParseException, IOException {
protected static EntryTree<EntryMapping> readMappings(Path path, ProgressListener progress) throws MappingParseException, IOException {
MappingFormat format = MappingFormat.parseFromFile(path);
return format.read(path, progress, saveParameters);
return format.read(path, progress);
}

protected static File getWritableFile(String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cuchaz.enigma.command;

import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.translation.mapping.MappingOperations;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingParseException;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFileNameFormat;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.MappingsWriter;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.utils.Utils;

Expand All @@ -18,35 +21,36 @@ public ComposeMappingsCommand() {

@Override
public String getUsage() {
return "<left-format> <left> <right-format> <right> <result-format> <result> <keep-mode>";
return "<left> <right> <result-format> <result> <keep-mode>";
}

@Override
public boolean isValidArgument(int length) {
return length == 7;
return length == 5;
}

@Override
public void run(String... args) throws IOException, MappingParseException {
String leftFormat = getArg(args, 0, "left-format", true);
Path left = getReadablePath(getArg(args, 1, "left", true));
String rightFormat = getArg(args, 2, "right-format", true);
Path right = getReadablePath(getArg(args, 3, "right", true));
String resultFormat = getArg(args, 4, "result-format", true);
Path result = getWritablePath(getArg(args, 5, "result", true));
String keepMode = getArg(args, 6, "keep-mode", true);

run(leftFormat, left, rightFormat, right, resultFormat, result, keepMode);
Path left = getReadablePath(getArg(args, 0, "left", true));
Path right = getReadablePath(getArg(args, 1, "right", true));
String resultFormat = getArg(args, 2, "result-format", true);
Path result = getWritablePath(getArg(args, 3, "result", true));
String keepMode = getArg(args, 4, "keep-mode", true);

run(left, right, resultFormat, result, keepMode);
}

public static void run(String leftFormat, Path leftFile, String rightFormat, Path rightFile, String resultFormat, Path resultFile, String keepMode) throws IOException, MappingParseException {
public static void run(Path leftFile, Path rightFile, String resultFormat, Path resultFile, String keepMode) throws IOException, MappingParseException {
MappingSaveParameters saveParameters = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF);

EntryTree<EntryMapping> left = MappingCommandsUtil.read(leftFormat, leftFile, saveParameters);
EntryTree<EntryMapping> right = MappingCommandsUtil.read(rightFormat, rightFile, saveParameters);
MappingFormat leftFormat = MappingFormat.parseFromFile(leftFile);
EntryTree<EntryMapping> left = leftFormat.read(leftFile);
MappingFormat rightFormat = MappingFormat.parseFromFile(rightFile);
EntryTree<EntryMapping> right = rightFormat.read(rightFile);
EntryTree<EntryMapping> result = MappingOperations.compose(left, right, keepMode.equals("left") || keepMode.equals("both"), keepMode.equals("right") || keepMode.equals("both"));

Utils.delete(resultFile);
MappingCommandsUtil.write(result, resultFormat, resultFile, saveParameters);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(result, resultFile, ProgressListener.none(), saveParameters);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cuchaz.enigma.command;

import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingParseException;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFileNameFormat;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.MappingsWriter;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.utils.Utils;

Expand All @@ -17,30 +20,31 @@ public ConvertMappingsCommand() {

@Override
public String getUsage() {
return "<source-format> <source> <result-format> <result>";
return "<source> <result-format> <result>";
}

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

@Override
public void run(String... args) throws IOException, MappingParseException {
String sourceFormat = getArg(args, 0, "source-format", true);
Path source = getReadablePath(getArg(args, 1, "source", true));
String resultFormat = getArg(args, 2, "result-format", true);
Path result = getWritablePath(getArg(args, 3, "result", true));
Path source = getReadablePath(getArg(args, 0, "source", true));
String resultFormat = getArg(args, 1, "result-format", true);
Path result = getWritablePath(getArg(args, 2, "result", true));

run(sourceFormat, source, resultFormat, result);
run(source, resultFormat, result);
}

public static void run(String sourceFormat, Path source, String resultFormat, Path output) throws MappingParseException, IOException {
public static void run(Path source, String resultFormat, Path output) throws MappingParseException, IOException {
MappingSaveParameters saveParameters = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF);

EntryTree<EntryMapping> mappings = MappingCommandsUtil.read(sourceFormat, source, saveParameters);
MappingFormat format = MappingFormat.parseFromFile(source);
EntryTree<EntryMapping> mappings = format.read(source);

Utils.delete(output);
MappingCommandsUtil.write(mappings, resultFormat, output, saveParameters);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(mappings, output, ProgressListener.none(), saveParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFileNameFormat;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.MappingsWriter;
import cuchaz.enigma.translation.mapping.tree.DeltaTrackingTree;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.mapping.tree.EntryTreeNode;
Expand Down Expand Up @@ -51,13 +52,14 @@ public static void run(Path jar, Path source, Path result, String resultFormat,

Logger.info("Reading mappings...");
MappingSaveParameters saveParameters = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF);
EntryTree<EntryMapping> sourceMappings = readMappings(source, ProgressListener.none(), saveParameters);
EntryTree<EntryMapping> sourceMappings = readMappings(source, ProgressListener.none());

EntryTree<EntryMapping> resultMappings = exec(jarIndex, sourceMappings, fillAll, debug);

Logger.info("Writing mappings...");
Utils.delete(result);
MappingCommandsUtil.write(resultMappings, resultFormat, result, saveParameters);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(resultMappings, result, ProgressListener.none(), saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) resultMappings, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProfile;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.EntryIndex;
import cuchaz.enigma.api.EnigmaPlugin;
import cuchaz.enigma.api.service.NameProposalService;
Expand All @@ -11,6 +12,7 @@
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.EntryRemapper;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.MappingsWriter;
import cuchaz.enigma.translation.mapping.tree.DeltaTrackingTree;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.mapping.tree.HashEntryTree;
Expand Down Expand Up @@ -74,7 +76,8 @@ public static void run(Path inJar, Path source, Path output, String resultFormat

Utils.delete(output);
MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
MappingCommandsUtil.write(mappings, resultFormat, output, saveParameters);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(mappings, output, ProgressListener.none(), saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) mappings, output);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cuchaz.enigma.command;

import cuchaz.enigma.translation.mapping.MappingOperations;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingParseException;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFileNameFormat;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.MappingsWriter;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.utils.Utils;

Expand All @@ -18,7 +20,7 @@ public InvertMappingsCommand() {

@Override
public String getUsage() {
return "<source-format> <source> <result-format> <result>";
return "<source> <result-format> <result>";
}

@Override
Expand All @@ -28,21 +30,22 @@ public boolean isValidArgument(int length) {

@Override
public void run(String... args) throws IOException, MappingParseException {
String sourceFormat = getArg(args, 0, "source-format", true);
Path source = getReadablePath(getArg(args, 1, "source", true));
String resultFormat = getArg(args, 2, "result-format", true);
Path result = getWritablePath(getArg(args, 3, "result", true));
Path source = getReadablePath(getArg(args, 0, "source", true));
String resultFormat = getArg(args, 1, "result-format", true);
Path result = getWritablePath(getArg(args, 2, "result", true));

run(sourceFormat, source, resultFormat, result);
run(source, resultFormat, result);
}

public static void run(String sourceFormat, Path sourceFile, String resultFormat, Path resultFile) throws MappingParseException, IOException {
public static void run(Path sourceFile, String resultFormat, Path resultFile) throws MappingParseException, IOException {
MappingSaveParameters saveParameters = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF);
MappingFormat format = MappingFormat.parseFromFile(sourceFile);

EntryTree<EntryMapping> source = MappingCommandsUtil.read(sourceFormat, sourceFile, saveParameters);
EntryTree<EntryMapping> source = format.read(sourceFile);
EntryTree<EntryMapping> result = MappingOperations.invert(source);

Utils.delete(resultFile);
MappingCommandsUtil.write(result, resultFormat, resultFile, saveParameters);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(result, resultFile, saveParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import cuchaz.enigma.translation.Translator;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFileNameFormat;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingParseException;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.serde.MappingsWriter;
import cuchaz.enigma.translation.mapping.tree.DeltaTrackingTree;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.mapping.tree.EntryTreeNode;
Expand All @@ -28,36 +30,37 @@ public MapSpecializedMethodsCommand() {

@Override
public String getUsage() {
return "<jar> <source-format> <source> <result-format> <result>";
return "<jar> <source> <result-format> <result>";
}

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

@Override
public void run(String... args) throws IOException, MappingParseException {
Path jar = getReadablePath(getArg(args, 0, "jar", true));
String sourceFormat = getArg(args, 1, "source-format", true);
Path source = getReadablePath(getArg(args, 2, "source", true));
String resultFormat = getArg(args, 3, "result-format", true);
Path result = getWritablePath(getArg(args, 4, "result", true));
Path source = getReadablePath(getArg(args, 1, "source", true));
String resultFormat = getArg(args, 2, "result-format", true);
Path result = getWritablePath(getArg(args, 3, "result", true));

run(jar, sourceFormat, source, resultFormat, result);
run(jar, source, resultFormat, result);
}

public static void run(Path jar, String sourceFormat, Path sourcePath, String resultFormat, Path output) throws IOException, MappingParseException {
public static void run(Path jar, Path sourcePath, String resultFormat, Path output) throws IOException, MappingParseException {
boolean debug = shouldDebug(NAME);
JarIndex jarIndex = loadJar(jar);

MappingSaveParameters saveParameters = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF);
EntryTree<EntryMapping> source = MappingCommandsUtil.read(sourceFormat, sourcePath, saveParameters);
MappingFormat sourceFormat = MappingFormat.parseFromFile(sourcePath);
EntryTree<EntryMapping> source = sourceFormat.read(sourcePath);

EntryTree<EntryMapping> result = run(jarIndex, source, debug);

Utils.delete(output);
MappingCommandsUtil.write(result, resultFormat, output, saveParameters);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(result, output, saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) result, output);
Expand Down
Loading

0 comments on commit 48837c8

Please sign in to comment.