From 37f55e2c5762fc28806c62d2f74a249fb5a8f1e7 Mon Sep 17 00:00:00 2001 From: ice_iix Date: Sat, 19 Dec 2020 13:27:58 -0800 Subject: [PATCH] Add --network-parse-packet option --- protocol/src/protocol/mod.rs | 38 ++++++++++++++++++++++++++++++++++++ src/main.rs | 11 +++++++++++ 2 files changed, 49 insertions(+) diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index 81d9b9c..87b9455 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -1308,6 +1308,44 @@ impl Conn { } } +/// Parse a clientbound packet, for debugging packet parsing issues (Conn::read_packet) +pub fn try_parse_packet(ibuf: Vec, protocol_version: i32) { + println!("trying to parse packet data {:?}", ibuf); + + let mut buf = io::Cursor::new(ibuf); + + let id = VarInt::read_from(&mut buf).unwrap().0; + let dir = Direction::Clientbound; + let state = State::Play; // TODO: allow parsing other states + + println!( + "about to parse id={:x}, dir={:?} state={:?}", + id, dir, state + ); + + let packet = packet::packet_by_id(protocol_version, state, dir, id, &mut buf).unwrap(); + + println!("packet = {:?}", packet); + + match packet { + Some(_val) => { + let pos = buf.position() as usize; + let ibuf = buf.into_inner(); + if ibuf.len() != pos { + println!("pos = {:?}", pos); + println!("ibuf = {:?}", ibuf); + println!( + "Failed to read all of packet 0x{:X}, \ + had {} bytes left", + id, + ibuf.len() - pos + ) + } + } + None => println!("missing packet"), + } +} + #[derive(Debug)] pub struct Status { pub version: StatusVersion, diff --git a/src/main.rs b/src/main.rs index c95b46e..49bfde1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ #![allow(clippy::float_cmp)] // float comparison used to check if changed use log::{error, info, warn}; +use std::fs; use std::time::{Duration, Instant}; extern crate steven_shared as shared; @@ -193,6 +194,10 @@ struct Opt { #[structopt(short = "n", long = "network-debug")] network_debug: bool, + /// Parse a network packet from a file + #[structopt(short = "N", long = "network-parse-packet")] + network_parse_packet: Option, + /// Protocol version to use in the autodetection ping #[structopt(short = "p", long = "default-protocol-version")] default_protocol_version: Option, @@ -328,6 +333,12 @@ fn main2() { protocol::enable_network_debug(); } + if let Some(filename) = opt.network_parse_packet { + let data = fs::read(filename).unwrap(); + protocol::try_parse_packet(data, default_protocol_version); + return; + } + if opt.server.is_some() { game.connect_to(&opt.server.unwrap()); }