Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new "print stats" command #176

Merged
merged 2 commits into from
Jan 17, 2024
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
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,19 @@
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 {
// just here to manually verify output
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;