From 660b9dc1d74093826ae356500a00731e9b1939bf Mon Sep 17 00:00:00 2001 From: glyphpoch <26898765+glyphpoch@users.noreply.github.com> Date: Mon, 6 Apr 2026 16:34:37 +0100 Subject: [PATCH] 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. --- src/demo/sendprop.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/demo/sendprop.rs b/src/demo/sendprop.rs index b7d4850..9698b63 100644 --- a/src/demo/sendprop.rs +++ b/src/demo/sendprop.rs @@ -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 for SendPropValue { fn from(value: i32) -> Self { SendPropValue::Integer(value as i64)