Skip to content

Commit

Permalink
Resize vectors as needed.
Browse files Browse the repository at this point in the history
Also ignore PacketEntities.max_entries. It is a lie.
  • Loading branch information
abenea committed Feb 8, 2024
1 parent 3669e15 commit 9aa1835
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
10 changes: 7 additions & 3 deletions cs2-demo/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ impl Entities {
) -> Result<()> {
let mut next_entity_id = 0;
let mut reader = BitReader::new(msg.entity_data());
let max_entries = msg.max_entries() as usize;
self.entities.resize_with(max_entries, || None);
for _ in 0..msg.updated_entries() {
let entity_id = next_entity_id + reader.read_ubitvar()?;
next_entity_id = entity_id + 1;
Expand All @@ -69,6 +67,9 @@ impl Entities {
entity.read_props(&mut BitReader::new(baseline), &mut self.field_paths)?;
};
entity.read_props(&mut reader, &mut self.field_paths)?;
if self.entities.len() <= entity_id as usize {
self.entities.resize_with(entity_id as usize + 1, || None);
}
self.entities[entity_id as usize] = Some(entity);
}
(true, _) => {
Expand Down Expand Up @@ -154,12 +155,15 @@ impl Entity {
(&mut a[i as usize], f.element.as_ref())
}
(Some(Property::Array(a)), Field::Vector(f)) => {
if a.len() <= i as usize {
a.resize(i as usize + 1, Default::default());
}
(&mut a[i as usize], f.element.as_ref())
}
(Some(p), f) => unreachable!("{p:?} {f:?}"),
(p, Field::Array(f)) if p.is_none() => {
*p = Some(Property::Array(
vec![None; f.size as usize].into_boxed_slice(),
vec![None; f.size as usize],
));
match p {
Some(Property::Array(a)) => (&mut a[i as usize], &f.element.as_ref()),
Expand Down
4 changes: 2 additions & 2 deletions cs2-demo/src/entity/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ impl DecoderCache {
}

pub(super) fn decode_fixed_array(&self, size: u16) -> Decoder {
Rc::new(move |_| Ok(Some(Property::Array(vec![None; size as usize].into_boxed_slice()))))
Rc::new(move |_| Ok(Some(Property::Array(vec![None; size as usize]))))
}

pub(super) fn decode_var_array(&self, init: Option<Property>) -> Decoder {
Rc::new(move |r| {
let vec = vec![init.clone(); r.read_varuint32()? as usize];
Ok(Some(Property::Array(vec.into_boxed_slice())))
Ok(Some(Property::Array(vec)))
})
}

Expand Down
2 changes: 1 addition & 1 deletion cs2-demo/src/entity/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum Property {
Vec4([f32; 4]),
Vec6([f32; 6]),
Object(Object),
Array(Box<[Option<Property>]>),
Array(Vec<Option<Property>>),
}

impl std::fmt::Display for Property {
Expand Down

0 comments on commit 9aa1835

Please sign in to comment.