fix key input with filters, also removed regex from gui and make text… (#298)

… filters case-insensitive

Closes #297

## Description

Up and down arrow to select previous/next packet now ignore filtered
packets.

## Test Plan

1. Query the server using refresh on the server browser
2. Uncheck QueryResponseS2c in the packet-selector list
3. Click HandshakeC2s
4. Press down arrow until last packet is selected
5. It shouldn't go "blank" anymore when going from QueryRequestC2s to
QueryPingC2s

As an additive request, I've removed the regex check from the main GUI
filter box, such that this is now case-insensitive when filtering
specific packets.
CLI still uses regex to be able to filter on multiple packets
(unchanged)
This commit is contained in:
AviiNL 2023-03-22 07:49:44 +01:00 committed by GitHub
parent 4cf6e1a207
commit 628a0ff3c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 4 deletions

View file

@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::RwLock;
@ -16,6 +17,7 @@ use valence_protocol::packet::{C2sPlayPacket, S2cLoginPacket, S2cPlayPacket};
use valence_protocol::raw::RawPacket;
use crate::packet_widget::{systemtime_strftime, PacketDirection};
use crate::MetaPacket;
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum Stage {
@ -276,6 +278,7 @@ pub struct Context {
pub(crate) packet_count: RwLock<usize>,
pub(crate) has_encryption_enabled_error: AtomicBool,
pub filter: RwLock<String>,
pub visible_packets: RwLock<BTreeMap<MetaPacket, bool>>,
c2s_style: Style,
s2c_style: Style,
}
@ -287,7 +290,10 @@ impl Context {
last_packet: AtomicUsize::new(0),
selected_packet: RwLock::new(None),
packets: RwLock::new(Vec::new()),
filter: RwLock::new("".into()),
visible_packets: RwLock::new(BTreeMap::new()),
packet_count: RwLock::new(0),
has_encryption_enabled_error: AtomicBool::new(false),
@ -354,11 +360,51 @@ impl Context {
}
}
pub fn set_selected_packets(&self, packets: BTreeMap<MetaPacket, bool>) {
*self.visible_packets.write().expect("Poisoned RwLock") = packets;
}
pub fn is_packet_hidden(&self, index: usize) -> bool {
let packets = self.packets.read().expect("Poisoned RwLock");
let packet = packets.get(index).expect("Packet not found");
let visible_packets = self.visible_packets.read().expect("Poisoned RwLock");
let meta_packet: MetaPacket = (*packet).clone().into();
if let Some(visible) = visible_packets.get(&meta_packet) {
if !visible {
return true;
}
}
let filter = self.filter.read().expect("Poisoned RwLock");
let filter = filter.as_str();
if !filter.is_empty()
&& packet
.packet_name
.to_lowercase()
.contains(&filter.to_lowercase())
{
return true;
}
false
}
pub fn select_previous_packet(&self) {
let mut selected_packet = self.selected_packet.write().expect("Poisoned RwLock");
if let Some(idx) = *selected_packet {
if idx > 0 {
*selected_packet = Some(idx - 1);
let mut new_index = idx - 1;
while self.is_packet_hidden(new_index) {
if new_index == 0 {
new_index = idx;
break;
}
new_index -= 1;
}
*selected_packet = Some(new_index);
}
} else {
let packets = self.packets.read().expect("Poisoned RwLock");
@ -372,7 +418,16 @@ impl Context {
let mut selected_packet = self.selected_packet.write().expect("Poisoned RwLock");
if let Some(idx) = *selected_packet {
if idx < self.packets.read().expect("Poisoned RwLock").len() - 1 {
*selected_packet = Some(idx + 1);
let mut new_index = idx + 1;
while self.is_packet_hidden(new_index) {
if new_index == self.packets.read().expect("Poisoned RwLock").len() - 1 {
new_index = idx;
break;
}
new_index += 1;
}
*selected_packet = Some(new_index);
}
} else {
let packets = self.packets.read().expect("Poisoned RwLock");

View file

@ -329,6 +329,17 @@ impl From<(Stage, i32, PacketDirection, String)> for MetaPacket {
}
}
impl From<Packet> for MetaPacket {
fn from(packet: Packet) -> Self {
Self {
stage: packet.stage,
id: packet.packet_type,
direction: packet.direction,
name: packet.packet_name,
}
}
}
// to string and from string to be used in toml
impl ToString for MetaPacket {
fn to_string(&self) -> String {
@ -485,6 +496,8 @@ impl GuiApp {
None => BTreeMap::new(),
};
context.set_selected_packets(selected_packets.clone());
Self {
config,
context,
@ -573,6 +586,8 @@ impl GuiApp {
if changed {
self.config
.set_selected_packets(self.selected_packets.clone());
self.context
.set_selected_packets(self.selected_packets.clone());
}
}
}
@ -839,6 +854,8 @@ impl eframe::App for GuiApp {
self.selected_packets.insert(m_packet.clone(), true);
self.config
.set_selected_packets(self.selected_packets.clone());
self.context
.set_selected_packets(self.selected_packets.clone());
} else {
// if it does exist, check if the names are the same, if not
// update the key
@ -850,6 +867,8 @@ impl eframe::App for GuiApp {
self.selected_packets.insert(m_packet.clone(), value);
self.config
.set_selected_packets(self.selected_packets.clone());
self.context
.set_selected_packets(self.selected_packets.clone());
}
}
@ -863,8 +882,11 @@ impl eframe::App for GuiApp {
return true;
}
if let Ok(re) = regex::Regex::new(&self.filter) {
return re.is_match(&p.packet_name);
if p.packet_name
.to_lowercase()
.contains(&self.filter.to_lowercase())
{
return true;
}
false