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

improve status bar with stat generation progress #183

Merged
merged 6 commits into from
Mar 9, 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
14 changes: 7 additions & 7 deletions enigma-cli/src/main/java/org/quiltmc/enigma/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ 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());
index.indexJar(classProvider.getClassNames(), new CachingClassProvider(classProvider), ProgressListener.createEmpty());

return index;
}
Expand Down Expand Up @@ -274,29 +274,29 @@ protected static <T> void writeDebugDelta(DeltaTrackingTree<T> tree, Path output
Logger.debug("Wrote debug output to {}", debugOutput.toAbsolutePath());
}

public static class ConsoleProgressListener implements ProgressListener {
public static class ConsoleProgressListener extends ProgressListener {
private static final int REPORT_TIME = 5000; // 5s

private int totalWork;
private long startTime;
private long lastReportTime;

@Override
public void init(int totalWork, String title) {
this.totalWork = totalWork;
super.init(totalWork, title);
this.startTime = System.currentTimeMillis();
this.lastReportTime = this.startTime;
Logger.info(title);
}

@Override
public void step(int numDone, String message) {
public void step(int workDone, String message) {
super.step(workDone, message);
long now = System.currentTimeMillis();
boolean isLastUpdate = numDone == this.totalWork;
boolean isLastUpdate = workDone == this.totalWork;
boolean shouldReport = isLastUpdate || now - this.lastReportTime > REPORT_TIME;

if (shouldReport) {
int percent = numDone * 100 / this.totalWork;
int percent = workDone * 100 / this.totalWork;
Logger.info("\tProgress: {}%", percent);
this.lastReportTime = now;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public static void run(Path leftFile, Path rightFile, String resultFormat, Path

Utils.delete(resultFile);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(result, resultFile, ProgressListener.none(), saveParameters);
writer.write(result, resultFile, ProgressListener.createEmpty(), saveParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public static void run(Path source, String resultFormat, Path output) throws Map

Utils.delete(output);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(mappings, output, ProgressListener.none(), saveParameters);
writer.write(mappings, output, ProgressListener.createEmpty(), saveParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void run(Path jarIn, Path mappingsIn, Path mappingsOut) throws Exc

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

project.dropMappings(ProgressListener.none());
project.dropMappings(ProgressListener.createEmpty());

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

Expand All @@ -75,6 +75,6 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
}

MappingSaveParameters saveParameters = project.getEnigma().getProfile().getMappingSaveParameters();
format.write(project.getRemapper().getMappings(), mappingsOut, ProgressListener.none(), saveParameters);
format.write(project.getRemapper().getMappings(), mappingsOut, ProgressListener.createEmpty(), saveParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public static void run(Path jar, Path source, Path result, String resultFormat,

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

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

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

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) resultMappings, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void run(Path inJar, Path source, Path output, String resultFormat
Utils.delete(output);
MappingSaveParameters saveParameters = new MappingSaveParameters(enigma.getProfile().getMappingSaveParameters().fileNameFormat(), true);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(mappings, output, ProgressListener.none(), saveParameters);
writer.write(mappings, output, ProgressListener.createEmpty(), saveParameters);

if (debug) {
writeDebugDelta(mappings, output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void test() throws Exception {
Path resultFile = Files.createTempFile("mapSpecializedMethods", ".mappings");
MapSpecializedMethodsCommand.run(JAR, MAPPINGS, MappingFormat.ENIGMA_FILE.name(), resultFile);

EntryTree<EntryMapping> result = MappingFormat.ENIGMA_FILE.read(resultFile, ProgressListener.none());
EntryTree<EntryMapping> result = MappingFormat.ENIGMA_FILE.read(resultFile, ProgressListener.createEmpty());

assertNotNull(result.findNode(BASE_CLASS));
assertEquals("foo", getName(result, BASE_FOO_1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void main(String[] args) {
EnigmaProfile profile = EnigmaProfile.read(profileFile);
Enigma enigma = Enigma.builder().setProfile(profile).build();
Logger.info("Indexing Jar...");
EnigmaProject project = enigma.openJar(jar, new ClasspathClassProvider(), ProgressListener.none());
EnigmaProject project = enigma.openJar(jar, new ClasspathClassProvider(), ProgressListener.createEmpty());

MappingFormat mappingFormat = MappingFormat.parseFromFile(mappingsFile);
EntryRemapper mappings;
Expand Down Expand Up @@ -154,7 +154,7 @@ public synchronized void stop() {
}

private void saveMappings() {
this.mappingFormat.write(this.getRemapper().getMappings(), this.getRemapper().takeMappingDelta(), this.mappingsFile, ProgressListener.none(), this.profile.getMappingSaveParameters());
this.mappingFormat.write(this.getRemapper().getMappings(), this.getRemapper().takeMappingDelta(), this.mappingsFile, ProgressListener.createEmpty(), this.profile.getMappingSaveParameters());
this.log.flush();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class NetworkTest {
@BeforeAll
public static void startServer() throws IOException {
Enigma enigma = Enigma.create();
EnigmaProject project = enigma.openJar(JAR, new ClasspathClassProvider(), ProgressListener.none());
EnigmaProject project = enigma.openJar(JAR, new ClasspathClassProvider(), ProgressListener.createEmpty());

checksum = Utils.zipSha1(JAR);
remapper = project.getRemapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.quiltmc.enigma.gui;

import org.quiltmc.enigma.api.stats.StatType;
import org.quiltmc.enigma.api.translation.representation.entry.ClassEntry;
import org.quiltmc.enigma.api.translation.representation.entry.Entry;
import org.quiltmc.enigma.api.translation.representation.entry.FieldEntry;
import org.quiltmc.enigma.api.translation.representation.entry.LocalVariableEntry;
import org.quiltmc.enigma.api.translation.representation.entry.MethodEntry;

import javax.annotation.Nullable;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public enum EditableType {
CLASS,
Expand Down Expand Up @@ -43,4 +47,19 @@ public static EditableType fromEntry(Entry<?> entry) {

return type;
}

@Nullable
public StatType toStatType() {
return switch (this) {
case CLASS -> StatType.CLASSES;
case METHOD -> StatType.METHODS;
case FIELD -> StatType.FIELDS;
case PARAMETER -> StatType.PARAMETERS;
default -> null;
};
}

public static Set<StatType> toStatTypes(Set<EditableType> editables) {
return editables.stream().map(EditableType::toStatType).filter(Objects::nonNull).collect(Collectors.toSet());
}
}
6 changes: 5 additions & 1 deletion enigma-swing/src/main/java/org/quiltmc/enigma/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ public List<Throwable> getCrashHistory() {
return this.crashHistory;
}

public Set<EditableType> getEditableTypes() {
return this.editableTypes;
}

public void addCrash(Throwable t) {
this.crashHistory.add(t);
this.menuBar.prepareCrashHistoryMenu();
Expand Down Expand Up @@ -659,7 +663,7 @@ public boolean isOffline() {
*/
public void updateUiState() {
this.menuBar.updateUiState();
this.connectionStatusLabel.setText(I18n.translate(this.connectionState == ConnectionState.NOT_CONNECTED ? "status.disconnected" : "status.connected"));
this.connectionStatusLabel.setText(I18n.translate("status.prefix") + I18n.translate(this.connectionState == ConnectionState.NOT_CONNECTED ? "status.disconnected" : "status.connected"));
}

public void retranslateUi() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.quiltmc.enigma.api.analysis.tree.ClassReferenceTreeNode;
import org.quiltmc.enigma.api.analysis.EntryReference;
import org.quiltmc.enigma.api.analysis.tree.FieldReferenceTreeNode;
import org.quiltmc.enigma.gui.dialog.CrashDialog;
import org.quiltmc.enigma.gui.network.IntegratedEnigmaClient;
import org.quiltmc.enigma.impl.analysis.IndexTreeBuilder;
import org.quiltmc.enigma.api.analysis.tree.MethodImplementationsTreeNode;
Expand Down Expand Up @@ -167,8 +168,15 @@ public CompletableFuture<Void> openMappings(MappingFormat format, Path path) {
this.refreshClasses();
this.chp.invalidateJavadoc();
this.statsGenerator = new StatsGenerator(this.project);
new Thread(() -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already inside another secondary thread, and I feel like we have a better way to spawn threads instead of new Thread()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it executor time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what the best way of doing this would be, this is kinda a bodge that just got left in because I don't know what's better

ProgressListener progressListener = ProgressListener.createEmpty();
this.gui.getMainWindow().getStatusBar().syncWith(progressListener);
this.statsGenerator.generate(progressListener, EditableType.toStatTypes(this.gui.getEditableTypes()), false);
}).start();
} catch (MappingParseException e) {
JOptionPane.showMessageDialog(this.gui.getFrame(), e.getMessage());
} catch (Exception e) {
CrashDialog.show(e);
}
});
}
Expand Down Expand Up @@ -227,7 +235,7 @@ public CompletableFuture<Void> saveMappings(Path path, MappingFormat format) {
public void closeMappings() {
if (this.project == null) return;

this.project.setMappings(null, ProgressListener.none());
this.project.setMappings(null, ProgressListener.createEmpty());

this.gui.setMappingsFile(null);
this.refreshClasses();
Expand Down Expand Up @@ -556,7 +564,7 @@ private void applyChange0(ValidationContext vc, EntryChange<?> change, boolean u

public void openStatsTree(Set<StatType> includedTypes) {
ProgressDialog.runOffThread(this.gui, progress -> {
StatsResult overall = this.getStatsGenerator().getResultNullable().getOverall();
StatsResult overall = this.getStatsGenerator().getResult(EditableType.toStatTypes(this.gui.getEditableTypes()), false).getOverall();
StatsTree<Integer> tree = overall.buildTree(Config.main().stats.lastTopLevelPackage.value(), includedTypes);
String treeJson = GSON.toJson(tree.root);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.quiltmc.enigma.gui.Gui;
import org.quiltmc.enigma.gui.util.GridBagConstraintsBuilder;
import org.quiltmc.enigma.gui.util.GuiUtil;
import org.quiltmc.enigma.gui.util.ProgressBar;
import org.quiltmc.enigma.gui.util.ScaleUtil;
import org.quiltmc.enigma.util.I18n;

Expand All @@ -16,17 +17,18 @@
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ProgressDialog implements ProgressListener, AutoCloseable {
public class ProgressDialog extends ProgressBar implements AutoCloseable {
private final JDialog dialog;
private final JLabel labelTitle = new JLabel();
private final JLabel labelText = GuiUtil.unboldLabel(new JLabel());
private final JProgressBar progress = new JProgressBar();

public ProgressDialog(JFrame parent) {
JLabel labelTitle = new JLabel();
this.addTitleSetListener(labelTitle::setText);
JLabel labelText = GuiUtil.unboldLabel(new JLabel());
this.addMessageUpdateListener((message, done) -> labelText.setText(message));

// init frame
this.dialog = new JDialog(parent, String.format(I18n.translate("progress.operation"), Enigma.NAME));
Container pane = this.dialog.getContentPane();
Expand All @@ -38,15 +40,15 @@ public ProgressDialog(JFrame parent) {
.fill(GridBagConstraints.BOTH)
.weight(1.0, 0.0);

pane.add(this.labelTitle, cb.pos(0, 0).build());
pane.add(this.labelText, cb.pos(0, 1).build());
pane.add(this.progress, cb.pos(0, 2).weight(1.0, 1.0).build());
pane.add(labelTitle, cb.pos(0, 0).build());
pane.add(labelText, cb.pos(0, 1).build());
pane.add(this.delegate, cb.pos(0, 2).weight(1.0, 1.0).build());

// Set label text since otherwise the label height is 0, which makes the
// window size get set incorrectly
this.labelTitle.setText("Idle");
this.labelText.setText("Idle");
this.progress.setPreferredSize(ScaleUtil.getDimension(0, 20));
labelTitle.setText("Idle");
labelText.setText("Idle");
this.delegate.setPreferredSize(ScaleUtil.getDimension(0, 20));

// show the frame
this.dialog.setResizable(false);
Expand Down Expand Up @@ -101,29 +103,6 @@ public void close() {
SwingUtilities.invokeLater(this.dialog::dispose);
}

@Override
public void init(int totalWork, String title) {
SwingUtilities.invokeLater(() -> {
this.labelTitle.setText(title);
this.progress.setMinimum(0);
this.progress.setMaximum(totalWork);
this.progress.setValue(0);
});
}

@Override
public void step(int numDone, String message) {
SwingUtilities.invokeLater(() -> {
this.labelText.setText(message);
if (numDone != -1) {
this.progress.setValue(numDone);
this.progress.setIndeterminate(false);
} else {
this.progress.setIndeterminate(true);
}
});
}

public interface ProgressRunnable {
void run(ProgressListener listener) throws Exception;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.quiltmc.enigma.gui.dialog;

import org.quiltmc.enigma.api.stats.StatsGenerator;
import org.quiltmc.enigma.gui.EditableType;
import org.quiltmc.enigma.gui.Gui;
import org.quiltmc.enigma.gui.config.Config;
import org.quiltmc.enigma.api.stats.ProjectStatsResult;
Expand All @@ -26,10 +28,22 @@

public class StatsDialog {
public static void show(Gui gui) {
ProgressDialog.runOffThread(gui, listener -> {
ProjectStatsResult result = gui.getController().getStatsGenerator().getResult(false);
SwingUtilities.invokeLater(() -> show(gui, result, ""));
});
StatsGenerator generator = gui.getController().getStatsGenerator();
ProjectStatsResult nullableResult = generator.getResultNullable();

if (nullableResult == null) {
ProgressDialog.runOffThread(gui, listener -> {
// hook into current stat generation progress
if (generator.getOverallProgress() != null) {
listener.sync(generator.getOverallProgress());
}

ProjectStatsResult result = gui.getController().getStatsGenerator().getResult(EditableType.toStatTypes(gui.getEditableTypes()), false);
SwingUtilities.invokeLater(() -> show(gui, result, ""));
});
} else {
SwingUtilities.invokeLater(() -> show(gui, nullableResult, ""));
}
}

public static void show(Gui gui, ProjectStatsResult result, String packageName) {
Expand Down Expand Up @@ -83,7 +97,7 @@ public static void show(Gui gui, ProjectStatsResult result, String packageName)
ProgressDialog.runOffThread(gui, listener -> {
Config.main().stats.lastTopLevelPackage.setValue(topLevelPackage.getText(), true);

ProjectStatsResult projectResult = gui.getController().getStatsGenerator().getResult(syntheticParametersOption.isSelected()).filter(Config.main().stats.lastTopLevelPackage.value());
ProjectStatsResult projectResult = gui.getController().getStatsGenerator().getResult(EditableType.toStatTypes(gui.getEditableTypes()), syntheticParametersOption.isSelected()).filter(Config.main().stats.lastTopLevelPackage.value());
SwingUtilities.invokeLater(() -> show(gui, projectResult, Config.main().stats.lastTopLevelPackage.value()));
});
});
Expand Down
Loading
Loading