From 1cdaed5f89d02b8d14c6625d920a5af28f6c1976 Mon Sep 17 00:00:00 2001 From: koe Date: Mon, 19 Feb 2024 19:02:23 -0600 Subject: [PATCH] update entity serialization for bevy v0.13 --- src/client.rs | 7 ++++--- src/server/replication_messages.rs | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/client.rs b/src/client.rs index 484e8a87..12ef30f5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -453,14 +453,15 @@ fn apply_update_components( /// Deserializes `entity` from compressed index and generation. /// -/// For details see [`ReplicationBuffer::write_entity`](crate::server::replication_message::replication_buffer::write_entity). +/// For details see +/// [`ReplicationBuffer::write_entity`](crate::server::replication_message::replication_buffer::write_entity). fn deserialize_entity(cursor: &mut Cursor<&[u8]>) -> bincode::Result { let flagged_index: u64 = cursor.read_u64_varint()?; let has_generation = (flagged_index & 1) > 0; let generation = if has_generation { - cursor.read_u32_varint()? + cursor.read_u32_varint()? + 1 } else { - 0u32 + 1u32 }; let bits = (generation as u64) << 32 | (flagged_index >> 1); diff --git a/src/server/replication_messages.rs b/src/server/replication_messages.rs index 3d3e4cdf..e05cb79a 100644 --- a/src/server/replication_messages.rs +++ b/src/server/replication_messages.rs @@ -674,15 +674,17 @@ fn can_pack(header_size: usize, base: usize, add: usize) -> bool { /// Serializes `entity` by writing its index and generation as separate varints. /// /// The index is first prepended with a bit flag to indicate if the generation -/// is serialized or not (it is not serialized if equal to zero). +/// is serialized or not. It is not serialized if <= 1; note that generations are `NonZeroU32` +/// and a value of zero is used in `Option` to signify `None`, so generation 1 is the first +/// generation. fn serialize_entity(cursor: &mut Cursor>, entity: Entity) -> bincode::Result<()> { let mut flagged_index = (entity.index() as u64) << 1; - let flag = entity.generation() > 0; + let flag = entity.generation() > 1; flagged_index |= flag as u64; cursor.write_u64_varint(flagged_index)?; if flag { - cursor.write_u32_varint(entity.generation())?; + cursor.write_u32_varint(entity.generation() - 1)?; } Ok(())