Skip to content

Commit

Permalink
Add "Add libraries from directory" button
Browse files Browse the repository at this point in the history
  • Loading branch information
vimasig committed Sep 11, 2021
1 parent da08306 commit c887882
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.vimasig.bozar.obfuscator.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileUtils {

public static List<File> getAllFiles(File file) {
if(file.isFile()) return List.of(file);

File[] files = file.listFiles();
if(files == null) return List.of();

final var fileList = new ArrayList<File>();
Arrays.stream(files).forEach(f -> {
if(f.isFile()) fileList.add(f);
else fileList.addAll(getAllFiles(f));
});
return fileList;
}
}
36 changes: 30 additions & 6 deletions src/main/java/io/github/vimasig/bozar/ui/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import io.github.vimasig.bozar.obfuscator.transformer.ClassTransformer;
import io.github.vimasig.bozar.obfuscator.transformer.TransformManager;
import io.github.vimasig.bozar.obfuscator.utils.BozarUtils;
import io.github.vimasig.bozar.obfuscator.utils.FileUtils;
import io.github.vimasig.bozar.obfuscator.utils.model.BozarCategory;
import io.github.vimasig.bozar.obfuscator.utils.model.BozarConfig;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
Expand All @@ -17,14 +19,18 @@
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Window;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Controller {

Expand All @@ -34,7 +40,8 @@ public class Controller {
@FXML private Button browseOutput;
@FXML private ListView<String> console;
@FXML private Button buttonObf;
@FXML private Button buttonAddLib;
@FXML private Button buttonAddJAR;
@FXML private Button buttonAddDir;
@FXML private Button buttonRemoveLib;
@FXML private TabPane optionsTab;

Expand Down Expand Up @@ -167,19 +174,20 @@ public void initialize() {
// Example usage of exclude
exclude.setPromptText("com.example.myapp.MyClass\r\ncom.example.myapp.MyClass.myField\r\ncom.example.myapp.MyClass.myMethod()\r\ncom.example.mypackage.**\r\nFieldRenamerTransformer:com.example.MyClass");

var jarFilter = new FileChooser.ExtensionFilter("JAR files (*.jar)", "*.jar");
final Function<ActionEvent, Window> getWindowFunc = actionEvent -> ((Button)actionEvent.getSource()).getScene().getWindow();
final var jarFilter = new FileChooser.ExtensionFilter("JAR files (*.jar)", "*.jar");
browseInput.setOnAction(actionEvent -> {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(jarFilter);
File file = fileChooser.showOpenDialog(((Button)actionEvent.getSource()).getScene().getWindow());
File file = fileChooser.showOpenDialog(getWindowFunc.apply(actionEvent));
if (file == null || !file.exists() || !file.isFile())
return;
input.setText(file.getAbsolutePath());
});
browseOutput.setOnAction(actionEvent -> {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(jarFilter);
File file = fileChooser.showSaveDialog(((Button)actionEvent.getSource()).getScene().getWindow());
File file = fileChooser.showSaveDialog(getWindowFunc.apply(actionEvent));
if (file == null || !file.exists() || !file.isFile())
return;
output.setText(file.getAbsolutePath());
Expand All @@ -189,17 +197,33 @@ public void initialize() {
this.buttonObf.setDisable(true);
new Thread(this::obfuscate).start();
});
buttonAddLib.setOnAction(actionEvent -> {
buttonAddJAR.setOnAction(actionEvent -> {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(jarFilter);
List<File> files = fileChooser.showOpenMultipleDialog(((Button)actionEvent.getSource()).getScene().getWindow());
List<File> files = fileChooser.showOpenMultipleDialog(getWindowFunc.apply(actionEvent));
if(files == null) return;
files.forEach(file -> {
if (file == null || !file.exists() || !file.isFile())
return;
libraries.getItems().add(file.getAbsolutePath());
});
});
buttonAddDir.setOnAction(actionEvent -> {
DirectoryChooser dirChooser = new DirectoryChooser();
File file = dirChooser.showDialog(getWindowFunc.apply(actionEvent));
if (file == null || !file.exists() || !file.isDirectory()) return;
libraries.getItems().addAll(FileUtils.getAllFiles(file).stream()
.filter(f -> jarFilter.getExtensions().stream()
.map(s -> s.substring(1)) // remove star
.allMatch(s -> f.getName().endsWith(s)))
.map(f -> {
try {
return f.getCanonicalPath();
} catch (IOException e) {
throw new RuntimeException(String.format("Cannot get canonical path of file %s", f.getName()), e);
}
}).collect(Collectors.toList()));
});
buttonRemoveLib.setOnAction(actionEvent -> {
int index = libraries.getSelectionModel().getSelectedIndex();
if(index != -1)
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/menu.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
<children>
<Label text="Libraries:" />
<Region HBox.hgrow="ALWAYS" />
<Button fx:id="buttonAddLib" mnemonicParsing="false" text="Add" />
<Button fx:id="buttonAddJAR" mnemonicParsing="false" text="Add JAR" />
<Button fx:id="buttonAddDir" mnemonicParsing="false" text="Add from directory" />
<Button fx:id="buttonRemoveLib" mnemonicParsing="false" text="Remove" />
</children>
</HBox>
Expand Down

0 comments on commit c887882

Please sign in to comment.