Skip to content

Commit

Permalink
fix: gui bugs and qol
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed Dec 29, 2024
1 parent e210d25 commit e9bce19
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.skidfuscator.obfuscator;

import com.formdev.flatlaf.intellijthemes.FlatDarkPurpleIJTheme;
import com.formdev.flatlaf.intellijthemes.FlatGradiantoMidnightBlueIJTheme;
import dev.skidfuscator.obfuscator.command.HelpCommand;
import dev.skidfuscator.obfuscator.command.MappingsCommand;
import dev.skidfuscator.obfuscator.command.ObfuscateCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.awt.*;
import java.io.File;

public class ActionPanel extends JPanel {
public class ActionPanel extends JPanel implements SkidPanel{
private final MainFrame mainFrame;
private final JTextArea logArea;
private final JButton startButton;
Expand Down Expand Up @@ -40,6 +40,8 @@ public void startObfuscation() {
return;
}



// Create session
SkidfuscatorSession session = SkidfuscatorSession.builder()
.input(new File(config.getInputPath()))
Expand All @@ -48,10 +50,7 @@ public void startObfuscation() {
? new File[0]
: new File(config.getLibsPath()).listFiles()
)
.runtime(config.getRuntimePath().isEmpty()
? null
: new File(config.getRuntimePath())
)
.runtime(null)
.phantom(false)
.debug(config.isDebugEnabled())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@
import dev.skidfuscator.obfuscator.gui.autosave.AutoSaveDocumentListener;
import dev.skidfuscator.obfuscator.gui.config.SkidfuscatorConfig;
import dev.skidfuscator.obfuscator.util.JdkDownloader;
import dev.skidfuscator.obfuscator.util.Observable;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;

public class ConfigPanel extends JPanel {
public class ConfigPanel extends JPanel implements SkidPanel{
private final JTextField inputField;
private final JTextField outputField;
private final JTextField libsField;
private final JTextField runtimeField;
private final JCheckBox debugBox;
private final SkidfuscatorConfig config;
private Observable<Boolean> runtimeInstalled = new Observable.SimpleObservable<>(
JdkDownloader.isJdkDownloaded()
);

public ConfigPanel() {
setLayout(new GridBagLayout());
Expand Down Expand Up @@ -116,7 +112,9 @@ private void updateCheck() {
boolean valid = new File(inputField.getText()).exists();
inputCheck.setText(valid ? "✓" : "✗");
inputCheck.setForeground(valid ? new Color(46, 204, 64) : new Color(255, 65, 54));

System.out.println("Input valid: " + valid);
config.getValidInput().set(valid);

if (!valid) {
StringBuilder tooltip = new StringBuilder("<html><body style='width: 250px; padding: 3px; background-color: #FFF3CD; border: 2px solid #FFE69C; border-radius: 4px'>");
tooltip.append("<div style='color: #856404; font-weight: bold; margin-bottom: 5px'>⚠ Warning: Invalid Input Configuration</div>");
Expand Down Expand Up @@ -171,7 +169,10 @@ private void updateCheck() {

boolean valid = parent != null && parent.exists() && validEnd && validInput;
outputCheck.setText(valid ? "✓" : "✗");
outputCheck.setForeground(valid ? new Color(46, 204, 64) : new Color(255, 65, 54));
outputCheck.setForeground(valid
? new Color(46, 204, 64)
: new Color(255, 65, 54)
);
// Set tooltip explaining validation failure
StringBuilder tooltip = new StringBuilder("<html><body style='width: 250px; padding: 3px; background-color: #FFF3CD; border: 2px solid #FFE69C; border-radius: 4px'>");
tooltip.append("<div style='color: #856404; font-weight: bold; margin-bottom: 5px'>⚠ Warning: Invalid Output Configuration</div>");
Expand All @@ -186,14 +187,15 @@ private void updateCheck() {
tooltip.append("<div style='color: #664D03; margin: 3px 0'>Output file cannot be the same as input file</div>");
}
tooltip.append("</body></html>");
config.getValidOutput().set(validInput);

if (!valid) {
ToolTipManager.sharedInstance().setInitialDelay(0);
ToolTipManager.sharedInstance().setDismissDelay(10000);

outputField.setToolTipText(tooltip.toString());
} else {
outputCheck.setToolTipText(null);
outputField.setToolTipText(null);
}
}
public void insertUpdate(DocumentEvent e) { updateCheck(); }
Expand All @@ -204,9 +206,15 @@ private void updateCheck() {
if (config.getLastOutputPath() != null) {
outputField.setText(config.getLastOutputPath());
outputListener.insertUpdate(null);
} else if (config.getLastInputPath() != null) {
outputField.setText(config.getLastInputPath().replace(".jar", "-obf.jar"));
} else if (config.getLastInputPath() != null || inputField.getText() != null) {
final String input = config.getLastInputPath() != null
? config.getLastInputPath()
: inputField.getText();

outputField.setText(input.replace(".jar", "-obf.jar"));
outputListener.insertUpdate(null);
} else {
config.getValidOutput().set(false);
}
add(outputField, gbc);
gbc.gridx = 2;
Expand Down Expand Up @@ -271,12 +279,14 @@ private void updateCheck() {
String jmodPath = JdkDownloader.getCachedJmodPath();
runtimeField.setText(jmodPath);
runtimeField.setEnabled(!JdkDownloader.isJdkDownloaded());
runtimeInstalled.set(JdkDownloader.isJdkDownloaded());
} catch (IOException e) {
// Fallback to config
if (config.getLastRuntimePath() != null) {
if (config.getLastRuntimePath().isEmpty()) {
runtimeField.setText(Jvm.getLibsPath());
runtimeField.setEnabled(false);
runtimeInstalled.set(true);
} else {
runtimeField.setText(config.getLastRuntimePath());
}
Expand All @@ -288,12 +298,19 @@ private void updateCheck() {
// Add download button next to browse button
gbc.gridx = 2;
JPanel runtimeButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
JButton downloadButton = new JButton("Download JDK");

JLabel downloadCheck = new JLabel("✗");
downloadCheck.setForeground(new Color(255, 65, 54));

JButton downloadButton = new JButton("Install");
runtimeButtonPanel.setPreferredSize(new Dimension(150, 30));

// Set initial button state based on JDK download status
if (JdkDownloader.isJdkDownloaded()) {
downloadButton.setText("Downloaded");
downloadButton.setText("Installed");
downloadButton.setEnabled(false);
downloadCheck.setText("✓");
downloadCheck.setForeground(new Color(46, 204, 64));
runtimeInstalled.set(true);
}

downloadButton.addActionListener(e -> {
Expand All @@ -312,23 +329,32 @@ protected void done() {
String path = get();
runtimeField.setText(path);
runtimeField.setEnabled(false);
downloadButton.setText("Downloaded");
downloadButton.setText("Installed");
downloadButton.setEnabled(false);
downloadCheck.setText("✓");
downloadCheck.setForeground(new Color(46, 204, 64));
runtimeInstalled.set(true);

} catch (Exception ex) {
JOptionPane.showMessageDialog(
ConfigPanel.this,
"Failed to download JDK: " + ex.getMessage(),
"Download Error",
JOptionPane.ERROR_MESSAGE
);
downloadButton.setText("Download JDK");
downloadButton.setText("Install");
downloadButton.setEnabled(true);
downloadCheck.setText("✗");
downloadCheck.setForeground(new Color(255, 65, 54));
runtimeInstalled.set(false);
}
}
};
worker.execute();
});
runtimeButtonPanel.add(downloadButton);
runtimeButtonPanel.add(Box.createHorizontalGlue());
runtimeButtonPanel.add(downloadCheck);

// removing for now
//runtimeButtonPanel.add(createBrowseButton(runtimeField, false));
Expand Down Expand Up @@ -413,5 +439,13 @@ public String getLibraryPath() {
// TODO: Add a library path field to the config panel
return null;
}

public Observable<Boolean> getRuntimeInstalled() {
return runtimeInstalled;
}

public SkidfuscatorConfig getConfig() {
return config;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConsolePanel extends JPanel {
public class ConsolePanel extends JPanel implements SkidPanel {
private final JTextPane consoleOutput;
private final SimpleDateFormat timeFormat;
private final StyledDocument doc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.Timer;

public class LibrariesPanel extends JPanel {
public class LibrariesPanel extends JPanel implements SkidPanel {
private final JList<String> libraryList;
private final DefaultListModel<String> libraryModel;
private final JList<String> missingClassesList;
Expand Down Expand Up @@ -179,6 +179,9 @@ public LibrariesPanel(ConfigPanel configPanel, SkidApplicationClassSource classS
add(bottomPanel, BorderLayout.SOUTH);

// Analyze the input jar if specified in config
}

public void open() {
SwingUtilities.invokeLater(this::analyzeConfigJar);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.formdev.flatlaf.ui.FlatTabbedPaneUI;
import dev.skidfuscator.obfuscator.Skidfuscator;
import dev.skidfuscator.obfuscator.SkidfuscatorSession;
import dev.skidfuscator.obfuscator.util.JdkDownloader;
import dev.skidfuscator.obfuscator.util.Observable;
import lombok.Getter;

import javax.imageio.ImageIO;
Expand Down Expand Up @@ -96,7 +98,7 @@ protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
g.drawString("Skidfuscator Community", 20, 175);
g.setColor(new Color(130, 130, 130));
g.setFont(new Font("Segoe UI", Font.ITALIC, 11));
g.drawString("Build: 2023.1", 20, 190);
g.drawString("Build: " + Skidfuscator.VERSION, 20, 190);

// Draw second separator
g.setColor(Color.DARK_GRAY);
Expand Down Expand Up @@ -228,22 +230,37 @@ public void mouseClicked(MouseEvent e) {
contentPanel.removeAll();
switch (tabbedPane.getSelectedIndex()) {
case 0:
configPanel.open();
contentPanel.add(configPanel, BorderLayout.CENTER);
break;
case 1:
if (!JdkDownloader.isJdkDownloaded()) {
JOptionPane.showMessageDialog(this, "Please download the JDK first", "Error", JOptionPane.ERROR_MESSAGE);
tabbedPane.setSelectedIndex(0);
return;
}
librariesPanel.open();
contentPanel.add(librariesPanel, BorderLayout.CENTER);
break;
case 2:
contentPanel.add(transformerPanel, BorderLayout.CENTER);
break;
case 3:
consolePanel.open();
contentPanel.add(consolePanel, BorderLayout.CENTER);
break;
}
contentPanel.revalidate();
contentPanel.repaint();
});


// Observe input/output and adapt button to it
configPanel.getConfig().getValidInput().addObserver(value -> refreshStartButton());
configPanel.getConfig().getValidOutput().addObserver(value -> refreshStartButton());
configPanel.getRuntimeInstalled().addObserver(value -> refreshStartButton());
refreshStartButton();

// Final setup
pack();
setLocationRelativeTo(null);
Expand Down Expand Up @@ -309,6 +326,23 @@ public void updateTabStates(boolean configEnabled, boolean transformersEnabled)
tabbedPane.setEnabledAt(1, transformersEnabled);
}

private void refreshStartButton() {
System.out.println("Refreshing start button");

if (!configPanel.getConfig().isValid() || !configPanel.getRuntimeInstalled().get()) {
// Set warning icon
final Image warningIcon = new ImageIcon(getClass().getResource("/images/warning.png")).getImage();
final Image warningImage = warningIcon.getScaledInstance(16, 16, Image.SCALE_SMOOTH);
final ImageIcon warningIconScaled = new ImageIcon(warningImage);
startButton.setIcon(warningIconScaled);
startButton.setEnabled(false);
} else {
startButton.setToolTipText(null);
startButton.setIcon(null);
startButton.setEnabled(true);
}
}

public void startObfuscation() {
ConfigPanel config = this.getConfigPanel();
TransformerPanel transformers = this.getTransformerPanel();
Expand Down Expand Up @@ -347,15 +381,17 @@ public void startObfuscation() {
? libraryFolder.toFile().listFiles()
: new File(config.getLibsPath()).listFiles()
)
.runtime(config.getRuntimePath().isEmpty()
? null
: new File(config.getRuntimePath())
)
//.runtime(config.getRuntimePath().isEmpty()
// ? null
// : new File(config.getRuntimePath())
//)
.jmod(config.getRuntimePath().contains("jmods"))
.debug(config.isDebugEnabled())
.build();
// Start obfuscation in background
startButton.setEnabled(false);


SwingWorker<Void, String> worker = new SwingWorker() {
@Override
protected Void doInBackground() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.skidfuscator.obfuscator.gui;

public interface SkidPanel {
default void open() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import dev.skidfuscator.obfuscator.util.Observable;
import lombok.Data;

import java.io.*;
import java.nio.file.*;
import java.util.Properties;

@Data
public class SkidfuscatorConfig {
Expand All @@ -22,6 +21,14 @@ public class SkidfuscatorConfig {
private boolean phantomEnabled;
private String lastDirectory;

private transient Observable<Boolean>
validInput = new Observable.SimpleObservable<>(false),
validOutput = new Observable.SimpleObservable<>(false);

public boolean isValid() {
return validInput.get() && validOutput.get();
}

public static class Builder {
private final SkidfuscatorConfig config;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.skidfuscator.obfuscator.util;

import dev.skidfuscator.obfuscator.Skidfuscator;
import lombok.experimental.UtilityClass;

import java.text.DateFormat;
Expand Down Expand Up @@ -61,7 +62,7 @@ public static void printLogo() {
" │ " + topMemory + " │",
" └───────────────────────────────────────────┘",
"",
" Author: Ghast Version: 2.0.11 Today: "
" Author: Ghast Version: " + Skidfuscator.VERSION + " Today: "
+ DateFormat.getDateTimeInstance().format(new Date(Instant.now().toEpochMilli())),
""
};
Expand Down
Loading

0 comments on commit e9bce19

Please sign in to comment.