Skip to content

Commit

Permalink
shove changes_often into the enum
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Feb 13, 2021
1 parent 0a34318 commit a3fc003
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/demo/packet/datatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl ParseSendTable {
// sort often changed props before the others
let mut start = 0;
for i in 0..flat.len() {
if flat[i].changes_often {
if flat[i].parse_definition.changes_often() {
if i != start {
flat.swap(i, start);
}
Expand Down
39 changes: 34 additions & 5 deletions src/demo/sendprop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::convert::{TryFrom, TryInto};
use fnv::FnvHasher;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::num::NonZeroU64;
use std::rc::Rc;

#[derive(
Expand Down Expand Up @@ -348,7 +347,6 @@ impl FloatDefinition {

#[derive(Debug, Clone)]
pub struct SendPropDefinition {
pub changes_often: bool,
pub identifier: SendPropIdentifier,
pub parse_definition: SendPropParseDefinition,
}
Expand All @@ -359,7 +357,6 @@ impl TryFrom<&RawSendPropDefinition> for SendPropDefinition {
fn try_from(definition: &RawSendPropDefinition) -> std::result::Result<Self, Self::Error> {
let parse_definition = definition.try_into()?;
Ok(SendPropDefinition {
changes_often: definition.flags.contains(SendPropFlag::ChangesOften),
parse_definition,
identifier: definition.identifier(),
})
Expand All @@ -369,60 +366,90 @@ impl TryFrom<&RawSendPropDefinition> for SendPropDefinition {
#[derive(Debug, Clone)]
pub enum SendPropParseDefinition {
NormalVarInt {
changes_often: bool,
unsigned: bool,
},
UnsignedInt {
changes_often: bool,
bit_count: u8,
},
Int {
changes_often: bool,
bit_count: u8,
},
Float {
changes_often: bool,
definition: FloatDefinition,
},
String,
String {
changes_often: bool,
},
Vector {
changes_often: bool,
definition: FloatDefinition,
},
VectorXY {
changes_often: bool,
definition: FloatDefinition,
},
Array {
changes_often: bool,
inner_definition: Box<SendPropParseDefinition>,
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 {
type Error = MalformedSendPropDefinitionError;

fn try_from(definition: &RawSendPropDefinition) -> std::result::Result<Self, Self::Error> {
let changes_often = definition.flags.contains(SendPropFlag::ChangesOften);
match definition.prop_type {
SendPropType::Int => {
if definition.flags.contains(SendPropFlag::NormalVarInt) {
Ok(SendPropParseDefinition::NormalVarInt {
changes_often,
unsigned: definition.flags.contains(SendPropFlag::Unsigned),
})
} else if definition.flags.contains(SendPropFlag::Unsigned) {
Ok(SendPropParseDefinition::UnsignedInt {
changes_often,
bit_count: definition.bit_count.unwrap_or(32) as u8,
})
} else {
Ok(SendPropParseDefinition::Int {
changes_often,
bit_count: definition.bit_count.unwrap_or(32) as u8,
})
}
}
SendPropType::Float => Ok(SendPropParseDefinition::Float {
changes_often,
definition: FloatDefinition::new(
definition.flags,
definition.bit_count,
definition.high_value,
definition.low_value,
)?,
}),
SendPropType::String => Ok(SendPropParseDefinition::String),
SendPropType::String => Ok(SendPropParseDefinition::String { changes_often }),
SendPropType::Vector => Ok(SendPropParseDefinition::Vector {
changes_often,
definition: FloatDefinition::new(
definition.flags,
definition.bit_count,
Expand All @@ -431,6 +458,7 @@ impl TryFrom<&RawSendPropDefinition> for SendPropParseDefinition {
)?,
}),
SendPropType::VectorXY => Ok(SendPropParseDefinition::VectorXY {
changes_often,
definition: FloatDefinition::new(
definition.flags,
definition.bit_count,
Expand All @@ -450,6 +478,7 @@ impl TryFrom<&RawSendPropDefinition> for SendPropParseDefinition {
.as_deref()
.ok_or(MalformedSendPropDefinitionError::UntypedArray)?;
Ok(SendPropParseDefinition::Array {
changes_often,
inner_definition: Box::new(SendPropParseDefinition::try_from(
child_definition,
)?),
Expand Down

0 comments on commit a3fc003

Please sign in to comment.