Skip to content

Commit

Permalink
Use shutdown hook for ensuring mods are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
skycatminepokie committed Dec 13, 2024
1 parent c68d8ff commit 7a3041d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 64 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/skycatdev/binarysearchtool/CliUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public void initialize(SearchHandler searchHandler) {
public void onFinished(Mod problematicMod) {
assert searchHandler != null : "searchHandler should be the one calling, why is it null?";
System.out.printf("Finished! The problematic mod was: %s (%s)%n", problematicMod.name(), problematicMod.filename());
searchHandler.onUiClosing();
}

@Override
Expand Down
48 changes: 1 addition & 47 deletions src/main/java/com/skycatdev/binarysearchtool/SearchGui.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.skycatdev.binarysearchtool;

import com.skycatdev.binarysearchtool.advanced.OptionsPane;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
Expand All @@ -36,7 +33,7 @@ public class SearchGui extends JFrame implements SearchUi {
*/
public SearchGui() {
setTitle("Skycat's Fabric Binary Search Tool");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 500);
mainPanel = new JPanel();
mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
Expand Down Expand Up @@ -135,7 +132,6 @@ public SearchGui() {
startButton.addActionListener((event) -> start());
topPanel.add(startButton);

addWindowListener(createWindowListener());
setVisible(true);

Main.log("Initialized");
Expand Down Expand Up @@ -264,46 +260,4 @@ protected Void doInBackground() {
failureButton.setEnabled(false);
successButton.setEnabled(false);
}

private WindowListener createWindowListener() {
return new WindowListener() {
@Override
public void windowActivated(WindowEvent e) {

}

@Override
public void windowClosed(WindowEvent e) {

}

@Override
public void windowClosing(WindowEvent e) {
if (searchHandler != null) {
searchHandler.onUiClosing();
}
System.exit(0);
}

@Override
public void windowDeactivated(WindowEvent e) {

}

@Override
public void windowDeiconified(WindowEvent e) {

}

@Override
public void windowIconified(WindowEvent e) {

}

@Override
public void windowOpened(WindowEvent e) {

}
};
}
}
31 changes: 16 additions & 15 deletions src/main/java/com/skycatdev/binarysearchtool/SearchHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class SearchHandler {
private int maxIterations = 0;
private int iterations = 0;
private boolean finished = false;
private boolean madeShutdownHook = false;
private static final Option[] DO_NOTHING_OPTION = {new Option("OK", null)};

private SearchHandler(Path modsPath, SearchUi ui) {
Expand All @@ -64,9 +65,21 @@ public static SearchHandler createWithUi(Path inputPath, SearchUi ui) throws Ill
SearchHandler searchHandler = new SearchHandler(inputPath, ui);
searchHandler.discoverMods();
ui.initialize(searchHandler);
searchHandler.addShutdownHook();
return searchHandler;
}

private void addShutdownHook() {
if (!madeShutdownHook) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (!finished) {
mods.forEach((mod) -> mod.tryEnable(modsPath));
}
}));
madeShutdownHook = true;
}
}

private void addDeps(Mod mod) {
Main.log("Resolving dependencies");
// Add dependencies
Expand Down Expand Up @@ -147,9 +160,11 @@ public void bisect(boolean lastSuccessful) {
// Ready for next step
if (candidateMods.size() == 1) {
iterations++;
finished = true;
ui.updateLists(candidateMods, workingMods);
ui.updateProgress(iterations, maxIterations);
ui.onFinished(candidateMods.getFirst());
enableAll(mods);
return;
} else {
if (candidateMods.isEmpty()) {
Expand Down Expand Up @@ -265,7 +280,7 @@ private void enableMod(Mod mod) {
}
}

public boolean forceEnable(Mod mod) {
public boolean addForceEnabled(Mod mod) {
if (forceEnabled.contains(mod)) {
return false;
}
Expand All @@ -290,20 +305,6 @@ private void onFatalError() {
System.exit(1);
}

private void onFinished() {
mods.forEach(this::enableMod);
finished = true;
}

/**
* Used for making sure all mods are enabled before closing the program.
*/
public void onUiClosing() {
if (!finished) {
enableAll(mods);
}
}

private @Nullable Mod parseMod(JarFile jarFile) throws IOException {
Main.log("Parsing mod");
JarEntry fmj = jarFile.getJarEntry("fabric.mod.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ModsPanel(SearchHandler searchHandler) {
}

private void onForceEnablePressed(ActionEvent actionEvent) {
searchHandler.forceEnable(modSelectionBox.getItemAt(modSelectionBox.getSelectedIndex()));
searchHandler.addForceEnabled(modSelectionBox.getItemAt(modSelectionBox.getSelectedIndex()));
JOptionPane.showMessageDialog(this, "Mod force enabled!");
}
}

0 comments on commit 7a3041d

Please sign in to comment.