G-Earth/Extensions/BlockReplacePackets/src/main/java/extensions/blockreplacepackets/BlockAndReplacePackets.java

215 lines
7.3 KiB
Java
Raw Normal View History

2018-10-13 22:54:52 +02:00
package extensions.blockreplacepackets;
2018-09-22 18:30:32 +02:00
2018-11-06 20:32:36 +01:00
import extensions.blockreplacepackets.rules.BlockReplaceRule;
import extensions.blockreplacepackets.rules.RuleFactory;
2018-11-06 20:46:11 +01:00
import gearth.extensions.Extension;
2018-11-06 15:02:54 +01:00
import gearth.protocol.HMessage;
2018-11-06 13:56:24 +01:00
import gearth.protocol.HPacket;
2018-10-13 22:54:52 +02:00
import gearth.ui.GEarthController;
2018-11-06 13:56:24 +01:00
import javafx.application.Platform;
2018-11-06 20:32:36 +01:00
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
2018-11-06 13:56:24 +01:00
import javafx.event.ActionEvent;
2018-09-22 18:30:32 +02:00
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
2018-11-06 20:32:36 +01:00
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
2018-11-06 20:32:36 +01:00
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
2018-09-22 18:30:32 +02:00
import javafx.stage.Stage;
2018-10-13 22:54:52 +02:00
import gearth.extensions.ExtensionForm;
import gearth.extensions.ExtensionInfo;
2018-09-22 18:30:32 +02:00
2018-11-06 20:32:36 +01:00
import java.util.ArrayList;
import java.util.List;
2018-09-22 18:30:32 +02:00
/**
* Created by Jonas on 22/09/18.
*/
@ExtensionInfo(
2018-11-06 20:32:36 +01:00
Title = "G-Manipulate",
Description = "Block &/ replace packets",
Version = "0.1",
Author = "sirjonasxx"
)
2018-09-22 18:30:32 +02:00
public class BlockAndReplacePackets extends ExtensionForm {
public TextField txt_replacement;
public ComboBox<String> cmb_type;
public Button btn_add;
2018-11-06 15:02:54 +01:00
public volatile ComboBox<String> cmb_side;
2018-11-06 13:56:24 +01:00
public TextField txt_value;
2018-11-06 20:32:36 +01:00
public ScrollPane scrollpane;
public VBox vbox;
public GridPane header;
List<BlockReplaceRule> rules = new ArrayList<>();
2018-09-22 18:30:32 +02:00
public static void main(String[] args) {
2018-11-06 19:30:50 +01:00
runExtensionForm(args, BlockAndReplacePackets.class);
2018-09-22 18:30:32 +02:00
}
//initialize javaFX elements
public void initialize() {
2018-11-06 13:56:24 +01:00
cmb_type.getItems().addAll("Block packet", "Replace packet", "Replace integer", "Replace string", "Replace substring");
cmb_type.getSelectionModel().selectFirst();
2018-11-06 13:56:24 +01:00
cmb_side.getItems().addAll("Incoming", "Outgoing");
cmb_side.getSelectionModel().selectFirst();
cmb_side.getSelectionModel().selectedItemProperty().addListener(observable -> Platform.runLater(this::refreshOptions));
cmb_type.getSelectionModel().selectedItemProperty().addListener(observable -> Platform.runLater(this::refreshOptions));
txt_replacement.textProperty().addListener(event -> Platform.runLater(this::refreshOptions));
txt_value.textProperty().addListener(event -> Platform.runLater(this::refreshOptions));
refreshOptions();
2018-11-06 15:02:54 +01:00
cmb_type.requestFocus();
}
2018-11-06 13:56:24 +01:00
private void refreshOptions() {
txt_replacement.setDisable(cmb_type.getSelectionModel().getSelectedItem().startsWith("Block"));
if (cmb_side.getItems().size() == 2 && !cmb_type.getSelectionModel().getSelectedItem().endsWith("packet")) {
cmb_side.getItems().add("All");
}
else if (cmb_side.getItems().size() == 3 && cmb_type.getSelectionModel().getSelectedItem().endsWith("packet")) {
if (cmb_side.getSelectionModel().getSelectedItem() != null && cmb_side.getSelectionModel().getSelectedItem().equals("All")) {
cmb_side.getSelectionModel().selectFirst();
}
cmb_side.getItems().remove(2);
}
boolean isValid = false;
String val = txt_value.getText();
String repl = txt_replacement.getText();
String type = cmb_type.getSelectionModel().getSelectedItem();
String side = cmb_side.getSelectionModel().getSelectedItem();
if (side == null) {
isValid = false;
}
else if (type.equals("Block packet")) {
try {
int v = Integer.parseInt(val);
isValid = (v < (Short.MAX_VALUE * 2 + 2) && v > 0);
}
catch (Exception e) {
isValid = false;
}
}
else {
if (type.endsWith("packet")) {
try {
int v = Integer.parseInt(val);
isValid = (v < (Short.MAX_VALUE * 2 + 2) && v > 0);
if (isValid) {
HPacket packet = new HPacket(repl);
isValid = !packet.isCorrupted();
}
}
catch (Exception e) {
isValid = false;
}
}
else if (type.endsWith("string")) {
isValid = !val.equals("") && !repl.equals("") && !val.equals(repl);
}
else if (type.endsWith("integer")) {
try {
int v1 = Integer.parseInt(val);
int v2 = Integer.parseInt(repl);
isValid = (v1 != v2);
}
catch (Exception e) {
isValid = false;
}
}
}
btn_add.setDisable(!isValid);
2018-11-06 15:02:54 +01:00
String[] spl = type.split(" ");
if (repl.equals("") && spl[0].equals("Replace")) {
if (spl[1].equals("packet")) {
txt_replacement.setPromptText("Enter a packet here");
}
else if (spl[1].equals("integer")) {
txt_replacement.setPromptText("Enter an integer here");
}
else if (spl[1].endsWith("string")) {
txt_replacement.setPromptText("Enter a string here");
}
}
else {
txt_replacement.setPromptText("");
}
2018-11-06 13:56:24 +01:00
2018-11-06 15:02:54 +01:00
if (val.equals("")) {
if (spl[1].equals("packet")) {
txt_value.setPromptText("Enter the headerID");
}
else if (spl[1].equals("integer")) {
txt_value.setPromptText("Enter an integer");
}
else if (spl[1].endsWith("string")) {
txt_value.setPromptText("Enter a string");
}
}
else {
txt_value.setPromptText("");
}
}
2018-11-06 13:56:24 +01:00
2018-11-06 20:32:36 +01:00
private void clearInput() {
txt_value.clear();
txt_replacement.clear();
refreshOptions();
cmb_type.requestFocus();
}
2018-09-22 18:30:32 +02:00
@Override
protected void initExtension() {
2018-11-06 20:46:11 +01:00
Extension.MessageListener messageListener = message -> {
for (BlockReplaceRule rule : rules) {
rule.appendRuleToMessage(message);
}
};
intercept(HMessage.Side.TOSERVER, messageListener);
intercept(HMessage.Side.TOCLIENT, messageListener);
2018-09-22 18:30:32 +02:00
}
@Override
2018-11-06 15:02:54 +01:00
public ExtensionForm launchForm(Stage primaryStage) throws Exception {
2018-09-22 18:30:32 +02:00
FXMLLoader loader = new FXMLLoader(BlockAndReplacePackets.class.getResource("blockreplace.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Packet blocker &/ replacer");
2018-11-06 13:56:24 +01:00
primaryStage.setScene(new Scene(root));
primaryStage.setResizable(false);
primaryStage.getScene().getStylesheets().add(GEarthController.class.getResource("/gearth/ui/bootstrap3.css").toExternalForm());
2018-11-06 15:02:54 +01:00
return loader.getController();
}
@Override
protected void onShow() {
Platform.runLater(() -> cmb_type.requestFocus());
2018-09-22 18:30:32 +02:00
}
2018-11-06 13:56:24 +01:00
public void click_btnAddRule(ActionEvent actionEvent) {
2018-11-06 20:32:36 +01:00
BlockReplaceRule rule = RuleFactory.getRule(cmb_type.getSelectionModel().getSelectedItem(), cmb_side.getSelectionModel().getSelectedItem(), txt_value.getText(), txt_replacement.getText());
rules.add(rule);
rule.onDelete(observable -> rules.remove(rule));
new RuleContainer(rule, vbox);
2018-11-06 13:56:24 +01:00
2018-11-06 20:32:36 +01:00
clearInput();
2018-11-06 13:56:24 +01:00
}
2018-09-22 18:30:32 +02:00
}