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

shove changes_often into the enum

This commit is contained in:
Robin Appelman 2021-02-13 16:49:04 +01:00
commit a3fc003437
2 changed files with 35 additions and 6 deletions

View file

@ -129,7 +129,7 @@ impl ParseSendTable {
// sort often changed props before the others // sort often changed props before the others
let mut start = 0; let mut start = 0;
for i in 0..flat.len() { for i in 0..flat.len() {
if flat[i].changes_often { if flat[i].parse_definition.changes_often() {
if i != start { if i != start {
flat.swap(i, start); flat.swap(i, start);
} }

View file

@ -16,7 +16,6 @@ use std::convert::{TryFrom, TryInto};
use fnv::FnvHasher; use fnv::FnvHasher;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::num::NonZeroU64;
use std::rc::Rc; use std::rc::Rc;
#[derive( #[derive(
@ -348,7 +347,6 @@ impl FloatDefinition {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SendPropDefinition { pub struct SendPropDefinition {
pub changes_often: bool,
pub identifier: SendPropIdentifier, pub identifier: SendPropIdentifier,
pub parse_definition: SendPropParseDefinition, pub parse_definition: SendPropParseDefinition,
} }
@ -359,7 +357,6 @@ impl TryFrom<&RawSendPropDefinition> for SendPropDefinition {
fn try_from(definition: &RawSendPropDefinition) -> std::result::Result<Self, Self::Error> { fn try_from(definition: &RawSendPropDefinition) -> std::result::Result<Self, Self::Error> {
let parse_definition = definition.try_into()?; let parse_definition = definition.try_into()?;
Ok(SendPropDefinition { Ok(SendPropDefinition {
changes_often: definition.flags.contains(SendPropFlag::ChangesOften),
parse_definition, parse_definition,
identifier: definition.identifier(), identifier: definition.identifier(),
}) })
@ -369,51 +366,80 @@ impl TryFrom<&RawSendPropDefinition> for SendPropDefinition {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SendPropParseDefinition { pub enum SendPropParseDefinition {
NormalVarInt { NormalVarInt {
changes_often: bool,
unsigned: bool, unsigned: bool,
}, },
UnsignedInt { UnsignedInt {
changes_often: bool,
bit_count: u8, bit_count: u8,
}, },
Int { Int {
changes_often: bool,
bit_count: u8, bit_count: u8,
}, },
Float { Float {
changes_often: bool,
definition: FloatDefinition, definition: FloatDefinition,
}, },
String, String {
changes_often: bool,
},
Vector { Vector {
changes_often: bool,
definition: FloatDefinition, definition: FloatDefinition,
}, },
VectorXY { VectorXY {
changes_often: bool,
definition: FloatDefinition, definition: FloatDefinition,
}, },
Array { Array {
changes_often: bool,
inner_definition: Box<SendPropParseDefinition>, inner_definition: Box<SendPropParseDefinition>,
count_bit_count: u16, count_bit_count: u16,
}, },
} }
impl SendPropParseDefinition {
pub fn changes_often(&self) -> bool {
match self {
SendPropParseDefinition::NormalVarInt { changes_often, .. } => *changes_often,
SendPropParseDefinition::UnsignedInt { changes_often, .. } => *changes_often,
SendPropParseDefinition::Int { changes_often, .. } => *changes_often,
SendPropParseDefinition::Float { changes_often, .. } => *changes_often,
SendPropParseDefinition::String { changes_often, .. } => *changes_often,
SendPropParseDefinition::Vector { changes_often, .. } => *changes_often,
SendPropParseDefinition::VectorXY { changes_often, .. } => *changes_often,
SendPropParseDefinition::Array { changes_often, .. } => *changes_often,
}
}
}
impl TryFrom<&RawSendPropDefinition> for SendPropParseDefinition { impl TryFrom<&RawSendPropDefinition> for SendPropParseDefinition {
type Error = MalformedSendPropDefinitionError; type Error = MalformedSendPropDefinitionError;
fn try_from(definition: &RawSendPropDefinition) -> std::result::Result<Self, Self::Error> { fn try_from(definition: &RawSendPropDefinition) -> std::result::Result<Self, Self::Error> {
let changes_often = definition.flags.contains(SendPropFlag::ChangesOften);
match definition.prop_type { match definition.prop_type {
SendPropType::Int => { SendPropType::Int => {
if definition.flags.contains(SendPropFlag::NormalVarInt) { if definition.flags.contains(SendPropFlag::NormalVarInt) {
Ok(SendPropParseDefinition::NormalVarInt { Ok(SendPropParseDefinition::NormalVarInt {
changes_often,
unsigned: definition.flags.contains(SendPropFlag::Unsigned), unsigned: definition.flags.contains(SendPropFlag::Unsigned),
}) })
} else if definition.flags.contains(SendPropFlag::Unsigned) { } else if definition.flags.contains(SendPropFlag::Unsigned) {
Ok(SendPropParseDefinition::UnsignedInt { Ok(SendPropParseDefinition::UnsignedInt {
changes_often,
bit_count: definition.bit_count.unwrap_or(32) as u8, bit_count: definition.bit_count.unwrap_or(32) as u8,
}) })
} else { } else {
Ok(SendPropParseDefinition::Int { Ok(SendPropParseDefinition::Int {
changes_often,
bit_count: definition.bit_count.unwrap_or(32) as u8, bit_count: definition.bit_count.unwrap_or(32) as u8,
}) })
} }
} }
SendPropType::Float => Ok(SendPropParseDefinition::Float { SendPropType::Float => Ok(SendPropParseDefinition::Float {
changes_often,
definition: FloatDefinition::new( definition: FloatDefinition::new(
definition.flags, definition.flags,
definition.bit_count, definition.bit_count,
@ -421,8 +447,9 @@ impl TryFrom<&RawSendPropDefinition> for SendPropParseDefinition {
definition.low_value, definition.low_value,
)?, )?,
}), }),
SendPropType::String => Ok(SendPropParseDefinition::String), SendPropType::String => Ok(SendPropParseDefinition::String { changes_often }),
SendPropType::Vector => Ok(SendPropParseDefinition::Vector { SendPropType::Vector => Ok(SendPropParseDefinition::Vector {
changes_often,
definition: FloatDefinition::new( definition: FloatDefinition::new(
definition.flags, definition.flags,
definition.bit_count, definition.bit_count,
@ -431,6 +458,7 @@ impl TryFrom<&RawSendPropDefinition> for SendPropParseDefinition {
)?, )?,
}), }),
SendPropType::VectorXY => Ok(SendPropParseDefinition::VectorXY { SendPropType::VectorXY => Ok(SendPropParseDefinition::VectorXY {
changes_often,
definition: FloatDefinition::new( definition: FloatDefinition::new(
definition.flags, definition.flags,
definition.bit_count, definition.bit_count,
@ -450,6 +478,7 @@ impl TryFrom<&RawSendPropDefinition> for SendPropParseDefinition {
.as_deref() .as_deref()
.ok_or(MalformedSendPropDefinitionError::UntypedArray)?; .ok_or(MalformedSendPropDefinitionError::UntypedArray)?;
Ok(SendPropParseDefinition::Array { Ok(SendPropParseDefinition::Array {
changes_often,
inner_definition: Box::new(SendPropParseDefinition::try_from( inner_definition: Box::new(SendPropParseDefinition::try_from(
child_definition, child_definition,
)?), )?),