Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update entity serialization for bevy v0.13 #200

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity> {
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);
Expand Down
8 changes: 5 additions & 3 deletions src/server/replication_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity>` to signify `None`, so generation 1 is the first
/// generation.
fn serialize_entity(cursor: &mut Cursor<Vec<u8>>, 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(())
Expand Down
Loading