Packet inspector fixes (#375)

## Description

packet_to_string now returns a Result as not to crash the app when
showing TextView on a packet that fails to decode for whatever reason.
This commit is contained in:
AviiNL 2023-06-18 07:54:53 +02:00 committed by GitHub
parent 3a3fd9f202
commit d36998dc9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View file

@ -162,7 +162,7 @@ fn write_transformer(packets: &[Packet]) -> anyhow::Result<()> {
match_arms.extend(quote! {
#name::ID => {
format!("{:#?}", #name::decode(&mut data).unwrap())
Ok(format!("{:#?}", #name::decode(&mut data)))
}
});
}
@ -172,14 +172,14 @@ fn write_transformer(packets: &[Packet]) -> anyhow::Result<()> {
side_arms.extend(quote! {
PacketState::#state => match packet.id {
#match_arms
_ => NOT_AVAILABLE.to_string(),
_ => Ok(NOT_AVAILABLE.to_string()),
},
});
}
if side == "Clientbound" {
side_arms.extend(quote! {
_ => NOT_AVAILABLE.to_string(),
_ => Ok(NOT_AVAILABLE.to_string()),
});
}
@ -196,7 +196,7 @@ fn write_transformer(packets: &[Packet]) -> anyhow::Result<()> {
let generated = quote! {
const NOT_AVAILABLE: &str = "Not yet implemented";
pub fn packet_to_string(packet: &ProxyPacket) -> String {
pub fn packet_to_string(packet: &ProxyPacket) -> Result<String, Box<dyn std::error::Error>> {
let bytes = packet.data.as_ref().unwrap();
let mut data = &bytes.clone()[..];

View file

@ -70,7 +70,11 @@ impl View for TextView {
if self.last_packet_id != Some(packet_index) {
self.last_packet_id = Some(packet_index);
self.packet_str = utils::packet_to_string(&packets[packet_index]);
self.packet_str = match utils::packet_to_string(&packets[packet_index]) {
Ok(str) => str,
Err(err) => format!("Error: {}", err),
};
}
code_view_ui(ui, &self.packet_str);