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:
parent
4b7a0c7320
commit
660b9dc1d7
1 changed files with 41 additions and 1 deletions
|
|
@ -775,7 +775,10 @@ impl SendPropValue {
|
||||||
let val: Vector = self.try_into()?;
|
let val: Vector = self.try_into()?;
|
||||||
Self::write_float(val.x, stream, float_definition)?;
|
Self::write_float(val.x, stream, float_definition)?;
|
||||||
Self::write_float(val.y, 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
SendPropParseDefinition::VectorXY {
|
SendPropParseDefinition::VectorXY {
|
||||||
|
|
@ -929,6 +932,17 @@ fn test_send_prop_value_roundtrip() {
|
||||||
definition: FloatDefinition::Coord,
|
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(
|
send_prop_value_roundtrip(
|
||||||
SendPropValue::VectorXY(VectorXY { x: 1.0, y: 0.0 }),
|
SendPropValue::VectorXY(VectorXY { x: 1.0, y: 0.0 }),
|
||||||
SendPropParseDefinition::VectorXY {
|
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 {
|
impl From<i32> for SendPropValue {
|
||||||
fn from(value: i32) -> Self {
|
fn from(value: i32) -> Self {
|
||||||
SendPropValue::Integer(value as i64)
|
SendPropValue::Integer(value as i64)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue