From 54b476451c22c99992e388f8948c332e34cf9781 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sat, 7 Nov 2026 19:11:06 +0800 Subject: [PATCH] Feat: Add dark injector GUI - Replace the console selection with a FlatDarkLaf Swing window - List detected Minecraft windows, support a hidden custom PID input and refresh - Run injection on a background thread and report the result in the status label - Bundle FlatLaf into injector.jar so the packaged GUI runs standalone --- Injector/build.gradle | 9 + .../orangex/injector/InjectorWindow.java | 306 ++++++++++++++++++ .../cn/xiaozhou233/orangex/injector/Main.java | 167 +++++----- 3 files changed, 398 insertions(+), 84 deletions(-) create mode 100644 Injector/src/main/java/cn/xiaozhou233/orangex/injector/InjectorWindow.java diff --git a/Injector/build.gradle b/Injector/build.gradle index e66a553..5fea8b8 100644 --- a/Injector/build.gradle +++ b/Injector/build.gradle @@ -12,6 +12,9 @@ repositories { dependencies { testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation 'org.junit.jupiter:junit-jupiter' + + // Source: https://mvnrepository.com/artifact/com.formdev/flatlaf + implementation 'com.formdev:flatlaf:3.8' } test { @@ -24,6 +27,12 @@ tasks.named('jar') { manifest { attributes 'Main-Class': 'cn.xiaozhou233.orangex.injector.Main' } + // Bundle runtime dependencies (FlatLaf) so injector.jar is self-contained. + from { + configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } + } + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA' } tasks.withType(JavaCompile).configureEach { diff --git a/Injector/src/main/java/cn/xiaozhou233/orangex/injector/InjectorWindow.java b/Injector/src/main/java/cn/xiaozhou233/orangex/injector/InjectorWindow.java new file mode 100644 index 0000000..3933586 --- /dev/null +++ b/Injector/src/main/java/cn/xiaozhou233/orangex/injector/InjectorWindow.java @@ -0,0 +1,306 @@ +package cn.xiaozhou233.orangex.injector; + +import cn.xiaozhou233.juiceagent.injector.Injector; +import cn.xiaozhou233.juiceagent.injector.WindowInfo; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +/** + * Injector front-end: lists the running Minecraft windows, optionally accepts a + * PID typed by hand, and runs {@link Injector#inject(int, String, String)} on a + * background thread. + */ +public class InjectorWindow extends JFrame { + + private static final String[] WINDOW_TITLES = { + "Minecraft", "minecraft", "1.8.9", "Badlion", "Lunar" + }; + private static final String[] IGNORED_TITLES = { + "Badlion Client", "Badlion Chat", "Home - Lunar Client" + }; + + private static final Color ACCENT = new Color(0xFF8A00); + private static final Color SUCCESS = new Color(0x66BB6A); + private static final Color ERROR = new Color(0xEF5350); + private static final Color MUTED = new Color(0xAAAAAA); + + private final String agentPath; + private final String configDir; + + private final DefaultComboBoxModel processModel = new DefaultComboBoxModel<>(); + private final JComboBox processBox = new JComboBox<>(processModel); + private final JButton refreshButton = new JButton("Refresh"); + private final JCheckBox customPidCheck = new JCheckBox("Use custom PID"); + private final JTextField pidField = new JTextField(); + private final JButton injectButton = new JButton("Inject"); + private final JLabel statusLabel = new JLabel("Ready", SwingConstants.CENTER); + + private boolean busy; + + public InjectorWindow(String agentPath, String configDir) { + super("OrangeX Injector"); + this.agentPath = agentPath; + this.configDir = configDir; + + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setResizable(false); + setContentPane(buildContent()); + setSize(470, 340); + setLocationRelativeTo(null); + getRootPane().setDefaultButton(injectButton); + + refreshButton.addActionListener(event -> refreshProcesses()); + customPidCheck.addActionListener(event -> { + boolean custom = customPidCheck.isSelected(); + pidField.setVisible(custom); + revalidate(); + repaint(); + if (custom) { + pidField.requestFocusInWindow(); + } + updateControls(); + }); + injectButton.addActionListener(event -> inject()); + + refreshProcesses(); + } + + private JPanel buildContent() { + JPanel root = new JPanel(new GridBagLayout()); + root.setBorder(new EmptyBorder(20, 26, 16, 26)); + + GridBagConstraints gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.weightx = 1.0; + gbc.fill = GridBagConstraints.HORIZONTAL; + + int row = 0; + + JLabel title = new JLabel( + "OrangeX Injector", + SwingConstants.CENTER); + title.setFont(title.getFont().deriveFont(Font.BOLD, 26f)); + gbc.gridy = row++; + gbc.insets = new Insets(0, 0, 20, 0); + root.add(title, gbc); + + processBox.setPreferredSize(new Dimension(200, 34)); + JPanel processRow = new JPanel(new BorderLayout(8, 0)); + processRow.add(processBox, BorderLayout.CENTER); + processRow.add(refreshButton, BorderLayout.EAST); + gbc.gridy = row++; + gbc.insets = new Insets(0, 0, 10, 0); + root.add(processRow, gbc); + + gbc.gridy = row++; + gbc.insets = new Insets(0, 0, 8, 0); + root.add(customPidCheck, gbc); + + pidField.setPreferredSize(new Dimension(0, 34)); + pidField.putClientProperty("JTextField.placeholderText", "Enter a process ID"); + pidField.setVisible(false); + gbc.gridy = row++; + gbc.insets = new Insets(0, 0, 10, 0); + root.add(pidField, gbc); + + injectButton.setPreferredSize(new Dimension(0, 40)); + injectButton.setFont(injectButton.getFont().deriveFont(Font.BOLD, 15f)); + injectButton.setBackground(ACCENT); + injectButton.setForeground(Color.WHITE); + injectButton.putClientProperty("JButton.buttonType", "roundRect"); + gbc.gridy = row++; + gbc.insets = new Insets(10, 0, 12, 0); + root.add(injectButton, gbc); + + statusLabel.setFont(statusLabel.getFont().deriveFont(12f)); + statusLabel.setForeground(MUTED); + gbc.gridy = row++; + gbc.insets = new Insets(0, 0, 0, 0); + root.add(statusLabel, gbc); + + // Keeps the content aligned to the top when the PID field is hidden. + gbc.gridy = row; + gbc.weighty = 1.0; + root.add(Box.createVerticalGlue(), gbc); + + return root; + } + + /** + * Rescans the open windows and fills the selection box. Errors are shown + * in the status label instead of terminating the window. + */ + public void refreshProcesses() { + processModel.removeAllElements(); + + List processes; + try { + processes = scanProcesses(); + } catch (Throwable throwable) { + throwable.printStackTrace(); + setStatus("Failed to scan windows: " + throwable.getClass().getSimpleName(), ERROR); + updateControls(); + return; + } + + for (ProcessEntry process : processes) { + processModel.addElement(process); + } + + if (processes.isEmpty()) { + setStatus("No Minecraft process found", ERROR); + } else { + setStatus(processes.size() + " Minecraft process(es) found", MUTED); + } + updateControls(); + } + + private List scanProcesses() { + Map found = new LinkedHashMap<>(); + for (String title : WINDOW_TITLES) { + WindowInfo[] windows = Injector.findWindowsByTitle(title); + if (windows == null) { + continue; + } + for (WindowInfo window : windows) { + if (window == null || window.title == null || found.containsKey(window.pid)) { + continue; + } + if (isIgnored(window.title)) { + continue; + } + found.put(window.pid, new ProcessEntry(window.pid, window.title)); + } + } + return new ArrayList<>(found.values()); + } + + private static boolean isIgnored(String title) { + for (String ignored : IGNORED_TITLES) { + if (title.contains(ignored)) { + return true; + } + } + return false; + } + + private void inject() { + Integer pid = selectedPid(); + if (pid == null) { + return; + } + + busy = true; + updateControls(); + setStatus("Injecting into PID " + pid + "...", MUTED); + + new SwingWorker() { + @Override + protected Boolean doInBackground() { + return Injector.inject(pid, agentPath, configDir); + } + + @Override + protected void done() { + busy = false; + updateControls(); + try { + if (Boolean.TRUE.equals(get())) { + setStatus("Injection succeeded (PID " + pid + ")", SUCCESS); + } else { + setStatus("Injection failed (PID " + pid + ")", ERROR); + JOptionPane.showMessageDialog( + InjectorWindow.this, + "Injector.inject returned false for PID " + pid + ".", + "Injection failed", + JOptionPane.ERROR_MESSAGE); + } + } catch (InterruptedException exception) { + Thread.currentThread().interrupt(); + setStatus("Injection interrupted", ERROR); + } catch (ExecutionException exception) { + Throwable cause = exception.getCause() == null + ? exception : exception.getCause(); + cause.printStackTrace(); + setStatus("Injection failed (PID " + pid + ")", ERROR); + JOptionPane.showMessageDialog( + InjectorWindow.this, + cause.toString(), + "Injection failed", + JOptionPane.ERROR_MESSAGE); + } + } + }.execute(); + } + + private Integer selectedPid() { + if (customPidCheck.isSelected()) { + String text = pidField.getText().trim(); + if (text.isEmpty()) { + showError("Enter a PID first, or untick \"Use custom PID\"."); + return null; + } + try { + int pid = Integer.parseInt(text); + if (pid <= 0) { + throw new NumberFormatException("PID must be positive"); + } + return pid; + } catch (NumberFormatException exception) { + showError("PID must be a positive integer."); + return null; + } + } + + ProcessEntry selected = (ProcessEntry) processBox.getSelectedItem(); + if (selected == null) { + showError("No Minecraft process selected. Start the game or use a custom PID."); + return null; + } + return selected.pid; + } + + private void updateControls() { + boolean custom = customPidCheck.isSelected(); + boolean hasProcess = processModel.getSize() > 0; + + processBox.setEnabled(!busy && !custom && hasProcess); + refreshButton.setEnabled(!busy && !custom); + customPidCheck.setEnabled(!busy); + pidField.setEnabled(!busy); + injectButton.setEnabled(!busy && (custom || hasProcess)); + } + + private void setStatus(String text, Color color) { + statusLabel.setText(text); + statusLabel.setForeground(color); + } + + private void showError(String message) { + setStatus(message, ERROR); + JOptionPane.showMessageDialog( + this, message, "OrangeX Injector", JOptionPane.WARNING_MESSAGE); + } + + private static final class ProcessEntry { + private final int pid; + private final String title; + + private ProcessEntry(int pid, String title) { + this.pid = pid; + this.title = title; + } + + @Override + public String toString() { + return title + " (PID: " + pid + ")"; + } + } +} diff --git a/Injector/src/main/java/cn/xiaozhou233/orangex/injector/Main.java b/Injector/src/main/java/cn/xiaozhou233/orangex/injector/Main.java index a54b749..c9ec06d 100644 --- a/Injector/src/main/java/cn/xiaozhou233/orangex/injector/Main.java +++ b/Injector/src/main/java/cn/xiaozhou233/orangex/injector/Main.java @@ -1,105 +1,104 @@ package cn.xiaozhou233.orangex.injector; -import cn.xiaozhou233.juiceagent.injector.Injector; -import cn.xiaozhou233.juiceagent.injector.WindowInfo; +import com.formdev.flatlaf.FlatDarkLaf; -import java.io.*; +import javax.swing.JOptionPane; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import java.awt.Color; +import java.io.File; +import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Scanner; public class Main { - public static String orangexPath = new File(System.getProperty("user.home") + "/.orangex").getAbsolutePath(); - private static int pid = -1; + public static final String orangexPath = + new File(System.getProperty("user.home"), ".orangex").getAbsolutePath(); + public static final String agentPath = + new File(orangexPath, "libloader.dll").getAbsolutePath(); + public static final String configDir = orangexPath; - public static void main(String[] args) throws Exception { - System.setOut(new PrintStream( - new FileOutputStream(FileDescriptor.out), - true, - StandardCharsets.UTF_8.name() - )); - - System.setErr(new PrintStream( - new FileOutputStream(FileDescriptor.err), - true, - StandardCharsets.UTF_8.name() - )); + public static void main(String[] args) { + setupConsole(); System.out.println("================"); System.out.println("OrangeX Injector v1.0.0"); System.out.println("OrangeX Path: " + orangexPath); System.out.println("================"); - // Unzip injection + extractInjection(); + if (!loadNativeLibrary()) { + return; + } + + SwingUtilities.invokeLater(() -> { + installLookAndFeel(); + InjectorWindow window = new InjectorWindow(agentPath, configDir); + window.setVisible(true); + }); + } + + private static void setupConsole() { + try { + System.setOut(new PrintStream( + new FileOutputStream(FileDescriptor.out), + true, + StandardCharsets.UTF_8.name() + )); + System.setErr(new PrintStream( + new FileOutputStream(FileDescriptor.err), + true, + StandardCharsets.UTF_8.name() + )); + } catch (Exception exception) { + exception.printStackTrace(); + } + } + + private static void extractInjection() { File zipFile = new File("./injection.zip"); if (!zipFile.exists()) { System.err.println("injection.zip not found!"); + return; } + try (InputStream zipStream = new FileInputStream(zipFile)) { FileUtils.unzip(zipStream, new File(orangexPath)); - System.out.println("Done!"); - } catch (IOException e) { - e.printStackTrace(); + System.out.println("Extracted injection.zip to " + orangexPath); + } catch (IOException exception) { + System.err.println("[ERROR] Failed to extract injection.zip: " + exception.getMessage()); + exception.printStackTrace(); } - - // Load libinject native library - System.load(new File(orangexPath + "/libinject.dll").getAbsolutePath()); - System.out.println("Loaded libinject.dll"); - - // Find Minecraft - ArrayList pids = new ArrayList<>(); - ArrayList windowTitles = new ArrayList<>(); - try { - String[] titles = {"Minecraft", "minecraft", "1.8.9", "Badlion", "Lunar"}; - String[] ignoreTitles = {"Badlion Client", "Badlion Chat", "Home - Lunar Client"}; - for (String title : titles) { - WindowInfo[] list = Injector.findWindowsByTitle(title); - if (list == null || list.length == 0) { - } else { - for (WindowInfo w : list) { - if (pids.contains(w.pid)) - continue; - if (Arrays.stream(ignoreTitles).anyMatch(w.title::contains)) - continue; - pids.add(w.pid); - windowTitles.add(w.title); - } - } - } - } catch (UnsatisfiedLinkError ule) { - throw new RuntimeException("Native method findWindowsByTitle not available", ule); - } catch (Exception ex) { - System.out.println("[ERROR] Error while calling native findWindowsByTitle: " + ex.getMessage()); - ex.printStackTrace(); - } - - // Print results - System.out.println("======================="); - for (int i = 0; i < pids.size(); i++) { - System.out.printf("[ %d ] %s (PID: %d)%n", i + 1, windowTitles.get(i), pids.get(i)); - } - System.out.println("======================="); - if (pids.isEmpty()) { - System.err.println("[ERROR] No Minecraft process found!"); - } - - // Input the sequence number - try (Scanner scanner = new Scanner(System.in)) { - System.out.printf("[INPUT] Select (1-%d): ", pids.size()); - int index = scanner.nextInt(); - if (index < 1 || index > pids.size()) { - throw new RuntimeException("Invalid selection: " + index); - } - pid = pids.get(index - 1); - } catch (RuntimeException e) { - throw e; - } catch (Exception e) { - throw new RuntimeException("Invalid input", e); - } - - String agentPath = orangexPath + "/libloader.dll"; - String configDir = orangexPath; - Injector.inject(pid, agentPath, configDir); } -} \ No newline at end of file + + private static boolean loadNativeLibrary() { + try { + System.load(new File(orangexPath, "libinject.dll").getAbsolutePath()); + System.out.println("Loaded libinject.dll"); + return true; + } catch (Throwable throwable) { + throwable.printStackTrace(); + JOptionPane.showMessageDialog( + null, + "Failed to load libinject.dll:\n" + throwable, + "OrangeX Injector", + JOptionPane.ERROR_MESSAGE); + return false; + } + } + + private static void installLookAndFeel() { + try { + FlatDarkLaf.setup(); + UIManager.put("Component.accentColor", new Color(0xFF8A00)); + UIManager.put("Component.arc", 10); + UIManager.put("Button.arc", 12); + } catch (Throwable throwable) { + System.out.println("[WARN] FlatLaf is not available; using the default look and feel"); + } + } +}