From 0c74260503d516afa02f1cd1aee0bd66a923ecec Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Tue, 27 Dec 2022 20:36:28 +0100 Subject: [PATCH] Packet safety in scheduler --- .../src/main/java/gearth/protocol/HPacket.java | 12 ++++++++++-- .../subforms/scheduler/SchedulerController.java | 15 +++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/G-Earth/src/main/java/gearth/protocol/HPacket.java b/G-Earth/src/main/java/gearth/protocol/HPacket.java index f8fdd62..bceb962 100644 --- a/G-Earth/src/main/java/gearth/protocol/HPacket.java +++ b/G-Earth/src/main/java/gearth/protocol/HPacket.java @@ -121,7 +121,7 @@ public class HPacket implements StringifyAble { } public void completePacket(PacketInfoManager packetInfoManager) { - if (isCorrupted() || identifier == null) return; + if (isCorrupted() || identifier == null || packetInfoManager == null) return; PacketInfo packetInfo = packetInfoManager.getPacketInfoFromName(identifierDirection, identifier); if (packetInfo == null) { @@ -718,7 +718,15 @@ public class HPacket implements StringifyAble { public String toExpression() { if (isCorrupted()) return ""; - return PacketStringUtils.predictedExpression(this, null); + return PacketStringUtils.predictedExpression(this, dummyPacketInfo()); + } + + /** + * Provides dummy packet information for a packet that hasn't been completed with headerId yet + */ + private PacketInfo dummyPacketInfo() { + if (isPacketComplete()) return null; + return new PacketInfo(identifierDirection, -1, "", identifier, null, ""); } public void setBytes(byte[] bytes) { diff --git a/G-Earth/src/main/java/gearth/ui/subforms/scheduler/SchedulerController.java b/G-Earth/src/main/java/gearth/ui/subforms/scheduler/SchedulerController.java index 5f0552b..307b21c 100644 --- a/G-Earth/src/main/java/gearth/ui/subforms/scheduler/SchedulerController.java +++ b/G-Earth/src/main/java/gearth/ui/subforms/scheduler/SchedulerController.java @@ -1,14 +1,12 @@ package gearth.ui.subforms.scheduler; import com.tulskiy.keymaster.common.Provider; -import gearth.GEarth; import gearth.services.scheduler.Interval; import gearth.services.scheduler.Scheduler; import gearth.ui.translations.LanguageBundle; import gearth.ui.translations.TranslatableString; import javafx.application.Platform; import javafx.event.ActionEvent; -import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.layout.GridPane; import javafx.scene.layout.VBox; @@ -28,7 +26,7 @@ import java.util.List; public class SchedulerController extends SubForm { private static final Interval defaultInterval = new Interval(0, 500); - private static final HPacket defaultPacket = new HPacket(0); + private static final HPacket defaultPacket = new HPacket("Chat", HMessage.Direction.TOCLIENT, -1, "Frank loves G-Earth", 0, 33, 0, 0); public VBox schedulecontainer; public GridPane header; @@ -81,6 +79,7 @@ public class SchedulerController extends SubForm { System.setErr(err); initLanguageBinding(); + setInputDefault(true); } @Override @@ -130,7 +129,7 @@ public class SchedulerController extends SubForm { isBeingEdited.isUpdatedTrigger(); isBeingEdited = null; - setInputDefault(); + setInputDefault(false); } } @@ -142,7 +141,7 @@ public class SchedulerController extends SubForm { newItem.onDelete(() -> { if (isBeingEdited == newItem) { - setInputDefault(); + setInputDefault(false); isBeingEdited = null; } scheduler.remove(newItem); @@ -167,16 +166,16 @@ public class SchedulerController extends SubForm { newItem.onIsBeingUpdatedTrigger(); } else { - setInputDefault(); + setInputDefault(false); isBeingEdited.isUpdatedTrigger(); isBeingEdited = null; } }); } - private void setInputDefault() { + private void setInputDefault(boolean showDummyPacket) { txt_delay.setText(defaultInterval.toString()); - txt_packet.setText(defaultPacket.toString()); + txt_packet.setText(showDummyPacket ? defaultPacket.toExpression() : ""); rb_incoming.setSelected(true); rb_outgoing.setSelected(false);