Skip to content

Commit

Permalink
Allow running some commands directly without using file inputs
Browse files Browse the repository at this point in the history
Also improve part of the enigma-cli code
  • Loading branch information
IotaBread committed Feb 26, 2023
1 parent 2cf7c98 commit aa05df2
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package cuchaz.enigma.command;

import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import org.tinylog.Logger;

Expand Down Expand Up @@ -38,19 +32,7 @@ public void run(String... args) throws Exception {
}

public static void run(Path fileJarIn, Path fileMappings) throws Exception {
Enigma enigma = Enigma.create();

Logger.info("Reading JAR...");

EnigmaProject project = enigma.openJar(fileJarIn, new ClasspathClassProvider(), ProgressListener.none());

Logger.info("Reading mappings...");

MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();

EntryTree<EntryMapping> mappings = readMappings(fileMappings, ProgressListener.none(), saveParameters);
project.setMappings(mappings);

EnigmaProject project = openProject(fileJarIn, fileMappings);
JarIndex idx = project.getJarIndex();

boolean error = false;
Expand Down
51 changes: 48 additions & 3 deletions enigma-cli/src/main/java/cuchaz/enigma/command/Command.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cuchaz.enigma.command;

import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProfile;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.api.EnigmaPlugin;
import cuchaz.enigma.classprovider.CachingClassProvider;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
import cuchaz.enigma.classprovider.JarClassProvider;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.MappingDelta;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
Expand All @@ -18,12 +23,15 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import com.google.common.io.MoreFiles;
import org.tinylog.Logger;

import javax.annotation.Nullable;

public abstract class Command {
public final String name;

Expand All @@ -37,12 +45,49 @@ protected Command(String name) {

public abstract void run(String... args) throws Exception;

public static JarIndex loadJar(Path jar) throws IOException {
Logger.info("Reading JAR...");
JarClassProvider classProvider = new JarClassProvider(jar);
JarIndex index = JarIndex.empty();
index.indexJar(classProvider.getClassNames(), new CachingClassProvider(classProvider), ProgressListener.none());

return index;
}

protected static Enigma createEnigma() {
return Enigma.create();
}

protected static Enigma createEnigma(EnigmaProfile profile) {
return createEnigma(profile, null);
}

protected static Enigma createEnigma(EnigmaProfile profile, @Nullable Iterable<EnigmaPlugin> plugins) {
Enigma.Builder builder = Enigma.builder().setProfile(profile);

if (plugins != null) {
builder.setPlugins(plugins);
}

return builder.build();
}

protected static EnigmaProject openProject(Path fileJarIn, Path fileMappings) throws Exception {
ProgressListener progress = new ConsoleProgressListener();
return openProject(fileJarIn, fileMappings, createEnigma());
}

Enigma enigma = Enigma.create();
protected static EnigmaProject openProject(Path fileJarIn, Path fileMappings, EnigmaProfile profile) throws Exception {
return openProject(fileJarIn, fileMappings, profile, null);
}

protected static EnigmaProject openProject(Path fileJarIn, Path fileMappings, EnigmaProfile profile, @Nullable Iterable<EnigmaPlugin> plugins) throws Exception {
return openProject(fileJarIn, fileMappings, createEnigma(profile, plugins));
}

protected static EnigmaProject openProject(Path fileJarIn, Path fileMappings, Enigma enigma) throws Exception {
ProgressListener progress = new ConsoleProgressListener();

Logger.info("Reading jar...");
Logger.info("Reading JAR...");
EnigmaProject project = enigma.openJar(fileJarIn, new ClasspathClassProvider(), progress);

if (fileMappings != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package cuchaz.enigma.command;

import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import org.tinylog.Logger;

import java.io.IOException;
Expand Down Expand Up @@ -47,17 +43,7 @@ public static void run(Path jarIn, Path mappingsIn, Path mappingsOut) throws Exc
return;
}

Enigma enigma = Enigma.create();

Logger.info("Reading JAR...");

EnigmaProject project = enigma.openJar(jarIn, new ClasspathClassProvider(), ProgressListener.none());

Logger.info("Reading mappings...");

MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
EntryTree<EntryMapping> mappings = readMappings(mappingsIn, ProgressListener.none(), saveParameters);
project.setMappings(mappings);
EnigmaProject project = openProject(jarIn, mappingsIn);

Logger.info("Dropping invalid mappings...");

Expand All @@ -84,6 +70,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
Files.deleteIfExists(mappingsIn);
}

MappingSaveParameters saveParameters = project.getEnigma().getProfile().getMappingSaveParameters();
writeMappings(project.getMapper().getObfToDeobf(), mappingsOut, ProgressListener.none(), saveParameters);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package cuchaz.enigma.command;

import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
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.tree.DeltaTrackingTree;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.mapping.tree.EntryTreeNode;
import cuchaz.enigma.translation.mapping.tree.HashEntryTree;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.ParentedEntry;
import cuchaz.enigma.utils.Utils;
import org.tinylog.Logger;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

Expand Down Expand Up @@ -47,38 +47,41 @@ public void run(String... args) throws Exception {

public static void run(Path jar, Path source, Path result, String resultFormat) throws Exception {
boolean debug = shouldDebug(NAME);

Logger.info("Reading JAR...");
Enigma enigma = Enigma.create();
EnigmaProject project = enigma.openJar(jar, new ClasspathClassProvider(), ProgressListener.none());
JarIndex jarIndex = loadJar(jar);

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

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

Logger.info("Writing mappings...");
Utils.delete(result);
MappingCommandsUtil.write(resultMappings, resultFormat, result, saveParameters);

if (debug) {
mappings = new DeltaTrackingTree<>(mappings);
writeDebugDelta((DeltaTrackingTree<EntryMapping>) resultMappings, result);
}
}

public static EntryTree<EntryMapping> run(JarIndex jarIndex, EntryTree<EntryMapping> source, boolean trackDelta) throws IOException {
EntryTree<EntryMapping> result = new HashEntryTree<>(source);

if (trackDelta) {
result = new DeltaTrackingTree<>(result);
}

Logger.info("Adding mappings...");
JarIndex index = project.getJarIndex();
List<ClassEntry> rootEntries = mappings.getRootNodes().map(EntryTreeNode::getEntry)
List<ClassEntry> rootEntries = source.getRootNodes().map(EntryTreeNode::getEntry)
.filter(entry -> entry instanceof ClassEntry)
.map(entry -> (ClassEntry) entry)
.toList();
for (ClassEntry rootEntry : rootEntries) {
// These entries already have a mapping tree node
recursiveAddMappings(mappings, index, rootEntry, false);
recursiveAddMappings(result, jarIndex, rootEntry, false);
}

Logger.info("Writing mappings...");
Utils.delete(result);
MappingCommandsUtil.write(mappings, resultFormat, result, saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) mappings, result);
}
return result;
}

private static void recursiveAddMappings(EntryTree<EntryMapping> mappings, JarIndex index, ClassEntry entry, boolean addMapping) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
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;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
import cuchaz.enigma.translation.ProposingTranslator;
import cuchaz.enigma.translation.Translator;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.EntryRemapper;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.DeltaTrackingTree;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.mapping.tree.HashEntryTree;
import cuchaz.enigma.translation.representation.TypeDescriptor;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.Entry;
Expand Down Expand Up @@ -56,35 +55,36 @@ public void run(String... args) throws Exception {
}

public static void run(Path inJar, Path source, Path output, String resultFormat, @Nullable Path profilePath, @Nullable Iterable<EnigmaPlugin> plugins) throws Exception {
boolean debug = shouldDebug(NAME);

EnigmaProfile profile = EnigmaProfile.read(profilePath);
Enigma.Builder builder = Enigma.builder().setProfile(profile);

if (plugins != null) {
builder.setPlugins(plugins);
}
Enigma enigma = createEnigma(profile, plugins);

Enigma enigma = builder.build();
run(inJar, source, output, resultFormat, enigma);
}

public static void run(Path inJar, Path source, Path output, String resultFormat, Enigma enigma) throws Exception {
boolean debug = shouldDebug(NAME);
NameProposalService[] nameProposalServices = enigma.getServices().get(NameProposalService.TYPE).toArray(new NameProposalService[0]);
if (nameProposalServices.length == 0) {
Logger.error("No name proposal service found");
return;
}

Logger.info("Reading JAR...");

EnigmaProject project = enigma.openJar(inJar, new ClasspathClassProvider(), ProgressListener.none());

Logger.info("Reading mappings...");
EnigmaProject project = openProject(inJar, source, enigma);
EntryTree<EntryMapping> mappings = run(nameProposalServices, project, debug);

Utils.delete(output);
MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();

EntryTree<EntryMapping> mappings = readMappings(source, ProgressListener.none(), saveParameters);
project.setMappings(mappings);
MappingCommandsUtil.write(mappings, resultFormat, output, saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) mappings, output);
}
}

public static EntryTree<EntryMapping> run(NameProposalService[] nameProposalServices, EnigmaProject project, boolean trackDelta) {
EntryTree<EntryMapping> mappings = new HashEntryTree<>(project.getMapper().getObfToDeobf());

if (trackDelta) {
mappings = new DeltaTrackingTree<>(mappings);
}

Expand Down Expand Up @@ -127,13 +127,7 @@ public static void run(Path inJar, Path source, Path output, String resultFormat
}

Logger.info("Proposed names for {} classes, {} fields, {} methods, {} parameters!", classes, fields, methods, parameters);

Utils.delete(output);
MappingCommandsUtil.write(mappings, resultFormat, output, saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) mappings, output);
}
return mappings;
}

private static <T extends Entry<?>> boolean insertMapping(T entry, EntryTree<EntryMapping> mappings, EntryRemapper mapper, Translator translator) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package cuchaz.enigma.command;

import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.index.BridgeMethodIndex;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.classprovider.CachingClassProvider;
import cuchaz.enigma.classprovider.JarClassProvider;
import cuchaz.enigma.translation.MappingTranslator;
import cuchaz.enigma.translation.Translator;
import cuchaz.enigma.translation.mapping.EntryMapping;
Expand Down Expand Up @@ -52,14 +49,23 @@ public void run(String... args) throws IOException, MappingParseException {

public static void run(Path jar, String sourceFormat, 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);
EntryTree<EntryMapping> result = new HashEntryTree<>();

JarClassProvider jcp = new JarClassProvider(jar);
JarIndex jarIndex = JarIndex.empty();
jarIndex.indexJar(jcp.getClassNames(), new CachingClassProvider(jcp), ProgressListener.none());
EntryTree<EntryMapping> result = run(jarIndex, source, debug);

Utils.delete(output);
MappingCommandsUtil.write(result, resultFormat, output, saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) result, output);
}
}

public static EntryTree<EntryMapping> run(JarIndex jarIndex, EntryTree<EntryMapping> source, boolean trackDelta) throws IOException, MappingParseException {
EntryTree<EntryMapping> result = new HashEntryTree<>();

BridgeMethodIndex bridgeMethodIndex = jarIndex.getBridgeMethodIndex();
Translator translator = new MappingTranslator(source, jarIndex.getEntryResolver());
Expand All @@ -71,7 +77,7 @@ public static void run(Path jar, String sourceFormat, Path sourcePath, String re
}
}

if (debug) {
if (trackDelta) {
result = new DeltaTrackingTree<>(result);
}

Expand All @@ -83,11 +89,6 @@ public static void run(Path jar, String sourceFormat, Path sourcePath, String re
result.insert(specialized, new EntryMapping(name));
}

Utils.delete(output);
MappingCommandsUtil.write(result, resultFormat, output, saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) result, output);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public final class MappingCommandsUtil {
private MappingCommandsUtil() {}

// TODO: Merge with methods in Command
public static EntryTree<EntryMapping> read(String type, Path path, MappingSaveParameters saveParameters) throws MappingParseException, IOException {
if (type.equals("enigma")) {
return (Files.isDirectory(path) ? EnigmaMappingsReader.DIRECTORY : EnigmaMappingsReader.ZIP).read(path, ProgressListener.none(), saveParameters);
Expand Down

0 comments on commit aa05df2

Please sign in to comment.