mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 10:14:06 +02:00
add cli option to parse all messages for easier debugging
This commit is contained in:
parent
baad66bc0c
commit
e6e32b292b
5 changed files with 57 additions and 16 deletions
|
|
@ -3,8 +3,6 @@ use std::fs;
|
||||||
|
|
||||||
pub use tf_demo_parser::{Demo, DemoParser, Parse, ParseError, ParserState, Result, Stream};
|
pub use tf_demo_parser::{Demo, DemoParser, Parse, ParseError, ParserState, Result, Stream};
|
||||||
|
|
||||||
extern crate jemallocator;
|
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
||||||
|
|
||||||
|
|
@ -17,9 +15,17 @@ fn main() -> std::result::Result<(), Box<ParseError>> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let path = args[1].clone();
|
let path = args[1].clone();
|
||||||
|
let all = args
|
||||||
|
.get(2)
|
||||||
|
.map(|arg| arg.as_str() == "all")
|
||||||
|
.unwrap_or_default();
|
||||||
let file = fs::read(path).expect("Unable to read file");
|
let file = fs::read(path).expect("Unable to read file");
|
||||||
let demo = Demo::new(file);
|
let demo = Demo::new(file);
|
||||||
let (_, state) = DemoParser::parse_demo(demo.get_stream())?;
|
let (_, state) = if all {
|
||||||
|
DemoParser::parse_all(demo.get_stream())
|
||||||
|
} else {
|
||||||
|
DemoParser::parse_demo(demo.get_stream())
|
||||||
|
}?;
|
||||||
let json = serde_json::to_string(&state).unwrap_or("err".to_string());
|
let json = serde_json::to_string(&state).unwrap_or("err".to_string());
|
||||||
println!("{}", json);
|
println!("{}", json);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -204,14 +204,13 @@ impl MessageHandler for Analyser {
|
||||||
|
|
||||||
fn handle_string_entry(&mut self, table: &String, _index: usize, entry: &StringTableEntry) {
|
fn handle_string_entry(&mut self, table: &String, _index: usize, entry: &StringTableEntry) {
|
||||||
match table.as_str() {
|
match table.as_str() {
|
||||||
"userinfo" => match (&entry.text, &entry.extra_data) {
|
"userinfo" => {
|
||||||
(Some(text), Some(data)) => {
|
if let (Some(text), Some(data)) = (&entry.text, &entry.extra_data) {
|
||||||
if data.byte_len > 32 {
|
if data.byte_len > 32 {
|
||||||
let _ = self.parse_user_info(text, data.data.clone());
|
let _ = self.parse_user_info(text, data.data.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
}
|
||||||
},
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,17 @@ impl DemoHandler<Analyser> {
|
||||||
|
|
||||||
impl<T: MessageHandler> DemoHandler<T> {
|
impl<T: MessageHandler> DemoHandler<T> {
|
||||||
pub fn with_analyser(analyser: T) -> Self {
|
pub fn with_analyser(analyser: T) -> Self {
|
||||||
let state_handler = ParserState::new(T::does_handle);
|
let state_handler = ParserState::new(T::does_handle, false);
|
||||||
|
|
||||||
|
DemoHandler {
|
||||||
|
tick: 0,
|
||||||
|
string_table_names: Vec::new(),
|
||||||
|
analyser,
|
||||||
|
state_handler,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn parse_all_with_analyser(analyser: T) -> Self {
|
||||||
|
let state_handler = ParserState::new(T::does_handle, true);
|
||||||
|
|
||||||
DemoHandler {
|
DemoHandler {
|
||||||
tick: 0,
|
tick: 0,
|
||||||
|
|
|
||||||
|
|
@ -183,11 +183,34 @@ impl DemoParser {
|
||||||
Self::parse_with_analyser(stream, Analyser::new())
|
Self::parse_with_analyser(stream, Analyser::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_all(stream: Stream) -> Result<(Header, MatchState)> {
|
||||||
|
Self::parse_all_with_analyser(stream, Analyser::new())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_with_analyser<T: MessageHandler>(
|
pub fn parse_with_analyser<T: MessageHandler>(
|
||||||
mut stream: Stream,
|
stream: Stream,
|
||||||
analyser: T,
|
analyser: T,
|
||||||
) -> Result<(Header, T::Output)> {
|
) -> Result<(Header, T::Output)> {
|
||||||
let mut handler = DemoHandler::with_analyser(analyser);
|
Self::parse(stream, analyser, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_all_with_analyser<T: MessageHandler>(
|
||||||
|
stream: Stream,
|
||||||
|
analyser: T,
|
||||||
|
) -> Result<(Header, T::Output)> {
|
||||||
|
Self::parse(stream, analyser, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse<T: MessageHandler>(
|
||||||
|
mut stream: Stream,
|
||||||
|
analyser: T,
|
||||||
|
all: bool,
|
||||||
|
) -> Result<(Header, T::Output)> {
|
||||||
|
let mut handler = if all {
|
||||||
|
DemoHandler::parse_all_with_analyser(analyser)
|
||||||
|
} else {
|
||||||
|
DemoHandler::with_analyser(analyser)
|
||||||
|
};
|
||||||
let header = Header::read(&mut stream)?;
|
let header = Header::read(&mut stream)?;
|
||||||
loop {
|
loop {
|
||||||
let packet = Packet::parse(&mut stream, handler.get_parser_state())?;
|
let packet = Packet::parse(&mut stream, handler.get_parser_state())?;
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ pub struct ParserState {
|
||||||
pub demo_meta: DemoMeta,
|
pub demo_meta: DemoMeta,
|
||||||
analyser_handles: fn(message_type: MessageType) -> bool,
|
analyser_handles: fn(message_type: MessageType) -> bool,
|
||||||
handle_entities: bool,
|
handle_entities: bool,
|
||||||
|
parse_all: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct StaticBaseline {
|
pub struct StaticBaseline {
|
||||||
|
|
@ -56,7 +57,7 @@ impl StaticBaseline {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParserState {
|
impl ParserState {
|
||||||
pub fn new(analyser_handles: fn(message_type: MessageType) -> bool) -> Self {
|
pub fn new(analyser_handles: fn(message_type: MessageType) -> bool, parse_all: bool) -> Self {
|
||||||
ParserState {
|
ParserState {
|
||||||
static_baselines: HashMap::with_hasher(NullHasherBuilder),
|
static_baselines: HashMap::with_hasher(NullHasherBuilder),
|
||||||
parsed_static_baselines: RefCell::new(HashMap::with_hasher(NullHasherBuilder)),
|
parsed_static_baselines: RefCell::new(HashMap::with_hasher(NullHasherBuilder)),
|
||||||
|
|
@ -71,7 +72,8 @@ impl ParserState {
|
||||||
],
|
],
|
||||||
demo_meta: DemoMeta::default(),
|
demo_meta: DemoMeta::default(),
|
||||||
analyser_handles,
|
analyser_handles,
|
||||||
handle_entities: analyser_handles(MessageType::PacketEntities),
|
handle_entities: analyser_handles(MessageType::PacketEntities) || parse_all,
|
||||||
|
parse_all,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,11 +139,12 @@ impl ParserState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn should_parse_message(&self, message_type: MessageType) -> bool {
|
pub fn should_parse_message(&self, message_type: MessageType) -> bool {
|
||||||
if message_type == MessageType::PacketEntities {
|
self.parse_all
|
||||||
(self.analyser_handles)(message_type)
|
|| if message_type == MessageType::PacketEntities {
|
||||||
} else {
|
self.handle_entities
|
||||||
Self::does_handle(message_type) || (self.analyser_handles)(message_type)
|
} else {
|
||||||
}
|
Self::does_handle(message_type) || (self.analyser_handles)(message_type)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn does_handle(message_type: MessageType) -> bool {
|
pub fn does_handle(message_type: MessageType) -> bool {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue