1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

keep baselineprops seperate and add some tracing

This commit is contained in:
Robin Appelman 2021-07-29 19:16:24 +02:00
commit 24e83957d5
8 changed files with 213 additions and 40 deletions

View file

@ -56,6 +56,7 @@ pub enum PVS {
pub struct PacketEntity {
pub server_class: ClassId,
pub entity_index: EntityId,
pub baseline_props: Vec<SendProp>,
pub props: Vec<SendProp>,
pub in_pvs: bool,
pub pvs: PVS,
@ -75,11 +76,11 @@ impl fmt::Display for PacketEntity {
impl PacketEntity {
pub fn mut_prop_by_identifier(&mut self, index: &SendPropIdentifier) -> Option<&mut SendProp> {
self.props.iter_mut().find(|prop| prop.identifier == *index)
self.props_mut().find(|prop| prop.identifier == *index)
}
pub fn get_prop_by_identifier(&self, index: &SendPropIdentifier) -> Option<&SendProp> {
self.props.iter().find(|prop| prop.identifier == *index)
self.props().find(|prop| prop.identifier == *index)
}
pub fn apply_update(&mut self, props: Vec<SendProp>) {
@ -96,22 +97,18 @@ impl PacketEntity {
self.get_prop_by_identifier(&identifier)
}
pub fn diff_from_baseline<'a>(
&'a self,
baseline: &'a [SendProp],
) -> impl Iterator<Item = &'a SendProp> + 'a {
// self.props.iter().filter(move |prop| {
// !baseline
// .iter()
// .any(|base_prop| base_prop.index == prop.index && base_prop.value == prop.value)
// })
self.props.iter().filter(move |prop| {
baseline
.iter()
.find(|base_prop| base_prop.identifier == prop.identifier)
.map(|base_prop| base_prop.value != prop.value)
.unwrap_or(true)
})
pub fn props(&self) -> impl Iterator<Item = &SendProp> {
self.baseline_props.iter().chain(self.props.iter())
}
pub fn props_mut(&mut self) -> impl Iterator<Item = &mut SendProp> {
self.baseline_props.iter_mut().chain(self.props.iter_mut())
}
pub fn into_props(self) -> impl Iterator<Item = SendProp> {
self.baseline_props
.into_iter()
.chain(self.props.into_iter())
}
}
@ -208,6 +205,7 @@ fn get_entity_for_update(
Ok(PacketEntity {
server_class: class_id,
entity_index,
baseline_props: vec![],
props: Vec::with_capacity(8),
in_pvs: false,
pvs,
@ -259,6 +257,7 @@ impl Parse<'_> for PacketEntitiesMessage {
entities.push(PacketEntity {
server_class: 0.into(),
entity_index,
baseline_props: vec![],
props: vec![],
in_pvs: false,
pvs,
@ -313,17 +312,7 @@ impl Encode for PacketEntitiesMessage {
match entity.pvs {
PVS::Enter => {
Self::write_enter(entity, stream, state)?;
let baseline = state.get_baseline(
self.base_line as usize,
entity.entity_index,
entity.server_class,
send_table,
)?;
Self::write_update(
entity.props.iter().skip(baseline.len()),
stream,
send_table,
)?;
Self::write_update(&entity.props, stream, send_table)?;
}
PVS::Preserve => {
Self::write_update(&entity.props, stream, send_table)?;
@ -363,12 +352,14 @@ impl PacketEntitiesMessage {
.get(usize::from(class_index))
.ok_or(ParseError::UnknownServerClass(class_index))?;
let props = state.get_baseline(baseline_index, entity_index, class_index, send_table)?;
let baseline_props =
state.get_baseline(baseline_index, entity_index, class_index, send_table)?;
Ok(PacketEntity {
server_class: class_index,
entity_index,
props,
baseline_props,
props: vec![],
in_pvs: true,
pvs: PVS::Enter,
serial_number: serial,
@ -431,10 +422,17 @@ impl PacketEntitiesMessage {
true.write(stream)?;
let index = prop.index as usize;
let definition = send_table
.flattened_props
.get(index)
.ok_or(ParseError::UnknownDefinition(prop.identifier))?;
if index >= send_table.flattened_props.len() {
dbg!(&send_table.name);
}
let definition =
send_table
.flattened_props
.get(index)
.ok_or(ParseError::PropIndexOutOfBounds {
index: index as i32,
prop_count: send_table.flattened_props.len(),
})?;
write_bit_var((index as i32 - last_index - 1) as u32, stream)?;
last_index = index as i32;
prop.value.encode(stream, &definition.parse_definition)?;
@ -530,6 +528,7 @@ fn test_packet_entitier_message_roundtrip() {
entities: vec![PacketEntity {
server_class: ClassId::from(0),
entity_index: Default::default(),
baseline_props: vec![],
props: vec![],
in_pvs: true,
pvs: PVS::Enter,
@ -550,6 +549,7 @@ fn test_packet_entitier_message_roundtrip() {
PacketEntity {
server_class: ClassId::from(0),
entity_index: EntityId::from(0),
baseline_props: vec![],
props: vec![],
in_pvs: true,
pvs: PVS::Enter,
@ -559,6 +559,7 @@ fn test_packet_entitier_message_roundtrip() {
PacketEntity {
server_class: ClassId::from(1),
entity_index: EntityId::from(4),
baseline_props: vec![],
props: vec![
SendProp {
index: 0,
@ -579,6 +580,7 @@ fn test_packet_entitier_message_roundtrip() {
PacketEntity {
server_class: ClassId::from(1),
entity_index: EntityId::from(5),
baseline_props: vec![],
props: vec![
SendProp {
index: 0,

View file

@ -5,6 +5,7 @@ use crate::demo::message::{Message, MessageType};
use crate::demo::parser::Encode;
use crate::demo::vector::Vector;
use crate::{Parse, ParserState, ReadResult, Result, Stream};
use tracing::{event, span, Level};
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, BitRead, BitWrite, PartialEq, Serialize, Deserialize, Clone)]
@ -118,15 +119,20 @@ impl<'a> Parse<'a> for MessagePacket<'a> {
let meta = stream.read()?;
let length: u32 = stream.read()?;
event!(Level::DEBUG, length, "reading packet data");
let mut packet_data = stream.read_bits(length as usize * 8)?;
let mut messages = Vec::with_capacity(8);
while packet_data.bits_left() > 6 {
let message_type = MessageType::read(&mut packet_data)?;
let _span =
span!(Level::INFO, "reading packet", message_type = ?message_type).entered();
if state.should_parse_message(message_type) && message_type != MessageType::Empty {
event!(Level::INFO, "parsing message");
messages.push(Message::from_type(message_type, &mut packet_data, state)?);
} else {
event!(Level::INFO, "skipping message");
Message::skip_type(message_type, &mut packet_data, state)?;
}
}

View file

@ -11,6 +11,7 @@ use self::synctick::SyncTickPacket;
use self::usercmd::UserCmdPacket;
use crate::demo::parser::Encode;
use serde::{Deserialize, Serialize};
use tracing::{event, span, Level};
pub mod consolecmd;
pub mod datatable;
@ -48,6 +49,19 @@ impl Packet<'_> {
Packet::StringTables(msg) => msg.tick,
}
}
pub fn set_tick(&mut self, tick: u32) {
match self {
Packet::Sigon(msg) => msg.tick = tick,
Packet::Message(msg) => msg.tick = tick,
Packet::SyncTick(msg) => msg.tick = tick,
Packet::ConsoleCmd(msg) => msg.tick = tick,
Packet::UserCmd(msg) => msg.tick = tick,
Packet::DataTables(msg) => msg.tick = tick,
Packet::Stop(msg) => msg.tick = tick,
Packet::StringTables(msg) => msg.tick = tick,
}
}
}
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
@ -83,6 +97,8 @@ impl Packet<'_> {
impl<'a> Parse<'a> for Packet<'a> {
fn parse(stream: &mut Stream<'a>, state: &ParserState) -> Result<Self> {
let packet_type = PacketType::read(stream)?;
let _span = span!(Level::INFO, "reading packet", packet_type = ?packet_type).entered();
event!(Level::INFO, "parsing packet");
Ok(match packet_type {
PacketType::Sigon => Packet::Sigon(MessagePacket::parse(stream, state)?),
PacketType::Message => Packet::Message(MessagePacket::parse(stream, state)?),

View file

@ -214,7 +214,7 @@ impl GameStateAnalyser {
}
pub fn handle_player_resource(&mut self, entity: &PacketEntity) {
for prop in &entity.props {
for prop in entity.props() {
if let Some((table_name, prop_name)) = self.prop_names.get(&prop.identifier) {
if let Ok(player_id) = u32::from_str(prop_name.as_str()) {
let entity_id = EntityId::from(player_id);
@ -248,7 +248,7 @@ impl GameStateAnalyser {
pub fn handle_player_entity(&mut self, entity: &PacketEntity) {
let player = self.state.get_or_create_player(entity.entity_index);
for prop in &entity.props {
for prop in entity.props() {
if let Some((table_name, prop_name)) = self.prop_names.get(&prop.identifier) {
match table_name.as_str() {
"DT_BasePlayer" => match prop_name.as_str() {