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

Fix SendPropValue::Vector encoding when the float definition is NormalFloatVar

This is a follow up to PR #4. Handling for the Vector prop needs to be
fixed on the write/encoding side as well. When the float definition is
NormalFloatVar we need to skip writing the last component (z) to the
stream, since it's calculated from the first two components (x, y), and
just write out its sign.
This commit is contained in:
glyphpoch 2026-04-06 16:34:37 +01:00
commit 660b9dc1d7

View file

@ -775,7 +775,10 @@ impl SendPropValue {
let val: Vector = self.try_into()?;
Self::write_float(val.x, stream, float_definition)?;
Self::write_float(val.y, stream, float_definition)?;
Self::write_float(val.z, stream, float_definition)?;
match float_definition {
FloatDefinition::NormalVarFloat => stream.write_bool(val.z.is_negative())?,
_ => Self::write_float(val.z, stream, float_definition)?,
}
Ok(())
}
SendPropParseDefinition::VectorXY {
@ -929,6 +932,17 @@ fn test_send_prop_value_roundtrip() {
definition: FloatDefinition::Coord,
},
);
send_prop_value_roundtrip(
SendPropValue::Vector(Vector {
x: 0.0,
y: 0.0,
z: -1.0,
}),
SendPropParseDefinition::Vector {
changes_often: false,
definition: FloatDefinition::NormalVarFloat,
},
);
send_prop_value_roundtrip(
SendPropValue::VectorXY(VectorXY { x: 1.0, y: 0.0 }),
SendPropParseDefinition::VectorXY {
@ -1015,6 +1029,32 @@ fn test_send_prop_value_roundtrip() {
);
}
#[test]
#[cfg(feature = "write")]
fn test_encode_vector_normal_var_float() {
use bitbuffer::BitWriteStream;
let vector = SendPropValue::Vector(Vector {
x: 0.0f32,
y: 0.0f32,
z: -1.0f32,
});
let def = SendPropParseDefinition::Vector {
changes_often: false,
definition: FloatDefinition::NormalVarFloat,
};
let mut data = Vec::new();
let pos = {
let mut write = BitWriteStream::new(&mut data, LittleEndian);
vector.encode(&mut write, &def).unwrap();
write.bit_len()
};
assert_eq!(pos, 25);
assert_eq!(data, vec![0, 0, 0, 1]);
}
impl From<i32> for SendPropValue {
fn from(value: i32) -> Self {
SendPropValue::Integer(value as i64)