Skip to content

Commit

Permalink
add new "print stats" command
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Jan 14, 2024
1 parent e35cc47 commit 0875372
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Enigma

A tool for deobfuscation of Java bytecode. Forked from <https://bitbucket.org/cuchaz/enigma>, copyright Jeff Martin.
A tool for deobfuscation of Java bytecode. Forked from <https://bitbucket.org/cuchaz/enigma>, originally created by [Jeff Martin](https://www.cuchazinteractive.com/).

## License

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private static void logEnigmaInfo() {
register(new DropInvalidMappingsCommand());
register(new FillClassMappingsCommand());
register(new HelpCommand());
register(new PrintStatsCommand());
}

private static final class CommandHelpException extends IllegalArgumentException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.quiltmc.enigma.command;

import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.EnigmaPlugin;
import org.quiltmc.enigma.api.EnigmaProfile;
import org.quiltmc.enigma.api.stats.ProjectStatsResult;
import org.quiltmc.enigma.api.stats.StatType;
import org.quiltmc.enigma.api.stats.StatsGenerator;
import org.tinylog.Logger;

import javax.annotation.Nullable;
import java.nio.file.Path;
import java.util.Set;

public class PrintStatsCommand extends Command {
public PrintStatsCommand() {
super(Argument.INPUT_JAR.required(),
Argument.INPUT_MAPPINGS.required(),
Argument.ENIGMA_PROFILE.optional());
}

@Override
public void run(String... args) throws Exception {
Path inJar = getReadablePath(this.getArg(args, 0));
Path mappings = getReadablePath(this.getArg(args, 1));
Path profilePath = getReadablePath(this.getArg(args, 2));

run(inJar, mappings, profilePath, null);
}

@Override
public String getName() {
return "print-stats";
}

@Override
public String getDescription() {
return "Generates and prints out the statistics of how well the provided mappings cover the provided JAR file.";
}

public static void run(Path inJar, Path mappings, @Nullable Path profilePath, @Nullable Iterable<EnigmaPlugin> plugins) throws Exception {
EnigmaProfile profile = EnigmaProfile.read(profilePath);
Enigma enigma = createEnigma(profile, plugins);

run(inJar, mappings, enigma);
}

public static void run(Path inJar, Path mappings, Enigma enigma) throws Exception {
StatsGenerator generator = new StatsGenerator(openProject(inJar, mappings, enigma));
ProjectStatsResult result = generator.generate(new ConsoleProgressListener(), Set.of(StatType.values()), false);

Logger.info(String.format("Overall mapped: %.2f%% (%s / %s)", result.getPercentage(), result.getMapped(), result.getMappable()));
Logger.info(String.format("Classes: %.2f%% (%s / %s)", result.getPercentage(StatType.CLASSES), result.getMapped(StatType.CLASSES), result.getMappable(StatType.CLASSES)));
Logger.info(String.format("Fields: %.2f%% (%s / %s)", result.getPercentage(StatType.FIELDS), result.getMapped(StatType.FIELDS), result.getMappable(StatType.FIELDS)));
Logger.info(String.format("Methods: %.2f%% (%s / %s)", result.getPercentage(StatType.METHODS), result.getMapped(StatType.METHODS), result.getMappable(StatType.METHODS)));
Logger.info(String.format("Parameters: %.2f%% (%s / %s)", result.getPercentage(StatType.PARAMETERS), result.getMapped(StatType.PARAMETERS), result.getMappable(StatType.PARAMETERS)));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.quiltmc.enigma.command;

import org.junit.jupiter.api.Test;
import org.quiltmc.enigma.TestUtil;

import java.nio.file.Path;

import static org.quiltmc.enigma.TestUtil.getResource;

public class PrintStatsCommandTest {
private static final Path JAR = TestUtil.obfJar("lone_class");
private static final Path MAPPINGS = getResource("/print_stats");

@Test
public void test() throws Exception {
PrintStatsCommand.run(JAR, MAPPINGS, null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CLASS a AwesomeClass
FIELD a gaming Ljava/lang/String;
METHOD <init> (Ljava/lang/String;)V
ARG 1 gaming
METHOD a getGaming ()Ljava/lang/String;

0 comments on commit 0875372

Please sign in to comment.