mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 18:24:05 +02:00
better packetentities
This commit is contained in:
parent
3c128e54c8
commit
f0bb2af3ca
4 changed files with 25 additions and 19 deletions
|
|
@ -72,16 +72,16 @@ impl fmt::Display for PacketEntity {
|
||||||
|
|
||||||
impl PacketEntity {
|
impl PacketEntity {
|
||||||
pub fn mut_prop_by_identifier(&mut self, index: &SendPropIdentifier) -> Option<&mut SendProp> {
|
pub fn mut_prop_by_identifier(&mut self, index: &SendPropIdentifier) -> Option<&mut SendProp> {
|
||||||
self.props.iter_mut().find(|prop| prop.index == *index)
|
self.props.iter_mut().find(|prop| prop.identifier == *index)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_prop_by_identifier(&self, index: &SendPropIdentifier) -> Option<&SendProp> {
|
pub fn get_prop_by_identifier(&self, index: &SendPropIdentifier) -> Option<&SendProp> {
|
||||||
self.props.iter().find(|prop| prop.index == *index)
|
self.props.iter().find(|prop| prop.identifier == *index)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_update(&mut self, props: Vec<SendProp>) {
|
pub fn apply_update(&mut self, props: Vec<SendProp>) {
|
||||||
for prop in props {
|
for prop in props {
|
||||||
match self.mut_prop_by_identifier(&prop.index) {
|
match self.mut_prop_by_identifier(&prop.identifier) {
|
||||||
Some(existing_prop) => existing_prop.value = prop.value,
|
Some(existing_prop) => existing_prop.value = prop.value,
|
||||||
None => self.props.push(prop),
|
None => self.props.push(prop),
|
||||||
}
|
}
|
||||||
|
|
@ -105,7 +105,7 @@ impl PacketEntity {
|
||||||
self.props.iter().filter(move |prop| {
|
self.props.iter().filter(move |prop| {
|
||||||
baseline
|
baseline
|
||||||
.iter()
|
.iter()
|
||||||
.find(|base_prop| base_prop.index == prop.index)
|
.find(|base_prop| base_prop.identifier == prop.identifier)
|
||||||
.map(|base_prop| base_prop.value != prop.value)
|
.map(|base_prop| base_prop.value != prop.value)
|
||||||
.unwrap_or(true)
|
.unwrap_or(true)
|
||||||
})
|
})
|
||||||
|
|
@ -397,7 +397,8 @@ impl PacketEntitiesMessage {
|
||||||
Some(definition) => {
|
Some(definition) => {
|
||||||
let value = SendPropValue::parse(stream, &definition.parse_definition)?;
|
let value = SendPropValue::parse(stream, &definition.parse_definition)?;
|
||||||
props.push(SendProp {
|
props.push(SendProp {
|
||||||
index: definition.identifier,
|
index: index as u32,
|
||||||
|
identifier: definition.identifier,
|
||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -422,12 +423,12 @@ impl PacketEntitiesMessage {
|
||||||
|
|
||||||
for prop in props {
|
for prop in props {
|
||||||
true.write(stream)?;
|
true.write(stream)?;
|
||||||
let (index, definition) = send_table
|
|
||||||
|
let index = prop.index as usize;
|
||||||
|
let definition = send_table
|
||||||
.flattened_props
|
.flattened_props
|
||||||
.iter()
|
.get(index)
|
||||||
.enumerate()
|
.ok_or(ParseError::UnknownDefinition(prop.identifier))?;
|
||||||
.find(|(_, def)| def.identifier == prop.index)
|
|
||||||
.ok_or(ParseError::UnknownDefinition(prop.index))?;
|
|
||||||
write_bit_var((index as i32 - last_index - 1) as u32, stream)?;
|
write_bit_var((index as i32 - last_index - 1) as u32, stream)?;
|
||||||
last_index = index as i32;
|
last_index = index as i32;
|
||||||
prop.value.encode(stream, &definition.parse_definition)?;
|
prop.value.encode(stream, &definition.parse_definition)?;
|
||||||
|
|
@ -554,11 +555,13 @@ fn test_packet_entitier_message_roundtrip() {
|
||||||
entity_index: EntityId::from(4),
|
entity_index: EntityId::from(4),
|
||||||
props: vec![
|
props: vec![
|
||||||
SendProp {
|
SendProp {
|
||||||
index: SendPropIdentifier::new("table2", "prop1"),
|
index: 0,
|
||||||
|
identifier: SendPropIdentifier::new("table2", "prop1"),
|
||||||
value: SendPropValue::Integer(4),
|
value: SendPropValue::Integer(4),
|
||||||
},
|
},
|
||||||
SendProp {
|
SendProp {
|
||||||
index: SendPropIdentifier::new("table2", "prop3"),
|
index: 3,
|
||||||
|
identifier: SendPropIdentifier::new("table2", "prop3"),
|
||||||
value: SendPropValue::Float(1.0),
|
value: SendPropValue::Float(1.0),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
@ -572,11 +575,13 @@ fn test_packet_entitier_message_roundtrip() {
|
||||||
entity_index: EntityId::from(5),
|
entity_index: EntityId::from(5),
|
||||||
props: vec![
|
props: vec![
|
||||||
SendProp {
|
SendProp {
|
||||||
index: SendPropIdentifier::new("table2", "prop1"),
|
index: 0,
|
||||||
|
identifier: SendPropIdentifier::new("table2", "prop1"),
|
||||||
value: SendPropValue::Integer(4),
|
value: SendPropValue::Integer(4),
|
||||||
},
|
},
|
||||||
SendProp {
|
SendProp {
|
||||||
index: SendPropIdentifier::new("table2", "prop3"),
|
index: 2,
|
||||||
|
identifier: SendPropIdentifier::new("table2", "prop3"),
|
||||||
value: SendPropValue::Float(1.0),
|
value: SendPropValue::Float(1.0),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ use crate::{ReadResult, Stream};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct ConsoleCmdPacket {
|
pub struct ConsoleCmdPacket {
|
||||||
tick: u32,
|
pub tick: u32,
|
||||||
command: String,
|
pub command: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BitRead<'_, LittleEndian> for ConsoleCmdPacket {
|
impl BitRead<'_, LittleEndian> for ConsoleCmdPacket {
|
||||||
|
|
|
||||||
|
|
@ -215,7 +215,7 @@ impl GameStateAnalyser {
|
||||||
|
|
||||||
pub fn handle_player_resource(&mut self, entity: &PacketEntity) {
|
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.index) {
|
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()) {
|
if let Ok(player_id) = u32::from_str(prop_name.as_str()) {
|
||||||
let entity_id = EntityId::from(player_id);
|
let entity_id = EntityId::from(player_id);
|
||||||
if let Some(player) = self
|
if let Some(player) = self
|
||||||
|
|
@ -249,7 +249,7 @@ impl GameStateAnalyser {
|
||||||
let player = self.state.get_or_create_player(entity.entity_index);
|
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.index) {
|
if let Some((table_name, prop_name)) = self.prop_names.get(&prop.identifier) {
|
||||||
match table_name.as_str() {
|
match table_name.as_str() {
|
||||||
"DT_BasePlayer" => match prop_name.as_str() {
|
"DT_BasePlayer" => match prop_name.as_str() {
|
||||||
"m_iHealth" => {
|
"m_iHealth" => {
|
||||||
|
|
|
||||||
|
|
@ -1087,7 +1087,8 @@ impl From<u64> for SendPropIdentifier {
|
||||||
#[derive(Debug, Clone, Display, PartialEq)]
|
#[derive(Debug, Clone, Display, PartialEq)]
|
||||||
#[display("{index} = {value}")]
|
#[display("{index} = {value}")]
|
||||||
pub struct SendProp {
|
pub struct SendProp {
|
||||||
pub index: SendPropIdentifier,
|
pub index: u32,
|
||||||
|
pub identifier: SendPropIdentifier,
|
||||||
pub value: SendPropValue,
|
pub value: SendPropValue,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue