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

minor cleanup

This commit is contained in:
Robin Appelman 2019-08-11 13:25:47 +02:00
commit 8442c5e385
4 changed files with 34 additions and 28 deletions

View file

@ -186,7 +186,9 @@ fn parse_string_table_list(
for _ in 0..entry_count {
if !stream.read::<bool>()? {
return Err(ParseError::InvalidDemo("there should be no holes when reading CreateStringTable message".to_string()));
return Err(ParseError::InvalidDemo(
"there should be no holes when reading CreateStringTable message".to_string(),
));
};
let entry = read_table_entry(stream, table_meta, &history)?;

View file

@ -91,7 +91,7 @@ impl Parse for ParseSendTable {
impl ParseSendTable {
pub fn flatten_props(&self, tables: &[ParseSendTable]) -> Vec<SendPropDefinition> {
let mut flat = Vec::new();
let mut flat = Vec::with_capacity(32);
self.get_all_props(tables, &self.get_excludes(tables), &mut flat);
// sort often changed props before the others
@ -109,20 +109,18 @@ impl ParseSendTable {
}
fn get_excludes<'a>(&'a self, tables: &'a [ParseSendTable]) -> Vec<Exclude<'a>> {
let mut excludes = Vec::new();
let mut excludes = Vec::with_capacity(8);
for prop in self.props.iter() {
if prop.is_exclude() && prop.table_name.is_some() {
if let Some(exclude_table) = prop.get_exclude_table() {
excludes.push(Exclude {
table: prop.table_name.as_ref().unwrap(),
table: exclude_table,
prop: &prop.name,
})
} else if prop.prop_type == SendPropType::DataTable {
if let Some(table) = prop.get_data_table(tables) {
} else if let Some(table) = prop.get_data_table(tables) {
excludes.extend_from_slice(&table.get_excludes(tables));
}
}
}
excludes
}
@ -147,27 +145,21 @@ impl ParseSendTable {
local_props: &mut Vec<&'a SendPropDefinition>,
props: &mut Vec<&'a SendPropDefinition>,
) {
for prop in self.props.iter() {
if prop.is_exclude() {
continue;
}
if excludes.iter().any(|exclude| exclude.matches(prop)) {
continue;
}
if prop.prop_type == SendPropType::DataTable {
self.props
.iter()
.filter(|prop| !prop.is_exclude())
.filter(|prop| !excludes.iter().any(|exclude| exclude.matches(prop)))
.for_each(|prop| {
if let Some(table) = prop.get_data_table(tables) {
if prop.flags.contains(SendPropFlag::Collapsible) {
table.get_all_props_iterator_props(tables, excludes, local_props, props);
} else {
table.get_all_props(tables, excludes, props);
}
}
} else {
local_props.push(prop);
}
}
})
}
}
@ -179,7 +171,7 @@ struct Exclude<'a> {
impl<'a> Exclude<'a> {
pub fn matches(&self, prop: &SendPropDefinition) -> bool {
self.prop == prop.name && self.table == &prop.owner_table
self.table == &prop.owner_table && self.prop == prop.name
}
}

View file

@ -52,7 +52,7 @@ pub enum ParseError {
size: u32,
},
/// Misc malformed demo error
InvalidDemo(String)
InvalidDemo(String),
}
impl From<ReadError> for ParseError {

View file

@ -44,9 +44,13 @@ impl SendPropDefinition {
///
/// Note that this is not the owner table
pub fn get_data_table<'a>(&self, tables: &'a [ParseSendTable]) -> Option<&'a ParseSendTable> {
if self.prop_type == SendPropType::DataTable {
self.table_name
.as_ref()
.and_then(|name| tables.iter().find(|table| table.name == *name))
} else {
None
}
}
pub fn read(stream: &mut Stream, owner_table: SendTableName) -> ReadResult<Self> {
@ -99,6 +103,14 @@ impl SendPropDefinition {
pub fn is_exclude(&self) -> bool {
self.flags.contains(SendPropFlag::Exclude)
}
pub fn get_exclude_table(&self) -> Option<&SendTableName> {
if self.is_exclude() {
self.table_name.as_ref()
} else {
None
}
}
}
#[derive(BitRead, Copy, Clone, PartialEq, Debug)]