Skip to content

Commit

Permalink
A TON of progress.
Browse files Browse the repository at this point in the history
- added org.json and toml4j
-  Fixed bugs with UI updating during extraction
- realised init is kinda pointless tbh
- cleaned up the code a little, removed useless imports
- added TOML and JSON storage in DataManager
- fixed bugs / missing code in extraction
- grammar correction
- Added JSON Parsing
- Added TOML Parsing
- Added YAML Parsing

- created overview FXML but not started
  • Loading branch information
Skullians committed Feb 15, 2024
1 parent 6b7413e commit bbbb01a
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 35 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@
<version>20.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240205</version>
</dependency>
<dependency>
<groupId>com.moandjiezana.toml</groupId>
<artifactId>toml4j</artifactId>
<version>0.7.2</version>
</dependency>
</dependencies>

<repositories>
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
module skullian.binarysearchinator {
// JavaFX
requires org.kordamp.ikonli.javafx;
requires javafx.controls;
requires javafx.fxml;
requires org.yaml.snakeyaml;
requires javafx.web;

// Miscellaneous
requires java.desktop;

requires org.kordamp.ikonli.javafx;
requires java.logging;
requires java.sql;

// File Parsing
requires org.yaml.snakeyaml;
requires org.json;
requires toml4j;

// JavaFX
opens skullian.binarysearchinator to javafx.fxml;
exports skullian.binarysearchinator;
exports skullian.binarysearchinator.control;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ public void userConfirmation(BorderPane borderPane1) {
void confirmChoices(MouseEvent event) {
input = dirField.getText();
output = tempField.getText();



loadPage("binsearch/decom");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package skullian.binarysearchinator.control;

import javafx.animation.RotateTransition;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
Expand All @@ -9,16 +10,20 @@
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
import skullian.binarysearchinator.MainApp;
import skullian.binarysearchinator.util.jar.Extractor;

import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class DecomManager implements Initializable {
private static Logger LOGGER = MainApp.LOGGER;

@FXML
private AnchorPane anchorPane;
Expand All @@ -41,15 +46,21 @@ public void initialize(URL location, ResourceBundle resources) {
setRotate(c1, true, 360, 5);
setRotate(c2, true, 270, 7);

beginExtraction();
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.submit(() -> {
beginExtraction();
});

ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(() -> {
scannedCount.setText(Extractor.count);
extractingField.setText(Extractor.processing);
dependenciesField.setText(Arrays.toString(Extractor.dependencies));
if (Extractor.completed) { executorService.shutdown(); }
}, 0, 1, TimeUnit.SECONDS);
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
Platform.runLater(() -> {
scannedCount.setText(Extractor.count);
extractingField.setText(Extractor.processing);
dependenciesField.setText(Arrays.toString(Extractor.dependencies));
if (Extractor.completed) { executorService.shutdown(); }
});
};
executorService.scheduleAtFixedRate(task, 0, 1, TimeUnit.MILLISECONDS);
}

private void setRotate(Circle c, boolean reverse, int angle, int duration) {
Expand All @@ -58,7 +69,7 @@ private void setRotate(Circle c, boolean reverse, int angle, int duration) {
rt.setByAngle(angle);
rt.setAutoReverse(reverse);
rt.setDelay(Duration.seconds(0));
rt.setCycleCount(-1);
rt.setCycleCount(10000);
rt.play();
}

Expand All @@ -67,15 +78,19 @@ private void beginExtraction() {
switch (ConfirmationHandler.jtype) {

case "Forge":
LOGGER.warning("Extracting Forge / NeoForge Mods.");
Extractor.extractForgeMods(ConfirmationHandler.input, ConfirmationHandler.output);
break;
case "Plugin":
LOGGER.warning("Extracting Spigot / Paper Plugins.");
Extractor.extractPlugins(ConfirmationHandler.input, ConfirmationHandler.output);
break;
case "Fabric":
LOGGER.warning("Extracting Fabric Mods.");
Extractor.extractFabricQuiltMods(ConfirmationHandler.input, ConfirmationHandler.output, "fabric.mod.json");
break;
case "Quilt":
LOGGER.warning("Extracting Quilt Mods.");
Extractor.extractFabricQuiltMods(ConfirmationHandler.input, ConfirmationHandler.output, "quilt.mod.json");
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package skullian.binarysearchinator.control;


import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
Expand All @@ -20,7 +15,6 @@
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javafx.util.Duration;
import skullian.binarysearchinator.MainApp;

import java.awt.*;
Expand Down
35 changes: 24 additions & 11 deletions src/main/java/skullian/binarysearchinator/util/jar/Extractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import javafx.scene.layout.BorderPane;
import skullian.binarysearchinator.MainApp;
import skullian.binarysearchinator.control.ErrorHandler;
import skullian.binarysearchinator.util.jar.parsing.ParseJSON;
import skullian.binarysearchinator.util.jar.parsing.ParseTOML;
import skullian.binarysearchinator.util.jar.parsing.ParseYAML;

import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -18,9 +19,7 @@
import java.util.zip.ZipFile;

public class Extractor {
private static Logger LOGGER = MainApp.LOGGER;
private ErrorHandler errorHandler = new ErrorHandler();

private static final Logger LOGGER = MainApp.LOGGER;

public static String processing = "N/A";
public static String[] dependencies;
Expand All @@ -42,7 +41,6 @@ public String getType(String input, String output) {
}

Path jarFilePath = getRandomJarFile(input);
List<String> toCheck = Arrays.asList("plugin.yml", "fabric.mod.json", "mods.toml", "quilt.mod.json");
ZipFile jarFile = new ZipFile(jarFilePath.toFile());
Enumeration<? extends ZipEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
Expand Down Expand Up @@ -153,27 +151,35 @@ public static void extractPlugins(String input, String output) {
for (File file : files) {
if (file.getName().endsWith(".jar")) {
try (JarFile jarFile = new JarFile(file)) {
processing = jarFile.getName();
processing = file.getName();
JarEntry fileEntry = jarFile.getJarEntry("plugin.yml");

if (fileEntry != null) {
try (InputStream inputStream = jarFile.getInputStream(fileEntry);

FileOutputStream outputStream = new FileOutputStream(new File(output + "/plugin.yml"))) {
byte[] buffer = new byte[1024];
int length;

while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}

ParseYAML.parseYAML(output, file.getName());
}
}
}
aNum++;
count = aNum + " / " + totalcount;
}
}
processing = "Done!";
completed = true;
}
} catch (Exception error) {
ErrorHandler.error = "Failed to extract plugin: \n" + error;
ErrorHandler.setErrorMessage(pane);

LOGGER.severe("An unexpected error occured during extracting plugins.");
LOGGER.severe(Arrays.toString(error.getStackTrace()));
}
Expand All @@ -194,7 +200,7 @@ public static void extractFabricQuiltMods(String input, String output, String de
for (File file : files) {
if (file.getName().endsWith(".jar")) {
try (JarFile jarFile = new JarFile(file)) {
processing = jarFile.getName();
processing = file.getName();
JarEntry fileEntry = jarFile.getJarEntry(desiredOutput);
if (fileEntry != null) {
try (InputStream inputStream = jarFile.getInputStream(fileEntry);
Expand All @@ -204,6 +210,8 @@ public static void extractFabricQuiltMods(String input, String output, String de
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}

ParseJSON.parseJSON(output, file.getName(), desiredOutput);
}
}
}
Expand All @@ -212,6 +220,7 @@ public static void extractFabricQuiltMods(String input, String output, String de
}
}
processing = "Done!";
completed = true;
}
} catch (Exception error) {
ErrorHandler.error = "Failed to extract plugin: \n" + error;
Expand All @@ -236,8 +245,8 @@ public static void extractForgeMods(String input, String output) {
for (File file : files) {
if (file.getName().endsWith(".jar")) {
try (JarFile jarFile = new JarFile(file)) {
processing = jarFile.getName();
JarEntry fileEntry = jarFile.getJarEntry("mods.toml");
processing = file.getName();
JarEntry fileEntry = jarFile.getJarEntry("META-INF/mods.toml");
if (fileEntry != null) {
try (InputStream inputStream = jarFile.getInputStream(fileEntry);
FileOutputStream outputStream = new FileOutputStream(new File(output + "/mods.toml"))) {
Expand All @@ -246,13 +255,17 @@ public static void extractForgeMods(String input, String output) {
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}

ParseTOML.parseTOML(output, file.getName());
}
}
}
aNum++;
count = aNum + " / " + totalcount;
}
}
processing = "Done!";
completed = true;
}
} catch (Exception error) {
ErrorHandler.error = "Failed to extract plugin: \n" + error;
Expand All @@ -268,7 +281,7 @@ private static void createTempDirectory(String output) {
if (!directory.exists()) {
boolean created = directory.mkdirs();
if (created) {
LOGGER.info("Successfully crated temporary directory at [" + output + "].");
LOGGER.info("Successfully created temporary directory at [" + output + "].");
} else {
LOGGER.severe("Failed to create temporary directory at [" + output + "].");
ErrorHandler.error = "Failed to create temporary directory at [" + output + "].";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package skullian.binarysearchinator.util.jar.data;

import com.moandjiezana.toml.Toml;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class DataManager {

public static Map<String, String> fileStorage = new HashMap<>(); // file name, full name (e.g. viaversion.jar / ViaVersion)
public static Map<String, Object> yamlStorage = new HashMap<>(); // full name, yaml object (e.g. ViaVersion / Object)

public static Map<String, Map<String, Object>> yamlStorage = new HashMap<>(); // full name, yaml object (e.g. ViaVersion / Object)
public static Map<String, JSONObject> jsonStorage = new HashMap<>();
public static Map<String, Toml> tomlStorage = new HashMap<>();

public static void addFileStorage(String fileName, String parsedName) {
fileStorage.put(fileName, parsedName);
}

public static void addYAMLStorage(String fullName, Object data) {
public static void addYAMLStorage(String fullName, Map<String, Object> data) {
yamlStorage.put(fullName, data);
}
public static void addJSONStorage(String fullName, JSONObject data) { jsonStorage.put(fullName, data); }
public static void addTOMLStorage(String fullName, Toml data) { tomlStorage.put(fullName, data); }

public static String queryFiles(String fileName) {
if (fileStorage.containsKey(fileName)) {
Expand All @@ -23,10 +31,24 @@ public static String queryFiles(String fileName) {
return null;
}

public static Object queryYAML(String fullName) {
public static Map<String, Object> queryYAML(String fullName) {
if (yamlStorage.containsKey(fullName)) {
return yamlStorage.get(fullName);
}
return null;
}

public static JSONObject queryJSON(String fullName) {
if (jsonStorage.containsKey(fullName)) {
return jsonStorage.get(fullName);
}
return null;
}

public static Toml queryTOML(String fullName) {
if (tomlStorage.containsKey(fullName)) {
return tomlStorage.get(fullName);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
package skullian.binarysearchinator.util.jar.parsing;

import org.json.JSONObject;
import skullian.binarysearchinator.MainApp;
import skullian.binarysearchinator.control.ErrorHandler;
import skullian.binarysearchinator.util.jar.Extractor;
import skullian.binarysearchinator.util.jar.data.DataManager;

import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Logger;

public class ParseJSON {
private static Logger LOGGER = MainApp.LOGGER;

public static void parseJSON(String output, String processed, String filename) {
try {
FileReader reader = new FileReader(output + "\\" + filename);
JSONObject jsonObject = new JSONObject(reader);

String fullName = jsonObject.getString("name");

DataManager.addFileStorage(processed, fullName);
DataManager.addJSONStorage(fullName, jsonObject);
} catch (IOException error) {
ErrorHandler.error = "An unexpected error occured when parsing JSON files: \n" + error;
ErrorHandler.setErrorMessage(Extractor.pane);
LOGGER.severe("An unexpected error occured when parsing JSON files.");
LOGGER.severe(Arrays.toString(error.getStackTrace()));
}
}
}
Loading

0 comments on commit bbbb01a

Please sign in to comment.