From 777248ab3c597b36103a9e0fa05e606134a1503c Mon Sep 17 00:00:00 2001 From: Lol3rrr Date: Mon, 30 Sep 2024 13:46:12 +0200 Subject: [PATCH] Some minor improvements and fixes --- src/container.rs | 3 +-- src/frame.rs | 26 ++++++++++++--------- src/lib.rs | 2 +- src/parser.rs | 19 ++++++++++----- src/parser/entities.rs | 8 +++++-- src/parser/propcontroller.rs | 45 ++++++++++++++++++++++++++++++++---- tests/parse.rs | 26 +++++++++++---------- 7 files changed, 91 insertions(+), 38 deletions(-) diff --git a/src/container.rs b/src/container.rs index 5958a53..b316991 100644 --- a/src/container.rs +++ b/src/container.rs @@ -28,8 +28,7 @@ impl<'b> Container<'b> { return Err(ParseContainerError::MissingHeader); } - let magic = - core::str::from_utf8(&input[..8]).map_err(ParseContainerError::InvalidMagic)?; + let magic = core::str::from_utf8(&input[..8]).map_err(ParseContainerError::InvalidMagic)?; let raw_len: [u8; 4] = input[8..12] .try_into() .expect("We know that the input buffer is at least 16 bytes large"); diff --git a/src/frame.rs b/src/frame.rs index 7913400..645678c 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -23,15 +23,19 @@ impl<'b> Frame<'b> { where 'ib: 'b, { - let (input, raw_cmd) = crate::varint::parse_varint(input).map_err(FrameParseError::ParseVarint)?; - let (input, tick) = crate::varint::parse_varint(input).map_err(FrameParseError::ParseVarint)?; - let (input, size) = crate::varint::parse_varint(input).map_err(FrameParseError::ParseVarint)?; + let (input, raw_cmd) = + crate::varint::parse_varint(input).map_err(FrameParseError::ParseVarint)?; + let (input, tick) = + crate::varint::parse_varint(input).map_err(FrameParseError::ParseVarint)?; + let (input, size) = + crate::varint::parse_varint(input).map_err(FrameParseError::ParseVarint)?; if input.len() < size as usize { return Err(FrameParseError::NotEnoughBytes); } - let demo_cmd = crate::DemoCommand::try_from((raw_cmd & !64) as i32).map_err(FrameParseError::ParseDemoCommand)?; + let demo_cmd = crate::DemoCommand::try_from((raw_cmd & !64) as i32) + .map_err(FrameParseError::ParseDemoCommand)?; Ok(( &input[size as usize..], @@ -52,7 +56,10 @@ impl<'b> Frame<'b> { Some(self.inner.as_ref()) } - pub fn decompress_with_buf<'s, 'buf>(&'s self, buf: &'b mut Vec) -> Result<&'buf [u8], FrameDecompressError> + pub fn decompress_with_buf<'s, 'buf>( + &'s self, + buf: &'b mut Vec, + ) -> Result<&'buf [u8], FrameDecompressError> where 's: 'buf, { @@ -60,16 +67,13 @@ impl<'b> Frame<'b> { return Ok(&self.inner); } - let uncompressed_len = snap::raw::decompress_len(&self.inner).map_err(|e| { - FrameDecompressError::GettingDecompressedLength(e) - })?; + let uncompressed_len = snap::raw::decompress_len(&self.inner) + .map_err(|e| FrameDecompressError::GettingDecompressedLength(e))?; buf.resize(uncompressed_len, 0); snap::raw::Decoder::new() .decompress(&self.inner, buf.as_mut_slice()) - .map_err(|e| { - FrameDecompressError::Decompressing(e) - })?; + .map_err(|e| FrameDecompressError::Decompressing(e))?; Ok(buf.as_slice()) } diff --git a/src/lib.rs b/src/lib.rs index a55183a..0b9f00d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ mod container; pub use container::{Container, ParseContainerError}; mod frame; -pub use frame::{Frame, FrameIterator, FrameDecompressError, FrameParseError}; +pub use frame::{Frame, FrameDecompressError, FrameIterator, FrameParseError}; mod democmd; pub use democmd::DemoCommand; diff --git a/src/parser.rs b/src/parser.rs index c099122..ba18a7c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,4 +1,4 @@ -use crate::{packet::DemoEvent, DemoCommand, Frame, UserId, FrameDecompressError}; +use crate::{packet::DemoEvent, DemoCommand, Frame, FrameDecompressError, UserId}; mod fieldpath; pub use fieldpath::{FieldPath, Paths}; @@ -9,8 +9,8 @@ mod propcontroller; mod sendtables; mod variant; -pub use variant::Variant; pub use entities::EntityFilter; +pub use variant::Variant; #[derive(Debug)] pub enum FirstPassError { @@ -62,7 +62,7 @@ impl EntityTickList { Self { ticks: vec![EntityTickStates { tick: 0, - states: Vec::new() + states: Vec::new(), }], } } @@ -338,7 +338,12 @@ fn inner_parse_packet( let raw: crate::csgo_proto::CnetMsgTick = prost::Message::decode(msg_bytes.as_slice())?; - assert!(*current_tick <= raw.tick(), "Current Tick {} <= Tick Packet {}", *current_tick, raw.tick()); + assert!( + *current_tick <= raw.tick(), + "Current Tick {} <= Tick Packet {}", + *current_tick, + raw.tick() + ); if raw.tick() > *current_tick { *current_tick = raw.tick(); entity_states.new_tick(*current_tick); @@ -368,7 +373,7 @@ fn inner_parse_packet( if let Some(baseline_bytes) = baselines.get(&cls) { let mut br = crate::bitreader::Bitreader::new(baseline_bytes); - + // TODO // How should we handle is this? let _state = update_entity( @@ -394,7 +399,9 @@ fn inner_parse_packet( } } 0b00 => { - if raw.has_pvs_vis_bits() > 0 && bitreader.read_nbits(2)? & 0x01 == 1 { + if raw.has_pvs_vis_bits() > 0 + && bitreader.read_nbits(2)? & 0x01 == 1 + { continue; } diff --git a/src/parser/entities.rs b/src/parser/entities.rs index 89eb436..307cf42 100644 --- a/src/parser/entities.rs +++ b/src/parser/entities.rs @@ -91,8 +91,10 @@ impl EntityContext { value: result, }); } else { - println!("Missing PropInfo for {:?}", fi); + // println!("Missing PropInfo for {:?} = {:?}", fi, result); } + } else { + // println!("Missing Field Info for {:?} with {:?} = {:?}", field, path, result); } } @@ -114,6 +116,8 @@ impl EntityContext { impl EntityState { pub fn get_prop(&self, name: &str) -> Option<&EntityProp> { - self.props.iter().find(|p| p.prop_info.prop_name.as_ref() == name) + self.props + .iter() + .find(|p| p.prop_info.prop_name.as_ref() == name) } } diff --git a/src/parser/propcontroller.rs b/src/parser/propcontroller.rs index 153f67a..7dde1fc 100644 --- a/src/parser/propcontroller.rs +++ b/src/parser/propcontroller.rs @@ -106,7 +106,42 @@ impl PropController { name_to_id: HashMap::new(), id_to_name: HashMap::new(), path_to_name: HashMap::new(), - prop_infos: HashMap::new(), + prop_infos: [ + ( + WEAPON_SKIN_ID, + PropInfo { + id: WEAPON_SKIN_ID, + prop_name: "weapon_skin_id".into(), + }, + ), + ( + WEAPON_PAINT_SEED, + PropInfo { + id: WEAPON_PAINT_SEED, + prop_name: "weapon_paint_seed".into(), + }, + ), + ( + WEAPON_FLOAT, + PropInfo { + id: WEAPON_FLOAT, + prop_name: "weapon_float".into(), + }, + ), + ( + MY_WEAPONS_OFFSET, + PropInfo { + id: MY_WEAPONS_OFFSET, + prop_name: "my_weapons_offset".into(), + }, + ), + (ITEM_PURCHASE_COST, PropInfo { + id: ITEM_PURCHASE_COST, + prop_name: "item_purchase_cost".into(), + }), + ] + .into_iter() + .collect(), } } @@ -138,9 +173,11 @@ impl PropController { path.clone(), ); } - Field::Array(ser) => if let Field::Value(v) = &mut ser.field_enum.as_mut() { - self.handle_prop(&(ser_name.clone() + "." + &v.name), v, path); - }, + Field::Array(ser) => { + if let Field::Value(v) = &mut ser.field_enum.as_mut() { + self.handle_prop(&(ser_name.clone() + "." + &v.name), v, path); + } + } Field::Vector(_x) => { let vec_path = path.clone(); if let Ok(inner) = f.get_inner_mut(0) { diff --git a/tests/parse.rs b/tests/parse.rs index 05a5d78..77297ab 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -18,19 +18,21 @@ fn mirage_1() { assert_eq!("de_mirage", output.header.map_name()); for event in output.events.iter() { - if let DemoEvent::GameEvent(gevent) = event { if let GameEvent::PlayerDeath(death) = gevent.as_ref() { - assert!( - death.remaining.is_empty(), - "Remaining for PlayerDeath: {:?}", - death.remaining - ); + if let DemoEvent::GameEvent(gevent) = event { + if let GameEvent::PlayerDeath(death) = gevent.as_ref() { + assert!( + death.remaining.is_empty(), + "Remaining for PlayerDeath: {:?}", + death.remaining + ); - let died_user = output - .player_info - .get(death.userid.as_ref().unwrap()) - .unwrap(); - // dbg!(died_user); - } }; + let died_user = output + .player_info + .get(death.userid.as_ref().unwrap()) + .unwrap(); + // dbg!(died_user); + } + }; } todo!()